Always require DNSSEC.
[gnutls.git] / libdane / dane.c
blob6f9a2db4cdba0933dd1e88ff2ab6674ea22cc200
1 /*
2 * Copyright (C) 2012 KU Leuven
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of libdane.
8 * libdane is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <arpa/inet.h>
30 #include <unbound.h>
31 #include <gnutls/dane.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
34 #include <gnutls/crypto.h>
36 #define MAX_DATA_ENTRIES 4
38 struct dane_query_st
40 unsigned int data_entries;
41 dane_cert_usage_t usage[MAX_DATA_ENTRIES];
42 dane_cert_type_t type[MAX_DATA_ENTRIES];
43 dane_match_type_t match[MAX_DATA_ENTRIES];
44 gnutls_datum_t data[MAX_DATA_ENTRIES];
45 struct ub_ctx* ctx;
46 struct ub_result* result;
47 unsigned int flags;
48 dane_query_status_t status;
51 /**
52 * dane_query_status:
53 * @q: The query structure
55 * This function will return the status of the query response.
56 * See %dane_query_status_t for the possible types.
58 * Returns: The status type.
59 **/
60 dane_query_status_t dane_query_status(dane_query_t q)
62 return q->status;
65 /**
66 * dane_query_entries:
67 * @q: The query structure
69 * This function will return the number of entries in a query.
71 * Returns: The number of entries.
72 **/
73 unsigned int dane_query_entries(dane_query_t q)
75 return q->data_entries;
78 /**
79 * dane_query_data:
80 * @q: The query structure
81 * @idx: The index of the query response.
82 * @usage: The certificate usage (see %dane_cert_usage_t)
83 * @type: The certificate type (see %dane_cert_type_t)
84 * @match: The DANE matching type (see %dane_match_type_t)
85 * @data: The DANE data.
87 * This function will provide the DANE data from the query
88 * response.
90 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
91 * negative error value.
92 **/
93 int dane_query_data(dane_query_t q, unsigned int idx,
94 unsigned int *usage, unsigned int *type,
95 unsigned int *match, gnutls_datum_t * data)
97 if (idx >= q->data_entries)
98 return DANE_E_REQUESTED_DATA_NOT_AVAILABLE;
100 if (usage)
101 *usage = q->usage[idx];
102 if (type)
103 *type = q->type[idx];
104 if (match)
105 *match = q->match[idx];
106 if (data) {
107 data->data = q->data[idx].data;
108 data->size = q->data[idx].size;
111 return DANE_E_SUCCESS;
115 * dane_query_init:
116 * @q: The structure to be initialized
117 * @flags: flags from the DANE_F_* definitions
119 * This function will initialize a DANE query structure.
121 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
122 * negative error value.
124 int dane_query_init(dane_query_t* q, unsigned int flags)
126 struct ub_ctx* ctx;
127 int ret;
129 *q = calloc(1, sizeof(struct dane_query_st));
130 if (*q == NULL)
131 return DANE_E_MEMORY_ERROR;
133 ctx = ub_ctx_create();
134 if(!ctx) {
135 ret = DANE_E_INITIALIZATION_ERROR;
136 goto cleanup;
138 ub_ctx_debugout(ctx, stderr);
140 if (!(flags & DANE_F_IGNORE_LOCAL_RESOLVER)) {
141 if( (ret=ub_ctx_resolvconf(ctx, NULL)) != 0) {
142 ret = DANE_E_INITIALIZATION_ERROR;
143 goto cleanup;
146 if( (ret=ub_ctx_hosts(ctx, NULL)) != 0) {
147 ret = DANE_E_INITIALIZATION_ERROR;
148 goto cleanup;
152 /* read public keys for DNSSEC verification */
153 if( (ret=ub_ctx_add_ta_file(ctx, (char*)UNBOUND_ROOT_KEY_FILE)) != 0) {
154 ret = DANE_E_INITIALIZATION_ERROR;
155 goto cleanup;
158 (*q)->ctx = ctx;
159 (*q)->flags = flags;
161 return DANE_E_SUCCESS;
162 cleanup:
164 if (ctx)
165 ub_ctx_delete(ctx);
166 free(*q);
168 return ret;
172 * dane_query_init:
173 * @q: The structure to be deinitialized
175 * This function will deinitialize a DANE query structure.
178 void dane_query_deinit(dane_query_t q)
180 if (q->result)
181 ub_ctx_delete(q->ctx);
182 ub_resolve_free(q->result);
184 free(q);
188 * dane_query_resolve_tlsa:
189 * @q: The query structure
190 * @host: The host name to resolve.
191 * @proto: The protocol type (tcp, udp, etc.)
192 * @port: The service port number (eg. 443).
194 * This function will query the DNS server for the TLSA (DANE)
195 * data for the given host.
197 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
198 * negative error value.
200 int dane_query_resolve_tlsa(dane_query_t q, const char* host, const char* proto, unsigned int port)
202 char ns[1024];
203 int ret;
204 unsigned int i;
206 if (q->result) {
207 ub_resolve_free(q->result);
208 q->result = NULL;
211 snprintf(ns, sizeof(ns), "_%u._%s.%s", port, proto, host);
213 /* query for webserver */
214 ret = ub_resolve(q->ctx, ns, 52, 1, &q->result);
215 if(ret != 0) {
216 return DANE_E_RESOLVING_ERROR;
219 /* show first result */
220 if(!q->result->havedata) {
221 return DANE_E_NO_DANE_DATA;
224 i = 0;
225 do {
227 if (q->result->len[i] > 3)
228 ret = DANE_E_SUCCESS;
229 else {
230 return DANE_E_RECEIVED_CORRUPT_DATA;
233 q->usage[i] = q->result->data[i][0];
234 q->type[i] = q->result->data[i][1];
235 q->match[i] = q->result->data[i][2];
236 q->data[i].data = (void*)&q->result->data[i][3];
237 q->data[i].size = q->result->len[i];
238 i++;
239 } while(q->result->data[i] != NULL);
241 q->data_entries = i;
243 if (!q->result->secure) {
244 if (q->result->bogus)
245 ret = DANE_E_INVALID_DNSSEC_SIG;
246 else
247 ret = DANE_E_NO_DNSSEC_SIG;
250 /* show security status */
251 if (q->result->secure)
252 q->status = DANE_QUERY_DNSSEC_VERIFIED;
253 else if (q->result->bogus)
254 q->status = DANE_QUERY_BOGUS;
255 else q->status = DANE_QUERY_NO_DNSSEC;
257 return ret;
260 static unsigned int matches(const gnutls_datum_t *raw1, const gnutls_datum_t *raw2,
261 dane_match_type_t match)
263 uint8_t digest[64];
264 int ret;
266 if (match == DANE_MATCH_EXACT) {
267 if (raw1->size != raw2->size)
268 return 0;
270 if (memcmp(raw1->data, raw2->data, raw1->size) != 0)
271 return 0;
273 return 1;
274 } else if (match == DANE_MATCH_SHA2_256) {
276 if (raw2->size < 32)
277 return 0;
279 ret = gnutls_hash_fast(GNUTLS_DIG_SHA256, raw1->data, raw1->size, digest);
280 if (ret < 0)
281 return 0;
283 if (memcmp(digest, raw2->data, 32) != 0)
284 return 0;
286 return 1;
287 } else if (match == DANE_MATCH_SHA2_512) {
288 if (raw2->size < 64)
289 return 0;
291 ret = gnutls_hash_fast(GNUTLS_DIG_SHA512, raw1->data, raw1->size, digest);
292 if (ret < 0)
293 return 0;
295 if (memcmp(digest, raw2->data, 64) != 0)
296 return 0;
298 return 1;
301 return 0;
304 static int crt_to_pubkey(const gnutls_datum_t *raw_crt, gnutls_datum_t * out)
306 gnutls_pubkey_t pub = NULL;
307 gnutls_x509_crt_t crt = NULL;
308 int ret;
309 size_t size;
311 out->data = NULL;
313 ret = gnutls_x509_crt_init(&crt);
314 if (ret < 0)
315 return DANE_E_PUBKEY_ERROR;
317 ret = gnutls_pubkey_init( &pub);
318 if (ret < 0) {
319 ret = DANE_E_PUBKEY_ERROR;
320 goto cleanup;
323 ret = gnutls_x509_crt_import(crt, raw_crt, GNUTLS_X509_FMT_DER);
324 if (ret < 0) {
325 ret = DANE_E_PUBKEY_ERROR;
326 goto cleanup;
329 ret = gnutls_pubkey_import_x509(pub, crt, 0);
330 if (ret < 0) {
331 ret = DANE_E_PUBKEY_ERROR;
332 goto cleanup;
335 size = 0;
336 ret = gnutls_pubkey_export(pub, GNUTLS_X509_FMT_DER, NULL, &size);
337 if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER) {
338 ret = DANE_E_PUBKEY_ERROR;
339 goto cleanup;
342 out->data = malloc(size);
343 if (out->data == NULL) {
344 ret = DANE_E_MEMORY_ERROR;
345 goto cleanup;
348 ret = gnutls_pubkey_export(pub, GNUTLS_X509_FMT_DER, out->data, &size);
349 if (ret < 0) {
350 ret = DANE_E_PUBKEY_ERROR;
351 goto cleanup;
354 out->size = size;
356 ret = 0;
357 goto clean_certs;
359 cleanup:
360 free(out->data);
361 clean_certs:
362 if (pub)
363 gnutls_pubkey_deinit(pub);
364 if (crt)
365 gnutls_x509_crt_deinit(crt);
367 return ret;
370 static int verify_ca(const gnutls_datum_t *raw_crt, unsigned raw_crt_size,
371 gnutls_certificate_type_t crt_type,
372 dane_cert_type_t ctype,
373 dane_match_type_t match, gnutls_datum_t * data,
374 unsigned int *verify)
376 gnutls_datum_t pubkey = {NULL, 0};
377 int ret;
379 if (raw_crt_size < 2)
380 return DANE_E_INVALID_REQUEST;
382 if (ctype == DANE_CERT_X509 && crt_type == GNUTLS_CRT_X509) {
384 if (!matches(&raw_crt[1], data, match))
385 *verify |= DANE_VERIFY_CA_CONSTRAINS_VIOLATED;
387 } else if (ctype == DANE_CERT_PK && crt_type == GNUTLS_CRT_X509) {
388 ret = crt_to_pubkey(&raw_crt[1], &pubkey);
389 if (ret < 0)
390 goto cleanup;
392 if (!matches(&pubkey, data, match))
393 *verify |= DANE_VERIFY_CA_CONSTRAINS_VIOLATED;
396 ret = 0;
397 cleanup:
398 free(pubkey.data);
399 return ret;
402 static int verify_ee(const gnutls_datum_t *raw_crt, gnutls_certificate_type_t crt_type,
403 dane_cert_type_t ctype, dane_match_type_t match, gnutls_datum_t * data,
404 unsigned int *verify)
406 gnutls_datum_t pubkey = {NULL, 0};
407 int ret;
409 if (ctype == DANE_CERT_X509 && crt_type == GNUTLS_CRT_X509) {
411 if (!matches(raw_crt, data, match))
412 *verify |= DANE_VERIFY_CERT_DIFFERS;
414 } else if (ctype == DANE_CERT_PK && crt_type == GNUTLS_CRT_X509) {
416 ret = crt_to_pubkey(raw_crt, &pubkey);
417 if (ret < 0)
418 goto cleanup;
420 if (!matches(&pubkey, data, match))
421 *verify |= DANE_VERIFY_CERT_DIFFERS;
424 ret = 0;
425 cleanup:
426 free(pubkey.data);
427 return ret;
431 * dane_verify_crt:
432 * @chain: A certificate chain
433 * @chain_size: The size of the chain
434 * @chain_type: The type of the certificate chain
435 * @hostname: The hostname associated with the chain
436 * @proto: The protocol of the service connecting (e.g. tcp)
437 * @port: The port of the service connecting (e.g. 443)
438 * @flags: The %DANE_F flags.
439 * @verify: An OR'ed list of %dane_verify_status_t.
441 * This function will verify the given certificate chain against the
442 * CA constrains and/or the certificate available via DANE.
443 * If no information via DANE can be obtained the flag %DANE_VERIFY_NO_DANE_INFO
444 * is set. If a DNSSEC signature is not available for the DANE
445 * record then the verify flag %DANE_VERIFY_NO_DNSSEC_DATA is set.
447 * Due to the many possible options of DANE, there is no single threat
448 * model countered. When notifying the user about DANE verification results
449 * it may be better to mention: DANE verification did not reject the certificate,
450 * rather than mentioning a successful DANE verication.
452 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
453 * negative error value.
456 int dane_verify_crt (
457 const gnutls_datum_t *chain, unsigned chain_size,
458 gnutls_certificate_type_t chain_type,
459 const char * hostname, const char* proto, unsigned int port,
460 unsigned int flags, unsigned int *verify)
462 dane_query_t q;
463 int ret;
464 unsigned int usage, type, match, idx, status;
465 gnutls_datum_t data;
467 if (chain_type != GNUTLS_CRT_X509)
468 return DANE_E_INVALID_REQUEST;
470 *verify = 0;
472 ret = dane_query_init(&q, flags);
473 if (ret < 0) {
474 return ret;
477 ret = dane_query_resolve_tlsa(q, hostname, proto, port);
478 if (ret < 0) {
479 goto cleanup;
482 status = dane_query_status(q);
483 if (status == DANE_QUERY_BOGUS) {
484 *verify |= DANE_VERIFY_DNSSEC_DATA_INVALID;
485 goto cleanup;
486 } else if (status == DANE_QUERY_NO_DNSSEC) {
487 *verify |= DANE_VERIFY_NO_DNSSEC_DATA;
488 goto cleanup;
491 idx = 0;
492 do {
493 ret = dane_query_data(q, idx++, &usage, &type, &match, &data);
494 if (ret == DANE_E_REQUESTED_DATA_NOT_AVAILABLE)
495 break;
497 if (ret < 0) {
498 goto cleanup;
501 if (usage == DANE_CERT_USAGE_LOCAL_CA || usage == DANE_CERT_USAGE_CA) {
502 ret = verify_ca(chain, chain_size, chain_type, type, match, &data, verify);
503 if (ret < 0)
504 goto cleanup;
506 } else if (usage == DANE_CERT_USAGE_LOCAL_EE || usage == DANE_CERT_USAGE_EE) {
507 ret = verify_ee(&chain[0], chain_type, type, match, &data, verify);
508 if (ret < 0)
509 goto cleanup;
511 } while(1);
513 ret = 0;
515 cleanup:
516 dane_query_deinit(q);
517 return ret;
521 * dane_verify_session_crt:
522 * @session: A gnutls session
523 * @hostname: The hostname associated with the chain
524 * @proto: The protocol of the service connecting (e.g. tcp)
525 * @port: The port of the service connecting (e.g. 443)
526 * @flags: The %DANE_F flags.
527 * @verify: An OR'ed list of %dane_verify_status_t.
529 * This function will verify session's certificate chain against the
530 * CA constrains and/or the certificate available via DANE.
531 * See dane_verify_crt() for more information.
533 * Returns: On success, %DANE_E_SUCCESS (0) is returned, otherwise a
534 * negative error value.
537 int dane_verify_session_crt (
538 gnutls_session_t session,
539 const char * hostname, const char* proto, unsigned int port,
540 unsigned int flags, unsigned int *verify)
542 const gnutls_datum_t *cert_list;
543 unsigned int cert_list_size = 0;
544 unsigned int type;
546 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
547 if (cert_list_size == 0) {
548 return DANE_E_NO_CERT;
551 type = gnutls_certificate_type_get(session);
553 return dane_verify_crt(cert_list, cert_list_size, type, hostname, proto, port, flags, verify);