Make the naming scheme for dispatched functions more consistent
[oweals/openssl.git] / providers / implementations / serializers / serializer_dh_priv.c
1 /*
2  * Copyright 2019-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 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/err.h>
19 #include <openssl/pem.h>
20 #include <openssl/dh.h>
21 #include <openssl/types.h>
22 #include <openssl/params.h>
23 #include "prov/bio.h"
24 #include "prov/implementations.h"
25 #include "prov/provider_ctx.h"
26 #include "serializer_local.h"
27
28 static OSSL_FUNC_serializer_newctx_fn dh_priv_newctx;
29 static OSSL_FUNC_serializer_freectx_fn dh_priv_freectx;
30 static OSSL_FUNC_serializer_set_ctx_params_fn dh_priv_set_ctx_params;
31 static OSSL_FUNC_serializer_settable_ctx_params_fn dh_priv_settable_ctx_params;
32 static OSSL_FUNC_serializer_serialize_data_fn dh_priv_der_data;
33 static OSSL_FUNC_serializer_serialize_object_fn dh_priv_der;
34 static OSSL_FUNC_serializer_serialize_data_fn dh_pem_priv_data;
35 static OSSL_FUNC_serializer_serialize_object_fn dh_pem_priv;
36
37 static OSSL_FUNC_serializer_newctx_fn dh_print_newctx;
38 static OSSL_FUNC_serializer_freectx_fn dh_print_freectx;
39 static OSSL_FUNC_serializer_serialize_data_fn dh_priv_print_data;
40 static OSSL_FUNC_serializer_serialize_object_fn dh_priv_print;
41
42  /*
43  * Context used for private key serialization.
44  */
45 struct dh_priv_ctx_st {
46     void *provctx;
47
48     struct pkcs8_encrypt_ctx_st sc;
49 };
50
51 /* Private key : context */
52 static void *dh_priv_newctx(void *provctx)
53 {
54     struct dh_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
55
56     if (ctx != NULL) {
57         ctx->provctx = provctx;
58
59         /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
60         ctx->sc.pbe_nid = -1;
61     }
62     return ctx;
63 }
64
65 static void dh_priv_freectx(void *vctx)
66 {
67     struct dh_priv_ctx_st *ctx = vctx;
68
69     EVP_CIPHER_free(ctx->sc.cipher);
70     OPENSSL_free(ctx->sc.cipher_pass);
71     OPENSSL_free(ctx);
72 }
73
74 static const OSSL_PARAM *dh_priv_settable_ctx_params(void)
75 {
76     static const OSSL_PARAM settables[] = {
77         OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
78         OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
79         OSSL_PARAM_END,
80     };
81
82     return settables;
83 }
84
85 static int dh_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
86 {
87     struct dh_priv_ctx_st *ctx = vctx;
88     const OSSL_PARAM *p;
89
90     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
91         != NULL) {
92         const OSSL_PARAM *propsp =
93             OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
94         const char *props = NULL;
95
96         if (p->data_type != OSSL_PARAM_UTF8_STRING)
97             return 0;
98         if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
99             return 0;
100         props = (propsp != NULL ? propsp->data : NULL);
101
102         EVP_CIPHER_free(ctx->sc.cipher);
103         ctx->sc.cipher_intent = p->data != NULL;
104         if (p->data != NULL
105             && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
106                 == NULL))
107             return 0;
108     }
109     if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
110         != NULL) {
111         OPENSSL_free(ctx->sc.cipher_pass);
112         ctx->sc.cipher_pass = NULL;
113         if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
114                                          &ctx->sc.cipher_pass_length))
115             return 0;
116     }
117     return 1;
118 }
119
120 /* Private key : DER */
121 static int dh_priv_der_data(void *vctx, const OSSL_PARAM params[],
122                             OSSL_CORE_BIO *out,
123                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
124 {
125     struct dh_priv_ctx_st *ctx = vctx;
126     OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
127     OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
128     OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
129     int ok = 0;
130
131     if (dh_import != NULL) {
132         DH *dh;
133
134         if ((dh = dh_new(ctx->provctx)) != NULL
135             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
136             && dh_priv_der(ctx, dh, out, cb, cbarg))
137             ok = 1;
138         dh_free(dh);
139     }
140     return ok;
141 }
142
143 static int dh_priv_der(void *vctx, void *dh, OSSL_CORE_BIO *cout,
144                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
145 {
146     struct dh_priv_ctx_st *ctx = vctx;
147     int ret;
148     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
149
150     if (out == NULL)
151         return 0;
152
153     ctx->sc.cb = cb;
154     ctx->sc.cbarg = cbarg;
155
156     ret = ossl_prov_write_priv_der_from_obj(out, dh, EVP_PKEY_DH,
157                                             ossl_prov_prepare_dh_params,
158                                             ossl_prov_dh_priv_to_der,
159                                             &ctx->sc);
160     BIO_free(out);
161
162     return ret;
163 }
164
165 /* Private key : PEM */
166 static int dh_pem_priv_data(void *vctx, const OSSL_PARAM params[],
167                             OSSL_CORE_BIO *out,
168                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
169 {
170     struct dh_priv_ctx_st *ctx = vctx;
171     OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
172     OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
173     OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
174     int ok = 0;
175
176     if (dh_import != NULL) {
177         DH *dh;
178
179         if ((dh = dh_new(ctx->provctx)) != NULL
180             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
181             && dh_pem_priv(ctx->provctx, dh, out, cb, cbarg))
182             ok = 1;
183         dh_free(dh);
184     }
185     return ok;
186 }
187
188 static int dh_pem_priv(void *vctx, void *dh, OSSL_CORE_BIO *cout,
189                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
190 {
191     struct dh_priv_ctx_st *ctx = vctx;
192     int ret;
193     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
194
195     if (out == NULL)
196         return 0;
197
198     ctx->sc.cb = cb;
199     ctx->sc.cbarg = cbarg;
200
201     ret = ossl_prov_write_priv_pem_from_obj(out, dh, EVP_PKEY_DH,
202                                             ossl_prov_prepare_dh_params,
203                                             ossl_prov_dh_priv_to_der,
204                                             &ctx->sc);
205     BIO_free(out);
206
207     return ret;
208 }
209
210 /*
211  * There's no specific print context, so we use the provider context
212  */
213 static void *dh_print_newctx(void *provctx)
214 {
215     return provctx;
216 }
217
218 static void dh_print_freectx(void *ctx)
219 {
220 }
221
222 static int dh_priv_print_data(void *vctx, const OSSL_PARAM params[],
223                               OSSL_CORE_BIO *out,
224                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
225 {
226     struct dh_priv_ctx_st *ctx = vctx;
227     OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
228     OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
229     OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
230     int ok = 0;
231
232     if (dh_import != NULL) {
233         DH *dh;
234
235         if ((dh = dh_new(ctx->provctx)) != NULL
236             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
237             && dh_priv_print(ctx, dh, out, cb, cbarg))
238             ok = 1;
239         dh_free(dh);
240     }
241     return ok;
242 }
243
244 static int dh_priv_print(void *ctx, void *dh, 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_dh(out, dh, dh_print_priv);
254     BIO_free(out);
255
256     return ret;
257 }
258
259 const OSSL_DISPATCH dh_priv_der_serializer_functions[] = {
260     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
261     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
262     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
263       (void (*)(void))dh_priv_set_ctx_params },
264     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
265       (void (*)(void))dh_priv_settable_ctx_params },
266     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_priv_der_data },
267     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_der },
268     { 0, NULL }
269 };
270
271 const OSSL_DISPATCH dh_priv_pem_serializer_functions[] = {
272     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
273     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
274     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
275       (void (*)(void))dh_priv_set_ctx_params },
276     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
277       (void (*)(void))dh_priv_settable_ctx_params },
278     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pem_priv_data },
279     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pem_priv },
280     { 0, NULL }
281 };
282
283 const OSSL_DISPATCH dh_priv_text_serializer_functions[] = {
284     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_print_newctx },
285     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_print_freectx },
286     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_print },
287     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
288       (void (*)(void))dh_priv_print_data },
289     { 0, NULL }
290 };