2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "gmp_rsa_public_key.h"
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
30 #include <crypto/hashers/hasher.h>
33 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t
;
36 * Private data structure with signing context.
38 struct private_gmp_rsa_public_key_t
{
40 * Public interface for this signer.
42 gmp_rsa_public_key_t
public;
60 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
62 identification_t
*keyid_info
;
65 * Keyid formed as a SHA-1 hash of a publicKey object
67 identification_t
*keyid
;
76 * Shared functions defined in gmp_rsa_private_key.c
78 extern chunk_t
gmp_mpz_to_chunk(const mpz_t value
);
79 extern chunk_t
gmp_mpz_to_asn1(const mpz_t value
);
82 * RSAEP algorithm specified in PKCS#1.
84 static chunk_t
rsaep(private_gmp_rsa_public_key_t
*this, chunk_t data
)
92 mpz_import(m
, data
.len
, 1, 1, 1, 0, data
.ptr
);
94 mpz_powm(c
, m
, this->e
, this->n
);
96 encrypted
.len
= this->k
;
97 encrypted
.ptr
= mpz_export(NULL
, NULL
, 1, encrypted
.len
, 1, 0, c
);
98 if (encrypted
.ptr
== NULL
)
110 * RSAVP1 algorithm specified in PKCS#1.
112 static chunk_t
rsavp1(private_gmp_rsa_public_key_t
*this, chunk_t data
)
114 return rsaep(this, data
);
118 * ASN.1 definition of digestInfo
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
}
126 #define DIGEST_INFO 0
127 #define DIGEST_INFO_ALGORITHM 1
128 #define DIGEST_INFO_DIGEST 2
131 * Verification of an EMPSA PKCS1 signature described in PKCS#1
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
)
138 bool success
= FALSE
;
140 /* remove any preceding 0-bytes from signature */
141 while (signature
.len
&& *(signature
.ptr
) == 0x00)
143 signature
= chunk_skip(signature
, 1);
146 if (signature
.len
== 0 || signature
.len
> this->k
)
151 /* unpack signature */
152 em_ori
= em
= rsavp1(this, signature
);
154 /* result should look like this:
155 * EM = 0x00 || 0x01 || PS || 0x00 || T.
156 * PS = 0xFF padding, with length to fill em
160 /* check magic bytes */
161 if (*(em
.ptr
) != 0x00 || *(em
.ptr
+1) != 0x01)
165 em
= chunk_skip(em
, 2);
167 /* find magic 0x00 */
172 /* found magic byte, stop */
173 em
= chunk_skip(em
, 1);
176 else if (*em
.ptr
!= 0xFF)
178 /* bad padding, decryption failed ?!*/
181 em
= chunk_skip(em
, 1);
186 /* no digestInfo found */
190 if (algorithm
== HASH_UNKNOWN
)
191 { /* IKEv1 signatures without digestInfo */
192 if (em
.len
!= data
.len
)
194 DBG1("hash size in signature is %u bytes instead of %u bytes",
198 success
= memeq(em
.ptr
, data
.ptr
, data
.len
);
201 { /* IKEv2 and X.509 certificate signatures */
202 asn1_parser_t
*parser
;
205 hash_algorithm_t hash_algorithm
= HASH_UNKNOWN
;
207 DBG2("signature verification:");
208 parser
= asn1_parser_create(digestInfoObjects
, em
);
210 while (parser
->iterate(parser
, &objectID
, &object
))
216 if (em
.len
> object
.len
)
218 DBG1("digestInfo field in signature is followed by %u surplus bytes",
219 em
.len
- object
.len
);
224 case DIGEST_INFO_ALGORITHM
:
226 int hash_oid
= asn1_parse_algorithmIdentifier(object
,
227 parser
->get_level(parser
)+1, NULL
);
229 hash_algorithm
= hasher_algorithm_from_oid(hash_oid
);
230 if (hash_algorithm
== HASH_UNKNOWN
|| hash_algorithm
!= algorithm
)
232 DBG1("expected hash algorithm %N, but found %N (OID: %#B)",
233 hash_algorithm_names
, algorithm
,
234 hash_algorithm_names
, hash_algorithm
, &object
);
239 case DIGEST_INFO_DIGEST
:
244 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
247 DBG1("hash algorithm %N not supported",
248 hash_algorithm_names
, hash_algorithm
);
252 if (object
.len
!= hasher
->get_hash_size(hasher
))
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
);
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
);
273 success
&= parser
->success(parser
);
274 parser
->destroy(parser
);
283 * Implementation of public_key_t.get_type.
285 static key_type_t
get_type(private_gmp_rsa_public_key_t
*this)
291 * Implementation of public_key_t.verify.
293 static bool verify(private_gmp_rsa_public_key_t
*this, signature_scheme_t scheme
,
294 chunk_t data
, chunk_t signature
)
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_SHA224
:
305 return verify_emsa_pkcs1_signature(this, HASH_SHA224
, data
, signature
);
306 case SIGN_RSA_EMSA_PKCS1_SHA256
:
307 return verify_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
308 case SIGN_RSA_EMSA_PKCS1_SHA384
:
309 return verify_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
310 case SIGN_RSA_EMSA_PKCS1_SHA512
:
311 return verify_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
313 DBG1("signature scheme %N not supported in RSA",
314 signature_scheme_names
, scheme
);
319 #define MIN_PS_PADDING 8
322 * Implementation of public_key_t.encrypt.
324 static bool encrypt_(private_gmp_rsa_public_key_t
*this, chunk_t plain
,
332 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
335 DBG1("no random generator available");
339 /* number of pseudo-random padding octets */
340 padding
= this->k
- plain
.len
- 3;
341 if (padding
< MIN_PS_PADDING
)
343 DBG1("pseudo-random padding must be at least %d octets", MIN_PS_PADDING
);
347 /* padding according to PKCS#1 7.2.1 (RSAES-PKCS1-v1.5-ENCRYPT) */
348 DBG2("padding %u bytes of data to the rsa modulus size of %u bytes",
351 em
.ptr
= malloc(em
.len
);
356 /* fill with pseudo random octets */
357 rng
->get_bytes(rng
, padding
, pos
);
359 /* replace zero-valued random octets */
360 for (i
= 0; i
< padding
; i
++)
364 rng
->get_bytes(rng
, 1, pos
);
370 /* append the padding terminator */
373 /* now add the data */
374 memcpy(pos
, plain
.ptr
, plain
.len
);
375 DBG3("padded data before rsa encryption: %B", &em
);
377 /* rsa encryption using PKCS#1 RSAEP */
378 *crypto
= rsaep(this, em
);
379 DBG3("rsa encrypted data: %B", crypto
);
385 * Implementation of gmp_rsa_public_key.equals.
387 static bool equals(private_gmp_rsa_public_key_t
*this, public_key_t
*other
)
389 identification_t
*keyid
;
391 if (&this->public.interface
== other
)
395 if (other
->get_type(other
) != KEY_RSA
)
399 keyid
= other
->get_id(other
, ID_PUBKEY_SHA1
);
400 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
404 keyid
= other
->get_id(other
, ID_PUBKEY_INFO_SHA1
);
405 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
413 * Implementation of public_key_t.get_keysize.
415 static size_t get_keysize(private_gmp_rsa_public_key_t
*this)
421 * Build the PGP version 3 RSA key identifier from n and e using
422 * MD5 hashed modulus and exponent.
424 static identification_t
* gmp_rsa_build_pgp_v3_keyid(mpz_t n
, mpz_t e
)
426 identification_t
*keyid
;
427 chunk_t modulus
, mod
, exponent
, exp
, hash
;
430 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_MD5
);
433 DBG1("computation of PGP V3 keyid failed, no MD5 hasher is available");
436 mod
= modulus
= gmp_mpz_to_chunk(n
);
437 exp
= exponent
= gmp_mpz_to_chunk(e
);
439 /* remove leading zero bytes before hashing modulus and exponent */
440 while (mod
.len
> 0 && *mod
.ptr
== 0x00)
445 while (exp
.len
> 0 && *exp
.ptr
== 0x00)
450 hasher
->allocate_hash(hasher
, mod
, NULL
);
451 hasher
->allocate_hash(hasher
, exp
, &hash
);
452 hasher
->destroy(hasher
);
453 keyid
= identification_create_from_encoding(ID_KEY_ID
, hash
);
461 * Implementation of public_key_t.get_id.
463 static identification_t
*get_id(private_gmp_rsa_public_key_t
*this,
468 case ID_PUBKEY_INFO_SHA1
:
469 return this->keyid_info
;
473 return gmp_rsa_build_pgp_v3_keyid(this->n
, this->e
);
480 * Implementation of public_key_t.get_encoding.
482 static chunk_t
get_encoding(private_gmp_rsa_public_key_t
*this)
484 return asn1_wrap(ASN1_SEQUENCE
, "mm",
485 gmp_mpz_to_asn1(this->n
),
486 gmp_mpz_to_asn1(this->e
));
490 * Implementation of public_key_t.get_ref.
492 static private_gmp_rsa_public_key_t
* get_ref(private_gmp_rsa_public_key_t
*this)
499 * Implementation of gmp_rsa_public_key.destroy.
501 static void destroy(private_gmp_rsa_public_key_t
*this)
503 if (ref_put(&this->ref
))
507 DESTROY_IF(this->keyid
);
508 DESTROY_IF(this->keyid_info
);
514 * Generic private constructor
516 static private_gmp_rsa_public_key_t
*gmp_rsa_public_key_create_empty()
518 private_gmp_rsa_public_key_t
*this = malloc_thing(private_gmp_rsa_public_key_t
);
520 this->public.interface
.get_type
= (key_type_t (*) (public_key_t
*))get_type
;
521 this->public.interface
.verify
= (bool (*) (public_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
))verify
;
522 this->public.interface
.encrypt
= (bool (*) (public_key_t
*, chunk_t
, chunk_t
*))encrypt_
;
523 this->public.interface
.equals
= (bool (*) (public_key_t
*, public_key_t
*))equals
;
524 this->public.interface
.get_keysize
= (size_t (*) (public_key_t
*))get_keysize
;
525 this->public.interface
.get_id
= (identification_t
* (*) (public_key_t
*, id_type_t
))get_id
;
526 this->public.interface
.get_encoding
= (chunk_t(*) (public_key_t
*))get_encoding
;
527 this->public.interface
.get_ref
= (public_key_t
* (*) (public_key_t
*this))get_ref
;
528 this->public.interface
.destroy
= (void (*) (public_key_t
*this))destroy
;
531 this->keyid_info
= NULL
;
538 * Build the RSA key identifier from n and e using SHA1 hashed publicKey(Info).
539 * Also used in rsa_private_key.c.
541 bool gmp_rsa_public_key_build_id(mpz_t n
, mpz_t e
, identification_t
**keyid
,
542 identification_t
**keyid_info
)
544 chunk_t publicKeyInfo
, publicKey
, hash
;
547 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
550 DBG1("SHA1 hash algorithm not supported, unable to use RSA");
553 publicKey
= asn1_wrap(ASN1_SEQUENCE
, "mm",
556 hasher
->allocate_hash(hasher
, publicKey
, &hash
);
557 *keyid
= identification_create_from_encoding(ID_PUBKEY_SHA1
, hash
);
560 publicKeyInfo
= asn1_wrap(ASN1_SEQUENCE
, "cm",
561 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
562 asn1_bitstring("m", publicKey
));
563 hasher
->allocate_hash(hasher
, publicKeyInfo
, &hash
);
564 *keyid_info
= identification_create_from_encoding(ID_PUBKEY_INFO_SHA1
, hash
);
567 hasher
->destroy(hasher
);
568 chunk_free(&publicKeyInfo
);
574 * Create a public key from mpz values, used in gmp_rsa_private_key
576 gmp_rsa_public_key_t
*gmp_rsa_public_key_create_from_n_e(mpz_t n
, mpz_t e
)
578 private_gmp_rsa_public_key_t
*this = gmp_rsa_public_key_create_empty();
580 mpz_init_set(this->n
, n
);
581 mpz_init_set(this->e
, e
);
583 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
584 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
585 &this->keyid
, &this->keyid_info
))
590 return &this->public;
594 * ASN.1 definition of RSApublicKey
596 static const asn1Object_t pubkeyObjects
[] = {
597 { 0, "RSAPublicKey", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 0 */
598 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
599 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
600 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
602 #define PUB_KEY_RSA_PUBLIC_KEY 0
603 #define PUB_KEY_MODULUS 1
604 #define PUB_KEY_EXPONENT 2
607 * Load a public key from an ASN.1 encoded blob
609 static gmp_rsa_public_key_t
*load_asn1_der(chunk_t blob
)
611 asn1_parser_t
*parser
;
614 bool success
= FALSE
;
616 private_gmp_rsa_public_key_t
*this = gmp_rsa_public_key_create_empty();
621 parser
= asn1_parser_create(pubkeyObjects
, blob
);
623 while (parser
->iterate(parser
, &objectID
, &object
))
627 case PUB_KEY_MODULUS
:
628 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
630 case PUB_KEY_EXPONENT
:
631 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
635 success
= parser
->success(parser
);
637 parser
->destroy(parser
);
645 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
647 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
648 &this->keyid
, &this->keyid_info
))
653 return &this->public;
657 * Load a public key from an OpenPGP blob
659 static gmp_rsa_public_key_t
* load_pgp(chunk_t blob
)
662 chunk_t packet
= blob
;
663 private_gmp_rsa_public_key_t
*this = gmp_rsa_public_key_create_empty();
668 for (objectID
= PUB_KEY_MODULUS
; objectID
<= PUB_KEY_EXPONENT
; objectID
++)
672 DBG2("L3 - %s:", pubkeyObjects
[objectID
].name
);
673 object
.len
= pgp_length(&packet
, 2);
675 if (object
.len
== PGP_INVALID_LENGTH
)
677 DBG1("OpenPGP length is invalid");
680 object
.len
= (object
.len
+ 7) / BITS_PER_BYTE
;
681 if (object
.len
> packet
.len
)
683 DBG1("OpenPGP field is too short");
686 object
.ptr
= packet
.ptr
;
687 packet
.ptr
+= object
.len
;
688 packet
.len
-= object
.len
;
693 case PUB_KEY_MODULUS
:
694 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
696 case PUB_KEY_EXPONENT
:
697 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
702 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
705 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
706 &this->keyid
, &this->keyid_info
))
711 return &this->public;
720 * Load a public key from an RFC 3110 encoded blob
722 static gmp_rsa_public_key_t
*load_rfc_3110(chunk_t blob
)
724 chunk_t exponent
, modulus
;
725 u_char
*pos
= blob
.ptr
;
726 size_t len
= blob
.len
;
727 private_gmp_rsa_public_key_t
*this = gmp_rsa_public_key_create_empty();
734 DBG1("RFC 3110 public key blob too short for exponent length");
739 exponent
= chunk_create(pos
+ 1, pos
[0]);
745 exponent
= chunk_create(pos
+ 3, 256*pos
[1] + pos
[2]);
749 if (exponent
.len
> len
)
751 DBG1("RFC 3110 public key blob too short for exponent");
759 DBG1("RFC 3110 public key blob has zero length modulus");
762 modulus
= chunk_create(pos
, len
);
764 mpz_import(this->n
, modulus
.len
, 1, 1, 1, 0, modulus
.ptr
);
765 mpz_import(this->e
, exponent
.len
, 1, 1, 1, 0, exponent
.ptr
);
766 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
769 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
770 &this->keyid
, &this->keyid_info
))
775 return &this->public;
783 typedef struct private_builder_t private_builder_t
;
785 * Builder implementation for key loading
787 struct private_builder_t
{
788 /** implements the builder interface */
790 /** loaded public key */
791 gmp_rsa_public_key_t
*key
;
795 * Implementation of builder_t.build
797 static gmp_rsa_public_key_t
*build(private_builder_t
*this)
799 gmp_rsa_public_key_t
*key
= this->key
;
806 * Implementation of builder_t.add
808 static void add(private_builder_t
*this, builder_part_t part
, ...)
817 case BUILD_BLOB_ASN1_DER
:
819 va_start(args
, part
);
820 chunk
= va_arg(args
, chunk_t
);
821 this->key
= load_asn1_der(chunk_clone(chunk
));
827 va_start(args
, part
);
828 chunk
= va_arg(args
, chunk_t
);
829 this->key
= load_pgp(chunk_clone(chunk
));
833 case BUILD_BLOB_RFC_3110
:
835 va_start(args
, part
);
836 chunk
= va_arg(args
, chunk_t
);
837 this->key
= load_rfc_3110(chunk_clone(chunk
));
847 destroy((private_gmp_rsa_public_key_t
*)this->key
);
849 builder_cancel(&this->public);
853 * Builder construction function
855 builder_t
*gmp_rsa_public_key_builder(key_type_t type
)
857 private_builder_t
*this;
864 this = malloc_thing(private_builder_t
);
867 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
868 this->public.build
= (void*(*)(builder_t
*this))build
;
870 return &this->public;