]>
Commit | Line | Data |
---|---|---|
f552d900 SL |
1 | /* |
2 | * Copyright 2020 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 | ||
23c48d94 | 10 | #include <openssl/core_dispatch.h> |
f552d900 SL |
11 | #include <openssl/core_names.h> |
12 | #include <openssl/err.h> | |
13 | #include <openssl/pem.h> | |
14 | #include <openssl/ec.h> | |
15 | #include <openssl/types.h> | |
16 | #include <openssl/params.h> | |
17 | #include "prov/bio.h" | |
18 | #include "prov/implementations.h" | |
d40b42ab | 19 | #include "prov/provider_ctx.h" |
ece9304c RL |
20 | #include "encoder_local.h" |
21 | ||
22 | static OSSL_FUNC_encoder_newctx_fn ec_priv_newctx; | |
23 | static OSSL_FUNC_encoder_freectx_fn ec_priv_freectx; | |
24 | static OSSL_FUNC_encoder_set_ctx_params_fn ec_priv_set_ctx_params; | |
25 | static OSSL_FUNC_encoder_settable_ctx_params_fn ec_priv_settable_ctx_params; | |
26 | static OSSL_FUNC_encoder_encode_data_fn ec_priv_der_data; | |
27 | static OSSL_FUNC_encoder_encode_object_fn ec_priv_der; | |
28 | static OSSL_FUNC_encoder_encode_data_fn ec_pem_priv_data; | |
29 | static OSSL_FUNC_encoder_encode_object_fn ec_pem_priv; | |
30 | ||
31 | static OSSL_FUNC_encoder_newctx_fn ec_print_newctx; | |
32 | static OSSL_FUNC_encoder_freectx_fn ec_print_freectx; | |
33 | static OSSL_FUNC_encoder_encode_data_fn ec_priv_print_data; | |
34 | static OSSL_FUNC_encoder_encode_object_fn ec_priv_print; | |
35 | ||
36 | /* | |
37 | * Context used for private key encoding. | |
f552d900 SL |
38 | */ |
39 | struct ec_priv_ctx_st { | |
40 | void *provctx; | |
41 | ||
42 | struct pkcs8_encrypt_ctx_st sc; | |
43 | }; | |
44 | ||
45 | /* Private key : context */ | |
46 | static void *ec_priv_newctx(void *provctx) | |
47 | { | |
48 | struct ec_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
49 | ||
50 | if (ctx != NULL) { | |
51 | ctx->provctx = provctx; | |
52 | ||
53 | /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */ | |
54 | ctx->sc.pbe_nid = -1; | |
55 | } | |
56 | return ctx; | |
57 | } | |
58 | ||
59 | static void ec_priv_freectx(void *vctx) | |
60 | { | |
61 | struct ec_priv_ctx_st *ctx = vctx; | |
62 | ||
63 | EVP_CIPHER_free(ctx->sc.cipher); | |
64 | OPENSSL_free(ctx->sc.cipher_pass); | |
65 | OPENSSL_free(ctx); | |
66 | } | |
67 | ||
1017ab21 | 68 | static const OSSL_PARAM *ec_priv_settable_ctx_params(ossl_unused void *provctx) |
f552d900 SL |
69 | { |
70 | static const OSSL_PARAM settables[] = { | |
ece9304c RL |
71 | OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0), |
72 | OSSL_PARAM_octet_string(OSSL_ENCODER_PARAM_PASS, NULL, 0), | |
f552d900 SL |
73 | OSSL_PARAM_END, |
74 | }; | |
75 | ||
76 | return settables; | |
77 | } | |
78 | ||
79 | static int ec_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
80 | { | |
81 | struct ec_priv_ctx_st *ctx = vctx; | |
82 | const OSSL_PARAM *p; | |
83 | ||
ece9304c | 84 | if ((p = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER)) |
f552d900 SL |
85 | != NULL) { |
86 | const OSSL_PARAM *propsp = | |
ece9304c | 87 | OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); |
f552d900 SL |
88 | const char *props = NULL; |
89 | ||
90 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
91 | return 0; | |
92 | if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING) | |
93 | return 0; | |
94 | props = (propsp != NULL ? propsp->data : NULL); | |
95 | ||
96 | EVP_CIPHER_free(ctx->sc.cipher); | |
97 | ctx->sc.cipher_intent = p->data != NULL; | |
98 | if (p->data != NULL | |
99 | && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props)) | |
100 | == NULL)) | |
101 | return 0; | |
102 | } | |
ece9304c | 103 | if ((p = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PASS)) |
f552d900 SL |
104 | != NULL) { |
105 | OPENSSL_free(ctx->sc.cipher_pass); | |
106 | ctx->sc.cipher_pass = NULL; | |
107 | if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0, | |
108 | &ctx->sc.cipher_pass_length)) | |
109 | return 0; | |
110 | } | |
111 | return 1; | |
112 | } | |
113 | ||
114 | /* Private key : DER */ | |
d40b42ab MC |
115 | static int ec_priv_der_data(void *vctx, const OSSL_PARAM params[], |
116 | OSSL_CORE_BIO *out, | |
117 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
f552d900 SL |
118 | { |
119 | struct ec_priv_ctx_st *ctx = vctx; | |
363b1e5d DMSP |
120 | OSSL_FUNC_keymgmt_new_fn *ec_new; |
121 | OSSL_FUNC_keymgmt_free_fn *ec_free; | |
122 | OSSL_FUNC_keymgmt_import_fn *ec_import; | |
f552d900 SL |
123 | int ok = 0; |
124 | ||
125 | ec_get_new_free_import(&ec_new, &ec_free, &ec_import); | |
126 | ||
127 | if (ec_import != NULL) { | |
128 | EC_KEY *eckey; | |
129 | ||
130 | if ((eckey = ec_new(ctx->provctx)) != NULL | |
be63e587 | 131 | && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL, params) |
f552d900 SL |
132 | && ec_priv_der(ctx, eckey, out, cb, cbarg)) |
133 | ok = 1; | |
134 | ec_free(eckey); | |
135 | } | |
136 | return ok; | |
137 | } | |
138 | ||
d40b42ab | 139 | static int ec_priv_der(void *vctx, void *eckey, OSSL_CORE_BIO *cout, |
ece9304c | 140 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
f552d900 SL |
141 | { |
142 | struct ec_priv_ctx_st *ctx = vctx; | |
d40b42ab MC |
143 | BIO *out = bio_new_from_core_bio(ctx->provctx, cout); |
144 | int ret; | |
145 | ||
146 | if (out == NULL) | |
147 | return 0; | |
f552d900 SL |
148 | |
149 | ctx->sc.cb = cb; | |
150 | ctx->sc.cbarg = cbarg; | |
151 | ||
d40b42ab MC |
152 | ret = ossl_prov_write_priv_der_from_obj(out, eckey, EVP_PKEY_EC, |
153 | ossl_prov_prepare_ec_params, | |
154 | ossl_prov_ec_priv_to_der, | |
155 | &ctx->sc); | |
156 | BIO_free(out); | |
157 | ||
158 | return ret; | |
f552d900 SL |
159 | } |
160 | ||
161 | /* Private key : PEM */ | |
d40b42ab MC |
162 | static int ec_pem_priv_data(void *vctx, const OSSL_PARAM params[], |
163 | OSSL_CORE_BIO *out, | |
164 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
f552d900 SL |
165 | { |
166 | struct ec_priv_ctx_st *ctx = vctx; | |
363b1e5d DMSP |
167 | OSSL_FUNC_keymgmt_new_fn *ec_new; |
168 | OSSL_FUNC_keymgmt_free_fn *ec_free; | |
169 | OSSL_FUNC_keymgmt_import_fn *ec_import; | |
f552d900 SL |
170 | int ok = 0; |
171 | ||
172 | ec_get_new_free_import(&ec_new, &ec_free, &ec_import); | |
173 | ||
174 | if (ec_import != NULL) { | |
175 | EC_KEY *eckey; | |
176 | ||
177 | if ((eckey = ec_new(ctx->provctx)) != NULL | |
be63e587 | 178 | && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL, params) |
f552d900 SL |
179 | && ec_pem_priv(ctx, eckey, out, cb, cbarg)) |
180 | ok = 1; | |
181 | ec_free(eckey); | |
182 | } | |
183 | return ok; | |
184 | } | |
185 | ||
d40b42ab | 186 | static int ec_pem_priv(void *vctx, void *eckey, OSSL_CORE_BIO *cout, |
ece9304c | 187 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
f552d900 SL |
188 | { |
189 | struct ec_priv_ctx_st *ctx = vctx; | |
d40b42ab MC |
190 | BIO *out = bio_new_from_core_bio(ctx->provctx, cout); |
191 | int ret; | |
192 | ||
193 | if (out == NULL) | |
194 | return 0; | |
f552d900 SL |
195 | |
196 | ctx->sc.cb = cb; | |
197 | ctx->sc.cbarg = cbarg; | |
198 | ||
d40b42ab MC |
199 | ret = ossl_prov_write_priv_pem_from_obj(out, eckey, EVP_PKEY_EC, |
200 | ossl_prov_prepare_ec_params, | |
201 | ossl_prov_ec_priv_to_der, | |
202 | &ctx->sc); | |
203 | BIO_free(out); | |
204 | ||
205 | return ret; | |
f552d900 SL |
206 | } |
207 | ||
208 | /* | |
209 | * There's no specific print context, so we use the provider context | |
210 | */ | |
211 | static void *ec_print_newctx(void *provctx) | |
212 | { | |
213 | return provctx; | |
214 | } | |
215 | ||
216 | static void ec_print_freectx(void *ctx) | |
217 | { | |
218 | } | |
219 | ||
d40b42ab MC |
220 | static int ec_priv_print_data(void *vctx, const OSSL_PARAM params[], |
221 | OSSL_CORE_BIO *out, | |
f552d900 SL |
222 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
223 | { | |
224 | struct ec_priv_ctx_st *ctx = vctx; | |
363b1e5d DMSP |
225 | OSSL_FUNC_keymgmt_new_fn *ec_new; |
226 | OSSL_FUNC_keymgmt_free_fn *ec_free; | |
227 | OSSL_FUNC_keymgmt_import_fn *ec_import; | |
f552d900 SL |
228 | int ok = 0; |
229 | ||
230 | ec_get_new_free_import(&ec_new, &ec_free, &ec_import); | |
231 | ||
232 | if (ec_import != NULL) { | |
233 | EC_KEY *eckey; | |
234 | ||
235 | if ((eckey = ec_new(ctx->provctx)) != NULL | |
be63e587 | 236 | && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL, params) |
f552d900 SL |
237 | && ec_priv_print(ctx, eckey, out, cb, cbarg)) |
238 | ok = 1; | |
239 | ec_free(eckey); | |
240 | } | |
241 | return ok; | |
242 | } | |
243 | ||
d40b42ab | 244 | static int ec_priv_print(void *ctx, void *eckey, OSSL_CORE_BIO *cout, |
ece9304c | 245 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
f552d900 | 246 | { |
d40b42ab MC |
247 | BIO *out = bio_new_from_core_bio(ctx, cout); |
248 | int ret; | |
249 | ||
250 | if (out == NULL) | |
251 | return 0; | |
252 | ||
253 | ret = ossl_prov_print_eckey(out, eckey, ec_print_priv); | |
254 | BIO_free(out); | |
255 | ||
256 | return ret; | |
f552d900 SL |
257 | } |
258 | ||
ece9304c RL |
259 | const OSSL_DISPATCH ec_priv_der_encoder_functions[] = { |
260 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_priv_newctx }, | |
261 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_priv_freectx }, | |
262 | { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, | |
f552d900 | 263 | (void (*)(void))ec_priv_set_ctx_params }, |
ece9304c | 264 | { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, |
f552d900 | 265 | (void (*)(void))ec_priv_settable_ctx_params }, |
ece9304c RL |
266 | { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_priv_der_data }, |
267 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_priv_der }, | |
f552d900 SL |
268 | { 0, NULL } |
269 | }; | |
270 | ||
ece9304c RL |
271 | const OSSL_DISPATCH ec_priv_pem_encoder_functions[] = { |
272 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_priv_newctx }, | |
273 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_priv_freectx }, | |
274 | { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, | |
f552d900 | 275 | (void (*)(void))ec_priv_set_ctx_params }, |
ece9304c | 276 | { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, |
f552d900 | 277 | (void (*)(void))ec_priv_settable_ctx_params }, |
ece9304c RL |
278 | { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_pem_priv_data }, |
279 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_pem_priv }, | |
f552d900 SL |
280 | { 0, NULL } |
281 | }; | |
282 | ||
ece9304c RL |
283 | const OSSL_DISPATCH ec_priv_text_encoder_functions[] = { |
284 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_print_newctx }, | |
285 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_print_freectx }, | |
286 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_priv_print }, | |
287 | { OSSL_FUNC_ENCODER_ENCODE_DATA, | |
f552d900 SL |
288 | (void (*)(void))ec_priv_print_data }, |
289 | { 0, NULL } | |
290 | }; |