簡體   English   中英

在 android .net 6.0 應用程序中讀取 X509 證書

[英]Read X509 certificate in android .net 6.0 application

我正在嘗試將我的 .net android 應用程序從“android xamarin 應用程序”遷移到 Z2D50972FCECD170620295495607. 我終於建立了,現在每次我從字節數組中讀取 X509 證書時它都會下降:

return new X509Certificate(bytes);

我確信證書是有效的,因為在遷移之前沒有問題。 異常的堆棧跟蹤是:

System.PlatformNotSupportedException: Cryptography_AlgorithmNotSupported, RC2
at System.Security.Cryptography.PasswordBasedEncryption.CreateRC2()
at System.Security.Cryptography.PasswordBasedEncryption.Decrypt(AlgorithmIdentifierAsn& algorithmIdentifier, ReadOnlySpan`1 password, ReadOnlySpan`1 passwordBytes, ReadOnlySpan`1 encryptedData, Span`1 destination)
Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptAndProcessSafeContents(ReadOnlySpan`1 password, CertBagAsn[]& certBags, AttributeAsn[][]& certBagAttrs, Int32& certBagIdx, SafeBagAsn[]& keyBags, Int32& keyBagIdx)
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
at Internal.Cryptography.Pal.UnixPkcs12Reader.VerifyAndDecrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
Exception_EndOfInnerExceptionStack
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
at Internal.Cryptography.Pal.AndroidCertificatePal.ReadPkcs12(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean ephemeralSpecified)
at Internal.Cryptography.Pal.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at Internal.Cryptography.Pal.CertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] data)
...

我試圖找到一些關於它的信息,只是發現我無法在 runtime/src/libraries/Common/src/Internal/Cryptography/Helpers.cs 文件中創建“RC2”:

#if NET5_0_OR_GREATER
        [UnsupportedOSPlatformGuard("android")]
        [UnsupportedOSPlatformGuard("browser")]
        public static bool IsRC2Supported => !OperatingSystem.IsAndroid();
#else
        public static bool IsRC2Supported => true;
#endif

所以問題是:
有誰知道為什么不支持此代碼?
我可以在我的 .net 6.0 android 應用程序中以某種方式使用 x509 證書嗎?

有誰知道為什么不支持此代碼?

.NET 6 for Android 使用 Android 內置加密提供程序,並且不包括 RC2。

現在每次我從字節數組中讀取 X509 證書時它都會下降

從技術上講,您的字節不是證書。 它們是 PFX,並且 PFX 中的證書是用 40 位 RC2 加密的(這種算法太弱了,它也可能是 ROT13,但這是 1990 年代美國加密法規的平衡行為......然后傳統)。

我可以在我的 .net 6.0 android 應用程序中以某種方式使用 x509 證書嗎?

If you load the PFX in .NET 6 for Windows, Linux, or macOS as exportable, then re-export it as a new PFX, it should work on Android; 因為所有現代堆棧都應該使用 3DES 而不是 RC2-40(他們仍在使用 3DES 而不是 AES,因為並非所有市場上的操作系統版本都將 AES 連接到其 PFX 閱讀器)。

X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Import(bytes, pwd, X509KeyStorageFlags.Exportable);
byte[] newPfx = coll.Export(X509ContentType.Pfx, pwd);

或者,如果您使用 OpenSSL 的命令行實用程序來構建您的 PFX,請將-certpbe 3DES添加到您的命令行(例如openssl pkcs12 -export -in cert.cer -inkey cert.key -certpbe 3DES -out cert.pfx )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM