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