1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
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 #include <string.h>
30 #include <errno.h>
31 #include <gnutls/gnutls.h>
32 #include "utils.h"
33 #include "eagain-common.h"
34 #include "cert-common.h"
35
36 /* This tests whether the server reacts as expected on various client
37 * hello TLS versions */
38
39 void _gnutls_hello_set_default_version(gnutls_session_t session,
40 unsigned char major,
41 unsigned char minor);
42
43 const char *side;
44
tls_log_func(int level,const char * str)45 static void tls_log_func(int level, const char *str)
46 {
47 fprintf(stderr, "%s|<%d>| %s", side, level, str);
48 }
49
try(unsigned char major,unsigned char minor,int ret1,int ret2)50 static void try(unsigned char major, unsigned char minor, int ret1, int ret2)
51 {
52 int ret;
53 /* Server stuff. */
54 gnutls_certificate_credentials_t serverx509cred;
55 gnutls_session_t server;
56 int sret = GNUTLS_E_AGAIN;
57 /* Client stuff. */
58 gnutls_certificate_credentials_t clientx509cred;
59 gnutls_session_t client;
60 int cret = GNUTLS_E_AGAIN;
61
62 /* General init. */
63 gnutls_global_set_log_function(tls_log_func);
64 if (debug)
65 gnutls_global_set_log_level(6);
66
67 /* Init server */
68 gnutls_certificate_allocate_credentials(&serverx509cred);
69 gnutls_certificate_set_x509_key_mem(serverx509cred,
70 &server_cert, &server_key,
71 GNUTLS_X509_FMT_PEM);
72
73 gnutls_init(&server, GNUTLS_SERVER);
74 gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
75 serverx509cred);
76
77 gnutls_priority_set_direct(server,
78 "NORMAL:+VERS-TLS-ALL",
79 NULL);
80 gnutls_transport_set_push_function(server, server_push);
81 gnutls_transport_set_pull_function(server, server_pull);
82 gnutls_transport_set_ptr(server, server);
83
84 /* Init client */
85
86 ret = gnutls_certificate_allocate_credentials(&clientx509cred);
87 if (ret < 0)
88 exit(1);
89
90 ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca_cert, GNUTLS_X509_FMT_PEM);
91 if (ret < 0)
92 exit(1);
93
94 ret = gnutls_init(&client, GNUTLS_CLIENT);
95 if (ret < 0)
96 exit(1);
97
98 ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE,
99 clientx509cred);
100 if (ret < 0)
101 exit(1);
102
103 ret = gnutls_priority_set_direct(client, "NORMAL", NULL);
104 if (ret < 0)
105 exit(1);
106
107 gnutls_transport_set_push_function(client, client_push);
108 gnutls_transport_set_pull_function(client, client_pull);
109 gnutls_transport_set_ptr(client, client);
110 _gnutls_hello_set_default_version(client, major, minor);
111
112 HANDSHAKE_EXPECT(client, server, ret1, ret2);
113
114 gnutls_bye(client, GNUTLS_SHUT_RDWR);
115 gnutls_bye(server, GNUTLS_SHUT_RDWR);
116
117 gnutls_deinit(client);
118 gnutls_deinit(server);
119
120 gnutls_certificate_free_credentials(serverx509cred);
121 gnutls_certificate_free_credentials(clientx509cred);
122 }
123
doit(void)124 void doit(void)
125 {
126 global_init();
127
128 try(1,1, GNUTLS_E_AGAIN, GNUTLS_E_UNSUPPORTED_VERSION_PACKET);
129 reset_buffers();
130 try(2,1, GNUTLS_E_AGAIN, GNUTLS_E_UNSUPPORTED_VERSION_PACKET);
131 reset_buffers();
132 /* check SSL 3.0 which is disabled by default */
133 try(3,0, GNUTLS_E_AGAIN, GNUTLS_E_UNSUPPORTED_VERSION_PACKET);
134 reset_buffers();
135 try(3,2, 0, 0);
136 reset_buffers();
137 try(3,23, 0, 0);
138 reset_buffers();
139 try(4,0, 0, 0);
140 reset_buffers();
141 gnutls_global_deinit();
142 }
143