Gracefully free a NULL HANDSHAKE_RESULT
[openssl.git] / test / danetest.c
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <limits.h>
14 #include <errno.h>
15
16 #include <openssl/crypto.h>
17 #include <openssl/evp.h>
18 #include <openssl/x509.h>
19 #include <openssl/ssl.h>
20 #include <openssl/err.h>
21 #include <openssl/conf.h>
22 #ifndef OPENSSL_NO_ENGINE
23 #include <openssl/engine.h>
24 #endif
25
26 #include "../e_os.h"
27
28 #define _UC(c) ((unsigned char)(c))
29
30 static const char *progname;
31
32 /*
33  * Forward declaration, of function that uses internal interfaces, from headers
34  * included at the end of this module.
35  */
36 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
37
38 static int saved_errno;
39
40 static void save_errno(void)
41 {
42     saved_errno = errno;
43 }
44
45 static int restore_errno(void)
46 {
47     int ret = errno;
48     errno = saved_errno;
49     return ret;
50 }
51
52 static void test_usage(void)
53 {
54     fprintf(stderr, "usage: %s: danetest basedomain CAfile tlsafile\n", progname);
55 }
56
57 static void print_errors(void)
58 {
59     unsigned long err;
60     char buffer[1024];
61     const char *file;
62     const char *data;
63     int line;
64     int flags;
65
66     while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
67         ERR_error_string_n(err, buffer, sizeof(buffer));
68         if (flags & ERR_TXT_STRING)
69             fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
70         else
71             fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
72     }
73 }
74
75 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
76 {
77     int ret = -1;
78     X509_STORE_CTX *store_ctx;
79     SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
80     X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
81     int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
82     X509 *cert = sk_X509_value(chain, 0);
83
84     if ((store_ctx = X509_STORE_CTX_new()) == NULL)
85         return -1;
86
87     if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
88         goto end;
89     if (!X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl))
90         goto end;
91
92     X509_STORE_CTX_set_default(store_ctx,
93             SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
94     X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
95             SSL_get0_param(ssl));
96     store_ctx_dane_init(store_ctx, ssl);
97
98     if (SSL_get_verify_callback(ssl))
99         X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
100
101     ret = X509_verify_cert(store_ctx);
102
103     SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
104     X509_STORE_CTX_cleanup(store_ctx);
105 end:
106     X509_STORE_CTX_free(store_ctx);
107
108     return (ret);
109 }
110
111 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
112 {
113     int count;
114     char *name = 0;
115     char *header = 0;
116     unsigned char *data = 0;
117     long len;
118     char *errtype = 0;                /* if error: cert or pkey? */
119     STACK_OF(X509) *chain;
120     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
121
122     if ((chain = sk_X509_new_null()) == 0) {
123         perror("malloc");
124         exit(1);
125     }
126
127     for (count = 0;
128          count < nelem && errtype == 0
129          && PEM_read_bio(fp, &name, &header, &data, &len);
130          ++count) {
131         const unsigned char *p = data;
132
133         if (strcmp(name, PEM_STRING_X509) == 0
134             || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
135             || strcmp(name, PEM_STRING_X509_OLD) == 0) {
136             d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) ?
137                 d2i_X509_AUX : d2i_X509;
138             X509 *cert = d(0, &p, len);
139
140             if (cert == 0 || (p - data) != len)
141                 errtype = "certificate";
142             else if (sk_X509_push(chain, cert) == 0) {
143                 perror("malloc");
144                 goto err;
145             }
146         } else {
147             fprintf(stderr, "unexpected chain file object: %s\n", name);
148             goto err;
149         }
150
151         /*
152          * If any of these were null, PEM_read() would have failed.
153          */
154         OPENSSL_free(name);
155         OPENSSL_free(header);
156         OPENSSL_free(data);
157     }
158
159     if (errtype) {
160         fprintf(stderr, "error reading: malformed %s\n", errtype);
161         goto err;
162     }
163
164     if (count == nelem) {
165         ERR_clear_error();
166         return chain;
167     }
168
169 err:
170     /* Some other PEM read error */
171     sk_X509_pop_free(chain, X509_free);
172     print_errors();
173     return NULL;
174 }
175
176 static char *read_to_eol(BIO *f)
177 {
178     static char buf[1024];
179     int n;
180
181     if (!BIO_gets(f, buf, sizeof(buf)))
182         return NULL;
183
184     n = strlen(buf);
185
186     if (buf[n-1] != '\n') {
187         if (n+1 == sizeof(buf)) {
188             fprintf(stderr, "%s: warning: input too long\n", progname);
189         } else {
190             fprintf(stderr, "%s: warning: EOF before newline\n", progname);
191         }
192         return NULL;
193     }
194
195     /* Trim trailing whitespace */
196     while (n > 0 && isspace(_UC(buf[n-1])))
197         buf[--n] = '\0';
198
199     return buf;
200 }
201
202 /*
203  * Hex decoder that tolerates optional whitespace
204  */
205 static ossl_ssize_t hexdecode(const char *in, void *result)
206 {
207     unsigned char **out = (unsigned char **)result;
208     unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
209     unsigned char *cp = ret;
210     uint8_t byte;
211     int nibble = 0;
212
213     if (ret == NULL)
214         return -1;
215
216     for (byte = 0; *in; ++in) {
217         int x;
218
219         if (isspace(_UC(*in)))
220             continue;
221         x = OPENSSL_hexchar2int(*in);
222         if (x < 0) {
223             OPENSSL_free(ret);
224             return 0;
225         }
226         byte |= (char)x;
227         if ((nibble ^= 1) == 0) {
228             *cp++ = byte;
229             byte = 0;
230         } else {
231             byte <<= 4;
232         }
233     }
234     if (nibble != 0) {
235         OPENSSL_free(ret);
236         return 0;
237     }
238
239     return cp - (*out = ret);
240 }
241
242 static ossl_ssize_t checked_uint8(const char *in, void *out)
243 {
244     uint8_t *result = (uint8_t *)out;
245     const char *cp = in;
246     char *endp;
247     long v;
248     int e;
249
250     save_errno();
251     v = strtol(cp, &endp, 10);
252     e = restore_errno();
253
254     if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
255         endp == cp || !isspace(_UC(*endp)) ||
256         v != (*(uint8_t *)result = (uint8_t) v)) {
257         return -1;
258     }
259     for (cp = endp; isspace(_UC(*cp)); ++cp)
260         continue;
261     return cp - in;
262 }
263
264 struct tlsa_field {
265     void *var;
266     const char *name;
267     ossl_ssize_t (*parser)(const char *, void *);
268 };
269
270 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
271 {
272     static uint8_t usage;
273     static uint8_t selector;
274     static uint8_t mtype;
275     static unsigned char *data = NULL;
276     static struct tlsa_field tlsa_fields[] = {
277         { &usage, "usage", checked_uint8 },
278         { &selector, "selector", checked_uint8 },
279         { &mtype, "mtype", checked_uint8 },
280         { &data, "data", hexdecode },
281         { NULL, }
282     };
283     int ret;
284     struct tlsa_field *f;
285     const char *cp = rrdata;
286     ossl_ssize_t len = 0;
287
288     for (f = tlsa_fields; f->var; ++f) {
289         if ((len = f->parser(cp += len, f->var)) <= 0) {
290             fprintf(stderr, "%s: warning: bad TLSA %s field in: %s\n",
291                     progname, f->name, rrdata);
292             return 0;
293         }
294     }
295     ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
296     OPENSSL_free(data);
297
298     if (ret == 0) {
299         print_errors();
300         fprintf(stderr, "%s: warning: unusable TLSA rrdata: %s\n",
301                 progname, rrdata);
302         return 0;
303     }
304     if (ret < 0) {
305         fprintf(stderr, "%s: warning: error loading TLSA rrdata: %s\n",
306                 progname, rrdata);
307         return 0;
308     }
309     return ret;
310 }
311
312 static int allws(const char *cp)
313 {
314     while (*cp)
315         if (!isspace(_UC(*cp++)))
316             return 0;
317     return 1;
318 }
319
320 static int test_tlsafile(SSL_CTX *ctx, const char *basename,
321                          BIO *f, const char *path)
322 {
323     char *line;
324     int testno = 0;
325     int ret = 1;
326     SSL *ssl;
327
328     while (ret > 0 && (line = read_to_eol(f)) != NULL) {
329         STACK_OF(X509) *chain;
330         int ntlsa;
331         int ncert;
332         int noncheck;
333         int want;
334         int want_depth;
335         int off;
336         int i;
337         int ok;
338         int err;
339         int mdpth;
340
341         if (*line == '\0' || *line == '#')
342             continue;
343
344         ++testno;
345         if (sscanf(line, "%d %d %d %d %d%n",
346                    &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
347             || !allws(line + off)) {
348             fprintf(stderr, "Expected tlsa count, cert count and result"
349                     " at test %d of %s\n", testno, path);
350             return 0;
351         }
352
353         if ((ssl = SSL_new(ctx)) == NULL)
354             return -1;
355         SSL_set_connect_state(ssl);
356         if (SSL_dane_enable(ssl, basename) <= 0) {
357             SSL_free(ssl);
358             return -1;
359         }
360         if (noncheck)
361             SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
362
363         for (i = 0; i < ntlsa; ++i) {
364             if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
365                 SSL_free(ssl);
366                 return 0;
367             }
368         }
369
370         /* Don't report old news */
371         ERR_clear_error();
372         chain = load_chain(f, ncert);
373         if (chain == NULL) {
374             SSL_free(ssl);
375             return -1;
376         }
377
378         ok = verify_chain(ssl, chain);
379         sk_X509_pop_free(chain, X509_free);
380         err = SSL_get_verify_result(ssl);
381         /*
382          * Peek under the hood, normally TLSA match data is hidden when
383          * verification fails, we can obtain any suppressed data by setting the
384          * verification result to X509_V_OK before looking.
385          */
386         SSL_set_verify_result(ssl, X509_V_OK);
387         mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
388         /* Not needed any more, but lead by example and put the error back. */
389         SSL_set_verify_result(ssl, err);
390         SSL_free(ssl);
391
392         if (ok < 0) {
393             ret = 0;
394             fprintf(stderr, "verify_chain internal error in %s test %d\n",
395                     path, testno);
396             print_errors();
397             continue;
398         }
399         if (err != want || (want == 0 && !ok)) {
400             ret = 0;
401             if (err != want) {
402                 if (want == X509_V_OK)
403                     fprintf(stderr, "Verification failure in %s test %d: %d: %s\n",
404                             path, testno, err, X509_verify_cert_error_string(err));
405                 else
406                     fprintf(stderr, "Unexpected error in %s test %d: %d: wanted %d\n",
407                             path, testno, err, want);
408             } else {
409                 fprintf(stderr, "Verification failure in %s test %d: ok=0\n",
410                         path, testno);
411             }
412             print_errors();
413             continue;
414         }
415         if (mdpth != want_depth) {
416             ret = 0;
417             fprintf(stderr, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
418                     path, testno, want_depth, mdpth);
419         }
420         fprintf(stderr, "%s: test %d successful\n", path, testno);
421     }
422     ERR_clear_error();
423
424     return ret;
425 }
426
427 int main(int argc, char *argv[])
428 {
429     BIO *f;
430     BIO *bio_err;
431     SSL_CTX *ctx = NULL;
432     const char *basedomain;
433     const char *CAfile;
434     const char *tlsafile;
435     const char *p;
436     int ret = 1;
437
438     progname = argv[0];
439     if (argc != 4) {
440         test_usage();
441         EXIT(ret);
442     }
443     basedomain = argv[1];
444     CAfile = argv[2];
445     tlsafile = argv[3];
446
447     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
448
449     p = getenv("OPENSSL_DEBUG_MEMORY");
450     if (p != NULL && strcmp(p, "on") == 0)
451         CRYPTO_set_mem_debug(1);
452     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
453
454     f = BIO_new_file(tlsafile, "r");
455     if (f == NULL) {
456         fprintf(stderr, "%s: Error opening tlsa record file: '%s': %s\n",
457                 progname, tlsafile, strerror(errno));
458         EXIT(ret);
459     }
460
461     ctx = SSL_CTX_new(TLS_client_method());
462     if (SSL_CTX_dane_enable(ctx) <= 0) {
463         print_errors();
464         goto end;
465     }
466     if (!SSL_CTX_load_verify_locations(ctx, CAfile, NULL)) {
467         print_errors();
468         goto end;
469     }
470     if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1)) <= 0) {
471         print_errors();
472         goto end;
473     }
474     if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2)) <= 0) {
475         print_errors();
476         goto end;
477     }
478
479     if (test_tlsafile(ctx, basedomain, f, tlsafile) <= 0) {
480         print_errors();
481         goto end;
482     }
483
484     ret = 0;
485
486 end:
487
488     BIO_free(f);
489     SSL_CTX_free(ctx);
490
491 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
492     if (CRYPTO_mem_leaks(bio_err) <= 0)
493         ret = 1;
494 #endif
495     BIO_free(bio_err);
496     EXIT(ret);
497 }
498
499 #include <internal/dane.h>
500
501 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
502 {
503     X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
504 }