]>
Commit | Line | Data |
---|---|---|
8ff8c33d MW |
1 | /** |
2 | * @file rsa_private_key.c | |
3 | * | |
4 | * @brief Implementation of rsa_private_key_t. | |
5 | * | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (C) 2005 Jan Hutter, Martin Willi | |
10 | * Hochschule fuer Technik Rapperswil | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2 of the License, or (at your | |
15 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
19 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | */ | |
22 | ||
23 | #include <gmp.h> | |
efadbf79 MW |
24 | #include <sys/stat.h> |
25 | #include <unistd.h> | |
5113680f | 26 | #include <string.h> |
8ff8c33d MW |
27 | |
28 | #include "rsa_private_key.h" | |
29 | ||
30 | #include <daemon.h> | |
ec6582cc | 31 | #include <asn1/asn1.h> |
8ff8c33d | 32 | |
4b41a0d4 JH |
33 | /* |
34 | * Oids for hash algorithms are defined in | |
35 | * rsa_public_key.c. | |
8ff8c33d MW |
36 | */ |
37 | extern u_int8_t md2_oid[18]; | |
38 | extern u_int8_t md5_oid[18]; | |
39 | extern u_int8_t sha1_oid[15]; | |
40 | extern u_int8_t sha256_oid[19]; | |
41 | extern u_int8_t sha384_oid[19]; | |
42 | extern u_int8_t sha512_oid[19]; | |
43 | ||
dec59822 | 44 | |
8ff8c33d | 45 | /** |
4b41a0d4 | 46 | * Public exponent to use for key generation. |
8ff8c33d MW |
47 | */ |
48 | #define PUBLIC_EXPONENT 0x10001 | |
49 | ||
50 | ||
51 | typedef struct private_rsa_private_key_t private_rsa_private_key_t; | |
52 | ||
53 | /** | |
4b41a0d4 | 54 | * Private data of a rsa_private_key_t object. |
8ff8c33d MW |
55 | */ |
56 | struct private_rsa_private_key_t { | |
57 | /** | |
58 | * Public interface for this signer. | |
59 | */ | |
60 | rsa_private_key_t public; | |
61 | ||
dec59822 MW |
62 | /** |
63 | * Version of key, as encoded in PKCS#1 | |
64 | */ | |
65 | u_int version; | |
66 | ||
8ff8c33d | 67 | /** |
4b41a0d4 | 68 | * Public modulus. |
8ff8c33d MW |
69 | */ |
70 | mpz_t n; | |
71 | ||
72 | /** | |
4b41a0d4 | 73 | * Public exponent. |
8ff8c33d MW |
74 | */ |
75 | mpz_t e; | |
76 | ||
77 | /** | |
4b41a0d4 | 78 | * Private prime 1. |
8ff8c33d MW |
79 | */ |
80 | mpz_t p; | |
81 | ||
82 | /** | |
4b41a0d4 | 83 | * Private Prime 2. |
8ff8c33d MW |
84 | */ |
85 | mpz_t q; | |
86 | ||
87 | /** | |
4b41a0d4 | 88 | * Private exponent. |
8ff8c33d MW |
89 | */ |
90 | mpz_t d; | |
91 | ||
92 | /** | |
4b41a0d4 | 93 | * Private exponent 1. |
8ff8c33d MW |
94 | */ |
95 | mpz_t exp1; | |
96 | ||
97 | /** | |
4b41a0d4 | 98 | * Private exponent 2. |
8ff8c33d MW |
99 | */ |
100 | mpz_t exp2; | |
101 | ||
102 | /** | |
4b41a0d4 | 103 | * Private coefficient. |
8ff8c33d MW |
104 | */ |
105 | mpz_t coeff; | |
106 | ||
107 | /** | |
4b41a0d4 | 108 | * Keysize in bytes. |
8ff8c33d MW |
109 | */ |
110 | size_t k; | |
111 | ||
112 | /** | |
113 | * @brief Implements the RSADP algorithm specified in PKCS#1. | |
4b41a0d4 JH |
114 | * |
115 | * @param this calling object | |
116 | * @param data data to process | |
117 | * @return processed data | |
8ff8c33d MW |
118 | */ |
119 | chunk_t (*rsadp) (private_rsa_private_key_t *this, chunk_t data); | |
120 | ||
121 | /** | |
122 | * @brief Implements the RSASP1 algorithm specified in PKCS#1. | |
4b41a0d4 JH |
123 | * @param this calling object |
124 | * @param data data to process | |
125 | * @return processed data | |
8ff8c33d MW |
126 | */ |
127 | chunk_t (*rsasp1) (private_rsa_private_key_t *this, chunk_t data); | |
128 | ||
c3903a18 MW |
129 | /** |
130 | * @brief Generate a prime value. | |
131 | * | |
132 | * @param this calling object | |
133 | * @param prime_size size of the prime, in bytes | |
134 | * @param[out] prime uninitialized mpz | |
135 | */ | |
68621281 | 136 | status_t (*compute_prime) (private_rsa_private_key_t *this, size_t prime_size, mpz_t *prime); |
c3903a18 | 137 | |
8ff8c33d MW |
138 | }; |
139 | ||
b5cb0210 | 140 | /* ASN.1 definition of a PKCS#1 RSA private key */ |
ec6582cc | 141 | static const asn1Object_t privkey_objects[] = { |
b5cb0210 MW |
142 | { 0, "RSAPrivateKey", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */ |
143 | { 1, "version", ASN1_INTEGER, ASN1_BODY }, /* 1 */ | |
144 | { 1, "modulus", ASN1_INTEGER, ASN1_BODY }, /* 2 */ | |
145 | { 1, "publicExponent", ASN1_INTEGER, ASN1_BODY }, /* 3 */ | |
146 | { 1, "privateExponent", ASN1_INTEGER, ASN1_BODY }, /* 4 */ | |
147 | { 1, "prime1", ASN1_INTEGER, ASN1_BODY }, /* 5 */ | |
148 | { 1, "prime2", ASN1_INTEGER, ASN1_BODY }, /* 6 */ | |
149 | { 1, "exponent1", ASN1_INTEGER, ASN1_BODY }, /* 7 */ | |
150 | { 1, "exponent2", ASN1_INTEGER, ASN1_BODY }, /* 8 */ | |
151 | { 1, "coefficient", ASN1_INTEGER, ASN1_BODY }, /* 9 */ | |
152 | { 1, "otherPrimeInfos", ASN1_SEQUENCE, ASN1_OPT | | |
153 | ASN1_LOOP }, /* 10 */ | |
154 | { 2, "otherPrimeInfo", ASN1_SEQUENCE, ASN1_NONE }, /* 11 */ | |
155 | { 3, "prime", ASN1_INTEGER, ASN1_BODY }, /* 12 */ | |
156 | { 3, "exponent", ASN1_INTEGER, ASN1_BODY }, /* 13 */ | |
157 | { 3, "coefficient", ASN1_INTEGER, ASN1_BODY }, /* 14 */ | |
158 | { 1, "end opt or loop", ASN1_EOC, ASN1_END } /* 15 */ | |
159 | }; | |
160 | ||
ec6582cc MW |
161 | #define PRIV_KEY_VERSION 1 |
162 | #define PRIV_KEY_MODULUS 2 | |
163 | #define PRIV_KEY_PUB_EXP 3 | |
164 | #define PRIV_KEY_PRIV_EXP 4 | |
165 | #define PRIV_KEY_PRIME1 5 | |
166 | #define PRIV_KEY_PRIME2 6 | |
167 | #define PRIV_KEY_EXP1 7 | |
168 | #define PRIV_KEY_EXP2 8 | |
169 | #define PRIV_KEY_COEFF 9 | |
170 | #define PRIV_KEY_ROOF 16 | |
9c781c15 | 171 | |
f2ee13a7 MW |
172 | static private_rsa_private_key_t *rsa_private_key_create_empty(); |
173 | ||
c3903a18 MW |
174 | /** |
175 | * Implementation of private_rsa_private_key_t.compute_prime. | |
176 | */ | |
68621281 | 177 | static status_t compute_prime(private_rsa_private_key_t *this, size_t prime_size, mpz_t *prime) |
c3903a18 MW |
178 | { |
179 | randomizer_t *randomizer; | |
180 | chunk_t random_bytes; | |
68621281 | 181 | status_t status; |
c3903a18 MW |
182 | |
183 | randomizer = randomizer_create(); | |
184 | mpz_init(*prime); | |
185 | ||
186 | do | |
187 | { | |
68621281 MW |
188 | status = randomizer->allocate_random_bytes(randomizer, prime_size, &random_bytes); |
189 | if (status != SUCCESS) | |
190 | { | |
191 | randomizer->destroy(randomizer); | |
192 | mpz_clear(*prime); | |
193 | return FAILED; | |
194 | } | |
c3903a18 MW |
195 | |
196 | /* make sure most significant bit is set */ | |
197 | random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80; | |
198 | ||
199 | /* convert chunk to mpz value */ | |
200 | mpz_import(*prime, random_bytes.len, 1, 1, 1, 0, random_bytes.ptr); | |
9c781c15 | 201 | |
c3903a18 MW |
202 | /* get next prime */ |
203 | mpz_nextprime (*prime, *prime); | |
9c781c15 | 204 | |
5113680f | 205 | free(random_bytes.ptr); |
c3903a18 MW |
206 | } |
207 | /* check if it isnt too large */ | |
208 | while (((mpz_sizeinbase(*prime, 2) + 7) / 8) > prime_size); | |
209 | ||
210 | randomizer->destroy(randomizer); | |
68621281 | 211 | return SUCCESS; |
c3903a18 MW |
212 | } |
213 | ||
8ff8c33d | 214 | /** |
4b41a0d4 | 215 | * Implementation of private_rsa_private_key_t.rsadp and private_rsa_private_key_t.rsasp1. |
8ff8c33d MW |
216 | */ |
217 | static chunk_t rsadp(private_rsa_private_key_t *this, chunk_t data) | |
218 | { | |
219 | mpz_t t1, t2; | |
220 | chunk_t decrypted; | |
221 | ||
9c781c15 MW |
222 | mpz_init(t1); |
223 | mpz_init(t2); | |
224 | ||
225 | mpz_import(t1, data.len, 1, 1, 1, 0, data.ptr); | |
226 | ||
227 | mpz_powm(t2, t1, this->exp1, this->p); /* m1 = c^dP mod p */ | |
228 | mpz_powm(t1, t1, this->exp2, this->q); /* m2 = c^dQ mod Q */ | |
229 | mpz_sub(t2, t2, t1); /* h = qInv (m1 - m2) mod p */ | |
230 | mpz_mod(t2, t2, this->p); | |
231 | mpz_mul(t2, t2, this->coeff); | |
232 | mpz_mod(t2, t2, this->p); | |
233 | ||
234 | mpz_mul(t2, t2, this->q); /* m = m2 + h q */ | |
235 | mpz_add(t1, t1, t2); | |
236 | ||
237 | decrypted.len = this->k; | |
238 | decrypted.ptr = mpz_export(NULL, NULL, 1, decrypted.len, 1, 0, t1); | |
239 | ||
240 | mpz_clear(t1); | |
241 | mpz_clear(t2); | |
242 | ||
243 | return decrypted; | |
8ff8c33d MW |
244 | } |
245 | ||
246 | /** | |
4b41a0d4 | 247 | * Implementation of rsa_private_key.build_emsa_signature. |
8ff8c33d MW |
248 | */ |
249 | static status_t build_emsa_pkcs1_signature(private_rsa_private_key_t *this, hash_algorithm_t hash_algorithm, chunk_t data, chunk_t *signature) | |
250 | { | |
251 | hasher_t *hasher; | |
252 | chunk_t hash; | |
253 | chunk_t oid; | |
254 | chunk_t em; | |
255 | ||
256 | /* get oid string prepended to hash */ | |
257 | switch (hash_algorithm) | |
258 | { | |
259 | case HASH_MD2: | |
260 | { | |
261 | oid.ptr = md2_oid; | |
262 | oid.len = sizeof(md2_oid); | |
263 | break; | |
264 | } | |
265 | case HASH_MD5: | |
266 | { | |
267 | oid.ptr = md5_oid; | |
268 | oid.len = sizeof(md5_oid); | |
269 | break; | |
270 | } | |
271 | case HASH_SHA1: | |
272 | { | |
273 | oid.ptr = sha1_oid; | |
274 | oid.len = sizeof(sha1_oid); | |
275 | break; | |
276 | } | |
277 | case HASH_SHA256: | |
278 | { | |
279 | oid.ptr = sha256_oid; | |
280 | oid.len = sizeof(sha256_oid); | |
281 | break; | |
282 | } | |
283 | case HASH_SHA384: | |
284 | { | |
285 | oid.ptr = sha384_oid; | |
286 | oid.len = sizeof(sha384_oid); | |
287 | break; | |
288 | } | |
289 | case HASH_SHA512: | |
290 | { | |
291 | oid.ptr = sha512_oid; | |
292 | oid.len = sizeof(sha512_oid); | |
293 | break; | |
294 | } | |
295 | default: | |
296 | { | |
297 | return NOT_SUPPORTED; | |
298 | } | |
299 | } | |
300 | ||
301 | /* get hasher */ | |
302 | hasher = hasher_create(hash_algorithm); | |
303 | if (hasher == NULL) | |
304 | { | |
305 | return NOT_SUPPORTED; | |
306 | } | |
307 | ||
308 | /* build hash */ | |
309 | hasher->allocate_hash(hasher, data, &hash); | |
310 | hasher->destroy(hasher); | |
311 | ||
312 | /* build chunk to rsa-decrypt: | |
313 | * EM = 0x00 || 0x01 || PS || 0x00 || T. | |
314 | * PS = 0xFF padding, with length to fill em | |
315 | * T = oid || hash | |
316 | */ | |
317 | em.len = this->k; | |
5113680f | 318 | em.ptr = malloc(em.len); |
8ff8c33d MW |
319 | |
320 | /* fill em with padding */ | |
321 | memset(em.ptr, 0xFF, em.len); | |
322 | /* set magic bytes */ | |
323 | *(em.ptr) = 0x00; | |
324 | *(em.ptr+1) = 0x01; | |
325 | *(em.ptr + em.len - hash.len - oid.len - 1) = 0x00; | |
326 | /* set hash */ | |
327 | memcpy(em.ptr + em.len - hash.len, hash.ptr, hash.len); | |
328 | /* set oid */ | |
329 | memcpy(em.ptr + em.len - hash.len - oid.len, oid.ptr, oid.len); | |
8ff8c33d MW |
330 | |
331 | /* build signature */ | |
332 | *signature = this->rsasp1(this, em); | |
333 | ||
5113680f MW |
334 | free(hash.ptr); |
335 | free(em.ptr); | |
8ff8c33d MW |
336 | |
337 | return SUCCESS; | |
338 | } | |
339 | ||
8ff8c33d | 340 | /** |
4b41a0d4 | 341 | * Implementation of rsa_private_key.get_key. |
8ff8c33d MW |
342 | */ |
343 | static status_t get_key(private_rsa_private_key_t *this, chunk_t *key) | |
efadbf79 | 344 | { |
8ff8c33d MW |
345 | chunk_t n, e, p, q, d, exp1, exp2, coeff; |
346 | ||
347 | n.len = this->k; | |
348 | n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n); | |
349 | e.len = this->k; | |
350 | e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e); | |
351 | p.len = this->k; | |
352 | p.ptr = mpz_export(NULL, NULL, 1, p.len, 1, 0, this->p); | |
353 | q.len = this->k; | |
354 | q.ptr = mpz_export(NULL, NULL, 1, q.len, 1, 0, this->q); | |
355 | d.len = this->k; | |
356 | d.ptr = mpz_export(NULL, NULL, 1, d.len, 1, 0, this->d); | |
357 | exp1.len = this->k; | |
358 | exp1.ptr = mpz_export(NULL, NULL, 1, exp1.len, 1, 0, this->exp1); | |
359 | exp2.len = this->k; | |
360 | exp2.ptr = mpz_export(NULL, NULL, 1, exp2.len, 1, 0, this->exp2); | |
361 | coeff.len = this->k; | |
362 | coeff.ptr = mpz_export(NULL, NULL, 1, coeff.len, 1, 0, this->coeff); | |
363 | ||
364 | key->len = this->k * 8; | |
5113680f | 365 | key->ptr = malloc(key->len); |
8ff8c33d MW |
366 | memcpy(key->ptr + this->k * 0, n.ptr , n.len); |
367 | memcpy(key->ptr + this->k * 1, e.ptr, e.len); | |
368 | memcpy(key->ptr + this->k * 2, p.ptr, p.len); | |
369 | memcpy(key->ptr + this->k * 3, q.ptr, q.len); | |
370 | memcpy(key->ptr + this->k * 4, d.ptr, d.len); | |
371 | memcpy(key->ptr + this->k * 5, exp1.ptr, exp1.len); | |
372 | memcpy(key->ptr + this->k * 6, exp2.ptr, exp2.len); | |
373 | memcpy(key->ptr + this->k * 7, coeff.ptr, coeff.len); | |
374 | ||
5113680f MW |
375 | free(n.ptr); |
376 | free(e.ptr); | |
377 | free(p.ptr); | |
378 | free(q.ptr); | |
379 | free(d.ptr); | |
380 | free(exp1.ptr); | |
381 | free(exp2.ptr); | |
382 | free(coeff.ptr); | |
8ff8c33d MW |
383 | |
384 | return SUCCESS; | |
385 | } | |
efadbf79 | 386 | |
8ff8c33d | 387 | /** |
efadbf79 | 388 | * Implementation of rsa_private_key.save_key. |
8ff8c33d | 389 | */ |
efadbf79 | 390 | static status_t save_key(private_rsa_private_key_t *this, char *file) |
8ff8c33d MW |
391 | { |
392 | return NOT_SUPPORTED; | |
393 | } | |
394 | ||
395 | /** | |
efadbf79 | 396 | * Implementation of rsa_private_key.get_public_key. |
8ff8c33d | 397 | */ |
efadbf79 | 398 | rsa_public_key_t *get_public_key(private_rsa_private_key_t *this) |
8ff8c33d | 399 | { |
efadbf79 | 400 | return NULL; |
8ff8c33d MW |
401 | } |
402 | ||
403 | /** | |
efadbf79 | 404 | * Implementation of rsa_private_key.belongs_to. |
8ff8c33d | 405 | */ |
68621281 | 406 | static bool belongs_to(private_rsa_private_key_t *this, rsa_public_key_t *public) |
8ff8c33d | 407 | { |
efadbf79 | 408 | if (mpz_cmp(this->n, *public->get_modulus(public)) == 0) |
8ff8c33d | 409 | { |
efadbf79 | 410 | return TRUE; |
8ff8c33d | 411 | } |
efadbf79 MW |
412 | return FALSE; |
413 | } | |
414 | ||
2cb7d3ca MW |
415 | /** |
416 | * Check the loaded key if it is valid and usable | |
417 | * TODO: Log errors | |
418 | */ | |
419 | static status_t check(private_rsa_private_key_t *this) | |
420 | { | |
421 | mpz_t t, u, q1; | |
422 | status_t status = SUCCESS; | |
423 | ||
424 | /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets. | |
425 | * We actually require more (for security). | |
426 | */ | |
427 | if (this->k < 512/8) | |
428 | { | |
429 | return FAILED; | |
430 | } | |
431 | ||
432 | /* we picked a max modulus size to simplify buffer allocation */ | |
433 | if (this->k > 8192/8) | |
434 | { | |
435 | return FAILED; | |
436 | } | |
437 | ||
438 | mpz_init(t); | |
439 | mpz_init(u); | |
440 | mpz_init(q1); | |
441 | ||
442 | /* check that n == p * q */ | |
443 | mpz_mul(u, this->p, this->q); | |
444 | if (mpz_cmp(u, this->n) != 0) | |
445 | { | |
446 | status = FAILED; | |
447 | } | |
448 | ||
449 | /* check that e divides neither p-1 nor q-1 */ | |
450 | mpz_sub_ui(t, this->p, 1); | |
451 | mpz_mod(t, t, this->e); | |
452 | if (mpz_cmp_ui(t, 0) == 0) | |
453 | { | |
454 | status = FAILED; | |
455 | } | |
456 | ||
457 | mpz_sub_ui(t, this->q, 1); | |
458 | mpz_mod(t, t, this->e); | |
459 | if (mpz_cmp_ui(t, 0) == 0) | |
460 | { | |
461 | status = FAILED; | |
462 | } | |
463 | ||
464 | /* check that d is e^-1 (mod lcm(p-1, q-1)) */ | |
465 | /* see PKCS#1v2, aka RFC 2437, for the "lcm" */ | |
466 | mpz_sub_ui(q1, this->q, 1); | |
467 | mpz_sub_ui(u, this->p, 1); | |
468 | mpz_gcd(t, u, q1); /* t := gcd(p-1, q-1) */ | |
469 | mpz_mul(u, u, q1); /* u := (p-1) * (q-1) */ | |
470 | mpz_divexact(u, u, t); /* u := lcm(p-1, q-1) */ | |
471 | ||
472 | mpz_mul(t, this->d, this->e); | |
473 | mpz_mod(t, t, u); | |
474 | if (mpz_cmp_ui(t, 1) != 0) | |
475 | { | |
476 | status = FAILED; | |
477 | } | |
478 | ||
479 | /* check that exp1 is d mod (p-1) */ | |
480 | mpz_sub_ui(u, this->p, 1); | |
481 | mpz_mod(t, this->d, u); | |
482 | if (mpz_cmp(t, this->exp1) != 0) | |
483 | { | |
484 | status = FAILED; | |
485 | } | |
486 | ||
487 | /* check that exp2 is d mod (q-1) */ | |
488 | mpz_sub_ui(u, this->q, 1); | |
489 | mpz_mod(t, this->d, u); | |
490 | if (mpz_cmp(t, this->exp2) != 0) | |
491 | { | |
492 | status = FAILED; | |
493 | } | |
494 | ||
495 | /* check that coeff is (q^-1) mod p */ | |
496 | mpz_mul(t, this->coeff, this->q); | |
497 | mpz_mod(t, t, this->p); | |
498 | if (mpz_cmp_ui(t, 1) != 0) | |
499 | { | |
500 | status = FAILED; | |
501 | } | |
502 | ||
503 | mpz_clear(t); | |
504 | mpz_clear(u); | |
505 | mpz_clear(q1); | |
506 | return status; | |
507 | } | |
508 | ||
f2ee13a7 MW |
509 | /** |
510 | * Implementation of rsa_private_key.clone. | |
511 | */ | |
512 | static rsa_private_key_t* _clone(private_rsa_private_key_t *this) | |
513 | { | |
514 | private_rsa_private_key_t *clone = rsa_private_key_create_empty(); | |
515 | ||
516 | mpz_init_set(clone->n, this->n); | |
517 | mpz_init_set(clone->e, this->e); | |
518 | mpz_init_set(clone->p, this->p); | |
519 | mpz_init_set(clone->q, this->q); | |
520 | mpz_init_set(clone->d, this->d); | |
521 | mpz_init_set(clone->exp1, this->exp1); | |
522 | mpz_init_set(clone->exp2, this->exp2); | |
523 | mpz_init_set(clone->coeff, this->coeff); | |
524 | clone->k = this->k; | |
525 | ||
526 | return &clone->public; | |
527 | } | |
efadbf79 MW |
528 | |
529 | /** | |
530 | * Implementation of rsa_private_key.destroy. | |
531 | */ | |
532 | static void destroy(private_rsa_private_key_t *this) | |
533 | { | |
9c781c15 MW |
534 | mpz_clear(this->n); |
535 | mpz_clear(this->e); | |
536 | mpz_clear(this->p); | |
537 | mpz_clear(this->q); | |
538 | mpz_clear(this->d); | |
539 | mpz_clear(this->exp1); | |
540 | mpz_clear(this->exp2); | |
541 | mpz_clear(this->coeff); | |
5113680f | 542 | free(this); |
efadbf79 MW |
543 | } |
544 | ||
545 | /** | |
546 | * Internal generic constructor | |
547 | */ | |
548 | static private_rsa_private_key_t *rsa_private_key_create_empty() | |
549 | { | |
5113680f | 550 | private_rsa_private_key_t *this = malloc_thing(private_rsa_private_key_t); |
efadbf79 MW |
551 | |
552 | /* public functions */ | |
553 | this->public.build_emsa_pkcs1_signature = (status_t (*) (rsa_private_key_t*,hash_algorithm_t,chunk_t,chunk_t*))build_emsa_pkcs1_signature; | |
554 | this->public.get_key = (status_t (*) (rsa_private_key_t*,chunk_t*))get_key; | |
555 | this->public.save_key = (status_t (*) (rsa_private_key_t*,char*))save_key; | |
556 | this->public.get_public_key = (rsa_public_key_t *(*) (rsa_private_key_t*))get_public_key; | |
557 | this->public.belongs_to = (bool (*) (rsa_private_key_t*,rsa_public_key_t*))belongs_to; | |
f2ee13a7 | 558 | this->public.clone = (rsa_private_key_t*(*)(rsa_private_key_t*))_clone; |
efadbf79 MW |
559 | this->public.destroy = (void (*) (rsa_private_key_t*))destroy; |
560 | ||
561 | /* private functions */ | |
562 | this->rsadp = rsadp; | |
563 | this->rsasp1 = rsadp; /* same algorithm */ | |
564 | this->compute_prime = compute_prime; | |
565 | ||
566 | return this; | |
567 | } | |
568 | ||
569 | /* | |
570 | * See header | |
571 | */ | |
572 | rsa_private_key_t *rsa_private_key_create(size_t key_size) | |
573 | { | |
574 | mpz_t p, q, n, e, d, exp1, exp2, coeff; | |
575 | mpz_t m, q1, t; | |
576 | private_rsa_private_key_t *this; | |
577 | ||
578 | this = rsa_private_key_create_empty(); | |
8ff8c33d MW |
579 | key_size = key_size / 8; |
580 | ||
68621281 MW |
581 | /* Get values of primes p and q */ |
582 | if (this->compute_prime(this, key_size/2, &p) != SUCCESS) | |
583 | { | |
5113680f | 584 | free(this); |
68621281 MW |
585 | return NULL; |
586 | } | |
587 | if (this->compute_prime(this, key_size/2, &q) != SUCCESS) | |
588 | { | |
589 | mpz_clear(p); | |
5113680f | 590 | free(this); |
68621281 MW |
591 | return NULL; |
592 | } | |
593 | ||
8ff8c33d MW |
594 | mpz_init(t); |
595 | mpz_init(n); | |
596 | mpz_init(d); | |
597 | mpz_init(exp1); | |
598 | mpz_init(exp2); | |
599 | mpz_init(coeff); | |
600 | ||
8ff8c33d | 601 | /* Swapping Primes so p is larger then q */ |
9c781c15 | 602 | if (mpz_cmp(p, q) < 0) |
8ff8c33d MW |
603 | { |
604 | mpz_set(t, p); | |
605 | mpz_set(p, q); | |
606 | mpz_set(q, t); | |
607 | } | |
608 | ||
609 | mpz_mul(n, p, q); /* n = p*q */ | |
610 | mpz_init_set_ui(e, PUBLIC_EXPONENT); /* assign public exponent */ | |
611 | mpz_init_set(m, p); /* m = p */ | |
612 | mpz_sub_ui(m, m, 1); /* m = m -1 */ | |
613 | mpz_init_set(q1, q); /* q1 = q */ | |
614 | mpz_sub_ui(q1, q1, 1); /* q1 = q1 -1 */ | |
615 | mpz_gcd(t, m, q1); /* t = gcd(p-1, q-1) */ | |
616 | mpz_mul(m, m, q1); /* m = (p-1)*(q-1) */ | |
617 | mpz_divexact(m, m, t); /* m = m / t */ | |
618 | mpz_gcd(t, m, e); /* t = gcd(m, e) (greatest common divisor) */ | |
619 | ||
620 | mpz_invert(d, e, m); /* e has an inverse mod m */ | |
621 | if (mpz_cmp_ui(d, 0) < 0) /* make sure d is positive */ | |
622 | { | |
623 | mpz_add(d, d, m); | |
624 | } | |
625 | mpz_sub_ui(t, p, 1); /* t = p-1 */ | |
626 | mpz_mod(exp1, d, t); /* exp1 = d mod p-1 */ | |
627 | mpz_sub_ui(t, q, 1); /* t = q-1 */ | |
628 | mpz_mod(exp2, d, t); /* exp2 = d mod q-1 */ | |
629 | ||
630 | mpz_invert(coeff, q, p); /* coeff = q^-1 mod p */ | |
631 | if (mpz_cmp_ui(coeff, 0) < 0) /* make coeff d is positive */ | |
632 | { | |
633 | mpz_add(coeff, coeff, p); | |
634 | } | |
635 | ||
636 | mpz_clear(q1); | |
637 | mpz_clear(m); | |
638 | mpz_clear(t); | |
639 | ||
640 | /* apply values */ | |
9c781c15 | 641 | *(this->p) = *p; |
8ff8c33d MW |
642 | *(this->q) = *q; |
643 | *(this->n) = *n; | |
644 | *(this->e) = *e; | |
645 | *(this->d) = *d; | |
646 | *(this->exp1) = *exp1; | |
647 | *(this->exp2) = *exp2; | |
648 | *(this->coeff) = *coeff; | |
649 | ||
650 | /* set key size in bytes */ | |
8ff8c33d MW |
651 | this->k = key_size; |
652 | ||
efadbf79 | 653 | return &this->public; |
8ff8c33d MW |
654 | } |
655 | ||
b5cb0210 | 656 | /* |
2cb7d3ca | 657 | * see header |
b5cb0210 MW |
658 | */ |
659 | rsa_private_key_t *rsa_private_key_create_from_chunk(chunk_t blob) | |
660 | { | |
661 | asn1_ctx_t ctx; | |
662 | chunk_t object; | |
663 | u_int level; | |
664 | int objectID = 0; | |
665 | private_rsa_private_key_t *this; | |
666 | ||
667 | this = rsa_private_key_create_empty(); | |
668 | ||
669 | mpz_init(this->n); | |
670 | mpz_init(this->e); | |
671 | mpz_init(this->p); | |
672 | mpz_init(this->q); | |
673 | mpz_init(this->d); | |
674 | mpz_init(this->exp1); | |
675 | mpz_init(this->exp2); | |
676 | mpz_init(this->coeff); | |
677 | ||
678 | asn1_init(&ctx, blob, 0, FALSE); | |
679 | ||
ec6582cc | 680 | while (objectID < PRIV_KEY_ROOF) |
b5cb0210 | 681 | { |
ec6582cc | 682 | if (!extract_object(privkey_objects, &objectID, &object, &level, &ctx)) |
b5cb0210 | 683 | { |
2cb7d3ca | 684 | destroy(this); |
b5cb0210 MW |
685 | return FALSE; |
686 | } | |
ec6582cc | 687 | switch (objectID) |
b5cb0210 | 688 | { |
ec6582cc MW |
689 | case PRIV_KEY_VERSION: |
690 | if (object.len > 0 && *object.ptr != 0) | |
691 | { | |
692 | destroy(this); | |
693 | return NULL; | |
694 | } | |
695 | break; | |
696 | case PRIV_KEY_MODULUS: | |
697 | mpz_import(this->n, object.len, 1, 1, 1, 0, object.ptr); | |
698 | break; | |
699 | case PRIV_KEY_PUB_EXP: | |
700 | mpz_import(this->e, object.len, 1, 1, 1, 0, object.ptr); | |
701 | break; | |
702 | case PRIV_KEY_PRIV_EXP: | |
703 | mpz_import(this->d, object.len, 1, 1, 1, 0, object.ptr); | |
704 | break; | |
705 | case PRIV_KEY_PRIME1: | |
706 | mpz_import(this->p, object.len, 1, 1, 1, 0, object.ptr); | |
707 | break; | |
708 | case PRIV_KEY_PRIME2: | |
709 | mpz_import(this->q, object.len, 1, 1, 1, 0, object.ptr); | |
710 | break; | |
711 | case PRIV_KEY_EXP1: | |
712 | mpz_import(this->exp1, object.len, 1, 1, 1, 0, object.ptr); | |
713 | break; | |
714 | case PRIV_KEY_EXP2: | |
715 | mpz_import(this->exp2, object.len, 1, 1, 1, 0, object.ptr); | |
716 | break; | |
717 | case PRIV_KEY_COEFF: | |
718 | mpz_import(this->coeff, object.len, 1, 1, 1, 0, object.ptr); | |
719 | break; | |
b5cb0210 MW |
720 | } |
721 | objectID++; | |
722 | } | |
2cb7d3ca MW |
723 | |
724 | this->k = (mpz_sizeinbase(this->n, 2) + 7) / 8; | |
725 | ||
b5cb0210 MW |
726 | if (check(this) != SUCCESS) |
727 | { | |
728 | destroy(this); | |
729 | return NULL; | |
730 | } | |
731 | else | |
732 | { | |
733 | return &this->public; | |
734 | } | |
735 | } | |
8ff8c33d MW |
736 | |
737 | /* | |
efadbf79 | 738 | * see header |
2cb7d3ca | 739 | * TODO: PEM files |
8ff8c33d | 740 | */ |
efadbf79 | 741 | rsa_private_key_t *rsa_private_key_create_from_file(char *filename, char *passphrase) |
8ff8c33d | 742 | { |
efadbf79 MW |
743 | chunk_t chunk; |
744 | struct stat stb; | |
745 | FILE *file; | |
746 | char *buffer; | |
8ff8c33d | 747 | |
efadbf79 MW |
748 | if (stat(filename, &stb) == -1) |
749 | { | |
750 | return NULL; | |
751 | } | |
8ff8c33d | 752 | |
efadbf79 | 753 | buffer = alloca(stb.st_size); |
8ff8c33d | 754 | |
efadbf79 MW |
755 | file = fopen(filename, "r"); |
756 | if (file == NULL) | |
757 | { | |
758 | return NULL; | |
759 | } | |
760 | ||
761 | if (fread(buffer, stb.st_size, 1, file) != 1) | |
762 | { | |
5113680f | 763 | fclose(file); |
efadbf79 MW |
764 | return NULL; |
765 | } | |
5113680f | 766 | fclose(file); |
efadbf79 MW |
767 | |
768 | chunk.ptr = buffer; | |
769 | chunk.len = stb.st_size; | |
8ff8c33d | 770 | |
efadbf79 | 771 | return rsa_private_key_create_from_chunk(chunk); |
8ff8c33d | 772 | } |