PROV: Fix bignum printout in text serializers
[openssl.git] / providers / implementations / serializers / serializer_dsa_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 #include <openssl/core_numbers.h>
11 #include <openssl/err.h>
12 #include <openssl/pem.h>
13 #include <openssl/dsa.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 dsa_pub_newctx;
21 static OSSL_OP_serializer_freectx_fn dsa_pub_freectx;
22 static OSSL_OP_serializer_serialize_data_fn dsa_pub_der_data;
23 static OSSL_OP_serializer_serialize_object_fn dsa_pub_der;
24 static OSSL_OP_serializer_serialize_data_fn dsa_pub_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn dsa_pub_pem;
26
27 static OSSL_OP_serializer_serialize_data_fn dsa_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn dsa_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 *dsa_pub_newctx(void *provctx)
36 {
37     return provctx;
38 }
39
40 static void dsa_pub_freectx(void *ctx)
41 {
42 }
43
44 /* Public key : DER */
45 static int dsa_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 *dsa_importkey =
49         ossl_prov_get_dsa_importkey();
50     int ok = 0;
51
52     if (dsa_importkey != NULL) {
53         DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
54
55         ok = dsa_pub_der(ctx, dsa, out, cb, cbarg);
56         DSA_free(dsa);
57     }
58     return ok;
59 }
60
61 static int dsa_pub_der(void *ctx, void *dsa, BIO *out,
62                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
63 {
64     /*
65      * TODO(v3.0) implement setting save_parameters, see dsa_pub_encode()
66      * in crypto/dsa/dsa_ameth.c
67      */
68     int save_parameters = 1;
69
70     return
71         save_parameters
72         ? ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA,
73                                            ossl_prov_prepare_all_dsa_params,
74                                            ossl_prov_dsa_pub_to_der)
75         : ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA,
76                                            ossl_prov_prepare_dsa_params,
77                                            ossl_prov_dsa_pub_to_der);
78
79 }
80
81 /* Public key : PEM */
82 static int dsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
83                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
84 {
85     OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
86         ossl_prov_get_dsa_importkey();
87     int ok = 0;
88
89     if (dsa_importkey != NULL) {
90         DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
91
92         ok = dsa_pub_pem(ctx, dsa, out, cb, cbarg);
93         DSA_free(dsa);
94     }
95     return ok;
96 }
97
98 static int dsa_pub_pem(void *ctx, void *dsa, BIO *out,
99                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
100 {
101     return ossl_prov_write_pub_pem_from_obj(out, dsa, EVP_PKEY_DSA,
102                                             ossl_prov_prepare_dsa_params,
103                                             ossl_prov_dsa_pub_to_der);
104 }
105
106 static int dsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
107                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
108 {
109     OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
110         ossl_prov_get_dsa_importkey();
111     int ok = 0;
112
113     if (dsa_importkey != NULL) {
114         DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
115
116         ok = dsa_pub_print(ctx, dsa, out, cb, cbarg);
117         DSA_free(dsa);
118     }
119     return ok;
120 }
121
122 static int dsa_pub_print(void *ctx, void *dsa, BIO *out,
123                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
124 {
125     return ossl_prov_print_dsa(out, dsa, 0);
126 }
127
128 const OSSL_DISPATCH dsa_pub_der_serializer_functions[] = {
129     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
130     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
131     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pub_der_data },
132     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_der },
133     { 0, NULL }
134 };
135
136 const OSSL_DISPATCH dsa_pub_pem_serializer_functions[] = {
137     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
138     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
139     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pub_pem_data },
140     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_pem },
141     { 0, NULL }
142 };
143
144 const OSSL_DISPATCH dsa_pub_text_serializer_functions[] = {
145     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
146     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
147     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_print },
148     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
149       (void (*)(void))dsa_pub_print_data },
150     { 0, NULL }
151 };