1 /*
2 * Copyright 2020-2026 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 /*
11 * This is a decoder that's completely internal to the 'file:' store
12 * implementation. Only code in file_store.c know about this one. Because
13 * of this close relationship, we can cut certain corners, such as making
14 * assumptions about the "provider context", which is currently simply the
15 * provider context that the file_store.c code operates within.
16 *
17 * All this does is to read known binary encodings (currently: DER, MSBLOB,
18 * PVK) from the input if it can, and passes it on to the data callback as
19 * an object abstraction, leaving it to the callback to figure out what it
20 * actually is.
21 *
22 * This MUST be made the last decoder in a chain, leaving it to other more
23 * specialized decoders to recognise and process their stuff first.
24 */
25
26 #include <openssl/core_dispatch.h>
27 #include <openssl/core_names.h>
28 #include <openssl/core_object.h>
29 #include <openssl/bio.h>
30 #include <openssl/buffer.h>
31 #include <openssl/err.h>
32 #include <openssl/asn1err.h>
33 #include <openssl/params.h>
34 #include "internal/asn1.h"
35 #include "internal/sizes.h"
36 #include "crypto/pem.h" /* For internal PVK and "blob" headers */
37 #include "prov/bio.h"
38 #include "file_store_local.h"
39
40 /*
41 * newctx and freectx are not strictly necessary. However, the method creator,
42 * ossl_decoder_from_algorithm(), demands that they exist, so we make sure to
43 * oblige.
44 */
45
46 static OSSL_FUNC_decoder_newctx_fn any2obj_newctx;
47 static OSSL_FUNC_decoder_freectx_fn any2obj_freectx;
48
49 struct any2obj_ctx_st {
50 PROV_CTX *provctx;
51 char data_structure[OSSL_MAX_CODEC_STRUCT_SIZE];
52 };
53
any2obj_newctx(void * provctx)54 static void *any2obj_newctx(void *provctx)
55 {
56 struct any2obj_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
57
58 if (ctx != NULL)
59 ctx->provctx = provctx;
60 return ctx;
61 }
62
any2obj_freectx(void * ctx)63 static void any2obj_freectx(void *ctx)
64 {
65 OPENSSL_free(ctx);
66 }
67
any2obj_set_ctx_params(void * vctx,const OSSL_PARAM params[])68 static int any2obj_set_ctx_params(void *vctx, const OSSL_PARAM params[])
69 {
70 struct any2obj_ctx_st *ctx = vctx;
71 const OSSL_PARAM *p;
72 char *str;
73
74 p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
75 str = ctx->data_structure;
76 if (p != NULL
77 && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->data_structure)))
78 return 0;
79
80 return 1;
81 }
82
any2obj_settable_ctx_params(ossl_unused void * provctx)83 static const OSSL_PARAM *any2obj_settable_ctx_params(ossl_unused void *provctx)
84 {
85 static const OSSL_PARAM settables[] = {
86 OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, NULL, 0),
87 OSSL_PARAM_END
88 };
89 return settables;
90 }
91
any2obj_decode_final(void * vctx,int objtype,const char * input_type,const char * data_type,BUF_MEM * mem,OSSL_CALLBACK * data_cb,void * data_cbarg)92 static int any2obj_decode_final(void *vctx, int objtype, const char *input_type,
93 const char *data_type, BUF_MEM *mem,
94 OSSL_CALLBACK *data_cb, void *data_cbarg)
95 {
96 struct any2obj_ctx_st *ctx = vctx;
97 /*
98 * 1 indicates that we successfully decoded something, or not at all.
99 * Ending up "empty handed" is not an error.
100 */
101 int ok = 1;
102
103 if (mem != NULL) {
104 OSSL_PARAM params[6], *p = params;
105
106 if (data_type != NULL)
107 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
108 (char *)data_type, 0);
109 if (input_type != NULL)
110 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_INPUT_TYPE,
111 (char *)input_type, 0);
112 if (*ctx->data_structure != '\0')
113 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
114 (char *)ctx->data_structure, 0);
115 *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
116 *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
117 mem->data, mem->length);
118 *p = OSSL_PARAM_construct_end();
119
120 ok = data_cb(params, data_cbarg);
121 BUF_MEM_free(mem);
122 }
123 return ok;
124 }
125
126 static OSSL_FUNC_decoder_decode_fn der2obj_decode;
der2obj_decode(void * vctx,OSSL_CORE_BIO * cin,int selection,OSSL_CALLBACK * data_cb,void * data_cbarg,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)127 static int der2obj_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
128 OSSL_CALLBACK *data_cb, void *data_cbarg,
129 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
130 {
131 struct any2obj_ctx_st *ctx = vctx;
132 BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
133 BUF_MEM *mem = NULL;
134 int ok;
135
136 if (in == NULL)
137 return 0;
138
139 ERR_set_mark();
140 ok = (asn1_d2i_read_bio(in, &mem) >= 0);
141 ERR_pop_to_mark();
142 if (!ok && mem != NULL) {
143 BUF_MEM_free(mem);
144 mem = NULL;
145 }
146 BIO_free(in);
147
148 /* any2obj_decode_final() frees |mem| for us */
149 return any2obj_decode_final(ctx, OSSL_OBJECT_UNKNOWN, NULL, NULL, mem,
150 data_cb, data_cbarg);
151 }
152
153 static OSSL_FUNC_decoder_decode_fn msblob2obj_decode;
msblob2obj_decode(void * vctx,OSSL_CORE_BIO * cin,int selection,OSSL_CALLBACK * data_cb,void * data_cbarg,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)154 static int msblob2obj_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
155 OSSL_CALLBACK *data_cb, void *data_cbarg,
156 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
157 {
158 struct any2obj_ctx_st *ctx = vctx;
159 BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
160 BUF_MEM *mem = NULL;
161 size_t mem_len = 0, mem_want;
162 const unsigned char *p;
163 unsigned int bitlen, magic;
164 int isdss = -1;
165 int ispub = -1;
166 int ok = 0;
167
168 if (in == NULL)
169 goto err;
170
171 mem_want = 16; /* The size of the MSBLOB header */
172 if ((mem = BUF_MEM_new()) == NULL
173 || !BUF_MEM_grow(mem, mem_want)) {
174 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
175 goto err;
176 }
177
178 ERR_set_mark();
179 ok = BIO_read(in, &mem->data[0], mem_want) == (int)mem_want;
180 mem_len += mem_want;
181 ERR_pop_to_mark();
182 if (!ok)
183 goto next;
184
185 ERR_set_mark();
186 p = (unsigned char *)&mem->data[0];
187 ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
188 ERR_pop_to_mark();
189 if (!ok)
190 goto next;
191
192 ok = 0;
193 mem_want = ossl_blob_length(bitlen, isdss, ispub);
194
195 if (mem_want > BLOB_MAX_LENGTH) {
196 goto next;
197 }
198 if (!BUF_MEM_grow(mem, mem_len + mem_want)) {
199 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
200 goto err;
201 }
202
203 ERR_set_mark();
204 ok = BIO_read(in, &mem->data[mem_len], mem_want) == (int)mem_want;
205 mem_len += mem_want;
206 ERR_pop_to_mark();
207
208 next:
209 /* Free resources we no longer need. */
210 BIO_free(in);
211 if (!ok && mem != NULL) {
212 BUF_MEM_free(mem);
213 mem = NULL;
214 }
215
216 /* any2obj_decode_final() frees |mem| for us */
217 return any2obj_decode_final(ctx, OSSL_OBJECT_PKEY, "msblob",
218 isdss ? "DSA" : "RSA", mem,
219 data_cb, data_cbarg);
220
221 err:
222 BIO_free(in);
223 BUF_MEM_free(mem);
224 return 0;
225 }
226
227 static OSSL_FUNC_decoder_decode_fn pvk2obj_decode;
pvk2obj_decode(void * vctx,OSSL_CORE_BIO * cin,int selection,OSSL_CALLBACK * data_cb,void * data_cbarg,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)228 static int pvk2obj_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
229 OSSL_CALLBACK *data_cb, void *data_cbarg,
230 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
231 {
232 struct any2obj_ctx_st *ctx = vctx;
233 BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
234 BUF_MEM *mem = NULL;
235 size_t mem_len = 0, mem_want;
236 const unsigned char *p;
237 unsigned int saltlen, keylen;
238 int ok = 0, isdss = -1;
239
240 if (in == NULL)
241 goto err;
242
243 mem_want = 24; /* The size of the PVK header */
244 if ((mem = BUF_MEM_new()) == NULL
245 || !BUF_MEM_grow(mem, mem_want)) {
246 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
247 goto err;
248 }
249
250 ERR_set_mark();
251 ok = BIO_read(in, &mem->data[0], mem_want) == (int)mem_want;
252 mem_len += mem_want;
253 ERR_pop_to_mark();
254 if (!ok)
255 goto next;
256
257 ERR_set_mark();
258 p = (unsigned char *)&mem->data[0];
259 ok = ossl_do_PVK_header(&p, 24, 0, &isdss, &saltlen, &keylen) > 0;
260 ERR_pop_to_mark();
261 if (!ok)
262 goto next;
263
264 ok = 0;
265 mem_want = saltlen + keylen;
266 if (!BUF_MEM_grow(mem, mem_len + mem_want)) {
267 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
268 goto err;
269 }
270
271 ERR_set_mark();
272 ok = BIO_read(in, &mem->data[mem_len], mem_want) == (int)mem_want;
273 mem_len += mem_want;
274 ERR_pop_to_mark();
275
276 next:
277 /* Free resources we no longer need. */
278 BIO_free(in);
279 if (!ok && mem != NULL) {
280 BUF_MEM_free(mem);
281 mem = NULL;
282 }
283
284 /* any2obj_decode_final() frees |mem| for us */
285 return any2obj_decode_final(ctx, OSSL_OBJECT_PKEY, "pvk",
286 ok ? (isdss ? "DSA" : "RSA") : NULL, mem,
287 data_cb, data_cbarg);
288
289 err:
290 BIO_free(in);
291 BUF_MEM_free(mem);
292 return 0;
293 }
294
295 #define MAKE_DECODER(fromtype, objtype) \
296 static const OSSL_DISPATCH fromtype##_to_obj_decoder_functions[] = { \
297 { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))any2obj_newctx }, \
298 { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))any2obj_freectx }, \
299 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))fromtype##2obj_decode }, \
300 { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \
301 (void (*)(void))any2obj_settable_ctx_params }, \
302 { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \
303 (void (*)(void))any2obj_set_ctx_params }, \
304 OSSL_DISPATCH_END \
305 }
306
307 MAKE_DECODER(der, OSSL_OBJECT_UNKNOWN);
308 MAKE_DECODER(msblob, OSSL_OBJECT_PKEY);
309 MAKE_DECODER(pvk, OSSL_OBJECT_PKEY);
310
311 const OSSL_ALGORITHM ossl_any_to_obj_algorithm[] = {
312 { "obj", "input=DER", der_to_obj_decoder_functions },
313 { "obj", "input=MSBLOB", msblob_to_obj_decoder_functions },
314 { "obj", "input=PVK", pvk_to_obj_decoder_functions },
315 {
316 NULL,
317 }
318 };
319