x509_vfy.c 77 KB

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