2 * Verify or create TLS authentication with DANE (RFC6698)
6 * See the file LICENSE for the license.
10 #include <ldns/config.h>
12 #include <ldns/ldns.h>
13 #include <ldns/dane.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
22 #include <openssl/ssl.h>
23 #include <openssl/err.h>
24 #include <openssl/x509v3.h>
28 ldns_dane_create_tlsa_owner(ldns_rdf
** tlsa_owner
, const ldns_rdf
* name
,
29 uint16_t port
, ldns_dane_transport transport
)
31 char buf
[LDNS_MAX_DOMAINLEN
];
34 assert(tlsa_owner
!= NULL
);
36 assert(ldns_rdf_get_type(name
) == LDNS_RDF_TYPE_DNAME
);
38 s
= (size_t)snprintf(buf
, LDNS_MAX_DOMAINLEN
, "X_%d", (int)port
);
39 buf
[0] = (char)(s
- 1);
42 case LDNS_DANE_TRANSPORT_TCP
:
43 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\004_tcp");
46 case LDNS_DANE_TRANSPORT_UDP
:
47 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\004_udp");
50 case LDNS_DANE_TRANSPORT_SCTP
:
51 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\005_sctp");
55 return LDNS_STATUS_DANE_UNKNOWN_TRANSPORT
;
57 if (s
+ ldns_rdf_size(name
) > LDNS_MAX_DOMAINLEN
) {
58 return LDNS_STATUS_DOMAINNAME_OVERFLOW
;
60 memcpy(buf
+ s
, ldns_rdf_data(name
), ldns_rdf_size(name
));
61 *tlsa_owner
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME
,
62 s
+ ldns_rdf_size(name
), buf
);
63 if (*tlsa_owner
== NULL
) {
64 return LDNS_STATUS_MEM_ERR
;
66 return LDNS_STATUS_OK
;
72 ldns_dane_cert2rdf(ldns_rdf
** rdf
, X509
* cert
,
73 ldns_tlsa_selector selector
,
74 ldns_tlsa_matching_type matching_type
)
76 unsigned char* buf
= NULL
;
82 unsigned char* digest
;
88 case LDNS_TLSA_SELECTOR_FULL_CERTIFICATE
:
90 len
= (size_t)i2d_X509(cert
, &buf
);
93 case LDNS_TLSA_SELECTOR_SUBJECTPUBLICKEYINFO
:
96 xpubkey
= X509_get_X509_PUBKEY(cert
);
99 return LDNS_STATUS_SSL_ERR
;
101 epubkey
= X509_PUBKEY_get(xpubkey
);
103 return LDNS_STATUS_SSL_ERR
;
105 len
= (size_t)i2d_PUBKEY(epubkey
, &buf
);
109 return LDNS_STATUS_DANE_UNKNOWN_SELECTOR
;
112 switch(matching_type
) {
113 case LDNS_TLSA_MATCHING_TYPE_NO_HASH_USED
:
115 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, len
, buf
);
117 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
120 case LDNS_TLSA_MATCHING_TYPE_SHA256
:
122 digest
= LDNS_XMALLOC(unsigned char, SHA256_DIGEST_LENGTH
);
123 if (digest
== NULL
) {
125 return LDNS_STATUS_MEM_ERR
;
127 (void) ldns_sha256(buf
, (unsigned int)len
, digest
);
128 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, SHA256_DIGEST_LENGTH
,
132 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
135 case LDNS_TLSA_MATCHING_TYPE_SHA512
:
137 digest
= LDNS_XMALLOC(unsigned char, SHA512_DIGEST_LENGTH
);
138 if (digest
== NULL
) {
140 return LDNS_STATUS_MEM_ERR
;
142 (void) ldns_sha512(buf
, (unsigned int)len
, digest
);
143 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, SHA512_DIGEST_LENGTH
,
147 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
152 return LDNS_STATUS_DANE_UNKNOWN_MATCHING_TYPE
;
157 /* Ordinary PKIX validation of cert (with extra_certs to help)
158 * against the CA's in store
161 ldns_dane_pkix_validate(X509
* cert
, STACK_OF(X509
)* extra_certs
,
164 X509_STORE_CTX
* vrfy_ctx
;
168 return LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
170 vrfy_ctx
= X509_STORE_CTX_new();
173 return LDNS_STATUS_SSL_ERR
;
175 } else if (X509_STORE_CTX_init(vrfy_ctx
, store
,
176 cert
, extra_certs
) != 1) {
177 s
= LDNS_STATUS_SSL_ERR
;
179 } else if (X509_verify_cert(vrfy_ctx
) == 1) {
184 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
186 X509_STORE_CTX_free(vrfy_ctx
);
191 /* Orinary PKIX validation of cert (with extra_certs to help)
192 * against the CA's in store, but also return the validation chain.
195 ldns_dane_pkix_validate_and_get_chain(STACK_OF(X509
)** chain
, X509
* cert
,
196 STACK_OF(X509
)* extra_certs
, X509_STORE
* store
)
199 X509_STORE
* empty_store
= NULL
;
200 X509_STORE_CTX
* vrfy_ctx
;
202 assert(chain
!= NULL
);
205 store
= empty_store
= X509_STORE_new();
207 s
= LDNS_STATUS_SSL_ERR
;
208 vrfy_ctx
= X509_STORE_CTX_new();
211 goto exit_free_empty_store
;
213 } else if (X509_STORE_CTX_init(vrfy_ctx
, store
,
214 cert
, extra_certs
) != 1) {
215 goto exit_free_vrfy_ctx
;
217 } else if (X509_verify_cert(vrfy_ctx
) == 1) {
222 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
224 *chain
= X509_STORE_CTX_get1_chain(vrfy_ctx
);
226 s
= LDNS_STATUS_SSL_ERR
;
230 X509_STORE_CTX_free(vrfy_ctx
);
232 exit_free_empty_store
:
234 X509_STORE_free(empty_store
);
240 /* Return the validation chain that can be build out of cert, with extra_certs.
243 ldns_dane_pkix_get_chain(STACK_OF(X509
)** chain
,
244 X509
* cert
, STACK_OF(X509
)* extra_certs
)
247 X509_STORE
* empty_store
= NULL
;
248 X509_STORE_CTX
* vrfy_ctx
;
250 assert(chain
!= NULL
);
252 empty_store
= X509_STORE_new();
253 s
= LDNS_STATUS_SSL_ERR
;
254 vrfy_ctx
= X509_STORE_CTX_new();
257 goto exit_free_empty_store
;
259 } else if (X509_STORE_CTX_init(vrfy_ctx
, empty_store
,
260 cert
, extra_certs
) != 1) {
261 goto exit_free_vrfy_ctx
;
263 (void) X509_verify_cert(vrfy_ctx
);
264 *chain
= X509_STORE_CTX_get1_chain(vrfy_ctx
);
266 s
= LDNS_STATUS_SSL_ERR
;
271 X509_STORE_CTX_free(vrfy_ctx
);
273 exit_free_empty_store
:
274 X509_STORE_free(empty_store
);
279 /* Pop n+1 certs and return the last popped.
282 ldns_dane_get_nth_cert_from_validation_chain(
283 X509
** cert
, STACK_OF(X509
)* chain
, int n
, bool ca
)
285 if (n
>= sk_X509_num(chain
) || n
< 0) {
286 return LDNS_STATUS_DANE_OFFSET_OUT_OF_RANGE
;
288 *cert
= sk_X509_pop(chain
);
291 *cert
= sk_X509_pop(chain
);
293 if (ca
&& ! X509_check_ca(*cert
)) {
294 return LDNS_STATUS_DANE_NON_CA_CERTIFICATE
;
296 return LDNS_STATUS_OK
;
300 /* Create validation chain with cert and extra_certs and returns the last
301 * self-signed (if present).
304 ldns_dane_pkix_get_last_self_signed(X509
** out_cert
,
305 X509
* cert
, STACK_OF(X509
)* extra_certs
)
308 X509_STORE
* empty_store
= NULL
;
309 X509_STORE_CTX
* vrfy_ctx
;
311 assert(out_cert
!= NULL
);
313 empty_store
= X509_STORE_new();
314 s
= LDNS_STATUS_SSL_ERR
;
315 vrfy_ctx
= X509_STORE_CTX_new();
317 goto exit_free_empty_store
;
319 } else if (X509_STORE_CTX_init(vrfy_ctx
, empty_store
,
320 cert
, extra_certs
) != 1) {
321 goto exit_free_vrfy_ctx
;
324 (void) X509_verify_cert(vrfy_ctx
);
325 if (vrfy_ctx
->error
== X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
||
326 vrfy_ctx
->error
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
){
328 *out_cert
= X509_STORE_CTX_get_current_cert( vrfy_ctx
);
331 s
= LDNS_STATUS_DANE_PKIX_NO_SELF_SIGNED_TRUST_ANCHOR
;
334 X509_STORE_CTX_free(vrfy_ctx
);
336 exit_free_empty_store
:
337 X509_STORE_free(empty_store
);
343 ldns_dane_select_certificate(X509
** selected_cert
,
344 X509
* cert
, STACK_OF(X509
)* extra_certs
,
345 X509_STORE
* pkix_validation_store
,
346 ldns_tlsa_certificate_usage cert_usage
, int offset
)
349 STACK_OF(X509
)* pkix_validation_chain
= NULL
;
351 assert(selected_cert
!= NULL
);
352 assert(cert
!= NULL
);
354 /* With PKIX validation explicitely turned off (pkix_validation_store
355 * == NULL), treat the "CA constraint" and "Service certificate
356 * constraint" the same as "Trust anchor assertion" and "Domain issued
357 * certificate" respectively.
359 if (pkix_validation_store
== NULL
) {
360 switch (cert_usage
) {
362 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
364 cert_usage
= LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
;
367 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
369 cert_usage
= LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
;
377 /* Now what to do with each Certificate usage...
379 switch (cert_usage
) {
381 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
383 s
= ldns_dane_pkix_validate_and_get_chain(
384 &pkix_validation_chain
,
386 pkix_validation_store
);
387 if (! pkix_validation_chain
) {
390 if (s
== LDNS_STATUS_OK
) {
394 s
= ldns_dane_get_nth_cert_from_validation_chain(
395 selected_cert
, pkix_validation_chain
,
398 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
403 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
405 *selected_cert
= cert
;
406 return ldns_dane_pkix_validate(cert
, extra_certs
,
407 pkix_validation_store
);
411 case LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
:
414 s
= ldns_dane_pkix_get_last_self_signed(
415 selected_cert
, cert
, extra_certs
);
418 s
= ldns_dane_pkix_get_chain(
419 &pkix_validation_chain
,
421 if (s
== LDNS_STATUS_OK
) {
423 ldns_dane_get_nth_cert_from_validation_chain(
424 selected_cert
, pkix_validation_chain
,
426 } else if (! pkix_validation_chain
) {
429 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
435 case LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
:
437 *selected_cert
= cert
;
438 return LDNS_STATUS_OK
;
442 return LDNS_STATUS_DANE_UNKNOWN_CERTIFICATE_USAGE
;
449 ldns_dane_create_tlsa_rr(ldns_rr
** tlsa
,
450 ldns_tlsa_certificate_usage certificate_usage
,
451 ldns_tlsa_selector selector
,
452 ldns_tlsa_matching_type matching_type
,
458 assert(tlsa
!= NULL
);
459 assert(cert
!= NULL
);
462 *tlsa
= ldns_rr_new_frm_type(LDNS_RR_TYPE_TLSA
);
464 return LDNS_STATUS_MEM_ERR
;
467 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
,
468 (uint8_t)certificate_usage
);
472 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 0);
474 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, (uint8_t)selector
);
478 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 1);
480 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, (uint8_t)matching_type
);
484 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 2);
486 s
= ldns_dane_cert2rdf(&rdf
, cert
, selector
, matching_type
);
487 if (s
== LDNS_STATUS_OK
) {
488 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 3);
489 return LDNS_STATUS_OK
;
498 return LDNS_STATUS_MEM_ERR
;
502 /* Return tlsas that actually are TLSA resource records with known values
503 * for the Certificate usage, Selector and Matching type rdata fields.
506 ldns_dane_filter_unusable_records(const ldns_rr_list
* tlsas
)
509 ldns_rr_list
* r
= ldns_rr_list_new();
515 for (i
= 0; i
< ldns_rr_list_rr_count(tlsas
); i
++) {
516 tlsa_rr
= ldns_rr_list_rr(tlsas
, i
);
517 if (ldns_rr_get_type(tlsa_rr
) == LDNS_RR_TYPE_TLSA
&&
518 ldns_rr_rd_count(tlsa_rr
) == 4 &&
519 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 0)) <= 3 &&
520 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 1)) <= 1 &&
521 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 2)) <= 2) {
523 if (! ldns_rr_list_push_rr(r
, tlsa_rr
)) {
524 ldns_rr_list_free(r
);
533 /* Return whether cert/selector/matching_type matches data.
536 ldns_dane_match_cert_with_data(X509
* cert
, ldns_tlsa_selector selector
,
537 ldns_tlsa_matching_type matching_type
, ldns_rdf
* data
)
540 ldns_rdf
* match_data
;
542 s
= ldns_dane_cert2rdf(&match_data
, cert
, selector
, matching_type
);
543 if (s
== LDNS_STATUS_OK
) {
544 if (ldns_rdf_compare(data
, match_data
) != 0) {
545 s
= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
;
547 ldns_rdf_free(match_data
);
553 /* Return whether any certificate from the chain with selector/matching_type
555 * ca should be true if the certificate has to be a CA certificate too.
558 ldns_dane_match_any_cert_with_data(STACK_OF(X509
)* chain
,
559 ldns_tlsa_selector selector
,
560 ldns_tlsa_matching_type matching_type
,
561 ldns_rdf
* data
, bool ca
)
563 ldns_status s
= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
;
567 n
= (size_t)sk_X509_num(chain
);
568 for (i
= 0; i
< n
; i
++) {
569 cert
= sk_X509_pop(chain
);
571 s
= LDNS_STATUS_SSL_ERR
;
574 s
= ldns_dane_match_cert_with_data(cert
,
575 selector
, matching_type
, data
);
576 if (ca
&& s
== LDNS_STATUS_OK
&& ! X509_check_ca(cert
)) {
577 s
= LDNS_STATUS_DANE_NON_CA_CERTIFICATE
;
580 if (s
!= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
) {
583 /* when s == LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH,
584 * try to match the next certificate
592 ldns_dane_verify_rr(const ldns_rr
* tlsa_rr
,
593 X509
* cert
, STACK_OF(X509
)* extra_certs
,
594 X509_STORE
* pkix_validation_store
)
598 STACK_OF(X509
)* pkix_validation_chain
= NULL
;
600 ldns_tlsa_certificate_usage cert_usage
;
601 ldns_tlsa_selector selector
;
602 ldns_tlsa_matching_type matching_type
;
606 /* No TLSA, so regular PKIX validation
608 return ldns_dane_pkix_validate(cert
, extra_certs
,
609 pkix_validation_store
);
611 cert_usage
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 0));
612 selector
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 1));
613 matching_type
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 2));
614 data
= ldns_rr_rdf(tlsa_rr
, 3) ;
616 switch (cert_usage
) {
617 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
618 s
= ldns_dane_pkix_validate_and_get_chain(
619 &pkix_validation_chain
,
621 pkix_validation_store
);
622 if (! pkix_validation_chain
) {
625 if (s
== LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
) {
627 * NO PKIX validation. We still try to match *any*
628 * certificate from the chain, so we return
629 * TLSA errors over PKIX errors.
631 * i.e. When the TLSA matches no certificate, we return
632 * TLSA_DID_NOT_MATCH and not PKIX_DID_NOT_VALIDATE
634 s
= ldns_dane_match_any_cert_with_data(
635 pkix_validation_chain
,
636 selector
, matching_type
, data
, true);
638 if (s
== LDNS_STATUS_OK
) {
639 /* A TLSA record did match a cert from the
640 * chain, thus the error is failed PKIX
643 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
646 } else if (s
== LDNS_STATUS_OK
) {
647 /* PKIX validated, does the TLSA match too? */
649 s
= ldns_dane_match_any_cert_with_data(
650 pkix_validation_chain
,
651 selector
, matching_type
, data
, true);
653 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
657 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
658 s
= ldns_dane_match_cert_with_data(cert
,
659 selector
, matching_type
, data
);
661 if (s
== LDNS_STATUS_OK
) {
662 return ldns_dane_pkix_validate(cert
, extra_certs
,
663 pkix_validation_store
);
668 case LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
:
669 s
= ldns_dane_pkix_get_chain(&pkix_validation_chain
,
672 if (s
== LDNS_STATUS_OK
) {
673 s
= ldns_dane_match_any_cert_with_data(
674 pkix_validation_chain
,
675 selector
, matching_type
, data
, false);
677 } else if (! pkix_validation_chain
) {
680 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
684 case LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
:
685 return ldns_dane_match_cert_with_data(cert
,
686 selector
, matching_type
, data
);
692 return LDNS_STATUS_DANE_UNKNOWN_CERTIFICATE_USAGE
;
697 ldns_dane_verify(ldns_rr_list
* tlsas
,
698 X509
* cert
, STACK_OF(X509
)* extra_certs
,
699 X509_STORE
* pkix_validation_store
)
703 ldns_status s
= LDNS_STATUS_OK
, ps
;
705 assert(cert
!= NULL
);
707 if (tlsas
&& ldns_rr_list_rr_count(tlsas
) > 0) {
708 tlsas
= ldns_dane_filter_unusable_records(tlsas
);
710 return LDNS_STATUS_MEM_ERR
;
713 if (! tlsas
|| ldns_rr_list_rr_count(tlsas
) == 0) {
714 /* No TLSA's, so regular PKIX validation
716 return ldns_dane_pkix_validate(cert
, extra_certs
,
717 pkix_validation_store
);
719 for (i
= 0; i
< ldns_rr_list_rr_count(tlsas
); i
++) {
720 tlsa_rr
= ldns_rr_list_rr(tlsas
, i
);
722 s
= ldns_dane_verify_rr(tlsa_rr
, cert
, extra_certs
,
723 pkix_validation_store
);
725 if (s
!= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
&&
726 s
!= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
) {
728 /* which would be LDNS_STATUS_OK (match)
729 * or some fatal error preventing use from
730 * trying the next TLSA record.
734 s
= (s
> ps
? s
: ps
); /* prefer PKIX_DID_NOT_VALIDATE
735 * over TLSA_DID_NOT_MATCH
738 ldns_rr_list_free(tlsas
);
742 #endif /* HAVE_SSL */