x509_vfy.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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_purpose(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 internal_verify(X509_STORE_CTX *ctx);
  78. const char *X509_version="X.509" OPENSSL_VERSION_PTEXT;
  79. static int null_callback(int ok, X509_STORE_CTX *e)
  80. {
  81. return ok;
  82. }
  83. #if 0
  84. static int x509_subject_cmp(X509 **a, X509 **b)
  85. {
  86. return X509_subject_name_cmp(*a,*b);
  87. }
  88. #endif
  89. int X509_verify_cert(X509_STORE_CTX *ctx)
  90. {
  91. X509 *x,*xtmp,*chain_ss=NULL;
  92. X509_NAME *xn;
  93. int depth,i,ok=0;
  94. int num;
  95. int (*cb)();
  96. STACK_OF(X509) *sktmp=NULL;
  97. if (ctx->cert == NULL)
  98. {
  99. X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  100. return -1;
  101. }
  102. cb=ctx->verify_cb;
  103. /* first we make sure the chain we are going to build is
  104. * present and that the first entry is in place */
  105. if (ctx->chain == NULL)
  106. {
  107. if ( ((ctx->chain=sk_X509_new_null()) == NULL) ||
  108. (!sk_X509_push(ctx->chain,ctx->cert)))
  109. {
  110. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  111. goto end;
  112. }
  113. CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
  114. ctx->last_untrusted=1;
  115. }
  116. /* We use a temporary STACK so we can chop and hack at it */
  117. if (ctx->untrusted != NULL
  118. && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
  119. {
  120. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  121. goto end;
  122. }
  123. num=sk_X509_num(ctx->chain);
  124. x=sk_X509_value(ctx->chain,num-1);
  125. depth=ctx->depth;
  126. for (;;)
  127. {
  128. /* If we have enough, we break */
  129. if (depth < num) break; /* FIXME: If this happens, we should take
  130. * note of it and, if appropriate, use the
  131. * X509_V_ERR_CERT_CHAIN_TOO_LONG error
  132. * code later.
  133. */
  134. /* If we are self signed, we break */
  135. xn=X509_get_issuer_name(x);
  136. if (ctx->check_issued(ctx, x,x)) break;
  137. /* If we were passed a cert chain, use it first */
  138. if (ctx->untrusted != NULL)
  139. {
  140. xtmp=find_issuer(ctx, sktmp,x);
  141. if (xtmp != NULL)
  142. {
  143. if (!sk_X509_push(ctx->chain,xtmp))
  144. {
  145. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  146. goto end;
  147. }
  148. CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
  149. sk_X509_delete_ptr(sktmp,xtmp);
  150. ctx->last_untrusted++;
  151. x=xtmp;
  152. num++;
  153. /* reparse the full chain for
  154. * the next one */
  155. continue;
  156. }
  157. }
  158. break;
  159. }
  160. /* at this point, chain should contain a list of untrusted
  161. * certificates. We now need to add at least one trusted one,
  162. * if possible, otherwise we complain. */
  163. /* Examine last certificate in chain and see if it
  164. * is self signed.
  165. */
  166. i=sk_X509_num(ctx->chain);
  167. x=sk_X509_value(ctx->chain,i-1);
  168. xn = X509_get_subject_name(x);
  169. if (ctx->check_issued(ctx, x, x))
  170. {
  171. /* we have a self signed certificate */
  172. if (sk_X509_num(ctx->chain) == 1)
  173. {
  174. /* We have a single self signed certificate: see if
  175. * we can find it in the store. We must have an exact
  176. * match to avoid possible impersonation.
  177. */
  178. ok = ctx->get_issuer(&xtmp, ctx, x);
  179. if ((ok <= 0) || X509_cmp(x, xtmp))
  180. {
  181. ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  182. ctx->current_cert=x;
  183. ctx->error_depth=i-1;
  184. if (ok == 1) X509_free(xtmp);
  185. ok=cb(0,ctx);
  186. if (!ok) goto end;
  187. }
  188. else
  189. {
  190. /* We have a match: replace certificate with store version
  191. * so we get any trust settings.
  192. */
  193. X509_free(x);
  194. x = xtmp;
  195. sk_X509_set(ctx->chain, i - 1, x);
  196. ctx->last_untrusted=0;
  197. }
  198. }
  199. else
  200. {
  201. /* extract and save self signed certificate for later use */
  202. chain_ss=sk_X509_pop(ctx->chain);
  203. ctx->last_untrusted--;
  204. num--;
  205. x=sk_X509_value(ctx->chain,num-1);
  206. }
  207. }
  208. /* We now lookup certs from the certificate store */
  209. for (;;)
  210. {
  211. /* If we have enough, we break */
  212. if (depth < num) break;
  213. /* If we are self signed, we break */
  214. xn=X509_get_issuer_name(x);
  215. if (ctx->check_issued(ctx,x,x)) break;
  216. ok = ctx->get_issuer(&xtmp, ctx, x);
  217. if (ok < 0) return ok;
  218. if (ok == 0) break;
  219. x = xtmp;
  220. if (!sk_X509_push(ctx->chain,x))
  221. {
  222. X509_free(xtmp);
  223. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  224. return 0;
  225. }
  226. num++;
  227. }
  228. /* we now have our chain, lets check it... */
  229. xn=X509_get_issuer_name(x);
  230. /* Is last certificate looked up self signed? */
  231. if (!ctx->check_issued(ctx,x,x))
  232. {
  233. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
  234. {
  235. if (ctx->last_untrusted >= num)
  236. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  237. else
  238. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  239. ctx->current_cert=x;
  240. }
  241. else
  242. {
  243. sk_X509_push(ctx->chain,chain_ss);
  244. num++;
  245. ctx->last_untrusted=num;
  246. ctx->current_cert=chain_ss;
  247. ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  248. chain_ss=NULL;
  249. }
  250. ctx->error_depth=num-1;
  251. ok=cb(0,ctx);
  252. if (!ok) goto end;
  253. }
  254. /* We have the chain complete: now we need to check its purpose */
  255. if (ctx->purpose > 0) ok = check_chain_purpose(ctx);
  256. if (!ok) goto end;
  257. /* The chain extensions are OK: check trust */
  258. if (ctx->trust > 0) ok = check_trust(ctx);
  259. if (!ok) goto end;
  260. /* We may as well copy down any DSA parameters that are required */
  261. X509_get_pubkey_parameters(NULL,ctx->chain);
  262. /* Check revocation status: we do this after copying parameters
  263. * because they may be needed for CRL signature verification.
  264. */
  265. ok = ctx->check_revocation(ctx);
  266. if(!ok) goto end;
  267. /* At this point, we have a chain and just need to verify it */
  268. if (ctx->verify != NULL)
  269. ok=ctx->verify(ctx);
  270. else
  271. ok=internal_verify(ctx);
  272. if (0)
  273. {
  274. end:
  275. X509_get_pubkey_parameters(NULL,ctx->chain);
  276. }
  277. if (sktmp != NULL) sk_X509_free(sktmp);
  278. if (chain_ss != NULL) X509_free(chain_ss);
  279. return ok;
  280. }
  281. /* Given a STACK_OF(X509) find the issuer of cert (if any)
  282. */
  283. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  284. {
  285. int i;
  286. X509 *issuer;
  287. for (i = 0; i < sk_X509_num(sk); i++)
  288. {
  289. issuer = sk_X509_value(sk, i);
  290. if (ctx->check_issued(ctx, x, issuer))
  291. return issuer;
  292. }
  293. return NULL;
  294. }
  295. /* Given a possible certificate and issuer check them */
  296. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  297. {
  298. int ret;
  299. ret = X509_check_issued(issuer, x);
  300. if (ret == X509_V_OK)
  301. return 1;
  302. /* If we haven't asked for issuer errors don't set ctx */
  303. if (!(ctx->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  304. return 0;
  305. ctx->error = ret;
  306. ctx->current_cert = x;
  307. ctx->current_issuer = issuer;
  308. return ctx->verify_cb(0, ctx);
  309. return 0;
  310. }
  311. /* Alternative lookup method: look from a STACK stored in other_ctx */
  312. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  313. {
  314. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  315. if (*issuer)
  316. {
  317. CRYPTO_add(&(*issuer)->references,1,CRYPTO_LOCK_X509);
  318. return 1;
  319. }
  320. else
  321. return 0;
  322. }
  323. /* Check a certificate chains extensions for consistency
  324. * with the supplied purpose
  325. */
  326. static int check_chain_purpose(X509_STORE_CTX *ctx)
  327. {
  328. #ifdef OPENSSL_NO_CHAIN_VERIFY
  329. return 1;
  330. #else
  331. int i, ok=0;
  332. X509 *x;
  333. int (*cb)();
  334. cb=ctx->verify_cb;
  335. /* Check all untrusted certificates */
  336. for (i = 0; i < ctx->last_untrusted; i++)
  337. {
  338. x = sk_X509_value(ctx->chain, i);
  339. if (!(ctx->flags & X509_V_FLAG_IGNORE_CRITICAL)
  340. && (x->ex_flags & EXFLAG_CRITICAL))
  341. {
  342. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  343. ctx->error_depth = i;
  344. ctx->current_cert = x;
  345. ok=cb(0,ctx);
  346. if (!ok) goto end;
  347. }
  348. if (!X509_check_purpose(x, ctx->purpose, i))
  349. {
  350. if (i)
  351. ctx->error = X509_V_ERR_INVALID_CA;
  352. else
  353. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  354. ctx->error_depth = i;
  355. ctx->current_cert = x;
  356. ok=cb(0,ctx);
  357. if (!ok) goto end;
  358. }
  359. /* Check pathlen */
  360. if ((i > 1) && (x->ex_pathlen != -1)
  361. && (i > (x->ex_pathlen + 1)))
  362. {
  363. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  364. ctx->error_depth = i;
  365. ctx->current_cert = x;
  366. ok=cb(0,ctx);
  367. if (!ok) goto end;
  368. }
  369. }
  370. ok = 1;
  371. end:
  372. return ok;
  373. #endif
  374. }
  375. static int check_trust(X509_STORE_CTX *ctx)
  376. {
  377. #ifdef OPENSSL_NO_CHAIN_VERIFY
  378. return 1;
  379. #else
  380. int i, ok;
  381. X509 *x;
  382. int (*cb)();
  383. cb=ctx->verify_cb;
  384. /* For now just check the last certificate in the chain */
  385. i = sk_X509_num(ctx->chain) - 1;
  386. x = sk_X509_value(ctx->chain, i);
  387. ok = X509_check_trust(x, ctx->trust, 0);
  388. if (ok == X509_TRUST_TRUSTED)
  389. return 1;
  390. ctx->error_depth = i;
  391. ctx->current_cert = x;
  392. if (ok == X509_TRUST_REJECTED)
  393. ctx->error = X509_V_ERR_CERT_REJECTED;
  394. else
  395. ctx->error = X509_V_ERR_CERT_UNTRUSTED;
  396. ok = cb(0, ctx);
  397. return ok;
  398. #endif
  399. }
  400. static int check_revocation(X509_STORE_CTX *ctx)
  401. {
  402. int i, last, ok;
  403. if (!(ctx->flags & X509_V_FLAG_CRL_CHECK))
  404. return 1;
  405. if (ctx->flags & X509_V_FLAG_CRL_CHECK_ALL)
  406. last = 0;
  407. else
  408. last = sk_X509_num(ctx->chain) - 1;
  409. for(i = 0; i <= last; i++)
  410. {
  411. ctx->error_depth = i;
  412. ok = check_cert(ctx);
  413. if (!ok) return ok;
  414. }
  415. return 1;
  416. }
  417. static int check_cert(X509_STORE_CTX *ctx)
  418. {
  419. X509_CRL *crl = NULL;
  420. X509 *x;
  421. int ok, cnum;
  422. cnum = ctx->error_depth;
  423. x = sk_X509_value(ctx->chain, cnum);
  424. ctx->current_cert = x;
  425. /* Try to retrieve relevant CRL */
  426. ok = ctx->get_crl(ctx, &crl, x);
  427. /* If error looking up CRL, nothing we can do except
  428. * notify callback
  429. */
  430. if(!ok)
  431. {
  432. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  433. ok = ctx->verify_cb(0, ctx);
  434. goto err;
  435. }
  436. ctx->current_crl = crl;
  437. ok = ctx->check_crl(ctx, crl);
  438. if (!ok) goto err;
  439. ok = ctx->cert_crl(ctx, crl, x);
  440. err:
  441. ctx->current_crl = NULL;
  442. X509_CRL_free(crl);
  443. return ok;
  444. }
  445. /* Retrieve CRL corresponding to certificate: currently just a
  446. * subject lookup: maybe use AKID later...
  447. * Also might look up any included CRLs too (e.g PKCS#7 signedData).
  448. */
  449. static int get_crl(X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x)
  450. {
  451. int ok;
  452. X509_OBJECT xobj;
  453. ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, X509_get_issuer_name(x), &xobj);
  454. if (!ok) return 0;
  455. *crl = xobj.data.crl;
  456. return 1;
  457. }
  458. /* Check CRL validity */
  459. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  460. {
  461. X509 *issuer = NULL;
  462. EVP_PKEY *ikey = NULL;
  463. int ok = 0, chnum, cnum, i;
  464. time_t *ptime;
  465. cnum = ctx->error_depth;
  466. chnum = sk_X509_num(ctx->chain) - 1;
  467. /* Find CRL issuer: if not last certificate then issuer
  468. * is next certificate in chain.
  469. */
  470. if(cnum < chnum)
  471. issuer = sk_X509_value(ctx->chain, cnum + 1);
  472. else
  473. {
  474. issuer = sk_X509_value(ctx->chain, chnum);
  475. /* If not self signed, can't check signature */
  476. if(!ctx->check_issued(ctx, issuer, issuer))
  477. {
  478. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  479. ok = ctx->verify_cb(0, ctx);
  480. if(!ok) goto err;
  481. }
  482. }
  483. if(issuer)
  484. {
  485. /* Attempt to get issuer certificate public key */
  486. ikey = X509_get_pubkey(issuer);
  487. if(!ikey)
  488. {
  489. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  490. ok = ctx->verify_cb(0, ctx);
  491. if (!ok) goto err;
  492. }
  493. else
  494. {
  495. /* Verify CRL signature */
  496. if(X509_CRL_verify(crl, ikey) <= 0)
  497. {
  498. ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
  499. ok = ctx->verify_cb(0, ctx);
  500. if (!ok) goto err;
  501. }
  502. }
  503. }
  504. /* OK, CRL signature valid check times */
  505. if (ctx->flags & X509_V_FLAG_USE_CHECK_TIME)
  506. ptime = &ctx->check_time;
  507. else
  508. ptime = NULL;
  509. i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  510. if (i == 0)
  511. {
  512. ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  513. ok = ctx->verify_cb(0, ctx);
  514. if (!ok) goto err;
  515. }
  516. if (i > 0)
  517. {
  518. ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
  519. ok = ctx->verify_cb(0, ctx);
  520. if (!ok) goto err;
  521. }
  522. if(X509_CRL_get_nextUpdate(crl))
  523. {
  524. i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  525. if (i == 0)
  526. {
  527. ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  528. ok = ctx->verify_cb(0, ctx);
  529. if (!ok) goto err;
  530. }
  531. if (i < 0)
  532. {
  533. ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
  534. ok = ctx->verify_cb(0, ctx);
  535. if (!ok) goto err;
  536. }
  537. }
  538. ok = 1;
  539. err:
  540. EVP_PKEY_free(ikey);
  541. return ok;
  542. }
  543. /* Check certificate against CRL */
  544. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  545. {
  546. int idx, ok;
  547. X509_REVOKED rtmp;
  548. /* Look for serial number of certificate in CRL */
  549. rtmp.serialNumber = X509_get_serialNumber(x);
  550. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  551. /* Not found: OK */
  552. if(idx == -1) return 1;
  553. /* Otherwise revoked: want something cleverer than
  554. * this to handle entry extensions in V2 CRLs.
  555. */
  556. ctx->error = X509_V_ERR_CERT_REVOKED;
  557. ok = ctx->verify_cb(0, ctx);
  558. return ok;
  559. }
  560. static int internal_verify(X509_STORE_CTX *ctx)
  561. {
  562. int i,ok=0,n;
  563. X509 *xs,*xi;
  564. EVP_PKEY *pkey=NULL;
  565. time_t *ptime;
  566. int (*cb)();
  567. cb=ctx->verify_cb;
  568. n=sk_X509_num(ctx->chain);
  569. ctx->error_depth=n-1;
  570. n--;
  571. xi=sk_X509_value(ctx->chain,n);
  572. if (ctx->flags & X509_V_FLAG_USE_CHECK_TIME)
  573. ptime = &ctx->check_time;
  574. else
  575. ptime = NULL;
  576. if (ctx->check_issued(ctx, xi, xi))
  577. xs=xi;
  578. else
  579. {
  580. if (n <= 0)
  581. {
  582. ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  583. ctx->current_cert=xi;
  584. ok=cb(0,ctx);
  585. goto end;
  586. }
  587. else
  588. {
  589. n--;
  590. ctx->error_depth=n;
  591. xs=sk_X509_value(ctx->chain,n);
  592. }
  593. }
  594. /* ctx->error=0; not needed */
  595. while (n >= 0)
  596. {
  597. ctx->error_depth=n;
  598. if (!xs->valid)
  599. {
  600. if ((pkey=X509_get_pubkey(xi)) == NULL)
  601. {
  602. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  603. ctx->current_cert=xi;
  604. ok=(*cb)(0,ctx);
  605. if (!ok) goto end;
  606. }
  607. if (X509_verify(xs,pkey) <= 0)
  608. /* XXX For the final trusted self-signed cert,
  609. * this is a waste of time. That check should
  610. * optional so that e.g. 'openssl x509' can be
  611. * used to detect invalid self-signatures, but
  612. * we don't verify again and again in SSL
  613. * handshakes and the like once the cert has
  614. * been declared trusted. */
  615. {
  616. ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
  617. ctx->current_cert=xs;
  618. ok=(*cb)(0,ctx);
  619. if (!ok)
  620. {
  621. EVP_PKEY_free(pkey);
  622. goto end;
  623. }
  624. }
  625. EVP_PKEY_free(pkey);
  626. pkey=NULL;
  627. i=X509_cmp_time(X509_get_notBefore(xs), ptime);
  628. if (i == 0)
  629. {
  630. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  631. ctx->current_cert=xs;
  632. ok=(*cb)(0,ctx);
  633. if (!ok) goto end;
  634. }
  635. if (i > 0)
  636. {
  637. ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
  638. ctx->current_cert=xs;
  639. ok=(*cb)(0,ctx);
  640. if (!ok) goto end;
  641. }
  642. xs->valid=1;
  643. }
  644. i=X509_cmp_time(X509_get_notAfter(xs), ptime);
  645. if (i == 0)
  646. {
  647. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  648. ctx->current_cert=xs;
  649. ok=(*cb)(0,ctx);
  650. if (!ok) goto end;
  651. }
  652. if (i < 0)
  653. {
  654. ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
  655. ctx->current_cert=xs;
  656. ok=(*cb)(0,ctx);
  657. if (!ok) goto end;
  658. }
  659. /* The last error (if any) is still in the error value */
  660. ctx->current_cert=xs;
  661. ok=(*cb)(1,ctx);
  662. if (!ok) goto end;
  663. n--;
  664. if (n >= 0)
  665. {
  666. xi=xs;
  667. xs=sk_X509_value(ctx->chain,n);
  668. }
  669. }
  670. ok=1;
  671. end:
  672. return ok;
  673. }
  674. int X509_cmp_current_time(ASN1_TIME *ctm)
  675. {
  676. return X509_cmp_time(ctm, NULL);
  677. }
  678. int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
  679. {
  680. char *str;
  681. ASN1_TIME atm;
  682. long offset;
  683. char buff1[24],buff2[24],*p;
  684. int i,j;
  685. p=buff1;
  686. i=ctm->length;
  687. str=(char *)ctm->data;
  688. if (ctm->type == V_ASN1_UTCTIME)
  689. {
  690. if ((i < 11) || (i > 17)) return 0;
  691. memcpy(p,str,10);
  692. p+=10;
  693. str+=10;
  694. }
  695. else
  696. {
  697. if (i < 13) return 0;
  698. memcpy(p,str,12);
  699. p+=12;
  700. str+=12;
  701. }
  702. if ((*str == 'Z') || (*str == '-') || (*str == '+'))
  703. { *(p++)='0'; *(p++)='0'; }
  704. else
  705. {
  706. *(p++)= *(str++);
  707. *(p++)= *(str++);
  708. /* Skip any fractional seconds... */
  709. if (*str == '.')
  710. {
  711. str++;
  712. while ((*str >= '0') && (*str <= '9')) str++;
  713. }
  714. }
  715. *(p++)='Z';
  716. *(p++)='\0';
  717. if (*str == 'Z')
  718. offset=0;
  719. else
  720. {
  721. if ((*str != '+') && (str[5] != '-'))
  722. return 0;
  723. offset=((str[1]-'0')*10+(str[2]-'0'))*60;
  724. offset+=(str[3]-'0')*10+(str[4]-'0');
  725. if (*str == '-')
  726. offset= -offset;
  727. }
  728. atm.type=ctm->type;
  729. atm.length=sizeof(buff2);
  730. atm.data=(unsigned char *)buff2;
  731. X509_time_adj(&atm,-offset*60, cmp_time);
  732. if (ctm->type == V_ASN1_UTCTIME)
  733. {
  734. i=(buff1[0]-'0')*10+(buff1[1]-'0');
  735. if (i < 50) i+=100; /* cf. RFC 2459 */
  736. j=(buff2[0]-'0')*10+(buff2[1]-'0');
  737. if (j < 50) j+=100;
  738. if (i < j) return -1;
  739. if (i > j) return 1;
  740. }
  741. i=strcmp(buff1,buff2);
  742. if (i == 0) /* wait a second then return younger :-) */
  743. return -1;
  744. else
  745. return i;
  746. }
  747. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  748. {
  749. return X509_time_adj(s, adj, NULL);
  750. }
  751. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
  752. {
  753. time_t t;
  754. int type = -1;
  755. if (in_tm) t = *in_tm;
  756. else time(&t);
  757. t+=adj;
  758. if (s) type = s->type;
  759. if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t);
  760. if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t);
  761. return ASN1_TIME_set(s, t);
  762. }
  763. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  764. {
  765. EVP_PKEY *ktmp=NULL,*ktmp2;
  766. int i,j;
  767. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
  768. for (i=0; i<sk_X509_num(chain); i++)
  769. {
  770. ktmp=X509_get_pubkey(sk_X509_value(chain,i));
  771. if (ktmp == NULL)
  772. {
  773. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  774. return 0;
  775. }
  776. if (!EVP_PKEY_missing_parameters(ktmp))
  777. break;
  778. else
  779. {
  780. EVP_PKEY_free(ktmp);
  781. ktmp=NULL;
  782. }
  783. }
  784. if (ktmp == NULL)
  785. {
  786. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  787. return 0;
  788. }
  789. /* first, populate the other certs */
  790. for (j=i-1; j >= 0; j--)
  791. {
  792. ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
  793. EVP_PKEY_copy_parameters(ktmp2,ktmp);
  794. EVP_PKEY_free(ktmp2);
  795. }
  796. if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
  797. EVP_PKEY_free(ktmp);
  798. return 1;
  799. }
  800. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  801. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  802. {
  803. /* This function is (usually) called only once, by
  804. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
  805. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  806. new_func, dup_func, free_func);
  807. }
  808. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  809. {
  810. return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
  811. }
  812. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  813. {
  814. return CRYPTO_get_ex_data(&ctx->ex_data,idx);
  815. }
  816. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  817. {
  818. return ctx->error;
  819. }
  820. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  821. {
  822. ctx->error=err;
  823. }
  824. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  825. {
  826. return ctx->error_depth;
  827. }
  828. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  829. {
  830. return ctx->current_cert;
  831. }
  832. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  833. {
  834. return ctx->chain;
  835. }
  836. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  837. {
  838. int i;
  839. X509 *x;
  840. STACK_OF(X509) *chain;
  841. if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
  842. for (i = 0; i < sk_X509_num(chain); i++)
  843. {
  844. x = sk_X509_value(chain, i);
  845. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  846. }
  847. return chain;
  848. }
  849. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  850. {
  851. ctx->cert=x;
  852. }
  853. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  854. {
  855. ctx->untrusted=sk;
  856. }
  857. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  858. {
  859. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  860. }
  861. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  862. {
  863. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  864. }
  865. /* This function is used to set the X509_STORE_CTX purpose and trust
  866. * values. This is intended to be used when another structure has its
  867. * own trust and purpose values which (if set) will be inherited by
  868. * the ctx. If they aren't set then we will usually have a default
  869. * purpose in mind which should then be used to set the trust value.
  870. * An example of this is SSL use: an SSL structure will have its own
  871. * purpose and trust settings which the application can set: if they
  872. * aren't set then we use the default of SSL client/server.
  873. */
  874. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  875. int purpose, int trust)
  876. {
  877. int idx;
  878. /* If purpose not set use default */
  879. if (!purpose) purpose = def_purpose;
  880. /* If we have a purpose then check it is valid */
  881. if (purpose)
  882. {
  883. X509_PURPOSE *ptmp;
  884. idx = X509_PURPOSE_get_by_id(purpose);
  885. if (idx == -1)
  886. {
  887. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  888. X509_R_UNKNOWN_PURPOSE_ID);
  889. return 0;
  890. }
  891. ptmp = X509_PURPOSE_get0(idx);
  892. if (ptmp->trust == X509_TRUST_DEFAULT)
  893. {
  894. idx = X509_PURPOSE_get_by_id(def_purpose);
  895. if (idx == -1)
  896. {
  897. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  898. X509_R_UNKNOWN_PURPOSE_ID);
  899. return 0;
  900. }
  901. ptmp = X509_PURPOSE_get0(idx);
  902. }
  903. /* If trust not set then get from purpose default */
  904. if (!trust) trust = ptmp->trust;
  905. }
  906. if (trust)
  907. {
  908. idx = X509_TRUST_get_by_id(trust);
  909. if (idx == -1)
  910. {
  911. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  912. X509_R_UNKNOWN_TRUST_ID);
  913. return 0;
  914. }
  915. }
  916. if (purpose && !ctx->purpose) ctx->purpose = purpose;
  917. if (trust && !ctx->trust) ctx->trust = trust;
  918. return 1;
  919. }
  920. X509_STORE_CTX *X509_STORE_CTX_new(void)
  921. {
  922. X509_STORE_CTX *ctx;
  923. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  924. if (!ctx)
  925. {
  926. X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
  927. return NULL;
  928. }
  929. memset(ctx, 0, sizeof(X509_STORE_CTX));
  930. return ctx;
  931. }
  932. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  933. {
  934. X509_STORE_CTX_cleanup(ctx);
  935. OPENSSL_free(ctx);
  936. }
  937. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  938. STACK_OF(X509) *chain)
  939. {
  940. ctx->ctx=store;
  941. ctx->current_method=0;
  942. ctx->cert=x509;
  943. ctx->untrusted=chain;
  944. ctx->last_untrusted=0;
  945. ctx->check_time=0;
  946. ctx->other_ctx=NULL;
  947. ctx->valid=0;
  948. ctx->chain=NULL;
  949. ctx->depth=9;
  950. ctx->error=0;
  951. ctx->error_depth=0;
  952. ctx->current_cert=NULL;
  953. ctx->current_issuer=NULL;
  954. /* Inherit callbacks and flags from X509_STORE if not set
  955. * use defaults.
  956. */
  957. if (store)
  958. {
  959. ctx->purpose=store->purpose;
  960. ctx->trust=store->trust;
  961. ctx->flags = store->flags;
  962. ctx->cleanup = store->cleanup;
  963. }
  964. else
  965. {
  966. ctx->purpose = 0;
  967. ctx->trust = 0;
  968. ctx->flags = 0;
  969. ctx->cleanup = 0;
  970. }
  971. if (store && store->check_issued)
  972. ctx->check_issued = store->check_issued;
  973. else
  974. ctx->check_issued = check_issued;
  975. if (store && store->get_issuer)
  976. ctx->get_issuer = store->get_issuer;
  977. else
  978. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  979. if (store && store->verify_cb)
  980. ctx->verify_cb = store->verify_cb;
  981. else
  982. ctx->verify_cb = null_callback;
  983. if (store && store->verify)
  984. ctx->verify = store->verify;
  985. else
  986. ctx->verify = internal_verify;
  987. if (store && store->check_revocation)
  988. ctx->check_revocation = store->check_revocation;
  989. else
  990. ctx->check_revocation = check_revocation;
  991. if (store && store->get_crl)
  992. ctx->get_crl = store->get_crl;
  993. else
  994. ctx->get_crl = get_crl;
  995. if (store && store->check_crl)
  996. ctx->check_crl = store->check_crl;
  997. else
  998. ctx->check_crl = check_crl;
  999. if (store && store->cert_crl)
  1000. ctx->cert_crl = store->cert_crl;
  1001. else
  1002. ctx->cert_crl = cert_crl;
  1003. /* This memset() can't make any sense anyway, so it's removed. As
  1004. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  1005. * corresponding "new" here and remove this bogus initialisation. */
  1006. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  1007. if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  1008. &(ctx->ex_data)))
  1009. {
  1010. OPENSSL_free(ctx);
  1011. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1012. return 0;
  1013. }
  1014. return 1;
  1015. }
  1016. /* Set alternative lookup method: just a STACK of trusted certificates.
  1017. * This avoids X509_STORE nastiness where it isn't needed.
  1018. */
  1019. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1020. {
  1021. ctx->other_ctx = sk;
  1022. ctx->get_issuer = get_issuer_sk;
  1023. }
  1024. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  1025. {
  1026. if (ctx->cleanup) ctx->cleanup(ctx);
  1027. if (ctx->chain != NULL)
  1028. {
  1029. sk_X509_pop_free(ctx->chain,X509_free);
  1030. ctx->chain=NULL;
  1031. }
  1032. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  1033. OPENSSL_cleanse(&ctx->ex_data,sizeof(CRYPTO_EX_DATA));
  1034. }
  1035. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, long flags)
  1036. {
  1037. ctx->flags |= flags;
  1038. }
  1039. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, long flags, time_t t)
  1040. {
  1041. ctx->check_time = t;
  1042. ctx->flags |= X509_V_FLAG_USE_CHECK_TIME;
  1043. }
  1044. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  1045. int (*verify_cb)(int, X509_STORE_CTX *))
  1046. {
  1047. ctx->verify_cb=verify_cb;
  1048. }
  1049. IMPLEMENT_STACK_OF(X509)
  1050. IMPLEMENT_ASN1_SET_OF(X509)
  1051. IMPLEMENT_STACK_OF(X509_NAME)
  1052. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  1053. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)