2 * Copyright (C) 2005-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "gcrypt_rsa_private_key.h"
22 #include <asn1/asn1.h>
23 #include <asn1/asn1_parser.h>
25 typedef struct private_gcrypt_rsa_private_key_t private_gcrypt_rsa_private_key_t
;
28 * Private data of a gcrypt_rsa_private_key_t object.
30 struct private_gcrypt_rsa_private_key_t
{
35 gcrypt_rsa_private_key_t
public;
38 * gcrypt S-expression representing an RSA key
43 * Keyid formed as a SHA-1 hash of a publicKey object
45 identification_t
* keyid
;
48 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
50 identification_t
* keyid_info
;
59 * find a token in a S-expression
61 static chunk_t
gcrypt_rsa_find_token(gcry_sexp_t sexp
, char *name
)
64 chunk_t data
= chunk_empty
;
66 token
= gcry_sexp_find_token(sexp
, name
, 1);
69 data
.ptr
= (char*)gcry_sexp_nth_data(token
, 1, &data
.len
);
74 data
= chunk_clone(data
);
75 gcry_sexp_release(token
);
81 * Implementation of gcrypt_rsa_private_key_t.build_emsa_pkcs1_signature.
83 static bool sign_pkcs1(private_gcrypt_rsa_private_key_t
*this,
84 hash_algorithm_t hash_algorithm
, char *hash_name
,
85 chunk_t data
, chunk_t
*signature
)
93 hash_oid
= hasher_algorithm_to_oid(hash_algorithm
);
94 if (hash_oid
== OID_UNKNOWN
)
98 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
103 hasher
->allocate_hash(hasher
, data
, &hash
);
104 hasher
->destroy(hasher
);
106 err
= gcry_sexp_build(&in
, NULL
, "(data(flags pkcs1)(hash %s %b))",
107 hash_name
, hash
.len
, hash
.ptr
);
111 DBG1("building signature S-expression failed: %s", gpg_strerror(err
));
114 err
= gcry_pk_sign(&out
, in
, this->key
);
115 gcry_sexp_release(in
);
118 DBG1("creating pkcs1 signature failed: %s", gpg_strerror(err
));
121 *signature
= gcrypt_rsa_find_token(out
, "s");
122 gcry_sexp_release(out
);
123 return !!signature
->len
;
127 * Implementation of gcrypt_rsa_private_key.destroy.
129 static key_type_t
get_type(private_gcrypt_rsa_private_key_t
*this)
135 * Implementation of gcrypt_rsa_private_key.destroy.
137 static bool sign(private_gcrypt_rsa_private_key_t
*this, signature_scheme_t scheme
,
138 chunk_t data
, chunk_t
*sig
)
143 /* default is EMSA-PKCS1 using SHA1 */
144 case SIGN_RSA_EMSA_PKCS1_SHA1
:
145 return sign_pkcs1(this, HASH_SHA1
, "sha1", data
, sig
);
146 case SIGN_RSA_EMSA_PKCS1_SHA256
:
147 return sign_pkcs1(this, HASH_SHA256
, "sha256", data
, sig
);
148 case SIGN_RSA_EMSA_PKCS1_SHA384
:
149 return sign_pkcs1(this, HASH_SHA384
, "sha384", data
, sig
);
150 case SIGN_RSA_EMSA_PKCS1_SHA512
:
151 return sign_pkcs1(this, HASH_SHA512
, "sha512", data
, sig
);
152 case SIGN_RSA_EMSA_PKCS1_MD5
:
153 return sign_pkcs1(this, HASH_MD5
, "md5", data
, sig
);
155 DBG1("signature scheme %N not supported in RSA",
156 signature_scheme_names
, scheme
);
162 * Implementation of gcrypt_rsa_private_key.destroy.
164 static bool decrypt(private_gcrypt_rsa_private_key_t
*this,
165 chunk_t crypto
, chunk_t
*plain
)
167 DBG1("RSA private key decryption not implemented");
172 * Implementation of gcrypt_rsa_private_key.get_keysize.
174 static size_t get_keysize(private_gcrypt_rsa_private_key_t
*this)
176 return gcry_pk_get_nbits(this->key
) / 8;
180 * Implementation of gcrypt_rsa_private_key.destroy.
182 static identification_t
* get_id(private_gcrypt_rsa_private_key_t
*this,
187 case ID_PUBKEY_INFO_SHA1
:
188 return this->keyid_info
;
197 * Implementation of gcrypt_rsa_private_key.get_public_key.
199 static public_key_t
* get_public_key(private_gcrypt_rsa_private_key_t
*this)
205 * Implementation of gcrypt_rsa_private_key.equals.
207 static bool equals(private_gcrypt_rsa_private_key_t
*this, private_key_t
*other
)
209 identification_t
*keyid
;
211 if (&this->public.interface
== other
)
215 if (other
->get_type(other
) != KEY_RSA
)
219 keyid
= other
->get_id(other
, ID_PUBKEY_SHA1
);
220 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
224 keyid
= other
->get_id(other
, ID_PUBKEY_INFO_SHA1
);
225 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
233 * Implementation of gcrypt_rsa_private_key.belongs_to.
235 static bool belongs_to(private_gcrypt_rsa_private_key_t
*this,
236 public_key_t
*public)
238 identification_t
*keyid
;
240 if (public->get_type(public) != KEY_RSA
)
244 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
245 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
249 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
250 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
258 * Implementation of private_key_t.get_encoding.
260 static chunk_t
get_encoding(private_gcrypt_rsa_private_key_t
*this)
262 chunk_t cp
, cq
, cd
, cexp1
= chunk_empty
, cexp2
= chunk_empty
;
263 gcry_mpi_t p
= NULL
, q
= NULL
, d
= NULL
, exp1
, exp2
;
266 /* p and q are swapped, gcrypt expects p < q */
267 cp
= gcrypt_rsa_find_token(this->key
, "q");
268 cq
= gcrypt_rsa_find_token(this->key
, "p");
269 cd
= gcrypt_rsa_find_token(this->key
, "d");
271 err
= gcry_mpi_scan(&p
, GCRYMPI_FMT_USG
, cp
.ptr
, cp
.len
, NULL
)
272 | gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, cq
.ptr
, cq
.len
, NULL
)
273 | gcry_mpi_scan(&d
, GCRYMPI_FMT_USG
, cd
.ptr
, cd
.len
, NULL
);
282 DBG1("scanning mpi for export failed: %s", gpg_strerror(err
));
286 gcry_mpi_sub_ui(p
, p
, 1);
287 exp1
= gcry_mpi_new(gcry_pk_get_nbits(this->key
));
288 gcry_mpi_mod(exp1
, d
, p
);
291 gcry_mpi_sub_ui(q
, q
, 1);
292 exp2
= gcry_mpi_new(gcry_pk_get_nbits(this->key
));
293 gcry_mpi_mod(exp1
, d
, q
);
296 err
= gcry_mpi_aprint(GCRYMPI_FMT_USG
, &cexp1
.ptr
, &cexp1
.len
, exp1
)
297 | gcry_mpi_aprint(GCRYMPI_FMT_USG
, &cexp2
.ptr
, &cexp2
.len
, exp2
);
300 gcry_mpi_release(exp1
);
301 gcry_mpi_release(exp2
);
305 DBG1("printing mpi for export failed: %s", gpg_strerror(err
));
314 return asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmmm", ASN1_INTEGER_0
,
315 asn1_wrap(ASN1_INTEGER
, "m", gcrypt_rsa_find_token(this->key
, "n")),
316 asn1_wrap(ASN1_INTEGER
, "m", gcrypt_rsa_find_token(this->key
, "e")),
317 asn1_wrap(ASN1_INTEGER
, "m", cd
),
318 asn1_wrap(ASN1_INTEGER
, "m", cp
),
319 asn1_wrap(ASN1_INTEGER
, "m", cq
),
320 asn1_wrap(ASN1_INTEGER
, "m", cexp1
),
321 asn1_wrap(ASN1_INTEGER
, "m", cexp2
),
322 asn1_wrap(ASN1_INTEGER
, "m", gcrypt_rsa_find_token(this->key
, "u")));
326 * Implementation of gcrypt_rsa_private_key.get_ref.
328 static private_key_t
* get_ref(private_gcrypt_rsa_private_key_t
*this)
331 return &this->public.interface
;
335 * Implementation of gcrypt_rsa_private_key.destroy.
337 static void destroy(private_gcrypt_rsa_private_key_t
*this)
339 if (ref_put(&this->ref
))
341 DESTROY_IF(this->keyid
);
342 DESTROY_IF(this->keyid_info
);
343 gcry_sexp_release(this->key
);
349 * Internal generic constructor
351 static private_gcrypt_rsa_private_key_t
*gcrypt_rsa_private_key_create_empty()
353 private_gcrypt_rsa_private_key_t
*this = malloc_thing(private_gcrypt_rsa_private_key_t
);
355 this->public.interface
.get_type
= (key_type_t (*)(private_key_t
*this))get_type
;
356 this->public.interface
.sign
= (bool (*)(private_key_t
*this, signature_scheme_t scheme
, chunk_t data
, chunk_t
*signature
))sign
;
357 this->public.interface
.decrypt
= (bool (*)(private_key_t
*this, chunk_t crypto
, chunk_t
*plain
))decrypt
;
358 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*this))get_keysize
;
359 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*this,id_type_t
))get_id
;
360 this->public.interface
.get_public_key
= (public_key_t
* (*)(private_key_t
*this))get_public_key
;
361 this->public.interface
.equals
= (bool (*) (private_key_t
*, private_key_t
*))equals
;
362 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*this, public_key_t
*public))belongs_to
;
363 this->public.interface
.get_encoding
= (chunk_t(*)(private_key_t
*))get_encoding
;
364 this->public.interface
.get_ref
= (private_key_t
* (*)(private_key_t
*this))get_ref
;
365 this->public.interface
.destroy
= (void (*)(private_key_t
*this))destroy
;
369 this->keyid_info
= NULL
;
376 * build the keyids of a private/public key
378 static bool gcrypt_rsa_build_keyids(gcry_sexp_t key
, identification_t
**keyid
,
379 identification_t
**keyid_info
)
381 chunk_t publicKeyInfo
, publicKey
, hash
;
384 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
387 DBG1("SHA1 hash algorithm not supported, unable to use RSA");
390 publicKey
= asn1_wrap(ASN1_SEQUENCE
, "mm",
391 asn1_wrap(ASN1_INTEGER
, "m", gcrypt_rsa_find_token(key
, "n")),
392 asn1_wrap(ASN1_INTEGER
, "m", gcrypt_rsa_find_token(key
, "e")));
393 hasher
->allocate_hash(hasher
, publicKey
, &hash
);
394 *keyid
= identification_create_from_encoding(ID_PUBKEY_SHA1
, hash
);
397 publicKeyInfo
= asn1_wrap(ASN1_SEQUENCE
, "cm",
398 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
399 asn1_bitstring("m", publicKey
));
400 hasher
->allocate_hash(hasher
, publicKeyInfo
, &hash
);
401 *keyid_info
= identification_create_from_encoding(ID_PUBKEY_INFO_SHA1
, hash
);
404 hasher
->destroy(hasher
);
405 chunk_free(&publicKeyInfo
);
411 * Generate an RSA key of specified key size
413 static gcrypt_rsa_private_key_t
*generate(size_t key_size
)
415 private_gcrypt_rsa_private_key_t
*this;
416 gcry_sexp_t param
, key
;
419 err
= gcry_sexp_build(¶m
, NULL
, "(genkey(rsa(nbits %d)))", key_size
);
422 DBG1("building S-expression failed: %s", gpg_strerror(err
));
426 err
= gcry_pk_genkey(&key
, param
);
427 gcry_sexp_release(param
);
430 DBG1("generating RSA key failed: %s", gpg_strerror(err
));
433 this = gcrypt_rsa_private_key_create_empty();
436 if (!gcrypt_rsa_build_keyids(this->key
, &this->keyid
, &this->keyid_info
))
442 return &this->public;
446 * ASN.1 definition of a PKCS#1 RSA private key
448 static const asn1Object_t privkeyObjects
[] = {
449 { 0, "RSAPrivateKey", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
450 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
451 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
452 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
453 { 1, "privateExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
454 { 1, "prime1", ASN1_INTEGER
, ASN1_BODY
}, /* 5 */
455 { 1, "prime2", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
456 { 1, "exponent1", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
457 { 1, "exponent2", ASN1_INTEGER
, ASN1_BODY
}, /* 8 */
458 { 1, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 9 */
459 { 1, "otherPrimeInfos", ASN1_SEQUENCE
, ASN1_OPT
|
460 ASN1_LOOP
}, /* 10 */
461 { 2, "otherPrimeInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 11 */
462 { 3, "prime", ASN1_INTEGER
, ASN1_BODY
}, /* 12 */
463 { 3, "exponent", ASN1_INTEGER
, ASN1_BODY
}, /* 13 */
464 { 3, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
465 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 15 */
466 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
468 #define PRIV_KEY_VERSION 1
469 #define PRIV_KEY_MODULUS 2
470 #define PRIV_KEY_PUB_EXP 3
471 #define PRIV_KEY_PRIV_EXP 4
472 #define PRIV_KEY_PRIME1 5
473 #define PRIV_KEY_PRIME2 6
474 #define PRIV_KEY_EXP1 7
475 #define PRIV_KEY_EXP2 8
476 #define PRIV_KEY_COEFF 9
479 * load private key from a ASN1 encoded blob
481 static gcrypt_rsa_private_key_t
*load(chunk_t blob
)
483 private_gcrypt_rsa_private_key_t
*this;
484 asn1_parser_t
*parser
;
487 bool success
= FALSE
;
488 chunk_t n
, e
, d
, u
, p
, q
;
491 parser
= asn1_parser_create(privkeyObjects
, blob
);
492 parser
->set_flags(parser
, FALSE
, TRUE
);
494 while (parser
->iterate(parser
, &objectID
, &object
))
498 case PRIV_KEY_VERSION
:
499 if (object
.len
> 0 && *object
.ptr
!= 0)
504 case PRIV_KEY_MODULUS
:
507 case PRIV_KEY_PUB_EXP
:
510 case PRIV_KEY_PRIV_EXP
:
513 case PRIV_KEY_PRIME1
:
514 /* p and q are swapped, as gcrypt expects p < q */
517 case PRIV_KEY_PRIME2
:
528 success
= parser
->success(parser
);
531 parser
->destroy(parser
);
538 this = gcrypt_rsa_private_key_create_empty();
539 err
= gcry_sexp_build(&this->key
, NULL
,
540 "(private-key(rsa(n %b)(e %b)(d %b)(p %b)(q %b)(u %b)))",
541 n
.len
, n
.ptr
, e
.len
, e
.ptr
, d
.len
, d
.ptr
,
542 p
.len
, p
.ptr
, q
.len
, q
.ptr
, u
.len
, u
.ptr
);
545 DBG1("loading private key failed: %s", gpg_strerror(err
));
549 err
= gcry_pk_testkey(this->key
);
552 DBG1("private key sanity check failed: %s", gpg_strerror(err
));
556 if (!gcrypt_rsa_build_keyids(this->key
, &this->keyid
, &this->keyid_info
))
561 return &this->public;
564 typedef struct private_builder_t private_builder_t
;
567 * Builder implementation for key loading/generation
569 struct private_builder_t
{
570 /** implements the builder interface */
572 /** loaded/generated private key */
573 gcrypt_rsa_private_key_t
*key
;
577 * Implementation of builder_t.build
579 static gcrypt_rsa_private_key_t
*build(private_builder_t
*this)
581 gcrypt_rsa_private_key_t
*key
= this->key
;
588 * Implementation of builder_t.add
590 static void add(private_builder_t
*this, builder_part_t part
, ...)
598 case BUILD_BLOB_ASN1_DER
:
600 va_start(args
, part
);
601 this->key
= load(va_arg(args
, chunk_t
));
607 va_start(args
, part
);
608 this->key
= generate(va_arg(args
, u_int
));
618 destroy((private_gcrypt_rsa_private_key_t
*)this->key
);
620 builder_cancel(&this->public);
624 * Builder construction function
626 builder_t
*gcrypt_rsa_private_key_builder(key_type_t type
)
628 private_builder_t
*this;
635 this = malloc_thing(private_builder_t
);
638 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
639 this->public.build
= (void*(*)(builder_t
*this))build
;
641 return &this->public;