diff options
author | Martin Baulig <mabaul@microsoft.com> | 2019-02-26 02:37:37 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2019-02-26 12:18:54 +0300 |
commit | ef7e64a5beb6d6ccc4e44f0dedfd65550e7dbbab (patch) | |
tree | 565973ba217be2fa73516894d05d11ffa2b6c313 /mcs/class/Mono.Security | |
parent | 07287648b2fab401debcee787a8cc520b8dbd8d6 (diff) |
[Mono.Security]: minor `CryptoConvert` changes to make it more linker-friendly.
* Add new internal `CryptoConvert.TryImportCapiPrivateKeyBlob()`
method that's using `RSAManaged` directly to make it more
linker-friendly.
* Use it in `AssemblyName.IsPublicKeyValid`.
Diffstat (limited to 'mcs/class/Mono.Security')
-rw-r--r-- | mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs | 81 |
1 files changed, 52 insertions, 29 deletions
diff --git a/mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs b/mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs index 3f06114dd0f..decaeca0c4c 100644 --- a/mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs +++ b/mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs @@ -77,6 +77,24 @@ namespace Mono.Security.Cryptography { return null; } +#if INSIDE_CORLIB + static internal bool TryImportCapiPrivateKeyBlob (byte[] blob, int offset) + { + try { + var rsap = GetParametersFromCapiPrivateKeyBlob (blob, offset); + // Since we are only checking whether this throws an exception and + // not actually returning the `RSA` object, we can use `RSAManaged` + // here because that's what the `RSACryptoServiceProvider` implementation + // does internally. + var rsa = new RSAManaged (); + rsa.ImportParameters (rsap); + return true; + } catch (CryptographicException) { + return false; + } + } +#endif + // convert the key from PRIVATEKEYBLOB to RSA // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/private_key_blobs.asp // e.g. SNK files, PVK files @@ -87,6 +105,38 @@ namespace Mono.Security.Cryptography { static public RSA FromCapiPrivateKeyBlob (byte[] blob, int offset) { + RSAParameters rsap = GetParametersFromCapiPrivateKeyBlob (blob, offset); + +#if INSIDE_CORLIB && MOBILE + RSA rsa = RSA.Create (); + rsa.ImportParameters (rsap); +#else + RSA rsa = null; + try { + rsa = RSA.Create (); + rsa.ImportParameters (rsap); + } + catch (CryptographicException ce) { + // this may cause problem when this code is run under + // the SYSTEM identity on Windows (e.g. ASP.NET). See + // http://bugzilla.ximian.com/show_bug.cgi?id=77559 + try { + CspParameters csp = new CspParameters (); + csp.Flags = CspProviderFlags.UseMachineKeyStore; + rsa = new RSACryptoServiceProvider (csp); + rsa.ImportParameters (rsap); + } + catch { + // rethrow original, not the later, exception if this fails + throw ce; + } + } +#endif + return rsa; + } + + static RSAParameters GetParametersFromCapiPrivateKeyBlob (byte[] blob, int offset) + { if (blob == null) throw new ArgumentNullException ("blob"); if (offset >= blob.Length) @@ -161,37 +211,10 @@ namespace Mono.Security.Cryptography { Buffer.BlockCopy (blob, pos, rsap.D, 0, byteLen); Array.Reverse (rsap.D); } - } - catch (Exception e) { + return rsap; + } catch (Exception e) { throw new CryptographicException ("Invalid blob.", e); } - -#if INSIDE_CORLIB && MOBILE - RSA rsa = RSA.Create (); - rsa.ImportParameters (rsap); -#else - RSA rsa = null; - try { - rsa = RSA.Create (); - rsa.ImportParameters (rsap); - } - catch (CryptographicException ce) { - // this may cause problem when this code is run under - // the SYSTEM identity on Windows (e.g. ASP.NET). See - // http://bugzilla.ximian.com/show_bug.cgi?id=77559 - try { - CspParameters csp = new CspParameters (); - csp.Flags = CspProviderFlags.UseMachineKeyStore; - rsa = new RSACryptoServiceProvider (csp); - rsa.ImportParameters (rsap); - } - catch { - // rethrow original, not the later, exception if this fails - throw ce; - } - } -#endif - return rsa; } static public DSA FromCapiPrivateKeyBlobDSA (byte[] blob) |