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_dispatch.h>
17 #include <openssl/pem.h>
18 #include <openssl/dh.h>
19 #include <openssl/types.h>
20 #include <openssl/params.h>
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "prov/provider_ctx.h"
25 #include "encoder_local.h"
27 static OSSL_FUNC_encoder_newctx_fn dh_param_newctx
;
28 static OSSL_FUNC_encoder_freectx_fn dh_param_freectx
;
29 static OSSL_FUNC_encoder_encode_data_fn dh_param_der_data
;
30 static OSSL_FUNC_encoder_encode_object_fn dh_param_der
;
31 static OSSL_FUNC_encoder_encode_data_fn dh_param_pem_data
;
32 static OSSL_FUNC_encoder_encode_object_fn dh_param_pem
;
34 static OSSL_FUNC_encoder_encode_data_fn dh_param_print_data
;
35 static OSSL_FUNC_encoder_encode_object_fn dh_param_print
;
37 /* Parameters : context */
40 * There's no specific implementation context, so we use the provider context
42 static void *dh_param_newctx(void *provctx
)
47 static void dh_param_freectx(void *ctx
)
51 /* Public key : DER */
52 static int dh_param_der_data(void *ctx
, const OSSL_PARAM params
[],
54 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
56 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
57 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
58 OSSL_FUNC_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_ALL_PARAMETERS
, params
)
67 && dh_param_der(ctx
, dh
, out
, cb
, cbarg
))
74 static int dh_param_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
);
82 ret
= i2d_DHparams_bio(out
, dh
);
88 /* Public key : PEM */
89 static int dh_param_pem_data(void *ctx
, const OSSL_PARAM params
[],
91 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
93 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
94 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
95 OSSL_FUNC_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
98 if (dh_import
!= NULL
) {
102 if ((dh
= dh_new(ctx
)) != NULL
103 && dh_import(dh
, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
, params
)
104 && dh_param_pem(ctx
, dh
, out
, cb
, cbarg
))
111 static int dh_param_pem(void *ctx
, void *dh
, OSSL_CORE_BIO
*cout
,
112 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
114 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
120 ret
= PEM_write_bio_DHparams(out
, dh
);
126 static int dh_param_print_data(void *ctx
, const OSSL_PARAM params
[],
128 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
130 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
131 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
132 OSSL_FUNC_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
135 if (dh_import
!= NULL
) {
139 if ((dh
= dh_new(ctx
)) != NULL
140 && dh_import(dh
, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
, params
)
141 && dh_param_print(ctx
, dh
, out
, cb
, cbarg
))
148 static int dh_param_print(void *ctx
, void *dh
, OSSL_CORE_BIO
*cout
,
149 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
151 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
157 ret
= ossl_prov_print_dh(out
, dh
, dh_print_params
);
163 const OSSL_DISPATCH dh_param_der_encoder_functions
[] = {
164 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_param_newctx
},
165 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_param_freectx
},
166 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))dh_param_der_data
},
167 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_param_der
},
171 const OSSL_DISPATCH dh_param_pem_encoder_functions
[] = {
172 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_param_newctx
},
173 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_param_freectx
},
174 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))dh_param_pem_data
},
175 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_param_pem
},
179 const OSSL_DISPATCH dh_param_text_encoder_functions
[] = {
180 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_param_newctx
},
181 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_param_freectx
},
182 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_param_print
},
183 { OSSL_FUNC_ENCODER_ENCODE_DATA
,
184 (void (*)(void))dh_param_print_data
},