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/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 "prov/provider_ctx.h"
26 #include "encoder_local.h"
28 static OSSL_FUNC_encoder_newctx_fn dh_priv_newctx
;
29 static OSSL_FUNC_encoder_freectx_fn dh_priv_freectx
;
30 static OSSL_FUNC_encoder_set_ctx_params_fn dh_priv_set_ctx_params
;
31 static OSSL_FUNC_encoder_settable_ctx_params_fn dh_priv_settable_ctx_params
;
32 static OSSL_FUNC_encoder_encode_data_fn dh_priv_der_data
;
33 static OSSL_FUNC_encoder_encode_object_fn dh_priv_der
;
34 static OSSL_FUNC_encoder_encode_data_fn dh_pem_priv_data
;
35 static OSSL_FUNC_encoder_encode_object_fn dh_pem_priv
;
37 static OSSL_FUNC_encoder_newctx_fn dh_print_newctx
;
38 static OSSL_FUNC_encoder_freectx_fn dh_print_freectx
;
39 static OSSL_FUNC_encoder_encode_data_fn dh_priv_print_data
;
40 static OSSL_FUNC_encoder_encode_object_fn dh_priv_print
;
43 * Context used for private key encoding.
45 struct dh_priv_ctx_st
{
48 struct pkcs8_encrypt_ctx_st sc
;
51 /* Private key : context */
52 static void *dh_priv_newctx(void *provctx
)
54 struct dh_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
57 ctx
->provctx
= provctx
;
59 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
65 static void dh_priv_freectx(void *vctx
)
67 struct dh_priv_ctx_st
*ctx
= vctx
;
69 EVP_CIPHER_free(ctx
->sc
.cipher
);
70 OPENSSL_free(ctx
->sc
.cipher_pass
);
74 static const OSSL_PARAM
*dh_priv_settable_ctx_params(ossl_unused
void *provctx
)
76 static const OSSL_PARAM settables
[] = {
77 OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER
, NULL
, 0),
78 OSSL_PARAM_octet_string(OSSL_ENCODER_PARAM_PASS
, NULL
, 0),
85 static int dh_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
87 struct dh_priv_ctx_st
*ctx
= vctx
;
90 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_ENCODER_PARAM_CIPHER
))
92 const OSSL_PARAM
*propsp
=
93 OSSL_PARAM_locate_const(params
, OSSL_ENCODER_PARAM_PROPERTIES
);
94 const char *props
= NULL
;
96 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
98 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
100 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
102 EVP_CIPHER_free(ctx
->sc
.cipher
);
103 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
105 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
109 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_ENCODER_PARAM_PASS
))
111 OPENSSL_free(ctx
->sc
.cipher_pass
);
112 ctx
->sc
.cipher_pass
= NULL
;
113 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
114 &ctx
->sc
.cipher_pass_length
))
120 /* Private key : DER */
121 static int dh_priv_der_data(void *vctx
, const OSSL_PARAM params
[],
123 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
125 struct dh_priv_ctx_st
*ctx
= vctx
;
126 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
127 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
128 OSSL_FUNC_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
131 if (dh_import
!= NULL
) {
134 if ((dh
= dh_new(ctx
->provctx
)) != NULL
135 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
136 && dh_priv_der(ctx
, dh
, out
, cb
, cbarg
))
143 static int dh_priv_der(void *vctx
, void *dh
, OSSL_CORE_BIO
*cout
,
144 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
146 struct dh_priv_ctx_st
*ctx
= vctx
;
148 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
154 ctx
->sc
.cbarg
= cbarg
;
156 ret
= ossl_prov_write_priv_der_from_obj(out
, dh
,
157 ossl_prov_dh_type_to_evp(dh
),
158 ossl_prov_prepare_dh_params
,
159 ossl_prov_dh_priv_to_der
,
166 /* Private key : PEM */
167 static int dh_pem_priv_data(void *vctx
, const OSSL_PARAM params
[],
169 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
171 struct dh_priv_ctx_st
*ctx
= vctx
;
172 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
173 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
174 OSSL_FUNC_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
177 if (dh_import
!= NULL
) {
180 if ((dh
= dh_new(ctx
->provctx
)) != NULL
181 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
182 && dh_pem_priv(ctx
->provctx
, dh
, out
, cb
, cbarg
))
189 static int dh_pem_priv(void *vctx
, void *dh
, OSSL_CORE_BIO
*cout
,
190 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
192 struct dh_priv_ctx_st
*ctx
= vctx
;
194 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
200 ctx
->sc
.cbarg
= cbarg
;
202 ret
= ossl_prov_write_priv_pem_from_obj(out
, dh
,
203 ossl_prov_dh_type_to_evp(dh
),
204 ossl_prov_prepare_dh_params
,
205 ossl_prov_dh_priv_to_der
,
213 * There's no specific print context, so we use the provider context
215 static void *dh_print_newctx(void *provctx
)
220 static void dh_print_freectx(void *ctx
)
224 static int dh_priv_print_data(void *vctx
, const OSSL_PARAM params
[],
226 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
228 struct dh_priv_ctx_st
*ctx
= vctx
;
229 OSSL_FUNC_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
230 OSSL_FUNC_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
231 OSSL_FUNC_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
234 if (dh_import
!= NULL
) {
237 if ((dh
= dh_new(ctx
->provctx
)) != NULL
238 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
239 && dh_priv_print(ctx
, dh
, out
, cb
, cbarg
))
246 static int dh_priv_print(void *ctx
, void *dh
, OSSL_CORE_BIO
*cout
,
247 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
249 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
255 ret
= ossl_prov_print_dh(out
, dh
, dh_print_priv
);
261 const OSSL_DISPATCH dh_priv_der_encoder_functions
[] = {
262 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_priv_newctx
},
263 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_priv_freectx
},
264 { OSSL_FUNC_ENCODER_SET_CTX_PARAMS
,
265 (void (*)(void))dh_priv_set_ctx_params
},
266 { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
,
267 (void (*)(void))dh_priv_settable_ctx_params
},
268 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))dh_priv_der_data
},
269 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_priv_der
},
273 const OSSL_DISPATCH dh_priv_pem_encoder_functions
[] = {
274 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_priv_newctx
},
275 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_priv_freectx
},
276 { OSSL_FUNC_ENCODER_SET_CTX_PARAMS
,
277 (void (*)(void))dh_priv_set_ctx_params
},
278 { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
,
279 (void (*)(void))dh_priv_settable_ctx_params
},
280 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))dh_pem_priv_data
},
281 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_pem_priv
},
285 const OSSL_DISPATCH dh_priv_text_encoder_functions
[] = {
286 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))dh_print_newctx
},
287 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))dh_print_freectx
},
288 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))dh_priv_print
},
289 { OSSL_FUNC_ENCODER_ENCODE_DATA
,
290 (void (*)(void))dh_priv_print_data
},