Update copyright year
[openssl.git] / providers / implementations / serializers / serializer_dsa.c
1 /*
2  * Copyright 2019-2020 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/dsa.h>
17 #include <openssl/err.h>
18 #include "prov/bio.h"             /* ossl_prov_bio_printf() */
19 #include "prov/implementations.h" /* rsa_keymgmt_functions */
20 #include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
21 #include "serializer_local.h"
22 #include "internal/ffc.h"
23 #include "crypto/dsa.h"
24
25 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dsa_new(void)
26 {
27     return ossl_prov_get_keymgmt_new(dsa_keymgmt_functions);
28 }
29
30 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dsa_free(void)
31 {
32     return ossl_prov_get_keymgmt_free(dsa_keymgmt_functions);
33 }
34
35 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dsa_import(void)
36 {
37     return ossl_prov_get_keymgmt_import(dsa_keymgmt_functions);
38 }
39
40 int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type)
41 {
42     const char *type_label = NULL;
43     const BIGNUM *priv_key = NULL, *pub_key = NULL;
44     const BIGNUM *p = NULL;
45
46
47     switch (type) {
48     case dsa_print_priv:
49         type_label = "Private-Key";
50         break;
51     case dsa_print_pub:
52         type_label = "Public-Key";
53         break;
54     case dsa_print_params:
55         type_label = "DSA-Parameters";
56         break;
57     }
58
59     if (type == dsa_print_priv) {
60         priv_key = DSA_get0_priv_key(dsa);
61         if (priv_key == NULL)
62             goto null_err;
63     }
64
65     if (type == dsa_print_priv || type == dsa_print_pub) {
66         pub_key = DSA_get0_pub_key(dsa);
67         if (pub_key == NULL)
68             goto null_err;
69     }
70
71
72     p = DSA_get0_p(dsa);
73     if (p == NULL)
74         goto null_err;
75
76     if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label,
77                              BN_num_bits(p)) <= 0)
78         goto err;
79     if (priv_key != NULL
80         && !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
81         goto err;
82     if (pub_key != NULL
83         && !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
84         goto err;
85     if (!ffc_params_prov_print(out, dsa_get0_params(dsa)))
86         goto err;
87
88     return 1;
89  err:
90     return 0;
91  null_err:
92     ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
93     goto err;
94 }
95
96 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
97                                  void **pstr, int *pstrtype)
98 {
99     ASN1_STRING *params = ASN1_STRING_new();
100
101     if (params == NULL) {
102         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
103         return 0;
104     }
105
106     params->length = i2d_DSAparams(dsa, &params->data);
107
108     if (params->length <= 0) {
109         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110         ASN1_STRING_free(params);
111         return 0;
112     }
113
114     *pstrtype = V_ASN1_SEQUENCE;
115     *pstr = params;
116     return 1;
117 }
118
119 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
120                                      void **pstr, int *pstrtype)
121 {
122     const BIGNUM *p = DSA_get0_p(dsa);
123     const BIGNUM *q = DSA_get0_q(dsa);
124     const BIGNUM *g = DSA_get0_g(dsa);
125
126     if (p != NULL && q != NULL && g != NULL)
127         return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
128
129     *pstr = NULL;
130     *pstrtype = V_ASN1_UNDEF;
131     return 1;
132 }
133
134 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
135 {
136     ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa), NULL);
137     int ret;
138
139     if (pub_key == NULL) {
140         ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
141         return 0;
142     }
143
144     ret = i2d_ASN1_INTEGER(pub_key, pder);
145
146     ASN1_STRING_clear_free(pub_key);
147     return ret;
148 }
149
150 int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder)
151 {
152     ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa), NULL);
153     int ret;
154
155     if (priv_key == NULL) {
156         ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
157         return 0;
158     }
159
160     ret = i2d_ASN1_INTEGER(priv_key, pder);
161
162     ASN1_STRING_clear_free(priv_key);
163     return ret;
164 }