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