v3_purp.c 35 KB

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