2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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"
25 OSSL_OP_keymgmt_new_fn
*ossl_prov_get_keymgmt_dsa_new(void)
27 return ossl_prov_get_keymgmt_new(dsa_keymgmt_functions
);
30 OSSL_OP_keymgmt_free_fn
*ossl_prov_get_keymgmt_dsa_free(void)
32 return ossl_prov_get_keymgmt_free(dsa_keymgmt_functions
);
35 OSSL_OP_keymgmt_import_fn
*ossl_prov_get_keymgmt_dsa_import(void)
37 return ossl_prov_get_keymgmt_import(dsa_keymgmt_functions
);
40 int ossl_prov_print_dsa(BIO
*out
, DSA
*dsa
, enum dsa_print_type type
)
42 const char *type_label
= NULL
;
43 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
44 const BIGNUM
*p
= NULL
;
49 type_label
= "Private-Key";
52 type_label
= "Public-Key";
54 case dsa_print_params
:
55 type_label
= "DSA-Parameters";
59 if (type
== dsa_print_priv
) {
60 priv_key
= DSA_get0_priv_key(dsa
);
65 if (type
== dsa_print_priv
|| type
== dsa_print_pub
) {
66 pub_key
= DSA_get0_pub_key(dsa
);
76 if (ossl_prov_bio_printf(out
, "%s: (%d bit)\n", type_label
,
80 && !ossl_prov_print_labeled_bignum(out
, "priv:", priv_key
))
83 && !ossl_prov_print_labeled_bignum(out
, "pub: ", pub_key
))
85 if (!ffc_params_prov_print(out
, dsa_get0_params(dsa
)))
92 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
96 int ossl_prov_prepare_dsa_params(const void *dsa
, int nid
,
97 void **pstr
, int *pstrtype
)
99 ASN1_STRING
*params
= ASN1_STRING_new();
101 if (params
== NULL
) {
102 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
106 params
->length
= i2d_DSAparams(dsa
, ¶ms
->data
);
108 if (params
->length
<= 0) {
109 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
110 ASN1_STRING_free(params
);
114 *pstrtype
= V_ASN1_SEQUENCE
;
119 int ossl_prov_prepare_all_dsa_params(const void *dsa
, int nid
,
120 void **pstr
, int *pstrtype
)
122 const BIGNUM
*p
= DSA_get0_p(dsa
);
123 const BIGNUM
*q
= DSA_get0_q(dsa
);
124 const BIGNUM
*g
= DSA_get0_g(dsa
);
126 if (p
!= NULL
&& q
!= NULL
&& g
!= NULL
)
127 return ossl_prov_prepare_dsa_params(dsa
, nid
, pstr
, pstrtype
);
130 *pstrtype
= V_ASN1_UNDEF
;
134 int ossl_prov_dsa_pub_to_der(const void *dsa
, unsigned char **pder
)
136 ASN1_INTEGER
*pub_key
= BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa
), NULL
);
139 if (pub_key
== NULL
) {
140 ERR_raise(ERR_LIB_PROV
, PROV_R_BN_ERROR
);
144 ret
= i2d_ASN1_INTEGER(pub_key
, pder
);
146 ASN1_STRING_clear_free(pub_key
);
150 int ossl_prov_dsa_priv_to_der(const void *dsa
, unsigned char **pder
)
152 ASN1_INTEGER
*priv_key
= BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa
), NULL
);
155 if (priv_key
== NULL
) {
156 ERR_raise(ERR_LIB_PROV
, PROV_R_BN_ERROR
);
160 ret
= i2d_ASN1_INTEGER(priv_key
, pder
);
162 ASN1_STRING_clear_free(priv_key
);