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
274
275
276
|
/*
* Copyright (C) 2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* The GNUTLS library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; 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_errors.h>
#include <x509_b64.h>
#include <auth_cert.h>
#include <gnutls_cert.h>
#include <x509_asn1.h>
#include <x509_der.h>
#include <gnutls_datum.h>
#include <gnutls_mpi.h>
#include <gnutls_global.h>
void _gcry_mpi_invm( MPI x, MPI a, MPI n );
/* Converts an RSA PKCS#1 key to
* an internal structure (gnutls_private_key)
*/
int _gnutls_PKCS1key2gnutlsKey(gnutls_private_key * pkey,
gnutls_datum raw_key)
{
int result;
opaque str[MAX_PARAMETER_SIZE];
node_asn *pkey_asn;
pkey->pk_algorithm = GNUTLS_PK_RSA;
if ((result =
asn1_create_structure(_gnutls_get_gnutls_asn(),
"GNUTLS.RSAPrivateKey", &pkey_asn,
"rsakey")) != ASN_OK) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if ((sizeof(pkey->params) / sizeof(GNUTLS_MPI)) < RSA_PRIVATE_PARAMS) {
gnutls_assert();
/* internal error. Increase the GNUTLS_MPIs in params */
return GNUTLS_E_INTERNAL_ERROR;
}
result = asn1_get_der(pkey_asn, raw_key.data, raw_key.size);
if (result != ASN_OK) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if ((result = _gnutls_x509_read_int(pkey_asn, "rsakey.modulus",
str, sizeof(str) - 1,
&pkey->params[0])) < 0) {
gnutls_assert();
asn1_delete_structure(pkey_asn);
return result;
}
if ((result =
_gnutls_x509_read_int(pkey_asn, "rsakey.publicExponent", str,
sizeof(str) - 1,
&pkey->params[1])) < 0) {
gnutls_assert();
asn1_delete_structure(pkey_asn);
_gnutls_mpi_release(&pkey->params[0]);
return result;
}
if ((result =
_gnutls_x509_read_int(pkey_asn, "rsakey.privateExponent", str,
sizeof(str) - 1,
&pkey->params[2])) < 0) {
gnutls_assert();
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
asn1_delete_structure(pkey_asn);
return result;
}
if ((result = _gnutls_x509_read_int(pkey_asn, "rsakey.prime1",
str, sizeof(str) - 1,
&pkey->params[3])) < 0) {
gnutls_assert();
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
asn1_delete_structure(pkey_asn);
return result;
}
if ((result = _gnutls_x509_read_int(pkey_asn, "rsakey.prime2",
str, sizeof(str) - 1,
&pkey->params[4])) < 0) {
gnutls_assert();
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
_gnutls_mpi_release(&pkey->params[3]);
asn1_delete_structure(pkey_asn);
return result;
}
#if 1
/* Calculate the coefficient. This is because the gcrypt
* library is uses the p,q in the reverse order.
*/
pkey->params[5] =
_gnutls_mpi_snew(_gnutls_mpi_get_nbits(pkey->params[0]));
if (pkey->params[5] == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
_gnutls_mpi_invm(pkey->params[5], pkey->params[3], pkey->params[4]);
/* p, q */
#else
if ( (result=_gnutls_x509_read_int( pkey_asn, "rsakey.coefficient",
str, sizeof(str)-1, &pkey->params[5])) < 0) {
gnutls_assert();
_gnutls_mpi_release( &pkey->params[0]);
_gnutls_mpi_release( &pkey->params[1]);
_gnutls_mpi_release( &pkey->params[2]);
_gnutls_mpi_release( &pkey->params[3]);
_gnutls_mpi_release( &pkey->params[4]);
asn1_delete_structure(pkey_asn);
return result;
}
#endif
pkey->params_size = RSA_PRIVATE_PARAMS;
asn1_delete_structure(pkey_asn);
if (gnutls_set_datum(&pkey->raw, raw_key.data, raw_key.size) < 0) {
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
_gnutls_mpi_release(&pkey->params[3]);
_gnutls_mpi_release(&pkey->params[4]);
_gnutls_mpi_release(&pkey->params[5]);
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
return 0;
}
int _gnutls_DSAkey2gnutlsKey(gnutls_private_key * pkey,
gnutls_datum raw_key)
{
int result;
opaque str[MAX_PARAMETER_SIZE];
node_asn *dsa_asn;
pkey->pk_algorithm = GNUTLS_PK_DSA;
if ((result =
asn1_create_structure(_gnutls_get_gnutls_asn(),
"GNUTLS.DSAPrivateKey", &dsa_asn,
"dsakey")) != ASN_OK) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if ((sizeof(pkey->params) / sizeof(GNUTLS_MPI)) < DSA_PRIVATE_PARAMS) {
gnutls_assert();
/* internal error. Increase the GNUTLS_MPIs in params */
return GNUTLS_E_INTERNAL_ERROR;
}
result = asn1_get_der(dsa_asn, raw_key.data, raw_key.size);
if (result != ASN_OK) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if ((result = _gnutls_x509_read_int(dsa_asn, "dsakey.p",
str, sizeof(str) - 1,
&pkey->params[0])) < 0) {
gnutls_assert();
asn1_delete_structure(dsa_asn);
return result;
}
if ((result = _gnutls_x509_read_int(dsa_asn, "dsakey.q",
str, sizeof(str) - 1,
&pkey->params[1])) < 0) {
gnutls_assert();
asn1_delete_structure(dsa_asn);
_gnutls_mpi_release(&pkey->params[0]);
return result;
}
if ((result = _gnutls_x509_read_int(dsa_asn, "dsakey.g",
str, sizeof(str) - 1,
&pkey->params[2])) < 0) {
gnutls_assert();
asn1_delete_structure(dsa_asn);
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
return result;
}
if ((result = _gnutls_x509_read_int(dsa_asn, "dsakey.Y",
str, sizeof(str) - 1,
&pkey->params[3])) < 0) {
gnutls_assert();
asn1_delete_structure(dsa_asn);
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
return result;
}
if ((result = _gnutls_x509_read_int(dsa_asn, "dsakey.priv",
str, sizeof(str) - 1,
&pkey->params[4])) < 0) {
gnutls_assert();
asn1_delete_structure(dsa_asn);
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
_gnutls_mpi_release(&pkey->params[3]);
return result;
}
pkey->params_size = DSA_PRIVATE_PARAMS;
asn1_delete_structure(dsa_asn);
if (gnutls_set_datum(&pkey->raw, raw_key.data, raw_key.size) < 0) {
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
_gnutls_mpi_release(&pkey->params[2]);
_gnutls_mpi_release(&pkey->params[3]);
_gnutls_mpi_release(&pkey->params[4]);
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
return 0;
}
void _gnutls_free_private_key(gnutls_private_key pkey)
{
int i;
for (i = 0; i < pkey.params_size; i++) {
_gnutls_mpi_release(&pkey.params[i]);
}
gnutls_free_datum(&pkey.raw);
return;
}
|