x509_str.c 38 KB

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