Fix no-ec build
[oweals/openssl.git] / providers / implementations / serializers / serializer_dh_priv.c
1 /*
2  * Copyright 2019 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/dh.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 dh_priv_newctx;
22 static OSSL_OP_serializer_freectx_fn dh_priv_freectx;
23 static OSSL_OP_serializer_set_ctx_params_fn dh_priv_set_ctx_params;
24 static OSSL_OP_serializer_settable_ctx_params_fn dh_priv_settable_ctx_params;
25 static OSSL_OP_serializer_serialize_data_fn dh_priv_der_data;
26 static OSSL_OP_serializer_serialize_object_fn dh_priv_der;
27 static OSSL_OP_serializer_serialize_data_fn dh_pem_priv_data;
28 static OSSL_OP_serializer_serialize_object_fn dh_pem_priv;
29
30 static OSSL_OP_serializer_newctx_fn dh_print_newctx;
31 static OSSL_OP_serializer_freectx_fn dh_print_freectx;
32 static OSSL_OP_serializer_serialize_data_fn dh_priv_print_data;
33 static OSSL_OP_serializer_serialize_object_fn dh_priv_print;
34
35  /*
36  * Context used for private key serialization.
37  */
38 struct dh_priv_ctx_st {
39     void *provctx;
40
41     struct pkcs8_encrypt_ctx_st sc;
42 };
43
44 /* Private key : context */
45 static void *dh_priv_newctx(void *provctx)
46 {
47     struct dh_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 dh_priv_freectx(void *vctx)
59 {
60     struct dh_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 *dh_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 dh_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
79 {
80     struct dh_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 dh_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
115                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
116 {
117     struct dh_priv_ctx_st *ctx = vctx;
118     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
119     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
120     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
121     int ok = 0;
122
123     if (dh_import != NULL) {
124         DH *dh;
125
126         if ((dh = dh_new(ctx->provctx)) != NULL
127             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
128             && dh_priv_der(ctx, dh, out, cb, cbarg))
129             ok = 1;
130         dh_free(dh);
131     }
132     return ok;
133 }
134
135 static int dh_priv_der(void *vctx, void *dh, BIO *out,
136                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
137 {
138     struct dh_priv_ctx_st *ctx = vctx;
139     int ret;
140
141     ctx->sc.cb = cb;
142     ctx->sc.cbarg = cbarg;
143
144     ret = ossl_prov_write_priv_der_from_obj(out, dh, EVP_PKEY_DH,
145                                             ossl_prov_prepare_dh_params,
146                                             ossl_prov_dh_priv_to_der,
147                                             &ctx->sc);
148
149     return ret;
150 }
151
152 /* Private key : PEM */
153 static int dh_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
154                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
155 {
156     struct dh_priv_ctx_st *ctx = vctx;
157     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
158     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
159     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
160     int ok = 0;
161
162     if (dh_import != NULL) {
163         DH *dh;
164
165         if ((dh = dh_new(ctx->provctx)) != NULL
166             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
167             && dh_pem_priv(ctx->provctx, dh, out, cb, cbarg))
168             ok = 1;
169         dh_free(dh);
170     }
171     return ok;
172 }
173
174 static int dh_pem_priv(void *vctx, void *dh, BIO *out,
175                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
176 {
177     struct dh_priv_ctx_st *ctx = vctx;
178     int ret;
179
180     ctx->sc.cb = cb;
181     ctx->sc.cbarg = cbarg;
182
183     ret = ossl_prov_write_priv_pem_from_obj(out, dh, EVP_PKEY_DH,
184                                             ossl_prov_prepare_dh_params,
185                                             ossl_prov_dh_priv_to_der,
186                                             &ctx->sc);
187
188     return ret;
189 }
190
191 /*
192  * There's no specific print context, so we use the provider context
193  */
194 static void *dh_print_newctx(void *provctx)
195 {
196     return provctx;
197 }
198
199 static void dh_print_freectx(void *ctx)
200 {
201 }
202
203 static int dh_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
204                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
205 {
206     struct dh_priv_ctx_st *ctx = vctx;
207     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
208     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
209     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
210     int ok = 0;
211
212     if (dh_import != NULL) {
213         DH *dh;
214
215         if ((dh = dh_new(ctx->provctx)) != NULL
216             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
217             && dh_priv_print(ctx, dh, out, cb, cbarg))
218             ok = 1;
219         dh_free(dh);
220     }
221     return ok;
222 }
223
224 static int dh_priv_print(void *ctx, void *dh, BIO *out,
225                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
226 {
227     return ossl_prov_print_dh(out, dh, dh_print_priv);
228 }
229
230 const OSSL_DISPATCH dh_priv_der_serializer_functions[] = {
231     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
232     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
233     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
234       (void (*)(void))dh_priv_set_ctx_params },
235     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
236       (void (*)(void))dh_priv_settable_ctx_params },
237     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_priv_der_data },
238     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_der },
239     { 0, NULL }
240 };
241
242 const OSSL_DISPATCH dh_priv_pem_serializer_functions[] = {
243     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
244     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
245     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
246       (void (*)(void))dh_priv_set_ctx_params },
247     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
248       (void (*)(void))dh_priv_settable_ctx_params },
249     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pem_priv_data },
250     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pem_priv },
251     { 0, NULL }
252 };
253
254 const OSSL_DISPATCH dh_priv_text_serializer_functions[] = {
255     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_print_newctx },
256     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_print_freectx },
257     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_print },
258     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
259       (void (*)(void))dh_priv_print_data },
260     { 0, NULL }
261 };