Deprecate the low level DSA functions.
[openssl.git] / providers / implementations / serializers / serializer_dh_param.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/pem.h>
12 #include <openssl/dh.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
15 #include "prov/bio.h"
16 #include "prov/implementations.h"
17 #include "prov/providercommonerr.h"
18 #include "serializer_local.h"
19
20 static OSSL_OP_serializer_newctx_fn dh_param_newctx;
21 static OSSL_OP_serializer_freectx_fn dh_param_freectx;
22 static OSSL_OP_serializer_serialize_data_fn dh_param_der_data;
23 static OSSL_OP_serializer_serialize_object_fn dh_param_der;
24 static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn dh_param_pem;
26
27 static OSSL_OP_serializer_serialize_data_fn dh_param_print_data;
28 static OSSL_OP_serializer_serialize_object_fn dh_param_print;
29
30 /* Parameters : context */
31
32 /*
33  * There's no specific implementation context, so we use the provider context
34  */
35 static void *dh_param_newctx(void *provctx)
36 {
37     return provctx;
38 }
39
40 static void dh_param_freectx(void *ctx)
41 {
42 }
43
44 /* Public key : DER */
45 static int dh_param_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_ALL_PARAMETERS, params)
59             && dh_param_der(ctx, dh, out, cb, cbarg))
60             ok = 1;
61         dh_free(dh);
62     }
63     return ok;
64 }
65
66 static int dh_param_der(void *ctx, void *dh, BIO *out,
67                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
68 {
69     return i2d_DHparams_bio(out, dh);
70 }
71
72 /* Public key : PEM */
73 static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
74                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
75 {
76     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
77     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
78     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
79     int ok = 0;
80
81     if (dh_import != NULL) {
82         DH *dh;
83
84         /* ctx == provctx */
85         if ((dh = dh_new(ctx)) != NULL
86             && dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
87             && dh_param_pem(ctx, dh, out, cb, cbarg))
88             ok = 1;
89         dh_free(dh);
90     }
91     return ok;
92 }
93
94 static int dh_param_pem(void *ctx, void *dh, BIO *out,
95                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
96 {
97     return PEM_write_bio_DHparams(out, dh);
98 }
99
100 static int dh_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
101                                OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
102 {
103     OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
104     OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
105     OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
106     int ok = 0;
107
108     if (dh_import != NULL) {
109         DH *dh;
110
111         /* ctx == provctx */
112         if ((dh = dh_new(ctx)) != NULL
113             && dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
114             && dh_param_print(ctx, dh, out, cb, cbarg))
115             ok = 1;
116         dh_free(dh);
117     }
118     return ok;
119 }
120
121 static int dh_param_print(void *ctx, void *dh, BIO *out,
122                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
123 {
124     return ossl_prov_print_dh(out, dh, dh_print_params);
125 }
126
127 const OSSL_DISPATCH dh_param_der_serializer_functions[] = {
128     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
129     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
130     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_der_data },
131     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_der },
132     { 0, NULL }
133 };
134
135 const OSSL_DISPATCH dh_param_pem_serializer_functions[] = {
136     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
137     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
138     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_pem_data },
139     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_pem },
140     { 0, NULL }
141 };
142
143 const OSSL_DISPATCH dh_param_text_serializer_functions[] = {
144     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
145     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
146     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_print },
147     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
148       (void (*)(void))dh_param_print_data },
149     { 0, NULL }
150 };