]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/encoder_dh_pub.c
Fix up issue on AIX caused by broken compiler handling of macro expansion
[thirdparty/openssl.git] / providers / implementations / encode_decode / encoder_dh_pub.c
CommitLineData
045e51cb 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
045e51cb
RL
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
ada66e78
P
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
23c48d94 16#include <openssl/core_dispatch.h>
045e51cb
RL
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"
d40b42ab 24#include "prov/provider_ctx.h"
ece9304c 25#include "encoder_local.h"
045e51cb 26
ece9304c
RL
27static OSSL_FUNC_encoder_newctx_fn dh_pub_newctx;
28static OSSL_FUNC_encoder_freectx_fn dh_pub_freectx;
29static OSSL_FUNC_encoder_encode_data_fn dh_pub_der_data;
30static OSSL_FUNC_encoder_encode_object_fn dh_pub_der;
31static OSSL_FUNC_encoder_encode_data_fn dh_pub_pem_data;
32static OSSL_FUNC_encoder_encode_object_fn dh_pub_pem;
045e51cb 33
ece9304c
RL
34static OSSL_FUNC_encoder_encode_data_fn dh_pub_print_data;
35static OSSL_FUNC_encoder_encode_object_fn dh_pub_print;
045e51cb 36
3fab5663
SL
37#define DH_SELECT_PUBLIC_IMPORTABLE \
38 (OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
39
045e51cb
RL
40/* Public key : context */
41
42/*
43 * There's no specific implementation context, so we use the provider context
44 */
45static void *dh_pub_newctx(void *provctx)
46{
47 return provctx;
48}
49
50static void dh_pub_freectx(void *ctx)
51{
52}
53
54/* Public key : DER */
d40b42ab 55static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[],
ece9304c
RL
56 OSSL_CORE_BIO *out,
57 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 58{
363b1e5d
DMSP
59 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
60 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
61 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
62 int ok = 0;
63
32b0645c
RL
64 if (dh_import != NULL) {
65 DH *dh;
045e51cb 66
32b0645c
RL
67 /* ctx == provctx */
68 if ((dh = dh_new(ctx)) != NULL
3fab5663 69 && dh_import(dh, DH_SELECT_PUBLIC_IMPORTABLE, params)
32b0645c
RL
70 && dh_pub_der(ctx, dh, out, cb, cbarg))
71 ok = 1;
72 dh_free(dh);
045e51cb
RL
73 }
74 return ok;
75}
76
d40b42ab 77static int dh_pub_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
ece9304c 78 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 79{
d40b42ab
MC
80 BIO *out = bio_new_from_core_bio(ctx, cout);
81 int ret;
82
83 if (out == NULL)
84 return 0;
85
31d2daec
SL
86 ret = ossl_prov_write_pub_der_from_obj(out, dh,
87 ossl_prov_dh_type_to_evp(dh),
d40b42ab
MC
88 ossl_prov_prepare_dh_params,
89 ossl_prov_dh_pub_to_der);
90 BIO_free(out);
91
92 return ret;
045e51cb
RL
93}
94
95/* Public key : PEM */
d40b42ab
MC
96static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[],
97 OSSL_CORE_BIO *out,
98 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 99{
363b1e5d
DMSP
100 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
101 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
102 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
103 int ok = 0;
104
32b0645c
RL
105 if (dh_import != NULL) {
106 DH *dh;
045e51cb 107
32b0645c
RL
108 /* ctx == provctx */
109 if ((dh = dh_new(ctx)) != NULL
3fab5663 110 && dh_import(dh, DH_SELECT_PUBLIC_IMPORTABLE, params)
32b0645c
RL
111 && dh_pub_pem(ctx, dh, out, cb, cbarg))
112 ok = 1;
113 dh_free(dh);
045e51cb
RL
114 }
115 return ok;
116}
117
d40b42ab 118static int dh_pub_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
ece9304c 119 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 120{
d40b42ab
MC
121 BIO *out = bio_new_from_core_bio(ctx, cout);
122 int ret;
123
124 if (out == NULL)
125 return 0;
045e51cb 126
31d2daec
SL
127 ret = ossl_prov_write_pub_pem_from_obj(out, dh,
128 ossl_prov_dh_type_to_evp(dh),
d40b42ab
MC
129 ossl_prov_prepare_dh_params,
130 ossl_prov_dh_pub_to_der);
131 BIO_free(out);
132
133 return ret;
045e51cb
RL
134}
135
d40b42ab
MC
136static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[],
137 OSSL_CORE_BIO *out,
138 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 139{
363b1e5d
DMSP
140 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
141 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
142 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
143 int ok = 0;
144
32b0645c
RL
145 if (dh_import != NULL) {
146 DH *dh;
045e51cb 147
32b0645c
RL
148 /* ctx == provctx */
149 if ((dh = dh_new(ctx)) != NULL
3fab5663 150 && dh_import(dh, DH_SELECT_PUBLIC_IMPORTABLE, params)
32b0645c
RL
151 && dh_pub_print(ctx, dh, out, cb, cbarg))
152 ok = 1;
153 dh_free(dh);
045e51cb
RL
154 }
155 return ok;
156}
157
d40b42ab 158static int dh_pub_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
ece9304c 159 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 160{
d40b42ab
MC
161 BIO *out = bio_new_from_core_bio(ctx, cout);
162 int ret;
163
164 if (out == NULL)
165 return 0;
166
167 ret = ossl_prov_print_dh(out, dh, dh_print_pub);
168 BIO_free(out);
169
170 return ret;
045e51cb
RL
171}
172
ece9304c
RL
173const OSSL_DISPATCH dh_pub_der_encoder_functions[] = {
174 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx },
175 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx },
176 { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dh_pub_der_data },
177 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_der },
045e51cb
RL
178 { 0, NULL }
179};
180
ece9304c
RL
181const OSSL_DISPATCH dh_pub_pem_encoder_functions[] = {
182 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx },
183 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx },
184 { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dh_pub_pem_data },
185 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_pem },
045e51cb
RL
186 { 0, NULL }
187};
188
ece9304c
RL
189const OSSL_DISPATCH dh_pub_text_encoder_functions[] = {
190 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx },
191 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx },
192 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_print },
193 { OSSL_FUNC_ENCODER_ENCODE_DATA,
045e51cb
RL
194 (void (*)(void))dh_pub_print_data },
195 { 0, NULL }
196};