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