botan: Fixes, code style changes plus some refactorings

Some changes rely on newly added FFI functions in Botan's master
branch.
This commit is contained in:
Tobias Brunner
2018-09-12 16:25:00 +02:00
parent 13f113f7a9
commit de2a24310c
19 changed files with 694 additions and 1092 deletions
@@ -39,6 +39,7 @@ typedef struct private_botan_rsa_private_key_t private_botan_rsa_private_key_t;
* Private data of a botan_rsa_private_key_t object.
*/
struct private_botan_rsa_private_key_t {
/**
* Public interface for this signer.
*/
@@ -49,7 +50,6 @@ struct private_botan_rsa_private_key_t {
*/
botan_privkey_t key;
/**
* reference count
*/
@@ -59,32 +59,23 @@ struct private_botan_rsa_private_key_t {
/**
* Get the binary representation of a named RSA parameter
*/
static int botan_rsa_get_field(botan_privkey_t *key, const char *field_name,
chunk_t *value)
static bool get_rsa_field(botan_privkey_t *key, const char *field_name,
chunk_t *value)
{
botan_mp_t field;
size_t field_size = 0;
if (botan_mp_init(&field))
{
return -1;
return FALSE;
}
if (botan_privkey_get_field(field, *key, field_name))
if (botan_privkey_get_field(field, *key, field_name) ||
botan_mp_num_bytes(field, &field_size) ||
!field_size)
{
botan_mp_destroy(field);
return -1;
}
size_t field_size = 0;
if (botan_mp_num_bytes(field, &field_size))
{
botan_mp_destroy(field);
return -1;
}
if (field_size == 0)
{
botan_mp_destroy(field);
return -1;
return FALSE;
}
*value = chunk_alloc(field_size);
@@ -92,105 +83,9 @@ static int botan_rsa_get_field(botan_privkey_t *key, const char *field_name,
{
botan_mp_destroy(field);
chunk_clear(value);
return -1;
return FALSE;
}
botan_mp_destroy(field);
return 0;
}
/**
* Build RSA signature
*/
static bool build_rsa_signature(private_botan_rsa_private_key_t *this,
const char* hash_and_padding, chunk_t data, chunk_t* signature)
{
botan_pk_op_sign_t sign_op;
if (botan_pk_op_sign_create(&sign_op, this->key, hash_and_padding, 0))
{
return FALSE;
}
botan_rng_t rng;
if (botan_rng_init(&rng, "user"))
{
botan_pk_op_sign_destroy(sign_op);
return FALSE;
}
/* get size of signature first */
if (botan_pk_op_sign_update(sign_op, data.ptr, data.len))
{
botan_rng_destroy(rng);
botan_pk_op_sign_destroy(sign_op);
return FALSE;
}
signature->len = 0;
if (botan_pk_op_sign_finish(sign_op, rng, NULL, &signature->len)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
{
botan_rng_destroy(rng);
botan_pk_op_sign_destroy(sign_op);
return FALSE;
}
/* now get the signature */
*signature = chunk_alloc(signature->len);
if (botan_pk_op_sign_update(sign_op, data.ptr, data.len))
{
chunk_free(signature);
botan_rng_destroy(rng);
botan_pk_op_sign_destroy(sign_op);
return FALSE;
}
if (botan_pk_op_sign_finish(sign_op, rng, signature->ptr, &signature->len))
{
chunk_free(signature);
botan_rng_destroy(rng);
botan_pk_op_sign_destroy(sign_op);
return FALSE;
}
botan_rng_destroy(rng);
botan_pk_op_sign_destroy(sign_op);
return TRUE;
}
/**
* Build an EMSA PKCS1 signature described in PKCS#1
*/
static bool build_emsa_pkcs1_signature(private_botan_rsa_private_key_t *this,
const char* hash_and_padding, chunk_t data, chunk_t* signature)
{
return build_rsa_signature(this, hash_and_padding, data, signature);
}
static bool botan_get_hash(hash_algorithm_t hash, char* hash_str)
{
switch (hash)
{
case HASH_SHA1:
sprintf(hash_str, "SHA-1");
break;
case HASH_SHA224:
sprintf(hash_str, "SHA-224");
break;
case HASH_SHA256:
sprintf(hash_str, "SHA-256");
break;
case HASH_SHA384:
sprintf(hash_str, "SHA-384");
break;
case HASH_SHA512:
sprintf(hash_str, "SHA-512");
break;
default:
return FALSE;
}
return TRUE;
}
@@ -201,10 +96,8 @@ static bool build_emsa_pss_signature(private_botan_rsa_private_key_t *this,
rsa_pss_params_t *params, chunk_t data,
chunk_t *sig)
{
char* hash_and_padding, *hash, *mgf1_hash;
char* salt_len = NULL;
size_t len;
bool success = FALSE;
const char *hash;
char hash_and_padding[BUF_LEN];
if (!params)
{
@@ -218,50 +111,23 @@ static bool build_emsa_pss_signature(private_botan_rsa_private_key_t *this,
return FALSE;
}
hash = malloc(8);
if (!botan_get_hash(params->hash, hash))
hash = botan_get_hash(params->hash);
if (!hash)
{
free(hash);
return FALSE;
}
mgf1_hash = malloc(8);
if (!botan_get_hash(params->mgf1_hash, mgf1_hash))
{
free(hash);
free(mgf1_hash);
return FALSE;
}
if (params->salt_len > RSA_PSS_SALT_LEN_DEFAULT)
{
salt_len = malloc(6);
snprintf(salt_len, 5, "%d", params->salt_len);
}
len = 24 + strlen(hash) + strlen(mgf1_hash);
hash_and_padding = malloc(len+1);
if (salt_len)
{
snprintf(hash_and_padding, len, "EMSA-PSS(%s,MGF1,%s)", hash, salt_len);
snprintf(hash_and_padding, sizeof(hash_and_padding),
"EMSA-PSS(%s,MGF1,%u)", hash, params->salt_len);
}
else
{
snprintf(hash_and_padding, len, "EMSA-PSS(%s,MGF1)", hash);
snprintf(hash_and_padding, sizeof(hash_and_padding),
"EMSA-PSS(%s,MGF1)", hash);
}
if (build_rsa_signature(this, hash_and_padding, data, sig))
{
success = TRUE;
}
if (salt_len)
free(salt_len);
free(hash);
free(mgf1_hash);
free(hash_and_padding);
return success;
return botan_get_signature(this->key, hash_and_padding, data, sig);
}
METHOD(private_key_t, get_type, key_type_t,
@@ -277,23 +143,23 @@ METHOD(private_key_t, sign, bool,
switch (scheme)
{
case SIGN_RSA_EMSA_PKCS1_NULL:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(Raw)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(Raw)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA1:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(SHA-1)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(SHA-1)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_224:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(SHA-224)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(SHA-224)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_256:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(SHA-256)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(SHA-256)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_384:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(SHA-384)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(SHA-384)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_512:
return build_emsa_pkcs1_signature(this, "EMSA_PKCS1(SHA-512)", data,
signature);
return botan_get_signature(this->key, "EMSA_PKCS1(SHA-512)", data,
signature);
case SIGN_RSA_EMSA_PSS:
return build_emsa_pss_signature(this, params, data, signature);
default:
@@ -303,9 +169,11 @@ METHOD(private_key_t, sign, bool,
}
}
METHOD(private_key_t, decrypt, bool, private_botan_rsa_private_key_t *this,
encryption_scheme_t scheme, chunk_t crypto, chunk_t *plain)
METHOD(private_key_t, decrypt, bool,
private_botan_rsa_private_key_t *this, encryption_scheme_t scheme,
chunk_t crypto, chunk_t *plain)
{
botan_pk_op_decrypt_t decrypt_op;
const char *padding;
switch (scheme)
@@ -334,26 +202,18 @@ METHOD(private_key_t, decrypt, bool, private_botan_rsa_private_key_t *this,
return FALSE;
}
botan_pk_op_decrypt_t decrypt_op;
if (botan_pk_op_decrypt_create(&decrypt_op, this->key, padding, 0))
{
return FALSE;
}
/*
* get size of plaintext first
*/
if (botan_pk_op_decrypt(decrypt_op, NULL, &plain->len, crypto.ptr,
crypto.len)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
plain->len = 0;
if (botan_pk_op_decrypt_output_length(decrypt_op, crypto.len, &plain->len))
{
botan_pk_op_decrypt_destroy(decrypt_op);
return FALSE;
}
/*
* now get the plaintext
*/
*plain = chunk_alloc(plain->len);
if (botan_pk_op_decrypt(decrypt_op, plain->ptr, &plain->len, crypto.ptr,
crypto.len))
@@ -362,7 +222,6 @@ METHOD(private_key_t, decrypt, bool, private_botan_rsa_private_key_t *this,
botan_pk_op_decrypt_destroy(decrypt_op);
return FALSE;
}
botan_pk_op_decrypt_destroy(decrypt_op);
return TRUE;
}
@@ -371,21 +230,18 @@ METHOD(private_key_t, get_keysize, int,
private_botan_rsa_private_key_t *this)
{
botan_mp_t n;
size_t bits = 0;
if (botan_mp_init(&n))
{
return -1;
return 0;
}
if (botan_privkey_rsa_get_n(n, this->key))
{
return -1;
}
size_t bits = 0;
if (botan_mp_num_bits(n, &bits))
if (botan_privkey_rsa_get_n(n, this->key) ||
botan_mp_num_bits(n, &bits))
{
botan_mp_destroy(n);
return -1;
return 0;
}
botan_mp_destroy(n);
@@ -395,22 +251,23 @@ METHOD(private_key_t, get_keysize, int,
METHOD(private_key_t, get_public_key, public_key_t*,
private_botan_rsa_private_key_t *this)
{
public_key_t *pub_key;
chunk_t n, e;
if (botan_rsa_get_field(&this->key, "n", &n))
if (!get_rsa_field(&this->key, "n", &n))
{
return NULL;
}
if (botan_rsa_get_field(&this->key, "e", &e))
if (!get_rsa_field(&this->key, "e", &e))
{
chunk_clear(&n);
chunk_free(&n);
return NULL;
}
public_key_t *pub_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
KEY_RSA, BUILD_RSA_MODULUS, n,
BUILD_RSA_PUB_EXP, e, BUILD_END);
pub_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
BUILD_RSA_MODULUS, n, BUILD_RSA_PUB_EXP, e,
BUILD_END);
chunk_free(&n);
chunk_free(&e);
@@ -421,32 +278,22 @@ METHOD(private_key_t, get_fingerprint, bool,
private_botan_rsa_private_key_t *this, cred_encoding_type_t type,
chunk_t *fingerprint)
{
chunk_t n, e;
bool success;
botan_pubkey_t pubkey;
bool success = FALSE;
if (lib->encoding->get_cache(lib->encoding, type, &this->key, fingerprint))
/* check the cache before doing the export */
if (lib->encoding->get_cache(lib->encoding, type, this, fingerprint))
{
return TRUE;
}
if (botan_rsa_get_field(&this->key, "n", &n))
if (botan_privkey_export_pubkey(&pubkey, this->key))
{
return FALSE;
}
if (botan_rsa_get_field(&this->key, "e", &e))
{
chunk_clear(&n);
return FALSE;
}
success = lib->encoding->encode(lib->encoding, type, &this->key,
fingerprint, CRED_PART_RSA_MODULUS, n,
CRED_PART_RSA_PUB_EXP, e, CRED_PART_END);
chunk_free(&n);
chunk_free(&e);
success = botan_get_fingerprint(pubkey, this, type, fingerprint);
botan_pubkey_destroy(pubkey);
return success;
}
METHOD(private_key_t, get_encoding, bool,
@@ -458,27 +305,28 @@ METHOD(private_key_t, get_encoding, bool,
case PRIVKEY_ASN1_DER:
case PRIVKEY_PEM:
{
uint32_t format = BOTAN_PRIVKEY_EXPORT_FLAG_DER;
size_t len = 0;
bool success = TRUE;
uint32_t format = BOTAN_PRIVKEY_EXPORT_FLAG_DER;
if (type == PRIVKEY_PEM)
{
format = BOTAN_PRIVKEY_EXPORT_FLAG_PEM;
}
size_t bits = 0;
if(botan_privkey_rsa_get_privkey(this->key, NULL, &bits, format))
if (botan_privkey_rsa_get_privkey(this->key, NULL, &len, format)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
{
return FALSE;
}
*encoding = chunk_alloc(bits);
if(botan_privkey_rsa_get_privkey(this->key, encoding->ptr, &bits, format))
*encoding = chunk_alloc(len);
if (botan_privkey_rsa_get_privkey(this->key, encoding->ptr, &len,
format))
{
chunk_clear(encoding);
return FALSE;
}
return success;
}
default:
@@ -498,11 +346,8 @@ METHOD(private_key_t, destroy, void,
{
if (ref_put(&this->ref))
{
if (&this->key)
{
lib->encoding->clear_cache(lib->encoding, &this->key);
botan_privkey_destroy(this->key);
}
lib->encoding->clear_cache(lib->encoding, this);
botan_privkey_destroy(this->key);
free(this);
}
}
@@ -538,13 +383,13 @@ static private_botan_rsa_private_key_t *create_empty()
}
/*
* See header.
* Described in header
*/
botan_rsa_private_key_t *botan_rsa_private_key_gen(key_type_t type,
va_list args)
{
private_botan_rsa_private_key_t *this;
botan_rng_t rng;
u_int key_size = 0;
while (TRUE)
@@ -567,21 +412,19 @@ botan_rsa_private_key_t *botan_rsa_private_key_gen(key_type_t type,
return NULL;
}
botan_rng_t rng;
if (botan_rng_init(&rng, "user"))
if (botan_rng_init(&rng, "system"))
{
return NULL;
}
this = create_empty();
if(botan_privkey_create_rsa(&this->key, rng, key_size))
if (botan_privkey_create_rsa(&this->key, rng, key_size))
{
botan_rng_destroy(rng);
destroy(this);
free(this);
return NULL;
}
botan_rng_destroy(rng);
return &this->public;
}
@@ -593,52 +436,35 @@ botan_rsa_private_key_t *botan_rsa_private_key_gen(key_type_t type,
static bool calculate_pq(botan_mp_t *n, botan_mp_t *e, botan_mp_t *d,
botan_mp_t *p, botan_mp_t *q)
{
botan_mp_t k, one, r, zero, two, n1, x, y, g, rem;
botan_mp_t k = NULL, one = NULL, r = NULL, zero = NULL, two = NULL;
botan_mp_t n1 = NULL, x = NULL, y = NULL, g = NULL, rem = NULL;
botan_rng_t rng = NULL;
int i, t, j;
bool success = TRUE;
bool success = FALSE;
if (botan_mp_init(&k))
if (botan_mp_init(&k) ||
botan_mp_init(&one) ||
botan_mp_set_from_int(one, 1))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&one))
{
success = FALSE;
goto error;
}
if (botan_mp_set_from_int(one, 1))
{
success = FALSE;
goto error;
}
/* 1. k = de - 1 */
/* 1. k = d * e - 1 */
if (botan_mp_mul(k, *d, *e) || botan_mp_sub(k, k, one))
{
success = FALSE;
goto error;
}
/* k must be even */
if (!botan_mp_is_even(k))
{
success = FALSE;
goto error;
}
/* 2. k = 2^t * r, where r is the largest odd integer dividing k, and t >= 1 */
if (botan_mp_init(&r))
if (botan_mp_init(&r) ||
botan_mp_set_from_mp(r, k))
{
success = FALSE;
goto error;
}
if (botan_mp_set_from_mp(r, k))
{
success = FALSE;
goto error;
}
@@ -646,58 +472,41 @@ static bool calculate_pq(botan_mp_t *n, botan_mp_t *e, botan_mp_t *d,
{
if (botan_mp_rshift(r, r, 1))
{
success = FALSE;
goto error;
}
}
/* need 0, 2, n-1 below */
if (botan_mp_init(&zero))
/* need 0 and n-1 below */
if (botan_mp_init(&zero) ||
botan_mp_init(&n1) ||
botan_mp_sub(n1, *n, one))
{
success = FALSE;
goto error;
}
if (botan_mp_set_from_int(zero, 0))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&n1))
{
success = FALSE;
goto error;
}
if (botan_mp_sub(n1, *n, one))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&g))
{
success = FALSE;
goto error;
}
botan_rng_t rng;
if (botan_rng_init(&rng, "user"))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&two))
{
success = FALSE;
goto error;
}
if (botan_mp_set_from_int(two, 2))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&y) ||
botan_mp_init(&x))
{
goto error;
}
@@ -706,20 +515,11 @@ static bool calculate_pq(botan_mp_t *n, botan_mp_t *e, botan_mp_t *d,
/* 3a. generate a random integer g in the range [0, n-1] */
if (botan_mp_rand_range(g, rng, zero, n1))
{
success = FALSE;
goto error;
}
/* 3b. y = g^r mod n */
if (botan_mp_init(&y))
{
success = FALSE;
goto error;
}
if (botan_mp_powmod(y, g, r, *n))
{
success = FALSE;
goto error;
}
@@ -729,18 +529,11 @@ static bool calculate_pq(botan_mp_t *n, botan_mp_t *e, botan_mp_t *d,
continue;
}
if (botan_mp_init(&x))
{
success = FALSE;
goto error;
}
for (j = 0; j < t; j++)
{
/* x = y^2 mod n */
if (botan_mp_powmod(x, y, two, *n))
{
success = FALSE;
goto error;
}
@@ -757,9 +550,8 @@ static bool calculate_pq(botan_mp_t *n, botan_mp_t *e, botan_mp_t *d,
}
/* let y = x */
if(botan_mp_set_from_mp(y, x))
if (botan_mp_set_from_mp(y, x))
{
success = FALSE;
goto error;
}
}
@@ -769,53 +561,36 @@ done:
/* 5. p = GCD(y – 1, n) and q = n/p */
if (botan_mp_sub(y, y, one))
{
success = FALSE;
goto error;
}
if (botan_mp_init(p))
if (botan_mp_init(p) ||
botan_mp_gcd(*p, y, *n))
{
success = FALSE;
goto error;
}
if (botan_mp_gcd(*p, y, *n))
if (botan_mp_init(q) ||
botan_mp_init(&rem) ||
botan_mp_div(*q, rem, *n, *p))
{
success = FALSE;
goto error;
}
if (botan_mp_init(q))
{
success = FALSE;
goto error;
}
if (botan_mp_init(&rem))
{
success = FALSE;
goto error;
}
if (botan_mp_div(*q, rem, *n, *p))
{
success = FALSE;
goto error;
}
if (!botan_mp_is_zero(rem))
{
success = FALSE;
goto error;
}
success = TRUE;
error:
if (!success)
{
botan_mp_destroy(*p);
botan_mp_destroy(*q);
}
botan_rng_destroy(rng);
botan_mp_destroy(k);
botan_mp_destroy(one);
botan_mp_destroy(r);
@@ -824,12 +599,13 @@ error:
botan_mp_destroy(n1);
botan_mp_destroy(x);
botan_mp_destroy(y);
botan_mp_destroy(g);
botan_mp_destroy(rem);
return success;
}
/*
* See header
* Described in header
*/
botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
va_list args)
@@ -880,38 +656,37 @@ botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
if (botan_privkey_load_rsa_pkcs1(&this->key, blob.ptr, blob.len))
{
destroy(this);
free(this);
return NULL;
}
return &this->public;
}
if (n.ptr && e.ptr && d.ptr)
{
botan_mp_t n_mp, e_mp, d_mp;
if (chunk_to_botan_mp(n, &n_mp))
botan_mp_t n_mp, e_mp, d_mp, p_mp, q_mp;
if (!chunk_to_botan_mp(n, &n_mp))
{
return NULL;
}
if (chunk_to_botan_mp(e, &e_mp))
if (!chunk_to_botan_mp(e, &e_mp))
{
botan_mp_destroy(n_mp);
return NULL;
}
if (chunk_to_botan_mp(d, &d_mp))
if (!chunk_to_botan_mp(d, &d_mp))
{
botan_mp_destroy(n_mp);
botan_mp_destroy(e_mp);
return NULL;
}
botan_mp_t p_mp, q_mp;
if (p.ptr && q.ptr)
{
if (chunk_to_botan_mp(p, &p_mp))
if (!chunk_to_botan_mp(p, &p_mp))
{
botan_mp_destroy(n_mp);
botan_mp_destroy(e_mp);
@@ -919,7 +694,7 @@ botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
return NULL;
}
if (chunk_to_botan_mp(q, &q_mp))
if (!chunk_to_botan_mp(q, &q_mp))
{
botan_mp_destroy(n_mp);
botan_mp_destroy(e_mp);
@@ -930,7 +705,7 @@ botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
}
else
{
// calculate p,q from n, e, d
/* calculate p,q from n, e, d */
if (!calculate_pq(&n_mp, &e_mp, &d_mp, &p_mp, &q_mp))
{
botan_mp_destroy(n_mp);
@@ -939,6 +714,8 @@ botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
return NULL;
}
}
botan_mp_destroy(n_mp);
botan_mp_destroy(d_mp);
this = create_empty();
@@ -947,7 +724,7 @@ botan_rsa_private_key_t *botan_rsa_private_key_load(key_type_t type,
botan_mp_destroy(e_mp);
botan_mp_destroy(p_mp);
botan_mp_destroy(q_mp);
destroy(this);
free(this);
return NULL;
}