Blame


1 f9ab77a8 2023-08-23 op /* $OpenBSD: by_mem.c,v 1.4 2017/01/29 17:49:23 beck Exp $ */
2 f9ab77a8 2023-08-23 op /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 f9ab77a8 2023-08-23 op * All rights reserved.
4 f9ab77a8 2023-08-23 op *
5 f9ab77a8 2023-08-23 op * This package is an SSL implementation written
6 f9ab77a8 2023-08-23 op * by Eric Young (eay@cryptsoft.com).
7 f9ab77a8 2023-08-23 op * The implementation was written so as to conform with Netscapes SSL.
8 f9ab77a8 2023-08-23 op *
9 f9ab77a8 2023-08-23 op * This library is free for commercial and non-commercial use as long as
10 f9ab77a8 2023-08-23 op * the following conditions are aheared to. The following conditions
11 f9ab77a8 2023-08-23 op * apply to all code found in this distribution, be it the RC4, RSA,
12 f9ab77a8 2023-08-23 op * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 f9ab77a8 2023-08-23 op * included with this distribution is covered by the same copyright terms
14 f9ab77a8 2023-08-23 op * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 f9ab77a8 2023-08-23 op *
16 f9ab77a8 2023-08-23 op * Copyright remains Eric Young's, and as such any Copyright notices in
17 f9ab77a8 2023-08-23 op * the code are not to be removed.
18 f9ab77a8 2023-08-23 op * If this package is used in a product, Eric Young should be given attribution
19 f9ab77a8 2023-08-23 op * as the author of the parts of the library used.
20 f9ab77a8 2023-08-23 op * This can be in the form of a textual message at program startup or
21 f9ab77a8 2023-08-23 op * in documentation (online or textual) provided with the package.
22 f9ab77a8 2023-08-23 op *
23 f9ab77a8 2023-08-23 op * Redistribution and use in source and binary forms, with or without
24 f9ab77a8 2023-08-23 op * modification, are permitted provided that the following conditions
25 f9ab77a8 2023-08-23 op * are met:
26 f9ab77a8 2023-08-23 op * 1. Redistributions of source code must retain the copyright
27 f9ab77a8 2023-08-23 op * notice, this list of conditions and the following disclaimer.
28 f9ab77a8 2023-08-23 op * 2. Redistributions in binary form must reproduce the above copyright
29 f9ab77a8 2023-08-23 op * notice, this list of conditions and the following disclaimer in the
30 f9ab77a8 2023-08-23 op * documentation and/or other materials provided with the distribution.
31 f9ab77a8 2023-08-23 op * 3. All advertising materials mentioning features or use of this software
32 f9ab77a8 2023-08-23 op * must display the following acknowledgement:
33 f9ab77a8 2023-08-23 op * "This product includes cryptographic software written by
34 f9ab77a8 2023-08-23 op * Eric Young (eay@cryptsoft.com)"
35 f9ab77a8 2023-08-23 op * The word 'cryptographic' can be left out if the rouines from the library
36 f9ab77a8 2023-08-23 op * being used are not cryptographic related :-).
37 f9ab77a8 2023-08-23 op * 4. If you include any Windows specific code (or a derivative thereof) from
38 f9ab77a8 2023-08-23 op * the apps directory (application code) you must include an acknowledgement:
39 f9ab77a8 2023-08-23 op * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 f9ab77a8 2023-08-23 op *
41 f9ab77a8 2023-08-23 op * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 f9ab77a8 2023-08-23 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 f9ab77a8 2023-08-23 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 f9ab77a8 2023-08-23 op * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 f9ab77a8 2023-08-23 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 f9ab77a8 2023-08-23 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 f9ab77a8 2023-08-23 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 f9ab77a8 2023-08-23 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 f9ab77a8 2023-08-23 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 f9ab77a8 2023-08-23 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 f9ab77a8 2023-08-23 op * SUCH DAMAGE.
52 f9ab77a8 2023-08-23 op *
53 f9ab77a8 2023-08-23 op * The licence and distribution terms for any publically available version or
54 f9ab77a8 2023-08-23 op * derivative of this code cannot be changed. i.e. this code cannot simply be
55 f9ab77a8 2023-08-23 op * copied and put under another distribution licence
56 f9ab77a8 2023-08-23 op * [including the GNU Public Licence.]
57 f9ab77a8 2023-08-23 op */
58 f9ab77a8 2023-08-23 op
59 f9ab77a8 2023-08-23 op #include "config.h"
60 f9ab77a8 2023-08-23 op
61 f9ab77a8 2023-08-23 op #ifndef HAVE_X509_LOOKUP_MEM
62 f9ab77a8 2023-08-23 op
63 f9ab77a8 2023-08-23 op #include <sys/uio.h>
64 f9ab77a8 2023-08-23 op #include <errno.h>
65 f9ab77a8 2023-08-23 op #include <stdio.h>
66 f9ab77a8 2023-08-23 op #include <time.h>
67 f9ab77a8 2023-08-23 op #include <unistd.h>
68 f9ab77a8 2023-08-23 op
69 f9ab77a8 2023-08-23 op #include <openssl/buffer.h>
70 f9ab77a8 2023-08-23 op #include <openssl/err.h>
71 f9ab77a8 2023-08-23 op #include <openssl/pem.h>
72 f9ab77a8 2023-08-23 op #include <openssl/lhash.h>
73 f9ab77a8 2023-08-23 op #include <openssl/x509.h>
74 f9ab77a8 2023-08-23 op
75 f9ab77a8 2023-08-23 op #define X509error(r) ERR_PUT_error(ERR_LIB_X509,(0xfff),(r),__FILE__,__LINE__)
76 f9ab77a8 2023-08-23 op
77 f9ab77a8 2023-08-23 op #define X509_L_MEM 3
78 f9ab77a8 2023-08-23 op
79 f9ab77a8 2023-08-23 op static int by_mem_ctrl(X509_LOOKUP *, int, const char *, long, char **);
80 f9ab77a8 2023-08-23 op
81 f9ab77a8 2023-08-23 op static X509_LOOKUP_METHOD *x509_mem_lookup;
82 f9ab77a8 2023-08-23 op
83 f9ab77a8 2023-08-23 op X509_LOOKUP_METHOD *
84 f9ab77a8 2023-08-23 op X509_LOOKUP_mem(void)
85 f9ab77a8 2023-08-23 op {
86 f9ab77a8 2023-08-23 op if (x509_mem_lookup == NULL) {
87 f9ab77a8 2023-08-23 op x509_mem_lookup = X509_LOOKUP_meth_new("Load cert from memory");
88 f9ab77a8 2023-08-23 op X509_LOOKUP_meth_set_ctrl(x509_mem_lookup, by_mem_ctrl);
89 f9ab77a8 2023-08-23 op }
90 f9ab77a8 2023-08-23 op return (x509_mem_lookup);
91 f9ab77a8 2023-08-23 op }
92 f9ab77a8 2023-08-23 op
93 f9ab77a8 2023-08-23 op static int
94 f9ab77a8 2023-08-23 op by_mem_ctrl(X509_LOOKUP *lu, int cmd, const char *buf,
95 f9ab77a8 2023-08-23 op long type, char **ret)
96 f9ab77a8 2023-08-23 op {
97 f9ab77a8 2023-08-23 op STACK_OF(X509_INFO) *inf = NULL;
98 f9ab77a8 2023-08-23 op const struct iovec *iov;
99 f9ab77a8 2023-08-23 op X509_INFO *itmp;
100 f9ab77a8 2023-08-23 op BIO *in = NULL;
101 f9ab77a8 2023-08-23 op int i, count = 0, ok = 0;
102 f9ab77a8 2023-08-23 op
103 f9ab77a8 2023-08-23 op iov = (const struct iovec *)buf;
104 f9ab77a8 2023-08-23 op
105 f9ab77a8 2023-08-23 op if (!(cmd == X509_L_MEM && type == X509_FILETYPE_PEM))
106 f9ab77a8 2023-08-23 op goto done;
107 f9ab77a8 2023-08-23 op
108 f9ab77a8 2023-08-23 op if ((in = BIO_new_mem_buf(iov->iov_base, iov->iov_len)) == NULL)
109 f9ab77a8 2023-08-23 op goto done;
110 f9ab77a8 2023-08-23 op
111 f9ab77a8 2023-08-23 op if ((inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL)) == NULL)
112 f9ab77a8 2023-08-23 op goto done;
113 f9ab77a8 2023-08-23 op
114 f9ab77a8 2023-08-23 op for (i = 0; i < sk_X509_INFO_num(inf); i++) {
115 f9ab77a8 2023-08-23 op itmp = sk_X509_INFO_value(inf, i);
116 f9ab77a8 2023-08-23 op if (itmp->x509) {
117 f9ab77a8 2023-08-23 op ok = X509_STORE_add_cert(X509_LOOKUP_get_store(lu), itmp->x509);
118 f9ab77a8 2023-08-23 op if (!ok)
119 f9ab77a8 2023-08-23 op goto done;
120 f9ab77a8 2023-08-23 op count++;
121 f9ab77a8 2023-08-23 op }
122 f9ab77a8 2023-08-23 op if (itmp->crl) {
123 f9ab77a8 2023-08-23 op ok = X509_STORE_add_crl(X509_LOOKUP_get_store(lu), itmp->crl);
124 f9ab77a8 2023-08-23 op if (!ok)
125 f9ab77a8 2023-08-23 op goto done;
126 f9ab77a8 2023-08-23 op count++;
127 f9ab77a8 2023-08-23 op }
128 f9ab77a8 2023-08-23 op }
129 f9ab77a8 2023-08-23 op
130 f9ab77a8 2023-08-23 op ok = count != 0;
131 f9ab77a8 2023-08-23 op done:
132 f9ab77a8 2023-08-23 op if (count == 0)
133 f9ab77a8 2023-08-23 op X509error(ERR_R_PEM_LIB);
134 f9ab77a8 2023-08-23 op if (inf != NULL)
135 f9ab77a8 2023-08-23 op sk_X509_INFO_pop_free(inf, X509_INFO_free);
136 f9ab77a8 2023-08-23 op if (in != NULL)
137 f9ab77a8 2023-08-23 op BIO_free(in);
138 f9ab77a8 2023-08-23 op return (ok);
139 f9ab77a8 2023-08-23 op }
140 f9ab77a8 2023-08-23 op
141 f9ab77a8 2023-08-23 op #endif /* HAVE_X509_LOOKUP_MEM */