From ab6d047405cc97793f3a37c56c037f27dada7fad Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Mon, 19 May 2014 23:19:08 +0300 Subject: [PATCH] Add function for building RSA public key from n and e parameters This is similar to the existing functionality that parsed ASN.1-encoded RSA public key by generating a similar public key instance from already parsed n and e parameters. Signed-off-by: Jouni Malinen --- src/crypto/crypto.h | 4 ++++ src/crypto/crypto_internal-rsa.c | 9 +++++++++ src/tls/rsa.c | 25 ++++++++++++++++++++++++- src/tls/rsa.h | 3 +++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 4caa277de..f2d5662ff 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -271,6 +271,10 @@ struct crypto_private_key; */ struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len); +struct crypto_public_key * +crypto_public_key_import_parts(const u8 *n, size_t n_len, + const u8 *e, size_t e_len); + /** * crypto_private_key_import - Import an RSA private key * @key: Key buffer (DER encoded RSA private key) diff --git a/src/crypto/crypto_internal-rsa.c b/src/crypto/crypto_internal-rsa.c index 54209fad3..dc7f350af 100644 --- a/src/crypto/crypto_internal-rsa.c +++ b/src/crypto/crypto_internal-rsa.c @@ -26,6 +26,15 @@ struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) } +struct crypto_public_key * +crypto_public_key_import_parts(const u8 *n, size_t n_len, + const u8 *e, size_t e_len) +{ + return (struct crypto_public_key *) + crypto_rsa_import_public_key_parts(n, n_len, e, e_len); +} + + struct crypto_private_key * crypto_private_key_import(const u8 *key, size_t len, const char *passwd) diff --git a/src/tls/rsa.c b/src/tls/rsa.c index 125c4205b..0b7b530bc 100644 --- a/src/tls/rsa.c +++ b/src/tls/rsa.c @@ -1,6 +1,6 @@ /* * RSA - * Copyright (c) 2006, Jouni Malinen + * Copyright (c) 2006-2014, Jouni Malinen * * This software may be distributed under the terms of the BSD license. * See README for more details. @@ -116,6 +116,29 @@ error: } +struct crypto_rsa_key * +crypto_rsa_import_public_key_parts(const u8 *n, size_t n_len, + const u8 *e, size_t e_len) +{ + struct crypto_rsa_key *key; + + key = os_zalloc(sizeof(*key)); + if (key == NULL) + return NULL; + + key->n = bignum_init(); + key->e = bignum_init(); + if (key->n == NULL || key->e == NULL || + bignum_set_unsigned_bin(key->n, n, n_len) < 0 || + bignum_set_unsigned_bin(key->e, e, e_len) < 0) { + crypto_rsa_free(key); + return NULL; + } + + return key; +} + + /** * crypto_rsa_import_private_key - Import an RSA private key * @buf: Key buffer (DER encoded RSA private key) diff --git a/src/tls/rsa.h b/src/tls/rsa.h index c236a9df4..b65818ee1 100644 --- a/src/tls/rsa.h +++ b/src/tls/rsa.h @@ -14,6 +14,9 @@ struct crypto_rsa_key; struct crypto_rsa_key * crypto_rsa_import_public_key(const u8 *buf, size_t len); struct crypto_rsa_key * +crypto_rsa_import_public_key_parts(const u8 *n, size_t n_len, + const u8 *e, size_t e_len); +struct crypto_rsa_key * crypto_rsa_import_private_key(const u8 *buf, size_t len); size_t crypto_rsa_get_modulus_len(struct crypto_rsa_key *key); int crypto_rsa_exptmod(const u8 *in, size_t inlen, u8 *out, size_t *outlen,