1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (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 <ctype.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include <openssl/asn1.h>
|
---|
14 | #include <openssl/objects.h>
|
---|
15 | #include <openssl/x509.h>
|
---|
16 | #include <openssl/x509v3.h>
|
---|
17 | #include "internal/x509_int.h"
|
---|
18 |
|
---|
19 | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
|
---|
20 | {
|
---|
21 | int i;
|
---|
22 | const X509_CINF *ai, *bi;
|
---|
23 |
|
---|
24 | ai = &a->cert_info;
|
---|
25 | bi = &b->cert_info;
|
---|
26 | i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
|
---|
27 | if (i)
|
---|
28 | return (i);
|
---|
29 | return (X509_NAME_cmp(ai->issuer, bi->issuer));
|
---|
30 | }
|
---|
31 |
|
---|
32 | #ifndef OPENSSL_NO_MD5
|
---|
33 | unsigned long X509_issuer_and_serial_hash(X509 *a)
|
---|
34 | {
|
---|
35 | unsigned long ret = 0;
|
---|
36 | EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
---|
37 | unsigned char md[16];
|
---|
38 | char *f;
|
---|
39 |
|
---|
40 | if (ctx == NULL)
|
---|
41 | goto err;
|
---|
42 | f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
|
---|
43 | if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
|
---|
44 | goto err;
|
---|
45 | if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
|
---|
46 | goto err;
|
---|
47 | OPENSSL_free(f);
|
---|
48 | if (!EVP_DigestUpdate
|
---|
49 | (ctx, (unsigned char *)a->cert_info.serialNumber.data,
|
---|
50 | (unsigned long)a->cert_info.serialNumber.length))
|
---|
51 | goto err;
|
---|
52 | if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
|
---|
53 | goto err;
|
---|
54 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
---|
55 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
---|
56 | ) & 0xffffffffL;
|
---|
57 | err:
|
---|
58 | EVP_MD_CTX_free(ctx);
|
---|
59 | return (ret);
|
---|
60 | }
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | int X509_issuer_name_cmp(const X509 *a, const X509 *b)
|
---|
64 | {
|
---|
65 | return (X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer));
|
---|
66 | }
|
---|
67 |
|
---|
68 | int X509_subject_name_cmp(const X509 *a, const X509 *b)
|
---|
69 | {
|
---|
70 | return (X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject));
|
---|
71 | }
|
---|
72 |
|
---|
73 | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
|
---|
74 | {
|
---|
75 | return (X509_NAME_cmp(a->crl.issuer, b->crl.issuer));
|
---|
76 | }
|
---|
77 |
|
---|
78 | int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
|
---|
79 | {
|
---|
80 | return memcmp(a->sha1_hash, b->sha1_hash, 20);
|
---|
81 | }
|
---|
82 |
|
---|
83 | X509_NAME *X509_get_issuer_name(const X509 *a)
|
---|
84 | {
|
---|
85 | return (a->cert_info.issuer);
|
---|
86 | }
|
---|
87 |
|
---|
88 | unsigned long X509_issuer_name_hash(X509 *x)
|
---|
89 | {
|
---|
90 | return (X509_NAME_hash(x->cert_info.issuer));
|
---|
91 | }
|
---|
92 |
|
---|
93 | #ifndef OPENSSL_NO_MD5
|
---|
94 | unsigned long X509_issuer_name_hash_old(X509 *x)
|
---|
95 | {
|
---|
96 | return (X509_NAME_hash_old(x->cert_info.issuer));
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | X509_NAME *X509_get_subject_name(const X509 *a)
|
---|
101 | {
|
---|
102 | return (a->cert_info.subject);
|
---|
103 | }
|
---|
104 |
|
---|
105 | ASN1_INTEGER *X509_get_serialNumber(X509 *a)
|
---|
106 | {
|
---|
107 | return &a->cert_info.serialNumber;
|
---|
108 | }
|
---|
109 |
|
---|
110 | const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
|
---|
111 | {
|
---|
112 | return &a->cert_info.serialNumber;
|
---|
113 | }
|
---|
114 |
|
---|
115 | unsigned long X509_subject_name_hash(X509 *x)
|
---|
116 | {
|
---|
117 | return (X509_NAME_hash(x->cert_info.subject));
|
---|
118 | }
|
---|
119 |
|
---|
120 | #ifndef OPENSSL_NO_MD5
|
---|
121 | unsigned long X509_subject_name_hash_old(X509 *x)
|
---|
122 | {
|
---|
123 | return (X509_NAME_hash_old(x->cert_info.subject));
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Compare two certificates: they must be identical for this to work. NB:
|
---|
129 | * Although "cmp" operations are generally prototyped to take "const"
|
---|
130 | * arguments (eg. for use in STACKs), the way X509 handling is - these
|
---|
131 | * operations may involve ensuring the hashes are up-to-date and ensuring
|
---|
132 | * certain cert information is cached. So this is the point where the
|
---|
133 | * "depth-first" constification tree has to halt with an evil cast.
|
---|
134 | */
|
---|
135 | int X509_cmp(const X509 *a, const X509 *b)
|
---|
136 | {
|
---|
137 | int rv;
|
---|
138 | /* ensure hash is valid */
|
---|
139 | X509_check_purpose((X509 *)a, -1, 0);
|
---|
140 | X509_check_purpose((X509 *)b, -1, 0);
|
---|
141 |
|
---|
142 | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
|
---|
143 | if (rv)
|
---|
144 | return rv;
|
---|
145 | /* Check for match against stored encoding too */
|
---|
146 | if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
|
---|
147 | if (a->cert_info.enc.len < b->cert_info.enc.len)
|
---|
148 | return -1;
|
---|
149 | if (a->cert_info.enc.len > b->cert_info.enc.len)
|
---|
150 | return 1;
|
---|
151 | return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
|
---|
152 | a->cert_info.enc.len);
|
---|
153 | }
|
---|
154 | return rv;
|
---|
155 | }
|
---|
156 |
|
---|
157 | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
|
---|
158 | {
|
---|
159 | int ret;
|
---|
160 |
|
---|
161 | /* Ensure canonical encoding is present and up to date */
|
---|
162 |
|
---|
163 | if (!a->canon_enc || a->modified) {
|
---|
164 | ret = i2d_X509_NAME((X509_NAME *)a, NULL);
|
---|
165 | if (ret < 0)
|
---|
166 | return -2;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (!b->canon_enc || b->modified) {
|
---|
170 | ret = i2d_X509_NAME((X509_NAME *)b, NULL);
|
---|
171 | if (ret < 0)
|
---|
172 | return -2;
|
---|
173 | }
|
---|
174 |
|
---|
175 | ret = a->canon_enclen - b->canon_enclen;
|
---|
176 |
|
---|
177 | if (ret)
|
---|
178 | return ret;
|
---|
179 |
|
---|
180 | return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | unsigned long X509_NAME_hash(X509_NAME *x)
|
---|
185 | {
|
---|
186 | unsigned long ret = 0;
|
---|
187 | unsigned char md[SHA_DIGEST_LENGTH];
|
---|
188 |
|
---|
189 | /* Make sure X509_NAME structure contains valid cached encoding */
|
---|
190 | i2d_X509_NAME(x, NULL);
|
---|
191 | if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
|
---|
192 | NULL))
|
---|
193 | return 0;
|
---|
194 |
|
---|
195 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
---|
196 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
---|
197 | ) & 0xffffffffL;
|
---|
198 | return (ret);
|
---|
199 | }
|
---|
200 |
|
---|
201 | #ifndef OPENSSL_NO_MD5
|
---|
202 | /*
|
---|
203 | * I now DER encode the name and hash it. Since I cache the DER encoding,
|
---|
204 | * this is reasonably efficient.
|
---|
205 | */
|
---|
206 |
|
---|
207 | unsigned long X509_NAME_hash_old(X509_NAME *x)
|
---|
208 | {
|
---|
209 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
|
---|
210 | unsigned long ret = 0;
|
---|
211 | unsigned char md[16];
|
---|
212 |
|
---|
213 | if (md_ctx == NULL)
|
---|
214 | return ret;
|
---|
215 |
|
---|
216 | /* Make sure X509_NAME structure contains valid cached encoding */
|
---|
217 | i2d_X509_NAME(x, NULL);
|
---|
218 | EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
|
---|
219 | if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
|
---|
220 | && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
|
---|
221 | && EVP_DigestFinal_ex(md_ctx, md, NULL))
|
---|
222 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
---|
223 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
---|
224 | ) & 0xffffffffL;
|
---|
225 | EVP_MD_CTX_free(md_ctx);
|
---|
226 |
|
---|
227 | return (ret);
|
---|
228 | }
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | /* Search a stack of X509 for a match */
|
---|
232 | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
|
---|
233 | ASN1_INTEGER *serial)
|
---|
234 | {
|
---|
235 | int i;
|
---|
236 | X509 x, *x509 = NULL;
|
---|
237 |
|
---|
238 | if (!sk)
|
---|
239 | return NULL;
|
---|
240 |
|
---|
241 | x.cert_info.serialNumber = *serial;
|
---|
242 | x.cert_info.issuer = name;
|
---|
243 |
|
---|
244 | for (i = 0; i < sk_X509_num(sk); i++) {
|
---|
245 | x509 = sk_X509_value(sk, i);
|
---|
246 | if (X509_issuer_and_serial_cmp(x509, &x) == 0)
|
---|
247 | return (x509);
|
---|
248 | }
|
---|
249 | return (NULL);
|
---|
250 | }
|
---|
251 |
|
---|
252 | X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
|
---|
253 | {
|
---|
254 | X509 *x509;
|
---|
255 | int i;
|
---|
256 |
|
---|
257 | for (i = 0; i < sk_X509_num(sk); i++) {
|
---|
258 | x509 = sk_X509_value(sk, i);
|
---|
259 | if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
|
---|
260 | return (x509);
|
---|
261 | }
|
---|
262 | return (NULL);
|
---|
263 | }
|
---|
264 |
|
---|
265 | EVP_PKEY *X509_get0_pubkey(const X509 *x)
|
---|
266 | {
|
---|
267 | if (x == NULL)
|
---|
268 | return NULL;
|
---|
269 | return X509_PUBKEY_get0(x->cert_info.key);
|
---|
270 | }
|
---|
271 |
|
---|
272 | EVP_PKEY *X509_get_pubkey(X509 *x)
|
---|
273 | {
|
---|
274 | if (x == NULL)
|
---|
275 | return NULL;
|
---|
276 | return X509_PUBKEY_get(x->cert_info.key);
|
---|
277 | }
|
---|
278 |
|
---|
279 | int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
|
---|
280 | {
|
---|
281 | const EVP_PKEY *xk;
|
---|
282 | int ret;
|
---|
283 |
|
---|
284 | xk = X509_get0_pubkey(x);
|
---|
285 |
|
---|
286 | if (xk)
|
---|
287 | ret = EVP_PKEY_cmp(xk, k);
|
---|
288 | else
|
---|
289 | ret = -2;
|
---|
290 |
|
---|
291 | switch (ret) {
|
---|
292 | case 1:
|
---|
293 | break;
|
---|
294 | case 0:
|
---|
295 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
|
---|
296 | break;
|
---|
297 | case -1:
|
---|
298 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
|
---|
299 | break;
|
---|
300 | case -2:
|
---|
301 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
|
---|
302 | }
|
---|
303 | if (ret > 0)
|
---|
304 | return 1;
|
---|
305 | return 0;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Check a suite B algorithm is permitted: pass in a public key and the NID
|
---|
310 | * of its signature (or 0 if no signature). The pflags is a pointer to a
|
---|
311 | * flags field which must contain the suite B verification flags.
|
---|
312 | */
|
---|
313 |
|
---|
314 | #ifndef OPENSSL_NO_EC
|
---|
315 |
|
---|
316 | static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
|
---|
317 | {
|
---|
318 | const EC_GROUP *grp = NULL;
|
---|
319 | int curve_nid;
|
---|
320 | if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
|
---|
321 | grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
|
---|
322 | if (!grp)
|
---|
323 | return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
|
---|
324 | curve_nid = EC_GROUP_get_curve_name(grp);
|
---|
325 | /* Check curve is consistent with LOS */
|
---|
326 | if (curve_nid == NID_secp384r1) { /* P-384 */
|
---|
327 | /*
|
---|
328 | * Check signature algorithm is consistent with curve.
|
---|
329 | */
|
---|
330 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
|
---|
331 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
|
---|
332 | if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
|
---|
333 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
|
---|
334 | /* If we encounter P-384 we cannot use P-256 later */
|
---|
335 | *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
|
---|
336 | } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
|
---|
337 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
|
---|
338 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
|
---|
339 | if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
|
---|
340 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
|
---|
341 | } else
|
---|
342 | return X509_V_ERR_SUITE_B_INVALID_CURVE;
|
---|
343 |
|
---|
344 | return X509_V_OK;
|
---|
345 | }
|
---|
346 |
|
---|
347 | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
|
---|
348 | unsigned long flags)
|
---|
349 | {
|
---|
350 | int rv, i, sign_nid;
|
---|
351 | EVP_PKEY *pk;
|
---|
352 | unsigned long tflags = flags;
|
---|
353 |
|
---|
354 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
|
---|
355 | return X509_V_OK;
|
---|
356 |
|
---|
357 | /* If no EE certificate passed in must be first in chain */
|
---|
358 | if (x == NULL) {
|
---|
359 | x = sk_X509_value(chain, 0);
|
---|
360 | i = 1;
|
---|
361 | } else
|
---|
362 | i = 0;
|
---|
363 |
|
---|
364 | pk = X509_get0_pubkey(x);
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
|
---|
368 | * a chain all, just report trust success or failure, but must also report
|
---|
369 | * Suite-B errors if applicable. This is indicated via a NULL chain
|
---|
370 | * pointer. All we need to do is check the leaf key algorithm.
|
---|
371 | */
|
---|
372 | if (chain == NULL)
|
---|
373 | return check_suite_b(pk, -1, &tflags);
|
---|
374 |
|
---|
375 | if (X509_get_version(x) != 2) {
|
---|
376 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
|
---|
377 | /* Correct error depth */
|
---|
378 | i = 0;
|
---|
379 | goto end;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* Check EE key only */
|
---|
383 | rv = check_suite_b(pk, -1, &tflags);
|
---|
384 | if (rv != X509_V_OK) {
|
---|
385 | /* Correct error depth */
|
---|
386 | i = 0;
|
---|
387 | goto end;
|
---|
388 | }
|
---|
389 | for (; i < sk_X509_num(chain); i++) {
|
---|
390 | sign_nid = X509_get_signature_nid(x);
|
---|
391 | x = sk_X509_value(chain, i);
|
---|
392 | if (X509_get_version(x) != 2) {
|
---|
393 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
|
---|
394 | goto end;
|
---|
395 | }
|
---|
396 | pk = X509_get0_pubkey(x);
|
---|
397 | rv = check_suite_b(pk, sign_nid, &tflags);
|
---|
398 | if (rv != X509_V_OK)
|
---|
399 | goto end;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* Final check: root CA signature */
|
---|
403 | rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
|
---|
404 | end:
|
---|
405 | if (rv != X509_V_OK) {
|
---|
406 | /* Invalid signature or LOS errors are for previous cert */
|
---|
407 | if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
|
---|
408 | || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
|
---|
409 | i--;
|
---|
410 | /*
|
---|
411 | * If we have LOS error and flags changed then we are signing P-384
|
---|
412 | * with P-256. Use more meaningful error.
|
---|
413 | */
|
---|
414 | if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
|
---|
415 | rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
|
---|
416 | if (perror_depth)
|
---|
417 | *perror_depth = i;
|
---|
418 | }
|
---|
419 | return rv;
|
---|
420 | }
|
---|
421 |
|
---|
422 | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
|
---|
423 | {
|
---|
424 | int sign_nid;
|
---|
425 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
|
---|
426 | return X509_V_OK;
|
---|
427 | sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
|
---|
428 | return check_suite_b(pk, sign_nid, &flags);
|
---|
429 | }
|
---|
430 |
|
---|
431 | #else
|
---|
432 | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
|
---|
433 | unsigned long flags)
|
---|
434 | {
|
---|
435 | return 0;
|
---|
436 | }
|
---|
437 |
|
---|
438 | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
|
---|
439 | {
|
---|
440 | return 0;
|
---|
441 | }
|
---|
442 |
|
---|
443 | #endif
|
---|
444 | /*
|
---|
445 | * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
|
---|
446 | * count but it has the same effect by duping the STACK and upping the ref of
|
---|
447 | * each X509 structure.
|
---|
448 | */
|
---|
449 | STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
|
---|
450 | {
|
---|
451 | STACK_OF(X509) *ret;
|
---|
452 | int i;
|
---|
453 | ret = sk_X509_dup(chain);
|
---|
454 | for (i = 0; i < sk_X509_num(ret); i++) {
|
---|
455 | X509 *x = sk_X509_value(ret, i);
|
---|
456 | X509_up_ref(x);
|
---|
457 | }
|
---|
458 | return ret;
|
---|
459 | }
|
---|