PROV: Add DERlib support for DSA
[openssl.git] / providers / implementations / serializers / serializer_dsa_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 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_numbers.h>
17 #include <openssl/pem.h>
18 #include <openssl/dsa.h>
19 #include <openssl/types.h>
20 #include <openssl/params.h>
21 #include "prov/bio.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "serializer_local.h"
25
26 static OSSL_OP_serializer_newctx_fn dsa_param_newctx;
27 static OSSL_OP_serializer_freectx_fn dsa_param_freectx;
28 static OSSL_OP_serializer_serialize_data_fn dsa_param_der_data;
29 static OSSL_OP_serializer_serialize_object_fn dsa_param_der;
30 static OSSL_OP_serializer_serialize_data_fn dsa_param_pem_data;
31 static OSSL_OP_serializer_serialize_object_fn dsa_param_pem;
32
33 static OSSL_OP_serializer_serialize_data_fn dsa_param_print_data;
34 static OSSL_OP_serializer_serialize_object_fn dsa_param_print;
35
36 /* Parameters : context */
37
38 /*
39  * There's no specific implementation context, so we use the provider context
40  */
41 static void *dsa_param_newctx(void *provctx)
42 {
43     return provctx;
44 }
45
46 static void dsa_param_freectx(void *ctx)
47 {
48 }
49
50 /* Public key : DER */
51 static int dsa_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
52                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
53 {
54     OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new();
55     OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free();
56     OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import();
57     int ok = 0;
58
59     if (dsa_import != NULL) {
60         DSA *dsa;
61
62         /* ctx == provctx */
63         if ((dsa = dsa_new(ctx)) != NULL
64             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
65             && dsa_param_der(ctx, dsa, out, cb, cbarg))
66             ok = 1;
67         dsa_free(dsa);
68     }
69     return ok;
70 }
71
72 static int dsa_param_der(void *ctx, void *dsa, BIO *out,
73                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
74 {
75     return i2d_DSAparams_bio(out, dsa);
76 }
77
78 /* Public key : PEM */
79 static int dsa_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
80                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
81 {
82     OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new();
83     OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free();
84     OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import();
85     int ok = 0;
86
87     if (dsa_import != NULL) {
88         DSA *dsa;
89
90         /* ctx == provctx */
91         if ((dsa = dsa_new(ctx)) != NULL
92             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
93             && dsa_param_pem(ctx, dsa, out, cb, cbarg))
94             ok = 1;
95         dsa_free(dsa);
96     }
97     return ok;
98 }
99
100 static int dsa_param_pem(void *ctx, void *dsa, BIO *out,
101                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
102 {
103     return PEM_write_bio_DSAparams(out, dsa);
104 }
105
106 static int dsa_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
107                                 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
108 {
109     OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new();
110     OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free();
111     OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import();
112     int ok = 0;
113
114     if (dsa_import != NULL) {
115         DSA *dsa;
116
117         /* ctx == provctx */
118         if ((dsa = dsa_new(ctx)) != NULL
119             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
120             && dsa_param_print(ctx, dsa, out, cb, cbarg))
121             ok = 1;
122         dsa_free(dsa);
123     }
124     return ok;
125 }
126
127 static int dsa_param_print(void *ctx, void *dsa, BIO *out,
128                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
129 {
130     return ossl_prov_print_dsa(out, dsa, dsa_print_params);
131 }
132
133 const OSSL_DISPATCH dsa_param_der_serializer_functions[] = {
134     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
135     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
136     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_param_der_data },
137     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_der },
138     { 0, NULL }
139 };
140
141 const OSSL_DISPATCH dsa_param_pem_serializer_functions[] = {
142     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
143     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
144     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_param_pem_data },
145     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_pem },
146     { 0, NULL }
147 };
148
149 const OSSL_DISPATCH dsa_param_text_serializer_functions[] = {
150     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
151     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
152     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_print },
153     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
154       (void (*)(void))dsa_param_print_data },
155     { 0, NULL }
156 };