1 /*
2 * Copyright 2020-2025 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
186 ERR_set_mark();
187 p = (unsigned char *)&mem->data[0];
188 ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
189 ERR_pop_to_mark();
190 if (!ok)
191 goto next;
192
193 ok = 0;
194 mem_want = ossl_blob_length(bitlen, isdss, ispub);
195 if (!BUF_MEM_grow(mem, mem_len + mem_want)) {
196 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
197 goto err;
198 }
199
200 ERR_set_mark();
201 ok = BIO_read(in, &mem->data[mem_len], mem_want) == (int)mem_want;
202 mem_len += mem_want;
203 ERR_pop_to_mark();
204
205 next:
206 /* Free resources we no longer need. */
207 BIO_free(in);
208 if (!ok && mem != NULL) {
209 BUF_MEM_free(mem);
210 mem = NULL;
211 }
212
213 /* any2obj_decode_final() frees |mem| for us */
214 return any2obj_decode_final(ctx, OSSL_OBJECT_PKEY, "msblob",
215 isdss ? "DSA" : "RSA", mem,
216 data_cb, data_cbarg);
217
218 err:
219 BIO_free(in);
220 BUF_MEM_free(mem);
221 return 0;
222 }
223
224 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)225 static int pvk2obj_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
226 OSSL_CALLBACK *data_cb, void *data_cbarg,
227 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
228 {
229 struct any2obj_ctx_st *ctx = vctx;
230 BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
231 BUF_MEM *mem = NULL;
232 size_t mem_len = 0, mem_want;
233 const unsigned char *p;
234 unsigned int saltlen, keylen;
235 int ok = 0, isdss = -1;
236
237 if (in == NULL)
238 goto err;
239
240 mem_want = 24; /* The size of the PVK header */
241 if ((mem = BUF_MEM_new()) == NULL
242 || !BUF_MEM_grow(mem, mem_want)) {
243 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
244 goto err;
245 }
246
247 ERR_set_mark();
248 ok = BIO_read(in, &mem->data[0], mem_want) == (int)mem_want;
249 mem_len += mem_want;
250 ERR_pop_to_mark();
251 if (!ok)
252 goto next;
253
254
255 ERR_set_mark();
256 p = (unsigned char *)&mem->data[0];
257 ok = ossl_do_PVK_header(&p, 24, 0, &isdss, &saltlen, &keylen) > 0;
258 ERR_pop_to_mark();
259 if (!ok)
260 goto next;
261
262 ok = 0;
263 mem_want = saltlen + keylen;
264 if (!BUF_MEM_grow(mem, mem_len + mem_want)) {
265 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
266 goto err;
267 }
268
269 ERR_set_mark();
270 ok = BIO_read(in, &mem->data[mem_len], mem_want) == (int)mem_want;
271 mem_len += mem_want;
272 ERR_pop_to_mark();
273
274 next:
275 /* Free resources we no longer need. */
276 BIO_free(in);
277 if (!ok && mem != NULL) {
278 BUF_MEM_free(mem);
279 mem = NULL;
280 }
281
282 /* any2obj_decode_final() frees |mem| for us */
283 return any2obj_decode_final(ctx, OSSL_OBJECT_PKEY, "pvk",
284 ok ? (isdss ? "DSA" : "RSA") : NULL, mem,
285 data_cb, data_cbarg);
286
287 err:
288 BIO_free(in);
289 BUF_MEM_free(mem);
290 return 0;
291 }
292
293 #define MAKE_DECODER(fromtype, objtype) \
294 static const OSSL_DISPATCH fromtype##_to_obj_decoder_functions[] = { \
295 { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))any2obj_newctx }, \
296 { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))any2obj_freectx }, \
297 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))fromtype##2obj_decode }, \
298 { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \
299 (void (*)(void))any2obj_settable_ctx_params }, \
300 { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \
301 (void (*)(void))any2obj_set_ctx_params }, \
302 OSSL_DISPATCH_END \
303 }
304
305 MAKE_DECODER(der, OSSL_OBJECT_UNKNOWN);
306 MAKE_DECODER(msblob, OSSL_OBJECT_PKEY);
307 MAKE_DECODER(pvk, OSSL_OBJECT_PKEY);
308
309 const OSSL_ALGORITHM ossl_any_to_obj_algorithm[] = {
310 { "obj", "input=DER", der_to_obj_decoder_functions },
311 { "obj", "input=MSBLOB", msblob_to_obj_decoder_functions },
312 { "obj", "input=PVK", pvk_to_obj_decoder_functions },
313 { NULL, }
314 };
315