cf55e1fb2e87a4c43e42b3fde719fc6b6750ae34
[strongswan.git] / src / libstrongswan / plugins / gmp / gmp_rsa_public_key.c
1 /*
2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <gmp.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "gmp_rsa_public_key.h"
24
25 #include <debug.h>
26 #include <asn1/oid.h>
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
29 #include <asn1/pem.h>
30 #include <crypto/hashers/hasher.h>
31 #include <pgp/pgp.h>
32
33 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t;
34
35 /**
36 * Private data structure with signing context.
37 */
38 struct private_gmp_rsa_public_key_t {
39 /**
40 * Public interface for this signer.
41 */
42 gmp_rsa_public_key_t public;
43
44 /**
45 * Public modulus.
46 */
47 mpz_t n;
48
49 /**
50 * Public exponent.
51 */
52 mpz_t e;
53
54 /**
55 * Keysize in bytes.
56 */
57 size_t k;
58
59 /**
60 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
61 */
62 identification_t *keyid_info;
63
64 /**
65 * Keyid formed as a SHA-1 hash of a publicKey object
66 */
67 identification_t *keyid;
68
69 /**
70 * reference counter
71 */
72 refcount_t ref;
73 };
74
75 /**
76 * Shared functions defined in gmp_rsa_private_key.c
77 */
78 extern chunk_t gmp_mpz_to_chunk(const mpz_t value);
79 extern chunk_t gmp_mpz_to_asn1(const mpz_t value);
80
81 /**
82 * RSAEP algorithm specified in PKCS#1.
83 */
84 static chunk_t rsaep(private_gmp_rsa_public_key_t *this, chunk_t data)
85 {
86 mpz_t m, c;
87 chunk_t encrypted;
88
89 mpz_init(c);
90 mpz_init(m);
91
92 mpz_import(m, data.len, 1, 1, 1, 0, data.ptr);
93
94 mpz_powm(c, m, this->e, this->n);
95
96 encrypted.len = this->k;
97 encrypted.ptr = mpz_export(NULL, NULL, 1, encrypted.len, 1, 0, c);
98 if (encrypted.ptr == NULL)
99 {
100 encrypted.len = 0;
101 }
102
103 mpz_clear(c);
104 mpz_clear(m);
105
106 return encrypted;
107 }
108
109 /**
110 * RSAVP1 algorithm specified in PKCS#1.
111 */
112 static chunk_t rsavp1(private_gmp_rsa_public_key_t *this, chunk_t data)
113 {
114 return rsaep(this, data);
115 }
116
117 /**
118 * ASN.1 definition of digestInfo
119 */
120 static const asn1Object_t digestInfoObjects[] = {
121 { 0, "digestInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
122 { 1, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 1 */
123 { 1, "digest", ASN1_OCTET_STRING, ASN1_BODY }, /* 2 */
124 { 0, "exit", ASN1_EOC, ASN1_EXIT }
125 };
126 #define DIGEST_INFO 0
127 #define DIGEST_INFO_ALGORITHM 1
128 #define DIGEST_INFO_DIGEST 2
129
130 /**
131 * Verification of an EMPSA PKCS1 signature described in PKCS#1
132 */
133 static bool verify_emsa_pkcs1_signature(private_gmp_rsa_public_key_t *this,
134 hash_algorithm_t algorithm,
135 chunk_t data, chunk_t signature)
136 {
137 chunk_t em_ori, em;
138 bool success = FALSE;
139
140 /* remove any preceding 0-bytes from signature */
141 while (signature.len && *(signature.ptr) == 0x00)
142 {
143 signature = chunk_skip(signature, 1);
144 }
145
146 if (signature.len == 0 || signature.len > this->k)
147 {
148 return INVALID_ARG;
149 }
150
151 /* unpack signature */
152 em_ori = em = rsavp1(this, signature);
153
154 /* result should look like this:
155 * EM = 0x00 || 0x01 || PS || 0x00 || T.
156 * PS = 0xFF padding, with length to fill em
157 * T = oid || hash
158 */
159
160 /* check magic bytes */
161 if (*(em.ptr) != 0x00 || *(em.ptr+1) != 0x01)
162 {
163 goto end;
164 }
165 em = chunk_skip(em, 2);
166
167 /* find magic 0x00 */
168 while (em.len > 0)
169 {
170 if (*em.ptr == 0x00)
171 {
172 /* found magic byte, stop */
173 em = chunk_skip(em, 1);
174 break;
175 }
176 else if (*em.ptr != 0xFF)
177 {
178 /* bad padding, decryption failed ?!*/
179 goto end;
180 }
181 em = chunk_skip(em, 1);
182 }
183
184 if (em.len == 0)
185 {
186 /* no digestInfo found */
187 goto end;
188 }
189
190 if (algorithm == HASH_UNKNOWN)
191 { /* IKEv1 signatures without digestInfo */
192 if (em.len != data.len)
193 {
194 DBG1("hash size in signature is %u bytes instead of %u bytes",
195 em.len, data.len);
196 goto end;
197 }
198 success = memeq(em.ptr, data.ptr, data.len);
199 }
200 else
201 { /* IKEv2 and X.509 certificate signatures */
202 asn1_parser_t *parser;
203 chunk_t object;
204 int objectID;
205 hash_algorithm_t hash_algorithm = HASH_UNKNOWN;
206
207 DBG2("signature verification:");
208 parser = asn1_parser_create(digestInfoObjects, em);
209
210 while (parser->iterate(parser, &objectID, &object))
211 {
212 switch (objectID)
213 {
214 case DIGEST_INFO:
215 {
216 if (em.len > object.len)
217 {
218 DBG1("digestInfo field in signature is followed by %u surplus bytes",
219 em.len - object.len);
220 goto end_parser;
221 }
222 break;
223 }
224 case DIGEST_INFO_ALGORITHM:
225 {
226 int hash_oid = asn1_parse_algorithmIdentifier(object,
227 parser->get_level(parser)+1, NULL);
228
229 hash_algorithm = hasher_algorithm_from_oid(hash_oid);
230 if (hash_algorithm == HASH_UNKNOWN || hash_algorithm != algorithm)
231 {
232 DBG1("expected hash algorithm %N, but found %N (OID: %#B)",
233 hash_algorithm_names, algorithm,
234 hash_algorithm_names, hash_algorithm, &object);
235 goto end_parser;
236 }
237 break;
238 }
239 case DIGEST_INFO_DIGEST:
240 {
241 chunk_t hash;
242 hasher_t *hasher;
243
244 hasher = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
245 if (hasher == NULL)
246 {
247 DBG1("hash algorithm %N not supported",
248 hash_algorithm_names, hash_algorithm);
249 goto end_parser;
250 }
251
252 if (object.len != hasher->get_hash_size(hasher))
253 {
254 DBG1("hash size in signature is %u bytes instead of %u "
255 "bytes", object.len, hasher->get_hash_size(hasher));
256 hasher->destroy(hasher);
257 goto end_parser;
258 }
259
260 /* build our own hash and compare */
261 hasher->allocate_hash(hasher, data, &hash);
262 hasher->destroy(hasher);
263 success = memeq(object.ptr, hash.ptr, hash.len);
264 free(hash.ptr);
265 break;
266 }
267 default:
268 break;
269 }
270 }
271
272 end_parser:
273 success &= parser->success(parser);
274 parser->destroy(parser);
275 }
276
277 end:
278 free(em_ori.ptr);
279 return success;
280 }
281
282 /**
283 * Implementation of public_key_t.get_type.
284 */
285 static key_type_t get_type(private_gmp_rsa_public_key_t *this)
286 {
287 return KEY_RSA;
288 }
289
290 /**
291 * Implementation of public_key_t.verify.
292 */
293 static bool verify(private_gmp_rsa_public_key_t *this, signature_scheme_t scheme,
294 chunk_t data, chunk_t signature)
295 {
296 switch (scheme)
297 {
298 case SIGN_RSA_EMSA_PKCS1_NULL:
299 return verify_emsa_pkcs1_signature(this, HASH_UNKNOWN, data, signature);
300 case SIGN_RSA_EMSA_PKCS1_MD5:
301 return verify_emsa_pkcs1_signature(this, HASH_MD5, data, signature);
302 case SIGN_RSA_EMSA_PKCS1_SHA1:
303 return verify_emsa_pkcs1_signature(this, HASH_SHA1, data, signature);
304 case SIGN_RSA_EMSA_PKCS1_SHA256:
305 return verify_emsa_pkcs1_signature(this, HASH_SHA256, data, signature);
306 case SIGN_RSA_EMSA_PKCS1_SHA384:
307 return verify_emsa_pkcs1_signature(this, HASH_SHA384, data, signature);
308 case SIGN_RSA_EMSA_PKCS1_SHA512:
309 return verify_emsa_pkcs1_signature(this, HASH_SHA512, data, signature);
310 default:
311 DBG1("signature scheme %N not supported in RSA",
312 signature_scheme_names, scheme);
313 return FALSE;
314 }
315 }
316
317 #define MIN_PS_PADDING 8
318
319 /**
320 * Implementation of public_key_t.encrypt.
321 */
322 static bool encrypt_(private_gmp_rsa_public_key_t *this, chunk_t plain,
323 chunk_t *crypto)
324 {
325 chunk_t em;
326 u_char *pos;
327 int padding, i;
328 rng_t *rng;
329
330 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
331 if (rng == NULL)
332 {
333 DBG1("no random generator available");
334 return FALSE;
335 }
336
337 /* number of pseudo-random padding octets */
338 padding = this->k - plain.len - 3;
339 if (padding < MIN_PS_PADDING)
340 {
341 DBG1("pseudo-random padding must be at least %d octets", MIN_PS_PADDING);
342 return FALSE;
343 }
344
345 /* padding according to PKCS#1 7.2.1 (RSAES-PKCS1-v1.5-ENCRYPT) */
346 DBG2("padding %u bytes of data to the rsa modulus size of %u bytes",
347 plain.len, this->k);
348 em.len = this->k;
349 em.ptr = malloc(em.len);
350 pos = em.ptr;
351 *pos++ = 0x00;
352 *pos++ = 0x02;
353
354 /* fill with pseudo random octets */
355 rng->get_bytes(rng, padding, pos);
356
357 /* replace zero-valued random octets */
358 for (i = 0; i < padding; i++)
359 {
360 while (*pos == 0)
361 {
362 rng->get_bytes(rng, 1, pos);
363 }
364 pos++;
365 }
366 rng->destroy(rng);
367
368 /* append the padding terminator */
369 *pos++ = 0x00;
370
371 /* now add the data */
372 memcpy(pos, plain.ptr, plain.len);
373 DBG3("padded data before rsa encryption: %B", &em);
374
375 /* rsa encryption using PKCS#1 RSAEP */
376 *crypto = rsaep(this, em);
377 DBG3("rsa encrypted data: %B", crypto);
378 chunk_clear(&em);
379 return TRUE;
380 }
381
382 /**
383 * Implementation of gmp_rsa_public_key.equals.
384 */
385 static bool equals(private_gmp_rsa_public_key_t *this, public_key_t *other)
386 {
387 identification_t *keyid;
388
389 if (&this->public.interface == other)
390 {
391 return TRUE;
392 }
393 if (other->get_type(other) != KEY_RSA)
394 {
395 return FALSE;
396 }
397 keyid = other->get_id(other, ID_PUBKEY_SHA1);
398 if (keyid && keyid->equals(keyid, this->keyid))
399 {
400 return TRUE;
401 }
402 keyid = other->get_id(other, ID_PUBKEY_INFO_SHA1);
403 if (keyid && keyid->equals(keyid, this->keyid_info))
404 {
405 return TRUE;
406 }
407 return FALSE;
408 }
409
410 /**
411 * Implementation of public_key_t.get_keysize.
412 */
413 static size_t get_keysize(private_gmp_rsa_public_key_t *this)
414 {
415 return this->k;
416 }
417
418 /**
419 * Build the PGP version 3 RSA key identifier from n and e using
420 * MD5 hashed modulus and exponent.
421 */
422 static identification_t* gmp_rsa_build_pgp_v3_keyid(mpz_t n, mpz_t e)
423 {
424 identification_t *keyid;
425 chunk_t modulus, mod, exponent, exp, hash;
426 hasher_t *hasher;
427
428 hasher= lib->crypto->create_hasher(lib->crypto, HASH_MD5);
429 if (hasher == NULL)
430 {
431 DBG1("computation of PGP V3 keyid failed, no MD5 hasher is available");
432 return NULL;
433 }
434 mod = modulus = gmp_mpz_to_chunk(n);
435 exp = exponent = gmp_mpz_to_chunk(e);
436
437 /* remove leading zero bytes before hashing modulus and exponent */
438 while (mod.len > 0 && *mod.ptr == 0x00)
439 {
440 mod.ptr++;
441 mod.len--;
442 }
443 while (exp.len > 0 && *exp.ptr == 0x00)
444 {
445 exp.ptr++;
446 exp.len--;
447 }
448 hasher->allocate_hash(hasher, mod, NULL);
449 hasher->allocate_hash(hasher, exp, &hash);
450 hasher->destroy(hasher);
451 keyid = identification_create_from_encoding(ID_KEY_ID, hash);
452 free(hash.ptr);
453 free(modulus.ptr);
454 free(exponent.ptr);
455 return keyid;
456 }
457
458 /**
459 * Implementation of public_key_t.get_id.
460 */
461 static identification_t *get_id(private_gmp_rsa_public_key_t *this,
462 id_type_t type)
463 {
464 switch (type)
465 {
466 case ID_PUBKEY_INFO_SHA1:
467 return this->keyid_info;
468 case ID_PUBKEY_SHA1:
469 return this->keyid;
470 case ID_KEY_ID:
471 return gmp_rsa_build_pgp_v3_keyid(this->n, this->e);
472 default:
473 return NULL;
474 }
475 }
476
477 /*
478 * Implementation of public_key_t.get_encoding.
479 */
480 static chunk_t get_encoding(private_gmp_rsa_public_key_t *this)
481 {
482 return asn1_wrap(ASN1_SEQUENCE, "mm",
483 gmp_mpz_to_asn1(this->n),
484 gmp_mpz_to_asn1(this->e));
485 }
486
487 /**
488 * Implementation of public_key_t.get_ref.
489 */
490 static private_gmp_rsa_public_key_t* get_ref(private_gmp_rsa_public_key_t *this)
491 {
492 ref_get(&this->ref);
493 return this;
494 }
495
496 /**
497 * Implementation of gmp_rsa_public_key.destroy.
498 */
499 static void destroy(private_gmp_rsa_public_key_t *this)
500 {
501 if (ref_put(&this->ref))
502 {
503 mpz_clear(this->n);
504 mpz_clear(this->e);
505 DESTROY_IF(this->keyid);
506 DESTROY_IF(this->keyid_info);
507 free(this);
508 }
509 }
510
511 /**
512 * Generic private constructor
513 */
514 static private_gmp_rsa_public_key_t *gmp_rsa_public_key_create_empty()
515 {
516 private_gmp_rsa_public_key_t *this = malloc_thing(private_gmp_rsa_public_key_t);
517
518 this->public.interface.get_type = (key_type_t (*) (public_key_t*))get_type;
519 this->public.interface.verify = (bool (*) (public_key_t*, signature_scheme_t, chunk_t, chunk_t))verify;
520 this->public.interface.encrypt = (bool (*) (public_key_t*, chunk_t, chunk_t*))encrypt_;
521 this->public.interface.equals = (bool (*) (public_key_t*, public_key_t*))equals;
522 this->public.interface.get_keysize = (size_t (*) (public_key_t*))get_keysize;
523 this->public.interface.get_id = (identification_t* (*) (public_key_t*, id_type_t))get_id;
524 this->public.interface.get_encoding = (chunk_t(*) (public_key_t*))get_encoding;
525 this->public.interface.get_ref = (public_key_t* (*) (public_key_t *this))get_ref;
526 this->public.interface.destroy = (void (*) (public_key_t *this))destroy;
527
528 this->keyid = NULL;
529 this->keyid_info = NULL;
530 this->ref = 1;
531
532 return this;
533 }
534
535 /**
536 * Build the RSA key identifier from n and e using SHA1 hashed publicKey(Info).
537 * Also used in rsa_private_key.c.
538 */
539 bool gmp_rsa_public_key_build_id(mpz_t n, mpz_t e, identification_t **keyid,
540 identification_t **keyid_info)
541 {
542 chunk_t publicKeyInfo, publicKey, hash;
543 hasher_t *hasher;
544
545 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
546 if (hasher == NULL)
547 {
548 DBG1("SHA1 hash algorithm not supported, unable to use RSA");
549 return FALSE;
550 }
551 publicKey = asn1_wrap(ASN1_SEQUENCE, "mm",
552 gmp_mpz_to_asn1(n),
553 gmp_mpz_to_asn1(e));
554 hasher->allocate_hash(hasher, publicKey, &hash);
555 *keyid = identification_create_from_encoding(ID_PUBKEY_SHA1, hash);
556 chunk_free(&hash);
557
558 publicKeyInfo = asn1_wrap(ASN1_SEQUENCE, "cm",
559 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
560 asn1_bitstring("m", publicKey));
561 hasher->allocate_hash(hasher, publicKeyInfo, &hash);
562 *keyid_info = identification_create_from_encoding(ID_PUBKEY_INFO_SHA1, hash);
563 chunk_free(&hash);
564
565 hasher->destroy(hasher);
566 chunk_free(&publicKeyInfo);
567
568 return TRUE;
569 }
570
571 /**
572 * Create a public key from mpz values, used in gmp_rsa_private_key
573 */
574 gmp_rsa_public_key_t *gmp_rsa_public_key_create_from_n_e(mpz_t n, mpz_t e)
575 {
576 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
577
578 mpz_init_set(this->n, n);
579 mpz_init_set(this->e, e);
580
581 this->k = (mpz_sizeinbase(this->n, 2) + 7) / BITS_PER_BYTE;
582 if (!gmp_rsa_public_key_build_id(this->n, this->e,
583 &this->keyid, &this->keyid_info))
584 {
585 destroy(this);
586 return NULL;
587 }
588 return &this->public;
589 }
590
591 /**
592 * ASN.1 definition of RSApublicKey
593 */
594 static const asn1Object_t pubkeyObjects[] = {
595 { 0, "RSAPublicKey", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
596 { 1, "modulus", ASN1_INTEGER, ASN1_BODY }, /* 1 */
597 { 1, "publicExponent", ASN1_INTEGER, ASN1_BODY }, /* 2 */
598 { 0, "exit", ASN1_EOC, ASN1_EXIT }
599 };
600 #define PUB_KEY_RSA_PUBLIC_KEY 0
601 #define PUB_KEY_MODULUS 1
602 #define PUB_KEY_EXPONENT 2
603
604 /**
605 * Load a public key from an ASN.1 encoded blob
606 */
607 static gmp_rsa_public_key_t *load_asn1_der(chunk_t blob)
608 {
609 asn1_parser_t *parser;
610 chunk_t object;
611 int objectID;
612 bool success = FALSE;
613
614 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
615
616 mpz_init(this->n);
617 mpz_init(this->e);
618
619 parser = asn1_parser_create(pubkeyObjects, blob);
620
621 while (parser->iterate(parser, &objectID, &object))
622 {
623 switch (objectID)
624 {
625 case PUB_KEY_MODULUS:
626 mpz_import(this->n, object.len, 1, 1, 1, 0, object.ptr);
627 break;
628 case PUB_KEY_EXPONENT:
629 mpz_import(this->e, object.len, 1, 1, 1, 0, object.ptr);
630 break;
631 }
632 }
633 success = parser->success(parser);
634 free(blob.ptr);
635 parser->destroy(parser);
636
637 if (!success)
638 {
639 destroy(this);
640 return NULL;
641 }
642
643 this->k = (mpz_sizeinbase(this->n, 2) + 7) / BITS_PER_BYTE;
644
645 if (!gmp_rsa_public_key_build_id(this->n, this->e,
646 &this->keyid, &this->keyid_info))
647 {
648 destroy(this);
649 return NULL;
650 }
651 return &this->public;
652 }
653
654 /**
655 * Load a public key from an OpenPGP blob
656 */
657 static gmp_rsa_public_key_t* load_pgp(chunk_t blob)
658 {
659 int objectID;
660 chunk_t packet = blob;
661 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
662
663 mpz_init(this->n);
664 mpz_init(this->e);
665
666 for (objectID = PUB_KEY_MODULUS; objectID <= PUB_KEY_EXPONENT; objectID++)
667 {
668 chunk_t object;
669
670 DBG2("L3 - %s:", pubkeyObjects[objectID].name);
671 object.len = pgp_length(&packet, 2);
672
673 if (object.len == PGP_INVALID_LENGTH)
674 {
675 DBG1("OpenPGP length is invalid");
676 goto end;
677 }
678 object.len = (object.len + 7) / BITS_PER_BYTE;
679 if (object.len > packet.len)
680 {
681 DBG1("OpenPGP field is too short");
682 goto end;
683 }
684 object.ptr = packet.ptr;
685 packet.ptr += object.len;
686 packet.len -= object.len;
687 DBG4("%B", &object);
688
689 switch (objectID)
690 {
691 case PUB_KEY_MODULUS:
692 mpz_import(this->n, object.len, 1, 1, 1, 0, object.ptr);
693 break;
694 case PUB_KEY_EXPONENT:
695 mpz_import(this->e, object.len, 1, 1, 1, 0, object.ptr);
696 break;
697 }
698 }
699
700 this->k = (mpz_sizeinbase(this->n, 2) + 7) / BITS_PER_BYTE;
701 free(blob.ptr);
702
703 if (!gmp_rsa_public_key_build_id(this->n, this->e,
704 &this->keyid, &this->keyid_info))
705 {
706 destroy(this);
707 return NULL;
708 }
709 return &this->public;
710
711 end:
712 free(blob.ptr);
713 destroy(this);
714 return NULL;
715 }
716
717 /**
718 * Load a public key from an RFC 3110 encoded blob
719 */
720 static gmp_rsa_public_key_t *load_rfc_3110(chunk_t blob)
721 {
722 chunk_t exponent, modulus;
723 u_char *pos = blob.ptr;
724 size_t len = blob.len;
725 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
726
727 mpz_init(this->n);
728 mpz_init(this->e);
729
730 if (blob.len < 3)
731 {
732 DBG1("RFC 3110 public key blob too short for exponent length");
733 goto end;
734 }
735 if (pos[0] != 0x00)
736 {
737 exponent = chunk_create(pos + 1, pos[0]);
738 pos++;
739 len--;
740 }
741 else
742 {
743 exponent = chunk_create(pos + 3, 256*pos[1] + pos[2]);
744 pos += 3;
745 len -= 3;
746 }
747 if (exponent.len > len)
748 {
749 DBG1("RFC 3110 public key blob too short for exponent");
750 goto end;
751 }
752 pos += exponent.len;
753 len -= exponent.len;
754
755 if (len == 0)
756 {
757 DBG1("RFC 3110 public key blob has zero length modulus");
758 goto end;
759 }
760 modulus = chunk_create(pos, len);
761
762 mpz_import(this->n, modulus.len, 1, 1, 1, 0, modulus.ptr);
763 mpz_import(this->e, exponent.len, 1, 1, 1, 0, exponent.ptr);
764 this->k = (mpz_sizeinbase(this->n, 2) + 7) / BITS_PER_BYTE;
765 free(blob.ptr);
766
767 if (!gmp_rsa_public_key_build_id(this->n, this->e,
768 &this->keyid, &this->keyid_info))
769 {
770 destroy(this);
771 return NULL;
772 }
773 return &this->public;
774
775 end:
776 free(blob.ptr);
777 destroy(this);
778 return NULL;
779 }
780
781 typedef struct private_builder_t private_builder_t;
782 /**
783 * Builder implementation for key loading
784 */
785 struct private_builder_t {
786 /** implements the builder interface */
787 builder_t public;
788 /** loaded public key */
789 gmp_rsa_public_key_t *key;
790 };
791
792 /**
793 * Implementation of builder_t.build
794 */
795 static gmp_rsa_public_key_t *build(private_builder_t *this)
796 {
797 gmp_rsa_public_key_t *key = this->key;
798
799 free(this);
800 return key;
801 }
802
803 /**
804 * Implementation of builder_t.add
805 */
806 static void add(private_builder_t *this, builder_part_t part, ...)
807 {
808 if (!this->key)
809 {
810 va_list args;
811 chunk_t chunk;
812
813 switch (part)
814 {
815 case BUILD_BLOB_ASN1_DER:
816 {
817 va_start(args, part);
818 chunk = va_arg(args, chunk_t);
819 this->key = load_asn1_der(chunk_clone(chunk));
820 va_end(args);
821 return;
822 }
823 case BUILD_BLOB_PGP:
824 {
825 va_start(args, part);
826 chunk = va_arg(args, chunk_t);
827 this->key = load_pgp(chunk_clone(chunk));
828 va_end(args);
829 return;
830 }
831 case BUILD_BLOB_RFC_3110:
832 {
833 va_start(args, part);
834 chunk = va_arg(args, chunk_t);
835 this->key = load_rfc_3110(chunk_clone(chunk));
836 va_end(args);
837 return;
838 }
839 default:
840 break;
841 }
842 }
843 if (this->key)
844 {
845 destroy((private_gmp_rsa_public_key_t*)this->key);
846 }
847 builder_cancel(&this->public);
848 }
849
850 /**
851 * Builder construction function
852 */
853 builder_t *gmp_rsa_public_key_builder(key_type_t type)
854 {
855 private_builder_t *this;
856
857 if (type != KEY_RSA)
858 {
859 return NULL;
860 }
861
862 this = malloc_thing(private_builder_t);
863
864 this->key = NULL;
865 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
866 this->public.build = (void*(*)(builder_t *this))build;
867
868 return &this->public;
869 }
870