Commit | Line | Data |
---|---|---|
aaba7d03 VD |
1 | /* |
2 | * Author: Viktor Dukhovni | |
3 | * License: THIS CODE IS IN THE PUBLIC DOMAIN. | |
4 | */ | |
e682570f TL |
5 | #include <stdio.h> |
6 | #include <string.h> | |
7 | #include <stdint.h> | |
8 | ||
9 | #include <openssl/opensslv.h> | |
10 | #include <openssl/err.h> | |
11 | #include <openssl/crypto.h> | |
12 | #include <openssl/safestack.h> | |
13 | #include <openssl/objects.h> | |
14 | #include <openssl/x509.h> | |
15 | #include <openssl/x509v3.h> | |
16 | #include <openssl/evp.h> | |
aaba7d03 | 17 | #include <openssl/bn.h> |
e682570f TL |
18 | |
19 | #if OPENSSL_VERSION_NUMBER < 0x1000000fL | |
880a1e77 JH |
20 | # error "OpenSSL 1.0.0 or higher required" |
21 | #else /* remainder of file */ | |
e682570f | 22 | |
aaba7d03 VD |
23 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
24 | #define X509_up_ref(x) CRYPTO_add(&((x)->references), 1, CRYPTO_LOCK_X509) | |
25 | #endif | |
26 | ||
e682570f TL |
27 | #include "danessl.h" |
28 | ||
aaba7d03 VD |
29 | #define DANESSL_F_ADD_SKID 100 |
30 | #define DANESSL_F_ADD_TLSA 101 | |
31 | #define DANESSL_F_CHECK_END_ENTITY 102 | |
32 | #define DANESSL_F_CTX_INIT 103 | |
33 | #define DANESSL_F_GROW_CHAIN 104 | |
34 | #define DANESSL_F_INIT 105 | |
35 | #define DANESSL_F_LIBRARY_INIT 106 | |
36 | #define DANESSL_F_LIST_ALLOC 107 | |
37 | #define DANESSL_F_MATCH 108 | |
38 | #define DANESSL_F_PUSH_EXT 109 | |
39 | #define DANESSL_F_SET_TRUST_ANCHOR 110 | |
40 | #define DANESSL_F_VERIFY_CERT 111 | |
41 | #define DANESSL_F_WRAP_CERT 112 | |
42 | ||
43 | #define DANESSL_R_BAD_CERT 100 | |
44 | #define DANESSL_R_BAD_CERT_PKEY 101 | |
45 | #define DANESSL_R_BAD_DATA_LENGTH 102 | |
46 | #define DANESSL_R_BAD_DIGEST 103 | |
47 | #define DANESSL_R_BAD_NULL_DATA 104 | |
48 | #define DANESSL_R_BAD_PKEY 105 | |
49 | #define DANESSL_R_BAD_SELECTOR 106 | |
50 | #define DANESSL_R_BAD_USAGE 107 | |
51 | #define DANESSL_R_INIT 108 | |
52 | #define DANESSL_R_LIBRARY_INIT 109 | |
53 | #define DANESSL_R_NOSIGN_KEY 110 | |
54 | #define DANESSL_R_SCTX_INIT 111 | |
55 | #define DANESSL_R_SUPPORT 112 | |
e682570f TL |
56 | |
57 | #ifndef OPENSSL_NO_ERR | |
aaba7d03 VD |
58 | #define DANESSL_F_PLACEHOLDER 0 /* FIRST! Value TBD */ |
59 | static ERR_STRING_DATA dane_str_functs[] = { | |
60 | {DANESSL_F_PLACEHOLDER, "DANE library"}, /* FIRST!!! */ | |
61 | {DANESSL_F_ADD_SKID, "add_skid"}, | |
62 | {DANESSL_F_ADD_TLSA, "DANESSL_add_tlsa"}, | |
63 | {DANESSL_F_CHECK_END_ENTITY, "check_end_entity"}, | |
64 | {DANESSL_F_CTX_INIT, "DANESSL_CTX_init"}, | |
65 | {DANESSL_F_GROW_CHAIN, "grow_chain"}, | |
66 | {DANESSL_F_INIT, "DANESSL_init"}, | |
67 | {DANESSL_F_LIBRARY_INIT, "DANESSL_library_init"}, | |
68 | {DANESSL_F_LIST_ALLOC, "list_alloc"}, | |
69 | {DANESSL_F_MATCH, "match"}, | |
70 | {DANESSL_F_PUSH_EXT, "push_ext"}, | |
71 | {DANESSL_F_SET_TRUST_ANCHOR, "set_trust_anchor"}, | |
72 | {DANESSL_F_VERIFY_CERT, "verify_cert"}, | |
73 | {DANESSL_F_WRAP_CERT, "wrap_cert"}, | |
74 | {0, NULL} | |
e682570f | 75 | }; |
aaba7d03 VD |
76 | static ERR_STRING_DATA dane_str_reasons[] = { |
77 | {DANESSL_R_BAD_CERT, "Bad TLSA record certificate"}, | |
78 | {DANESSL_R_BAD_CERT_PKEY, "Bad TLSA record certificate public key"}, | |
79 | {DANESSL_R_BAD_DATA_LENGTH, "Bad TLSA record digest length"}, | |
80 | {DANESSL_R_BAD_DIGEST, "Bad TLSA record digest"}, | |
81 | {DANESSL_R_BAD_NULL_DATA, "Bad TLSA record null data"}, | |
82 | {DANESSL_R_BAD_PKEY, "Bad TLSA record public key"}, | |
83 | {DANESSL_R_BAD_SELECTOR, "Bad TLSA record selector"}, | |
84 | {DANESSL_R_BAD_USAGE, "Bad TLSA record usage"}, | |
85 | {DANESSL_R_INIT, "DANESSL_init() required"}, | |
86 | {DANESSL_R_LIBRARY_INIT, "DANESSL_library_init() required"}, | |
87 | {DANESSL_R_NOSIGN_KEY, "Certificate usage 2 requires EC support"}, | |
88 | {DANESSL_R_SCTX_INIT, "DANESSL_CTX_init() required"}, | |
89 | {DANESSL_R_SUPPORT, "DANE library features not supported"}, | |
90 | {0, NULL} | |
e682570f | 91 | }; |
aaba7d03 | 92 | #endif |
e682570f TL |
93 | |
94 | #define DANEerr(f, r) ERR_PUT_error(err_lib_dane, (f), (r), __FILE__, __LINE__) | |
95 | ||
96 | static int err_lib_dane = -1; | |
97 | static int dane_idx = -1; | |
98 | ||
99 | #ifdef X509_V_FLAG_PARTIAL_CHAIN /* OpenSSL >= 1.0.2 */ | |
100 | static int wrap_to_root = 0; | |
101 | #else | |
102 | static int wrap_to_root = 1; | |
103 | #endif | |
104 | ||
105 | static void (*cert_free)(void *) = (void (*)(void *)) X509_free; | |
106 | static void (*pkey_free)(void *) = (void (*)(void *)) EVP_PKEY_free; | |
107 | ||
880a1e77 JH |
108 | typedef struct dane_list |
109 | { | |
e682570f TL |
110 | struct dane_list *next; |
111 | void *value; | |
112 | } *dane_list; | |
113 | ||
114 | #define LINSERT(h, e) do { (e)->next = (h); (h) = (e); } while (0) | |
115 | ||
880a1e77 JH |
116 | typedef struct dane_host_list |
117 | { | |
118 | struct dane_host_list *next; | |
e682570f | 119 | char *value; |
880a1e77 | 120 | } *dane_host_list; |
e682570f | 121 | |
880a1e77 JH |
122 | typedef struct dane_data |
123 | { | |
e682570f TL |
124 | size_t datalen; |
125 | unsigned char data[0]; | |
126 | } *dane_data; | |
127 | ||
880a1e77 JH |
128 | typedef struct dane_data_list |
129 | { | |
130 | struct dane_data_list *next; | |
e682570f | 131 | dane_data value; |
880a1e77 | 132 | } *dane_data_list; |
e682570f | 133 | |
880a1e77 JH |
134 | typedef struct dane_mtype |
135 | { | |
e682570f TL |
136 | int mdlen; |
137 | const EVP_MD *md; | |
880a1e77 | 138 | dane_data_list data; |
e682570f TL |
139 | } *dane_mtype; |
140 | ||
880a1e77 JH |
141 | typedef struct dane_mtype_list |
142 | { | |
143 | struct dane_mtype_list *next; | |
e682570f | 144 | dane_mtype value; |
880a1e77 | 145 | } *dane_mtype_list; |
e682570f | 146 | |
880a1e77 JH |
147 | typedef struct dane_selector |
148 | { | |
e682570f | 149 | uint8_t selector; |
880a1e77 | 150 | dane_mtype_list mtype; |
e682570f TL |
151 | } *dane_selector; |
152 | ||
880a1e77 JH |
153 | typedef struct dane_selector_list |
154 | { | |
155 | struct dane_selector_list *next; | |
e682570f | 156 | dane_selector value; |
880a1e77 | 157 | } *dane_selector_list; |
e682570f | 158 | |
880a1e77 JH |
159 | typedef struct dane_pkey_list |
160 | { | |
161 | struct dane_pkey_list *next; | |
e682570f | 162 | EVP_PKEY *value; |
880a1e77 | 163 | } *dane_pkey_list; |
e682570f | 164 | |
880a1e77 JH |
165 | typedef struct dane_cert_list |
166 | { | |
167 | struct dane_cert_list *next; | |
e682570f | 168 | X509 *value; |
880a1e77 | 169 | } *dane_cert_list; |
e682570f | 170 | |
880a1e77 JH |
171 | typedef struct ssl_dane |
172 | { | |
e682570f TL |
173 | int (*verify)(X509_STORE_CTX *); |
174 | STACK_OF(X509) *roots; | |
175 | STACK_OF(X509) *chain; | |
aaba7d03 VD |
176 | X509 *match; /* Matched cert */ |
177 | const char *thost; /* TLSA base domain */ | |
178 | char *mhost; /* Matched peer name */ | |
880a1e77 JH |
179 | dane_pkey_list pkeys; |
180 | dane_cert_list certs; | |
181 | dane_host_list hosts; | |
aaba7d03 | 182 | dane_selector_list selectors[DANESSL_USAGE_LAST + 1]; |
e682570f | 183 | int depth; |
aaba7d03 VD |
184 | int mdpth; /* Depth of matched cert */ |
185 | int multi; /* Multi-label wildcards? */ | |
186 | int count; /* Number of TLSA records */ | |
880a1e77 | 187 | } ssl_dane; |
e682570f TL |
188 | |
189 | #ifndef X509_V_ERR_HOSTNAME_MISMATCH | |
880a1e77 | 190 | # define X509_V_ERR_HOSTNAME_MISMATCH X509_V_ERR_APPLICATION_VERIFICATION |
e682570f TL |
191 | #endif |
192 | ||
f2f2c91b JH |
193 | |
194 | ||
880a1e77 JH |
195 | static int |
196 | match(dane_selector_list slist, X509 *cert, int depth) | |
e682570f | 197 | { |
880a1e77 | 198 | int matched; |
e682570f | 199 | |
880a1e77 JH |
200 | /* |
201 | * Note, set_trust_anchor() needs to know whether the match was for a | |
202 | * pkey digest or a certificate digest. We return MATCHED_PKEY or | |
203 | * MATCHED_CERT accordingly. | |
204 | */ | |
aaba7d03 VD |
205 | #define MATCHED_CERT (DANESSL_SELECTOR_CERT + 1) |
206 | #define MATCHED_PKEY (DANESSL_SELECTOR_SPKI + 1) | |
e682570f | 207 | |
880a1e77 JH |
208 | /* |
209 | * Loop over each selector, mtype, and associated data element looking | |
210 | * for a match. | |
211 | */ | |
aaba7d03 | 212 | for (matched = 0; !matched && slist; slist = slist->next) |
880a1e77 JH |
213 | { |
214 | dane_mtype_list m; | |
215 | unsigned char mdbuf[EVP_MAX_MD_SIZE]; | |
85098ee7 | 216 | unsigned char *buf = NULL; |
880a1e77 | 217 | unsigned char *buf2; |
85098ee7 | 218 | unsigned int len = 0; |
880a1e77 JH |
219 | |
220 | /* | |
221 | * Extract ASN.1 DER form of certificate or public key. | |
222 | */ | |
223 | switch(slist->value->selector) | |
224 | { | |
aaba7d03 | 225 | case DANESSL_SELECTOR_CERT: |
880a1e77 JH |
226 | len = i2d_X509(cert, NULL); |
227 | buf2 = buf = (unsigned char *) OPENSSL_malloc(len); | |
228 | if(buf) i2d_X509(cert, &buf2); | |
229 | break; | |
aaba7d03 | 230 | case DANESSL_SELECTOR_SPKI: |
880a1e77 JH |
231 | len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL); |
232 | buf2 = buf = (unsigned char *) OPENSSL_malloc(len); | |
233 | if(buf) i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf2); | |
234 | break; | |
235 | } | |
236 | ||
aaba7d03 | 237 | if (!buf) |
880a1e77 | 238 | { |
aaba7d03 | 239 | DANEerr(DANESSL_F_MATCH, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
240 | return 0; |
241 | } | |
242 | OPENSSL_assert(buf2 - buf == len); | |
243 | ||
244 | /* | |
245 | * Loop over each mtype and data element | |
246 | */ | |
aaba7d03 | 247 | for (m = slist->value->mtype; !matched && m; m = m->next) |
880a1e77 JH |
248 | { |
249 | dane_data_list d; | |
250 | unsigned char *cmpbuf = buf; | |
251 | unsigned int cmplen = len; | |
252 | ||
e682570f | 253 | /* |
880a1e77 JH |
254 | * If it is a digest, compute the corresponding digest of the |
255 | * DER data for comparison, otherwise, use the full object. | |
e682570f | 256 | */ |
aaba7d03 | 257 | if (m->value->md) |
880a1e77 JH |
258 | { |
259 | cmpbuf = mdbuf; | |
aaba7d03 | 260 | if (!EVP_Digest(buf, len, cmpbuf, &cmplen, m->value->md, 0)) |
880a1e77 JH |
261 | matched = -1; |
262 | } | |
aaba7d03 VD |
263 | for (d = m->value->data; !matched && d; d = d->next) |
264 | if ( cmplen == d->value->datalen | |
265 | && memcmp(cmpbuf, d->value->data, cmplen) == 0) | |
880a1e77 | 266 | matched = slist->value->selector + 1; |
e682570f TL |
267 | } |
268 | ||
880a1e77 JH |
269 | OPENSSL_free(buf); |
270 | } | |
e682570f | 271 | |
880a1e77 | 272 | return matched; |
e682570f TL |
273 | } |
274 | ||
880a1e77 JH |
275 | static int |
276 | push_ext(X509 *cert, X509_EXTENSION *ext) | |
e682570f | 277 | { |
aaba7d03 VD |
278 | if (ext) { |
279 | if (X509_add_ext(cert, ext, -1)) | |
280 | return 1; | |
281 | X509_EXTENSION_free(ext); | |
282 | } | |
283 | DANEerr(DANESSL_F_PUSH_EXT, ERR_R_MALLOC_FAILURE); | |
284 | return 0; | |
e682570f TL |
285 | } |
286 | ||
880a1e77 JH |
287 | static int |
288 | add_ext(X509 *issuer, X509 *subject, int ext_nid, char *ext_val) | |
e682570f | 289 | { |
880a1e77 | 290 | X509V3_CTX v3ctx; |
e682570f | 291 | |
880a1e77 JH |
292 | X509V3_set_ctx(&v3ctx, issuer, subject, 0, 0, 0); |
293 | return push_ext(subject, X509V3_EXT_conf_nid(0, &v3ctx, ext_nid, ext_val)); | |
e682570f TL |
294 | } |
295 | ||
880a1e77 JH |
296 | static int |
297 | set_serial(X509 *cert, AUTHORITY_KEYID *akid, X509 *subject) | |
e682570f | 298 | { |
880a1e77 JH |
299 | int ret = 0; |
300 | BIGNUM *bn; | |
301 | ||
aaba7d03 | 302 | if (akid && akid->serial) |
880a1e77 JH |
303 | return (X509_set_serialNumber(cert, akid->serial)); |
304 | ||
305 | /* | |
306 | * Add one to subject's serial to avoid collisions between TA serial and | |
307 | * serial of signing root. | |
308 | */ | |
aaba7d03 VD |
309 | if ( (bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(subject), 0)) != 0 |
310 | && BN_add_word(bn, 1) | |
311 | && BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(cert))) | |
880a1e77 JH |
312 | ret = 1; |
313 | ||
aaba7d03 | 314 | if (bn) |
880a1e77 JH |
315 | BN_free(bn); |
316 | return ret; | |
317 | } | |
e682570f | 318 | |
880a1e77 JH |
319 | static int |
320 | add_akid(X509 *cert, AUTHORITY_KEYID *akid) | |
321 | { | |
322 | int nid = NID_authority_key_identifier; | |
323 | ASN1_STRING *id; | |
324 | unsigned char c = 0; | |
325 | int ret = 0; | |
326 | ||
327 | /* | |
328 | * 0 will never be our subject keyid from a SHA-1 hash, but it could be | |
329 | * our subject keyid if forced from child's akid. If so, set our | |
330 | * authority keyid to 1. This way we are never self-signed, and thus | |
331 | * exempt from any potential (off by default for now in OpenSSL) | |
332 | * self-signature checks! | |
333 | */ | |
aaba7d03 VD |
334 | id = (akid && akid->keyid) ? akid->keyid : 0; |
335 | if (id && ASN1_STRING_length(id) == 1 && *ASN1_STRING_data(id) == c) | |
880a1e77 JH |
336 | c = 1; |
337 | ||
aaba7d03 VD |
338 | if ( (akid = AUTHORITY_KEYID_new()) != 0 |
339 | && (akid->keyid = ASN1_OCTET_STRING_new()) != 0 | |
340 | && M_ASN1_OCTET_STRING_set(akid->keyid, (void *) &c, 1) | |
341 | && X509_add1_ext_i2d(cert, nid, akid, 0, X509V3_ADD_APPEND)) | |
880a1e77 | 342 | ret = 1; |
aaba7d03 | 343 | if (akid) |
880a1e77 JH |
344 | AUTHORITY_KEYID_free(akid); |
345 | return ret; | |
e682570f TL |
346 | } |
347 | ||
880a1e77 JH |
348 | static int |
349 | add_skid(X509 *cert, AUTHORITY_KEYID *akid) | |
e682570f | 350 | { |
880a1e77 | 351 | int nid = NID_subject_key_identifier; |
e682570f | 352 | |
aaba7d03 | 353 | if (!akid || !akid->keyid) |
880a1e77 JH |
354 | return add_ext(0, cert, nid, "hash"); |
355 | return X509_add1_ext_i2d(cert, nid, akid->keyid, 0, X509V3_ADD_APPEND) > 0; | |
e682570f TL |
356 | } |
357 | ||
880a1e77 JH |
358 | static X509_NAME * |
359 | akid_issuer_name(AUTHORITY_KEYID *akid) | |
e682570f | 360 | { |
aaba7d03 | 361 | if (akid && akid->issuer) |
880a1e77 JH |
362 | { |
363 | int i; | |
364 | GENERAL_NAMES *gens = akid->issuer; | |
e682570f | 365 | |
aaba7d03 | 366 | for (i = 0; i < sk_GENERAL_NAME_num(gens); ++i) |
880a1e77 JH |
367 | { |
368 | GENERAL_NAME *gn = sk_GENERAL_NAME_value(gens, i); | |
e682570f | 369 | |
aaba7d03 | 370 | if (gn->type == GEN_DIRNAME) |
880a1e77 | 371 | return (gn->d.dirn); |
e682570f | 372 | } |
880a1e77 JH |
373 | } |
374 | return 0; | |
e682570f TL |
375 | } |
376 | ||
880a1e77 JH |
377 | static int |
378 | set_issuer_name(X509 *cert, AUTHORITY_KEYID *akid) | |
e682570f | 379 | { |
880a1e77 JH |
380 | X509_NAME *name = akid_issuer_name(akid); |
381 | ||
382 | /* | |
383 | * If subject's akid specifies an authority key identifer issuer name, we | |
384 | * must use that. | |
385 | */ | |
386 | return X509_set_issuer_name(cert, | |
387 | name ? name : X509_get_subject_name(cert)); | |
e682570f TL |
388 | } |
389 | ||
880a1e77 JH |
390 | static int |
391 | grow_chain(ssl_dane *dane, int trusted, X509 *cert) | |
e682570f | 392 | { |
880a1e77 JH |
393 | STACK_OF(X509) **xs = trusted ? &dane->roots : &dane->chain; |
394 | static ASN1_OBJECT *serverAuth = 0; | |
e682570f TL |
395 | |
396 | #define UNTRUSTED 0 | |
397 | #define TRUSTED 1 | |
398 | ||
aaba7d03 VD |
399 | if ( trusted && !serverAuth |
400 | && !(serverAuth = OBJ_nid2obj(NID_server_auth))) | |
880a1e77 | 401 | { |
aaba7d03 | 402 | DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
403 | return 0; |
404 | } | |
aaba7d03 | 405 | if (!*xs && !(*xs = sk_X509_new_null())) |
880a1e77 | 406 | { |
aaba7d03 | 407 | DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
408 | return 0; |
409 | } | |
410 | ||
aaba7d03 | 411 | if (cert) |
880a1e77 | 412 | { |
aaba7d03 | 413 | if (trusted && !X509_add1_trust_object(cert, serverAuth)) |
880a1e77 JH |
414 | return 0; |
415 | CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509); | |
416 | if (!sk_X509_push(*xs, cert)) | |
417 | { | |
418 | X509_free(cert); | |
aaba7d03 | 419 | DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE); |
880a1e77 | 420 | return 0; |
e682570f | 421 | } |
880a1e77 JH |
422 | } |
423 | return 1; | |
e682570f TL |
424 | } |
425 | ||
880a1e77 JH |
426 | static int |
427 | wrap_issuer(ssl_dane *dane, EVP_PKEY *key, X509 *subject, int depth, int top) | |
e682570f | 428 | { |
880a1e77 JH |
429 | int ret = 1; |
430 | X509 *cert = 0; | |
431 | AUTHORITY_KEYID *akid; | |
432 | X509_NAME *name = X509_get_issuer_name(subject); | |
433 | EVP_PKEY *newkey = key ? key : X509_get_pubkey(subject); | |
e682570f TL |
434 | |
435 | #define WRAP_MID 0 /* Ensure intermediate. */ | |
436 | #define WRAP_TOP 1 /* Ensure self-signed. */ | |
437 | ||
aaba7d03 | 438 | if (!name || !newkey || !(cert = X509_new())) |
880a1e77 JH |
439 | return 0; |
440 | ||
441 | /* | |
442 | * Record the depth of the trust-anchor certificate. | |
443 | */ | |
aaba7d03 | 444 | if (dane->depth < 0) |
880a1e77 JH |
445 | dane->depth = depth + 1; |
446 | ||
447 | /* | |
448 | * XXX: Uncaught error condition: | |
449 | * | |
450 | * The return value is NULL both when the extension is missing, and when | |
451 | * OpenSSL rans out of memory while parsing the extension. | |
452 | */ | |
453 | ERR_clear_error(); | |
454 | akid = X509_get_ext_d2i(subject, NID_authority_key_identifier, 0, 0); | |
455 | /* XXX: Should we peek at the error stack here??? */ | |
456 | ||
457 | /* | |
458 | * If top is true generate a self-issued root CA, otherwise an | |
459 | * intermediate CA and possibly its self-signed issuer. | |
460 | * | |
461 | * CA cert valid for +/- 30 days | |
462 | */ | |
aaba7d03 VD |
463 | if ( !X509_set_version(cert, 2) |
464 | || !set_serial(cert, akid, subject) | |
465 | || !X509_set_subject_name(cert, name) | |
466 | || !set_issuer_name(cert, akid) | |
467 | || !X509_gmtime_adj(X509_get_notBefore(cert), -30 * 86400L) | |
468 | || !X509_gmtime_adj(X509_get_notAfter(cert), 30 * 86400L) | |
469 | || !X509_set_pubkey(cert, newkey) | |
470 | || !add_ext(0, cert, NID_basic_constraints, "CA:TRUE") | |
471 | || (!top && !add_akid(cert, akid)) | |
472 | || !add_skid(cert, akid) | |
473 | || ( !top && wrap_to_root | |
474 | && !wrap_issuer(dane, newkey, cert, depth, WRAP_TOP))) | |
880a1e77 JH |
475 | ret = 0; |
476 | ||
aaba7d03 | 477 | if (akid) |
880a1e77 | 478 | AUTHORITY_KEYID_free(akid); |
aaba7d03 | 479 | if (!key) |
880a1e77 | 480 | EVP_PKEY_free(newkey); |
aaba7d03 | 481 | if (ret) |
880a1e77 | 482 | ret = grow_chain(dane, !top && wrap_to_root ? UNTRUSTED : TRUSTED, cert); |
aaba7d03 | 483 | if (cert) |
880a1e77 JH |
484 | X509_free(cert); |
485 | return ret; | |
e682570f TL |
486 | } |
487 | ||
880a1e77 JH |
488 | static int |
489 | wrap_cert(ssl_dane *dane, X509 *tacert, int depth) | |
e682570f | 490 | { |
aaba7d03 | 491 | if (dane->depth < 0) |
880a1e77 JH |
492 | dane->depth = depth + 1; |
493 | ||
494 | /* | |
495 | * If the TA certificate is self-issued, or need not be, use it directly. | |
496 | * Otherwise, synthesize requisuite ancestors. | |
497 | */ | |
aaba7d03 VD |
498 | if ( !wrap_to_root |
499 | || X509_check_issued(tacert, tacert) == X509_V_OK) | |
880a1e77 JH |
500 | return grow_chain(dane, TRUSTED, tacert); |
501 | ||
aaba7d03 | 502 | if (wrap_issuer(dane, 0, tacert, depth, WRAP_MID)) |
880a1e77 JH |
503 | return grow_chain(dane, UNTRUSTED, tacert); |
504 | return 0; | |
e682570f TL |
505 | } |
506 | ||
880a1e77 JH |
507 | static int |
508 | ta_signed(ssl_dane *dane, X509 *cert, int depth) | |
e682570f | 509 | { |
880a1e77 JH |
510 | dane_cert_list x; |
511 | dane_pkey_list k; | |
512 | EVP_PKEY *pk; | |
513 | int done = 0; | |
514 | ||
515 | /* | |
516 | * First check whether issued and signed by a TA cert, this is cheaper | |
517 | * than the bare-public key checks below, since we can determine whether | |
518 | * the candidate TA certificate issued the certificate to be checked | |
519 | * first (name comparisons), before we bother with signature checks | |
520 | * (public key operations). | |
521 | */ | |
522 | for (x = dane->certs; !done && x; x = x->next) | |
523 | { | |
aaba7d03 | 524 | if (X509_check_issued(x->value, cert) == X509_V_OK) |
880a1e77 | 525 | { |
aaba7d03 | 526 | if (!(pk = X509_get_pubkey(x->value))) |
880a1e77 JH |
527 | { |
528 | /* | |
529 | * The cert originally contained a valid pkey, which does | |
530 | * not just vanish, so this is most likely a memory error. | |
531 | */ | |
532 | done = -1; | |
533 | break; | |
534 | } | |
535 | /* Check signature, since some other TA may work if not this. */ | |
aaba7d03 | 536 | if (X509_verify(cert, pk) > 0) |
880a1e77 JH |
537 | done = wrap_cert(dane, x->value, depth) ? 1 : -1; |
538 | EVP_PKEY_free(pk); | |
e682570f | 539 | } |
880a1e77 JH |
540 | } |
541 | ||
542 | /* | |
543 | * With bare TA public keys, we can't check whether the trust chain is | |
544 | * issued by the key, but we can determine whether it is signed by the | |
545 | * key, so we go with that. | |
546 | * | |
547 | * Ideally, the corresponding certificate was presented in the chain, and we | |
548 | * matched it by its public key digest one level up. This code is here | |
549 | * to handle adverse conditions imposed by sloppy administrators of | |
550 | * receiving systems with poorly constructed chains. | |
551 | * | |
552 | * We'd like to optimize out keys that should not match when the cert's | |
553 | * authority key id does not match the key id of this key computed via | |
554 | * the RFC keyid algorithm (SHA-1 digest of public key bit-string sans | |
555 | * ASN1 tag and length thus also excluding the unused bits field that is | |
556 | * logically part of the length). However, some CAs have a non-standard | |
557 | * authority keyid, so we lose. Too bad. | |
558 | * | |
559 | * This may push errors onto the stack when the certificate signature is | |
560 | * not of the right type or length, throw these away, | |
561 | */ | |
aaba7d03 VD |
562 | for (k = dane->pkeys; !done && k; k = k->next) |
563 | if (X509_verify(cert, k->value) > 0) | |
880a1e77 JH |
564 | done = wrap_issuer(dane, k->value, cert, depth, WRAP_MID) ? 1 : -1; |
565 | else | |
566 | ERR_clear_error(); | |
e682570f | 567 | |
880a1e77 | 568 | return done; |
e682570f TL |
569 | } |
570 | ||
880a1e77 JH |
571 | static int |
572 | set_trust_anchor(X509_STORE_CTX *ctx, ssl_dane *dane, X509 *cert) | |
e682570f | 573 | { |
880a1e77 JH |
574 | int matched = 0; |
575 | int n; | |
576 | int i; | |
577 | int depth = 0; | |
578 | EVP_PKEY *takey; | |
579 | X509 *ca; | |
580 | STACK_OF(X509) *in = ctx->untrusted; /* XXX: Accessor? */ | |
581 | ||
aaba7d03 | 582 | if (!grow_chain(dane, UNTRUSTED, 0)) |
880a1e77 JH |
583 | return -1; |
584 | ||
585 | /* | |
586 | * Accept a degenerate case: depth 0 self-signed trust-anchor. | |
587 | */ | |
aaba7d03 | 588 | if (X509_check_issued(cert, cert) == X509_V_OK) |
880a1e77 JH |
589 | { |
590 | dane->depth = 0; | |
aaba7d03 VD |
591 | matched = match(dane->selectors[DANESSL_USAGE_DANE_TA], cert, 0); |
592 | if (matched > 0 && !grow_chain(dane, TRUSTED, cert)) | |
880a1e77 JH |
593 | matched = -1; |
594 | return matched; | |
595 | } | |
596 | ||
597 | /* Make a shallow copy of the input untrusted chain. */ | |
aaba7d03 | 598 | if (!(in = sk_X509_dup(in))) |
880a1e77 | 599 | { |
aaba7d03 | 600 | DANEerr(DANESSL_F_SET_TRUST_ANCHOR, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
601 | return -1; |
602 | } | |
603 | ||
604 | /* | |
605 | * At each iteration we consume the issuer of the current cert. This | |
606 | * reduces the length of the "in" chain by one. If no issuer is found, | |
607 | * we are done. We also stop when a certificate matches a TA in the | |
608 | * peer's TLSA RRset. | |
609 | * | |
610 | * Caller ensures that the initial certificate is not self-signed. | |
611 | */ | |
aaba7d03 | 612 | for (n = sk_X509_num(in); n > 0; --n, ++depth) |
880a1e77 | 613 | { |
aaba7d03 VD |
614 | for (i = 0; i < n; ++i) |
615 | if (X509_check_issued(sk_X509_value(in, i), cert) == X509_V_OK) | |
880a1e77 JH |
616 | break; |
617 | ||
618 | /* | |
619 | * Final untrusted element with no issuer in the peer's chain, it may | |
620 | * however be signed by a pkey or cert obtained via a TLSA RR. | |
621 | */ | |
aaba7d03 | 622 | if (i == n) |
880a1e77 JH |
623 | break; |
624 | ||
625 | /* Peer's chain contains an issuer ca. */ | |
626 | ca = sk_X509_delete(in, i); | |
627 | ||
628 | /* If not a trust anchor, record untrusted ca and continue. */ | |
aaba7d03 VD |
629 | if ((matched = match(dane->selectors[DANESSL_USAGE_DANE_TA], ca, |
630 | depth + 1)) == 0) | |
880a1e77 | 631 | { |
aaba7d03 | 632 | if (grow_chain(dane, UNTRUSTED, ca)) |
880a1e77 | 633 | { |
aaba7d03 | 634 | if (!X509_check_issued(ca, ca) == X509_V_OK) |
880a1e77 JH |
635 | { |
636 | /* Restart with issuer as subject */ | |
637 | cert = ca; | |
638 | continue; | |
639 | } | |
640 | /* Final self-signed element, skip ta_signed() check. */ | |
641 | cert = 0; | |
642 | } | |
643 | else | |
644 | matched = -1; | |
e682570f | 645 | } |
880a1e77 JH |
646 | else if(matched == MATCHED_CERT) |
647 | { | |
648 | if(!wrap_cert(dane, ca, depth)) | |
649 | matched = -1; | |
e682570f | 650 | } |
880a1e77 JH |
651 | else if(matched == MATCHED_PKEY) |
652 | { | |
aaba7d03 VD |
653 | if ( !(takey = X509_get_pubkey(ca)) |
654 | || !wrap_issuer(dane, takey, cert, depth, WRAP_MID)) | |
880a1e77 | 655 | { |
aaba7d03 | 656 | if (takey) |
880a1e77 JH |
657 | EVP_PKEY_free(takey); |
658 | else | |
aaba7d03 | 659 | DANEerr(DANESSL_F_SET_TRUST_ANCHOR, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
660 | matched = -1; |
661 | } | |
e682570f | 662 | } |
880a1e77 JH |
663 | break; |
664 | } | |
e682570f | 665 | |
880a1e77 JH |
666 | /* Shallow free the duplicated input untrusted chain. */ |
667 | sk_X509_free(in); | |
e682570f | 668 | |
880a1e77 JH |
669 | /* |
670 | * When the loop exits, if "cert" is set, it is not self-signed and has | |
671 | * no issuer in the chain, we check for a possible signature via a DNS | |
672 | * obtained TA cert or public key. | |
673 | */ | |
aaba7d03 | 674 | if (matched == 0 && cert) |
880a1e77 | 675 | matched = ta_signed(dane, cert, depth); |
e682570f | 676 | |
880a1e77 | 677 | return matched; |
e682570f TL |
678 | } |
679 | ||
880a1e77 JH |
680 | static int |
681 | check_end_entity(X509_STORE_CTX *ctx, ssl_dane *dane, X509 *cert) | |
e682570f | 682 | { |
880a1e77 JH |
683 | int matched; |
684 | ||
aaba7d03 VD |
685 | matched = match(dane->selectors[DANESSL_USAGE_DANE_EE], cert, 0); |
686 | if (matched > 0) | |
687 | { | |
688 | dane->mdpth = 0; | |
689 | dane->match = cert; | |
690 | X509_up_ref(cert); | |
880a1e77 | 691 | if(!ctx->chain) |
85098ee7 | 692 | { |
aaba7d03 VD |
693 | if ( (ctx->chain = sk_X509_new_null()) != 0 |
694 | && sk_X509_push(ctx->chain, cert)) | |
695 | X509_up_ref(cert); | |
880a1e77 JH |
696 | else |
697 | { | |
aaba7d03 | 698 | DANEerr(DANESSL_F_CHECK_END_ENTITY, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
699 | return -1; |
700 | } | |
85098ee7 | 701 | } |
aaba7d03 | 702 | } |
880a1e77 | 703 | return matched; |
e682570f TL |
704 | } |
705 | ||
880a1e77 JH |
706 | static int |
707 | match_name(const char *certid, ssl_dane *dane) | |
e682570f | 708 | { |
880a1e77 JH |
709 | int multi = dane->multi; |
710 | dane_host_list hosts; | |
711 | ||
aaba7d03 | 712 | for (hosts = dane->hosts; hosts; hosts = hosts->next) |
880a1e77 JH |
713 | { |
714 | int match_subdomain = 0; | |
715 | const char *domain = hosts->value; | |
716 | const char *parent; | |
717 | int idlen; | |
718 | int domlen; | |
719 | ||
aaba7d03 | 720 | if (*domain == '.' && domain[1] != '\0') |
880a1e77 JH |
721 | { |
722 | ++domain; | |
723 | match_subdomain = 1; | |
e682570f | 724 | } |
880a1e77 JH |
725 | |
726 | /* | |
727 | * Sub-domain match: certid is any sub-domain of hostname. | |
728 | */ | |
729 | if(match_subdomain) | |
85098ee7 | 730 | { |
aaba7d03 VD |
731 | if ( (idlen = strlen(certid)) > (domlen = strlen(domain)) + 1 |
732 | && certid[idlen - domlen - 1] == '.' | |
733 | && !strcasecmp(certid + (idlen - domlen), domain)) | |
880a1e77 JH |
734 | return 1; |
735 | else | |
736 | continue; | |
85098ee7 | 737 | } |
880a1e77 JH |
738 | |
739 | /* | |
740 | * Exact match and initial "*" match. The initial "*" in a certid | |
741 | * matches one (if multi is false) or more hostname components under | |
742 | * the condition that the certid contains multiple hostname components. | |
743 | */ | |
aaba7d03 VD |
744 | if ( !strcasecmp(certid, domain) |
745 | || ( certid[0] == '*' && certid[1] == '.' && certid[2] != 0 | |
746 | && (parent = strchr(domain, '.')) != 0 | |
747 | && (idlen = strlen(certid + 1)) <= (domlen = strlen(parent)) | |
748 | && strcasecmp(multi ? parent + domlen - idlen : parent, certid+1) == 0)) | |
880a1e77 JH |
749 | return 1; |
750 | } | |
751 | return 0; | |
e682570f TL |
752 | } |
753 | ||
880a1e77 JH |
754 | static char * |
755 | check_name(char *name, int len) | |
e682570f | 756 | { |
880a1e77 JH |
757 | char *cp = name + len; |
758 | ||
aaba7d03 | 759 | while (len > 0 && !*--cp) |
880a1e77 | 760 | --len; /* Ignore trailing NULs */ |
aaba7d03 | 761 | if (len <= 0) |
880a1e77 | 762 | return 0; |
aaba7d03 | 763 | for (cp = name; *cp; cp++) |
880a1e77 JH |
764 | { |
765 | char c = *cp; | |
766 | if (!((c >= 'a' && c <= 'z') || | |
767 | (c >= '0' && c <= '9') || | |
768 | (c >= 'A' && c <= 'Z') || | |
769 | (c == '.' || c == '-') || | |
770 | (c == '*'))) | |
771 | return 0; /* Only LDH, '.' and '*' */ | |
772 | } | |
aaba7d03 | 773 | if (cp - name != len) /* Guard against internal NULs */ |
880a1e77 JH |
774 | return 0; |
775 | return name; | |
e682570f TL |
776 | } |
777 | ||
880a1e77 JH |
778 | static char * |
779 | parse_dns_name(const GENERAL_NAME *gn) | |
e682570f | 780 | { |
aaba7d03 | 781 | if (gn->type != GEN_DNS) |
880a1e77 | 782 | return 0; |
aaba7d03 | 783 | if (ASN1_STRING_type(gn->d.ia5) != V_ASN1_IA5STRING) |
880a1e77 JH |
784 | return 0; |
785 | return check_name((char *) ASN1_STRING_data(gn->d.ia5), | |
786 | ASN1_STRING_length(gn->d.ia5)); | |
e682570f TL |
787 | } |
788 | ||
880a1e77 JH |
789 | static char * |
790 | parse_subject_name(X509 *cert) | |
e682570f | 791 | { |
880a1e77 JH |
792 | X509_NAME *name = X509_get_subject_name(cert); |
793 | X509_NAME_ENTRY *entry; | |
794 | ASN1_STRING *entry_str; | |
795 | unsigned char *namebuf; | |
796 | int nid = NID_commonName; | |
797 | int len; | |
798 | int i; | |
799 | ||
aaba7d03 | 800 | if (!name || (i = X509_NAME_get_index_by_NID(name, nid, -1)) < 0) |
880a1e77 | 801 | return 0; |
aaba7d03 | 802 | if (!(entry = X509_NAME_get_entry(name, i))) |
880a1e77 | 803 | return 0; |
aaba7d03 | 804 | if (!(entry_str = X509_NAME_ENTRY_get_data(entry))) |
880a1e77 JH |
805 | return 0; |
806 | ||
aaba7d03 | 807 | if ((len = ASN1_STRING_to_UTF8(&namebuf, entry_str)) < 0) |
880a1e77 | 808 | return 0; |
aaba7d03 | 809 | if (len <= 0 || check_name((char *) namebuf, len) == 0) |
880a1e77 JH |
810 | { |
811 | OPENSSL_free(namebuf); | |
812 | return 0; | |
813 | } | |
814 | return (char *) namebuf; | |
e682570f TL |
815 | } |
816 | ||
880a1e77 JH |
817 | static int |
818 | name_check(ssl_dane *dane, X509 *cert) | |
e682570f | 819 | { |
880a1e77 JH |
820 | int matched = 0; |
821 | BOOL got_altname = FALSE; | |
822 | GENERAL_NAMES *gens; | |
823 | ||
824 | gens = X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0); | |
aaba7d03 | 825 | if (gens) |
880a1e77 JH |
826 | { |
827 | int n = sk_GENERAL_NAME_num(gens); | |
828 | int i; | |
829 | ||
aaba7d03 | 830 | for (i = 0; i < n; ++i) |
880a1e77 JH |
831 | { |
832 | const GENERAL_NAME *gn = sk_GENERAL_NAME_value(gens, i); | |
833 | const char *certid; | |
834 | ||
aaba7d03 | 835 | if (gn->type != GEN_DNS) |
880a1e77 JH |
836 | continue; |
837 | got_altname = TRUE; | |
838 | certid = parse_dns_name(gn); | |
aaba7d03 | 839 | if (certid && *certid) |
880a1e77 | 840 | { |
aaba7d03 | 841 | if ((matched = match_name(certid, dane)) == 0) |
880a1e77 | 842 | continue; |
aaba7d03 | 843 | if (!(dane->mhost = OPENSSL_strdup(certid))) |
880a1e77 | 844 | matched = -1; |
f2f2c91b | 845 | DEBUG(D_tls) debug_printf("Dane name_check: matched SAN %s\n", certid); |
880a1e77 JH |
846 | break; |
847 | } | |
e682570f | 848 | } |
880a1e77 JH |
849 | GENERAL_NAMES_free(gens); |
850 | } | |
851 | ||
852 | /* | |
853 | * XXX: Should the subjectName be skipped when *any* altnames are present, | |
854 | * or only when DNS altnames are present? | |
855 | */ | |
f2f2c91b | 856 | if (!got_altname) |
880a1e77 JH |
857 | { |
858 | char *certid = parse_subject_name(cert); | |
f2f2c91b JH |
859 | if (certid != 0 && *certid && (matched = match_name(certid, dane)) != 0) |
860 | { | |
861 | DEBUG(D_tls) debug_printf("Dane name_check: matched SN %s\n", certid); | |
aaba7d03 | 862 | dane->mhost = OPENSSL_strdup(certid); |
f2f2c91b JH |
863 | } |
864 | if (certid) | |
865 | OPENSSL_free(certid); | |
880a1e77 JH |
866 | } |
867 | return matched; | |
e682570f TL |
868 | } |
869 | ||
880a1e77 JH |
870 | static int |
871 | verify_chain(X509_STORE_CTX *ctx) | |
e682570f | 872 | { |
880a1e77 JH |
873 | dane_selector_list issuer_rrs; |
874 | dane_selector_list leaf_rrs; | |
875 | int (*cb)(int, X509_STORE_CTX *) = ctx->verify_cb; | |
876 | int ssl_idx = SSL_get_ex_data_X509_STORE_CTX_idx(); | |
877 | SSL *ssl = X509_STORE_CTX_get_ex_data(ctx, ssl_idx); | |
878 | ssl_dane *dane = SSL_get_ex_data(ssl, dane_idx); | |
879 | X509 *cert = ctx->cert; /* XXX: accessor? */ | |
880 | int matched = 0; | |
881 | int chain_length = sk_X509_num(ctx->chain); | |
882 | ||
f2f2c91b | 883 | DEBUG(D_tls) debug_printf("Dane verify_chain\n"); |
6634ac8d | 884 | |
aaba7d03 VD |
885 | issuer_rrs = dane->selectors[DANESSL_USAGE_PKIX_TA]; |
886 | leaf_rrs = dane->selectors[DANESSL_USAGE_PKIX_EE]; | |
880a1e77 JH |
887 | ctx->verify = dane->verify; |
888 | ||
aaba7d03 | 889 | if ((matched = name_check(dane, cert)) < 0) |
880a1e77 JH |
890 | { |
891 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM); | |
892 | return 0; | |
893 | } | |
894 | ||
aaba7d03 | 895 | if (!matched) |
880a1e77 JH |
896 | { |
897 | ctx->error_depth = 0; | |
898 | ctx->current_cert = cert; | |
899 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH); | |
aaba7d03 | 900 | if (!cb(0, ctx)) |
880a1e77 JH |
901 | return 0; |
902 | } | |
903 | matched = 0; | |
904 | ||
f2f2c91b JH |
905 | /* |
906 | * Satisfy at least one usage 0 or 1 constraint, unless we've already | |
907 | * matched a usage 2 trust anchor. | |
908 | * | |
909 | * XXX: internal_verify() doesn't callback with top certs that are not | |
910 | * self-issued. This should be fixed in a future OpenSSL. | |
911 | */ | |
912 | if (dane->roots && sk_X509_num(dane->roots)) | |
913 | { | |
914 | X509 *top = sk_X509_value(ctx->chain, dane->depth); | |
880a1e77 | 915 | |
f2f2c91b JH |
916 | dane->mdpth = dane->depth; |
917 | dane->match = top; | |
918 | X509_up_ref(top); | |
aaba7d03 VD |
919 | |
920 | #ifndef NO_CALLBACK_WORKAROUND | |
f2f2c91b JH |
921 | if (X509_check_issued(top, top) != X509_V_OK) |
922 | { | |
923 | ctx->error_depth = dane->depth; | |
924 | ctx->current_cert = top; | |
925 | if (!cb(1, ctx)) | |
926 | return 0; | |
927 | } | |
880a1e77 JH |
928 | #endif |
929 | /* Pop synthetic trust-anchor ancestors off the chain! */ | |
930 | while (--chain_length > dane->depth) | |
931 | X509_free(sk_X509_pop(ctx->chain)); | |
932 | } | |
aaba7d03 | 933 | else |
880a1e77 | 934 | { |
aaba7d03 VD |
935 | int n = 0; |
936 | X509 *xn = cert; | |
880a1e77 JH |
937 | |
938 | /* | |
939 | * Check for an EE match, then a CA match at depths > 0, and | |
940 | * finally, if the EE cert is self-issued, for a depth 0 CA match. | |
941 | */ | |
aaba7d03 VD |
942 | if (leaf_rrs) |
943 | matched = match(leaf_rrs, xn, 0); | |
f2f2c91b | 944 | if (matched) DEBUG(D_tls) debug_printf("Dane verify_chain: matched EE\n"); |
880a1e77 | 945 | |
f92c5522 VD |
946 | if (!matched && issuer_rrs) |
947 | for (n = chain_length-1; !matched && n >= 0; --n) | |
aaba7d03 | 948 | { |
f92c5522 VD |
949 | xn = sk_X509_value(ctx->chain, n); |
950 | if (n > 0 || X509_check_issued(xn, xn) == X509_V_OK) | |
951 | matched = match(issuer_rrs, xn, n); | |
aaba7d03 | 952 | } |
f2f2c91b JH |
953 | if (matched) DEBUG(D_tls) debug_printf("Dane verify_chain: matched %s\n", |
954 | n>0 ? "CA" : "selfisssued EE"); | |
f92c5522 VD |
955 | |
956 | if (!matched) | |
957 | { | |
958 | ctx->current_cert = cert; | |
959 | ctx->error_depth = 0; | |
960 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_UNTRUSTED); | |
961 | if (!cb(0, ctx)) | |
962 | return 0; | |
963 | } | |
964 | else | |
965 | { | |
966 | dane->mdpth = n; | |
967 | dane->match = xn; | |
968 | X509_up_ref(xn); | |
e682570f | 969 | } |
f92c5522 | 970 | } |
e682570f | 971 | |
aaba7d03 VD |
972 | return ctx->verify(ctx); |
973 | } | |
e682570f | 974 | |
aaba7d03 VD |
975 | static void |
976 | dane_reset(ssl_dane *dane) | |
977 | { | |
978 | dane->depth = -1; | |
979 | if (dane->mhost) | |
980 | { | |
981 | OPENSSL_free(dane->mhost); | |
982 | dane->mhost = 0; | |
880a1e77 | 983 | } |
aaba7d03 VD |
984 | if (dane->roots) |
985 | { | |
986 | sk_X509_pop_free(dane->roots, X509_free); | |
987 | dane->roots = 0; | |
988 | } | |
989 | if (dane->chain) | |
990 | { | |
991 | sk_X509_pop_free(dane->chain, X509_free); | |
992 | dane->chain = 0; | |
993 | } | |
994 | if (dane->match) | |
995 | { | |
996 | X509_free(dane->match); | |
997 | dane->match = 0; | |
998 | } | |
999 | dane->mdpth = -1; | |
e682570f TL |
1000 | } |
1001 | ||
880a1e77 JH |
1002 | static int |
1003 | verify_cert(X509_STORE_CTX *ctx, void *unused_ctx) | |
e682570f | 1004 | { |
880a1e77 JH |
1005 | static int ssl_idx = -1; |
1006 | SSL *ssl; | |
1007 | ssl_dane *dane; | |
1008 | int (*cb)(int, X509_STORE_CTX *) = ctx->verify_cb; | |
1009 | int matched; | |
1010 | X509 *cert = ctx->cert; /* XXX: accessor? */ | |
1011 | ||
f2f2c91b | 1012 | DEBUG(D_tls) debug_printf("Dane verify_cert\n"); |
6634ac8d | 1013 | |
aaba7d03 | 1014 | if (ssl_idx < 0) |
880a1e77 | 1015 | ssl_idx = SSL_get_ex_data_X509_STORE_CTX_idx(); |
aaba7d03 | 1016 | if (dane_idx < 0) |
880a1e77 | 1017 | { |
aaba7d03 | 1018 | DANEerr(DANESSL_F_VERIFY_CERT, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1019 | return -1; |
1020 | } | |
1021 | ||
1022 | ssl = X509_STORE_CTX_get_ex_data(ctx, ssl_idx); | |
aaba7d03 | 1023 | if (!(dane = SSL_get_ex_data(ssl, dane_idx)) || !cert) |
880a1e77 JH |
1024 | return X509_verify_cert(ctx); |
1025 | ||
f2f2c91b | 1026 | /* Reset for verification of a new chain, perhaps a renegotiation. */ |
aaba7d03 VD |
1027 | dane_reset(dane); |
1028 | ||
1029 | if (dane->selectors[DANESSL_USAGE_DANE_EE]) | |
880a1e77 | 1030 | { |
aaba7d03 | 1031 | if ((matched = check_end_entity(ctx, dane, cert)) > 0) |
880a1e77 JH |
1032 | { |
1033 | ctx->error_depth = 0; | |
1034 | ctx->current_cert = cert; | |
1035 | return cb(1, ctx); | |
e682570f | 1036 | } |
aaba7d03 | 1037 | if (matched < 0) |
880a1e77 JH |
1038 | { |
1039 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM); | |
1040 | return -1; | |
e682570f | 1041 | } |
880a1e77 | 1042 | } |
e682570f | 1043 | |
aaba7d03 | 1044 | if (dane->selectors[DANESSL_USAGE_DANE_TA]) |
880a1e77 | 1045 | { |
aaba7d03 VD |
1046 | if ((matched = set_trust_anchor(ctx, dane, cert)) < 0) |
1047 | { | |
1048 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM); | |
1049 | return -1; | |
1050 | } | |
1051 | if (matched) | |
1052 | { | |
1053 | /* | |
1054 | * Check that setting the untrusted chain updates the expected | |
1055 | * structure member at the expected offset. | |
1056 | */ | |
1057 | X509_STORE_CTX_trusted_stack(ctx, dane->roots); | |
1058 | X509_STORE_CTX_set_chain(ctx, dane->chain); | |
1059 | OPENSSL_assert(ctx->untrusted == dane->chain); | |
1060 | } | |
880a1e77 | 1061 | } |
e682570f | 1062 | |
aaba7d03 VD |
1063 | /* |
1064 | * Name checks and usage 0/1 constraint enforcement are delayed until | |
1065 | * X509_verify_cert() builds the full chain and calls our verify_chain() | |
1066 | * wrapper. | |
1067 | */ | |
1068 | dane->verify = ctx->verify; | |
1069 | ctx->verify = verify_chain; | |
1070 | ||
1071 | if (X509_verify_cert(ctx)) | |
1072 | return 1; | |
880a1e77 | 1073 | |
aaba7d03 VD |
1074 | /* |
1075 | * If the chain is invalid, clear any matching cert or hostname, to | |
1076 | * protect callers that might erroneously rely on these alone without | |
1077 | * checking the validation status. | |
1078 | */ | |
1079 | if (dane->match) | |
1080 | { | |
1081 | X509_free(dane->match); | |
1082 | dane->match = 0; | |
1083 | } | |
1084 | if (dane->mhost) | |
1085 | { | |
1086 | OPENSSL_free(dane->mhost); | |
1087 | dane->mhost = 0; | |
1088 | } | |
1089 | return 0; | |
e682570f TL |
1090 | } |
1091 | ||
880a1e77 JH |
1092 | static dane_list |
1093 | list_alloc(size_t vsize) | |
e682570f | 1094 | { |
880a1e77 JH |
1095 | void *value = (void *) OPENSSL_malloc(vsize); |
1096 | dane_list l; | |
1097 | ||
aaba7d03 | 1098 | if (!value) |
880a1e77 | 1099 | { |
aaba7d03 | 1100 | DANEerr(DANESSL_F_LIST_ALLOC, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1101 | return 0; |
1102 | } | |
aaba7d03 | 1103 | if (!(l = (dane_list) OPENSSL_malloc(sizeof(*l)))) |
880a1e77 JH |
1104 | { |
1105 | OPENSSL_free(value); | |
aaba7d03 | 1106 | DANEerr(DANESSL_F_LIST_ALLOC, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1107 | return 0; |
1108 | } | |
1109 | l->next = 0; | |
1110 | l->value = value; | |
1111 | return l; | |
e682570f TL |
1112 | } |
1113 | ||
880a1e77 JH |
1114 | static void |
1115 | list_free(void *list, void (*f)(void *)) | |
e682570f | 1116 | { |
880a1e77 JH |
1117 | dane_list head; |
1118 | dane_list next; | |
1119 | ||
aaba7d03 | 1120 | for (head = (dane_list) list; head; head = next) |
880a1e77 JH |
1121 | { |
1122 | next = head->next; | |
1123 | if (f && head->value) | |
1124 | f(head->value); | |
1125 | OPENSSL_free(head); | |
1126 | } | |
e682570f TL |
1127 | } |
1128 | ||
880a1e77 JH |
1129 | static void |
1130 | dane_mtype_free(void *p) | |
e682570f | 1131 | { |
aaba7d03 | 1132 | list_free(((dane_mtype) p)->data, CRYPTO_free); |
880a1e77 | 1133 | OPENSSL_free(p); |
e682570f TL |
1134 | } |
1135 | ||
880a1e77 JH |
1136 | static void |
1137 | dane_selector_free(void *p) | |
e682570f | 1138 | { |
880a1e77 JH |
1139 | list_free(((dane_selector) p)->mtype, dane_mtype_free); |
1140 | OPENSSL_free(p); | |
e682570f TL |
1141 | } |
1142 | ||
946ecbe0 JH |
1143 | |
1144 | ||
1145 | /* | |
1146 | ||
1147 | Tidy up once the connection is finished with. | |
1148 | ||
1149 | Arguments | |
1150 | ssl The ssl connection handle | |
1151 | ||
1152 | => Before calling SSL_free() | |
1153 | tls_close() and tls_getc() [the error path] are the obvious places. | |
1154 | Could we do it earlier - right after verification? In tls_client_start() | |
1155 | right after SSL_connect() returns, in that case. | |
1156 | ||
1157 | */ | |
1158 | ||
880a1e77 JH |
1159 | void |
1160 | DANESSL_cleanup(SSL *ssl) | |
e682570f | 1161 | { |
880a1e77 JH |
1162 | ssl_dane *dane; |
1163 | int u; | |
1164 | ||
e5cccda9 | 1165 | DEBUG(D_tls) debug_printf("Dane lib-cleanup\n"); |
6634ac8d | 1166 | |
aaba7d03 | 1167 | if (dane_idx < 0 || !(dane = SSL_get_ex_data(ssl, dane_idx))) |
880a1e77 JH |
1168 | return; |
1169 | (void) SSL_set_ex_data(ssl, dane_idx, 0); | |
1170 | ||
aaba7d03 VD |
1171 | dane_reset(dane); |
1172 | if (dane->hosts) | |
1173 | list_free(dane->hosts, CRYPTO_free); | |
1174 | for (u = 0; u <= DANESSL_USAGE_LAST; ++u) | |
1175 | if (dane->selectors[u]) | |
880a1e77 | 1176 | list_free(dane->selectors[u], dane_selector_free); |
aaba7d03 | 1177 | if (dane->pkeys) |
880a1e77 | 1178 | list_free(dane->pkeys, pkey_free); |
aaba7d03 | 1179 | if (dane->certs) |
880a1e77 | 1180 | list_free(dane->certs, cert_free); |
880a1e77 | 1181 | OPENSSL_free(dane); |
e682570f TL |
1182 | } |
1183 | ||
880a1e77 JH |
1184 | static dane_host_list |
1185 | host_list_init(const char **src) | |
e682570f | 1186 | { |
880a1e77 JH |
1187 | dane_host_list head = NULL; |
1188 | ||
aaba7d03 | 1189 | while (*src) |
880a1e77 JH |
1190 | { |
1191 | dane_host_list elem = (dane_host_list) OPENSSL_malloc(sizeof(*elem)); | |
aaba7d03 | 1192 | if (elem == 0) |
880a1e77 | 1193 | { |
aaba7d03 | 1194 | list_free(head, CRYPTO_free); |
880a1e77 | 1195 | return 0; |
e682570f | 1196 | } |
880a1e77 JH |
1197 | elem->value = OPENSSL_strdup(*src++); |
1198 | LINSERT(head, elem); | |
1199 | } | |
1200 | return head; | |
e682570f TL |
1201 | } |
1202 | ||
946ecbe0 | 1203 | |
aaba7d03 VD |
1204 | int |
1205 | DANESSL_get_match_cert(SSL *ssl, X509 **match, const char **mhost, int *depth) | |
1206 | { | |
1207 | ssl_dane *dane; | |
1208 | ||
1209 | if (dane_idx < 0 || (dane = SSL_get_ex_data(ssl, dane_idx)) == 0) | |
1210 | { | |
1211 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_INIT); | |
1212 | return -1; | |
1213 | } | |
1214 | ||
1215 | if (dane->match) | |
1216 | { | |
1217 | if (match) | |
1218 | *match = dane->match; | |
1219 | if (mhost) | |
1220 | *mhost = dane->mhost; | |
1221 | if (depth) | |
1222 | *depth = dane->mdpth; | |
1223 | } | |
1224 | ||
1225 | return (dane->match != 0); | |
1226 | } | |
1227 | ||
1228 | ||
1229 | #ifdef never_called | |
1230 | int | |
1231 | DANESSL_verify_chain(SSL *ssl, STACK_OF(X509) *chain) | |
1232 | { | |
1233 | int ret; | |
1234 | X509 *cert; | |
1235 | X509_STORE_CTX store_ctx; | |
1236 | SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl); | |
1237 | X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx); | |
1238 | int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx(); | |
1239 | ||
1240 | cert = sk_X509_value(chain, 0); | |
1241 | if (!X509_STORE_CTX_init(&store_ctx, store, cert, chain)) | |
1242 | return 0; | |
1243 | X509_STORE_CTX_set_ex_data(&store_ctx, store_ctx_idx, ssl); | |
1244 | ||
1245 | X509_STORE_CTX_set_default(&store_ctx, | |
1246 | SSL_is_server(ssl) ? "ssl_client" : "ssl_server"); | |
1247 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&store_ctx), | |
1248 | SSL_get0_param(ssl)); | |
1249 | ||
1250 | if (SSL_get_verify_callback(ssl)) | |
1251 | X509_STORE_CTX_set_verify_cb(&store_ctx, SSL_get_verify_callback(ssl)); | |
1252 | ||
1253 | ret = verify_cert(&store_ctx, NULL); | |
1254 | ||
1255 | SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(&store_ctx)); | |
1256 | X509_STORE_CTX_cleanup(&store_ctx); | |
1257 | ||
1258 | return (ret); | |
1259 | } | |
1260 | #endif | |
1261 | ||
1262 | ||
946ecbe0 JH |
1263 | |
1264 | ||
1265 | /* | |
1266 | ||
1267 | Call this for each TLSA record found for the target, after the | |
1268 | DANE setup has been done on the ssl connection handle. | |
1269 | ||
1270 | Arguments: | |
1271 | ssl Connection handle | |
1272 | usage TLSA record field | |
1273 | selector TLSA record field | |
1274 | mdname ??? message digest name? | |
1275 | data ??? TLSA record megalump? | |
1276 | dlen length of data | |
1277 | ||
1278 | Return | |
1279 | -1 on error | |
1280 | 0 action not taken | |
1281 | 1 record accepted | |
1282 | */ | |
1283 | ||
880a1e77 JH |
1284 | int |
1285 | DANESSL_add_tlsa(SSL *ssl, uint8_t usage, uint8_t selector, const char *mdname, | |
1286 | unsigned const char *data, size_t dlen) | |
e682570f | 1287 | { |
880a1e77 JH |
1288 | ssl_dane *dane; |
1289 | dane_selector_list s = 0; | |
1290 | dane_mtype_list m = 0; | |
1291 | dane_data_list d = 0; | |
1292 | dane_cert_list xlist = 0; | |
1293 | dane_pkey_list klist = 0; | |
1294 | const EVP_MD *md = 0; | |
1295 | ||
b4161d10 JH |
1296 | DEBUG(D_tls) debug_printf("Dane add-tlsa: usage %u sel %u mdname \"%s\"\n", |
1297 | usage, selector, mdname); | |
6634ac8d | 1298 | |
880a1e77 JH |
1299 | if(dane_idx < 0 || !(dane = SSL_get_ex_data(ssl, dane_idx))) |
1300 | { | |
aaba7d03 | 1301 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_INIT); |
880a1e77 JH |
1302 | return -1; |
1303 | } | |
1304 | ||
aaba7d03 | 1305 | if (usage > DANESSL_USAGE_LAST) |
880a1e77 | 1306 | { |
aaba7d03 | 1307 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_USAGE); |
880a1e77 JH |
1308 | return 0; |
1309 | } | |
aaba7d03 | 1310 | if (selector > DANESSL_SELECTOR_LAST) |
880a1e77 | 1311 | { |
aaba7d03 | 1312 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_SELECTOR); |
880a1e77 JH |
1313 | return 0; |
1314 | } | |
1315 | ||
aaba7d03 VD |
1316 | /* Support built-in standard one-digit mtypes */ |
1317 | if (mdname && *mdname && mdname[1] == '\0') | |
1318 | switch (*mdname - '0') | |
1319 | { | |
1320 | case DANESSL_MATCHING_FULL: mdname = 0; break; | |
1321 | case DANESSL_MATCHING_2256: mdname = "sha256"; break; | |
1322 | case DANESSL_MATCHING_2512: mdname = "sha512"; break; | |
1323 | } | |
1324 | if (mdname && *mdname && (md = EVP_get_digestbyname(mdname)) == 0) | |
1325 | { | |
1326 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_DIGEST); | |
1327 | return 0; | |
1328 | } | |
1329 | if (mdname && *mdname && dlen != EVP_MD_size(md)) | |
1330 | { | |
1331 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_DATA_LENGTH); | |
1332 | return 0; | |
1333 | } | |
1334 | if (!data) | |
1335 | { | |
1336 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_NULL_DATA); | |
1337 | return 0; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Full Certificate or Public Key when NULL or empty digest name | |
1342 | */ | |
1343 | if (!mdname || !*mdname) | |
1344 | { | |
1345 | X509 *x = 0; | |
1346 | EVP_PKEY *k = 0; | |
1347 | const unsigned char *p = data; | |
e682570f TL |
1348 | |
1349 | #define xklistinit(lvar, ltype, var, freeFunc) do { \ | |
aaba7d03 VD |
1350 | (lvar) = (ltype) OPENSSL_malloc(sizeof(*(lvar))); \ |
1351 | if ((lvar) == 0) { \ | |
1352 | DANEerr(DANESSL_F_ADD_TLSA, ERR_R_MALLOC_FAILURE); \ | |
1353 | freeFunc((var)); \ | |
1354 | return 0; \ | |
1355 | } \ | |
1356 | (lvar)->next = 0; \ | |
1357 | lvar->value = var; \ | |
1358 | } while (0) | |
e682570f | 1359 | #define xkfreeret(ret) do { \ |
aaba7d03 VD |
1360 | if (xlist) list_free(xlist, cert_free); \ |
1361 | if (klist) list_free(klist, pkey_free); \ | |
1362 | return (ret); \ | |
1363 | } while (0) | |
880a1e77 | 1364 | |
aaba7d03 | 1365 | switch (selector) |
880a1e77 | 1366 | { |
aaba7d03 VD |
1367 | case DANESSL_SELECTOR_CERT: |
1368 | if (!d2i_X509(&x, &p, dlen) || dlen != p - data) | |
880a1e77 JH |
1369 | { |
1370 | if (x) | |
aaba7d03 VD |
1371 | X509_free(x); |
1372 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_CERT); | |
880a1e77 JH |
1373 | return 0; |
1374 | } | |
1375 | k = X509_get_pubkey(x); | |
1376 | EVP_PKEY_free(k); | |
aaba7d03 | 1377 | if (k == 0) |
880a1e77 JH |
1378 | { |
1379 | X509_free(x); | |
aaba7d03 | 1380 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_CERT_PKEY); |
880a1e77 JH |
1381 | return 0; |
1382 | } | |
aaba7d03 | 1383 | if (usage == DANESSL_USAGE_DANE_TA) |
880a1e77 JH |
1384 | xklistinit(xlist, dane_cert_list, x, X509_free); |
1385 | break; | |
1386 | ||
aaba7d03 VD |
1387 | case DANESSL_SELECTOR_SPKI: |
1388 | if (!d2i_PUBKEY(&k, &p, dlen) || dlen != p - data) | |
880a1e77 | 1389 | { |
aaba7d03 | 1390 | if (k) |
880a1e77 | 1391 | EVP_PKEY_free(k); |
aaba7d03 VD |
1392 | DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_PKEY); |
1393 | return 0; | |
880a1e77 | 1394 | } |
aaba7d03 | 1395 | if (usage == DANESSL_USAGE_DANE_TA) |
880a1e77 JH |
1396 | xklistinit(klist, dane_pkey_list, k, EVP_PKEY_free); |
1397 | break; | |
e682570f | 1398 | } |
880a1e77 JH |
1399 | } |
1400 | ||
1401 | /* Find insertion point and don't add duplicate elements. */ | |
aaba7d03 VD |
1402 | for (s = dane->selectors[usage]; s; s = s->next) |
1403 | if (s->value->selector == selector) | |
1404 | { | |
1405 | for (m = s->value->mtype; m; m = m->next) | |
1406 | if (m->value->md == md) | |
1407 | { | |
1408 | for (d = m->value->data; d; d = d->next) | |
1409 | if ( d->value->datalen == dlen | |
1410 | && memcmp(d->value->data, data, dlen) == 0) | |
880a1e77 | 1411 | xkfreeret(1); |
aaba7d03 VD |
1412 | break; |
1413 | } | |
1414 | break; | |
1415 | } | |
880a1e77 | 1416 | |
aaba7d03 | 1417 | if ((d = (dane_data_list) list_alloc(sizeof(*d->value) + dlen)) == 0) |
880a1e77 JH |
1418 | xkfreeret(0); |
1419 | d->value->datalen = dlen; | |
1420 | memcpy(d->value->data, data, dlen); | |
aaba7d03 | 1421 | if (!m) |
880a1e77 | 1422 | { |
aaba7d03 | 1423 | if ((m = (dane_mtype_list) list_alloc(sizeof(*m->value))) == 0) |
880a1e77 | 1424 | { |
aaba7d03 | 1425 | list_free(d, CRYPTO_free); |
880a1e77 | 1426 | xkfreeret(0); |
e682570f | 1427 | } |
880a1e77 | 1428 | m->value->data = 0; |
aaba7d03 | 1429 | if ((m->value->md = md) != 0) |
880a1e77 | 1430 | m->value->mdlen = dlen; |
aaba7d03 | 1431 | if (!s) |
880a1e77 | 1432 | { |
aaba7d03 | 1433 | if ((s = (dane_selector_list) list_alloc(sizeof(*s->value))) == 0) |
880a1e77 JH |
1434 | { |
1435 | list_free(m, dane_mtype_free); | |
1436 | xkfreeret(0); | |
1437 | } | |
1438 | s->value->mtype = 0; | |
1439 | s->value->selector = selector; | |
1440 | LINSERT(dane->selectors[usage], s); | |
1441 | } | |
1442 | LINSERT(s->value->mtype, m); | |
1443 | } | |
1444 | LINSERT(m->value->data, d); | |
1445 | ||
aaba7d03 | 1446 | if (xlist) |
880a1e77 | 1447 | LINSERT(dane->certs, xlist); |
aaba7d03 | 1448 | else if (klist) |
880a1e77 JH |
1449 | LINSERT(dane->pkeys, klist); |
1450 | ++dane->count; | |
1451 | return 1; | |
e682570f TL |
1452 | } |
1453 | ||
946ecbe0 JH |
1454 | |
1455 | ||
1456 | ||
1457 | /* | |
1458 | Call this once we have an ssl connection handle but before | |
1459 | making the TLS connection. | |
1460 | ||
1461 | => In tls_client_start() after the call to SSL_new() | |
1462 | and before the call to SSL_connect(). Exactly where | |
1463 | probably does not matter. | |
1464 | We probably want to keep our existing SNI handling; | |
1465 | call this with NULL. | |
1466 | ||
1467 | Arguments: | |
1468 | ssl Connection handle | |
1469 | sni_domain Optional peer server name | |
868f5672 | 1470 | hostnames list of names to chack against peer cert |
946ecbe0 JH |
1471 | |
1472 | Return | |
1473 | -1 on fatal error | |
1474 | 0 nonfatal error | |
1475 | 1 success | |
1476 | */ | |
1477 | ||
880a1e77 JH |
1478 | int |
1479 | DANESSL_init(SSL *ssl, const char *sni_domain, const char **hostnames) | |
e682570f | 1480 | { |
880a1e77 JH |
1481 | ssl_dane *dane; |
1482 | int i; | |
e682570f | 1483 | |
aaba7d03 VD |
1484 | DEBUG(D_tls) debug_printf("Dane ssl_init\n"); |
1485 | if (dane_idx < 0) | |
880a1e77 | 1486 | { |
aaba7d03 | 1487 | DANEerr(DANESSL_F_INIT, DANESSL_R_LIBRARY_INIT); |
880a1e77 JH |
1488 | return -1; |
1489 | } | |
e682570f | 1490 | |
aaba7d03 VD |
1491 | if (sni_domain && !SSL_set_tlsext_host_name(ssl, sni_domain)) |
1492 | return 0; | |
e682570f | 1493 | |
aaba7d03 | 1494 | if ((dane = (ssl_dane *) OPENSSL_malloc(sizeof(ssl_dane))) == 0) |
880a1e77 | 1495 | { |
aaba7d03 | 1496 | DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1497 | return 0; |
1498 | } | |
aaba7d03 | 1499 | if (!SSL_set_ex_data(ssl, dane_idx, dane)) |
880a1e77 | 1500 | { |
aaba7d03 | 1501 | DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1502 | OPENSSL_free(dane); |
1503 | return 0; | |
1504 | } | |
1505 | ||
6634ac8d JH |
1506 | dane->verify = 0; |
1507 | dane->hosts = 0; | |
1508 | dane->thost = 0; | |
880a1e77 JH |
1509 | dane->pkeys = 0; |
1510 | dane->certs = 0; | |
1511 | dane->chain = 0; | |
aaba7d03 | 1512 | dane->match = 0; |
880a1e77 JH |
1513 | dane->roots = 0; |
1514 | dane->depth = -1; | |
aaba7d03 VD |
1515 | dane->mhost = 0; /* Future SSL control interface */ |
1516 | dane->mdpth = 0; /* Future SSL control interface */ | |
1517 | dane->multi = 0; /* Future SSL control interface */ | |
880a1e77 | 1518 | dane->count = 0; |
aaba7d03 | 1519 | dane->hosts = 0; |
880a1e77 | 1520 | |
aaba7d03 VD |
1521 | for (i = 0; i <= DANESSL_USAGE_LAST; ++i) |
1522 | dane->selectors[i] = 0; | |
880a1e77 | 1523 | |
aaba7d03 | 1524 | if (hostnames && (dane->hosts = host_list_init(hostnames)) == 0) |
880a1e77 | 1525 | { |
aaba7d03 | 1526 | DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE); |
880a1e77 JH |
1527 | DANESSL_cleanup(ssl); |
1528 | return 0; | |
1529 | } | |
1530 | ||
1531 | return 1; | |
e682570f TL |
1532 | } |
1533 | ||
946ecbe0 JH |
1534 | |
1535 | /* | |
1536 | ||
1537 | Call this once we have a context to work with, but | |
1538 | before DANESSL_init() | |
1539 | ||
1540 | => in tls_client_start(), after tls_init() call gives us the ctx, | |
1541 | if we decide we want to (policy) and can (TLSA records available) | |
1542 | replacing (? what about fallback) everything from testing tls_verify_hosts | |
1543 | down to just before calling SSL_new() for the conn handle. | |
1544 | ||
1545 | Arguments | |
1546 | ctx SSL context | |
1547 | ||
1548 | Return | |
1549 | -1 Error | |
1550 | 1 Success | |
1551 | */ | |
1552 | ||
880a1e77 JH |
1553 | int |
1554 | DANESSL_CTX_init(SSL_CTX *ctx) | |
e682570f | 1555 | { |
6634ac8d | 1556 | DEBUG(D_tls) debug_printf("Dane ctx-init\n"); |
aaba7d03 | 1557 | if (dane_idx >= 0) |
880a1e77 JH |
1558 | { |
1559 | SSL_CTX_set_cert_verify_callback(ctx, verify_cert, 0); | |
1560 | return 1; | |
1561 | } | |
aaba7d03 | 1562 | DANEerr(DANESSL_F_CTX_INIT, DANESSL_R_LIBRARY_INIT); |
880a1e77 | 1563 | return -1; |
e682570f TL |
1564 | } |
1565 | ||
880a1e77 JH |
1566 | static int |
1567 | init_once(volatile int *value, int (*init)(void), void (*postinit)(void)) | |
e682570f | 1568 | { |
880a1e77 JH |
1569 | int wlock = 0; |
1570 | ||
1571 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); | |
aaba7d03 | 1572 | if (*value < 0) |
880a1e77 JH |
1573 | { |
1574 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); | |
1575 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); | |
1576 | wlock = 1; | |
aaba7d03 | 1577 | if (*value < 0) |
880a1e77 JH |
1578 | { |
1579 | *value = init(); | |
aaba7d03 | 1580 | if (postinit) |
880a1e77 | 1581 | postinit(); |
e682570f | 1582 | } |
880a1e77 JH |
1583 | } |
1584 | if (wlock) | |
1585 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); | |
1586 | else | |
1587 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); | |
1588 | return *value; | |
e682570f TL |
1589 | } |
1590 | ||
880a1e77 JH |
1591 | static void |
1592 | dane_init(void) | |
e682570f | 1593 | { |
880a1e77 JH |
1594 | /* |
1595 | * Store library id in zeroth function slot, used to locate the library | |
1596 | * name. This must be done before we load the error strings. | |
1597 | */ | |
e682570f | 1598 | #ifndef OPENSSL_NO_ERR |
880a1e77 JH |
1599 | dane_str_functs[0].error |= ERR_PACK(err_lib_dane, 0, 0); |
1600 | ERR_load_strings(err_lib_dane, dane_str_functs); | |
1601 | ERR_load_strings(err_lib_dane, dane_str_reasons); | |
e682570f TL |
1602 | #endif |
1603 | ||
880a1e77 JH |
1604 | /* |
1605 | * Register SHA-2 digests, if implemented and not already registered. | |
1606 | */ | |
e682570f | 1607 | #if defined(LN_sha256) && defined(NID_sha256) && !defined(OPENSSL_NO_SHA256) |
aaba7d03 VD |
1608 | if (!EVP_get_digestbyname(LN_sha224)) EVP_add_digest(EVP_sha224()); |
1609 | if (!EVP_get_digestbyname(LN_sha256)) EVP_add_digest(EVP_sha256()); | |
e682570f TL |
1610 | #endif |
1611 | #if defined(LN_sha512) && defined(NID_sha512) && !defined(OPENSSL_NO_SHA512) | |
aaba7d03 VD |
1612 | if (!EVP_get_digestbyname(LN_sha384)) EVP_add_digest(EVP_sha384()); |
1613 | if (!EVP_get_digestbyname(LN_sha512)) EVP_add_digest(EVP_sha512()); | |
e682570f TL |
1614 | #endif |
1615 | ||
880a1e77 JH |
1616 | /* |
1617 | * Register an SSL index for the connection-specific ssl_dane structure. | |
1618 | * Using a separate index makes it possible to add DANE support to | |
1619 | * existing OpenSSL releases that don't have a suitable pointer in the | |
1620 | * SSL structure. | |
1621 | */ | |
1622 | dane_idx = SSL_get_ex_new_index(0, 0, 0, 0, 0); | |
e682570f TL |
1623 | } |
1624 | ||
946ecbe0 JH |
1625 | |
1626 | ||
1627 | /* | |
1628 | ||
1629 | Call this once. Probably early in startup will do; may need | |
1630 | to be after SSL library init. | |
1631 | ||
043b1248 JH |
1632 | => put after call to tls_init() for now |
1633 | ||
1634 | Return | |
1635 | 1 Success | |
1636 | 0 Fail | |
946ecbe0 JH |
1637 | */ |
1638 | ||
880a1e77 JH |
1639 | int |
1640 | DANESSL_library_init(void) | |
e682570f | 1641 | { |
6634ac8d | 1642 | DEBUG(D_tls) debug_printf("Dane lib-init\n"); |
aaba7d03 | 1643 | if (err_lib_dane < 0) |
880a1e77 | 1644 | init_once(&err_lib_dane, ERR_get_next_error_library, dane_init); |
e682570f TL |
1645 | |
1646 | #if defined(LN_sha256) | |
880a1e77 | 1647 | /* No DANE without SHA256 support */ |
aaba7d03 | 1648 | if (dane_idx >= 0 && EVP_get_digestbyname(LN_sha256) != 0) |
880a1e77 | 1649 | return 1; |
e682570f | 1650 | #endif |
aaba7d03 | 1651 | DANEerr(DANESSL_F_LIBRARY_INIT, DANESSL_R_SUPPORT); |
880a1e77 | 1652 | return 0; |
e682570f TL |
1653 | } |
1654 | ||
946ecbe0 | 1655 | |
e682570f | 1656 | #endif /* OPENSSL_VERSION_NUMBER */ |
880a1e77 JH |
1657 | /* vi: aw ai sw=2 |
1658 | */ |