x509_str.c 39 KB

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