]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_dh_priv.c
Deprecate the low level Diffie-Hellman functions.
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_dh_priv.c
CommitLineData
045e51cb
RL
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
ada66e78
P
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
045e51cb
RL
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
27static OSSL_OP_serializer_newctx_fn dh_priv_newctx;
28static OSSL_OP_serializer_freectx_fn dh_priv_freectx;
29static OSSL_OP_serializer_set_ctx_params_fn dh_priv_set_ctx_params;
30static OSSL_OP_serializer_settable_ctx_params_fn dh_priv_settable_ctx_params;
31static OSSL_OP_serializer_serialize_data_fn dh_priv_der_data;
32static OSSL_OP_serializer_serialize_object_fn dh_priv_der;
33static OSSL_OP_serializer_serialize_data_fn dh_pem_priv_data;
34static OSSL_OP_serializer_serialize_object_fn dh_pem_priv;
35
36static OSSL_OP_serializer_newctx_fn dh_print_newctx;
37static OSSL_OP_serializer_freectx_fn dh_print_freectx;
38static OSSL_OP_serializer_serialize_data_fn dh_priv_print_data;
39static OSSL_OP_serializer_serialize_object_fn dh_priv_print;
40
41 /*
42 * Context used for private key serialization.
43 */
44struct dh_priv_ctx_st {
45 void *provctx;
46
47 struct pkcs8_encrypt_ctx_st sc;
48};
49
50/* Private key : context */
51static 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;
045e51cb 57
4c1e06fc
P
58 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
59 ctx->sc.pbe_nid = -1;
60 }
045e51cb
RL
61 return ctx;
62}
63
64static 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
73static 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
84static 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 */
120static int dh_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
32b0645c 121 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb
RL
122{
123 struct dh_priv_ctx_st *ctx = vctx;
32b0645c
RL
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();
045e51cb
RL
127 int ok = 0;
128
32b0645c
RL
129 if (dh_import != NULL) {
130 DH *dh;
045e51cb 131
32b0645c
RL
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);
045e51cb
RL
137 }
138 return ok;
139}
140
141static 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 */
159static int dh_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
32b0645c 160 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb
RL
161{
162 struct dh_priv_ctx_st *ctx = vctx;
32b0645c
RL
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();
045e51cb
RL
166 int ok = 0;
167
32b0645c
RL
168 if (dh_import != NULL) {
169 DH *dh;
045e51cb 170
32b0645c
RL
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);
045e51cb
RL
176 }
177 return ok;
178}
179
180static int dh_pem_priv(void *vctx, void *dh, BIO *out,
32b0645c 181 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb
RL
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 */
200static void *dh_print_newctx(void *provctx)
201{
202 return provctx;
203}
204
205static void dh_print_freectx(void *ctx)
206{
207}
208
32b0645c
RL
209static int dh_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
210 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 211{
32b0645c
RL
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();
045e51cb
RL
216 int ok = 0;
217
32b0645c
RL
218 if (dh_import != NULL) {
219 DH *dh;
045e51cb 220
32b0645c
RL
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);
045e51cb
RL
226 }
227 return ok;
228}
229
230static int dh_priv_print(void *ctx, void *dh, BIO *out,
32b0645c 231 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb
RL
232{
233 return ossl_prov_print_dh(out, dh, dh_print_priv);
234}
235
236const 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
248const 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
260const 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};