Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / mini-x509-callbacks.c
1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
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
35 /* Tests whether the verify callbacks are operational.
36  * In addition gnutls_handshake_get_last_in() and gnutls_handshake_get_last_out() 
37  * are tested.
38  */
39
40 const char *side;
41 static int client_ok = 0, server_ok = 0;
42 static int pch_ok = 0;
43
44 static int client_callback(gnutls_session_t session)
45 {
46         client_ok = 1;
47         return 0;
48 }
49
50 static void verify_alpn(gnutls_session_t session)
51 {
52         int ret;
53         gnutls_datum_t selected;
54         char str[64];
55
56         snprintf(str, sizeof(str), "myproto");
57
58         ret = gnutls_alpn_get_selected_protocol(session, &selected);
59         if (ret < 0) {
60                 fail("gnutls_alpn_get_selected_protocol: %s\n", gnutls_strerror(ret));
61                 exit(1);
62         }
63
64         if (strlen(str) != selected.size || memcmp(str, selected.data, selected.size) != 0) {
65                 fail("expected protocol %s, got %.*s\n", str, selected.size, selected.data);
66                 exit(1);
67         }
68
69         if (debug)
70                 success("ALPN got: %s\n", str);
71 }
72
73 static int post_client_hello_callback(gnutls_session_t session)
74 {
75         /* verify that the post-client-hello callback has access to ALPN data */
76         verify_alpn(session);
77         pch_ok = 1;
78         return 0;
79 }
80
81 unsigned int msg_order[] = {
82         GNUTLS_HANDSHAKE_CLIENT_HELLO,
83         GNUTLS_HANDSHAKE_SERVER_HELLO,
84         GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
85         GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
86         GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST,
87         GNUTLS_HANDSHAKE_SERVER_HELLO_DONE,
88         GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
89         GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE,
90         GNUTLS_HANDSHAKE_FINISHED,
91         GNUTLS_HANDSHAKE_FINISHED,
92 };
93
94 static int handshake_callback(gnutls_session_t session, unsigned int htype,
95                               unsigned post, unsigned int incoming)
96 {
97         static unsigned idx = 0;
98         unsigned int msg;
99
100         if (msg_order[idx] != htype) {
101                 fail("%s: %s, expected %s\n",
102                      incoming != 0 ? "Received" : "Sent",
103                      gnutls_handshake_description_get_name(htype),
104                      gnutls_handshake_description_get_name(msg_order
105                                                            [idx]));
106                 exit(1);
107         }
108         idx++;
109
110         if (incoming != 0) {
111                 msg = gnutls_handshake_get_last_in(session);
112                 if (msg != htype) {
113                         fail("last input message was not recorded (exp: %d, found: %d) \n", msg, htype);
114                         exit(1);
115                 }
116         } else {
117                 msg = gnutls_handshake_get_last_out(session);
118                 if (msg != htype) {
119                         fail("last output message was not recorded (exp: %d, found: %d) \n", msg, htype);
120                         exit(1);
121                 }
122         }
123
124         return 0;
125 }
126
127 static int server_callback(gnutls_session_t session)
128 {
129         server_ok = 1;
130
131         if (gnutls_handshake_get_last_in(session) !=
132             GNUTLS_HANDSHAKE_CERTIFICATE_PKT) {
133                 fail("client's last input message was unexpected\n");
134                 exit(1);
135         }
136
137         if (gnutls_handshake_get_last_out(session) !=
138             GNUTLS_HANDSHAKE_SERVER_HELLO_DONE) {
139                 fail("client's last output message was unexpected\n");
140                 exit(1);
141         }
142
143         return 0;
144 }
145
146 static void tls_log_func(int level, const char *str)
147 {
148         fprintf(stderr, "%s|<%d>| %s", side, level, str);
149 }
150
151 static unsigned char server_cert_pem[] =
152     "-----BEGIN CERTIFICATE-----\n"
153     "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
154     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
155     "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
156     "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
157     "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
158     "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
159     "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
160     "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
161     "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
162     "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
163     "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
164     "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
165     "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
166
167 const gnutls_datum_t server_cert = { server_cert_pem,
168         sizeof(server_cert_pem)
169 };
170
171 static unsigned char server_key_pem[] =
172     "-----BEGIN RSA PRIVATE KEY-----\n"
173     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
174     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
175     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
176     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
177     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
178     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
179     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
180     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
181     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
182     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
183     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
184     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
185     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
186     "-----END RSA PRIVATE KEY-----\n";
187
188 const gnutls_datum_t server_key = { server_key_pem,
189         sizeof(server_key_pem)
190 };
191
192 static void append_alpn(gnutls_session_t session)
193 {
194         gnutls_datum_t protocol;
195         int ret;
196         char str[64];
197
198         snprintf(str, sizeof(str), "myproto");
199
200         protocol.data = (void*)str;
201         protocol.size = strlen(str);
202
203         ret = gnutls_alpn_set_protocols(session, &protocol, 1, 0);
204         if (ret < 0) {
205                 gnutls_perror(ret);
206                 exit(1);
207         }
208 }
209
210 void doit(void)
211 {
212         /* Server stuff. */
213         gnutls_certificate_credentials_t serverx509cred;
214         gnutls_session_t server;
215         int sret = GNUTLS_E_AGAIN;
216         /* Client stuff. */
217         gnutls_certificate_credentials_t clientx509cred;
218         gnutls_session_t client;
219         int cret = GNUTLS_E_AGAIN;
220
221         /* General init. */
222         global_init();
223         gnutls_global_set_log_function(tls_log_func);
224         if (debug)
225                 gnutls_global_set_log_level(2);
226
227         /* Init server */
228         gnutls_certificate_allocate_credentials(&serverx509cred);
229         gnutls_certificate_set_x509_key_mem(serverx509cred,
230                                             &server_cert, &server_key,
231                                             GNUTLS_X509_FMT_PEM);
232         gnutls_init(&server, GNUTLS_SERVER);
233         gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
234                                serverx509cred);
235         gnutls_priority_set_direct(server, "NORMAL", NULL);
236         gnutls_transport_set_push_function(server, server_push);
237         gnutls_transport_set_pull_function(server, server_pull);
238         gnutls_transport_set_ptr(server, server);
239         gnutls_certificate_set_verify_function(serverx509cred,
240                                                server_callback);
241         gnutls_certificate_server_set_request(server, GNUTLS_CERT_REQUEST);
242         gnutls_handshake_set_post_client_hello_function(server,
243                                                         post_client_hello_callback);
244         gnutls_handshake_set_hook_function(server, GNUTLS_HANDSHAKE_ANY,
245                                            GNUTLS_HOOK_POST,
246                                            handshake_callback);
247         append_alpn(server);
248
249         /* Init client */
250         gnutls_certificate_allocate_credentials(&clientx509cred);
251         gnutls_init(&client, GNUTLS_CLIENT);
252         gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE,
253                                clientx509cred);
254         gnutls_priority_set_direct(client, "NORMAL", NULL);
255         gnutls_transport_set_push_function(client, client_push);
256         gnutls_transport_set_pull_function(client, client_pull);
257         gnutls_transport_set_ptr(client, client);
258         gnutls_certificate_set_verify_function(clientx509cred,
259                                                client_callback);
260         append_alpn(client);
261
262         HANDSHAKE(client, server);
263
264         gnutls_bye(client, GNUTLS_SHUT_RDWR);
265         gnutls_bye(server, GNUTLS_SHUT_RDWR);
266
267         gnutls_deinit(client);
268         gnutls_deinit(server);
269
270         gnutls_certificate_free_credentials(serverx509cred);
271         gnutls_certificate_free_credentials(clientx509cred);
272
273         gnutls_global_deinit();
274
275         if (pch_ok == 0)
276                 fail("Post client hello callback wasn't called\n");
277
278         if (server_ok == 0)
279                 fail("Server certificate verify callback wasn't called\n");
280
281         if (client_ok == 0)
282                 fail("Client certificate verify callback wasn't called\n");
283 }