]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/serializers/serializer_ecx_priv.c
c746109424115bc57f7355fedcd67ebc375985fc
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_ecx_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/types.h>
15 #include <openssl/params.h>
16 #include "crypto/ecx.h"
17 #include "prov/bio.h"
18 #include "prov/implementations.h"
19 #include "serializer_local.h"
20
21 static OSSL_OP_serializer_newctx_fn x25519_priv_newctx;
22 static OSSL_OP_serializer_newctx_fn x448_priv_newctx;
23 static OSSL_OP_serializer_newctx_fn ed25519_priv_newctx;
24 static OSSL_OP_serializer_newctx_fn ed448_priv_newctx;
25 static OSSL_OP_serializer_freectx_fn ecx_priv_freectx;
26 static OSSL_OP_serializer_set_ctx_params_fn ecx_priv_set_ctx_params;
27 static OSSL_OP_serializer_settable_ctx_params_fn ecx_priv_settable_ctx_params;
28 static OSSL_OP_serializer_serialize_data_fn ecx_priv_der_data;
29 static OSSL_OP_serializer_serialize_object_fn ecx_priv_der;
30 static OSSL_OP_serializer_serialize_data_fn ecx_priv_pem_data;
31 static OSSL_OP_serializer_serialize_object_fn ecx_priv_pem;
32
33 static OSSL_OP_serializer_serialize_data_fn ecx_priv_print_data;
34 static OSSL_OP_serializer_serialize_object_fn ecx_priv_print;
35
36 /*
37 * Context used for private key serialization.
38 */
39 struct ecx_priv_ctx_st {
40 void *provctx;
41
42 struct pkcs8_encrypt_ctx_st sc;
43 ECX_KEY_TYPE type;
44 };
45
46 /* Private key : context */
47 static void *ecx_priv_newctx(void *provctx, ECX_KEY_TYPE type)
48 {
49 struct ecx_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
50
51 if (ctx != NULL) {
52 ctx->provctx = provctx;
53
54 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
55 ctx->sc.pbe_nid = -1;
56 ctx->type = type;
57 }
58 return ctx;
59 }
60
61 static void *x25519_priv_newctx(void *provctx)
62 {
63 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_X25519);
64 }
65
66 static void *x448_priv_newctx(void *provctx)
67 {
68 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_X448);
69 }
70
71 static void *ed25519_priv_newctx(void *provctx)
72 {
73 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED25519);
74 }
75
76 static void *ed448_priv_newctx(void *provctx)
77 {
78 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED448);
79 }
80
81 static void ecx_priv_freectx(void *vctx)
82 {
83 struct ecx_priv_ctx_st *ctx = vctx;
84
85 EVP_CIPHER_free(ctx->sc.cipher);
86 OPENSSL_free(ctx->sc.cipher_pass);
87 OPENSSL_free(ctx);
88 }
89
90 static const OSSL_PARAM *ecx_priv_settable_ctx_params(void)
91 {
92 static const OSSL_PARAM settables[] = {
93 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
94 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
95 OSSL_PARAM_END,
96 };
97
98 return settables;
99 }
100
101 static int ecx_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
102 {
103 struct ecx_priv_ctx_st *ctx = vctx;
104 const OSSL_PARAM *p;
105
106 p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER);
107 if (p != NULL) {
108 const OSSL_PARAM *propsp =
109 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
110 const char *props;
111
112 if (p->data_type != OSSL_PARAM_UTF8_STRING)
113 return 0;
114 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
115 return 0;
116 props = (propsp != NULL ? propsp->data : NULL);
117
118 EVP_CIPHER_free(ctx->sc.cipher);
119 ctx->sc.cipher_intent = p->data != NULL;
120 if (p->data != NULL
121 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
122 == NULL))
123 return 0;
124 }
125 p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS);
126 if (p != NULL) {
127 OPENSSL_free(ctx->sc.cipher_pass);
128 ctx->sc.cipher_pass = NULL;
129 if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
130 &ctx->sc.cipher_pass_length))
131 return 0;
132 }
133 return 1;
134 }
135
136 /* Private key : DER */
137 static int ecx_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
138 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
139 {
140 struct ecx_priv_ctx_st *ctx = vctx;
141 OSSL_OP_keymgmt_new_fn *ecx_new;
142 OSSL_OP_keymgmt_free_fn *ecx_free;
143 OSSL_OP_keymgmt_import_fn *ecx_import;
144 int ok = 0;
145
146 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
147
148 if (ecx_import != NULL) {
149 ECX_KEY *ecxkey;
150
151 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
152 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
153 && ecx_priv_der(ctx, ecxkey, out, cb, cbarg))
154 ok = 1;
155 ecx_free(ecxkey);
156 }
157 return ok;
158 }
159
160 static int ecx_priv_der(void *vctx, void *vecxkey, BIO *out,
161 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
162 {
163 struct ecx_priv_ctx_st *ctx = vctx;
164 ECX_KEY *ecxkey = vecxkey;
165 int ret;
166 int nid = KEYTYPE2NID(ctx->type);
167
168 ctx->sc.cb = cb;
169 ctx->sc.cbarg = cbarg;
170
171 ret = ossl_prov_write_priv_der_from_obj(out, ecxkey,
172 nid,
173 NULL,
174 ossl_prov_ecx_priv_to_der,
175 &ctx->sc);
176
177 return ret;
178 }
179
180 /* Private key : PEM */
181 static int ecx_priv_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
182 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
183 {
184 struct ecx_priv_ctx_st *ctx = vctx;
185 OSSL_OP_keymgmt_new_fn *ecx_new;
186 OSSL_OP_keymgmt_free_fn *ecx_free;
187 OSSL_OP_keymgmt_import_fn *ecx_import;
188 int ok = 0;
189
190 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
191
192 if (ecx_import != NULL) {
193 ECX_KEY *ecxkey;
194
195 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
196 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
197 && ecx_priv_pem(ctx->provctx, ecxkey, out, cb, cbarg))
198 ok = 1;
199 ecx_free(ecxkey);
200 }
201 return ok;
202 }
203
204 static int ecx_priv_pem(void *vctx, void *ecxkey, BIO *out,
205 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
206 {
207 struct ecx_priv_ctx_st *ctx = vctx;
208 int ret;
209 int nid = KEYTYPE2NID(ctx->type);
210
211 ctx->sc.cb = cb;
212 ctx->sc.cbarg = cbarg;
213
214 ret = ossl_prov_write_priv_pem_from_obj(out, ecxkey,
215 nid,
216 NULL,
217 ossl_prov_ecx_priv_to_der,
218 &ctx->sc);
219
220 return ret;
221 }
222
223 static int ecx_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
224 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
225 {
226 struct ecx_priv_ctx_st *ctx = vctx;
227 OSSL_OP_keymgmt_new_fn *ecx_new;
228 OSSL_OP_keymgmt_free_fn *ecx_free;
229 OSSL_OP_keymgmt_import_fn *ecx_import;
230 int ok = 0;
231
232 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
233
234 if (ecx_import != NULL) {
235 ECX_KEY *ecxkey;
236
237 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
238 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
239 && ecx_priv_print(ctx, ecxkey, out, cb, cbarg))
240 ok = 1;
241 ecx_free(ecxkey);
242 }
243 return ok;
244 }
245
246 static int ecx_priv_print(void *ctx, void *ecxkey, BIO *out,
247 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
248 {
249 return ossl_prov_print_ecx(out, ecxkey, ecx_print_priv);
250 }
251
252 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
253 const OSSL_DISPATCH alg##_priv_##type##_serializer_functions[] = { \
254 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
255 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
256 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS, \
257 (void (*)(void))ecx_priv_set_ctx_params }, \
258 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS, \
259 (void (*)(void))ecx_priv_settable_ctx_params }, \
260 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
261 (void (*)(void))ecx_priv_##type##_data }, \
262 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
263 (void (*)(void))ecx_priv_##type }, \
264 { 0, NULL } \
265 };
266
267 #define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
268 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
269 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
270 const OSSL_DISPATCH alg##_priv_print_serializer_functions[] = { \
271 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
272 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
273 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
274 (void (*)(void))ecx_priv_print }, \
275 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
276 (void (*)(void))ecx_priv_print_data }, \
277 { 0, NULL } \
278 };
279
280 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
281 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
282 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
283 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)