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