/src/openssl/providers/implementations/encode_decode/decode_epki2pki.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include <openssl/core.h> |
12 | | #include <openssl/core_dispatch.h> |
13 | | #include <openssl/core_names.h> |
14 | | #include <openssl/core_object.h> |
15 | | #include <openssl/asn1.h> |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/objects.h> |
18 | | #include <openssl/pkcs12.h> |
19 | | #include <openssl/x509.h> |
20 | | #include <openssl/proverr.h> |
21 | | #include "internal/cryptlib.h" |
22 | | #include "internal/asn1.h" |
23 | | #include "internal/sizes.h" |
24 | | #include "prov/bio.h" |
25 | | #include "prov/decoders.h" |
26 | | #include "prov/implementations.h" |
27 | | #include "prov/endecoder_local.h" |
28 | | |
29 | | static OSSL_FUNC_decoder_newctx_fn epki2pki_newctx; |
30 | | static OSSL_FUNC_decoder_freectx_fn epki2pki_freectx; |
31 | | static OSSL_FUNC_decoder_decode_fn epki2pki_decode; |
32 | | static OSSL_FUNC_decoder_settable_ctx_params_fn epki2pki_settable_ctx_params; |
33 | | static OSSL_FUNC_decoder_set_ctx_params_fn epki2pki_set_ctx_params; |
34 | | |
35 | | /* |
36 | | * Context used for EncryptedPrivateKeyInfo to PrivateKeyInfo decoding. |
37 | | */ |
38 | | struct epki2pki_ctx_st { |
39 | | PROV_CTX *provctx; |
40 | | char propq[OSSL_MAX_PROPQUERY_SIZE]; |
41 | | }; |
42 | | |
43 | | static void *epki2pki_newctx(void *provctx) |
44 | 0 | { |
45 | 0 | struct epki2pki_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
46 | |
|
47 | 0 | if (ctx != NULL) |
48 | 0 | ctx->provctx = provctx; |
49 | 0 | return ctx; |
50 | 0 | } |
51 | | |
52 | | static void epki2pki_freectx(void *vctx) |
53 | 0 | { |
54 | 0 | struct epki2pki_ctx_st *ctx = vctx; |
55 | |
|
56 | 0 | OPENSSL_free(ctx); |
57 | 0 | } |
58 | | |
59 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
60 | | #ifndef epki2pki_set_ctx_params_list |
61 | | static const OSSL_PARAM epki2pki_set_ctx_params_list[] = { |
62 | | OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0), |
63 | | OSSL_PARAM_END |
64 | | }; |
65 | | #endif |
66 | | |
67 | | #ifndef epki2pki_set_ctx_params_st |
68 | | struct epki2pki_set_ctx_params_st { |
69 | | OSSL_PARAM *propq; |
70 | | }; |
71 | | #endif |
72 | | |
73 | | #ifndef epki2pki_set_ctx_params_decoder |
74 | | static int epki2pki_set_ctx_params_decoder |
75 | | (const OSSL_PARAM *p, struct epki2pki_set_ctx_params_st *r) |
76 | 0 | { |
77 | 0 | const char *s; |
78 | |
|
79 | 0 | memset(r, 0, sizeof(*r)); |
80 | 0 | if (p != NULL) |
81 | 0 | for (; (s = p->key) != NULL; p++) |
82 | 0 | if (ossl_likely(strcmp("properties", s + 0) == 0)) { |
83 | | /* DECODER_PARAM_PROPERTIES */ |
84 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
85 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
86 | 0 | "param %s is repeated", s); |
87 | 0 | return 0; |
88 | 0 | } |
89 | 0 | r->propq = (OSSL_PARAM *)p; |
90 | 0 | } |
91 | 0 | return 1; |
92 | 0 | } |
93 | | #endif |
94 | | /* End of machine generated */ |
95 | | |
96 | | static const OSSL_PARAM *epki2pki_settable_ctx_params(ossl_unused void *provctx) |
97 | 0 | { |
98 | 0 | return epki2pki_set_ctx_params_list; |
99 | 0 | } |
100 | | |
101 | | static int epki2pki_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
102 | 0 | { |
103 | 0 | struct epki2pki_ctx_st *ctx = vctx; |
104 | 0 | struct epki2pki_set_ctx_params_st p; |
105 | 0 | char *str; |
106 | |
|
107 | 0 | if (ctx == NULL || !epki2pki_set_ctx_params_decoder(params, &p)) |
108 | 0 | return 0; |
109 | | |
110 | 0 | str = ctx->propq; |
111 | 0 | if (p.propq != NULL |
112 | 0 | && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq))) |
113 | 0 | return 0; |
114 | | |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | /* |
119 | | * The selection parameter in epki2pki_decode() is not used by this function |
120 | | * because it's not relevant just to decode EncryptedPrivateKeyInfo to |
121 | | * PrivateKeyInfo. |
122 | | */ |
123 | | static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, |
124 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
125 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
126 | 0 | { |
127 | 0 | struct epki2pki_ctx_st *ctx = vctx; |
128 | 0 | BUF_MEM *mem = NULL; |
129 | 0 | unsigned char *der = NULL; |
130 | 0 | long der_len = 0; |
131 | 0 | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
132 | 0 | int ok = 0; |
133 | |
|
134 | 0 | if (in == NULL) |
135 | 0 | return 0; |
136 | | |
137 | 0 | ok = (asn1_d2i_read_bio(in, &mem) >= 0); |
138 | 0 | BIO_free(in); |
139 | | |
140 | | /* We return "empty handed". This is not an error. */ |
141 | 0 | if (!ok) |
142 | 0 | return 1; |
143 | | |
144 | 0 | der = (unsigned char *)mem->data; |
145 | 0 | der_len = (long)mem->length; |
146 | 0 | OPENSSL_free(mem); |
147 | |
|
148 | 0 | ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb, data_cbarg, |
149 | 0 | pw_cb, pw_cbarg, PROV_LIBCTX_OF(ctx->provctx), |
150 | 0 | ctx->propq); |
151 | 0 | OPENSSL_free(der); |
152 | 0 | return ok; |
153 | 0 | } |
154 | | |
155 | | int ossl_epki2pki_der_decode(unsigned char *der, long der_len, int selection, |
156 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
157 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg, |
158 | | OSSL_LIB_CTX *libctx, const char *propq) |
159 | 0 | { |
160 | 0 | const unsigned char *pder = der; |
161 | 0 | unsigned char *new_der = NULL; |
162 | 0 | X509_SIG *p8 = NULL; |
163 | 0 | PKCS8_PRIV_KEY_INFO *p8inf = NULL; |
164 | 0 | const X509_ALGOR *alg = NULL; |
165 | 0 | int ok = 1; /* Assume good */ |
166 | |
|
167 | 0 | ERR_set_mark(); |
168 | 0 | if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) { |
169 | 0 | char pbuf[1024]; |
170 | 0 | size_t plen = 0; |
171 | |
|
172 | 0 | ERR_clear_last_mark(); |
173 | |
|
174 | 0 | if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) { |
175 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE); |
176 | 0 | ok = 0; |
177 | 0 | } else { |
178 | 0 | const ASN1_OCTET_STRING *oct; |
179 | 0 | int new_der_len = 0; |
180 | |
|
181 | 0 | X509_SIG_get0(p8, &alg, &oct); |
182 | 0 | if (!PKCS12_pbe_crypt_ex(alg, pbuf, (int)plen, |
183 | 0 | oct->data, oct->length, |
184 | 0 | &new_der, &new_der_len, 0, |
185 | 0 | libctx, propq)) { |
186 | 0 | ok = 0; |
187 | 0 | } else { |
188 | 0 | der = new_der; |
189 | 0 | der_len = new_der_len; |
190 | 0 | } |
191 | 0 | alg = NULL; |
192 | 0 | } |
193 | 0 | X509_SIG_free(p8); |
194 | 0 | } else { |
195 | 0 | ERR_pop_to_mark(); |
196 | 0 | } |
197 | |
|
198 | 0 | ERR_set_mark(); |
199 | 0 | pder = der; |
200 | 0 | p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pder, der_len); |
201 | 0 | ERR_pop_to_mark(); |
202 | |
|
203 | 0 | if (p8inf != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)) { |
204 | | /* |
205 | | * We have something and recognised it as PrivateKeyInfo, so let's |
206 | | * pass all the applicable data to the callback. |
207 | | */ |
208 | 0 | char keytype[OSSL_MAX_NAME_SIZE]; |
209 | 0 | OSSL_PARAM params[6], *p = params; |
210 | 0 | int objtype = OSSL_OBJECT_PKEY; |
211 | |
|
212 | 0 | OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0); |
213 | |
|
214 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
215 | 0 | keytype, 0); |
216 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_INPUT_TYPE, |
217 | 0 | "DER", 0); |
218 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
219 | 0 | "PrivateKeyInfo", 0); |
220 | 0 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, |
221 | 0 | der, der_len); |
222 | 0 | *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); |
223 | 0 | *p = OSSL_PARAM_construct_end(); |
224 | |
|
225 | 0 | ok = data_cb(params, data_cbarg); |
226 | 0 | } |
227 | 0 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
228 | 0 | OPENSSL_free(new_der); |
229 | 0 | return ok; |
230 | 0 | } |
231 | | |
232 | | const OSSL_DISPATCH ossl_EncryptedPrivateKeyInfo_der_to_der_decoder_functions[] = { |
233 | | { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))epki2pki_newctx }, |
234 | | { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))epki2pki_freectx }, |
235 | | { OSSL_FUNC_DECODER_DECODE, (void (*)(void))epki2pki_decode }, |
236 | | { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, |
237 | | (void (*)(void))epki2pki_settable_ctx_params }, |
238 | | { OSSL_FUNC_DECODER_SET_CTX_PARAMS, |
239 | | (void (*)(void))epki2pki_set_ctx_params }, |
240 | | OSSL_DISPATCH_END |
241 | | }; |