DH: Move the code to set the DH sub-type
[openssl.git] / crypto / dh / dh_asn1.c
1 /*
2  * Copyright 2000-2020 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 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/bn.h>
19 #include "dh_local.h"
20 #include <openssl/objects.h>
21 #include <openssl/asn1t.h>
22
23 /* Override the default free and new methods */
24 static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
25                  void *exarg)
26 {
27     if (operation == ASN1_OP_NEW_PRE) {
28         *pval = (ASN1_VALUE *)DH_new();
29         if (*pval != NULL)
30             return 2;
31         return 0;
32     } else if (operation == ASN1_OP_FREE_PRE) {
33         DH_free((DH *)*pval);
34         *pval = NULL;
35         return 2;
36     } else if (operation == ASN1_OP_D2I_POST) {
37         DH *dh = (DH *)*pval;
38
39         DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
40         DH_set_flags(dh, DH_FLAG_TYPE_DH);
41         dh->dirty_cnt++;
42     }
43     return 1;
44 }
45
46 ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
47         ASN1_SIMPLE(DH, params.p, BIGNUM),
48         ASN1_SIMPLE(DH, params.g, BIGNUM),
49         ASN1_OPT_EMBED(DH, length, ZINT32),
50 } ASN1_SEQUENCE_END_cb(DH, DHparams)
51
52 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
53
54 /*
55  * Internal only structures for handling X9.42 DH: this gets translated to or
56  * from a DH structure straight away.
57  */
58
59 typedef struct {
60     ASN1_BIT_STRING *seed;
61     BIGNUM *counter;
62 } int_dhvparams;
63
64 typedef struct {
65     BIGNUM *p;
66     BIGNUM *q;
67     BIGNUM *g;
68     BIGNUM *j;
69     int_dhvparams *vparams;
70 } int_dhx942_dh;
71
72 ASN1_SEQUENCE(DHvparams) = {
73         ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
74         ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
75 } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
76
77 ASN1_SEQUENCE(DHxparams) = {
78         ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
79         ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
80         ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
81         ASN1_OPT(int_dhx942_dh, j, BIGNUM),
82         ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
83 } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
84
85 int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
86                            const unsigned char **pp, long length);
87 int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
88
89 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
90
91 /* Application public function: read in X9.42 DH parameters into DH structure */
92
93 DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
94 {
95     FFC_PARAMS *params;
96     int_dhx942_dh *dhx = NULL;
97     DH *dh = NULL;
98
99     dh = DH_new();
100     if (dh == NULL)
101         return NULL;
102     dhx = d2i_int_dhx(NULL, pp, length);
103     if (dhx == NULL) {
104         DH_free(dh);
105         return NULL;
106     }
107
108     if (a != NULL) {
109         DH_free(*a);
110         *a = dh;
111     }
112
113     params = &dh->params;
114     DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
115     ossl_ffc_params_set0_j(params, dhx->j);
116
117     if (dhx->vparams != NULL) {
118         /* The counter has a maximum value of 4 * numbits(p) - 1 */
119         size_t counter = (size_t)BN_get_word(dhx->vparams->counter);
120         ossl_ffc_params_set_validate_params(params, dhx->vparams->seed->data,
121                                             dhx->vparams->seed->length,
122                                             counter);
123         ASN1_BIT_STRING_free(dhx->vparams->seed);
124         BN_free(dhx->vparams->counter);
125         OPENSSL_free(dhx->vparams);
126         dhx->vparams = NULL;
127     }
128
129     OPENSSL_free(dhx);
130     DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
131     DH_set_flags(dh, DH_FLAG_TYPE_DHX);
132     return dh;
133 }
134
135 int i2d_DHxparams(const DH *dh, unsigned char **pp)
136 {
137     int ret = 0;
138     int_dhx942_dh dhx;
139     int_dhvparams dhv = { NULL, NULL };
140     ASN1_BIT_STRING seed;
141     size_t seedlen = 0;
142     const FFC_PARAMS *params = &dh->params;
143     int counter;
144
145     ossl_ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
146                              (const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
147     dhx.j = params->j;
148     ossl_ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
149     seed.length = (int)seedlen;
150
151     if (counter != -1 && seed.data != NULL && seed.length > 0) {
152         seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
153         dhv.seed = &seed;
154         dhv.counter = BN_new();
155         if (dhv.counter == NULL)
156             return 0;
157         if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
158             goto err;
159         dhx.vparams = &dhv;
160     } else {
161         dhx.vparams = NULL;
162     }
163     ret = i2d_int_dhx(&dhx, pp);
164 err:
165     BN_free(dhv.counter);
166     return ret;
167 }