Fix some issues from consuming RSA and ECDiffieHellman in certs on Android
authorElinor Fung <elfung@microsoft.com>
Mon, 8 Mar 2021 20:39:21 +0000 (12:39 -0800)
committerGitHub <noreply@github.com>
Mon, 8 Mar 2021 20:39:21 +0000 (12:39 -0800)
src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanAndroid.Derive.cs [moved from src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECDiffieHellmanAndroid.Derive.cs with 100% similarity]
src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c
src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.h
src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Android/AndroidPkcs12Reader.cs
src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj

index 0acd535..b96db20 100644 (file)
@@ -13,7 +13,7 @@ PALEXPORT RSA* AndroidCryptoNative_RsaCreate()
     RSA* rsa = malloc(sizeof(RSA));
     rsa->privateKey = NULL;
     rsa->publicKey = NULL;
-    rsa->keyWidth = 0;
+    rsa->keyWidthInBits = 0;
     atomic_init(&rsa->refCount, 1);
     return rsa;
 }
@@ -129,7 +129,7 @@ PALEXPORT int32_t AndroidCryptoNative_RsaSize(RSA* rsa)
 {
     if (!rsa)
         return FAIL;
-    return rsa->keyWidth / 8;
+    return rsa->keyWidthInBits / 8;
 }
 
 PALEXPORT RSA* AndroidCryptoNative_DecodeRsaSubjectPublicKeyInfo(uint8_t* buf, int32_t len)
@@ -248,7 +248,7 @@ PALEXPORT int32_t AndroidCryptoNative_RsaGenerateKeyEx(RSA* rsa, int32_t bits)
 
     rsa->privateKey = ToGRef(env, (*env)->CallObjectMethod(env, keyPair, g_keyPairGetPrivateMethod));
     rsa->publicKey = ToGRef(env, (*env)->CallObjectMethod(env, keyPair, g_keyPairGetPublicMethod));
-    rsa->keyWidth = bits;
+    rsa->keyWidthInBits = bits;
 
     (*env)->DeleteLocalRef(env, rsaStr);
     (*env)->DeleteLocalRef(env, kpgObj);
@@ -332,7 +332,7 @@ PALEXPORT int32_t AndroidCryptoNative_SetRsaParameters(RSA* rsa,
     jobject nObj = AndroidCryptoNative_BigNumFromBinary(n, nLength);
     jobject eObj = AndroidCryptoNative_BigNumFromBinary(e, eLength);
 
-    rsa->keyWidth = nLength * 8;
+    rsa->keyWidthInBits = nLength * 8;
 
     jobject algName = JSTRING("RSA");
     jobject keyFactory = (*env)->CallStaticObjectMethod(env, g_KeyFactoryClass, g_KeyFactoryGetInstanceMethod, algName);
@@ -385,7 +385,7 @@ RSA* AndroidCryptoNative_NewRsaFromPublicKey(JNIEnv* env, jobject /*RSAPublicKey
 
     RSA* ret = AndroidCryptoNative_RsaCreate();
     ret->publicKey = AddGRef(env, key);
-    ret->keyWidth = AndroidCryptoNative_GetBigNumBytes(modulus);
+    ret->keyWidthInBits = AndroidCryptoNative_GetBigNumBytes(modulus) * 8;
 
     (*env)->DeleteLocalRef(env, modulus);
     return ret;
index fa08c61..2bba552 100644 (file)
@@ -18,7 +18,7 @@ typedef struct RSA
     jobject privateKey; // RSAPrivateCrtKey
     jobject publicKey;  // RSAPublicCrtKey
     atomic_int refCount;
-    int32_t keyWidth;
+    int32_t keyWidthInBits;
 } RSA;
 
 #define CIPHER_ENCRYPT_MODE 1
index 32c81f8..e7c4bfa 100644 (file)
              Link="Common\System\Security\Cryptography\ECAndroid.ImportExport.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroid.cs"
              Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroid.cs" />
+    <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs"
+             Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroidPublicKey.cs"
              Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroidPublicKey.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDsaAndroid.cs"
     <Compile Include="Internal\Cryptography\RC2Implementation.Android.cs" />
     <Compile Include="System\Security\Cryptography\AesCcm.Android.cs" />
     <Compile Include="System\Security\Cryptography\AesGcm.Android.cs" />
-    <Compile Include="System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs" />
     <Compile Include="System\Security\Cryptography\ECDiffieHellman.Create.Android.cs" />
     <Compile Include="System\Security\Cryptography\ECDsa.Create.Android.cs" />
     <Compile Include="System\Security\Cryptography\RSA.Create.Android.cs" />
index 19f7397..943b9dc 100644 (file)
@@ -53,8 +53,10 @@ namespace Internal.Cryptography.Pal
             switch (algorithm)
             {
                 case Oids.Rsa:
+                    key = new RSAImplementation.RSAAndroid();
+                    break;
                 case Oids.Dsa:
-                    // TODO: [AndroidCrypto] Handle RSA / DSA
+                    // TODO: [AndroidCrypto] Handle DSA
                     throw new NotImplementedException($"{nameof(LoadKey)} ({algorithm})");
                 case Oids.EcDiffieHellman:
                 case Oids.EcPublicKey:
@@ -78,7 +80,12 @@ namespace Internal.Cryptography.Pal
                 return ecdsa.DuplicateKeyHandle();
             }
 
-            // TODO: [AndroidCrypto] Handle RSA / DSA
+            if (key is RSAImplementation.RSAAndroid rsa)
+            {
+                return rsa.DuplicateKeyHandle();
+            }
+
+            // TODO: [AndroidCrypto] Handle DSA
             throw new NotImplementedException($"{nameof(GetPrivateKey)} ({key.GetType()})");
         }
     }
index b2e55f5..d654599 100644 (file)
              Link="Common\Interop\Android\Interop.JObjectLifetime.cs" />
     <Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\Interop.Bignum.cs"
              Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\Interop.Bignum.cs" />
+    <Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\Interop.Ecdh.cs"
+             Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\Interop.Ecdh.cs" />
     <Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\Interop.EcDsa.cs"
              Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\Interop.EcDsa.cs" />
     <Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\Interop.EcDsa.ImportExport.cs"
              Link="Common\System\Security\Cryptography\ECAndroid.ImportExport.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroid.cs"
              Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroid.cs" />
+    <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs"
+             Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanAndroidPublicKey.cs"
              Link="Common\System\Security\Cryptography\ECDiffieHellmanAndroidPublicKey.cs" />
+    <Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanDerivation.cs"
+             Link="Common\System\Security\Cryptography\ECDiffieHellmanDerivation.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\ECDsaAndroid.cs"
              Link="Common\System\Security\Cryptography\ECDsaAndroid.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\RSAAndroid.cs"