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/err.h>
12 #include <openssl/pem.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "encoder_local.h"
20 #define EC_SELECT_PUBLIC_IMPORTABLE \
21 OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
23 static OSSL_FUNC_encoder_newctx_fn ec_pub_newctx
;
24 static OSSL_FUNC_encoder_freectx_fn ec_pub_freectx
;
25 static OSSL_FUNC_encoder_encode_data_fn ec_pub_der_data
;
26 static OSSL_FUNC_encoder_encode_object_fn ec_pub_der
;
27 static OSSL_FUNC_encoder_encode_data_fn ec_pub_pem_data
;
28 static OSSL_FUNC_encoder_encode_object_fn ec_pub_pem
;
29 static OSSL_FUNC_encoder_encode_data_fn ec_pub_print_data
;
30 static OSSL_FUNC_encoder_encode_object_fn ec_pub_print
;
32 /* Public key : context */
35 * There's no specific implementation context, so we use the provider context
37 static void *ec_pub_newctx(void *provctx
)
42 static void ec_pub_freectx(void *ctx
)
46 /* Public key : DER */
47 static int ec_pub_der_data(void *vctx
, const OSSL_PARAM params
[],
49 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
51 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
52 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
53 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
56 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
58 if (ec_import
!= NULL
) {
62 if ((eckey
= ec_new(vctx
)) != NULL
63 && ec_import(eckey
, EC_SELECT_PUBLIC_IMPORTABLE
, params
)
64 && ec_pub_der(vctx
, eckey
, out
, cb
, cbarg
))
71 static int ec_pub_der(void *ctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
72 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
74 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
80 ret
= ossl_prov_write_pub_der_from_obj(out
, eckey
, EVP_PKEY_EC
,
81 ossl_prov_prepare_ec_params
,
82 ossl_prov_ec_pub_to_der
);
88 /* Public key : PEM */
89 static int ec_pub_pem_data(void *vctx
, const OSSL_PARAM params
[],
91 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
93 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
94 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
95 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
98 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
100 if (ec_import
!= NULL
) {
104 if ((eckey
= ec_new(vctx
)) != NULL
105 && ec_import(eckey
, EC_SELECT_PUBLIC_IMPORTABLE
, params
)
106 && ec_pub_pem(vctx
, eckey
, out
, cb
, cbarg
))
113 static int ec_pub_pem(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
114 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
116 BIO
*out
= bio_new_from_core_bio(vctx
, cout
);
122 ret
= ossl_prov_write_pub_pem_from_obj(out
, eckey
, EVP_PKEY_EC
,
123 ossl_prov_prepare_ec_params
,
124 ossl_prov_ec_pub_to_der
);
130 static int ec_pub_print_data(void *vctx
, const OSSL_PARAM params
[],
132 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
134 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
135 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
136 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
139 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
141 if (ec_import
!= NULL
) {
145 if ((eckey
= ec_new(vctx
)) != NULL
146 && ec_import(eckey
, EC_SELECT_PUBLIC_IMPORTABLE
, params
)
147 && ec_pub_print(vctx
, eckey
, out
, cb
, cbarg
))
154 static int ec_pub_print(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
155 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
157 BIO
*out
= bio_new_from_core_bio(vctx
, cout
);
163 ret
= ossl_prov_print_eckey(out
, eckey
, ec_print_pub
);
169 const OSSL_DISPATCH ec_pub_der_encoder_functions
[] = {
170 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_pub_newctx
},
171 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_pub_freectx
},
172 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))ec_pub_der_data
},
173 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_pub_der
},
177 const OSSL_DISPATCH ec_pub_pem_encoder_functions
[] = {
178 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_pub_newctx
},
179 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_pub_freectx
},
180 { OSSL_FUNC_ENCODER_ENCODE_DATA
, (void (*)(void))ec_pub_pem_data
},
181 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_pub_pem
},
185 const OSSL_DISPATCH ec_pub_text_encoder_functions
[] = {
186 { OSSL_FUNC_ENCODER_NEWCTX
, (void (*)(void))ec_pub_newctx
},
187 { OSSL_FUNC_ENCODER_FREECTX
, (void (*)(void))ec_pub_freectx
},
188 { OSSL_FUNC_ENCODER_ENCODE_OBJECT
, (void (*)(void))ec_pub_print
},
189 { OSSL_FUNC_ENCODER_ENCODE_DATA
,
190 (void (*)(void))ec_pub_print_data
},