by_file.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 1995-2023 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. count++;
  117. }
  118. } else if (type == X509_FILETYPE_ASN1) {
  119. if (d2i_X509_bio(in, &x) == NULL) {
  120. ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
  121. goto err;
  122. }
  123. count = X509_STORE_add_cert(ctx->store_ctx, x);
  124. } else {
  125. ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
  126. goto err;
  127. }
  128. err:
  129. X509_free(x);
  130. BIO_free(in);
  131. return count;
  132. }
  133. int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
  134. {
  135. return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
  136. }
  137. int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  138. {
  139. BIO *in = NULL;
  140. int count = 0;
  141. X509_CRL *x = NULL;
  142. in = BIO_new(BIO_s_file());
  143. if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
  144. ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
  145. goto err;
  146. }
  147. if (type == X509_FILETYPE_PEM) {
  148. for (;;) {
  149. x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
  150. if (x == NULL) {
  151. if ((ERR_GET_REASON(ERR_peek_last_error()) ==
  152. PEM_R_NO_START_LINE) && (count > 0)) {
  153. ERR_clear_error();
  154. break;
  155. } else {
  156. if (count == 0) {
  157. ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
  158. } else {
  159. ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
  160. count = 0;
  161. }
  162. goto err;
  163. }
  164. }
  165. if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
  166. count = 0;
  167. goto err;
  168. }
  169. count++;
  170. }
  171. } else if (type == X509_FILETYPE_ASN1) {
  172. x = d2i_X509_CRL_bio(in, NULL);
  173. if (x == NULL) {
  174. ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
  175. goto err;
  176. }
  177. count = X509_STORE_add_crl(ctx->store_ctx, x);
  178. } else {
  179. ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
  180. goto err;
  181. }
  182. err:
  183. X509_CRL_free(x);
  184. BIO_free(in);
  185. return count;
  186. }
  187. int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
  188. OSSL_LIB_CTX *libctx, const char *propq)
  189. {
  190. STACK_OF(X509_INFO) *inf = NULL;
  191. X509_INFO *itmp = NULL;
  192. BIO *in = NULL;
  193. int i, count = 0;
  194. if (type != X509_FILETYPE_PEM)
  195. return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
  196. in = BIO_new_file(file, "r");
  197. if (in == NULL) {
  198. ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
  199. return 0;
  200. }
  201. inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
  202. BIO_free(in);
  203. if (inf == NULL) {
  204. ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
  205. return 0;
  206. }
  207. for (i = 0; i < sk_X509_INFO_num(inf); i++) {
  208. itmp = sk_X509_INFO_value(inf, i);
  209. if (itmp->x509) {
  210. if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
  211. count = 0;
  212. goto err;
  213. }
  214. count++;
  215. }
  216. if (itmp->crl) {
  217. if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
  218. count = 0;
  219. goto err;
  220. }
  221. count++;
  222. }
  223. }
  224. if (count == 0)
  225. ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
  226. err:
  227. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  228. return count;
  229. }
  230. int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  231. {
  232. return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
  233. }