2 * Copyright 2019-2020 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/err.h>
18 #include <openssl/pem.h>
19 #include <openssl/dh.h>
20 #include <openssl/types.h>
21 #include <openssl/params.h>
23 #include "prov/implementations.h"
24 #include "prov/provider_ctx.h"
25 #include "serializer_local.h"
27 static OSSL_OP_serializer_newctx_fn dh_pub_newctx;
28 static OSSL_OP_serializer_freectx_fn dh_pub_freectx;
29 static OSSL_OP_serializer_serialize_data_fn dh_pub_der_data;
30 static OSSL_OP_serializer_serialize_object_fn dh_pub_der;
31 static OSSL_OP_serializer_serialize_data_fn dh_pub_pem_data;
32 static OSSL_OP_serializer_serialize_object_fn dh_pub_pem;
34 static OSSL_OP_serializer_serialize_data_fn dh_pub_print_data;
35 static OSSL_OP_serializer_serialize_object_fn dh_pub_print;
37 /* Public key : context */
40 * There's no specific implementation context, so we use the provider context
42 static void *dh_pub_newctx(void *provctx)
47 static void dh_pub_freectx(void *ctx)
51 /* Public key : DER */
52 static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[],
54 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
56 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
57 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
58 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
61 if (dh_import != NULL) {
65 if ((dh = dh_new(ctx)) != NULL
66 && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
67 && dh_pub_der(ctx, dh, out, cb, cbarg))
74 static int dh_pub_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
75 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
77 BIO *out = bio_new_from_core_bio(ctx, cout);
83 ret = ossl_prov_write_pub_der_from_obj(out, dh, EVP_PKEY_DH,
84 ossl_prov_prepare_dh_params,
85 ossl_prov_dh_pub_to_der);
91 /* Public key : PEM */
92 static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[],
94 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
96 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
97 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
98 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
101 if (dh_import != NULL) {
105 if ((dh = dh_new(ctx)) != NULL
106 && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
107 && dh_pub_pem(ctx, dh, out, cb, cbarg))
114 static int dh_pub_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
115 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
117 BIO *out = bio_new_from_core_bio(ctx, cout);
123 ret = ossl_prov_write_pub_pem_from_obj(out, dh, EVP_PKEY_DH,
124 ossl_prov_prepare_dh_params,
125 ossl_prov_dh_pub_to_der);
131 static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[],
133 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
135 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
136 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
137 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
140 if (dh_import != NULL) {
144 if ((dh = dh_new(ctx)) != NULL
145 && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
146 && dh_pub_print(ctx, dh, out, cb, cbarg))
153 static int dh_pub_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
154 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
156 BIO *out = bio_new_from_core_bio(ctx, cout);
162 ret = ossl_prov_print_dh(out, dh, dh_print_pub);
168 const OSSL_DISPATCH dh_pub_der_serializer_functions[] = {
169 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
170 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
171 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_der_data },
172 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_der },
176 const OSSL_DISPATCH dh_pub_pem_serializer_functions[] = {
177 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
178 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
179 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_pem_data },
180 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_pem },
184 const OSSL_DISPATCH dh_pub_text_serializer_functions[] = {
185 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
186 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
187 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_print },
188 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
189 (void (*)(void))dh_pub_print_data },