diff options
author | Martin Baulig <mabaul@microsoft.com> | 2019-02-27 14:56:07 +0300 |
---|---|---|
committer | Alexander Köplinger <alex.koeplinger@outlook.com> | 2019-02-27 14:56:06 +0300 |
commit | 55a34261c9af13f9a223920f111005d39e7a3d9e (patch) | |
tree | 53b098512ad0f2c685451958b8626a91853f0934 /mcs/class/Mono.Security | |
parent | ce2259d0099bb81e90944dc2335f636e1bbbb2e4 (diff) |
[Mono.Security]: minor `CryptoConvert` changes to make it more linker-friendly. (#13204)
* Add new internal `CryptoConvert.TryImportCapiPrivateKeyBlob()` method that's using `RSAManaged` directly to make it more linker-friendly.
* Also add new internal `CryptoConvert.TryImportCapiPublicKeyBlob()`.
* Use it in `AssemblyName.IsPublicKeyValid`.
Diffstat (limited to 'mcs/class/Mono.Security')
-rw-r--r-- | mcs/class/Mono.Security/Mono.Security.Cryptography/CryptoConvert.cs | 153 |
1 files changed, 102 insertions, 51 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..b0571bcf8cc 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) @@ -405,6 +428,24 @@ namespace Mono.Security.Cryptography { return blob; } +#if INSIDE_CORLIB + static internal bool TryImportCapiPublicKeyBlob (byte[] blob, int offset) + { + try { + var rsap = GetParametersFromCapiPublicKeyBlob (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 + static public RSA FromCapiPublicKeyBlob (byte[] blob) { return FromCapiPublicKeyBlob (blob, 0); @@ -412,6 +453,36 @@ namespace Mono.Security.Cryptography { static public RSA FromCapiPublicKeyBlob (byte[] blob, int offset) { + var rsap = GetParametersFromCapiPublicKeyBlob (blob, offset); + + try { +#if INSIDE_CORLIB && MOBILE + RSA rsa = RSA.Create (); + rsa.ImportParameters (rsap); +#else + RSA rsa = null; + try { + rsa = RSA.Create (); + rsa.ImportParameters (rsap); + } + catch (CryptographicException) { + // 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 + CspParameters csp = new CspParameters (); + csp.Flags = CspProviderFlags.UseMachineKeyStore; + rsa = new RSACryptoServiceProvider (csp); + rsa.ImportParameters (rsap); + } +#endif + return rsa; + } catch (Exception e) { + throw new CryptographicException ("Invalid blob.", e); + } + } + + static RSAParameters GetParametersFromCapiPublicKeyBlob (byte[] blob, int offset) + { if (blob == null) throw new ArgumentNullException ("blob"); if (offset >= blob.Length) @@ -444,28 +515,8 @@ namespace Mono.Security.Cryptography { rsap.Modulus = new byte [byteLen]; Buffer.BlockCopy (blob, pos, rsap.Modulus, 0, byteLen); Array.Reverse (rsap.Modulus); -#if INSIDE_CORLIB && MOBILE - RSA rsa = RSA.Create (); - rsa.ImportParameters (rsap); -#else - RSA rsa = null; - try { - rsa = RSA.Create (); - rsa.ImportParameters (rsap); - } - catch (CryptographicException) { - // 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 - CspParameters csp = new CspParameters (); - csp.Flags = CspProviderFlags.UseMachineKeyStore; - rsa = new RSACryptoServiceProvider (csp); - rsa.ImportParameters (rsap); - } -#endif - return rsa; - } - catch (Exception e) { + return rsap; + } catch (Exception e) { throw new CryptographicException ("Invalid blob.", e); } } |