/src/openssl30/crypto/ct/ct_b64.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <limits.h> |
11 | | #include <string.h> |
12 | | |
13 | | #include <openssl/ct.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/evp.h> |
16 | | |
17 | | #include "ct_local.h" |
18 | | |
19 | | /* |
20 | | * Decodes the base64 string |in| into |out|. |
21 | | * A new string will be malloc'd and assigned to |out|. This will be owned by |
22 | | * the caller. Do not provide a pre-allocated string in |out|. |
23 | | */ |
24 | | static int ct_base64_decode(const char *in, unsigned char **out) |
25 | 0 | { |
26 | 0 | size_t inlen = strlen(in); |
27 | 0 | int outlen, i; |
28 | 0 | unsigned char *outbuf = NULL; |
29 | |
|
30 | 0 | if (inlen == 0) { |
31 | 0 | *out = NULL; |
32 | 0 | return 0; |
33 | 0 | } |
34 | | |
35 | 0 | outlen = (inlen / 4) * 3; |
36 | 0 | outbuf = OPENSSL_malloc(outlen); |
37 | 0 | if (outbuf == NULL) { |
38 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); |
39 | 0 | goto err; |
40 | 0 | } |
41 | | |
42 | 0 | outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen); |
43 | 0 | if (outlen < 0) { |
44 | 0 | ERR_raise(ERR_LIB_CT, CT_R_BASE64_DECODE_ERROR); |
45 | 0 | goto err; |
46 | 0 | } |
47 | | |
48 | | /* Subtract padding bytes from |outlen|. Any more than 2 is malformed. */ |
49 | 0 | i = 0; |
50 | 0 | while (in[--inlen] == '=') { |
51 | 0 | --outlen; |
52 | 0 | if (++i > 2) |
53 | 0 | goto err; |
54 | 0 | } |
55 | | |
56 | 0 | *out = outbuf; |
57 | 0 | return outlen; |
58 | 0 | err: |
59 | 0 | OPENSSL_free(outbuf); |
60 | 0 | return -1; |
61 | 0 | } |
62 | | |
63 | | SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, |
64 | | ct_log_entry_type_t entry_type, uint64_t timestamp, |
65 | | const char *extensions_base64, |
66 | | const char *signature_base64) |
67 | 0 | { |
68 | 0 | SCT *sct = SCT_new(); |
69 | 0 | unsigned char *dec = NULL; |
70 | 0 | const unsigned char* p = NULL; |
71 | 0 | int declen; |
72 | |
|
73 | 0 | if (sct == NULL) { |
74 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); |
75 | 0 | return NULL; |
76 | 0 | } |
77 | | |
78 | | /* |
79 | | * RFC6962 section 4.1 says we "MUST NOT expect this to be 0", but we |
80 | | * can only construct SCT versions that have been defined. |
81 | | */ |
82 | 0 | if (!SCT_set_version(sct, version)) { |
83 | 0 | ERR_raise(ERR_LIB_CT, CT_R_SCT_UNSUPPORTED_VERSION); |
84 | 0 | goto err; |
85 | 0 | } |
86 | | |
87 | 0 | declen = ct_base64_decode(logid_base64, &dec); |
88 | 0 | if (declen < 0) { |
89 | 0 | ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR); |
90 | 0 | goto err; |
91 | 0 | } |
92 | 0 | if (!SCT_set0_log_id(sct, dec, declen)) |
93 | 0 | goto err; |
94 | 0 | dec = NULL; |
95 | |
|
96 | 0 | declen = ct_base64_decode(extensions_base64, &dec); |
97 | 0 | if (declen < 0) { |
98 | 0 | ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR); |
99 | 0 | goto err; |
100 | 0 | } |
101 | 0 | SCT_set0_extensions(sct, dec, declen); |
102 | 0 | dec = NULL; |
103 | |
|
104 | 0 | declen = ct_base64_decode(signature_base64, &dec); |
105 | 0 | if (declen < 0) { |
106 | 0 | ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR); |
107 | 0 | goto err; |
108 | 0 | } |
109 | | |
110 | 0 | p = dec; |
111 | 0 | if (o2i_SCT_signature(sct, &p, declen) <= 0) |
112 | 0 | goto err; |
113 | 0 | OPENSSL_free(dec); |
114 | 0 | dec = NULL; |
115 | |
|
116 | 0 | SCT_set_timestamp(sct, timestamp); |
117 | |
|
118 | 0 | if (!SCT_set_log_entry_type(sct, entry_type)) |
119 | 0 | goto err; |
120 | | |
121 | 0 | return sct; |
122 | | |
123 | 0 | err: |
124 | 0 | OPENSSL_free(dec); |
125 | 0 | SCT_free(sct); |
126 | 0 | return NULL; |
127 | 0 | } |
128 | | |
129 | | /* |
130 | | * Allocate, build and returns a new |ct_log| from input |pkey_base64| |
131 | | * It returns 1 on success, |
132 | | * 0 on decoding failure, or invalid parameter if any |
133 | | * -1 on internal (malloc) failure |
134 | | */ |
135 | | int CTLOG_new_from_base64_ex(CTLOG **ct_log, const char *pkey_base64, |
136 | | const char *name, OSSL_LIB_CTX *libctx, |
137 | | const char *propq) |
138 | 0 | { |
139 | 0 | unsigned char *pkey_der = NULL; |
140 | 0 | int pkey_der_len; |
141 | 0 | const unsigned char *p; |
142 | 0 | EVP_PKEY *pkey = NULL; |
143 | |
|
144 | 0 | if (ct_log == NULL) { |
145 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_PASSED_INVALID_ARGUMENT); |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | 0 | pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der); |
150 | 0 | if (pkey_der_len < 0) { |
151 | 0 | ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID_KEY); |
152 | 0 | return 0; |
153 | 0 | } |
154 | | |
155 | 0 | p = pkey_der; |
156 | 0 | pkey = d2i_PUBKEY_ex(NULL, &p, pkey_der_len, libctx, propq); |
157 | 0 | OPENSSL_free(pkey_der); |
158 | 0 | if (pkey == NULL) { |
159 | 0 | ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID_KEY); |
160 | 0 | return 0; |
161 | 0 | } |
162 | | |
163 | 0 | *ct_log = CTLOG_new_ex(pkey, name, libctx, propq); |
164 | 0 | if (*ct_log == NULL) { |
165 | 0 | EVP_PKEY_free(pkey); |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | 0 | return 1; |
170 | 0 | } |
171 | | |
172 | | int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64, |
173 | | const char *name) |
174 | 0 | { |
175 | 0 | return CTLOG_new_from_base64_ex(ct_log, pkey_base64, name, NULL, NULL); |
176 | 0 | } |