1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* Copyright (C) 2000,2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "gnutls_int.h"
#include "gnutls_auth_int.h"
#include "gnutls_errors.h"
#include "gnutls_dh.h"
#include "auth_anon.h"
#include "gnutls_num.h"
#include "gnutls_gcry.h"
#define DEFAULT_BITS 1024
int gen_anon_server_kx( GNUTLS_KEY, opaque**);
int gen_anon_client_kx( GNUTLS_KEY, opaque**);
int proc_anon_server_kx( GNUTLS_KEY, opaque*, int);
int proc_anon_client_kx( GNUTLS_KEY, opaque*, int);
MOD_AUTH_STRUCT anon_auth_struct = {
"ANON",
NULL,
gen_anon_server_kx,
NULL,
NULL,
gen_anon_client_kx,
NULL,
NULL,
NULL, /* certificate */
proc_anon_server_kx,
NULL,
NULL,
proc_anon_client_kx,
NULL,
NULL
};
/* this function will copy an MPI key to
* opaque data.
*/
int _gnutls_generate_key(GNUTLS_KEY key) {
gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &key->key.size, key->KEY);
key->key.data = secure_malloc( key->key.size);
if (key->key.data==NULL) {
return GNUTLS_E_MEMORY_ERROR;
}
gcry_mpi_print(GCRYMPI_FMT_USG, key->key.data, &key->key.size, key->KEY);
return 0;
}
int gen_anon_server_kx( GNUTLS_KEY key, opaque** data) {
MPI x, X, g, p;
int bits;
size_t n_X, n_g, n_p;
uint8 *data_p;
uint8 *data_g;
uint8 *data_X;
const ANON_SERVER_CREDENTIALS cred;
cred = _gnutls_get_cred( key, GNUTLS_ANON, NULL);
if (cred==NULL) {
bits = DEFAULT_BITS; /* default */
} else {
bits = cred->dh_bits;
}
g = gnutls_get_dh_params(&p, bits);
key->auth_info = gnutls_malloc(sizeof(ANON_SERVER_AUTH_INFO));
if (key->auth_info==NULL) return GNUTLS_E_MEMORY_ERROR;
((ANON_SERVER_AUTH_INFO*)key->auth_info)->dh_bits = gcry_mpi_get_nbits(p);
key->auth_info_size = sizeof(ANON_SERVER_AUTH_INFO);
X = gnutls_calc_dh_secret(&x, g, p);
key->dh_secret = x;
gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &n_g, g);
gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &n_p, p);
gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &n_X, X);
(*data) = gnutls_malloc(n_g + n_p + n_X + 6);
data_p = &(*data)[0];
gcry_mpi_print(GCRYMPI_FMT_USG, &data_p[2], &n_p, p);
_gnutls_mpi_release(&p);
WRITEuint16( n_p, data_p);
data_g = &data_p[2 + n_p];
gcry_mpi_print(GCRYMPI_FMT_USG, &data_g[2], &n_g, g);
_gnutls_mpi_release(&g);
WRITEuint16( n_g, data_g);
data_X = &data_g[2 + n_g];
gcry_mpi_print(GCRYMPI_FMT_USG, &data_X[2], &n_X, X);
_gnutls_mpi_release(&X);
WRITEuint16( n_X, data_X);
return n_p+n_g+n_X+6;
}
int gen_anon_client_kx( GNUTLS_KEY key, opaque** data) {
MPI x, X;
size_t n_X;
int ret;
X = gnutls_calc_dh_secret(&x, key->client_g,
key->client_p);
gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &n_X, X);
(*data) = gnutls_malloc(n_X + 2);
gcry_mpi_print(GCRYMPI_FMT_USG, &(*data)[2], &n_X, X);
(*data)[0] = 1; /* extern - explicit since we do not have
certificate */
_gnutls_mpi_release(&X);
WRITEuint16( n_X, &(*data)[0]);
/* calculate the key after calculating the message */
key->KEY = gnutls_calc_dh_key(key->client_Y, x, key->client_p);
/* THESE SHOULD BE DISCARDED */
_gnutls_mpi_release(&key->client_Y);
_gnutls_mpi_release(&key->client_p);
_gnutls_mpi_release(&key->client_g);
ret = _gnutls_generate_key( key);
_gnutls_mpi_release(&key->KEY);
if (ret < 0) {
return ret;
}
return n_X+2;
}
int proc_anon_server_kx( GNUTLS_KEY key, opaque* data, int data_size) {
uint16 n_Y, n_g, n_p;
size_t _n_Y, _n_g, _n_p;
uint8 *data_p;
uint8 *data_g;
uint8 *data_Y;
int i;
i = 0;
n_p = READuint16( &data[i]);
i += 2;
data_p = &data[i];
i += n_p;
if (i > data_size) {
gnutls_assert();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
n_g = READuint16( &data[i]);
i += 2;
data_g = &data[i];
i += n_g;
if (i > data_size) {
gnutls_assert();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
n_Y = READuint16( &data[i]);
i += 2;
data_Y = &data[i];
i += n_Y;
if (i > data_size) {
gnutls_assert();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
_n_Y = n_Y;
_n_g = n_g;
_n_p = n_p;
if (gcry_mpi_scan(&key->client_Y,
GCRYMPI_FMT_USG, data_Y, &_n_Y) != 0) {
gnutls_assert();
return GNUTLS_E_MPI_SCAN_FAILED;
}
if (gcry_mpi_scan(&key->client_g,
GCRYMPI_FMT_USG, data_g, &_n_g) != 0) {
gnutls_assert();
return GNUTLS_E_MPI_SCAN_FAILED;
}
if (gcry_mpi_scan(&key->client_p,
GCRYMPI_FMT_USG, data_p, &_n_p) != 0) {
gnutls_assert();
return GNUTLS_E_MPI_SCAN_FAILED;
}
/* set auth_info */
key->auth_info = gnutls_malloc(sizeof(ANON_CLIENT_AUTH_INFO));
if (key->auth_info==NULL) return GNUTLS_E_MEMORY_ERROR;
((ANON_CLIENT_AUTH_INFO*)key->auth_info)->dh_bits = gcry_mpi_get_nbits(key->client_p);
key->auth_info_size = sizeof(ANON_CLIENT_AUTH_INFO);
/* We should check signature in non-anonymous KX
* this is anonymous however
*/
return 0;
}
int proc_anon_client_kx( GNUTLS_KEY key, opaque* data, int data_size) {
uint16 n_Y;
size_t _n_Y;
MPI g, p;
int bits, ret;
const ANON_SERVER_CREDENTIALS cred;
cred = _gnutls_get_cred( key, GNUTLS_ANON, NULL);
if (cred==NULL) {
bits = DEFAULT_BITS; /* default */
} else {
bits = cred->dh_bits;
}
#if 0 /* removed. I do not know why - maybe I didn't get the protocol,
* but openssl does not use that byte
*/
if (data[0] != 1) {
gnutls_assert();
return GNUTLS_E_UNIMPLEMENTED_FEATURE;
}
#endif
n_Y = READuint16( &data[0]);
_n_Y = n_Y;
if (gcry_mpi_scan(&key->client_Y,
GCRYMPI_FMT_USG, &data[2], &_n_Y)) {
gnutls_assert();
return GNUTLS_E_MPI_SCAN_FAILED;
}
g = gnutls_get_dh_params(&p, bits);
key->KEY = gnutls_calc_dh_key( key->client_Y, key->dh_secret, p);
_gnutls_mpi_release(&key->client_Y);
_gnutls_mpi_release(&key->dh_secret);
_gnutls_mpi_release(&p);
_gnutls_mpi_release(&g);
ret = _gnutls_generate_key( key);
_gnutls_mpi_release(&key->KEY);
if (ret < 0) {
return ret;
}
return 0;
}
|