• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com). */
56 
57 #include <stdio.h>
58 
59 #include <string.h>
60 
61 #include <openssl/digest.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/thread.h>
66 #include <openssl/x509v3.h>
67 
68 #include "../internal.h"
69 #include "../x509/internal.h"
70 #include "internal.h"
71 
72 #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
73 #define ku_reject(x, usage) \
74   (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
75 #define xku_reject(x, usage) \
76   (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
77 #define ns_reject(x, usage) \
78   (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
79 
80 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
81                                     int ca);
82 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
83                                     int ca);
84 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
85                                        int ca);
86 static int purpose_smime(const X509 *x, int ca);
87 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
88                                     int ca);
89 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
90                                        int ca);
91 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
92                                   int ca);
93 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
94                                         int ca);
95 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
96 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
97 
98 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
99 static void xptable_free(X509_PURPOSE *p);
100 
101 static X509_PURPOSE xstandard[] = {
102     {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
103      check_purpose_ssl_client, (char *)"SSL client", (char *)"sslclient", NULL},
104     {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
105      check_purpose_ssl_server, (char *)"SSL server", (char *)"sslserver", NULL},
106     {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
107      check_purpose_ns_ssl_server, (char *)"Netscape SSL server",
108      (char *)"nssslserver", NULL},
109     {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
110      (char *)"S/MIME signing", (char *)"smimesign", NULL},
111     {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
112      check_purpose_smime_encrypt, (char *)"S/MIME encryption",
113      (char *)"smimeencrypt", NULL},
114     {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
115      (char *)"CRL signing", (char *)"crlsign", NULL},
116     {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, (char *)"Any Purpose",
117      (char *)"any", NULL},
118     {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
119      (char *)"OCSP helper", (char *)"ocsphelper", NULL},
120     {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
121      check_purpose_timestamp_sign, (char *)"Time Stamp signing",
122      (char *)"timestampsign", NULL},
123 };
124 
125 #define X509_PURPOSE_COUNT (sizeof(xstandard) / sizeof(X509_PURPOSE))
126 
127 static STACK_OF(X509_PURPOSE) *xptable = NULL;
128 
xp_cmp(const X509_PURPOSE * const * a,const X509_PURPOSE * const * b)129 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b) {
130   return (*a)->purpose - (*b)->purpose;
131 }
132 
133 // As much as I'd like to make X509_check_purpose use a "const" X509* I
134 // really can't because it does recalculate hashes and do other non-const
135 // things.
X509_check_purpose(X509 * x,int id,int ca)136 int X509_check_purpose(X509 *x, int id, int ca) {
137   int idx;
138   const X509_PURPOSE *pt;
139   if (!x509v3_cache_extensions(x)) {
140     return -1;
141   }
142 
143   if (id == -1) {
144     return 1;
145   }
146   idx = X509_PURPOSE_get_by_id(id);
147   if (idx == -1) {
148     return -1;
149   }
150   pt = X509_PURPOSE_get0(idx);
151   return pt->check_purpose(pt, x, ca);
152 }
153 
X509_PURPOSE_set(int * p,int purpose)154 int X509_PURPOSE_set(int *p, int purpose) {
155   if (X509_PURPOSE_get_by_id(purpose) == -1) {
156     OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_PURPOSE);
157     return 0;
158   }
159   *p = purpose;
160   return 1;
161 }
162 
X509_PURPOSE_get_count(void)163 int X509_PURPOSE_get_count(void) {
164   if (!xptable) {
165     return X509_PURPOSE_COUNT;
166   }
167   return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
168 }
169 
X509_PURPOSE_get0(int idx)170 X509_PURPOSE *X509_PURPOSE_get0(int idx) {
171   if (idx < 0) {
172     return NULL;
173   }
174   if (idx < (int)X509_PURPOSE_COUNT) {
175     return xstandard + idx;
176   }
177   return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
178 }
179 
X509_PURPOSE_get_by_sname(const char * sname)180 int X509_PURPOSE_get_by_sname(const char *sname) {
181   X509_PURPOSE *xptmp;
182   for (int i = 0; i < X509_PURPOSE_get_count(); i++) {
183     xptmp = X509_PURPOSE_get0(i);
184     if (!strcmp(xptmp->sname, sname)) {
185       return i;
186     }
187   }
188   return -1;
189 }
190 
X509_PURPOSE_get_by_id(int purpose)191 int X509_PURPOSE_get_by_id(int purpose) {
192   X509_PURPOSE tmp;
193   size_t idx;
194 
195   if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX)) {
196     return purpose - X509_PURPOSE_MIN;
197   }
198   tmp.purpose = purpose;
199   if (!xptable) {
200     return -1;
201   }
202 
203   if (!sk_X509_PURPOSE_find(xptable, &idx, &tmp)) {
204     return -1;
205   }
206   return idx + X509_PURPOSE_COUNT;
207 }
208 
X509_PURPOSE_add(int id,int trust,int flags,int (* ck)(const X509_PURPOSE *,const X509 *,int),const char * name,const char * sname,void * arg)209 int X509_PURPOSE_add(int id, int trust, int flags,
210                      int (*ck)(const X509_PURPOSE *, const X509 *, int),
211                      const char *name, const char *sname, void *arg) {
212   X509_PURPOSE *ptmp;
213   char *name_dup, *sname_dup;
214 
215   // This is set according to what we change: application can't set it
216   flags &= ~X509_PURPOSE_DYNAMIC;
217   // This will always be set for application modified trust entries
218   flags |= X509_PURPOSE_DYNAMIC_NAME;
219   // Get existing entry if any
220   int idx = X509_PURPOSE_get_by_id(id);
221   // Need a new entry
222   if (idx == -1) {
223     if (!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
224       return 0;
225     }
226     ptmp->flags = X509_PURPOSE_DYNAMIC;
227   } else {
228     ptmp = X509_PURPOSE_get0(idx);
229   }
230 
231   // Duplicate the supplied names.
232   name_dup = OPENSSL_strdup(name);
233   sname_dup = OPENSSL_strdup(sname);
234   if (name_dup == NULL || sname_dup == NULL) {
235     if (name_dup != NULL) {
236       OPENSSL_free(name_dup);
237     }
238     if (sname_dup != NULL) {
239       OPENSSL_free(sname_dup);
240     }
241     if (idx == -1) {
242       OPENSSL_free(ptmp);
243     }
244     return 0;
245   }
246 
247   // OPENSSL_free existing name if dynamic
248   if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
249     OPENSSL_free(ptmp->name);
250     OPENSSL_free(ptmp->sname);
251   }
252   // dup supplied name
253   ptmp->name = name_dup;
254   ptmp->sname = sname_dup;
255   // Keep the dynamic flag of existing entry
256   ptmp->flags &= X509_PURPOSE_DYNAMIC;
257   // Set all other flags
258   ptmp->flags |= flags;
259 
260   ptmp->purpose = id;
261   ptmp->trust = trust;
262   ptmp->check_purpose = ck;
263   ptmp->usr_data = arg;
264 
265   // If its a new entry manage the dynamic table
266   if (idx == -1) {
267     // TODO(davidben): This should be locked. Alternatively, remove the dynamic
268     // registration mechanism entirely. The trouble is there no way to pass in
269     // the various parameters into an |X509_VERIFY_PARAM| directly. You can only
270     // register it in the global table and get an ID.
271     if (!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
272       xptable_free(ptmp);
273       return 0;
274     }
275     if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
276       xptable_free(ptmp);
277       return 0;
278     }
279     sk_X509_PURPOSE_sort(xptable);
280   }
281   return 1;
282 }
283 
xptable_free(X509_PURPOSE * p)284 static void xptable_free(X509_PURPOSE *p) {
285   if (!p) {
286     return;
287   }
288   if (p->flags & X509_PURPOSE_DYNAMIC) {
289     if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
290       OPENSSL_free(p->name);
291       OPENSSL_free(p->sname);
292     }
293     OPENSSL_free(p);
294   }
295 }
296 
X509_PURPOSE_cleanup(void)297 void X509_PURPOSE_cleanup(void) {
298   unsigned int i;
299   sk_X509_PURPOSE_pop_free(xptable, xptable_free);
300   for (i = 0; i < X509_PURPOSE_COUNT; i++) {
301     xptable_free(xstandard + i);
302   }
303   xptable = NULL;
304 }
305 
X509_PURPOSE_get_id(const X509_PURPOSE * xp)306 int X509_PURPOSE_get_id(const X509_PURPOSE *xp) { return xp->purpose; }
307 
X509_PURPOSE_get0_name(const X509_PURPOSE * xp)308 char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp) { return xp->name; }
309 
X509_PURPOSE_get0_sname(const X509_PURPOSE * xp)310 char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) { return xp->sname; }
311 
X509_PURPOSE_get_trust(const X509_PURPOSE * xp)312 int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) { return xp->trust; }
313 
nid_cmp(const void * void_a,const void * void_b)314 static int nid_cmp(const void *void_a, const void *void_b) {
315   const int *a = void_a, *b = void_b;
316 
317   return *a - *b;
318 }
319 
X509_supported_extension(const X509_EXTENSION * ex)320 int X509_supported_extension(const X509_EXTENSION *ex) {
321   // This table is a list of the NIDs of supported extensions: that is
322   // those which are used by the verify process. If an extension is
323   // critical and doesn't appear in this list then the verify process will
324   // normally reject the certificate. The list must be kept in numerical
325   // order because it will be searched using bsearch.
326 
327   static const int supported_nids[] = {
328       NID_netscape_cert_type,    // 71
329       NID_key_usage,             // 83
330       NID_subject_alt_name,      // 85
331       NID_basic_constraints,     // 87
332       NID_certificate_policies,  // 89
333       NID_ext_key_usage,         // 126
334       NID_policy_constraints,    // 401
335       NID_name_constraints,      // 666
336       NID_policy_mappings,       // 747
337       NID_inhibit_any_policy     // 748
338   };
339 
340   int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
341 
342   if (ex_nid == NID_undef) {
343     return 0;
344   }
345 
346   if (bsearch(&ex_nid, supported_nids, sizeof(supported_nids) / sizeof(int),
347               sizeof(int), nid_cmp) != NULL) {
348     return 1;
349   }
350   return 0;
351 }
352 
setup_dp(X509 * x,DIST_POINT * dp)353 static int setup_dp(X509 *x, DIST_POINT *dp) {
354   X509_NAME *iname = NULL;
355   size_t i;
356   if (dp->reasons) {
357     if (dp->reasons->length > 0) {
358       dp->dp_reasons = dp->reasons->data[0];
359     }
360     if (dp->reasons->length > 1) {
361       dp->dp_reasons |= (dp->reasons->data[1] << 8);
362     }
363     dp->dp_reasons &= CRLDP_ALL_REASONS;
364   } else {
365     dp->dp_reasons = CRLDP_ALL_REASONS;
366   }
367   if (!dp->distpoint || (dp->distpoint->type != 1)) {
368     return 1;
369   }
370   for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
371     GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
372     if (gen->type == GEN_DIRNAME) {
373       iname = gen->d.directoryName;
374       break;
375     }
376   }
377   if (!iname) {
378     iname = X509_get_issuer_name(x);
379   }
380 
381   return DIST_POINT_set_dpname(dp->distpoint, iname);
382 }
383 
setup_crldp(X509 * x)384 static int setup_crldp(X509 *x) {
385   int j;
386   x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &j, NULL);
387   if (x->crldp == NULL && j != -1) {
388     return 0;
389   }
390   for (size_t i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
391     if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i))) {
392       return 0;
393     }
394   }
395   return 1;
396 }
397 
x509v3_cache_extensions(X509 * x)398 int x509v3_cache_extensions(X509 *x) {
399   BASIC_CONSTRAINTS *bs;
400   ASN1_BIT_STRING *usage;
401   ASN1_BIT_STRING *ns;
402   EXTENDED_KEY_USAGE *extusage;
403   size_t i;
404   int j;
405 
406   CRYPTO_MUTEX_lock_read(&x->lock);
407   const int is_set = x->ex_flags & EXFLAG_SET;
408   CRYPTO_MUTEX_unlock_read(&x->lock);
409 
410   if (is_set) {
411     return (x->ex_flags & EXFLAG_INVALID) == 0;
412   }
413 
414   CRYPTO_MUTEX_lock_write(&x->lock);
415   if (x->ex_flags & EXFLAG_SET) {
416     CRYPTO_MUTEX_unlock_write(&x->lock);
417     return (x->ex_flags & EXFLAG_INVALID) == 0;
418   }
419 
420   if (!X509_digest(x, EVP_sha256(), x->cert_hash, NULL)) {
421     x->ex_flags |= EXFLAG_INVALID;
422   }
423   // V1 should mean no extensions ...
424   if (X509_get_version(x) == X509_VERSION_1) {
425     x->ex_flags |= EXFLAG_V1;
426   }
427   // Handle basic constraints
428   if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &j, NULL))) {
429     if (bs->ca) {
430       x->ex_flags |= EXFLAG_CA;
431     }
432     if (bs->pathlen) {
433       if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) || !bs->ca) {
434         x->ex_flags |= EXFLAG_INVALID;
435         x->ex_pathlen = 0;
436       } else {
437         // TODO(davidben): |ASN1_INTEGER_get| returns -1 on overflow,
438         // which currently acts as if the constraint isn't present. This
439         // works (an overflowing path length constraint may as well be
440         // infinity), but Chromium's verifier simply treats values above
441         // 255 as an error.
442         x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
443       }
444     } else {
445       x->ex_pathlen = -1;
446     }
447     BASIC_CONSTRAINTS_free(bs);
448     x->ex_flags |= EXFLAG_BCONS;
449   } else if (j != -1) {
450     x->ex_flags |= EXFLAG_INVALID;
451   }
452   // Handle key usage
453   if ((usage = X509_get_ext_d2i(x, NID_key_usage, &j, NULL))) {
454     if (usage->length > 0) {
455       x->ex_kusage = usage->data[0];
456       if (usage->length > 1) {
457         x->ex_kusage |= usage->data[1] << 8;
458       }
459     } else {
460       x->ex_kusage = 0;
461     }
462     x->ex_flags |= EXFLAG_KUSAGE;
463     ASN1_BIT_STRING_free(usage);
464   } else if (j != -1) {
465     x->ex_flags |= EXFLAG_INVALID;
466   }
467   x->ex_xkusage = 0;
468   if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &j, NULL))) {
469     x->ex_flags |= EXFLAG_XKUSAGE;
470     for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
471       switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
472         case NID_server_auth:
473           x->ex_xkusage |= XKU_SSL_SERVER;
474           break;
475 
476         case NID_client_auth:
477           x->ex_xkusage |= XKU_SSL_CLIENT;
478           break;
479 
480         case NID_email_protect:
481           x->ex_xkusage |= XKU_SMIME;
482           break;
483 
484         case NID_code_sign:
485           x->ex_xkusage |= XKU_CODE_SIGN;
486           break;
487 
488         case NID_ms_sgc:
489         case NID_ns_sgc:
490           x->ex_xkusage |= XKU_SGC;
491           break;
492 
493         case NID_OCSP_sign:
494           x->ex_xkusage |= XKU_OCSP_SIGN;
495           break;
496 
497         case NID_time_stamp:
498           x->ex_xkusage |= XKU_TIMESTAMP;
499           break;
500 
501         case NID_dvcs:
502           x->ex_xkusage |= XKU_DVCS;
503           break;
504 
505         case NID_anyExtendedKeyUsage:
506           x->ex_xkusage |= XKU_ANYEKU;
507           break;
508       }
509     }
510     sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
511   } else if (j != -1) {
512     x->ex_flags |= EXFLAG_INVALID;
513   }
514 
515   if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &j, NULL))) {
516     if (ns->length > 0) {
517       x->ex_nscert = ns->data[0];
518     } else {
519       x->ex_nscert = 0;
520     }
521     x->ex_flags |= EXFLAG_NSCERT;
522     ASN1_BIT_STRING_free(ns);
523   } else if (j != -1) {
524     x->ex_flags |= EXFLAG_INVALID;
525   }
526   x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &j, NULL);
527   if (x->skid == NULL && j != -1) {
528     x->ex_flags |= EXFLAG_INVALID;
529   }
530   x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &j, NULL);
531   if (x->akid == NULL && j != -1) {
532     x->ex_flags |= EXFLAG_INVALID;
533   }
534   // Does subject name match issuer ?
535   if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
536     x->ex_flags |= EXFLAG_SI;
537     // If SKID matches AKID also indicate self signed
538     if (X509_check_akid(x, x->akid) == X509_V_OK &&
539         !ku_reject(x, KU_KEY_CERT_SIGN)) {
540       x->ex_flags |= EXFLAG_SS;
541     }
542   }
543   x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &j, NULL);
544   if (x->altname == NULL && j != -1) {
545     x->ex_flags |= EXFLAG_INVALID;
546   }
547   x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL);
548   if (x->nc == NULL && j != -1) {
549     x->ex_flags |= EXFLAG_INVALID;
550   }
551   if (!setup_crldp(x)) {
552     x->ex_flags |= EXFLAG_INVALID;
553   }
554 
555   for (j = 0; j < X509_get_ext_count(x); j++) {
556     const X509_EXTENSION *ex = X509_get_ext(x, j);
557     if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == NID_freshest_crl) {
558       x->ex_flags |= EXFLAG_FRESHEST;
559     }
560     if (!X509_EXTENSION_get_critical(ex)) {
561       continue;
562     }
563     if (!X509_supported_extension(ex)) {
564       x->ex_flags |= EXFLAG_CRITICAL;
565       break;
566     }
567   }
568   x->ex_flags |= EXFLAG_SET;
569 
570   CRYPTO_MUTEX_unlock_write(&x->lock);
571   return (x->ex_flags & EXFLAG_INVALID) == 0;
572 }
573 
574 // check_ca returns one if |x| should be considered a CA certificate and zero
575 // otherwise.
check_ca(const X509 * x)576 static int check_ca(const X509 *x) {
577   // keyUsage if present should allow cert signing
578   if (ku_reject(x, KU_KEY_CERT_SIGN)) {
579     return 0;
580   }
581   // Version 1 certificates are considered CAs and don't have extensions.
582   if ((x->ex_flags & V1_ROOT) == V1_ROOT) {
583     return 1;
584   }
585   // Otherwise, it's only a CA if basicConstraints says so.
586   return ((x->ex_flags & EXFLAG_BCONS) && (x->ex_flags & EXFLAG_CA));
587 }
588 
X509_check_ca(X509 * x)589 int X509_check_ca(X509 *x) {
590   if (!x509v3_cache_extensions(x)) {
591     return 0;
592   }
593   return check_ca(x);
594 }
595 
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int ca)596 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
597                                     int ca) {
598   if (xku_reject(x, XKU_SSL_CLIENT)) {
599     return 0;
600   }
601   if (ca) {
602     return check_ca(x);
603   }
604   // We need to do digital signatures or key agreement
605   if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) {
606     return 0;
607   }
608   // nsCertType if present should allow SSL client use
609   if (ns_reject(x, NS_SSL_CLIENT)) {
610     return 0;
611   }
612   return 1;
613 }
614 
615 // Key usage needed for TLS/SSL server: digital signature, encipherment or
616 // key agreement. The ssl code can check this more thoroughly for individual
617 // key types.
618 #define KU_TLS (KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT)
619 
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)620 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
621                                     int ca) {
622   if (xku_reject(x, XKU_SSL_SERVER)) {
623     return 0;
624   }
625   if (ca) {
626     return check_ca(x);
627   }
628 
629   if (ns_reject(x, NS_SSL_SERVER)) {
630     return 0;
631   }
632   if (ku_reject(x, KU_TLS)) {
633     return 0;
634   }
635 
636   return 1;
637 }
638 
check_purpose_ns_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)639 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
640                                        int ca) {
641   int ret;
642   ret = check_purpose_ssl_server(xp, x, ca);
643   if (!ret || ca) {
644     return ret;
645   }
646   // We need to encipher or Netscape complains
647   if (ku_reject(x, KU_KEY_ENCIPHERMENT)) {
648     return 0;
649   }
650   return ret;
651 }
652 
653 // purpose_smime returns one if |x| is a valid S/MIME leaf (|ca| is zero) or CA
654 // (|ca| is one) certificate, and zero otherwise.
purpose_smime(const X509 * x,int ca)655 static int purpose_smime(const X509 *x, int ca) {
656   if (xku_reject(x, XKU_SMIME)) {
657     return 0;
658   }
659   if (ca) {
660     // check nsCertType if present
661     if ((x->ex_flags & EXFLAG_NSCERT) && (x->ex_nscert & NS_SMIME_CA) == 0) {
662       return 0;
663     }
664 
665     return check_ca(x);
666   }
667   if (x->ex_flags & EXFLAG_NSCERT) {
668     return (x->ex_nscert & NS_SMIME) == NS_SMIME;
669   }
670   return 1;
671 }
672 
check_purpose_smime_sign(const X509_PURPOSE * xp,const X509 * x,int ca)673 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
674                                     int ca) {
675   int ret;
676   ret = purpose_smime(x, ca);
677   if (!ret || ca) {
678     return ret;
679   }
680   if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION)) {
681     return 0;
682   }
683   return ret;
684 }
685 
check_purpose_smime_encrypt(const X509_PURPOSE * xp,const X509 * x,int ca)686 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
687                                        int ca) {
688   int ret;
689   ret = purpose_smime(x, ca);
690   if (!ret || ca) {
691     return ret;
692   }
693   if (ku_reject(x, KU_KEY_ENCIPHERMENT)) {
694     return 0;
695   }
696   return ret;
697 }
698 
check_purpose_crl_sign(const X509_PURPOSE * xp,const X509 * x,int ca)699 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
700                                   int ca) {
701   if (ca) {
702     return check_ca(x);
703   }
704   if (ku_reject(x, KU_CRL_SIGN)) {
705     return 0;
706   }
707   return 1;
708 }
709 
710 // OCSP helper: this is *not* a full OCSP check. It just checks that each CA
711 // is valid. Additional checks must be made on the chain.
712 
ocsp_helper(const X509_PURPOSE * xp,const X509 * x,int ca)713 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca) {
714   if (ca) {
715     return check_ca(x);
716   }
717   // leaf certificate is checked in OCSP_verify()
718   return 1;
719 }
720 
check_purpose_timestamp_sign(const X509_PURPOSE * xp,const X509 * x,int ca)721 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
722                                         int ca) {
723   int i_ext;
724 
725   // If ca is true we must return if this is a valid CA certificate.
726   if (ca) {
727     return check_ca(x);
728   }
729 
730   // Check the optional key usage field:
731   // if Key Usage is present, it must be one of digitalSignature
732   // and/or nonRepudiation (other values are not consistent and shall
733   // be rejected).
734   if ((x->ex_flags & EXFLAG_KUSAGE) &&
735       ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
736        !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) {
737     return 0;
738   }
739 
740   // Only time stamp key usage is permitted and it's required.
741   if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) {
742     return 0;
743   }
744 
745   // Extended Key Usage MUST be critical
746   i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1);
747   if (i_ext >= 0) {
748     const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
749     if (!X509_EXTENSION_get_critical(ext)) {
750       return 0;
751     }
752   }
753 
754   return 1;
755 }
756 
no_check(const X509_PURPOSE * xp,const X509 * x,int ca)757 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) { return 1; }
758 
759 // Various checks to see if one certificate issued the second. This can be
760 // used to prune a set of possible issuer certificates which have been looked
761 // up using some simple method such as by subject name. These are: 1. Check
762 // issuer_name(subject) == subject_name(issuer) 2. If akid(subject) exists
763 // check it matches issuer 3. If key_usage(issuer) exists check it supports
764 // certificate signing returns 0 for OK, positive for reason for mismatch,
765 // reasons match codes for X509_verify_cert()
766 
X509_check_issued(X509 * issuer,X509 * subject)767 int X509_check_issued(X509 *issuer, X509 *subject) {
768   if (X509_NAME_cmp(X509_get_subject_name(issuer),
769                     X509_get_issuer_name(subject))) {
770     return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
771   }
772   if (!x509v3_cache_extensions(issuer) || !x509v3_cache_extensions(subject)) {
773     return X509_V_ERR_UNSPECIFIED;
774   }
775 
776   if (subject->akid) {
777     int ret = X509_check_akid(issuer, subject->akid);
778     if (ret != X509_V_OK) {
779       return ret;
780     }
781   }
782 
783   if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
784     return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
785   }
786   return X509_V_OK;
787 }
788 
X509_check_akid(X509 * issuer,AUTHORITY_KEYID * akid)789 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid) {
790   if (!akid) {
791     return X509_V_OK;
792   }
793 
794   // Check key ids (if present)
795   if (akid->keyid && issuer->skid &&
796       ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) {
797     return X509_V_ERR_AKID_SKID_MISMATCH;
798   }
799   // Check serial number
800   if (akid->serial &&
801       ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) {
802     return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
803   }
804   // Check issuer name
805   if (akid->issuer) {
806     // Ugh, for some peculiar reason AKID includes SEQUENCE OF
807     // GeneralName. So look for a DirName. There may be more than one but
808     // we only take any notice of the first.
809     GENERAL_NAMES *gens;
810     GENERAL_NAME *gen;
811     X509_NAME *nm = NULL;
812     size_t i;
813     gens = akid->issuer;
814     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
815       gen = sk_GENERAL_NAME_value(gens, i);
816       if (gen->type == GEN_DIRNAME) {
817         nm = gen->d.dirn;
818         break;
819       }
820     }
821     if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) {
822       return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
823     }
824   }
825   return X509_V_OK;
826 }
827 
X509_get_extension_flags(X509 * x)828 uint32_t X509_get_extension_flags(X509 *x) {
829   // Ignore the return value. On failure, |x->ex_flags| will include
830   // |EXFLAG_INVALID|.
831   x509v3_cache_extensions(x);
832   return x->ex_flags;
833 }
834 
X509_get_key_usage(X509 * x)835 uint32_t X509_get_key_usage(X509 *x) {
836   if (!x509v3_cache_extensions(x)) {
837     return 0;
838   }
839   if (x->ex_flags & EXFLAG_KUSAGE) {
840     return x->ex_kusage;
841   }
842   return UINT32_MAX;
843 }
844 
X509_get_extended_key_usage(X509 * x)845 uint32_t X509_get_extended_key_usage(X509 *x) {
846   if (!x509v3_cache_extensions(x)) {
847     return 0;
848   }
849   if (x->ex_flags & EXFLAG_XKUSAGE) {
850     return x->ex_xkusage;
851   }
852   return UINT32_MAX;
853 }
854 
X509_get0_subject_key_id(X509 * x509)855 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509) {
856   if (!x509v3_cache_extensions(x509)) {
857     return NULL;
858   }
859   return x509->skid;
860 }
861 
X509_get0_authority_key_id(X509 * x509)862 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509) {
863   if (!x509v3_cache_extensions(x509)) {
864     return NULL;
865   }
866   return x509->akid != NULL ? x509->akid->keyid : NULL;
867 }
868 
X509_get0_authority_issuer(X509 * x509)869 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509) {
870   if (!x509v3_cache_extensions(x509)) {
871     return NULL;
872   }
873   return x509->akid != NULL ? x509->akid->issuer : NULL;
874 }
875 
X509_get0_authority_serial(X509 * x509)876 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509) {
877   if (!x509v3_cache_extensions(x509)) {
878     return NULL;
879   }
880   return x509->akid != NULL ? x509->akid->serial : NULL;
881 }
882 
X509_get_pathlen(X509 * x509)883 long X509_get_pathlen(X509 *x509) {
884   if (!x509v3_cache_extensions(x509) || (x509->ex_flags & EXFLAG_BCONS) == 0) {
885     return -1;
886   }
887   return x509->ex_pathlen;
888 }
889