2 * Copyright (C) 2012 KU Leuven
3 * Copyright (C) 2013 Christian Grothoff
4 * Copyright (C) 2013 Nikos Mavrogiannopoulos
6 * Author: Nikos Mavrogiannopoulos
8 * This file is part of libdane.
10 * The libdane library is free software; you can redistribute it
11 * and/or modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>
31 #include <arpa/inet.h>
33 #include <gnutls/dane.h>
34 #include <gnutls/x509.h>
35 #include <gnutls/abstract.h>
36 #include <gnutls/crypto.h>
37 #include "../lib/gnutls_int.h"
39 #define MAX_DATA_ENTRIES 100
42 #undef gnutls_assert_val
45 #define gnutls_assert() fprintf(stderr, "ASSERT: %s: %d\n", __FILE__, __LINE__);
46 #define gnutls_assert_val(x) gnutls_assert_val_int(x, __FILE__, __LINE__)
47 static int gnutls_assert_val_int(int val, const char *file, int line)
49 fprintf(stderr, "ASSERT: %s: %d\n", file, line);
53 #define gnutls_assert()
54 #define gnutls_assert_val(x) (x)
57 struct dane_state_st {
62 struct dane_query_st {
63 struct ub_result *result;
64 unsigned int data_entries;
65 dane_cert_usage_t usage[MAX_DATA_ENTRIES];
66 dane_cert_type_t type[MAX_DATA_ENTRIES];
67 dane_match_type_t match[MAX_DATA_ENTRIES];
68 gnutls_datum_t data[MAX_DATA_ENTRIES];
70 dane_query_status_t status;
75 * @q: The query result structure
77 * This function will return the status of the query response.
78 * See %dane_query_status_t for the possible types.
80 * Returns: The status type.
82 dane_query_status_t dane_query_status(dane_query_t q)
89 * @q: The query result structure
91 * This function will return the number of entries in a query.
93 * Returns: The number of entries.
95 unsigned int dane_query_entries(dane_query_t q)
97 return q->data_entries;
102 * @q: The query result structure
103 * @idx: The index of the query response.
104 * @usage: The certificate usage (see %dane_cert_usage_t)
105 * @type: The certificate type (see %dane_cert_type_t)
106 * @match: The DANE matching type (see %dane_match_type_t)
107 * @data: The DANE data.
109 * This function will provide the DANE data from the query
112 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
113 * negative error value.
116 dane_query_data(dane_query_t q, unsigned int idx,
117 unsigned int *usage, unsigned int *type,
118 unsigned int *match, gnutls_datum_t * data)
120 if (idx >= q->data_entries)
122 gnutls_assert_val(DANE_E_REQUESTED_DATA_NOT_AVAILABLE);
125 *usage = q->usage[idx];
127 *type = q->type[idx];
129 *match = q->match[idx];
131 data->data = q->data[idx].data;
132 data->size = q->data[idx].size;
135 return DANE_E_SUCCESS;
139 * dane_query_to_raw_tlsa:
140 * @q: The query result structure
141 * @data_entries: Pointer set to the number of entries in the query
142 * @dane_data: Pointer to contain an array of DNS rdata items, terminated with a NULL pointer;
143 * caller must guarantee that the referenced data remains
144 * valid until dane_query_deinit() is called.
145 * @dane_data_len: Pointer to contain the length n bytes of the dane_data items
146 * @secure: Pointer set true if the result is validated securely, false if
147 * validation failed or the domain queried has no security info
148 * @bogus: Pointer set true if the result was not secure due to a security failure
150 * This function will provide the DANE data from the query
153 * The pointers dane_data and dane_data_len are allocated with gnutls_malloc()
154 * to contain the data from the query result structure (individual
155 * @dane_data items simply point to the original data and are not allocated separately).
156 * The returned @dane_data are only valid during the lifetime of @q.
158 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
159 * negative error value.
162 dane_query_to_raw_tlsa(dane_query_t q, unsigned int *data_entries,
163 char ***dane_data, int **dane_data_len, int *secure, int *bogus)
171 *dane_data_len = NULL;
174 if (q->status & DANE_QUERY_DNSSEC_VERIFIED)
181 if (q->status & DANE_QUERY_BOGUS)
187 /* pack dane_data pointer list followed by dane_data contents */
188 data_sz = sizeof (**dane_data) * (q->data_entries + 1);
189 for (idx = 0; idx < q->data_entries; idx++)
190 data_sz += 3 + q->data[idx].size;
192 *dane_data = gnutls_calloc (1, data_sz);
193 if (*dane_data == NULL)
194 return DANE_E_MEMORY_ERROR;
195 data_buf = (char *)*dane_data;
196 data_buf += sizeof (**dane_data) * (q->data_entries + 1);
198 *dane_data_len = gnutls_calloc (q->data_entries + 1, sizeof (**dane_data_len));
199 if (*dane_data_len == NULL) {
202 return DANE_E_MEMORY_ERROR;
205 for (idx = 0; idx < q->data_entries; idx++) {
206 (*dane_data)[idx] = data_buf;
207 (*dane_data)[idx][0] = q->usage[idx];
208 (*dane_data)[idx][1] = q->type[idx];
209 (*dane_data)[idx][2] = q->match[idx];
210 memcpy(&(*dane_data)[idx][3], q->data[idx].data, q->data[idx].size);
211 (*dane_data_len)[idx] = 3 + q->data[idx].size;
212 data_buf += 3 + q->data[idx].size;
214 (*dane_data)[idx] = NULL;
215 (*dane_data_len)[idx] = 0;
216 *data_entries = q->data_entries;
218 return DANE_E_SUCCESS;
223 * @s: The structure to be initialized
224 * @flags: flags from the %dane_state_flags enumeration
226 * This function will initialize a DANE query structure.
228 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
229 * negative error value.
231 int dane_state_init(dane_state_t * s, unsigned int flags)
236 *s = calloc(1, sizeof(struct dane_state_st));
238 return gnutls_assert_val(DANE_E_MEMORY_ERROR);
240 ctx = ub_ctx_create();
243 ret = DANE_E_INITIALIZATION_ERROR;
246 ub_ctx_debugout(ctx, stderr);
248 if (!(flags & DANE_F_IGNORE_LOCAL_RESOLVER)) {
249 if ((ret = ub_ctx_resolvconf(ctx, NULL)) != 0) {
251 ret = DANE_E_INITIALIZATION_ERROR;
255 if ((ret = ub_ctx_hosts(ctx, NULL)) != 0) {
257 ret = DANE_E_INITIALIZATION_ERROR;
262 /* read public keys for DNSSEC verification */
263 if (!(flags & DANE_F_IGNORE_DNSSEC)) {
265 ub_ctx_add_ta_file(ctx,
266 (char *) UNBOUND_ROOT_KEY_FILE)) !=
269 ret = DANE_E_INITIALIZATION_ERROR;
277 return DANE_E_SUCCESS;
289 * @s: The structure to be deinitialized
291 * This function will deinitialize a DANE query structure.
294 void dane_state_deinit(dane_state_t s)
296 ub_ctx_delete(s->ctx);
301 * dane_state_set_dlv_file:
302 * @s: The structure to be deinitialized
303 * @file: The file holding the DLV keys.
305 * This function will set a file with trusted keys
306 * for DLV (DNSSEC Lookaside Validation).
309 int dane_state_set_dlv_file(dane_state_t s, const char *file)
314 ub_ctx_set_option(s->ctx, (char *) "dlv-anchor-file:",
317 return gnutls_assert_val(DANE_E_FILE_ERROR);
324 * @q: The structure to be deinitialized
326 * This function will deinitialize a DANE query result structure.
329 void dane_query_deinit(dane_query_t q)
332 ub_resolve_free(q->result);
339 * @s: The DANE state structure
340 * @r: A structure to place the result
341 * @dane_data: array of DNS rdata items, terminated with a NULL pointer;
342 * caller must guarantee that the referenced data remains
343 * valid until dane_query_deinit() is called.
344 * @dane_data_len: the length n bytes of the dane_data items
345 * @secure: true if the result is validated securely, false if
346 * validation failed or the domain queried has no security info
347 * @bogus: if the result was not secure (secure = 0) due to a security failure,
348 * and the result is due to a security failure, bogus is true.
350 * This function will fill in the TLSA (DANE) structure from
351 * the given raw DNS record data. The @dane_data must be valid
352 * during the lifetime of the query.
354 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
355 * negative error value.
358 dane_raw_tlsa(dane_state_t s, dane_query_t * r, char *const *dane_data,
359 const int *dane_data_len, int secure, int bogus)
361 int ret = DANE_E_SUCCESS;
364 *r = calloc(1, sizeof(struct dane_query_st));
366 return gnutls_assert_val(DANE_E_MEMORY_ERROR);
368 (*r)->data_entries = 0;
370 for (i = 0; i < MAX_DATA_ENTRIES; i++) {
371 if (dane_data[i] == NULL)
374 if (dane_data_len[i] <= 3)
377 (DANE_E_RECEIVED_CORRUPT_DATA);
379 (*r)->usage[i] = dane_data[i][0];
380 (*r)->type[i] = dane_data[i][1];
381 (*r)->match[i] = dane_data[i][2];
382 (*r)->data[i].data = (void *) &dane_data[i][3];
383 (*r)->data[i].size = dane_data_len[i] - 3;
384 (*r)->data_entries++;
387 if (!(s->flags & DANE_F_INSECURE) && !secure) {
389 ret = gnutls_assert_val(DANE_E_INVALID_DNSSEC_SIG);
391 ret = gnutls_assert_val(DANE_E_NO_DNSSEC_SIG);
394 /* show security status */
396 (*r)->status = DANE_QUERY_DNSSEC_VERIFIED;
399 (*r)->status = DANE_QUERY_BOGUS;
402 (*r)->status = DANE_QUERY_NO_DNSSEC;
411 * @s: The DANE state structure
412 * @r: A structure to place the result
413 * @host: The host name to resolve.
414 * @proto: The protocol type (tcp, udp, etc.)
415 * @port: The service port number (eg. 443).
417 * This function will query the DNS server for the TLSA (DANE)
418 * data for the given host.
420 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
421 * negative error value.
424 dane_query_tlsa(dane_state_t s, dane_query_t * r, const char *host,
425 const char *proto, unsigned int port)
429 struct ub_result *result;
431 snprintf(ns, sizeof(ns), "_%u._%s.%s", port, proto, host);
433 /* query for webserver */
434 ret = ub_resolve(s->ctx, ns, 52, 1, &result);
436 return gnutls_assert_val(DANE_E_RESOLVING_ERROR);
439 /* show first result */
440 if (!result->havedata) {
441 ub_resolve_free(result);
442 return gnutls_assert_val(DANE_E_NO_DANE_DATA);
446 dane_raw_tlsa(s, r, result->data, result->len, result->secure,
449 ub_resolve_free(result);
452 (*r)->result = result;
458 matches(const gnutls_datum_t * raw1, const gnutls_datum_t * raw2,
459 dane_match_type_t match)
464 if (match == DANE_MATCH_EXACT) {
465 if (raw1->size != raw2->size)
466 return gnutls_assert_val(0);
468 if (memcmp(raw1->data, raw2->data, raw1->size) != 0)
469 return gnutls_assert_val(0);
472 } else if (match == DANE_MATCH_SHA2_256) {
474 if (raw2->size != 32)
475 return gnutls_assert_val(0);
478 gnutls_hash_fast(GNUTLS_DIG_SHA256, raw1->data,
481 return gnutls_assert_val(0);
483 if (memcmp(digest, raw2->data, 32) != 0)
484 return gnutls_assert_val(0);
487 } else if (match == DANE_MATCH_SHA2_512) {
488 if (raw2->size != 64)
489 return gnutls_assert_val(0);
492 gnutls_hash_fast(GNUTLS_DIG_SHA512, raw1->data,
495 return gnutls_assert_val(0);
497 if (memcmp(digest, raw2->data, 64) != 0)
498 return gnutls_assert_val(0);
503 return gnutls_assert_val(0);
507 crt_to_pubkey(const gnutls_datum_t * raw_crt, gnutls_datum_t * out)
509 gnutls_pubkey_t pub = NULL;
510 gnutls_x509_crt_t crt = NULL;
515 ret = gnutls_x509_crt_init(&crt);
517 return gnutls_assert_val(DANE_E_PUBKEY_ERROR);
519 ret = gnutls_pubkey_init(&pub);
522 ret = DANE_E_PUBKEY_ERROR;
526 ret = gnutls_x509_crt_import(crt, raw_crt, GNUTLS_X509_FMT_DER);
529 ret = DANE_E_PUBKEY_ERROR;
533 ret = gnutls_pubkey_import_x509(pub, crt, 0);
536 ret = DANE_E_PUBKEY_ERROR;
540 ret = gnutls_pubkey_export2(pub, GNUTLS_X509_FMT_DER, out);
543 ret = DANE_E_PUBKEY_ERROR;
555 gnutls_pubkey_deinit(pub);
557 gnutls_x509_crt_deinit(crt);
563 verify_ca(const gnutls_datum_t * raw_crt, unsigned raw_crt_size,
564 gnutls_certificate_type_t crt_type,
565 dane_cert_type_t ctype,
566 dane_match_type_t match, gnutls_datum_t * data,
567 unsigned int *verify)
569 gnutls_datum_t pubkey = { NULL, 0 };
571 unsigned int vstatus = 0;
572 gnutls_x509_crt_t crt = NULL, ca = NULL;
575 if (raw_crt_size < 2)
576 return gnutls_assert_val(DANE_E_INVALID_REQUEST);
578 if (ctype == DANE_CERT_X509 && crt_type == GNUTLS_CRT_X509) {
580 for (i=raw_crt_size-1;i>=1;i--) {
581 if (matches(&raw_crt[i], data, match)) {
589 *verify |= DANE_VERIFY_CA_CONSTRAINTS_VIOLATED;
592 } else if (ctype == DANE_CERT_PK && crt_type == GNUTLS_CRT_X509) {
595 for (i=raw_crt_size-1;i>=1;i--) {
596 ret = crt_to_pubkey(&raw_crt[i], &pubkey);
602 if (matches(&pubkey, data, match)) {
613 *verify |= DANE_VERIFY_CA_CONSTRAINTS_VIOLATED;
616 ret = gnutls_assert_val(DANE_E_UNKNOWN_DANE_DATA);
620 /* check if the certificate chain is actually a chain */
621 ret = gnutls_x509_crt_init(&crt);
623 ret = gnutls_assert_val(DANE_E_CERT_ERROR);
628 gnutls_x509_crt_import(crt, &raw_crt[0], GNUTLS_X509_FMT_DER);
630 ret = gnutls_assert_val(DANE_E_CERT_ERROR);
634 for (i=raw_crt_size-1;i>=1;i--) {
635 ret = gnutls_x509_crt_init(&ca);
637 ret = gnutls_assert_val(DANE_E_CERT_ERROR);
641 ret = gnutls_x509_crt_import(ca, &raw_crt[i], GNUTLS_X509_FMT_DER);
643 ret = gnutls_assert_val(DANE_E_CERT_ERROR);
647 ret = gnutls_x509_crt_check_issuer(crt, ca);
651 gnutls_x509_crt_deinit(ca);
657 *verify |= DANE_VERIFY_CA_CONSTRAINTS_VIOLATED;
659 ret = gnutls_x509_crt_verify(crt, &ca, 1, 0, &vstatus);
661 ret = gnutls_assert_val(DANE_E_CERT_ERROR);
666 *verify |= DANE_VERIFY_CA_CONSTRAINTS_VIOLATED;
673 gnutls_x509_crt_deinit(crt);
675 gnutls_x509_crt_deinit(ca);
680 verify_ee(const gnutls_datum_t * raw_crt,
681 gnutls_certificate_type_t crt_type, dane_cert_type_t ctype,
682 dane_match_type_t match, gnutls_datum_t * data,
683 unsigned int *verify)
685 gnutls_datum_t pubkey = { NULL, 0 };
688 if (ctype == DANE_CERT_X509 && crt_type == GNUTLS_CRT_X509) {
690 if (!matches(raw_crt, data, match)) {
692 *verify |= DANE_VERIFY_CERT_DIFFERS;
695 } else if (ctype == DANE_CERT_PK && crt_type == GNUTLS_CRT_X509) {
697 ret = crt_to_pubkey(raw_crt, &pubkey);
703 if (!matches(&pubkey, data, match)) {
705 *verify |= DANE_VERIFY_CERT_DIFFERS;
708 ret = gnutls_assert_val(DANE_E_UNKNOWN_DANE_DATA);
718 #define CHECK_VRET(ret, checked, record_status, status) \
719 if (ret == DANE_E_UNKNOWN_DANE_DATA) { \
720 /* skip that entry */ \
722 } else if (ret < 0) { \
727 if (record_status == 0) { \
731 status |= record_status; \
735 * dane_verify_crt_raw:
736 * @s: A DANE state structure (may be NULL)
737 * @chain: A certificate chain
738 * @chain_size: The size of the chain
739 * @chain_type: The type of the certificate chain
740 * @r: DANE data to check against
741 * @sflags: Flags for the the initialization of @s (if NULL)
742 * @vflags: Verification flags; an OR'ed list of %dane_verify_flags_t.
743 * @verify: An OR'ed list of %dane_verify_status_t.
745 * This function will verify the given certificate chain against the
746 * CA constrains and/or the certificate available via DANE.
747 * If no information via DANE can be obtained the flag %DANE_VERIFY_NO_DANE_INFO
748 * is set. If a DNSSEC signature is not available for the DANE
749 * record then the verify flag %DANE_VERIFY_NO_DNSSEC_DATA is set.
751 * Due to the many possible options of DANE, there is no single threat
752 * model countered. When notifying the user about DANE verification results
753 * it may be better to mention: DANE verification did not reject the certificate,
754 * rather than mentioning a successful DANE verication.
756 * Note that this function is designed to be run in addition to
757 * PKIX - certificate chain - verification. To be run independently
758 * the %DANE_VFLAG_ONLY_CHECK_EE_USAGE flag should be specified;
759 * then the function will check whether the key of the peer matches the
760 * key advertized in the DANE entry.
762 * If the @q parameter is provided it will be used for caching entries.
764 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
765 * negative error value.
769 dane_verify_crt_raw(dane_state_t s,
770 const gnutls_datum_t * chain, unsigned chain_size,
771 gnutls_certificate_type_t chain_type,
773 unsigned int sflags, unsigned int vflags,
774 unsigned int *verify)
777 unsigned checked = 0;
778 unsigned int usage, type, match, idx;
781 if (chain_type != GNUTLS_CRT_X509)
782 return gnutls_assert_val(DANE_E_INVALID_REQUEST);
785 return gnutls_assert_val(DANE_E_NO_CERT);
790 unsigned int record_verify = 0;
793 dane_query_data(r, idx++, &usage, &type, &match,
795 if (ret == DANE_E_REQUESTED_DATA_NOT_AVAILABLE)
803 if (!(vflags & DANE_VFLAG_ONLY_CHECK_EE_USAGE)
804 && (usage == DANE_CERT_USAGE_LOCAL_CA
805 || usage == DANE_CERT_USAGE_CA)) {
807 verify_ca(chain, chain_size, chain_type, type,
808 match, &data, &record_verify);
809 CHECK_VRET(ret, checked, record_verify, *verify);
811 } else if (!(vflags & DANE_VFLAG_ONLY_CHECK_CA_USAGE)
812 && (usage == DANE_CERT_USAGE_LOCAL_EE
813 || usage == DANE_CERT_USAGE_EE)) {
815 verify_ee(&chain[0], chain_type, type, match,
816 &data, &record_verify);
817 CHECK_VRET(ret, checked, record_verify, *verify);
822 if ((vflags & DANE_VFLAG_FAIL_IF_NOT_CHECKED) && checked == 0) {
824 gnutls_assert_val(DANE_E_REQUESTED_DATA_NOT_AVAILABLE);
825 } else if (checked == 0) {
826 *verify |= DANE_VERIFY_UNKNOWN_DANE_INFO;
838 * @s: A DANE state structure (may be NULL)
839 * @chain: A certificate chain
840 * @chain_size: The size of the chain
841 * @chain_type: The type of the certificate chain
842 * @hostname: The hostname associated with the chain
843 * @proto: The protocol of the service connecting (e.g. tcp)
844 * @port: The port of the service connecting (e.g. 443)
845 * @sflags: Flags for the the initialization of @s (if NULL)
846 * @vflags: Verification flags; an OR'ed list of %dane_verify_flags_t.
847 * @verify: An OR'ed list of %dane_verify_status_t.
849 * This function will verify the given certificate chain against the
850 * CA constrains and/or the certificate available via DANE.
851 * If no information via DANE can be obtained the flag %DANE_VERIFY_NO_DANE_INFO
852 * is set. If a DNSSEC signature is not available for the DANE
853 * record then the verify flag %DANE_VERIFY_NO_DNSSEC_DATA is set.
855 * Due to the many possible options of DANE, there is no single threat
856 * model countered. When notifying the user about DANE verification results
857 * it may be better to mention: DANE verification did not reject the certificate,
858 * rather than mentioning a successful DANE verication.
860 * Note that this function is designed to be run in addition to
861 * PKIX - certificate chain - verification. To be run independently
862 * the %DANE_VFLAG_ONLY_CHECK_EE_USAGE flag should be specified;
863 * then the function will check whether the key of the peer matches the
864 * key advertized in the DANE entry.
866 * If the @q parameter is provided it will be used for caching entries.
868 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
869 * negative error value.
873 dane_verify_crt(dane_state_t s,
874 const gnutls_datum_t * chain, unsigned chain_size,
875 gnutls_certificate_type_t chain_type,
876 const char *hostname, const char *proto, unsigned int port,
877 unsigned int sflags, unsigned int vflags,
878 unsigned int *verify)
880 dane_state_t state = NULL;
881 dane_query_t r = NULL;
886 ret = dane_state_init(&state, sflags);
894 ret = dane_query_tlsa(state, &r, hostname, proto, port);
899 ret = dane_verify_crt_raw(state, chain, chain_size, chain_type,
900 r, sflags, vflags, verify);
903 dane_state_deinit(state);
905 dane_query_deinit(r);
910 * dane_verify_session_crt:
911 * @s: A DANE state structure (may be NULL)
912 * @session: A gnutls session
913 * @hostname: The hostname associated with the chain
914 * @proto: The protocol of the service connecting (e.g. tcp)
915 * @port: The port of the service connecting (e.g. 443)
916 * @sflags: Flags for the the initialization of @s (if NULL)
917 * @vflags: Verification flags; an OR'ed list of %dane_verify_flags_t.
918 * @verify: An OR'ed list of %dane_verify_status_t.
920 * This function will verify session's certificate chain against the
921 * CA constrains and/or the certificate available via DANE.
922 * See dane_verify_crt() for more information.
924 * This will not verify the chain for validity; unless the DANE
925 * verification is restricted to end certificates, this must be
926 * be performed separately using gnutls_certificate_verify_peers3().
928 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
929 * negative error value.
933 dane_verify_session_crt(dane_state_t s,
934 gnutls_session_t session,
935 const char *hostname, const char *proto,
936 unsigned int port, unsigned int sflags,
937 unsigned int vflags, unsigned int *verify)
939 const gnutls_datum_t *cert_list;
940 unsigned int cert_list_size = 0;
944 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
945 if (cert_list_size == 0) {
946 return gnutls_assert_val(DANE_E_NO_CERT);
949 type = gnutls_certificate_type_get(session);
951 /* this list may be incomplete, try to get the self-signed CA if any */
952 if (cert_list_size > 0) {
953 gnutls_datum_t new_cert_list[cert_list_size+1];
954 gnutls_x509_crt_t crt, ca;
955 gnutls_certificate_credentials_t sc;
957 ret = gnutls_x509_crt_init(&crt);
963 ret = gnutls_x509_crt_import(crt, &cert_list[cert_list_size-1], GNUTLS_X509_FMT_DER);
966 gnutls_x509_crt_deinit(crt);
970 /* if it is already self signed continue normally */
971 ret = gnutls_x509_crt_check_issuer(crt, crt);
974 gnutls_x509_crt_deinit(crt);
978 /* chain does not finish in a self signed cert, try to obtain the issuer */
979 ret = gnutls_credentials_get(session, GNUTLS_CRD_CERTIFICATE, (void**)&sc);
982 gnutls_x509_crt_deinit(crt);
986 ret = gnutls_certificate_get_issuer(sc, crt, &ca, 0);
989 gnutls_x509_crt_deinit(crt);
993 /* make the new list */
994 memcpy(new_cert_list, cert_list, cert_list_size*sizeof(gnutls_datum_t));
996 ret = gnutls_x509_crt_export2(ca, GNUTLS_X509_FMT_DER, &new_cert_list[cert_list_size]);
999 gnutls_x509_crt_deinit(crt);
1003 ret = dane_verify_crt(s, new_cert_list, cert_list_size+1, type,
1004 hostname, proto, port, sflags, vflags,
1009 gnutls_free(new_cert_list[cert_list_size].data);
1014 return dane_verify_crt(s, cert_list, cert_list_size, type,
1015 hostname, proto, port, sflags, vflags,
1020 * dane_verification_status_print:
1021 * @status: The status flags to be printed
1022 * @type: The certificate type
1023 * @out: Newly allocated datum with (0) terminated string.
1024 * @flags: should be zero
1026 * This function will pretty print the status of a verification
1027 * process -- eg. the one obtained by dane_verify_crt().
1029 * The output @out needs to be deallocated using gnutls_free().
1031 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1032 * negative error value.
1035 dane_verification_status_print(unsigned int status,
1036 gnutls_datum_t * out, unsigned int flags)
1038 gnutls_buffer_st str;
1040 _gnutls_buffer_init(&str);
1043 _gnutls_buffer_append_str(&str,
1044 _("Certificate matches. "));
1046 _gnutls_buffer_append_str(&str,
1047 _("Verification failed. "));
1049 if (status & DANE_VERIFY_CA_CONSTRAINTS_VIOLATED)
1050 _gnutls_buffer_append_str(&str,
1052 ("CA constrains were violated. "));
1054 if (status & DANE_VERIFY_CERT_DIFFERS)
1055 _gnutls_buffer_append_str(&str,
1056 _("The certificate differs. "));
1058 if (status & DANE_VERIFY_NO_DANE_INFO)
1059 _gnutls_buffer_append_str(&str,
1061 ("There were no DANE information. "));
1063 return _gnutls_buffer_to_datum(&str, out, 1);