Merge branch 'upstream' into tizen
[platform/upstream/gnutls.git] / tests / sign-md5-rep.c
1 /*
2  * Copyright (C) 2015 Nikos Mavrogiannopoulos
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #if defined(_WIN32)
31
32 int main()
33 {
34         exit(77);
35 }
36
37 #else
38
39 #include <string.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
48 #include <signal.h>
49
50 #include "utils.h"
51
52 static void terminate(void);
53
54 /* This program tests whether MD5 is rejected by a client as a
55  * signature algorithm for the ServerKeyExchange.
56  */
57
58 static void server_log_func(int level, const char *str)
59 {
60         fprintf(stderr, "server|<%d>| %s", level, str);
61 }
62
63 static void client_log_func(int level, const char *str)
64 {
65         fprintf(stderr, "client|<%d>| %s", level, str);
66 }
67
68 static unsigned char server_cert_pem[] =
69     "-----BEGIN CERTIFICATE-----\n"
70     "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
71     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
72     "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
73     "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
74     "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
75     "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
76     "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
77     "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
78     "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
79     "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
80     "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
81     "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
82     "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
83
84 const gnutls_datum_t server_cert = { server_cert_pem,
85         sizeof(server_cert_pem)
86 };
87
88 static unsigned char server_key_pem[] =
89     "-----BEGIN RSA PRIVATE KEY-----\n"
90     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
91     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
92     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
93     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
94     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
95     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
96     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
97     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
98     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
99     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
100     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
101     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
102     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
103     "-----END RSA PRIVATE KEY-----\n";
104
105 const gnutls_datum_t server_key = { server_key_pem,
106         sizeof(server_key_pem)
107 };
108
109
110 static int handshake_callback(gnutls_session_t session, unsigned int htype,
111         unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
112 {
113         gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA", NULL);
114         return 0;
115 }
116         
117
118 /* A very basic TLS client, with anonymous authentication.
119  */
120
121 #define MAX_BUF 1024
122
123 static void client(int fd)
124 {
125         int ret;
126         char buffer[MAX_BUF + 1];
127         gnutls_certificate_credentials_t x509_cred;
128         gnutls_session_t session;
129         /* Need to enable anonymous KX specifically. */
130
131         global_init();
132
133         if (debug) {
134                 gnutls_global_set_log_function(client_log_func);
135                 gnutls_global_set_log_level(7);
136         }
137
138         gnutls_certificate_allocate_credentials(&x509_cred);
139
140         /* Initialize TLS session
141          */
142         gnutls_init(&session, GNUTLS_CLIENT);
143
144         /* Use default priorities */
145         gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA:-SIGN-ALL:+SIGN-RSA-MD5", NULL);
146
147         /* put the anonymous credentials to the current session
148          */
149         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
150
151         gnutls_transport_set_int(session, fd);
152         gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
153                                            GNUTLS_HOOK_PRE,
154                                            handshake_callback);
155
156         /* Perform the TLS handshake
157          */
158         do {
159                 ret = gnutls_handshake(session);
160         }
161         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
162
163         if (ret == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) {
164                 /* success */
165                 goto end;
166         }
167
168         if (ret < 0) {
169                 terminate();
170                 fail("client: Handshake failed: %s\n", gnutls_strerror(ret));
171                 exit(1);
172         } else {
173                 if (debug)
174                         success("client: Handshake was completed\n");
175         }
176
177         if (gnutls_sign_algorithm_get(session) == GNUTLS_SIGN_RSA_MD5) {
178                 terminate();
179                 fail("client: MD5 was negotiated\n");
180                 exit(1);
181         }
182         success("client: %s was negotiated\n", gnutls_sign_get_name(gnutls_sign_algorithm_get(session)));
183
184         if (debug)
185                 success("client: TLS version is: %s\n",
186                         gnutls_protocol_get_name
187                         (gnutls_protocol_get_version(session)));
188
189         do {
190                 do {
191                         ret = gnutls_record_recv(session, buffer, MAX_BUF);
192                 } while (ret == GNUTLS_E_AGAIN
193                          || ret == GNUTLS_E_INTERRUPTED);
194         } while (ret > 0);
195
196         if (ret == 0) {
197                 if (debug)
198                         success
199                             ("client: Peer has closed the TLS connection\n");
200                 goto end;
201         } else if (ret < 0) {
202                 terminate();
203                 fail("client: Error: %s\n", gnutls_strerror(ret));
204                 exit(1);
205         }
206
207         gnutls_bye(session, GNUTLS_SHUT_WR);
208
209       end:
210
211         close(fd);
212
213         gnutls_deinit(session);
214
215         gnutls_certificate_free_credentials(x509_cred);
216
217         gnutls_global_deinit();
218 }
219
220
221 /* These are global */
222 pid_t child;
223
224 static void terminate(void)
225 {
226         kill(child, SIGTERM);
227         exit(1);
228 }
229
230 static void server(int fd)
231 {
232         int ret;
233         char buffer[MAX_BUF + 1];
234         gnutls_session_t session;
235         gnutls_certificate_credentials_t x509_cred;
236
237         /* this must be called once in the program
238          */
239         global_init();
240         memset(buffer, 0, sizeof(buffer));
241
242         if (debug) {
243                 gnutls_global_set_log_function(server_log_func);
244                 gnutls_global_set_log_level(4711);
245         }
246
247         gnutls_certificate_allocate_credentials(&x509_cred);
248         gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
249                                             &server_key,
250                                             GNUTLS_X509_FMT_PEM);
251
252         gnutls_init(&session, GNUTLS_SERVER);
253
254         /* avoid calling all the priority functions, since the defaults
255          * are adequate.
256          */
257         gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA:-SIGN-ALL:+SIGN-RSA-MD5", NULL);
258
259         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
260
261         gnutls_transport_set_int(session, fd);
262
263         do {
264                 ret = gnutls_handshake(session);
265         } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
266         if (ret < 0) {
267                 /* failure is expected here */
268                 goto end;
269         }
270
271         if (debug) {
272                 success("server: Handshake was completed\n");
273                 success("server: %s was negotiated\n", gnutls_sign_get_name(gnutls_sign_algorithm_get(session)));
274         }
275
276         if (debug)
277                 success("server: TLS version is: %s\n",
278                         gnutls_protocol_get_name
279                         (gnutls_protocol_get_version(session)));
280
281         /* do not wait for the peer to close the connection.
282          */
283         gnutls_bye(session, GNUTLS_SHUT_WR);
284
285  end:
286         close(fd);
287         gnutls_deinit(session);
288
289         gnutls_certificate_free_credentials(x509_cred);
290
291         gnutls_global_deinit();
292
293         if (debug)
294                 success("server: finished\n");
295 }
296
297 static void ch_handler(int sig)
298 {
299         int status;
300         wait(&status);
301         if (WEXITSTATUS(status) != 0 ||
302             (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
303                 if (WIFSIGNALED(status))
304                         fail("Child died with sigsegv\n");
305                 else
306                         fail("Child died with status %d\n",
307                              WEXITSTATUS(status));
308                 terminate();
309         }
310         return;
311 }
312
313 void doit(void)
314 {
315         int fd[2];
316         int ret;
317
318         signal(SIGCHLD, ch_handler);
319
320         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
321         if (ret < 0) {
322                 perror("socketpair");
323                 exit(1);
324         }
325
326         child = fork();
327         if (child < 0) {
328                 perror("fork");
329                 fail("fork");
330                 exit(1);
331         }
332
333         if (child) {
334                 /* parent */
335                 close(fd[1]);
336                 client(fd[0]);
337                 kill(child, SIGTERM);
338         } else {
339                 close(fd[0]);
340                 server(fd[1]);
341                 exit(0);
342         }
343 }
344
345 #endif                          /* _WIN32 */