2 * Verify or create TLS authentication with DANE (RFC6698)
6 * See the file LICENSE for the license.
10 #include <ldns/config.h>
13 #include <ldns/ldns.h>
14 #include <ldns/dane.h>
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 #include <sys/socket.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #include <openssl/x509v3.h>
33 ldns_dane_create_tlsa_owner(ldns_rdf
** tlsa_owner
, const ldns_rdf
* name
,
34 uint16_t port
, ldns_dane_transport transport
)
36 char buf
[LDNS_MAX_DOMAINLEN
];
39 assert(tlsa_owner
!= NULL
);
41 assert(ldns_rdf_get_type(name
) == LDNS_RDF_TYPE_DNAME
);
43 s
= (size_t)snprintf(buf
, LDNS_MAX_DOMAINLEN
, "X_%d", (int)port
);
44 buf
[0] = (char)(s
- 1);
47 case LDNS_DANE_TRANSPORT_TCP
:
48 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\004_tcp");
51 case LDNS_DANE_TRANSPORT_UDP
:
52 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\004_udp");
55 case LDNS_DANE_TRANSPORT_SCTP
:
56 s
+= snprintf(buf
+ s
, LDNS_MAX_DOMAINLEN
- s
, "\005_sctp");
60 return LDNS_STATUS_DANE_UNKNOWN_TRANSPORT
;
62 if (s
+ ldns_rdf_size(name
) > LDNS_MAX_DOMAINLEN
) {
63 return LDNS_STATUS_DOMAINNAME_OVERFLOW
;
65 memcpy(buf
+ s
, ldns_rdf_data(name
), ldns_rdf_size(name
));
66 *tlsa_owner
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME
,
67 s
+ ldns_rdf_size(name
), buf
);
68 if (*tlsa_owner
== NULL
) {
69 return LDNS_STATUS_MEM_ERR
;
71 return LDNS_STATUS_OK
;
77 ldns_dane_cert2rdf(ldns_rdf
** rdf
, X509
* cert
,
78 ldns_tlsa_selector selector
,
79 ldns_tlsa_matching_type matching_type
)
81 unsigned char* buf
= NULL
;
87 unsigned char* digest
;
93 case LDNS_TLSA_SELECTOR_FULL_CERTIFICATE
:
95 len
= (size_t)i2d_X509(cert
, &buf
);
98 case LDNS_TLSA_SELECTOR_SUBJECTPUBLICKEYINFO
:
101 xpubkey
= X509_get_X509_PUBKEY(cert
);
104 return LDNS_STATUS_SSL_ERR
;
106 epubkey
= X509_PUBKEY_get(xpubkey
);
108 return LDNS_STATUS_SSL_ERR
;
110 len
= (size_t)i2d_PUBKEY(epubkey
, &buf
);
114 return LDNS_STATUS_DANE_UNKNOWN_SELECTOR
;
117 switch(matching_type
) {
118 case LDNS_TLSA_MATCHING_TYPE_NO_HASH_USED
:
120 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, len
, buf
);
122 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
125 case LDNS_TLSA_MATCHING_TYPE_SHA256
:
127 digest
= LDNS_XMALLOC(unsigned char, LDNS_SHA256_DIGEST_LENGTH
);
128 if (digest
== NULL
) {
130 return LDNS_STATUS_MEM_ERR
;
132 (void) ldns_sha256(buf
, (unsigned int)len
, digest
);
133 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, LDNS_SHA256_DIGEST_LENGTH
,
137 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
140 case LDNS_TLSA_MATCHING_TYPE_SHA512
:
142 digest
= LDNS_XMALLOC(unsigned char, LDNS_SHA512_DIGEST_LENGTH
);
143 if (digest
== NULL
) {
145 return LDNS_STATUS_MEM_ERR
;
147 (void) ldns_sha512(buf
, (unsigned int)len
, digest
);
148 *rdf
= ldns_rdf_new(LDNS_RDF_TYPE_HEX
, LDNS_SHA512_DIGEST_LENGTH
,
152 return *rdf
? LDNS_STATUS_OK
: LDNS_STATUS_MEM_ERR
;
157 return LDNS_STATUS_DANE_UNKNOWN_MATCHING_TYPE
;
162 /* Ordinary PKIX validation of cert (with extra_certs to help)
163 * against the CA's in store
166 ldns_dane_pkix_validate(X509
* cert
, STACK_OF(X509
)* extra_certs
,
169 X509_STORE_CTX
* vrfy_ctx
;
173 return LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
175 vrfy_ctx
= X509_STORE_CTX_new();
178 return LDNS_STATUS_SSL_ERR
;
180 } else if (X509_STORE_CTX_init(vrfy_ctx
, store
,
181 cert
, extra_certs
) != 1) {
182 s
= LDNS_STATUS_SSL_ERR
;
184 } else if (X509_verify_cert(vrfy_ctx
) == 1) {
189 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
191 X509_STORE_CTX_free(vrfy_ctx
);
196 /* Orinary PKIX validation of cert (with extra_certs to help)
197 * against the CA's in store, but also return the validation chain.
200 ldns_dane_pkix_validate_and_get_chain(STACK_OF(X509
)** chain
, X509
* cert
,
201 STACK_OF(X509
)* extra_certs
, X509_STORE
* store
)
204 X509_STORE
* empty_store
= NULL
;
205 X509_STORE_CTX
* vrfy_ctx
;
207 assert(chain
!= NULL
);
210 store
= empty_store
= X509_STORE_new();
212 s
= LDNS_STATUS_SSL_ERR
;
213 vrfy_ctx
= X509_STORE_CTX_new();
216 goto exit_free_empty_store
;
218 } else if (X509_STORE_CTX_init(vrfy_ctx
, store
,
219 cert
, extra_certs
) != 1) {
220 goto exit_free_vrfy_ctx
;
222 } else if (X509_verify_cert(vrfy_ctx
) == 1) {
227 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
229 *chain
= X509_STORE_CTX_get1_chain(vrfy_ctx
);
231 s
= LDNS_STATUS_SSL_ERR
;
235 X509_STORE_CTX_free(vrfy_ctx
);
237 exit_free_empty_store
:
239 X509_STORE_free(empty_store
);
245 /* Return the validation chain that can be build out of cert, with extra_certs.
248 ldns_dane_pkix_get_chain(STACK_OF(X509
)** chain
,
249 X509
* cert
, STACK_OF(X509
)* extra_certs
)
252 X509_STORE
* empty_store
= NULL
;
253 X509_STORE_CTX
* vrfy_ctx
;
255 assert(chain
!= NULL
);
257 empty_store
= X509_STORE_new();
258 s
= LDNS_STATUS_SSL_ERR
;
259 vrfy_ctx
= X509_STORE_CTX_new();
262 goto exit_free_empty_store
;
264 } else if (X509_STORE_CTX_init(vrfy_ctx
, empty_store
,
265 cert
, extra_certs
) != 1) {
266 goto exit_free_vrfy_ctx
;
268 (void) X509_verify_cert(vrfy_ctx
);
269 *chain
= X509_STORE_CTX_get1_chain(vrfy_ctx
);
271 s
= LDNS_STATUS_SSL_ERR
;
276 X509_STORE_CTX_free(vrfy_ctx
);
278 exit_free_empty_store
:
279 X509_STORE_free(empty_store
);
284 /* Pop n+1 certs and return the last popped.
287 ldns_dane_get_nth_cert_from_validation_chain(
288 X509
** cert
, STACK_OF(X509
)* chain
, int n
, bool ca
)
290 if (n
>= sk_X509_num(chain
) || n
< 0) {
291 return LDNS_STATUS_DANE_OFFSET_OUT_OF_RANGE
;
293 *cert
= sk_X509_pop(chain
);
296 *cert
= sk_X509_pop(chain
);
298 if (ca
&& ! X509_check_ca(*cert
)) {
299 return LDNS_STATUS_DANE_NON_CA_CERTIFICATE
;
301 return LDNS_STATUS_OK
;
305 /* Create validation chain with cert and extra_certs and returns the last
306 * self-signed (if present).
309 ldns_dane_pkix_get_last_self_signed(X509
** out_cert
,
310 X509
* cert
, STACK_OF(X509
)* extra_certs
)
313 X509_STORE
* empty_store
= NULL
;
314 X509_STORE_CTX
* vrfy_ctx
;
316 assert(out_cert
!= NULL
);
318 empty_store
= X509_STORE_new();
319 s
= LDNS_STATUS_SSL_ERR
;
320 vrfy_ctx
= X509_STORE_CTX_new();
322 goto exit_free_empty_store
;
324 } else if (X509_STORE_CTX_init(vrfy_ctx
, empty_store
,
325 cert
, extra_certs
) != 1) {
326 goto exit_free_vrfy_ctx
;
329 (void) X509_verify_cert(vrfy_ctx
);
330 if (vrfy_ctx
->error
== X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
||
331 vrfy_ctx
->error
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
){
333 *out_cert
= X509_STORE_CTX_get_current_cert( vrfy_ctx
);
336 s
= LDNS_STATUS_DANE_PKIX_NO_SELF_SIGNED_TRUST_ANCHOR
;
339 X509_STORE_CTX_free(vrfy_ctx
);
341 exit_free_empty_store
:
342 X509_STORE_free(empty_store
);
348 ldns_dane_select_certificate(X509
** selected_cert
,
349 X509
* cert
, STACK_OF(X509
)* extra_certs
,
350 X509_STORE
* pkix_validation_store
,
351 ldns_tlsa_certificate_usage cert_usage
, int offset
)
354 STACK_OF(X509
)* pkix_validation_chain
= NULL
;
356 assert(selected_cert
!= NULL
);
357 assert(cert
!= NULL
);
359 /* With PKIX validation explicitely turned off (pkix_validation_store
360 * == NULL), treat the "CA constraint" and "Service certificate
361 * constraint" the same as "Trust anchor assertion" and "Domain issued
362 * certificate" respectively.
364 if (pkix_validation_store
== NULL
) {
365 switch (cert_usage
) {
367 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
369 cert_usage
= LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
;
372 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
374 cert_usage
= LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
;
382 /* Now what to do with each Certificate usage...
384 switch (cert_usage
) {
386 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
388 s
= ldns_dane_pkix_validate_and_get_chain(
389 &pkix_validation_chain
,
391 pkix_validation_store
);
392 if (! pkix_validation_chain
) {
395 if (s
== LDNS_STATUS_OK
) {
399 s
= ldns_dane_get_nth_cert_from_validation_chain(
400 selected_cert
, pkix_validation_chain
,
403 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
408 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
410 *selected_cert
= cert
;
411 return ldns_dane_pkix_validate(cert
, extra_certs
,
412 pkix_validation_store
);
416 case LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
:
419 s
= ldns_dane_pkix_get_last_self_signed(
420 selected_cert
, cert
, extra_certs
);
423 s
= ldns_dane_pkix_get_chain(
424 &pkix_validation_chain
,
426 if (s
== LDNS_STATUS_OK
) {
428 ldns_dane_get_nth_cert_from_validation_chain(
429 selected_cert
, pkix_validation_chain
,
431 } else if (! pkix_validation_chain
) {
434 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
440 case LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
:
442 *selected_cert
= cert
;
443 return LDNS_STATUS_OK
;
447 return LDNS_STATUS_DANE_UNKNOWN_CERTIFICATE_USAGE
;
454 ldns_dane_create_tlsa_rr(ldns_rr
** tlsa
,
455 ldns_tlsa_certificate_usage certificate_usage
,
456 ldns_tlsa_selector selector
,
457 ldns_tlsa_matching_type matching_type
,
463 assert(tlsa
!= NULL
);
464 assert(cert
!= NULL
);
467 *tlsa
= ldns_rr_new_frm_type(LDNS_RR_TYPE_TLSA
);
469 return LDNS_STATUS_MEM_ERR
;
472 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
,
473 (uint8_t)certificate_usage
);
477 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 0);
479 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, (uint8_t)selector
);
483 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 1);
485 rdf
= ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, (uint8_t)matching_type
);
489 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 2);
491 s
= ldns_dane_cert2rdf(&rdf
, cert
, selector
, matching_type
);
492 if (s
== LDNS_STATUS_OK
) {
493 (void) ldns_rr_set_rdf(*tlsa
, rdf
, 3);
494 return LDNS_STATUS_OK
;
503 return LDNS_STATUS_MEM_ERR
;
507 /* Return tlsas that actually are TLSA resource records with known values
508 * for the Certificate usage, Selector and Matching type rdata fields.
511 ldns_dane_filter_unusable_records(const ldns_rr_list
* tlsas
)
514 ldns_rr_list
* r
= ldns_rr_list_new();
520 for (i
= 0; i
< ldns_rr_list_rr_count(tlsas
); i
++) {
521 tlsa_rr
= ldns_rr_list_rr(tlsas
, i
);
522 if (ldns_rr_get_type(tlsa_rr
) == LDNS_RR_TYPE_TLSA
&&
523 ldns_rr_rd_count(tlsa_rr
) == 4 &&
524 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 0)) <= 3 &&
525 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 1)) <= 1 &&
526 ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 2)) <= 2) {
528 if (! ldns_rr_list_push_rr(r
, tlsa_rr
)) {
529 ldns_rr_list_free(r
);
538 /* Return whether cert/selector/matching_type matches data.
541 ldns_dane_match_cert_with_data(X509
* cert
, ldns_tlsa_selector selector
,
542 ldns_tlsa_matching_type matching_type
, ldns_rdf
* data
)
545 ldns_rdf
* match_data
;
547 s
= ldns_dane_cert2rdf(&match_data
, cert
, selector
, matching_type
);
548 if (s
== LDNS_STATUS_OK
) {
549 if (ldns_rdf_compare(data
, match_data
) != 0) {
550 s
= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
;
552 ldns_rdf_free(match_data
);
558 /* Return whether any certificate from the chain with selector/matching_type
560 * ca should be true if the certificate has to be a CA certificate too.
563 ldns_dane_match_any_cert_with_data(STACK_OF(X509
)* chain
,
564 ldns_tlsa_selector selector
,
565 ldns_tlsa_matching_type matching_type
,
566 ldns_rdf
* data
, bool ca
)
568 ldns_status s
= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
;
572 n
= (size_t)sk_X509_num(chain
);
573 for (i
= 0; i
< n
; i
++) {
574 cert
= sk_X509_pop(chain
);
576 s
= LDNS_STATUS_SSL_ERR
;
579 s
= ldns_dane_match_cert_with_data(cert
,
580 selector
, matching_type
, data
);
581 if (ca
&& s
== LDNS_STATUS_OK
&& ! X509_check_ca(cert
)) {
582 s
= LDNS_STATUS_DANE_NON_CA_CERTIFICATE
;
585 if (s
!= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
) {
588 /* when s == LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH,
589 * try to match the next certificate
597 ldns_dane_verify_rr(const ldns_rr
* tlsa_rr
,
598 X509
* cert
, STACK_OF(X509
)* extra_certs
,
599 X509_STORE
* pkix_validation_store
)
603 STACK_OF(X509
)* pkix_validation_chain
= NULL
;
605 ldns_tlsa_certificate_usage cert_usage
;
606 ldns_tlsa_selector selector
;
607 ldns_tlsa_matching_type matching_type
;
611 /* No TLSA, so regular PKIX validation
613 return ldns_dane_pkix_validate(cert
, extra_certs
,
614 pkix_validation_store
);
616 cert_usage
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 0));
617 selector
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 1));
618 matching_type
= ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr
, 2));
619 data
= ldns_rr_rdf(tlsa_rr
, 3) ;
621 switch (cert_usage
) {
622 case LDNS_TLSA_USAGE_CA_CONSTRAINT
:
623 s
= ldns_dane_pkix_validate_and_get_chain(
624 &pkix_validation_chain
,
626 pkix_validation_store
);
627 if (! pkix_validation_chain
) {
630 if (s
== LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
) {
632 * NO PKIX validation. We still try to match *any*
633 * certificate from the chain, so we return
634 * TLSA errors over PKIX errors.
636 * i.e. When the TLSA matches no certificate, we return
637 * TLSA_DID_NOT_MATCH and not PKIX_DID_NOT_VALIDATE
639 s
= ldns_dane_match_any_cert_with_data(
640 pkix_validation_chain
,
641 selector
, matching_type
, data
, true);
643 if (s
== LDNS_STATUS_OK
) {
644 /* A TLSA record did match a cert from the
645 * chain, thus the error is failed PKIX
648 s
= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
;
651 } else if (s
== LDNS_STATUS_OK
) {
652 /* PKIX validated, does the TLSA match too? */
654 s
= ldns_dane_match_any_cert_with_data(
655 pkix_validation_chain
,
656 selector
, matching_type
, data
, true);
658 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
662 case LDNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT
:
663 s
= ldns_dane_match_cert_with_data(cert
,
664 selector
, matching_type
, data
);
666 if (s
== LDNS_STATUS_OK
) {
667 return ldns_dane_pkix_validate(cert
, extra_certs
,
668 pkix_validation_store
);
673 case LDNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION
:
674 s
= ldns_dane_pkix_get_chain(&pkix_validation_chain
,
677 if (s
== LDNS_STATUS_OK
) {
678 s
= ldns_dane_match_any_cert_with_data(
679 pkix_validation_chain
,
680 selector
, matching_type
, data
, false);
682 } else if (! pkix_validation_chain
) {
685 sk_X509_pop_free(pkix_validation_chain
, X509_free
);
689 case LDNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE
:
690 return ldns_dane_match_cert_with_data(cert
,
691 selector
, matching_type
, data
);
697 return LDNS_STATUS_DANE_UNKNOWN_CERTIFICATE_USAGE
;
702 ldns_dane_verify(ldns_rr_list
* tlsas
,
703 X509
* cert
, STACK_OF(X509
)* extra_certs
,
704 X509_STORE
* pkix_validation_store
)
708 ldns_status s
= LDNS_STATUS_OK
, ps
;
710 assert(cert
!= NULL
);
712 if (tlsas
&& ldns_rr_list_rr_count(tlsas
) > 0) {
713 tlsas
= ldns_dane_filter_unusable_records(tlsas
);
715 return LDNS_STATUS_MEM_ERR
;
718 if (! tlsas
|| ldns_rr_list_rr_count(tlsas
) == 0) {
719 /* No TLSA's, so regular PKIX validation
721 return ldns_dane_pkix_validate(cert
, extra_certs
,
722 pkix_validation_store
);
724 for (i
= 0; i
< ldns_rr_list_rr_count(tlsas
); i
++) {
725 tlsa_rr
= ldns_rr_list_rr(tlsas
, i
);
727 s
= ldns_dane_verify_rr(tlsa_rr
, cert
, extra_certs
,
728 pkix_validation_store
);
730 if (s
!= LDNS_STATUS_DANE_TLSA_DID_NOT_MATCH
&&
731 s
!= LDNS_STATUS_DANE_PKIX_DID_NOT_VALIDATE
) {
733 /* which would be LDNS_STATUS_OK (match)
734 * or some fatal error preventing use from
735 * trying the next TLSA record.
739 s
= (s
> ps
? s
: ps
); /* prefer PKIX_DID_NOT_VALIDATE
740 * over TLSA_DID_NOT_MATCH
743 ldns_rr_list_free(tlsas
);
747 #endif /* HAVE_SSL */
748 #endif /* USE_DANE */