2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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>
28 #define _UC(c) ((unsigned char)(c))
30 static const char *progname
;
33 * Forward declaration, of function that uses internal interfaces, from headers
34 * included at the end of this module.
36 static void store_ctx_dane_init(X509_STORE_CTX
*, SSL
*);
38 static int saved_errno
;
40 static void save_errno(void)
45 static int restore_errno(void)
52 static void test_usage(void)
54 fprintf(stderr
, "usage: %s: danetest basedomain CAfile tlsafile\n", progname
);
57 static void print_errors(void)
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
);
71 fprintf(stderr
, "Error: %s:%s:%d\n", buffer
, file
, line
);
75 static int verify_chain(SSL
*ssl
, STACK_OF(X509
) *chain
)
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);
84 if ((store_ctx
= X509_STORE_CTX_new()) == NULL
)
87 if (!X509_STORE_CTX_init(store_ctx
, store
, cert
, chain
))
89 if (!X509_STORE_CTX_set_ex_data(store_ctx
, store_ctx_idx
, ssl
))
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
),
96 store_ctx_dane_init(store_ctx
, ssl
);
98 if (SSL_get_verify_callback(ssl
))
99 X509_STORE_CTX_set_verify_cb(store_ctx
, SSL_get_verify_callback(ssl
));
101 ret
= X509_verify_cert(store_ctx
);
103 SSL_set_verify_result(ssl
, X509_STORE_CTX_get_error(store_ctx
));
104 X509_STORE_CTX_cleanup(store_ctx
);
106 X509_STORE_CTX_free(store_ctx
);
111 static STACK_OF(X509
) *load_chain(BIO
*fp
, int nelem
)
116 unsigned char *data
= 0;
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);
122 if ((chain
= sk_X509_new_null()) == 0) {
128 count
< nelem
&& errtype
== 0
129 && PEM_read_bio(fp
, &name
, &header
, &data
, &len
);
131 const unsigned char *p
= data
;
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
);
140 if (cert
== 0 || (p
- data
) != len
)
141 errtype
= "certificate";
142 else if (sk_X509_push(chain
, cert
) == 0) {
147 fprintf(stderr
, "unexpected chain file object: %s\n", name
);
152 * If any of these were null, PEM_read() would have failed.
155 OPENSSL_free(header
);
160 fprintf(stderr
, "error reading: malformed %s\n", errtype
);
164 if (count
== nelem
) {
170 /* Some other PEM read error */
171 sk_X509_pop_free(chain
, X509_free
);
176 static char *read_to_eol(BIO
*f
)
178 static char buf
[1024];
181 if (!BIO_gets(f
, buf
, sizeof(buf
)))
186 if (buf
[n
-1] != '\n') {
187 if (n
+1 == sizeof(buf
)) {
188 fprintf(stderr
, "%s: warning: input too long\n", progname
);
190 fprintf(stderr
, "%s: warning: EOF before newline\n", progname
);
195 /* Trim trailing whitespace */
196 while (n
> 0 && isspace(_UC(buf
[n
-1])))
203 * Hex decoder that tolerates optional whitespace
205 static ossl_ssize_t
hexdecode(const char *in
, void *result
)
207 unsigned char **out
= (unsigned char **)result
;
208 unsigned char *ret
= OPENSSL_malloc(strlen(in
)/2);
209 unsigned char *cp
= ret
;
216 for (byte
= 0; *in
; ++in
) {
219 if (isspace(_UC(*in
)))
221 x
= OPENSSL_hexchar2int(*in
);
227 if ((nibble
^= 1) == 0) {
239 return cp
- (*out
= ret
);
242 static ossl_ssize_t
checked_uint8(const char *in
, void *out
)
244 uint8_t *result
= (uint8_t *)out
;
251 v
= strtol(cp
, &endp
, 10);
254 if (((v
== LONG_MIN
|| v
== LONG_MAX
) && e
== ERANGE
) ||
255 endp
== cp
|| !isspace(_UC(*endp
)) ||
256 v
!= (*(uint8_t *)result
= (uint8_t) v
)) {
259 for (cp
= endp
; isspace(_UC(*cp
)); ++cp
)
267 ossl_ssize_t (*parser
)(const char *, void *);
270 static int tlsa_import_rr(SSL
*ssl
, const char *rrdata
)
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
},
284 struct tlsa_field
*f
;
285 const char *cp
= rrdata
;
286 ossl_ssize_t len
= 0;
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
);
295 ret
= SSL_dane_tlsa_add(ssl
, usage
, selector
, mtype
, data
, len
);
300 fprintf(stderr
, "%s: warning: unusable TLSA rrdata: %s\n",
305 fprintf(stderr
, "%s: warning: error loading TLSA rrdata: %s\n",
312 static int allws(const char *cp
)
315 if (!isspace(_UC(*cp
++)))
320 static int test_tlsafile(SSL_CTX
*ctx
, const char *basename
,
321 BIO
*f
, const char *path
)
328 while (ret
> 0 && (line
= read_to_eol(f
)) != NULL
) {
329 STACK_OF(X509
) *chain
;
341 if (*line
== '\0' || *line
== '#')
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
);
353 if ((ssl
= SSL_new(ctx
)) == NULL
)
355 SSL_set_connect_state(ssl
);
356 if (SSL_dane_enable(ssl
, basename
) <= 0) {
361 SSL_dane_set_flags(ssl
, DANE_FLAG_NO_DANE_EE_NAMECHECKS
);
363 for (i
= 0; i
< ntlsa
; ++i
) {
364 if ((line
= read_to_eol(f
)) == NULL
|| !tlsa_import_rr(ssl
, line
)) {
370 /* Don't report old news */
372 chain
= load_chain(f
, ncert
);
378 ok
= verify_chain(ssl
, chain
);
379 sk_X509_pop_free(chain
, X509_free
);
380 err
= SSL_get_verify_result(ssl
);
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.
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
);
394 fprintf(stderr
, "verify_chain internal error in %s test %d\n",
399 if (err
!= want
|| (want
== 0 && !ok
)) {
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
));
406 fprintf(stderr
, "Unexpected error in %s test %d: %d: wanted %d\n",
407 path
, testno
, err
, want
);
409 fprintf(stderr
, "Verification failure in %s test %d: ok=0\n",
415 if (mdpth
!= want_depth
) {
417 fprintf(stderr
, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
418 path
, testno
, want_depth
, mdpth
);
420 fprintf(stderr
, "%s: test %d successful\n", path
, testno
);
427 int main(int argc
, char *argv
[])
432 const char *basedomain
;
434 const char *tlsafile
;
443 basedomain
= argv
[1];
447 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
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
);
454 f
= BIO_new_file(tlsafile
, "r");
456 fprintf(stderr
, "%s: Error opening tlsa record file: '%s': %s\n",
457 progname
, tlsafile
, strerror(errno
));
461 ctx
= SSL_CTX_new(TLS_client_method());
462 if (SSL_CTX_dane_enable(ctx
) <= 0) {
466 if (!SSL_CTX_load_verify_locations(ctx
, CAfile
, NULL
)) {
470 if ((SSL_CTX_dane_mtype_set(ctx
, EVP_sha512(), 2, 1)) <= 0) {
474 if ((SSL_CTX_dane_mtype_set(ctx
, EVP_sha256(), 1, 2)) <= 0) {
479 if (test_tlsafile(ctx
, basedomain
, f
, tlsafile
) <= 0) {
491 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
492 if (CRYPTO_mem_leaks(bio_err
) <= 0)
499 #include <internal/dane.h>
501 static void store_ctx_dane_init(X509_STORE_CTX
*store_ctx
, SSL
*ssl
)
503 X509_STORE_CTX_set0_dane(store_ctx
, SSL_get0_dane(ssl
));