| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdlib.h> |
| 16 |
| 17 | #include <openssl/err.h> |
| 18 | #include <openssl/pem.h> |
| 19 | #include <openssl/thread.h> |
| 20 |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
| 25 | char **ret); |
| 26 | static const X509_LOOKUP_METHOD x509_file_lookup = { |
| 27 | NULL, // new |
| 28 | NULL, // free |
| 29 | by_file_ctrl, // ctrl |
| 30 | NULL, // get_by_subject |
| 31 | }; |
| 32 |
| 33 | const X509_LOOKUP_METHOD *X509_LOOKUP_file(void) { return &x509_file_lookup; } |
| 34 |
| 35 | static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
| 36 | char **ret) { |
| 37 | if (cmd != X509_L_FILE_LOAD) { |
| 38 | return 0; |
| 39 | } |
| 40 | const char *file = argp; |
| 41 | int type = argl; |
| 42 | if (argl == X509_FILETYPE_DEFAULT) { |
| 43 | if ((file = getenv(X509_get_default_cert_file_env())) == NULL) { |
| 44 | file = X509_get_default_cert_file(); |
| 45 | } |
| 46 | type = X509_FILETYPE_PEM; |
| 47 | } |
| 48 | if (X509_load_cert_crl_file(ctx, file, type) != 0) { |
| 49 | return 1; |
| 50 | } |
| 51 | if (argl == X509_FILETYPE_DEFAULT) { |
| 52 | OPENSSL_PUT_ERROR(X509, X509_R_LOADING_DEFAULTS); |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 |
| 57 | int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) { |
| 58 | int ret = 0; |
| 59 | BIO *in = NULL; |
| 60 | int i, count = 0; |
| 61 | X509 *x = NULL; |
| 62 |
| 63 | in = BIO_new(BIO_s_file()); |
| 64 |
| 65 | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
| 66 | OPENSSL_PUT_ERROR(X509, ERR_R_SYS_LIB); |
| 67 | goto err; |
| 68 | } |
| 69 |
| 70 | if (type == X509_FILETYPE_PEM) { |
| 71 | for (;;) { |
| 72 | x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 73 | if (x == NULL) { |
| 74 | uint32_t error = ERR_peek_last_error(); |
| 75 | if (ERR_GET_LIB(error) == ERR_LIB_PEM && |
| 76 | ERR_GET_REASON(error) == PEM_R_NO_START_LINE && count > 0) { |
| 77 | ERR_clear_error(); |
| 78 | break; |
| 79 | } |
| 80 | OPENSSL_PUT_ERROR(X509, ERR_R_PEM_LIB); |
| 81 | goto err; |
| 82 | } |
| 83 | i = X509_STORE_add_cert(ctx->store_ctx, x); |
| 84 | if (!i) { |
| 85 | goto err; |
| 86 | } |
| 87 | count++; |
| 88 | X509_free(x); |
| 89 | x = NULL; |
| 90 | } |
| 91 | ret = count; |
| 92 | } else if (type == X509_FILETYPE_ASN1) { |
| 93 | x = d2i_X509_bio(in, NULL); |
| 94 | if (x == NULL) { |
| 95 | OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB); |
| 96 | goto err; |
| 97 | } |
| 98 | i = X509_STORE_add_cert(ctx->store_ctx, x); |
| 99 | if (!i) { |
| 100 | goto err; |
| 101 | } |
| 102 | ret = i; |
| 103 | } else { |
| 104 | OPENSSL_PUT_ERROR(X509, X509_R_BAD_X509_FILETYPE); |
| 105 | goto err; |
| 106 | } |
| 107 |
| 108 | if (ret == 0) { |
| 109 | OPENSSL_PUT_ERROR(X509, X509_R_NO_CERTIFICATE_FOUND); |
| 110 | } |
| 111 |
| 112 | err: |
| 113 | X509_free(x); |
| 114 | BIO_free(in); |
| 115 | return ret; |
| 116 | } |
| 117 |
| 118 | int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) { |
| 119 | int ret = 0; |
| 120 | BIO *in = NULL; |
| 121 | int i, count = 0; |
| 122 | X509_CRL *x = NULL; |
| 123 |
| 124 | in = BIO_new(BIO_s_file()); |
| 125 |
| 126 | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
| 127 | OPENSSL_PUT_ERROR(X509, ERR_R_SYS_LIB); |
| 128 | goto err; |
| 129 | } |
| 130 |
| 131 | if (type == X509_FILETYPE_PEM) { |
| 132 | for (;;) { |
| 133 | x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); |
| 134 | if (x == NULL) { |
| 135 | uint32_t error = ERR_peek_last_error(); |
| 136 | if (ERR_GET_LIB(error) == ERR_LIB_PEM && |
| 137 | ERR_GET_REASON(error) == PEM_R_NO_START_LINE && count > 0) { |
| 138 | ERR_clear_error(); |
| 139 | break; |
| 140 | } |
| 141 | OPENSSL_PUT_ERROR(X509, ERR_R_PEM_LIB); |
| 142 | goto err; |
| 143 | } |
| 144 | i = X509_STORE_add_crl(ctx->store_ctx, x); |
| 145 | if (!i) { |
| 146 | goto err; |
| 147 | } |
| 148 | count++; |
| 149 | X509_CRL_free(x); |
| 150 | x = NULL; |
| 151 | } |
| 152 | ret = count; |
| 153 | } else if (type == X509_FILETYPE_ASN1) { |
| 154 | x = d2i_X509_CRL_bio(in, NULL); |
| 155 | if (x == NULL) { |
| 156 | OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB); |
| 157 | goto err; |
| 158 | } |
| 159 | i = X509_STORE_add_crl(ctx->store_ctx, x); |
| 160 | if (!i) { |
| 161 | goto err; |
| 162 | } |
| 163 | ret = i; |
| 164 | } else { |
| 165 | OPENSSL_PUT_ERROR(X509, X509_R_BAD_X509_FILETYPE); |
| 166 | goto err; |
| 167 | } |
| 168 |
| 169 | if (ret == 0) { |
| 170 | OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_FOUND); |
| 171 | } |
| 172 |
| 173 | err: |
| 174 | X509_CRL_free(x); |
| 175 | BIO_free(in); |
| 176 | return ret; |
| 177 | } |
| 178 |
| 179 | int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) { |
| 180 | STACK_OF(X509_INFO) *inf; |
| 181 | X509_INFO *itmp; |
| 182 | BIO *in; |
| 183 | size_t i; |
| 184 | int count = 0; |
| 185 |
| 186 | if (type != X509_FILETYPE_PEM) { |
| 187 | return X509_load_cert_file(ctx, file, type); |
| 188 | } |
| 189 | in = BIO_new_file(file, "rb"); |
| 190 | if (!in) { |
| 191 | OPENSSL_PUT_ERROR(X509, ERR_R_SYS_LIB); |
| 192 | return 0; |
| 193 | } |
| 194 | inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); |
| 195 | BIO_free(in); |
| 196 | if (!inf) { |
| 197 | OPENSSL_PUT_ERROR(X509, ERR_R_PEM_LIB); |
| 198 | return 0; |
| 199 | } |
| 200 | for (i = 0; i < sk_X509_INFO_num(inf); i++) { |
| 201 | itmp = sk_X509_INFO_value(inf, i); |
| 202 | if (itmp->x509) { |
| 203 | if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) { |
| 204 | goto err; |
| 205 | } |
| 206 | count++; |
| 207 | } |
| 208 | if (itmp->crl) { |
| 209 | if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) { |
| 210 | goto err; |
| 211 | } |
| 212 | count++; |
| 213 | } |
| 214 | } |
| 215 |
| 216 | if (count == 0) { |
| 217 | OPENSSL_PUT_ERROR(X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND); |
| 218 | } |
| 219 |
| 220 | err: |
| 221 | sk_X509_INFO_pop_free(inf, X509_INFO_free); |
| 222 | return count; |
| 223 | } |
| 224 |
| 225 | int X509_LOOKUP_load_file(X509_LOOKUP *lookup, const char *name, int type) { |
| 226 | return X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, name, type, NULL); |
| 227 | } |
| 228 |