Imported Upstream version 5.18.0.142

Former-commit-id: 7467d4b717762eeaf652d77f1486dd11ffb1ff1f
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-10-09 08:20:59 +00:00
parent e52655b4dc
commit 0abdbe5a7d
1547 changed files with 93792 additions and 47893 deletions

View File

@@ -62,6 +62,8 @@ namespace System.Security.Cryptography {
if (name == null)
throw new ArgumentNullException ("name");
Type algoClass = null;
// TODO: These ignore args
switch (name.ToLowerInvariant ()) {
case "system.security.cryptography.dsacryptoserviceprovider":
@@ -190,48 +192,51 @@ namespace System.Security.Cryptography {
case "3des":
return new TripleDESCryptoServiceProvider ();
// These are not yet linker friendly
// Use Type.GetType to be linker friendly
// TODO: This does not work when the assembly is not referenced by the project
case "x509chain":
name = "System.Security.Cryptography.X509Certificates.X509Chain, System";
algoClass = Type.GetType ("System.Security.Cryptography.X509Certificates.X509Chain, System");
break;
case "2.5.29.15":
name = "System.Security.Cryptography.X509Certificates.X509KeyUsageExtension, System";
algoClass = Type.GetType ("System.Security.Cryptography.X509Certificates.X509KeyUsageExtension, System");
break;
case "2.5.29.19":
name = "System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension, System";
algoClass = Type.GetType ("System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension, System");
break;
case "2.5.29.14":
name = "System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension, System";
algoClass = Type.GetType ("System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension, System");
break;
case "2.5.29.37":
name = "System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension, System";
algoClass = Type.GetType ("System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension, System");
break;
case "aes":
#if MONOTOUCH || XAMMAC
name = "System.Security.Cryptography.AesManaged, System.Core";
algoClass = Type.GetType ("System.Security.Cryptography.AesManaged, System.Core");
#else
name = "System.Security.Cryptography.AesCryptoServiceProvider, System.Core";
algoClass = Type.GetType ("System.Security.Cryptography.AesCryptoServiceProvider, System.Core");
#endif
break;
}
lock (lockObject) {
Type algoClass = null;
if (algorithms?.TryGetValue (name, out algoClass) == true) {
try {
return Activator.CreateInstance (algoClass, args);
} catch {
if (algoClass == null) {
lock (lockObject) {
if (algorithms?.TryGetValue (name, out algoClass) == true) {
try {
return Activator.CreateInstance (algoClass, args);
} catch {
}
}
}
algoClass = Type.GetType (name);
}
try {
// last resort, the request type might be available (if care is taken for the type not to be linked
// away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
// unit tests happy
return Activator.CreateInstance (Type.GetType (name), args);
}
catch {
return Activator.CreateInstance (algoClass, args);
} catch {
// method doesn't throw any exception
return null;
}

View File

@@ -53,36 +53,40 @@ namespace System.Security.Cryptography {
_lock = new object ();
}
public RNGCryptoServiceProvider ()
unsafe public RNGCryptoServiceProvider ()
{
_handle = RngInitialize (null);
_handle = RngInitialize (null, IntPtr.Zero);
Check ();
}
public RNGCryptoServiceProvider (byte[] rgb)
unsafe public RNGCryptoServiceProvider (byte[] rgb)
{
_handle = RngInitialize (rgb);
fixed (byte* fixed_rgb = rgb)
_handle = RngInitialize (fixed_rgb, (rgb != null) ? (IntPtr)rgb.Length : IntPtr.Zero);
Check ();
}
public RNGCryptoServiceProvider (CspParameters cspParams)
unsafe public RNGCryptoServiceProvider (CspParameters cspParams)
{
// CSP selection isn't supported but we still return
// random data (no exception) for compatibility
_handle = RngInitialize (null);
_handle = RngInitialize (null, IntPtr.Zero);
Check ();
}
public RNGCryptoServiceProvider (string str)
unsafe public RNGCryptoServiceProvider (string str)
{
if (str == null)
_handle = RngInitialize (null);
else
_handle = RngInitialize (Encoding.UTF8.GetBytes (str));
_handle = RngInitialize (null, IntPtr.Zero);
else {
byte[] bytes = Encoding.UTF8.GetBytes (str);
fixed (byte* fixed_bytes = bytes)
_handle = RngInitialize (fixed_bytes, (IntPtr)bytes.Length);
}
Check ();
}
private void Check ()
private void Check ()
{
if (_handle == IntPtr.Zero) {
throw new CryptographicException (
@@ -94,51 +98,67 @@ namespace System.Security.Cryptography {
private static extern bool RngOpen ();
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern IntPtr RngInitialize (byte[] seed);
unsafe private static extern IntPtr RngInitialize (byte* seed, IntPtr seed_length);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern IntPtr RngGetBytes (IntPtr handle, byte[] data);
unsafe private static extern IntPtr RngGetBytes (IntPtr handle, byte* data, IntPtr data_length);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void RngClose (IntPtr handle);
public override void GetBytes (byte[] data)
unsafe public override void GetBytes (byte[] data)
{
if (data == null)
throw new ArgumentNullException ("data");
if (_lock == null) {
_handle = RngGetBytes (_handle, data);
} else {
// using a global handle for randomness
lock (_lock) {
_handle = RngGetBytes (_handle, data);
fixed (byte* fixed_data = data) {
if (_lock == null) {
_handle = RngGetBytes (_handle, fixed_data, (IntPtr)data.LongLength);
} else {
// using a global handle for randomness
lock (_lock) {
_handle = RngGetBytes (_handle, fixed_data, (IntPtr)data.LongLength);
}
}
}
Check ();
}
public override void GetNonZeroBytes (byte[] data)
unsafe internal void GetBytes (byte* data, IntPtr data_length)
{
if (_lock == null) {
_handle = RngGetBytes (_handle, data, data_length);
} else {
// using a global handle for randomness
lock (_lock) {
_handle = RngGetBytes (_handle, data, data_length);
}
}
Check ();
}
unsafe public override void GetNonZeroBytes (byte[] data)
{
if (data == null)
throw new ArgumentNullException ("data");
byte[] random = new byte [data.Length * 2];
int i = 0;
// one pass should be enough but hey this is random ;-)
while (i < data.Length) {
_handle = RngGetBytes (_handle, random);
byte[] random = new byte [data.LongLength * 2];
long i = 0;
// one pass should be enough but hey this is random ;-)
while (i < data.LongLength) {
fixed (byte* fixed_random = random)
_handle = RngGetBytes (_handle, fixed_random, (IntPtr)random.LongLength);
Check ();
for (int j=0; j < random.Length; j++) {
if (i == data.Length)
break;
if (random [j] != 0)
data [i++] = random [j];
for (long j = 0; j < random.LongLength; j++) {
if (i == data.LongLength)
break;
if (random [j] != 0)
data [i++] = random [j];
}
}
}
~RNGCryptoServiceProvider ()
~RNGCryptoServiceProvider ()
{
if (_handle != IntPtr.Zero) {
RngClose (_handle);