x509_vfy.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  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. #include "vpm_int.h"
  71. /* CRL score values */
  72. /* No unhandled critical extensions */
  73. #define CRL_SCORE_NOCRITICAL 0x100
  74. /* certificate is within CRL scope */
  75. #define CRL_SCORE_SCOPE 0x080
  76. /* CRL times valid */
  77. #define CRL_SCORE_TIME 0x040
  78. /* Issuer name matches certificate */
  79. #define CRL_SCORE_ISSUER_NAME 0x020
  80. /* If this score or above CRL is probably valid */
  81. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  82. /* CRL issuer is certificate issuer */
  83. #define CRL_SCORE_ISSUER_CERT 0x018
  84. /* CRL issuer is on certificate path */
  85. #define CRL_SCORE_SAME_PATH 0x008
  86. /* CRL issuer matches CRL AKID */
  87. #define CRL_SCORE_AKID 0x004
  88. /* Have a delta CRL with valid times */
  89. #define CRL_SCORE_TIME_DELTA 0x002
  90. static int null_callback(int ok, X509_STORE_CTX *e);
  91. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  92. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  93. static int check_chain_extensions(X509_STORE_CTX *ctx);
  94. static int check_name_constraints(X509_STORE_CTX *ctx);
  95. static int check_id(X509_STORE_CTX *ctx);
  96. static int check_trust(X509_STORE_CTX *ctx);
  97. static int check_revocation(X509_STORE_CTX *ctx);
  98. static int check_cert(X509_STORE_CTX *ctx);
  99. static int check_policy(X509_STORE_CTX *ctx);
  100. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  101. unsigned int *preasons, X509_CRL *crl, X509 *x);
  102. static int get_crl_delta(X509_STORE_CTX *ctx,
  103. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  104. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
  105. int *pcrl_score, X509_CRL *base,
  106. STACK_OF(X509_CRL) *crls);
  107. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
  108. int *pcrl_score);
  109. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  110. unsigned int *preasons);
  111. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  112. static int check_crl_chain(X509_STORE_CTX *ctx,
  113. STACK_OF(X509) *cert_path,
  114. STACK_OF(X509) *crl_path);
  115. static int internal_verify(X509_STORE_CTX *ctx);
  116. const char X509_version[] = "X.509" OPENSSL_VERSION_PTEXT;
  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. int 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. CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
  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. X509err(X509_F_X509_VERIFY_CERT, 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. X509err(X509_F_X509_VERIFY_CERT, 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. if (((ctx->chain = sk_X509_new_null()) == NULL) ||
  186. (!sk_X509_push(ctx->chain, ctx->cert))) {
  187. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  188. goto end;
  189. }
  190. CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
  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. X509err(X509_F_X509_VERIFY_CERT, 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. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  233. goto end;
  234. }
  235. CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
  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. X509err(X509_F_X509_VERIFY_CERT, 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. /* We may as well copy down any DSA parameters that are required */
  403. X509_get_pubkey_parameters(NULL, ctx->chain);
  404. /*
  405. * Check revocation status: we do this after copying parameters because
  406. * they may be needed for CRL signature verification.
  407. */
  408. ok = ctx->check_revocation(ctx);
  409. if (!ok)
  410. goto end;
  411. i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
  412. ctx->param->flags);
  413. if (i != X509_V_OK) {
  414. ctx->error = i;
  415. ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
  416. ok = cb(0, ctx);
  417. if (!ok)
  418. goto end;
  419. }
  420. /* At this point, we have a chain and need to verify it */
  421. if (ctx->verify != NULL)
  422. ok = ctx->verify(ctx);
  423. else
  424. ok = internal_verify(ctx);
  425. if (!ok)
  426. goto end;
  427. #ifndef OPENSSL_NO_RFC3779
  428. /* RFC 3779 path validation, now that CRL check has been done */
  429. ok = v3_asid_validate_path(ctx);
  430. if (!ok)
  431. goto end;
  432. ok = v3_addr_validate_path(ctx);
  433. if (!ok)
  434. goto end;
  435. #endif
  436. /* If we get this far evaluate policies */
  437. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  438. ok = ctx->check_policy(ctx);
  439. if (!ok)
  440. goto end;
  441. if (0) {
  442. end:
  443. X509_get_pubkey_parameters(NULL, ctx->chain);
  444. }
  445. if (sktmp != NULL)
  446. sk_X509_free(sktmp);
  447. if (chain_ss != NULL)
  448. X509_free(chain_ss);
  449. return ok;
  450. }
  451. /*
  452. * Given a STACK_OF(X509) find the issuer of cert (if any)
  453. */
  454. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  455. {
  456. int i;
  457. X509 *issuer;
  458. for (i = 0; i < sk_X509_num(sk); i++) {
  459. issuer = sk_X509_value(sk, i);
  460. if (ctx->check_issued(ctx, x, issuer))
  461. return issuer;
  462. }
  463. return NULL;
  464. }
  465. /* Given a possible certificate and issuer check them */
  466. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  467. {
  468. int ret;
  469. ret = X509_check_issued(issuer, x);
  470. if (ret == X509_V_OK)
  471. return 1;
  472. /* If we haven't asked for issuer errors don't set ctx */
  473. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  474. return 0;
  475. ctx->error = ret;
  476. ctx->current_cert = x;
  477. ctx->current_issuer = issuer;
  478. return ctx->verify_cb(0, ctx);
  479. }
  480. /* Alternative lookup method: look from a STACK stored in other_ctx */
  481. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  482. {
  483. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  484. if (*issuer) {
  485. CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
  486. return 1;
  487. } else
  488. return 0;
  489. }
  490. /*
  491. * Check a certificate chains extensions for consistency with the supplied
  492. * purpose
  493. */
  494. static int check_chain_extensions(X509_STORE_CTX *ctx)
  495. {
  496. #ifdef OPENSSL_NO_CHAIN_VERIFY
  497. return 1;
  498. #else
  499. int i, ok = 0, must_be_ca, plen = 0;
  500. X509 *x;
  501. int (*cb) (int xok, X509_STORE_CTX *xctx);
  502. int proxy_path_length = 0;
  503. int purpose;
  504. int allow_proxy_certs;
  505. cb = ctx->verify_cb;
  506. /*-
  507. * must_be_ca can have 1 of 3 values:
  508. * -1: we accept both CA and non-CA certificates, to allow direct
  509. * use of self-signed certificates (which are marked as CA).
  510. * 0: we only accept non-CA certificates. This is currently not
  511. * used, but the possibility is present for future extensions.
  512. * 1: we only accept CA certificates. This is currently used for
  513. * all certificates in the chain except the leaf certificate.
  514. */
  515. must_be_ca = -1;
  516. /* CRL path validation */
  517. if (ctx->parent) {
  518. allow_proxy_certs = 0;
  519. purpose = X509_PURPOSE_CRL_SIGN;
  520. } else {
  521. allow_proxy_certs =
  522. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  523. /*
  524. * A hack to keep people who don't want to modify their software
  525. * happy
  526. */
  527. if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
  528. allow_proxy_certs = 1;
  529. purpose = ctx->param->purpose;
  530. }
  531. /* Check all untrusted certificates */
  532. for (i = 0; i < ctx->last_untrusted; i++) {
  533. int ret;
  534. x = sk_X509_value(ctx->chain, i);
  535. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  536. && (x->ex_flags & EXFLAG_CRITICAL)) {
  537. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  538. ctx->error_depth = i;
  539. ctx->current_cert = x;
  540. ok = cb(0, ctx);
  541. if (!ok)
  542. goto end;
  543. }
  544. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  545. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  546. ctx->error_depth = i;
  547. ctx->current_cert = x;
  548. ok = cb(0, ctx);
  549. if (!ok)
  550. goto end;
  551. }
  552. ret = X509_check_ca(x);
  553. switch (must_be_ca) {
  554. case -1:
  555. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  556. && (ret != 1) && (ret != 0)) {
  557. ret = 0;
  558. ctx->error = X509_V_ERR_INVALID_CA;
  559. } else
  560. ret = 1;
  561. break;
  562. case 0:
  563. if (ret != 0) {
  564. ret = 0;
  565. ctx->error = X509_V_ERR_INVALID_NON_CA;
  566. } else
  567. ret = 1;
  568. break;
  569. default:
  570. if ((ret == 0)
  571. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  572. && (ret != 1))) {
  573. ret = 0;
  574. ctx->error = X509_V_ERR_INVALID_CA;
  575. } else
  576. ret = 1;
  577. break;
  578. }
  579. if (ret == 0) {
  580. ctx->error_depth = i;
  581. ctx->current_cert = x;
  582. ok = cb(0, ctx);
  583. if (!ok)
  584. goto end;
  585. }
  586. if (ctx->param->purpose > 0) {
  587. ret = X509_check_purpose(x, purpose, must_be_ca > 0);
  588. if ((ret == 0)
  589. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  590. && (ret != 1))) {
  591. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  592. ctx->error_depth = i;
  593. ctx->current_cert = x;
  594. ok = cb(0, ctx);
  595. if (!ok)
  596. goto end;
  597. }
  598. }
  599. /* Check pathlen if not self issued */
  600. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  601. && (x->ex_pathlen != -1)
  602. && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
  603. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  604. ctx->error_depth = i;
  605. ctx->current_cert = x;
  606. ok = cb(0, ctx);
  607. if (!ok)
  608. goto end;
  609. }
  610. /* Increment path length if not self issued */
  611. if (!(x->ex_flags & EXFLAG_SI))
  612. plen++;
  613. /*
  614. * If this certificate is a proxy certificate, the next certificate
  615. * must be another proxy certificate or a EE certificate. If not,
  616. * the next certificate must be a CA certificate.
  617. */
  618. if (x->ex_flags & EXFLAG_PROXY) {
  619. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
  620. ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  621. ctx->error_depth = i;
  622. ctx->current_cert = x;
  623. ok = cb(0, ctx);
  624. if (!ok)
  625. goto end;
  626. }
  627. proxy_path_length++;
  628. must_be_ca = 0;
  629. } else
  630. must_be_ca = 1;
  631. }
  632. ok = 1;
  633. end:
  634. return ok;
  635. #endif
  636. }
  637. static int check_name_constraints(X509_STORE_CTX *ctx)
  638. {
  639. X509 *x;
  640. int i, j, rv;
  641. /* Check name constraints for all certificates */
  642. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
  643. x = sk_X509_value(ctx->chain, i);
  644. /* Ignore self issued certs unless last in chain */
  645. if (i && (x->ex_flags & EXFLAG_SI))
  646. continue;
  647. /*
  648. * Check against constraints for all certificates higher in chain
  649. * including trust anchor. Trust anchor not strictly speaking needed
  650. * but if it includes constraints it is to be assumed it expects them
  651. * to be obeyed.
  652. */
  653. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
  654. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  655. if (nc) {
  656. rv = NAME_CONSTRAINTS_check(x, nc);
  657. if (rv != X509_V_OK) {
  658. ctx->error = rv;
  659. ctx->error_depth = i;
  660. ctx->current_cert = x;
  661. if (!ctx->verify_cb(0, ctx))
  662. return 0;
  663. }
  664. }
  665. }
  666. }
  667. return 1;
  668. }
  669. static int check_id_error(X509_STORE_CTX *ctx, int errcode)
  670. {
  671. ctx->error = errcode;
  672. ctx->current_cert = ctx->cert;
  673. ctx->error_depth = 0;
  674. return ctx->verify_cb(0, ctx);
  675. }
  676. static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
  677. {
  678. int i;
  679. int n = sk_OPENSSL_STRING_num(id->hosts);
  680. char *name;
  681. if (id->peername != NULL) {
  682. OPENSSL_free(id->peername);
  683. id->peername = NULL;
  684. }
  685. for (i = 0; i < n; ++i) {
  686. name = sk_OPENSSL_STRING_value(id->hosts, i);
  687. if (X509_check_host(x, name, 0, id->hostflags, &id->peername) > 0)
  688. return 1;
  689. }
  690. return n == 0;
  691. }
  692. static int check_id(X509_STORE_CTX *ctx)
  693. {
  694. X509_VERIFY_PARAM *vpm = ctx->param;
  695. X509_VERIFY_PARAM_ID *id = vpm->id;
  696. X509 *x = ctx->cert;
  697. if (id->hosts && check_hosts(x, id) <= 0) {
  698. if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
  699. return 0;
  700. }
  701. if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
  702. if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
  703. return 0;
  704. }
  705. if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
  706. if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
  707. return 0;
  708. }
  709. return 1;
  710. }
  711. static int check_trust(X509_STORE_CTX *ctx)
  712. {
  713. int i, ok;
  714. X509 *x = NULL;
  715. int (*cb) (int xok, X509_STORE_CTX *xctx);
  716. cb = ctx->verify_cb;
  717. /* Check all trusted certificates in chain */
  718. for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
  719. x = sk_X509_value(ctx->chain, i);
  720. ok = X509_check_trust(x, ctx->param->trust, 0);
  721. /* If explicitly trusted return trusted */
  722. if (ok == X509_TRUST_TRUSTED)
  723. return X509_TRUST_TRUSTED;
  724. /*
  725. * If explicitly rejected notify callback and reject if not
  726. * overridden.
  727. */
  728. if (ok == X509_TRUST_REJECTED) {
  729. ctx->error_depth = i;
  730. ctx->current_cert = x;
  731. ctx->error = X509_V_ERR_CERT_REJECTED;
  732. ok = cb(0, ctx);
  733. if (!ok)
  734. return X509_TRUST_REJECTED;
  735. }
  736. }
  737. /*
  738. * If we accept partial chains and have at least one trusted certificate
  739. * return success.
  740. */
  741. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  742. X509 *mx;
  743. if (ctx->last_untrusted < sk_X509_num(ctx->chain))
  744. return X509_TRUST_TRUSTED;
  745. x = sk_X509_value(ctx->chain, 0);
  746. mx = lookup_cert_match(ctx, x);
  747. if (mx) {
  748. (void)sk_X509_set(ctx->chain, 0, mx);
  749. X509_free(x);
  750. ctx->last_untrusted = 0;
  751. return X509_TRUST_TRUSTED;
  752. }
  753. }
  754. /*
  755. * If no trusted certs in chain at all return untrusted and allow
  756. * standard (no issuer cert) etc errors to be indicated.
  757. */
  758. return X509_TRUST_UNTRUSTED;
  759. }
  760. static int check_revocation(X509_STORE_CTX *ctx)
  761. {
  762. int i, last, ok;
  763. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  764. return 1;
  765. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  766. last = sk_X509_num(ctx->chain) - 1;
  767. else {
  768. /* If checking CRL paths this isn't the EE certificate */
  769. if (ctx->parent)
  770. return 1;
  771. last = 0;
  772. }
  773. for (i = 0; i <= last; i++) {
  774. ctx->error_depth = i;
  775. ok = check_cert(ctx);
  776. if (!ok)
  777. return ok;
  778. }
  779. return 1;
  780. }
  781. static int check_cert(X509_STORE_CTX *ctx)
  782. {
  783. X509_CRL *crl = NULL, *dcrl = NULL;
  784. X509 *x;
  785. int ok, cnum;
  786. unsigned int last_reasons;
  787. cnum = ctx->error_depth;
  788. x = sk_X509_value(ctx->chain, cnum);
  789. ctx->current_cert = x;
  790. ctx->current_issuer = NULL;
  791. ctx->current_crl_score = 0;
  792. ctx->current_reasons = 0;
  793. while (ctx->current_reasons != CRLDP_ALL_REASONS) {
  794. last_reasons = ctx->current_reasons;
  795. /* Try to retrieve relevant CRL */
  796. if (ctx->get_crl)
  797. ok = ctx->get_crl(ctx, &crl, x);
  798. else
  799. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  800. /*
  801. * If error looking up CRL, nothing we can do except notify callback
  802. */
  803. if (!ok) {
  804. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  805. ok = ctx->verify_cb(0, ctx);
  806. goto err;
  807. }
  808. ctx->current_crl = crl;
  809. ok = ctx->check_crl(ctx, crl);
  810. if (!ok)
  811. goto err;
  812. if (dcrl) {
  813. ok = ctx->check_crl(ctx, dcrl);
  814. if (!ok)
  815. goto err;
  816. ok = ctx->cert_crl(ctx, dcrl, x);
  817. if (!ok)
  818. goto err;
  819. } else
  820. ok = 1;
  821. /* Don't look in full CRL if delta reason is removefromCRL */
  822. if (ok != 2) {
  823. ok = ctx->cert_crl(ctx, crl, x);
  824. if (!ok)
  825. goto err;
  826. }
  827. X509_CRL_free(crl);
  828. X509_CRL_free(dcrl);
  829. crl = NULL;
  830. dcrl = NULL;
  831. /*
  832. * If reasons not updated we wont get anywhere by another iteration,
  833. * so exit loop.
  834. */
  835. if (last_reasons == ctx->current_reasons) {
  836. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  837. ok = ctx->verify_cb(0, ctx);
  838. goto err;
  839. }
  840. }
  841. err:
  842. X509_CRL_free(crl);
  843. X509_CRL_free(dcrl);
  844. ctx->current_crl = NULL;
  845. return ok;
  846. }
  847. /* Check CRL times against values in X509_STORE_CTX */
  848. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  849. {
  850. time_t *ptime;
  851. int i;
  852. if (notify)
  853. ctx->current_crl = crl;
  854. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  855. ptime = &ctx->param->check_time;
  856. else
  857. ptime = NULL;
  858. i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  859. if (i == 0) {
  860. if (!notify)
  861. return 0;
  862. ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  863. if (!ctx->verify_cb(0, ctx))
  864. return 0;
  865. }
  866. if (i > 0) {
  867. if (!notify)
  868. return 0;
  869. ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
  870. if (!ctx->verify_cb(0, ctx))
  871. return 0;
  872. }
  873. if (X509_CRL_get_nextUpdate(crl)) {
  874. i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  875. if (i == 0) {
  876. if (!notify)
  877. return 0;
  878. ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  879. if (!ctx->verify_cb(0, ctx))
  880. return 0;
  881. }
  882. /* Ignore expiry of base CRL is delta is valid */
  883. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
  884. if (!notify)
  885. return 0;
  886. ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
  887. if (!ctx->verify_cb(0, ctx))
  888. return 0;
  889. }
  890. }
  891. if (notify)
  892. ctx->current_crl = NULL;
  893. return 1;
  894. }
  895. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  896. X509 **pissuer, int *pscore, unsigned int *preasons,
  897. STACK_OF(X509_CRL) *crls)
  898. {
  899. int i, crl_score, best_score = *pscore;
  900. unsigned int reasons, best_reasons = 0;
  901. X509 *x = ctx->current_cert;
  902. X509_CRL *crl, *best_crl = NULL;
  903. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  904. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  905. crl = sk_X509_CRL_value(crls, i);
  906. reasons = *preasons;
  907. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  908. if (crl_score > best_score) {
  909. best_crl = crl;
  910. best_crl_issuer = crl_issuer;
  911. best_score = crl_score;
  912. best_reasons = reasons;
  913. }
  914. }
  915. if (best_crl) {
  916. if (*pcrl)
  917. X509_CRL_free(*pcrl);
  918. *pcrl = best_crl;
  919. *pissuer = best_crl_issuer;
  920. *pscore = best_score;
  921. *preasons = best_reasons;
  922. CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
  923. if (*pdcrl) {
  924. X509_CRL_free(*pdcrl);
  925. *pdcrl = NULL;
  926. }
  927. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  928. }
  929. if (best_score >= CRL_SCORE_VALID)
  930. return 1;
  931. return 0;
  932. }
  933. /*
  934. * Compare two CRL extensions for delta checking purposes. They should be
  935. * both present or both absent. If both present all fields must be identical.
  936. */
  937. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  938. {
  939. ASN1_OCTET_STRING *exta, *extb;
  940. int i;
  941. i = X509_CRL_get_ext_by_NID(a, nid, -1);
  942. if (i >= 0) {
  943. /* Can't have multiple occurrences */
  944. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  945. return 0;
  946. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  947. } else
  948. exta = NULL;
  949. i = X509_CRL_get_ext_by_NID(b, nid, -1);
  950. if (i >= 0) {
  951. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  952. return 0;
  953. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  954. } else
  955. extb = NULL;
  956. if (!exta && !extb)
  957. return 1;
  958. if (!exta || !extb)
  959. return 0;
  960. if (ASN1_OCTET_STRING_cmp(exta, extb))
  961. return 0;
  962. return 1;
  963. }
  964. /* See if a base and delta are compatible */
  965. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  966. {
  967. /* Delta CRL must be a delta */
  968. if (!delta->base_crl_number)
  969. return 0;
  970. /* Base must have a CRL number */
  971. if (!base->crl_number)
  972. return 0;
  973. /* Issuer names must match */
  974. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
  975. return 0;
  976. /* AKID and IDP must match */
  977. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  978. return 0;
  979. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  980. return 0;
  981. /* Delta CRL base number must not exceed Full CRL number. */
  982. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  983. return 0;
  984. /* Delta CRL number must exceed full CRL number */
  985. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  986. return 1;
  987. return 0;
  988. }
  989. /*
  990. * For a given base CRL find a delta... maybe extend to delta scoring or
  991. * retrieve a chain of deltas...
  992. */
  993. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  994. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  995. {
  996. X509_CRL *delta;
  997. int i;
  998. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  999. return;
  1000. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  1001. return;
  1002. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1003. delta = sk_X509_CRL_value(crls, i);
  1004. if (check_delta_base(delta, base)) {
  1005. if (check_crl_time(ctx, delta, 0))
  1006. *pscore |= CRL_SCORE_TIME_DELTA;
  1007. CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
  1008. *dcrl = delta;
  1009. return;
  1010. }
  1011. }
  1012. *dcrl = NULL;
  1013. }
  1014. /*
  1015. * For a given CRL return how suitable it is for the supplied certificate
  1016. * 'x'. The return value is a mask of several criteria. If the issuer is not
  1017. * the certificate issuer this is returned in *pissuer. The reasons mask is
  1018. * also used to determine if the CRL is suitable: if no new reasons the CRL
  1019. * is rejected, otherwise reasons is updated.
  1020. */
  1021. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  1022. unsigned int *preasons, X509_CRL *crl, X509 *x)
  1023. {
  1024. int crl_score = 0;
  1025. unsigned int tmp_reasons = *preasons, crl_reasons;
  1026. /* First see if we can reject CRL straight away */
  1027. /* Invalid IDP cannot be processed */
  1028. if (crl->idp_flags & IDP_INVALID)
  1029. return 0;
  1030. /* Reason codes or indirect CRLs need extended CRL support */
  1031. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
  1032. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  1033. return 0;
  1034. } else if (crl->idp_flags & IDP_REASONS) {
  1035. /* If no new reasons reject */
  1036. if (!(crl->idp_reasons & ~tmp_reasons))
  1037. return 0;
  1038. }
  1039. /* Don't process deltas at this stage */
  1040. else if (crl->base_crl_number)
  1041. return 0;
  1042. /* If issuer name doesn't match certificate need indirect CRL */
  1043. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
  1044. if (!(crl->idp_flags & IDP_INDIRECT))
  1045. return 0;
  1046. } else
  1047. crl_score |= CRL_SCORE_ISSUER_NAME;
  1048. if (!(crl->flags & EXFLAG_CRITICAL))
  1049. crl_score |= CRL_SCORE_NOCRITICAL;
  1050. /* Check expiry */
  1051. if (check_crl_time(ctx, crl, 0))
  1052. crl_score |= CRL_SCORE_TIME;
  1053. /* Check authority key ID and locate certificate issuer */
  1054. crl_akid_check(ctx, crl, pissuer, &crl_score);
  1055. /* If we can't locate certificate issuer at this point forget it */
  1056. if (!(crl_score & CRL_SCORE_AKID))
  1057. return 0;
  1058. /* Check cert for matching CRL distribution points */
  1059. if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
  1060. /* If no new reasons reject */
  1061. if (!(crl_reasons & ~tmp_reasons))
  1062. return 0;
  1063. tmp_reasons |= crl_reasons;
  1064. crl_score |= CRL_SCORE_SCOPE;
  1065. }
  1066. *preasons = tmp_reasons;
  1067. return crl_score;
  1068. }
  1069. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  1070. X509 **pissuer, int *pcrl_score)
  1071. {
  1072. X509 *crl_issuer = NULL;
  1073. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  1074. int cidx = ctx->error_depth;
  1075. int i;
  1076. if (cidx != sk_X509_num(ctx->chain) - 1)
  1077. cidx++;
  1078. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1079. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1080. if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
  1081. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
  1082. *pissuer = crl_issuer;
  1083. return;
  1084. }
  1085. }
  1086. for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
  1087. crl_issuer = sk_X509_value(ctx->chain, cidx);
  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. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
  1092. *pissuer = crl_issuer;
  1093. return;
  1094. }
  1095. }
  1096. /* Anything else needs extended CRL support */
  1097. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  1098. return;
  1099. /*
  1100. * Otherwise the CRL issuer is not on the path. Look for it in the set of
  1101. * untrusted certificates.
  1102. */
  1103. for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
  1104. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1105. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1106. continue;
  1107. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1108. *pissuer = crl_issuer;
  1109. *pcrl_score |= CRL_SCORE_AKID;
  1110. return;
  1111. }
  1112. }
  1113. }
  1114. /*
  1115. * Check the path of a CRL issuer certificate. This creates a new
  1116. * X509_STORE_CTX and populates it with most of the parameters from the
  1117. * parent. This could be optimised somewhat since a lot of path checking will
  1118. * be duplicated by the parent, but this will rarely be used in practice.
  1119. */
  1120. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1121. {
  1122. X509_STORE_CTX crl_ctx;
  1123. int ret;
  1124. /* Don't allow recursive CRL path validation */
  1125. if (ctx->parent)
  1126. return 0;
  1127. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1128. return -1;
  1129. crl_ctx.crls = ctx->crls;
  1130. /* Copy verify params across */
  1131. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1132. crl_ctx.parent = ctx;
  1133. crl_ctx.verify_cb = ctx->verify_cb;
  1134. /* Verify CRL issuer */
  1135. ret = X509_verify_cert(&crl_ctx);
  1136. if (ret <= 0)
  1137. goto err;
  1138. /* Check chain is acceptable */
  1139. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1140. err:
  1141. X509_STORE_CTX_cleanup(&crl_ctx);
  1142. return ret;
  1143. }
  1144. /*
  1145. * RFC3280 says nothing about the relationship between CRL path and
  1146. * certificate path, which could lead to situations where a certificate could
  1147. * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  1148. * strict and states that the two paths must end in the same trust anchor,
  1149. * though some discussions remain... until this is resolved we use the
  1150. * RFC5280 version
  1151. */
  1152. static int check_crl_chain(X509_STORE_CTX *ctx,
  1153. STACK_OF(X509) *cert_path,
  1154. STACK_OF(X509) *crl_path)
  1155. {
  1156. X509 *cert_ta, *crl_ta;
  1157. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1158. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1159. if (!X509_cmp(cert_ta, crl_ta))
  1160. return 1;
  1161. return 0;
  1162. }
  1163. /*-
  1164. * Check for match between two dist point names: three separate cases.
  1165. * 1. Both are relative names and compare X509_NAME types.
  1166. * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
  1167. * 3. Both are full names and compare two GENERAL_NAMES.
  1168. * 4. One is NULL: automatic match.
  1169. */
  1170. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1171. {
  1172. X509_NAME *nm = NULL;
  1173. GENERAL_NAMES *gens = NULL;
  1174. GENERAL_NAME *gena, *genb;
  1175. int i, j;
  1176. if (!a || !b)
  1177. return 1;
  1178. if (a->type == 1) {
  1179. if (!a->dpname)
  1180. return 0;
  1181. /* Case 1: two X509_NAME */
  1182. if (b->type == 1) {
  1183. if (!b->dpname)
  1184. return 0;
  1185. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1186. return 1;
  1187. else
  1188. return 0;
  1189. }
  1190. /* Case 2: set name and GENERAL_NAMES appropriately */
  1191. nm = a->dpname;
  1192. gens = b->name.fullname;
  1193. } else if (b->type == 1) {
  1194. if (!b->dpname)
  1195. return 0;
  1196. /* Case 2: set name and GENERAL_NAMES appropriately */
  1197. gens = a->name.fullname;
  1198. nm = b->dpname;
  1199. }
  1200. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1201. if (nm) {
  1202. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  1203. gena = sk_GENERAL_NAME_value(gens, i);
  1204. if (gena->type != GEN_DIRNAME)
  1205. continue;
  1206. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1207. return 1;
  1208. }
  1209. return 0;
  1210. }
  1211. /* Else case 3: two GENERAL_NAMES */
  1212. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
  1213. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1214. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
  1215. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1216. if (!GENERAL_NAME_cmp(gena, genb))
  1217. return 1;
  1218. }
  1219. }
  1220. return 0;
  1221. }
  1222. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1223. {
  1224. int i;
  1225. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1226. /* If no CRLissuer return is successful iff don't need a match */
  1227. if (!dp->CRLissuer)
  1228. return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
  1229. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  1230. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1231. if (gen->type != GEN_DIRNAME)
  1232. continue;
  1233. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1234. return 1;
  1235. }
  1236. return 0;
  1237. }
  1238. /* Check CRLDP and IDP */
  1239. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1240. unsigned int *preasons)
  1241. {
  1242. int i;
  1243. if (crl->idp_flags & IDP_ONLYATTR)
  1244. return 0;
  1245. if (x->ex_flags & EXFLAG_CA) {
  1246. if (crl->idp_flags & IDP_ONLYUSER)
  1247. return 0;
  1248. } else {
  1249. if (crl->idp_flags & IDP_ONLYCA)
  1250. return 0;
  1251. }
  1252. *preasons = crl->idp_reasons;
  1253. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  1254. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1255. if (crldp_check_crlissuer(dp, crl, crl_score)) {
  1256. if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
  1257. *preasons &= dp->dp_reasons;
  1258. return 1;
  1259. }
  1260. }
  1261. }
  1262. if ((!crl->idp || !crl->idp->distpoint)
  1263. && (crl_score & CRL_SCORE_ISSUER_NAME))
  1264. return 1;
  1265. return 0;
  1266. }
  1267. /*
  1268. * Retrieve CRL corresponding to current certificate. If deltas enabled try
  1269. * to find a delta CRL too
  1270. */
  1271. static int get_crl_delta(X509_STORE_CTX *ctx,
  1272. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1273. {
  1274. int ok;
  1275. X509 *issuer = NULL;
  1276. int crl_score = 0;
  1277. unsigned int reasons;
  1278. X509_CRL *crl = NULL, *dcrl = NULL;
  1279. STACK_OF(X509_CRL) *skcrl;
  1280. X509_NAME *nm = X509_get_issuer_name(x);
  1281. reasons = ctx->current_reasons;
  1282. ok = get_crl_sk(ctx, &crl, &dcrl,
  1283. &issuer, &crl_score, &reasons, ctx->crls);
  1284. if (ok)
  1285. goto done;
  1286. /* Lookup CRLs from store */
  1287. skcrl = ctx->lookup_crls(ctx, nm);
  1288. /* If no CRLs found and a near match from get_crl_sk use that */
  1289. if (!skcrl && crl)
  1290. goto done;
  1291. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1292. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1293. done:
  1294. /* If we got any kind of CRL use it and return success */
  1295. if (crl) {
  1296. ctx->current_issuer = issuer;
  1297. ctx->current_crl_score = crl_score;
  1298. ctx->current_reasons = reasons;
  1299. *pcrl = crl;
  1300. *pdcrl = dcrl;
  1301. return 1;
  1302. }
  1303. return 0;
  1304. }
  1305. /* Check CRL validity */
  1306. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1307. {
  1308. X509 *issuer = NULL;
  1309. EVP_PKEY *ikey = NULL;
  1310. int ok = 0, chnum, cnum;
  1311. cnum = ctx->error_depth;
  1312. chnum = sk_X509_num(ctx->chain) - 1;
  1313. /* if we have an alternative CRL issuer cert use that */
  1314. if (ctx->current_issuer)
  1315. issuer = ctx->current_issuer;
  1316. /*
  1317. * Else find CRL issuer: if not last certificate then issuer is next
  1318. * certificate in chain.
  1319. */
  1320. else if (cnum < chnum)
  1321. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1322. else {
  1323. issuer = sk_X509_value(ctx->chain, chnum);
  1324. /* If not self signed, can't check signature */
  1325. if (!ctx->check_issued(ctx, issuer, issuer)) {
  1326. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  1327. ok = ctx->verify_cb(0, ctx);
  1328. if (!ok)
  1329. goto err;
  1330. }
  1331. }
  1332. if (issuer) {
  1333. /*
  1334. * Skip most tests for deltas because they have already been done
  1335. */
  1336. if (!crl->base_crl_number) {
  1337. /* Check for cRLSign bit if keyUsage present */
  1338. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1339. !(issuer->ex_kusage & KU_CRL_SIGN)) {
  1340. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  1341. ok = ctx->verify_cb(0, ctx);
  1342. if (!ok)
  1343. goto err;
  1344. }
  1345. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
  1346. ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
  1347. ok = ctx->verify_cb(0, ctx);
  1348. if (!ok)
  1349. goto err;
  1350. }
  1351. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
  1352. if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
  1353. ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  1354. ok = ctx->verify_cb(0, ctx);
  1355. if (!ok)
  1356. goto err;
  1357. }
  1358. }
  1359. if (crl->idp_flags & IDP_INVALID) {
  1360. ctx->error = X509_V_ERR_INVALID_EXTENSION;
  1361. ok = ctx->verify_cb(0, ctx);
  1362. if (!ok)
  1363. goto err;
  1364. }
  1365. }
  1366. if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
  1367. ok = check_crl_time(ctx, crl, 1);
  1368. if (!ok)
  1369. goto err;
  1370. }
  1371. /* Attempt to get issuer certificate public key */
  1372. ikey = X509_get_pubkey(issuer);
  1373. if (!ikey) {
  1374. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1375. ok = ctx->verify_cb(0, ctx);
  1376. if (!ok)
  1377. goto err;
  1378. } else {
  1379. int rv;
  1380. rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
  1381. if (rv != X509_V_OK) {
  1382. ctx->error = rv;
  1383. ok = ctx->verify_cb(0, ctx);
  1384. if (!ok)
  1385. goto err;
  1386. }
  1387. /* Verify CRL signature */
  1388. if (X509_CRL_verify(crl, ikey) <= 0) {
  1389. ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
  1390. ok = ctx->verify_cb(0, ctx);
  1391. if (!ok)
  1392. goto err;
  1393. }
  1394. }
  1395. }
  1396. ok = 1;
  1397. err:
  1398. EVP_PKEY_free(ikey);
  1399. return ok;
  1400. }
  1401. /* Check certificate against CRL */
  1402. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1403. {
  1404. int ok;
  1405. X509_REVOKED *rev;
  1406. /*
  1407. * The rules changed for this... previously if a CRL contained unhandled
  1408. * critical extensions it could still be used to indicate a certificate
  1409. * was revoked. This has since been changed since critical extension can
  1410. * change the meaning of CRL entries.
  1411. */
  1412. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1413. && (crl->flags & EXFLAG_CRITICAL)) {
  1414. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  1415. ok = ctx->verify_cb(0, ctx);
  1416. if (!ok)
  1417. return 0;
  1418. }
  1419. /*
  1420. * Look for serial number of certificate in CRL If found make sure reason
  1421. * is not removeFromCRL.
  1422. */
  1423. if (X509_CRL_get0_by_cert(crl, &rev, x)) {
  1424. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1425. return 2;
  1426. ctx->error = X509_V_ERR_CERT_REVOKED;
  1427. ok = ctx->verify_cb(0, ctx);
  1428. if (!ok)
  1429. return 0;
  1430. }
  1431. return 1;
  1432. }
  1433. static int check_policy(X509_STORE_CTX *ctx)
  1434. {
  1435. int ret;
  1436. if (ctx->parent)
  1437. return 1;
  1438. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1439. ctx->param->policies, ctx->param->flags);
  1440. if (ret == 0) {
  1441. X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
  1442. return 0;
  1443. }
  1444. /* Invalid or inconsistent extensions */
  1445. if (ret == -1) {
  1446. /*
  1447. * Locate certificates with bad extensions and notify callback.
  1448. */
  1449. X509 *x;
  1450. int i;
  1451. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  1452. x = sk_X509_value(ctx->chain, i);
  1453. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1454. continue;
  1455. ctx->current_cert = x;
  1456. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  1457. if (!ctx->verify_cb(0, ctx))
  1458. return 0;
  1459. }
  1460. return 1;
  1461. }
  1462. if (ret == -2) {
  1463. ctx->current_cert = NULL;
  1464. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1465. return ctx->verify_cb(0, ctx);
  1466. }
  1467. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  1468. ctx->current_cert = NULL;
  1469. ctx->error = X509_V_OK;
  1470. if (!ctx->verify_cb(2, ctx))
  1471. return 0;
  1472. }
  1473. return 1;
  1474. }
  1475. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  1476. {
  1477. time_t *ptime;
  1478. int i;
  1479. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1480. ptime = &ctx->param->check_time;
  1481. else
  1482. ptime = NULL;
  1483. i = X509_cmp_time(X509_get_notBefore(x), ptime);
  1484. if (i == 0) {
  1485. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  1486. ctx->current_cert = x;
  1487. if (!ctx->verify_cb(0, ctx))
  1488. return 0;
  1489. }
  1490. if (i > 0) {
  1491. ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
  1492. ctx->current_cert = x;
  1493. if (!ctx->verify_cb(0, ctx))
  1494. return 0;
  1495. }
  1496. i = X509_cmp_time(X509_get_notAfter(x), ptime);
  1497. if (i == 0) {
  1498. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  1499. ctx->current_cert = x;
  1500. if (!ctx->verify_cb(0, ctx))
  1501. return 0;
  1502. }
  1503. if (i < 0) {
  1504. ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
  1505. ctx->current_cert = x;
  1506. if (!ctx->verify_cb(0, ctx))
  1507. return 0;
  1508. }
  1509. return 1;
  1510. }
  1511. static int internal_verify(X509_STORE_CTX *ctx)
  1512. {
  1513. int ok = 0, n;
  1514. X509 *xs, *xi;
  1515. EVP_PKEY *pkey = NULL;
  1516. int (*cb) (int xok, X509_STORE_CTX *xctx);
  1517. cb = ctx->verify_cb;
  1518. n = sk_X509_num(ctx->chain);
  1519. ctx->error_depth = n - 1;
  1520. n--;
  1521. xi = sk_X509_value(ctx->chain, n);
  1522. if (ctx->check_issued(ctx, xi, xi))
  1523. xs = xi;
  1524. else {
  1525. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  1526. xs = xi;
  1527. goto check_cert;
  1528. }
  1529. if (n <= 0) {
  1530. ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  1531. ctx->current_cert = xi;
  1532. ok = cb(0, ctx);
  1533. goto end;
  1534. } else {
  1535. n--;
  1536. ctx->error_depth = n;
  1537. xs = sk_X509_value(ctx->chain, n);
  1538. }
  1539. }
  1540. /* ctx->error=0; not needed */
  1541. while (n >= 0) {
  1542. ctx->error_depth = n;
  1543. /*
  1544. * Skip signature check for self signed certificates unless
  1545. * explicitly asked for. It doesn't add any security and just wastes
  1546. * time.
  1547. */
  1548. if (!xs->valid
  1549. && (xs != xi
  1550. || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
  1551. if ((pkey = X509_get_pubkey(xi)) == NULL) {
  1552. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1553. ctx->current_cert = xi;
  1554. ok = (*cb) (0, ctx);
  1555. if (!ok)
  1556. goto end;
  1557. } else if (X509_verify(xs, pkey) <= 0) {
  1558. ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
  1559. ctx->current_cert = xs;
  1560. ok = (*cb) (0, ctx);
  1561. if (!ok) {
  1562. EVP_PKEY_free(pkey);
  1563. goto end;
  1564. }
  1565. }
  1566. EVP_PKEY_free(pkey);
  1567. pkey = NULL;
  1568. }
  1569. xs->valid = 1;
  1570. check_cert:
  1571. ok = check_cert_time(ctx, xs);
  1572. if (!ok)
  1573. goto end;
  1574. /* The last error (if any) is still in the error value */
  1575. ctx->current_issuer = xi;
  1576. ctx->current_cert = xs;
  1577. ok = (*cb) (1, ctx);
  1578. if (!ok)
  1579. goto end;
  1580. n--;
  1581. if (n >= 0) {
  1582. xi = xs;
  1583. xs = sk_X509_value(ctx->chain, n);
  1584. }
  1585. }
  1586. ok = 1;
  1587. end:
  1588. return ok;
  1589. }
  1590. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1591. {
  1592. return X509_cmp_time(ctm, NULL);
  1593. }
  1594. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1595. {
  1596. char *str;
  1597. ASN1_TIME atm;
  1598. long offset;
  1599. char buff1[24], buff2[24], *p;
  1600. int i, j, remaining;
  1601. p = buff1;
  1602. remaining = ctm->length;
  1603. str = (char *)ctm->data;
  1604. /*
  1605. * Note that the following (historical) code allows much more slack in the
  1606. * time format than RFC5280. In RFC5280, the representation is fixed:
  1607. * UTCTime: YYMMDDHHMMSSZ
  1608. * GeneralizedTime: YYYYMMDDHHMMSSZ
  1609. */
  1610. if (ctm->type == V_ASN1_UTCTIME) {
  1611. /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
  1612. int min_length = sizeof("YYMMDDHHMMZ") - 1;
  1613. int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
  1614. if (remaining < min_length || remaining > max_length)
  1615. return 0;
  1616. memcpy(p, str, 10);
  1617. p += 10;
  1618. str += 10;
  1619. remaining -= 10;
  1620. } else {
  1621. /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
  1622. int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
  1623. int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
  1624. if (remaining < min_length || remaining > max_length)
  1625. return 0;
  1626. memcpy(p, str, 12);
  1627. p += 12;
  1628. str += 12;
  1629. remaining -= 12;
  1630. }
  1631. if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
  1632. *(p++) = '0';
  1633. *(p++) = '0';
  1634. } else {
  1635. /* SS (seconds) */
  1636. if (remaining < 2)
  1637. return 0;
  1638. *(p++) = *(str++);
  1639. *(p++) = *(str++);
  1640. remaining -= 2;
  1641. /*
  1642. * Skip any (up to three) fractional seconds...
  1643. * TODO(emilia): in RFC5280, fractional seconds are forbidden.
  1644. * Can we just kill them altogether?
  1645. */
  1646. if (remaining && *str == '.') {
  1647. str++;
  1648. remaining--;
  1649. for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
  1650. if (*str < '0' || *str > '9')
  1651. break;
  1652. }
  1653. }
  1654. }
  1655. *(p++) = 'Z';
  1656. *(p++) = '\0';
  1657. /* We now need either a terminating 'Z' or an offset. */
  1658. if (!remaining)
  1659. return 0;
  1660. if (*str == 'Z') {
  1661. if (remaining != 1)
  1662. return 0;
  1663. offset = 0;
  1664. } else {
  1665. /* (+-)HHMM */
  1666. if ((*str != '+') && (*str != '-'))
  1667. return 0;
  1668. /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
  1669. if (remaining != 5)
  1670. return 0;
  1671. if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
  1672. str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
  1673. return 0;
  1674. offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
  1675. offset += (str[3] - '0') * 10 + (str[4] - '0');
  1676. if (*str == '-')
  1677. offset = -offset;
  1678. }
  1679. atm.type = ctm->type;
  1680. atm.flags = 0;
  1681. atm.length = sizeof(buff2);
  1682. atm.data = (unsigned char *)buff2;
  1683. if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
  1684. return 0;
  1685. if (ctm->type == V_ASN1_UTCTIME) {
  1686. i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
  1687. if (i < 50)
  1688. i += 100; /* cf. RFC 2459 */
  1689. j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
  1690. if (j < 50)
  1691. j += 100;
  1692. if (i < j)
  1693. return -1;
  1694. if (i > j)
  1695. return 1;
  1696. }
  1697. i = strcmp(buff1, buff2);
  1698. if (i == 0) /* wait a second then return younger :-) */
  1699. return -1;
  1700. else
  1701. return i;
  1702. }
  1703. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1704. {
  1705. return X509_time_adj(s, adj, NULL);
  1706. }
  1707. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1708. {
  1709. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1710. }
  1711. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1712. int offset_day, long offset_sec, time_t *in_tm)
  1713. {
  1714. time_t t;
  1715. if (in_tm)
  1716. t = *in_tm;
  1717. else
  1718. time(&t);
  1719. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
  1720. if (s->type == V_ASN1_UTCTIME)
  1721. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  1722. if (s->type == V_ASN1_GENERALIZEDTIME)
  1723. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  1724. }
  1725. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1726. }
  1727. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  1728. {
  1729. EVP_PKEY *ktmp = NULL, *ktmp2;
  1730. int i, j;
  1731. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
  1732. return 1;
  1733. for (i = 0; i < sk_X509_num(chain); i++) {
  1734. ktmp = X509_get_pubkey(sk_X509_value(chain, i));
  1735. if (ktmp == NULL) {
  1736. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1737. X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1738. return 0;
  1739. }
  1740. if (!EVP_PKEY_missing_parameters(ktmp))
  1741. break;
  1742. else {
  1743. EVP_PKEY_free(ktmp);
  1744. ktmp = NULL;
  1745. }
  1746. }
  1747. if (ktmp == NULL) {
  1748. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1749. X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1750. return 0;
  1751. }
  1752. /* first, populate the other certs */
  1753. for (j = i - 1; j >= 0; j--) {
  1754. ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
  1755. EVP_PKEY_copy_parameters(ktmp2, ktmp);
  1756. EVP_PKEY_free(ktmp2);
  1757. }
  1758. if (pkey != NULL)
  1759. EVP_PKEY_copy_parameters(pkey, ktmp);
  1760. EVP_PKEY_free(ktmp);
  1761. return 1;
  1762. }
  1763. /* Make a delta CRL as the diff between two full CRLs */
  1764. X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
  1765. EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
  1766. {
  1767. X509_CRL *crl = NULL;
  1768. int i;
  1769. STACK_OF(X509_REVOKED) *revs = NULL;
  1770. /* CRLs can't be delta already */
  1771. if (base->base_crl_number || newer->base_crl_number) {
  1772. X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
  1773. return NULL;
  1774. }
  1775. /* Base and new CRL must have a CRL number */
  1776. if (!base->crl_number || !newer->crl_number) {
  1777. X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
  1778. return NULL;
  1779. }
  1780. /* Issuer names must match */
  1781. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
  1782. X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
  1783. return NULL;
  1784. }
  1785. /* AKID and IDP must match */
  1786. if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
  1787. X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
  1788. return NULL;
  1789. }
  1790. if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
  1791. X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
  1792. return NULL;
  1793. }
  1794. /* Newer CRL number must exceed full CRL number */
  1795. if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
  1796. X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
  1797. return NULL;
  1798. }
  1799. /* CRLs must verify */
  1800. if (skey && (X509_CRL_verify(base, skey) <= 0 ||
  1801. X509_CRL_verify(newer, skey) <= 0)) {
  1802. X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
  1803. return NULL;
  1804. }
  1805. /* Create new CRL */
  1806. crl = X509_CRL_new();
  1807. if (!crl || !X509_CRL_set_version(crl, 1))
  1808. goto memerr;
  1809. /* Set issuer name */
  1810. if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
  1811. goto memerr;
  1812. if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
  1813. goto memerr;
  1814. if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
  1815. goto memerr;
  1816. /* Set base CRL number: must be critical */
  1817. if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
  1818. goto memerr;
  1819. /*
  1820. * Copy extensions across from newest CRL to delta: this will set CRL
  1821. * number to correct value too.
  1822. */
  1823. for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
  1824. X509_EXTENSION *ext;
  1825. ext = X509_CRL_get_ext(newer, i);
  1826. if (!X509_CRL_add_ext(crl, ext, -1))
  1827. goto memerr;
  1828. }
  1829. /* Go through revoked entries, copying as needed */
  1830. revs = X509_CRL_get_REVOKED(newer);
  1831. for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
  1832. X509_REVOKED *rvn, *rvtmp;
  1833. rvn = sk_X509_REVOKED_value(revs, i);
  1834. /*
  1835. * Add only if not also in base. TODO: need something cleverer here
  1836. * for some more complex CRLs covering multiple CAs.
  1837. */
  1838. if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
  1839. rvtmp = X509_REVOKED_dup(rvn);
  1840. if (!rvtmp)
  1841. goto memerr;
  1842. if (!X509_CRL_add0_revoked(crl, rvtmp)) {
  1843. X509_REVOKED_free(rvtmp);
  1844. goto memerr;
  1845. }
  1846. }
  1847. }
  1848. /* TODO: optionally prune deleted entries */
  1849. if (skey && md && !X509_CRL_sign(crl, skey, md))
  1850. goto memerr;
  1851. return crl;
  1852. memerr:
  1853. X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
  1854. if (crl)
  1855. X509_CRL_free(crl);
  1856. return NULL;
  1857. }
  1858. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
  1859. CRYPTO_EX_new *new_func,
  1860. CRYPTO_EX_dup *dup_func,
  1861. CRYPTO_EX_free *free_func)
  1862. {
  1863. /*
  1864. * This function is (usually) called only once, by
  1865. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
  1866. */
  1867. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  1868. new_func, dup_func, free_func);
  1869. }
  1870. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1871. {
  1872. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1873. }
  1874. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1875. {
  1876. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1877. }
  1878. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1879. {
  1880. return ctx->error;
  1881. }
  1882. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1883. {
  1884. ctx->error = err;
  1885. }
  1886. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1887. {
  1888. return ctx->error_depth;
  1889. }
  1890. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1891. {
  1892. return ctx->current_cert;
  1893. }
  1894. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1895. {
  1896. return ctx->chain;
  1897. }
  1898. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1899. {
  1900. if (!ctx->chain)
  1901. return NULL;
  1902. return X509_chain_up_ref(ctx->chain);
  1903. }
  1904. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1905. {
  1906. return ctx->current_issuer;
  1907. }
  1908. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1909. {
  1910. return ctx->current_crl;
  1911. }
  1912. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1913. {
  1914. return ctx->parent;
  1915. }
  1916. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1917. {
  1918. ctx->cert = x;
  1919. }
  1920. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1921. {
  1922. ctx->untrusted = sk;
  1923. }
  1924. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1925. {
  1926. ctx->crls = sk;
  1927. }
  1928. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1929. {
  1930. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1931. }
  1932. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1933. {
  1934. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1935. }
  1936. /*
  1937. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1938. * This is intended to be used when another structure has its own trust and
  1939. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1940. * set then we will usually have a default purpose in mind which should then
  1941. * be used to set the trust value. An example of this is SSL use: an SSL
  1942. * structure will have its own purpose and trust settings which the
  1943. * application can set: if they aren't set then we use the default of SSL
  1944. * client/server.
  1945. */
  1946. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1947. int purpose, int trust)
  1948. {
  1949. int idx;
  1950. /* If purpose not set use default */
  1951. if (!purpose)
  1952. purpose = def_purpose;
  1953. /* If we have a purpose then check it is valid */
  1954. if (purpose) {
  1955. X509_PURPOSE *ptmp;
  1956. idx = X509_PURPOSE_get_by_id(purpose);
  1957. if (idx == -1) {
  1958. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1959. X509_R_UNKNOWN_PURPOSE_ID);
  1960. return 0;
  1961. }
  1962. ptmp = X509_PURPOSE_get0(idx);
  1963. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1964. idx = X509_PURPOSE_get_by_id(def_purpose);
  1965. if (idx == -1) {
  1966. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1967. X509_R_UNKNOWN_PURPOSE_ID);
  1968. return 0;
  1969. }
  1970. ptmp = X509_PURPOSE_get0(idx);
  1971. }
  1972. /* If trust not set then get from purpose default */
  1973. if (!trust)
  1974. trust = ptmp->trust;
  1975. }
  1976. if (trust) {
  1977. idx = X509_TRUST_get_by_id(trust);
  1978. if (idx == -1) {
  1979. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1980. X509_R_UNKNOWN_TRUST_ID);
  1981. return 0;
  1982. }
  1983. }
  1984. if (purpose && !ctx->param->purpose)
  1985. ctx->param->purpose = purpose;
  1986. if (trust && !ctx->param->trust)
  1987. ctx->param->trust = trust;
  1988. return 1;
  1989. }
  1990. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1991. {
  1992. X509_STORE_CTX *ctx;
  1993. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1994. if (!ctx) {
  1995. X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
  1996. return NULL;
  1997. }
  1998. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1999. return ctx;
  2000. }
  2001. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  2002. {
  2003. if (!ctx)
  2004. return;
  2005. X509_STORE_CTX_cleanup(ctx);
  2006. OPENSSL_free(ctx);
  2007. }
  2008. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  2009. STACK_OF(X509) *chain)
  2010. {
  2011. int ret = 1;
  2012. ctx->ctx = store;
  2013. ctx->current_method = 0;
  2014. ctx->cert = x509;
  2015. ctx->untrusted = chain;
  2016. ctx->crls = NULL;
  2017. ctx->last_untrusted = 0;
  2018. ctx->other_ctx = NULL;
  2019. ctx->valid = 0;
  2020. ctx->chain = NULL;
  2021. ctx->error = 0;
  2022. ctx->explicit_policy = 0;
  2023. ctx->error_depth = 0;
  2024. ctx->current_cert = NULL;
  2025. ctx->current_issuer = NULL;
  2026. ctx->current_crl = NULL;
  2027. ctx->current_crl_score = 0;
  2028. ctx->current_reasons = 0;
  2029. ctx->tree = NULL;
  2030. ctx->parent = NULL;
  2031. ctx->param = X509_VERIFY_PARAM_new();
  2032. if (!ctx->param) {
  2033. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2034. return 0;
  2035. }
  2036. /*
  2037. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  2038. */
  2039. if (store)
  2040. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  2041. else
  2042. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  2043. if (store) {
  2044. ctx->verify_cb = store->verify_cb;
  2045. ctx->cleanup = store->cleanup;
  2046. } else
  2047. ctx->cleanup = 0;
  2048. if (ret)
  2049. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  2050. X509_VERIFY_PARAM_lookup("default"));
  2051. if (ret == 0) {
  2052. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2053. return 0;
  2054. }
  2055. if (store && store->check_issued)
  2056. ctx->check_issued = store->check_issued;
  2057. else
  2058. ctx->check_issued = check_issued;
  2059. if (store && store->get_issuer)
  2060. ctx->get_issuer = store->get_issuer;
  2061. else
  2062. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  2063. if (store && store->verify_cb)
  2064. ctx->verify_cb = store->verify_cb;
  2065. else
  2066. ctx->verify_cb = null_callback;
  2067. if (store && store->verify)
  2068. ctx->verify = store->verify;
  2069. else
  2070. ctx->verify = internal_verify;
  2071. if (store && store->check_revocation)
  2072. ctx->check_revocation = store->check_revocation;
  2073. else
  2074. ctx->check_revocation = check_revocation;
  2075. if (store && store->get_crl)
  2076. ctx->get_crl = store->get_crl;
  2077. else
  2078. ctx->get_crl = NULL;
  2079. if (store && store->check_crl)
  2080. ctx->check_crl = store->check_crl;
  2081. else
  2082. ctx->check_crl = check_crl;
  2083. if (store && store->cert_crl)
  2084. ctx->cert_crl = store->cert_crl;
  2085. else
  2086. ctx->cert_crl = cert_crl;
  2087. if (store && store->lookup_certs)
  2088. ctx->lookup_certs = store->lookup_certs;
  2089. else
  2090. ctx->lookup_certs = X509_STORE_get1_certs;
  2091. if (store && store->lookup_crls)
  2092. ctx->lookup_crls = store->lookup_crls;
  2093. else
  2094. ctx->lookup_crls = X509_STORE_get1_crls;
  2095. ctx->check_policy = check_policy;
  2096. /*
  2097. * This memset() can't make any sense anyway, so it's removed. As
  2098. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  2099. * corresponding "new" here and remove this bogus initialisation.
  2100. */
  2101. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  2102. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  2103. &(ctx->ex_data))) {
  2104. OPENSSL_free(ctx);
  2105. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2106. return 0;
  2107. }
  2108. return 1;
  2109. }
  2110. /*
  2111. * Set alternative lookup method: just a STACK of trusted certificates. This
  2112. * avoids X509_STORE nastiness where it isn't needed.
  2113. */
  2114. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2115. {
  2116. ctx->other_ctx = sk;
  2117. ctx->get_issuer = get_issuer_sk;
  2118. }
  2119. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  2120. {
  2121. if (ctx->cleanup)
  2122. ctx->cleanup(ctx);
  2123. if (ctx->param != NULL) {
  2124. if (ctx->parent == NULL)
  2125. X509_VERIFY_PARAM_free(ctx->param);
  2126. ctx->param = NULL;
  2127. }
  2128. if (ctx->tree != NULL) {
  2129. X509_policy_tree_free(ctx->tree);
  2130. ctx->tree = NULL;
  2131. }
  2132. if (ctx->chain != NULL) {
  2133. sk_X509_pop_free(ctx->chain, X509_free);
  2134. ctx->chain = NULL;
  2135. }
  2136. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  2137. memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
  2138. }
  2139. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  2140. {
  2141. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2142. }
  2143. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  2144. {
  2145. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  2146. }
  2147. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  2148. time_t t)
  2149. {
  2150. X509_VERIFY_PARAM_set_time(ctx->param, t);
  2151. }
  2152. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  2153. int (*verify_cb) (int, X509_STORE_CTX *))
  2154. {
  2155. ctx->verify_cb = verify_cb;
  2156. }
  2157. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  2158. {
  2159. return ctx->tree;
  2160. }
  2161. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  2162. {
  2163. return ctx->explicit_policy;
  2164. }
  2165. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  2166. {
  2167. const X509_VERIFY_PARAM *param;
  2168. param = X509_VERIFY_PARAM_lookup(name);
  2169. if (!param)
  2170. return 0;
  2171. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  2172. }
  2173. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  2174. {
  2175. return ctx->param;
  2176. }
  2177. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  2178. {
  2179. if (ctx->param)
  2180. X509_VERIFY_PARAM_free(ctx->param);
  2181. ctx->param = param;
  2182. }
  2183. IMPLEMENT_STACK_OF(X509)
  2184. IMPLEMENT_ASN1_SET_OF(X509)
  2185. IMPLEMENT_STACK_OF(X509_NAME)
  2186. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  2187. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)