/src/openssl30/providers/implementations/encode_decode/decode_msblob2key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2023 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 | | * low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <string.h> |
17 | | |
18 | | #include <openssl/core_dispatch.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/core_object.h> |
21 | | #include <openssl/crypto.h> |
22 | | #include <openssl/params.h> |
23 | | #include <openssl/pem.h> /* For public PVK functions */ |
24 | | #include <openssl/x509.h> |
25 | | #include <openssl/err.h> |
26 | | #include "internal/passphrase.h" |
27 | | #include "crypto/pem.h" /* For internal PVK and "blob" headers */ |
28 | | #include "crypto/rsa.h" |
29 | | #include "prov/bio.h" |
30 | | #include "prov/implementations.h" |
31 | | #include "endecoder_local.h" |
32 | | |
33 | | struct msblob2key_ctx_st; /* Forward declaration */ |
34 | | typedef void *b2i_of_void_fn(const unsigned char **in, unsigned int bitlen, |
35 | | int ispub); |
36 | | typedef void adjust_key_fn(void *, struct msblob2key_ctx_st *ctx); |
37 | | typedef void free_key_fn(void *); |
38 | | struct keytype_desc_st { |
39 | | int type; /* EVP key type */ |
40 | | const char *name; /* Keytype */ |
41 | | const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */ |
42 | | |
43 | | b2i_of_void_fn *read_private_key; |
44 | | b2i_of_void_fn *read_public_key; |
45 | | adjust_key_fn *adjust_key; |
46 | | free_key_fn *free_key; |
47 | | }; |
48 | | |
49 | | static OSSL_FUNC_decoder_freectx_fn msblob2key_freectx; |
50 | | static OSSL_FUNC_decoder_decode_fn msblob2key_decode; |
51 | | static OSSL_FUNC_decoder_export_object_fn msblob2key_export_object; |
52 | | |
53 | | /* |
54 | | * Context used for DER to key decoding. |
55 | | */ |
56 | | struct msblob2key_ctx_st { |
57 | | PROV_CTX *provctx; |
58 | | const struct keytype_desc_st *desc; |
59 | | /* The selection that is passed to msblob2key_decode() */ |
60 | | int selection; |
61 | | }; |
62 | | |
63 | | static struct msblob2key_ctx_st * |
64 | | msblob2key_newctx(void *provctx, const struct keytype_desc_st *desc) |
65 | 358k | { |
66 | 358k | struct msblob2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
67 | | |
68 | 358k | if (ctx != NULL) { |
69 | 358k | ctx->provctx = provctx; |
70 | 358k | ctx->desc = desc; |
71 | 358k | } |
72 | 358k | return ctx; |
73 | 358k | } |
74 | | |
75 | | static void msblob2key_freectx(void *vctx) |
76 | 358k | { |
77 | 358k | struct msblob2key_ctx_st *ctx = vctx; |
78 | | |
79 | 358k | OPENSSL_free(ctx); |
80 | 358k | } |
81 | | |
82 | | static int msblob2key_does_selection(void *provctx, int selection) |
83 | 1.01M | { |
84 | 1.01M | if (selection == 0) |
85 | 4 | return 1; |
86 | | |
87 | 1.01M | if ((selection & (OSSL_KEYMGMT_SELECT_PRIVATE_KEY |
88 | 1.01M | | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) != 0) |
89 | 1.01M | return 1; |
90 | | |
91 | 0 | return 0; |
92 | 1.01M | } |
93 | | |
94 | | static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, |
95 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
96 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
97 | 17.0k | { |
98 | 17.0k | struct msblob2key_ctx_st *ctx = vctx; |
99 | 17.0k | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
100 | 17.0k | const unsigned char *p; |
101 | 17.0k | unsigned char hdr_buf[16], *buf = NULL; |
102 | 17.0k | unsigned int bitlen, magic, length; |
103 | 17.0k | int isdss = -1; |
104 | 17.0k | int ispub = -1; |
105 | 17.0k | void *key = NULL; |
106 | 17.0k | int ok = 0; |
107 | | |
108 | 17.0k | if (in == NULL) |
109 | 0 | return 0; |
110 | | |
111 | 17.0k | if (BIO_read(in, hdr_buf, 16) != 16) { |
112 | 5.88k | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
113 | 5.88k | goto next; |
114 | 5.88k | } |
115 | 11.1k | ERR_set_mark(); |
116 | 11.1k | p = hdr_buf; |
117 | 11.1k | ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0; |
118 | 11.1k | ERR_pop_to_mark(); |
119 | 11.1k | if (!ok) |
120 | 9.06k | goto next; |
121 | | |
122 | 2.08k | ctx->selection = selection; |
123 | 2.08k | ok = 0; /* Assume that we fail */ |
124 | | |
125 | 2.08k | if ((isdss && ctx->desc->type != EVP_PKEY_DSA) |
126 | 2.08k | || (!isdss && ctx->desc->type != EVP_PKEY_RSA)) |
127 | 832 | goto next; |
128 | | |
129 | 1.25k | length = ossl_blob_length(bitlen, isdss, ispub); |
130 | 1.25k | if (length > BLOB_MAX_LENGTH) { |
131 | 21 | ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG); |
132 | 21 | goto next; |
133 | 21 | } |
134 | 1.22k | buf = OPENSSL_malloc(length); |
135 | 1.22k | if (buf == NULL) { |
136 | 0 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); |
137 | 0 | goto end; |
138 | 0 | } |
139 | 1.22k | p = buf; |
140 | 1.22k | if (BIO_read(in, buf, length) != (int)length) { |
141 | 48 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
142 | 48 | goto next; |
143 | 48 | } |
144 | | |
145 | 1.18k | if ((selection == 0 |
146 | 1.18k | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
147 | 1.18k | && !ispub |
148 | 1.18k | && ctx->desc->read_private_key != NULL) { |
149 | 522 | struct ossl_passphrase_data_st pwdata; |
150 | | |
151 | 522 | memset(&pwdata, 0, sizeof(pwdata)); |
152 | 522 | if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg)) |
153 | 0 | goto end; |
154 | 522 | p = buf; |
155 | 522 | key = ctx->desc->read_private_key(&p, bitlen, ispub); |
156 | 522 | if (selection != 0 && key == NULL) |
157 | 0 | goto next; |
158 | 522 | } |
159 | 1.18k | if (key == NULL && (selection == 0 |
160 | 661 | || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
161 | 1.18k | && ispub |
162 | 1.18k | && ctx->desc->read_public_key != NULL) { |
163 | 659 | p = buf; |
164 | 659 | key = ctx->desc->read_public_key(&p, bitlen, ispub); |
165 | 659 | if (selection != 0 && key == NULL) |
166 | 0 | goto next; |
167 | 659 | } |
168 | | |
169 | 1.18k | if (key != NULL && ctx->desc->adjust_key != NULL) |
170 | 761 | ctx->desc->adjust_key(key, ctx); |
171 | | |
172 | 17.0k | next: |
173 | | /* |
174 | | * Indicated that we successfully decoded something, or not at all. |
175 | | * Ending up "empty handed" is not an error. |
176 | | */ |
177 | 17.0k | ok = 1; |
178 | | |
179 | | /* |
180 | | * We free resources here so it's not held up during the callback, because |
181 | | * we know the process is recursive and the allocated chunks of memory |
182 | | * add up. |
183 | | */ |
184 | 17.0k | OPENSSL_free(buf); |
185 | 17.0k | BIO_free(in); |
186 | 17.0k | buf = NULL; |
187 | 17.0k | in = NULL; |
188 | | |
189 | 17.0k | if (key != NULL) { |
190 | 1.17k | OSSL_PARAM params[4]; |
191 | 1.17k | int object_type = OSSL_OBJECT_PKEY; |
192 | | |
193 | 1.17k | params[0] = |
194 | 1.17k | OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); |
195 | 1.17k | params[1] = |
196 | 1.17k | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
197 | 1.17k | (char *)ctx->desc->name, 0); |
198 | | /* The address of the key becomes the octet string */ |
199 | 1.17k | params[2] = |
200 | 1.17k | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, |
201 | 1.17k | &key, sizeof(key)); |
202 | 1.17k | params[3] = OSSL_PARAM_construct_end(); |
203 | | |
204 | 1.17k | ok = data_cb(params, data_cbarg); |
205 | 1.17k | } |
206 | | |
207 | 17.0k | end: |
208 | 17.0k | BIO_free(in); |
209 | 17.0k | OPENSSL_free(buf); |
210 | 17.0k | ctx->desc->free_key(key); |
211 | | |
212 | 17.0k | return ok; |
213 | 17.0k | } |
214 | | |
215 | | static int |
216 | | msblob2key_export_object(void *vctx, |
217 | | const void *reference, size_t reference_sz, |
218 | | OSSL_CALLBACK *export_cb, void *export_cbarg) |
219 | 0 | { |
220 | 0 | struct msblob2key_ctx_st *ctx = vctx; |
221 | 0 | OSSL_FUNC_keymgmt_export_fn *export = |
222 | 0 | ossl_prov_get_keymgmt_export(ctx->desc->fns); |
223 | 0 | void *keydata; |
224 | |
|
225 | 0 | if (reference_sz == sizeof(keydata) && export != NULL) { |
226 | 0 | int selection = ctx->selection; |
227 | |
|
228 | 0 | if (selection == 0) |
229 | 0 | selection = OSSL_KEYMGMT_SELECT_ALL; |
230 | | /* The contents of the reference is the address to our object */ |
231 | 0 | keydata = *(void **)reference; |
232 | |
|
233 | 0 | return export(keydata, selection, export_cb, export_cbarg); |
234 | 0 | } |
235 | 0 | return 0; |
236 | 0 | } |
237 | | |
238 | | /* ---------------------------------------------------------------------- */ |
239 | | |
240 | | #define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header |
241 | | #define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header |
242 | | #define dsa_adjust NULL |
243 | | #define dsa_free (void (*)(void *))DSA_free |
244 | | |
245 | | /* ---------------------------------------------------------------------- */ |
246 | | |
247 | | #define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header |
248 | | #define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header |
249 | | |
250 | | static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx) |
251 | 761 | { |
252 | 761 | ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); |
253 | 761 | } |
254 | | |
255 | | #define rsa_free (void (*)(void *))RSA_free |
256 | | |
257 | | /* ---------------------------------------------------------------------- */ |
258 | | |
259 | | #define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \ |
260 | | static const struct keytype_desc_st mstype##2##keytype##_desc = { \ |
261 | | EVP_PKEY_##KEYTYPE, #KEYTYPE, \ |
262 | | ossl_##keytype##_keymgmt_functions, \ |
263 | | keytype##_decode_private_key, \ |
264 | | keytype##_decode_public_key, \ |
265 | | keytype##_adjust, \ |
266 | | keytype##_free \ |
267 | | }; \ |
268 | | static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \ |
269 | | static void *msblob2##keytype##_newctx(void *provctx) \ |
270 | 358k | { \ |
271 | 358k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ |
272 | 358k | } \ decode_msblob2key.c:msblob2dsa_newctx Line | Count | Source | 270 | 203k | { \ | 271 | 203k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ | 272 | 203k | } \ |
decode_msblob2key.c:msblob2rsa_newctx Line | Count | Source | 270 | 155k | { \ | 271 | 155k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ | 272 | 155k | } \ |
|
273 | | const OSSL_DISPATCH \ |
274 | | ossl_msblob_to_##keytype##_decoder_functions[] = { \ |
275 | | { OSSL_FUNC_DECODER_NEWCTX, \ |
276 | | (void (*)(void))msblob2##keytype##_newctx }, \ |
277 | | { OSSL_FUNC_DECODER_FREECTX, \ |
278 | | (void (*)(void))msblob2key_freectx }, \ |
279 | | { OSSL_FUNC_DECODER_DOES_SELECTION, \ |
280 | | (void (*)(void))msblob2key_does_selection }, \ |
281 | | { OSSL_FUNC_DECODER_DECODE, \ |
282 | | (void (*)(void))msblob2key_decode }, \ |
283 | | { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ |
284 | | (void (*)(void))msblob2key_export_object }, \ |
285 | | { 0, NULL } \ |
286 | | } |
287 | | |
288 | | #ifndef OPENSSL_NO_DSA |
289 | | IMPLEMENT_MSBLOB(DSA, dsa); |
290 | | #endif |
291 | | IMPLEMENT_MSBLOB(RSA, rsa); |