6e37b9b3c50a5c9ac8f957c4c41161070d3d1db4
[openssl.git] / providers / implementations / serializers / serializer_ec_priv.c
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
10 #include <openssl/core_dispatch.h>
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"
19 #include "prov/provider_ctx.h"
20 #include "serializer_local.h"
21
22 static OSSL_FUNC_serializer_newctx_fn ec_priv_newctx;
23 static OSSL_FUNC_serializer_freectx_fn ec_priv_freectx;
24 static OSSL_FUNC_serializer_set_ctx_params_fn ec_priv_set_ctx_params;
25 static OSSL_FUNC_serializer_settable_ctx_params_fn ec_priv_settable_ctx_params;
26 static OSSL_FUNC_serializer_serialize_data_fn ec_priv_der_data;
27 static OSSL_FUNC_serializer_serialize_object_fn ec_priv_der;
28 static OSSL_FUNC_serializer_serialize_data_fn ec_pem_priv_data;
29 static OSSL_FUNC_serializer_serialize_object_fn ec_pem_priv;
30
31 static OSSL_FUNC_serializer_newctx_fn ec_print_newctx;
32 static OSSL_FUNC_serializer_freectx_fn ec_print_freectx;
33 static OSSL_FUNC_serializer_serialize_data_fn ec_priv_print_data;
34 static OSSL_FUNC_serializer_serialize_object_fn ec_priv_print;
35
36  /*
37  * Context used for private key serialization.
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
68 static const OSSL_PARAM *ec_priv_settable_ctx_params(void *provctx)
69 {
70     static const OSSL_PARAM settables[] = {
71         OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
72         OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
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
84     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
85         != NULL) {
86         const OSSL_PARAM *propsp =
87             OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
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     }
103     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
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 */
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)
118 {
119     struct ec_priv_ctx_st *ctx = vctx;
120     OSSL_FUNC_keymgmt_new_fn *ec_new;
121     OSSL_FUNC_keymgmt_free_fn *ec_free;
122     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
131             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
132             && ec_priv_der(ctx, eckey, out, cb, cbarg))
133             ok = 1;
134         ec_free(eckey);
135     }
136     return ok;
137 }
138
139 static int ec_priv_der(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
140                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
141 {
142     struct ec_priv_ctx_st *ctx = vctx;
143     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
144     int ret;
145
146     if (out == NULL)
147         return 0;
148
149     ctx->sc.cb = cb;
150     ctx->sc.cbarg = cbarg;
151
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;
159 }
160
161 /* Private key : PEM */
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)
165 {
166     struct ec_priv_ctx_st *ctx = vctx;
167     OSSL_FUNC_keymgmt_new_fn *ec_new;
168     OSSL_FUNC_keymgmt_free_fn *ec_free;
169     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
178             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
179             && ec_pem_priv(ctx, eckey, out, cb, cbarg))
180             ok = 1;
181         ec_free(eckey);
182     }
183     return ok;
184 }
185
186 static int ec_pem_priv(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
187                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
188 {
189     struct ec_priv_ctx_st *ctx = vctx;
190     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
191     int ret;
192
193     if (out == NULL)
194         return 0;
195
196     ctx->sc.cb = cb;
197     ctx->sc.cbarg = cbarg;
198
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;
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
220 static int ec_priv_print_data(void *vctx, const OSSL_PARAM params[],
221                               OSSL_CORE_BIO *out,
222                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
223 {
224     struct ec_priv_ctx_st *ctx = vctx;
225     OSSL_FUNC_keymgmt_new_fn *ec_new;
226     OSSL_FUNC_keymgmt_free_fn *ec_free;
227     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
236             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
237             && ec_priv_print(ctx, eckey, out, cb, cbarg))
238             ok = 1;
239         ec_free(eckey);
240     }
241     return ok;
242 }
243
244 static int ec_priv_print(void *ctx, void *eckey, OSSL_CORE_BIO *cout,
245                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
246 {
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;
257 }
258
259 const OSSL_DISPATCH ec_priv_der_serializer_functions[] = {
260     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_priv_newctx },
261     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_priv_freectx },
262     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
263       (void (*)(void))ec_priv_set_ctx_params },
264     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
265       (void (*)(void))ec_priv_settable_ctx_params },
266     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_priv_der_data },
267     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_priv_der },
268     { 0, NULL }
269 };
270
271 const OSSL_DISPATCH ec_priv_pem_serializer_functions[] = {
272     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_priv_newctx },
273     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_priv_freectx },
274     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
275       (void (*)(void))ec_priv_set_ctx_params },
276     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
277       (void (*)(void))ec_priv_settable_ctx_params },
278     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pem_priv_data },
279     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pem_priv },
280     { 0, NULL }
281 };
282
283 const OSSL_DISPATCH ec_priv_text_serializer_functions[] = {
284     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_print_newctx },
285     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_print_freectx },
286     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_priv_print },
287     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
288       (void (*)(void))ec_priv_print_data },
289     { 0, NULL }
290 };