]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 BM |
12 | #include <openssl/asn1.h> |
13 | #include <openssl/objects.h> | |
14 | #include <openssl/x509.h> | |
e947f396 | 15 | #include <openssl/x509v3.h> |
47b4ccea | 16 | #include <openssl/core_names.h> |
25f2138b | 17 | #include "crypto/x509.h" |
d02b48c6 | 18 | |
ccd86b68 | 19 | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
0f113f3e MC |
20 | { |
21 | int i; | |
5cf6abd8 | 22 | const X509_CINF *ai, *bi; |
0f113f3e | 23 | |
e0331eb8 DDO |
24 | if (b == NULL) |
25 | return a != NULL; | |
26 | if (a == NULL) | |
27 | return -1; | |
5cf6abd8 DSH |
28 | ai = &a->cert_info; |
29 | bi = &b->cert_info; | |
81e49438 | 30 | i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber); |
bc64c5a6 DDO |
31 | if (i != 0) |
32 | return i < 0 ? -1 : 1; | |
26a7d938 | 33 | return X509_NAME_cmp(ai->issuer, bi->issuer); |
0f113f3e | 34 | } |
d02b48c6 | 35 | |
cf1b7d96 | 36 | #ifndef OPENSSL_NO_MD5 |
6b691a5c | 37 | unsigned long X509_issuer_and_serial_hash(X509 *a) |
0f113f3e MC |
38 | { |
39 | unsigned long ret = 0; | |
bfb0641f | 40 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
0f113f3e MC |
41 | unsigned char md[16]; |
42 | char *f; | |
43 | ||
6e59a892 RL |
44 | if (ctx == NULL) |
45 | goto err; | |
5cf6abd8 | 46 | f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0); |
6e59a892 | 47 | if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL)) |
0f113f3e | 48 | goto err; |
6e59a892 | 49 | if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f))) |
0f113f3e MC |
50 | goto err; |
51 | OPENSSL_free(f); | |
52 | if (!EVP_DigestUpdate | |
6e59a892 | 53 | (ctx, (unsigned char *)a->cert_info.serialNumber.data, |
81e49438 | 54 | (unsigned long)a->cert_info.serialNumber.length)) |
0f113f3e | 55 | goto err; |
6e59a892 | 56 | if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL)) |
0f113f3e MC |
57 | goto err; |
58 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | | |
59 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) | |
60 | ) & 0xffffffffL; | |
61 | err: | |
bfb0641f | 62 | EVP_MD_CTX_free(ctx); |
26a7d938 | 63 | return ret; |
0f113f3e | 64 | } |
d02b48c6 | 65 | #endif |
0f113f3e | 66 | |
ccd86b68 | 67 | int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
0f113f3e | 68 | { |
26a7d938 | 69 | return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer); |
0f113f3e | 70 | } |
d02b48c6 | 71 | |
ccd86b68 | 72 | int X509_subject_name_cmp(const X509 *a, const X509 *b) |
0f113f3e | 73 | { |
26a7d938 | 74 | return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject); |
0f113f3e | 75 | } |
d02b48c6 | 76 | |
ccd86b68 | 77 | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
0f113f3e | 78 | { |
26a7d938 | 79 | return X509_NAME_cmp(a->crl.issuer, b->crl.issuer); |
0f113f3e | 80 | } |
d02b48c6 | 81 | |
edc54021 | 82 | int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) |
0f113f3e | 83 | { |
f2a04587 DDO |
84 | int rv; |
85 | ||
86 | if ((a->flags & EXFLAG_NO_FINGERPRINT) == 0 | |
87 | && (b->flags & EXFLAG_NO_FINGERPRINT) == 0) | |
88 | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); | |
89 | else | |
90 | return -2; | |
bc64c5a6 DDO |
91 | |
92 | return rv < 0 ? -1 : rv > 0; | |
0f113f3e | 93 | } |
edc54021 | 94 | |
1421aead | 95 | X509_NAME *X509_get_issuer_name(const X509 *a) |
0f113f3e | 96 | { |
26a7d938 | 97 | return a->cert_info.issuer; |
0f113f3e | 98 | } |
d02b48c6 | 99 | |
6b691a5c | 100 | unsigned long X509_issuer_name_hash(X509 *x) |
0f113f3e | 101 | { |
bf973d06 | 102 | return X509_NAME_hash_ex(x->cert_info.issuer, NULL, NULL, NULL); |
0f113f3e | 103 | } |
d02b48c6 | 104 | |
0e0c6821 DSH |
105 | #ifndef OPENSSL_NO_MD5 |
106 | unsigned long X509_issuer_name_hash_old(X509 *x) | |
0f113f3e | 107 | { |
26a7d938 | 108 | return X509_NAME_hash_old(x->cert_info.issuer); |
0f113f3e | 109 | } |
0e0c6821 DSH |
110 | #endif |
111 | ||
1421aead | 112 | X509_NAME *X509_get_subject_name(const X509 *a) |
0f113f3e | 113 | { |
26a7d938 | 114 | return a->cert_info.subject; |
0f113f3e | 115 | } |
d02b48c6 | 116 | |
22293ea1 | 117 | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
0f113f3e | 118 | { |
81e49438 | 119 | return &a->cert_info.serialNumber; |
0f113f3e | 120 | } |
d02b48c6 | 121 | |
68c12bfc DSH |
122 | const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a) |
123 | { | |
124 | return &a->cert_info.serialNumber; | |
125 | } | |
126 | ||
6b691a5c | 127 | unsigned long X509_subject_name_hash(X509 *x) |
0f113f3e | 128 | { |
bf973d06 | 129 | return X509_NAME_hash_ex(x->cert_info.subject, NULL, NULL, NULL); |
0f113f3e | 130 | } |
731d9c5f | 131 | |
0e0c6821 DSH |
132 | #ifndef OPENSSL_NO_MD5 |
133 | unsigned long X509_subject_name_hash_old(X509 *x) | |
0f113f3e | 134 | { |
26a7d938 | 135 | return X509_NAME_hash_old(x->cert_info.subject); |
0f113f3e | 136 | } |
0e0c6821 DSH |
137 | #endif |
138 | ||
0f113f3e MC |
139 | /* |
140 | * Compare two certificates: they must be identical for this to work. NB: | |
141 | * Although "cmp" operations are generally prototyped to take "const" | |
142 | * arguments (eg. for use in STACKs), the way X509 handling is - these | |
143 | * operations may involve ensuring the hashes are up-to-date and ensuring | |
144 | * certain cert information is cached. So this is the point where the | |
145 | * "depth-first" constification tree has to halt with an evil cast. | |
e947f396 | 146 | */ |
ccd86b68 | 147 | int X509_cmp(const X509 *a, const X509 *b) |
e947f396 | 148 | { |
f2a04587 | 149 | int rv = 0; |
7e06a675 | 150 | |
3bed88a3 DDO |
151 | if (a == b) /* for efficiency */ |
152 | return 0; | |
0f113f3e | 153 | |
f2a04587 DDO |
154 | /* attempt to compute cert hash */ |
155 | (void)X509_check_purpose((X509 *)a, -1, 0); | |
156 | (void)X509_check_purpose((X509 *)b, -1, 0); | |
157 | ||
158 | if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0 | |
159 | && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) | |
160 | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); | |
bc64c5a6 DDO |
161 | if (rv != 0) |
162 | return rv < 0 ? -1 : 1; | |
f2a04587 | 163 | |
0f113f3e | 164 | /* Check for match against stored encoding too */ |
5cf6abd8 | 165 | if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) { |
87a8405b DB |
166 | if (a->cert_info.enc.len < b->cert_info.enc.len) |
167 | return -1; | |
168 | if (a->cert_info.enc.len > b->cert_info.enc.len) | |
169 | return 1; | |
bc64c5a6 DDO |
170 | rv = memcmp(a->cert_info.enc.enc, |
171 | b->cert_info.enc.enc, a->cert_info.enc.len); | |
0f113f3e | 172 | } |
bc64c5a6 | 173 | return rv < 0 ? -1 : rv > 0; |
e947f396 | 174 | } |
d02b48c6 | 175 | |
eeccc237 DDO |
176 | int X509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags) |
177 | { | |
178 | if (*sk == NULL | |
179 | && (*sk = sk_X509_new_null()) == NULL) { | |
9311d0c4 | 180 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
eeccc237 DDO |
181 | return 0; |
182 | } | |
183 | return X509_add_cert(*sk, cert, flags); | |
184 | } | |
185 | ||
186 | int X509_add_cert(STACK_OF(X509) *sk, X509 *cert, int flags) | |
187 | { | |
188 | if (sk == NULL) { | |
9311d0c4 | 189 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
eeccc237 DDO |
190 | return 0; |
191 | } | |
192 | if ((flags & X509_ADD_FLAG_NO_DUP) != 0) { | |
193 | /* | |
194 | * not using sk_X509_set_cmp_func() and sk_X509_find() | |
195 | * because this re-orders the certs on the stack | |
196 | */ | |
197 | int i; | |
198 | ||
199 | for (i = 0; i < sk_X509_num(sk); i++) { | |
200 | if (X509_cmp(sk_X509_value(sk, i), cert) == 0) | |
201 | return 1; | |
202 | } | |
203 | } | |
204 | if ((flags & X509_ADD_FLAG_NO_SS) != 0 && X509_self_signed(cert, 0)) | |
205 | return 1; | |
206 | if (!sk_X509_insert(sk, cert, | |
207 | (flags & X509_ADD_FLAG_PREPEND) != 0 ? 0 : -1)) { | |
9311d0c4 | 208 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
eeccc237 DDO |
209 | return 0; |
210 | } | |
211 | if ((flags & X509_ADD_FLAG_UP_REF) != 0) | |
212 | (void)X509_up_ref(cert); | |
213 | return 1; | |
214 | } | |
215 | ||
216 | int X509_add_certs(STACK_OF(X509) *sk, STACK_OF(X509) *certs, int flags) | |
217 | /* compiler would allow 'const' for the list of certs, yet they are up-ref'ed */ | |
218 | { | |
219 | int n = sk_X509_num(certs); /* certs may be NULL */ | |
220 | int i; | |
bc64c5a6 | 221 | |
eeccc237 DDO |
222 | for (i = 0; i < n; i++) { |
223 | int j = (flags & X509_ADD_FLAG_PREPEND) == 0 ? i : n - 1 - i; | |
224 | /* if prepend, add certs in reverse order to keep original order */ | |
225 | ||
226 | if (!X509_add_cert(sk, sk_X509_value(certs, j), flags)) | |
227 | return 0; | |
228 | } | |
229 | return 1; | |
230 | } | |
231 | ||
450ea834 | 232 | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) |
0f113f3e MC |
233 | { |
234 | int ret; | |
c81a1509 | 235 | |
e0331eb8 DDO |
236 | if (b == NULL) |
237 | return a != NULL; | |
238 | if (a == NULL) | |
239 | return -1; | |
c81a1509 | 240 | |
e0331eb8 | 241 | /* Ensure canonical encoding is present and up to date */ |
0f113f3e MC |
242 | if (!a->canon_enc || a->modified) { |
243 | ret = i2d_X509_NAME((X509_NAME *)a, NULL); | |
244 | if (ret < 0) | |
245 | return -2; | |
246 | } | |
1862dae8 | 247 | |
0f113f3e MC |
248 | if (!b->canon_enc || b->modified) { |
249 | ret = i2d_X509_NAME((X509_NAME *)b, NULL); | |
250 | if (ret < 0) | |
251 | return -2; | |
252 | } | |
1862dae8 | 253 | |
0f113f3e | 254 | ret = a->canon_enclen - b->canon_enclen; |
bc64c5a6 DDO |
255 | if (ret == 0 && a->canon_enclen != 0) |
256 | ret = memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); | |
d02b48c6 | 257 | |
bc64c5a6 | 258 | return ret < 0 ? -1 : ret > 0; |
0f113f3e | 259 | } |
d02b48c6 | 260 | |
bf973d06 DDO |
261 | unsigned long X509_NAME_hash_ex(const X509_NAME *x, OSSL_LIB_CTX *libctx, |
262 | const char *propq, int *ok) | |
0f113f3e MC |
263 | { |
264 | unsigned long ret = 0; | |
265 | unsigned char md[SHA_DIGEST_LENGTH]; | |
bf973d06 | 266 | EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq); |
0f113f3e MC |
267 | |
268 | /* Make sure X509_NAME structure contains valid cached encoding */ | |
269 | i2d_X509_NAME(x, NULL); | |
bf973d06 DDO |
270 | if (ok != NULL) |
271 | *ok = 0; | |
272 | if (sha1 != NULL | |
273 | && EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, sha1, NULL)) { | |
274 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | | |
275 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) | |
276 | ) & 0xffffffffL; | |
277 | if (ok != NULL) | |
278 | *ok = 1; | |
279 | } | |
280 | EVP_MD_free(sha1); | |
26a7d938 | 281 | return ret; |
0f113f3e | 282 | } |
450ea834 | 283 | |
cf1b7d96 | 284 | #ifndef OPENSSL_NO_MD5 |
0f113f3e MC |
285 | /* |
286 | * I now DER encode the name and hash it. Since I cache the DER encoding, | |
287 | * this is reasonably efficient. | |
288 | */ | |
8cc86b81 | 289 | unsigned long X509_NAME_hash_old(const X509_NAME *x) |
0f113f3e | 290 | { |
47b4ccea | 291 | EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips"); |
bfb0641f | 292 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
0f113f3e MC |
293 | unsigned long ret = 0; |
294 | unsigned char md[16]; | |
295 | ||
47b4ccea RL |
296 | if (md5 == NULL || md_ctx == NULL) |
297 | goto end; | |
6e59a892 | 298 | |
0f113f3e MC |
299 | /* Make sure X509_NAME structure contains valid cached encoding */ |
300 | i2d_X509_NAME(x, NULL); | |
47b4ccea | 301 | if (EVP_DigestInit_ex(md_ctx, md5, NULL) |
6e59a892 RL |
302 | && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length) |
303 | && EVP_DigestFinal_ex(md_ctx, md, NULL)) | |
0f113f3e MC |
304 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
305 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) | |
306 | ) & 0xffffffffL; | |
47b4ccea RL |
307 | |
308 | end: | |
bfb0641f | 309 | EVP_MD_CTX_free(md_ctx); |
47b4ccea | 310 | EVP_MD_free(md5); |
0f113f3e | 311 | |
26a7d938 | 312 | return ret; |
0f113f3e | 313 | } |
d02b48c6 RE |
314 | #endif |
315 | ||
316 | /* Search a stack of X509 for a match */ | |
8cc86b81 DDO |
317 | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name, |
318 | const ASN1_INTEGER *serial) | |
0f113f3e MC |
319 | { |
320 | int i; | |
0f113f3e MC |
321 | X509 x, *x509 = NULL; |
322 | ||
323 | if (!sk) | |
324 | return NULL; | |
325 | ||
81e49438 | 326 | x.cert_info.serialNumber = *serial; |
8cc86b81 | 327 | x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */ |
0f113f3e MC |
328 | |
329 | for (i = 0; i < sk_X509_num(sk); i++) { | |
330 | x509 = sk_X509_value(sk, i); | |
331 | if (X509_issuer_and_serial_cmp(x509, &x) == 0) | |
26a7d938 | 332 | return x509; |
0f113f3e | 333 | } |
26a7d938 | 334 | return NULL; |
0f113f3e | 335 | } |
d02b48c6 | 336 | |
8cc86b81 | 337 | X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name) |
0f113f3e MC |
338 | { |
339 | X509 *x509; | |
340 | int i; | |
341 | ||
342 | for (i = 0; i < sk_X509_num(sk); i++) { | |
343 | x509 = sk_X509_value(sk, i); | |
344 | if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) | |
26a7d938 | 345 | return x509; |
0f113f3e | 346 | } |
26a7d938 | 347 | return NULL; |
0f113f3e | 348 | } |
d02b48c6 | 349 | |
fdaf7bee | 350 | EVP_PKEY *X509_get0_pubkey(const X509 *x) |
c01ff880 DSH |
351 | { |
352 | if (x == NULL) | |
353 | return NULL; | |
354 | return X509_PUBKEY_get0(x->cert_info.key); | |
355 | } | |
356 | ||
6b691a5c | 357 | EVP_PKEY *X509_get_pubkey(X509 *x) |
0f113f3e | 358 | { |
5cf6abd8 | 359 | if (x == NULL) |
c01ff880 DSH |
360 | return NULL; |
361 | return X509_PUBKEY_get(x->cert_info.key); | |
0f113f3e | 362 | } |
dfeab068 | 363 | |
fdaf7bee | 364 | int X509_check_private_key(const X509 *x, const EVP_PKEY *k) |
0f113f3e | 365 | { |
fdaf7bee | 366 | const EVP_PKEY *xk; |
0f113f3e MC |
367 | int ret; |
368 | ||
c01ff880 | 369 | xk = X509_get0_pubkey(x); |
0f113f3e MC |
370 | |
371 | if (xk) | |
c74aaa39 | 372 | ret = EVP_PKEY_eq(xk, k); |
0f113f3e MC |
373 | else |
374 | ret = -2; | |
375 | ||
376 | switch (ret) { | |
377 | case 1: | |
378 | break; | |
379 | case 0: | |
9311d0c4 | 380 | ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH); |
0f113f3e MC |
381 | break; |
382 | case -1: | |
9311d0c4 | 383 | ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH); |
0f113f3e MC |
384 | break; |
385 | case -2: | |
9311d0c4 | 386 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE); |
0f113f3e | 387 | } |
0f113f3e MC |
388 | if (ret > 0) |
389 | return 1; | |
390 | return 0; | |
391 | } | |
392 | ||
393 | /* | |
394 | * Check a suite B algorithm is permitted: pass in a public key and the NID | |
395 | * of its signature (or 0 if no signature). The pflags is a pointer to a | |
396 | * flags field which must contain the suite B verification flags. | |
3ad344a5 DSH |
397 | */ |
398 | ||
14536c8c DSH |
399 | #ifndef OPENSSL_NO_EC |
400 | ||
3ad344a5 | 401 | static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) |
0f113f3e | 402 | { |
5b5eea4b SL |
403 | char curve_name[80]; |
404 | size_t curve_name_len; | |
0f113f3e | 405 | int curve_nid; |
5b5eea4b SL |
406 | |
407 | if (pkey == NULL || !EVP_PKEY_is_a(pkey, "EC")) | |
0f113f3e | 408 | return X509_V_ERR_SUITE_B_INVALID_ALGORITHM; |
5b5eea4b | 409 | |
0c8e98e6 TM |
410 | if (!EVP_PKEY_get_group_name(pkey, curve_name, sizeof(curve_name), |
411 | &curve_name_len)) | |
5b5eea4b SL |
412 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
413 | ||
414 | curve_nid = OBJ_txt2nid(curve_name); | |
0f113f3e MC |
415 | /* Check curve is consistent with LOS */ |
416 | if (curve_nid == NID_secp384r1) { /* P-384 */ | |
417 | /* | |
418 | * Check signature algorithm is consistent with curve. | |
419 | */ | |
420 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) | |
421 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; | |
422 | if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) | |
423 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; | |
424 | /* If we encounter P-384 we cannot use P-256 later */ | |
425 | *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY; | |
426 | } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */ | |
427 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) | |
428 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; | |
429 | if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) | |
430 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; | |
bc64c5a6 | 431 | } else { |
0f113f3e | 432 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
bc64c5a6 | 433 | } |
0f113f3e MC |
434 | return X509_V_OK; |
435 | } | |
3ad344a5 | 436 | |
3b0648eb | 437 | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
0f113f3e MC |
438 | unsigned long flags) |
439 | { | |
440 | int rv, i, sign_nid; | |
6e328256 VD |
441 | EVP_PKEY *pk; |
442 | unsigned long tflags = flags; | |
443 | ||
0f113f3e MC |
444 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
445 | return X509_V_OK; | |
6e328256 | 446 | |
0f113f3e MC |
447 | /* If no EE certificate passed in must be first in chain */ |
448 | if (x == NULL) { | |
449 | x = sk_X509_value(chain, 0); | |
450 | i = 1; | |
bc64c5a6 | 451 | } else { |
0f113f3e | 452 | i = 0; |
bc64c5a6 | 453 | } |
6e328256 VD |
454 | pk = X509_get0_pubkey(x); |
455 | ||
456 | /* | |
457 | * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build | |
458 | * a chain all, just report trust success or failure, but must also report | |
459 | * Suite-B errors if applicable. This is indicated via a NULL chain | |
460 | * pointer. All we need to do is check the leaf key algorithm. | |
461 | */ | |
462 | if (chain == NULL) | |
463 | return check_suite_b(pk, -1, &tflags); | |
464 | ||
0f113f3e MC |
465 | if (X509_get_version(x) != 2) { |
466 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; | |
467 | /* Correct error depth */ | |
468 | i = 0; | |
469 | goto end; | |
470 | } | |
471 | ||
0f113f3e MC |
472 | /* Check EE key only */ |
473 | rv = check_suite_b(pk, -1, &tflags); | |
474 | if (rv != X509_V_OK) { | |
475 | /* Correct error depth */ | |
476 | i = 0; | |
477 | goto end; | |
478 | } | |
479 | for (; i < sk_X509_num(chain); i++) { | |
480 | sign_nid = X509_get_signature_nid(x); | |
481 | x = sk_X509_value(chain, i); | |
482 | if (X509_get_version(x) != 2) { | |
483 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; | |
484 | goto end; | |
485 | } | |
8382fd3a | 486 | pk = X509_get0_pubkey(x); |
0f113f3e MC |
487 | rv = check_suite_b(pk, sign_nid, &tflags); |
488 | if (rv != X509_V_OK) | |
489 | goto end; | |
490 | } | |
491 | ||
492 | /* Final check: root CA signature */ | |
493 | rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags); | |
494 | end: | |
0f113f3e MC |
495 | if (rv != X509_V_OK) { |
496 | /* Invalid signature or LOS errors are for previous cert */ | |
497 | if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM | |
498 | || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i) | |
499 | i--; | |
500 | /* | |
501 | * If we have LOS error and flags changed then we are signing P-384 | |
0d4fb843 | 502 | * with P-256. Use more meaningful error. |
0f113f3e MC |
503 | */ |
504 | if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) | |
505 | rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256; | |
506 | if (perror_depth) | |
507 | *perror_depth = i; | |
508 | } | |
509 | return rv; | |
510 | } | |
3ad344a5 | 511 | |
3b0648eb | 512 | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
0f113f3e MC |
513 | { |
514 | int sign_nid; | |
515 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) | |
516 | return X509_V_OK; | |
6e63c142 | 517 | sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm); |
0f113f3e MC |
518 | return check_suite_b(pk, sign_nid, &flags); |
519 | } | |
14536c8c DSH |
520 | |
521 | #else | |
522 | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, | |
0f113f3e MC |
523 | unsigned long flags) |
524 | { | |
525 | return 0; | |
526 | } | |
14536c8c DSH |
527 | |
528 | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) | |
0f113f3e MC |
529 | { |
530 | return 0; | |
531 | } | |
14536c8c DSH |
532 | |
533 | #endif | |
0f113f3e MC |
534 | /* |
535 | * Not strictly speaking an "up_ref" as a STACK doesn't have a reference | |
536 | * count but it has the same effect by duping the STACK and upping the ref of | |
537 | * each X509 structure. | |
3b0648eb DSH |
538 | */ |
539 | STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) | |
0f113f3e MC |
540 | { |
541 | STACK_OF(X509) *ret; | |
542 | int i; | |
543 | ret = sk_X509_dup(chain); | |
cae665df BE |
544 | if (ret == NULL) |
545 | return NULL; | |
0f113f3e MC |
546 | for (i = 0; i < sk_X509_num(ret); i++) { |
547 | X509 *x = sk_X509_value(ret, i); | |
cae665df BE |
548 | if (!X509_up_ref(x)) |
549 | goto err; | |
0f113f3e MC |
550 | } |
551 | return ret; | |
cae665df BE |
552 | err: |
553 | while (i-- > 0) | |
bc64c5a6 | 554 | X509_free(sk_X509_value(ret, i)); |
cae665df BE |
555 | sk_X509_free(ret); |
556 | return NULL; | |
0f113f3e | 557 | } |