x509_vfy.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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. /* CRL score values */
  71. /* No unhandled critical extensions */
  72. #define CRL_SCORE_NOCRITICAL 0x100
  73. /* certificate is within CRL scope */
  74. #define CRL_SCORE_SCOPE 0x080
  75. /* CRL times valid */
  76. #define CRL_SCORE_TIME 0x040
  77. /* Issuer name matches certificate */
  78. #define CRL_SCORE_ISSUER_NAME 0x020
  79. /* If this score or above CRL is probably valid */
  80. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  81. /* CRL issuer is certificate issuer */
  82. #define CRL_SCORE_ISSUER_CERT 0x018
  83. /* CRL issuer is on certificate path */
  84. #define CRL_SCORE_SAME_PATH 0x008
  85. /* CRL issuer matches CRL AKID */
  86. #define CRL_SCORE_AKID 0x004
  87. /* Have a delta CRL with valid times */
  88. #define CRL_SCORE_TIME_DELTA 0x002
  89. static int null_callback(int ok,X509_STORE_CTX *e);
  90. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  91. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  92. static int check_chain_extensions(X509_STORE_CTX *ctx);
  93. static int check_name_constraints(X509_STORE_CTX *ctx);
  94. static int check_trust(X509_STORE_CTX *ctx);
  95. static int check_revocation(X509_STORE_CTX *ctx);
  96. static int check_cert(X509_STORE_CTX *ctx);
  97. static int check_policy(X509_STORE_CTX *ctx);
  98. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  99. unsigned int *preasons,
  100. X509_CRL *crl, X509 *x);
  101. static int get_crl_delta(X509_STORE_CTX *ctx,
  102. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  103. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score,
  104. X509_CRL *base, STACK_OF(X509_CRL) *crls);
  105. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  106. X509 **pissuer, int *pcrl_score);
  107. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  108. unsigned int *preasons);
  109. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  110. static int check_crl_chain(X509_STORE_CTX *ctx,
  111. STACK_OF(X509) *cert_path,
  112. STACK_OF(X509) *crl_path);
  113. static int internal_verify(X509_STORE_CTX *ctx);
  114. const char X509_version[]="X.509" OPENSSL_VERSION_PTEXT;
  115. static int null_callback(int ok, X509_STORE_CTX *e)
  116. {
  117. return ok;
  118. }
  119. #if 0
  120. static int x509_subject_cmp(X509 **a, X509 **b)
  121. {
  122. return X509_subject_name_cmp(*a,*b);
  123. }
  124. #endif
  125. /* Return 1 is a certificate is self signed */
  126. static int cert_self_signed(X509 *x)
  127. {
  128. X509_check_purpose(x, -1, 0);
  129. if (x->ex_flags & EXFLAG_SS)
  130. return 1;
  131. else
  132. return 0;
  133. }
  134. int X509_verify_cert(X509_STORE_CTX *ctx)
  135. {
  136. X509 *x,*xtmp,*chain_ss=NULL;
  137. int bad_chain = 0;
  138. X509_VERIFY_PARAM *param = ctx->param;
  139. int depth,i,ok=0;
  140. int num;
  141. int (*cb)(int xok,X509_STORE_CTX *xctx);
  142. STACK_OF(X509) *sktmp=NULL;
  143. if (ctx->cert == NULL)
  144. {
  145. X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  146. return -1;
  147. }
  148. cb=ctx->verify_cb;
  149. /* first we make sure the chain we are going to build is
  150. * present and that the first entry is in place */
  151. if (ctx->chain == NULL)
  152. {
  153. if ( ((ctx->chain=sk_X509_new_null()) == NULL) ||
  154. (!sk_X509_push(ctx->chain,ctx->cert)))
  155. {
  156. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  157. goto end;
  158. }
  159. CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
  160. ctx->last_untrusted=1;
  161. }
  162. /* We use a temporary STACK so we can chop and hack at it */
  163. if (ctx->untrusted != NULL
  164. && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
  165. {
  166. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  167. goto end;
  168. }
  169. num=sk_X509_num(ctx->chain);
  170. x=sk_X509_value(ctx->chain,num-1);
  171. depth=param->depth;
  172. for (;;)
  173. {
  174. /* If we have enough, we break */
  175. if (depth < num) break; /* FIXME: If this happens, we should take
  176. * note of it and, if appropriate, use the
  177. * X509_V_ERR_CERT_CHAIN_TOO_LONG error
  178. * code later.
  179. */
  180. /* If we are self signed, we break */
  181. if (cert_self_signed(x))
  182. break;
  183. /* If asked see if we can find issuer in trusted store first */
  184. if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
  185. {
  186. ok = ctx->get_issuer(&xtmp, ctx, x);
  187. if (ok < 0)
  188. return ok;
  189. /* If successful for now free up cert so it
  190. * will be picked up again later.
  191. */
  192. if (ok > 0)
  193. {
  194. X509_free(xtmp);
  195. break;
  196. }
  197. }
  198. /* If we were passed a cert chain, use it first */
  199. if (ctx->untrusted != NULL)
  200. {
  201. xtmp=find_issuer(ctx, sktmp,x);
  202. if (xtmp != NULL)
  203. {
  204. if (!sk_X509_push(ctx->chain,xtmp))
  205. {
  206. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  207. goto end;
  208. }
  209. CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
  210. (void)sk_X509_delete_ptr(sktmp,xtmp);
  211. ctx->last_untrusted++;
  212. x=xtmp;
  213. num++;
  214. /* reparse the full chain for
  215. * the next one */
  216. continue;
  217. }
  218. }
  219. break;
  220. }
  221. /* at this point, chain should contain a list of untrusted
  222. * certificates. We now need to add at least one trusted one,
  223. * if possible, otherwise we complain. */
  224. /* Examine last certificate in chain and see if it
  225. * is self signed.
  226. */
  227. i=sk_X509_num(ctx->chain);
  228. x=sk_X509_value(ctx->chain,i-1);
  229. if (cert_self_signed(x))
  230. {
  231. /* we have a self signed certificate */
  232. if (sk_X509_num(ctx->chain) == 1)
  233. {
  234. /* We have a single self signed certificate: see if
  235. * we can find it in the store. We must have an exact
  236. * match to avoid possible impersonation.
  237. */
  238. ok = ctx->get_issuer(&xtmp, ctx, x);
  239. if ((ok <= 0) || X509_cmp(x, xtmp))
  240. {
  241. ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  242. ctx->current_cert=x;
  243. ctx->error_depth=i-1;
  244. if (ok == 1) X509_free(xtmp);
  245. bad_chain = 1;
  246. ok=cb(0,ctx);
  247. if (!ok) goto end;
  248. }
  249. else
  250. {
  251. /* We have a match: replace certificate with store version
  252. * so we get any trust settings.
  253. */
  254. X509_free(x);
  255. x = xtmp;
  256. (void)sk_X509_set(ctx->chain, i - 1, x);
  257. ctx->last_untrusted=0;
  258. }
  259. }
  260. else
  261. {
  262. /* extract and save self signed certificate for later use */
  263. chain_ss=sk_X509_pop(ctx->chain);
  264. ctx->last_untrusted--;
  265. num--;
  266. x=sk_X509_value(ctx->chain,num-1);
  267. }
  268. }
  269. /* We now lookup certs from the certificate store */
  270. for (;;)
  271. {
  272. /* If we have enough, we break */
  273. if (depth < num) break;
  274. /* If we are self signed, we break */
  275. if (cert_self_signed(x))
  276. break;
  277. ok = ctx->get_issuer(&xtmp, ctx, x);
  278. if (ok < 0) return ok;
  279. if (ok == 0) break;
  280. x = xtmp;
  281. if (!sk_X509_push(ctx->chain,x))
  282. {
  283. X509_free(xtmp);
  284. X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
  285. return 0;
  286. }
  287. num++;
  288. }
  289. /* we now have our chain, lets check it... */
  290. i = check_trust(ctx);
  291. /* If explicitly rejected error */
  292. if (i == X509_TRUST_REJECTED)
  293. goto end;
  294. /* If not explicitly trusted then indicate error */
  295. if (i != X509_TRUST_TRUSTED)
  296. {
  297. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
  298. {
  299. if (ctx->last_untrusted >= num)
  300. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  301. else
  302. ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  303. ctx->current_cert=x;
  304. }
  305. else
  306. {
  307. sk_X509_push(ctx->chain,chain_ss);
  308. num++;
  309. ctx->last_untrusted=num;
  310. ctx->current_cert=chain_ss;
  311. ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  312. chain_ss=NULL;
  313. }
  314. ctx->error_depth=num-1;
  315. bad_chain = 1;
  316. ok=cb(0,ctx);
  317. if (!ok) goto end;
  318. }
  319. /* We have the chain complete: now we need to check its purpose */
  320. ok = check_chain_extensions(ctx);
  321. if (!ok) goto end;
  322. /* Check name constraints */
  323. ok = check_name_constraints(ctx);
  324. if (!ok) goto end;
  325. /* We may as well copy down any DSA parameters that are required */
  326. X509_get_pubkey_parameters(NULL,ctx->chain);
  327. /* Check revocation status: we do this after copying parameters
  328. * because they may be needed for CRL signature verification.
  329. */
  330. ok = ctx->check_revocation(ctx);
  331. if(!ok) goto end;
  332. /* At this point, we have a chain and need to verify it */
  333. if (ctx->verify != NULL)
  334. ok=ctx->verify(ctx);
  335. else
  336. ok=internal_verify(ctx);
  337. if(!ok) goto end;
  338. #ifndef OPENSSL_NO_RFC3779
  339. /* RFC 3779 path validation, now that CRL check has been done */
  340. ok = v3_asid_validate_path(ctx);
  341. if (!ok) goto end;
  342. ok = v3_addr_validate_path(ctx);
  343. if (!ok) goto end;
  344. #endif
  345. /* If we get this far evaluate policies */
  346. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  347. ok = ctx->check_policy(ctx);
  348. if(!ok) goto end;
  349. if (0)
  350. {
  351. end:
  352. X509_get_pubkey_parameters(NULL,ctx->chain);
  353. }
  354. if (sktmp != NULL) sk_X509_free(sktmp);
  355. if (chain_ss != NULL) X509_free(chain_ss);
  356. return ok;
  357. }
  358. /* Given a STACK_OF(X509) find the issuer of cert (if any)
  359. */
  360. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  361. {
  362. int i;
  363. X509 *issuer;
  364. for (i = 0; i < sk_X509_num(sk); i++)
  365. {
  366. issuer = sk_X509_value(sk, i);
  367. if (ctx->check_issued(ctx, x, issuer))
  368. return issuer;
  369. }
  370. return NULL;
  371. }
  372. /* Given a possible certificate and issuer check them */
  373. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  374. {
  375. int ret;
  376. ret = X509_check_issued(issuer, x);
  377. if (ret == X509_V_OK)
  378. {
  379. int i;
  380. X509 *ch;
  381. for (i = 0; i < sk_X509_num(ctx->chain); i++)
  382. {
  383. ch = sk_X509_value(ctx->chain, i);
  384. if (ch == issuer || !X509_cmp(ch, issuer))
  385. {
  386. ret = X509_V_ERR_PATH_LOOP;
  387. break;
  388. }
  389. }
  390. }
  391. if (ret == X509_V_OK)
  392. return 1;
  393. /* If we haven't asked for issuer errors don't set ctx */
  394. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  395. return 0;
  396. ctx->error = ret;
  397. ctx->current_cert = x;
  398. ctx->current_issuer = issuer;
  399. return ctx->verify_cb(0, ctx);
  400. return 0;
  401. }
  402. /* Alternative lookup method: look from a STACK stored in other_ctx */
  403. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  404. {
  405. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  406. if (*issuer)
  407. {
  408. CRYPTO_add(&(*issuer)->references,1,CRYPTO_LOCK_X509);
  409. return 1;
  410. }
  411. else
  412. return 0;
  413. }
  414. /* Check a certificate chains extensions for consistency
  415. * with the supplied purpose
  416. */
  417. static int check_chain_extensions(X509_STORE_CTX *ctx)
  418. {
  419. #ifdef OPENSSL_NO_CHAIN_VERIFY
  420. return 1;
  421. #else
  422. int i, ok=0, must_be_ca, plen = 0;
  423. X509 *x;
  424. int (*cb)(int xok,X509_STORE_CTX *xctx);
  425. int proxy_path_length = 0;
  426. int purpose;
  427. int allow_proxy_certs;
  428. cb=ctx->verify_cb;
  429. /* must_be_ca can have 1 of 3 values:
  430. -1: we accept both CA and non-CA certificates, to allow direct
  431. use of self-signed certificates (which are marked as CA).
  432. 0: we only accept non-CA certificates. This is currently not
  433. used, but the possibility is present for future extensions.
  434. 1: we only accept CA certificates. This is currently used for
  435. all certificates in the chain except the leaf certificate.
  436. */
  437. must_be_ca = -1;
  438. /* CRL path validation */
  439. if (ctx->parent)
  440. {
  441. allow_proxy_certs = 0;
  442. purpose = X509_PURPOSE_CRL_SIGN;
  443. }
  444. else
  445. {
  446. allow_proxy_certs =
  447. !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  448. /* A hack to keep people who don't want to modify their
  449. software happy */
  450. if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
  451. allow_proxy_certs = 1;
  452. purpose = ctx->param->purpose;
  453. }
  454. /* Check all untrusted certificates */
  455. for (i = 0; i < ctx->last_untrusted; i++)
  456. {
  457. int ret;
  458. x = sk_X509_value(ctx->chain, i);
  459. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  460. && (x->ex_flags & EXFLAG_CRITICAL))
  461. {
  462. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  463. ctx->error_depth = i;
  464. ctx->current_cert = x;
  465. ok=cb(0,ctx);
  466. if (!ok) goto end;
  467. }
  468. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))
  469. {
  470. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  471. ctx->error_depth = i;
  472. ctx->current_cert = x;
  473. ok=cb(0,ctx);
  474. if (!ok) goto end;
  475. }
  476. ret = X509_check_ca(x);
  477. switch(must_be_ca)
  478. {
  479. case -1:
  480. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  481. && (ret != 1) && (ret != 0))
  482. {
  483. ret = 0;
  484. ctx->error = X509_V_ERR_INVALID_CA;
  485. }
  486. else
  487. ret = 1;
  488. break;
  489. case 0:
  490. if (ret != 0)
  491. {
  492. ret = 0;
  493. ctx->error = X509_V_ERR_INVALID_NON_CA;
  494. }
  495. else
  496. ret = 1;
  497. break;
  498. default:
  499. if ((ret == 0)
  500. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  501. && (ret != 1)))
  502. {
  503. ret = 0;
  504. ctx->error = X509_V_ERR_INVALID_CA;
  505. }
  506. else
  507. ret = 1;
  508. break;
  509. }
  510. if (ret == 0)
  511. {
  512. ctx->error_depth = i;
  513. ctx->current_cert = x;
  514. ok=cb(0,ctx);
  515. if (!ok) goto end;
  516. }
  517. if (ctx->param->purpose > 0)
  518. {
  519. ret = X509_check_purpose(x, purpose, must_be_ca > 0);
  520. if ((ret == 0)
  521. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  522. && (ret != 1)))
  523. {
  524. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  525. ctx->error_depth = i;
  526. ctx->current_cert = x;
  527. ok=cb(0,ctx);
  528. if (!ok) goto end;
  529. }
  530. }
  531. /* Check pathlen if not self issued */
  532. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  533. && (x->ex_pathlen != -1)
  534. && (plen > (x->ex_pathlen + proxy_path_length + 1)))
  535. {
  536. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  537. ctx->error_depth = i;
  538. ctx->current_cert = x;
  539. ok=cb(0,ctx);
  540. if (!ok) goto end;
  541. }
  542. /* Increment path length if not self issued */
  543. if (!(x->ex_flags & EXFLAG_SI))
  544. plen++;
  545. /* If this certificate is a proxy certificate, the next
  546. certificate must be another proxy certificate or a EE
  547. certificate. If not, the next certificate must be a
  548. CA certificate. */
  549. if (x->ex_flags & EXFLAG_PROXY)
  550. {
  551. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)
  552. {
  553. ctx->error =
  554. X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  555. ctx->error_depth = i;
  556. ctx->current_cert = x;
  557. ok=cb(0,ctx);
  558. if (!ok) goto end;
  559. }
  560. proxy_path_length++;
  561. must_be_ca = 0;
  562. }
  563. else
  564. must_be_ca = 1;
  565. }
  566. ok = 1;
  567. end:
  568. return ok;
  569. #endif
  570. }
  571. static int check_name_constraints(X509_STORE_CTX *ctx)
  572. {
  573. X509 *x;
  574. int i, j, rv;
  575. /* Check name constraints for all certificates */
  576. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--)
  577. {
  578. x = sk_X509_value(ctx->chain, i);
  579. /* Ignore self issued certs unless last in chain */
  580. if (i && (x->ex_flags & EXFLAG_SI))
  581. continue;
  582. /* Check against constraints for all certificates higher in
  583. * chain including trust anchor. Trust anchor not strictly
  584. * speaking needed but if it includes constraints it is to be
  585. * assumed it expects them to be obeyed.
  586. */
  587. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--)
  588. {
  589. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  590. if (nc)
  591. {
  592. rv = NAME_CONSTRAINTS_check(x, nc);
  593. if (rv != X509_V_OK)
  594. {
  595. ctx->error = rv;
  596. ctx->error_depth = i;
  597. ctx->current_cert = x;
  598. if (!ctx->verify_cb(0,ctx))
  599. return 0;
  600. }
  601. }
  602. }
  603. }
  604. return 1;
  605. }
  606. static int check_trust(X509_STORE_CTX *ctx)
  607. {
  608. int i, ok;
  609. X509 *x = NULL;
  610. int (*cb)(int xok,X509_STORE_CTX *xctx);
  611. cb=ctx->verify_cb;
  612. /* Check all trusted certificates in chain */
  613. for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++)
  614. {
  615. x = sk_X509_value(ctx->chain, i);
  616. ok = X509_check_trust(x, ctx->param->trust, 0);
  617. /* If explicitly trusted return trusted */
  618. if (ok == X509_TRUST_TRUSTED)
  619. return X509_TRUST_TRUSTED;
  620. /* If explicitly rejected notify callback and reject if
  621. * not overridden.
  622. */
  623. if (ok == X509_TRUST_REJECTED)
  624. {
  625. ctx->error_depth = i;
  626. ctx->current_cert = x;
  627. ctx->error = X509_V_ERR_CERT_REJECTED;
  628. ok = cb(0, ctx);
  629. if (!ok)
  630. return X509_TRUST_REJECTED;
  631. }
  632. }
  633. /* If no trusted certs in chain at all return untrusted and
  634. * allow standard (no issuer cert) etc errors to be indicated.
  635. */
  636. return X509_TRUST_UNTRUSTED;
  637. }
  638. static int check_revocation(X509_STORE_CTX *ctx)
  639. {
  640. int i, last, ok;
  641. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  642. return 1;
  643. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  644. last = sk_X509_num(ctx->chain) - 1;
  645. else
  646. {
  647. /* If checking CRL paths this isn't the EE certificate */
  648. if (ctx->parent)
  649. return 1;
  650. last = 0;
  651. }
  652. for(i = 0; i <= last; i++)
  653. {
  654. ctx->error_depth = i;
  655. ok = check_cert(ctx);
  656. if (!ok) return ok;
  657. }
  658. return 1;
  659. }
  660. static int check_cert(X509_STORE_CTX *ctx)
  661. {
  662. X509_CRL *crl = NULL, *dcrl = NULL;
  663. X509 *x;
  664. int ok, cnum;
  665. cnum = ctx->error_depth;
  666. x = sk_X509_value(ctx->chain, cnum);
  667. ctx->current_cert = x;
  668. ctx->current_issuer = NULL;
  669. ctx->current_crl_score = 0;
  670. ctx->current_reasons = 0;
  671. while (ctx->current_reasons != CRLDP_ALL_REASONS)
  672. {
  673. /* Try to retrieve relevant CRL */
  674. if (ctx->get_crl)
  675. ok = ctx->get_crl(ctx, &crl, x);
  676. else
  677. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  678. /* If error looking up CRL, nothing we can do except
  679. * notify callback
  680. */
  681. if(!ok)
  682. {
  683. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  684. ok = ctx->verify_cb(0, ctx);
  685. goto err;
  686. }
  687. ctx->current_crl = crl;
  688. ok = ctx->check_crl(ctx, crl);
  689. if (!ok)
  690. goto err;
  691. if (dcrl)
  692. {
  693. ok = ctx->check_crl(ctx, dcrl);
  694. if (!ok)
  695. goto err;
  696. ok = ctx->cert_crl(ctx, dcrl, x);
  697. if (!ok)
  698. goto err;
  699. }
  700. else
  701. ok = 1;
  702. /* Don't look in full CRL if delta reason is removefromCRL */
  703. if (ok != 2)
  704. {
  705. ok = ctx->cert_crl(ctx, crl, x);
  706. if (!ok)
  707. goto err;
  708. }
  709. X509_CRL_free(crl);
  710. X509_CRL_free(dcrl);
  711. crl = NULL;
  712. dcrl = NULL;
  713. }
  714. err:
  715. X509_CRL_free(crl);
  716. X509_CRL_free(dcrl);
  717. ctx->current_crl = NULL;
  718. return ok;
  719. }
  720. /* Check CRL times against values in X509_STORE_CTX */
  721. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  722. {
  723. time_t *ptime;
  724. int i;
  725. if (notify)
  726. ctx->current_crl = crl;
  727. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  728. ptime = &ctx->param->check_time;
  729. else
  730. ptime = NULL;
  731. i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  732. if (i == 0)
  733. {
  734. if (!notify)
  735. return 0;
  736. ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  737. if (!ctx->verify_cb(0, ctx))
  738. return 0;
  739. }
  740. if (i > 0)
  741. {
  742. if (!notify)
  743. return 0;
  744. ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
  745. if (!ctx->verify_cb(0, ctx))
  746. return 0;
  747. }
  748. if(X509_CRL_get_nextUpdate(crl))
  749. {
  750. i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  751. if (i == 0)
  752. {
  753. if (!notify)
  754. return 0;
  755. ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  756. if (!ctx->verify_cb(0, ctx))
  757. return 0;
  758. }
  759. /* Ignore expiry of base CRL is delta is valid */
  760. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA))
  761. {
  762. if (!notify)
  763. return 0;
  764. ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
  765. if (!ctx->verify_cb(0, ctx))
  766. return 0;
  767. }
  768. }
  769. if (notify)
  770. ctx->current_crl = NULL;
  771. return 1;
  772. }
  773. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  774. X509 **pissuer, int *pscore, unsigned int *preasons,
  775. STACK_OF(X509_CRL) *crls)
  776. {
  777. int i, crl_score, best_score = *pscore;
  778. unsigned int reasons, best_reasons = 0;
  779. X509 *x = ctx->current_cert;
  780. X509_CRL *crl, *best_crl = NULL;
  781. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  782. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  783. {
  784. crl = sk_X509_CRL_value(crls, i);
  785. reasons = *preasons;
  786. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  787. if (crl_score > best_score)
  788. {
  789. best_crl = crl;
  790. best_crl_issuer = crl_issuer;
  791. best_score = crl_score;
  792. best_reasons = reasons;
  793. }
  794. }
  795. if (best_crl)
  796. {
  797. if (*pcrl)
  798. X509_CRL_free(*pcrl);
  799. *pcrl = best_crl;
  800. *pissuer = best_crl_issuer;
  801. *pscore = best_score;
  802. *preasons = best_reasons;
  803. CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
  804. if (*pdcrl)
  805. {
  806. X509_CRL_free(*pdcrl);
  807. *pdcrl = NULL;
  808. }
  809. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  810. }
  811. if (best_score >= CRL_SCORE_VALID)
  812. return 1;
  813. return 0;
  814. }
  815. /* Compare two CRL extensions for delta checking purposes. They should be
  816. * both present or both absent. If both present all fields must be identical.
  817. */
  818. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  819. {
  820. ASN1_OCTET_STRING *exta, *extb;
  821. int i;
  822. i = X509_CRL_get_ext_by_NID(a, nid, 0);
  823. if (i >= 0)
  824. {
  825. /* Can't have multiple occurrences */
  826. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  827. return 0;
  828. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  829. }
  830. else
  831. exta = NULL;
  832. i = X509_CRL_get_ext_by_NID(b, nid, 0);
  833. if (i >= 0)
  834. {
  835. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  836. return 0;
  837. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  838. }
  839. else
  840. extb = NULL;
  841. if (!exta && !extb)
  842. return 1;
  843. if (!exta || !extb)
  844. return 0;
  845. if (ASN1_OCTET_STRING_cmp(exta, extb))
  846. return 0;
  847. return 1;
  848. }
  849. /* See if a base and delta are compatible */
  850. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  851. {
  852. /* Delta CRL must be a delta */
  853. if (!delta->base_crl_number)
  854. return 0;
  855. /* Base must have a CRL number */
  856. if (!base->crl_number)
  857. return 0;
  858. /* Issuer names must match */
  859. if (X509_NAME_cmp(X509_CRL_get_issuer(base),
  860. X509_CRL_get_issuer(delta)))
  861. return 0;
  862. /* AKID and IDP must match */
  863. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  864. return 0;
  865. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  866. return 0;
  867. /* Delta CRL base number must not exceed Full CRL number. */
  868. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  869. return 0;
  870. /* Delta CRL number must exceed full CRL number */
  871. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  872. return 1;
  873. return 0;
  874. }
  875. /* For a given base CRL find a delta... maybe extend to delta scoring
  876. * or retrieve a chain of deltas...
  877. */
  878. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  879. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  880. {
  881. X509_CRL *delta;
  882. int i;
  883. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  884. return;
  885. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  886. return;
  887. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  888. {
  889. delta = sk_X509_CRL_value(crls, i);
  890. if (check_delta_base(delta, base))
  891. {
  892. if (check_crl_time(ctx, delta, 0))
  893. *pscore |= CRL_SCORE_TIME_DELTA;
  894. CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
  895. *dcrl = delta;
  896. return;
  897. }
  898. }
  899. *dcrl = NULL;
  900. }
  901. /* For a given CRL return how suitable it is for the supplied certificate 'x'.
  902. * The return value is a mask of several criteria.
  903. * If the issuer is not the certificate issuer this is returned in *pissuer.
  904. * The reasons mask is also used to determine if the CRL is suitable: if
  905. * no new reasons the CRL is rejected, otherwise reasons is updated.
  906. */
  907. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  908. unsigned int *preasons,
  909. X509_CRL *crl, X509 *x)
  910. {
  911. int crl_score = 0;
  912. unsigned int tmp_reasons = *preasons, crl_reasons;
  913. /* First see if we can reject CRL straight away */
  914. /* Invalid IDP cannot be processed */
  915. if (crl->idp_flags & IDP_INVALID)
  916. return 0;
  917. /* Reason codes or indirect CRLs need extended CRL support */
  918. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  919. {
  920. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  921. return 0;
  922. }
  923. else if (crl->idp_flags & IDP_REASONS)
  924. {
  925. /* If no new reasons reject */
  926. if (!(crl->idp_reasons & ~tmp_reasons))
  927. return 0;
  928. }
  929. /* Don't process deltas at this stage */
  930. else if (crl->base_crl_number)
  931. return 0;
  932. /* If issuer name doesn't match certificate need indirect CRL */
  933. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl)))
  934. {
  935. if (!(crl->idp_flags & IDP_INDIRECT))
  936. return 0;
  937. }
  938. else
  939. crl_score |= CRL_SCORE_ISSUER_NAME;
  940. if (!(crl->flags & EXFLAG_CRITICAL))
  941. crl_score |= CRL_SCORE_NOCRITICAL;
  942. /* Check expiry */
  943. if (check_crl_time(ctx, crl, 0))
  944. crl_score |= CRL_SCORE_TIME;
  945. /* Check authority key ID and locate certificate issuer */
  946. crl_akid_check(ctx, crl, pissuer, &crl_score);
  947. /* If we can't locate certificate issuer at this point forget it */
  948. if (!(crl_score & CRL_SCORE_AKID))
  949. return 0;
  950. /* Check cert for matching CRL distribution points */
  951. if (crl_crldp_check(x, crl, crl_score, &crl_reasons))
  952. {
  953. /* If no new reasons reject */
  954. if (!(crl_reasons & ~tmp_reasons))
  955. return 0;
  956. tmp_reasons |= crl_reasons;
  957. crl_score |= CRL_SCORE_SCOPE;
  958. }
  959. *preasons = tmp_reasons;
  960. return crl_score;
  961. }
  962. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  963. X509 **pissuer, int *pcrl_score)
  964. {
  965. X509 *crl_issuer = NULL;
  966. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  967. int cidx = ctx->error_depth;
  968. int i;
  969. if (cidx != sk_X509_num(ctx->chain) - 1)
  970. cidx++;
  971. crl_issuer = sk_X509_value(ctx->chain, cidx);
  972. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
  973. {
  974. if (*pcrl_score & CRL_SCORE_ISSUER_NAME)
  975. {
  976. *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
  977. *pissuer = crl_issuer;
  978. return;
  979. }
  980. }
  981. for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++)
  982. {
  983. crl_issuer = sk_X509_value(ctx->chain, cidx);
  984. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  985. continue;
  986. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
  987. {
  988. *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
  989. *pissuer = crl_issuer;
  990. return;
  991. }
  992. }
  993. /* Anything else needs extended CRL support */
  994. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  995. return;
  996. /* Otherwise the CRL issuer is not on the path. Look for it in the
  997. * set of untrusted certificates.
  998. */
  999. for (i = 0; i < sk_X509_num(ctx->untrusted); i++)
  1000. {
  1001. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1002. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1003. continue;
  1004. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
  1005. {
  1006. *pissuer = crl_issuer;
  1007. *pcrl_score |= CRL_SCORE_AKID;
  1008. return;
  1009. }
  1010. }
  1011. }
  1012. /* Check the path of a CRL issuer certificate. This creates a new
  1013. * X509_STORE_CTX and populates it with most of the parameters from the
  1014. * parent. This could be optimised somewhat since a lot of path checking
  1015. * will be duplicated by the parent, but this will rarely be used in
  1016. * practice.
  1017. */
  1018. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1019. {
  1020. X509_STORE_CTX crl_ctx;
  1021. int ret;
  1022. /* Don't allow recursive CRL path validation */
  1023. if (ctx->parent)
  1024. return 0;
  1025. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1026. return -1;
  1027. crl_ctx.crls = ctx->crls;
  1028. /* Copy verify params across */
  1029. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1030. crl_ctx.parent = ctx;
  1031. crl_ctx.verify_cb = ctx->verify_cb;
  1032. /* Verify CRL issuer */
  1033. ret = X509_verify_cert(&crl_ctx);
  1034. if (ret <= 0)
  1035. goto err;
  1036. /* Check chain is acceptable */
  1037. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1038. err:
  1039. X509_STORE_CTX_cleanup(&crl_ctx);
  1040. return ret;
  1041. }
  1042. /* RFC3280 says nothing about the relationship between CRL path
  1043. * and certificate path, which could lead to situations where a
  1044. * certificate could be revoked or validated by a CA not authorised
  1045. * to do so. RFC5280 is more strict and states that the two paths must
  1046. * end in the same trust anchor, though some discussions remain...
  1047. * until this is resolved we use the RFC5280 version
  1048. */
  1049. static int check_crl_chain(X509_STORE_CTX *ctx,
  1050. STACK_OF(X509) *cert_path,
  1051. STACK_OF(X509) *crl_path)
  1052. {
  1053. X509 *cert_ta, *crl_ta;
  1054. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1055. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1056. if (!X509_cmp(cert_ta, crl_ta))
  1057. return 1;
  1058. return 0;
  1059. }
  1060. /* Check for match between two dist point names: three separate cases.
  1061. * 1. Both are relative names and compare X509_NAME types.
  1062. * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
  1063. * 3. Both are full names and compare two GENERAL_NAMES.
  1064. * 4. One is NULL: automatic match.
  1065. */
  1066. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1067. {
  1068. X509_NAME *nm = NULL;
  1069. GENERAL_NAMES *gens = NULL;
  1070. GENERAL_NAME *gena, *genb;
  1071. int i, j;
  1072. if (!a || !b)
  1073. return 1;
  1074. if (a->type == 1)
  1075. {
  1076. if (!a->dpname)
  1077. return 0;
  1078. /* Case 1: two X509_NAME */
  1079. if (b->type == 1)
  1080. {
  1081. if (!b->dpname)
  1082. return 0;
  1083. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1084. return 1;
  1085. else
  1086. return 0;
  1087. }
  1088. /* Case 2: set name and GENERAL_NAMES appropriately */
  1089. nm = a->dpname;
  1090. gens = b->name.fullname;
  1091. }
  1092. else if (b->type == 1)
  1093. {
  1094. if (!b->dpname)
  1095. return 0;
  1096. /* Case 2: set name and GENERAL_NAMES appropriately */
  1097. gens = a->name.fullname;
  1098. nm = b->dpname;
  1099. }
  1100. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1101. if (nm)
  1102. {
  1103. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
  1104. {
  1105. gena = sk_GENERAL_NAME_value(gens, i);
  1106. if (gena->type != GEN_DIRNAME)
  1107. continue;
  1108. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1109. return 1;
  1110. }
  1111. return 0;
  1112. }
  1113. /* Else case 3: two GENERAL_NAMES */
  1114. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++)
  1115. {
  1116. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1117. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++)
  1118. {
  1119. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1120. if (!GENERAL_NAME_cmp(gena, genb))
  1121. return 1;
  1122. }
  1123. }
  1124. return 0;
  1125. }
  1126. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1127. {
  1128. int i;
  1129. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1130. /* If no CRLissuer return is successful iff don't need a match */
  1131. if (!dp->CRLissuer)
  1132. return !!(crl_score & CRL_SCORE_ISSUER_NAME);
  1133. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++)
  1134. {
  1135. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1136. if (gen->type != GEN_DIRNAME)
  1137. continue;
  1138. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1139. return 1;
  1140. }
  1141. return 0;
  1142. }
  1143. /* Check CRLDP and IDP */
  1144. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1145. unsigned int *preasons)
  1146. {
  1147. int i;
  1148. if (crl->idp_flags & IDP_ONLYATTR)
  1149. return 0;
  1150. if (x->ex_flags & EXFLAG_CA)
  1151. {
  1152. if (crl->idp_flags & IDP_ONLYUSER)
  1153. return 0;
  1154. }
  1155. else
  1156. {
  1157. if (crl->idp_flags & IDP_ONLYCA)
  1158. return 0;
  1159. }
  1160. *preasons = crl->idp_reasons;
  1161. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
  1162. {
  1163. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1164. if (crldp_check_crlissuer(dp, crl, crl_score))
  1165. {
  1166. if (!crl->idp ||
  1167. idp_check_dp(dp->distpoint, crl->idp->distpoint))
  1168. {
  1169. *preasons &= dp->dp_reasons;
  1170. return 1;
  1171. }
  1172. }
  1173. }
  1174. if ((!crl->idp || !crl->idp->distpoint) && (crl_score & CRL_SCORE_ISSUER_NAME))
  1175. return 1;
  1176. return 0;
  1177. }
  1178. /* Retrieve CRL corresponding to current certificate.
  1179. * If deltas enabled try to find a delta CRL too
  1180. */
  1181. static int get_crl_delta(X509_STORE_CTX *ctx,
  1182. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1183. {
  1184. int ok;
  1185. X509 *issuer = NULL;
  1186. int crl_score = 0;
  1187. unsigned int reasons;
  1188. X509_CRL *crl = NULL, *dcrl = NULL;
  1189. STACK_OF(X509_CRL) *skcrl;
  1190. X509_NAME *nm = X509_get_issuer_name(x);
  1191. reasons = ctx->current_reasons;
  1192. ok = get_crl_sk(ctx, &crl, &dcrl,
  1193. &issuer, &crl_score, &reasons, ctx->crls);
  1194. if (ok)
  1195. goto done;
  1196. /* Lookup CRLs from store */
  1197. skcrl = ctx->lookup_crls(ctx, nm);
  1198. /* If no CRLs found and a near match from get_crl_sk use that */
  1199. if (!skcrl && crl)
  1200. goto done;
  1201. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1202. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1203. done:
  1204. /* If we got any kind of CRL use it and return success */
  1205. if (crl)
  1206. {
  1207. ctx->current_issuer = issuer;
  1208. ctx->current_crl_score = crl_score;
  1209. ctx->current_reasons = reasons;
  1210. *pcrl = crl;
  1211. *pdcrl = dcrl;
  1212. return 1;
  1213. }
  1214. return 0;
  1215. }
  1216. /* Check CRL validity */
  1217. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1218. {
  1219. X509 *issuer = NULL;
  1220. EVP_PKEY *ikey = NULL;
  1221. int ok = 0, chnum, cnum;
  1222. cnum = ctx->error_depth;
  1223. chnum = sk_X509_num(ctx->chain) - 1;
  1224. /* if we have an alternative CRL issuer cert use that */
  1225. if (ctx->current_issuer)
  1226. issuer = ctx->current_issuer;
  1227. /* Else find CRL issuer: if not last certificate then issuer
  1228. * is next certificate in chain.
  1229. */
  1230. else if (cnum < chnum)
  1231. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1232. else
  1233. {
  1234. issuer = sk_X509_value(ctx->chain, chnum);
  1235. /* If not self signed, can't check signature */
  1236. if(!ctx->check_issued(ctx, issuer, issuer))
  1237. {
  1238. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  1239. ok = ctx->verify_cb(0, ctx);
  1240. if(!ok) goto err;
  1241. }
  1242. }
  1243. if(issuer)
  1244. {
  1245. /* Skip most tests for deltas because they have already
  1246. * been done
  1247. */
  1248. if (!crl->base_crl_number)
  1249. {
  1250. /* Check for cRLSign bit if keyUsage present */
  1251. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1252. !(issuer->ex_kusage & KU_CRL_SIGN))
  1253. {
  1254. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  1255. ok = ctx->verify_cb(0, ctx);
  1256. if(!ok) goto err;
  1257. }
  1258. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE))
  1259. {
  1260. ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
  1261. ok = ctx->verify_cb(0, ctx);
  1262. if(!ok) goto err;
  1263. }
  1264. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH))
  1265. {
  1266. if (check_crl_path(ctx, ctx->current_issuer) <= 0)
  1267. {
  1268. ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  1269. ok = ctx->verify_cb(0, ctx);
  1270. if(!ok) goto err;
  1271. }
  1272. }
  1273. if (crl->idp_flags & IDP_INVALID)
  1274. {
  1275. ctx->error = X509_V_ERR_INVALID_EXTENSION;
  1276. ok = ctx->verify_cb(0, ctx);
  1277. if(!ok) goto err;
  1278. }
  1279. }
  1280. if (!(ctx->current_crl_score & CRL_SCORE_TIME))
  1281. {
  1282. ok = check_crl_time(ctx, crl, 1);
  1283. if (!ok)
  1284. goto err;
  1285. }
  1286. /* Attempt to get issuer certificate public key */
  1287. ikey = X509_get_pubkey(issuer);
  1288. if(!ikey)
  1289. {
  1290. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1291. ok = ctx->verify_cb(0, ctx);
  1292. if (!ok) goto err;
  1293. }
  1294. else
  1295. {
  1296. /* Verify CRL signature */
  1297. if(X509_CRL_verify(crl, ikey) <= 0)
  1298. {
  1299. ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
  1300. ok = ctx->verify_cb(0, ctx);
  1301. if (!ok) goto err;
  1302. }
  1303. }
  1304. }
  1305. ok = 1;
  1306. err:
  1307. EVP_PKEY_free(ikey);
  1308. return ok;
  1309. }
  1310. /* Check certificate against CRL */
  1311. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1312. {
  1313. int ok;
  1314. X509_REVOKED *rev;
  1315. /* The rules changed for this... previously if a CRL contained
  1316. * unhandled critical extensions it could still be used to indicate
  1317. * a certificate was revoked. This has since been changed since
  1318. * critical extension can change the meaning of CRL entries.
  1319. */
  1320. if (crl->flags & EXFLAG_CRITICAL)
  1321. {
  1322. if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1323. return 1;
  1324. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  1325. ok = ctx->verify_cb(0, ctx);
  1326. if(!ok)
  1327. return 0;
  1328. }
  1329. /* Look for serial number of certificate in CRL
  1330. * If found make sure reason is not removeFromCRL.
  1331. */
  1332. if (X509_CRL_get0_by_cert(crl, &rev, x))
  1333. {
  1334. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1335. return 2;
  1336. ctx->error = X509_V_ERR_CERT_REVOKED;
  1337. ok = ctx->verify_cb(0, ctx);
  1338. if (!ok)
  1339. return 0;
  1340. }
  1341. return 1;
  1342. }
  1343. static int check_policy(X509_STORE_CTX *ctx)
  1344. {
  1345. int ret;
  1346. if (ctx->parent)
  1347. return 1;
  1348. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1349. ctx->param->policies, ctx->param->flags);
  1350. if (ret == 0)
  1351. {
  1352. X509err(X509_F_CHECK_POLICY,ERR_R_MALLOC_FAILURE);
  1353. return 0;
  1354. }
  1355. /* Invalid or inconsistent extensions */
  1356. if (ret == -1)
  1357. {
  1358. /* Locate certificates with bad extensions and notify
  1359. * callback.
  1360. */
  1361. X509 *x;
  1362. int i;
  1363. for (i = 1; i < sk_X509_num(ctx->chain); i++)
  1364. {
  1365. x = sk_X509_value(ctx->chain, i);
  1366. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1367. continue;
  1368. ctx->current_cert = x;
  1369. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  1370. if(!ctx->verify_cb(0, ctx))
  1371. return 0;
  1372. }
  1373. return 1;
  1374. }
  1375. if (ret == -2)
  1376. {
  1377. ctx->current_cert = NULL;
  1378. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1379. return ctx->verify_cb(0, ctx);
  1380. }
  1381. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY)
  1382. {
  1383. ctx->current_cert = NULL;
  1384. ctx->error = X509_V_OK;
  1385. if (!ctx->verify_cb(2, ctx))
  1386. return 0;
  1387. }
  1388. return 1;
  1389. }
  1390. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  1391. {
  1392. time_t *ptime;
  1393. int i;
  1394. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1395. ptime = &ctx->param->check_time;
  1396. else
  1397. ptime = NULL;
  1398. i=X509_cmp_time(X509_get_notBefore(x), ptime);
  1399. if (i == 0)
  1400. {
  1401. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  1402. ctx->current_cert=x;
  1403. if (!ctx->verify_cb(0, ctx))
  1404. return 0;
  1405. }
  1406. if (i > 0)
  1407. {
  1408. ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
  1409. ctx->current_cert=x;
  1410. if (!ctx->verify_cb(0, ctx))
  1411. return 0;
  1412. }
  1413. i=X509_cmp_time(X509_get_notAfter(x), ptime);
  1414. if (i == 0)
  1415. {
  1416. ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  1417. ctx->current_cert=x;
  1418. if (!ctx->verify_cb(0, ctx))
  1419. return 0;
  1420. }
  1421. if (i < 0)
  1422. {
  1423. ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
  1424. ctx->current_cert=x;
  1425. if (!ctx->verify_cb(0, ctx))
  1426. return 0;
  1427. }
  1428. return 1;
  1429. }
  1430. static int internal_verify(X509_STORE_CTX *ctx)
  1431. {
  1432. int ok=0,n;
  1433. X509 *xs,*xi;
  1434. EVP_PKEY *pkey=NULL;
  1435. int (*cb)(int xok,X509_STORE_CTX *xctx);
  1436. cb=ctx->verify_cb;
  1437. n=sk_X509_num(ctx->chain);
  1438. ctx->error_depth=n-1;
  1439. n--;
  1440. xi=sk_X509_value(ctx->chain,n);
  1441. if (ctx->check_issued(ctx, xi, xi))
  1442. xs=xi;
  1443. else
  1444. {
  1445. if (n <= 0)
  1446. {
  1447. ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  1448. ctx->current_cert=xi;
  1449. ok=cb(0,ctx);
  1450. goto end;
  1451. }
  1452. else
  1453. {
  1454. n--;
  1455. ctx->error_depth=n;
  1456. xs=sk_X509_value(ctx->chain,n);
  1457. }
  1458. }
  1459. /* ctx->error=0; not needed */
  1460. while (n >= 0)
  1461. {
  1462. ctx->error_depth=n;
  1463. /* Skip signature check for self signed certificates unless
  1464. * explicitly asked for. It doesn't add any security and
  1465. * just wastes time.
  1466. */
  1467. if (!xs->valid && (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)))
  1468. {
  1469. if ((pkey=X509_get_pubkey(xi)) == NULL)
  1470. {
  1471. ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1472. ctx->current_cert=xi;
  1473. ok=(*cb)(0,ctx);
  1474. if (!ok) goto end;
  1475. }
  1476. else if (X509_verify(xs,pkey) <= 0)
  1477. {
  1478. ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
  1479. ctx->current_cert=xs;
  1480. ok=(*cb)(0,ctx);
  1481. if (!ok)
  1482. {
  1483. EVP_PKEY_free(pkey);
  1484. goto end;
  1485. }
  1486. }
  1487. EVP_PKEY_free(pkey);
  1488. pkey=NULL;
  1489. }
  1490. xs->valid = 1;
  1491. ok = check_cert_time(ctx, xs);
  1492. if (!ok)
  1493. goto end;
  1494. /* The last error (if any) is still in the error value */
  1495. ctx->current_issuer=xi;
  1496. ctx->current_cert=xs;
  1497. ok=(*cb)(1,ctx);
  1498. if (!ok) goto end;
  1499. n--;
  1500. if (n >= 0)
  1501. {
  1502. xi=xs;
  1503. xs=sk_X509_value(ctx->chain,n);
  1504. }
  1505. }
  1506. ok=1;
  1507. end:
  1508. return ok;
  1509. }
  1510. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1511. {
  1512. return X509_cmp_time(ctm, NULL);
  1513. }
  1514. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1515. {
  1516. char *str;
  1517. ASN1_TIME atm;
  1518. long offset;
  1519. char buff1[24],buff2[24],*p;
  1520. int i,j;
  1521. p=buff1;
  1522. i=ctm->length;
  1523. str=(char *)ctm->data;
  1524. if (ctm->type == V_ASN1_UTCTIME)
  1525. {
  1526. if ((i < 11) || (i > 17)) return 0;
  1527. memcpy(p,str,10);
  1528. p+=10;
  1529. str+=10;
  1530. }
  1531. else
  1532. {
  1533. if (i < 13) return 0;
  1534. memcpy(p,str,12);
  1535. p+=12;
  1536. str+=12;
  1537. }
  1538. if ((*str == 'Z') || (*str == '-') || (*str == '+'))
  1539. { *(p++)='0'; *(p++)='0'; }
  1540. else
  1541. {
  1542. *(p++)= *(str++);
  1543. *(p++)= *(str++);
  1544. /* Skip any fractional seconds... */
  1545. if (*str == '.')
  1546. {
  1547. str++;
  1548. while ((*str >= '0') && (*str <= '9')) str++;
  1549. }
  1550. }
  1551. *(p++)='Z';
  1552. *(p++)='\0';
  1553. if (*str == 'Z')
  1554. offset=0;
  1555. else
  1556. {
  1557. if ((*str != '+') && (*str != '-'))
  1558. return 0;
  1559. offset=((str[1]-'0')*10+(str[2]-'0'))*60;
  1560. offset+=(str[3]-'0')*10+(str[4]-'0');
  1561. if (*str == '-')
  1562. offset= -offset;
  1563. }
  1564. atm.type=ctm->type;
  1565. atm.flags = 0;
  1566. atm.length=sizeof(buff2);
  1567. atm.data=(unsigned char *)buff2;
  1568. if (X509_time_adj(&atm, offset*60, cmp_time) == NULL)
  1569. return 0;
  1570. if (ctm->type == V_ASN1_UTCTIME)
  1571. {
  1572. i=(buff1[0]-'0')*10+(buff1[1]-'0');
  1573. if (i < 50) i+=100; /* cf. RFC 2459 */
  1574. j=(buff2[0]-'0')*10+(buff2[1]-'0');
  1575. if (j < 50) j+=100;
  1576. if (i < j) return -1;
  1577. if (i > j) return 1;
  1578. }
  1579. i=strcmp(buff1,buff2);
  1580. if (i == 0) /* wait a second then return younger :-) */
  1581. return -1;
  1582. else
  1583. return i;
  1584. }
  1585. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1586. {
  1587. return X509_time_adj(s, adj, NULL);
  1588. }
  1589. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1590. {
  1591. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1592. }
  1593. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1594. int offset_day, long offset_sec, time_t *in_tm)
  1595. {
  1596. time_t t;
  1597. if (in_tm) t = *in_tm;
  1598. else time(&t);
  1599. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING))
  1600. {
  1601. if (s->type == V_ASN1_UTCTIME)
  1602. return ASN1_UTCTIME_adj(s,t, offset_day, offset_sec);
  1603. if (s->type == V_ASN1_GENERALIZEDTIME)
  1604. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day,
  1605. offset_sec);
  1606. }
  1607. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1608. }
  1609. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  1610. {
  1611. EVP_PKEY *ktmp=NULL,*ktmp2;
  1612. int i,j;
  1613. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
  1614. for (i=0; i<sk_X509_num(chain); i++)
  1615. {
  1616. ktmp=X509_get_pubkey(sk_X509_value(chain,i));
  1617. if (ktmp == NULL)
  1618. {
  1619. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1620. return 0;
  1621. }
  1622. if (!EVP_PKEY_missing_parameters(ktmp))
  1623. break;
  1624. else
  1625. {
  1626. EVP_PKEY_free(ktmp);
  1627. ktmp=NULL;
  1628. }
  1629. }
  1630. if (ktmp == NULL)
  1631. {
  1632. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1633. return 0;
  1634. }
  1635. /* first, populate the other certs */
  1636. for (j=i-1; j >= 0; j--)
  1637. {
  1638. ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
  1639. EVP_PKEY_copy_parameters(ktmp2,ktmp);
  1640. EVP_PKEY_free(ktmp2);
  1641. }
  1642. if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
  1643. EVP_PKEY_free(ktmp);
  1644. return 1;
  1645. }
  1646. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1647. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  1648. {
  1649. /* This function is (usually) called only once, by
  1650. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
  1651. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  1652. new_func, dup_func, free_func);
  1653. }
  1654. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1655. {
  1656. return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
  1657. }
  1658. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1659. {
  1660. return CRYPTO_get_ex_data(&ctx->ex_data,idx);
  1661. }
  1662. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1663. {
  1664. return ctx->error;
  1665. }
  1666. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1667. {
  1668. ctx->error=err;
  1669. }
  1670. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1671. {
  1672. return ctx->error_depth;
  1673. }
  1674. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1675. {
  1676. return ctx->current_cert;
  1677. }
  1678. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1679. {
  1680. return ctx->chain;
  1681. }
  1682. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1683. {
  1684. int i;
  1685. X509 *x;
  1686. STACK_OF(X509) *chain;
  1687. if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
  1688. for (i = 0; i < sk_X509_num(chain); i++)
  1689. {
  1690. x = sk_X509_value(chain, i);
  1691. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1692. }
  1693. return chain;
  1694. }
  1695. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1696. {
  1697. return ctx->current_issuer;
  1698. }
  1699. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1700. {
  1701. return ctx->current_crl;
  1702. }
  1703. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1704. {
  1705. return ctx->parent;
  1706. }
  1707. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1708. {
  1709. ctx->cert=x;
  1710. }
  1711. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1712. {
  1713. ctx->untrusted=sk;
  1714. }
  1715. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1716. {
  1717. ctx->crls=sk;
  1718. }
  1719. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1720. {
  1721. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1722. }
  1723. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1724. {
  1725. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1726. }
  1727. /* This function is used to set the X509_STORE_CTX purpose and trust
  1728. * values. This is intended to be used when another structure has its
  1729. * own trust and purpose values which (if set) will be inherited by
  1730. * the ctx. If they aren't set then we will usually have a default
  1731. * purpose in mind which should then be used to set the trust value.
  1732. * An example of this is SSL use: an SSL structure will have its own
  1733. * purpose and trust settings which the application can set: if they
  1734. * aren't set then we use the default of SSL client/server.
  1735. */
  1736. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1737. int purpose, int trust)
  1738. {
  1739. int idx;
  1740. /* If purpose not set use default */
  1741. if (!purpose) purpose = def_purpose;
  1742. /* If we have a purpose then check it is valid */
  1743. if (purpose)
  1744. {
  1745. X509_PURPOSE *ptmp;
  1746. idx = X509_PURPOSE_get_by_id(purpose);
  1747. if (idx == -1)
  1748. {
  1749. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1750. X509_R_UNKNOWN_PURPOSE_ID);
  1751. return 0;
  1752. }
  1753. ptmp = X509_PURPOSE_get0(idx);
  1754. if (ptmp->trust == X509_TRUST_DEFAULT)
  1755. {
  1756. idx = X509_PURPOSE_get_by_id(def_purpose);
  1757. if (idx == -1)
  1758. {
  1759. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1760. X509_R_UNKNOWN_PURPOSE_ID);
  1761. return 0;
  1762. }
  1763. ptmp = X509_PURPOSE_get0(idx);
  1764. }
  1765. /* If trust not set then get from purpose default */
  1766. if (!trust) trust = ptmp->trust;
  1767. }
  1768. if (trust)
  1769. {
  1770. idx = X509_TRUST_get_by_id(trust);
  1771. if (idx == -1)
  1772. {
  1773. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1774. X509_R_UNKNOWN_TRUST_ID);
  1775. return 0;
  1776. }
  1777. }
  1778. if (purpose && !ctx->param->purpose) ctx->param->purpose = purpose;
  1779. if (trust && !ctx->param->trust) ctx->param->trust = trust;
  1780. return 1;
  1781. }
  1782. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1783. {
  1784. X509_STORE_CTX *ctx;
  1785. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1786. if (!ctx)
  1787. {
  1788. X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1789. return NULL;
  1790. }
  1791. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1792. return ctx;
  1793. }
  1794. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1795. {
  1796. X509_STORE_CTX_cleanup(ctx);
  1797. OPENSSL_free(ctx);
  1798. }
  1799. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1800. STACK_OF(X509) *chain)
  1801. {
  1802. int ret = 1;
  1803. ctx->ctx=store;
  1804. ctx->current_method=0;
  1805. ctx->cert=x509;
  1806. ctx->untrusted=chain;
  1807. ctx->crls = NULL;
  1808. ctx->last_untrusted=0;
  1809. ctx->other_ctx=NULL;
  1810. ctx->valid=0;
  1811. ctx->chain=NULL;
  1812. ctx->error=0;
  1813. ctx->explicit_policy=0;
  1814. ctx->error_depth=0;
  1815. ctx->current_cert=NULL;
  1816. ctx->current_issuer=NULL;
  1817. ctx->current_crl=NULL;
  1818. ctx->current_crl_score=0;
  1819. ctx->current_reasons=0;
  1820. ctx->tree = NULL;
  1821. ctx->parent = NULL;
  1822. ctx->param = X509_VERIFY_PARAM_new();
  1823. if (!ctx->param)
  1824. {
  1825. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1826. return 0;
  1827. }
  1828. /* Inherit callbacks and flags from X509_STORE if not set
  1829. * use defaults.
  1830. */
  1831. if (store)
  1832. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1833. else
  1834. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
  1835. if (store)
  1836. {
  1837. ctx->verify_cb = store->verify_cb;
  1838. ctx->cleanup = store->cleanup;
  1839. }
  1840. else
  1841. ctx->cleanup = 0;
  1842. if (ret)
  1843. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1844. X509_VERIFY_PARAM_lookup("default"));
  1845. if (ret == 0)
  1846. {
  1847. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1848. return 0;
  1849. }
  1850. if (store && store->check_issued)
  1851. ctx->check_issued = store->check_issued;
  1852. else
  1853. ctx->check_issued = check_issued;
  1854. if (store && store->get_issuer)
  1855. ctx->get_issuer = store->get_issuer;
  1856. else
  1857. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1858. if (store && store->verify_cb)
  1859. ctx->verify_cb = store->verify_cb;
  1860. else
  1861. ctx->verify_cb = null_callback;
  1862. if (store && store->verify)
  1863. ctx->verify = store->verify;
  1864. else
  1865. ctx->verify = internal_verify;
  1866. if (store && store->check_revocation)
  1867. ctx->check_revocation = store->check_revocation;
  1868. else
  1869. ctx->check_revocation = check_revocation;
  1870. if (store && store->get_crl)
  1871. ctx->get_crl = store->get_crl;
  1872. else
  1873. ctx->get_crl = NULL;
  1874. if (store && store->check_crl)
  1875. ctx->check_crl = store->check_crl;
  1876. else
  1877. ctx->check_crl = check_crl;
  1878. if (store && store->cert_crl)
  1879. ctx->cert_crl = store->cert_crl;
  1880. else
  1881. ctx->cert_crl = cert_crl;
  1882. if (store && store->lookup_certs)
  1883. ctx->lookup_certs = store->lookup_certs;
  1884. else
  1885. ctx->lookup_certs = X509_STORE_get1_certs;
  1886. if (store && store->lookup_crls)
  1887. ctx->lookup_crls = store->lookup_crls;
  1888. else
  1889. ctx->lookup_crls = X509_STORE_get1_crls;
  1890. ctx->check_policy = check_policy;
  1891. /* This memset() can't make any sense anyway, so it's removed. As
  1892. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  1893. * corresponding "new" here and remove this bogus initialisation. */
  1894. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  1895. if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  1896. &(ctx->ex_data)))
  1897. {
  1898. OPENSSL_free(ctx);
  1899. X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
  1900. return 0;
  1901. }
  1902. return 1;
  1903. }
  1904. /* Set alternative lookup method: just a STACK of trusted certificates.
  1905. * This avoids X509_STORE nastiness where it isn't needed.
  1906. */
  1907. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1908. {
  1909. ctx->other_ctx = sk;
  1910. ctx->get_issuer = get_issuer_sk;
  1911. }
  1912. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  1913. {
  1914. if (ctx->cleanup) ctx->cleanup(ctx);
  1915. if (ctx->param != NULL)
  1916. {
  1917. if (ctx->parent == NULL)
  1918. X509_VERIFY_PARAM_free(ctx->param);
  1919. ctx->param=NULL;
  1920. }
  1921. if (ctx->tree != NULL)
  1922. {
  1923. X509_policy_tree_free(ctx->tree);
  1924. ctx->tree=NULL;
  1925. }
  1926. if (ctx->chain != NULL)
  1927. {
  1928. sk_X509_pop_free(ctx->chain,X509_free);
  1929. ctx->chain=NULL;
  1930. }
  1931. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  1932. memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
  1933. }
  1934. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  1935. {
  1936. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1937. }
  1938. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  1939. {
  1940. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  1941. }
  1942. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
  1943. {
  1944. X509_VERIFY_PARAM_set_time(ctx->param, t);
  1945. }
  1946. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  1947. int (*verify_cb)(int, X509_STORE_CTX *))
  1948. {
  1949. ctx->verify_cb=verify_cb;
  1950. }
  1951. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  1952. {
  1953. return ctx->tree;
  1954. }
  1955. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  1956. {
  1957. return ctx->explicit_policy;
  1958. }
  1959. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  1960. {
  1961. const X509_VERIFY_PARAM *param;
  1962. param = X509_VERIFY_PARAM_lookup(name);
  1963. if (!param)
  1964. return 0;
  1965. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  1966. }
  1967. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  1968. {
  1969. return ctx->param;
  1970. }
  1971. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  1972. {
  1973. if (ctx->param)
  1974. X509_VERIFY_PARAM_free(ctx->param);
  1975. ctx->param = param;
  1976. }
  1977. IMPLEMENT_STACK_OF(X509)
  1978. IMPLEMENT_ASN1_SET_OF(X509)
  1979. IMPLEMENT_STACK_OF(X509_NAME)
  1980. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  1981. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)