by_file.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 1995-2020 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. DEFINE_STACK_OF(X509_INFO)
  18. static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
  19. long argl, char **ret);
  20. static X509_LOOKUP_METHOD x509_file_lookup = {
  21. "Load file into cache",
  22. NULL, /* new_item */
  23. NULL, /* free */
  24. NULL, /* init */
  25. NULL, /* shutdown */
  26. by_file_ctrl, /* ctrl */
  27. NULL, /* get_by_subject */
  28. NULL, /* get_by_issuer_serial */
  29. NULL, /* get_by_fingerprint */
  30. NULL, /* get_by_alias */
  31. };
  32. X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
  33. {
  34. return &x509_file_lookup;
  35. }
  36. static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp,
  37. long argl, char **ret)
  38. {
  39. int ok = 0;
  40. const char *file;
  41. switch (cmd) {
  42. case X509_L_FILE_LOAD:
  43. if (argl == X509_FILETYPE_DEFAULT) {
  44. file = ossl_safe_getenv(X509_get_default_cert_file_env());
  45. if (file)
  46. ok = (X509_load_cert_crl_file(ctx, file,
  47. X509_FILETYPE_PEM) != 0);
  48. else
  49. ok = (X509_load_cert_crl_file
  50. (ctx, X509_get_default_cert_file(),
  51. X509_FILETYPE_PEM) != 0);
  52. if (!ok) {
  53. X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS);
  54. }
  55. } else {
  56. if (argl == X509_FILETYPE_PEM)
  57. ok = (X509_load_cert_crl_file(ctx, argp,
  58. X509_FILETYPE_PEM) != 0);
  59. else
  60. ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0);
  61. }
  62. break;
  63. }
  64. return ok;
  65. }
  66. int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
  67. {
  68. int ret = 0;
  69. BIO *in = NULL;
  70. int i, count = 0;
  71. X509 *x = NULL;
  72. in = BIO_new(BIO_s_file());
  73. if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
  74. X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
  75. goto err;
  76. }
  77. if (type == X509_FILETYPE_PEM) {
  78. for (;;) {
  79. x = PEM_read_bio_X509_AUX(in, NULL, NULL, "");
  80. if (x == NULL) {
  81. if ((ERR_GET_REASON(ERR_peek_last_error()) ==
  82. PEM_R_NO_START_LINE) && (count > 0)) {
  83. ERR_clear_error();
  84. break;
  85. } else {
  86. X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
  87. goto err;
  88. }
  89. }
  90. i = X509_STORE_add_cert(ctx->store_ctx, x);
  91. if (!i)
  92. goto err;
  93. count++;
  94. X509_free(x);
  95. x = NULL;
  96. }
  97. ret = count;
  98. } else if (type == X509_FILETYPE_ASN1) {
  99. x = d2i_X509_bio(in, NULL);
  100. if (x == NULL) {
  101. X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
  102. goto err;
  103. }
  104. i = X509_STORE_add_cert(ctx->store_ctx, x);
  105. if (!i)
  106. goto err;
  107. ret = i;
  108. } else {
  109. X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
  110. goto err;
  111. }
  112. if (ret == 0)
  113. X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_NO_CERTIFICATE_FOUND);
  114. err:
  115. X509_free(x);
  116. BIO_free(in);
  117. return ret;
  118. }
  119. int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  120. {
  121. int ret = 0;
  122. BIO *in = NULL;
  123. int i, count = 0;
  124. X509_CRL *x = NULL;
  125. in = BIO_new(BIO_s_file());
  126. if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
  127. X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
  128. goto err;
  129. }
  130. if (type == X509_FILETYPE_PEM) {
  131. for (;;) {
  132. x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
  133. if (x == NULL) {
  134. if ((ERR_GET_REASON(ERR_peek_last_error()) ==
  135. PEM_R_NO_START_LINE) && (count > 0)) {
  136. ERR_clear_error();
  137. break;
  138. } else {
  139. X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
  140. goto err;
  141. }
  142. }
  143. i = X509_STORE_add_crl(ctx->store_ctx, x);
  144. if (!i)
  145. goto err;
  146. count++;
  147. X509_CRL_free(x);
  148. x = NULL;
  149. }
  150. ret = count;
  151. } else if (type == X509_FILETYPE_ASN1) {
  152. x = d2i_X509_CRL_bio(in, NULL);
  153. if (x == NULL) {
  154. X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
  155. goto err;
  156. }
  157. i = X509_STORE_add_crl(ctx->store_ctx, x);
  158. if (!i)
  159. goto err;
  160. ret = i;
  161. } else {
  162. X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
  163. goto err;
  164. }
  165. if (ret == 0)
  166. X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
  167. err:
  168. X509_CRL_free(x);
  169. BIO_free(in);
  170. return ret;
  171. }
  172. int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
  173. {
  174. STACK_OF(X509_INFO) *inf;
  175. X509_INFO *itmp;
  176. BIO *in;
  177. int i, count = 0;
  178. if (type != X509_FILETYPE_PEM)
  179. return X509_load_cert_file(ctx, file, type);
  180. in = BIO_new_file(file, "r");
  181. if (!in) {
  182. X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
  183. return 0;
  184. }
  185. inf = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
  186. BIO_free(in);
  187. if (!inf) {
  188. X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
  189. return 0;
  190. }
  191. for (i = 0; i < sk_X509_INFO_num(inf); i++) {
  192. itmp = sk_X509_INFO_value(inf, i);
  193. if (itmp->x509) {
  194. if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
  195. goto err;
  196. count++;
  197. }
  198. if (itmp->crl) {
  199. if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
  200. goto err;
  201. count++;
  202. }
  203. }
  204. if (count == 0)
  205. X509err(X509_F_X509_LOAD_CERT_CRL_FILE,
  206. X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
  207. err:
  208. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  209. return count;
  210. }