/src/openssl/crypto/x509/x509_cmp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1.h> |
13 | | #include <openssl/objects.h> |
14 | | #include <openssl/x509.h> |
15 | | #include <openssl/x509v3.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include "crypto/x509.h" |
18 | | |
19 | | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
20 | 6.37k | { |
21 | 6.37k | int i; |
22 | 6.37k | const X509_CINF *ai, *bi; |
23 | | |
24 | 6.37k | if (b == NULL) |
25 | 0 | return a != NULL; |
26 | 6.37k | if (a == NULL) |
27 | 0 | return -1; |
28 | 6.37k | ai = &a->cert_info; |
29 | 6.37k | bi = &b->cert_info; |
30 | 6.37k | i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber); |
31 | 6.37k | if (i != 0) |
32 | 1.00k | return i < 0 ? -1 : 1; |
33 | 5.37k | return X509_NAME_cmp(ai->issuer, bi->issuer); |
34 | 6.37k | } |
35 | | |
36 | | #ifndef OPENSSL_NO_MD5 |
37 | | unsigned long X509_issuer_and_serial_hash(const X509 *a) |
38 | 36.8k | { |
39 | 36.8k | unsigned long ret = 0; |
40 | 36.8k | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
41 | 36.8k | unsigned char md[16]; |
42 | 36.8k | char *f = NULL; |
43 | 36.8k | EVP_MD *digest = NULL; |
44 | 36.8k | OSSL_LIB_CTX *libctx; |
45 | 36.8k | const char *propq; |
46 | | |
47 | 36.8k | if (ctx == NULL) |
48 | 0 | goto err; |
49 | 36.8k | f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0); |
50 | 36.8k | if (f == NULL) |
51 | 47 | goto err; |
52 | 36.8k | ossl_x509_get0_libctx(a, &libctx, &propq); |
53 | 36.8k | digest = EVP_MD_fetch(libctx, SN_md5, propq); |
54 | 36.8k | if (digest == NULL) |
55 | 0 | goto err; |
56 | | |
57 | 36.8k | if (!EVP_DigestInit_ex(ctx, digest, NULL)) |
58 | 0 | goto err; |
59 | 36.8k | if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f))) |
60 | 0 | goto err; |
61 | 36.8k | if (!EVP_DigestUpdate(ctx, (unsigned char *)a->cert_info.serialNumber.data, |
62 | 36.8k | (unsigned long)a->cert_info.serialNumber.length)) |
63 | 0 | goto err; |
64 | 36.8k | if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL)) |
65 | 0 | goto err; |
66 | 36.8k | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
67 | 36.8k | err: |
68 | 36.8k | OPENSSL_free(f); |
69 | 36.8k | EVP_MD_free(digest); |
70 | 36.8k | EVP_MD_CTX_free(ctx); |
71 | 36.8k | return ret; |
72 | 36.8k | } |
73 | | #endif |
74 | | |
75 | | int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
76 | 0 | { |
77 | 0 | return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer); |
78 | 0 | } |
79 | | |
80 | | int X509_subject_name_cmp(const X509 *a, const X509 *b) |
81 | 17.8k | { |
82 | 17.8k | return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject); |
83 | 17.8k | } |
84 | | |
85 | | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
86 | 0 | { |
87 | 0 | return X509_NAME_cmp(a->crl.issuer, b->crl.issuer); |
88 | 0 | } |
89 | | |
90 | | int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) |
91 | 0 | { |
92 | 0 | int rv = 0; |
93 | |
|
94 | 0 | if (a == b) |
95 | 0 | return 0; |
96 | | |
97 | | /* A CRL modified since it was signed or decoded is equal only to itself */ |
98 | 0 | if (a->crl.enc.modified || b->crl.enc.modified) |
99 | 0 | return a->crl.enc.modified ? 1 : -1; |
100 | | |
101 | 0 | if ((a->flags & EXFLAG_NO_FINGERPRINT) == 0 |
102 | 0 | && (b->flags & EXFLAG_NO_FINGERPRINT) == 0) |
103 | 0 | rv = memcmp(a->fingerprint, b->fingerprint, sizeof(a->fingerprint)); |
104 | 0 | if (rv != 0) |
105 | 0 | return rv < 0 ? -1 : 1; |
106 | | |
107 | | /* Check for match against stored encoding too */ |
108 | 0 | if (a->crl.enc.len < b->crl.enc.len) |
109 | 0 | return -1; |
110 | 0 | if (a->crl.enc.len > b->crl.enc.len) |
111 | 0 | return 1; |
112 | 0 | rv = memcmp(a->crl.enc.enc, b->crl.enc.enc, a->crl.enc.len); |
113 | 0 | if (rv != 0) |
114 | 0 | return rv < 0 ? -1 : 1; |
115 | | /* Same TBS: the signature algorithm and signature must match too */ |
116 | 0 | rv = X509_ALGOR_cmp(&a->sig_alg, &b->sig_alg); |
117 | 0 | if (rv != 0) |
118 | 0 | return rv < 0 ? -1 : 1; |
119 | 0 | rv = ASN1_STRING_cmp(&a->signature, &b->signature); |
120 | 0 | return rv < 0 ? -1 : rv > 0; |
121 | 0 | } |
122 | | |
123 | | const X509_NAME *X509_get_issuer_name(const X509 *a) |
124 | 415k | { |
125 | 415k | return a->cert_info.issuer; |
126 | 415k | } |
127 | | |
128 | | unsigned long X509_issuer_name_hash(const X509 *x) |
129 | 0 | { |
130 | 0 | return X509_NAME_hash_ex(x->cert_info.issuer, NULL, NULL, NULL); |
131 | 0 | } |
132 | | |
133 | | #ifndef OPENSSL_NO_MD5 |
134 | | unsigned long X509_issuer_name_hash_old(const X509 *x) |
135 | 0 | { |
136 | 0 | return X509_NAME_hash_old(x->cert_info.issuer); |
137 | 0 | } |
138 | | #endif |
139 | | |
140 | | const X509_NAME *X509_get_subject_name(const X509 *a) |
141 | 309k | { |
142 | 309k | return a->cert_info.subject; |
143 | 309k | } |
144 | | |
145 | | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
146 | 0 | { |
147 | 0 | return &a->cert_info.serialNumber; |
148 | 0 | } |
149 | | |
150 | | const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a) |
151 | 56.7k | { |
152 | 56.7k | return &a->cert_info.serialNumber; |
153 | 56.7k | } |
154 | | |
155 | | unsigned long X509_subject_name_hash(const X509 *x) |
156 | 0 | { |
157 | 0 | return X509_NAME_hash_ex(x->cert_info.subject, NULL, NULL, NULL); |
158 | 0 | } |
159 | | |
160 | | #ifndef OPENSSL_NO_MD5 |
161 | | unsigned long X509_subject_name_hash_old(const X509 *x) |
162 | 0 | { |
163 | 0 | return X509_NAME_hash_old(x->cert_info.subject); |
164 | 0 | } |
165 | | #endif |
166 | | |
167 | | /* |
168 | | * Compare two certificates: they must be identical for this to work. NB: |
169 | | * Although "cmp" operations are generally prototyped to take "const" |
170 | | * arguments (eg. for use in STACKs), the way X509 handling is - these |
171 | | * operations may involve ensuring the hashes are up-to-date and ensuring |
172 | | * certain cert information is cached. So this is the point where the |
173 | | * "depth-first" constification tree has to halt with an evil cast. |
174 | | */ |
175 | | int X509_cmp(const X509 *a, const X509 *b) |
176 | 14.7k | { |
177 | 14.7k | int rv = 0; |
178 | | |
179 | 14.7k | if (a == b) /* for efficiency */ |
180 | 7.42k | return 0; |
181 | | |
182 | | /* |
183 | | * A certificate modified since it was signed or decoded is equal only |
184 | | * to itself |
185 | | */ |
186 | 7.31k | if (a->cert_info.enc.modified || b->cert_info.enc.modified) |
187 | 0 | return a->cert_info.enc.modified ? 1 : -1; |
188 | | |
189 | | /* attempt to compute cert hash */ |
190 | 7.31k | (void)X509_check_purpose((X509 *)a, -1, 0); |
191 | 7.31k | (void)X509_check_purpose((X509 *)b, -1, 0); |
192 | | |
193 | 7.31k | if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0 |
194 | 7.31k | && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) |
195 | 7.31k | rv = memcmp(a->fingerprint, b->fingerprint, sizeof(a->fingerprint)); |
196 | 7.31k | if (rv != 0) |
197 | 7.30k | return rv < 0 ? -1 : 1; |
198 | | |
199 | | /* Check for match against stored encoding too */ |
200 | 3 | if (a->cert_info.enc.len < b->cert_info.enc.len) |
201 | 0 | return -1; |
202 | 3 | if (a->cert_info.enc.len > b->cert_info.enc.len) |
203 | 0 | return 1; |
204 | 3 | rv = memcmp(a->cert_info.enc.enc, |
205 | 3 | b->cert_info.enc.enc, a->cert_info.enc.len); |
206 | 3 | if (rv != 0) |
207 | 0 | return rv < 0 ? -1 : 1; |
208 | | /* Same TBS: the signature algorithm and signature must match too */ |
209 | 3 | rv = X509_ALGOR_cmp(&a->sig_alg, &b->sig_alg); |
210 | 3 | if (rv != 0) |
211 | 0 | return rv < 0 ? -1 : 1; |
212 | 3 | rv = ASN1_STRING_cmp(&a->signature, &b->signature); |
213 | 3 | return rv < 0 ? -1 : rv > 0; |
214 | 3 | } |
215 | | |
216 | | int ossl_x509_add_cert_new(STACK_OF(X509) **p_sk, const X509 *cert, int flags) |
217 | 126k | { |
218 | 126k | if (*p_sk == NULL && (*p_sk = sk_X509_new_null()) == NULL) { |
219 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 126k | return X509_add_cert(*p_sk, cert, flags); |
223 | 126k | } |
224 | | |
225 | | int X509_add_cert(STACK_OF(X509) *sk, const X509 *cert, int flags) |
226 | 106k | { |
227 | 106k | if (sk == NULL) { |
228 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
229 | 0 | return 0; |
230 | 0 | } |
231 | 106k | if (cert == NULL) |
232 | 0 | return 0; |
233 | 106k | if ((flags & X509_ADD_FLAG_NO_DUP) != 0) { |
234 | | /* |
235 | | * not using sk_X509_set_cmp_func() and sk_X509_find() |
236 | | * because this re-orders the certs on the stack |
237 | | */ |
238 | 10.6k | int i; |
239 | | |
240 | 23.5k | for (i = 0; i < sk_X509_num(sk); i++) { |
241 | 12.9k | if (X509_cmp(sk_X509_value(sk, i), cert) == 0) |
242 | 0 | return 1; |
243 | 12.9k | } |
244 | 10.6k | } |
245 | 106k | if ((flags & X509_ADD_FLAG_NO_SS) != 0) { |
246 | 0 | int ret = X509_self_signed(cert, 0); |
247 | |
|
248 | 0 | if (ret != 0) |
249 | 0 | return ret > 0 ? 1 : 0; |
250 | 0 | } |
251 | | /* |
252 | | * Note: We're technically mutating the cert here, but its just to up |
253 | | * the reference count, so that should be safe, so cast away |
254 | | */ |
255 | 106k | if ((flags & X509_ADD_FLAG_UP_REF) != 0 && !X509_up_ref((X509 *)cert)) |
256 | 0 | return 0; |
257 | 106k | if (!sk_X509_insert(sk, (X509 *)cert, |
258 | 106k | (flags & X509_ADD_FLAG_PREPEND) != 0 ? 0 : -1)) { |
259 | 0 | if ((flags & X509_ADD_FLAG_UP_REF) != 0) |
260 | 0 | X509_free((X509 *)cert); |
261 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 106k | return 1; |
265 | 106k | } |
266 | | |
267 | | int X509_add_certs(STACK_OF(X509) *sk, const STACK_OF(X509) *certs, int flags) |
268 | | /* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
269 | 69.7k | { |
270 | 69.7k | if (sk == NULL) { |
271 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
272 | 0 | return 0; |
273 | 0 | } |
274 | 69.7k | return ossl_x509_add_certs_new(&sk, certs, flags); |
275 | 69.7k | } |
276 | | |
277 | | int ossl_x509_add_certs_new(STACK_OF(X509) **p_sk, const STACK_OF(X509) *certs, int flags) |
278 | | /* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
279 | 120k | { |
280 | 120k | int n = sk_X509_num(certs /* may be NULL */); |
281 | 120k | int i; |
282 | | |
283 | 175k | for (i = 0; i < n; i++) { |
284 | 55.7k | int j = (flags & X509_ADD_FLAG_PREPEND) == 0 ? i : n - 1 - i; |
285 | | /* if prepend, add certs in reverse order to keep original order */ |
286 | | |
287 | 55.7k | if (!ossl_x509_add_cert_new(p_sk, sk_X509_value(certs, j), flags)) |
288 | 0 | return 0; |
289 | 55.7k | } |
290 | 120k | return 1; |
291 | 120k | } |
292 | | |
293 | | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) |
294 | 249k | { |
295 | 249k | int ret; |
296 | | |
297 | 249k | if (b == NULL) |
298 | 0 | return a != NULL; |
299 | 249k | if (a == NULL) |
300 | 0 | return -1; |
301 | | |
302 | | /* Ensure canonical encoding is present and up to date */ |
303 | 249k | if (a->canon_enc == NULL || a->modified) { |
304 | 12.1k | ret = i2d_X509_NAME((X509_NAME *)a, NULL); |
305 | 12.1k | if (ret < 0) |
306 | 0 | return -2; |
307 | 12.1k | } |
308 | | |
309 | 249k | if (b->canon_enc == NULL || b->modified) { |
310 | 11.7k | ret = i2d_X509_NAME((X509_NAME *)b, NULL); |
311 | 11.7k | if (ret < 0) |
312 | 0 | return -2; |
313 | 11.7k | } |
314 | | |
315 | 249k | ret = a->canon_enclen - b->canon_enclen; |
316 | 249k | if (ret == 0 && a->canon_enclen == 0) |
317 | 11.3k | return 0; |
318 | | |
319 | 237k | if (ret == 0) { |
320 | 166k | if (a->canon_enc == NULL || b->canon_enc == NULL) |
321 | 0 | return -2; |
322 | 166k | ret = memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); |
323 | 166k | } |
324 | | |
325 | 237k | return ret < 0 ? -1 : ret > 0; |
326 | 237k | } |
327 | | |
328 | | unsigned long X509_NAME_hash_ex(const X509_NAME *x, OSSL_LIB_CTX *libctx, |
329 | | const char *propq, int *ok) |
330 | 0 | { |
331 | 0 | unsigned long ret = 0; |
332 | 0 | unsigned char md[SHA_DIGEST_LENGTH]; |
333 | 0 | EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq); |
334 | 0 | int i2d_ret; |
335 | | |
336 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
337 | 0 | i2d_ret = i2d_X509_NAME(x, NULL); |
338 | 0 | if (ok != NULL) |
339 | 0 | *ok = 0; |
340 | 0 | if (i2d_ret >= 0 && sha1 != NULL |
341 | 0 | && EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, sha1, NULL)) { |
342 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
343 | 0 | if (ok != NULL) |
344 | 0 | *ok = 1; |
345 | 0 | } |
346 | 0 | EVP_MD_free(sha1); |
347 | 0 | return ret; |
348 | 0 | } |
349 | | |
350 | | #ifndef OPENSSL_NO_MD5 |
351 | | /* |
352 | | * I now DER encode the name and hash it. Since I cache the DER encoding, |
353 | | * this is reasonably efficient. |
354 | | */ |
355 | | unsigned long X509_NAME_hash_old(const X509_NAME *x) |
356 | 0 | { |
357 | 0 | EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips"); |
358 | 0 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
359 | 0 | unsigned long ret = 0; |
360 | 0 | unsigned char md[16]; |
361 | |
|
362 | 0 | if (md5 == NULL || md_ctx == NULL) |
363 | 0 | goto end; |
364 | | |
365 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
366 | 0 | if (i2d_X509_NAME(x, NULL) < 0) |
367 | 0 | goto end; |
368 | | |
369 | 0 | if (EVP_DigestInit_ex(md_ctx, md5, NULL) |
370 | 0 | && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length) |
371 | 0 | && EVP_DigestFinal_ex(md_ctx, md, NULL)) |
372 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
373 | |
|
374 | 0 | end: |
375 | 0 | EVP_MD_CTX_free(md_ctx); |
376 | 0 | EVP_MD_free(md5); |
377 | |
|
378 | 0 | return ret; |
379 | 0 | } |
380 | | #endif |
381 | | |
382 | | /* Search a stack of X509 for a match */ |
383 | | X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk, const X509_NAME *name, |
384 | | const ASN1_INTEGER *serial) |
385 | 10.8k | { |
386 | 10.8k | int i; |
387 | 10.8k | X509 x, *x509 = NULL; |
388 | | |
389 | 10.8k | if (!sk) |
390 | 5.29k | return NULL; |
391 | | |
392 | 5.53k | x.cert_info.serialNumber = *serial; |
393 | 5.53k | x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */ |
394 | | |
395 | 6.77k | for (i = 0; i < sk_X509_num(sk); i++) { |
396 | 6.37k | x509 = sk_X509_value(sk, i); |
397 | 6.37k | if (X509_issuer_and_serial_cmp(x509, &x) == 0) |
398 | 5.14k | return x509; |
399 | 6.37k | } |
400 | 396 | return NULL; |
401 | 5.53k | } |
402 | | |
403 | | X509 *X509_find_by_subject(const STACK_OF(X509) *sk, const X509_NAME *name) |
404 | 1.02k | { |
405 | 1.02k | X509 *x509; |
406 | 1.02k | int i; |
407 | | |
408 | 2.07k | for (i = 0; i < sk_X509_num(sk); i++) { |
409 | 1.55k | x509 = sk_X509_value(sk, i); |
410 | 1.55k | if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) |
411 | 495 | return x509; |
412 | 1.55k | } |
413 | 525 | return NULL; |
414 | 1.02k | } |
415 | | |
416 | | EVP_PKEY *X509_get0_pubkey(const X509 *x) |
417 | 982k | { |
418 | 982k | if (x == NULL) |
419 | 0 | return NULL; |
420 | 982k | return X509_PUBKEY_get0(x->cert_info.key); |
421 | 982k | } |
422 | | |
423 | | EVP_PKEY *X509_get_pubkey(const X509 *x) |
424 | 7.36k | { |
425 | 7.36k | if (x == NULL) |
426 | 0 | return NULL; |
427 | 7.36k | return X509_PUBKEY_get(x->cert_info.key); |
428 | 7.36k | } |
429 | | |
430 | | int X509_check_private_key(const X509 *cert, const EVP_PKEY *pkey) |
431 | 153k | { |
432 | 153k | const EVP_PKEY *xk = X509_get0_pubkey(cert); |
433 | | |
434 | 153k | if (xk == NULL) { |
435 | 0 | ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); |
436 | 0 | return 0; |
437 | 0 | } |
438 | 153k | return ossl_x509_check_private_key(xk, pkey); |
439 | 153k | } |
440 | | |
441 | | int ossl_x509_check_private_key(const EVP_PKEY *x, const EVP_PKEY *pkey) |
442 | 153k | { |
443 | 153k | if (x == NULL) { |
444 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
445 | 0 | return 0; |
446 | 0 | } |
447 | 153k | switch (EVP_PKEY_eq(x, pkey)) { |
448 | 153k | case 1: |
449 | 153k | return 1; |
450 | 0 | case 0: |
451 | 0 | ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH); |
452 | 0 | return 0; |
453 | 0 | case -1: |
454 | 0 | ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH); |
455 | 0 | return 0; |
456 | 0 | case -2: |
457 | 0 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE); |
458 | | /* fall thru */ |
459 | 0 | default: |
460 | 0 | return 0; |
461 | 153k | } |
462 | 153k | } |
463 | | |
464 | | /* |
465 | | * Check a suite B algorithm is permitted: pass in a public key and the NID |
466 | | * of its signature (or 0 if no signature). The pflags is a pointer to a |
467 | | * flags field which must contain the suite B verification flags. |
468 | | */ |
469 | | |
470 | | #ifndef OPENSSL_NO_EC |
471 | | |
472 | | static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) |
473 | 0 | { |
474 | 0 | char curve_name[80]; |
475 | 0 | size_t curve_name_len; |
476 | 0 | int curve_nid; |
477 | |
|
478 | 0 | if (pkey == NULL || !EVP_PKEY_is_a(pkey, "EC")) |
479 | 0 | return X509_V_ERR_SUITE_B_INVALID_ALGORITHM; |
480 | | |
481 | 0 | if (!EVP_PKEY_get_group_name(pkey, curve_name, sizeof(curve_name), |
482 | 0 | &curve_name_len)) |
483 | 0 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
484 | | |
485 | 0 | curve_nid = OBJ_txt2nid(curve_name); |
486 | | /* Check curve is consistent with LOS */ |
487 | 0 | if (curve_nid == NID_secp384r1) { /* P-384 */ |
488 | | /* |
489 | | * Check signature algorithm is consistent with curve. |
490 | | */ |
491 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) |
492 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
493 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) |
494 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
495 | | /* If we encounter P-384 we cannot use P-256 later */ |
496 | 0 | *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY; |
497 | 0 | } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */ |
498 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) |
499 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
500 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) |
501 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
502 | 0 | } else { |
503 | 0 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
504 | 0 | } |
505 | 0 | return X509_V_OK; |
506 | 0 | } |
507 | | |
508 | | int X509_chain_check_suiteb(int *perror_depth, const X509 *x, STACK_OF(X509) *chain, |
509 | | unsigned long flags) |
510 | 5.50k | { |
511 | 5.50k | int rv, i, sign_nid; |
512 | 5.50k | EVP_PKEY *pk; |
513 | 5.50k | unsigned long tflags = flags; |
514 | | |
515 | 5.50k | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
516 | 5.50k | return X509_V_OK; |
517 | | |
518 | | /* If no EE certificate passed in must be first in chain */ |
519 | 0 | if (x == NULL) { |
520 | 0 | x = sk_X509_value(chain, 0); |
521 | 0 | i = 1; |
522 | 0 | } else { |
523 | 0 | i = 0; |
524 | 0 | } |
525 | 0 | pk = X509_get0_pubkey(x); |
526 | | |
527 | | /* |
528 | | * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build |
529 | | * a chain all, just report trust success or failure, but must also report |
530 | | * Suite-B errors if applicable. This is indicated via a NULL chain |
531 | | * pointer. All we need to do is check the leaf key algorithm. |
532 | | */ |
533 | 0 | if (chain == NULL) |
534 | 0 | return check_suite_b(pk, -1, &tflags); |
535 | | |
536 | 0 | if (X509_get_version(x) != X509_VERSION_3) { |
537 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
538 | | /* Correct error depth */ |
539 | 0 | i = 0; |
540 | 0 | goto end; |
541 | 0 | } |
542 | | |
543 | | /* Check EE key only */ |
544 | 0 | rv = check_suite_b(pk, -1, &tflags); |
545 | 0 | if (rv != X509_V_OK) { |
546 | | /* Correct error depth */ |
547 | 0 | i = 0; |
548 | 0 | goto end; |
549 | 0 | } |
550 | 0 | for (; i < sk_X509_num(chain); i++) { |
551 | 0 | sign_nid = X509_get_signature_nid(x); |
552 | 0 | x = sk_X509_value(chain, i); |
553 | 0 | if (X509_get_version(x) != X509_VERSION_3) { |
554 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
555 | 0 | goto end; |
556 | 0 | } |
557 | 0 | pk = X509_get0_pubkey(x); |
558 | 0 | rv = check_suite_b(pk, sign_nid, &tflags); |
559 | 0 | if (rv != X509_V_OK) |
560 | 0 | goto end; |
561 | 0 | } |
562 | | |
563 | | /* Final check: root CA signature */ |
564 | 0 | rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags); |
565 | 0 | end: |
566 | 0 | if (rv != X509_V_OK) { |
567 | | /* Invalid signature or LOS errors are for previous cert */ |
568 | 0 | if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM |
569 | 0 | || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) |
570 | 0 | && i) |
571 | 0 | i--; |
572 | | /* |
573 | | * If we have LOS error and flags changed then we are signing P-384 |
574 | | * with P-256. Use more meaningful error. |
575 | | */ |
576 | 0 | if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) |
577 | 0 | rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256; |
578 | 0 | if (perror_depth) |
579 | 0 | *perror_depth = i; |
580 | 0 | } |
581 | 0 | return rv; |
582 | 0 | } |
583 | | |
584 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
585 | 3.57k | { |
586 | 3.57k | int sign_nid; |
587 | 3.57k | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
588 | 3.57k | return X509_V_OK; |
589 | 0 | sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm); |
590 | 0 | return check_suite_b(pk, sign_nid, &flags); |
591 | 3.57k | } |
592 | | |
593 | | #else |
594 | | int X509_chain_check_suiteb(int *perror_depth, const X509 *x, STACK_OF(X509) *chain, |
595 | | unsigned long flags) |
596 | | { |
597 | | return 0; |
598 | | } |
599 | | |
600 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
601 | | { |
602 | | return 0; |
603 | | } |
604 | | |
605 | | #endif |
606 | | |
607 | | /* |
608 | | * Not strictly speaking an "up_ref" as a STACK doesn't have a reference |
609 | | * count but it has the same effect by duping the STACK and upping the ref of |
610 | | * each X509 structure. |
611 | | */ |
612 | | STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) |
613 | 39.6k | { |
614 | 39.6k | STACK_OF(X509) *ret = sk_X509_dup(chain); |
615 | 39.6k | int i; |
616 | | |
617 | 39.6k | if (ret == NULL) |
618 | 0 | return NULL; |
619 | 80.4k | for (i = 0; i < sk_X509_num(ret); i++) { |
620 | 40.7k | X509 *x = sk_X509_value(ret, i); |
621 | | |
622 | 40.7k | if (!X509_up_ref(x)) |
623 | 0 | goto err; |
624 | 40.7k | } |
625 | 39.6k | return ret; |
626 | | |
627 | 0 | err: |
628 | 0 | while (i-- > 0) |
629 | 0 | X509_free(sk_X509_value(ret, i)); |
630 | 0 | sk_X509_free(ret); |
631 | | return NULL; |
632 | 39.6k | } |