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