Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/by_file.c

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