2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
44 #include <arpa/inet.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
52 static void terminate(void);
54 /* This program tests the robustness of record
59 server_log_func (int level
, const char *str
)
61 // fprintf (stderr, "server|<%d>| %s", level, str);
65 client_log_func (int level
, const char *str
)
67 fprintf (stderr
, "client|<%d>| %s", level
, str
);
70 static unsigned char server_cert_pem
[] =
71 "-----BEGIN CERTIFICATE-----\n"
72 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
73 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
74 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
75 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
76 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
77 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
78 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
79 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
80 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
81 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
82 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
83 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
84 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
86 const gnutls_datum_t server_cert
= { server_cert_pem
,
87 sizeof (server_cert_pem
)
90 static unsigned char server_key_pem
[] =
91 "-----BEGIN RSA PRIVATE KEY-----\n"
92 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
93 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
94 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
95 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
96 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
97 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
98 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
99 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
100 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
101 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
102 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
103 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
104 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
105 "-----END RSA PRIVATE KEY-----\n";
107 const gnutls_datum_t server_key
= { server_key_pem
,
108 sizeof (server_key_pem
)
112 /* A very basic TLS client, with anonymous authentication.
117 static int to_send
= -1;
120 push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
122 int fd
= (long int)tr
;
124 return send(fd
, data
, len
, 0);
127 #define RECORD_HEADER_SIZE (5+8)
130 push_crippled (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
132 int fd
= (long int)tr
;
134 uint8_t* _data
= (void*)data
;
137 return send(fd
, data
, len
, 0);
140 // _len = ((uint8_t*)data)[11] << 8 | ((uint8_t*)data)[12];
141 //fprintf(stderr, "len : %d\n", (int)_len);
144 _data
[11] = _len
>> 8;
148 ret
= send(fd
, data
, RECORD_HEADER_SIZE
+_len
, 0);
150 if (ret
< 0) return ret
;
157 client (int fd
, const char* prio
)
160 char buffer
[MAX_BUF
+ 1];
161 gnutls_anon_client_credentials_t anoncred
;
162 gnutls_certificate_credentials_t x509_cred
;
163 gnutls_session_t session
;
164 /* Need to enable anonymous KX specifically. */
166 gnutls_global_init ();
170 gnutls_global_set_log_function (client_log_func
);
171 gnutls_global_set_log_level (7);
174 gnutls_anon_allocate_client_credentials (&anoncred
);
175 gnutls_certificate_allocate_credentials (&x509_cred
);
177 /* Initialize TLS session
179 gnutls_init (&session
, GNUTLS_CLIENT
|GNUTLS_DATAGRAM
);
180 gnutls_dtls_set_mtu( session
, 1500);
182 /* Use default priorities */
183 gnutls_priority_set_direct (session
, prio
, NULL
);
185 /* put the anonymous credentials to the current session
187 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
188 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
190 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
191 gnutls_transport_set_push_function (session
, push
);
193 /* Perform the TLS handshake
197 ret
= gnutls_handshake (session
);
199 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
203 fail ("client: Handshake failed\n");
210 success ("client: Handshake was completed\n");
214 success ("client: TLS version is: %s\n",
215 gnutls_protocol_get_name (gnutls_protocol_get_version
220 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
221 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
227 success ("client: Peer has closed the TLS connection\n");
234 fail ("client: Error: %s\n", gnutls_strerror (ret
));
239 gnutls_bye (session
, GNUTLS_SHUT_WR
);
245 gnutls_deinit (session
);
247 gnutls_anon_free_client_credentials (anoncred
);
248 gnutls_certificate_free_credentials (x509_cred
);
250 gnutls_global_deinit ();
254 /* These are global */
255 gnutls_anon_server_credentials_t anoncred
;
256 gnutls_certificate_credentials_t x509_cred
;
259 static gnutls_session_t
260 initialize_tls_session (const char* prio
)
262 gnutls_session_t session
;
264 gnutls_init (&session
, GNUTLS_SERVER
|GNUTLS_DATAGRAM
);
265 gnutls_dtls_set_mtu( session
, 1500);
267 /* avoid calling all the priority functions, since the defaults
270 gnutls_priority_set_direct (session
, prio
, NULL
);
272 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
273 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
278 static void terminate(void)
280 kill(child
, SIGTERM
);
285 server (int fd
, const char* prio
)
288 char buffer
[MAX_BUF
+ 1];
289 gnutls_session_t session
;
291 /* this must be called once in the program
293 gnutls_global_init ();
294 memset(buffer
, 0, sizeof(buffer
));
298 gnutls_global_set_log_function (server_log_func
);
299 gnutls_global_set_log_level (4711);
302 gnutls_certificate_allocate_credentials (&x509_cred
);
303 gnutls_certificate_set_x509_key_mem (x509_cred
, &server_cert
, &server_key
,
304 GNUTLS_X509_FMT_PEM
);
306 gnutls_anon_allocate_server_credentials (&anoncred
);
308 session
= initialize_tls_session (prio
);
310 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
311 gnutls_transport_set_push_function (session
, push_crippled
);
315 ret
= gnutls_handshake (session
);
317 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
321 gnutls_deinit (session
);
322 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
326 success ("server: Handshake was completed\n");
329 success ("server: TLS version is: %s\n",
330 gnutls_protocol_get_name (gnutls_protocol_get_version
335 ret
= gnutls_record_send (session
, buffer
, sizeof (buffer
));
336 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
341 fail("Error sending %d byte packet\n", to_send
);
349 /* do not wait for the peer to close the connection.
351 gnutls_bye (session
, GNUTLS_SHUT_WR
);
354 gnutls_deinit (session
);
356 gnutls_anon_free_server_credentials (anoncred
);
357 gnutls_certificate_free_credentials (x509_cred
);
359 gnutls_global_deinit ();
362 success ("server: finished\n");
365 static void start (const char* prio
)
370 ret
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fd
);
373 perror("socketpair");
389 server (fd
[0], prio
);
390 kill(child
, SIGTERM
);
395 client (fd
[1], prio
);
400 #define AES_CBC "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
401 #define AES_CBC_SHA256 "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+RSA:+AES-128-CBC:+AES-256-CBC:+SHA256:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
402 #define AES_GCM "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+RSA:+AES-128-GCM:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
404 static void ch_handler(int sig
)
408 if (WEXITSTATUS(status
) != 0 ||
409 (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGSEGV
))
411 if (WIFSIGNALED(status
))
412 fail("Child died with sigsegv\n");
414 fail("Child died with status %d\n", WEXITSTATUS(status
));
423 signal(SIGCHLD
, ch_handler
);
426 start(AES_CBC_SHA256
);