Add Serializers for EC
[openssl.git] / providers / implementations / serializers / serializer_ec_pub.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <openssl/core_numbers.h>
11 #include <openssl/err.h>
12 #include <openssl/pem.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
15 #include "prov/bio.h"
16 #include "prov/implementations.h"
17 #include "serializer_local.h"
18
19 static OSSL_OP_serializer_newctx_fn ec_pub_newctx;
20 static OSSL_OP_serializer_freectx_fn ec_pub_freectx;
21 static OSSL_OP_serializer_serialize_data_fn ec_pub_der_data;
22 static OSSL_OP_serializer_serialize_object_fn ec_pub_der;
23 static OSSL_OP_serializer_serialize_data_fn ec_pub_pem_data;
24 static OSSL_OP_serializer_serialize_object_fn ec_pub_pem;
25
26 static OSSL_OP_serializer_serialize_data_fn ec_pub_print_data;
27 static OSSL_OP_serializer_serialize_object_fn ec_pub_print;
28
29 /* Public key : context */
30
31 /*
32  * There's no specific implementation context, so we use the provider context
33  */
34 static void *ec_pub_newctx(void *provctx)
35 {
36     return provctx;
37 }
38
39 static void ec_pub_freectx(void *ctx)
40 {
41 }
42
43 /* Public key : DER */
44 static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
45                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
46 {
47     OSSL_OP_keymgmt_new_fn *ec_new;
48     OSSL_OP_keymgmt_free_fn *ec_free;
49     OSSL_OP_keymgmt_import_fn *ec_import;
50     int ok = 0;
51
52     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
53
54     if (ec_import != NULL) {
55         EC_KEY *eckey;
56
57         /* vctx == provctx */
58         if ((eckey = ec_new(vctx)) != NULL
59             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
60             && ec_pub_der(vctx, eckey, out, cb, cbarg))
61             ok = 1;
62         ec_free(eckey);
63     }
64     return ok;
65 }
66
67 static int ec_pub_der(void *ctx, void *eckey, BIO *out,
68                       OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
69 {
70     return ossl_prov_write_pub_der_from_obj(out, eckey, EVP_PKEY_EC,
71                                            ossl_prov_prepare_ec_params,
72                                            ossl_prov_ec_pub_to_der);
73 }
74
75 /* Public key : PEM */
76 static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
77                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
78 {
79     OSSL_OP_keymgmt_new_fn *ec_new;
80     OSSL_OP_keymgmt_free_fn *ec_free;
81     OSSL_OP_keymgmt_import_fn *ec_import;
82     int ok = 0;
83
84     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
85
86     if (ec_import != NULL) {
87         EC_KEY *eckey;
88
89         /* ctx == provctx */
90         if ((eckey = ec_new(vctx)) != NULL
91             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
92             && ec_pub_pem(vctx, eckey, out, cb, cbarg))
93             ok = 1;
94         ec_free(eckey);
95     }
96     return ok;
97 }
98
99 static int ec_pub_pem(void *vctx, void *eckey, BIO *out,
100                       OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
101 {
102     return ossl_prov_write_pub_pem_from_obj(out, eckey, EVP_PKEY_EC,
103                                             ossl_prov_prepare_ec_params,
104                                             ossl_prov_ec_pub_to_der);
105 }
106
107 static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
108                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
109 {
110     OSSL_OP_keymgmt_new_fn *ec_new;
111     OSSL_OP_keymgmt_free_fn *ec_free;
112     OSSL_OP_keymgmt_import_fn *ec_import;
113     int ok = 0;
114
115     ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
116
117     if (ec_import != NULL) {
118         EC_KEY *eckey;
119
120         /* ctx == provctx */
121         if ((eckey = ec_new(vctx)) != NULL
122             && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
123             && ec_pub_print(vctx, eckey, out, cb, cbarg))
124             ok = 1;
125         ec_free(eckey);
126     }
127     return ok;
128 }
129
130 static int ec_pub_print(void *vctx, void *eckey, BIO *out,
131                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
132 {
133     return ossl_prov_print_eckey(out, eckey, ec_print_pub);
134 }
135
136 const OSSL_DISPATCH ec_pub_der_serializer_functions[] = {
137     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
138     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
139     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_der_data },
140     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_der },
141     { 0, NULL }
142 };
143
144 const OSSL_DISPATCH ec_pub_pem_serializer_functions[] = {
145     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
146     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
147     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_pem_data },
148     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_pem },
149     { 0, NULL }
150 };
151
152 const OSSL_DISPATCH ec_pub_text_serializer_functions[] = {
153     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
154     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
155     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_print },
156     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
157       (void (*)(void))ec_pub_print_data },
158     { 0, NULL }
159 };