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
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"
17 OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dsa_importkey(void)
19 return ossl_prov_get_importkey(dsa_keymgmt_functions);
22 int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type)
24 const char *type_label = NULL;
25 const BIGNUM *priv_key = NULL, *pub_key = NULL;
26 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
31 type_label = "Private-Key";
34 type_label = "Public-Key";
36 case dsa_print_params:
37 type_label = "DSA-Parameters";
41 if (type == dsa_print_priv) {
42 priv_key = DSA_get0_priv_key(dsa);
47 if (type == dsa_print_priv || type == dsa_print_pub) {
48 pub_key = DSA_get0_pub_key(dsa);
57 if (p == NULL || q == NULL || g == NULL)
60 if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
64 && !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
67 && !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
69 if (!ossl_prov_print_labeled_bignum(out, "P: ", p))
71 if (!ossl_prov_print_labeled_bignum(out, "Q: ", q))
73 if (!ossl_prov_print_labeled_bignum(out, "G: ", g))
80 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
84 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
85 ASN1_STRING **pstr, int *pstrtype)
87 ASN1_STRING *params = ASN1_STRING_new();
90 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
94 params->length = i2d_DSAparams(dsa, ¶ms->data);
96 if (params->length <= 0) {
97 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
98 ASN1_STRING_free(params);
102 *pstrtype = V_ASN1_SEQUENCE;
107 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
108 ASN1_STRING **pstr, int *pstrtype)
110 const BIGNUM *p = DSA_get0_p(dsa);
111 const BIGNUM *q = DSA_get0_q(dsa);
112 const BIGNUM *g = DSA_get0_g(dsa);
114 if (p != NULL && q != NULL && g != NULL)
115 return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
118 *pstrtype = V_ASN1_UNDEF;
122 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
124 ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa), NULL);
127 if (pub_key == NULL) {
128 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
132 ret = i2d_ASN1_INTEGER(pub_key, pder);
134 ASN1_STRING_clear_free(pub_key);
138 int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder)
140 ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa), NULL);
143 if (priv_key == NULL) {
144 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
148 ret = i2d_ASN1_INTEGER(priv_key, pder);
150 ASN1_STRING_clear_free(priv_key);