v3_purp.c 33 KB

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