2 * Copyright 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
10 #include <openssl/core_dispatch.h>
11 #include <openssl/pem.h>
12 #include <openssl/ec.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
16 #include "prov/implementations.h"
17 #include "prov/providercommonerr.h"
18 #include "prov/provider_ctx.h"
19 #include "encoder_local.h"
21 static OSSL_FUNC_encoder_newctx_fn ec_param_newctx
;
22 static OSSL_FUNC_encoder_freectx_fn ec_param_freectx
;
23 static OSSL_FUNC_encoder_encode_data_fn ec_param_der_data
;
24 static OSSL_FUNC_encoder_encode_object_fn ec_param_der
;
25 static OSSL_FUNC_encoder_encode_data_fn ec_param_pem_data
;
26 static OSSL_FUNC_encoder_encode_object_fn ec_param_pem
;
28 static OSSL_FUNC_encoder_encode_data_fn ec_param_print_data
;
29 static OSSL_FUNC_encoder_encode_object_fn ec_param_print
;
32 /* There is no specific implementation context, so use the provider context */
33 static void *ec_param_newctx(void *provctx
)
38 static void ec_param_freectx(void *vctx
)
42 /* Public key : DER */
43 static int ec_param_der_data(void *vctx
, const OSSL_PARAM params
[],
45 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
47 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
48 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
49 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
52 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
54 if (ec_import
!= NULL
) {
58 if ((eckey
= ec_new(vctx
)) != NULL
59 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
, params
)
60 && ec_param_der(vctx
, eckey
, out
, cb
, cbarg
))
67 static int ec_param_der(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
68 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
70 BIO
*out
= bio_new_from_core_bio(vctx
, cout
);
76 ret
= i2d_ECPKParameters_bio(out
, EC_KEY_get0_group(eckey
));
82 /* Public key : PEM */
83 static int ec_param_pem_data(void *vctx
, const OSSL_PARAM params
[],
85 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
87 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
88 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
89 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
92 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
94 if (ec_import
!= NULL
) {
98 if ((eckey
= ec_new(vctx
)) != NULL
99 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
, params
)
100 && ec_param_pem(vctx
, eckey
, out
, cb
, cbarg
))
107 static int ec_param_pem(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
108 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
110 BIO
*out
= bio_new_from_core_bio(vctx
, cout
);
116 ret
= PEM_write_bio_ECPKParameters(out
, EC_KEY_get0_group(eckey
));
122 static int ec_param_print_data(void *vctx
, const OSSL_PARAM params
[],
124 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
126 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
127 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
128 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
131 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
133 if (ec_import
!= NULL
) {
136 /* vctx == provctx */
137 if ((eckey
= ec_new(vctx
)) != NULL
138 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
, params
)
139 && ec_param_print(vctx
, eckey
, out
, cb
, cbarg
))
146 static int ec_param_print(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
147 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
149 BIO
*out
= bio_new_from_core_bio(vctx
, cout
);
155 ret
= ossl_prov_print_eckey(out
, eckey
, ec_print_params
);
161 const OSSL_DISPATCH ec_param_der_encoder_functions
[] = {
162 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_param_newctx
},
163 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_param_freectx
},
164 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))ec_param_der_data
},
165 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_param_der
},
169 const OSSL_DISPATCH ec_param_pem_encoder_functions
[] = {
170 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_param_newctx
},
171 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_param_freectx
},
172 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))ec_param_pem_data
},
173 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_param_pem
},
177 const OSSL_DISPATCH ec_param_text_encoder_functions
[] = {
178 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_param_newctx
},
179 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_param_freectx
},
180 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_param_print
},
181 { OSSL_FUNC_ENCODER_ENCODE_DATA
,
182 (void (*)(void))ec_param_print_data
},