x509_str.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /* x509_str.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #if !defined(WOLFSSL_X509_STORE_INCLUDED)
  26. #ifndef WOLFSSL_IGNORE_FILE_WARN
  27. #warning x509_str.c does not need to be compiled separately from ssl.c
  28. #endif
  29. #else
  30. #ifndef WOLFCRYPT_ONLY
  31. #ifndef NO_CERTS
  32. /*******************************************************************************
  33. * START OF X509_STORE_CTX APIs
  34. ******************************************************************************/
  35. #ifdef OPENSSL_EXTRA
  36. WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
  37. {
  38. WOLFSSL_X509_STORE_CTX* ctx;
  39. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_new");
  40. ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(sizeof(WOLFSSL_X509_STORE_CTX), NULL,
  41. DYNAMIC_TYPE_X509_CTX);
  42. if (ctx != NULL) {
  43. ctx->param = NULL;
  44. if (wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL) !=
  45. WOLFSSL_SUCCESS) {
  46. XFREE(ctx, NULL, DYNAMIC_TYPE_X509_CTX);
  47. ctx = NULL;
  48. }
  49. }
  50. return ctx;
  51. }
  52. int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
  53. WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, WOLF_STACK_OF(WOLFSSL_X509)* sk)
  54. {
  55. int ret = 0;
  56. (void)sk;
  57. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
  58. if (ctx != NULL) {
  59. ctx->store = store;
  60. #ifndef WOLFSSL_X509_STORE_CERTS
  61. ctx->current_cert = x509;
  62. #else
  63. if(x509 != NULL){
  64. ctx->current_cert = wolfSSL_X509_d2i(NULL, x509->derCert->buffer,
  65. x509->derCert->length);
  66. if(ctx->current_cert == NULL)
  67. return WOLFSSL_FAILURE;
  68. } else
  69. ctx->current_cert = NULL;
  70. #endif
  71. ctx->chain = sk;
  72. /* Add intermediate certificates from stack to store */
  73. while (sk != NULL) {
  74. WOLFSSL_X509* x509_cert = sk->data.x509;
  75. if (x509_cert != NULL && x509_cert->isCa) {
  76. ret = wolfSSL_X509_STORE_add_cert(store, x509_cert);
  77. if (ret < 0) {
  78. return WOLFSSL_FAILURE;
  79. }
  80. }
  81. sk = sk->next;
  82. }
  83. ctx->sesChain = NULL;
  84. ctx->domain = NULL;
  85. #ifdef HAVE_EX_DATA
  86. XMEMSET(&ctx->ex_data, 0, sizeof(ctx->ex_data));
  87. #endif
  88. ctx->userCtx = NULL;
  89. ctx->error = 0;
  90. ctx->error_depth = 0;
  91. ctx->discardSessionCerts = 0;
  92. if (ctx->param == NULL) {
  93. ctx->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  94. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  95. NULL, DYNAMIC_TYPE_OPENSSL);
  96. if (ctx->param == NULL){
  97. WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_init failed");
  98. return WOLFSSL_FAILURE;
  99. }
  100. }
  101. return WOLFSSL_SUCCESS;
  102. }
  103. return WOLFSSL_FAILURE;
  104. }
  105. /* free's extra data */
  106. void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
  107. {
  108. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_free");
  109. if (ctx != NULL) {
  110. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  111. wolfSSL_CRYPTO_cleanup_ex_data(&ctx->ex_data);
  112. #endif
  113. if (ctx->param != NULL) {
  114. XFREE(ctx->param, NULL, DYNAMIC_TYPE_OPENSSL);
  115. ctx->param = NULL;
  116. }
  117. XFREE(ctx, NULL, DYNAMIC_TYPE_X509_CTX);
  118. }
  119. }
  120. void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
  121. {
  122. if (ctx != NULL) {
  123. if (ctx->param != NULL) {
  124. XFREE(ctx->param, NULL, DYNAMIC_TYPE_OPENSSL);
  125. ctx->param = NULL;
  126. }
  127. wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
  128. }
  129. }
  130. void wolfSSL_X509_STORE_CTX_trusted_stack(WOLFSSL_X509_STORE_CTX *ctx, WOLF_STACK_OF(WOLFSSL_X509) *sk)
  131. {
  132. if (ctx != NULL) {
  133. ctx->chain = sk;
  134. }
  135. }
  136. /* Returns corresponding X509 error from internal ASN error <e> */
  137. int GetX509Error(int e)
  138. {
  139. switch (e) {
  140. case ASN_BEFORE_DATE_E:
  141. return WOLFSSL_X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  142. case ASN_AFTER_DATE_E:
  143. return WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED;
  144. case ASN_NO_SIGNER_E: /* get issuer error if no CA found locally */
  145. return WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  146. case ASN_SELF_SIGNED_E:
  147. return WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  148. case ASN_PATHLEN_INV_E:
  149. case ASN_PATHLEN_SIZE_E:
  150. return WOLFSSL_X509_V_ERR_PATH_LENGTH_EXCEEDED;
  151. case ASN_SIG_OID_E:
  152. case ASN_SIG_CONFIRM_E:
  153. case ASN_SIG_HASH_E:
  154. case ASN_SIG_KEY_E:
  155. return WOLFSSL_X509_V_ERR_CERT_SIGNATURE_FAILURE;
  156. case CRL_CERT_REVOKED:
  157. return WOLFSSL_X509_V_ERR_CERT_REVOKED;
  158. default:
  159. #ifdef HAVE_WOLFSSL_MSG_EX
  160. WOLFSSL_MSG_EX("Error not configured or implemented yet: %d", e);
  161. #else
  162. WOLFSSL_MSG("Error not configured or implemented yet");
  163. #endif
  164. return e;
  165. }
  166. }
  167. /* Verifies certificate chain using WOLFSSL_X509_STORE_CTX
  168. * returns 0 on success or < 0 on failure.
  169. */
  170. int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
  171. {
  172. WOLFSSL_ENTER("wolfSSL_X509_verify_cert");
  173. if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
  174. && ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) {
  175. int ret = 0;
  176. int depth = 0;
  177. int error;
  178. #ifndef NO_ASN_TIME
  179. byte *afterDate, *beforeDate;
  180. #endif
  181. ret = wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
  182. ctx->current_cert->derCert->buffer,
  183. ctx->current_cert->derCert->length,
  184. WOLFSSL_FILETYPE_ASN1);
  185. /* If there was an error, process it and add it to CTX */
  186. if (ret < 0) {
  187. /* Get corresponding X509 error */
  188. error = GetX509Error(ret);
  189. /* Set error depth */
  190. if (ctx->chain)
  191. depth = (int)ctx->chain->num;
  192. wolfSSL_X509_STORE_CTX_set_error(ctx, error);
  193. wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
  194. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  195. if (ctx->store && ctx->store->verify_cb)
  196. ctx->store->verify_cb(0, ctx);
  197. #endif
  198. }
  199. #ifndef NO_ASN_TIME
  200. error = 0;
  201. /* wolfSSL_CertManagerVerifyBuffer only returns ASN_AFTER_DATE_E or
  202. ASN_BEFORE_DATE_E if there are no additional errors found in the
  203. cert. Therefore, check if the cert is expired or not yet valid
  204. in order to return the correct expected error. */
  205. afterDate = ctx->current_cert->notAfter.data;
  206. beforeDate = ctx->current_cert->notBefore.data;
  207. if (XVALIDATE_DATE(afterDate, (byte)ctx->current_cert->notAfter.type,
  208. AFTER) < 1) {
  209. error = WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED;
  210. }
  211. else if (XVALIDATE_DATE(beforeDate,
  212. (byte)ctx->current_cert->notBefore.type, BEFORE) < 1) {
  213. error = WOLFSSL_X509_V_ERR_CERT_NOT_YET_VALID;
  214. }
  215. if (error != 0 ) {
  216. wolfSSL_X509_STORE_CTX_set_error(ctx, error);
  217. wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
  218. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  219. if (ctx->store && ctx->store->verify_cb)
  220. ctx->store->verify_cb(0, ctx);
  221. #endif
  222. }
  223. #endif
  224. /* OpenSSL returns 0 when a chain can't be built */
  225. if (ret == ASN_NO_SIGNER_E)
  226. return WOLFSSL_FAILURE;
  227. else
  228. return ret;
  229. }
  230. return WOLFSSL_FATAL_ERROR;
  231. }
  232. #endif /* OPENSSL_EXTRA */
  233. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  234. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
  235. WOLFSSL_X509_STORE_CTX* ctx)
  236. {
  237. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
  238. if (ctx)
  239. return ctx->current_cert;
  240. return NULL;
  241. }
  242. int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
  243. {
  244. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
  245. if (ctx != NULL)
  246. return ctx->error;
  247. return 0;
  248. }
  249. int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
  250. {
  251. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
  252. if(ctx)
  253. return ctx->error_depth;
  254. return WOLFSSL_FATAL_ERROR;
  255. }
  256. /* get X509_STORE_CTX ex_data, max idx is MAX_EX_DATA */
  257. void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
  258. {
  259. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
  260. #ifdef HAVE_EX_DATA
  261. if (ctx != NULL) {
  262. return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
  263. }
  264. #else
  265. (void)ctx;
  266. (void)idx;
  267. #endif
  268. return NULL;
  269. }
  270. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  271. #ifdef OPENSSL_EXTRA
  272. void wolfSSL_X509_STORE_CTX_set_verify_cb(WOLFSSL_X509_STORE_CTX *ctx,
  273. WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
  274. {
  275. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_verify_cb");
  276. if(ctx == NULL)
  277. return;
  278. ctx->verify_cb = verify_cb;
  279. }
  280. /* Gets pointer to X509_STORE that was used to create context.
  281. *
  282. * Return valid pointer on success, NULL if ctx was NULL or not initialized
  283. */
  284. WOLFSSL_X509_STORE* wolfSSL_X509_STORE_CTX_get0_store(
  285. WOLFSSL_X509_STORE_CTX* ctx)
  286. {
  287. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_store");
  288. if (ctx == NULL)
  289. return NULL;
  290. return ctx->store;
  291. }
  292. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_cert(WOLFSSL_X509_STORE_CTX* ctx)
  293. {
  294. if (ctx == NULL)
  295. return NULL;
  296. return ctx->current_cert;
  297. }
  298. void wolfSSL_X509_STORE_CTX_set_time(WOLFSSL_X509_STORE_CTX* ctx,
  299. unsigned long flags,
  300. time_t t)
  301. {
  302. (void)flags;
  303. if (ctx == NULL || ctx->param == NULL)
  304. return;
  305. ctx->param->check_time = t;
  306. ctx->param->flags |= WOLFSSL_USE_CHECK_TIME;
  307. }
  308. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  309. #ifndef NO_WOLFSSL_STUB
  310. int wolfSSL_X509_STORE_CTX_set_purpose(WOLFSSL_X509_STORE_CTX *ctx,
  311. int purpose)
  312. {
  313. (void)ctx;
  314. (void)purpose;
  315. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_purpose (not implemented)");
  316. return 0;
  317. }
  318. void wolfSSL_X509_STORE_CTX_set_flags(WOLFSSL_X509_STORE_CTX *ctx,
  319. unsigned long flags)
  320. {
  321. (void)ctx;
  322. (void)flags;
  323. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_flags (not implemented)");
  324. }
  325. #endif /* !NO_WOLFSSL_STUB */
  326. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  327. #endif /* OPENSSL_EXTRA */
  328. #ifdef OPENSSL_EXTRA
  329. /* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
  330. * on success, WOLFSSL_FAILURE on error. */
  331. int wolfSSL_X509_STORE_CTX_set_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx,
  332. void *data)
  333. {
  334. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data");
  335. #ifdef HAVE_EX_DATA
  336. if (ctx != NULL)
  337. {
  338. return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  339. }
  340. #else
  341. (void)ctx;
  342. (void)idx;
  343. (void)data;
  344. #endif
  345. return WOLFSSL_FAILURE;
  346. }
  347. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  348. /* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
  349. * on success, WOLFSSL_FAILURE on error. */
  350. int wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup(
  351. WOLFSSL_X509_STORE_CTX* ctx,
  352. int idx,
  353. void *data,
  354. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  355. {
  356. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup");
  357. if (ctx != NULL)
  358. {
  359. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&ctx->ex_data, idx, data,
  360. cleanup_routine);
  361. }
  362. return WOLFSSL_FAILURE;
  363. }
  364. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  365. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL)
  366. void wolfSSL_X509_STORE_CTX_set_depth(WOLFSSL_X509_STORE_CTX* ctx, int depth)
  367. {
  368. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_depth");
  369. if (ctx)
  370. ctx->depth = depth;
  371. }
  372. #endif
  373. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_current_issuer(
  374. WOLFSSL_X509_STORE_CTX* ctx)
  375. {
  376. int ret;
  377. WOLFSSL_X509* issuer;
  378. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_current_issuer");
  379. if (ctx == NULL) {
  380. return NULL;
  381. }
  382. ret = wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, ctx->current_cert);
  383. if (ret == WOLFSSL_SUCCESS) {
  384. return issuer;
  385. }
  386. return NULL;
  387. }
  388. /* Set an error stat in the X509 STORE CTX
  389. *
  390. */
  391. void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int er)
  392. {
  393. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error");
  394. if (ctx != NULL) {
  395. ctx->error = er;
  396. }
  397. }
  398. /* Set the error depth in the X509 STORE CTX */
  399. void wolfSSL_X509_STORE_CTX_set_error_depth(WOLFSSL_X509_STORE_CTX* ctx,
  400. int depth)
  401. {
  402. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error_depth");
  403. if (ctx != NULL) {
  404. ctx->error_depth = depth;
  405. }
  406. }
  407. WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get_chain(WOLFSSL_X509_STORE_CTX* ctx)
  408. {
  409. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_chain");
  410. if (ctx == NULL) {
  411. return NULL;
  412. }
  413. #ifdef SESSION_CERTS
  414. /* if chain is null but sesChain is available then populate stack */
  415. if (ctx->chain == NULL && ctx->sesChain != NULL) {
  416. int i;
  417. WOLFSSL_X509_CHAIN* c = ctx->sesChain;
  418. WOLFSSL_STACK* sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK),
  419. NULL, DYNAMIC_TYPE_X509);
  420. if (sk == NULL) {
  421. return NULL;
  422. }
  423. XMEMSET(sk, 0, sizeof(WOLFSSL_STACK));
  424. for (i = 0; i < c->count && i < MAX_CHAIN_DEPTH; i++) {
  425. WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, i);
  426. if (x509 == NULL) {
  427. WOLFSSL_MSG("Unable to get x509 from chain");
  428. wolfSSL_sk_X509_pop_free(sk, NULL);
  429. return NULL;
  430. }
  431. if (wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
  432. WOLFSSL_MSG("Unable to load x509 into stack");
  433. wolfSSL_sk_X509_pop_free(sk, NULL);
  434. wolfSSL_X509_free(x509);
  435. return NULL;
  436. }
  437. }
  438. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  439. /* add CA used to verify top of chain to the list */
  440. if (c->count > 0) {
  441. WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, c->count - 1);
  442. if (x509 != NULL) {
  443. WOLFSSL_X509* issuer = NULL;
  444. if (wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, x509)
  445. == WOLFSSL_SUCCESS) {
  446. /* check that the certificate being looked up is not self
  447. * signed and that a issuer was found */
  448. if (issuer != NULL && wolfSSL_X509_NAME_cmp(&x509->issuer,
  449. &x509->subject) != 0) {
  450. if (wolfSSL_sk_X509_push(sk, issuer) != WOLFSSL_SUCCESS) {
  451. WOLFSSL_MSG("Unable to load CA x509 into stack");
  452. wolfSSL_sk_X509_pop_free(sk, NULL);
  453. wolfSSL_X509_free(issuer);
  454. return NULL;
  455. }
  456. }
  457. else {
  458. WOLFSSL_MSG("Certificate is self signed");
  459. if (issuer != NULL)
  460. wolfSSL_X509_free(issuer);
  461. }
  462. }
  463. else {
  464. WOLFSSL_MSG("Could not find CA for certificate");
  465. }
  466. }
  467. }
  468. #endif
  469. ctx->chain = sk;
  470. }
  471. #endif /* SESSION_CERTS */
  472. return ctx->chain;
  473. }
  474. /* like X509_STORE_CTX_get_chain(), but return a copy with data reference
  475. counts increased */
  476. WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get1_chain(WOLFSSL_X509_STORE_CTX* ctx)
  477. {
  478. WOLFSSL_STACK* ref;
  479. if (ctx == NULL) {
  480. return NULL;
  481. }
  482. /* get chain in ctx */
  483. ref = wolfSSL_X509_STORE_CTX_get_chain(ctx);
  484. if (ref == NULL) {
  485. return ref;
  486. }
  487. /* create duplicate of ctx chain */
  488. return wolfSSL_sk_dup(ref);
  489. }
  490. #ifndef NO_WOLFSSL_STUB
  491. WOLFSSL_X509_STORE_CTX *wolfSSL_X509_STORE_CTX_get0_parent_ctx(
  492. WOLFSSL_X509_STORE_CTX *ctx)
  493. {
  494. (void)ctx;
  495. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_get0_parent_ctx");
  496. return NULL;
  497. }
  498. int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
  499. WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
  500. {
  501. (void)ctx;
  502. (void)idx;
  503. (void)name;
  504. (void)obj;
  505. WOLFSSL_STUB("X509_STORE_get_by_subject");
  506. return 0;
  507. }
  508. #endif
  509. #endif /* OPENSSL_EXTRA */
  510. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  511. #if defined(WOLFSSL_SIGNER_DER_CERT)
  512. WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_X509_STORE_get1_certs(
  513. WOLFSSL_X509_STORE_CTX* ctx, WOLFSSL_X509_NAME* name)
  514. {
  515. WOLF_STACK_OF(WOLFSSL_X509)* ret = NULL;
  516. int err = 0;
  517. WOLFSSL_X509_STORE* store = NULL;
  518. WOLFSSL_STACK* sk = NULL;
  519. WOLFSSL_STACK* certToFilter = NULL;
  520. WOLFSSL_X509_NAME* certToFilterName = NULL;
  521. WOLF_STACK_OF(WOLFSSL_X509)* filteredCerts = NULL;
  522. WOLFSSL_X509* filteredCert = NULL;
  523. WOLFSSL_ENTER("wolfSSL_X509_STORE_get1_certs");
  524. if (name == NULL) {
  525. err = 1;
  526. }
  527. if (err == 0) {
  528. store = wolfSSL_X509_STORE_CTX_get0_store(ctx);
  529. if (store == NULL) {
  530. err = 1;
  531. }
  532. }
  533. if (err == 0) {
  534. filteredCerts = wolfSSL_sk_X509_new_null();
  535. if (filteredCerts == NULL) {
  536. err = 1;
  537. }
  538. }
  539. if (err == 0) {
  540. sk = wolfSSL_CertManagerGetCerts(store->cm);
  541. if (sk == NULL) {
  542. err = 1;
  543. }
  544. }
  545. if (err == 0) {
  546. certToFilter = sk;
  547. while (certToFilter != NULL) {
  548. certToFilterName = wolfSSL_X509_get_subject_name(
  549. certToFilter->data.x509);
  550. if (certToFilterName != NULL) {
  551. if (wolfSSL_X509_NAME_cmp(certToFilterName, name) == 0) {
  552. filteredCert = wolfSSL_X509_dup(certToFilter->data.x509);
  553. if (filteredCert == NULL) {
  554. err = 1;
  555. break;
  556. }
  557. else {
  558. wolfSSL_sk_X509_push(filteredCerts, filteredCert);
  559. }
  560. }
  561. }
  562. certToFilter = certToFilter->next;
  563. }
  564. }
  565. if (err == 1) {
  566. if (filteredCerts != NULL) {
  567. wolfSSL_sk_X509_pop_free(filteredCerts, NULL);
  568. }
  569. ret = NULL;
  570. }
  571. else {
  572. ret = filteredCerts;
  573. }
  574. if (sk != NULL) {
  575. wolfSSL_sk_X509_pop_free(sk, NULL);
  576. }
  577. return ret;
  578. }
  579. #endif /* WOLFSSL_SIGNER_DER_CERT */
  580. #endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
  581. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  582. defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  583. int wolfSSL_X509_STORE_CTX_get1_issuer(WOLFSSL_X509 **issuer,
  584. WOLFSSL_X509_STORE_CTX *ctx, WOLFSSL_X509 *x)
  585. {
  586. WOLFSSL_STACK* node;
  587. if (issuer == NULL || ctx == NULL || x == NULL)
  588. return WOLFSSL_FATAL_ERROR;
  589. if (ctx->chain != NULL) {
  590. for (node = ctx->chain; node != NULL; node = node->next) {
  591. if (wolfSSL_X509_check_issued(node->data.x509, x) ==
  592. WOLFSSL_X509_V_OK) {
  593. *issuer = x;
  594. return WOLFSSL_SUCCESS;
  595. }
  596. }
  597. }
  598. /* Result is ignored when passed to wolfSSL_OCSP_cert_to_id(). */
  599. return x509GetIssuerFromCM(issuer, ctx->store->cm, x);
  600. }
  601. #endif /* WOLFSSL_NGINX || WOLFSSL_HAPROXY || OPENSSL_EXTRA || OPENSSL_ALL */
  602. /*******************************************************************************
  603. * END OF X509_STORE_CTX APIs
  604. ******************************************************************************/
  605. /*******************************************************************************
  606. * START OF X509_STORE APIs
  607. ******************************************************************************/
  608. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  609. defined(WOLFSSL_WPAS_SMALL)
  610. WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
  611. {
  612. int ret;
  613. WOLFSSL_X509_STORE* store = NULL;
  614. WOLFSSL_ENTER("wolfSSL_X509_STORE_new");
  615. if ((store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL,
  616. DYNAMIC_TYPE_X509_STORE)) == NULL)
  617. goto err_exit;
  618. XMEMSET(store, 0, sizeof(WOLFSSL_X509_STORE));
  619. store->isDynamic = 1;
  620. wolfSSL_RefInit(&store->ref, &ret);
  621. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  622. if (ret != 0)
  623. goto err_exit;
  624. #else
  625. (void)ret;
  626. #endif
  627. if ((store->cm = wolfSSL_CertManagerNew()) == NULL)
  628. goto err_exit;
  629. #ifdef HAVE_CRL
  630. store->crl = store->cm->crl;
  631. #endif
  632. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  633. /* Link store's new Certificate Manager to self by default */
  634. store->cm->x509_store_p = store;
  635. if ((store->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  636. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  637. NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
  638. goto err_exit;
  639. }
  640. XMEMSET(store->param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  641. if ((store->lookup.dirs = (WOLFSSL_BY_DIR*)XMALLOC(sizeof(WOLFSSL_BY_DIR),
  642. NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
  643. WOLFSSL_MSG("store->lookup.dir memory allocation error");
  644. goto err_exit;
  645. }
  646. XMEMSET(store->lookup.dirs, 0, sizeof(WOLFSSL_BY_DIR));
  647. if (wc_InitMutex(&store->lookup.dirs->lock) != 0) {
  648. WOLFSSL_MSG("Bad mutex init");
  649. goto err_exit;
  650. }
  651. #endif
  652. return store;
  653. err_exit:
  654. if (store == NULL)
  655. return NULL;
  656. wolfSSL_X509_STORE_free(store);
  657. return NULL;
  658. }
  659. void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
  660. {
  661. int doFree = 0;
  662. if (store != NULL && store->isDynamic) {
  663. int ret;
  664. wolfSSL_RefDec(&store->ref, &doFree, &ret);
  665. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  666. if (ret != 0) {
  667. WOLFSSL_MSG("Couldn't lock store mutex");
  668. }
  669. #else
  670. (void)ret;
  671. #endif
  672. if (doFree) {
  673. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  674. wolfSSL_CRYPTO_cleanup_ex_data(&store->ex_data);
  675. #endif
  676. if (store->cm != NULL) {
  677. wolfSSL_CertManagerFree(store->cm);
  678. store->cm = NULL;
  679. }
  680. #ifdef OPENSSL_ALL
  681. if (store->objs != NULL) {
  682. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  683. }
  684. #endif
  685. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  686. if (store->param != NULL) {
  687. XFREE(store->param, NULL, DYNAMIC_TYPE_OPENSSL);
  688. store->param = NULL;
  689. }
  690. if (store->lookup.dirs != NULL) {
  691. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  692. if (store->lookup.dirs->dir_entry) {
  693. wolfSSL_sk_BY_DIR_entry_free(store->lookup.dirs->dir_entry);
  694. }
  695. #endif
  696. wc_FreeMutex(&store->lookup.dirs->lock);
  697. XFREE(store->lookup.dirs, NULL, DYNAMIC_TYPE_OPENSSL);
  698. store->lookup.dirs = NULL;
  699. }
  700. #endif
  701. XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
  702. }
  703. }
  704. }
  705. /**
  706. * Get ex_data in WOLFSSL_STORE at given index
  707. * @param store a pointer to WOLFSSL_X509_STORE structure
  708. * @param idx Index of ex_data to get data from
  709. * @return void pointer to ex_data on success or NULL on failure
  710. */
  711. void* wolfSSL_X509_STORE_get_ex_data(WOLFSSL_X509_STORE* store, int idx)
  712. {
  713. WOLFSSL_ENTER("wolfSSL_X509_STORE_get_ex_data");
  714. #ifdef HAVE_EX_DATA
  715. if (store != NULL && idx < MAX_EX_DATA && idx >= 0) {
  716. return wolfSSL_CRYPTO_get_ex_data(&store->ex_data, idx);
  717. }
  718. #else
  719. (void)store;
  720. (void)idx;
  721. #endif
  722. return NULL;
  723. }
  724. int wolfSSL_X509_STORE_up_ref(WOLFSSL_X509_STORE* store)
  725. {
  726. if (store) {
  727. int ret;
  728. wolfSSL_RefInc(&store->ref, &ret);
  729. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  730. if (ret != 0) {
  731. WOLFSSL_MSG("Failed to lock store mutex");
  732. return WOLFSSL_FAILURE;
  733. }
  734. #else
  735. (void)ret;
  736. #endif
  737. return WOLFSSL_SUCCESS;
  738. }
  739. return WOLFSSL_FAILURE;
  740. }
  741. /**
  742. * Set ex_data for WOLFSSL_STORE
  743. * @param store a pointer to WOLFSSL_X509_STORE structure
  744. * @param idx Index of ex data to set
  745. * @param data Data to set in ex data
  746. * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
  747. */
  748. int wolfSSL_X509_STORE_set_ex_data(WOLFSSL_X509_STORE* store, int idx,
  749. void *data)
  750. {
  751. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data");
  752. #ifdef HAVE_EX_DATA
  753. if (store != NULL && idx < MAX_EX_DATA) {
  754. return wolfSSL_CRYPTO_set_ex_data(&store->ex_data, idx, data);
  755. }
  756. #else
  757. (void)store;
  758. (void)idx;
  759. (void)data;
  760. #endif
  761. return WOLFSSL_FAILURE;
  762. }
  763. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  764. /**
  765. * Set ex_data for WOLFSSL_STORE
  766. * @param store a pointer to WOLFSSL_X509_STORE structure
  767. * @param idx Index of ex data to set
  768. * @param data Data to set in ex data
  769. * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
  770. */
  771. int wolfSSL_X509_STORE_set_ex_data_with_cleanup(
  772. WOLFSSL_X509_STORE* store,
  773. int idx,
  774. void *data,
  775. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  776. {
  777. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data_with_cleanup");
  778. if (store != NULL && idx < MAX_EX_DATA) {
  779. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&store->ex_data, idx,
  780. data, cleanup_routine);
  781. }
  782. return WOLFSSL_FAILURE;
  783. }
  784. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  785. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER || WOLFSSL_WPAS_SMALL */
  786. #ifdef OPENSSL_EXTRA
  787. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  788. void wolfSSL_X509_STORE_set_verify_cb(WOLFSSL_X509_STORE *st,
  789. WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
  790. {
  791. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_verify_cb");
  792. if (st != NULL) {
  793. st->verify_cb = verify_cb;
  794. }
  795. }
  796. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  797. WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
  798. WOLFSSL_X509_LOOKUP_METHOD* m)
  799. {
  800. WOLFSSL_ENTER("wolfSSL_X509_STORE_add_lookup");
  801. if (store == NULL || m == NULL)
  802. return NULL;
  803. /* Make sure the lookup has a back reference to the store. */
  804. store->lookup.store = store;
  805. /* store a type to know which method wants to be used for */
  806. store->lookup.type = m->type;
  807. return &store->lookup;
  808. }
  809. int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
  810. {
  811. int result = WOLFSSL_FATAL_ERROR;
  812. WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
  813. if (store != NULL && store->cm != NULL && x509 != NULL
  814. && x509->derCert != NULL) {
  815. DerBuffer* derCert = NULL;
  816. result = AllocDer(&derCert, x509->derCert->length,
  817. x509->derCert->type, NULL);
  818. if (result == 0) {
  819. /* AddCA() frees the buffer. */
  820. XMEMCPY(derCert->buffer,
  821. x509->derCert->buffer, x509->derCert->length);
  822. result = AddCA(store->cm, &derCert, WOLFSSL_USER_CA, VERIFY);
  823. }
  824. }
  825. WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
  826. if (result != WOLFSSL_SUCCESS) {
  827. result = WOLFSSL_FATAL_ERROR;
  828. }
  829. return result;
  830. }
  831. int wolfSSL_X509_STORE_set_flags(WOLFSSL_X509_STORE* store, unsigned long flag)
  832. {
  833. int ret = WOLFSSL_SUCCESS;
  834. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_flags");
  835. if (store == NULL)
  836. return WOLFSSL_FAILURE;
  837. if ((flag & WOLFSSL_CRL_CHECKALL) || (flag & WOLFSSL_CRL_CHECK)) {
  838. ret = wolfSSL_CertManagerEnableCRL(store->cm, (int)flag);
  839. }
  840. #if defined(OPENSSL_COMPATIBLE_DEFAULTS)
  841. else if (flag == 0) {
  842. ret = wolfSSL_CertManagerDisableCRL(store->cm);
  843. }
  844. #endif
  845. return ret;
  846. }
  847. int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE* store)
  848. {
  849. (void)store;
  850. return WOLFSSL_SUCCESS;
  851. }
  852. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  853. /* Loads certificate(s) files in pem format into X509_STORE struct from either
  854. * a file or directory.
  855. * Returns WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE if an error occurs.
  856. */
  857. WOLFSSL_API int wolfSSL_X509_STORE_load_locations(WOLFSSL_X509_STORE *str,
  858. const char *file, const char *dir)
  859. {
  860. WOLFSSL_CTX* ctx;
  861. char *name = NULL;
  862. int ret = WOLFSSL_SUCCESS;
  863. #ifdef WOLFSSL_SMALL_STACK
  864. ReadDirCtx* readCtx = NULL;
  865. #else
  866. ReadDirCtx readCtx[1];
  867. #endif
  868. WOLFSSL_ENTER("wolfSSL_X509_STORE_load_locations");
  869. if (str == NULL || str->cm == NULL || (file == NULL && dir == NULL))
  870. return WOLFSSL_FAILURE;
  871. /* tmp ctx for setting our cert manager */
  872. ctx = wolfSSL_CTX_new(cm_pick_method());
  873. if (ctx == NULL)
  874. return WOLFSSL_FAILURE;
  875. wolfSSL_CertManagerFree(ctx->cm);
  876. ctx->cm = str->cm;
  877. #ifdef HAVE_CRL
  878. if (str->cm->crl == NULL) {
  879. if (wolfSSL_CertManagerEnableCRL(str->cm, WOLFSSL_CRL_CHECK)
  880. != WOLFSSL_SUCCESS) {
  881. WOLFSSL_MSG("Enable CRL failed");
  882. wolfSSL_CTX_free(ctx);
  883. return WOLFSSL_FAILURE;
  884. }
  885. }
  886. #endif
  887. /* Load individual file */
  888. if (file) {
  889. /* Try to process file with type DETECT_CERT_TYPE to parse the
  890. correct certificate header and footer type */
  891. ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
  892. NULL, 0, str->cm->crl, 0);
  893. if (ret != WOLFSSL_SUCCESS) {
  894. WOLFSSL_MSG("Failed to load file");
  895. ret = WOLFSSL_FAILURE;
  896. }
  897. }
  898. /* Load files in dir */
  899. if (dir && ret == WOLFSSL_SUCCESS) {
  900. int successes = 0;
  901. #ifdef WOLFSSL_SMALL_STACK
  902. readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
  903. DYNAMIC_TYPE_TMP_BUFFER);
  904. if (readCtx == NULL) {
  905. WOLFSSL_MSG("Memory error");
  906. wolfSSL_CTX_free(ctx);
  907. return WOLFSSL_FAILURE;
  908. }
  909. #endif
  910. /* try to load each regular file in dir */
  911. ret = wc_ReadDirFirst(readCtx, dir, &name);
  912. while (ret == 0 && name) {
  913. WOLFSSL_MSG(name);
  914. /* Try to process file with type DETECT_CERT_TYPE to parse the
  915. correct certificate header and footer type */
  916. ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
  917. NULL, 0, str->cm->crl, 0);
  918. /* Not failing on load errors */
  919. if (ret != WOLFSSL_SUCCESS)
  920. WOLFSSL_MSG("Failed to load file in path, continuing");
  921. else
  922. successes++;
  923. ret = wc_ReadDirNext(readCtx, dir, &name);
  924. }
  925. wc_ReadDirClose(readCtx);
  926. /* Success if at least one file in dir was loaded */
  927. if (successes > 0)
  928. ret = WOLFSSL_SUCCESS;
  929. else {
  930. WOLFSSL_ERROR(ret);
  931. ret = WOLFSSL_FAILURE;
  932. }
  933. #ifdef WOLFSSL_SMALL_STACK
  934. XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  935. #endif
  936. }
  937. ctx->cm = NULL;
  938. wolfSSL_CTX_free(ctx);
  939. return ret;
  940. }
  941. #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
  942. int wolfSSL_X509_CA_num(WOLFSSL_X509_STORE* store)
  943. {
  944. int cnt_ret = 0;
  945. Signer **table;
  946. WOLFSSL_ENTER("wolfSSL_X509_CA_num");
  947. if (store == NULL || store->cm == NULL){
  948. WOLFSSL_MSG("invalid parameter");
  949. return WOLFSSL_FAILURE;
  950. }
  951. table = store->cm->caTable;
  952. if (table){
  953. if (wc_LockMutex(&store->cm->caLock) == 0){
  954. int i = 0;
  955. for (i = 0; i < CA_TABLE_SIZE; i++) {
  956. Signer* signer = table[i];
  957. while (signer) {
  958. Signer* next = signer->next;
  959. cnt_ret++;
  960. signer = next;
  961. }
  962. }
  963. wc_UnLockMutex(&store->cm->caLock);
  964. }
  965. }
  966. return cnt_ret;
  967. }
  968. /******************************************************************************
  969. * wolfSSL_X509_STORE_GetCerts - retrieve stack of X509 in a certificate store ctx
  970. *
  971. * This API can be used in SSL verify callback function to view cert chain
  972. * See examples/client/client.c and myVerify() function in test.h
  973. *
  974. * RETURNS:
  975. * returns stack of X509 certs on success, otherwise returns a NULL.
  976. */
  977. WOLFSSL_STACK* wolfSSL_X509_STORE_GetCerts(WOLFSSL_X509_STORE_CTX* s)
  978. {
  979. int certIdx = 0;
  980. WOLFSSL_BUFFER_INFO* cert = NULL;
  981. DecodedCert* dCert = NULL;
  982. WOLFSSL_X509* x509 = NULL;
  983. WOLFSSL_STACK* sk = NULL;
  984. int found = 0;
  985. if (s == NULL) {
  986. return NULL;
  987. }
  988. sk = wolfSSL_sk_X509_new_null();
  989. if (sk == NULL) {
  990. return NULL;
  991. }
  992. for (certIdx = s->totalCerts - 1; certIdx >= 0; certIdx--) {
  993. /* get certificate buffer */
  994. cert = &s->certs[certIdx];
  995. dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
  996. if (dCert == NULL) {
  997. goto error;
  998. }
  999. XMEMSET(dCert, 0, sizeof(DecodedCert));
  1000. InitDecodedCert(dCert, cert->buffer, cert->length, NULL);
  1001. /* Parse Certificate */
  1002. if (ParseCert(dCert, CERT_TYPE, NO_VERIFY, NULL)){
  1003. goto error;
  1004. }
  1005. x509 = wolfSSL_X509_new();
  1006. if (x509 == NULL) {
  1007. goto error;
  1008. }
  1009. InitX509(x509, 1, NULL);
  1010. if (CopyDecodedToX509(x509, dCert) == 0) {
  1011. if (wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
  1012. WOLFSSL_MSG("Unable to load x509 into stack");
  1013. wolfSSL_X509_free(x509);
  1014. goto error;
  1015. }
  1016. }
  1017. else {
  1018. goto error;
  1019. }
  1020. found = 1;
  1021. FreeDecodedCert(dCert);
  1022. XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
  1023. dCert = NULL;
  1024. }
  1025. if (!found) {
  1026. wolfSSL_sk_X509_pop_free(sk, NULL);
  1027. sk = NULL;
  1028. }
  1029. return sk;
  1030. error:
  1031. if (dCert) {
  1032. FreeDecodedCert(dCert);
  1033. XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
  1034. }
  1035. if (sk)
  1036. wolfSSL_sk_X509_pop_free(sk, NULL);
  1037. return NULL;
  1038. }
  1039. #endif /* OPENSSL_EXTRA */
  1040. #ifdef OPENSSL_ALL
  1041. WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* wolfSSL_X509_STORE_get0_objects(
  1042. WOLFSSL_X509_STORE* store)
  1043. {
  1044. WOLFSSL_STACK* ret = NULL;
  1045. WOLFSSL_STACK* cert_stack = NULL;
  1046. WOLFSSL_X509* x509 = NULL;
  1047. WOLFSSL_ENTER("wolfSSL_X509_STORE_get0_objects");
  1048. if (store == NULL || store->cm == NULL) {
  1049. WOLFSSL_MSG("Missing or empty store");
  1050. return NULL;
  1051. }
  1052. if (store->objs != NULL) {
  1053. #if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
  1054. /* want to update objs stack by cm stack again before returning it*/
  1055. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  1056. store->objs = NULL;
  1057. #else
  1058. if (wolfSSL_sk_X509_OBJECT_num(store->objs) == 0) {
  1059. /* Let's try generating the stack again */
  1060. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  1061. store->objs = NULL;
  1062. }
  1063. else
  1064. return store->objs;
  1065. #endif
  1066. }
  1067. if ((ret = wolfSSL_sk_X509_OBJECT_new()) == NULL) {
  1068. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_new error");
  1069. goto err_cleanup;
  1070. }
  1071. #if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
  1072. cert_stack = wolfSSL_CertManagerGetCerts(store->cm);
  1073. /* wolfSSL_sk_X509_pop checks for NULL */
  1074. while ((x509 = wolfSSL_sk_X509_pop(cert_stack)) != NULL) {
  1075. WOLFSSL_X509_OBJECT* obj = wolfSSL_X509_OBJECT_new();
  1076. if (obj == NULL) {
  1077. WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
  1078. goto err_cleanup;
  1079. }
  1080. if (wolfSSL_sk_X509_OBJECT_push(ret, obj) != WOLFSSL_SUCCESS) {
  1081. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
  1082. wolfSSL_X509_OBJECT_free(obj);
  1083. goto err_cleanup;
  1084. }
  1085. obj->type = WOLFSSL_X509_LU_X509;
  1086. obj->data.x509 = x509;
  1087. x509 = NULL;
  1088. }
  1089. #endif
  1090. #ifdef HAVE_CRL
  1091. if (store->cm->crl != NULL) {
  1092. WOLFSSL_X509_OBJECT* obj = wolfSSL_X509_OBJECT_new();
  1093. if (obj == NULL) {
  1094. WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
  1095. goto err_cleanup;
  1096. }
  1097. if (wolfSSL_sk_X509_OBJECT_push(ret, obj) != WOLFSSL_SUCCESS) {
  1098. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
  1099. wolfSSL_X509_OBJECT_free(obj);
  1100. goto err_cleanup;
  1101. }
  1102. obj->type = WOLFSSL_X509_LU_CRL;
  1103. obj->data.crl = store->cm->crl;
  1104. }
  1105. #endif
  1106. if (cert_stack)
  1107. wolfSSL_sk_X509_pop_free(cert_stack, NULL);
  1108. store->objs = ret;
  1109. return ret;
  1110. err_cleanup:
  1111. if (ret != NULL)
  1112. wolfSSL_sk_X509_OBJECT_pop_free(ret, NULL);
  1113. if (cert_stack != NULL)
  1114. wolfSSL_sk_X509_pop_free(cert_stack, NULL);
  1115. if (x509 != NULL)
  1116. wolfSSL_X509_free(x509);
  1117. return NULL;
  1118. }
  1119. #endif /* OPENSSL_ALL */
  1120. /*******************************************************************************
  1121. * END OF X509_STORE APIs
  1122. ******************************************************************************/
  1123. #endif /* NO_CERTS */
  1124. #endif /* !WOLFCRYPT_ONLY */
  1125. #endif /* !WOLFSSL_X509_STORE_INCLUDED */