Make crypto_pk_cmp_keys do something sane for NULL keys

Fixes bug 4283; bugfix on r76
(Git commit 01aadefbfc).
This commit is contained in:
Robert Ransom 2012-09-15 02:47:14 -07:00 committed by Nick Mathewson
parent 32d9cea289
commit f3916a6855

View File

@ -748,19 +748,23 @@ crypto_pk_public_exponent_ok(crypto_pk_t *env)
return BN_is_word(env->key->e, 65537); return BN_is_word(env->key->e, 65537);
} }
/** Compare the public-key components of a and b. Return -1 if a\<b, 0 /** Compare the public-key components of a and b. Return -1 if a\<b,
* if a==b, and 1 if a\>b. * 0 if a==b, and 1 if a\>b. A NULL key is considered to be less than
* all non-NULL keys, and equal to itself.
*
* Note that this may leak information about the keys through timing.
*/ */
int int
crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b) crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b)
{ {
int result; int result;
char a_is_non_null = (a != NULL) && (a->key != NULL);
char b_is_non_null = (b != NULL) && (b->key != NULL);
char an_argument_is_null = !a_is_non_null | !b_is_non_null;
if (!a || !b) result = tor_memcmp(&a_is_non_null, &b_is_non_null, sizeof(a_is_non_null));
return -1; if (an_argument_is_null)
return result;
if (!a->key || !b->key)
return -1;
tor_assert(PUBLIC_KEY_OK(a)); tor_assert(PUBLIC_KEY_OK(a));
tor_assert(PUBLIC_KEY_OK(b)); tor_assert(PUBLIC_KEY_OK(b));