/src/openssl32/providers/implementations/encode_decode/decode_epki2pki.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 | | #include <openssl/core.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/core_object.h> |
14 | | #include <openssl/asn1.h> |
15 | | #include <openssl/err.h> |
16 | | #include <openssl/objects.h> |
17 | | #include <openssl/pkcs12.h> |
18 | | #include <openssl/x509.h> |
19 | | #include <openssl/proverr.h> |
20 | | #include "internal/asn1.h" |
21 | | #include "internal/sizes.h" |
22 | | #include "prov/bio.h" |
23 | | #include "prov/implementations.h" |
24 | | #include "endecoder_local.h" |
25 | | |
26 | | static OSSL_FUNC_decoder_newctx_fn epki2pki_newctx; |
27 | | static OSSL_FUNC_decoder_freectx_fn epki2pki_freectx; |
28 | | static OSSL_FUNC_decoder_decode_fn epki2pki_decode; |
29 | | static OSSL_FUNC_decoder_settable_ctx_params_fn epki2pki_settable_ctx_params; |
30 | | static OSSL_FUNC_decoder_set_ctx_params_fn epki2pki_set_ctx_params; |
31 | | |
32 | | /* |
33 | | * Context used for EncryptedPrivateKeyInfo to PrivateKeyInfo decoding. |
34 | | */ |
35 | | struct epki2pki_ctx_st { |
36 | | PROV_CTX *provctx; |
37 | | char propq[OSSL_MAX_PROPQUERY_SIZE]; |
38 | | }; |
39 | | |
40 | | static void *epki2pki_newctx(void *provctx) |
41 | 916k | { |
42 | 916k | struct epki2pki_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
43 | | |
44 | 916k | if (ctx != NULL) |
45 | 916k | ctx->provctx = provctx; |
46 | 916k | return ctx; |
47 | 916k | } |
48 | | |
49 | | static void epki2pki_freectx(void *vctx) |
50 | 916k | { |
51 | 916k | struct epki2pki_ctx_st *ctx = vctx; |
52 | | |
53 | 916k | OPENSSL_free(ctx); |
54 | 916k | } |
55 | | |
56 | | static const OSSL_PARAM *epki2pki_settable_ctx_params(ossl_unused void *provctx) |
57 | 0 | { |
58 | 0 | static const OSSL_PARAM settables[] = { |
59 | 0 | OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0), |
60 | 0 | OSSL_PARAM_END |
61 | 0 | }; |
62 | 0 | return settables; |
63 | 0 | } |
64 | | |
65 | | static int epki2pki_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
66 | 196 | { |
67 | 196 | struct epki2pki_ctx_st *ctx = vctx; |
68 | 196 | const OSSL_PARAM *p; |
69 | 196 | char *str = ctx->propq; |
70 | | |
71 | 196 | p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES); |
72 | 196 | if (p != NULL && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq))) |
73 | 0 | return 0; |
74 | | |
75 | 196 | return 1; |
76 | 196 | } |
77 | | |
78 | | /* |
79 | | * The selection parameter in epki2pki_decode() is not used by this function |
80 | | * because it's not relevant just to decode EncryptedPrivateKeyInfo to |
81 | | * PrivateKeyInfo. |
82 | | */ |
83 | | static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, |
84 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
85 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
86 | 19.3k | { |
87 | 19.3k | struct epki2pki_ctx_st *ctx = vctx; |
88 | 19.3k | BUF_MEM *mem = NULL; |
89 | 19.3k | unsigned char *der = NULL; |
90 | 19.3k | const unsigned char *pder = NULL; |
91 | 19.3k | long der_len = 0; |
92 | 19.3k | X509_SIG *p8 = NULL; |
93 | 19.3k | PKCS8_PRIV_KEY_INFO *p8inf = NULL; |
94 | 19.3k | const X509_ALGOR *alg = NULL; |
95 | 19.3k | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
96 | 19.3k | int ok = 0; |
97 | | |
98 | 19.3k | if (in == NULL) |
99 | 0 | return 0; |
100 | | |
101 | 19.3k | ok = (asn1_d2i_read_bio(in, &mem) >= 0); |
102 | 19.3k | BIO_free(in); |
103 | | |
104 | | /* We return "empty handed". This is not an error. */ |
105 | 19.3k | if (!ok) |
106 | 4.23k | return 1; |
107 | | |
108 | 15.1k | pder = der = (unsigned char *)mem->data; |
109 | 15.1k | der_len = (long)mem->length; |
110 | 15.1k | OPENSSL_free(mem); |
111 | | |
112 | 15.1k | ok = 1; /* Assume good */ |
113 | 15.1k | ERR_set_mark(); |
114 | 15.1k | if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) { |
115 | 52 | char pbuf[1024]; |
116 | 52 | size_t plen = 0; |
117 | | |
118 | 52 | ERR_clear_last_mark(); |
119 | | |
120 | 52 | if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) { |
121 | 52 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE); |
122 | 52 | ok = 0; |
123 | 52 | } else { |
124 | 0 | const ASN1_OCTET_STRING *oct; |
125 | 0 | unsigned char *new_der = NULL; |
126 | 0 | int new_der_len = 0; |
127 | |
|
128 | 0 | X509_SIG_get0(p8, &alg, &oct); |
129 | 0 | if (!PKCS12_pbe_crypt_ex(alg, pbuf, plen, |
130 | 0 | oct->data, oct->length, |
131 | 0 | &new_der, &new_der_len, 0, |
132 | 0 | PROV_LIBCTX_OF(ctx->provctx), |
133 | 0 | ctx->propq)) { |
134 | 0 | ok = 0; |
135 | 0 | } else { |
136 | 0 | OPENSSL_free(der); |
137 | 0 | der = new_der; |
138 | 0 | der_len = new_der_len; |
139 | 0 | } |
140 | 0 | alg = NULL; |
141 | 0 | } |
142 | 52 | X509_SIG_free(p8); |
143 | 15.0k | } else { |
144 | 15.0k | ERR_pop_to_mark(); |
145 | 15.0k | } |
146 | | |
147 | 15.1k | ERR_set_mark(); |
148 | 15.1k | pder = der; |
149 | 15.1k | p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pder, der_len); |
150 | 15.1k | ERR_pop_to_mark(); |
151 | | |
152 | 15.1k | if (p8inf != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)) { |
153 | | /* |
154 | | * We have something and recognised it as PrivateKeyInfo, so let's |
155 | | * pass all the applicable data to the callback. |
156 | | */ |
157 | 475 | char keytype[OSSL_MAX_NAME_SIZE]; |
158 | 475 | OSSL_PARAM params[5], *p = params; |
159 | 475 | int objtype = OSSL_OBJECT_PKEY; |
160 | | |
161 | 475 | OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0); |
162 | | |
163 | 475 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
164 | 475 | keytype, 0); |
165 | 475 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
166 | 475 | "PrivateKeyInfo", 0); |
167 | 475 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, |
168 | 475 | der, der_len); |
169 | 475 | *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); |
170 | 475 | *p = OSSL_PARAM_construct_end(); |
171 | | |
172 | 475 | ok = data_cb(params, data_cbarg); |
173 | 475 | } |
174 | 15.1k | PKCS8_PRIV_KEY_INFO_free(p8inf); |
175 | 15.1k | OPENSSL_free(der); |
176 | 15.1k | return ok; |
177 | 19.3k | } |
178 | | |
179 | | const OSSL_DISPATCH ossl_EncryptedPrivateKeyInfo_der_to_der_decoder_functions[] = { |
180 | | { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))epki2pki_newctx }, |
181 | | { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))epki2pki_freectx }, |
182 | | { OSSL_FUNC_DECODER_DECODE, (void (*)(void))epki2pki_decode }, |
183 | | { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, |
184 | | (void (*)(void))epki2pki_settable_ctx_params }, |
185 | | { OSSL_FUNC_DECODER_SET_CTX_PARAMS, |
186 | | (void (*)(void))epki2pki_set_ctx_params }, |
187 | | OSSL_DISPATCH_END |
188 | | }; |