--- platform/system/core/libcrypto_utils/android_pubkey.c.omv~ 2017-09-02 14:38:59.204591842 +0200 +++ platform/system/core/libcrypto_utils/android_pubkey.c 2017-09-02 14:58:51.204164288 +0200 @@ -81,16 +81,17 @@ bool android_pubkey_decode(const uint8_t // Convert the modulus to big-endian byte order as expected by BN_bin2bn. memcpy(modulus_buffer, key_struct->modulus, sizeof(modulus_buffer)); reverse_bytes(modulus_buffer, sizeof(modulus_buffer)); - new_key->n = BN_bin2bn(modulus_buffer, sizeof(modulus_buffer), NULL); - if (!new_key->n) { + BIGNUM *new_key_n = BN_bin2bn(modulus_buffer, sizeof(modulus_buffer), NULL); + if (!new_key_n) { goto cleanup; } // Read the exponent. - new_key->e = BN_new(); - if (!new_key->e || !BN_set_word(new_key->e, key_struct->exponent)) { + BIGNUM *new_key_e = BN_new(); + if (!new_key_e || !BN_set_word(new_key_e, key_struct->exponent)) { goto cleanup; } + RSA_set0_key(new_key, new_key_n, new_key_e, NULL); // Note that we don't extract the montgomery parameters n0inv and rr from // the RSAPublicKey structure. They assume a word size of 32 bits, but @@ -111,7 +112,11 @@ cleanup: } static bool android_pubkey_encode_bignum(const BIGNUM* num, uint8_t* buffer) { +#ifdef OPENSSL_IS_BORINGSSL if (!BN_bn2bin_padded(buffer, ANDROID_PUBKEY_MODULUS_SIZE, num)) { +#else + if (!BN_bn2binpad(num, buffer, ANDROID_PUBKEY_MODULUS_SIZE)) { +#endif return false; } @@ -135,28 +140,30 @@ bool android_pubkey_encode(const RSA* ke // Store the modulus size. key_struct->modulus_size_words = ANDROID_PUBKEY_MODULUS_SIZE_WORDS; + const BIGNUM *key_n, *key_e; + RSA_get0_key(key, &key_n, &key_e, NULL); // Compute and store n0inv = -1 / N[0] mod 2^32. if (!ctx || !r32 || !n0inv || !BN_set_bit(r32, 32) || - !BN_mod(n0inv, key->n, r32, ctx) || + !BN_mod(n0inv, key_n, r32, ctx) || !BN_mod_inverse(n0inv, n0inv, r32, ctx) || !BN_sub(n0inv, r32, n0inv)) { goto cleanup; } key_struct->n0inv = (uint32_t)BN_get_word(n0inv); // Store the modulus. - if (!android_pubkey_encode_bignum(key->n, key_struct->modulus)) { + if (!android_pubkey_encode_bignum(key_n, key_struct->modulus)) { goto cleanup; } // Compute and store rr = (2^(rsa_size)) ^ 2 mod N. if (!ctx || !rr || !BN_set_bit(rr, ANDROID_PUBKEY_MODULUS_SIZE * 8) || - !BN_mod_sqr(rr, rr, key->n, ctx) || + !BN_mod_sqr(rr, rr, key_n, ctx) || !android_pubkey_encode_bignum(rr, key_struct->rr)) { goto cleanup; } // Store the exponent. - key_struct->exponent = (uint32_t)BN_get_word(key->e); + key_struct->exponent = (uint32_t)BN_get_word(key_e); ret = true;