x509_vfy.c 52 KB

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