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"
23 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dsa_new(void)
25 return ossl_prov_get_keymgmt_new(dsa_keymgmt_functions);
28 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dsa_free(void)
30 return ossl_prov_get_keymgmt_free(dsa_keymgmt_functions);
33 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dsa_import(void)
35 return ossl_prov_get_keymgmt_import(dsa_keymgmt_functions);
38 int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type)
40 const char *type_label = NULL;
41 const BIGNUM *priv_key = NULL, *pub_key = NULL;
42 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
47 type_label = "Private-Key";
50 type_label = "Public-Key";
52 case dsa_print_params:
53 type_label = "DSA-Parameters";
57 if (type == dsa_print_priv) {
58 priv_key = DSA_get0_priv_key(dsa);
63 if (type == dsa_print_priv || type == dsa_print_pub) {
64 pub_key = DSA_get0_pub_key(dsa);
73 if (p == NULL || q == NULL || g == NULL)
76 if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
80 && !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
83 && !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
85 if (!ossl_prov_print_labeled_bignum(out, "P: ", p))
87 if (!ossl_prov_print_labeled_bignum(out, "Q: ", q))
89 if (!ossl_prov_print_labeled_bignum(out, "G: ", g))
96 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
100 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
101 ASN1_STRING **pstr, int *pstrtype)
103 ASN1_STRING *params = ASN1_STRING_new();
105 if (params == NULL) {
106 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110 params->length = i2d_DSAparams(dsa, ¶ms->data);
112 if (params->length <= 0) {
113 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
114 ASN1_STRING_free(params);
118 *pstrtype = V_ASN1_SEQUENCE;
123 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
124 ASN1_STRING **pstr, int *pstrtype)
126 const BIGNUM *p = DSA_get0_p(dsa);
127 const BIGNUM *q = DSA_get0_q(dsa);
128 const BIGNUM *g = DSA_get0_g(dsa);
130 if (p != NULL && q != NULL && g != NULL)
131 return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
134 *pstrtype = V_ASN1_UNDEF;
138 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
140 ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa), NULL);
143 if (pub_key == 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 ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa), NULL);
159 if (priv_key == NULL) {
160 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
164 ret = i2d_ASN1_INTEGER(priv_key, pder);
166 ASN1_STRING_clear_free(priv_key);