]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_dh_param.c
Adapt existing SERIALIZER implementations to the redesigned interface
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_dh_param.c
CommitLineData
045e51cb
RL
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
20static OSSL_OP_serializer_newctx_fn dh_param_newctx;
21static OSSL_OP_serializer_freectx_fn dh_param_freectx;
22static OSSL_OP_serializer_serialize_data_fn dh_param_der_data;
23static OSSL_OP_serializer_serialize_object_fn dh_param_der;
24static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data;
25static OSSL_OP_serializer_serialize_object_fn dh_param_pem;
26
27static OSSL_OP_serializer_serialize_data_fn dh_param_print_data;
28static 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 */
35static void *dh_param_newctx(void *provctx)
36{
37 return provctx;
38}
39
40static void dh_param_freectx(void *ctx)
41{
42}
43
44/* Public key : DER */
45static int dh_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
46 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
47{
32b0645c
RL
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();
045e51cb
RL
51 int ok = 0;
52
32b0645c
RL
53 if (dh_import != NULL) {
54 DH *dh;
045e51cb 55
32b0645c
RL
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);
045e51cb
RL
62 }
63 return ok;
64}
65
66static 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 */
73static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
32b0645c 74 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 75{
32b0645c
RL
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();
045e51cb
RL
79 int ok = 0;
80
32b0645c
RL
81 if (dh_import != NULL) {
82 DH *dh;
045e51cb 83
32b0645c
RL
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);
045e51cb
RL
90 }
91 return ok;
92}
93
94static 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
100static int dh_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
32b0645c 101 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 102{
32b0645c
RL
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();
045e51cb
RL
106 int ok = 0;
107
32b0645c
RL
108 if (dh_import != NULL) {
109 DH *dh;
045e51cb 110
32b0645c
RL
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);
045e51cb
RL
117 }
118 return ok;
119}
120
121static 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
127const 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
135const 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
143const 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};