by_file.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/buffer.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/pem.h>
  16. #include "x509_local.h"
  17. static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
  18. long argl, char **ret);
  19. static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc,
  20. long argl, char **ret, OSSL_LIB_CTX *libctx,
  21. const char *propq);
  22. static X509_LOOKUP_METHOD x509_file_lookup = {
  23. "Load file into cache",
  24. NULL, /* new_item */
  25. NULL, /* free */
  26. NULL, /* init */
  27. NULL, /* shutdown */
  28. by_file_ctrl, /* ctrl */
  29. NULL, /* get_by_subject */
  30. NULL, /* get_by_issuer_serial */
  31. NULL, /* get_by_fingerprint */
  32. NULL, /* get_by_alias */
  33. NULL, /* get_by_subject_ex */
  34. by_file_ctrl_ex, /* ctrl_ex */
  35. };
  36. X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
  37. {
  38. return &x509_file_lookup;
  39. }
  40. static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
  41. long argl, char **ret, OSSL_LIB_CTX *libctx,
  42. const char *propq)
  43. {
  44. int ok = 0;
  45. const char *file;
  46. switch (cmd) {
  47. case X509_L_FILE_LOAD:
  48. if (argl == X509_FILETYPE_DEFAULT) {
  49. file = ossl_safe_getenv(X509_get_default_cert_file_env());
  50. if (file)
  51. ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,
  52. libctx, propq) != 0);
  53. else
  54. ok = (X509_load_cert_crl_file_ex(
  55. ctx, X509_get_default_cert_file(),
  56. X509_FILETYPE_PEM, libctx, propq) != 0);
  57. if (!ok)
  58. ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
  59. } else {
  60. if (argl == X509_FILETYPE_PEM)
  61. ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
  62. libctx, propq) != 0);
  63. else
  64. ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
  65. propq) != 0);
  66. }
  67. break;
  68. }
  69. return ok;
  70. }
  71. static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
  72. const char *argp, long argl, char **ret)
  73. {
  74. return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
  75. }
  76. int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
  77. OSSL_LIB_CTX *libctx, const char *propq)
  78. {
  79. BIO *in = NULL;
  80. int count = 0;
  81. X509 *x = NULL;
  82. in = BIO_new(BIO_s_file());
  83. if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
  84. ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
  85. goto err;
  86. }
  87. x = X509_new_ex(libctx, propq);
  88. if (x == NULL) {
  89. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  90. goto err;
  91. }
  92. if (type == X509_FILETYPE_PEM) {
  93. for (;;) {
  94. ERR_set_mark();
  95. if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
  96. if ((ERR_GET_REASON(ERR_peek_last_error()) ==
  97. PEM_R_NO_START_LINE) && (count > 0)) {
  98. ERR_pop_to_mark();
  99. break;
  100. } else {
  101. ERR_clear_last_mark();
  102. if (count == 0) {
  103. ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
  104. } else {
  105. ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
  106. count = 0;
  107. }
  108. goto err;
  109. }
  110. }
  111. ERR_clear_last_mark();
  112. if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
  113. count = 0;
  114. goto err;
  115. }
  116. /*
  117. * X509_STORE_add_cert() added a reference rather than a copy,
  118. * so we need a fresh X509 object.
  119. */
  120. X509_free(x);
  121. x = X509_new_ex(libctx, propq);
  122. if (x == NULL) {
  123. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  124. count = 0;
  125. goto err;
  126. }
  127. count++;
  128. }
  129. } else if (type == X509_FILETYPE_ASN1) {
  130. if (d2i_X509_bio(in, &x) == NULL) {
  131. ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
  132. goto err;
  133. }
  134. count = X509_STORE_add_cert(ctx->store_ctx, x);
  135. } else {
  136. ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
  137. goto err;
  138. }
  139. err:
  140. X509_free(x);
  141. BIO_free(in);
  142. return count;
  143. }
  144. int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
  145. {
  146. return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
  147. }
  148. int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  149. {
  150. BIO *in = NULL;
  151. int count = 0;
  152. X509_CRL *x = NULL;
  153. in = BIO_new(BIO_s_file());
  154. if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
  155. ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
  156. goto err;
  157. }
  158. if (type == X509_FILETYPE_PEM) {
  159. for (;;) {
  160. x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
  161. if (x == NULL) {
  162. if ((ERR_GET_REASON(ERR_peek_last_error()) ==
  163. PEM_R_NO_START_LINE) && (count > 0)) {
  164. ERR_clear_error();
  165. break;
  166. } else {
  167. if (count == 0) {
  168. ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
  169. } else {
  170. ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
  171. count = 0;
  172. }
  173. goto err;
  174. }
  175. }
  176. if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
  177. count = 0;
  178. goto err;
  179. }
  180. count++;
  181. X509_CRL_free(x);
  182. x = NULL;
  183. }
  184. } else if (type == X509_FILETYPE_ASN1) {
  185. x = d2i_X509_CRL_bio(in, NULL);
  186. if (x == NULL) {
  187. ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
  188. goto err;
  189. }
  190. count = X509_STORE_add_crl(ctx->store_ctx, x);
  191. } else {
  192. ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
  193. goto err;
  194. }
  195. err:
  196. X509_CRL_free(x);
  197. BIO_free(in);
  198. return count;
  199. }
  200. int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
  201. OSSL_LIB_CTX *libctx, const char *propq)
  202. {
  203. STACK_OF(X509_INFO) *inf = NULL;
  204. X509_INFO *itmp = NULL;
  205. BIO *in = NULL;
  206. int i, count = 0;
  207. if (type != X509_FILETYPE_PEM)
  208. return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
  209. in = BIO_new_file(file, "r");
  210. if (in == NULL) {
  211. ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
  212. return 0;
  213. }
  214. inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
  215. BIO_free(in);
  216. if (inf == NULL) {
  217. ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
  218. return 0;
  219. }
  220. for (i = 0; i < sk_X509_INFO_num(inf); i++) {
  221. itmp = sk_X509_INFO_value(inf, i);
  222. if (itmp->x509) {
  223. if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
  224. count = 0;
  225. goto err;
  226. }
  227. count++;
  228. }
  229. if (itmp->crl) {
  230. if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
  231. count = 0;
  232. goto err;
  233. }
  234. count++;
  235. }
  236. }
  237. if (count == 0)
  238. ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
  239. err:
  240. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  241. return count;
  242. }
  243. int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  244. {
  245. return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
  246. }