ct_b64.c revision 1.5
1/*	$OpenBSD: ct_b64.c,v 1.5 2021/12/18 16:34:52 tb Exp $ */
2/*
3 * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
4 * (steve@openssl.org) for the OpenSSL project 2014.
5 */
6/* ====================================================================
7 * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 *    software must display the following acknowledgment:
23 *    "This product includes software developed by the OpenSSL Project
24 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 *    endorse or promote products derived from this software without
28 *    prior written permission. For written permission, please contact
29 *    licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 *    nor may "OpenSSL" appear in their names without prior written
33 *    permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 *    acknowledgment:
37 *    "This product includes software developed by the OpenSSL Project
38 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com).  This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60#include <limits.h>
61#include <string.h>
62
63#include <openssl/ct.h>
64#include <openssl/err.h>
65#include <openssl/evp.h>
66
67#include "ct_local.h"
68
69/*
70 * Decodes the base64 string |in| into |out|.
71 * A new string will be malloc'd and assigned to |out|. This will be owned by
72 * the caller. Do not provide a pre-allocated string in |out|.
73 */
74static int
75ct_base64_decode(const char *in, unsigned char **out)
76{
77	size_t inlen = strlen(in);
78	int outlen, i;
79	unsigned char *outbuf = NULL;
80
81	if (inlen == 0) {
82		*out = NULL;
83		return 0;
84	}
85
86	outlen = (inlen / 4) * 3;
87	outbuf = malloc(outlen);
88	if (outbuf == NULL) {
89		CTerror(ERR_R_MALLOC_FAILURE);
90		goto err;
91	}
92
93	outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
94	if (outlen < 0) {
95		CTerror(CT_R_BASE64_DECODE_ERROR);
96		goto err;
97	}
98
99	/*
100	 * Subtract padding bytes from |outlen|.
101	 * Any more than 2 is malformed.
102	 */
103	i = 0;
104	while (in[--inlen] == '=') {
105		--outlen;
106		if (++i > 2)
107			goto err;
108	}
109
110	*out = outbuf;
111	return outlen;
112 err:
113	free(outbuf);
114	return -1;
115}
116
117SCT *
118SCT_new_from_base64(unsigned char version, const char *logid_base64,
119    ct_log_entry_type_t entry_type, uint64_t timestamp,
120    const char *extensions_base64, const char *signature_base64)
121{
122	SCT *sct = SCT_new();
123	unsigned char *dec = NULL;
124	const unsigned char* p = NULL;
125	int declen;
126
127	if (sct == NULL) {
128		CTerror(ERR_R_MALLOC_FAILURE);
129		return NULL;
130	}
131
132	/*
133	 * RFC6962 section 4.1 says we "MUST NOT expect this to be 0", but we
134	 * can only construct SCT versions that have been defined.
135	 */
136	if (!SCT_set_version(sct, version)) {
137		CTerror(CT_R_SCT_UNSUPPORTED_VERSION);
138		goto err;
139	}
140
141	declen = ct_base64_decode(logid_base64, &dec);
142	if (declen < 0) {
143		CTerror(X509_R_BASE64_DECODE_ERROR);
144		goto err;
145	}
146	if (!SCT_set0_log_id(sct, dec, declen))
147		goto err;
148	dec = NULL;
149
150	declen = ct_base64_decode(extensions_base64, &dec);
151	if (declen < 0) {
152		CTerror(X509_R_BASE64_DECODE_ERROR);
153		goto err;
154	}
155	SCT_set0_extensions(sct, dec, declen);
156	dec = NULL;
157
158	declen = ct_base64_decode(signature_base64, &dec);
159	if (declen < 0) {
160		CTerror(X509_R_BASE64_DECODE_ERROR);
161		goto err;
162	}
163
164	p = dec;
165	if (o2i_SCT_signature(sct, &p, declen) <= 0)
166		goto err;
167	free(dec);
168	dec = NULL;
169
170	SCT_set_timestamp(sct, timestamp);
171
172	if (!SCT_set_log_entry_type(sct, entry_type))
173		goto err;
174
175	return sct;
176
177 err:
178	free(dec);
179	SCT_free(sct);
180	return NULL;
181}
182
183/*
184 * Allocate, build and returns a new |ct_log| from input |pkey_base64|
185 * It returns 1 on success,
186 * 0 on decoding failure, or invalid parameter if any
187 * -1 on internal (malloc) failure
188 */
189int
190CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64, const char *name)
191{
192	unsigned char *pkey_der = NULL;
193	int pkey_der_len;
194	const unsigned char *p;
195	EVP_PKEY *pkey = NULL;
196
197	if (ct_log == NULL) {
198	        CTerror(ERR_R_PASSED_NULL_PARAMETER);
199		return 0;
200	}
201
202	pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);
203	if (pkey_der_len < 0) {
204		CTerror(CT_R_LOG_CONF_INVALID_KEY);
205		return 0;
206	}
207
208	p = pkey_der;
209	pkey = d2i_PUBKEY(NULL, &p, pkey_der_len);
210	free(pkey_der);
211	if (pkey == NULL) {
212		CTerror(CT_R_LOG_CONF_INVALID_KEY);
213		return 0;
214	}
215
216	*ct_log = CTLOG_new(pkey, name);
217	if (*ct_log == NULL) {
218		EVP_PKEY_free(pkey);
219		return 0;
220	}
221
222	return 1;
223}
224