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
10 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/dh.h>
15 #include <openssl/types.h>
16 #include <openssl/params.h>
18 #include "prov/implementations.h"
19 #include "serializer_local.h"
21 static OSSL_OP_serializer_newctx_fn dh_priv_newctx
;
22 static OSSL_OP_serializer_freectx_fn dh_priv_freectx
;
23 static OSSL_OP_serializer_set_ctx_params_fn dh_priv_set_ctx_params
;
24 static OSSL_OP_serializer_settable_ctx_params_fn dh_priv_settable_ctx_params
;
25 static OSSL_OP_serializer_serialize_data_fn dh_priv_der_data
;
26 static OSSL_OP_serializer_serialize_object_fn dh_priv_der
;
27 static OSSL_OP_serializer_serialize_data_fn dh_pem_priv_data
;
28 static OSSL_OP_serializer_serialize_object_fn dh_pem_priv
;
30 static OSSL_OP_serializer_newctx_fn dh_print_newctx
;
31 static OSSL_OP_serializer_freectx_fn dh_print_freectx
;
32 static OSSL_OP_serializer_serialize_data_fn dh_priv_print_data
;
33 static OSSL_OP_serializer_serialize_object_fn dh_priv_print
;
36 * Context used for private key serialization.
38 struct dh_priv_ctx_st
{
41 struct pkcs8_encrypt_ctx_st sc
;
44 /* Private key : context */
45 static void *dh_priv_newctx(void *provctx
)
47 struct dh_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
50 ctx
->provctx
= provctx
;
52 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
58 static void dh_priv_freectx(void *vctx
)
60 struct dh_priv_ctx_st
*ctx
= vctx
;
62 EVP_CIPHER_free(ctx
->sc
.cipher
);
63 OPENSSL_free(ctx
->sc
.cipher_pass
);
67 static const OSSL_PARAM
*dh_priv_settable_ctx_params(void)
69 static const OSSL_PARAM settables
[] = {
70 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER
, NULL
, 0),
71 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS
, NULL
, 0),
78 static int dh_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
80 struct dh_priv_ctx_st
*ctx
= vctx
;
83 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_CIPHER
))
85 const OSSL_PARAM
*propsp
=
86 OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PROPERTIES
);
87 const char *props
= NULL
;
89 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
91 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
93 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
95 EVP_CIPHER_free(ctx
->sc
.cipher
);
96 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
98 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
102 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PASS
))
104 OPENSSL_free(ctx
->sc
.cipher_pass
);
105 ctx
->sc
.cipher_pass
= NULL
;
106 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
107 &ctx
->sc
.cipher_pass_length
))
113 /* Private key : DER */
114 static int dh_priv_der_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
115 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
117 struct dh_priv_ctx_st
*ctx
= vctx
;
118 OSSL_OP_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
119 OSSL_OP_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
120 OSSL_OP_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
123 if (dh_import
!= NULL
) {
126 if ((dh
= dh_new(ctx
->provctx
)) != NULL
127 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
128 && dh_priv_der(ctx
, dh
, out
, cb
, cbarg
))
135 static int dh_priv_der(void *vctx
, void *dh
, BIO
*out
,
136 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
138 struct dh_priv_ctx_st
*ctx
= vctx
;
142 ctx
->sc
.cbarg
= cbarg
;
144 ret
= ossl_prov_write_priv_der_from_obj(out
, dh
, EVP_PKEY_DH
,
145 ossl_prov_prepare_dh_params
,
146 ossl_prov_dh_priv_to_der
,
152 /* Private key : PEM */
153 static int dh_pem_priv_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
154 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
156 struct dh_priv_ctx_st
*ctx
= vctx
;
157 OSSL_OP_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
158 OSSL_OP_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
159 OSSL_OP_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
162 if (dh_import
!= NULL
) {
165 if ((dh
= dh_new(ctx
->provctx
)) != NULL
166 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
167 && dh_pem_priv(ctx
->provctx
, dh
, out
, cb
, cbarg
))
174 static int dh_pem_priv(void *vctx
, void *dh
, BIO
*out
,
175 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
177 struct dh_priv_ctx_st
*ctx
= vctx
;
181 ctx
->sc
.cbarg
= cbarg
;
183 ret
= ossl_prov_write_priv_pem_from_obj(out
, dh
, EVP_PKEY_DH
,
184 ossl_prov_prepare_dh_params
,
185 ossl_prov_dh_priv_to_der
,
192 * There's no specific print context, so we use the provider context
194 static void *dh_print_newctx(void *provctx
)
199 static void dh_print_freectx(void *ctx
)
203 static int dh_priv_print_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
204 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
206 struct dh_priv_ctx_st
*ctx
= vctx
;
207 OSSL_OP_keymgmt_new_fn
*dh_new
= ossl_prov_get_keymgmt_dh_new();
208 OSSL_OP_keymgmt_free_fn
*dh_free
= ossl_prov_get_keymgmt_dh_free();
209 OSSL_OP_keymgmt_import_fn
*dh_import
= ossl_prov_get_keymgmt_dh_import();
212 if (dh_import
!= NULL
) {
215 if ((dh
= dh_new(ctx
->provctx
)) != NULL
216 && dh_import(dh
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
217 && dh_priv_print(ctx
, dh
, out
, cb
, cbarg
))
224 static int dh_priv_print(void *ctx
, void *dh
, BIO
*out
,
225 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
227 return ossl_prov_print_dh(out
, dh
, dh_print_priv
);
230 const OSSL_DISPATCH dh_priv_der_serializer_functions
[] = {
231 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))dh_priv_newctx
},
232 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))dh_priv_freectx
},
233 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
234 (void (*)(void))dh_priv_set_ctx_params
},
235 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
236 (void (*)(void))dh_priv_settable_ctx_params
},
237 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))dh_priv_der_data
},
238 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))dh_priv_der
},
242 const OSSL_DISPATCH dh_priv_pem_serializer_functions
[] = {
243 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))dh_priv_newctx
},
244 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))dh_priv_freectx
},
245 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
246 (void (*)(void))dh_priv_set_ctx_params
},
247 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
248 (void (*)(void))dh_priv_settable_ctx_params
},
249 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))dh_pem_priv_data
},
250 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))dh_pem_priv
},
254 const OSSL_DISPATCH dh_priv_text_serializer_functions
[] = {
255 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))dh_print_newctx
},
256 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))dh_print_freectx
},
257 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))dh_priv_print
},
258 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
,
259 (void (*)(void))dh_priv_print_data
},