x509_vfy.c 52 KB

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