2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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>
24 #include "prov/implementations.h"
25 #include "serializer_local.h"
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;
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;
42 * Context used for private key serialization.
44 struct dh_priv_ctx_st {
47 struct pkcs8_encrypt_ctx_st sc;
50 /* Private key : context */
51 static void *dh_priv_newctx(void *provctx)
53 struct dh_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
56 ctx->provctx = provctx;
58 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
64 static void dh_priv_freectx(void *vctx)
66 struct dh_priv_ctx_st *ctx = vctx;
68 EVP_CIPHER_free(ctx->sc.cipher);
69 OPENSSL_free(ctx->sc.cipher_pass);
73 static const OSSL_PARAM *dh_priv_settable_ctx_params(void)
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),
84 static int dh_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
86 struct dh_priv_ctx_st *ctx = vctx;
89 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
91 const OSSL_PARAM *propsp =
92 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
93 const char *props = NULL;
95 if (p->data_type != OSSL_PARAM_UTF8_STRING)
97 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
99 props = (propsp != NULL ? propsp->data : NULL);
101 EVP_CIPHER_free(ctx->sc.cipher);
102 ctx->sc.cipher_intent = p->data != NULL;
104 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
108 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
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))
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)
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();
129 if (dh_import != NULL) {
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))
141 static int dh_priv_der(void *vctx, void *dh, BIO *out,
142 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
144 struct dh_priv_ctx_st *ctx = vctx;
148 ctx->sc.cbarg = cbarg;
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,
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)
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();
168 if (dh_import != NULL) {
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))
180 static int dh_pem_priv(void *vctx, void *dh, BIO *out,
181 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
183 struct dh_priv_ctx_st *ctx = vctx;
187 ctx->sc.cbarg = cbarg;
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,
198 * There's no specific print context, so we use the provider context
200 static void *dh_print_newctx(void *provctx)
205 static void dh_print_freectx(void *ctx)
209 static int dh_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
210 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
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();
218 if (dh_import != NULL) {
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))
230 static int dh_priv_print(void *ctx, void *dh, BIO *out,
231 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
233 return ossl_prov_print_dh(out, dh, dh_print_priv);
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 },
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 },
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 },