2
0

by_file.c 6.5 KB

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