x509_vfy.c 70 KB

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