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