x509_vfy.c 36 KB

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