]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_dsa.c
Maintain strict type discipline between the core and providers
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_dsa.c
CommitLineData
264b789b 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
264b789b
RL
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
f41ac0ee
P
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
264b789b
RL
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"
b03ec3b5
SL
22#include "internal/ffc.h"
23#include "crypto/dsa.h"
264b789b 24
32b0645c 25OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dsa_new(void)
264b789b 26{
32b0645c
RL
27 return ossl_prov_get_keymgmt_new(dsa_keymgmt_functions);
28}
29
30OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dsa_free(void)
31{
32 return ossl_prov_get_keymgmt_free(dsa_keymgmt_functions);
33}
34
35OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dsa_import(void)
36{
37 return ossl_prov_get_keymgmt_import(dsa_keymgmt_functions);
264b789b
RL
38}
39
40int 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;
b03ec3b5 44 const BIGNUM *p = NULL;
264b789b
RL
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
264b789b 71
b03ec3b5
SL
72 p = DSA_get0_p(dsa);
73 if (p == NULL)
264b789b
RL
74 goto null_err;
75
d40b42ab 76 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
264b789b
RL
77 goto err;
78 if (priv_key != NULL
79 && !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
80 goto err;
81 if (pub_key != NULL
82 && !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
83 goto err;
b03ec3b5 84 if (!ffc_params_prov_print(out, dsa_get0_params(dsa)))
264b789b
RL
85 goto err;
86
87 return 1;
88 err:
89 return 0;
90 null_err:
91 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
92 goto err;
93}
94
95int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
f552d900 96 void **pstr, int *pstrtype)
264b789b
RL
97{
98 ASN1_STRING *params = ASN1_STRING_new();
99
100 if (params == NULL) {
101 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
102 return 0;
103 }
104
105 params->length = i2d_DSAparams(dsa, &params->data);
106
107 if (params->length <= 0) {
108 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
109 ASN1_STRING_free(params);
110 return 0;
111 }
112
113 *pstrtype = V_ASN1_SEQUENCE;
114 *pstr = params;
115 return 1;
116}
117
118int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
f552d900 119 void **pstr, int *pstrtype)
264b789b
RL
120{
121 const BIGNUM *p = DSA_get0_p(dsa);
122 const BIGNUM *q = DSA_get0_q(dsa);
123 const BIGNUM *g = DSA_get0_g(dsa);
124
125 if (p != NULL && q != NULL && g != NULL)
126 return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
127
128 *pstr = NULL;
129 *pstrtype = V_ASN1_UNDEF;
130 return 1;
131}
132
133int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
134{
135 ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa), NULL);
136 int ret;
137
138 if (pub_key == NULL) {
139 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
140 return 0;
141 }
142
143 ret = i2d_ASN1_INTEGER(pub_key, pder);
144
145 ASN1_STRING_clear_free(pub_key);
146 return ret;
147}
148
149int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder)
150{
151 ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa), NULL);
152 int ret;
153
154 if (priv_key == NULL) {
155 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
156 return 0;
157 }
158
159 ret = i2d_ASN1_INTEGER(priv_key, pder);
160
161 ASN1_STRING_clear_free(priv_key);
162 return ret;
163}