xref: /freebsd/crypto/openssl/include/internal/dane.h (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*
2  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OSSL_INTERNAL_DANE_H
11 #define OSSL_INTERNAL_DANE_H
12 # pragma once
13 
14 # include <openssl/safestack.h>
15 
16 /*-
17  * Certificate usages:
18  * https://tools.ietf.org/html/rfc6698#section-2.1.1
19  */
20 #define DANETLS_USAGE_PKIX_TA   0
21 #define DANETLS_USAGE_PKIX_EE   1
22 #define DANETLS_USAGE_DANE_TA   2
23 #define DANETLS_USAGE_DANE_EE   3
24 #define DANETLS_USAGE_LAST      DANETLS_USAGE_DANE_EE
25 
26 /*-
27  * Selectors:
28  * https://tools.ietf.org/html/rfc6698#section-2.1.2
29  */
30 #define DANETLS_SELECTOR_CERT   0
31 #define DANETLS_SELECTOR_SPKI   1
32 #define DANETLS_SELECTOR_LAST   DANETLS_SELECTOR_SPKI
33 
34 /*-
35  * Matching types:
36  * https://tools.ietf.org/html/rfc6698#section-2.1.3
37  */
38 #define DANETLS_MATCHING_FULL   0
39 #define DANETLS_MATCHING_2256   1
40 #define DANETLS_MATCHING_2512   2
41 #define DANETLS_MATCHING_LAST   DANETLS_MATCHING_2512
42 
43 typedef struct danetls_record_st {
44     uint8_t usage;
45     uint8_t selector;
46     uint8_t mtype;
47     unsigned char *data;
48     size_t dlen;
49     EVP_PKEY *spki;
50 } danetls_record;
51 
52 DEFINE_STACK_OF(danetls_record)
53 
54 /*
55  * Shared DANE context
56  */
57 struct dane_ctx_st {
58     const EVP_MD  **mdevp;      /* mtype -> digest */
59     uint8_t        *mdord;      /* mtype -> preference */
60     uint8_t         mdmax;      /* highest supported mtype */
61     unsigned long   flags;      /* feature bitmask */
62 };
63 
64 /*
65  * Per connection DANE state
66  */
67 struct ssl_dane_st {
68     struct dane_ctx_st *dctx;
69     STACK_OF(danetls_record) *trecs;
70     STACK_OF(X509) *certs;      /* DANE-TA(2) Cert(0) Full(0) certs */
71     danetls_record *mtlsa;      /* Matching TLSA record */
72     X509           *mcert;      /* DANE matched cert */
73     uint32_t        umask;      /* Usages present */
74     int             mdpth;      /* Depth of matched cert */
75     int             pdpth;      /* Depth of PKIX trust */
76     unsigned long   flags;      /* feature bitmask */
77 };
78 
79 #define DANETLS_ENABLED(dane)  \
80     ((dane) != NULL && sk_danetls_record_num((dane)->trecs) > 0)
81 
82 #define DANETLS_USAGE_BIT(u)   (((uint32_t)1) << u)
83 
84 #define DANETLS_PKIX_TA_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_PKIX_TA))
85 #define DANETLS_PKIX_EE_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_PKIX_EE))
86 #define DANETLS_DANE_TA_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_DANE_TA))
87 #define DANETLS_DANE_EE_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_DANE_EE))
88 
89 #define DANETLS_PKIX_MASK (DANETLS_PKIX_TA_MASK | DANETLS_PKIX_EE_MASK)
90 #define DANETLS_DANE_MASK (DANETLS_DANE_TA_MASK | DANETLS_DANE_EE_MASK)
91 #define DANETLS_TA_MASK (DANETLS_PKIX_TA_MASK | DANETLS_DANE_TA_MASK)
92 #define DANETLS_EE_MASK (DANETLS_PKIX_EE_MASK | DANETLS_DANE_EE_MASK)
93 
94 #define DANETLS_HAS_PKIX(dane) ((dane) && ((dane)->umask & DANETLS_PKIX_MASK))
95 #define DANETLS_HAS_DANE(dane) ((dane) && ((dane)->umask & DANETLS_DANE_MASK))
96 #define DANETLS_HAS_TA(dane)   ((dane) && ((dane)->umask & DANETLS_TA_MASK))
97 #define DANETLS_HAS_EE(dane)   ((dane) && ((dane)->umask & DANETLS_EE_MASK))
98 
99 #define DANETLS_HAS_PKIX_TA(dane) ((dane)&&((dane)->umask & DANETLS_PKIX_TA_MASK))
100 #define DANETLS_HAS_PKIX_EE(dane) ((dane)&&((dane)->umask & DANETLS_PKIX_EE_MASK))
101 #define DANETLS_HAS_DANE_TA(dane) ((dane)&&((dane)->umask & DANETLS_DANE_TA_MASK))
102 #define DANETLS_HAS_DANE_EE(dane) ((dane)&&((dane)->umask & DANETLS_DANE_EE_MASK))
103 
104 #endif /* OSSL_INTERNAL_DANE_H */
105