From 0b9aea9d4745ac35faa0c3eec117758fc1acb40e Mon Sep 17 00:00:00 2001
From: Divya Theja <divyathe@codeaurora.org>
Date: Tue, 7 May 2019 16:35:48 +0530
Subject: [PATCH] openssl-1.1.1* compatibility.

   * RSA structure is opaque now and henceforth direct references to
     members of RSA structure is illegal.
   * void RSA_get0_key(const RSA *r,const BIGNUM **n, const BIGNUM **e,
                       const BIGNUM **d) to collect n, e and d values
     from r.
   * int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
     to backfill n ,e, d from r. On success, returns 1.

   * Refer https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes for
     a detailed discussion of APIs.
---
 android_pubkey.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/android_pubkey.c b/android_pubkey.c
index 3052e52..2d89178 100644
--- a/android_pubkey.c
+++ b/android_pubkey.c
@@ -66,6 +66,8 @@ bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key) {
   bool ret = false;
   uint8_t modulus_buffer[ANDROID_PUBKEY_MODULUS_SIZE];
   RSA* new_key = RSA_new();
+  BIGNUM* new_key_n;
+  BIGNUM* new_key_e;
   if (!new_key) {
     goto cleanup;
   }
@@ -81,14 +83,14 @@ bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key) {
   // 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) {
+  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)) {
+  new_key_e = BN_new();
+  if (!new_key_e || !BN_set_word(new_key_e, key_struct->exponent)) {
     goto cleanup;
   }

@@ -100,6 +102,7 @@ bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key) {
   // be added here if/when we want the additional speedup from using the
   // pre-computed montgomery parameters.

+  RSA_set0_key(new_key, new_key_n, new_key_e, NULL);
   *key = new_key;
   ret = true;

@@ -126,37 +129,40 @@ bool android_pubkey_encode(const RSA* key, uint8_t* key_buffer, size_t size) {
   BIGNUM* r32 = BN_new();
   BIGNUM* n0inv = BN_new();
   BIGNUM* rr = BN_new();
+  BIGNUM* n;
+  BIGNUM* e;

   if (sizeof(RSAPublicKey) > size ||
       RSA_size(key) != ANDROID_PUBKEY_MODULUS_SIZE) {
     goto cleanup;
   }
+  RSA_get0_key(key, &n, &e, NULL);

   // Store the modulus size.
   key_struct->modulus_size_words = ANDROID_PUBKEY_MODULUS_SIZE_WORDS;

   // 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, 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(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, 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(e);

   ret = true;

--
1.9.1

