x509_vfy.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /* crypto/x509/x509_vfy.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <time.h>
  60. #include <errno.h>
  61. #include "cryptlib.h"
  62. #include <openssl/crypto.h>
  63. #include <openssl/lhash.h>
  64. #include <openssl/buffer.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/asn1.h>
  67. #include <openssl/x509.h>
  68. #include <openssl/x509v3.h>
  69. #include <openssl/objects.h>
  70. static int null_callback(int ok,X509_STORE_CTX *e);
  71. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  72. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  73. static int check_chain_extensions(X509_STORE_CTX *ctx);
  74. static int check_trust(X509_STORE_CTX *ctx);
  75. static int check_revocation(X509_STORE_CTX *ctx);
  76. static int check_cert(X509_STORE_CTX *ctx);
  77. static int check_policy(X509_STORE_CTX *ctx);
  78. static int internal_verify(X509_STORE_CTX *ctx);
  79. const char *X509_version="X.509" OPENSSL_VERSION_PTEXT;
  80. static int null_callback(int ok, X509_STORE_CTX *e)
  81. {
  82. return ok;
  83. }
  84. #if 0
  85. static int x509_subject_cmp(X509 **a, X509 **b)
  86. {
  87. return X509_subject_name_cmp(*a,*b);
  88. }
  89. #endif
  90. int X509_verify_cert(X509_STORE_CTX *ctx)
  91. {
  92. X509 *x,*xtmp,*chain_ss=NULL;
  93. X509_NAME *xn;
  94. int bad_chain = 0;
  95. X509_VERIFY_PARAM *param = ctx->param;
  96. int depth,i,ok=0;
  97. int num;
  98. int (*cb)(int ok,X509_STORE_CTX *ctx);
  99. STACK_OF(X509) *sktmp=NULL;
  100. if (ctx->cert == NULL)
  101. {
  102. X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  103. return -1;
  104. }
  105. cb=ctx->verify_cb;
  106. /* first we make sure the chain we are going to build is
  107. * present and that the first entry is in place */
  108. if (ctx->chain == NULL)
  109. {
  110. if ( ((ctx->chain=sk_X509_new_null()) == NULL) ||
  111. (!sk_X509_push(ctx->chain,ctx->cert)))
  112. {
  113. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  114. goto end;
  115. }
  116. CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
  117. ctx->last_untrusted=1;
  118. }
  119. /* We use a temporary STACK so we can chop and hack at it */
  120. if (ctx->untrusted != NULL
  121. && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
  122. {
  123. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  124. goto end;
  125. }
  126. num=sk_X509_num(ctx->chain);
  127. x=sk_X509_value(ctx->chain,num-1);
  128. depth=param->depth;
  129. for (;;)
  130. {
  131. /* If we have enough, we break */
  132. if (depth < num) break; /* FIXME: If this happens, we should take
  133. * note of it and, if appropriate, use the
  134. * X509_V_ERR_CERT_CHAIN_TOO_LONG error
  135. * code later.
  136. */
  137. /* If we are self signed, we break */
  138. xn=X509_get_issuer_name(x);
  139. if (ctx->check_issued(ctx, x,x)) break;
  140. /* If we were passed a cert chain, use it first */
  141. if (ctx->untrusted != NULL)
  142. {
  143. xtmp=find_issuer(ctx, sktmp,x);
  144. if (xtmp != NULL)
  145. {
  146. if (!sk_X509_push(ctx->chain,xtmp))
  147. {
  148. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  149. goto end;
  150. }
  151. CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
  152. sk_X509_delete_ptr(sktmp,xtmp);
  153. ctx->last_untrusted++;
  154. x=xtmp;
  155. num++;
  156. /* reparse the full chain for
  157. * the next one */
  158. continue;
  159. }
  160. }
  161. break;
  162. }
  163. /* at this point, chain should contain a list of untrusted
  164. * certificates. We now need to add at least one trusted one,
  165. * if possible, otherwise we complain. */
  166. /* Examine last certificate in chain and see if it
  167. * is self signed.
  168. */
  169. i=sk_X509_num(ctx->chain);
  170. x=sk_X509_value(ctx->chain,i-1);
  171. xn = X509_get_subject_name(x);
  172. if (ctx->check_issued(ctx, x, x))
  173. {
  174. /* we have a self signed certificate */
  175. if (sk_X509_num(ctx->chain) == 1)
  176. {
  177. /* We have a single self signed certificate: see if
  178. * we can find it in the store. We must have an exact
  179. * match to avoid possible impersonation.
  180. */
  181. ok = ctx->get_issuer(&xtmp, ctx, x);
  182. if ((ok <= 0) || X509_cmp(x, xtmp))
  183. {
  184. ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  185. ctx->current_cert=x;
  186. ctx->error_depth=i-1;
  187. if (ok == 1) X509_free(xtmp);
  188. bad_chain = 1;
  189. ok=cb(0,ctx);
  190. if (!ok) goto end;
  191. }
  192. else
  193. {
  194. /* We have a match: replace certificate with store version
  195. * so we get any trust settings.
  196. */
  197. X509_free(x);
  198. x = xtmp;
  199. sk_X509_set(ctx->chain, i - 1, x);
  200. ctx->last_untrusted=0;
  201. }
  202. }
  203. else
  204. {
  205. /* extract and save self signed certificate for later use */
  206. chain_ss=sk_X509_pop(ctx->chain);
  207. ctx->last_untrusted--;
  208. num--;
  209. x=sk_X509_value(ctx->chain,num-1);
  210. }
  211. }
  212. /* We now lookup certs from the certificate store */
  213. for (;;)
  214. {
  215. /* If we have enough, we break */
  216. if (depth < num) break;
  217. /* If we are self signed, we break */
  218. xn=X509_get_issuer_name(x);
  219. if (ctx->check_issued(ctx,x,x)) break;
  220. ok = ctx->get_issuer(&xtmp, ctx, x);
  221. if (ok < 0) return ok;
  222. if (ok == 0) break;
  223. x = xtmp;
  224. if (!sk_X509_push(ctx->chain,x))
  225. {
  226. X509_free(xtmp);
  227. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  228. return 0;
  229. }
  230. num++;
  231. }
  232. /* we now have our chain, lets check it... */
  233. xn=X509_get_issuer_name(x);
  234. /* Is last certificate looked up self signed? */
  235. if (!ctx->check_issued(ctx,x,x))
  236. {
  237. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
  238. {
  239. if (ctx->last_untrusted >= num)
  240. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  241. else
  242. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  243. ctx->current_cert=x;
  244. }
  245. else
  246. {
  247. sk_X509_push(ctx->chain,chain_ss);
  248. num++;
  249. ctx->last_untrusted=num;
  250. ctx->current_cert=chain_ss;
  251. ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  252. chain_ss=NULL;
  253. }
  254. ctx->error_depth=num-1;
  255. bad_chain = 1;
  256. ok=cb(0,ctx);
  257. if (!ok) goto end;
  258. }
  259. /* We have the chain complete: now we need to check its purpose */
  260. ok = check_chain_extensions(ctx);
  261. if (!ok) goto end;
  262. /* The chain extensions are OK: check trust */
  263. if (param->trust > 0) ok = check_trust(ctx);
  264. if (!ok) goto end;
  265. /* We may as well copy down any DSA parameters that are required */
  266. X509_get_pubkey_parameters(NULL,ctx->chain);
  267. /* Check revocation status: we do this after copying parameters
  268. * because they may be needed for CRL signature verification.
  269. */
  270. ok = ctx->check_revocation(ctx);
  271. if(!ok) goto end;
  272. /* At this point, we have a chain and need to verify it */
  273. if (ctx->verify != NULL)
  274. ok=ctx->verify(ctx);
  275. else
  276. ok=internal_verify(ctx);
  277. if(!ok) goto end;
  278. /* If we get this far evaluate policies */
  279. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  280. ok = ctx->check_policy(ctx);
  281. if(!ok) goto end;
  282. if (0)
  283. {
  284. end:
  285. X509_get_pubkey_parameters(NULL,ctx->chain);
  286. }
  287. if (sktmp != NULL) sk_X509_free(sktmp);
  288. if (chain_ss != NULL) X509_free(chain_ss);
  289. return ok;
  290. }
  291. /* Given a STACK_OF(X509) find the issuer of cert (if any)
  292. */
  293. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  294. {
  295. int i;
  296. X509 *issuer;
  297. for (i = 0; i < sk_X509_num(sk); i++)
  298. {
  299. issuer = sk_X509_value(sk, i);
  300. if (ctx->check_issued(ctx, x, issuer))
  301. return issuer;
  302. }
  303. return NULL;
  304. }
  305. /* Given a possible certificate and issuer check them */
  306. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  307. {
  308. int ret;
  309. ret = X509_check_issued(issuer, x);
  310. if (ret == X509_V_OK)
  311. return 1;
  312. /* If we haven't asked for issuer errors don't set ctx */
  313. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  314. return 0;
  315. ctx->error = ret;
  316. ctx->current_cert = x;
  317. ctx->current_issuer = issuer;
  318. return ctx->verify_cb(0, ctx);
  319. return 0;
  320. }
  321. /* Alternative lookup method: look from a STACK stored in other_ctx */
  322. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  323. {
  324. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  325. if (*issuer)
  326. {
  327. CRYPTO_add(&(*issuer)->references,1,CRYPTO_LOCK_X509);
  328. return 1;
  329. }
  330. else
  331. return 0;
  332. }
  333. /* Check a certificate chains extensions for consistency
  334. * with the supplied purpose
  335. */
  336. static int check_chain_extensions(X509_STORE_CTX *ctx)
  337. {
  338. #ifdef OPENSSL_NO_CHAIN_VERIFY
  339. return 1;
  340. #else
  341. int i, ok=0, must_be_ca;
  342. X509 *x;
  343. int (*cb)(int ok,X509_STORE_CTX *ctx);
  344. int proxy_path_length = 0;
  345. cb=ctx->verify_cb;
  346. /* must_be_ca can have 1 of 3 values:
  347. -1: we accept both CA and non-CA certificates, to allow direct
  348. use of self-signed certificates (which are marked as CA).
  349. 0: we only accept non-CA certificates. This is currently not
  350. used, but the possibility is present for future extensions.
  351. 1: we only accept CA certificates. This is currently used for
  352. all certificates in the chain except the leaf certificate.
  353. */
  354. must_be_ca = -1;
  355. /* Check all untrusted certificates */
  356. for (i = 0; i < ctx->last_untrusted; i++)
  357. {
  358. int ret;
  359. x = sk_X509_value(ctx->chain, i);
  360. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  361. && (x->ex_flags & EXFLAG_CRITICAL))
  362. {
  363. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  364. ctx->error_depth = i;
  365. ctx->current_cert = x;
  366. ok=cb(0,ctx);
  367. if (!ok) goto end;
  368. }
  369. ret = X509_check_ca(x);
  370. switch(must_be_ca)
  371. {
  372. case -1:
  373. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  374. && (ret != 1) && (ret != 0))
  375. {
  376. ret = 0;
  377. ctx->error = X509_V_ERR_INVALID_CA;
  378. }
  379. else
  380. ret = 1;
  381. break;
  382. case 0:
  383. if (ret != 0)
  384. {
  385. ret = 0;
  386. ctx->error = X509_V_ERR_INVALID_NON_CA;
  387. }
  388. else
  389. ret = 1;
  390. break;
  391. default:
  392. if ((ret == 0)
  393. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  394. && (ret != 1)))
  395. {
  396. ret = 0;
  397. ctx->error = X509_V_ERR_INVALID_CA;
  398. }
  399. else
  400. ret = 1;
  401. break;
  402. }
  403. if (ret == 0)
  404. {
  405. ctx->error_depth = i;
  406. ctx->current_cert = x;
  407. ok=cb(0,ctx);
  408. if (!ok) goto end;
  409. }
  410. if (ctx->param->purpose > 0)
  411. {
  412. ret = X509_check_purpose(x, ctx->param->purpose,
  413. must_be_ca > 0);
  414. if ((ret == 0)
  415. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  416. && (ret != 1)))
  417. {
  418. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  419. ctx->error_depth = i;
  420. ctx->current_cert = x;
  421. ok=cb(0,ctx);
  422. if (!ok) goto end;
  423. }
  424. }
  425. /* Check pathlen */
  426. if ((i > 1) && (x->ex_pathlen != -1)
  427. && (i > (x->ex_pathlen + proxy_path_length + 1)))
  428. {
  429. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  430. ctx->error_depth = i;
  431. ctx->current_cert = x;
  432. ok=cb(0,ctx);
  433. if (!ok) goto end;
  434. }
  435. /* If this certificate is a proxy certificate, the next
  436. certificate must be another proxy certificate or a EE
  437. certificate. If not, the next certificate must be a
  438. CA certificate. */
  439. if (x->ex_flags & EXFLAG_PROXY)
  440. {
  441. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)
  442. {
  443. ctx->error =
  444. X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  445. ctx->error_depth = i;
  446. ctx->current_cert = x;
  447. ok=cb(0,ctx);
  448. if (!ok) goto end;
  449. }
  450. proxy_path_length++;
  451. must_be_ca = 0;
  452. }
  453. else
  454. must_be_ca = 1;
  455. }
  456. ok = 1;
  457. end:
  458. return ok;
  459. #endif
  460. }
  461. static int check_trust(X509_STORE_CTX *ctx)
  462. {
  463. #ifdef OPENSSL_NO_CHAIN_VERIFY
  464. return 1;
  465. #else
  466. int i, ok;
  467. X509 *x;
  468. int (*cb)(int ok,X509_STORE_CTX *ctx);
  469. cb=ctx->verify_cb;
  470. /* For now just check the last certificate in the chain */
  471. i = sk_X509_num(ctx->chain) - 1;
  472. x = sk_X509_value(ctx->chain, i);
  473. ok = X509_check_trust(x, ctx->param->trust, 0);
  474. if (ok == X509_TRUST_TRUSTED)
  475. return 1;
  476. ctx->error_depth = i;
  477. ctx->current_cert = x;
  478. if (ok == X509_TRUST_REJECTED)
  479. ctx->error = X509_V_ERR_CERT_REJECTED;
  480. else
  481. ctx->error = X509_V_ERR_CERT_UNTRUSTED;
  482. ok = cb(0, ctx);
  483. return ok;
  484. #endif
  485. }
  486. static int check_revocation(X509_STORE_CTX *ctx)
  487. {
  488. int i, last, ok;
  489. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  490. return 1;
  491. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  492. last = sk_X509_num(ctx->chain) - 1;
  493. else
  494. last = 0;
  495. for(i = 0; i <= last; i++)
  496. {
  497. ctx->error_depth = i;
  498. ok = check_cert(ctx);
  499. if (!ok) return ok;
  500. }
  501. return 1;
  502. }
  503. static int check_cert(X509_STORE_CTX *ctx)
  504. {
  505. X509_CRL *crl = NULL;
  506. X509 *x;
  507. int ok, cnum;
  508. cnum = ctx->error_depth;
  509. x = sk_X509_value(ctx->chain, cnum);
  510. ctx->current_cert = x;
  511. /* Try to retrieve relevant CRL */
  512. ok = ctx->get_crl(ctx, &crl, x);
  513. /* If error looking up CRL, nothing we can do except
  514. * notify callback
  515. */
  516. if(!ok)
  517. {
  518. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  519. ok = ctx->verify_cb(0, ctx);
  520. goto err;
  521. }
  522. ctx->current_crl = crl;
  523. ok = ctx->check_crl(ctx, crl);
  524. if (!ok) goto err;
  525. ok = ctx->cert_crl(ctx, crl, x);
  526. err:
  527. ctx->current_crl = NULL;
  528. X509_CRL_free(crl);
  529. return ok;
  530. }
  531. /* Check CRL times against values in X509_STORE_CTX */
  532. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  533. {
  534. time_t *ptime;
  535. int i;
  536. ctx->current_crl = crl;
  537. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  538. ptime = &ctx->param->check_time;
  539. else
  540. ptime = NULL;
  541. i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  542. if (i == 0)
  543. {
  544. ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  545. if (!notify || !ctx->verify_cb(0, ctx))
  546. return 0;
  547. }
  548. if (i > 0)
  549. {
  550. ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
  551. if (!notify || !ctx->verify_cb(0, ctx))
  552. return 0;
  553. }
  554. if(X509_CRL_get_nextUpdate(crl))
  555. {
  556. i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  557. if (i == 0)
  558. {
  559. ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  560. if (!notify || !ctx->verify_cb(0, ctx))
  561. return 0;
  562. }
  563. if (i < 0)
  564. {
  565. ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
  566. if (!notify || !ctx->verify_cb(0, ctx))
  567. return 0;
  568. }
  569. }
  570. ctx->current_crl = NULL;
  571. return 1;
  572. }
  573. /* Lookup CRLs from the supplied list. Look for matching isser name
  574. * and validity. If we can't find a valid CRL return the last one
  575. * with matching name. This gives more meaningful error codes. Otherwise
  576. * we'd get a CRL not found error if a CRL existed with matching name but
  577. * was invalid.
  578. */
  579. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl,
  580. X509_NAME *nm, STACK_OF(X509_CRL) *crls)
  581. {
  582. int i;
  583. X509_CRL *crl, *best_crl = NULL;
  584. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  585. {
  586. crl = sk_X509_CRL_value(crls, i);
  587. if (X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  588. continue;
  589. if (check_crl_time(ctx, crl, 0))
  590. {
  591. *pcrl = crl;
  592. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509);
  593. return 1;
  594. }
  595. best_crl = crl;
  596. }
  597. if (best_crl)
  598. {
  599. *pcrl = best_crl;
  600. CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509);
  601. }
  602. return 0;
  603. }
  604. /* Retrieve CRL corresponding to certificate: currently just a
  605. * subject lookup: maybe use AKID later...
  606. */
  607. static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x)
  608. {
  609. int ok;
  610. X509_CRL *crl = NULL;
  611. X509_OBJECT xobj;
  612. X509_NAME *nm;
  613. nm = X509_get_issuer_name(x);
  614. ok = get_crl_sk(ctx, &crl, nm, ctx->crls);
  615. if (ok)
  616. {
  617. *pcrl = crl;
  618. return 1;
  619. }
  620. ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj);
  621. if (!ok)
  622. {
  623. /* If we got a near match from get_crl_sk use that */
  624. if (crl)
  625. {
  626. *pcrl = crl;
  627. return 1;
  628. }
  629. return 0;
  630. }
  631. *pcrl = xobj.data.crl;
  632. if (crl)
  633. X509_CRL_free(crl);
  634. return 1;
  635. }
  636. /* Check CRL validity */
  637. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  638. {
  639. X509 *issuer = NULL;
  640. EVP_PKEY *ikey = NULL;
  641. int ok = 0, chnum, cnum;
  642. cnum = ctx->error_depth;
  643. chnum = sk_X509_num(ctx->chain) - 1;
  644. /* Find CRL issuer: if not last certificate then issuer
  645. * is next certificate in chain.
  646. */
  647. if(cnum < chnum)
  648. issuer = sk_X509_value(ctx->chain, cnum + 1);
  649. else
  650. {
  651. issuer = sk_X509_value(ctx->chain, chnum);
  652. /* If not self signed, can't check signature */
  653. if(!ctx->check_issued(ctx, issuer, issuer))
  654. {
  655. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  656. ok = ctx->verify_cb(0, ctx);
  657. if(!ok) goto err;
  658. }
  659. }
  660. if(issuer)
  661. {
  662. /* Check for cRLSign bit if keyUsage present */
  663. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  664. !(issuer->ex_kusage & KU_CRL_SIGN))
  665. {
  666. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  667. ok = ctx->verify_cb(0, ctx);
  668. if(!ok) goto err;
  669. }
  670. /* Attempt to get issuer certificate public key */
  671. ikey = X509_get_pubkey(issuer);
  672. if(!ikey)
  673. {
  674. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  675. ok = ctx->verify_cb(0, ctx);
  676. if (!ok) goto err;
  677. }
  678. else
  679. {
  680. /* Verify CRL signature */
  681. if(X509_CRL_verify(crl, ikey) <= 0)
  682. {
  683. ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
  684. ok = ctx->verify_cb(0, ctx);
  685. if (!ok) goto err;
  686. }
  687. }
  688. }
  689. if (!check_crl_time(ctx, crl, 1))
  690. goto err;
  691. ok = 1;
  692. err:
  693. EVP_PKEY_free(ikey);
  694. return ok;
  695. }
  696. /* Check certificate against CRL */
  697. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  698. {
  699. int idx, ok;
  700. X509_REVOKED rtmp;
  701. STACK_OF(X509_EXTENSION) *exts;
  702. X509_EXTENSION *ext;
  703. /* Look for serial number of certificate in CRL */
  704. rtmp.serialNumber = X509_get_serialNumber(x);
  705. /* Sort revoked into serial number order if not already sorted.
  706. * Do this under a lock to avoid race condition.
  707. */
  708. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
  709. {
  710. CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
  711. sk_X509_REVOKED_sort(crl->crl->revoked);
  712. CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
  713. }
  714. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  715. /* If found assume revoked: want something cleverer than
  716. * this to handle entry extensions in V2 CRLs.
  717. */
  718. if(idx >= 0)
  719. {
  720. ctx->error = X509_V_ERR_CERT_REVOKED;
  721. ok = ctx->verify_cb(0, ctx);
  722. if (!ok) return 0;
  723. }
  724. if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  725. return 1;
  726. /* See if we have any critical CRL extensions: since we
  727. * currently don't handle any CRL extensions the CRL must be
  728. * rejected.
  729. * This code accesses the X509_CRL structure directly: applications
  730. * shouldn't do this.
  731. */
  732. exts = crl->crl->extensions;
  733. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
  734. {
  735. ext = sk_X509_EXTENSION_value(exts, idx);
  736. if (ext->critical > 0)
  737. {
  738. ctx->error =
  739. X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  740. ok = ctx->verify_cb(0, ctx);
  741. if(!ok) return 0;
  742. break;
  743. }
  744. }
  745. return 1;
  746. }
  747. static int check_policy(X509_STORE_CTX *ctx)
  748. {
  749. int ret;
  750. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  751. ctx->param->policies, ctx->param->flags);
  752. if (ret == 0)
  753. {
  754. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  755. return 0;
  756. }
  757. /* Invalid or inconsistent extensions */
  758. if (ret == -1)
  759. {
  760. /* Locate certificates with bad extensions and notify
  761. * callback.
  762. */
  763. X509 *x;
  764. int i;
  765. for (i = 1; i < sk_X509_num(ctx->chain); i++)
  766. {
  767. x = sk_X509_value(ctx->chain, i);
  768. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  769. continue;
  770. ctx->current_cert = x;
  771. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  772. ret = ctx->verify_cb(0, ctx);
  773. }
  774. return 1;
  775. }
  776. if (ret == -2)
  777. {
  778. ctx->current_cert = NULL;
  779. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  780. return ctx->verify_cb(0, ctx);
  781. }
  782. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY)
  783. {
  784. ctx->current_cert = NULL;
  785. ctx->error = X509_V_OK;
  786. if (!ctx->verify_cb(2, ctx))
  787. return 0;
  788. }
  789. return 1;
  790. }
  791. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  792. {
  793. time_t *ptime;
  794. int i;
  795. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  796. ptime = &ctx->param->check_time;
  797. else
  798. ptime = NULL;
  799. i=X509_cmp_time(X509_get_notBefore(x), ptime);
  800. if (i == 0)
  801. {
  802. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  803. ctx->current_cert=x;
  804. if (!ctx->verify_cb(0, ctx))
  805. return 0;
  806. }
  807. if (i > 0)
  808. {
  809. ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
  810. ctx->current_cert=x;
  811. if (!ctx->verify_cb(0, ctx))
  812. return 0;
  813. }
  814. i=X509_cmp_time(X509_get_notAfter(x), ptime);
  815. if (i == 0)
  816. {
  817. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  818. ctx->current_cert=x;
  819. if (!ctx->verify_cb(0, ctx))
  820. return 0;
  821. }
  822. if (i < 0)
  823. {
  824. ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
  825. ctx->current_cert=x;
  826. if (!ctx->verify_cb(0, ctx))
  827. return 0;
  828. }
  829. return 1;
  830. }
  831. static int internal_verify(X509_STORE_CTX *ctx)
  832. {
  833. int ok=0,n;
  834. X509 *xs,*xi;
  835. EVP_PKEY *pkey=NULL;
  836. int (*cb)(int ok,X509_STORE_CTX *ctx);
  837. cb=ctx->verify_cb;
  838. n=sk_X509_num(ctx->chain);
  839. ctx->error_depth=n-1;
  840. n--;
  841. xi=sk_X509_value(ctx->chain,n);
  842. if (ctx->check_issued(ctx, xi, xi))
  843. xs=xi;
  844. else
  845. {
  846. if (n <= 0)
  847. {
  848. ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  849. ctx->current_cert=xi;
  850. ok=cb(0,ctx);
  851. goto end;
  852. }
  853. else
  854. {
  855. n--;
  856. ctx->error_depth=n;
  857. xs=sk_X509_value(ctx->chain,n);
  858. }
  859. }
  860. /* ctx->error=0; not needed */
  861. while (n >= 0)
  862. {
  863. ctx->error_depth=n;
  864. if (!xs->valid)
  865. {
  866. if ((pkey=X509_get_pubkey(xi)) == NULL)
  867. {
  868. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  869. ctx->current_cert=xi;
  870. ok=(*cb)(0,ctx);
  871. if (!ok) goto end;
  872. }
  873. else if (X509_verify(xs,pkey) <= 0)
  874. /* XXX For the final trusted self-signed cert,
  875. * this is a waste of time. That check should
  876. * optional so that e.g. 'openssl x509' can be
  877. * used to detect invalid self-signatures, but
  878. * we don't verify again and again in SSL
  879. * handshakes and the like once the cert has
  880. * been declared trusted. */
  881. {
  882. ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
  883. ctx->current_cert=xs;
  884. ok=(*cb)(0,ctx);
  885. if (!ok)
  886. {
  887. EVP_PKEY_free(pkey);
  888. goto end;
  889. }
  890. }
  891. EVP_PKEY_free(pkey);
  892. pkey=NULL;
  893. }
  894. xs->valid = 1;
  895. if (!check_cert_time(ctx, xs))
  896. goto end;
  897. /* The last error (if any) is still in the error value */
  898. ctx->current_issuer=xi;
  899. ctx->current_cert=xs;
  900. ok=(*cb)(1,ctx);
  901. if (!ok) goto end;
  902. n--;
  903. if (n >= 0)
  904. {
  905. xi=xs;
  906. xs=sk_X509_value(ctx->chain,n);
  907. }
  908. }
  909. ok=1;
  910. end:
  911. return ok;
  912. }
  913. int X509_cmp_current_time(ASN1_TIME *ctm)
  914. {
  915. return X509_cmp_time(ctm, NULL);
  916. }
  917. int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
  918. {
  919. char *str;
  920. ASN1_TIME atm;
  921. long offset;
  922. char buff1[24],buff2[24],*p;
  923. int i,j;
  924. p=buff1;
  925. i=ctm->length;
  926. str=(char *)ctm->data;
  927. if (ctm->type == V_ASN1_UTCTIME)
  928. {
  929. if ((i < 11) || (i > 17)) return 0;
  930. memcpy(p,str,10);
  931. p+=10;
  932. str+=10;
  933. }
  934. else
  935. {
  936. if (i < 13) return 0;
  937. memcpy(p,str,12);
  938. p+=12;
  939. str+=12;
  940. }
  941. if ((*str == 'Z') || (*str == '-') || (*str == '+'))
  942. { *(p++)='0'; *(p++)='0'; }
  943. else
  944. {
  945. *(p++)= *(str++);
  946. *(p++)= *(str++);
  947. /* Skip any fractional seconds... */
  948. if (*str == '.')
  949. {
  950. str++;
  951. while ((*str >= '0') && (*str <= '9')) str++;
  952. }
  953. }
  954. *(p++)='Z';
  955. *(p++)='\0';
  956. if (*str == 'Z')
  957. offset=0;
  958. else
  959. {
  960. if ((*str != '+') && (str[5] != '-'))
  961. return 0;
  962. offset=((str[1]-'0')*10+(str[2]-'0'))*60;
  963. offset+=(str[3]-'0')*10+(str[4]-'0');
  964. if (*str == '-')
  965. offset= -offset;
  966. }
  967. atm.type=ctm->type;
  968. atm.length=sizeof(buff2);
  969. atm.data=(unsigned char *)buff2;
  970. if (X509_time_adj(&atm,-offset*60, cmp_time) == NULL)
  971. return 0;
  972. if (ctm->type == V_ASN1_UTCTIME)
  973. {
  974. i=(buff1[0]-'0')*10+(buff1[1]-'0');
  975. if (i < 50) i+=100; /* cf. RFC 2459 */
  976. j=(buff2[0]-'0')*10+(buff2[1]-'0');
  977. if (j < 50) j+=100;
  978. if (i < j) return -1;
  979. if (i > j) return 1;
  980. }
  981. i=strcmp(buff1,buff2);
  982. if (i == 0) /* wait a second then return younger :-) */
  983. return -1;
  984. else
  985. return i;
  986. }
  987. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  988. {
  989. return X509_time_adj(s, adj, NULL);
  990. }
  991. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
  992. {
  993. time_t t;
  994. int type = -1;
  995. if (in_tm) t = *in_tm;
  996. else time(&t);
  997. t+=adj;
  998. if (s) type = s->type;
  999. if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t);
  1000. if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t);
  1001. return ASN1_TIME_set(s, t);
  1002. }
  1003. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  1004. {
  1005. EVP_PKEY *ktmp=NULL,*ktmp2;
  1006. int i,j;
  1007. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
  1008. for (i=0; i<sk_X509_num(chain); i++)
  1009. {
  1010. ktmp=X509_get_pubkey(sk_X509_value(chain,i));
  1011. if (ktmp == NULL)
  1012. {
  1013. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1014. return 0;
  1015. }
  1016. if (!EVP_PKEY_missing_parameters(ktmp))
  1017. break;
  1018. else
  1019. {
  1020. EVP_PKEY_free(ktmp);
  1021. ktmp=NULL;
  1022. }
  1023. }
  1024. if (ktmp == NULL)
  1025. {
  1026. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1027. return 0;
  1028. }
  1029. /* first, populate the other certs */
  1030. for (j=i-1; j >= 0; j--)
  1031. {
  1032. ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
  1033. EVP_PKEY_copy_parameters(ktmp2,ktmp);
  1034. EVP_PKEY_free(ktmp2);
  1035. }
  1036. if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
  1037. EVP_PKEY_free(ktmp);
  1038. return 1;
  1039. }
  1040. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1041. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  1042. {
  1043. /* This function is (usually) called only once, by
  1044. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
  1045. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  1046. new_func, dup_func, free_func);
  1047. }
  1048. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1049. {
  1050. return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
  1051. }
  1052. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1053. {
  1054. return CRYPTO_get_ex_data(&ctx->ex_data,idx);
  1055. }
  1056. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1057. {
  1058. return ctx->error;
  1059. }
  1060. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1061. {
  1062. ctx->error=err;
  1063. }
  1064. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1065. {
  1066. return ctx->error_depth;
  1067. }
  1068. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1069. {
  1070. return ctx->current_cert;
  1071. }
  1072. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1073. {
  1074. return ctx->chain;
  1075. }
  1076. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1077. {
  1078. int i;
  1079. X509 *x;
  1080. STACK_OF(X509) *chain;
  1081. if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
  1082. for (i = 0; i < sk_X509_num(chain); i++)
  1083. {
  1084. x = sk_X509_value(chain, i);
  1085. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1086. }
  1087. return chain;
  1088. }
  1089. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1090. {
  1091. ctx->cert=x;
  1092. }
  1093. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1094. {
  1095. ctx->untrusted=sk;
  1096. }
  1097. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1098. {
  1099. ctx->crls=sk;
  1100. }
  1101. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1102. {
  1103. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1104. }
  1105. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1106. {
  1107. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1108. }
  1109. /* This function is used to set the X509_STORE_CTX purpose and trust
  1110. * values. This is intended to be used when another structure has its
  1111. * own trust and purpose values which (if set) will be inherited by
  1112. * the ctx. If they aren't set then we will usually have a default
  1113. * purpose in mind which should then be used to set the trust value.
  1114. * An example of this is SSL use: an SSL structure will have its own
  1115. * purpose and trust settings which the application can set: if they
  1116. * aren't set then we use the default of SSL client/server.
  1117. */
  1118. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1119. int purpose, int trust)
  1120. {
  1121. int idx;
  1122. /* If purpose not set use default */
  1123. if (!purpose) purpose = def_purpose;
  1124. /* If we have a purpose then check it is valid */
  1125. if (purpose)
  1126. {
  1127. X509_PURPOSE *ptmp;
  1128. idx = X509_PURPOSE_get_by_id(purpose);
  1129. if (idx == -1)
  1130. {
  1131. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1132. X509_R_UNKNOWN_PURPOSE_ID);
  1133. return 0;
  1134. }
  1135. ptmp = X509_PURPOSE_get0(idx);
  1136. if (ptmp->trust == X509_TRUST_DEFAULT)
  1137. {
  1138. idx = X509_PURPOSE_get_by_id(def_purpose);
  1139. if (idx == -1)
  1140. {
  1141. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1142. X509_R_UNKNOWN_PURPOSE_ID);
  1143. return 0;
  1144. }
  1145. ptmp = X509_PURPOSE_get0(idx);
  1146. }
  1147. /* If trust not set then get from purpose default */
  1148. if (!trust) trust = ptmp->trust;
  1149. }
  1150. if (trust)
  1151. {
  1152. idx = X509_TRUST_get_by_id(trust);
  1153. if (idx == -1)
  1154. {
  1155. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1156. X509_R_UNKNOWN_TRUST_ID);
  1157. return 0;
  1158. }
  1159. }
  1160. if (purpose && !ctx->param->purpose) ctx->param->purpose = purpose;
  1161. if (trust && !ctx->param->trust) ctx->param->trust = trust;
  1162. return 1;
  1163. }
  1164. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1165. {
  1166. X509_STORE_CTX *ctx;
  1167. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1168. if (!ctx)
  1169. {
  1170. X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1171. return NULL;
  1172. }
  1173. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1174. return ctx;
  1175. }
  1176. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1177. {
  1178. X509_STORE_CTX_cleanup(ctx);
  1179. OPENSSL_free(ctx);
  1180. }
  1181. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1182. STACK_OF(X509) *chain)
  1183. {
  1184. int ret = 1;
  1185. ctx->ctx=store;
  1186. ctx->current_method=0;
  1187. ctx->cert=x509;
  1188. ctx->untrusted=chain;
  1189. ctx->crls = NULL;
  1190. ctx->last_untrusted=0;
  1191. ctx->other_ctx=NULL;
  1192. ctx->valid=0;
  1193. ctx->chain=NULL;
  1194. ctx->error=0;
  1195. ctx->explicit_policy=0;
  1196. ctx->error_depth=0;
  1197. ctx->current_cert=NULL;
  1198. ctx->current_issuer=NULL;
  1199. ctx->tree = NULL;
  1200. ctx->param = X509_VERIFY_PARAM_new();
  1201. if (!ctx->param)
  1202. {
  1203. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1204. return 0;
  1205. }
  1206. /* Inherit callbacks and flags from X509_STORE if not set
  1207. * use defaults.
  1208. */
  1209. if (store)
  1210. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1211. else
  1212. ctx->param->flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
  1213. if (store)
  1214. {
  1215. ctx->verify_cb = store->verify_cb;
  1216. ctx->cleanup = store->cleanup;
  1217. }
  1218. else
  1219. ctx->cleanup = 0;
  1220. if (ret)
  1221. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1222. X509_VERIFY_PARAM_lookup("default"));
  1223. if (ret == 0)
  1224. {
  1225. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1226. return 0;
  1227. }
  1228. if (store && store->check_issued)
  1229. ctx->check_issued = store->check_issued;
  1230. else
  1231. ctx->check_issued = check_issued;
  1232. if (store && store->get_issuer)
  1233. ctx->get_issuer = store->get_issuer;
  1234. else
  1235. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1236. if (store && store->verify_cb)
  1237. ctx->verify_cb = store->verify_cb;
  1238. else
  1239. ctx->verify_cb = null_callback;
  1240. if (store && store->verify)
  1241. ctx->verify = store->verify;
  1242. else
  1243. ctx->verify = internal_verify;
  1244. if (store && store->check_revocation)
  1245. ctx->check_revocation = store->check_revocation;
  1246. else
  1247. ctx->check_revocation = check_revocation;
  1248. if (store && store->get_crl)
  1249. ctx->get_crl = store->get_crl;
  1250. else
  1251. ctx->get_crl = get_crl;
  1252. if (store && store->check_crl)
  1253. ctx->check_crl = store->check_crl;
  1254. else
  1255. ctx->check_crl = check_crl;
  1256. if (store && store->cert_crl)
  1257. ctx->cert_crl = store->cert_crl;
  1258. else
  1259. ctx->cert_crl = cert_crl;
  1260. ctx->check_policy = check_policy;
  1261. /* This memset() can't make any sense anyway, so it's removed. As
  1262. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  1263. * corresponding "new" here and remove this bogus initialisation. */
  1264. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  1265. if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  1266. &(ctx->ex_data)))
  1267. {
  1268. OPENSSL_free(ctx);
  1269. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1270. return 0;
  1271. }
  1272. return 1;
  1273. }
  1274. /* Set alternative lookup method: just a STACK of trusted certificates.
  1275. * This avoids X509_STORE nastiness where it isn't needed.
  1276. */
  1277. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1278. {
  1279. ctx->other_ctx = sk;
  1280. ctx->get_issuer = get_issuer_sk;
  1281. }
  1282. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  1283. {
  1284. if (ctx->cleanup) ctx->cleanup(ctx);
  1285. X509_VERIFY_PARAM_free(ctx->param);
  1286. if (ctx->tree)
  1287. X509_policy_tree_free(ctx->tree);
  1288. if (ctx->chain != NULL)
  1289. {
  1290. sk_X509_pop_free(ctx->chain,X509_free);
  1291. ctx->chain=NULL;
  1292. }
  1293. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  1294. memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
  1295. }
  1296. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  1297. {
  1298. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1299. }
  1300. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  1301. {
  1302. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  1303. }
  1304. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
  1305. {
  1306. X509_VERIFY_PARAM_set_time(ctx->param, t);
  1307. }
  1308. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  1309. int (*verify_cb)(int, X509_STORE_CTX *))
  1310. {
  1311. ctx->verify_cb=verify_cb;
  1312. }
  1313. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  1314. {
  1315. return ctx->tree;
  1316. }
  1317. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  1318. {
  1319. return ctx->explicit_policy;
  1320. }
  1321. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  1322. {
  1323. const X509_VERIFY_PARAM *param;
  1324. param = X509_VERIFY_PARAM_lookup(name);
  1325. if (!param)
  1326. return 0;
  1327. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  1328. }
  1329. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  1330. {
  1331. return ctx->param;
  1332. }
  1333. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  1334. {
  1335. if (ctx->param)
  1336. X509_VERIFY_PARAM_free(ctx->param);
  1337. ctx->param = param;
  1338. }
  1339. IMPLEMENT_STACK_OF(X509)
  1340. IMPLEMENT_ASN1_SET_OF(X509)
  1341. IMPLEMENT_STACK_OF(X509_NAME)
  1342. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  1343. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)