da74e221ef268108811285aedebc8829bb84dfc1
[openssl.git] / providers / implementations / serializers / serializer_dh.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/dh.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"
16
17 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
18 {
19     return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
20 }
21
22 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
23 {
24     return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
25 }
26
27 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
28 {
29     return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
30 }
31
32 int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
33 {
34     const char *type_label = NULL;
35     const BIGNUM *priv_key = NULL, *pub_key = NULL;
36     const BIGNUM *p = NULL, *g = NULL;
37
38
39     switch (type) {
40     case dh_print_priv:
41         type_label = "DH Private-Key";
42         break;
43     case dh_print_pub:
44         type_label = "DH Public-Key";
45         break;
46     case dh_print_params:
47         type_label = "DH Parameters";
48         break;
49     }
50
51     if (type == dh_print_priv) {
52         priv_key = DH_get0_priv_key(dh);
53         if (priv_key == NULL)
54             goto null_err;
55     }
56
57     if (type == dh_print_priv || type == dh_print_pub) {
58         pub_key = DH_get0_pub_key(dh);
59         if (pub_key == NULL)
60             goto null_err;
61     }
62
63     p = DH_get0_p(dh);
64     g = DH_get0_g(dh);
65     if (p == NULL || g == NULL)
66         goto null_err;
67
68     /*
69      * TODO(3.0): add printing of:
70      *
71      * - q (label "subgroup order:")
72      * - j (label "subgroup factor:")
73      * - seed (label "seed:")
74      * - counter (label "counter:")
75      *
76      * This can happen as soon as there are DH_get0_ functions for them.
77      */
78
79     if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
80         <= 0)
81         goto err;
82     if (priv_key != NULL
83         && !ossl_prov_print_labeled_bignum(out, "    private-key:", priv_key))
84         goto err;
85     if (pub_key != NULL
86         && !ossl_prov_print_labeled_bignum(out, "    public-key:", pub_key))
87         goto err;
88     if (p != NULL
89         && !ossl_prov_print_labeled_bignum(out, "    prime:", p))
90         goto err;
91     if (g != NULL
92         && !ossl_prov_print_labeled_bignum(out, "    generator:", g))
93         goto err;
94
95     return 1;
96  err:
97     return 0;
98  null_err:
99     ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
100     goto err;
101 }
102
103 int ossl_prov_prepare_dh_params(const void *dh, int nid,
104                                 ASN1_STRING **pstr, int *pstrtype)
105 {
106     ASN1_STRING *params = ASN1_STRING_new();
107
108     if (params == NULL) {
109         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110         return 0;
111     }
112
113     if (nid == EVP_PKEY_DHX)
114         params->length = i2d_DHxparams(dh, &params->data);
115     else
116         params->length = i2d_DHparams(dh, &params->data);
117
118     if (params->length <= 0) {
119         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
120         ASN1_STRING_free(params);
121         return 0;
122     }
123     params->type = V_ASN1_SEQUENCE;
124
125     *pstr = params;
126     *pstrtype = V_ASN1_SEQUENCE;
127     return 1;
128 }
129
130 int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
131 {
132     ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL);
133     int ret;
134
135     if (pub_key == NULL) {
136         ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
137         return 0;
138     }
139
140     ret = i2d_ASN1_INTEGER(pub_key, pder);
141
142     ASN1_STRING_clear_free(pub_key);
143     return ret;
144 }
145
146 int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
147 {
148     ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL);
149     int ret;
150
151     if (priv_key == NULL) {
152         ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
153         return 0;
154     }
155
156     ret = i2d_ASN1_INTEGER(priv_key, pder);
157
158     ASN1_STRING_clear_free(priv_key);
159     return ret;
160 }
161