Imported Upstream version 6.4.0.137

Former-commit-id: 943baa9f16a098c33e129777827f3a9d20da00d6
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-07-26 19:53:28 +00:00
parent e9207cf623
commit ef583813eb
2712 changed files with 74169 additions and 40587 deletions

View File

@@ -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
@@ -86,6 +104,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");
@@ -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,12 +428,60 @@ 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);
}
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");
@@ -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);
}
}

View File

@@ -239,7 +239,7 @@ namespace Mono.Security {
byte[] publicKey = PublicKey;
if (publicKey == null)
return null;
HashAlgorithm ha = HashAlgorithm.Create (TokenAlgorithm);
HashAlgorithm ha = GetHashAlgorithm (TokenAlgorithm);
byte[] hash = ha.ComputeHash (publicKey);
// we need the last 8 bytes in reverse order
keyToken = new byte [8];
@@ -250,6 +250,22 @@ namespace Mono.Security {
}
}
static HashAlgorithm GetHashAlgorithm (string algorithm)
{
#if FULL_AOT_RUNTIME
switch (algorithm.ToUpper (CultureInfo.InvariantCulture)) {
case "SHA1":
return new SHA1CryptoServiceProvider ();
case "MD5":
return new MD5CryptoServiceProvider ();
default:
throw new ArgumentException ("Unsupported hash algorithm for token");
}
#else
return HashAlgorithm.Create (algorithm);
#endif
}
public string TokenAlgorithm {
get {
if (tokenAlgorithm == null)

View File

@@ -180,7 +180,7 @@ namespace MonoTests.Mono.Security.Cryptography {
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException) && !(ce.InnerException is IOException))
if (!(ce.InnerException is UnauthorizedAccessException) && !(ce.InnerException is IOException ioe && ioe.HResult == 30 /* Read-only file system */))
throw;
Assert.Ignore ("Access denied to key containers files.");
}
@@ -208,7 +208,7 @@ namespace MonoTests.Mono.Security.Cryptography {
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException) && !(ce.InnerException is IOException))
if (!(ce.InnerException is UnauthorizedAccessException) && !(ce.InnerException is IOException ioe && ioe.HResult == 30 /* Read-only file system */))
throw;
Assert.Ignore ("Access denied to key containers files.");
}