Implement serializers for ED25519 and ED448
[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_numbers.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 "serializer_local.h"
20
21 static OSSL_OP_serializer_newctx_fn ec_priv_newctx;
22 static OSSL_OP_serializer_freectx_fn ec_priv_freectx;
23 static OSSL_OP_serializer_set_ctx_params_fn ec_priv_set_ctx_params;
24 static OSSL_OP_serializer_settable_ctx_params_fn ec_priv_settable_ctx_params;
25 static OSSL_OP_serializer_serialize_data_fn ec_priv_der_data;
26 static OSSL_OP_serializer_serialize_object_fn ec_priv_der;
27 static OSSL_OP_serializer_serialize_data_fn ec_pem_priv_data;
28 static OSSL_OP_serializer_serialize_object_fn ec_pem_priv;
29
30 static OSSL_OP_serializer_newctx_fn ec_print_newctx;
31 static OSSL_OP_serializer_freectx_fn ec_print_freectx;
32 static OSSL_OP_serializer_serialize_data_fn ec_priv_print_data;
33 static OSSL_OP_serializer_serialize_object_fn ec_priv_print;
34
35  /*
36  * Context used for private key serialization.
37  */
38 struct ec_priv_ctx_st {
39     void *provctx;
40
41     struct pkcs8_encrypt_ctx_st sc;
42 };
43
44 /* Private key : context */
45 static void *ec_priv_newctx(void *provctx)
46 {
47     struct ec_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
48
49     if (ctx != NULL) {
50         ctx->provctx = provctx;
51
52         /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
53         ctx->sc.pbe_nid = -1;
54     }
55     return ctx;
56 }
57
58 static void ec_priv_freectx(void *vctx)
59 {
60     struct ec_priv_ctx_st *ctx = vctx;
61
62     EVP_CIPHER_free(ctx->sc.cipher);
63     OPENSSL_free(ctx->sc.cipher_pass);
64     OPENSSL_free(ctx);
65 }
66
67 static const OSSL_PARAM *ec_priv_settable_ctx_params(void)
68 {
69     static const OSSL_PARAM settables[] = {
70         OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
71         OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
72         OSSL_PARAM_END,
73     };
74
75     return settables;
76 }
77
78 static int ec_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
79 {
80     struct ec_priv_ctx_st *ctx = vctx;
81     const OSSL_PARAM *p;
82
83     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
84         != NULL) {
85         const OSSL_PARAM *propsp =
86             OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
87         const char *props = NULL;
88
89         if (p->data_type != OSSL_PARAM_UTF8_STRING)
90             return 0;
91         if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
92             return 0;
93         props = (propsp != NULL ? propsp->data : NULL);
94
95         EVP_CIPHER_free(ctx->sc.cipher);
96         ctx->sc.cipher_intent = p->data != NULL;
97         if (p->data != NULL
98             && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
99                 == NULL))
100             return 0;
101     }
102     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
103         != NULL) {
104         OPENSSL_free(ctx->sc.cipher_pass);
105         ctx->sc.cipher_pass = NULL;
106         if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
107                                          &ctx->sc.cipher_pass_length))
108             return 0;
109     }
110     return 1;
111 }
112
113 /* Private key : DER */
114 static int ec_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
115                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
116 {
117     struct ec_priv_ctx_st *ctx = vctx;
118     OSSL_OP_keymgmt_new_fn *ec_new;
119     OSSL_OP_keymgmt_free_fn *ec_free;
120     OSSL_OP_keymgmt_import_fn *ec_import;
121     int ok = 0;
122
123     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
124
125     if (ec_import != NULL) {
126         EC_KEY *eckey;
127
128         if ((eckey = ec_new(ctx->provctx)) != NULL
129             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
130             && ec_priv_der(ctx, eckey, out, cb, cbarg))
131             ok = 1;
132         ec_free(eckey);
133     }
134     return ok;
135 }
136
137 static int ec_priv_der(void *vctx, void *eckey, BIO *out,
138                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
139 {
140     struct ec_priv_ctx_st *ctx = vctx;
141
142     ctx->sc.cb = cb;
143     ctx->sc.cbarg = cbarg;
144
145     return ossl_prov_write_priv_der_from_obj(out, eckey, EVP_PKEY_EC,
146                                              ossl_prov_prepare_ec_params,
147                                              ossl_prov_ec_priv_to_der,
148                                              &ctx->sc);
149 }
150
151 /* Private key : PEM */
152 static int ec_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
153                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
154 {
155     struct ec_priv_ctx_st *ctx = vctx;
156     OSSL_OP_keymgmt_new_fn *ec_new;
157     OSSL_OP_keymgmt_free_fn *ec_free;
158     OSSL_OP_keymgmt_import_fn *ec_import;
159     int ok = 0;
160
161     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
162
163     if (ec_import != NULL) {
164         EC_KEY *eckey;
165
166         if ((eckey = ec_new(ctx->provctx)) != NULL
167             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
168             && ec_pem_priv(ctx, eckey, out, cb, cbarg))
169             ok = 1;
170         ec_free(eckey);
171     }
172     return ok;
173 }
174
175 static int ec_pem_priv(void *vctx, void *eckey, BIO *out,
176                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
177 {
178     struct ec_priv_ctx_st *ctx = vctx;
179
180     ctx->sc.cb = cb;
181     ctx->sc.cbarg = cbarg;
182
183     return ossl_prov_write_priv_pem_from_obj(out, eckey, EVP_PKEY_EC,
184                                              ossl_prov_prepare_ec_params,
185                                              ossl_prov_ec_priv_to_der,
186                                              &ctx->sc);
187 }
188
189 /*
190  * There's no specific print context, so we use the provider context
191  */
192 static void *ec_print_newctx(void *provctx)
193 {
194     return provctx;
195 }
196
197 static void ec_print_freectx(void *ctx)
198 {
199 }
200
201 static int ec_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
202                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
203 {
204     struct ec_priv_ctx_st *ctx = vctx;
205     OSSL_OP_keymgmt_new_fn *ec_new;
206     OSSL_OP_keymgmt_free_fn *ec_free;
207     OSSL_OP_keymgmt_import_fn *ec_import;
208     int ok = 0;
209
210     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
211
212     if (ec_import != NULL) {
213         EC_KEY *eckey;
214
215         if ((eckey = ec_new(ctx->provctx)) != NULL
216             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
217             && ec_priv_print(ctx, eckey, out, cb, cbarg))
218             ok = 1;
219         ec_free(eckey);
220     }
221     return ok;
222 }
223
224 static int ec_priv_print(void *vctx, void *eckey, BIO *out,
225                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
226 {
227     return ossl_prov_print_eckey(out, eckey, ec_print_priv);
228 }
229
230 const OSSL_DISPATCH ec_priv_der_serializer_functions[] = {
231     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_priv_newctx },
232     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_priv_freectx },
233     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
234       (void (*)(void))ec_priv_set_ctx_params },
235     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
236       (void (*)(void))ec_priv_settable_ctx_params },
237     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_priv_der_data },
238     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_priv_der },
239     { 0, NULL }
240 };
241
242 const OSSL_DISPATCH ec_priv_pem_serializer_functions[] = {
243     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_priv_newctx },
244     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_priv_freectx },
245     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
246       (void (*)(void))ec_priv_set_ctx_params },
247     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
248       (void (*)(void))ec_priv_settable_ctx_params },
249     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pem_priv_data },
250     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pem_priv },
251     { 0, NULL }
252 };
253
254 const OSSL_DISPATCH ec_priv_text_serializer_functions[] = {
255     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_print_newctx },
256     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_print_freectx },
257     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_priv_print },
258     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
259       (void (*)(void))ec_priv_print_data },
260     { 0, NULL }
261 };