Skip to content

Commit

Permalink
Fix some issues from consuming RSA and ECDiffieHellman in certs on An…
Browse files Browse the repository at this point in the history
…droid
  • Loading branch information
elinor-fung authored Mar 8, 2021
1 parent df7a4e3 commit ecc354a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@
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"
Expand All @@ -647,7 +649,6 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ protected override AsymmetricAlgorithm LoadKey(ReadOnlyMemory<byte> pkcs8)
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:
Expand All @@ -78,7 +80,12 @@ internal static SafeKeyHandle GetPrivateKey(AsymmetricAlgorithm key)
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()})");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@
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"
Expand All @@ -430,8 +432,12 @@
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"
Expand Down

0 comments on commit ecc354a

Please sign in to comment.