PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/mini-rsa-psk.c

https://gitlab.com/axil/gnutls
C | 346 lines | 231 code | 65 blank | 50 comment | 28 complexity | 95c05c4c7cc717e0d7fba60dc25fc08c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. * Copyright (C) 2013 Adam Sampson <ats@offog.org>
  4. * Copyright (C) 2013 Nikos Mavrogiannopoulos
  5. *
  6. * Author: Nikos Mavrogiannopoulos
  7. *
  8. * This file is part of GnuTLS.
  9. *
  10. * GnuTLS is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GnuTLS is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GnuTLS; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /* Parts copied from GnuTLS example programs. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #if defined(_WIN32)
  31. /* socketpair isn't supported on Win32. */
  32. int main(int argc, char **argv)
  33. {
  34. exit(77);
  35. }
  36. #else
  37. #include <string.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #if !defined(_WIN32)
  41. #include <sys/wait.h>
  42. #endif
  43. #include <unistd.h>
  44. #include <gnutls/gnutls.h>
  45. #include "utils.h"
  46. /* A very basic TLS client, with PSK authentication.
  47. */
  48. const char *side = "";
  49. static void tls_log_func(int level, const char *str)
  50. {
  51. fprintf(stderr, "%s|<%d>| %s", side, level, str);
  52. }
  53. #define MAX_BUF 1024
  54. #define MSG "Hello TLS"
  55. static void client(int sd)
  56. {
  57. int ret, ii;
  58. gnutls_session_t session;
  59. gnutls_certificate_credentials_t clientx509cred;
  60. char buffer[MAX_BUF + 1];
  61. gnutls_psk_client_credentials_t pskcred;
  62. /* Need to enable anonymous KX specifically. */
  63. const gnutls_datum_t key = { (void *) "DEADBEEF", 8 };
  64. global_init();
  65. gnutls_global_set_log_function(tls_log_func);
  66. if (debug)
  67. gnutls_global_set_log_level(4711);
  68. side = "client";
  69. gnutls_certificate_allocate_credentials(&clientx509cred);
  70. gnutls_psk_allocate_client_credentials(&pskcred);
  71. gnutls_psk_set_client_credentials(pskcred, "test", &key,
  72. GNUTLS_PSK_KEY_HEX);
  73. /* Initialize TLS session
  74. */
  75. gnutls_init(&session, GNUTLS_CLIENT);
  76. /* Use default priorities */
  77. gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+RSA-PSK",
  78. NULL);
  79. /* put the anonymous credentials to the current session
  80. */
  81. gnutls_credentials_set(session, GNUTLS_CRD_PSK, pskcred);
  82. gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
  83. clientx509cred);
  84. gnutls_transport_set_int(session, sd);
  85. /* Perform the TLS handshake
  86. */
  87. ret = gnutls_handshake(session);
  88. if (ret < 0) {
  89. fail("client: Handshake failed\n");
  90. gnutls_perror(ret);
  91. goto end;
  92. } else {
  93. if (debug)
  94. success("client: Handshake was completed\n");
  95. }
  96. gnutls_record_send(session, MSG, strlen(MSG));
  97. ret = gnutls_record_recv(session, buffer, MAX_BUF);
  98. if (ret == 0) {
  99. if (debug)
  100. success
  101. ("client: Peer has closed the TLS connection\n");
  102. goto end;
  103. } else if (ret < 0) {
  104. fail("client: Error: %s\n", gnutls_strerror(ret));
  105. goto end;
  106. }
  107. if (debug) {
  108. printf("- Received %d bytes: ", ret);
  109. for (ii = 0; ii < ret; ii++) {
  110. fputc(buffer[ii], stdout);
  111. }
  112. fputs("\n", stdout);
  113. }
  114. gnutls_bye(session, GNUTLS_SHUT_RDWR);
  115. end:
  116. close(sd);
  117. gnutls_deinit(session);
  118. gnutls_psk_free_client_credentials(pskcred);
  119. gnutls_certificate_free_credentials(clientx509cred);
  120. gnutls_global_deinit();
  121. }
  122. /* This is a sample TLS 1.0 echo server, for PSK authentication.
  123. */
  124. #define MAX_BUF 1024
  125. static unsigned char server_cert_pem[] =
  126. "-----BEGIN CERTIFICATE-----\n"
  127. "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
  128. "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
  129. "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
  130. "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
  131. "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
  132. "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
  133. "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
  134. "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
  135. "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
  136. "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
  137. "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
  138. "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
  139. "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
  140. const gnutls_datum_t server_cert = { server_cert_pem,
  141. sizeof(server_cert_pem)
  142. };
  143. static unsigned char server_key_pem[] =
  144. "-----BEGIN RSA PRIVATE KEY-----\n"
  145. "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
  146. "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
  147. "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
  148. "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
  149. "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
  150. "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
  151. "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
  152. "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
  153. "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
  154. "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
  155. "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
  156. "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
  157. "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
  158. "-----END RSA PRIVATE KEY-----\n";
  159. const gnutls_datum_t server_key = { server_key_pem,
  160. sizeof(server_key_pem)
  161. };
  162. /* These are global */
  163. gnutls_psk_server_credentials_t server_pskcred;
  164. static gnutls_session_t initialize_tls_session(void)
  165. {
  166. gnutls_session_t session;
  167. gnutls_init(&session, GNUTLS_SERVER);
  168. /* avoid calling all the priority functions, since the defaults
  169. * are adequate.
  170. */
  171. gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+RSA-PSK",
  172. NULL);
  173. gnutls_credentials_set(session, GNUTLS_CRD_PSK, server_pskcred);
  174. return session;
  175. }
  176. static int
  177. pskfunc(gnutls_session_t session, const char *username,
  178. gnutls_datum_t * key)
  179. {
  180. if (debug)
  181. printf("psk: username %s\n", username);
  182. key->data = gnutls_malloc(4);
  183. key->data[0] = 0xDE;
  184. key->data[1] = 0xAD;
  185. key->data[2] = 0xBE;
  186. key->data[3] = 0xEF;
  187. key->size = 4;
  188. return 0;
  189. }
  190. int err, ret;
  191. char topbuf[512];
  192. gnutls_session_t session;
  193. char buffer[MAX_BUF + 1];
  194. int optval = 1;
  195. static void server(int sd)
  196. {
  197. gnutls_certificate_credentials_t serverx509cred;
  198. /* this must be called once in the program
  199. */
  200. global_init();
  201. gnutls_global_set_log_function(tls_log_func);
  202. if (debug)
  203. gnutls_global_set_log_level(4711);
  204. side = "server";
  205. gnutls_psk_allocate_server_credentials(&server_pskcred);
  206. gnutls_psk_set_server_credentials_function(server_pskcred,
  207. pskfunc);
  208. gnutls_certificate_allocate_credentials(&serverx509cred);
  209. gnutls_certificate_set_x509_key_mem(serverx509cred,
  210. &server_cert, &server_key,
  211. GNUTLS_X509_FMT_PEM);
  212. session = initialize_tls_session();
  213. gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
  214. serverx509cred);
  215. gnutls_transport_set_int(session, sd);
  216. ret = gnutls_handshake(session);
  217. if (ret < 0) {
  218. close(sd);
  219. gnutls_deinit(session);
  220. fail("server: Handshake has failed (%s)\n\n",
  221. gnutls_strerror(ret));
  222. return;
  223. }
  224. if (debug)
  225. success("server: Handshake was completed\n");
  226. /* see the Getting peer's information example */
  227. /* print_info(session); */
  228. for (;;) {
  229. memset(buffer, 0, MAX_BUF + 1);
  230. ret = gnutls_record_recv(session, buffer, MAX_BUF);
  231. if (ret == 0) {
  232. if (debug)
  233. success
  234. ("server: Peer has closed the GnuTLS connection\n");
  235. break;
  236. } else if (ret < 0) {
  237. fail("server: Received corrupted data(%d). Closing...\n", ret);
  238. break;
  239. } else if (ret > 0) {
  240. /* echo data back to the client
  241. */
  242. gnutls_record_send(session, buffer,
  243. strlen(buffer));
  244. }
  245. }
  246. /* do not wait for the peer to close the connection.
  247. */
  248. gnutls_bye(session, GNUTLS_SHUT_WR);
  249. close(sd);
  250. gnutls_deinit(session);
  251. gnutls_psk_free_server_credentials(server_pskcred);
  252. gnutls_certificate_free_credentials(serverx509cred);
  253. gnutls_global_deinit();
  254. if (debug)
  255. success("server: finished\n");
  256. }
  257. void doit(void)
  258. {
  259. pid_t child;
  260. int sockets[2];
  261. err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
  262. if (err == -1) {
  263. perror("socketpair");
  264. fail("socketpair failed\n");
  265. return;
  266. }
  267. child = fork();
  268. if (child < 0) {
  269. perror("fork");
  270. fail("fork");
  271. return;
  272. }
  273. if (child) {
  274. int status;
  275. /* parent */
  276. server(sockets[0]);
  277. wait(&status);
  278. } else
  279. client(sockets[1]);
  280. }
  281. #endif /* _WIN32 */