未验证 提交 7f5265a3 编写于 作者: H hrrrrustic 提交者: GitHub

Implement generic IEnumerable on various X509 collection types

X509Certificate2Collection gets it, X509CertificateCollection doesn't.

We don't really want people using the older collection type, and we have inheritance, and being both
`IEnumerable<X509Certificate>` and `IEnumerable<X509Certificate2>` makes some things weird.
上级 872698f6
......@@ -278,7 +278,7 @@ public partial class X509Certificate2 : System.Security.Cryptography.X509Certifi
public override string ToString(bool verbose) { throw null; }
public bool Verify() { throw null; }
}
public partial class X509Certificate2Collection : System.Security.Cryptography.X509Certificates.X509CertificateCollection
public partial class X509Certificate2Collection : System.Security.Cryptography.X509Certificates.X509CertificateCollection, System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509Certificate2>, System.Collections.IEnumerable
{
public X509Certificate2Collection() { }
public X509Certificate2Collection(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { }
......@@ -307,8 +307,9 @@ public partial class X509Certificate2Collection : System.Security.Cryptography.X
public void Remove(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { }
public void RemoveRange(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates) { }
public void RemoveRange(System.Security.Cryptography.X509Certificates.X509Certificate2[] certificates) { }
System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509Certificate2> System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509Certificate2>.GetEnumerator() { throw null; }
}
public sealed partial class X509Certificate2Enumerator : System.Collections.IEnumerator
public sealed partial class X509Certificate2Enumerator : System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509Certificate2>, System.Collections.IEnumerator, System.IDisposable
{
internal X509Certificate2Enumerator() { }
public System.Security.Cryptography.X509Certificates.X509Certificate2 Current { get { throw null; } }
......@@ -317,6 +318,7 @@ public sealed partial class X509Certificate2Enumerator : System.Collections.IEnu
public void Reset() { }
bool System.Collections.IEnumerator.MoveNext() { throw null; }
void System.Collections.IEnumerator.Reset() { }
void System.IDisposable.Dispose() { }
}
public partial class X509CertificateCollection : System.Collections.CollectionBase
{
......@@ -370,7 +372,7 @@ public partial class X509ChainElement
public System.Security.Cryptography.X509Certificates.X509ChainStatus[] ChainElementStatus { get { throw null; } }
public string Information { get { throw null; } }
}
public sealed partial class X509ChainElementCollection : System.Collections.ICollection, System.Collections.IEnumerable
public sealed partial class X509ChainElementCollection : System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509ChainElement>, System.Collections.ICollection, System.Collections.IEnumerable
{
internal X509ChainElementCollection() { }
public int Count { get { throw null; } }
......@@ -379,16 +381,18 @@ public sealed partial class X509ChainElementCollection : System.Collections.ICol
public object SyncRoot { get { throw null; } }
public void CopyTo(System.Security.Cryptography.X509Certificates.X509ChainElement[] array, int index) { }
public System.Security.Cryptography.X509Certificates.X509ChainElementEnumerator GetEnumerator() { throw null; }
System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509ChainElement> System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509ChainElement>.GetEnumerator() { throw null; }
void System.Collections.ICollection.CopyTo(System.Array array, int index) { }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
}
public sealed partial class X509ChainElementEnumerator : System.Collections.IEnumerator
public sealed partial class X509ChainElementEnumerator : System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509ChainElement>, System.Collections.IEnumerator, System.IDisposable
{
internal X509ChainElementEnumerator() { }
public System.Security.Cryptography.X509Certificates.X509ChainElement Current { get { throw null; } }
object System.Collections.IEnumerator.Current { get { throw null; } }
public bool MoveNext() { throw null; }
public void Reset() { }
void System.IDisposable.Dispose() { }
}
public sealed partial class X509ChainPolicy
{
......@@ -479,7 +483,7 @@ public partial class X509Extension : System.Security.Cryptography.AsnEncodedData
public bool Critical { get { throw null; } set { } }
public override void CopyFrom(System.Security.Cryptography.AsnEncodedData asnEncodedData) { }
}
public sealed partial class X509ExtensionCollection : System.Collections.ICollection, System.Collections.IEnumerable
public sealed partial class X509ExtensionCollection : System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509Extension>, System.Collections.ICollection, System.Collections.IEnumerable
{
public X509ExtensionCollection() { }
public int Count { get { throw null; } }
......@@ -490,16 +494,18 @@ public sealed partial class X509ExtensionCollection : System.Collections.ICollec
public int Add(System.Security.Cryptography.X509Certificates.X509Extension extension) { throw null; }
public void CopyTo(System.Security.Cryptography.X509Certificates.X509Extension[] array, int index) { }
public System.Security.Cryptography.X509Certificates.X509ExtensionEnumerator GetEnumerator() { throw null; }
System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509Extension> System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509Extension>.GetEnumerator() { throw null; }
void System.Collections.ICollection.CopyTo(System.Array array, int index) { }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
}
public sealed partial class X509ExtensionEnumerator : System.Collections.IEnumerator
public sealed partial class X509ExtensionEnumerator : System.Collections.Generic.IEnumerator<System.Security.Cryptography.X509Certificates.X509Extension>, System.Collections.IEnumerator, System.IDisposable
{
internal X509ExtensionEnumerator() { }
public System.Security.Cryptography.X509Certificates.X509Extension Current { get { throw null; } }
object System.Collections.IEnumerator.Current { get { throw null; } }
public bool MoveNext() { throw null; }
public void Reset() { }
void System.IDisposable.Dispose() { }
}
public enum X509FindType
{
......
......@@ -7,10 +7,11 @@
using System.Diagnostics;
using System.Formats.Asn1;
using System.Security.Cryptography.X509Certificates.Asn1;
using System.Collections.Generic;
namespace System.Security.Cryptography.X509Certificates
{
public class X509Certificate2Collection : X509CertificateCollection
public class X509Certificate2Collection : X509CertificateCollection, IEnumerable<X509Certificate2>
{
public X509Certificate2Collection()
{
......@@ -133,6 +134,8 @@ public new X509Certificate2Enumerator GetEnumerator()
return new X509Certificate2Enumerator(this);
}
IEnumerator<X509Certificate2> IEnumerable<X509Certificate2>.GetEnumerator() => GetEnumerator();
public void Import(byte[] rawData)
{
if (rawData == null)
......
......@@ -2,11 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Security.Cryptography.X509Certificates
{
public sealed class X509Certificate2Enumerator : IEnumerator
public sealed class X509Certificate2Enumerator : IEnumerator, IEnumerator<X509Certificate2>
{
private readonly IEnumerator _enumerator;
......@@ -46,5 +47,8 @@ void IEnumerator.Reset()
{
Reset();
}
void IDisposable.Dispose()
{
}
}
}
......@@ -2,11 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Security.Cryptography.X509Certificates
{
public sealed class X509ChainElementCollection : ICollection, IEnumerable
public sealed class X509ChainElementCollection : ICollection, IEnumerable<X509ChainElement>
{
private readonly X509ChainElement[] _elements;
......@@ -81,5 +82,7 @@ IEnumerator IEnumerable.GetEnumerator()
{
return new X509ChainElementEnumerator(this);
}
IEnumerator<X509ChainElement> IEnumerable<X509ChainElement>.GetEnumerator() => GetEnumerator();
}
}
......@@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
namespace System.Security.Cryptography.X509Certificates
{
public sealed class X509ChainElementEnumerator : IEnumerator
public sealed class X509ChainElementEnumerator : IEnumerator, IEnumerator<X509ChainElement>
{
private readonly X509ChainElementCollection _chainElements;
private int _current;
......@@ -32,6 +33,10 @@ object IEnumerator.Current
}
}
void IDisposable.Dispose()
{
}
public bool MoveNext()
{
if (_current == _chainElements.Count - 1)
......
......@@ -14,7 +14,7 @@
namespace System.Security.Cryptography.X509Certificates
{
public sealed class X509ExtensionCollection : ICollection, IEnumerable
public sealed class X509ExtensionCollection : ICollection, IEnumerable<X509Extension>
{
public X509ExtensionCollection()
{
......@@ -103,6 +103,8 @@ IEnumerator IEnumerable.GetEnumerator()
return new X509ExtensionEnumerator(this);
}
IEnumerator<X509Extension> IEnumerable<X509Extension>.GetEnumerator() => GetEnumerator();
private readonly List<X509Extension> _list = new List<X509Extension>();
}
}
......@@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
namespace System.Security.Cryptography.X509Certificates
{
public sealed class X509ExtensionEnumerator : IEnumerator
public sealed class X509ExtensionEnumerator : IEnumerator, IEnumerator<X509Extension>
{
internal X509ExtensionEnumerator(X509ExtensionCollection extensions)
{
......@@ -36,6 +37,10 @@ public void Reset()
_current = -1;
}
void IDisposable.Dispose()
{
}
private readonly X509ExtensionCollection _extensions;
private int _current;
}
......
......@@ -1067,7 +1067,6 @@ public static void BuildChainForFraudulentCertificate()
else
{
X509ChainElement certElement = chain.ChainElements
.OfType<X509ChainElement>()
.Single(e => e.Certificate.Subject == cert.Subject);
const X509ChainStatusFlags ExpectedFlag = X509ChainStatusFlags.ExplicitDistrust;
......@@ -1154,7 +1153,6 @@ public static void BuildChainForCertificateSignedWithDisallowedKey()
else
{
X509ChainElement certElement = chain.ChainElements
.OfType<X509ChainElement>()
.Single(e => e.Certificate.Subject == intermediateCert.Subject);
const X509ChainStatusFlags ExpectedFlag = X509ChainStatusFlags.ExplicitDistrust;
......
......@@ -383,7 +383,7 @@ public static void ImportPkcs12File_Chain_VerifyContents(X509KeyStorageFlags key
"test.local",
};
string[] actualSubjects = certs.OfType<X509Certificate2>().
string[] actualSubjects = certs.
Select(cert => cert.GetNameInfo(X509NameType.SimpleName, false)).
ToArray();
......@@ -397,7 +397,7 @@ public static void ImportPkcs12File_Chain_VerifyContents(X509KeyStorageFlags key
true,
};
bool[] actualHasPrivateKeys = certs.OfType<X509Certificate2>().
bool[] actualHasPrivateKeys = certs.
Select(cert => cert.HasPrivateKey).
ToArray();
......
......@@ -773,7 +773,7 @@ public static void ExportUnrelatedPfx()
// But, really, the collections should be equivalent
// (after being coerced to IEnumerable<X509Certificate2>)
Assert.Equal(collection.OfType<X509Certificate2>(), importedCollection.OfType<X509Certificate2>());
Assert.Equal(collection, importedCollection);
}
}
}
......@@ -808,7 +808,7 @@ public static void ExportMultiplePrivateKeys()
collection.Import(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible);
// Pre-condition, we have multiple private keys
int originalPrivateKeyCount = collection.OfType<X509Certificate2>().Count(c => c.HasPrivateKey);
int originalPrivateKeyCount = collection.Count(c => c.HasPrivateKey);
Assert.Equal(2, originalPrivateKeyCount);
byte[] exported = collection.Export(X509ContentType.Pkcs12);
......@@ -819,7 +819,7 @@ public static void ExportMultiplePrivateKeys()
Assert.Equal(collection.Count, importedCollection.Count);
int importedPrivateKeyCount = importedCollection.OfType<X509Certificate2>().Count(c => c.HasPrivateKey);
int importedPrivateKeyCount = importedCollection.Count(c => c.HasPrivateKey);
Assert.Equal(originalPrivateKeyCount, importedPrivateKeyCount);
}
}
......@@ -846,7 +846,7 @@ public static void CanAddMultipleCertsWithSinglePrivateKey()
twoWithoutKey,
};
Assert.Equal(1, col.Cast<X509Certificate2>().Count(x => x.HasPrivateKey));
Assert.Equal(1, col.Count(x => x.HasPrivateKey));
Assert.Equal(2, col.Count);
byte[] buffer = col.Export(X509ContentType.Pfx);
......@@ -1616,15 +1616,5 @@ private static void TestExportStore(X509ContentType ct)
}
public static IEnumerable<object[]> StorageFlags => CollectionImportTests.StorageFlags;
private static X509Certificate2[] ToArray(this X509Certificate2Collection col)
{
X509Certificate2[] array = new X509Certificate2[col.Count];
for (int i = 0; i < col.Count; i++)
{
array[i] = col[i];
}
return array;
}
}
}
......@@ -357,7 +357,7 @@ public static void TestLeafCertificateWithUnknownCriticalExtension()
}
else
{
X509ChainElement certElement = chain.ChainElements.OfType<X509ChainElement>().Single();
X509ChainElement certElement = chain.ChainElements.Single();
const X509ChainStatusFlags ExpectedFlag = X509ChainStatusFlags.HasNotSupportedCriticalExtension;
X509ChainStatusFlags actualFlags = certElement.AllStatusFlags();
Assert.True((actualFlags & ExpectedFlag) == ExpectedFlag, $"Has expected flag {ExpectedFlag} but was {actualFlags}");
......
......@@ -340,7 +340,7 @@ public static void AiaFetchTimeout()
}
private static X509ChainStatusFlags GetFlags(X509Chain chain, string thumbprint) =>
chain.ChainElements.OfType<X509ChainElement>().
chain.ChainElements.
Single(e => e.Certificate.Thumbprint == thumbprint).
ChainElementStatus.Aggregate((X509ChainStatusFlags)0, (a, e) => a | e.Status);
}
......
......@@ -634,7 +634,7 @@ private static void AssertEqualContents(X509Store storeA, X509Store storeB)
X509Certificate2Collection storeACerts = storeATracker.Collection;
X509Certificate2Collection storeBCerts = storeBTracker.Collection;
Assert.Equal(storeACerts.OfType<X509Certificate2>(), storeBCerts.OfType<X509Certificate2>());
Assert.Equal(storeACerts, storeBCerts);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册