Adapt existing SERIALIZER implementations to the redesigned interface
[openssl.git] / providers / implementations / serializers / serializer_dh_pub.c
1 /*
2  * Copyright 2019 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/dh.h>
14 #include <openssl/types.h>
15 #include <openssl/params.h>
16 #include "prov/bio.h"
17 #include "prov/implementations.h"
18 #include "serializer_local.h"
19
20 static OSSL_OP_serializer_newctx_fn dh_pub_newctx;
21 static OSSL_OP_serializer_freectx_fn dh_pub_freectx;
22 static OSSL_OP_serializer_serialize_data_fn dh_pub_der_data;
23 static OSSL_OP_serializer_serialize_object_fn dh_pub_der;
24 static OSSL_OP_serializer_serialize_data_fn dh_pub_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn dh_pub_pem;
26
27 static OSSL_OP_serializer_serialize_data_fn dh_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn dh_pub_print;
29
30 /* Public key : context */
31
32 /*
33  * There's no specific implementation context, so we use the provider context
34  */
35 static void *dh_pub_newctx(void *provctx)
36 {
37     return provctx;
38 }
39
40 static void dh_pub_freectx(void *ctx)
41 {
42 }
43
44 /* Public key : DER */
45 static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
46                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
47 {
48     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
49     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
50     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
51     int ok = 0;
52
53     if (dh_import != NULL) {
54         DH *dh;
55
56         /* ctx == provctx */
57         if ((dh = dh_new(ctx)) != NULL
58             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
59             && dh_pub_der(ctx, dh, out, cb, cbarg))
60             ok = 1;
61         dh_free(dh);
62     }
63     return ok;
64 }
65
66 static int dh_pub_der(void *ctx, void *dh, BIO *out,
67                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
68 {
69     return ossl_prov_write_pub_der_from_obj(out, dh, EVP_PKEY_DH,
70                                             ossl_prov_prepare_dh_params,
71                                             ossl_prov_dh_pub_to_der);
72 }
73
74 /* Public key : PEM */
75 static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
76                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
77 {
78     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
79     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
80     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
81     int ok = 0;
82
83     if (dh_import != NULL) {
84         DH *dh;
85
86         /* ctx == provctx */
87         if ((dh = dh_new(ctx)) != NULL
88             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
89             && dh_pub_pem(ctx, dh, out, cb, cbarg))
90             ok = 1;
91         dh_free(dh);
92     }
93     return ok;
94 }
95
96 static int dh_pub_pem(void *ctx, void *dh, BIO *out,
97                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
98 {
99     return ossl_prov_write_pub_pem_from_obj(out, dh, EVP_PKEY_DH,
100                                             ossl_prov_prepare_dh_params,
101                                             ossl_prov_dh_pub_to_der);
102
103 }
104
105 static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
106                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
107 {
108     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
109     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
110     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
111     int ok = 0;
112
113     if (dh_import != NULL) {
114         DH *dh;
115
116         /* ctx == provctx */
117         if ((dh = dh_new(ctx)) != NULL
118             && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
119             && dh_pub_print(ctx, dh, out, cb, cbarg))
120             ok = 1;
121         dh_free(dh);
122     }
123     return ok;
124 }
125
126 static int dh_pub_print(void *ctx, void *dh, BIO *out,
127                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
128 {
129     return ossl_prov_print_dh(out, dh, 0);
130 }
131
132 const OSSL_DISPATCH dh_pub_der_serializer_functions[] = {
133     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
134     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
135     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_der_data },
136     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_der },
137     { 0, NULL }
138 };
139
140 const OSSL_DISPATCH dh_pub_pem_serializer_functions[] = {
141     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
142     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
143     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_pem_data },
144     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_pem },
145     { 0, NULL }
146 };
147
148 const OSSL_DISPATCH dh_pub_text_serializer_functions[] = {
149     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
150     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
151     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_print },
152     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
153       (void (*)(void))dh_pub_print_data },
154     { 0, NULL }
155 };