diff options
author | Martin Willi <martin@strongswan.org> | 2006-03-24 15:37:49 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2006-03-24 15:37:49 +0000 |
commit | 9c781c152ad66a73139447e40a2081c38080c651 (patch) | |
tree | e214ab37398230685621ac3732444e279c40b785 /Source/charon/transforms/rsa/rsa_public_key.c | |
parent | dec598220b9a293c4ec75e593ab642a8945fa4fc (diff) | |
download | strongswan-9c781c152ad66a73139447e40a2081c38080c651.tar.bz2 strongswan-9c781c152ad66a73139447e40a2081c38080c651.tar.xz |
- starter work on asn1 with der de/encoder
- RSA private and public key can load read key from ASN1 DER
- some other fixes here and there
Diffstat (limited to 'Source/charon/transforms/rsa/rsa_public_key.c')
-rw-r--r-- | Source/charon/transforms/rsa/rsa_public_key.c | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/Source/charon/transforms/rsa/rsa_public_key.c b/Source/charon/transforms/rsa/rsa_public_key.c index 6271e4a05..fb3fe3c67 100644 --- a/Source/charon/transforms/rsa/rsa_public_key.c +++ b/Source/charon/transforms/rsa/rsa_public_key.c @@ -27,16 +27,17 @@ #include <daemon.h> #include <utils/allocator.h> #include <transforms/hashers/hasher.h> +#include <asn1/der_decoder.h> /* - * Since we don't have an ASN1 parser/generator, + * For simplicity, * we use these predefined values for - * hash algorithm oids. These also contain + * hash algorithm OIDs. These also contain * the length of the following hash. * These values are also used in rsa_private_key.c. */ -u_int8_t md2_oid[18] = { +u_int8_t md2_oid[] = { 0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86, 0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00, 0x04,0x10 @@ -92,6 +93,7 @@ struct private_rsa_public_key_t { * Public modulus. */ mpz_t n; + /** * Public exponent. */ @@ -122,7 +124,17 @@ struct private_rsa_public_key_t { }; /** - * Implementation of private_rsa_public_key_t.rsadp and private_rsa_public_key_t.rsavp1 + * Rules for de-/encoding of a public key from/in ASN1 + */ +static asn1_rule_t rsa_public_key_rules[] = { + {ASN1_SEQUENCE, 0, 0, 0}, + { ASN1_INTEGER, ASN1_MPZ, offsetof(private_rsa_public_key_t, n), 0}, + { ASN1_INTEGER, ASN1_MPZ, offsetof(private_rsa_public_key_t, e), 0}, + {ASN1_END, 0, 0, 0}, +}; + +/** + * Implementation of private_rsa_public_key_t.rsaep and private_rsa_public_key_t.rsavp1 */ static chunk_t rsaep(private_rsa_public_key_t *this, chunk_t data) { @@ -146,7 +158,7 @@ static chunk_t rsaep(private_rsa_public_key_t *this, chunk_t data) } /** - * Implementation of rsa_public_key.verify_emsa_signature. + * Implementation of rsa_public_key.verify_emsa_pkcs1_signature. */ static status_t verify_emsa_pkcs1_signature(private_rsa_public_key_t *this, chunk_t data, chunk_t signature) { @@ -278,25 +290,20 @@ static status_t verify_emsa_pkcs1_signature(private_rsa_public_key_t *this, chun */ static status_t set_key(private_rsa_public_key_t *this, chunk_t key) { - chunk_t n, e; - - n.len = key.len/2; - n.ptr = key.ptr; - e.len = n.len; - e.ptr = key.ptr + n.len; - - mpz_init(this->n); - mpz_init(this->e); + der_decoder_t *dd; + status_t status; - mpz_import(this->n, n.len, 1, 1, 1, 0, n.ptr); - mpz_import(this->e, n.len, 1, 1, 1, 0, e.ptr); + dd = der_decoder_create(rsa_public_key_rules); - this->k = n.len; - - this->is_key_set = TRUE; - - return SUCCESS; -} + status = dd->decode(dd, key, this); + if (status == SUCCESS) + { + this->is_key_set = TRUE; + this->k = mpz_sizeinbase(this->n, 2) / 8; + } + dd->destroy(dd); + return status; +} /** @@ -347,11 +354,8 @@ static status_t save_key(private_rsa_public_key_t *this, char *file) */ static void destroy(private_rsa_public_key_t *this) { - if (this->is_key_set) - { - mpz_clear(this->n); - mpz_clear(this->e); - } + mpz_clear(this->n); + mpz_clear(this->e); allocator_free(this); } @@ -374,6 +378,8 @@ rsa_public_key_t *rsa_public_key_create() this->rsaep = rsaep; this->rsavp1 = rsaep; /* same algorithm */ + mpz_init(this->n); + mpz_init(this->e); this->is_key_set = FALSE; return &(this->public); |