Throw InvalidKeyException from ECDH_compute_key

Instead of throwing RuntimeException for ECDH_compute_key failures just
throw an InvalidKeyException since the higher-level API declares that as
a checked exception.

Bug: 21277055
Change-Id: Ieed26ad4f6cb76a49a7edb77bb3b1da7ae88043b
diff --git a/src/main/java/org/conscrypt/NativeCrypto.java b/src/main/java/org/conscrypt/NativeCrypto.java
index 0176699..933470d 100644
--- a/src/main/java/org/conscrypt/NativeCrypto.java
+++ b/src/main/java/org/conscrypt/NativeCrypto.java
@@ -217,7 +217,8 @@
             boolean enabled);
 
     public static native int ECDH_compute_key(byte[] out, int outOffset,
-            NativeRef.EVP_PKEY publicKeyRef, NativeRef.EVP_PKEY privateKeyRef);
+            NativeRef.EVP_PKEY publicKeyRef, NativeRef.EVP_PKEY privateKeyRef) throws
+            InvalidKeyException;
 
     // --- Message digest functions --------------
 
diff --git a/src/main/native/org_conscrypt_NativeCrypto.cpp b/src/main/native/org_conscrypt_NativeCrypto.cpp
index 6ae621b..c1cae98 100644
--- a/src/main/native/org_conscrypt_NativeCrypto.cpp
+++ b/src/main/native/org_conscrypt_NativeCrypto.cpp
@@ -4093,6 +4093,7 @@
     }
 
     if (pubPkey == NULL) {
+        JNI_TRACE("ECDH_compute_key(%p) => pubPkey == null", pubPkey);
         jniThrowNullPointerException(env, "pubPkey == null");
         return -1;
     }
@@ -4100,25 +4101,26 @@
     Unique_EC_KEY pubkey(EVP_PKEY_get1_EC_KEY(pubPkey));
     if (pubkey.get() == NULL) {
         JNI_TRACE("ECDH_compute_key(%p) => can't get public key", pubPkey);
-        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY public");
+        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY public", throwInvalidKeyException);
         return -1;
     }
 
     const EC_POINT* pubkeyPoint = EC_KEY_get0_public_key(pubkey.get());
     if (pubkeyPoint == NULL) {
         JNI_TRACE("ECDH_compute_key(%p) => can't get public key point", pubPkey);
-        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY public");
+        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY public", throwInvalidKeyException);
         return -1;
     }
 
     if (privPkey == NULL) {
+        JNI_TRACE("ECDH_compute_key(%p) => privKey == null", pubPkey);
         jniThrowNullPointerException(env, "privPkey == null");
         return -1;
     }
 
     Unique_EC_KEY privkey(EVP_PKEY_get1_EC_KEY(privPkey));
     if (privkey.get() == NULL) {
-        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY private");
+        throwExceptionIfNecessary(env, "EVP_PKEY_get1_EC_KEY private", throwInvalidKeyException);
         return -1;
     }
 
@@ -4130,10 +4132,12 @@
             NULL // No KDF
             );
     if (outputLength == -1) {
-        throwExceptionIfNecessary(env, "ECDH_compute_key");
+        JNI_TRACE("ECDH_compute_key(%p) => outputLength = -1", pubPkey);
+        throwExceptionIfNecessary(env, "ECDH_compute_key", throwInvalidKeyException);
         return -1;
     }
 
+    JNI_TRACE("ECDH_compute_key(%p) => outputLength=%d", pubPkey, outputLength);
     return outputLength;
 }