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