1/*
2 * Copyright 1995-2018 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
19int 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
33unsigned 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
63int 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
68int 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
73int 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
78int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
79{
80 return memcmp(a->sha1_hash, b->sha1_hash, 20);
81}
82
83X509_NAME *X509_get_issuer_name(const X509 *a)
84{
85 return a->cert_info.issuer;
86}
87
88unsigned long X509_issuer_name_hash(X509 *x)
89{
90 return X509_NAME_hash(x->cert_info.issuer);
91}
92
93#ifndef OPENSSL_NO_MD5
94unsigned long X509_issuer_name_hash_old(X509 *x)
95{
96 return X509_NAME_hash_old(x->cert_info.issuer);
97}
98#endif
99
100X509_NAME *X509_get_subject_name(const X509 *a)
101{
102 return a->cert_info.subject;
103}
104
105ASN1_INTEGER *X509_get_serialNumber(X509 *a)
106{
107 return &a->cert_info.serialNumber;
108}
109
110const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
111{
112 return &a->cert_info.serialNumber;
113}
114
115unsigned long X509_subject_name_hash(X509 *x)
116{
117 return X509_NAME_hash(x->cert_info.subject);
118}
119
120#ifndef OPENSSL_NO_MD5
121unsigned 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 */
135int 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
157int 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 != 0 || a->canon_enclen == 0)
178 return ret;
179
180 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
181
182}
183
184unsigned 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
207unsigned long X509_NAME_hash_old(X509_NAME *x)
208{
209 EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips");
210 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
211 unsigned long ret = 0;
212 unsigned char md[16];
213
214 if (md5 == NULL || md_ctx == NULL)
215 goto end;
216
217 /* Make sure X509_NAME structure contains valid cached encoding */
218 i2d_X509_NAME(x, NULL);
219 if (EVP_DigestInit_ex(md_ctx, 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
226 end:
227 EVP_MD_CTX_free(md_ctx);
228 EVP_MD_free(md5);
229
230 return ret;
231}
232#endif
233
234/* Search a stack of X509 for a match */
235X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
236 ASN1_INTEGER *serial)
237{
238 int i;
239 X509 x, *x509 = NULL;
240
241 if (!sk)
242 return NULL;
243
244 x.cert_info.serialNumber = *serial;
245 x.cert_info.issuer = name;
246
247 for (i = 0; i < sk_X509_num(sk); i++) {
248 x509 = sk_X509_value(sk, i);
249 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
250 return x509;
251 }
252 return NULL;
253}
254
255X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
256{
257 X509 *x509;
258 int i;
259
260 for (i = 0; i < sk_X509_num(sk); i++) {
261 x509 = sk_X509_value(sk, i);
262 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
263 return x509;
264 }
265 return NULL;
266}
267
268EVP_PKEY *X509_get0_pubkey(const X509 *x)
269{
270 if (x == NULL)
271 return NULL;
272 return X509_PUBKEY_get0(x->cert_info.key);
273}
274
275EVP_PKEY *X509_get_pubkey(X509 *x)
276{
277 if (x == NULL)
278 return NULL;
279 return X509_PUBKEY_get(x->cert_info.key);
280}
281
282int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
283{
284 const EVP_PKEY *xk;
285 int ret;
286
287 xk = X509_get0_pubkey(x);
288
289 if (xk)
290 ret = EVP_PKEY_cmp(xk, k);
291 else
292 ret = -2;
293
294 switch (ret) {
295 case 1:
296 break;
297 case 0:
298 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
299 break;
300 case -1:
301 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
302 break;
303 case -2:
304 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
305 }
306 if (ret > 0)
307 return 1;
308 return 0;
309}
310
311/*
312 * Check a suite B algorithm is permitted: pass in a public key and the NID
313 * of its signature (or 0 if no signature). The pflags is a pointer to a
314 * flags field which must contain the suite B verification flags.
315 */
316
317#ifndef OPENSSL_NO_EC
318
319static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
320{
321 const EC_GROUP *grp = NULL;
322 int curve_nid;
323 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
324 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
325 if (!grp)
326 return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
327 curve_nid = EC_GROUP_get_curve_name(grp);
328 /* Check curve is consistent with LOS */
329 if (curve_nid == NID_secp384r1) { /* P-384 */
330 /*
331 * Check signature algorithm is consistent with curve.
332 */
333 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
334 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
335 if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
336 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
337 /* If we encounter P-384 we cannot use P-256 later */
338 *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
339 } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
340 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
341 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
342 if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
343 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
344 } else
345 return X509_V_ERR_SUITE_B_INVALID_CURVE;
346
347 return X509_V_OK;
348}
349
350int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
351 unsigned long flags)
352{
353 int rv, i, sign_nid;
354 EVP_PKEY *pk;
355 unsigned long tflags = flags;
356
357 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
358 return X509_V_OK;
359
360 /* If no EE certificate passed in must be first in chain */
361 if (x == NULL) {
362 x = sk_X509_value(chain, 0);
363 i = 1;
364 } else
365 i = 0;
366
367 pk = X509_get0_pubkey(x);
368
369 /*
370 * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
371 * a chain all, just report trust success or failure, but must also report
372 * Suite-B errors if applicable. This is indicated via a NULL chain
373 * pointer. All we need to do is check the leaf key algorithm.
374 */
375 if (chain == NULL)
376 return check_suite_b(pk, -1, &tflags);
377
378 if (X509_get_version(x) != 2) {
379 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
380 /* Correct error depth */
381 i = 0;
382 goto end;
383 }
384
385 /* Check EE key only */
386 rv = check_suite_b(pk, -1, &tflags);
387 if (rv != X509_V_OK) {
388 /* Correct error depth */
389 i = 0;
390 goto end;
391 }
392 for (; i < sk_X509_num(chain); i++) {
393 sign_nid = X509_get_signature_nid(x);
394 x = sk_X509_value(chain, i);
395 if (X509_get_version(x) != 2) {
396 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
397 goto end;
398 }
399 pk = X509_get0_pubkey(x);
400 rv = check_suite_b(pk, sign_nid, &tflags);
401 if (rv != X509_V_OK)
402 goto end;
403 }
404
405 /* Final check: root CA signature */
406 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
407 end:
408 if (rv != X509_V_OK) {
409 /* Invalid signature or LOS errors are for previous cert */
410 if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
411 || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
412 i--;
413 /*
414 * If we have LOS error and flags changed then we are signing P-384
415 * with P-256. Use more meaningful error.
416 */
417 if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
418 rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
419 if (perror_depth)
420 *perror_depth = i;
421 }
422 return rv;
423}
424
425int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
426{
427 int sign_nid;
428 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
429 return X509_V_OK;
430 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
431 return check_suite_b(pk, sign_nid, &flags);
432}
433
434#else
435int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
436 unsigned long flags)
437{
438 return 0;
439}
440
441int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
442{
443 return 0;
444}
445
446#endif
447/*
448 * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
449 * count but it has the same effect by duping the STACK and upping the ref of
450 * each X509 structure.
451 */
452STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
453{
454 STACK_OF(X509) *ret;
455 int i;
456 ret = sk_X509_dup(chain);
457 if (ret == NULL)
458 return NULL;
459 for (i = 0; i < sk_X509_num(ret); i++) {
460 X509 *x = sk_X509_value(ret, i);
461 if (!X509_up_ref(x))
462 goto err;
463 }
464 return ret;
465 err:
466 while (i-- > 0)
467 X509_free (sk_X509_value(ret, i));
468 sk_X509_free(ret);
469 return NULL;
470}
471