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