private void CommonStuff(CryptographicAttributeObjectCollection coll) { Assert.IsFalse(coll.IsSynchronized, "IsSynchronized"); Assert.AreSame(coll, coll.SyncRoot, "SyncRoot"); Assert.IsNotNull(coll.GetEnumerator(), "GetEnumerator"); int i = coll.Count; Oid o1 = new Oid("1.2.840.113549.1.7.3"); AsnEncodedData aed = new AsnEncodedData(o1, new byte[] { 0x05, 0x00 }); Assert.AreEqual(i, coll.Add(aed), "Add(AsnEncodedData)"); Assert.IsTrue((coll[i++] is CryptographicAttributeObject), "converted"); Oid o2 = new Oid("1.2.840.113549.1.7.2"); CryptographicAttributeObject cao = new CryptographicAttributeObject(o2); Assert.AreEqual(i, coll.Add(cao), "Add(CryptographicAttributeObject)"); CryptographicAttributeObject[] array = new CryptographicAttributeObject [coll.Count]; coll.CopyTo(array, 0); Array a = (Array) new object [coll.Count]; ICollection c = (ICollection)coll; c.CopyTo(a, 0); IEnumerable e = (IEnumerable)coll; Assert.IsNotNull(e.GetEnumerator(), "GetEnumerator"); coll.Remove(cao); Assert.AreEqual(i, coll.Count, "Remove(CryptographicAttributeObject)"); }
public static void CopyExceptions() { CryptographicAttributeObject a0 = s_ca0; CryptographicAttributeObject a1 = s_ca1; CryptographicAttributeObject a2 = s_ca2; CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); c.Add(a0); c.Add(a1); c.Add(a2); CryptographicAttributeObject[] a = new CryptographicAttributeObject[3]; Assert.Throws <ArgumentNullException>(() => c.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, 3)); Assert.Throws <ArgumentException>(() => c.CopyTo(a, 1)); ICollection ic = c; Assert.Throws <ArgumentNullException>(() => ic.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, 3)); Assert.Throws <ArgumentException>(() => ic.CopyTo(a, 1)); Assert.Throws <ArgumentException>(() => ic.CopyTo(new CryptographicAttributeObject[2, 2], 0)); Assert.Throws <InvalidCastException>(() => ic.CopyTo(new int[10], 0)); }
public static void AddNegative() { CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); Assert.Throws <ArgumentNullException>(() => c.Add((CryptographicAttributeObject)null)); Assert.Throws <ArgumentNullException>(() => c.Add((AsnEncodedData)null)); }
public static void CopyExceptions() { CryptographicAttributeObject a0 = s_ca0; CryptographicAttributeObject a1 = s_ca1; CryptographicAttributeObject a2 = s_ca2; CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); c.Add(a0); c.Add(a1); c.Add(a2); CryptographicAttributeObject[] a = new CryptographicAttributeObject[3]; Assert.Throws <ArgumentNullException>(() => c.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, 3)); AssertExtensions.Throws <ArgumentException>(null, () => c.CopyTo(a, 1)); ICollection ic = c; Assert.Throws <ArgumentNullException>(() => ic.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, 3)); AssertExtensions.Throws <ArgumentException>(null, () => ic.CopyTo(a, 1)); AssertExtensions.Throws <ArgumentException>(null, () => ic.CopyTo(new CryptographicAttributeObject[2, 2], 0)); Assert.Throws <InvalidCastException>(() => ic.CopyTo(new int[10], 0)); if (PlatformDetection.IsNonZeroLowerBoundArraySupported) { // Array has non-zero lower bound Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 }); Assert.Throws <IndexOutOfRangeException>(() => ic.CopyTo(array, 0)); } }
public static CryptographicAttributeObjectCollection CreateSignedAttributes( SignPackageRequest request, IReadOnlyList <X509Certificate2> chainList) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (chainList == null || chainList.Count == 0) { throw new ArgumentException(Strings.ArgumentCannotBeNullOrEmpty, nameof(chainList)); } var attributes = new CryptographicAttributeObjectCollection { new Pkcs9SigningTime() }; if (request.SignatureType != SignatureType.Unknown) { // Add signature type if set. attributes.Add(AttributeUtility.CreateCommitmentTypeIndication(request.SignatureType)); } attributes.Add(AttributeUtility.CreateSigningCertificateV2(chainList[0], request.SignatureHashAlgorithm)); return(attributes); }
public void Two_Both() { coll = new CryptographicAttributeObjectCollection(); Oid o1 = new Oid(defaultOid + ".1"); AsnEncodedData aed = new AsnEncodedData(o1, new byte[] { 0x05, 0x00 }); coll.Add(aed); Oid o2 = new Oid(defaultOid + ".2"); coll.Add(new CryptographicAttributeObject(o2)); Count(2); }
public static void IndexOutOfBounds() { CryptographicAttributeObject a0 = s_ca0; CryptographicAttributeObject a1 = s_ca1; CryptographicAttributeObject a2 = s_ca2; CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); c.Add(a0); c.Add(a1); c.Add(a2); object ignore = null; Assert.Throws <ArgumentOutOfRangeException>(() => ignore = c[-1]); Assert.Throws <ArgumentOutOfRangeException>(() => ignore = c[3]); }
public static void Add() { CryptographicAttributeObject a0 = s_ca0; CryptographicAttributeObject a1 = s_ca1; CryptographicAttributeObject a2 = s_ca2; CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); int index; index = c.Add(a0); Assert.Equal(0, index); index = c.Add(a1); Assert.Equal(1, index); index = c.Add(a2); Assert.Equal(2, index); AssertEquals(c, new CryptographicAttributeObject[] { a0, a1, a2 }); }
public void One_AsnEncodedData() { Oid o = new Oid(defaultOid); AsnEncodedData aed = new AsnEncodedData(o, new byte[] { 0x05, 0x00 }); coll = new CryptographicAttributeObjectCollection(); coll.Add(aed); Count(1); }
public static CryptographicAttributeObjectCollection GetSignedAttributes( SignPackageRequest request, IReadOnlyList <X509Certificate2> chain) { var attributes = new CryptographicAttributeObjectCollection { new Pkcs9SigningTime() }; if (request.SignatureType != SignatureType.Unknown) { // Add signature type if set. attributes.Add(AttributeUtility.GetCommitmentTypeIndication(request.SignatureType)); } // Add the full chain of certificate hashes attributes.Add(AttributeUtility.GetSigningCertificateV2(chain, request.SignatureHashAlgorithm)); return(attributes); }
public static void AddFold() { AsnEncodedData dd1 = new Pkcs9DocumentDescription("My Description 1"); AsnEncodedData dd2 = new Pkcs9DocumentDescription("My Description 2"); CryptographicAttributeObjectCollection c = new CryptographicAttributeObjectCollection(); int index; index = c.Add(dd1); Assert.Equal(0, index); index = c.Add(dd2); Assert.Equal(0, index); AsnEncodedDataCollection expectedValues = new AsnEncodedDataCollection(); expectedValues.Add(dd1); expectedValues.Add(dd2); CryptographicAttributeObject expected = new CryptographicAttributeObject(dd1.Oid, expectedValues); AssertEquals(c, new CryptographicAttributeObject[] { expected }); }
private static unsafe void AddCryptAttribute(CryptographicAttributeObjectCollection collection, CRYPT_ATTRIBUTE *pCryptAttribute) { string oidValue = pCryptAttribute->pszObjId.ToStringAnsi(); Oid oid = new Oid(oidValue); for (int i = 0; i < pCryptAttribute->cValue; i++) { byte[] encodedAttribute = pCryptAttribute->rgValue[i].ToByteArray(); AsnEncodedData attributeObject = Helpers.CreateBestPkcs9AttributeObjectAvailable(oid, encodedAttribute); collection.Add(attributeObject); } }
public void Remove_MultipleSameOid_First() { Oid o = new Oid(defaultOid); CryptographicAttributeObject cao = new CryptographicAttributeObject(o); CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection(cao); Oid o1 = new Oid(defaultOid); AsnEncodedData aed = new AsnEncodedData(o1, new byte[] { 0x04, (byte)0 }); coll.Add(aed); aed = new AsnEncodedData(o1, new byte[] { 0x04, (byte)0 }); coll.Add(aed); Oid o2 = new Oid(defaultOid); coll.Add(new CryptographicAttributeObject(o2)); Assert.AreEqual(1, coll.Count, "before Remove"); coll.Remove(cao); Assert.AreEqual(0, coll.Count, "after Remove"); }
public static void AttributesIsMutable() { Pkcs12SafeBag safeBag = new TestSafeBag(Oids.Aes192); CryptographicAttributeObjectCollection firstCall = safeBag.Attributes; Assert.Same(firstCall, safeBag.Attributes); Assert.Equal(0, firstCall.Count); firstCall.Add(new Pkcs9DocumentDescription("Description")); Assert.Equal(1, safeBag.Attributes.Count); Assert.Same(firstCall, safeBag.Attributes); }
public void Add_MultipleSameOid() { Oid o = new Oid(defaultOid); CryptographicAttributeObject cao = new CryptographicAttributeObject(o); CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection(cao); int i = 0; while (i < 10) { Assert.AreEqual(1, coll.Count, String.Format("Count-{0}", i)); Assert.AreEqual(i * 2, coll[0].Values.Count, String.Format("Values.Count-{0}", i++)); Oid o1 = new Oid(defaultOid); AsnEncodedData aed = new AsnEncodedData(o1, new byte[] { 0x04, (byte)i }); coll.Add(aed); aed = new AsnEncodedData(o1, new byte[] { 0x04, (byte)i }); coll.Add(aed); Oid o2 = new Oid(defaultOid); coll.Add(new CryptographicAttributeObject(o2)); } }
private static unsafe void AddCryptAttribute(CryptographicAttributeObjectCollection collection, CRYPT_ATTRIBUTE *pCryptAttribute) { string oidValue = pCryptAttribute->pszObjId.ToStringAnsi(); Oid oid = new Oid(oidValue); AsnEncodedDataCollection attributeCollection = new AsnEncodedDataCollection(); for (int i = 0; i < pCryptAttribute->cValue; i++) { // CreateBestPkcs9AttributeObjectAvailable is expected to create a copy of the data so that it has ownership // of the underlying data. ReadOnlySpan <byte> encodedAttribute = pCryptAttribute->rgValue[i].DangerousAsSpan(); AsnEncodedData attributeObject = PkcsHelpers.CreateBestPkcs9AttributeObjectAvailable(oid, encodedAttribute); attributeCollection.Add(attributeObject); } collection.Add(new CryptographicAttributeObject(oid, attributeCollection)); }
internal unsafe CryptographicAttributeObjectCollection ReadAttributes(CRYPT_ATTRIBUTES attributes) { var collection = new CryptographicAttributeObjectCollection(); var attributeSize = Marshal.SizeOf <CRYPT_ATTRIBUTE>(); var blobSize = Marshal.SizeOf <CRYPTOAPI_BLOB>(); for (var i = 0; i < attributes.cAttr; i++) { var structure = Marshal.PtrToStructure <CRYPT_ATTRIBUTE>(attributes.rgAttr + (i * attributeSize)); var asnValues = new AsnEncodedDataCollection(); for (var j = 0; j < structure.cValue; j++) { var blob = Marshal.PtrToStructure <CRYPTOAPI_BLOB>(structure.rgValue + j * blobSize); asnValues.Add(new AsnEncodedData(structure.pszObjId, ReadBlob(blob))); } collection.Add(new CryptographicAttributeObject(new Oid(structure.pszObjId), asnValues)); } return(collection); }
public FakeSignature() { SignedAttributes = new CryptographicAttributeObjectCollection(); UnsignedAttributes = new CryptographicAttributeObjectCollection(); SignedAttributes.Add(new AsnEncodedData(new Oid(KnownOids.MessageDigest), new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })); }