sha/keccak1600.c: add another 1x variant.
[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 #include "testutil.h"
26
27 #include "e_os.h"
28
29 #define _UC(c) ((unsigned char)(c))
30
31 static const char *basedomain;
32 static const char *CAfile;
33 static const char *tlsafile;
34
35 /*
36  * Forward declaration, of function that uses internal interfaces, from headers
37  * included at the end of this module.
38  */
39 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
40
41 static int saved_errno;
42
43 static void save_errno(void)
44 {
45     saved_errno = errno;
46 }
47
48 static int restore_errno(void)
49 {
50     int ret = errno;
51     errno = saved_errno;
52     return ret;
53 }
54
55 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
56 {
57     X509_STORE_CTX *store_ctx = NULL;
58     SSL_CTX *ssl_ctx = NULL;
59     X509_STORE *store = NULL;
60     X509 *cert = NULL;
61     int ret = 0;
62     int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
63
64     if (!TEST_ptr(store_ctx = X509_STORE_CTX_new())
65             || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl))
66             || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx))
67             || !TEST_ptr(cert = sk_X509_value(chain, 0))
68             || !TEST_true(X509_STORE_CTX_init(store_ctx, store, cert, chain))
69             || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx,
70                                                      ssl)))
71         goto end;
72
73     X509_STORE_CTX_set_default(store_ctx,
74             SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
75     X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
76             SSL_get0_param(ssl));
77     store_ctx_dane_init(store_ctx, ssl);
78
79     if (SSL_get_verify_callback(ssl) != NULL)
80         X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
81
82     /* Mask "internal failures" (-1) from our return value. */
83     if (!TEST_int_ge(ret = X509_verify_cert(store_ctx), 0))
84         ret = 0;
85
86     SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
87     X509_STORE_CTX_cleanup(store_ctx);
88
89 end:
90     X509_STORE_CTX_free(store_ctx);
91     return ret;
92 }
93
94 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
95 {
96     int count;
97     char *name = 0;
98     char *header = 0;
99     unsigned char *data = 0;
100     long len;
101     char *errtype = 0;                /* if error: cert or pkey? */
102     STACK_OF(X509) *chain;
103     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
104
105     if (!TEST_ptr(chain = sk_X509_new_null()))
106         goto err;
107
108     for (count = 0;
109          count < nelem && errtype == 0
110          && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
111          ++count) {
112         if (strcmp(name, PEM_STRING_X509) == 0
113                     || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
114                     || strcmp(name, PEM_STRING_X509_OLD) == 0) {
115             d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
116                 ? d2i_X509_AUX : d2i_X509;
117             X509 *cert;
118             const unsigned char *p = data;
119
120             if (!TEST_ptr(cert = d(0, &p, len))
121                     || !TEST_long_eq(p - data, len)) {
122                 TEST_info("Certificate parsing error");
123                 goto err;
124             }
125
126             if (!TEST_true(sk_X509_push(chain, cert)))
127                 goto err;
128         } else {
129             TEST_info("Unknown chain file object %s", name);
130             goto err;
131         }
132
133         OPENSSL_free(name);
134         OPENSSL_free(header);
135         OPENSSL_free(data);
136     }
137
138     if (count == nelem) {
139         ERR_clear_error();
140         return chain;
141     }
142
143 err:
144     OPENSSL_free(name);
145     OPENSSL_free(header);
146     OPENSSL_free(data);
147     sk_X509_pop_free(chain, X509_free);
148     return NULL;
149 }
150
151 static char *read_to_eol(BIO *f)
152 {
153     static char buf[1024];
154     int n;
155
156     if (!BIO_gets(f, buf, sizeof(buf)))
157         return NULL;
158
159     n = strlen(buf);
160     if (buf[n - 1] != '\n') {
161         if (n + 1 == sizeof(buf))
162             TEST_error("input too long");
163         else
164             TEST_error("EOF before newline");
165         return NULL;
166     }
167
168     /* Trim trailing whitespace */
169     while (n > 0 && isspace(_UC(buf[n - 1])))
170         buf[--n] = '\0';
171
172     return buf;
173 }
174
175 /*
176  * Hex decoder that tolerates optional whitespace
177  */
178 static ossl_ssize_t hexdecode(const char *in, void *result)
179 {
180     unsigned char **out = (unsigned char **)result;
181     unsigned char *ret;
182     unsigned char *cp;
183     uint8_t byte;
184     int nibble = 0;
185
186     if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
187         return -1;
188     cp = ret;
189
190     for (byte = 0; *in; ++in) {
191         int x;
192
193         if (isspace(_UC(*in)))
194             continue;
195         x = OPENSSL_hexchar2int(*in);
196         if (x < 0) {
197             OPENSSL_free(ret);
198             return 0;
199         }
200         byte |= (char)x;
201         if ((nibble ^= 1) == 0) {
202             *cp++ = byte;
203             byte = 0;
204         } else {
205             byte <<= 4;
206         }
207     }
208     if (nibble != 0) {
209         OPENSSL_free(ret);
210         return 0;
211     }
212
213     return cp - (*out = ret);
214 }
215
216 static ossl_ssize_t checked_uint8(const char *in, void *out)
217 {
218     uint8_t *result = (uint8_t *)out;
219     const char *cp = in;
220     char *endp;
221     long v;
222     int e;
223
224     save_errno();
225     v = strtol(cp, &endp, 10);
226     e = restore_errno();
227
228     if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
229         endp == cp || !isspace(_UC(*endp)) ||
230         v != (*(uint8_t *)result = (uint8_t) v)) {
231         return -1;
232     }
233     for (cp = endp; isspace(_UC(*cp)); ++cp)
234         continue;
235     return cp - in;
236 }
237
238 struct tlsa_field {
239     void *var;
240     const char *name;
241     ossl_ssize_t (*parser)(const char *, void *);
242 };
243
244 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
245 {
246     static uint8_t usage;
247     static uint8_t selector;
248     static uint8_t mtype;
249     static unsigned char *data = NULL;
250     static struct tlsa_field tlsa_fields[] = {
251         { &usage, "usage", checked_uint8 },
252         { &selector, "selector", checked_uint8 },
253         { &mtype, "mtype", checked_uint8 },
254         { &data, "data", hexdecode },
255         { NULL, }
256     };
257     int ret;
258     struct tlsa_field *f;
259     const char *cp = rrdata;
260     ossl_ssize_t len = 0;
261
262     for (f = tlsa_fields; f->var; ++f) {
263         if ((len = f->parser(cp += len, f->var)) <= 0) {
264             TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
265             return 0;
266         }
267     }
268
269     ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
270     OPENSSL_free(data);
271     if (ret == 0) {
272         TEST_info("unusable TLSA rrdata: %s", rrdata);
273         return 0;
274     }
275     if (ret < 0) {
276         TEST_info("error loading TLSA rrdata: %s", rrdata);
277         return 0;
278     }
279
280     return ret;
281 }
282
283 static int allws(const char *cp)
284 {
285     while (*cp)
286         if (!isspace(_UC(*cp++)))
287             return 0;
288     return 1;
289 }
290
291 static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
292                          BIO *f, const char *path)
293 {
294     char *line;
295     int testno = 0;
296     int ret = 1;
297     SSL *ssl;
298
299     while (ret > 0 && (line = read_to_eol(f)) != NULL) {
300         STACK_OF(X509) *chain;
301         int ntlsa;
302         int ncert;
303         int noncheck;
304         int want;
305         int want_depth;
306         int off;
307         int i;
308         int ok;
309         int err;
310         int mdpth;
311
312         if (*line == '\0' || *line == '#')
313             continue;
314
315         ++testno;
316         if (sscanf(line, "%d %d %d %d %d%n",
317                    &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
318             || !allws(line + off)) {
319             TEST_error("Malformed line for test %d", testno);
320             return 0;
321         }
322
323         if (!TEST_ptr(ssl = SSL_new(ctx)))
324             return 0;
325         SSL_set_connect_state(ssl);
326         if (SSL_dane_enable(ssl, base_name) <= 0) {
327             SSL_free(ssl);
328             return 0;
329         }
330         if (noncheck)
331             SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
332
333         for (i = 0; i < ntlsa; ++i) {
334             if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
335                 SSL_free(ssl);
336                 return 0;
337             }
338         }
339
340         /* Don't report old news */
341         ERR_clear_error();
342         if (!TEST_ptr(chain = load_chain(f, ncert))) {
343             SSL_free(ssl);
344             return 0;
345         }
346
347         ok = verify_chain(ssl, chain);
348         sk_X509_pop_free(chain, X509_free);
349         err = SSL_get_verify_result(ssl);
350         /*
351          * Peek under the hood, normally TLSA match data is hidden when
352          * verification fails, we can obtain any suppressed data by setting the
353          * verification result to X509_V_OK before looking.
354          */
355         SSL_set_verify_result(ssl, X509_V_OK);
356         mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
357         /* Not needed any more, but lead by example and put the error back. */
358         SSL_set_verify_result(ssl, err);
359         SSL_free(ssl);
360
361         if (!TEST_int_eq(err, want)) {
362             if (want == X509_V_OK)
363                 TEST_info("Verification failure in test %d: %d=%s",
364                           testno, err, X509_verify_cert_error_string(err));
365             else
366                 TEST_info("Unexpected error in test %d", testno);
367             ret = 0;
368             continue;
369         }
370         if (!TEST_false(want == 0 && ok == 0)) {
371             TEST_info("Verification failure in test %d: ok=0", testno);
372             ret = 0;
373             continue;
374         }
375         if (!TEST_int_eq(mdpth, want_depth)) {
376             TEST_info("In test test %d", testno);
377             ret = 0;
378         }
379     }
380     ERR_clear_error();
381
382     return ret;
383 }
384
385 static int run_tlsatest()
386 {
387     SSL_CTX *ctx = NULL;
388     BIO *f = NULL;
389     int ret = 0;
390
391     if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
392             || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
393             || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
394             || !TEST_true(SSL_CTX_load_verify_locations(ctx, CAfile, NULL))
395             || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1),
396                             0)
397             || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2),
398                             0)
399             || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
400         goto end;
401     ret = 1;
402
403 end:
404     BIO_free(f);
405     SSL_CTX_free(ctx);
406
407     return ret;
408 }
409
410 int test_main(int argc, char *argv[])
411 {
412     int ret = 0;
413
414     if (argc != 4) {
415         TEST_error("Usage error: danetest basedomain CAfile tlsafile");
416         return 0;
417     }
418     basedomain = argv[1];
419     CAfile = argv[2];
420     tlsafile = argv[3];
421
422     ADD_TEST(run_tlsatest);
423
424     ret = run_tests(argv[0]);
425     return ret;
426 }
427
428 #include <internal/dane.h>
429
430 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
431 {
432     X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
433 }