]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509v3/v3_purp.c
Fix error handling in x509v3_cache_extensions and related functions
[thirdparty/openssl.git] / crypto / x509v3 / v3_purp.c
CommitLineData
0f113f3e 1/*
ba4356ae 2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
673b102c 3 *
d2e9e320
RS
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
673b102c
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
1912c5d8 12#include "internal/numbers.h"
673b102c 13#include <openssl/x509v3.h>
2f043896 14#include <openssl/x509_vfy.h>
0c994d54 15#include "crypto/x509.h"
f21b5b64 16#include "internal/tsan_assist.h"
673b102c 17
ce1b4fe1 18static void x509v3_cache_extensions(X509 *x);
673b102c 19
ccd86b68 20static int check_ssl_ca(const X509 *x);
0f113f3e
MC
21static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
22 int ca);
23static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
24 int ca);
25static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
26 int ca);
ccd86b68 27static int purpose_smime(const X509 *x, int ca);
0f113f3e
MC
28static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
29 int ca);
30static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
31 int ca);
32static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
33 int ca);
34static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
35 int ca);
ccd86b68 36static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
81f169e9 37static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
ccd86b68 38
0f113f3e 39static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
d4cec6a1 40static void xptable_free(X509_PURPOSE *p);
673b102c
DSH
41
42static X509_PURPOSE xstandard[] = {
0f113f3e
MC
43 {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
44 check_purpose_ssl_client, "SSL client", "sslclient", NULL},
45 {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
46 check_purpose_ssl_server, "SSL server", "sslserver", NULL},
47 {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
48 check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
49 {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
50 "S/MIME signing", "smimesign", NULL},
51 {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
52 check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
53 {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
54 "CRL signing", "crlsign", NULL},
55 {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
56 NULL},
57 {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
58 "OCSP helper", "ocsphelper", NULL},
59 {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
60 check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
61 NULL},
673b102c
DSH
62};
63
b6eb9827 64#define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
dd413410 65
79875776 66static STACK_OF(X509_PURPOSE) *xptable = NULL;
673b102c 67
0f113f3e 68static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
673b102c 69{
0f113f3e 70 return (*a)->purpose - (*b)->purpose;
673b102c
DSH
71}
72
0f113f3e
MC
73/*
74 * As much as I'd like to make X509_check_purpose use a "const" X509* I
75 * really can't because it does recalculate hashes and do other non-const
76 * things.
77 */
673b102c
DSH
78int X509_check_purpose(X509 *x, int id, int ca)
79{
0f113f3e
MC
80 int idx;
81 const X509_PURPOSE *pt;
bc624bd9
DMSP
82
83 x509v3_cache_extensions(x);
ba4356ae
BE
84 if (x->ex_flags & EXFLAG_INVALID)
85 return -1;
bc624bd9 86
109f8b5d 87 /* Return if side-effect only call */
0f113f3e
MC
88 if (id == -1)
89 return 1;
90 idx = X509_PURPOSE_get_by_id(id);
91 if (idx == -1)
92 return -1;
93 pt = X509_PURPOSE_get0(idx);
94 return pt->check_purpose(pt, x, ca);
673b102c 95}
e947f396 96
926a56bf
DSH
97int X509_PURPOSE_set(int *p, int purpose)
98{
0f113f3e
MC
99 if (X509_PURPOSE_get_by_id(purpose) == -1) {
100 X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
101 return 0;
102 }
103 *p = purpose;
104 return 1;
926a56bf
DSH
105}
106
d4cec6a1
DSH
107int X509_PURPOSE_get_count(void)
108{
0f113f3e
MC
109 if (!xptable)
110 return X509_PURPOSE_COUNT;
111 return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
d4cec6a1 112}
ce1b4fe1 113
0f113f3e 114X509_PURPOSE *X509_PURPOSE_get0(int idx)
d4cec6a1 115{
0f113f3e
MC
116 if (idx < 0)
117 return NULL;
118 if (idx < (int)X509_PURPOSE_COUNT)
119 return xstandard + idx;
120 return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
d4cec6a1
DSH
121}
122
c8f717fe 123int X509_PURPOSE_get_by_sname(const char *sname)
d4cec6a1 124{
0f113f3e
MC
125 int i;
126 X509_PURPOSE *xptmp;
127 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
128 xptmp = X509_PURPOSE_get0(i);
86885c28 129 if (strcmp(xptmp->sname, sname) == 0)
0f113f3e
MC
130 return i;
131 }
132 return -1;
d4cec6a1 133}
673b102c 134
13938ace 135int X509_PURPOSE_get_by_id(int purpose)
673b102c 136{
0f113f3e
MC
137 X509_PURPOSE tmp;
138 int idx;
5b37fef0 139
0f113f3e
MC
140 if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
141 return purpose - X509_PURPOSE_MIN;
5b37fef0 142 if (xptable == NULL)
0f113f3e 143 return -1;
5b37fef0 144 tmp.purpose = purpose;
0f113f3e 145 idx = sk_X509_PURPOSE_find(xptable, &tmp);
5b37fef0 146 if (idx < 0)
0f113f3e
MC
147 return -1;
148 return idx + X509_PURPOSE_COUNT;
673b102c
DSH
149}
150
dd413410 151int X509_PURPOSE_add(int id, int trust, int flags,
0f113f3e 152 int (*ck) (const X509_PURPOSE *, const X509 *, int),
c8f717fe 153 const char *name, const char *sname, void *arg)
0f113f3e
MC
154{
155 int idx;
156 X509_PURPOSE *ptmp;
157 /*
158 * This is set according to what we change: application can't set it
159 */
160 flags &= ~X509_PURPOSE_DYNAMIC;
161 /* This will always be set for application modified trust entries */
162 flags |= X509_PURPOSE_DYNAMIC_NAME;
163 /* Get existing entry if any */
164 idx = X509_PURPOSE_get_by_id(id);
165 /* Need a new entry */
166 if (idx == -1) {
75ebbd9a 167 if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) {
0f113f3e
MC
168 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
169 return 0;
170 }
171 ptmp->flags = X509_PURPOSE_DYNAMIC;
172 } else
173 ptmp = X509_PURPOSE_get0(idx);
174
175 /* OPENSSL_free existing name if dynamic */
176 if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
177 OPENSSL_free(ptmp->name);
178 OPENSSL_free(ptmp->sname);
179 }
180 /* dup supplied name */
7644a9ae
RS
181 ptmp->name = OPENSSL_strdup(name);
182 ptmp->sname = OPENSSL_strdup(sname);
0f113f3e
MC
183 if (!ptmp->name || !ptmp->sname) {
184 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
137e5555 185 goto err;
0f113f3e
MC
186 }
187 /* Keep the dynamic flag of existing entry */
188 ptmp->flags &= X509_PURPOSE_DYNAMIC;
189 /* Set all other flags */
190 ptmp->flags |= flags;
191
192 ptmp->purpose = id;
193 ptmp->trust = trust;
194 ptmp->check_purpose = ck;
195 ptmp->usr_data = arg;
196
197 /* If its a new entry manage the dynamic table */
198 if (idx == -1) {
75ebbd9a
RS
199 if (xptable == NULL
200 && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
0f113f3e 201 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
137e5555 202 goto err;
0f113f3e
MC
203 }
204 if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
205 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
137e5555 206 goto err;
0f113f3e
MC
207 }
208 }
209 return 1;
137e5555
MC
210 err:
211 if (idx == -1) {
212 OPENSSL_free(ptmp->name);
213 OPENSSL_free(ptmp->sname);
214 OPENSSL_free(ptmp);
215 }
216 return 0;
673b102c
DSH
217}
218
79875776 219static void xptable_free(X509_PURPOSE *p)
0f113f3e
MC
220{
221 if (!p)
222 return;
223 if (p->flags & X509_PURPOSE_DYNAMIC) {
224 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
225 OPENSSL_free(p->name);
226 OPENSSL_free(p->sname);
227 }
228 OPENSSL_free(p);
229 }
230}
79875776
BM
231
232void X509_PURPOSE_cleanup(void)
233{
0f113f3e 234 sk_X509_PURPOSE_pop_free(xptable, xptable_free);
0f113f3e 235 xptable = NULL;
79875776
BM
236}
237
c8f717fe 238int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
673b102c 239{
0f113f3e 240 return xp->purpose;
673b102c
DSH
241}
242
c8f717fe 243char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
d4cec6a1 244{
0f113f3e 245 return xp->name;
d4cec6a1 246}
673b102c 247
c8f717fe 248char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
673b102c 249{
0f113f3e 250 return xp->sname;
673b102c
DSH
251}
252
c8f717fe 253int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
673b102c 254{
0f113f3e 255 return xp->trust;
673b102c
DSH
256}
257
babb3798 258static int nid_cmp(const int *a, const int *b)
0f113f3e
MC
259{
260 return *a - *b;
261}
f1558bb4 262
e19106f5
DSH
263DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
264IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
babb3798 265
f1558bb4 266int X509_supported_extension(X509_EXTENSION *ex)
0f113f3e
MC
267{
268 /*
269 * This table is a list of the NIDs of supported extensions: that is
270 * those which are used by the verify process. If an extension is
271 * critical and doesn't appear in this list then the verify process will
272 * normally reject the certificate. The list must be kept in numerical
273 * order because it will be searched using bsearch.
274 */
275
276 static const int supported_nids[] = {
277 NID_netscape_cert_type, /* 71 */
278 NID_key_usage, /* 83 */
279 NID_subject_alt_name, /* 85 */
280 NID_basic_constraints, /* 87 */
281 NID_certificate_policies, /* 89 */
e6f648fd 282 NID_crl_distribution_points, /* 103 */
0f113f3e 283 NID_ext_key_usage, /* 126 */
47bbaa5b 284#ifndef OPENSSL_NO_RFC3779
0f113f3e
MC
285 NID_sbgp_ipAddrBlock, /* 290 */
286 NID_sbgp_autonomousSysNum, /* 291 */
47bbaa5b 287#endif
0f113f3e
MC
288 NID_policy_constraints, /* 401 */
289 NID_proxyCertInfo, /* 663 */
290 NID_name_constraints, /* 666 */
291 NID_policy_mappings, /* 747 */
292 NID_inhibit_any_policy /* 748 */
293 };
294
295 int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
296
297 if (ex_nid == NID_undef)
298 return 0;
299
b6eb9827 300 if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
0f113f3e
MC
301 return 1;
302 return 0;
303}
3e727a3b 304
ba4356ae 305static int setup_dp(X509 *x, DIST_POINT *dp)
0f113f3e
MC
306{
307 X509_NAME *iname = NULL;
308 int i;
ba4356ae 309
0f113f3e
MC
310 if (dp->reasons) {
311 if (dp->reasons->length > 0)
312 dp->dp_reasons = dp->reasons->data[0];
313 if (dp->reasons->length > 1)
314 dp->dp_reasons |= (dp->reasons->data[1] << 8);
315 dp->dp_reasons &= CRLDP_ALL_REASONS;
316 } else
317 dp->dp_reasons = CRLDP_ALL_REASONS;
318 if (!dp->distpoint || (dp->distpoint->type != 1))
ba4356ae 319 return 1;
0f113f3e
MC
320 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
321 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
322 if (gen->type == GEN_DIRNAME) {
323 iname = gen->d.directoryName;
324 break;
325 }
326 }
327 if (!iname)
328 iname = X509_get_issuer_name(x);
329
ba4356ae 330 return DIST_POINT_set_dpname(dp->distpoint, iname);
0f113f3e 331}
3e727a3b 332
ba4356ae 333static int setup_crldp(X509 *x)
0f113f3e
MC
334{
335 int i;
ba4356ae
BE
336
337 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
338 if (x->crldp == NULL && i != -1)
339 return 0;
340 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
341 if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i)))
342 return 0;
343 }
344 return 1;
0f113f3e 345}
f1558bb4 346
f51e5ed6
DSH
347#define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
348#define ku_reject(x, usage) \
349 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
350#define xku_reject(x, usage) \
351 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
352#define ns_reject(x, usage) \
353 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
354
ce1b4fe1 355static void x509v3_cache_extensions(X509 *x)
673b102c 356{
0f113f3e
MC
357 BASIC_CONSTRAINTS *bs;
358 PROXY_CERT_INFO_EXTENSION *pci;
359 ASN1_BIT_STRING *usage;
360 ASN1_BIT_STRING *ns;
361 EXTENDED_KEY_USAGE *extusage;
362 X509_EXTENSION *ex;
0f113f3e 363 int i;
bc624bd9 364
7d38ca3f 365#ifdef tsan_ld_acq
f21b5b64 366 /* fast lock-free check, see end of the function for details. */
7d38ca3f 367 if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
f21b5b64 368 return;
7d38ca3f 369#endif
f21b5b64 370
bc624bd9
DMSP
371 CRYPTO_THREAD_write_lock(x->lock);
372 if (x->ex_flags & EXFLAG_SET) {
373 CRYPTO_THREAD_unlock(x->lock);
374 return;
375 }
376
ba4356ae
BE
377 if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
378 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
379 /* V1 should mean no extensions ... */
380 if (!X509_get_version(x))
381 x->ex_flags |= EXFLAG_V1;
382 /* Handle basic constraints */
ba4356ae 383 if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
0f113f3e
MC
384 if (bs->ca)
385 x->ex_flags |= EXFLAG_CA;
386 if (bs->pathlen) {
387 if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
388 || !bs->ca) {
389 x->ex_flags |= EXFLAG_INVALID;
390 x->ex_pathlen = 0;
391 } else
392 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
393 } else
394 x->ex_pathlen = -1;
395 BASIC_CONSTRAINTS_free(bs);
396 x->ex_flags |= EXFLAG_BCONS;
ba4356ae
BE
397 } else if (i != -1) {
398 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
399 }
400 /* Handle proxy certificates */
ba4356ae 401 if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
0f113f3e
MC
402 if (x->ex_flags & EXFLAG_CA
403 || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
404 || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
405 x->ex_flags |= EXFLAG_INVALID;
406 }
407 if (pci->pcPathLengthConstraint) {
408 x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
409 } else
410 x->ex_pcpathlen = -1;
411 PROXY_CERT_INFO_EXTENSION_free(pci);
412 x->ex_flags |= EXFLAG_PROXY;
ba4356ae
BE
413 } else if (i != -1) {
414 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
415 }
416 /* Handle key usage */
ba4356ae 417 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
0f113f3e
MC
418 if (usage->length > 0) {
419 x->ex_kusage = usage->data[0];
420 if (usage->length > 1)
421 x->ex_kusage |= usage->data[1] << 8;
422 } else
423 x->ex_kusage = 0;
424 x->ex_flags |= EXFLAG_KUSAGE;
425 ASN1_BIT_STRING_free(usage);
ba4356ae
BE
426 } else if (i != -1) {
427 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
428 }
429 x->ex_xkusage = 0;
ba4356ae 430 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
0f113f3e
MC
431 x->ex_flags |= EXFLAG_XKUSAGE;
432 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
433 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
434 case NID_server_auth:
435 x->ex_xkusage |= XKU_SSL_SERVER;
436 break;
437
438 case NID_client_auth:
439 x->ex_xkusage |= XKU_SSL_CLIENT;
440 break;
441
442 case NID_email_protect:
443 x->ex_xkusage |= XKU_SMIME;
444 break;
445
446 case NID_code_sign:
447 x->ex_xkusage |= XKU_CODE_SIGN;
448 break;
449
450 case NID_ms_sgc:
451 case NID_ns_sgc:
452 x->ex_xkusage |= XKU_SGC;
453 break;
454
455 case NID_OCSP_sign:
456 x->ex_xkusage |= XKU_OCSP_SIGN;
457 break;
458
459 case NID_time_stamp:
460 x->ex_xkusage |= XKU_TIMESTAMP;
461 break;
462
463 case NID_dvcs:
464 x->ex_xkusage |= XKU_DVCS;
465 break;
466
467 case NID_anyExtendedKeyUsage:
468 x->ex_xkusage |= XKU_ANYEKU;
469 break;
470 }
471 }
472 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
ba4356ae
BE
473 } else if (i != -1) {
474 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
475 }
476
ba4356ae 477 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
0f113f3e
MC
478 if (ns->length > 0)
479 x->ex_nscert = ns->data[0];
480 else
481 x->ex_nscert = 0;
482 x->ex_flags |= EXFLAG_NSCERT;
483 ASN1_BIT_STRING_free(ns);
ba4356ae
BE
484 } else if (i != -1) {
485 x->ex_flags |= EXFLAG_INVALID;
0f113f3e 486 }
ba4356ae
BE
487 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
488 if (x->skid == NULL && i != -1)
489 x->ex_flags |= EXFLAG_INVALID;
490 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
491 if (x->akid == NULL && i != -1)
492 x->ex_flags |= EXFLAG_INVALID;
0f113f3e
MC
493 /* Does subject name match issuer ? */
494 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
495 x->ex_flags |= EXFLAG_SI;
496 /* If SKID matches AKID also indicate self signed */
f51e5ed6
DSH
497 if (X509_check_akid(x, x->akid) == X509_V_OK &&
498 !ku_reject(x, KU_KEY_CERT_SIGN))
0f113f3e
MC
499 x->ex_flags |= EXFLAG_SS;
500 }
ba4356ae
BE
501 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
502 if (x->altname == NULL && i != -1)
503 x->ex_flags |= EXFLAG_INVALID;
0f113f3e 504 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
ba4356ae
BE
505 if (x->nc == NULL && i != -1)
506 x->ex_flags |= EXFLAG_INVALID;
507 if (!setup_crldp(x))
0f113f3e 508 x->ex_flags |= EXFLAG_INVALID;
d43c4497 509
47bbaa5b 510#ifndef OPENSSL_NO_RFC3779
ba4356ae
BE
511 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
512 if (x->rfc3779_addr == NULL && i != -1)
513 x->ex_flags |= EXFLAG_INVALID;
514 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
515 if (x->rfc3779_asid == NULL && i != -1)
516 x->ex_flags |= EXFLAG_INVALID;
47bbaa5b 517#endif
0f113f3e
MC
518 for (i = 0; i < X509_get_ext_count(x); i++) {
519 ex = X509_get_ext(x, i);
520 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
521 == NID_freshest_crl)
522 x->ex_flags |= EXFLAG_FRESHEST;
523 if (!X509_EXTENSION_get_critical(ex))
524 continue;
525 if (!X509_supported_extension(ex)) {
526 x->ex_flags |= EXFLAG_CRITICAL;
527 break;
528 }
529 }
786dd2c2 530 x509_init_sig_info(x);
0f113f3e 531 x->ex_flags |= EXFLAG_SET;
7d38ca3f
AP
532#ifdef tsan_st_rel
533 tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
f21b5b64 534 /*
7d38ca3f
AP
535 * Above store triggers fast lock-free check in the beginning of the
536 * function. But one has to ensure that the structure is "stable", i.e.
537 * all stores are visible on all processors. Hence the release fence.
f21b5b64 538 */
7d38ca3f
AP
539#endif
540 CRYPTO_THREAD_unlock(x->lock);
673b102c
DSH
541}
542
1d97c843
TH
543/*-
544 * CA checks common to all purposes
673b102c
DSH
545 * return codes:
546 * 0 not a CA
547 * 1 is a CA
548 * 2 basicConstraints absent so "maybe" a CA
549 * 3 basicConstraints absent but self signed V1.
bc501570 550 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
673b102c
DSH
551 */
552
5073ff03 553static int check_ca(const X509 *x)
673b102c 554{
0f113f3e
MC
555 /* keyUsage if present should allow cert signing */
556 if (ku_reject(x, KU_KEY_CERT_SIGN))
557 return 0;
558 if (x->ex_flags & EXFLAG_BCONS) {
559 if (x->ex_flags & EXFLAG_CA)
560 return 1;
561 /* If basicConstraints says not a CA then say so */
562 else
563 return 0;
564 } else {
565 /* we support V1 roots for... uh, I don't really know why. */
566 if ((x->ex_flags & V1_ROOT) == V1_ROOT)
567 return 3;
568 /*
569 * If key usage present it must have certSign so tolerate it
570 */
571 else if (x->ex_flags & EXFLAG_KUSAGE)
572 return 4;
573 /* Older certificates could have Netscape-specific CA types */
574 else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
575 return 5;
576 /* can this still be regarded a CA certificate? I doubt it */
577 return 0;
578 }
673b102c
DSH
579}
580
9961cb77
RL
581void X509_set_proxy_flag(X509 *x)
582{
583 x->ex_flags |= EXFLAG_PROXY;
584}
585
fe0169b0
RL
586void X509_set_proxy_pathlen(X509 *x, long l)
587{
588 x->ex_pcpathlen = l;
589}
590
5073ff03
RL
591int X509_check_ca(X509 *x)
592{
bc624bd9 593 x509v3_cache_extensions(x);
5073ff03 594
0f113f3e 595 return check_ca(x);
5073ff03
RL
596}
597
0cb957a6 598/* Check SSL CA: common checks for SSL client and server */
ccd86b68 599static int check_ssl_ca(const X509 *x)
0cb957a6 600{
0f113f3e
MC
601 int ca_ret;
602 ca_ret = check_ca(x);
603 if (!ca_ret)
604 return 0;
605 /* check nsCertType if present */
606 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
607 return ca_ret;
608 else
609 return 0;
0cb957a6 610}
8cff6331 611
0f113f3e
MC
612static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
613 int ca)
673b102c 614{
0f113f3e
MC
615 if (xku_reject(x, XKU_SSL_CLIENT))
616 return 0;
617 if (ca)
618 return check_ssl_ca(x);
619 /* We need to do digital signatures or key agreement */
620 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
621 return 0;
622 /* nsCertType if present should allow SSL client use */
623 if (ns_reject(x, NS_SSL_CLIENT))
624 return 0;
625 return 1;
673b102c 626}
0f113f3e
MC
627
628/*
629 * Key usage needed for TLS/SSL server: digital signature, encipherment or
7568d15a
DSH
630 * key agreement. The ssl code can check this more thoroughly for individual
631 * key types.
632 */
633#define KU_TLS \
0f113f3e 634 KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
673b102c 635
0f113f3e
MC
636static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
637 int ca)
673b102c 638{
0f113f3e
MC
639 if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
640 return 0;
641 if (ca)
642 return check_ssl_ca(x);
643
644 if (ns_reject(x, NS_SSL_SERVER))
645 return 0;
646 if (ku_reject(x, KU_TLS))
647 return 0;
673b102c 648
0f113f3e 649 return 1;
673b102c
DSH
650
651}
652
0f113f3e
MC
653static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
654 int ca)
673b102c 655{
0f113f3e
MC
656 int ret;
657 ret = check_purpose_ssl_server(xp, x, ca);
658 if (!ret || ca)
659 return ret;
660 /* We need to encipher or Netscape complains */
661 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
662 return 0;
663 return ret;
673b102c
DSH
664}
665
666/* common S/MIME checks */
ccd86b68 667static int purpose_smime(const X509 *x, int ca)
673b102c 668{
0f113f3e
MC
669 if (xku_reject(x, XKU_SMIME))
670 return 0;
671 if (ca) {
672 int ca_ret;
673 ca_ret = check_ca(x);
674 if (!ca_ret)
675 return 0;
676 /* check nsCertType if present */
677 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
678 return ca_ret;
679 else
680 return 0;
681 }
682 if (x->ex_flags & EXFLAG_NSCERT) {
683 if (x->ex_nscert & NS_SMIME)
684 return 1;
685 /* Workaround for some buggy certificates */
686 if (x->ex_nscert & NS_SSL_CLIENT)
687 return 2;
688 return 0;
689 }
690 return 1;
673b102c
DSH
691}
692
0f113f3e
MC
693static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
694 int ca)
673b102c 695{
0f113f3e
MC
696 int ret;
697 ret = purpose_smime(x, ca);
698 if (!ret || ca)
699 return ret;
700 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
701 return 0;
702 return ret;
673b102c
DSH
703}
704
0f113f3e
MC
705static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
706 int ca)
673b102c 707{
0f113f3e
MC
708 int ret;
709 ret = purpose_smime(x, ca);
710 if (!ret || ca)
711 return ret;
712 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
713 return 0;
714 return ret;
673b102c
DSH
715}
716
0f113f3e
MC
717static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
718 int ca)
673b102c 719{
0f113f3e
MC
720 if (ca) {
721 int ca_ret;
722 if ((ca_ret = check_ca(x)) != 2)
723 return ca_ret;
724 else
725 return 0;
726 }
727 if (ku_reject(x, KU_CRL_SIGN))
728 return 0;
729 return 1;
673b102c 730}
068fdce8 731
0f113f3e
MC
732/*
733 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
734 * is valid. Additional checks must be made on the chain.
81f169e9
DSH
735 */
736
737static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
738{
0f113f3e
MC
739 /*
740 * Must be a valid CA. Should we really support the "I don't know" value
741 * (2)?
742 */
743 if (ca)
744 return check_ca(x);
745 /* leaf certificate is checked in OCSP_verify() */
746 return 1;
81f169e9
DSH
747}
748
c7235be6 749static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
0f113f3e 750 int ca)
c7235be6 751{
0f113f3e
MC
752 int i_ext;
753
754 /* If ca is true we must return if this is a valid CA certificate. */
755 if (ca)
756 return check_ca(x);
757
758 /*
759 * Check the optional key usage field:
760 * if Key Usage is present, it must be one of digitalSignature
761 * and/or nonRepudiation (other values are not consistent and shall
762 * be rejected).
763 */
764 if ((x->ex_flags & EXFLAG_KUSAGE)
765 && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
766 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
767 return 0;
768
769 /* Only time stamp key usage is permitted and it's required. */
770 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
771 return 0;
772
773 /* Extended Key Usage MUST be critical */
7569362e 774 i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
0f113f3e
MC
775 if (i_ext >= 0) {
776 X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
777 if (!X509_EXTENSION_get_critical(ext))
778 return 0;
779 }
780
781 return 1;
c7235be6
UM
782}
783
ccd86b68 784static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
068fdce8 785{
0f113f3e 786 return 1;
068fdce8 787}
2f043896 788
1d97c843
TH
789/*-
790 * Various checks to see if one certificate issued the second.
2f043896
DSH
791 * This can be used to prune a set of possible issuer certificates
792 * which have been looked up using some simple method such as by
793 * subject name.
794 * These are:
795 * 1. Check issuer_name(subject) == subject_name(issuer)
796 * 2. If akid(subject) exists check it matches issuer
797 * 3. If key_usage(issuer) exists check it supports certificate signing
798 * returns 0 for OK, positive for reason for mismatch, reasons match
799 * codes for X509_verify_cert()
800 */
801
802int X509_check_issued(X509 *issuer, X509 *subject)
803{
0f113f3e
MC
804 if (X509_NAME_cmp(X509_get_subject_name(issuer),
805 X509_get_issuer_name(subject)))
806 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
bc624bd9 807
0f113f3e 808 x509v3_cache_extensions(issuer);
ba4356ae
BE
809 if (issuer->ex_flags & EXFLAG_INVALID)
810 return X509_V_ERR_UNSPECIFIED;
0f113f3e 811 x509v3_cache_extensions(subject);
ba4356ae
BE
812 if (subject->ex_flags & EXFLAG_INVALID)
813 return X509_V_ERR_UNSPECIFIED;
0f113f3e
MC
814
815 if (subject->akid) {
816 int ret = X509_check_akid(issuer, subject->akid);
817 if (ret != X509_V_OK)
818 return ret;
819 }
820
821 if (subject->ex_flags & EXFLAG_PROXY) {
822 if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
823 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
824 } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
825 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
826 return X509_V_OK;
2f043896
DSH
827}
828
bc7535bc 829int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
0f113f3e 830{
bc7535bc 831
0f113f3e
MC
832 if (!akid)
833 return X509_V_OK;
834
835 /* Check key ids (if present) */
836 if (akid->keyid && issuer->skid &&
837 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
838 return X509_V_ERR_AKID_SKID_MISMATCH;
839 /* Check serial number */
840 if (akid->serial &&
841 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
842 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
843 /* Check issuer name */
844 if (akid->issuer) {
845 /*
846 * Ugh, for some peculiar reason AKID includes SEQUENCE OF
847 * GeneralName. So look for a DirName. There may be more than one but
848 * we only take any notice of the first.
849 */
850 GENERAL_NAMES *gens;
851 GENERAL_NAME *gen;
852 X509_NAME *nm = NULL;
853 int i;
854 gens = akid->issuer;
855 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
856 gen = sk_GENERAL_NAME_value(gens, i);
857 if (gen->type == GEN_DIRNAME) {
858 nm = gen->d.dirn;
859 break;
860 }
861 }
862 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
863 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
864 }
865 return X509_V_OK;
866}
063f1f0c
DSH
867
868uint32_t X509_get_extension_flags(X509 *x)
869{
109f8b5d 870 /* Call for side-effect of computing hash and caching extensions */
063f1f0c
DSH
871 X509_check_purpose(x, -1, -1);
872 return x->ex_flags;
873}
874
875uint32_t X509_get_key_usage(X509 *x)
876{
109f8b5d 877 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
878 if (X509_check_purpose(x, -1, -1) != 1)
879 return 0;
063f1f0c
DSH
880 if (x->ex_flags & EXFLAG_KUSAGE)
881 return x->ex_kusage;
882 return UINT32_MAX;
883}
884
885uint32_t X509_get_extended_key_usage(X509 *x)
886{
109f8b5d 887 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
888 if (X509_check_purpose(x, -1, -1) != 1)
889 return 0;
063f1f0c
DSH
890 if (x->ex_flags & EXFLAG_XKUSAGE)
891 return x->ex_xkusage;
892 return UINT32_MAX;
893}
d19a50c9
DSH
894
895const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
896{
109f8b5d 897 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
898 if (X509_check_purpose(x, -1, -1) != 1)
899 return NULL;
d19a50c9
DSH
900 return x->skid;
901}
e417070c 902
b383aa20
MP
903const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
904{
905 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
906 if (X509_check_purpose(x, -1, -1) != 1)
907 return NULL;
b383aa20
MP
908 return (x->akid != NULL ? x->akid->keyid : NULL);
909}
910
74b9ce2d
DMSP
911const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
912{
913 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
914 if (X509_check_purpose(x, -1, -1) != 1)
915 return NULL;
74b9ce2d
DMSP
916 return (x->akid != NULL ? x->akid->issuer : NULL);
917}
918
919const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
920{
921 /* Call for side-effect of computing hash and caching extensions */
ba4356ae
BE
922 if (X509_check_purpose(x, -1, -1) != 1)
923 return NULL;
74b9ce2d
DMSP
924 return (x->akid != NULL ? x->akid->serial : NULL);
925}
926
e417070c
RS
927long X509_get_pathlen(X509 *x)
928{
929 /* Called for side effect of caching extensions */
930 if (X509_check_purpose(x, -1, -1) != 1
931 || (x->ex_flags & EXFLAG_BCONS) == 0)
932 return -1;
933 return x->ex_pathlen;
934}
fe0169b0
RL
935
936long X509_get_proxy_pathlen(X509 *x)
937{
938 /* Called for side effect of caching extensions */
939 if (X509_check_purpose(x, -1, -1) != 1
940 || (x->ex_flags & EXFLAG_PROXY) == 0)
941 return -1;
942 return x->ex_pcpathlen;
943}