summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509')
-rw-r--r--src/lib/libcrypto/x509/by_dir.c422
-rw-r--r--src/lib/libcrypto/x509/by_file.c274
-rw-r--r--src/lib/libcrypto/x509/by_mem.c138
-rw-r--r--src/lib/libcrypto/x509/x509.h1270
-rw-r--r--src/lib/libcrypto/x509/x509_att.c403
-rw-r--r--src/lib/libcrypto/x509/x509_cmp.c369
-rw-r--r--src/lib/libcrypto/x509/x509_d2.c128
-rw-r--r--src/lib/libcrypto/x509/x509_def.c98
-rw-r--r--src/lib/libcrypto/x509/x509_err.c164
-rw-r--r--src/lib/libcrypto/x509/x509_ext.c232
-rw-r--r--src/lib/libcrypto/x509/x509_lcl.h61
-rw-r--r--src/lib/libcrypto/x509/x509_lu.c739
-rw-r--r--src/lib/libcrypto/x509/x509_obj.c179
-rw-r--r--src/lib/libcrypto/x509/x509_r2x.c115
-rw-r--r--src/lib/libcrypto/x509/x509_req.c347
-rw-r--r--src/lib/libcrypto/x509/x509_set.c154
-rw-r--r--src/lib/libcrypto/x509/x509_trs.c332
-rw-r--r--src/lib/libcrypto/x509/x509_txt.c189
-rw-r--r--src/lib/libcrypto/x509/x509_v3.c300
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.c2157
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.h560
-rw-r--r--src/lib/libcrypto/x509/x509_vpm.c449
-rw-r--r--src/lib/libcrypto/x509/x509cset.c173
-rw-r--r--src/lib/libcrypto/x509/x509name.c410
-rw-r--r--src/lib/libcrypto/x509/x509rset.c88
-rw-r--r--src/lib/libcrypto/x509/x509spki.c132
-rw-r--r--src/lib/libcrypto/x509/x509type.c130
-rw-r--r--src/lib/libcrypto/x509/x_all.c602
28 files changed, 0 insertions, 10615 deletions
diff --git a/src/lib/libcrypto/x509/by_dir.c b/src/lib/libcrypto/x509/by_dir.c
deleted file mode 100644
index 7b7d14a950..0000000000
--- a/src/lib/libcrypto/x509/by_dir.c
+++ /dev/null
@@ -1,422 +0,0 @@
1/* $OpenBSD: by_dir.c,v 1.37 2015/04/11 16:03:21 deraadt Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <sys/types.h>
60
61#include <errno.h>
62#include <stdio.h>
63#include <string.h>
64#include <time.h>
65#include <unistd.h>
66
67#include <openssl/opensslconf.h>
68
69#include <openssl/err.h>
70#include <openssl/lhash.h>
71#include <openssl/x509.h>
72
73# include <sys/stat.h>
74
75typedef struct lookup_dir_hashes_st {
76 unsigned long hash;
77 int suffix;
78} BY_DIR_HASH;
79
80typedef struct lookup_dir_entry_st {
81 char *dir;
82 int dir_type;
83 STACK_OF(BY_DIR_HASH) *hashes;
84} BY_DIR_ENTRY;
85
86typedef struct lookup_dir_st {
87 BUF_MEM *buffer;
88 STACK_OF(BY_DIR_ENTRY) *dirs;
89} BY_DIR;
90
91DECLARE_STACK_OF(BY_DIR_HASH)
92DECLARE_STACK_OF(BY_DIR_ENTRY)
93
94static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
95 char **ret);
96static int new_dir(X509_LOOKUP *lu);
97static void free_dir(X509_LOOKUP *lu);
98static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
99static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
100 X509_OBJECT *ret);
101
102static X509_LOOKUP_METHOD x509_dir_lookup = {
103 .name = "Load certs from files in a directory",
104 .new_item = new_dir,
105 .free = free_dir,
106 .init = NULL,
107 .shutdown = NULL,
108 .ctrl = dir_ctrl,
109 .get_by_subject = get_cert_by_subject,
110 .get_by_issuer_serial = NULL,
111 .get_by_fingerprint = NULL,
112 .get_by_alias = NULL,
113};
114
115X509_LOOKUP_METHOD *
116X509_LOOKUP_hash_dir(void)
117{
118 return (&x509_dir_lookup);
119}
120
121static int
122dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
123 char **retp)
124{
125 int ret = 0;
126 BY_DIR *ld;
127
128 ld = (BY_DIR *)ctx->method_data;
129
130 switch (cmd) {
131 case X509_L_ADD_DIR:
132 if (argl == X509_FILETYPE_DEFAULT) {
133 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
134 X509_FILETYPE_PEM);
135 if (!ret) {
136 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
137 }
138 } else
139 ret = add_cert_dir(ld, argp, (int)argl);
140 break;
141 }
142 return (ret);
143}
144
145static int
146new_dir(X509_LOOKUP *lu)
147{
148 BY_DIR *a;
149
150 if ((a = malloc(sizeof(BY_DIR))) == NULL)
151 return (0);
152 if ((a->buffer = BUF_MEM_new()) == NULL) {
153 free(a);
154 return (0);
155 }
156 a->dirs = NULL;
157 lu->method_data = (char *)a;
158 return (1);
159}
160
161static void
162by_dir_hash_free(BY_DIR_HASH *hash)
163{
164 free(hash);
165}
166
167static int
168by_dir_hash_cmp(const BY_DIR_HASH * const *a,
169 const BY_DIR_HASH * const *b)
170{
171 if ((*a)->hash > (*b)->hash)
172 return 1;
173 if ((*a)->hash < (*b)->hash)
174 return -1;
175 return 0;
176}
177
178static void
179by_dir_entry_free(BY_DIR_ENTRY *ent)
180{
181 free(ent->dir);
182 if (ent->hashes)
183 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
184 free(ent);
185}
186
187static void
188free_dir(X509_LOOKUP *lu)
189{
190 BY_DIR *a;
191
192 a = (BY_DIR *)lu->method_data;
193 if (a->dirs != NULL)
194 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
195 if (a->buffer != NULL)
196 BUF_MEM_free(a->buffer);
197 free(a);
198}
199
200static int
201add_cert_dir(BY_DIR *ctx, const char *dir, int type)
202{
203 int j;
204 const char *s, *ss, *p;
205 ptrdiff_t len;
206
207 if (dir == NULL || !*dir) {
208 X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
209 return 0;
210 }
211
212 s = dir;
213 p = s;
214 do {
215 if ((*p == ':') || (*p == '\0')) {
216 BY_DIR_ENTRY *ent;
217 ss = s;
218 s = p + 1;
219 len = p - ss;
220 if (len == 0)
221 continue;
222 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
223 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
224 if (strlen(ent->dir) == (size_t)len &&
225 strncmp(ent->dir, ss, (size_t)len) == 0)
226 break;
227 }
228 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
229 continue;
230 if (ctx->dirs == NULL) {
231 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
232 if (!ctx->dirs) {
233 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
234 return 0;
235 }
236 }
237 ent = malloc(sizeof(BY_DIR_ENTRY));
238 if (!ent) {
239 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
240 return 0;
241 }
242 ent->dir_type = type;
243 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
244 ent->dir = strndup(ss, (size_t)len);
245 if (!ent->dir || !ent->hashes) {
246 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
247 by_dir_entry_free(ent);
248 return 0;
249 }
250 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
251 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
252 by_dir_entry_free(ent);
253 return 0;
254 }
255 }
256 } while (*p++ != '\0');
257 return 1;
258}
259
260static int
261get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
262 X509_OBJECT *ret)
263{
264 BY_DIR *ctx;
265 union {
266 struct {
267 X509 st_x509;
268 X509_CINF st_x509_cinf;
269 } x509;
270 struct {
271 X509_CRL st_crl;
272 X509_CRL_INFO st_crl_info;
273 } crl;
274 } data;
275 int ok = 0;
276 int i, j, k;
277 unsigned long h;
278 BUF_MEM *b = NULL;
279 X509_OBJECT stmp, *tmp;
280 const char *postfix="";
281
282 if (name == NULL)
283 return (0);
284
285 stmp.type = type;
286 if (type == X509_LU_X509) {
287 data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
288 data.x509.st_x509_cinf.subject = name;
289 stmp.data.x509 = &data.x509.st_x509;
290 postfix="";
291 } else if (type == X509_LU_CRL) {
292 data.crl.st_crl.crl = &data.crl.st_crl_info;
293 data.crl.st_crl_info.issuer = name;
294 stmp.data.crl = &data.crl.st_crl;
295 postfix="r";
296 } else {
297 X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
298 goto finish;
299 }
300
301 if ((b = BUF_MEM_new()) == NULL) {
302 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
303 goto finish;
304 }
305
306 ctx = (BY_DIR *)xl->method_data;
307
308 h = X509_NAME_hash(name);
309 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
310 BY_DIR_ENTRY *ent;
311 int idx;
312 BY_DIR_HASH htmp, *hent;
313 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
314 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
315 if (!BUF_MEM_grow(b, j)) {
316 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
317 goto finish;
318 }
319 if (type == X509_LU_CRL && ent->hashes) {
320 htmp.hash = h;
321 CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
322 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
323 if (idx >= 0) {
324 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
325 k = hent->suffix;
326 } else {
327 hent = NULL;
328 k = 0;
329 }
330 CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE);
331 } else {
332 k = 0;
333 hent = NULL;
334 }
335 for (;;) {
336 (void) snprintf(b->data, b->max, "%s/%08lx.%s%d",
337 ent->dir, h, postfix, k);
338
339 {
340 struct stat st;
341 if (stat(b->data, &st) < 0)
342 break;
343 }
344 /* found one. */
345 if (type == X509_LU_X509) {
346 if ((X509_load_cert_file(xl, b->data,
347 ent->dir_type)) == 0)
348 break;
349 } else if (type == X509_LU_CRL) {
350 if ((X509_load_crl_file(xl, b->data,
351 ent->dir_type)) == 0)
352 break;
353 }
354 /* else case will caught higher up */
355 k++;
356 }
357
358 /* we have added it to the cache so now pull it out again */
359 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
360 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
361 if (j != -1)
362 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
363 else
364 tmp = NULL;
365 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
366
367 /* If a CRL, update the last file suffix added for this */
368 if (type == X509_LU_CRL) {
369 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
370 /*
371 * Look for entry again in case another thread added
372 * an entry first.
373 */
374 if (!hent) {
375 htmp.hash = h;
376 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
377 if (idx >= 0)
378 hent = sk_BY_DIR_HASH_value(
379 ent->hashes, idx);
380 }
381 if (!hent) {
382 hent = malloc(sizeof(BY_DIR_HASH));
383 if (!hent) {
384 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
385 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
386 ok = 0;
387 goto finish;
388 }
389 hent->hash = h;
390 hent->suffix = k;
391 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
392 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
393 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
394 free(hent);
395 ok = 0;
396 goto finish;
397 }
398 } else if (hent->suffix < k)
399 hent->suffix = k;
400
401 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
402
403 }
404
405 if (tmp != NULL) {
406 ok = 1;
407 ret->type = tmp->type;
408 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
409 /*
410 * If we were going to up the reference count,
411 * we would need to do it on a perl 'type' basis
412 */
413 /* CRYPTO_add(&tmp->data.x509->references,1,
414 CRYPTO_LOCK_X509);*/
415 goto finish;
416 }
417 }
418finish:
419 if (b != NULL)
420 BUF_MEM_free(b);
421 return (ok);
422}
diff --git a/src/lib/libcrypto/x509/by_file.c b/src/lib/libcrypto/x509/by_file.c
deleted file mode 100644
index 377b3b0a8b..0000000000
--- a/src/lib/libcrypto/x509/by_file.c
+++ /dev/null
@@ -1,274 +0,0 @@
1/* $OpenBSD: by_file.c,v 1.20 2016/03/11 07:08:45 mmcc Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <time.h>
62#include <unistd.h>
63
64#include <openssl/buffer.h>
65#include <openssl/err.h>
66#include <openssl/pem.h>
67#include <openssl/lhash.h>
68#include <openssl/x509.h>
69
70static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
71 long argl, char **ret);
72
73static X509_LOOKUP_METHOD x509_file_lookup = {
74 .name = "Load file into cache",
75 .new_item = NULL,
76 .free = NULL,
77 .init = NULL,
78 .shutdown = NULL,
79 .ctrl = by_file_ctrl,
80 .get_by_subject = NULL,
81 .get_by_issuer_serial = NULL,
82 .get_by_fingerprint = NULL,
83 .get_by_alias = NULL,
84};
85
86X509_LOOKUP_METHOD *
87X509_LOOKUP_file(void)
88{
89 return (&x509_file_lookup);
90}
91
92static int
93by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
94 char **ret)
95{
96 int ok = 0;
97
98 switch (cmd) {
99 case X509_L_FILE_LOAD:
100 if (argl == X509_FILETYPE_DEFAULT) {
101 ok = (X509_load_cert_crl_file(ctx,
102 X509_get_default_cert_file(),
103 X509_FILETYPE_PEM) != 0);
104 if (!ok) {
105 X509err(X509_F_BY_FILE_CTRL,
106 X509_R_LOADING_DEFAULTS);
107 }
108 } else {
109 if (argl == X509_FILETYPE_PEM)
110 ok = (X509_load_cert_crl_file(ctx, argp,
111 X509_FILETYPE_PEM) != 0);
112 else
113 ok = (X509_load_cert_file(ctx,
114 argp, (int)argl) != 0);
115 }
116 break;
117 }
118 return (ok);
119}
120
121int
122X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
123{
124 int ret = 0;
125 BIO *in = NULL;
126 int i, count = 0;
127 X509 *x = NULL;
128
129 if (file == NULL)
130 return (1);
131 in = BIO_new(BIO_s_file_internal());
132
133 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
134 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
135 goto err;
136 }
137
138 if (type == X509_FILETYPE_PEM) {
139 for (;;) {
140 x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
141 if (x == NULL) {
142 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
143 PEM_R_NO_START_LINE) && (count > 0)) {
144 ERR_clear_error();
145 break;
146 } else {
147 X509err(X509_F_X509_LOAD_CERT_FILE,
148 ERR_R_PEM_LIB);
149 goto err;
150 }
151 }
152 i = X509_STORE_add_cert(ctx->store_ctx, x);
153 if (!i)
154 goto err;
155 count++;
156 X509_free(x);
157 x = NULL;
158 }
159 ret = count;
160 } else if (type == X509_FILETYPE_ASN1) {
161 x = d2i_X509_bio(in, NULL);
162 if (x == NULL) {
163 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
164 goto err;
165 }
166 i = X509_STORE_add_cert(ctx->store_ctx, x);
167 if (!i)
168 goto err;
169 ret = i;
170 } else {
171 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
172 goto err;
173 }
174err:
175 X509_free(x);
176 BIO_free(in);
177 return (ret);
178}
179
180int
181X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
182{
183 int ret = 0;
184 BIO *in = NULL;
185 int i, count = 0;
186 X509_CRL *x = NULL;
187
188 if (file == NULL)
189 return (1);
190 in = BIO_new(BIO_s_file_internal());
191
192 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
193 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
194 goto err;
195 }
196
197 if (type == X509_FILETYPE_PEM) {
198 for (;;) {
199 x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
200 if (x == NULL) {
201 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
202 PEM_R_NO_START_LINE) && (count > 0)) {
203 ERR_clear_error();
204 break;
205 } else {
206 X509err(X509_F_X509_LOAD_CRL_FILE,
207 ERR_R_PEM_LIB);
208 goto err;
209 }
210 }
211 i = X509_STORE_add_crl(ctx->store_ctx, x);
212 if (!i)
213 goto err;
214 count++;
215 X509_CRL_free(x);
216 x = NULL;
217 }
218 ret = count;
219 } else if (type == X509_FILETYPE_ASN1) {
220 x = d2i_X509_CRL_bio(in, NULL);
221 if (x == NULL) {
222 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
223 goto err;
224 }
225 i = X509_STORE_add_crl(ctx->store_ctx, x);
226 if (!i)
227 goto err;
228 ret = i;
229 } else {
230 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
231 goto err;
232 }
233err:
234 if (x != NULL)
235 X509_CRL_free(x);
236 BIO_free(in);
237 return (ret);
238}
239
240int
241X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
242{
243 STACK_OF(X509_INFO) *inf;
244 X509_INFO *itmp;
245 BIO *in;
246 int i, count = 0;
247 if (type != X509_FILETYPE_PEM)
248 return X509_load_cert_file(ctx, file, type);
249 in = BIO_new_file(file, "r");
250 if (!in) {
251 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
252 return 0;
253 }
254 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
255 BIO_free(in);
256 if (!inf) {
257 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
258 return 0;
259 }
260 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
261 itmp = sk_X509_INFO_value(inf, i);
262 if (itmp->x509) {
263 X509_STORE_add_cert(ctx->store_ctx, itmp->x509);
264 count++;
265 }
266 if (itmp->crl) {
267 X509_STORE_add_crl(ctx->store_ctx, itmp->crl);
268 count++;
269 }
270 }
271 sk_X509_INFO_pop_free(inf, X509_INFO_free);
272 return count;
273}
274
diff --git a/src/lib/libcrypto/x509/by_mem.c b/src/lib/libcrypto/x509/by_mem.c
deleted file mode 100644
index ecab813406..0000000000
--- a/src/lib/libcrypto/x509/by_mem.c
+++ /dev/null
@@ -1,138 +0,0 @@
1/* $OpenBSD: by_mem.c,v 1.3 2015/02/05 01:33:22 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <sys/uio.h>
60#include <errno.h>
61#include <stdio.h>
62#include <time.h>
63#include <unistd.h>
64
65#include <openssl/buffer.h>
66#include <openssl/err.h>
67#include <openssl/pem.h>
68#include <openssl/lhash.h>
69#include <openssl/x509.h>
70
71static int by_mem_ctrl(X509_LOOKUP *, int, const char *, long, char **);
72
73static X509_LOOKUP_METHOD x509_mem_lookup = {
74 .name = "Load cert from memory",
75 .new_item = NULL,
76 .free = NULL,
77 .init = NULL,
78 .shutdown = NULL,
79 .ctrl = by_mem_ctrl,
80 .get_by_subject = NULL,
81 .get_by_issuer_serial = NULL,
82 .get_by_fingerprint = NULL,
83 .get_by_alias = NULL,
84};
85
86X509_LOOKUP_METHOD *
87X509_LOOKUP_mem(void)
88{
89 return (&x509_mem_lookup);
90}
91
92static int
93by_mem_ctrl(X509_LOOKUP *lu, int cmd, const char *buf,
94 long type, char **ret)
95{
96 STACK_OF(X509_INFO) *inf = NULL;
97 const struct iovec *iov;
98 X509_INFO *itmp;
99 BIO *in = NULL;
100 int i, count = 0, ok = 0;
101
102 iov = (const struct iovec *)buf;
103
104 if (!(cmd == X509_L_MEM && type == X509_FILETYPE_PEM))
105 goto done;
106
107 if ((in = BIO_new_mem_buf(iov->iov_base, iov->iov_len)) == NULL)
108 goto done;
109
110 if ((inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL)) == NULL)
111 goto done;
112
113 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
114 itmp = sk_X509_INFO_value(inf, i);
115 if (itmp->x509) {
116 ok = X509_STORE_add_cert(lu->store_ctx, itmp->x509);
117 if (!ok)
118 goto done;
119 count++;
120 }
121 if (itmp->crl) {
122 ok = X509_STORE_add_crl(lu->store_ctx, itmp->crl);
123 if (!ok)
124 goto done;
125 count++;
126 }
127 }
128
129 ok = count != 0;
130 done:
131 if (count == 0)
132 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,ERR_R_PEM_LIB);
133 if (inf != NULL)
134 sk_X509_INFO_pop_free(inf, X509_INFO_free);
135 if (in != NULL)
136 BIO_free(in);
137 return (ok);
138}
diff --git a/src/lib/libcrypto/x509/x509.h b/src/lib/libcrypto/x509/x509.h
deleted file mode 100644
index cf00ea5ebe..0000000000
--- a/src/lib/libcrypto/x509/x509.h
+++ /dev/null
@@ -1,1270 +0,0 @@
1/* $OpenBSD: x509.h,v 1.23 2015/04/12 15:15:51 doug Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * ECDH support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */
63
64#ifndef HEADER_X509_H
65#define HEADER_X509_H
66
67#include <openssl/opensslconf.h>
68
69#ifndef OPENSSL_NO_BUFFER
70#include <openssl/buffer.h>
71#endif
72#ifndef OPENSSL_NO_EVP
73#include <openssl/evp.h>
74#endif
75#ifndef OPENSSL_NO_BIO
76#include <openssl/bio.h>
77#endif
78#include <openssl/stack.h>
79#include <openssl/asn1.h>
80#include <openssl/safestack.h>
81
82#ifndef OPENSSL_NO_EC
83#include <openssl/ec.h>
84#endif
85
86#ifndef OPENSSL_NO_ECDSA
87#include <openssl/ecdsa.h>
88#endif
89
90#ifndef OPENSSL_NO_ECDH
91#include <openssl/ecdh.h>
92#endif
93
94#ifndef OPENSSL_NO_DEPRECATED
95#ifndef OPENSSL_NO_RSA
96#include <openssl/rsa.h>
97#endif
98#ifndef OPENSSL_NO_DSA
99#include <openssl/dsa.h>
100#endif
101#ifndef OPENSSL_NO_DH
102#include <openssl/dh.h>
103#endif
104#endif
105
106#ifndef OPENSSL_NO_SHA
107#include <openssl/sha.h>
108#endif
109#include <openssl/ossl_typ.h>
110
111#ifdef __cplusplus
112extern "C" {
113#endif
114
115#define X509_FILETYPE_PEM 1
116#define X509_FILETYPE_ASN1 2
117#define X509_FILETYPE_DEFAULT 3
118
119#define X509v3_KU_DIGITAL_SIGNATURE 0x0080
120#define X509v3_KU_NON_REPUDIATION 0x0040
121#define X509v3_KU_KEY_ENCIPHERMENT 0x0020
122#define X509v3_KU_DATA_ENCIPHERMENT 0x0010
123#define X509v3_KU_KEY_AGREEMENT 0x0008
124#define X509v3_KU_KEY_CERT_SIGN 0x0004
125#define X509v3_KU_CRL_SIGN 0x0002
126#define X509v3_KU_ENCIPHER_ONLY 0x0001
127#define X509v3_KU_DECIPHER_ONLY 0x8000
128#define X509v3_KU_UNDEF 0xffff
129
130typedef struct X509_objects_st
131 {
132 int nid;
133 int (*a2i)(void);
134 int (*i2a)(void);
135 } X509_OBJECTS;
136
137struct X509_algor_st
138 {
139 ASN1_OBJECT *algorithm;
140 ASN1_TYPE *parameter;
141 } /* X509_ALGOR */;
142
143DECLARE_ASN1_SET_OF(X509_ALGOR)
144
145typedef STACK_OF(X509_ALGOR) X509_ALGORS;
146
147typedef struct X509_val_st
148 {
149 ASN1_TIME *notBefore;
150 ASN1_TIME *notAfter;
151 } X509_VAL;
152
153struct X509_pubkey_st
154 {
155 X509_ALGOR *algor;
156 ASN1_BIT_STRING *public_key;
157 EVP_PKEY *pkey;
158 };
159
160typedef struct X509_sig_st
161 {
162 X509_ALGOR *algor;
163 ASN1_OCTET_STRING *digest;
164 } X509_SIG;
165
166typedef struct X509_name_entry_st
167 {
168 ASN1_OBJECT *object;
169 ASN1_STRING *value;
170 int set;
171 int size; /* temp variable */
172 } X509_NAME_ENTRY;
173
174DECLARE_STACK_OF(X509_NAME_ENTRY)
175DECLARE_ASN1_SET_OF(X509_NAME_ENTRY)
176
177/* we always keep X509_NAMEs in 2 forms. */
178struct X509_name_st
179 {
180 STACK_OF(X509_NAME_ENTRY) *entries;
181 int modified; /* true if 'bytes' needs to be built */
182#ifndef OPENSSL_NO_BUFFER
183 BUF_MEM *bytes;
184#else
185 char *bytes;
186#endif
187/* unsigned long hash; Keep the hash around for lookups */
188 unsigned char *canon_enc;
189 int canon_enclen;
190 } /* X509_NAME */;
191
192DECLARE_STACK_OF(X509_NAME)
193
194#define X509_EX_V_NETSCAPE_HACK 0x8000
195#define X509_EX_V_INIT 0x0001
196typedef struct X509_extension_st
197 {
198 ASN1_OBJECT *object;
199 ASN1_BOOLEAN critical;
200 ASN1_OCTET_STRING *value;
201 } X509_EXTENSION;
202
203typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS;
204
205DECLARE_STACK_OF(X509_EXTENSION)
206DECLARE_ASN1_SET_OF(X509_EXTENSION)
207
208/* a sequence of these are used */
209typedef struct x509_attributes_st
210 {
211 ASN1_OBJECT *object;
212 int single; /* 0 for a set, 1 for a single item (which is wrong) */
213 union {
214 char *ptr;
215/* 0 */ STACK_OF(ASN1_TYPE) *set;
216/* 1 */ ASN1_TYPE *single;
217 } value;
218 } X509_ATTRIBUTE;
219
220DECLARE_STACK_OF(X509_ATTRIBUTE)
221DECLARE_ASN1_SET_OF(X509_ATTRIBUTE)
222
223
224typedef struct X509_req_info_st
225 {
226 ASN1_ENCODING enc;
227 ASN1_INTEGER *version;
228 X509_NAME *subject;
229 X509_PUBKEY *pubkey;
230 /* d=2 hl=2 l= 0 cons: cont: 00 */
231 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
232 } X509_REQ_INFO;
233
234typedef struct X509_req_st
235 {
236 X509_REQ_INFO *req_info;
237 X509_ALGOR *sig_alg;
238 ASN1_BIT_STRING *signature;
239 int references;
240 } X509_REQ;
241
242typedef struct x509_cinf_st
243 {
244 ASN1_INTEGER *version; /* [ 0 ] default of v1 */
245 ASN1_INTEGER *serialNumber;
246 X509_ALGOR *signature;
247 X509_NAME *issuer;
248 X509_VAL *validity;
249 X509_NAME *subject;
250 X509_PUBKEY *key;
251 ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
252 ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
253 STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */
254 ASN1_ENCODING enc;
255 } X509_CINF;
256
257/* This stuff is certificate "auxiliary info"
258 * it contains details which are useful in certificate
259 * stores and databases. When used this is tagged onto
260 * the end of the certificate itself
261 */
262
263typedef struct x509_cert_aux_st
264 {
265 STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */
266 STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */
267 ASN1_UTF8STRING *alias; /* "friendly name" */
268 ASN1_OCTET_STRING *keyid; /* key id of private key */
269 STACK_OF(X509_ALGOR) *other; /* other unspecified info */
270 } X509_CERT_AUX;
271
272struct x509_st
273 {
274 X509_CINF *cert_info;
275 X509_ALGOR *sig_alg;
276 ASN1_BIT_STRING *signature;
277 int valid;
278 int references;
279 char *name;
280 CRYPTO_EX_DATA ex_data;
281 /* These contain copies of various extension values */
282 long ex_pathlen;
283 long ex_pcpathlen;
284 unsigned long ex_flags;
285 unsigned long ex_kusage;
286 unsigned long ex_xkusage;
287 unsigned long ex_nscert;
288 ASN1_OCTET_STRING *skid;
289 AUTHORITY_KEYID *akid;
290 X509_POLICY_CACHE *policy_cache;
291 STACK_OF(DIST_POINT) *crldp;
292 STACK_OF(GENERAL_NAME) *altname;
293 NAME_CONSTRAINTS *nc;
294#ifndef OPENSSL_NO_SHA
295 unsigned char sha1_hash[SHA_DIGEST_LENGTH];
296#endif
297 X509_CERT_AUX *aux;
298 } /* X509 */;
299
300DECLARE_STACK_OF(X509)
301DECLARE_ASN1_SET_OF(X509)
302
303/* This is used for a table of trust checking functions */
304
305typedef struct x509_trust_st {
306 int trust;
307 int flags;
308 int (*check_trust)(struct x509_trust_st *, X509 *, int);
309 char *name;
310 int arg1;
311 void *arg2;
312} X509_TRUST;
313
314DECLARE_STACK_OF(X509_TRUST)
315
316typedef struct x509_cert_pair_st {
317 X509 *forward;
318 X509 *reverse;
319} X509_CERT_PAIR;
320
321/* standard trust ids */
322
323#define X509_TRUST_DEFAULT -1 /* Only valid in purpose settings */
324
325#define X509_TRUST_COMPAT 1
326#define X509_TRUST_SSL_CLIENT 2
327#define X509_TRUST_SSL_SERVER 3
328#define X509_TRUST_EMAIL 4
329#define X509_TRUST_OBJECT_SIGN 5
330#define X509_TRUST_OCSP_SIGN 6
331#define X509_TRUST_OCSP_REQUEST 7
332#define X509_TRUST_TSA 8
333
334/* Keep these up to date! */
335#define X509_TRUST_MIN 1
336#define X509_TRUST_MAX 8
337
338
339/* trust_flags values */
340#define X509_TRUST_DYNAMIC 1
341#define X509_TRUST_DYNAMIC_NAME 2
342
343/* check_trust return codes */
344
345#define X509_TRUST_TRUSTED 1
346#define X509_TRUST_REJECTED 2
347#define X509_TRUST_UNTRUSTED 3
348
349/* Flags for X509_print_ex() */
350
351#define X509_FLAG_COMPAT 0
352#define X509_FLAG_NO_HEADER 1L
353#define X509_FLAG_NO_VERSION (1L << 1)
354#define X509_FLAG_NO_SERIAL (1L << 2)
355#define X509_FLAG_NO_SIGNAME (1L << 3)
356#define X509_FLAG_NO_ISSUER (1L << 4)
357#define X509_FLAG_NO_VALIDITY (1L << 5)
358#define X509_FLAG_NO_SUBJECT (1L << 6)
359#define X509_FLAG_NO_PUBKEY (1L << 7)
360#define X509_FLAG_NO_EXTENSIONS (1L << 8)
361#define X509_FLAG_NO_SIGDUMP (1L << 9)
362#define X509_FLAG_NO_AUX (1L << 10)
363#define X509_FLAG_NO_ATTRIBUTES (1L << 11)
364
365/* Flags specific to X509_NAME_print_ex() */
366
367/* The field separator information */
368
369#define XN_FLAG_SEP_MASK (0xf << 16)
370
371#define XN_FLAG_COMPAT 0 /* Traditional SSLeay: use old X509_NAME_print */
372#define XN_FLAG_SEP_COMMA_PLUS (1 << 16) /* RFC2253 ,+ */
373#define XN_FLAG_SEP_CPLUS_SPC (2 << 16) /* ,+ spaced: more readable */
374#define XN_FLAG_SEP_SPLUS_SPC (3 << 16) /* ;+ spaced */
375#define XN_FLAG_SEP_MULTILINE (4 << 16) /* One line per field */
376
377#define XN_FLAG_DN_REV (1 << 20) /* Reverse DN order */
378
379/* How the field name is shown */
380
381#define XN_FLAG_FN_MASK (0x3 << 21)
382
383#define XN_FLAG_FN_SN 0 /* Object short name */
384#define XN_FLAG_FN_LN (1 << 21) /* Object long name */
385#define XN_FLAG_FN_OID (2 << 21) /* Always use OIDs */
386#define XN_FLAG_FN_NONE (3 << 21) /* No field names */
387
388#define XN_FLAG_SPC_EQ (1 << 23) /* Put spaces round '=' */
389
390/* This determines if we dump fields we don't recognise:
391 * RFC2253 requires this.
392 */
393
394#define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24)
395
396#define XN_FLAG_FN_ALIGN (1 << 25) /* Align field names to 20 characters */
397
398/* Complete set of RFC2253 flags */
399
400#define XN_FLAG_RFC2253 (ASN1_STRFLGS_RFC2253 | \
401 XN_FLAG_SEP_COMMA_PLUS | \
402 XN_FLAG_DN_REV | \
403 XN_FLAG_FN_SN | \
404 XN_FLAG_DUMP_UNKNOWN_FIELDS)
405
406/* readable oneline form */
407
408#define XN_FLAG_ONELINE (ASN1_STRFLGS_RFC2253 | \
409 ASN1_STRFLGS_ESC_QUOTE | \
410 XN_FLAG_SEP_CPLUS_SPC | \
411 XN_FLAG_SPC_EQ | \
412 XN_FLAG_FN_SN)
413
414/* readable multiline form */
415
416#define XN_FLAG_MULTILINE (ASN1_STRFLGS_ESC_CTRL | \
417 ASN1_STRFLGS_ESC_MSB | \
418 XN_FLAG_SEP_MULTILINE | \
419 XN_FLAG_SPC_EQ | \
420 XN_FLAG_FN_LN | \
421 XN_FLAG_FN_ALIGN)
422
423struct x509_revoked_st
424 {
425 ASN1_INTEGER *serialNumber;
426 ASN1_TIME *revocationDate;
427 STACK_OF(X509_EXTENSION) /* optional */ *extensions;
428 /* Set up if indirect CRL */
429 STACK_OF(GENERAL_NAME) *issuer;
430 /* Revocation reason */
431 int reason;
432 int sequence; /* load sequence */
433 };
434
435DECLARE_STACK_OF(X509_REVOKED)
436DECLARE_ASN1_SET_OF(X509_REVOKED)
437
438typedef struct X509_crl_info_st
439 {
440 ASN1_INTEGER *version;
441 X509_ALGOR *sig_alg;
442 X509_NAME *issuer;
443 ASN1_TIME *lastUpdate;
444 ASN1_TIME *nextUpdate;
445 STACK_OF(X509_REVOKED) *revoked;
446 STACK_OF(X509_EXTENSION) /* [0] */ *extensions;
447 ASN1_ENCODING enc;
448 } X509_CRL_INFO;
449
450struct X509_crl_st
451 {
452 /* actual signature */
453 X509_CRL_INFO *crl;
454 X509_ALGOR *sig_alg;
455 ASN1_BIT_STRING *signature;
456 int references;
457 int flags;
458 /* Copies of various extensions */
459 AUTHORITY_KEYID *akid;
460 ISSUING_DIST_POINT *idp;
461 /* Convenient breakdown of IDP */
462 int idp_flags;
463 int idp_reasons;
464 /* CRL and base CRL numbers for delta processing */
465 ASN1_INTEGER *crl_number;
466 ASN1_INTEGER *base_crl_number;
467#ifndef OPENSSL_NO_SHA
468 unsigned char sha1_hash[SHA_DIGEST_LENGTH];
469#endif
470 STACK_OF(GENERAL_NAMES) *issuers;
471 const X509_CRL_METHOD *meth;
472 void *meth_data;
473 } /* X509_CRL */;
474
475DECLARE_STACK_OF(X509_CRL)
476DECLARE_ASN1_SET_OF(X509_CRL)
477
478typedef struct private_key_st
479 {
480 int version;
481 /* The PKCS#8 data types */
482 X509_ALGOR *enc_algor;
483 ASN1_OCTET_STRING *enc_pkey; /* encrypted pub key */
484
485 /* When decrypted, the following will not be NULL */
486 EVP_PKEY *dec_pkey;
487
488 /* used to encrypt and decrypt */
489 int key_length;
490 char *key_data;
491 int key_free; /* true if we should auto free key_data */
492
493 /* expanded version of 'enc_algor' */
494 EVP_CIPHER_INFO cipher;
495
496 int references;
497 } X509_PKEY;
498
499#ifndef OPENSSL_NO_EVP
500typedef struct X509_info_st
501 {
502 X509 *x509;
503 X509_CRL *crl;
504 X509_PKEY *x_pkey;
505
506 EVP_CIPHER_INFO enc_cipher;
507 int enc_len;
508 char *enc_data;
509
510 int references;
511 } X509_INFO;
512
513DECLARE_STACK_OF(X509_INFO)
514#endif
515
516/* The next 2 structures and their 8 routines were sent to me by
517 * Pat Richard <patr@x509.com> and are used to manipulate
518 * Netscapes spki structures - useful if you are writing a CA web page
519 */
520typedef struct Netscape_spkac_st
521 {
522 X509_PUBKEY *pubkey;
523 ASN1_IA5STRING *challenge; /* challenge sent in atlas >= PR2 */
524 } NETSCAPE_SPKAC;
525
526typedef struct Netscape_spki_st
527 {
528 NETSCAPE_SPKAC *spkac; /* signed public key and challenge */
529 X509_ALGOR *sig_algor;
530 ASN1_BIT_STRING *signature;
531 } NETSCAPE_SPKI;
532
533/* Netscape certificate sequence structure */
534typedef struct Netscape_certificate_sequence
535 {
536 ASN1_OBJECT *type;
537 STACK_OF(X509) *certs;
538 } NETSCAPE_CERT_SEQUENCE;
539
540/* Unused (and iv length is wrong)
541typedef struct CBCParameter_st
542 {
543 unsigned char iv[8];
544 } CBC_PARAM;
545*/
546
547/* Password based encryption structure */
548
549typedef struct PBEPARAM_st {
550ASN1_OCTET_STRING *salt;
551ASN1_INTEGER *iter;
552} PBEPARAM;
553
554/* Password based encryption V2 structures */
555
556typedef struct PBE2PARAM_st {
557X509_ALGOR *keyfunc;
558X509_ALGOR *encryption;
559} PBE2PARAM;
560
561typedef struct PBKDF2PARAM_st {
562ASN1_TYPE *salt; /* Usually OCTET STRING but could be anything */
563ASN1_INTEGER *iter;
564ASN1_INTEGER *keylength;
565X509_ALGOR *prf;
566} PBKDF2PARAM;
567
568
569/* PKCS#8 private key info structure */
570
571struct pkcs8_priv_key_info_st
572 {
573 int broken; /* Flag for various broken formats */
574#define PKCS8_OK 0
575#define PKCS8_NO_OCTET 1
576#define PKCS8_EMBEDDED_PARAM 2
577#define PKCS8_NS_DB 3
578#define PKCS8_NEG_PRIVKEY 4
579 ASN1_INTEGER *version;
580 X509_ALGOR *pkeyalg;
581 ASN1_TYPE *pkey; /* Should be OCTET STRING but some are broken */
582 STACK_OF(X509_ATTRIBUTE) *attributes;
583 };
584
585#ifdef __cplusplus
586}
587#endif
588
589#include <openssl/x509_vfy.h>
590#include <openssl/pkcs7.h>
591
592#ifdef __cplusplus
593extern "C" {
594#endif
595
596#define X509_EXT_PACK_UNKNOWN 1
597#define X509_EXT_PACK_STRING 2
598
599#define X509_get_version(x) ASN1_INTEGER_get((x)->cert_info->version)
600/* #define X509_get_serialNumber(x) ((x)->cert_info->serialNumber) */
601#define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
602#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
603#define X509_extract_key(x) X509_get_pubkey(x) /*****/
604#define X509_REQ_get_version(x) ASN1_INTEGER_get((x)->req_info->version)
605#define X509_REQ_get_subject_name(x) ((x)->req_info->subject)
606#define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
607#define X509_name_cmp(a,b) X509_NAME_cmp((a),(b))
608#define X509_get_signature_type(x) EVP_PKEY_type(OBJ_obj2nid((x)->sig_alg->algorithm))
609
610#define X509_CRL_get_version(x) ASN1_INTEGER_get((x)->crl->version)
611#define X509_CRL_get_lastUpdate(x) ((x)->crl->lastUpdate)
612#define X509_CRL_get_nextUpdate(x) ((x)->crl->nextUpdate)
613#define X509_CRL_get_issuer(x) ((x)->crl->issuer)
614#define X509_CRL_get_REVOKED(x) ((x)->crl->revoked)
615
616void X509_CRL_set_default_method(const X509_CRL_METHOD *meth);
617X509_CRL_METHOD *X509_CRL_METHOD_new(
618 int (*crl_init)(X509_CRL *crl),
619 int (*crl_free)(X509_CRL *crl),
620 int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
621 ASN1_INTEGER *ser, X509_NAME *issuer),
622 int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk));
623void X509_CRL_METHOD_free(X509_CRL_METHOD *m);
624
625void X509_CRL_set_meth_data(X509_CRL *crl, void *dat);
626void *X509_CRL_get_meth_data(X509_CRL *crl);
627
628/* This one is only used so that a binary form can output, as in
629 * i2d_X509_NAME(X509_get_X509_PUBKEY(x),&buf) */
630#define X509_get_X509_PUBKEY(x) ((x)->cert_info->key)
631
632
633const char *X509_verify_cert_error_string(long n);
634
635#ifndef OPENSSL_NO_EVP
636int X509_verify(X509 *a, EVP_PKEY *r);
637
638int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r);
639int X509_CRL_verify(X509_CRL *a, EVP_PKEY *r);
640int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r);
641
642NETSCAPE_SPKI * NETSCAPE_SPKI_b64_decode(const char *str, int len);
643char * NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *x);
644EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x);
645int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey);
646
647int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki);
648
649int X509_signature_dump(BIO *bp,const ASN1_STRING *sig, int indent);
650int X509_signature_print(BIO *bp,X509_ALGOR *alg, ASN1_STRING *sig);
651
652int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md);
653int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx);
654int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md);
655int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx);
656int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md);
657int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx);
658int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md);
659
660int X509_pubkey_digest(const X509 *data,const EVP_MD *type,
661 unsigned char *md, unsigned int *len);
662int X509_digest(const X509 *data,const EVP_MD *type,
663 unsigned char *md, unsigned int *len);
664int X509_CRL_digest(const X509_CRL *data,const EVP_MD *type,
665 unsigned char *md, unsigned int *len);
666int X509_REQ_digest(const X509_REQ *data,const EVP_MD *type,
667 unsigned char *md, unsigned int *len);
668int X509_NAME_digest(const X509_NAME *data,const EVP_MD *type,
669 unsigned char *md, unsigned int *len);
670#endif
671
672X509 *d2i_X509_fp(FILE *fp, X509 **x509);
673int i2d_X509_fp(FILE *fp,X509 *x509);
674X509_CRL *d2i_X509_CRL_fp(FILE *fp,X509_CRL **crl);
675int i2d_X509_CRL_fp(FILE *fp,X509_CRL *crl);
676X509_REQ *d2i_X509_REQ_fp(FILE *fp,X509_REQ **req);
677int i2d_X509_REQ_fp(FILE *fp,X509_REQ *req);
678#ifndef OPENSSL_NO_RSA
679RSA *d2i_RSAPrivateKey_fp(FILE *fp,RSA **rsa);
680int i2d_RSAPrivateKey_fp(FILE *fp,RSA *rsa);
681RSA *d2i_RSAPublicKey_fp(FILE *fp,RSA **rsa);
682int i2d_RSAPublicKey_fp(FILE *fp,RSA *rsa);
683RSA *d2i_RSA_PUBKEY_fp(FILE *fp,RSA **rsa);
684int i2d_RSA_PUBKEY_fp(FILE *fp,RSA *rsa);
685#endif
686#ifndef OPENSSL_NO_DSA
687DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa);
688int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa);
689int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
690#endif
691#ifndef OPENSSL_NO_EC
692EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey);
693int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey);
694EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey);
695int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey);
696#endif
697X509_SIG *d2i_PKCS8_fp(FILE *fp,X509_SIG **p8);
698int i2d_PKCS8_fp(FILE *fp,X509_SIG *p8);
699PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
700 PKCS8_PRIV_KEY_INFO **p8inf);
701int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,PKCS8_PRIV_KEY_INFO *p8inf);
702int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key);
703int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey);
704EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
705int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey);
706EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a);
707
708#ifndef OPENSSL_NO_BIO
709X509 *d2i_X509_bio(BIO *bp,X509 **x509);
710int i2d_X509_bio(BIO *bp,X509 *x509);
711X509_CRL *d2i_X509_CRL_bio(BIO *bp,X509_CRL **crl);
712int i2d_X509_CRL_bio(BIO *bp,X509_CRL *crl);
713X509_REQ *d2i_X509_REQ_bio(BIO *bp,X509_REQ **req);
714int i2d_X509_REQ_bio(BIO *bp,X509_REQ *req);
715#ifndef OPENSSL_NO_RSA
716RSA *d2i_RSAPrivateKey_bio(BIO *bp,RSA **rsa);
717int i2d_RSAPrivateKey_bio(BIO *bp,RSA *rsa);
718RSA *d2i_RSAPublicKey_bio(BIO *bp,RSA **rsa);
719int i2d_RSAPublicKey_bio(BIO *bp,RSA *rsa);
720RSA *d2i_RSA_PUBKEY_bio(BIO *bp,RSA **rsa);
721int i2d_RSA_PUBKEY_bio(BIO *bp,RSA *rsa);
722#endif
723#ifndef OPENSSL_NO_DSA
724DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa);
725int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa);
726DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa);
727int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
728#endif
729#ifndef OPENSSL_NO_EC
730EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey);
731int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey);
732EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey);
733int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey);
734#endif
735X509_SIG *d2i_PKCS8_bio(BIO *bp,X509_SIG **p8);
736int i2d_PKCS8_bio(BIO *bp,X509_SIG *p8);
737PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
738 PKCS8_PRIV_KEY_INFO **p8inf);
739int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,PKCS8_PRIV_KEY_INFO *p8inf);
740int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key);
741int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey);
742EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
743int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey);
744EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a);
745#endif
746
747X509 *X509_dup(X509 *x509);
748X509_ATTRIBUTE *X509_ATTRIBUTE_dup(X509_ATTRIBUTE *xa);
749X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *ex);
750X509_CRL *X509_CRL_dup(X509_CRL *crl);
751X509_REQ *X509_REQ_dup(X509_REQ *req);
752X509_ALGOR *X509_ALGOR_dup(X509_ALGOR *xn);
753int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval);
754void X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, void **ppval,
755 X509_ALGOR *algor);
756void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md);
757int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b);
758
759X509_NAME *X509_NAME_dup(X509_NAME *xn);
760X509_NAME_ENTRY *X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne);
761
762int X509_cmp_time(const ASN1_TIME *s, time_t *t);
763int X509_cmp_current_time(const ASN1_TIME *s);
764ASN1_TIME * X509_time_adj(ASN1_TIME *s, long adj, time_t *t);
765ASN1_TIME * X509_time_adj_ex(ASN1_TIME *s,
766 int offset_day, long offset_sec, time_t *t);
767ASN1_TIME * X509_gmtime_adj(ASN1_TIME *s, long adj);
768
769const char * X509_get_default_cert_area(void );
770const char * X509_get_default_cert_dir(void );
771const char * X509_get_default_cert_file(void );
772const char * X509_get_default_cert_dir_env(void );
773const char * X509_get_default_cert_file_env(void );
774const char * X509_get_default_private_dir(void );
775
776X509_REQ * X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md);
777X509 * X509_REQ_to_X509(X509_REQ *r, int days,EVP_PKEY *pkey);
778
779DECLARE_ASN1_FUNCTIONS(X509_ALGOR)
780DECLARE_ASN1_ENCODE_FUNCTIONS(X509_ALGORS, X509_ALGORS, X509_ALGORS)
781DECLARE_ASN1_FUNCTIONS(X509_VAL)
782
783DECLARE_ASN1_FUNCTIONS(X509_PUBKEY)
784
785int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
786EVP_PKEY * X509_PUBKEY_get(X509_PUBKEY *key);
787int X509_get_pubkey_parameters(EVP_PKEY *pkey,
788 STACK_OF(X509) *chain);
789int i2d_PUBKEY(EVP_PKEY *a,unsigned char **pp);
790EVP_PKEY * d2i_PUBKEY(EVP_PKEY **a,const unsigned char **pp,
791 long length);
792#ifndef OPENSSL_NO_RSA
793int i2d_RSA_PUBKEY(RSA *a,unsigned char **pp);
794RSA * d2i_RSA_PUBKEY(RSA **a,const unsigned char **pp,
795 long length);
796#endif
797#ifndef OPENSSL_NO_DSA
798int i2d_DSA_PUBKEY(DSA *a,unsigned char **pp);
799DSA * d2i_DSA_PUBKEY(DSA **a,const unsigned char **pp,
800 long length);
801#endif
802#ifndef OPENSSL_NO_EC
803int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp);
804EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp,
805 long length);
806#endif
807
808DECLARE_ASN1_FUNCTIONS(X509_SIG)
809DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO)
810DECLARE_ASN1_FUNCTIONS(X509_REQ)
811
812DECLARE_ASN1_FUNCTIONS(X509_ATTRIBUTE)
813X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value);
814
815DECLARE_ASN1_FUNCTIONS(X509_EXTENSION)
816DECLARE_ASN1_ENCODE_FUNCTIONS(X509_EXTENSIONS, X509_EXTENSIONS, X509_EXTENSIONS)
817
818DECLARE_ASN1_FUNCTIONS(X509_NAME_ENTRY)
819
820DECLARE_ASN1_FUNCTIONS(X509_NAME)
821
822int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
823
824DECLARE_ASN1_FUNCTIONS(X509_CINF)
825
826DECLARE_ASN1_FUNCTIONS(X509)
827DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX)
828
829DECLARE_ASN1_FUNCTIONS(X509_CERT_PAIR)
830
831int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
832 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
833int X509_set_ex_data(X509 *r, int idx, void *arg);
834void *X509_get_ex_data(X509 *r, int idx);
835int i2d_X509_AUX(X509 *a,unsigned char **pp);
836X509 * d2i_X509_AUX(X509 **a,const unsigned char **pp,long length);
837
838int X509_alias_set1(X509 *x, unsigned char *name, int len);
839int X509_keyid_set1(X509 *x, unsigned char *id, int len);
840unsigned char * X509_alias_get0(X509 *x, int *len);
841unsigned char * X509_keyid_get0(X509 *x, int *len);
842int (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int);
843int X509_TRUST_set(int *t, int trust);
844int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj);
845int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj);
846void X509_trust_clear(X509 *x);
847void X509_reject_clear(X509 *x);
848
849DECLARE_ASN1_FUNCTIONS(X509_REVOKED)
850DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO)
851DECLARE_ASN1_FUNCTIONS(X509_CRL)
852
853int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
854int X509_CRL_get0_by_serial(X509_CRL *crl,
855 X509_REVOKED **ret, ASN1_INTEGER *serial);
856int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x);
857
858X509_PKEY * X509_PKEY_new(void );
859void X509_PKEY_free(X509_PKEY *a);
860
861DECLARE_ASN1_FUNCTIONS(NETSCAPE_SPKI)
862DECLARE_ASN1_FUNCTIONS(NETSCAPE_SPKAC)
863DECLARE_ASN1_FUNCTIONS(NETSCAPE_CERT_SEQUENCE)
864
865#ifndef OPENSSL_NO_EVP
866X509_INFO * X509_INFO_new(void);
867void X509_INFO_free(X509_INFO *a);
868char * X509_NAME_oneline(X509_NAME *a,char *buf,int size);
869
870int ASN1_item_digest(const ASN1_ITEM *it,const EVP_MD *type,void *data,
871 unsigned char *md,unsigned int *len);
872
873int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *algor1,
874 ASN1_BIT_STRING *signature,void *data,EVP_PKEY *pkey);
875
876int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
877 ASN1_BIT_STRING *signature,
878 void *data, EVP_PKEY *pkey, const EVP_MD *type);
879int ASN1_item_sign_ctx(const ASN1_ITEM *it,
880 X509_ALGOR *algor1, X509_ALGOR *algor2,
881 ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx);
882#endif
883
884int X509_set_version(X509 *x,long version);
885int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial);
886ASN1_INTEGER * X509_get_serialNumber(X509 *x);
887int X509_set_issuer_name(X509 *x, X509_NAME *name);
888X509_NAME * X509_get_issuer_name(X509 *a);
889int X509_set_subject_name(X509 *x, X509_NAME *name);
890X509_NAME * X509_get_subject_name(X509 *a);
891int X509_set_notBefore(X509 *x, const ASN1_TIME *tm);
892int X509_set_notAfter(X509 *x, const ASN1_TIME *tm);
893int X509_set_pubkey(X509 *x, EVP_PKEY *pkey);
894EVP_PKEY * X509_get_pubkey(X509 *x);
895ASN1_BIT_STRING * X509_get0_pubkey_bitstr(const X509 *x);
896int X509_certificate_type(X509 *x,EVP_PKEY *pubkey /* optional */);
897
898int X509_REQ_set_version(X509_REQ *x,long version);
899int X509_REQ_set_subject_name(X509_REQ *req,X509_NAME *name);
900int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey);
901EVP_PKEY * X509_REQ_get_pubkey(X509_REQ *req);
902int X509_REQ_extension_nid(int nid);
903int * X509_REQ_get_extension_nids(void);
904void X509_REQ_set_extension_nids(int *nids);
905STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req);
906int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
907 int nid);
908int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts);
909int X509_REQ_get_attr_count(const X509_REQ *req);
910int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
911 int lastpos);
912int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj,
913 int lastpos);
914X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc);
915X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc);
916int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr);
917int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
918 const ASN1_OBJECT *obj, int type,
919 const unsigned char *bytes, int len);
920int X509_REQ_add1_attr_by_NID(X509_REQ *req,
921 int nid, int type,
922 const unsigned char *bytes, int len);
923int X509_REQ_add1_attr_by_txt(X509_REQ *req,
924 const char *attrname, int type,
925 const unsigned char *bytes, int len);
926
927int X509_CRL_set_version(X509_CRL *x, long version);
928int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name);
929int X509_CRL_set_lastUpdate(X509_CRL *x, const ASN1_TIME *tm);
930int X509_CRL_set_nextUpdate(X509_CRL *x, const ASN1_TIME *tm);
931int X509_CRL_sort(X509_CRL *crl);
932
933int X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial);
934int X509_REVOKED_set_revocationDate(X509_REVOKED *r, ASN1_TIME *tm);
935
936int X509_REQ_check_private_key(X509_REQ *x509,EVP_PKEY *pkey);
937
938int X509_check_private_key(X509 *x509,EVP_PKEY *pkey);
939
940int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b);
941unsigned long X509_issuer_and_serial_hash(X509 *a);
942
943int X509_issuer_name_cmp(const X509 *a, const X509 *b);
944unsigned long X509_issuer_name_hash(X509 *a);
945
946int X509_subject_name_cmp(const X509 *a, const X509 *b);
947unsigned long X509_subject_name_hash(X509 *x);
948
949#ifndef OPENSSL_NO_MD5
950unsigned long X509_issuer_name_hash_old(X509 *a);
951unsigned long X509_subject_name_hash_old(X509 *x);
952#endif
953
954int X509_cmp(const X509 *a, const X509 *b);
955int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b);
956unsigned long X509_NAME_hash(X509_NAME *x);
957unsigned long X509_NAME_hash_old(X509_NAME *x);
958
959int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b);
960int X509_CRL_match(const X509_CRL *a, const X509_CRL *b);
961int X509_print_ex_fp(FILE *bp,X509 *x, unsigned long nmflag, unsigned long cflag);
962int X509_print_fp(FILE *bp,X509 *x);
963int X509_CRL_print_fp(FILE *bp,X509_CRL *x);
964int X509_REQ_print_fp(FILE *bp,X509_REQ *req);
965int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags);
966
967#ifndef OPENSSL_NO_BIO
968int X509_NAME_print(BIO *bp, X509_NAME *name, int obase);
969int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags);
970int X509_print_ex(BIO *bp,X509 *x, unsigned long nmflag, unsigned long cflag);
971int X509_print(BIO *bp,X509 *x);
972int X509_ocspid_print(BIO *bp,X509 *x);
973int X509_CERT_AUX_print(BIO *bp,X509_CERT_AUX *x, int indent);
974int X509_CRL_print(BIO *bp,X509_CRL *x);
975int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag, unsigned long cflag);
976int X509_REQ_print(BIO *bp,X509_REQ *req);
977#endif
978
979int X509_NAME_entry_count(X509_NAME *name);
980int X509_NAME_get_text_by_NID(X509_NAME *name, int nid,
981 char *buf,int len);
982int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj,
983 char *buf,int len);
984
985/* NOTE: you should be passsing -1, not 0 as lastpos. The functions that use
986 * lastpos, search after that position on. */
987int X509_NAME_get_index_by_NID(X509_NAME *name,int nid,int lastpos);
988int X509_NAME_get_index_by_OBJ(X509_NAME *name,ASN1_OBJECT *obj,
989 int lastpos);
990X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc);
991X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc);
992int X509_NAME_add_entry(X509_NAME *name,X509_NAME_ENTRY *ne,
993 int loc, int set);
994int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
995 unsigned char *bytes, int len, int loc, int set);
996int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
997 unsigned char *bytes, int len, int loc, int set);
998X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
999 const char *field, int type, const unsigned char *bytes, int len);
1000X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
1001 int type,unsigned char *bytes, int len);
1002int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
1003 const unsigned char *bytes, int len, int loc, int set);
1004X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
1005 ASN1_OBJECT *obj, int type,const unsigned char *bytes,
1006 int len);
1007int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne,
1008 ASN1_OBJECT *obj);
1009int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
1010 const unsigned char *bytes, int len);
1011ASN1_OBJECT * X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne);
1012ASN1_STRING * X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne);
1013
1014int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x);
1015int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x,
1016 int nid, int lastpos);
1017int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x,
1018 ASN1_OBJECT *obj,int lastpos);
1019int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x,
1020 int crit, int lastpos);
1021X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc);
1022X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc);
1023STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
1024 X509_EXTENSION *ex, int loc);
1025
1026int X509_get_ext_count(X509 *x);
1027int X509_get_ext_by_NID(X509 *x, int nid, int lastpos);
1028int X509_get_ext_by_OBJ(X509 *x,ASN1_OBJECT *obj,int lastpos);
1029int X509_get_ext_by_critical(X509 *x, int crit, int lastpos);
1030X509_EXTENSION *X509_get_ext(X509 *x, int loc);
1031X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
1032int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
1033void * X509_get_ext_d2i(X509 *x, int nid, int *crit, int *idx);
1034int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit,
1035 unsigned long flags);
1036
1037int X509_CRL_get_ext_count(X509_CRL *x);
1038int X509_CRL_get_ext_by_NID(X509_CRL *x, int nid, int lastpos);
1039int X509_CRL_get_ext_by_OBJ(X509_CRL *x,ASN1_OBJECT *obj,int lastpos);
1040int X509_CRL_get_ext_by_critical(X509_CRL *x, int crit, int lastpos);
1041X509_EXTENSION *X509_CRL_get_ext(X509_CRL *x, int loc);
1042X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
1043int X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc);
1044void * X509_CRL_get_ext_d2i(X509_CRL *x, int nid, int *crit, int *idx);
1045int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value, int crit,
1046 unsigned long flags);
1047
1048int X509_REVOKED_get_ext_count(X509_REVOKED *x);
1049int X509_REVOKED_get_ext_by_NID(X509_REVOKED *x, int nid, int lastpos);
1050int X509_REVOKED_get_ext_by_OBJ(X509_REVOKED *x,ASN1_OBJECT *obj,int lastpos);
1051int X509_REVOKED_get_ext_by_critical(X509_REVOKED *x, int crit, int lastpos);
1052X509_EXTENSION *X509_REVOKED_get_ext(X509_REVOKED *x, int loc);
1053X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc);
1054int X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc);
1055void * X509_REVOKED_get_ext_d2i(X509_REVOKED *x, int nid, int *crit, int *idx);
1056int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid, void *value, int crit,
1057 unsigned long flags);
1058
1059X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
1060 int nid, int crit, ASN1_OCTET_STRING *data);
1061X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
1062 ASN1_OBJECT *obj,int crit,ASN1_OCTET_STRING *data);
1063int X509_EXTENSION_set_object(X509_EXTENSION *ex,ASN1_OBJECT *obj);
1064int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
1065int X509_EXTENSION_set_data(X509_EXTENSION *ex,
1066 ASN1_OCTET_STRING *data);
1067ASN1_OBJECT * X509_EXTENSION_get_object(X509_EXTENSION *ex);
1068ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ne);
1069int X509_EXTENSION_get_critical(X509_EXTENSION *ex);
1070
1071int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x);
1072int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
1073 int lastpos);
1074int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, ASN1_OBJECT *obj,
1075 int lastpos);
1076X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc);
1077X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc);
1078STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
1079 X509_ATTRIBUTE *attr);
1080STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
1081 const ASN1_OBJECT *obj, int type,
1082 const unsigned char *bytes, int len);
1083STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
1084 int nid, int type,
1085 const unsigned char *bytes, int len);
1086STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
1087 const char *attrname, int type,
1088 const unsigned char *bytes, int len);
1089void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
1090 ASN1_OBJECT *obj, int lastpos, int type);
1091X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
1092 int atrtype, const void *data, int len);
1093X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
1094 const ASN1_OBJECT *obj, int atrtype, const void *data, int len);
1095X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
1096 const char *atrname, int type, const unsigned char *bytes, int len);
1097int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj);
1098int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len);
1099void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
1100 int atrtype, void *data);
1101int X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr);
1102ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
1103ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx);
1104
1105int EVP_PKEY_get_attr_count(const EVP_PKEY *key);
1106int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid,
1107 int lastpos);
1108int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, ASN1_OBJECT *obj,
1109 int lastpos);
1110X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc);
1111X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc);
1112int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr);
1113int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
1114 const ASN1_OBJECT *obj, int type,
1115 const unsigned char *bytes, int len);
1116int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
1117 int nid, int type,
1118 const unsigned char *bytes, int len);
1119int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
1120 const char *attrname, int type,
1121 const unsigned char *bytes, int len);
1122
1123int X509_verify_cert(X509_STORE_CTX *ctx);
1124
1125/* lookup a cert from a X509 STACK */
1126X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk,X509_NAME *name,
1127 ASN1_INTEGER *serial);
1128X509 *X509_find_by_subject(STACK_OF(X509) *sk,X509_NAME *name);
1129
1130DECLARE_ASN1_FUNCTIONS(PBEPARAM)
1131DECLARE_ASN1_FUNCTIONS(PBE2PARAM)
1132DECLARE_ASN1_FUNCTIONS(PBKDF2PARAM)
1133
1134int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
1135 const unsigned char *salt, int saltlen);
1136
1137X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
1138 const unsigned char *salt, int saltlen);
1139X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
1140 unsigned char *salt, int saltlen);
1141X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
1142 unsigned char *salt, int saltlen,
1143 unsigned char *aiv, int prf_nid);
1144
1145X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
1146 int prf_nid, int keylen);
1147
1148/* PKCS#8 utilities */
1149
1150DECLARE_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
1151
1152EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8);
1153PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey);
1154PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken);
1155PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken);
1156
1157int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
1158 int version, int ptype, void *pval,
1159 unsigned char *penc, int penclen);
1160int PKCS8_pkey_get0(ASN1_OBJECT **ppkalg,
1161 const unsigned char **pk, int *ppklen,
1162 X509_ALGOR **pa,
1163 PKCS8_PRIV_KEY_INFO *p8);
1164
1165int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
1166 int ptype, void *pval,
1167 unsigned char *penc, int penclen);
1168int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
1169 const unsigned char **pk, int *ppklen,
1170 X509_ALGOR **pa,
1171 X509_PUBKEY *pub);
1172
1173int X509_check_trust(X509 *x, int id, int flags);
1174int X509_TRUST_get_count(void);
1175X509_TRUST * X509_TRUST_get0(int idx);
1176int X509_TRUST_get_by_id(int id);
1177int X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
1178 char *name, int arg1, void *arg2);
1179void X509_TRUST_cleanup(void);
1180int X509_TRUST_get_flags(X509_TRUST *xp);
1181char *X509_TRUST_get0_name(X509_TRUST *xp);
1182int X509_TRUST_get_trust(X509_TRUST *xp);
1183
1184/* BEGIN ERROR CODES */
1185/* The following lines are auto generated by the script mkerr.pl. Any changes
1186 * made after this point may be overwritten when the script is next run.
1187 */
1188void ERR_load_X509_strings(void);
1189
1190/* Error codes for the X509 functions. */
1191
1192/* Function codes. */
1193#define X509_F_ADD_CERT_DIR 100
1194#define X509_F_BY_FILE_CTRL 101
1195#define X509_F_CHECK_POLICY 145
1196#define X509_F_DIR_CTRL 102
1197#define X509_F_GET_CERT_BY_SUBJECT 103
1198#define X509_F_NETSCAPE_SPKI_B64_DECODE 129
1199#define X509_F_NETSCAPE_SPKI_B64_ENCODE 130
1200#define X509_F_X509AT_ADD1_ATTR 135
1201#define X509_F_X509V3_ADD_EXT 104
1202#define X509_F_X509_ATTRIBUTE_CREATE_BY_NID 136
1203#define X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ 137
1204#define X509_F_X509_ATTRIBUTE_CREATE_BY_TXT 140
1205#define X509_F_X509_ATTRIBUTE_GET0_DATA 139
1206#define X509_F_X509_ATTRIBUTE_SET1_DATA 138
1207#define X509_F_X509_CHECK_PRIVATE_KEY 128
1208#define X509_F_X509_CRL_PRINT_FP 147
1209#define X509_F_X509_EXTENSION_CREATE_BY_NID 108
1210#define X509_F_X509_EXTENSION_CREATE_BY_OBJ 109
1211#define X509_F_X509_GET_PUBKEY_PARAMETERS 110
1212#define X509_F_X509_LOAD_CERT_CRL_FILE 132
1213#define X509_F_X509_LOAD_CERT_FILE 111
1214#define X509_F_X509_LOAD_CRL_FILE 112
1215#define X509_F_X509_NAME_ADD_ENTRY 113
1216#define X509_F_X509_NAME_ENTRY_CREATE_BY_NID 114
1217#define X509_F_X509_NAME_ENTRY_CREATE_BY_TXT 131
1218#define X509_F_X509_NAME_ENTRY_SET_OBJECT 115
1219#define X509_F_X509_NAME_ONELINE 116
1220#define X509_F_X509_NAME_PRINT 117
1221#define X509_F_X509_PRINT_EX_FP 118
1222#define X509_F_X509_PUBKEY_GET 119
1223#define X509_F_X509_PUBKEY_SET 120
1224#define X509_F_X509_REQ_CHECK_PRIVATE_KEY 144
1225#define X509_F_X509_REQ_PRINT_EX 121
1226#define X509_F_X509_REQ_PRINT_FP 122
1227#define X509_F_X509_REQ_TO_X509 123
1228#define X509_F_X509_STORE_ADD_CERT 124
1229#define X509_F_X509_STORE_ADD_CRL 125
1230#define X509_F_X509_STORE_CTX_GET1_ISSUER 146
1231#define X509_F_X509_STORE_CTX_INIT 143
1232#define X509_F_X509_STORE_CTX_NEW 142
1233#define X509_F_X509_STORE_CTX_PURPOSE_INHERIT 134
1234#define X509_F_X509_TO_X509_REQ 126
1235#define X509_F_X509_TRUST_ADD 133
1236#define X509_F_X509_TRUST_SET 141
1237#define X509_F_X509_VERIFY_CERT 127
1238
1239/* Reason codes. */
1240#define X509_R_BAD_X509_FILETYPE 100
1241#define X509_R_BASE64_DECODE_ERROR 118
1242#define X509_R_CANT_CHECK_DH_KEY 114
1243#define X509_R_CERT_ALREADY_IN_HASH_TABLE 101
1244#define X509_R_ERR_ASN1_LIB 102
1245#define X509_R_INVALID_DIRECTORY 113
1246#define X509_R_INVALID_FIELD_NAME 119
1247#define X509_R_INVALID_TRUST 123
1248#define X509_R_KEY_TYPE_MISMATCH 115
1249#define X509_R_KEY_VALUES_MISMATCH 116
1250#define X509_R_LOADING_CERT_DIR 103
1251#define X509_R_LOADING_DEFAULTS 104
1252#define X509_R_METHOD_NOT_SUPPORTED 124
1253#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 105
1254#define X509_R_PUBLIC_KEY_DECODE_ERROR 125
1255#define X509_R_PUBLIC_KEY_ENCODE_ERROR 126
1256#define X509_R_SHOULD_RETRY 106
1257#define X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN 107
1258#define X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY 108
1259#define X509_R_UNKNOWN_KEY_TYPE 117
1260#define X509_R_UNKNOWN_NID 109
1261#define X509_R_UNKNOWN_PURPOSE_ID 121
1262#define X509_R_UNKNOWN_TRUST_ID 120
1263#define X509_R_UNSUPPORTED_ALGORITHM 111
1264#define X509_R_WRONG_LOOKUP_TYPE 112
1265#define X509_R_WRONG_TYPE 122
1266
1267#ifdef __cplusplus
1268}
1269#endif
1270#endif
diff --git a/src/lib/libcrypto/x509/x509_att.c b/src/lib/libcrypto/x509/x509_att.c
deleted file mode 100644
index ab11e79b0a..0000000000
--- a/src/lib/libcrypto/x509/x509_att.c
+++ /dev/null
@@ -1,403 +0,0 @@
1/* $OpenBSD: x509_att.c,v 1.14 2016/03/21 04:05:33 mmcc Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/err.h>
63#include <openssl/evp.h>
64#include <openssl/objects.h>
65#include <openssl/stack.h>
66#include <openssl/x509.h>
67#include <openssl/x509v3.h>
68
69int
70X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
71{
72 return sk_X509_ATTRIBUTE_num(x);
73}
74
75int
76X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid, int lastpos)
77{
78 ASN1_OBJECT *obj;
79
80 obj = OBJ_nid2obj(nid);
81 if (obj == NULL)
82 return (-2);
83 return (X509at_get_attr_by_OBJ(x, obj, lastpos));
84}
85
86int
87X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, ASN1_OBJECT *obj,
88 int lastpos)
89{
90 int n;
91 X509_ATTRIBUTE *ex;
92
93 if (sk == NULL)
94 return (-1);
95 lastpos++;
96 if (lastpos < 0)
97 lastpos = 0;
98 n = sk_X509_ATTRIBUTE_num(sk);
99 for (; lastpos < n; lastpos++) {
100 ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
101 if (OBJ_cmp(ex->object, obj) == 0)
102 return (lastpos);
103 }
104 return (-1);
105}
106
107X509_ATTRIBUTE *
108X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
109{
110 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
111 return NULL;
112 else
113 return sk_X509_ATTRIBUTE_value(x, loc);
114}
115
116X509_ATTRIBUTE *
117X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
118{
119 X509_ATTRIBUTE *ret;
120
121 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
122 return (NULL);
123 ret = sk_X509_ATTRIBUTE_delete(x, loc);
124 return (ret);
125}
126
127STACK_OF(X509_ATTRIBUTE) *
128X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x, X509_ATTRIBUTE *attr)
129{
130 X509_ATTRIBUTE *new_attr = NULL;
131 STACK_OF(X509_ATTRIBUTE) *sk = NULL;
132
133 if (x == NULL) {
134 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
135 return (NULL);
136 }
137
138 if (*x == NULL) {
139 if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
140 goto err;
141 } else
142 sk = *x;
143
144 if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
145 goto err2;
146 if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
147 goto err;
148 if (*x == NULL)
149 *x = sk;
150 return (sk);
151
152err:
153 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
154err2:
155 if (new_attr != NULL)
156 X509_ATTRIBUTE_free(new_attr);
157 if (sk != NULL && sk != *x)
158 sk_X509_ATTRIBUTE_free(sk);
159 return (NULL);
160}
161
162STACK_OF(X509_ATTRIBUTE) *
163X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x, const ASN1_OBJECT *obj,
164 int type, const unsigned char *bytes, int len)
165{
166 X509_ATTRIBUTE *attr;
167 STACK_OF(X509_ATTRIBUTE) *ret;
168
169 attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
170 if (!attr)
171 return 0;
172 ret = X509at_add1_attr(x, attr);
173 X509_ATTRIBUTE_free(attr);
174 return ret;
175}
176
177STACK_OF(X509_ATTRIBUTE) *
178X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x, int nid, int type,
179 const unsigned char *bytes, int len)
180{
181 X509_ATTRIBUTE *attr;
182 STACK_OF(X509_ATTRIBUTE) *ret;
183
184 attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
185 if (!attr)
186 return 0;
187 ret = X509at_add1_attr(x, attr);
188 X509_ATTRIBUTE_free(attr);
189 return ret;
190}
191
192STACK_OF(X509_ATTRIBUTE) *
193X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x, const char *attrname,
194 int type, const unsigned char *bytes, int len)
195{
196 X509_ATTRIBUTE *attr;
197 STACK_OF(X509_ATTRIBUTE) *ret;
198
199 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
200 if (!attr)
201 return 0;
202 ret = X509at_add1_attr(x, attr);
203 X509_ATTRIBUTE_free(attr);
204 return ret;
205}
206
207void *
208X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x, ASN1_OBJECT *obj,
209 int lastpos, int type)
210{
211 int i;
212 X509_ATTRIBUTE *at;
213
214 i = X509at_get_attr_by_OBJ(x, obj, lastpos);
215 if (i == -1)
216 return NULL;
217 if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
218 return NULL;
219 at = X509at_get_attr(x, i);
220 if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
221 return NULL;
222 return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
223}
224
225X509_ATTRIBUTE *
226X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid, int atrtype,
227 const void *data, int len)
228{
229 ASN1_OBJECT *obj;
230 X509_ATTRIBUTE *ret;
231
232 obj = OBJ_nid2obj(nid);
233 if (obj == NULL) {
234 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID,
235 X509_R_UNKNOWN_NID);
236 return (NULL);
237 }
238 ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
239 if (ret == NULL)
240 ASN1_OBJECT_free(obj);
241 return (ret);
242}
243
244X509_ATTRIBUTE *
245X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj,
246 int atrtype, const void *data, int len)
247{
248 X509_ATTRIBUTE *ret;
249
250 if ((attr == NULL) || (*attr == NULL)) {
251 if ((ret = X509_ATTRIBUTE_new()) == NULL) {
252 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,
253 ERR_R_MALLOC_FAILURE);
254 return (NULL);
255 }
256 } else
257 ret= *attr;
258
259 if (!X509_ATTRIBUTE_set1_object(ret, obj))
260 goto err;
261 if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
262 goto err;
263
264 if ((attr != NULL) && (*attr == NULL))
265 *attr = ret;
266 return (ret);
267
268err:
269 if ((attr == NULL) || (ret != *attr))
270 X509_ATTRIBUTE_free(ret);
271 return (NULL);
272}
273
274X509_ATTRIBUTE *
275X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr, const char *atrname,
276 int type, const unsigned char *bytes, int len)
277{
278 ASN1_OBJECT *obj;
279 X509_ATTRIBUTE *nattr;
280
281 obj = OBJ_txt2obj(atrname, 0);
282 if (obj == NULL) {
283 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
284 X509_R_INVALID_FIELD_NAME);
285 ERR_asprintf_error_data("name=%s", atrname);
286 return (NULL);
287 }
288 nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
289 ASN1_OBJECT_free(obj);
290 return nattr;
291}
292
293int
294X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
295{
296 if ((attr == NULL) || (obj == NULL))
297 return (0);
298 ASN1_OBJECT_free(attr->object);
299 attr->object = OBJ_dup(obj);
300 return attr->object != NULL;
301}
302
303int
304X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data,
305 int len)
306{
307 ASN1_TYPE *ttmp = NULL;
308 ASN1_STRING *stmp = NULL;
309 int atype = 0;
310
311 if (!attr)
312 return 0;
313 if (attrtype & MBSTRING_FLAG) {
314 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
315 OBJ_obj2nid(attr->object));
316 if (!stmp) {
317 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA,
318 ERR_R_ASN1_LIB);
319 return 0;
320 }
321 atype = stmp->type;
322 } else if (len != -1){
323 if (!(stmp = ASN1_STRING_type_new(attrtype)))
324 goto err;
325 if (!ASN1_STRING_set(stmp, data, len))
326 goto err;
327 atype = attrtype;
328 }
329 if (!(attr->value.set = sk_ASN1_TYPE_new_null()))
330 goto err;
331 attr->single = 0;
332 /* This is a bit naughty because the attribute should really have
333 * at least one value but some types use and zero length SET and
334 * require this.
335 */
336 if (attrtype == 0) {
337 ASN1_STRING_free(stmp);
338 return 1;
339 }
340
341 if (!(ttmp = ASN1_TYPE_new()))
342 goto err;
343 if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
344 if (!ASN1_TYPE_set1(ttmp, attrtype, data))
345 goto err;
346 } else
347 ASN1_TYPE_set(ttmp, atype, stmp);
348 if (!sk_ASN1_TYPE_push(attr->value.set, ttmp))
349 goto err;
350 return 1;
351
352err:
353 ASN1_TYPE_free(ttmp);
354 ASN1_STRING_free(stmp);
355 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
356 return 0;
357}
358
359int
360X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr)
361{
362 if (!attr->single)
363 return sk_ASN1_TYPE_num(attr->value.set);
364 if (attr->value.single)
365 return 1;
366 return 0;
367}
368
369ASN1_OBJECT *
370X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
371{
372 if (attr == NULL)
373 return (NULL);
374 return (attr->object);
375}
376
377void *
378X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int atrtype, void *data)
379{
380 ASN1_TYPE *ttmp;
381
382 ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
383 if (!ttmp)
384 return NULL;
385 if (atrtype != ASN1_TYPE_get(ttmp)){
386 X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
387 return NULL;
388 }
389 return ttmp->value.ptr;
390}
391
392ASN1_TYPE *
393X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
394{
395 if (attr == NULL)
396 return (NULL);
397 if (idx >= X509_ATTRIBUTE_count(attr))
398 return NULL;
399 if (!attr->single)
400 return sk_ASN1_TYPE_value(attr->value.set, idx);
401 else
402 return attr->value.single;
403}
diff --git a/src/lib/libcrypto/x509/x509_cmp.c b/src/lib/libcrypto/x509/x509_cmp.c
deleted file mode 100644
index 407e1e07ad..0000000000
--- a/src/lib/libcrypto/x509/x509_cmp.c
+++ /dev/null
@@ -1,369 +0,0 @@
1/* $OpenBSD: x509_cmp.c,v 1.26 2015/07/29 14:58:34 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <ctype.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/opensslconf.h>
64
65#include <openssl/asn1.h>
66#include <openssl/err.h>
67#include <openssl/objects.h>
68#include <openssl/x509.h>
69#include <openssl/x509v3.h>
70
71int
72X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
73{
74 int i;
75 X509_CINF *ai, *bi;
76
77 ai = a->cert_info;
78 bi = b->cert_info;
79 i = ASN1_STRING_cmp(ai->serialNumber, bi->serialNumber);
80 if (i)
81 return (i);
82 return (X509_NAME_cmp(ai->issuer, bi->issuer));
83}
84
85#ifndef OPENSSL_NO_MD5
86unsigned long
87X509_issuer_and_serial_hash(X509 *a)
88{
89 unsigned long ret = 0;
90 EVP_MD_CTX ctx;
91 unsigned char md[16];
92 char *f;
93
94 EVP_MD_CTX_init(&ctx);
95 f = X509_NAME_oneline(a->cert_info->issuer, NULL, 0);
96 if (f == NULL)
97 goto err;
98 if (!EVP_DigestInit_ex(&ctx, EVP_md5(), NULL))
99 goto err;
100 if (!EVP_DigestUpdate(&ctx, (unsigned char *)f, strlen(f)))
101 goto err;
102 free(f);
103 f = NULL;
104 if (!EVP_DigestUpdate(&ctx,
105 (unsigned char *)a->cert_info->serialNumber->data,
106 (unsigned long)a->cert_info->serialNumber->length))
107 goto err;
108 if (!EVP_DigestFinal_ex(&ctx, &(md[0]), NULL))
109 goto err;
110 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
111 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
112 0xffffffffL;
113
114err:
115 EVP_MD_CTX_cleanup(&ctx);
116 free(f);
117 return (ret);
118}
119#endif
120
121int
122X509_issuer_name_cmp(const X509 *a, const X509 *b)
123{
124 return (X509_NAME_cmp(a->cert_info->issuer, b->cert_info->issuer));
125}
126
127int
128X509_subject_name_cmp(const X509 *a, const X509 *b)
129{
130 return (X509_NAME_cmp(a->cert_info->subject, b->cert_info->subject));
131}
132
133int
134X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
135{
136 return (X509_NAME_cmp(a->crl->issuer, b->crl->issuer));
137}
138
139#ifndef OPENSSL_NO_SHA
140int
141X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
142{
143 return memcmp(a->sha1_hash, b->sha1_hash, 20);
144}
145#endif
146
147X509_NAME *
148X509_get_issuer_name(X509 *a)
149{
150 return (a->cert_info->issuer);
151}
152
153unsigned long
154X509_issuer_name_hash(X509 *x)
155{
156 return (X509_NAME_hash(x->cert_info->issuer));
157}
158
159#ifndef OPENSSL_NO_MD5
160unsigned long
161X509_issuer_name_hash_old(X509 *x)
162{
163 return (X509_NAME_hash_old(x->cert_info->issuer));
164}
165#endif
166
167X509_NAME *
168X509_get_subject_name(X509 *a)
169{
170 return (a->cert_info->subject);
171}
172
173ASN1_INTEGER *
174X509_get_serialNumber(X509 *a)
175{
176 return (a->cert_info->serialNumber);
177}
178
179unsigned long
180X509_subject_name_hash(X509 *x)
181{
182 return (X509_NAME_hash(x->cert_info->subject));
183}
184
185#ifndef OPENSSL_NO_MD5
186unsigned long
187X509_subject_name_hash_old(X509 *x)
188{
189 return (X509_NAME_hash_old(x->cert_info->subject));
190}
191#endif
192
193#ifndef OPENSSL_NO_SHA
194/* Compare two certificates: they must be identical for
195 * this to work. NB: Although "cmp" operations are generally
196 * prototyped to take "const" arguments (eg. for use in
197 * STACKs), the way X509 handling is - these operations may
198 * involve ensuring the hashes are up-to-date and ensuring
199 * certain cert information is cached. So this is the point
200 * where the "depth-first" constification tree has to halt
201 * with an evil cast.
202 */
203int
204X509_cmp(const X509 *a, const X509 *b)
205{
206 /* ensure hash is valid */
207 X509_check_purpose((X509 *)a, -1, 0);
208 X509_check_purpose((X509 *)b, -1, 0);
209
210 return memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
211}
212#endif
213
214int
215X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
216{
217 int ret;
218
219 /* Ensure canonical encoding is present and up to date */
220 if (!a->canon_enc || a->modified) {
221 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
222 if (ret < 0)
223 return -2;
224 }
225 if (!b->canon_enc || b->modified) {
226 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
227 if (ret < 0)
228 return -2;
229 }
230 ret = a->canon_enclen - b->canon_enclen;
231 if (ret)
232 return ret;
233 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
234}
235
236unsigned long
237X509_NAME_hash(X509_NAME *x)
238{
239 unsigned long ret = 0;
240 unsigned char md[SHA_DIGEST_LENGTH];
241
242 /* Make sure X509_NAME structure contains valid cached encoding */
243 i2d_X509_NAME(x, NULL);
244 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
245 NULL))
246 return 0;
247
248 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
249 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
250 0xffffffffL;
251 return (ret);
252}
253
254
255#ifndef OPENSSL_NO_MD5
256/* I now DER encode the name and hash it. Since I cache the DER encoding,
257 * this is reasonably efficient. */
258
259unsigned long
260X509_NAME_hash_old(X509_NAME *x)
261{
262 EVP_MD_CTX md_ctx;
263 unsigned long ret = 0;
264 unsigned char md[16];
265
266 /* Make sure X509_NAME structure contains valid cached encoding */
267 i2d_X509_NAME(x, NULL);
268 EVP_MD_CTX_init(&md_ctx);
269 if (EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL) &&
270 EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length) &&
271 EVP_DigestFinal_ex(&md_ctx, md, NULL))
272 ret = (((unsigned long)md[0]) |
273 ((unsigned long)md[1] << 8L) |
274 ((unsigned long)md[2] << 16L) |
275 ((unsigned long)md[3] << 24L)) &
276 0xffffffffL;
277 EVP_MD_CTX_cleanup(&md_ctx);
278
279 return (ret);
280}
281#endif
282
283/* Search a stack of X509 for a match */
284X509 *
285X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
286 ASN1_INTEGER *serial)
287{
288 int i;
289 X509_CINF cinf;
290 X509 x, *x509 = NULL;
291
292 if (!sk)
293 return NULL;
294
295 x.cert_info = &cinf;
296 cinf.serialNumber = serial;
297 cinf.issuer = name;
298
299 for (i = 0; i < sk_X509_num(sk); i++) {
300 x509 = sk_X509_value(sk, i);
301 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
302 return (x509);
303 }
304 return (NULL);
305}
306
307X509 *
308X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
309{
310 X509 *x509;
311 int i;
312
313 for (i = 0; i < sk_X509_num(sk); i++) {
314 x509 = sk_X509_value(sk, i);
315 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
316 return (x509);
317 }
318 return (NULL);
319}
320
321EVP_PKEY *
322X509_get_pubkey(X509 *x)
323{
324 if ((x == NULL) || (x->cert_info == NULL))
325 return (NULL);
326 return (X509_PUBKEY_get(x->cert_info->key));
327}
328
329ASN1_BIT_STRING *
330X509_get0_pubkey_bitstr(const X509 *x)
331{
332 if (!x)
333 return NULL;
334 return x->cert_info->key->public_key;
335}
336
337int
338X509_check_private_key(X509 *x, EVP_PKEY *k)
339{
340 EVP_PKEY *xk;
341 int ret;
342
343 xk = X509_get_pubkey(x);
344
345 if (xk)
346 ret = EVP_PKEY_cmp(xk, k);
347 else
348 ret = -2;
349
350 switch (ret) {
351 case 1:
352 break;
353 case 0:
354 X509err(X509_F_X509_CHECK_PRIVATE_KEY,
355 X509_R_KEY_VALUES_MISMATCH);
356 break;
357 case -1:
358 X509err(X509_F_X509_CHECK_PRIVATE_KEY,
359 X509_R_KEY_TYPE_MISMATCH);
360 break;
361 case -2:
362 X509err(X509_F_X509_CHECK_PRIVATE_KEY,
363 X509_R_UNKNOWN_KEY_TYPE);
364 }
365 EVP_PKEY_free(xk);
366 if (ret > 0)
367 return 1;
368 return 0;
369}
diff --git a/src/lib/libcrypto/x509/x509_d2.c b/src/lib/libcrypto/x509/x509_d2.c
deleted file mode 100644
index 5b0f80adda..0000000000
--- a/src/lib/libcrypto/x509/x509_d2.c
+++ /dev/null
@@ -1,128 +0,0 @@
1/* $OpenBSD: x509_d2.c,v 1.10 2015/01/22 09:06:39 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <sys/uio.h>
61
62#include <openssl/crypto.h>
63#include <openssl/err.h>
64#include <openssl/x509.h>
65
66int
67X509_STORE_set_default_paths(X509_STORE *ctx)
68{
69 X509_LOOKUP *lookup;
70
71 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_file());
72 if (lookup == NULL)
73 return (0);
74 X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
75
76 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_hash_dir());
77 if (lookup == NULL)
78 return (0);
79 X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
80
81 /* clear any errors */
82 ERR_clear_error();
83
84 return (1);
85}
86
87int
88X509_STORE_load_locations(X509_STORE *ctx, const char *file, const char *path)
89{
90 X509_LOOKUP *lookup;
91
92 if (file != NULL) {
93 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_file());
94 if (lookup == NULL)
95 return (0);
96 if (X509_LOOKUP_load_file(lookup, file, X509_FILETYPE_PEM) != 1)
97 return (0);
98 }
99 if (path != NULL) {
100 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_hash_dir());
101 if (lookup == NULL)
102 return (0);
103 if (X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1)
104 return (0);
105 }
106 if ((path == NULL) && (file == NULL))
107 return (0);
108 return (1);
109}
110
111int
112X509_STORE_load_mem(X509_STORE *ctx, void *buf, int len)
113{
114 X509_LOOKUP *lookup;
115 struct iovec iov;
116
117 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_mem());
118 if (lookup == NULL)
119 return (0);
120
121 iov.iov_base = buf;
122 iov.iov_len = len;
123
124 if (X509_LOOKUP_add_mem(lookup, &iov, X509_FILETYPE_PEM) != 1)
125 return (0);
126
127 return (1);
128}
diff --git a/src/lib/libcrypto/x509/x509_def.c b/src/lib/libcrypto/x509/x509_def.c
deleted file mode 100644
index 5e570eb9a2..0000000000
--- a/src/lib/libcrypto/x509/x509_def.c
+++ /dev/null
@@ -1,98 +0,0 @@
1/* $OpenBSD: x509_def.c,v 1.5 2014/06/12 15:49:31 deraadt Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/crypto.h>
62#include <openssl/x509.h>
63
64const char *
65X509_get_default_private_dir(void)
66{
67 return (X509_PRIVATE_DIR);
68}
69
70const char *
71X509_get_default_cert_area(void)
72{
73 return (X509_CERT_AREA);
74}
75
76const char *
77X509_get_default_cert_dir(void)
78{
79 return (X509_CERT_DIR);
80}
81
82const char *
83X509_get_default_cert_file(void)
84{
85 return (X509_CERT_FILE);
86}
87
88const char *
89X509_get_default_cert_dir_env(void)
90{
91 return (X509_CERT_DIR_EVP);
92}
93
94const char *
95X509_get_default_cert_file_env(void)
96{
97 return (X509_CERT_FILE_EVP);
98}
diff --git a/src/lib/libcrypto/x509/x509_err.c b/src/lib/libcrypto/x509/x509_err.c
deleted file mode 100644
index 6a15ac9fd0..0000000000
--- a/src/lib/libcrypto/x509/x509_err.c
+++ /dev/null
@@ -1,164 +0,0 @@
1/* $OpenBSD: x509_err.c,v 1.12 2014/07/10 22:45:58 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62
63#include <openssl/opensslconf.h>
64
65#include <openssl/err.h>
66#include <openssl/x509.h>
67
68/* BEGIN ERROR CODES */
69#ifndef OPENSSL_NO_ERR
70
71#define ERR_FUNC(func) ERR_PACK(ERR_LIB_X509,func,0)
72#define ERR_REASON(reason) ERR_PACK(ERR_LIB_X509,0,reason)
73
74static ERR_STRING_DATA X509_str_functs[] = {
75 {ERR_FUNC(X509_F_ADD_CERT_DIR), "ADD_CERT_DIR"},
76 {ERR_FUNC(X509_F_BY_FILE_CTRL), "BY_FILE_CTRL"},
77 {ERR_FUNC(X509_F_CHECK_POLICY), "CHECK_POLICY"},
78 {ERR_FUNC(X509_F_DIR_CTRL), "DIR_CTRL"},
79 {ERR_FUNC(X509_F_GET_CERT_BY_SUBJECT), "GET_CERT_BY_SUBJECT"},
80 {ERR_FUNC(X509_F_NETSCAPE_SPKI_B64_DECODE), "NETSCAPE_SPKI_b64_decode"},
81 {ERR_FUNC(X509_F_NETSCAPE_SPKI_B64_ENCODE), "NETSCAPE_SPKI_b64_encode"},
82 {ERR_FUNC(X509_F_X509AT_ADD1_ATTR), "X509at_add1_attr"},
83 {ERR_FUNC(X509_F_X509V3_ADD_EXT), "X509v3_add_ext"},
84 {ERR_FUNC(X509_F_X509_ATTRIBUTE_CREATE_BY_NID), "X509_ATTRIBUTE_create_by_NID"},
85 {ERR_FUNC(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ), "X509_ATTRIBUTE_create_by_OBJ"},
86 {ERR_FUNC(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT), "X509_ATTRIBUTE_create_by_txt"},
87 {ERR_FUNC(X509_F_X509_ATTRIBUTE_GET0_DATA), "X509_ATTRIBUTE_get0_data"},
88 {ERR_FUNC(X509_F_X509_ATTRIBUTE_SET1_DATA), "X509_ATTRIBUTE_set1_data"},
89 {ERR_FUNC(X509_F_X509_CHECK_PRIVATE_KEY), "X509_check_private_key"},
90 {ERR_FUNC(X509_F_X509_CRL_PRINT_FP), "X509_CRL_print_fp"},
91 {ERR_FUNC(X509_F_X509_EXTENSION_CREATE_BY_NID), "X509_EXTENSION_create_by_NID"},
92 {ERR_FUNC(X509_F_X509_EXTENSION_CREATE_BY_OBJ), "X509_EXTENSION_create_by_OBJ"},
93 {ERR_FUNC(X509_F_X509_GET_PUBKEY_PARAMETERS), "X509_get_pubkey_parameters"},
94 {ERR_FUNC(X509_F_X509_LOAD_CERT_CRL_FILE), "X509_load_cert_crl_file"},
95 {ERR_FUNC(X509_F_X509_LOAD_CERT_FILE), "X509_load_cert_file"},
96 {ERR_FUNC(X509_F_X509_LOAD_CRL_FILE), "X509_load_crl_file"},
97 {ERR_FUNC(X509_F_X509_NAME_ADD_ENTRY), "X509_NAME_add_entry"},
98 {ERR_FUNC(X509_F_X509_NAME_ENTRY_CREATE_BY_NID), "X509_NAME_ENTRY_create_by_NID"},
99 {ERR_FUNC(X509_F_X509_NAME_ENTRY_CREATE_BY_TXT), "X509_NAME_ENTRY_create_by_txt"},
100 {ERR_FUNC(X509_F_X509_NAME_ENTRY_SET_OBJECT), "X509_NAME_ENTRY_set_object"},
101 {ERR_FUNC(X509_F_X509_NAME_ONELINE), "X509_NAME_oneline"},
102 {ERR_FUNC(X509_F_X509_NAME_PRINT), "X509_NAME_print"},
103 {ERR_FUNC(X509_F_X509_PRINT_EX_FP), "X509_print_ex_fp"},
104 {ERR_FUNC(X509_F_X509_PUBKEY_GET), "X509_PUBKEY_get"},
105 {ERR_FUNC(X509_F_X509_PUBKEY_SET), "X509_PUBKEY_set"},
106 {ERR_FUNC(X509_F_X509_REQ_CHECK_PRIVATE_KEY), "X509_REQ_check_private_key"},
107 {ERR_FUNC(X509_F_X509_REQ_PRINT_EX), "X509_REQ_print_ex"},
108 {ERR_FUNC(X509_F_X509_REQ_PRINT_FP), "X509_REQ_print_fp"},
109 {ERR_FUNC(X509_F_X509_REQ_TO_X509), "X509_REQ_to_X509"},
110 {ERR_FUNC(X509_F_X509_STORE_ADD_CERT), "X509_STORE_add_cert"},
111 {ERR_FUNC(X509_F_X509_STORE_ADD_CRL), "X509_STORE_add_crl"},
112 {ERR_FUNC(X509_F_X509_STORE_CTX_GET1_ISSUER), "X509_STORE_CTX_get1_issuer"},
113 {ERR_FUNC(X509_F_X509_STORE_CTX_INIT), "X509_STORE_CTX_init"},
114 {ERR_FUNC(X509_F_X509_STORE_CTX_NEW), "X509_STORE_CTX_new"},
115 {ERR_FUNC(X509_F_X509_STORE_CTX_PURPOSE_INHERIT), "X509_STORE_CTX_purpose_inherit"},
116 {ERR_FUNC(X509_F_X509_TO_X509_REQ), "X509_to_X509_REQ"},
117 {ERR_FUNC(X509_F_X509_TRUST_ADD), "X509_TRUST_add"},
118 {ERR_FUNC(X509_F_X509_TRUST_SET), "X509_TRUST_set"},
119 {ERR_FUNC(X509_F_X509_VERIFY_CERT), "X509_verify_cert"},
120 {0, NULL}
121};
122
123static ERR_STRING_DATA X509_str_reasons[] = {
124 {ERR_REASON(X509_R_BAD_X509_FILETYPE) , "bad x509 filetype"},
125 {ERR_REASON(X509_R_BASE64_DECODE_ERROR) , "base64 decode error"},
126 {ERR_REASON(X509_R_CANT_CHECK_DH_KEY) , "cant check dh key"},
127 {ERR_REASON(X509_R_CERT_ALREADY_IN_HASH_TABLE), "cert already in hash table"},
128 {ERR_REASON(X509_R_ERR_ASN1_LIB) , "err asn1 lib"},
129 {ERR_REASON(X509_R_INVALID_DIRECTORY) , "invalid directory"},
130 {ERR_REASON(X509_R_INVALID_FIELD_NAME) , "invalid field name"},
131 {ERR_REASON(X509_R_INVALID_TRUST) , "invalid trust"},
132 {ERR_REASON(X509_R_KEY_TYPE_MISMATCH) , "key type mismatch"},
133 {ERR_REASON(X509_R_KEY_VALUES_MISMATCH) , "key values mismatch"},
134 {ERR_REASON(X509_R_LOADING_CERT_DIR) , "loading cert dir"},
135 {ERR_REASON(X509_R_LOADING_DEFAULTS) , "loading defaults"},
136 {ERR_REASON(X509_R_METHOD_NOT_SUPPORTED) , "method not supported"},
137 {ERR_REASON(X509_R_NO_CERT_SET_FOR_US_TO_VERIFY), "no cert set for us to verify"},
138 {ERR_REASON(X509_R_PUBLIC_KEY_DECODE_ERROR), "public key decode error"},
139 {ERR_REASON(X509_R_PUBLIC_KEY_ENCODE_ERROR), "public key encode error"},
140 {ERR_REASON(X509_R_SHOULD_RETRY) , "should retry"},
141 {ERR_REASON(X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN), "unable to find parameters in chain"},
142 {ERR_REASON(X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY), "unable to get certs public key"},
143 {ERR_REASON(X509_R_UNKNOWN_KEY_TYPE) , "unknown key type"},
144 {ERR_REASON(X509_R_UNKNOWN_NID) , "unknown nid"},
145 {ERR_REASON(X509_R_UNKNOWN_PURPOSE_ID) , "unknown purpose id"},
146 {ERR_REASON(X509_R_UNKNOWN_TRUST_ID) , "unknown trust id"},
147 {ERR_REASON(X509_R_UNSUPPORTED_ALGORITHM), "unsupported algorithm"},
148 {ERR_REASON(X509_R_WRONG_LOOKUP_TYPE) , "wrong lookup type"},
149 {ERR_REASON(X509_R_WRONG_TYPE) , "wrong type"},
150 {0, NULL}
151};
152
153#endif
154
155void
156ERR_load_X509_strings(void)
157{
158#ifndef OPENSSL_NO_ERR
159 if (ERR_func_error_string(X509_str_functs[0].error) == NULL) {
160 ERR_load_strings(0, X509_str_functs);
161 ERR_load_strings(0, X509_str_reasons);
162 }
163#endif
164}
diff --git a/src/lib/libcrypto/x509/x509_ext.c b/src/lib/libcrypto/x509/x509_ext.c
deleted file mode 100644
index e90befaba1..0000000000
--- a/src/lib/libcrypto/x509/x509_ext.c
+++ /dev/null
@@ -1,232 +0,0 @@
1/* $OpenBSD: x509_ext.c,v 1.9 2015/02/10 08:33:10 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/stack.h>
65#include <openssl/x509.h>
66#include <openssl/x509v3.h>
67
68int
69X509_CRL_get_ext_count(X509_CRL *x)
70{
71 return (X509v3_get_ext_count(x->crl->extensions));
72}
73
74int
75X509_CRL_get_ext_by_NID(X509_CRL *x, int nid, int lastpos)
76{
77 return (X509v3_get_ext_by_NID(x->crl->extensions, nid, lastpos));
78}
79
80int
81X509_CRL_get_ext_by_OBJ(X509_CRL *x, ASN1_OBJECT *obj, int lastpos)
82{
83 return (X509v3_get_ext_by_OBJ(x->crl->extensions, obj, lastpos));
84}
85
86int
87X509_CRL_get_ext_by_critical(X509_CRL *x, int crit, int lastpos)
88{
89 return (X509v3_get_ext_by_critical(x->crl->extensions, crit, lastpos));
90}
91
92X509_EXTENSION *
93X509_CRL_get_ext(X509_CRL *x, int loc)
94{
95 return (X509v3_get_ext(x->crl->extensions, loc));
96}
97
98X509_EXTENSION *
99X509_CRL_delete_ext(X509_CRL *x, int loc)
100{
101 return (X509v3_delete_ext(x->crl->extensions, loc));
102}
103
104void *
105X509_CRL_get_ext_d2i(X509_CRL *x, int nid, int *crit, int *idx)
106{
107 return X509V3_get_d2i(x->crl->extensions, nid, crit, idx);
108}
109
110int
111X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value, int crit,
112 unsigned long flags)
113{
114 return X509V3_add1_i2d(&x->crl->extensions, nid, value, crit, flags);
115}
116
117int
118X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc)
119{
120 return (X509v3_add_ext(&(x->crl->extensions), ex, loc) != NULL);
121}
122
123int
124X509_get_ext_count(X509 *x)
125{
126 return (X509v3_get_ext_count(x->cert_info->extensions));
127}
128
129int
130X509_get_ext_by_NID(X509 *x, int nid, int lastpos)
131{
132 return (X509v3_get_ext_by_NID(x->cert_info->extensions, nid, lastpos));
133}
134
135int
136X509_get_ext_by_OBJ(X509 *x, ASN1_OBJECT *obj, int lastpos)
137{
138 return (X509v3_get_ext_by_OBJ(x->cert_info->extensions, obj, lastpos));
139}
140
141int
142X509_get_ext_by_critical(X509 *x, int crit, int lastpos)
143{
144 return (X509v3_get_ext_by_critical(x->cert_info->extensions, crit,
145 lastpos));
146}
147
148X509_EXTENSION *
149X509_get_ext(X509 *x, int loc)
150{
151 return (X509v3_get_ext(x->cert_info->extensions, loc));
152}
153
154X509_EXTENSION *
155X509_delete_ext(X509 *x, int loc)
156{
157 return (X509v3_delete_ext(x->cert_info->extensions, loc));
158}
159
160int
161X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc)
162{
163 return (X509v3_add_ext(&(x->cert_info->extensions), ex, loc) != NULL);
164}
165
166void *
167X509_get_ext_d2i(X509 *x, int nid, int *crit, int *idx)
168{
169 return X509V3_get_d2i(x->cert_info->extensions, nid, crit, idx);
170}
171
172int
173X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit, unsigned long flags)
174{
175 return X509V3_add1_i2d(&x->cert_info->extensions, nid, value, crit,
176 flags);
177}
178
179int
180X509_REVOKED_get_ext_count(X509_REVOKED *x)
181{
182 return (X509v3_get_ext_count(x->extensions));
183}
184
185int
186X509_REVOKED_get_ext_by_NID(X509_REVOKED *x, int nid, int lastpos)
187{
188 return (X509v3_get_ext_by_NID(x->extensions, nid, lastpos));
189}
190
191int
192X509_REVOKED_get_ext_by_OBJ(X509_REVOKED *x, ASN1_OBJECT *obj, int lastpos)
193{
194 return (X509v3_get_ext_by_OBJ(x->extensions, obj, lastpos));
195}
196
197int
198X509_REVOKED_get_ext_by_critical(X509_REVOKED *x, int crit, int lastpos)
199{
200 return (X509v3_get_ext_by_critical(x->extensions, crit, lastpos));
201}
202
203X509_EXTENSION *
204X509_REVOKED_get_ext(X509_REVOKED *x, int loc)
205{
206 return (X509v3_get_ext(x->extensions, loc));
207}
208
209X509_EXTENSION *
210X509_REVOKED_delete_ext(X509_REVOKED *x, int loc)
211{
212 return (X509v3_delete_ext(x->extensions, loc));
213}
214
215int
216X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc)
217{
218 return (X509v3_add_ext(&(x->extensions), ex, loc) != NULL);
219}
220
221void *
222X509_REVOKED_get_ext_d2i(X509_REVOKED *x, int nid, int *crit, int *idx)
223{
224 return X509V3_get_d2i(x->extensions, nid, crit, idx);
225}
226
227int
228X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid, void *value, int crit,
229 unsigned long flags)
230{
231 return X509V3_add1_i2d(&x->extensions, nid, value, crit, flags);
232}
diff --git a/src/lib/libcrypto/x509/x509_lcl.h b/src/lib/libcrypto/x509/x509_lcl.h
deleted file mode 100644
index 9ffdd01e61..0000000000
--- a/src/lib/libcrypto/x509/x509_lcl.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* x509_lcl.h */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2013.
4 */
5/* ====================================================================
6 * Copyright (c) 2013 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet);
60int asn1_time_parse(const char *, size_t, struct tm *, int);
61int asn1_tm_cmp(struct tm *tm1, struct tm *tm2);
diff --git a/src/lib/libcrypto/x509/x509_lu.c b/src/lib/libcrypto/x509/x509_lu.c
deleted file mode 100644
index fdb10023be..0000000000
--- a/src/lib/libcrypto/x509/x509_lu.c
+++ /dev/null
@@ -1,739 +0,0 @@
1/* $OpenBSD: x509_lu.c,v 1.20 2015/04/25 16:02:55 doug Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/err.h>
62#include <openssl/lhash.h>
63#include <openssl/x509.h>
64#include <openssl/x509v3.h>
65#include "x509_lcl.h"
66
67X509_LOOKUP *
68X509_LOOKUP_new(X509_LOOKUP_METHOD *method)
69{
70 X509_LOOKUP *ret;
71
72 ret = malloc(sizeof(X509_LOOKUP));
73 if (ret == NULL)
74 return NULL;
75
76 ret->init = 0;
77 ret->skip = 0;
78 ret->method = method;
79 ret->method_data = NULL;
80 ret->store_ctx = NULL;
81 if ((method->new_item != NULL) && !method->new_item(ret)) {
82 free(ret);
83 return NULL;
84 }
85 return ret;
86}
87
88void
89X509_LOOKUP_free(X509_LOOKUP *ctx)
90{
91 if (ctx == NULL)
92 return;
93 if ((ctx->method != NULL) && (ctx->method->free != NULL))
94 (*ctx->method->free)(ctx);
95 free(ctx);
96}
97
98int
99X509_LOOKUP_init(X509_LOOKUP *ctx)
100{
101 if (ctx->method == NULL)
102 return 0;
103 if (ctx->method->init != NULL)
104 return ctx->method->init(ctx);
105 else
106 return 1;
107}
108
109int
110X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
111{
112 if (ctx->method == NULL)
113 return 0;
114 if (ctx->method->shutdown != NULL)
115 return ctx->method->shutdown(ctx);
116 else
117 return 1;
118}
119
120int
121X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
122 char **ret)
123{
124 if (ctx->method == NULL)
125 return -1;
126 if (ctx->method->ctrl != NULL)
127 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
128 else
129 return 1;
130}
131
132int
133X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
134 X509_OBJECT *ret)
135{
136 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
137 return X509_LU_FAIL;
138 if (ctx->skip)
139 return 0;
140 return ctx->method->get_by_subject(ctx, type, name, ret);
141}
142
143int
144X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
145 ASN1_INTEGER *serial, X509_OBJECT *ret)
146{
147 if ((ctx->method == NULL) ||
148 (ctx->method->get_by_issuer_serial == NULL))
149 return X509_LU_FAIL;
150 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
151}
152
153int
154X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type, unsigned char *bytes,
155 int len, X509_OBJECT *ret)
156{
157 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
158 return X509_LU_FAIL;
159 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
160}
161
162int
163X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str, int len,
164 X509_OBJECT *ret)
165{
166 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
167 return X509_LU_FAIL;
168 return ctx->method->get_by_alias(ctx, type, str, len, ret);
169}
170
171static int
172x509_object_cmp(const X509_OBJECT * const *a, const X509_OBJECT * const *b)
173{
174 int ret;
175
176 ret = ((*a)->type - (*b)->type);
177 if (ret)
178 return ret;
179 switch ((*a)->type) {
180 case X509_LU_X509:
181 ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
182 break;
183 case X509_LU_CRL:
184 ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
185 break;
186 default:
187 /* abort(); */
188 return 0;
189 }
190 return ret;
191}
192
193X509_STORE *
194X509_STORE_new(void)
195{
196 X509_STORE *ret;
197
198 if ((ret = malloc(sizeof(X509_STORE))) == NULL)
199 return NULL;
200 ret->objs = sk_X509_OBJECT_new(x509_object_cmp);
201 ret->cache = 1;
202 ret->get_cert_methods = sk_X509_LOOKUP_new_null();
203 ret->verify = 0;
204 ret->verify_cb = 0;
205
206 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL)
207 goto err;
208
209 ret->get_issuer = 0;
210 ret->check_issued = 0;
211 ret->check_revocation = 0;
212 ret->get_crl = 0;
213 ret->check_crl = 0;
214 ret->cert_crl = 0;
215 ret->lookup_certs = 0;
216 ret->lookup_crls = 0;
217 ret->cleanup = 0;
218
219 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data))
220 goto err;
221
222 ret->references = 1;
223 return ret;
224
225err:
226 X509_VERIFY_PARAM_free(ret->param);
227 sk_X509_LOOKUP_free(ret->get_cert_methods);
228 sk_X509_OBJECT_free(ret->objs);
229 free(ret);
230 return NULL;
231}
232
233static void
234cleanup(X509_OBJECT *a)
235{
236 if (a->type == X509_LU_X509) {
237 X509_free(a->data.x509);
238 } else if (a->type == X509_LU_CRL) {
239 X509_CRL_free(a->data.crl);
240 } else {
241 /* abort(); */
242 }
243
244 free(a);
245}
246
247void
248X509_STORE_free(X509_STORE *vfy)
249{
250 int i;
251 STACK_OF(X509_LOOKUP) *sk;
252 X509_LOOKUP *lu;
253
254 if (vfy == NULL)
255 return;
256
257 i = CRYPTO_add(&vfy->references, -1, CRYPTO_LOCK_X509_STORE);
258 if (i > 0)
259 return;
260
261 sk = vfy->get_cert_methods;
262 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
263 lu = sk_X509_LOOKUP_value(sk, i);
264 X509_LOOKUP_shutdown(lu);
265 X509_LOOKUP_free(lu);
266 }
267 sk_X509_LOOKUP_free(sk);
268 sk_X509_OBJECT_pop_free(vfy->objs, cleanup);
269
270 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
271 X509_VERIFY_PARAM_free(vfy->param);
272 free(vfy);
273}
274
275X509_LOOKUP *
276X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
277{
278 int i;
279 STACK_OF(X509_LOOKUP) *sk;
280 X509_LOOKUP *lu;
281
282 sk = v->get_cert_methods;
283 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
284 lu = sk_X509_LOOKUP_value(sk, i);
285 if (m == lu->method) {
286 return lu;
287 }
288 }
289 /* a new one */
290 lu = X509_LOOKUP_new(m);
291 if (lu == NULL)
292 return NULL;
293 else {
294 lu->store_ctx = v;
295 if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
296 return lu;
297 else {
298 X509_LOOKUP_free(lu);
299 return NULL;
300 }
301 }
302}
303
304int
305X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name,
306 X509_OBJECT *ret)
307{
308 X509_STORE *ctx = vs->ctx;
309 X509_LOOKUP *lu;
310 X509_OBJECT stmp, *tmp;
311 int i, j;
312
313 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
314 tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
315 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
316
317 if (tmp == NULL || type == X509_LU_CRL) {
318 for (i = vs->current_method;
319 i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
320 lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
321 j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
322 if (j < 0) {
323 vs->current_method = j;
324 return j;
325 } else if (j) {
326 tmp = &stmp;
327 break;
328 }
329 }
330 vs->current_method = 0;
331 if (tmp == NULL)
332 return 0;
333 }
334
335/* if (ret->data.ptr != NULL)
336 X509_OBJECT_free_contents(ret); */
337
338 ret->type = tmp->type;
339 ret->data.ptr = tmp->data.ptr;
340
341 X509_OBJECT_up_ref_count(ret);
342
343 return 1;
344}
345
346int
347X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
348{
349 X509_OBJECT *obj;
350 int ret = 1;
351
352 if (x == NULL)
353 return 0;
354 obj = malloc(sizeof(X509_OBJECT));
355 if (obj == NULL) {
356 X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
357 return 0;
358 }
359 obj->type = X509_LU_X509;
360 obj->data.x509 = x;
361
362 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
363
364 X509_OBJECT_up_ref_count(obj);
365
366 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
367 X509_OBJECT_free_contents(obj);
368 free(obj);
369 X509err(X509_F_X509_STORE_ADD_CERT,
370 X509_R_CERT_ALREADY_IN_HASH_TABLE);
371 ret = 0;
372 } else
373 sk_X509_OBJECT_push(ctx->objs, obj);
374
375 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
376
377 return ret;
378}
379
380int
381X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
382{
383 X509_OBJECT *obj;
384 int ret = 1;
385
386 if (x == NULL)
387 return 0;
388 obj = malloc(sizeof(X509_OBJECT));
389 if (obj == NULL) {
390 X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
391 return 0;
392 }
393 obj->type = X509_LU_CRL;
394 obj->data.crl = x;
395
396 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
397
398 X509_OBJECT_up_ref_count(obj);
399
400 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
401 X509_OBJECT_free_contents(obj);
402 free(obj);
403 X509err(X509_F_X509_STORE_ADD_CRL,
404 X509_R_CERT_ALREADY_IN_HASH_TABLE);
405 ret = 0;
406 } else
407 sk_X509_OBJECT_push(ctx->objs, obj);
408
409 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
410
411 return ret;
412}
413
414void
415X509_OBJECT_up_ref_count(X509_OBJECT *a)
416{
417 switch (a->type) {
418 case X509_LU_X509:
419 CRYPTO_add(&a->data.x509->references, 1, CRYPTO_LOCK_X509);
420 break;
421 case X509_LU_CRL:
422 CRYPTO_add(&a->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
423 break;
424 }
425}
426
427void
428X509_OBJECT_free_contents(X509_OBJECT *a)
429{
430 switch (a->type) {
431 case X509_LU_X509:
432 X509_free(a->data.x509);
433 break;
434 case X509_LU_CRL:
435 X509_CRL_free(a->data.crl);
436 break;
437 }
438}
439
440static int
441x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name,
442 int *pnmatch)
443{
444 X509_OBJECT stmp;
445 X509 x509_s;
446 X509_CINF cinf_s;
447 X509_CRL crl_s;
448 X509_CRL_INFO crl_info_s;
449 int idx;
450
451 stmp.type = type;
452 switch (type) {
453 case X509_LU_X509:
454 stmp.data.x509 = &x509_s;
455 x509_s.cert_info = &cinf_s;
456 cinf_s.subject = name;
457 break;
458 case X509_LU_CRL:
459 stmp.data.crl = &crl_s;
460 crl_s.crl = &crl_info_s;
461 crl_info_s.issuer = name;
462 break;
463 default:
464 /* abort(); */
465 return -1;
466 }
467
468 idx = sk_X509_OBJECT_find(h, &stmp);
469 if (idx >= 0 && pnmatch) {
470 int tidx;
471 const X509_OBJECT *tobj, *pstmp;
472 *pnmatch = 1;
473 pstmp = &stmp;
474 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
475 tobj = sk_X509_OBJECT_value(h, tidx);
476 if (x509_object_cmp(&tobj, &pstmp))
477 break;
478 (*pnmatch)++;
479 }
480 }
481 return idx;
482}
483
484int
485X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name)
486{
487 return x509_object_idx_cnt(h, type, name, NULL);
488}
489
490X509_OBJECT *
491X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type,
492 X509_NAME *name)
493{
494 int idx;
495
496 idx = X509_OBJECT_idx_by_subject(h, type, name);
497 if (idx == -1)
498 return NULL;
499 return sk_X509_OBJECT_value(h, idx);
500}
501
502STACK_OF(X509) *
503X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
504{
505 int i, idx, cnt;
506 STACK_OF(X509) *sk;
507 X509 *x;
508 X509_OBJECT *obj;
509
510 sk = sk_X509_new_null();
511 if (sk == NULL)
512 return NULL;
513 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
514 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
515 if (idx < 0) {
516 /* Nothing found in cache: do lookup to possibly add new
517 * objects to cache
518 */
519 X509_OBJECT xobj;
520 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
521 if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) {
522 sk_X509_free(sk);
523 return NULL;
524 }
525 X509_OBJECT_free_contents(&xobj);
526 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
527 idx = x509_object_idx_cnt(ctx->ctx->objs,
528 X509_LU_X509, nm, &cnt);
529 if (idx < 0) {
530 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
531 sk_X509_free(sk);
532 return NULL;
533 }
534 }
535 for (i = 0; i < cnt; i++, idx++) {
536 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
537 x = obj->data.x509;
538 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
539 if (!sk_X509_push(sk, x)) {
540 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
541 X509_free(x);
542 sk_X509_pop_free(sk, X509_free);
543 return NULL;
544 }
545 }
546 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
547 return sk;
548
549}
550
551STACK_OF(X509_CRL) *
552X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
553{
554 int i, idx, cnt;
555 STACK_OF(X509_CRL) *sk;
556 X509_CRL *x;
557 X509_OBJECT *obj, xobj;
558
559 sk = sk_X509_CRL_new_null();
560 if (sk == NULL)
561 return NULL;
562 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
563 /* Check cache first */
564 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
565
566 /* Always do lookup to possibly add new CRLs to cache
567 */
568 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
569 if (!X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) {
570 sk_X509_CRL_free(sk);
571 return NULL;
572 }
573 X509_OBJECT_free_contents(&xobj);
574 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
575 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
576 if (idx < 0) {
577 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
578 sk_X509_CRL_free(sk);
579 return NULL;
580 }
581
582 for (i = 0; i < cnt; i++, idx++) {
583 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
584 x = obj->data.crl;
585 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509_CRL);
586 if (!sk_X509_CRL_push(sk, x)) {
587 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
588 X509_CRL_free(x);
589 sk_X509_CRL_pop_free(sk, X509_CRL_free);
590 return NULL;
591 }
592 }
593 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
594 return sk;
595}
596
597X509_OBJECT *
598X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x)
599{
600 int idx, i;
601 X509_OBJECT *obj;
602
603 idx = sk_X509_OBJECT_find(h, x);
604 if (idx == -1)
605 return NULL;
606 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
607 return sk_X509_OBJECT_value(h, idx);
608 for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
609 obj = sk_X509_OBJECT_value(h, i);
610 if (x509_object_cmp((const X509_OBJECT **)&obj,
611 (const X509_OBJECT **)&x))
612 return NULL;
613 if (x->type == X509_LU_X509) {
614 if (!X509_cmp(obj->data.x509, x->data.x509))
615 return obj;
616 } else if (x->type == X509_LU_CRL) {
617 if (!X509_CRL_match(obj->data.crl, x->data.crl))
618 return obj;
619 } else
620 return obj;
621 }
622 return NULL;
623}
624
625
626/* Try to get issuer certificate from store. Due to limitations
627 * of the API this can only retrieve a single certificate matching
628 * a given subject name. However it will fill the cache with all
629 * matching certificates, so we can examine the cache for all
630 * matches.
631 *
632 * Return values are:
633 * 1 lookup successful.
634 * 0 certificate not found.
635 * -1 some other error.
636 */
637int
638X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
639{
640 X509_NAME *xn;
641 X509_OBJECT obj, *pobj;
642 int i, ok, idx, ret;
643
644 *issuer = NULL;
645 xn = X509_get_issuer_name(x);
646 ok = X509_STORE_get_by_subject(ctx, X509_LU_X509, xn, &obj);
647 if (ok != X509_LU_X509) {
648 if (ok == X509_LU_RETRY) {
649 X509_OBJECT_free_contents(&obj);
650 X509err(X509_F_X509_STORE_CTX_GET1_ISSUER,
651 X509_R_SHOULD_RETRY);
652 return -1;
653 } else if (ok != X509_LU_FAIL) {
654 X509_OBJECT_free_contents(&obj);
655 /* not good :-(, break anyway */
656 return -1;
657 }
658 return 0;
659 }
660 /* If certificate matches all OK */
661 if (ctx->check_issued(ctx, x, obj.data.x509)) {
662 if (x509_check_cert_time(ctx, obj.data.x509, 1)) {
663 *issuer = obj.data.x509;
664 return 1;
665 }
666 }
667 X509_OBJECT_free_contents(&obj);
668
669 /* Else find index of first cert accepted by 'check_issued' */
670 ret = 0;
671 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
672 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
673 if (idx != -1) /* should be true as we've had at least one match */ {
674 /* Look through all matching certs for suitable issuer */
675 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
676 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
677 /* See if we've run past the matches */
678 if (pobj->type != X509_LU_X509)
679 break;
680 if (X509_NAME_cmp(xn,
681 X509_get_subject_name(pobj->data.x509)))
682 break;
683 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
684 *issuer = pobj->data.x509;
685 ret = 1;
686 /*
687 * If times check, exit with match,
688 * otherwise keep looking. Leave last
689 * match in issuer so we return nearest
690 * match if no certificate time is OK.
691 */
692 if (x509_check_cert_time(ctx, *issuer, 1))
693 break;
694 }
695 }
696 }
697 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
698 if (*issuer)
699 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
700 return ret;
701}
702
703int
704X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
705{
706 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
707}
708
709int
710X509_STORE_set_depth(X509_STORE *ctx, int depth)
711{
712 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
713 return 1;
714}
715
716int
717X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
718{
719 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
720}
721
722int
723X509_STORE_set_trust(X509_STORE *ctx, int trust)
724{
725 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
726}
727
728int
729X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
730{
731 return X509_VERIFY_PARAM_set1(ctx->param, param);
732}
733
734void
735X509_STORE_set_verify_cb(X509_STORE *ctx,
736 int (*verify_cb)(int, X509_STORE_CTX *))
737{
738 ctx->verify_cb = verify_cb;
739}
diff --git a/src/lib/libcrypto/x509/x509_obj.c b/src/lib/libcrypto/x509/x509_obj.c
deleted file mode 100644
index f7f2a380a1..0000000000
--- a/src/lib/libcrypto/x509/x509_obj.c
+++ /dev/null
@@ -1,179 +0,0 @@
1/* $OpenBSD: x509_obj.c,v 1.16 2014/07/11 08:44:49 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/buffer.h>
63#include <openssl/err.h>
64#include <openssl/lhash.h>
65#include <openssl/objects.h>
66#include <openssl/x509.h>
67
68char *
69X509_NAME_oneline(X509_NAME *a, char *buf, int len)
70{
71 X509_NAME_ENTRY *ne;
72 int i;
73 int n, lold, l, l1, l2, num, j, type;
74 const char *s;
75 char *p;
76 unsigned char *q;
77 BUF_MEM *b = NULL;
78 static const char hex[17] = "0123456789ABCDEF";
79 int gs_doit[4];
80 char tmp_buf[80];
81
82 if (buf == NULL) {
83 if ((b = BUF_MEM_new()) == NULL)
84 goto err;
85 if (!BUF_MEM_grow(b, 200))
86 goto err;
87 b->data[0] = '\0';
88 len = 200;
89 }
90 if (a == NULL) {
91 if (b) {
92 buf = b->data;
93 free(b);
94 }
95 strlcpy(buf, "NO X509_NAME", len);
96 return buf;
97 }
98
99 len--; /* space for '\0' */
100 l = 0;
101 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
102 ne = sk_X509_NAME_ENTRY_value(a->entries, i);
103 n = OBJ_obj2nid(ne->object);
104 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
105 i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
106 s = tmp_buf;
107 }
108 l1 = strlen(s);
109
110 type = ne->value->type;
111 num = ne->value->length;
112 q = ne->value->data;
113 if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
114 gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
115 for (j = 0; j < num; j++)
116 if (q[j] != 0)
117 gs_doit[j & 3] = 1;
118
119 if (gs_doit[0]|gs_doit[1]|gs_doit[2])
120 gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
121 else {
122 gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
123 gs_doit[3] = 1;
124 }
125 } else
126 gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
127
128 for (l2 = j=0; j < num; j++) {
129 if (!gs_doit[j&3])
130 continue;
131 l2++;
132 if ((q[j] < ' ') || (q[j] > '~'))
133 l2 += 3;
134 }
135
136 lold = l;
137 l += 1 + l1 + 1 + l2;
138 if (b != NULL) {
139 if (!BUF_MEM_grow(b, l + 1))
140 goto err;
141 p = &(b->data[lold]);
142 } else if (l > len) {
143 break;
144 } else
145 p = &(buf[lold]);
146 *(p++) = '/';
147 memcpy(p, s, l1);
148 p += l1;
149 *(p++) = '=';
150 q = ne->value->data;
151 for (j = 0; j < num; j++) {
152 if (!gs_doit[j & 3])
153 continue;
154 n = q[j];
155 if ((n < ' ') || (n > '~')) {
156 *(p++) = '\\';
157 *(p++) = 'x';
158 *(p++) = hex[(n >> 4) & 0x0f];
159 *(p++) = hex[n & 0x0f];
160 } else
161 *(p++) = n;
162 }
163 *p = '\0';
164 }
165 if (b != NULL) {
166 p = b->data;
167 free(b);
168 } else
169 p = buf;
170 if (i == 0)
171 *p = '\0';
172 return (p);
173
174err:
175 X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
176 if (b != NULL)
177 BUF_MEM_free(b);
178 return (NULL);
179}
diff --git a/src/lib/libcrypto/x509/x509_r2x.c b/src/lib/libcrypto/x509/x509_r2x.c
deleted file mode 100644
index 76faa29b7f..0000000000
--- a/src/lib/libcrypto/x509/x509_r2x.c
+++ /dev/null
@@ -1,115 +0,0 @@
1/* $OpenBSD: x509_r2x.c,v 1.10 2015/09/30 17:30:16 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/bn.h>
63#include <openssl/buffer.h>
64#include <openssl/err.h>
65#include <openssl/evp.h>
66#include <openssl/objects.h>
67#include <openssl/x509.h>
68
69X509 *
70X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
71{
72 X509 *ret = NULL;
73 X509_CINF *xi = NULL;
74 X509_NAME *xn;
75
76 if ((ret = X509_new()) == NULL) {
77 X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE);
78 goto err;
79 }
80
81 /* duplicate the request */
82 xi = ret->cert_info;
83
84 if (sk_X509_ATTRIBUTE_num(r->req_info->attributes) != 0) {
85 if ((xi->version = ASN1_INTEGER_new()) == NULL)
86 goto err;
87 if (!ASN1_INTEGER_set(xi->version, 2))
88 goto err;
89/* xi->extensions=ri->attributes; <- bad, should not ever be done
90 ri->attributes=NULL; */
91 }
92
93 xn = X509_REQ_get_subject_name(r);
94 if (X509_set_subject_name(ret, X509_NAME_dup(xn)) == 0)
95 goto err;
96 if (X509_set_issuer_name(ret, X509_NAME_dup(xn)) == 0)
97 goto err;
98
99 if (X509_gmtime_adj(xi->validity->notBefore, 0) == NULL)
100 goto err;
101 if (X509_gmtime_adj(xi->validity->notAfter,
102 (long)60 * 60 * 24 * days) == NULL)
103 goto err;
104
105 X509_set_pubkey(ret, X509_REQ_get_pubkey(r));
106
107 if (!X509_sign(ret, pkey, EVP_md5()))
108 goto err;
109 if (0) {
110err:
111 X509_free(ret);
112 ret = NULL;
113 }
114 return (ret);
115}
diff --git a/src/lib/libcrypto/x509/x509_req.c b/src/lib/libcrypto/x509/x509_req.c
deleted file mode 100644
index dc4bc2639a..0000000000
--- a/src/lib/libcrypto/x509/x509_req.c
+++ /dev/null
@@ -1,347 +0,0 @@
1/* $OpenBSD: x509_req.c,v 1.18 2015/09/30 17:30:16 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/opensslconf.h>
62
63#include <openssl/asn1.h>
64#include <openssl/asn1t.h>
65#include <openssl/bn.h>
66#include <openssl/buffer.h>
67#include <openssl/err.h>
68#include <openssl/evp.h>
69#include <openssl/objects.h>
70#include <openssl/pem.h>
71#include <openssl/x509.h>
72
73X509_REQ *
74X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
75{
76 X509_REQ *ret;
77 X509_REQ_INFO *ri;
78 int i;
79 EVP_PKEY *pktmp;
80
81 ret = X509_REQ_new();
82 if (ret == NULL) {
83 X509err(X509_F_X509_TO_X509_REQ, ERR_R_MALLOC_FAILURE);
84 goto err;
85 }
86
87 ri = ret->req_info;
88
89 if ((ri->version = ASN1_INTEGER_new()) == NULL)
90 goto err;
91 if (ASN1_INTEGER_set(ri->version, 0) == 0)
92 goto err;
93
94 if (!X509_REQ_set_subject_name(ret, X509_get_subject_name(x)))
95 goto err;
96
97 if ((pktmp = X509_get_pubkey(x)) == NULL)
98 goto err;
99
100 i = X509_REQ_set_pubkey(ret, pktmp);
101 EVP_PKEY_free(pktmp);
102 if (!i)
103 goto err;
104
105 if (pkey != NULL) {
106 if (!X509_REQ_sign(ret, pkey, md))
107 goto err;
108 }
109 return (ret);
110
111err:
112 X509_REQ_free(ret);
113 return (NULL);
114}
115
116EVP_PKEY *
117X509_REQ_get_pubkey(X509_REQ *req)
118{
119 if ((req == NULL) || (req->req_info == NULL))
120 return (NULL);
121 return (X509_PUBKEY_get(req->req_info->pubkey));
122}
123
124int
125X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
126{
127 EVP_PKEY *xk = NULL;
128 int ok = 0;
129
130 xk = X509_REQ_get_pubkey(x);
131 switch (EVP_PKEY_cmp(xk, k)) {
132 case 1:
133 ok = 1;
134 break;
135 case 0:
136 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
137 X509_R_KEY_VALUES_MISMATCH);
138 break;
139 case -1:
140 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
141 X509_R_KEY_TYPE_MISMATCH);
142 break;
143 case -2:
144#ifndef OPENSSL_NO_EC
145 if (k->type == EVP_PKEY_EC) {
146 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
147 ERR_R_EC_LIB);
148 break;
149 }
150#endif
151#ifndef OPENSSL_NO_DH
152 if (k->type == EVP_PKEY_DH) {
153 /* No idea */
154 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
155 X509_R_CANT_CHECK_DH_KEY);
156 break;
157 }
158#endif
159 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
160 X509_R_UNKNOWN_KEY_TYPE);
161 }
162
163 EVP_PKEY_free(xk);
164 return (ok);
165}
166
167/* It seems several organisations had the same idea of including a list of
168 * extensions in a certificate request. There are at least two OIDs that are
169 * used and there may be more: so the list is configurable.
170 */
171
172static int ext_nid_list[] = {NID_ext_req, NID_ms_ext_req, NID_undef};
173
174static int *ext_nids = ext_nid_list;
175
176int
177X509_REQ_extension_nid(int req_nid)
178{
179 int i, nid;
180
181 for (i = 0; ; i++) {
182 nid = ext_nids[i];
183 if (nid == NID_undef)
184 return 0;
185 else if (req_nid == nid)
186 return 1;
187 }
188}
189
190int *
191X509_REQ_get_extension_nids(void)
192{
193 return ext_nids;
194}
195
196void
197X509_REQ_set_extension_nids(int *nids)
198{
199 ext_nids = nids;
200}
201
202STACK_OF(X509_EXTENSION) *
203X509_REQ_get_extensions(X509_REQ *req)
204{
205 X509_ATTRIBUTE *attr;
206 ASN1_TYPE *ext = NULL;
207 int idx, *pnid;
208 const unsigned char *p;
209
210 if ((req == NULL) || (req->req_info == NULL) || !ext_nids)
211 return (NULL);
212 for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
213 idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
214 if (idx == -1)
215 continue;
216 attr = X509_REQ_get_attr(req, idx);
217 if (attr->single)
218 ext = attr->value.single;
219 else if (sk_ASN1_TYPE_num(attr->value.set))
220 ext = sk_ASN1_TYPE_value(attr->value.set, 0);
221 break;
222 }
223 if (!ext || (ext->type != V_ASN1_SEQUENCE))
224 return NULL;
225 p = ext->value.sequence->data;
226 return (STACK_OF(X509_EXTENSION) *)ASN1_item_d2i(NULL, &p,
227 ext->value.sequence->length, ASN1_ITEM_rptr(X509_EXTENSIONS));
228}
229
230/* Add a STACK_OF extensions to a certificate request: allow alternative OIDs
231 * in case we want to create a non standard one.
232 */
233
234int
235X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
236 int nid)
237{
238 ASN1_TYPE *at = NULL;
239 X509_ATTRIBUTE *attr = NULL;
240
241 if (!(at = ASN1_TYPE_new()) ||
242 !(at->value.sequence = ASN1_STRING_new()))
243 goto err;
244
245 at->type = V_ASN1_SEQUENCE;
246 /* Generate encoding of extensions */
247 at->value.sequence->length = ASN1_item_i2d((ASN1_VALUE *)exts,
248 &at->value.sequence->data, ASN1_ITEM_rptr(X509_EXTENSIONS));
249 if (!(attr = X509_ATTRIBUTE_new()))
250 goto err;
251 if (!(attr->value.set = sk_ASN1_TYPE_new_null()))
252 goto err;
253 if (!sk_ASN1_TYPE_push(attr->value.set, at))
254 goto err;
255 at = NULL;
256 attr->single = 0;
257 attr->object = OBJ_nid2obj(nid);
258 if (!req->req_info->attributes) {
259 if (!(req->req_info->attributes = sk_X509_ATTRIBUTE_new_null()))
260 goto err;
261 }
262 if (!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr))
263 goto err;
264 return 1;
265
266err:
267 X509_ATTRIBUTE_free(attr);
268 ASN1_TYPE_free(at);
269 return 0;
270}
271
272/* This is the normal usage: use the "official" OID */
273int
274X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
275{
276 return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
277}
278
279/* Request attribute functions */
280
281int
282X509_REQ_get_attr_count(const X509_REQ *req)
283{
284 return X509at_get_attr_count(req->req_info->attributes);
285}
286
287int
288X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
289{
290 return X509at_get_attr_by_NID(req->req_info->attributes, nid, lastpos);
291}
292
293int
294X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj, int lastpos)
295{
296 return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos);
297}
298
299X509_ATTRIBUTE *
300X509_REQ_get_attr(const X509_REQ *req, int loc)
301{
302 return X509at_get_attr(req->req_info->attributes, loc);
303}
304
305X509_ATTRIBUTE *
306X509_REQ_delete_attr(X509_REQ *req, int loc)
307{
308 return X509at_delete_attr(req->req_info->attributes, loc);
309}
310
311int
312X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
313{
314 if (X509at_add1_attr(&req->req_info->attributes, attr))
315 return 1;
316 return 0;
317}
318
319int
320X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj, int type,
321 const unsigned char *bytes, int len)
322{
323 if (X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj,
324 type, bytes, len))
325 return 1;
326 return 0;
327}
328
329int
330X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int type,
331 const unsigned char *bytes, int len)
332{
333 if (X509at_add1_attr_by_NID(&req->req_info->attributes, nid,
334 type, bytes, len))
335 return 1;
336 return 0;
337}
338
339int
340X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int type,
341 const unsigned char *bytes, int len)
342{
343 if (X509at_add1_attr_by_txt(&req->req_info->attributes, attrname,
344 type, bytes, len))
345 return 1;
346 return 0;
347}
diff --git a/src/lib/libcrypto/x509/x509_set.c b/src/lib/libcrypto/x509/x509_set.c
deleted file mode 100644
index aeaf161024..0000000000
--- a/src/lib/libcrypto/x509/x509_set.c
+++ /dev/null
@@ -1,154 +0,0 @@
1/* $OpenBSD: x509_set.c,v 1.12 2015/09/30 17:49:59 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65
66int
67X509_set_version(X509 *x, long version)
68{
69 if (x == NULL)
70 return (0);
71 if (x->cert_info->version == NULL) {
72 if ((x->cert_info->version = ASN1_INTEGER_new()) == NULL)
73 return (0);
74 }
75 return (ASN1_INTEGER_set(x->cert_info->version, version));
76}
77
78int
79X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
80{
81 ASN1_INTEGER *in;
82
83 if (x == NULL)
84 return (0);
85 in = x->cert_info->serialNumber;
86 if (in != serial) {
87 in = ASN1_INTEGER_dup(serial);
88 if (in != NULL) {
89 ASN1_INTEGER_free(x->cert_info->serialNumber);
90 x->cert_info->serialNumber = in;
91 }
92 }
93 return (in != NULL);
94}
95
96int
97X509_set_issuer_name(X509 *x, X509_NAME *name)
98{
99 if ((x == NULL) || (x->cert_info == NULL))
100 return (0);
101 return (X509_NAME_set(&x->cert_info->issuer, name));
102}
103
104int
105X509_set_subject_name(X509 *x, X509_NAME *name)
106{
107 if ((x == NULL) || (x->cert_info == NULL))
108 return (0);
109 return (X509_NAME_set(&x->cert_info->subject, name));
110}
111
112int
113X509_set_notBefore(X509 *x, const ASN1_TIME *tm)
114{
115 ASN1_TIME *in;
116
117 if ((x == NULL) || (x->cert_info->validity == NULL))
118 return (0);
119 in = x->cert_info->validity->notBefore;
120 if (in != tm) {
121 in = ASN1_STRING_dup(tm);
122 if (in != NULL) {
123 ASN1_TIME_free(x->cert_info->validity->notBefore);
124 x->cert_info->validity->notBefore = in;
125 }
126 }
127 return (in != NULL);
128}
129
130int
131X509_set_notAfter(X509 *x, const ASN1_TIME *tm)
132{
133 ASN1_TIME *in;
134
135 if ((x == NULL) || (x->cert_info->validity == NULL))
136 return (0);
137 in = x->cert_info->validity->notAfter;
138 if (in != tm) {
139 in = ASN1_STRING_dup(tm);
140 if (in != NULL) {
141 ASN1_TIME_free(x->cert_info->validity->notAfter);
142 x->cert_info->validity->notAfter = in;
143 }
144 }
145 return (in != NULL);
146}
147
148int
149X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
150{
151 if ((x == NULL) || (x->cert_info == NULL))
152 return (0);
153 return (X509_PUBKEY_set(&(x->cert_info->key), pkey));
154}
diff --git a/src/lib/libcrypto/x509/x509_trs.c b/src/lib/libcrypto/x509/x509_trs.c
deleted file mode 100644
index 42fb97f571..0000000000
--- a/src/lib/libcrypto/x509/x509_trs.c
+++ /dev/null
@@ -1,332 +0,0 @@
1/* $OpenBSD: x509_trs.c,v 1.20 2015/02/10 11:22:21 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/err.h>
63#include <openssl/x509v3.h>
64
65static int tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b);
66static void trtable_free(X509_TRUST *p);
67
68static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
69static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
70static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
71
72static int obj_trust(int id, X509 *x, int flags);
73static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
74
75/* WARNING: the following table should be kept in order of trust
76 * and without any gaps so we can just subtract the minimum trust
77 * value to get an index into the table
78 */
79
80static X509_TRUST trstandard[] = {
81 {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL},
82 {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL},
83 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL},
84 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL},
85 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL},
86 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL},
87 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL},
88 {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
89};
90
91#define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST))
92
93static STACK_OF(X509_TRUST) *trtable = NULL;
94
95static int
96tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b)
97{
98 return (*a)->trust - (*b)->trust;
99}
100
101int
102(*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
103{
104 int (*oldtrust)(int , X509 *, int);
105
106 oldtrust = default_trust;
107 default_trust = trust;
108 return oldtrust;
109}
110
111int
112X509_check_trust(X509 *x, int id, int flags)
113{
114 X509_TRUST *pt;
115 int idx;
116
117 if (id == -1)
118 return 1;
119 idx = X509_TRUST_get_by_id(id);
120 if (idx == -1)
121 return default_trust(id, x, flags);
122 pt = X509_TRUST_get0(idx);
123 return pt->check_trust(pt, x, flags);
124}
125
126int
127X509_TRUST_get_count(void)
128{
129 if (!trtable)
130 return X509_TRUST_COUNT;
131 return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
132}
133
134X509_TRUST *
135X509_TRUST_get0(int idx)
136{
137 if (idx < 0)
138 return NULL;
139 if (idx < (int)X509_TRUST_COUNT)
140 return trstandard + idx;
141 return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
142}
143
144int
145X509_TRUST_get_by_id(int id)
146{
147 X509_TRUST tmp;
148 int idx;
149
150 if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
151 return id - X509_TRUST_MIN;
152 tmp.trust = id;
153 if (!trtable)
154 return -1;
155 idx = sk_X509_TRUST_find(trtable, &tmp);
156 if (idx == -1)
157 return -1;
158 return idx + X509_TRUST_COUNT;
159}
160
161int
162X509_TRUST_set(int *t, int trust)
163{
164 if (X509_TRUST_get_by_id(trust) == -1) {
165 X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
166 return 0;
167 }
168 *t = trust;
169 return 1;
170}
171
172int
173X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
174 char *name, int arg1, void *arg2)
175{
176 int idx;
177 X509_TRUST *trtmp;
178 char *name_dup;
179
180 /* This is set according to what we change: application can't set it */
181 flags &= ~X509_TRUST_DYNAMIC;
182 /* This will always be set for application modified trust entries */
183 flags |= X509_TRUST_DYNAMIC_NAME;
184 /* Get existing entry if any */
185 idx = X509_TRUST_get_by_id(id);
186 /* Need a new entry */
187 if (idx == -1) {
188 if (!(trtmp = malloc(sizeof(X509_TRUST)))) {
189 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
190 return 0;
191 }
192 trtmp->flags = X509_TRUST_DYNAMIC;
193 } else {
194 trtmp = X509_TRUST_get0(idx);
195 if (trtmp == NULL) {
196 X509err(X509_F_X509_TRUST_ADD, X509_R_INVALID_TRUST);
197 return 0;
198 }
199 }
200
201 if ((name_dup = strdup(name)) == NULL)
202 goto err;
203
204 /* free existing name if dynamic */
205 if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
206 free(trtmp->name);
207 /* dup supplied name */
208 trtmp->name = name_dup;
209 /* Keep the dynamic flag of existing entry */
210 trtmp->flags &= X509_TRUST_DYNAMIC;
211 /* Set all other flags */
212 trtmp->flags |= flags;
213
214 trtmp->trust = id;
215 trtmp->check_trust = ck;
216 trtmp->arg1 = arg1;
217 trtmp->arg2 = arg2;
218
219 /* If it's a new entry, manage the dynamic table */
220 if (idx == -1) {
221 if (trtable == NULL &&
222 (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL)
223 goto err;
224 if (sk_X509_TRUST_push(trtable, trtmp) == 0)
225 goto err;
226 }
227 return 1;
228
229err:
230 free(name_dup);
231 if (idx == -1)
232 free(trtmp);
233 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
234 return 0;
235}
236
237static void
238trtable_free(X509_TRUST *p)
239{
240 if (!p)
241 return;
242 if (p->flags & X509_TRUST_DYNAMIC) {
243 if (p->flags & X509_TRUST_DYNAMIC_NAME)
244 free(p->name);
245 free(p);
246 }
247}
248
249void
250X509_TRUST_cleanup(void)
251{
252 unsigned int i;
253
254 for (i = 0; i < X509_TRUST_COUNT; i++)
255 trtable_free(trstandard + i);
256 sk_X509_TRUST_pop_free(trtable, trtable_free);
257 trtable = NULL;
258}
259
260int
261X509_TRUST_get_flags(X509_TRUST *xp)
262{
263 return xp->flags;
264}
265
266char *
267X509_TRUST_get0_name(X509_TRUST *xp)
268{
269 return xp->name;
270}
271
272int
273X509_TRUST_get_trust(X509_TRUST *xp)
274{
275 return xp->trust;
276}
277
278static int
279trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
280{
281 if (x->aux && (x->aux->trust || x->aux->reject))
282 return obj_trust(trust->arg1, x, flags);
283 /* we don't have any trust settings: for compatibility
284 * we return trusted if it is self signed
285 */
286 return trust_compat(trust, x, flags);
287}
288
289static int
290trust_1oid(X509_TRUST *trust, X509 *x, int flags)
291{
292 if (x->aux)
293 return obj_trust(trust->arg1, x, flags);
294 return X509_TRUST_UNTRUSTED;
295}
296
297static int
298trust_compat(X509_TRUST *trust, X509 *x, int flags)
299{
300 X509_check_purpose(x, -1, 0);
301 if (x->ex_flags & EXFLAG_SS)
302 return X509_TRUST_TRUSTED;
303 else
304 return X509_TRUST_UNTRUSTED;
305}
306
307static int
308obj_trust(int id, X509 *x, int flags)
309{
310 ASN1_OBJECT *obj;
311 int i;
312 X509_CERT_AUX *ax;
313
314 ax = x->aux;
315 if (!ax)
316 return X509_TRUST_UNTRUSTED;
317 if (ax->reject) {
318 for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
319 obj = sk_ASN1_OBJECT_value(ax->reject, i);
320 if (OBJ_obj2nid(obj) == id)
321 return X509_TRUST_REJECTED;
322 }
323 }
324 if (ax->trust) {
325 for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
326 obj = sk_ASN1_OBJECT_value(ax->trust, i);
327 if (OBJ_obj2nid(obj) == id)
328 return X509_TRUST_TRUSTED;
329 }
330 }
331 return X509_TRUST_UNTRUSTED;
332}
diff --git a/src/lib/libcrypto/x509/x509_txt.c b/src/lib/libcrypto/x509/x509_txt.c
deleted file mode 100644
index 14fa2378c4..0000000000
--- a/src/lib/libcrypto/x509/x509_txt.c
+++ /dev/null
@@ -1,189 +0,0 @@
1/* $OpenBSD: x509_txt.c,v 1.19 2014/07/11 08:44:49 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <time.h>
62
63#include <openssl/asn1.h>
64#include <openssl/buffer.h>
65#include <openssl/evp.h>
66#include <openssl/lhash.h>
67#include <openssl/objects.h>
68#include <openssl/x509.h>
69
70const char *
71X509_verify_cert_error_string(long n)
72{
73 static char buf[100];
74
75 switch ((int)n) {
76 case X509_V_OK:
77 return("ok");
78 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
79 return("unable to get issuer certificate");
80 case X509_V_ERR_UNABLE_TO_GET_CRL:
81 return("unable to get certificate CRL");
82 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
83 return("unable to decrypt certificate's signature");
84 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
85 return("unable to decrypt CRL's signature");
86 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
87 return("unable to decode issuer public key");
88 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
89 return("certificate signature failure");
90 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
91 return("CRL signature failure");
92 case X509_V_ERR_CERT_NOT_YET_VALID:
93 return("certificate is not yet valid");
94 case X509_V_ERR_CRL_NOT_YET_VALID:
95 return("CRL is not yet valid");
96 case X509_V_ERR_CERT_HAS_EXPIRED:
97 return("certificate has expired");
98 case X509_V_ERR_CRL_HAS_EXPIRED:
99 return("CRL has expired");
100 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
101 return("format error in certificate's notBefore field");
102 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
103 return("format error in certificate's notAfter field");
104 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
105 return("format error in CRL's lastUpdate field");
106 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
107 return("format error in CRL's nextUpdate field");
108 case X509_V_ERR_OUT_OF_MEM:
109 return("out of memory");
110 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
111 return("self signed certificate");
112 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
113 return("self signed certificate in certificate chain");
114 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
115 return("unable to get local issuer certificate");
116 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
117 return("unable to verify the first certificate");
118 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
119 return("certificate chain too long");
120 case X509_V_ERR_CERT_REVOKED:
121 return("certificate revoked");
122 case X509_V_ERR_INVALID_CA:
123 return ("invalid CA certificate");
124 case X509_V_ERR_INVALID_NON_CA:
125 return ("invalid non-CA certificate (has CA markings)");
126 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
127 return ("path length constraint exceeded");
128 case X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED:
129 return("proxy path length constraint exceeded");
130 case X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED:
131 return("proxy certificates not allowed, please set the appropriate flag");
132 case X509_V_ERR_INVALID_PURPOSE:
133 return ("unsupported certificate purpose");
134 case X509_V_ERR_CERT_UNTRUSTED:
135 return ("certificate not trusted");
136 case X509_V_ERR_CERT_REJECTED:
137 return ("certificate rejected");
138 case X509_V_ERR_APPLICATION_VERIFICATION:
139 return("application verification failure");
140 case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
141 return("subject issuer mismatch");
142 case X509_V_ERR_AKID_SKID_MISMATCH:
143 return("authority and subject key identifier mismatch");
144 case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
145 return("authority and issuer serial number mismatch");
146 case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
147 return("key usage does not include certificate signing");
148 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
149 return("unable to get CRL issuer certificate");
150 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
151 return("unhandled critical extension");
152 case X509_V_ERR_KEYUSAGE_NO_CRL_SIGN:
153 return("key usage does not include CRL signing");
154 case X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE:
155 return("key usage does not include digital signature");
156 case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION:
157 return("unhandled critical CRL extension");
158 case X509_V_ERR_INVALID_EXTENSION:
159 return("invalid or inconsistent certificate extension");
160 case X509_V_ERR_INVALID_POLICY_EXTENSION:
161 return("invalid or inconsistent certificate policy extension");
162 case X509_V_ERR_NO_EXPLICIT_POLICY:
163 return("no explicit policy");
164 case X509_V_ERR_DIFFERENT_CRL_SCOPE:
165 return("Different CRL scope");
166 case X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE:
167 return("Unsupported extension feature");
168 case X509_V_ERR_UNNESTED_RESOURCE:
169 return("RFC 3779 resource not subset of parent's resources");
170 case X509_V_ERR_PERMITTED_VIOLATION:
171 return("permitted subtree violation");
172 case X509_V_ERR_EXCLUDED_VIOLATION:
173 return("excluded subtree violation");
174 case X509_V_ERR_SUBTREE_MINMAX:
175 return("name constraints minimum and maximum not supported");
176 case X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:
177 return("unsupported name constraint type");
178 case X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX:
179 return("unsupported or invalid name constraint syntax");
180 case X509_V_ERR_UNSUPPORTED_NAME_SYNTAX:
181 return("unsupported or invalid name syntax");
182 case X509_V_ERR_CRL_PATH_VALIDATION_ERROR:
183 return("CRL path validation error");
184
185 default:
186 (void) snprintf(buf, sizeof buf, "error number %ld", n);
187 return(buf);
188 }
189}
diff --git a/src/lib/libcrypto/x509/x509_v3.c b/src/lib/libcrypto/x509/x509_v3.c
deleted file mode 100644
index d9ec9c8c14..0000000000
--- a/src/lib/libcrypto/x509/x509_v3.c
+++ /dev/null
@@ -1,300 +0,0 @@
1/* $OpenBSD: x509_v3.c,v 1.13 2016/03/21 04:05:33 mmcc Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/err.h>
63#include <openssl/evp.h>
64#include <openssl/objects.h>
65#include <openssl/stack.h>
66#include <openssl/x509.h>
67#include <openssl/x509v3.h>
68
69int
70X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
71{
72 if (x == NULL)
73 return (0);
74 return (sk_X509_EXTENSION_num(x));
75}
76
77int
78X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid, int lastpos)
79{
80 ASN1_OBJECT *obj;
81
82 obj = OBJ_nid2obj(nid);
83 if (obj == NULL)
84 return (-2);
85 return (X509v3_get_ext_by_OBJ(x, obj, lastpos));
86}
87
88int
89X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, ASN1_OBJECT *obj,
90 int lastpos)
91{
92 int n;
93 X509_EXTENSION *ex;
94
95 if (sk == NULL)
96 return (-1);
97 lastpos++;
98 if (lastpos < 0)
99 lastpos = 0;
100 n = sk_X509_EXTENSION_num(sk);
101 for (; lastpos < n; lastpos++) {
102 ex = sk_X509_EXTENSION_value(sk, lastpos);
103 if (OBJ_cmp(ex->object, obj) == 0)
104 return (lastpos);
105 }
106 return (-1);
107}
108
109int
110X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
111 int lastpos)
112{
113 int n;
114 X509_EXTENSION *ex;
115
116 if (sk == NULL)
117 return (-1);
118 lastpos++;
119 if (lastpos < 0)
120 lastpos = 0;
121 n = sk_X509_EXTENSION_num(sk);
122 for (; lastpos < n; lastpos++) {
123 ex = sk_X509_EXTENSION_value(sk, lastpos);
124 if (((ex->critical > 0) && crit) ||
125 ((ex->critical <= 0) && !crit))
126 return (lastpos);
127 }
128 return (-1);
129}
130
131X509_EXTENSION *
132X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
133{
134 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
135 return NULL;
136 else
137 return sk_X509_EXTENSION_value(x, loc);
138}
139
140X509_EXTENSION *
141X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
142{
143 X509_EXTENSION *ret;
144
145 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
146 return (NULL);
147 ret = sk_X509_EXTENSION_delete(x, loc);
148 return (ret);
149}
150
151STACK_OF(X509_EXTENSION) *
152X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, X509_EXTENSION *ex, int loc)
153{
154 X509_EXTENSION *new_ex = NULL;
155 int n;
156 STACK_OF(X509_EXTENSION) *sk = NULL;
157
158 if (x == NULL) {
159 X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
160 goto err2;
161 }
162
163 if (*x == NULL) {
164 if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
165 goto err;
166 } else
167 sk= *x;
168
169 n = sk_X509_EXTENSION_num(sk);
170 if (loc > n)
171 loc = n;
172 else if (loc < 0)
173 loc = n;
174
175 if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
176 goto err2;
177 if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
178 goto err;
179 if (*x == NULL)
180 *x = sk;
181 return (sk);
182
183err:
184 X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
185err2:
186 if (new_ex != NULL)
187 X509_EXTENSION_free(new_ex);
188 if (sk != NULL && (x != NULL && sk != *x))
189 sk_X509_EXTENSION_free(sk);
190 return (NULL);
191}
192
193X509_EXTENSION *
194X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid, int crit,
195 ASN1_OCTET_STRING *data)
196{
197 ASN1_OBJECT *obj;
198 X509_EXTENSION *ret;
199
200 obj = OBJ_nid2obj(nid);
201 if (obj == NULL) {
202 X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,
203 X509_R_UNKNOWN_NID);
204 return (NULL);
205 }
206 ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
207 if (ret == NULL)
208 ASN1_OBJECT_free(obj);
209 return (ret);
210}
211
212X509_EXTENSION *
213X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, ASN1_OBJECT *obj, int crit,
214 ASN1_OCTET_STRING *data)
215{
216 X509_EXTENSION *ret;
217
218 if ((ex == NULL) || (*ex == NULL)) {
219 if ((ret = X509_EXTENSION_new()) == NULL) {
220 X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
221 ERR_R_MALLOC_FAILURE);
222 return (NULL);
223 }
224 } else
225 ret= *ex;
226
227 if (!X509_EXTENSION_set_object(ret, obj))
228 goto err;
229 if (!X509_EXTENSION_set_critical(ret, crit))
230 goto err;
231 if (!X509_EXTENSION_set_data(ret, data))
232 goto err;
233
234 if ((ex != NULL) && (*ex == NULL))
235 *ex = ret;
236 return (ret);
237
238err:
239 if ((ex == NULL) || (ret != *ex))
240 X509_EXTENSION_free(ret);
241 return (NULL);
242}
243
244int
245X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
246{
247 if ((ex == NULL) || (obj == NULL))
248 return (0);
249 ASN1_OBJECT_free(ex->object);
250 ex->object = OBJ_dup(obj);
251 return ex->object != NULL;
252}
253
254int
255X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
256{
257 if (ex == NULL)
258 return (0);
259 ex->critical = (crit) ? 0xFF : -1;
260 return (1);
261}
262
263int
264X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
265{
266 int i;
267
268 if (ex == NULL)
269 return (0);
270 i = ASN1_STRING_set(ex->value, data->data, data->length);
271 if (!i)
272 return (0);
273 return (1);
274}
275
276ASN1_OBJECT *
277X509_EXTENSION_get_object(X509_EXTENSION *ex)
278{
279 if (ex == NULL)
280 return (NULL);
281 return (ex->object);
282}
283
284ASN1_OCTET_STRING *
285X509_EXTENSION_get_data(X509_EXTENSION *ex)
286{
287 if (ex == NULL)
288 return (NULL);
289 return (ex->value);
290}
291
292int
293X509_EXTENSION_get_critical(X509_EXTENSION *ex)
294{
295 if (ex == NULL)
296 return (0);
297 if (ex->critical > 0)
298 return 1;
299 return 0;
300}
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c
deleted file mode 100644
index 5c043aa7b1..0000000000
--- a/src/lib/libcrypto/x509/x509_vfy.c
+++ /dev/null
@@ -1,2157 +0,0 @@
1/* $OpenBSD: x509_vfy.c,v 1.49 2016/03/11 07:08:45 mmcc Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <string.h>
62#include <time.h>
63#include <unistd.h>
64
65#include <openssl/opensslconf.h>
66
67#include <openssl/asn1.h>
68#include <openssl/buffer.h>
69#include <openssl/crypto.h>
70#include <openssl/err.h>
71#include <openssl/evp.h>
72#include <openssl/lhash.h>
73#include <openssl/objects.h>
74#include <openssl/x509.h>
75#include <openssl/x509v3.h>
76#include "x509_lcl.h"
77
78/* CRL score values */
79
80/* No unhandled critical extensions */
81
82#define CRL_SCORE_NOCRITICAL 0x100
83
84/* certificate is within CRL scope */
85
86#define CRL_SCORE_SCOPE 0x080
87
88/* CRL times valid */
89
90#define CRL_SCORE_TIME 0x040
91
92/* Issuer name matches certificate */
93
94#define CRL_SCORE_ISSUER_NAME 0x020
95
96/* If this score or above CRL is probably valid */
97
98#define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
99
100/* CRL issuer is certificate issuer */
101
102#define CRL_SCORE_ISSUER_CERT 0x018
103
104/* CRL issuer is on certificate path */
105
106#define CRL_SCORE_SAME_PATH 0x008
107
108/* CRL issuer matches CRL AKID */
109
110#define CRL_SCORE_AKID 0x004
111
112/* Have a delta CRL with valid times */
113
114#define CRL_SCORE_TIME_DELTA 0x002
115
116static int null_callback(int ok, X509_STORE_CTX *e);
117static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
118static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
119static int check_chain_extensions(X509_STORE_CTX *ctx);
120static int check_name_constraints(X509_STORE_CTX *ctx);
121static int check_trust(X509_STORE_CTX *ctx);
122static int check_revocation(X509_STORE_CTX *ctx);
123static int check_cert(X509_STORE_CTX *ctx);
124static int check_policy(X509_STORE_CTX *ctx);
125
126static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
127 unsigned int *preasons, X509_CRL *crl, X509 *x);
128static int get_crl_delta(X509_STORE_CTX *ctx,
129 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
130static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score,
131 X509_CRL *base, STACK_OF(X509_CRL) *crls);
132static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
133 int *pcrl_score);
134static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
135 unsigned int *preasons);
136static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
137static int check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
138 STACK_OF(X509) *crl_path);
139
140static int internal_verify(X509_STORE_CTX *ctx);
141
142static int
143null_callback(int ok, X509_STORE_CTX *e)
144{
145 return ok;
146}
147
148#if 0
149static int
150x509_subject_cmp(X509 **a, X509 **b)
151{
152 return X509_subject_name_cmp(*a, *b);
153}
154#endif
155
156int
157X509_verify_cert(X509_STORE_CTX *ctx)
158{
159 X509 *x, *xtmp, *chain_ss = NULL;
160 int bad_chain = 0;
161 X509_VERIFY_PARAM *param = ctx->param;
162 int depth, i, ok = 0;
163 int num;
164 int (*cb)(int xok, X509_STORE_CTX *xctx);
165 STACK_OF(X509) *sktmp = NULL;
166
167 if (ctx->cert == NULL) {
168 X509err(X509_F_X509_VERIFY_CERT,
169 X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
170 return -1;
171 }
172
173 cb = ctx->verify_cb;
174
175 /* first we make sure the chain we are going to build is
176 * present and that the first entry is in place */
177 if (ctx->chain == NULL) {
178 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
179 (!sk_X509_push(ctx->chain, ctx->cert))) {
180 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
181 goto end;
182 }
183 CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
184 ctx->last_untrusted = 1;
185 }
186
187 /* We use a temporary STACK so we can chop and hack at it */
188 if (ctx->untrusted != NULL &&
189 (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
190 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
191 goto end;
192 }
193
194 num = sk_X509_num(ctx->chain);
195 x = sk_X509_value(ctx->chain, num - 1);
196 depth = param->depth;
197
198 for (;;) {
199 /* If we have enough, we break */
200 if (depth < num)
201 break; /* FIXME: If this happens, we should take
202 * note of it and, if appropriate, use the
203 * X509_V_ERR_CERT_CHAIN_TOO_LONG error
204 * code later.
205 */
206
207 /* If we are self signed, we break */
208 if (ctx->check_issued(ctx, x, x))
209 break;
210
211 /* If we were passed a cert chain, use it first */
212 if (ctx->untrusted != NULL) {
213 xtmp = find_issuer(ctx, sktmp, x);
214 if (xtmp != NULL) {
215 if (!sk_X509_push(ctx->chain, xtmp)) {
216 X509err(X509_F_X509_VERIFY_CERT,
217 ERR_R_MALLOC_FAILURE);
218 goto end;
219 }
220 CRYPTO_add(&xtmp->references, 1,
221 CRYPTO_LOCK_X509);
222 (void)sk_X509_delete_ptr(sktmp, xtmp);
223 ctx->last_untrusted++;
224 x = xtmp;
225 num++;
226 /* reparse the full chain for
227 * the next one */
228 continue;
229 }
230 }
231 break;
232 }
233 sk_X509_free(sktmp);
234 sktmp = NULL;
235
236 /* at this point, chain should contain a list of untrusted
237 * certificates. We now need to add at least one trusted one,
238 * if possible, otherwise we complain. */
239
240 /* Examine last certificate in chain and see if it
241 * is self signed.
242 */
243
244 i = sk_X509_num(ctx->chain);
245 x = sk_X509_value(ctx->chain, i - 1);
246 if (ctx->check_issued(ctx, x, x)) {
247 /* we have a self signed certificate */
248 if (sk_X509_num(ctx->chain) == 1) {
249 /* We have a single self signed certificate: see if
250 * we can find it in the store. We must have an exact
251 * match to avoid possible impersonation.
252 */
253 ok = ctx->get_issuer(&xtmp, ctx, x);
254 if ((ok <= 0) || X509_cmp(x, xtmp)) {
255 ctx->error =
256 X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
257 ctx->current_cert = x;
258 ctx->error_depth = i - 1;
259 if (ok == 1)
260 X509_free(xtmp);
261 bad_chain = 1;
262 ok = cb(0, ctx);
263 if (!ok)
264 goto end;
265 } else {
266 /* We have a match: replace certificate with store version
267 * so we get any trust settings.
268 */
269 X509_free(x);
270 x = xtmp;
271 (void)sk_X509_set(ctx->chain, i - 1, x);
272 ctx->last_untrusted = 0;
273 }
274 } else {
275 /* extract and save self signed certificate for later use */
276 chain_ss = sk_X509_pop(ctx->chain);
277 ctx->last_untrusted--;
278 num--;
279 x = sk_X509_value(ctx->chain, num - 1);
280 }
281 }
282
283 /* We now lookup certs from the certificate store */
284 for (;;) {
285 /* If we have enough, we break */
286 if (depth < num)
287 break;
288
289 /* If we are self signed, we break */
290 if (ctx->check_issued(ctx, x, x))
291 break;
292
293 ok = ctx->get_issuer(&xtmp, ctx, x);
294 if (ok < 0)
295 return ok;
296 if (ok == 0)
297 break;
298
299 x = xtmp;
300 if (!sk_X509_push(ctx->chain, x)) {
301 X509_free(xtmp);
302 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
303 return 0;
304 }
305 num++;
306 }
307
308 /* we now have our chain, lets check it... */
309
310 /* Is last certificate looked up self signed? */
311 if (!ctx->check_issued(ctx, x, x)) {
312 if ((chain_ss == NULL) ||
313 !ctx->check_issued(ctx, x, chain_ss)) {
314 if (ctx->last_untrusted >= num)
315 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
316 else
317 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
318 ctx->current_cert = x;
319 } else {
320
321 if (!sk_X509_push(ctx->chain, chain_ss)) {
322 X509_free(chain_ss);
323 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
324 return 0;
325 }
326 num++;
327 ctx->last_untrusted = num;
328 ctx->current_cert = chain_ss;
329 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
330 chain_ss = NULL;
331 }
332
333 ctx->error_depth = num - 1;
334 bad_chain = 1;
335 ok = cb(0, ctx);
336 if (!ok)
337 goto end;
338 }
339
340 /* We have the chain complete: now we need to check its purpose */
341 ok = check_chain_extensions(ctx);
342
343 if (!ok)
344 goto end;
345
346 /* Check name constraints */
347
348 ok = check_name_constraints(ctx);
349
350 if (!ok)
351 goto end;
352
353 /* The chain extensions are OK: check trust */
354
355 if (param->trust > 0)
356 ok = check_trust(ctx);
357
358 if (!ok)
359 goto end;
360
361 /* We may as well copy down any DSA parameters that are required */
362 X509_get_pubkey_parameters(NULL, ctx->chain);
363
364 /* Check revocation status: we do this after copying parameters
365 * because they may be needed for CRL signature verification.
366 */
367
368 ok = ctx->check_revocation(ctx);
369 if (!ok)
370 goto end;
371
372 /* At this point, we have a chain and need to verify it */
373 if (ctx->verify != NULL)
374 ok = ctx->verify(ctx);
375 else
376 ok = internal_verify(ctx);
377 if (!ok)
378 goto end;
379
380 /* If we get this far evaluate policies */
381 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
382 ok = ctx->check_policy(ctx);
383 if (!ok)
384 goto end;
385 if (0) {
386end:
387 X509_get_pubkey_parameters(NULL, ctx->chain);
388 }
389 if (sktmp != NULL)
390 sk_X509_free(sktmp);
391 X509_free(chain_ss);
392 return ok;
393}
394
395
396/* Given a STACK_OF(X509) find the issuer of cert (if any)
397 */
398
399static X509 *
400find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
401{
402 int i;
403 X509 *issuer, *rv = NULL;
404
405 for (i = 0; i < sk_X509_num(sk); i++) {
406 issuer = sk_X509_value(sk, i);
407 if (ctx->check_issued(ctx, x, issuer)) {
408 rv = issuer;
409 if (x509_check_cert_time(ctx, rv, 1))
410 break;
411 }
412 }
413 return rv;
414}
415
416/* Given a possible certificate and issuer check them */
417
418static int
419check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
420{
421 int ret;
422
423 ret = X509_check_issued(issuer, x);
424 if (ret == X509_V_OK)
425 return 1;
426 /* If we haven't asked for issuer errors don't set ctx */
427 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
428 return 0;
429
430 ctx->error = ret;
431 ctx->current_cert = x;
432 ctx->current_issuer = issuer;
433 return ctx->verify_cb(0, ctx);
434}
435
436/* Alternative lookup method: look from a STACK stored in other_ctx */
437
438static int
439get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
440{
441 *issuer = find_issuer(ctx, ctx->other_ctx, x);
442 if (*issuer) {
443 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
444 return 1;
445 } else
446 return 0;
447}
448
449/* Check a certificate chains extensions for consistency
450 * with the supplied purpose
451 */
452
453static int
454check_chain_extensions(X509_STORE_CTX *ctx)
455{
456#ifdef OPENSSL_NO_CHAIN_VERIFY
457 return 1;
458#else
459 int i, ok = 0, must_be_ca, plen = 0;
460 X509 *x;
461 int (*cb)(int xok, X509_STORE_CTX *xctx);
462 int proxy_path_length = 0;
463 int purpose;
464 int allow_proxy_certs;
465
466 cb = ctx->verify_cb;
467
468 /* must_be_ca can have 1 of 3 values:
469 -1: we accept both CA and non-CA certificates, to allow direct
470 use of self-signed certificates (which are marked as CA).
471 0: we only accept non-CA certificates. This is currently not
472 used, but the possibility is present for future extensions.
473 1: we only accept CA certificates. This is currently used for
474 all certificates in the chain except the leaf certificate.
475 */
476 must_be_ca = -1;
477
478 /* CRL path validation */
479 if (ctx->parent) {
480 allow_proxy_certs = 0;
481 purpose = X509_PURPOSE_CRL_SIGN;
482 } else {
483 allow_proxy_certs =
484 !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
485 purpose = ctx->param->purpose;
486 }
487
488 /* Check all untrusted certificates */
489 for (i = 0; i < ctx->last_untrusted; i++) {
490 int ret;
491 x = sk_X509_value(ctx->chain, i);
492 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
493 (x->ex_flags & EXFLAG_CRITICAL)) {
494 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
495 ctx->error_depth = i;
496 ctx->current_cert = x;
497 ok = cb(0, ctx);
498 if (!ok)
499 goto end;
500 }
501 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
502 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
503 ctx->error_depth = i;
504 ctx->current_cert = x;
505 ok = cb(0, ctx);
506 if (!ok)
507 goto end;
508 }
509 ret = X509_check_ca(x);
510 switch (must_be_ca) {
511 case -1:
512 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
513 (ret != 1) && (ret != 0)) {
514 ret = 0;
515 ctx->error = X509_V_ERR_INVALID_CA;
516 } else
517 ret = 1;
518 break;
519 case 0:
520 if (ret != 0) {
521 ret = 0;
522 ctx->error = X509_V_ERR_INVALID_NON_CA;
523 } else
524 ret = 1;
525 break;
526 default:
527 if ((ret == 0) ||
528 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
529 (ret != 1))) {
530 ret = 0;
531 ctx->error = X509_V_ERR_INVALID_CA;
532 } else
533 ret = 1;
534 break;
535 }
536 if (ret == 0) {
537 ctx->error_depth = i;
538 ctx->current_cert = x;
539 ok = cb(0, ctx);
540 if (!ok)
541 goto end;
542 }
543 if (ctx->param->purpose > 0) {
544 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
545 if ((ret == 0) ||
546 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
547 (ret != 1))) {
548 ctx->error = X509_V_ERR_INVALID_PURPOSE;
549 ctx->error_depth = i;
550 ctx->current_cert = x;
551 ok = cb(0, ctx);
552 if (!ok)
553 goto end;
554 }
555 }
556 /* Check pathlen if not self issued */
557 if ((i > 1) && !(x->ex_flags & EXFLAG_SI) &&
558 (x->ex_pathlen != -1) &&
559 (plen > (x->ex_pathlen + proxy_path_length + 1))) {
560 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
561 ctx->error_depth = i;
562 ctx->current_cert = x;
563 ok = cb(0, ctx);
564 if (!ok)
565 goto end;
566 }
567 /* Increment path length if not self issued */
568 if (!(x->ex_flags & EXFLAG_SI))
569 plen++;
570 /* If this certificate is a proxy certificate, the next
571 certificate must be another proxy certificate or a EE
572 certificate. If not, the next certificate must be a
573 CA certificate. */
574 if (x->ex_flags & EXFLAG_PROXY) {
575 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
576 ctx->error =
577 X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
578 ctx->error_depth = i;
579 ctx->current_cert = x;
580 ok = cb(0, ctx);
581 if (!ok)
582 goto end;
583 }
584 proxy_path_length++;
585 must_be_ca = 0;
586 } else
587 must_be_ca = 1;
588 }
589 ok = 1;
590
591end:
592 return ok;
593#endif
594}
595
596static int
597check_name_constraints(X509_STORE_CTX *ctx)
598{
599 X509 *x;
600 int i, j, rv;
601
602 /* Check name constraints for all certificates */
603 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
604 x = sk_X509_value(ctx->chain, i);
605 /* Ignore self issued certs unless last in chain */
606 if (i && (x->ex_flags & EXFLAG_SI))
607 continue;
608 /* Check against constraints for all certificates higher in
609 * chain including trust anchor. Trust anchor not strictly
610 * speaking needed but if it includes constraints it is to be
611 * assumed it expects them to be obeyed.
612 */
613 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
614 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
615 if (nc) {
616 rv = NAME_CONSTRAINTS_check(x, nc);
617 if (rv != X509_V_OK) {
618 ctx->error = rv;
619 ctx->error_depth = i;
620 ctx->current_cert = x;
621 if (!ctx->verify_cb(0, ctx))
622 return 0;
623 }
624 }
625 }
626 }
627 return 1;
628}
629
630static int
631check_trust(X509_STORE_CTX *ctx)
632{
633#ifdef OPENSSL_NO_CHAIN_VERIFY
634 return 1;
635#else
636 int i, ok;
637 X509 *x;
638 int (*cb)(int xok, X509_STORE_CTX *xctx);
639
640 cb = ctx->verify_cb;
641 /* For now just check the last certificate in the chain */
642 i = sk_X509_num(ctx->chain) - 1;
643 x = sk_X509_value(ctx->chain, i);
644 ok = X509_check_trust(x, ctx->param->trust, 0);
645 if (ok == X509_TRUST_TRUSTED)
646 return 1;
647 ctx->error_depth = i;
648 ctx->current_cert = x;
649 if (ok == X509_TRUST_REJECTED)
650 ctx->error = X509_V_ERR_CERT_REJECTED;
651 else
652 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
653 ok = cb(0, ctx);
654 return ok;
655#endif
656}
657
658static int
659check_revocation(X509_STORE_CTX *ctx)
660{
661 int i, last, ok;
662
663 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
664 return 1;
665 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
666 last = sk_X509_num(ctx->chain) - 1;
667 else {
668 /* If checking CRL paths this isn't the EE certificate */
669 if (ctx->parent)
670 return 1;
671 last = 0;
672 }
673 for (i = 0; i <= last; i++) {
674 ctx->error_depth = i;
675 ok = check_cert(ctx);
676 if (!ok)
677 return ok;
678 }
679 return 1;
680}
681
682static int
683check_cert(X509_STORE_CTX *ctx)
684{
685 X509_CRL *crl = NULL, *dcrl = NULL;
686 X509 *x;
687 int ok = 0, cnum;
688 unsigned int last_reasons;
689
690 cnum = ctx->error_depth;
691 x = sk_X509_value(ctx->chain, cnum);
692 ctx->current_cert = x;
693 ctx->current_issuer = NULL;
694 ctx->current_crl_score = 0;
695 ctx->current_reasons = 0;
696 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
697 last_reasons = ctx->current_reasons;
698 /* Try to retrieve relevant CRL */
699 if (ctx->get_crl)
700 ok = ctx->get_crl(ctx, &crl, x);
701 else
702 ok = get_crl_delta(ctx, &crl, &dcrl, x);
703 /* If error looking up CRL, nothing we can do except
704 * notify callback
705 */
706 if (!ok) {
707 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
708 ok = ctx->verify_cb(0, ctx);
709 goto err;
710 }
711 ctx->current_crl = crl;
712 ok = ctx->check_crl(ctx, crl);
713 if (!ok)
714 goto err;
715
716 if (dcrl) {
717 ok = ctx->check_crl(ctx, dcrl);
718 if (!ok)
719 goto err;
720 ok = ctx->cert_crl(ctx, dcrl, x);
721 if (!ok)
722 goto err;
723 } else
724 ok = 1;
725
726 /* Don't look in full CRL if delta reason is removefromCRL */
727 if (ok != 2) {
728 ok = ctx->cert_crl(ctx, crl, x);
729 if (!ok)
730 goto err;
731 }
732
733 ctx->current_crl = NULL;
734 X509_CRL_free(crl);
735 X509_CRL_free(dcrl);
736 crl = NULL;
737 dcrl = NULL;
738 /* If reasons not updated we wont get anywhere by
739 * another iteration, so exit loop.
740 */
741 if (last_reasons == ctx->current_reasons) {
742 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
743 ok = ctx->verify_cb(0, ctx);
744 goto err;
745 }
746 }
747
748err:
749 ctx->current_crl = NULL;
750 X509_CRL_free(crl);
751 X509_CRL_free(dcrl);
752 return ok;
753}
754
755/* Check CRL times against values in X509_STORE_CTX */
756
757static int
758check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
759{
760 time_t *ptime = NULL;
761 int i;
762
763 if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
764 return (1);
765
766 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
767 ptime = &ctx->param->check_time;
768
769 if (notify)
770 ctx->current_crl = crl;
771
772 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
773 if (i == 0) {
774 if (!notify)
775 return 0;
776 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
777 if (!ctx->verify_cb(0, ctx))
778 return 0;
779 }
780
781 if (i > 0) {
782 if (!notify)
783 return 0;
784 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
785 if (!ctx->verify_cb(0, ctx))
786 return 0;
787 }
788
789 if (X509_CRL_get_nextUpdate(crl)) {
790 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
791
792 if (i == 0) {
793 if (!notify)
794 return 0;
795 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
796 if (!ctx->verify_cb(0, ctx))
797 return 0;
798 }
799 /* Ignore expiry of base CRL is delta is valid */
800 if ((i < 0) &&
801 !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
802 if (!notify)
803 return 0;
804 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
805 if (!ctx->verify_cb(0, ctx))
806 return 0;
807 }
808 }
809
810 if (notify)
811 ctx->current_crl = NULL;
812
813 return 1;
814}
815
816static int
817get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
818 X509 **pissuer, int *pscore, unsigned int *preasons,
819 STACK_OF(X509_CRL) *crls)
820{
821 int i, crl_score, best_score = *pscore;
822 unsigned int reasons, best_reasons = 0;
823 X509 *x = ctx->current_cert;
824 X509_CRL *crl, *best_crl = NULL;
825 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
826
827 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
828 crl = sk_X509_CRL_value(crls, i);
829 reasons = *preasons;
830 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
831
832 if (crl_score > best_score) {
833 best_crl = crl;
834 best_crl_issuer = crl_issuer;
835 best_score = crl_score;
836 best_reasons = reasons;
837 }
838 }
839
840 if (best_crl) {
841 if (*pcrl)
842 X509_CRL_free(*pcrl);
843 *pcrl = best_crl;
844 *pissuer = best_crl_issuer;
845 *pscore = best_score;
846 *preasons = best_reasons;
847 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
848 if (*pdcrl) {
849 X509_CRL_free(*pdcrl);
850 *pdcrl = NULL;
851 }
852 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
853 }
854
855 if (best_score >= CRL_SCORE_VALID)
856 return 1;
857
858 return 0;
859}
860
861/* Compare two CRL extensions for delta checking purposes. They should be
862 * both present or both absent. If both present all fields must be identical.
863 */
864
865static int
866crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
867{
868 ASN1_OCTET_STRING *exta, *extb;
869 int i;
870
871 i = X509_CRL_get_ext_by_NID(a, nid, -1);
872 if (i >= 0) {
873 /* Can't have multiple occurrences */
874 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
875 return 0;
876 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
877 } else
878 exta = NULL;
879
880 i = X509_CRL_get_ext_by_NID(b, nid, -1);
881
882 if (i >= 0) {
883 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
884 return 0;
885 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
886 } else
887 extb = NULL;
888
889 if (!exta && !extb)
890 return 1;
891
892 if (!exta || !extb)
893 return 0;
894
895 if (ASN1_OCTET_STRING_cmp(exta, extb))
896 return 0;
897
898 return 1;
899}
900
901/* See if a base and delta are compatible */
902
903static int
904check_delta_base(X509_CRL *delta, X509_CRL *base)
905{
906 /* Delta CRL must be a delta */
907 if (!delta->base_crl_number)
908 return 0;
909 /* Base must have a CRL number */
910 if (!base->crl_number)
911 return 0;
912 /* Issuer names must match */
913 if (X509_NAME_cmp(X509_CRL_get_issuer(base),
914 X509_CRL_get_issuer(delta)))
915 return 0;
916 /* AKID and IDP must match */
917 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
918 return 0;
919 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
920 return 0;
921 /* Delta CRL base number must not exceed Full CRL number. */
922 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
923 return 0;
924 /* Delta CRL number must exceed full CRL number */
925 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
926 return 1;
927 return 0;
928}
929
930/* For a given base CRL find a delta... maybe extend to delta scoring
931 * or retrieve a chain of deltas...
932 */
933
934static void
935get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, X509_CRL *base,
936 STACK_OF(X509_CRL) *crls)
937{
938 X509_CRL *delta;
939 int i;
940
941 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
942 return;
943 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
944 return;
945 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
946 delta = sk_X509_CRL_value(crls, i);
947 if (check_delta_base(delta, base)) {
948 if (check_crl_time(ctx, delta, 0))
949 *pscore |= CRL_SCORE_TIME_DELTA;
950 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
951 *dcrl = delta;
952 return;
953 }
954 }
955 *dcrl = NULL;
956}
957
958/* For a given CRL return how suitable it is for the supplied certificate 'x'.
959 * The return value is a mask of several criteria.
960 * If the issuer is not the certificate issuer this is returned in *pissuer.
961 * The reasons mask is also used to determine if the CRL is suitable: if
962 * no new reasons the CRL is rejected, otherwise reasons is updated.
963 */
964
965static int
966get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, unsigned int *preasons,
967 X509_CRL *crl, X509 *x)
968{
969 int crl_score = 0;
970 unsigned int tmp_reasons = *preasons, crl_reasons;
971
972 /* First see if we can reject CRL straight away */
973
974 /* Invalid IDP cannot be processed */
975 if (crl->idp_flags & IDP_INVALID)
976 return 0;
977 /* Reason codes or indirect CRLs need extended CRL support */
978 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
979 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
980 return 0;
981 } else if (crl->idp_flags & IDP_REASONS) {
982 /* If no new reasons reject */
983 if (!(crl->idp_reasons & ~tmp_reasons))
984 return 0;
985 }
986 /* Don't process deltas at this stage */
987 else if (crl->base_crl_number)
988 return 0;
989 /* If issuer name doesn't match certificate need indirect CRL */
990 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
991 if (!(crl->idp_flags & IDP_INDIRECT))
992 return 0;
993 } else
994 crl_score |= CRL_SCORE_ISSUER_NAME;
995
996 if (!(crl->flags & EXFLAG_CRITICAL))
997 crl_score |= CRL_SCORE_NOCRITICAL;
998
999 /* Check expiry */
1000 if (check_crl_time(ctx, crl, 0))
1001 crl_score |= CRL_SCORE_TIME;
1002
1003 /* Check authority key ID and locate certificate issuer */
1004 crl_akid_check(ctx, crl, pissuer, &crl_score);
1005
1006 /* If we can't locate certificate issuer at this point forget it */
1007
1008 if (!(crl_score & CRL_SCORE_AKID))
1009 return 0;
1010
1011 /* Check cert for matching CRL distribution points */
1012
1013 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1014 /* If no new reasons reject */
1015 if (!(crl_reasons & ~tmp_reasons))
1016 return 0;
1017 tmp_reasons |= crl_reasons;
1018 crl_score |= CRL_SCORE_SCOPE;
1019 }
1020
1021 *preasons = tmp_reasons;
1022
1023 return crl_score;
1024}
1025
1026static void
1027crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
1028 int *pcrl_score)
1029{
1030 X509 *crl_issuer = NULL;
1031 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1032 int cidx = ctx->error_depth;
1033 int i;
1034
1035 if (cidx != sk_X509_num(ctx->chain) - 1)
1036 cidx++;
1037
1038 crl_issuer = sk_X509_value(ctx->chain, cidx);
1039
1040 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1041 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1042 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
1043 *pissuer = crl_issuer;
1044 return;
1045 }
1046 }
1047
1048 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1049 crl_issuer = sk_X509_value(ctx->chain, cidx);
1050 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1051 continue;
1052 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1053 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
1054 *pissuer = crl_issuer;
1055 return;
1056 }
1057 }
1058
1059 /* Anything else needs extended CRL support */
1060
1061 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1062 return;
1063
1064 /* Otherwise the CRL issuer is not on the path. Look for it in the
1065 * set of untrusted certificates.
1066 */
1067 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1068 crl_issuer = sk_X509_value(ctx->untrusted, i);
1069 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1070 continue;
1071 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1072 *pissuer = crl_issuer;
1073 *pcrl_score |= CRL_SCORE_AKID;
1074 return;
1075 }
1076 }
1077}
1078
1079/* Check the path of a CRL issuer certificate. This creates a new
1080 * X509_STORE_CTX and populates it with most of the parameters from the
1081 * parent. This could be optimised somewhat since a lot of path checking
1082 * will be duplicated by the parent, but this will rarely be used in
1083 * practice.
1084 */
1085
1086static int
1087check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1088{
1089 X509_STORE_CTX crl_ctx;
1090 int ret;
1091
1092 /* Don't allow recursive CRL path validation */
1093 if (ctx->parent)
1094 return 0;
1095 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted)) {
1096 ret = -1;
1097 goto err;
1098 }
1099
1100 crl_ctx.crls = ctx->crls;
1101 /* Copy verify params across */
1102 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1103
1104 crl_ctx.parent = ctx;
1105 crl_ctx.verify_cb = ctx->verify_cb;
1106
1107 /* Verify CRL issuer */
1108 ret = X509_verify_cert(&crl_ctx);
1109
1110 if (ret <= 0)
1111 goto err;
1112
1113 /* Check chain is acceptable */
1114 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1115
1116err:
1117 X509_STORE_CTX_cleanup(&crl_ctx);
1118 return ret;
1119}
1120
1121/* RFC3280 says nothing about the relationship between CRL path
1122 * and certificate path, which could lead to situations where a
1123 * certificate could be revoked or validated by a CA not authorised
1124 * to do so. RFC5280 is more strict and states that the two paths must
1125 * end in the same trust anchor, though some discussions remain...
1126 * until this is resolved we use the RFC5280 version
1127 */
1128
1129static int
1130check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
1131 STACK_OF(X509) *crl_path)
1132{
1133 X509 *cert_ta, *crl_ta;
1134
1135 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1136 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1137 if (!X509_cmp(cert_ta, crl_ta))
1138 return 1;
1139 return 0;
1140}
1141
1142/* Check for match between two dist point names: three separate cases.
1143 * 1. Both are relative names and compare X509_NAME types.
1144 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1145 * 3. Both are full names and compare two GENERAL_NAMES.
1146 * 4. One is NULL: automatic match.
1147 */
1148
1149static int
1150idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1151{
1152 X509_NAME *nm = NULL;
1153 GENERAL_NAMES *gens = NULL;
1154 GENERAL_NAME *gena, *genb;
1155 int i, j;
1156
1157 if (!a || !b)
1158 return 1;
1159 if (a->type == 1) {
1160 if (!a->dpname)
1161 return 0;
1162 /* Case 1: two X509_NAME */
1163 if (b->type == 1) {
1164 if (!b->dpname)
1165 return 0;
1166 if (!X509_NAME_cmp(a->dpname, b->dpname))
1167 return 1;
1168 else
1169 return 0;
1170 }
1171 /* Case 2: set name and GENERAL_NAMES appropriately */
1172 nm = a->dpname;
1173 gens = b->name.fullname;
1174 } else if (b->type == 1) {
1175 if (!b->dpname)
1176 return 0;
1177 /* Case 2: set name and GENERAL_NAMES appropriately */
1178 gens = a->name.fullname;
1179 nm = b->dpname;
1180 }
1181
1182 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1183 if (nm) {
1184 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1185 gena = sk_GENERAL_NAME_value(gens, i);
1186 if (gena->type != GEN_DIRNAME)
1187 continue;
1188 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1189 return 1;
1190 }
1191 return 0;
1192 }
1193
1194 /* Else case 3: two GENERAL_NAMES */
1195
1196 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1197 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1198 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1199 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1200 if (!GENERAL_NAME_cmp(gena, genb))
1201 return 1;
1202 }
1203 }
1204
1205 return 0;
1206}
1207
1208static int
1209crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1210{
1211 int i;
1212 X509_NAME *nm = X509_CRL_get_issuer(crl);
1213
1214 /* If no CRLissuer return is successful iff don't need a match */
1215 if (!dp->CRLissuer)
1216 return !!(crl_score & CRL_SCORE_ISSUER_NAME);
1217 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1218 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1219 if (gen->type != GEN_DIRNAME)
1220 continue;
1221 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1222 return 1;
1223 }
1224 return 0;
1225}
1226
1227/* Check CRLDP and IDP */
1228
1229static int
1230crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, unsigned int *preasons)
1231{
1232 int i;
1233
1234 if (crl->idp_flags & IDP_ONLYATTR)
1235 return 0;
1236 if (x->ex_flags & EXFLAG_CA) {
1237 if (crl->idp_flags & IDP_ONLYUSER)
1238 return 0;
1239 } else {
1240 if (crl->idp_flags & IDP_ONLYCA)
1241 return 0;
1242 }
1243 *preasons = crl->idp_reasons;
1244 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1245 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1246 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1247 if (!crl->idp ||
1248 idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1249 *preasons &= dp->dp_reasons;
1250 return 1;
1251 }
1252 }
1253 }
1254 if ((!crl->idp || !crl->idp->distpoint) &&
1255 (crl_score & CRL_SCORE_ISSUER_NAME))
1256 return 1;
1257 return 0;
1258}
1259
1260/* Retrieve CRL corresponding to current certificate.
1261 * If deltas enabled try to find a delta CRL too
1262 */
1263
1264static int
1265get_crl_delta(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1266{
1267 int ok;
1268 X509 *issuer = NULL;
1269 int crl_score = 0;
1270 unsigned int reasons;
1271 X509_CRL *crl = NULL, *dcrl = NULL;
1272 STACK_OF(X509_CRL) *skcrl;
1273 X509_NAME *nm = X509_get_issuer_name(x);
1274
1275 reasons = ctx->current_reasons;
1276 ok = get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons,
1277 ctx->crls);
1278 if (ok)
1279 goto done;
1280
1281 /* Lookup CRLs from store */
1282 skcrl = ctx->lookup_crls(ctx, nm);
1283
1284 /* If no CRLs found and a near match from get_crl_sk use that */
1285 if (!skcrl && crl)
1286 goto done;
1287
1288 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1289
1290 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1291
1292done:
1293
1294 /* If we got any kind of CRL use it and return success */
1295 if (crl) {
1296 ctx->current_issuer = issuer;
1297 ctx->current_crl_score = crl_score;
1298 ctx->current_reasons = reasons;
1299 *pcrl = crl;
1300 *pdcrl = dcrl;
1301 return 1;
1302 }
1303
1304 return 0;
1305}
1306
1307/* Check CRL validity */
1308static int
1309check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1310{
1311 X509 *issuer = NULL;
1312 EVP_PKEY *ikey = NULL;
1313 int ok = 0, chnum, cnum;
1314
1315 cnum = ctx->error_depth;
1316 chnum = sk_X509_num(ctx->chain) - 1;
1317 /* if we have an alternative CRL issuer cert use that */
1318 if (ctx->current_issuer) {
1319 issuer = ctx->current_issuer;
1320 } else if (cnum < chnum) {
1321 /* Else find CRL issuer: if not last certificate then issuer
1322 * is next certificate in chain.
1323 */
1324 issuer = sk_X509_value(ctx->chain, cnum + 1);
1325 } else {
1326 issuer = sk_X509_value(ctx->chain, chnum);
1327 /* If not self signed, can't check signature */
1328 if (!ctx->check_issued(ctx, issuer, issuer)) {
1329 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1330 ok = ctx->verify_cb(0, ctx);
1331 if (!ok)
1332 goto err;
1333 }
1334 }
1335
1336 if (issuer) {
1337 /* Skip most tests for deltas because they have already
1338 * been done
1339 */
1340 if (!crl->base_crl_number) {
1341 /* Check for cRLSign bit if keyUsage present */
1342 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1343 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1344 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1345 ok = ctx->verify_cb(0, ctx);
1346 if (!ok)
1347 goto err;
1348 }
1349
1350 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1351 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1352 ok = ctx->verify_cb(0, ctx);
1353 if (!ok)
1354 goto err;
1355 }
1356
1357 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1358 if (check_crl_path(ctx,
1359 ctx->current_issuer) <= 0) {
1360 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1361 ok = ctx->verify_cb(0, ctx);
1362 if (!ok)
1363 goto err;
1364 }
1365 }
1366
1367 if (crl->idp_flags & IDP_INVALID) {
1368 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1369 ok = ctx->verify_cb(0, ctx);
1370 if (!ok)
1371 goto err;
1372 }
1373
1374
1375 }
1376
1377 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1378 ok = check_crl_time(ctx, crl, 1);
1379 if (!ok)
1380 goto err;
1381 }
1382
1383 /* Attempt to get issuer certificate public key */
1384 ikey = X509_get_pubkey(issuer);
1385
1386 if (!ikey) {
1387 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1388 ok = ctx->verify_cb(0, ctx);
1389 if (!ok)
1390 goto err;
1391 } else {
1392 /* Verify CRL signature */
1393 if (X509_CRL_verify(crl, ikey) <= 0) {
1394 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1395 ok = ctx->verify_cb(0, ctx);
1396 if (!ok)
1397 goto err;
1398 }
1399 }
1400 }
1401
1402 ok = 1;
1403
1404err:
1405 EVP_PKEY_free(ikey);
1406 return ok;
1407}
1408
1409/* Check certificate against CRL */
1410static int
1411cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1412{
1413 int ok;
1414 X509_REVOKED *rev;
1415
1416 /* The rules changed for this... previously if a CRL contained
1417 * unhandled critical extensions it could still be used to indicate
1418 * a certificate was revoked. This has since been changed since
1419 * critical extension can change the meaning of CRL entries.
1420 */
1421 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1422 (crl->flags & EXFLAG_CRITICAL)) {
1423 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1424 ok = ctx->verify_cb(0, ctx);
1425 if (!ok)
1426 return 0;
1427 }
1428 /* Look for serial number of certificate in CRL
1429 * If found make sure reason is not removeFromCRL.
1430 */
1431 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1432 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1433 return 2;
1434 ctx->error = X509_V_ERR_CERT_REVOKED;
1435 ok = ctx->verify_cb(0, ctx);
1436 if (!ok)
1437 return 0;
1438 }
1439
1440 return 1;
1441}
1442
1443static int
1444check_policy(X509_STORE_CTX *ctx)
1445{
1446 int ret;
1447
1448 if (ctx->parent)
1449 return 1;
1450 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1451 ctx->param->policies, ctx->param->flags);
1452 if (ret == 0) {
1453 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1454 return 0;
1455 }
1456 /* Invalid or inconsistent extensions */
1457 if (ret == -1) {
1458 /* Locate certificates with bad extensions and notify
1459 * callback.
1460 */
1461 X509 *x;
1462 int i;
1463 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1464 x = sk_X509_value(ctx->chain, i);
1465 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1466 continue;
1467 ctx->current_cert = x;
1468 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1469 if (!ctx->verify_cb(0, ctx))
1470 return 0;
1471 }
1472 return 1;
1473 }
1474 if (ret == -2) {
1475 ctx->current_cert = NULL;
1476 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1477 return ctx->verify_cb(0, ctx);
1478 }
1479
1480 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1481 ctx->current_cert = NULL;
1482 ctx->error = X509_V_OK;
1483 if (!ctx->verify_cb(2, ctx))
1484 return 0;
1485 }
1486
1487 return 1;
1488}
1489
1490int
1491x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1492{
1493 time_t *ptime = NULL;
1494 int i;
1495
1496 if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1497 return (1);
1498
1499 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1500 ptime = &ctx->param->check_time;
1501
1502 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1503 if (i == 0) {
1504 if (quiet)
1505 return 0;
1506 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1507 ctx->current_cert = x;
1508 if (!ctx->verify_cb(0, ctx))
1509 return 0;
1510 }
1511
1512 if (i > 0) {
1513 if (quiet)
1514 return 0;
1515 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1516 ctx->current_cert = x;
1517 if (!ctx->verify_cb(0, ctx))
1518 return 0;
1519 }
1520
1521 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1522 if (i == 0) {
1523 if (quiet)
1524 return 0;
1525 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1526 ctx->current_cert = x;
1527 if (!ctx->verify_cb(0, ctx))
1528 return 0;
1529 }
1530
1531 if (i < 0) {
1532 if (quiet)
1533 return 0;
1534 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1535 ctx->current_cert = x;
1536 if (!ctx->verify_cb(0, ctx))
1537 return 0;
1538 }
1539
1540 return 1;
1541}
1542
1543static int
1544internal_verify(X509_STORE_CTX *ctx)
1545{
1546 int ok = 0, n;
1547 X509 *xs, *xi;
1548 EVP_PKEY *pkey = NULL;
1549 int (*cb)(int xok, X509_STORE_CTX *xctx);
1550
1551 cb = ctx->verify_cb;
1552
1553 n = sk_X509_num(ctx->chain);
1554 ctx->error_depth = n - 1;
1555 n--;
1556 xi = sk_X509_value(ctx->chain, n);
1557
1558 if (ctx->check_issued(ctx, xi, xi))
1559 xs = xi;
1560 else {
1561 if (n <= 0) {
1562 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1563 ctx->current_cert = xi;
1564 ok = cb(0, ctx);
1565 goto end;
1566 } else {
1567 n--;
1568 ctx->error_depth = n;
1569 xs = sk_X509_value(ctx->chain, n);
1570 }
1571 }
1572
1573/* ctx->error=0; not needed */
1574 while (n >= 0) {
1575 ctx->error_depth = n;
1576
1577 /* Skip signature check for self signed certificates unless
1578 * explicitly asked for. It doesn't add any security and
1579 * just wastes time.
1580 */
1581 if (!xs->valid && (xs != xi ||
1582 (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1583 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1584 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1585 ctx->current_cert = xi;
1586 ok = (*cb)(0, ctx);
1587 if (!ok)
1588 goto end;
1589 } else if (X509_verify(xs, pkey) <= 0) {
1590 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1591 ctx->current_cert = xs;
1592 ok = (*cb)(0, ctx);
1593 if (!ok) {
1594 EVP_PKEY_free(pkey);
1595 goto end;
1596 }
1597 }
1598 EVP_PKEY_free(pkey);
1599 pkey = NULL;
1600 }
1601
1602 xs->valid = 1;
1603
1604 ok = x509_check_cert_time(ctx, xs, 0);
1605 if (!ok)
1606 goto end;
1607
1608 /* The last error (if any) is still in the error value */
1609 ctx->current_issuer = xi;
1610 ctx->current_cert = xs;
1611 ok = (*cb)(1, ctx);
1612 if (!ok)
1613 goto end;
1614
1615 n--;
1616 if (n >= 0) {
1617 xi = xs;
1618 xs = sk_X509_value(ctx->chain, n);
1619 }
1620 }
1621 ok = 1;
1622
1623end:
1624 return ok;
1625}
1626
1627int
1628X509_cmp_current_time(const ASN1_TIME *ctm)
1629{
1630 return X509_cmp_time(ctm, NULL);
1631}
1632
1633/*
1634 * Compare a possibly unvalidated ASN1_TIME string against a time_t
1635 * using RFC 5280 rules for the time string. If *cmp_time is NULL
1636 * the current system time is used.
1637 *
1638 * XXX NOTE that unlike what you expect a "cmp" function to do in C,
1639 * XXX this one is "special", and returns 0 for error.
1640 *
1641 * Returns:
1642 * -1 if the ASN1_time is earlier than OR the same as *cmp_time.
1643 * 1 if the ASN1_time is later than *cmp_time.
1644 * 0 on error.
1645 */
1646int
1647X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1648{
1649 time_t time1, time2;
1650 struct tm tm1, tm2;
1651 int ret = 0;
1652 int type;
1653
1654 if (cmp_time == NULL)
1655 time2 = time(NULL);
1656 else
1657 time2 = *cmp_time;
1658
1659 memset(&tm1, 0, sizeof(tm1));
1660
1661 if ((type = asn1_time_parse(ctm->data, ctm->length, &tm1, 0)) == -1)
1662 goto out; /* invalid time */
1663
1664 /* RFC 5280 section 4.1.2.5 */
1665 if (tm1.tm_year < 150 && type != V_ASN1_UTCTIME)
1666 goto out;
1667 if (tm1.tm_year >= 150 && type != V_ASN1_GENERALIZEDTIME)
1668 goto out;
1669
1670 /*
1671 * Defensively fail if the time string is not representable as
1672 * a time_t. A time_t must be sane if you care about times after
1673 * Jan 19 2038.
1674 */
1675 if ((time1 = timegm(&tm1)) == -1)
1676 goto out;
1677
1678 if (gmtime_r(&time2, &tm2) == NULL)
1679 goto out;
1680
1681 ret = asn1_tm_cmp(&tm1, &tm2);
1682 if (ret == 0)
1683 ret = -1; /* 0 is used for error, so map same to less than */
1684 out:
1685 return (ret);
1686}
1687
1688ASN1_TIME *
1689X509_gmtime_adj(ASN1_TIME *s, long adj)
1690{
1691 return X509_time_adj(s, adj, NULL);
1692}
1693
1694ASN1_TIME *
1695X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_time)
1696{
1697 return X509_time_adj_ex(s, 0, offset_sec, in_time);
1698}
1699
1700ASN1_TIME *
1701X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, time_t *in_time)
1702{
1703 time_t t;
1704 if (in_time == NULL)
1705 t = time(NULL);
1706 else
1707 t = *in_time;
1708
1709 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1710}
1711
1712int
1713X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1714{
1715 EVP_PKEY *ktmp = NULL, *ktmp2;
1716 int i, j;
1717
1718 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1719 return 1;
1720
1721 for (i = 0; i < sk_X509_num(chain); i++) {
1722 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1723 if (ktmp == NULL) {
1724 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1725 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1726 return 0;
1727 }
1728 if (!EVP_PKEY_missing_parameters(ktmp))
1729 break;
1730 else {
1731 EVP_PKEY_free(ktmp);
1732 ktmp = NULL;
1733 }
1734 }
1735 if (ktmp == NULL) {
1736 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1737 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1738 return 0;
1739 }
1740
1741 /* first, populate the other certs */
1742 for (j = i - 1; j >= 0; j--) {
1743 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1744 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1745 EVP_PKEY_free(ktmp2);
1746 }
1747
1748 if (pkey != NULL)
1749 EVP_PKEY_copy_parameters(pkey, ktmp);
1750 EVP_PKEY_free(ktmp);
1751 return 1;
1752}
1753
1754int
1755X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1756 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1757{
1758 /* This function is (usually) called only once, by
1759 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
1760 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
1761 argl, argp, new_func, dup_func, free_func);
1762}
1763
1764int
1765X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1766{
1767 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1768}
1769
1770void *
1771X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1772{
1773 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1774}
1775
1776int
1777X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1778{
1779 return ctx->error;
1780}
1781
1782void
1783X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1784{
1785 ctx->error = err;
1786}
1787
1788int
1789X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1790{
1791 return ctx->error_depth;
1792}
1793
1794X509 *
1795X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1796{
1797 return ctx->current_cert;
1798}
1799
1800STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1801{
1802 return ctx->chain;
1803}
1804
1805STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1806{
1807 int i;
1808 X509 *x;
1809 STACK_OF(X509) *chain;
1810
1811 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
1812 return NULL;
1813 for (i = 0; i < sk_X509_num(chain); i++) {
1814 x = sk_X509_value(chain, i);
1815 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1816 }
1817 return chain;
1818}
1819
1820X509 *
1821X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1822{
1823 return ctx->current_issuer;
1824}
1825
1826X509_CRL *
1827X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1828{
1829 return ctx->current_crl;
1830}
1831
1832X509_STORE_CTX *
1833X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1834{
1835 return ctx->parent;
1836}
1837
1838void
1839X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1840{
1841 ctx->cert = x;
1842}
1843
1844void
1845X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1846{
1847 ctx->untrusted = sk;
1848}
1849
1850void
1851X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1852{
1853 ctx->crls = sk;
1854}
1855
1856int
1857X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1858{
1859 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1860}
1861
1862int
1863X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1864{
1865 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1866}
1867
1868/* This function is used to set the X509_STORE_CTX purpose and trust
1869 * values. This is intended to be used when another structure has its
1870 * own trust and purpose values which (if set) will be inherited by
1871 * the ctx. If they aren't set then we will usually have a default
1872 * purpose in mind which should then be used to set the trust value.
1873 * An example of this is SSL use: an SSL structure will have its own
1874 * purpose and trust settings which the application can set: if they
1875 * aren't set then we use the default of SSL client/server.
1876 */
1877
1878int
1879X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1880 int purpose, int trust)
1881{
1882 int idx;
1883
1884 /* If purpose not set use default */
1885 if (!purpose)
1886 purpose = def_purpose;
1887 /* If we have a purpose then check it is valid */
1888 if (purpose) {
1889 X509_PURPOSE *ptmp;
1890 idx = X509_PURPOSE_get_by_id(purpose);
1891 if (idx == -1) {
1892 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1893 X509_R_UNKNOWN_PURPOSE_ID);
1894 return 0;
1895 }
1896 ptmp = X509_PURPOSE_get0(idx);
1897 if (ptmp->trust == X509_TRUST_DEFAULT) {
1898 idx = X509_PURPOSE_get_by_id(def_purpose);
1899 if (idx == -1) {
1900 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1901 X509_R_UNKNOWN_PURPOSE_ID);
1902 return 0;
1903 }
1904 ptmp = X509_PURPOSE_get0(idx);
1905 }
1906 /* If trust not set then get from purpose default */
1907 if (!trust)
1908 trust = ptmp->trust;
1909 }
1910 if (trust) {
1911 idx = X509_TRUST_get_by_id(trust);
1912 if (idx == -1) {
1913 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1914 X509_R_UNKNOWN_TRUST_ID);
1915 return 0;
1916 }
1917 }
1918
1919 if (purpose && !ctx->param->purpose)
1920 ctx->param->purpose = purpose;
1921 if (trust && !ctx->param->trust)
1922 ctx->param->trust = trust;
1923 return 1;
1924}
1925
1926X509_STORE_CTX *
1927X509_STORE_CTX_new(void)
1928{
1929 X509_STORE_CTX *ctx;
1930
1931 ctx = calloc(1, sizeof(X509_STORE_CTX));
1932 if (!ctx) {
1933 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
1934 return NULL;
1935 }
1936 return ctx;
1937}
1938
1939void
1940X509_STORE_CTX_free(X509_STORE_CTX *ctx)
1941{
1942 if (ctx == NULL)
1943 return;
1944
1945 X509_STORE_CTX_cleanup(ctx);
1946 free(ctx);
1947}
1948
1949int
1950X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1951 STACK_OF(X509) *chain)
1952{
1953 int param_ret = 1;
1954
1955 /*
1956 * Make sure everything is initialized properly even in case of an
1957 * early return due to an error.
1958 *
1959 * While this 'ctx' can be reused, X509_STORE_CTX_cleanup() will have
1960 * freed everything and memset ex_data anyway. This also allows us
1961 * to safely use X509_STORE_CTX variables from the stack which will
1962 * have uninitialized data.
1963 */
1964 memset(ctx, 0, sizeof(*ctx));
1965
1966 /*
1967 * Set values other than 0. Keep this in the same order as
1968 * X509_STORE_CTX except for values that may fail. All fields that
1969 * may fail should go last to make sure 'ctx' is as consistent as
1970 * possible even on early exits.
1971 */
1972 ctx->ctx = store;
1973 ctx->cert = x509;
1974 ctx->untrusted = chain;
1975
1976 if (store && store->verify)
1977 ctx->verify = store->verify;
1978 else
1979 ctx->verify = internal_verify;
1980
1981 if (store && store->verify_cb)
1982 ctx->verify_cb = store->verify_cb;
1983 else
1984 ctx->verify_cb = null_callback;
1985
1986 if (store && store->get_issuer)
1987 ctx->get_issuer = store->get_issuer;
1988 else
1989 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
1990
1991 if (store && store->check_issued)
1992 ctx->check_issued = store->check_issued;
1993 else
1994 ctx->check_issued = check_issued;
1995
1996 if (store && store->check_revocation)
1997 ctx->check_revocation = store->check_revocation;
1998 else
1999 ctx->check_revocation = check_revocation;
2000
2001 if (store && store->get_crl)
2002 ctx->get_crl = store->get_crl;
2003 else
2004 ctx->get_crl = NULL;
2005
2006 if (store && store->check_crl)
2007 ctx->check_crl = store->check_crl;
2008 else
2009 ctx->check_crl = check_crl;
2010
2011 if (store && store->cert_crl)
2012 ctx->cert_crl = store->cert_crl;
2013 else
2014 ctx->cert_crl = cert_crl;
2015
2016 ctx->check_policy = check_policy;
2017
2018 if (store && store->lookup_certs)
2019 ctx->lookup_certs = store->lookup_certs;
2020 else
2021 ctx->lookup_certs = X509_STORE_get1_certs;
2022
2023 if (store && store->lookup_crls)
2024 ctx->lookup_crls = store->lookup_crls;
2025 else
2026 ctx->lookup_crls = X509_STORE_get1_crls;
2027
2028 if (store && store->cleanup)
2029 ctx->cleanup = store->cleanup;
2030 else
2031 ctx->cleanup = NULL;
2032
2033 ctx->param = X509_VERIFY_PARAM_new();
2034 if (!ctx->param) {
2035 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2036 return 0;
2037 }
2038
2039 /* Inherit callbacks and flags from X509_STORE if not set
2040 * use defaults.
2041 */
2042 if (store)
2043 param_ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2044 else
2045 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
2046
2047 if (param_ret)
2048 param_ret = X509_VERIFY_PARAM_inherit(ctx->param,
2049 X509_VERIFY_PARAM_lookup("default"));
2050
2051 if (param_ret == 0) {
2052 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2053 return 0;
2054 }
2055
2056 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2057 &(ctx->ex_data)) == 0) {
2058 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2059 return 0;
2060 }
2061 return 1;
2062}
2063
2064/* Set alternative lookup method: just a STACK of trusted certificates.
2065 * This avoids X509_STORE nastiness where it isn't needed.
2066 */
2067
2068void
2069X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2070{
2071 ctx->other_ctx = sk;
2072 ctx->get_issuer = get_issuer_sk;
2073}
2074
2075void
2076X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2077{
2078 if (ctx->cleanup)
2079 ctx->cleanup(ctx);
2080 if (ctx->param != NULL) {
2081 if (ctx->parent == NULL)
2082 X509_VERIFY_PARAM_free(ctx->param);
2083 ctx->param = NULL;
2084 }
2085 if (ctx->tree != NULL) {
2086 X509_policy_tree_free(ctx->tree);
2087 ctx->tree = NULL;
2088 }
2089 if (ctx->chain != NULL) {
2090 sk_X509_pop_free(ctx->chain, X509_free);
2091 ctx->chain = NULL;
2092 }
2093 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX,
2094 ctx, &(ctx->ex_data));
2095 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2096}
2097
2098void
2099X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2100{
2101 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2102}
2103
2104void
2105X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2106{
2107 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2108}
2109
2110void
2111X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
2112{
2113 X509_VERIFY_PARAM_set_time(ctx->param, t);
2114}
2115
2116void
2117X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2118 int (*verify_cb)(int, X509_STORE_CTX *))
2119{
2120 ctx->verify_cb = verify_cb;
2121}
2122
2123X509_POLICY_TREE *
2124X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2125{
2126 return ctx->tree;
2127}
2128
2129int
2130X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2131{
2132 return ctx->explicit_policy;
2133}
2134
2135int
2136X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2137{
2138 const X509_VERIFY_PARAM *param;
2139 param = X509_VERIFY_PARAM_lookup(name);
2140 if (!param)
2141 return 0;
2142 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2143}
2144
2145X509_VERIFY_PARAM *
2146X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2147{
2148 return ctx->param;
2149}
2150
2151void
2152X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2153{
2154 if (ctx->param)
2155 X509_VERIFY_PARAM_free(ctx->param);
2156 ctx->param = param;
2157}
diff --git a/src/lib/libcrypto/x509/x509_vfy.h b/src/lib/libcrypto/x509/x509_vfy.h
deleted file mode 100644
index e3a1db2407..0000000000
--- a/src/lib/libcrypto/x509/x509_vfy.h
+++ /dev/null
@@ -1,560 +0,0 @@
1/* $OpenBSD: x509_vfy.h,v 1.16 2015/09/14 16:13:39 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_X509_H
60#include <openssl/x509.h>
61/* openssl/x509.h ends up #include-ing this file at about the only
62 * appropriate moment. */
63#endif
64
65#ifndef HEADER_X509_VFY_H
66#define HEADER_X509_VFY_H
67
68#include <openssl/opensslconf.h>
69
70#ifndef OPENSSL_NO_LHASH
71#include <openssl/lhash.h>
72#endif
73#include <openssl/bio.h>
74#include <openssl/crypto.h>
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79
80typedef struct x509_file_st
81 {
82 int num_paths; /* number of paths to files or directories */
83 int num_alloced;
84 char **paths; /* the list of paths or directories */
85 int *path_type;
86 } X509_CERT_FILE_CTX;
87
88/*******************************/
89/*
90SSL_CTX -> X509_STORE
91 -> X509_LOOKUP
92 ->X509_LOOKUP_METHOD
93 -> X509_LOOKUP
94 ->X509_LOOKUP_METHOD
95
96SSL -> X509_STORE_CTX
97 ->X509_STORE
98
99The X509_STORE holds the tables etc for verification stuff.
100A X509_STORE_CTX is used while validating a single certificate.
101The X509_STORE has X509_LOOKUPs for looking up certs.
102The X509_STORE then calls a function to actually verify the
103certificate chain.
104*/
105
106#define X509_LU_RETRY -1
107#define X509_LU_FAIL 0
108#define X509_LU_X509 1
109#define X509_LU_CRL 2
110#define X509_LU_PKEY 3
111
112typedef struct x509_object_st
113 {
114 /* one of the above types */
115 int type;
116 union {
117 char *ptr;
118 X509 *x509;
119 X509_CRL *crl;
120 EVP_PKEY *pkey;
121 } data;
122 } X509_OBJECT;
123
124typedef struct x509_lookup_st X509_LOOKUP;
125
126DECLARE_STACK_OF(X509_LOOKUP)
127DECLARE_STACK_OF(X509_OBJECT)
128
129/* This is a static that defines the function interface */
130typedef struct x509_lookup_method_st
131 {
132 const char *name;
133 int (*new_item)(X509_LOOKUP *ctx);
134 void (*free)(X509_LOOKUP *ctx);
135 int (*init)(X509_LOOKUP *ctx);
136 int (*shutdown)(X509_LOOKUP *ctx);
137 int (*ctrl)(X509_LOOKUP *ctx,int cmd,const char *argc,long argl,
138 char **ret);
139 int (*get_by_subject)(X509_LOOKUP *ctx,int type,X509_NAME *name,
140 X509_OBJECT *ret);
141 int (*get_by_issuer_serial)(X509_LOOKUP *ctx,int type,X509_NAME *name,
142 ASN1_INTEGER *serial,X509_OBJECT *ret);
143 int (*get_by_fingerprint)(X509_LOOKUP *ctx,int type,
144 unsigned char *bytes,int len,
145 X509_OBJECT *ret);
146 int (*get_by_alias)(X509_LOOKUP *ctx,int type,char *str,int len,
147 X509_OBJECT *ret);
148 } X509_LOOKUP_METHOD;
149
150/* This structure hold all parameters associated with a verify operation
151 * by including an X509_VERIFY_PARAM structure in related structures the
152 * parameters used can be customized
153 */
154
155typedef struct X509_VERIFY_PARAM_st
156 {
157 char *name;
158 time_t check_time; /* Time to use */
159 unsigned long inh_flags; /* Inheritance flags */
160 unsigned long flags; /* Various verify flags */
161 int purpose; /* purpose to check untrusted certificates */
162 int trust; /* trust setting to check */
163 int depth; /* Verify depth */
164 STACK_OF(ASN1_OBJECT) *policies; /* Permissible policies */
165 } X509_VERIFY_PARAM;
166
167DECLARE_STACK_OF(X509_VERIFY_PARAM)
168
169/* This is used to hold everything. It is used for all certificate
170 * validation. Once we have a certificate chain, the 'verify'
171 * function is then called to actually check the cert chain. */
172struct x509_store_st
173 {
174 /* The following is a cache of trusted certs */
175 int cache; /* if true, stash any hits */
176 STACK_OF(X509_OBJECT) *objs; /* Cache of all objects */
177
178 /* These are external lookup methods */
179 STACK_OF(X509_LOOKUP) *get_cert_methods;
180
181 X509_VERIFY_PARAM *param;
182
183 /* Callbacks for various operations */
184 int (*verify)(X509_STORE_CTX *ctx); /* called to verify a certificate */
185 int (*verify_cb)(int ok,X509_STORE_CTX *ctx); /* error callback */
186 int (*get_issuer)(X509 **issuer, X509_STORE_CTX *ctx, X509 *x); /* get issuers cert from ctx */
187 int (*check_issued)(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); /* check issued */
188 int (*check_revocation)(X509_STORE_CTX *ctx); /* Check revocation status of chain */
189 int (*get_crl)(X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x); /* retrieve CRL */
190 int (*check_crl)(X509_STORE_CTX *ctx, X509_CRL *crl); /* Check CRL validity */
191 int (*cert_crl)(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); /* Check certificate against CRL */
192 STACK_OF(X509) * (*lookup_certs)(X509_STORE_CTX *ctx, X509_NAME *nm);
193 STACK_OF(X509_CRL) * (*lookup_crls)(X509_STORE_CTX *ctx, X509_NAME *nm);
194 int (*cleanup)(X509_STORE_CTX *ctx);
195
196 CRYPTO_EX_DATA ex_data;
197 int references;
198 } /* X509_STORE */;
199
200int X509_STORE_set_depth(X509_STORE *store, int depth);
201
202#define X509_STORE_set_verify_cb_func(ctx,func) ((ctx)->verify_cb=(func))
203#define X509_STORE_set_verify_func(ctx,func) ((ctx)->verify=(func))
204
205/* This is the functions plus an instance of the local variables. */
206struct x509_lookup_st
207 {
208 int init; /* have we been started */
209 int skip; /* don't use us. */
210 X509_LOOKUP_METHOD *method; /* the functions */
211 char *method_data; /* method data */
212
213 X509_STORE *store_ctx; /* who owns us */
214 } /* X509_LOOKUP */;
215
216/* This is a used when verifying cert chains. Since the
217 * gathering of the cert chain can take some time (and have to be
218 * 'retried', this needs to be kept and passed around. */
219struct x509_store_ctx_st /* X509_STORE_CTX */
220 {
221 X509_STORE *ctx;
222 int current_method; /* used when looking up certs */
223
224 /* The following are set by the caller */
225 X509 *cert; /* The cert to check */
226 STACK_OF(X509) *untrusted; /* chain of X509s - untrusted - passed in */
227 STACK_OF(X509_CRL) *crls; /* set of CRLs passed in */
228
229 X509_VERIFY_PARAM *param;
230 void *other_ctx; /* Other info for use with get_issuer() */
231
232 /* Callbacks for various operations */
233 int (*verify)(X509_STORE_CTX *ctx); /* called to verify a certificate */
234 int (*verify_cb)(int ok,X509_STORE_CTX *ctx); /* error callback */
235 int (*get_issuer)(X509 **issuer, X509_STORE_CTX *ctx, X509 *x); /* get issuers cert from ctx */
236 int (*check_issued)(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); /* check issued */
237 int (*check_revocation)(X509_STORE_CTX *ctx); /* Check revocation status of chain */
238 int (*get_crl)(X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x); /* retrieve CRL */
239 int (*check_crl)(X509_STORE_CTX *ctx, X509_CRL *crl); /* Check CRL validity */
240 int (*cert_crl)(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); /* Check certificate against CRL */
241 int (*check_policy)(X509_STORE_CTX *ctx);
242 STACK_OF(X509) * (*lookup_certs)(X509_STORE_CTX *ctx, X509_NAME *nm);
243 STACK_OF(X509_CRL) * (*lookup_crls)(X509_STORE_CTX *ctx, X509_NAME *nm);
244 int (*cleanup)(X509_STORE_CTX *ctx);
245
246 /* The following is built up */
247 int valid; /* if 0, rebuild chain */
248 int last_untrusted; /* index of last untrusted cert */
249 STACK_OF(X509) *chain; /* chain of X509s - built up and trusted */
250 X509_POLICY_TREE *tree; /* Valid policy tree */
251
252 int explicit_policy; /* Require explicit policy value */
253
254 /* When something goes wrong, this is why */
255 int error_depth;
256 int error;
257 X509 *current_cert;
258 X509 *current_issuer; /* cert currently being tested as valid issuer */
259 X509_CRL *current_crl; /* current CRL */
260
261 int current_crl_score; /* score of current CRL */
262 unsigned int current_reasons; /* Reason mask */
263
264 X509_STORE_CTX *parent; /* For CRL path validation: parent context */
265
266 CRYPTO_EX_DATA ex_data;
267 } /* X509_STORE_CTX */;
268
269void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth);
270
271#define X509_STORE_CTX_set_app_data(ctx,data) \
272 X509_STORE_CTX_set_ex_data(ctx,0,data)
273#define X509_STORE_CTX_get_app_data(ctx) \
274 X509_STORE_CTX_get_ex_data(ctx,0)
275
276#define X509_L_FILE_LOAD 1
277#define X509_L_ADD_DIR 2
278#define X509_L_MEM 3
279
280#define X509_LOOKUP_load_file(x,name,type) \
281 X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL)
282
283#define X509_LOOKUP_add_dir(x,name,type) \
284 X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL)
285
286#define X509_LOOKUP_add_mem(x,iov,type) \
287 X509_LOOKUP_ctrl((x),X509_L_MEM,(const char *)(iov),\
288 (long)(type),NULL)
289
290#define X509_V_OK 0
291/* illegal error (for uninitialized values, to avoid X509_V_OK): 1 */
292
293#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
294#define X509_V_ERR_UNABLE_TO_GET_CRL 3
295#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
296#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
297#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
298#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
299#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
300#define X509_V_ERR_CERT_NOT_YET_VALID 9
301#define X509_V_ERR_CERT_HAS_EXPIRED 10
302#define X509_V_ERR_CRL_NOT_YET_VALID 11
303#define X509_V_ERR_CRL_HAS_EXPIRED 12
304#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
305#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
306#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
307#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
308#define X509_V_ERR_OUT_OF_MEM 17
309#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
310#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
311#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
312#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
313#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
314#define X509_V_ERR_CERT_REVOKED 23
315#define X509_V_ERR_INVALID_CA 24
316#define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
317#define X509_V_ERR_INVALID_PURPOSE 26
318#define X509_V_ERR_CERT_UNTRUSTED 27
319#define X509_V_ERR_CERT_REJECTED 28
320/* These are 'informational' when looking for issuer cert */
321#define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
322#define X509_V_ERR_AKID_SKID_MISMATCH 30
323#define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
324#define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
325
326#define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
327#define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
328#define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
329#define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
330#define X509_V_ERR_INVALID_NON_CA 37
331#define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
332#define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
333#define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
334
335#define X509_V_ERR_INVALID_EXTENSION 41
336#define X509_V_ERR_INVALID_POLICY_EXTENSION 42
337#define X509_V_ERR_NO_EXPLICIT_POLICY 43
338#define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
339#define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
340
341#define X509_V_ERR_UNNESTED_RESOURCE 46
342
343#define X509_V_ERR_PERMITTED_VIOLATION 47
344#define X509_V_ERR_EXCLUDED_VIOLATION 48
345#define X509_V_ERR_SUBTREE_MINMAX 49
346#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
347#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
348#define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
349#define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
350
351/* The application is not happy */
352#define X509_V_ERR_APPLICATION_VERIFICATION 50
353
354/* Certificate verify flags */
355
356/* Send issuer+subject checks to verify_cb */
357#define X509_V_FLAG_CB_ISSUER_CHECK 0x1
358/* Use check time instead of current time */
359#define X509_V_FLAG_USE_CHECK_TIME 0x2
360/* Lookup CRLs */
361#define X509_V_FLAG_CRL_CHECK 0x4
362/* Lookup CRLs for whole chain */
363#define X509_V_FLAG_CRL_CHECK_ALL 0x8
364/* Ignore unhandled critical extensions */
365#define X509_V_FLAG_IGNORE_CRITICAL 0x10
366/* Disable workarounds for broken certificates */
367#define X509_V_FLAG_X509_STRICT 0x20
368/* Enable proxy certificate validation */
369#define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40
370/* Enable policy checking */
371#define X509_V_FLAG_POLICY_CHECK 0x80
372/* Policy variable require-explicit-policy */
373#define X509_V_FLAG_EXPLICIT_POLICY 0x100
374/* Policy variable inhibit-any-policy */
375#define X509_V_FLAG_INHIBIT_ANY 0x200
376/* Policy variable inhibit-policy-mapping */
377#define X509_V_FLAG_INHIBIT_MAP 0x400
378/* Notify callback that policy is OK */
379#define X509_V_FLAG_NOTIFY_POLICY 0x800
380/* Extended CRL features such as indirect CRLs, alternate CRL signing keys */
381#define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000
382/* Delta CRL support */
383#define X509_V_FLAG_USE_DELTAS 0x2000
384/* Check selfsigned CA signature */
385#define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000
386/* Do not check certificate or CRL validity against current time. */
387#define X509_V_FLAG_NO_CHECK_TIME 0x200000
388
389#define X509_VP_FLAG_DEFAULT 0x1
390#define X509_VP_FLAG_OVERWRITE 0x2
391#define X509_VP_FLAG_RESET_FLAGS 0x4
392#define X509_VP_FLAG_LOCKED 0x8
393#define X509_VP_FLAG_ONCE 0x10
394
395/* Internal use: mask of policy related options */
396#define X509_V_FLAG_POLICY_MASK (X509_V_FLAG_POLICY_CHECK \
397 | X509_V_FLAG_EXPLICIT_POLICY \
398 | X509_V_FLAG_INHIBIT_ANY \
399 | X509_V_FLAG_INHIBIT_MAP)
400
401int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
402 X509_NAME *name);
403X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,int type,X509_NAME *name);
404X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x);
405void X509_OBJECT_up_ref_count(X509_OBJECT *a);
406void X509_OBJECT_free_contents(X509_OBJECT *a);
407X509_STORE *X509_STORE_new(void );
408void X509_STORE_free(X509_STORE *v);
409
410STACK_OF(X509)* X509_STORE_get1_certs(X509_STORE_CTX *st, X509_NAME *nm);
411STACK_OF(X509_CRL)* X509_STORE_get1_crls(X509_STORE_CTX *st, X509_NAME *nm);
412int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags);
413int X509_STORE_set_purpose(X509_STORE *ctx, int purpose);
414int X509_STORE_set_trust(X509_STORE *ctx, int trust);
415int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *pm);
416
417void X509_STORE_set_verify_cb(X509_STORE *ctx,
418 int (*verify_cb)(int, X509_STORE_CTX *));
419
420X509_STORE_CTX *X509_STORE_CTX_new(void);
421
422int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
423
424void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
425int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
426 X509 *x509, STACK_OF(X509) *chain);
427void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk);
428void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
429
430X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m);
431
432X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
433X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
434X509_LOOKUP_METHOD *X509_LOOKUP_mem(void);
435
436int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
437int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
438
439int X509_STORE_get_by_subject(X509_STORE_CTX *vs,int type,X509_NAME *name,
440 X509_OBJECT *ret);
441
442int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
443 long argl, char **ret);
444
445int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type);
446int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type);
447int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type);
448
449
450X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
451void X509_LOOKUP_free(X509_LOOKUP *ctx);
452int X509_LOOKUP_init(X509_LOOKUP *ctx);
453int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
454 X509_OBJECT *ret);
455int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
456 ASN1_INTEGER *serial, X509_OBJECT *ret);
457int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type,
458 unsigned char *bytes, int len, X509_OBJECT *ret);
459int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str,
460 int len, X509_OBJECT *ret);
461int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
462
463int X509_STORE_load_locations (X509_STORE *ctx,
464 const char *file, const char *dir);
465int X509_STORE_load_mem(X509_STORE *ctx, void *buf, int len);
466int X509_STORE_set_default_paths(X509_STORE *ctx);
467
468int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
469 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
470int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx,int idx,void *data);
471void * X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx,int idx);
472int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
473void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx,int s);
474int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
475X509 * X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx);
476X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx);
477X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx);
478X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx);
479STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx);
480STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx);
481void X509_STORE_CTX_set_cert(X509_STORE_CTX *c,X509 *x);
482void X509_STORE_CTX_set_chain(X509_STORE_CTX *c,STACK_OF(X509) *sk);
483void X509_STORE_CTX_set0_crls(X509_STORE_CTX *c,STACK_OF(X509_CRL) *sk);
484int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose);
485int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust);
486int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
487 int purpose, int trust);
488void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags);
489void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
490 time_t t);
491void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
492 int (*verify_cb)(int, X509_STORE_CTX *));
493
494X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx);
495int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx);
496
497X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx);
498void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param);
499int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name);
500
501/* X509_VERIFY_PARAM functions */
502
503X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
504void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param);
505int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to,
506 const X509_VERIFY_PARAM *from);
507int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
508 const X509_VERIFY_PARAM *from);
509int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name);
510int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags);
511int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
512 unsigned long flags);
513unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param);
514int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose);
515int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust);
516void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth);
517void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t);
518int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
519 ASN1_OBJECT *policy);
520int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
521 STACK_OF(ASN1_OBJECT) *policies);
522int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param);
523
524int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param);
525const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name);
526void X509_VERIFY_PARAM_table_cleanup(void);
527
528int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
529 STACK_OF(X509) *certs,
530 STACK_OF(ASN1_OBJECT) *policy_oids,
531 unsigned int flags);
532
533void X509_policy_tree_free(X509_POLICY_TREE *tree);
534
535int X509_policy_tree_level_count(const X509_POLICY_TREE *tree);
536X509_POLICY_LEVEL *
537 X509_policy_tree_get0_level(const X509_POLICY_TREE *tree, int i);
538
539STACK_OF(X509_POLICY_NODE) *
540 X509_policy_tree_get0_policies(const X509_POLICY_TREE *tree);
541
542STACK_OF(X509_POLICY_NODE) *
543 X509_policy_tree_get0_user_policies(const X509_POLICY_TREE *tree);
544
545int X509_policy_level_node_count(X509_POLICY_LEVEL *level);
546
547X509_POLICY_NODE *X509_policy_level_get0_node(X509_POLICY_LEVEL *level, int i);
548
549const ASN1_OBJECT *X509_policy_node_get0_policy(const X509_POLICY_NODE *node);
550
551STACK_OF(POLICYQUALINFO) *
552 X509_policy_node_get0_qualifiers(const X509_POLICY_NODE *node);
553const X509_POLICY_NODE *
554 X509_policy_node_get0_parent(const X509_POLICY_NODE *node);
555
556#ifdef __cplusplus
557}
558#endif
559#endif
560
diff --git a/src/lib/libcrypto/x509/x509_vpm.c b/src/lib/libcrypto/x509/x509_vpm.c
deleted file mode 100644
index 8ec972050d..0000000000
--- a/src/lib/libcrypto/x509/x509_vpm.c
+++ /dev/null
@@ -1,449 +0,0 @@
1/* $OpenBSD: x509_vpm.c,v 1.11 2014/09/29 04:16:49 miod Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004.
4 */
5/* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/buffer.h>
63#include <openssl/crypto.h>
64#include <openssl/lhash.h>
65#include <openssl/x509.h>
66#include <openssl/x509v3.h>
67
68/* X509_VERIFY_PARAM functions */
69
70static void
71x509_verify_param_zero(X509_VERIFY_PARAM *param)
72{
73 if (!param)
74 return;
75 param->name = NULL;
76 param->purpose = 0;
77 param->trust = 0;
78 /*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
79 param->inh_flags = 0;
80 param->flags = 0;
81 param->depth = -1;
82 if (param->policies) {
83 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
84 param->policies = NULL;
85 }
86}
87
88X509_VERIFY_PARAM *
89X509_VERIFY_PARAM_new(void)
90{
91 X509_VERIFY_PARAM *param;
92
93 param = calloc(1, sizeof(X509_VERIFY_PARAM));
94 x509_verify_param_zero(param);
95 return param;
96}
97
98void
99X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
100{
101 x509_verify_param_zero(param);
102 free(param);
103}
104
105/* This function determines how parameters are "inherited" from one structure
106 * to another. There are several different ways this can happen.
107 *
108 * 1. If a child structure needs to have its values initialized from a parent
109 * they are simply copied across. For example SSL_CTX copied to SSL.
110 * 2. If the structure should take on values only if they are currently unset.
111 * For example the values in an SSL structure will take appropriate value
112 * for SSL servers or clients but only if the application has not set new
113 * ones.
114 *
115 * The "inh_flags" field determines how this function behaves.
116 *
117 * Normally any values which are set in the default are not copied from the
118 * destination and verify flags are ORed together.
119 *
120 * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
121 * to the destination. Effectively the values in "to" become default values
122 * which will be used only if nothing new is set in "from".
123 *
124 * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
125 * they are set or not. Flags is still Ored though.
126 *
127 * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
128 * of ORed.
129 *
130 * If X509_VP_FLAG_LOCKED is set then no values are copied.
131 *
132 * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
133 * after the next call.
134 */
135
136/* Macro to test if a field should be copied from src to dest */
137
138#define test_x509_verify_param_copy(field, def) \
139 (to_overwrite || \
140 ((src->field != def) && (to_default || (dest->field == def))))
141
142/* Macro to test and copy a field if necessary */
143
144#define x509_verify_param_copy(field, def) \
145 if (test_x509_verify_param_copy(field, def)) \
146 dest->field = src->field
147
148
149int
150X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, const X509_VERIFY_PARAM *src)
151{
152 unsigned long inh_flags;
153 int to_default, to_overwrite;
154
155 if (!src)
156 return 1;
157 inh_flags = dest->inh_flags | src->inh_flags;
158
159 if (inh_flags & X509_VP_FLAG_ONCE)
160 dest->inh_flags = 0;
161
162 if (inh_flags & X509_VP_FLAG_LOCKED)
163 return 1;
164
165 if (inh_flags & X509_VP_FLAG_DEFAULT)
166 to_default = 1;
167 else
168 to_default = 0;
169
170 if (inh_flags & X509_VP_FLAG_OVERWRITE)
171 to_overwrite = 1;
172 else
173 to_overwrite = 0;
174
175 x509_verify_param_copy(purpose, 0);
176 x509_verify_param_copy(trust, 0);
177 x509_verify_param_copy(depth, -1);
178
179 /* If overwrite or check time not set, copy across */
180
181 if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
182 dest->check_time = src->check_time;
183 dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
184 /* Don't need to copy flag: that is done below */
185 }
186
187 if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
188 dest->flags = 0;
189
190 dest->flags |= src->flags;
191
192 if (test_x509_verify_param_copy(policies, NULL)) {
193 if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
194 return 0;
195 }
196
197 return 1;
198}
199
200int
201X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, const X509_VERIFY_PARAM *from)
202{
203 unsigned long save_flags = to->inh_flags;
204 int ret;
205
206 to->inh_flags |= X509_VP_FLAG_DEFAULT;
207 ret = X509_VERIFY_PARAM_inherit(to, from);
208 to->inh_flags = save_flags;
209 return ret;
210}
211
212int
213X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
214{
215 free(param->name);
216 param->name = NULL;
217 if (name == NULL)
218 return 1;
219 param->name = strdup(name);
220 if (param->name)
221 return 1;
222 return 0;
223}
224
225int
226X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
227{
228 param->flags |= flags;
229 if (flags & X509_V_FLAG_POLICY_MASK)
230 param->flags |= X509_V_FLAG_POLICY_CHECK;
231 return 1;
232}
233
234int
235X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
236{
237 param->flags &= ~flags;
238 return 1;
239}
240
241unsigned long
242X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
243{
244 return param->flags;
245}
246
247int
248X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
249{
250 return X509_PURPOSE_set(&param->purpose, purpose);
251}
252
253int
254X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
255{
256 return X509_TRUST_set(&param->trust, trust);
257}
258
259void
260X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
261{
262 param->depth = depth;
263}
264
265void
266X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
267{
268 param->check_time = t;
269 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
270}
271
272int
273X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
274{
275 if (!param->policies) {
276 param->policies = sk_ASN1_OBJECT_new_null();
277 if (!param->policies)
278 return 0;
279 }
280 if (!sk_ASN1_OBJECT_push(param->policies, policy))
281 return 0;
282 return 1;
283}
284
285int
286X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
287 STACK_OF(ASN1_OBJECT) *policies)
288{
289 int i;
290 ASN1_OBJECT *oid, *doid;
291
292 if (!param)
293 return 0;
294 if (param->policies)
295 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
296
297 if (!policies) {
298 param->policies = NULL;
299 return 1;
300 }
301
302 param->policies = sk_ASN1_OBJECT_new_null();
303 if (!param->policies)
304 return 0;
305
306 for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
307 oid = sk_ASN1_OBJECT_value(policies, i);
308 doid = OBJ_dup(oid);
309 if (!doid)
310 return 0;
311 if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
312 ASN1_OBJECT_free(doid);
313 return 0;
314 }
315 }
316 param->flags |= X509_V_FLAG_POLICY_CHECK;
317 return 1;
318}
319
320int
321X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
322{
323 return param->depth;
324}
325
326/* Default verify parameters: these are used for various
327 * applications and can be overridden by the user specified table.
328 * NB: the 'name' field *must* be in alphabetical order because it
329 * will be searched using OBJ_search.
330 */
331
332static const X509_VERIFY_PARAM default_table[] = {
333 {
334 "default", /* X509 default parameters */
335 0, /* Check time */
336 0, /* internal flags */
337 0, /* flags */
338 0, /* purpose */
339 0, /* trust */
340 100, /* depth */
341 NULL /* policies */
342 },
343 {
344 "pkcs7", /* S/MIME sign parameters */
345 0, /* Check time */
346 0, /* internal flags */
347 0, /* flags */
348 X509_PURPOSE_SMIME_SIGN, /* purpose */
349 X509_TRUST_EMAIL, /* trust */
350 -1, /* depth */
351 NULL /* policies */
352 },
353 {
354 "smime_sign", /* S/MIME sign parameters */
355 0, /* Check time */
356 0, /* internal flags */
357 0, /* flags */
358 X509_PURPOSE_SMIME_SIGN, /* purpose */
359 X509_TRUST_EMAIL, /* trust */
360 -1, /* depth */
361 NULL /* policies */
362 },
363 {
364 "ssl_client", /* SSL/TLS client parameters */
365 0, /* Check time */
366 0, /* internal flags */
367 0, /* flags */
368 X509_PURPOSE_SSL_CLIENT, /* purpose */
369 X509_TRUST_SSL_CLIENT, /* trust */
370 -1, /* depth */
371 NULL /* policies */
372 },
373 {
374 "ssl_server", /* SSL/TLS server parameters */
375 0, /* Check time */
376 0, /* internal flags */
377 0, /* flags */
378 X509_PURPOSE_SSL_SERVER, /* purpose */
379 X509_TRUST_SSL_SERVER, /* trust */
380 -1, /* depth */
381 NULL /* policies */
382 }
383};
384
385static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
386
387static int
388table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b)
389{
390 return strcmp(a->name, b->name);
391}
392
393DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
394IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
395
396static int
397param_cmp(const X509_VERIFY_PARAM * const *a,
398 const X509_VERIFY_PARAM * const *b)
399{
400 return strcmp((*a)->name, (*b)->name);
401}
402
403int
404X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
405{
406 int idx;
407 X509_VERIFY_PARAM *ptmp;
408
409 if (!param_table) {
410 param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
411 if (!param_table)
412 return 0;
413 } else {
414 idx = sk_X509_VERIFY_PARAM_find(param_table, param);
415 if (idx != -1) {
416 ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
417 X509_VERIFY_PARAM_free(ptmp);
418 (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
419 }
420 }
421 if (!sk_X509_VERIFY_PARAM_push(param_table, param))
422 return 0;
423 return 1;
424}
425
426const X509_VERIFY_PARAM *
427X509_VERIFY_PARAM_lookup(const char *name)
428{
429 int idx;
430 X509_VERIFY_PARAM pm;
431
432 pm.name = (char *)name;
433 if (param_table) {
434 idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
435 if (idx != -1)
436 return sk_X509_VERIFY_PARAM_value(param_table, idx);
437 }
438 return OBJ_bsearch_table(&pm, default_table,
439 sizeof(default_table)/sizeof(X509_VERIFY_PARAM));
440}
441
442void
443X509_VERIFY_PARAM_table_cleanup(void)
444{
445 if (param_table)
446 sk_X509_VERIFY_PARAM_pop_free(param_table,
447 X509_VERIFY_PARAM_free);
448 param_table = NULL;
449}
diff --git a/src/lib/libcrypto/x509/x509cset.c b/src/lib/libcrypto/x509/x509cset.c
deleted file mode 100644
index afc1f0f2b3..0000000000
--- a/src/lib/libcrypto/x509/x509cset.c
+++ /dev/null
@@ -1,173 +0,0 @@
1/* $OpenBSD: x509cset.c,v 1.11 2015/09/30 17:49:59 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65
66int
67X509_CRL_set_version(X509_CRL *x, long version)
68{
69 if (x == NULL)
70 return (0);
71 if (x->crl->version == NULL) {
72 if ((x->crl->version = ASN1_INTEGER_new()) == NULL)
73 return (0);
74 }
75 return (ASN1_INTEGER_set(x->crl->version, version));
76}
77
78int
79X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name)
80{
81 if ((x == NULL) || (x->crl == NULL))
82 return (0);
83 return (X509_NAME_set(&x->crl->issuer, name));
84}
85
86int
87X509_CRL_set_lastUpdate(X509_CRL *x, const ASN1_TIME *tm)
88{
89 ASN1_TIME *in;
90
91 if (x == NULL)
92 return (0);
93 in = x->crl->lastUpdate;
94 if (in != tm) {
95 in = ASN1_STRING_dup(tm);
96 if (in != NULL) {
97 ASN1_TIME_free(x->crl->lastUpdate);
98 x->crl->lastUpdate = in;
99 }
100 }
101 return (in != NULL);
102}
103
104int
105X509_CRL_set_nextUpdate(X509_CRL *x, const ASN1_TIME *tm)
106{
107 ASN1_TIME *in;
108
109 if (x == NULL)
110 return (0);
111 in = x->crl->nextUpdate;
112 if (in != tm) {
113 in = ASN1_STRING_dup(tm);
114 if (in != NULL) {
115 ASN1_TIME_free(x->crl->nextUpdate);
116 x->crl->nextUpdate = in;
117 }
118 }
119 return (in != NULL);
120}
121
122int
123X509_CRL_sort(X509_CRL *c)
124{
125 int i;
126 X509_REVOKED *r;
127
128 /* sort the data so it will be written in serial
129 * number order */
130 sk_X509_REVOKED_sort(c->crl->revoked);
131 for (i = 0; i < sk_X509_REVOKED_num(c->crl->revoked); i++) {
132 r = sk_X509_REVOKED_value(c->crl->revoked, i);
133 r->sequence = i;
134 }
135 c->crl->enc.modified = 1;
136 return 1;
137}
138
139int
140X509_REVOKED_set_revocationDate(X509_REVOKED *x, ASN1_TIME *tm)
141{
142 ASN1_TIME *in;
143
144 if (x == NULL)
145 return (0);
146 in = x->revocationDate;
147 if (in != tm) {
148 in = ASN1_STRING_dup(tm);
149 if (in != NULL) {
150 ASN1_TIME_free(x->revocationDate);
151 x->revocationDate = in;
152 }
153 }
154 return (in != NULL);
155}
156
157int
158X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial)
159{
160 ASN1_INTEGER *in;
161
162 if (x == NULL)
163 return (0);
164 in = x->serialNumber;
165 if (in != serial) {
166 in = ASN1_INTEGER_dup(serial);
167 if (in != NULL) {
168 ASN1_INTEGER_free(x->serialNumber);
169 x->serialNumber = in;
170 }
171 }
172 return (in != NULL);
173}
diff --git a/src/lib/libcrypto/x509/x509name.c b/src/lib/libcrypto/x509/x509name.c
deleted file mode 100644
index 14634013cf..0000000000
--- a/src/lib/libcrypto/x509/x509name.c
+++ /dev/null
@@ -1,410 +0,0 @@
1/* $OpenBSD: x509name.c,v 1.13 2014/09/29 04:17:24 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/asn1.h>
63#include <openssl/err.h>
64#include <openssl/evp.h>
65#include <openssl/objects.h>
66#include <openssl/stack.h>
67#include <openssl/x509.h>
68
69int
70X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len)
71{
72 ASN1_OBJECT *obj;
73
74 obj = OBJ_nid2obj(nid);
75 if (obj == NULL)
76 return (-1);
77 return (X509_NAME_get_text_by_OBJ(name, obj, buf, len));
78}
79
80int
81X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf,
82 int len)
83{
84 int i;
85 ASN1_STRING *data;
86
87 i = X509_NAME_get_index_by_OBJ(name, obj, -1);
88 if (i < 0)
89 return (-1);
90 data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
91 i = (data->length > (len - 1)) ? (len - 1) : data->length;
92 if (buf == NULL)
93 return (data->length);
94 if (i >= 0) {
95 memcpy(buf, data->data, i);
96 buf[i] = '\0';
97 }
98 return (i);
99}
100
101int
102X509_NAME_entry_count(X509_NAME *name)
103{
104 if (name == NULL)
105 return (0);
106 return (sk_X509_NAME_ENTRY_num(name->entries));
107}
108
109int
110X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)
111{
112 ASN1_OBJECT *obj;
113
114 obj = OBJ_nid2obj(nid);
115 if (obj == NULL)
116 return (-2);
117 return (X509_NAME_get_index_by_OBJ(name, obj, lastpos));
118}
119
120/* NOTE: you should be passsing -1, not 0 as lastpos */
121int
122X509_NAME_get_index_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int lastpos)
123{
124 int n;
125 X509_NAME_ENTRY *ne;
126 STACK_OF(X509_NAME_ENTRY) *sk;
127
128 if (name == NULL)
129 return (-1);
130 if (lastpos < 0)
131 lastpos = -1;
132 sk = name->entries;
133 n = sk_X509_NAME_ENTRY_num(sk);
134 for (lastpos++; lastpos < n; lastpos++) {
135 ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
136 if (OBJ_cmp(ne->object, obj) == 0)
137 return (lastpos);
138 }
139 return (-1);
140}
141
142X509_NAME_ENTRY *
143X509_NAME_get_entry(X509_NAME *name, int loc)
144{
145 if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc ||
146 loc < 0)
147 return (NULL);
148 else
149 return (sk_X509_NAME_ENTRY_value(name->entries, loc));
150}
151
152X509_NAME_ENTRY *
153X509_NAME_delete_entry(X509_NAME *name, int loc)
154{
155 X509_NAME_ENTRY *ret;
156 int i, n, set_prev, set_next;
157 STACK_OF(X509_NAME_ENTRY) *sk;
158
159 if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc ||
160 loc < 0)
161 return (NULL);
162 sk = name->entries;
163 ret = sk_X509_NAME_ENTRY_delete(sk, loc);
164 n = sk_X509_NAME_ENTRY_num(sk);
165 name->modified = 1;
166 if (loc == n)
167 return (ret);
168
169 /* else we need to fixup the set field */
170 if (loc != 0)
171 set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
172 else
173 set_prev = ret->set - 1;
174 set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
175
176 /* set_prev is the previous set
177 * set is the current set
178 * set_next is the following
179 * prev 1 1 1 1 1 1 1 1
180 * set 1 1 2 2
181 * next 1 1 2 2 2 2 3 2
182 * so basically only if prev and next differ by 2, then
183 * re-number down by 1 */
184 if (set_prev + 1 < set_next)
185 for (i = loc; i < n; i++)
186 sk_X509_NAME_ENTRY_value(sk, i)->set--;
187 return (ret);
188}
189
190int
191X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
192 unsigned char *bytes, int len, int loc, int set)
193{
194 X509_NAME_ENTRY *ne;
195 int ret;
196
197 ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
198 if (!ne)
199 return 0;
200 ret = X509_NAME_add_entry(name, ne, loc, set);
201 X509_NAME_ENTRY_free(ne);
202 return ret;
203}
204
205int
206X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
207 unsigned char *bytes, int len, int loc, int set)
208{
209 X509_NAME_ENTRY *ne;
210 int ret;
211
212 ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
213 if (!ne)
214 return 0;
215 ret = X509_NAME_add_entry(name, ne, loc, set);
216 X509_NAME_ENTRY_free(ne);
217 return ret;
218}
219
220int
221X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
222 const unsigned char *bytes, int len, int loc, int set)
223{
224 X509_NAME_ENTRY *ne;
225 int ret;
226
227 ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
228 if (!ne)
229 return 0;
230 ret = X509_NAME_add_entry(name, ne, loc, set);
231 X509_NAME_ENTRY_free(ne);
232 return ret;
233}
234
235/* if set is -1, append to previous set, 0 'a new one', and 1,
236 * prepend to the guy we are about to stomp on. */
237int
238X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc, int set)
239{
240 X509_NAME_ENTRY *new_name = NULL;
241 int n, i, inc;
242 STACK_OF(X509_NAME_ENTRY) *sk;
243
244 if (name == NULL)
245 return (0);
246 sk = name->entries;
247 n = sk_X509_NAME_ENTRY_num(sk);
248 if (loc > n)
249 loc = n;
250 else if (loc < 0)
251 loc = n;
252
253 name->modified = 1;
254
255 if (set == -1) {
256 if (loc == 0) {
257 set = 0;
258 inc = 1;
259 } else {
260 set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
261 inc = 0;
262 }
263 } else /* if (set >= 0) */ {
264 if (loc >= n) {
265 if (loc != 0)
266 set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
267 else
268 set = 0;
269 } else
270 set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
271 inc = (set == 0) ? 1 : 0;
272 }
273
274 if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
275 goto err;
276 new_name->set = set;
277 if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
278 X509err(X509_F_X509_NAME_ADD_ENTRY, ERR_R_MALLOC_FAILURE);
279 goto err;
280 }
281 if (inc) {
282 n = sk_X509_NAME_ENTRY_num(sk);
283 for (i = loc + 1; i < n; i++)
284 sk_X509_NAME_ENTRY_value(sk, i - 1)->set += 1;
285 }
286 return (1);
287
288err:
289 if (new_name != NULL)
290 X509_NAME_ENTRY_free(new_name);
291 return (0);
292}
293
294X509_NAME_ENTRY *
295X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
296 const char *field, int type, const unsigned char *bytes, int len)
297{
298 ASN1_OBJECT *obj;
299 X509_NAME_ENTRY *nentry;
300
301 obj = OBJ_txt2obj(field, 0);
302 if (obj == NULL) {
303 X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_TXT,
304 X509_R_INVALID_FIELD_NAME);
305 ERR_asprintf_error_data("name=%s", field);
306 return (NULL);
307 }
308 nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
309 ASN1_OBJECT_free(obj);
310 return nentry;
311}
312
313X509_NAME_ENTRY *
314X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, int type,
315 unsigned char *bytes, int len)
316{
317 ASN1_OBJECT *obj;
318 X509_NAME_ENTRY *nentry;
319
320 obj = OBJ_nid2obj(nid);
321 if (obj == NULL) {
322 X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,
323 X509_R_UNKNOWN_NID);
324 return (NULL);
325 }
326 nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
327 ASN1_OBJECT_free(obj);
328 return nentry;
329}
330
331X509_NAME_ENTRY *
332X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, ASN1_OBJECT *obj, int type,
333 const unsigned char *bytes, int len)
334{
335 X509_NAME_ENTRY *ret;
336
337 if ((ne == NULL) || (*ne == NULL)) {
338 if ((ret = X509_NAME_ENTRY_new()) == NULL)
339 return (NULL);
340 } else
341 ret= *ne;
342
343 if (!X509_NAME_ENTRY_set_object(ret, obj))
344 goto err;
345 if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
346 goto err;
347
348 if ((ne != NULL) && (*ne == NULL))
349 *ne = ret;
350 return (ret);
351
352err:
353 if ((ne == NULL) || (ret != *ne))
354 X509_NAME_ENTRY_free(ret);
355 return (NULL);
356}
357
358int
359X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, ASN1_OBJECT *obj)
360{
361 if ((ne == NULL) || (obj == NULL)) {
362 X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,
363 ERR_R_PASSED_NULL_PARAMETER);
364 return (0);
365 }
366 ASN1_OBJECT_free(ne->object);
367 ne->object = OBJ_dup(obj);
368 return ((ne->object == NULL) ? 0 : 1);
369}
370
371int
372X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
373 const unsigned char *bytes, int len)
374{
375 int i;
376
377 if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
378 return (0);
379 if ((type > 0) && (type & MBSTRING_FLAG))
380 return ASN1_STRING_set_by_NID(&ne->value, bytes, len, type,
381 OBJ_obj2nid(ne->object)) ? 1 : 0;
382 if (len < 0)
383 len = strlen((const char *)bytes);
384 i = ASN1_STRING_set(ne->value, bytes, len);
385 if (!i)
386 return (0);
387 if (type != V_ASN1_UNDEF) {
388 if (type == V_ASN1_APP_CHOOSE)
389 ne->value->type = ASN1_PRINTABLE_type(bytes, len);
390 else
391 ne->value->type = type;
392 }
393 return (1);
394}
395
396ASN1_OBJECT *
397X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne)
398{
399 if (ne == NULL)
400 return (NULL);
401 return (ne->object);
402}
403
404ASN1_STRING *
405X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)
406{
407 if (ne == NULL)
408 return (NULL);
409 return (ne->value);
410}
diff --git a/src/lib/libcrypto/x509/x509rset.c b/src/lib/libcrypto/x509/x509rset.c
deleted file mode 100644
index cfac977636..0000000000
--- a/src/lib/libcrypto/x509/x509rset.c
+++ /dev/null
@@ -1,88 +0,0 @@
1/* $OpenBSD: x509rset.c,v 1.6 2014/07/11 08:44:49 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/asn1.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65
66int
67X509_REQ_set_version(X509_REQ *x, long version)
68{
69 if (x == NULL)
70 return (0);
71 return (ASN1_INTEGER_set(x->req_info->version, version));
72}
73
74int
75X509_REQ_set_subject_name(X509_REQ *x, X509_NAME *name)
76{
77 if ((x == NULL) || (x->req_info == NULL))
78 return (0);
79 return (X509_NAME_set(&x->req_info->subject, name));
80}
81
82int
83X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey)
84{
85 if ((x == NULL) || (x->req_info == NULL))
86 return (0);
87 return (X509_PUBKEY_set(&x->req_info->pubkey, pkey));
88}
diff --git a/src/lib/libcrypto/x509/x509spki.c b/src/lib/libcrypto/x509/x509spki.c
deleted file mode 100644
index cd29a8138a..0000000000
--- a/src/lib/libcrypto/x509/x509spki.c
+++ /dev/null
@@ -1,132 +0,0 @@
1/* $OpenBSD: x509spki.c,v 1.12 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/err.h>
63#include <openssl/x509.h>
64
65int
66NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
67{
68 if ((x == NULL) || (x->spkac == NULL))
69 return (0);
70 return (X509_PUBKEY_set(&(x->spkac->pubkey), pkey));
71}
72
73EVP_PKEY *
74NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x)
75{
76 if ((x == NULL) || (x->spkac == NULL))
77 return (NULL);
78 return (X509_PUBKEY_get(x->spkac->pubkey));
79}
80
81/* Load a Netscape SPKI from a base64 encoded string */
82
83NETSCAPE_SPKI *
84NETSCAPE_SPKI_b64_decode(const char *str, int len)
85{
86 unsigned char *spki_der;
87 const unsigned char *p;
88 int spki_len;
89 NETSCAPE_SPKI *spki;
90
91 if (len <= 0)
92 len = strlen(str);
93 if (!(spki_der = malloc(len + 1))) {
94 X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, ERR_R_MALLOC_FAILURE);
95 return NULL;
96 }
97 spki_len = EVP_DecodeBlock(spki_der, (const unsigned char *)str, len);
98 if (spki_len < 0) {
99 X509err(X509_F_NETSCAPE_SPKI_B64_DECODE,
100 X509_R_BASE64_DECODE_ERROR);
101 free(spki_der);
102 return NULL;
103 }
104 p = spki_der;
105 spki = d2i_NETSCAPE_SPKI(NULL, &p, spki_len);
106 free(spki_der);
107 return spki;
108}
109
110/* Generate a base64 encoded string from an SPKI */
111
112char *
113NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki)
114{
115 unsigned char *der_spki, *p;
116 char *b64_str;
117 int der_len;
118 der_len = i2d_NETSCAPE_SPKI(spki, NULL);
119 der_spki = malloc(der_len);
120 b64_str = reallocarray(NULL, der_len, 2);
121 if (!der_spki || !b64_str) {
122 X509err(X509_F_NETSCAPE_SPKI_B64_ENCODE, ERR_R_MALLOC_FAILURE);
123 free(der_spki);
124 free(b64_str);
125 return NULL;
126 }
127 p = der_spki;
128 i2d_NETSCAPE_SPKI(spki, &p);
129 EVP_EncodeBlock((unsigned char *)b64_str, der_spki, der_len);
130 free(der_spki);
131 return b64_str;
132}
diff --git a/src/lib/libcrypto/x509/x509type.c b/src/lib/libcrypto/x509/x509type.c
deleted file mode 100644
index d0dcffb290..0000000000
--- a/src/lib/libcrypto/x509/x509type.c
+++ /dev/null
@@ -1,130 +0,0 @@
1/* $OpenBSD: x509type.c,v 1.12 2015/06/13 08:38:10 doug Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/evp.h>
62#include <openssl/objects.h>
63#include <openssl/x509.h>
64
65int
66X509_certificate_type(X509 *x, EVP_PKEY *pkey)
67{
68 EVP_PKEY *pk;
69 int ret = 0, i;
70
71 if (x == NULL)
72 return (0);
73
74 if (pkey == NULL)
75 pk = X509_get_pubkey(x);
76 else
77 pk = pkey;
78
79 if (pk == NULL)
80 return (0);
81
82 switch (pk->type) {
83 case EVP_PKEY_RSA:
84 ret = EVP_PK_RSA|EVP_PKT_SIGN;
85/* if (!sign only extension) */
86 ret |= EVP_PKT_ENC;
87 break;
88 case EVP_PKEY_DSA:
89 ret = EVP_PK_DSA|EVP_PKT_SIGN;
90 break;
91 case EVP_PKEY_EC:
92 ret = EVP_PK_EC|EVP_PKT_SIGN|EVP_PKT_EXCH;
93 break;
94 case EVP_PKEY_DH:
95 ret = EVP_PK_DH|EVP_PKT_EXCH;
96 break;
97 case NID_id_GostR3410_94:
98 case NID_id_GostR3410_2001:
99 ret = EVP_PKT_EXCH|EVP_PKT_SIGN;
100 break;
101 default:
102 break;
103 }
104
105 i = OBJ_obj2nid(x->sig_alg->algorithm);
106 if (i && OBJ_find_sigid_algs(i, NULL, &i)) {
107 switch (i) {
108 case NID_rsaEncryption:
109 case NID_rsa:
110 ret |= EVP_PKS_RSA;
111 break;
112 case NID_dsa:
113 case NID_dsa_2:
114 ret |= EVP_PKS_DSA;
115 break;
116 case NID_X9_62_id_ecPublicKey:
117 ret |= EVP_PKS_EC;
118 break;
119 default:
120 break;
121 }
122 }
123
124 /* /8 because it's 1024 bits we look for, not bytes */
125 if (EVP_PKEY_size(pk) <= 1024 / 8)
126 ret |= EVP_PKT_EXP;
127 if (pkey == NULL)
128 EVP_PKEY_free(pk);
129 return (ret);
130}
diff --git a/src/lib/libcrypto/x509/x_all.c b/src/lib/libcrypto/x509/x_all.c
deleted file mode 100644
index 21a8535445..0000000000
--- a/src/lib/libcrypto/x509/x_all.c
+++ /dev/null
@@ -1,602 +0,0 @@
1/* $OpenBSD: x_all.c,v 1.21 2015/10/13 14:03:26 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/opensslconf.h>
62
63#include <openssl/asn1.h>
64#include <openssl/buffer.h>
65#include <openssl/evp.h>
66#include <openssl/stack.h>
67#include <openssl/x509.h>
68
69#ifndef OPENSSL_NO_DSA
70#include <openssl/dsa.h>
71#endif
72#ifndef OPENSSL_NO_RSA
73#include <openssl/rsa.h>
74#endif
75
76X509 *
77d2i_X509_bio(BIO *bp, X509 **x509)
78{
79 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
80}
81
82int
83i2d_X509_bio(BIO *bp, X509 *x509)
84{
85 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
86}
87
88X509 *
89d2i_X509_fp(FILE *fp, X509 **x509)
90{
91 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
92}
93
94int
95i2d_X509_fp(FILE *fp, X509 *x509)
96{
97 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
98}
99
100X509_CRL *
101d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
102{
103 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
104}
105
106int
107i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl)
108{
109 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
110}
111
112X509_CRL *
113d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
114{
115 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
116}
117
118int
119i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl)
120{
121 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
122}
123
124PKCS7 *
125d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
126{
127 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
128}
129
130int
131i2d_PKCS7_bio(BIO *bp, PKCS7 *p7)
132{
133 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
134}
135
136PKCS7 *
137d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
138{
139 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
140}
141
142int
143i2d_PKCS7_fp(FILE *fp, PKCS7 *p7)
144{
145 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
146}
147
148X509_REQ *
149d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
150{
151 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
152}
153
154int
155i2d_X509_REQ_bio(BIO *bp, X509_REQ *req)
156{
157 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
158}
159
160X509_REQ *
161d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
162{
163 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
164}
165
166int
167i2d_X509_REQ_fp(FILE *fp, X509_REQ *req)
168{
169 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
170}
171
172#ifndef OPENSSL_NO_RSA
173RSA *
174d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
175{
176 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
177}
178
179int
180i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa)
181{
182 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
183}
184
185RSA *
186d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
187{
188 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
189}
190
191int
192i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa)
193{
194 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
195}
196
197RSA *
198d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
199{
200 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
201}
202
203int
204i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa)
205{
206 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
207}
208
209RSA *
210d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
211{
212 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
213}
214
215int
216i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa)
217{
218 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
219}
220
221RSA *
222d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
223{
224 return ASN1_d2i_bio_of(RSA, RSA_new, d2i_RSA_PUBKEY, bp, rsa);
225}
226
227int
228i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa)
229{
230 return ASN1_i2d_bio_of(RSA, i2d_RSA_PUBKEY, bp, rsa);
231}
232
233int
234i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa)
235{
236 return ASN1_i2d_fp((I2D_OF(void))i2d_RSA_PUBKEY, fp, rsa);
237}
238
239RSA *
240d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
241{
242 return ASN1_d2i_fp((void *(*)(void))RSA_new,
243 (D2I_OF(void))d2i_RSA_PUBKEY, fp, (void **)rsa);
244}
245#endif
246
247#ifndef OPENSSL_NO_DSA
248DSA *
249d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
250{
251 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(DSAPrivateKey), bp, dsa);
252}
253
254int
255i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa)
256{
257 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(DSAPrivateKey), bp, dsa);
258}
259
260DSA *
261d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
262{
263 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(DSAPrivateKey), fp, dsa);
264}
265
266int
267i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa)
268{
269 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(DSAPrivateKey), fp, dsa);
270}
271
272DSA *
273d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
274{
275 return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSA_PUBKEY, bp, dsa);
276}
277
278int
279i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa)
280{
281 return ASN1_i2d_bio_of(DSA, i2d_DSA_PUBKEY, bp, dsa);
282}
283
284DSA *
285d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
286{
287 return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSA_PUBKEY, fp, dsa);
288}
289
290int
291i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa)
292{
293 return ASN1_i2d_fp_of(DSA, i2d_DSA_PUBKEY, fp, dsa);
294}
295#endif
296
297#ifndef OPENSSL_NO_EC
298EC_KEY *
299d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey)
300{
301 return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey);
302}
303
304int
305i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey)
306{
307 return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey);
308}
309
310EC_KEY *
311d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey)
312{
313 return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey);
314}
315
316int
317i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey)
318{
319 return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey);
320}
321
322EC_KEY *
323d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey)
324{
325 return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, bp, eckey);
326}
327
328int
329i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *ecdsa)
330{
331 return ASN1_i2d_bio_of(EC_KEY, i2d_EC_PUBKEY, bp, ecdsa);
332}
333EC_KEY *
334d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey)
335{
336 return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, fp, eckey);
337}
338
339int
340i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey)
341{
342 return ASN1_i2d_fp_of(EC_KEY, i2d_EC_PUBKEY, fp, eckey);
343}
344#endif
345
346X509_SIG *
347d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
348{
349 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_SIG), bp, p8);
350}
351
352int
353i2d_PKCS8_bio(BIO *bp, X509_SIG *p8)
354{
355 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_SIG), bp, p8);
356}
357
358X509_SIG *
359d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
360{
361 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_SIG), fp, p8);
362}
363
364int
365i2d_PKCS8_fp(FILE *fp, X509_SIG *p8)
366{
367 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_SIG), fp, p8);
368}
369
370PKCS8_PRIV_KEY_INFO *
371d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf)
372{
373 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), bp,
374 p8inf);
375}
376
377int
378i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf)
379{
380 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), bp,
381 p8inf);
382}
383
384PKCS8_PRIV_KEY_INFO *
385d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf)
386{
387 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), fp,
388 p8inf);
389}
390
391int
392i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf)
393{
394 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), fp,
395 p8inf);
396}
397
398EVP_PKEY *
399d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
400{
401 return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey,
402 bp, a);
403}
404
405int
406i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey)
407{
408 return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey);
409}
410
411EVP_PKEY *
412d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
413{
414 return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey,
415 fp, a);
416}
417
418int
419i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey)
420{
421 return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey);
422}
423
424EVP_PKEY *
425d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a)
426{
427 return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, bp, a);
428}
429
430int
431i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey)
432{
433 return ASN1_i2d_bio_of(EVP_PKEY, i2d_PUBKEY, bp, pkey);
434}
435
436int
437i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey)
438{
439 return ASN1_i2d_fp_of(EVP_PKEY, i2d_PUBKEY, fp, pkey);
440}
441
442EVP_PKEY *
443d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a)
444{
445 return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, fp, a);
446}
447
448int
449i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key)
450{
451 PKCS8_PRIV_KEY_INFO *p8inf;
452 int ret;
453
454 p8inf = EVP_PKEY2PKCS8(key);
455 if (!p8inf)
456 return 0;
457 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
458 PKCS8_PRIV_KEY_INFO_free(p8inf);
459 return ret;
460}
461
462int
463i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key)
464{
465 PKCS8_PRIV_KEY_INFO *p8inf;
466 int ret;
467 p8inf = EVP_PKEY2PKCS8(key);
468 if (!p8inf)
469 return 0;
470 ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
471 PKCS8_PRIV_KEY_INFO_free(p8inf);
472 return ret;
473}
474
475int
476X509_verify(X509 *a, EVP_PKEY *r)
477{
478 if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature))
479 return 0;
480 return(ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), a->sig_alg,
481 a->signature, a->cert_info, r));
482}
483
484int
485X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
486{
487 return (ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO),
488 a->sig_alg, a->signature, a->req_info, r));
489}
490
491int
492NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
493{
494 return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC),
495 a->sig_algor, a->signature, a->spkac, r));
496}
497
498int
499X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
500{
501 x->cert_info->enc.modified = 1;
502 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF),
503 x->cert_info->signature, x->sig_alg, x->signature,
504 x->cert_info, pkey, md));
505}
506
507int
508X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
509{
510 x->cert_info->enc.modified = 1;
511 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
512 x->cert_info->signature, x->sig_alg, x->signature,
513 x->cert_info, ctx);
514}
515
516int
517X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
518{
519 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO),
520 x->sig_alg, NULL, x->signature, x->req_info, pkey, md));
521}
522
523int
524X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
525{
526 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO),
527 x->sig_alg, NULL, x->signature, x->req_info, ctx);
528}
529
530int
531X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
532{
533 x->crl->enc.modified = 1;
534 return(ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
535 x->sig_alg, x->signature, x->crl, pkey, md));
536}
537
538int
539X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
540{
541 x->crl->enc.modified = 1;
542 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
543 x->crl->sig_alg, x->sig_alg, x->signature, x->crl, ctx);
544}
545
546int
547NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
548{
549 return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC),
550 x->sig_algor, NULL, x->signature, x->spkac, pkey, md));
551}
552
553int
554X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
555 unsigned int *len)
556{
557 ASN1_BIT_STRING *key;
558 key = X509_get0_pubkey_bitstr(data);
559 if (!key)
560 return 0;
561 return EVP_Digest(key->data, key->length, md, len, type, NULL);
562}
563
564int
565X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
566 unsigned int *len)
567{
568 return (ASN1_item_digest(ASN1_ITEM_rptr(X509), type, (char *)data,
569 md, len));
570}
571
572int
573X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md,
574 unsigned int *len)
575{
576 return (ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data,
577 md, len));
578}
579
580int
581X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md,
582 unsigned int *len)
583{
584 return (ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data,
585 md, len));
586}
587
588int
589X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, unsigned char *md,
590 unsigned int *len)
591{
592 return (ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data,
593 md, len));
594}
595
596int
597PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
598 const EVP_MD *type, unsigned char *md, unsigned int *len)
599{
600 return(ASN1_item_digest(ASN1_ITEM_rptr(PKCS7_ISSUER_AND_SERIAL), type,
601 (char *)data, md, len));
602}