2
0

x509_vfy.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 xok,X509_STORE_CTX *xctx);
  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 xok,X509_STORE_CTX *xctx);
  344. int proxy_path_length = 0;
  345. int allow_proxy_certs =
  346. !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  347. cb=ctx->verify_cb;
  348. /* must_be_ca can have 1 of 3 values:
  349. -1: we accept both CA and non-CA certificates, to allow direct
  350. use of self-signed certificates (which are marked as CA).
  351. 0: we only accept non-CA certificates. This is currently not
  352. used, but the possibility is present for future extensions.
  353. 1: we only accept CA certificates. This is currently used for
  354. all certificates in the chain except the leaf certificate.
  355. */
  356. must_be_ca = -1;
  357. /* A hack to keep people who don't want to modify their software
  358. happy */
  359. if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
  360. allow_proxy_certs = 1;
  361. /* Check all untrusted certificates */
  362. for (i = 0; i < ctx->last_untrusted; i++)
  363. {
  364. int ret;
  365. x = sk_X509_value(ctx->chain, i);
  366. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  367. && (x->ex_flags & EXFLAG_CRITICAL))
  368. {
  369. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  370. ctx->error_depth = i;
  371. ctx->current_cert = x;
  372. ok=cb(0,ctx);
  373. if (!ok) goto end;
  374. }
  375. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))
  376. {
  377. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  378. ctx->error_depth = i;
  379. ctx->current_cert = x;
  380. ok=cb(0,ctx);
  381. if (!ok) goto end;
  382. }
  383. ret = X509_check_ca(x);
  384. switch(must_be_ca)
  385. {
  386. case -1:
  387. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  388. && (ret != 1) && (ret != 0))
  389. {
  390. ret = 0;
  391. ctx->error = X509_V_ERR_INVALID_CA;
  392. }
  393. else
  394. ret = 1;
  395. break;
  396. case 0:
  397. if (ret != 0)
  398. {
  399. ret = 0;
  400. ctx->error = X509_V_ERR_INVALID_NON_CA;
  401. }
  402. else
  403. ret = 1;
  404. break;
  405. default:
  406. if ((ret == 0)
  407. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  408. && (ret != 1)))
  409. {
  410. ret = 0;
  411. ctx->error = X509_V_ERR_INVALID_CA;
  412. }
  413. else
  414. ret = 1;
  415. break;
  416. }
  417. if (ret == 0)
  418. {
  419. ctx->error_depth = i;
  420. ctx->current_cert = x;
  421. ok=cb(0,ctx);
  422. if (!ok) goto end;
  423. }
  424. if (ctx->param->purpose > 0)
  425. {
  426. ret = X509_check_purpose(x, ctx->param->purpose,
  427. must_be_ca > 0);
  428. if ((ret == 0)
  429. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  430. && (ret != 1)))
  431. {
  432. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  433. ctx->error_depth = i;
  434. ctx->current_cert = x;
  435. ok=cb(0,ctx);
  436. if (!ok) goto end;
  437. }
  438. }
  439. /* Check pathlen */
  440. if ((i > 1) && (x->ex_pathlen != -1)
  441. && (i > (x->ex_pathlen + proxy_path_length + 1)))
  442. {
  443. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  444. ctx->error_depth = i;
  445. ctx->current_cert = x;
  446. ok=cb(0,ctx);
  447. if (!ok) goto end;
  448. }
  449. /* If this certificate is a proxy certificate, the next
  450. certificate must be another proxy certificate or a EE
  451. certificate. If not, the next certificate must be a
  452. CA certificate. */
  453. if (x->ex_flags & EXFLAG_PROXY)
  454. {
  455. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)
  456. {
  457. ctx->error =
  458. X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  459. ctx->error_depth = i;
  460. ctx->current_cert = x;
  461. ok=cb(0,ctx);
  462. if (!ok) goto end;
  463. }
  464. proxy_path_length++;
  465. must_be_ca = 0;
  466. }
  467. else
  468. must_be_ca = 1;
  469. }
  470. ok = 1;
  471. end:
  472. return ok;
  473. #endif
  474. }
  475. static int check_trust(X509_STORE_CTX *ctx)
  476. {
  477. #ifdef OPENSSL_NO_CHAIN_VERIFY
  478. return 1;
  479. #else
  480. int i, ok;
  481. X509 *x;
  482. int (*cb)(int xok,X509_STORE_CTX *xctx);
  483. cb=ctx->verify_cb;
  484. /* For now just check the last certificate in the chain */
  485. i = sk_X509_num(ctx->chain) - 1;
  486. x = sk_X509_value(ctx->chain, i);
  487. ok = X509_check_trust(x, ctx->param->trust, 0);
  488. if (ok == X509_TRUST_TRUSTED)
  489. return 1;
  490. ctx->error_depth = i;
  491. ctx->current_cert = x;
  492. if (ok == X509_TRUST_REJECTED)
  493. ctx->error = X509_V_ERR_CERT_REJECTED;
  494. else
  495. ctx->error = X509_V_ERR_CERT_UNTRUSTED;
  496. ok = cb(0, ctx);
  497. return ok;
  498. #endif
  499. }
  500. static int check_revocation(X509_STORE_CTX *ctx)
  501. {
  502. int i, last, ok;
  503. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  504. return 1;
  505. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  506. last = sk_X509_num(ctx->chain) - 1;
  507. else
  508. last = 0;
  509. for(i = 0; i <= last; i++)
  510. {
  511. ctx->error_depth = i;
  512. ok = check_cert(ctx);
  513. if (!ok) return ok;
  514. }
  515. return 1;
  516. }
  517. static int check_cert(X509_STORE_CTX *ctx)
  518. {
  519. X509_CRL *crl = NULL;
  520. X509 *x;
  521. int ok, cnum;
  522. cnum = ctx->error_depth;
  523. x = sk_X509_value(ctx->chain, cnum);
  524. ctx->current_cert = x;
  525. /* Try to retrieve relevant CRL */
  526. ok = ctx->get_crl(ctx, &crl, x);
  527. /* If error looking up CRL, nothing we can do except
  528. * notify callback
  529. */
  530. if(!ok)
  531. {
  532. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  533. ok = ctx->verify_cb(0, ctx);
  534. goto err;
  535. }
  536. ctx->current_crl = crl;
  537. ok = ctx->check_crl(ctx, crl);
  538. if (!ok) goto err;
  539. ok = ctx->cert_crl(ctx, crl, x);
  540. err:
  541. ctx->current_crl = NULL;
  542. X509_CRL_free(crl);
  543. return ok;
  544. }
  545. /* Check CRL times against values in X509_STORE_CTX */
  546. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  547. {
  548. time_t *ptime;
  549. int i;
  550. ctx->current_crl = crl;
  551. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  552. ptime = &ctx->param->check_time;
  553. else
  554. ptime = NULL;
  555. i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  556. if (i == 0)
  557. {
  558. ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  559. if (!notify || !ctx->verify_cb(0, ctx))
  560. return 0;
  561. }
  562. if (i > 0)
  563. {
  564. ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
  565. if (!notify || !ctx->verify_cb(0, ctx))
  566. return 0;
  567. }
  568. if(X509_CRL_get_nextUpdate(crl))
  569. {
  570. i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  571. if (i == 0)
  572. {
  573. ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  574. if (!notify || !ctx->verify_cb(0, ctx))
  575. return 0;
  576. }
  577. if (i < 0)
  578. {
  579. ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
  580. if (!notify || !ctx->verify_cb(0, ctx))
  581. return 0;
  582. }
  583. }
  584. ctx->current_crl = NULL;
  585. return 1;
  586. }
  587. /* Lookup CRLs from the supplied list. Look for matching isser name
  588. * and validity. If we can't find a valid CRL return the last one
  589. * with matching name. This gives more meaningful error codes. Otherwise
  590. * we'd get a CRL not found error if a CRL existed with matching name but
  591. * was invalid.
  592. */
  593. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl,
  594. X509_NAME *nm, STACK_OF(X509_CRL) *crls)
  595. {
  596. int i;
  597. X509_CRL *crl, *best_crl = NULL;
  598. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  599. {
  600. crl = sk_X509_CRL_value(crls, i);
  601. if (X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  602. continue;
  603. if (check_crl_time(ctx, crl, 0))
  604. {
  605. *pcrl = crl;
  606. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509);
  607. return 1;
  608. }
  609. best_crl = crl;
  610. }
  611. if (best_crl)
  612. {
  613. *pcrl = best_crl;
  614. CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509);
  615. }
  616. return 0;
  617. }
  618. /* Retrieve CRL corresponding to certificate: currently just a
  619. * subject lookup: maybe use AKID later...
  620. */
  621. static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x)
  622. {
  623. int ok;
  624. X509_CRL *crl = NULL;
  625. X509_OBJECT xobj;
  626. X509_NAME *nm;
  627. nm = X509_get_issuer_name(x);
  628. ok = get_crl_sk(ctx, &crl, nm, ctx->crls);
  629. if (ok)
  630. {
  631. *pcrl = crl;
  632. return 1;
  633. }
  634. ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj);
  635. if (!ok)
  636. {
  637. /* If we got a near match from get_crl_sk use that */
  638. if (crl)
  639. {
  640. *pcrl = crl;
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. *pcrl = xobj.data.crl;
  646. if (crl)
  647. X509_CRL_free(crl);
  648. return 1;
  649. }
  650. /* Check CRL validity */
  651. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  652. {
  653. X509 *issuer = NULL;
  654. EVP_PKEY *ikey = NULL;
  655. int ok = 0, chnum, cnum;
  656. cnum = ctx->error_depth;
  657. chnum = sk_X509_num(ctx->chain) - 1;
  658. /* Find CRL issuer: if not last certificate then issuer
  659. * is next certificate in chain.
  660. */
  661. if(cnum < chnum)
  662. issuer = sk_X509_value(ctx->chain, cnum + 1);
  663. else
  664. {
  665. issuer = sk_X509_value(ctx->chain, chnum);
  666. /* If not self signed, can't check signature */
  667. if(!ctx->check_issued(ctx, issuer, issuer))
  668. {
  669. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  670. ok = ctx->verify_cb(0, ctx);
  671. if(!ok) goto err;
  672. }
  673. }
  674. if(issuer)
  675. {
  676. /* Check for cRLSign bit if keyUsage present */
  677. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  678. !(issuer->ex_kusage & KU_CRL_SIGN))
  679. {
  680. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  681. ok = ctx->verify_cb(0, ctx);
  682. if(!ok) goto err;
  683. }
  684. /* Attempt to get issuer certificate public key */
  685. ikey = X509_get_pubkey(issuer);
  686. if(!ikey)
  687. {
  688. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  689. ok = ctx->verify_cb(0, ctx);
  690. if (!ok) goto err;
  691. }
  692. else
  693. {
  694. /* Verify CRL signature */
  695. if(X509_CRL_verify(crl, ikey) <= 0)
  696. {
  697. ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
  698. ok = ctx->verify_cb(0, ctx);
  699. if (!ok) goto err;
  700. }
  701. }
  702. }
  703. ok = check_crl_time(ctx, crl, 1);
  704. if (!ok)
  705. goto err;
  706. ok = 1;
  707. err:
  708. EVP_PKEY_free(ikey);
  709. return ok;
  710. }
  711. /* Check certificate against CRL */
  712. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  713. {
  714. int idx, ok;
  715. X509_REVOKED rtmp;
  716. STACK_OF(X509_EXTENSION) *exts;
  717. X509_EXTENSION *ext;
  718. /* Look for serial number of certificate in CRL */
  719. rtmp.serialNumber = X509_get_serialNumber(x);
  720. /* Sort revoked into serial number order if not already sorted.
  721. * Do this under a lock to avoid race condition.
  722. */
  723. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
  724. {
  725. CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
  726. sk_X509_REVOKED_sort(crl->crl->revoked);
  727. CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
  728. }
  729. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  730. /* If found assume revoked: want something cleverer than
  731. * this to handle entry extensions in V2 CRLs.
  732. */
  733. if(idx >= 0)
  734. {
  735. ctx->error = X509_V_ERR_CERT_REVOKED;
  736. ok = ctx->verify_cb(0, ctx);
  737. if (!ok) return 0;
  738. }
  739. if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  740. return 1;
  741. /* See if we have any critical CRL extensions: since we
  742. * currently don't handle any CRL extensions the CRL must be
  743. * rejected.
  744. * This code accesses the X509_CRL structure directly: applications
  745. * shouldn't do this.
  746. */
  747. exts = crl->crl->extensions;
  748. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
  749. {
  750. ext = sk_X509_EXTENSION_value(exts, idx);
  751. if (ext->critical > 0)
  752. {
  753. ctx->error =
  754. X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  755. ok = ctx->verify_cb(0, ctx);
  756. if(!ok) return 0;
  757. break;
  758. }
  759. }
  760. return 1;
  761. }
  762. static int check_policy(X509_STORE_CTX *ctx)
  763. {
  764. int ret;
  765. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  766. ctx->param->policies, ctx->param->flags);
  767. if (ret == 0)
  768. {
  769. X509err(X509_F_CHECK_POLICY,ERR_R_MALLOC_FAILURE);
  770. return 0;
  771. }
  772. /* Invalid or inconsistent extensions */
  773. if (ret == -1)
  774. {
  775. /* Locate certificates with bad extensions and notify
  776. * callback.
  777. */
  778. X509 *x;
  779. int i;
  780. for (i = 1; i < sk_X509_num(ctx->chain); i++)
  781. {
  782. x = sk_X509_value(ctx->chain, i);
  783. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  784. continue;
  785. ctx->current_cert = x;
  786. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  787. ret = ctx->verify_cb(0, ctx);
  788. }
  789. return 1;
  790. }
  791. if (ret == -2)
  792. {
  793. ctx->current_cert = NULL;
  794. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  795. return ctx->verify_cb(0, ctx);
  796. }
  797. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY)
  798. {
  799. ctx->current_cert = NULL;
  800. ctx->error = X509_V_OK;
  801. if (!ctx->verify_cb(2, ctx))
  802. return 0;
  803. }
  804. return 1;
  805. }
  806. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  807. {
  808. time_t *ptime;
  809. int i;
  810. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  811. ptime = &ctx->param->check_time;
  812. else
  813. ptime = NULL;
  814. i=X509_cmp_time(X509_get_notBefore(x), ptime);
  815. if (i == 0)
  816. {
  817. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_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_NOT_YET_VALID;
  825. ctx->current_cert=x;
  826. if (!ctx->verify_cb(0, ctx))
  827. return 0;
  828. }
  829. i=X509_cmp_time(X509_get_notAfter(x), ptime);
  830. if (i == 0)
  831. {
  832. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  833. ctx->current_cert=x;
  834. if (!ctx->verify_cb(0, ctx))
  835. return 0;
  836. }
  837. if (i < 0)
  838. {
  839. ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
  840. ctx->current_cert=x;
  841. if (!ctx->verify_cb(0, ctx))
  842. return 0;
  843. }
  844. return 1;
  845. }
  846. static int internal_verify(X509_STORE_CTX *ctx)
  847. {
  848. int ok=0,n;
  849. X509 *xs,*xi;
  850. EVP_PKEY *pkey=NULL;
  851. int (*cb)(int xok,X509_STORE_CTX *xctx);
  852. cb=ctx->verify_cb;
  853. n=sk_X509_num(ctx->chain);
  854. ctx->error_depth=n-1;
  855. n--;
  856. xi=sk_X509_value(ctx->chain,n);
  857. if (ctx->check_issued(ctx, xi, xi))
  858. xs=xi;
  859. else
  860. {
  861. if (n <= 0)
  862. {
  863. ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  864. ctx->current_cert=xi;
  865. ok=cb(0,ctx);
  866. goto end;
  867. }
  868. else
  869. {
  870. n--;
  871. ctx->error_depth=n;
  872. xs=sk_X509_value(ctx->chain,n);
  873. }
  874. }
  875. /* ctx->error=0; not needed */
  876. while (n >= 0)
  877. {
  878. ctx->error_depth=n;
  879. if (!xs->valid)
  880. {
  881. if ((pkey=X509_get_pubkey(xi)) == NULL)
  882. {
  883. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  884. ctx->current_cert=xi;
  885. ok=(*cb)(0,ctx);
  886. if (!ok) goto end;
  887. }
  888. else if (X509_verify(xs,pkey) <= 0)
  889. /* XXX For the final trusted self-signed cert,
  890. * this is a waste of time. That check should
  891. * optional so that e.g. 'openssl x509' can be
  892. * used to detect invalid self-signatures, but
  893. * we don't verify again and again in SSL
  894. * handshakes and the like once the cert has
  895. * been declared trusted. */
  896. {
  897. ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
  898. ctx->current_cert=xs;
  899. ok=(*cb)(0,ctx);
  900. if (!ok)
  901. {
  902. EVP_PKEY_free(pkey);
  903. goto end;
  904. }
  905. }
  906. EVP_PKEY_free(pkey);
  907. pkey=NULL;
  908. }
  909. xs->valid = 1;
  910. ok = check_cert_time(ctx, xs);
  911. if (!ok)
  912. goto end;
  913. /* The last error (if any) is still in the error value */
  914. ctx->current_issuer=xi;
  915. ctx->current_cert=xs;
  916. ok=(*cb)(1,ctx);
  917. if (!ok) goto end;
  918. n--;
  919. if (n >= 0)
  920. {
  921. xi=xs;
  922. xs=sk_X509_value(ctx->chain,n);
  923. }
  924. }
  925. ok=1;
  926. end:
  927. return ok;
  928. }
  929. int X509_cmp_current_time(ASN1_TIME *ctm)
  930. {
  931. return X509_cmp_time(ctm, NULL);
  932. }
  933. int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
  934. {
  935. char *str;
  936. ASN1_TIME atm;
  937. long offset;
  938. char buff1[24],buff2[24],*p;
  939. int i,j;
  940. p=buff1;
  941. i=ctm->length;
  942. str=(char *)ctm->data;
  943. if (ctm->type == V_ASN1_UTCTIME)
  944. {
  945. if ((i < 11) || (i > 17)) return 0;
  946. memcpy(p,str,10);
  947. p+=10;
  948. str+=10;
  949. }
  950. else
  951. {
  952. if (i < 13) return 0;
  953. memcpy(p,str,12);
  954. p+=12;
  955. str+=12;
  956. }
  957. if ((*str == 'Z') || (*str == '-') || (*str == '+'))
  958. { *(p++)='0'; *(p++)='0'; }
  959. else
  960. {
  961. *(p++)= *(str++);
  962. *(p++)= *(str++);
  963. /* Skip any fractional seconds... */
  964. if (*str == '.')
  965. {
  966. str++;
  967. while ((*str >= '0') && (*str <= '9')) str++;
  968. }
  969. }
  970. *(p++)='Z';
  971. *(p++)='\0';
  972. if (*str == 'Z')
  973. offset=0;
  974. else
  975. {
  976. if ((*str != '+') && (*str != '-'))
  977. return 0;
  978. offset=((str[1]-'0')*10+(str[2]-'0'))*60;
  979. offset+=(str[3]-'0')*10+(str[4]-'0');
  980. if (*str == '-')
  981. offset= -offset;
  982. }
  983. atm.type=ctm->type;
  984. atm.length=sizeof(buff2);
  985. atm.data=(unsigned char *)buff2;
  986. if (X509_time_adj(&atm,-offset*60, cmp_time) == NULL)
  987. return 0;
  988. if (ctm->type == V_ASN1_UTCTIME)
  989. {
  990. i=(buff1[0]-'0')*10+(buff1[1]-'0');
  991. if (i < 50) i+=100; /* cf. RFC 2459 */
  992. j=(buff2[0]-'0')*10+(buff2[1]-'0');
  993. if (j < 50) j+=100;
  994. if (i < j) return -1;
  995. if (i > j) return 1;
  996. }
  997. i=strcmp(buff1,buff2);
  998. if (i == 0) /* wait a second then return younger :-) */
  999. return -1;
  1000. else
  1001. return i;
  1002. }
  1003. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1004. {
  1005. return X509_time_adj(s, adj, NULL);
  1006. }
  1007. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
  1008. {
  1009. time_t t;
  1010. int type = -1;
  1011. if (in_tm) t = *in_tm;
  1012. else time(&t);
  1013. t+=adj;
  1014. if (s) type = s->type;
  1015. if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t);
  1016. if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t);
  1017. return ASN1_TIME_set(s, t);
  1018. }
  1019. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  1020. {
  1021. EVP_PKEY *ktmp=NULL,*ktmp2;
  1022. int i,j;
  1023. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
  1024. for (i=0; i<sk_X509_num(chain); i++)
  1025. {
  1026. ktmp=X509_get_pubkey(sk_X509_value(chain,i));
  1027. if (ktmp == NULL)
  1028. {
  1029. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1030. return 0;
  1031. }
  1032. if (!EVP_PKEY_missing_parameters(ktmp))
  1033. break;
  1034. else
  1035. {
  1036. EVP_PKEY_free(ktmp);
  1037. ktmp=NULL;
  1038. }
  1039. }
  1040. if (ktmp == NULL)
  1041. {
  1042. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1043. return 0;
  1044. }
  1045. /* first, populate the other certs */
  1046. for (j=i-1; j >= 0; j--)
  1047. {
  1048. ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
  1049. EVP_PKEY_copy_parameters(ktmp2,ktmp);
  1050. EVP_PKEY_free(ktmp2);
  1051. }
  1052. if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
  1053. EVP_PKEY_free(ktmp);
  1054. return 1;
  1055. }
  1056. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1057. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  1058. {
  1059. /* This function is (usually) called only once, by
  1060. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
  1061. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  1062. new_func, dup_func, free_func);
  1063. }
  1064. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1065. {
  1066. return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
  1067. }
  1068. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1069. {
  1070. return CRYPTO_get_ex_data(&ctx->ex_data,idx);
  1071. }
  1072. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1073. {
  1074. return ctx->error;
  1075. }
  1076. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1077. {
  1078. ctx->error=err;
  1079. }
  1080. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1081. {
  1082. return ctx->error_depth;
  1083. }
  1084. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1085. {
  1086. return ctx->current_cert;
  1087. }
  1088. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1089. {
  1090. return ctx->chain;
  1091. }
  1092. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1093. {
  1094. int i;
  1095. X509 *x;
  1096. STACK_OF(X509) *chain;
  1097. if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
  1098. for (i = 0; i < sk_X509_num(chain); i++)
  1099. {
  1100. x = sk_X509_value(chain, i);
  1101. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1102. }
  1103. return chain;
  1104. }
  1105. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1106. {
  1107. ctx->cert=x;
  1108. }
  1109. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1110. {
  1111. ctx->untrusted=sk;
  1112. }
  1113. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1114. {
  1115. ctx->crls=sk;
  1116. }
  1117. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1118. {
  1119. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1120. }
  1121. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1122. {
  1123. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1124. }
  1125. /* This function is used to set the X509_STORE_CTX purpose and trust
  1126. * values. This is intended to be used when another structure has its
  1127. * own trust and purpose values which (if set) will be inherited by
  1128. * the ctx. If they aren't set then we will usually have a default
  1129. * purpose in mind which should then be used to set the trust value.
  1130. * An example of this is SSL use: an SSL structure will have its own
  1131. * purpose and trust settings which the application can set: if they
  1132. * aren't set then we use the default of SSL client/server.
  1133. */
  1134. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1135. int purpose, int trust)
  1136. {
  1137. int idx;
  1138. /* If purpose not set use default */
  1139. if (!purpose) purpose = def_purpose;
  1140. /* If we have a purpose then check it is valid */
  1141. if (purpose)
  1142. {
  1143. X509_PURPOSE *ptmp;
  1144. idx = X509_PURPOSE_get_by_id(purpose);
  1145. if (idx == -1)
  1146. {
  1147. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1148. X509_R_UNKNOWN_PURPOSE_ID);
  1149. return 0;
  1150. }
  1151. ptmp = X509_PURPOSE_get0(idx);
  1152. if (ptmp->trust == X509_TRUST_DEFAULT)
  1153. {
  1154. idx = X509_PURPOSE_get_by_id(def_purpose);
  1155. if (idx == -1)
  1156. {
  1157. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1158. X509_R_UNKNOWN_PURPOSE_ID);
  1159. return 0;
  1160. }
  1161. ptmp = X509_PURPOSE_get0(idx);
  1162. }
  1163. /* If trust not set then get from purpose default */
  1164. if (!trust) trust = ptmp->trust;
  1165. }
  1166. if (trust)
  1167. {
  1168. idx = X509_TRUST_get_by_id(trust);
  1169. if (idx == -1)
  1170. {
  1171. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1172. X509_R_UNKNOWN_TRUST_ID);
  1173. return 0;
  1174. }
  1175. }
  1176. if (purpose && !ctx->param->purpose) ctx->param->purpose = purpose;
  1177. if (trust && !ctx->param->trust) ctx->param->trust = trust;
  1178. return 1;
  1179. }
  1180. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1181. {
  1182. X509_STORE_CTX *ctx;
  1183. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1184. if (!ctx)
  1185. {
  1186. X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1187. return NULL;
  1188. }
  1189. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1190. return ctx;
  1191. }
  1192. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1193. {
  1194. X509_STORE_CTX_cleanup(ctx);
  1195. OPENSSL_free(ctx);
  1196. }
  1197. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1198. STACK_OF(X509) *chain)
  1199. {
  1200. int ret = 1;
  1201. ctx->ctx=store;
  1202. ctx->current_method=0;
  1203. ctx->cert=x509;
  1204. ctx->untrusted=chain;
  1205. ctx->crls = NULL;
  1206. ctx->last_untrusted=0;
  1207. ctx->other_ctx=NULL;
  1208. ctx->valid=0;
  1209. ctx->chain=NULL;
  1210. ctx->error=0;
  1211. ctx->explicit_policy=0;
  1212. ctx->error_depth=0;
  1213. ctx->current_cert=NULL;
  1214. ctx->current_issuer=NULL;
  1215. ctx->tree = NULL;
  1216. ctx->param = X509_VERIFY_PARAM_new();
  1217. if (!ctx->param)
  1218. {
  1219. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1220. return 0;
  1221. }
  1222. /* Inherit callbacks and flags from X509_STORE if not set
  1223. * use defaults.
  1224. */
  1225. if (store)
  1226. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1227. else
  1228. ctx->param->flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
  1229. if (store)
  1230. {
  1231. ctx->verify_cb = store->verify_cb;
  1232. ctx->cleanup = store->cleanup;
  1233. }
  1234. else
  1235. ctx->cleanup = 0;
  1236. if (ret)
  1237. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1238. X509_VERIFY_PARAM_lookup("default"));
  1239. if (ret == 0)
  1240. {
  1241. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1242. return 0;
  1243. }
  1244. if (store && store->check_issued)
  1245. ctx->check_issued = store->check_issued;
  1246. else
  1247. ctx->check_issued = check_issued;
  1248. if (store && store->get_issuer)
  1249. ctx->get_issuer = store->get_issuer;
  1250. else
  1251. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1252. if (store && store->verify_cb)
  1253. ctx->verify_cb = store->verify_cb;
  1254. else
  1255. ctx->verify_cb = null_callback;
  1256. if (store && store->verify)
  1257. ctx->verify = store->verify;
  1258. else
  1259. ctx->verify = internal_verify;
  1260. if (store && store->check_revocation)
  1261. ctx->check_revocation = store->check_revocation;
  1262. else
  1263. ctx->check_revocation = check_revocation;
  1264. if (store && store->get_crl)
  1265. ctx->get_crl = store->get_crl;
  1266. else
  1267. ctx->get_crl = get_crl;
  1268. if (store && store->check_crl)
  1269. ctx->check_crl = store->check_crl;
  1270. else
  1271. ctx->check_crl = check_crl;
  1272. if (store && store->cert_crl)
  1273. ctx->cert_crl = store->cert_crl;
  1274. else
  1275. ctx->cert_crl = cert_crl;
  1276. ctx->check_policy = check_policy;
  1277. /* This memset() can't make any sense anyway, so it's removed. As
  1278. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  1279. * corresponding "new" here and remove this bogus initialisation. */
  1280. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  1281. if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  1282. &(ctx->ex_data)))
  1283. {
  1284. OPENSSL_free(ctx);
  1285. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1286. return 0;
  1287. }
  1288. return 1;
  1289. }
  1290. /* Set alternative lookup method: just a STACK of trusted certificates.
  1291. * This avoids X509_STORE nastiness where it isn't needed.
  1292. */
  1293. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1294. {
  1295. ctx->other_ctx = sk;
  1296. ctx->get_issuer = get_issuer_sk;
  1297. }
  1298. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  1299. {
  1300. if (ctx->cleanup) ctx->cleanup(ctx);
  1301. X509_VERIFY_PARAM_free(ctx->param);
  1302. if (ctx->tree)
  1303. X509_policy_tree_free(ctx->tree);
  1304. if (ctx->chain != NULL)
  1305. {
  1306. sk_X509_pop_free(ctx->chain,X509_free);
  1307. ctx->chain=NULL;
  1308. }
  1309. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  1310. memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
  1311. }
  1312. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  1313. {
  1314. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1315. }
  1316. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  1317. {
  1318. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  1319. }
  1320. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
  1321. {
  1322. X509_VERIFY_PARAM_set_time(ctx->param, t);
  1323. }
  1324. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  1325. int (*verify_cb)(int, X509_STORE_CTX *))
  1326. {
  1327. ctx->verify_cb=verify_cb;
  1328. }
  1329. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  1330. {
  1331. return ctx->tree;
  1332. }
  1333. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  1334. {
  1335. return ctx->explicit_policy;
  1336. }
  1337. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  1338. {
  1339. const X509_VERIFY_PARAM *param;
  1340. param = X509_VERIFY_PARAM_lookup(name);
  1341. if (!param)
  1342. return 0;
  1343. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  1344. }
  1345. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  1346. {
  1347. return ctx->param;
  1348. }
  1349. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  1350. {
  1351. if (ctx->param)
  1352. X509_VERIFY_PARAM_free(ctx->param);
  1353. ctx->param = param;
  1354. }
  1355. IMPLEMENT_STACK_OF(X509)
  1356. IMPLEMENT_ASN1_SET_OF(X509)
  1357. IMPLEMENT_STACK_OF(X509_NAME)
  1358. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  1359. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)