/src/openssl/providers/implementations/encode_decode/decode_msblob2key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2022 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 | 236k | { |
66 | 236k | struct msblob2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
67 | | |
68 | 236k | if (ctx != NULL) { |
69 | 236k | ctx->provctx = provctx; |
70 | 236k | ctx->desc = desc; |
71 | 236k | } |
72 | 236k | return ctx; |
73 | 236k | } |
74 | | |
75 | | static void msblob2key_freectx(void *vctx) |
76 | 236k | { |
77 | 236k | struct msblob2key_ctx_st *ctx = vctx; |
78 | | |
79 | 236k | OPENSSL_free(ctx); |
80 | 236k | } |
81 | | |
82 | | static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, |
83 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
84 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
85 | 0 | { |
86 | 0 | struct msblob2key_ctx_st *ctx = vctx; |
87 | 0 | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
88 | 0 | const unsigned char *p; |
89 | 0 | unsigned char hdr_buf[16], *buf = NULL; |
90 | 0 | unsigned int bitlen, magic, length; |
91 | 0 | int isdss = -1; |
92 | 0 | int ispub = -1; |
93 | 0 | void *key = NULL; |
94 | 0 | int ok = 0; |
95 | |
|
96 | 0 | if (in == NULL) |
97 | 0 | return 0; |
98 | | |
99 | 0 | if (BIO_read(in, hdr_buf, 16) != 16) { |
100 | 0 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
101 | 0 | goto next; |
102 | 0 | } |
103 | 0 | ERR_set_mark(); |
104 | 0 | p = hdr_buf; |
105 | 0 | ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0; |
106 | 0 | ERR_pop_to_mark(); |
107 | 0 | if (!ok) |
108 | 0 | goto next; |
109 | | |
110 | 0 | ctx->selection = selection; |
111 | 0 | ok = 0; /* Assume that we fail */ |
112 | |
|
113 | 0 | if ((isdss && ctx->desc->type != EVP_PKEY_DSA) |
114 | 0 | || (!isdss && ctx->desc->type != EVP_PKEY_RSA)) |
115 | 0 | goto next; |
116 | | |
117 | 0 | length = ossl_blob_length(bitlen, isdss, ispub); |
118 | 0 | if (length > BLOB_MAX_LENGTH) { |
119 | 0 | ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG); |
120 | 0 | goto next; |
121 | 0 | } |
122 | 0 | buf = OPENSSL_malloc(length); |
123 | 0 | if (buf == NULL) |
124 | 0 | goto end; |
125 | 0 | p = buf; |
126 | 0 | if (BIO_read(in, buf, length) != (int)length) { |
127 | 0 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
128 | 0 | goto next; |
129 | 0 | } |
130 | | |
131 | 0 | if ((selection == 0 |
132 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
133 | 0 | && !ispub |
134 | 0 | && ctx->desc->read_private_key != NULL) { |
135 | 0 | struct ossl_passphrase_data_st pwdata; |
136 | |
|
137 | 0 | memset(&pwdata, 0, sizeof(pwdata)); |
138 | 0 | if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg)) |
139 | 0 | goto end; |
140 | 0 | p = buf; |
141 | 0 | key = ctx->desc->read_private_key(&p, bitlen, ispub); |
142 | 0 | if (selection != 0 && key == NULL) |
143 | 0 | goto next; |
144 | 0 | } |
145 | 0 | if (key == NULL && (selection == 0 |
146 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
147 | 0 | && ispub |
148 | 0 | && ctx->desc->read_public_key != NULL) { |
149 | 0 | p = buf; |
150 | 0 | key = ctx->desc->read_public_key(&p, bitlen, ispub); |
151 | 0 | if (selection != 0 && key == NULL) |
152 | 0 | goto next; |
153 | 0 | } |
154 | | |
155 | 0 | if (key != NULL && ctx->desc->adjust_key != NULL) |
156 | 0 | ctx->desc->adjust_key(key, ctx); |
157 | |
|
158 | 0 | next: |
159 | | /* |
160 | | * Indicated that we successfully decoded something, or not at all. |
161 | | * Ending up "empty handed" is not an error. |
162 | | */ |
163 | 0 | ok = 1; |
164 | | |
165 | | /* |
166 | | * We free resources here so it's not held up during the callback, because |
167 | | * we know the process is recursive and the allocated chunks of memory |
168 | | * add up. |
169 | | */ |
170 | 0 | OPENSSL_free(buf); |
171 | 0 | BIO_free(in); |
172 | 0 | buf = NULL; |
173 | 0 | in = NULL; |
174 | |
|
175 | 0 | if (key != NULL) { |
176 | 0 | OSSL_PARAM params[4]; |
177 | 0 | int object_type = OSSL_OBJECT_PKEY; |
178 | |
|
179 | 0 | params[0] = |
180 | 0 | OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); |
181 | 0 | params[1] = |
182 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
183 | 0 | (char *)ctx->desc->name, 0); |
184 | | /* The address of the key becomes the octet string */ |
185 | 0 | params[2] = |
186 | 0 | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, |
187 | 0 | &key, sizeof(key)); |
188 | 0 | params[3] = OSSL_PARAM_construct_end(); |
189 | |
|
190 | 0 | ok = data_cb(params, data_cbarg); |
191 | 0 | } |
192 | |
|
193 | 0 | end: |
194 | 0 | BIO_free(in); |
195 | 0 | OPENSSL_free(buf); |
196 | 0 | ctx->desc->free_key(key); |
197 | |
|
198 | 0 | return ok; |
199 | 0 | } |
200 | | |
201 | | static int |
202 | | msblob2key_export_object(void *vctx, |
203 | | const void *reference, size_t reference_sz, |
204 | | OSSL_CALLBACK *export_cb, void *export_cbarg) |
205 | 0 | { |
206 | 0 | struct msblob2key_ctx_st *ctx = vctx; |
207 | 0 | OSSL_FUNC_keymgmt_export_fn *export = |
208 | 0 | ossl_prov_get_keymgmt_export(ctx->desc->fns); |
209 | 0 | void *keydata; |
210 | |
|
211 | 0 | if (reference_sz == sizeof(keydata) && export != NULL) { |
212 | | /* The contents of the reference is the address to our object */ |
213 | 0 | keydata = *(void **)reference; |
214 | |
|
215 | 0 | return export(keydata, ctx->selection, export_cb, export_cbarg); |
216 | 0 | } |
217 | 0 | return 0; |
218 | 0 | } |
219 | | |
220 | | /* ---------------------------------------------------------------------- */ |
221 | | |
222 | | #define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header |
223 | | #define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header |
224 | | #define dsa_adjust NULL |
225 | | #define dsa_free (void (*)(void *))DSA_free |
226 | | |
227 | | /* ---------------------------------------------------------------------- */ |
228 | | |
229 | | #define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header |
230 | | #define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header |
231 | | |
232 | | static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx) |
233 | 0 | { |
234 | 0 | ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); |
235 | 0 | } |
236 | | |
237 | | #define rsa_free (void (*)(void *))RSA_free |
238 | | |
239 | | /* ---------------------------------------------------------------------- */ |
240 | | |
241 | | #define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \ |
242 | | static const struct keytype_desc_st mstype##2##keytype##_desc = { \ |
243 | | EVP_PKEY_##KEYTYPE, #KEYTYPE, \ |
244 | | ossl_##keytype##_keymgmt_functions, \ |
245 | | keytype##_decode_private_key, \ |
246 | | keytype##_decode_public_key, \ |
247 | | keytype##_adjust, \ |
248 | | keytype##_free \ |
249 | | }; \ |
250 | | static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \ |
251 | | static void *msblob2##keytype##_newctx(void *provctx) \ |
252 | 236k | { \ |
253 | 236k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ |
254 | 236k | } \ decode_msblob2key.c:msblob2dsa_newctx Line | Count | Source | 252 | 146k | { \ | 253 | 146k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ | 254 | 146k | } \ |
decode_msblob2key.c:msblob2rsa_newctx Line | Count | Source | 252 | 89.4k | { \ | 253 | 89.4k | return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ | 254 | 89.4k | } \ |
|
255 | | const OSSL_DISPATCH \ |
256 | | ossl_msblob_to_##keytype##_decoder_functions[] = { \ |
257 | | { OSSL_FUNC_DECODER_NEWCTX, \ |
258 | | (void (*)(void))msblob2##keytype##_newctx }, \ |
259 | | { OSSL_FUNC_DECODER_FREECTX, \ |
260 | | (void (*)(void))msblob2key_freectx }, \ |
261 | | { OSSL_FUNC_DECODER_DECODE, \ |
262 | | (void (*)(void))msblob2key_decode }, \ |
263 | | { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ |
264 | | (void (*)(void))msblob2key_export_object }, \ |
265 | | OSSL_DISPATCH_END \ |
266 | | } |
267 | | |
268 | | #ifndef OPENSSL_NO_DSA |
269 | | IMPLEMENT_MSBLOB(DSA, dsa); |
270 | | #endif |
271 | | IMPLEMENT_MSBLOB(RSA, rsa); |