2 * Copyright 2019-2020 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_FUNC_keymgmt_new_fn *ossl_prov_get_keymgmt_dsa_new(void)
27 return ossl_prov_get_keymgmt_new(dsa_keymgmt_functions);
30 OSSL_FUNC_keymgmt_free_fn *ossl_prov_get_keymgmt_dsa_free(void)
32 return ossl_prov_get_keymgmt_free(dsa_keymgmt_functions);
35 OSSL_FUNC_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 (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
79 && !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
82 && !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
84 if (!ffc_params_prov_print(out, dsa_get0_params(dsa)))
91 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
95 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
96 void **pstr, int *pstrtype)
98 ASN1_STRING *params = ASN1_STRING_new();
100 if (params == NULL) {
101 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
105 params->length = i2d_DSAparams(dsa, ¶ms->data);
107 if (params->length <= 0) {
108 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
109 ASN1_STRING_free(params);
113 *pstrtype = V_ASN1_SEQUENCE;
118 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
119 void **pstr, int *pstrtype)
121 const BIGNUM *p = DSA_get0_p(dsa);
122 const BIGNUM *q = DSA_get0_q(dsa);
123 const BIGNUM *g = DSA_get0_g(dsa);
125 if (p != NULL && q != NULL && g != NULL)
126 return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
129 *pstrtype = V_ASN1_UNDEF;
133 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
135 const BIGNUM *bn = NULL;
136 ASN1_INTEGER *pub_key = NULL;
139 if ((bn = DSA_get0_pub_key(dsa)) == NULL) {
140 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
143 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
144 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
148 ret = i2d_ASN1_INTEGER(pub_key, pder);
150 ASN1_STRING_clear_free(pub_key);
154 int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder)
156 const BIGNUM *bn = NULL;
157 ASN1_INTEGER *priv_key = NULL;
160 if ((bn = DSA_get0_priv_key(dsa)) == NULL) {
161 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
164 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
165 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
169 ret = i2d_ASN1_INTEGER(priv_key, pder);
171 ASN1_STRING_clear_free(priv_key);