]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/danetest.c
1 /* ====================================================================
2 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
56 #include <openssl/crypto.h>
57 #include <openssl/evp.h>
58 #include <openssl/x509.h>
59 #include <openssl/ssl.h>
60 #include <openssl/err.h>
61 #include <openssl/conf.h>
62 #ifndef OPENSSL_NO_ENGINE
63 #include <openssl/engine.h>
68 #define _UC(c) ((unsigned char)(c))
70 static const char *progname
;
73 * Forward declaration, of function that uses internal interfaces, from headers
74 * included at the end of this module.
76 static void store_ctx_dane_init(X509_STORE_CTX
*, SSL
*);
78 static int saved_errno
;
80 static void save_errno(void)
85 static int restore_errno(void)
92 static void test_usage(void)
94 fprintf(stderr
, "usage: %s: danetest basedomain CAfile tlsafile\n", progname
);
97 static void print_errors(void)
106 while ((err
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0) {
107 ERR_error_string_n(err
, buffer
, sizeof(buffer
));
108 if (flags
& ERR_TXT_STRING
)
109 fprintf(stderr
, "Error: %s:%s:%d:%s\n", buffer
, file
, line
, data
);
111 fprintf(stderr
, "Error: %s:%s:%d\n", buffer
, file
, line
);
115 static int verify_chain(SSL
*ssl
, STACK_OF(X509
) *chain
)
118 X509_STORE_CTX
*store_ctx
;
119 SSL_CTX
*ssl_ctx
= SSL_get_SSL_CTX(ssl
);
120 X509_STORE
*store
= SSL_CTX_get_cert_store(ssl_ctx
);
121 int store_ctx_idx
= SSL_get_ex_data_X509_STORE_CTX_idx();
122 X509
*cert
= sk_X509_value(chain
, 0);
124 if ((store_ctx
= X509_STORE_CTX_new()) == NULL
)
127 if (!X509_STORE_CTX_init(store_ctx
, store
, cert
, chain
))
129 X509_STORE_CTX_set_ex_data(store_ctx
, store_ctx_idx
, ssl
);
131 X509_STORE_CTX_set_default(store_ctx
,
132 SSL_is_server(ssl
) ? "ssl_client" : "ssl_server");
133 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx
),
134 SSL_get0_param(ssl
));
135 store_ctx_dane_init(store_ctx
, ssl
);
137 if (SSL_get_verify_callback(ssl
))
138 X509_STORE_CTX_set_verify_cb(store_ctx
, SSL_get_verify_callback(ssl
));
140 ret
= X509_verify_cert(store_ctx
);
142 SSL_set_verify_result(ssl
, X509_STORE_CTX_get_error(store_ctx
));
143 X509_STORE_CTX_cleanup(store_ctx
);
144 X509_STORE_CTX_free(store_ctx
);
149 static STACK_OF(X509
) *load_chain(BIO
*fp
, int nelem
)
154 unsigned char *data
= 0;
156 char *errtype
= 0; /* if error: cert or pkey? */
157 STACK_OF(X509
) *chain
;
158 typedef X509
*(*d2i_X509_t
)(X509
**, const unsigned char **, long);
160 if ((chain
= sk_X509_new_null()) == 0) {
166 count
< nelem
&& errtype
== 0
167 && PEM_read_bio(fp
, &name
, &header
, &data
, &len
);
169 const unsigned char *p
= data
;
171 if (strcmp(name
, PEM_STRING_X509
) == 0
172 || strcmp(name
, PEM_STRING_X509_TRUSTED
) == 0
173 || strcmp(name
, PEM_STRING_X509_OLD
) == 0) {
174 d2i_X509_t d
= strcmp(name
, PEM_STRING_X509_TRUSTED
) ?
175 d2i_X509_AUX
: d2i_X509
;
176 X509
*cert
= d(0, &p
, len
);
178 if (cert
== 0 || (p
- data
) != len
)
179 errtype
= "certificate";
180 else if (sk_X509_push(chain
, cert
) == 0) {
185 fprintf(stderr
, "unexpected chain file object: %s\n", name
);
190 * If any of these were null, PEM_read() would have failed.
193 OPENSSL_free(header
);
198 fprintf(stderr
, "error reading: malformed %s\n", errtype
);
202 if (count
== nelem
) {
208 /* Some other PEM read error */
209 sk_X509_pop_free(chain
, X509_free
);
214 static char *read_to_eol(BIO
*f
)
216 static char buf
[1024];
219 if (!BIO_gets(f
, buf
, sizeof(buf
)))
224 if (buf
[n
-1] != '\n') {
225 if (n
+1 == sizeof(buf
)) {
226 fprintf(stderr
, "%s: warning: input too long\n", progname
);
228 fprintf(stderr
, "%s: warning: EOF before newline\n", progname
);
233 /* Trim trailing whitespace */
234 while (n
> 0 && isspace(_UC(buf
[n
-1])))
241 * Hex decoder that tolerates optional whitespace
243 static ossl_ssize_t
hexdecode(const char *in
, void *result
)
245 unsigned char **out
= (unsigned char **)result
;
246 unsigned char *ret
= OPENSSL_malloc(strlen(in
)/2);
247 unsigned char *cp
= ret
;
254 for (byte
= 0; *in
; ++in
) {
257 if (isspace(_UC(*in
)))
259 c
= tolower(_UC(*in
));
260 if ('0' <= c
&& c
<= '9') {
262 } else if ('a' <= c
&& c
<= 'f') {
263 byte
|= c
- 'a' + 10;
268 if ((nibble
^= 1) == 0) {
280 return cp
- (*out
= ret
);
283 static ossl_ssize_t
checked_uint8(const char *in
, void *out
)
285 uint8_t *result
= (uint8_t *)out
;
292 v
= strtol(cp
, &endp
, 10);
295 if (((v
== LONG_MIN
|| v
== LONG_MAX
) && e
== ERANGE
) ||
296 endp
== cp
|| !isspace(_UC(*endp
)) ||
297 v
!= (*(uint8_t *)result
= (uint8_t) v
)) {
300 for (cp
= endp
; isspace(_UC(*cp
)); ++cp
)
308 ossl_ssize_t (*parser
)(const char *, void *);
311 static int tlsa_import_rr(SSL
*ssl
, const char *rrdata
)
313 static uint8_t usage
;
314 static uint8_t selector
;
315 static uint8_t mtype
;
316 static unsigned char *data
= NULL
;
317 static struct tlsa_field tlsa_fields
[] = {
318 { &usage
, "usage", checked_uint8
},
319 { &selector
, "selector", checked_uint8
},
320 { &mtype
, "mtype", checked_uint8
},
321 { &data
, "data", hexdecode
},
325 struct tlsa_field
*f
;
326 const char *cp
= rrdata
;
327 ossl_ssize_t len
= 0;
329 for (f
= tlsa_fields
; f
->var
; ++f
) {
330 if ((len
= f
->parser(cp
+= len
, f
->var
)) <= 0) {
331 fprintf(stderr
, "%s: warning: bad TLSA %s field in: %s\n",
332 progname
, f
->name
, rrdata
);
336 ret
= SSL_dane_tlsa_add(ssl
, usage
, selector
, mtype
, data
, len
);
341 fprintf(stderr
, "%s: warning: unusable TLSA rrdata: %s\n",
346 fprintf(stderr
, "%s: warning: error loading TLSA rrdata: %s\n",
353 static int allws(const char *cp
)
356 if (!isspace(_UC(*cp
++)))
361 static int test_tlsafile(SSL_CTX
*ctx
, const char *basename
,
362 BIO
*f
, const char *path
)
369 while (ret
> 0 && (line
= read_to_eol(f
)) != NULL
) {
370 STACK_OF(X509
) *chain
;
381 if (*line
== '\0' || *line
== '#')
385 if (sscanf(line
, "%d %d %d %d%n", &ntlsa
, &ncert
, &want
, &want_depth
, &off
) != 4
386 || !allws(line
+ off
)) {
387 fprintf(stderr
, "Expected tlsa count, cert count and result"
388 " at test %d of %s\n", testno
, path
);
392 if ((ssl
= SSL_new(ctx
)) == NULL
)
394 SSL_set_connect_state(ssl
);
395 if (SSL_dane_enable(ssl
, basename
) <= 0) {
400 for (i
= 0; i
< ntlsa
; ++i
) {
401 if ((line
= read_to_eol(f
)) == NULL
|| !tlsa_import_rr(ssl
, line
)) {
407 /* Don't report old news */
409 chain
= load_chain(f
, ncert
);
415 ok
= verify_chain(ssl
, chain
);
416 sk_X509_pop_free(chain
, X509_free
);
417 err
= SSL_get_verify_result(ssl
);
419 * Peek under the hood, normally TLSA match data is hidden when
420 * verification fails, we can obtain any suppressed data by setting the
421 * verification result to X509_V_OK before looking.
423 SSL_set_verify_result(ssl
, X509_V_OK
);
424 mdpth
= SSL_get0_dane_authority(ssl
, NULL
, NULL
);
425 /* Not needed any more, but lead by example and put the error back. */
426 SSL_set_verify_result(ssl
, err
);
431 fprintf(stderr
, "verify_chain internal error in %s test %d\n",
436 if (err
!= want
|| (want
== 0 && !ok
)) {
439 if (want
== X509_V_OK
)
440 fprintf(stderr
, "Verification failure in %s test %d: %d: %s\n",
441 path
, testno
, err
, X509_verify_cert_error_string(err
));
443 fprintf(stderr
, "Unexpected error in %s test %d: %d: wanted %d\n",
444 path
, testno
, err
, want
);
446 fprintf(stderr
, "Verification failure in %s test %d: ok=0\n",
452 if (mdpth
!= want_depth
) {
454 fprintf(stderr
, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
455 path
, testno
, want_depth
, mdpth
);
457 fprintf(stderr
, "%s: test %d successful\n", path
, testno
);
464 int main(int argc
, char *argv
[])
469 const char *basedomain
;
471 const char *tlsafile
;
480 basedomain
= argv
[1];
484 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
486 p
= getenv("OPENSSL_DEBUG_MEMORY");
487 if (p
!= NULL
&& strcmp(p
, "on") == 0)
488 CRYPTO_set_mem_debug(1);
489 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
491 f
= BIO_new_file(tlsafile
, "r");
493 fprintf(stderr
, "%s: Error opening tlsa record file: '%s': %s\n",
494 progname
, tlsafile
, strerror(errno
));
498 ctx
= SSL_CTX_new(TLS_client_method());
499 if (SSL_CTX_dane_enable(ctx
) <= 0) {
503 if (!SSL_CTX_load_verify_locations(ctx
, CAfile
, NULL
)) {
507 if ((SSL_CTX_dane_mtype_set(ctx
, EVP_sha512(), 2, 1)) <= 0) {
511 if ((SSL_CTX_dane_mtype_set(ctx
, EVP_sha256(), 1, 2)) <= 0) {
516 if (test_tlsafile(ctx
, basedomain
, f
, tlsafile
) <= 0) {
528 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
529 if (CRYPTO_mem_leaks(bio_err
) <= 0)
536 #include <internal/dane.h>
538 static void store_ctx_dane_init(X509_STORE_CTX
*store_ctx
, SSL
*ssl
)
540 X509_STORE_CTX_set0_dane(store_ctx
, SSL_get0_dane(ssl
));