]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_asn1.c
Deprecate the low level Diffie-Hellman functions.
[thirdparty/openssl.git] / crypto / dh / dh_asn1.c
1 /*
2 * Copyright 2000-2016 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 *)*pval)->dirty_cnt++;
38 }
39 return 1;
40 }
41
42 ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
43 ASN1_SIMPLE(DH, params.p, BIGNUM),
44 ASN1_SIMPLE(DH, params.g, BIGNUM),
45 ASN1_OPT_EMBED(DH, length, ZINT32),
46 } ASN1_SEQUENCE_END_cb(DH, DHparams)
47
48 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
49
50 /*
51 * Internal only structures for handling X9.42 DH: this gets translated to or
52 * from a DH structure straight away.
53 */
54
55 typedef struct {
56 ASN1_BIT_STRING *seed;
57 BIGNUM *counter;
58 } int_dhvparams;
59
60 typedef struct {
61 BIGNUM *p;
62 BIGNUM *q;
63 BIGNUM *g;
64 BIGNUM *j;
65 int_dhvparams *vparams;
66 } int_dhx942_dh;
67
68 ASN1_SEQUENCE(DHvparams) = {
69 ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
70 ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
71 } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
72
73 ASN1_SEQUENCE(DHxparams) = {
74 ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
75 ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
76 ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
77 ASN1_OPT(int_dhx942_dh, j, BIGNUM),
78 ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
79 } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
80
81 int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
82 const unsigned char **pp, long length);
83 int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
84
85 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
86
87 /* Application public function: read in X9.42 DH parameters into DH structure */
88
89 DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
90 {
91 FFC_PARAMS *params;
92 int_dhx942_dh *dhx = NULL;
93 DH *dh = NULL;
94
95 dh = DH_new();
96 if (dh == NULL)
97 return NULL;
98 dhx = d2i_int_dhx(NULL, pp, length);
99 if (dhx == NULL) {
100 DH_free(dh);
101 return NULL;
102 }
103
104 if (a != NULL) {
105 DH_free(*a);
106 *a = dh;
107 }
108
109 params = &dh->params;
110 DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
111 ffc_params_set0_j(params, dhx->j);
112
113 if (dhx->vparams != NULL) {
114 /* The counter has a maximum value of 4 * numbits(p) - 1 */
115 size_t counter = (size_t)BN_get_word(dhx->vparams->counter);
116 ffc_params_set_validate_params(params, dhx->vparams->seed->data,
117 dhx->vparams->seed->length, counter);
118 ASN1_BIT_STRING_free(dhx->vparams->seed);
119 BN_free(dhx->vparams->counter);
120 OPENSSL_free(dhx->vparams);
121 dhx->vparams = NULL;
122 }
123
124 OPENSSL_free(dhx);
125 return dh;
126 }
127
128 int i2d_DHxparams(const DH *dh, unsigned char **pp)
129 {
130 int ret = 0;
131 int_dhx942_dh dhx;
132 int_dhvparams dhv = { NULL, NULL };
133 ASN1_BIT_STRING seed;
134 size_t seedlen = 0;
135 const FFC_PARAMS *params = &dh->params;
136 int counter;
137
138 ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
139 (const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
140 dhx.j = params->j;
141 ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
142 seed.length = (int)seedlen;
143
144 if (counter != -1 && seed.data != NULL && seed.length > 0) {
145 seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
146 dhv.seed = &seed;
147 dhv.counter = BN_new();
148 if (dhv.counter == NULL)
149 return 0;
150 if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
151 goto err;
152 dhx.vparams = &dhv;
153 } else {
154 dhx.vparams = NULL;
155 }
156 ret = i2d_int_dhx(&dhx, pp);
157 err:
158 BN_free(dhv.counter);
159 return ret;
160 }