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_importkey_fn *dh_importkey = |
49 | ossl_prov_get_dh_importkey(); |
50 | int ok = 0; |
51 | |
52 | if (dh_importkey != NULL) { |
53 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
54 | |
55 | ok = dh_pub_der(ctx, dh, out, cb, cbarg); |
56 | DH_free(dh); |
57 | } |
58 | return ok; |
59 | } |
60 | |
61 | static int dh_pub_der(void *ctx, void *dh, BIO *out, |
62 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
63 | { |
64 | return ossl_prov_write_pub_der_from_obj(out, dh, EVP_PKEY_DH, |
65 | ossl_prov_prepare_dh_params, |
66 | ossl_prov_dh_pub_to_der); |
67 | } |
68 | |
69 | /* Public key : PEM */ |
70 | static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out, |
71 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
72 | { |
73 | OSSL_OP_keymgmt_importkey_fn *dh_importkey = |
74 | ossl_prov_get_dh_importkey(); |
75 | int ok = 0; |
76 | |
77 | if (dh_importkey != NULL) { |
78 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
79 | |
80 | ok = dh_pub_pem(ctx, dh, out, cb, cbarg); |
81 | DH_free(dh); |
82 | } |
83 | return ok; |
84 | } |
85 | |
86 | static int dh_pub_pem(void *ctx, void *dh, BIO *out, |
87 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
88 | { |
89 | return ossl_prov_write_pub_pem_from_obj(out, dh, EVP_PKEY_DH, |
90 | ossl_prov_prepare_dh_params, |
91 | ossl_prov_dh_pub_to_der); |
92 | |
93 | } |
94 | |
95 | static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out, |
96 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
97 | { |
98 | OSSL_OP_keymgmt_importkey_fn *dh_importkey = |
99 | ossl_prov_get_dh_importkey(); |
100 | int ok = 0; |
101 | |
102 | if (dh_importkey != NULL) { |
103 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
104 | |
105 | ok = dh_pub_print(ctx, dh, out, cb, cbarg); |
106 | DH_free(dh); |
107 | } |
108 | return ok; |
109 | } |
110 | |
111 | static int dh_pub_print(void *ctx, void *dh, BIO *out, |
112 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
113 | { |
114 | return ossl_prov_print_dh(out, dh, 0); |
115 | } |
116 | |
117 | const OSSL_DISPATCH dh_pub_der_serializer_functions[] = { |
118 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx }, |
119 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx }, |
120 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_der_data }, |
121 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_der }, |
122 | { 0, NULL } |
123 | }; |
124 | |
125 | const OSSL_DISPATCH dh_pub_pem_serializer_functions[] = { |
126 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx }, |
127 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx }, |
128 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_pem_data }, |
129 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_pem }, |
130 | { 0, NULL } |
131 | }; |
132 | |
133 | const OSSL_DISPATCH dh_pub_text_serializer_functions[] = { |
134 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx }, |
135 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx }, |
136 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_print }, |
137 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, |
138 | (void (*)(void))dh_pub_print_data }, |
139 | { 0, NULL } |
140 | }; |
141 | |