static void TestAdd() { asn = new AsnEncodedDataCollection() ; try { asn.Add( null ) ; Result = false ; } catch( ArgumentNullException ) { Result = true ; } catch( Exception ) { Result = false ; } //add normal case AsnEncodedData data1 = new AsnEncodedData( oid , new byte[]{1,2,3} ) ; AsnEncodedData data2 = new AsnEncodedData( oid , new byte[]{5,6,7} ) ; asn = new AsnEncodedDataCollection(data1) ; asn.Add( data2 ) ; Result = asn.Count == 2 ; //Add where OIDs don't match data2 = new AsnEncodedData(badOid , new byte[]{1,2,3,4} ) ; asn = GetCollection(data1) ; try { asn.Add(data2) ; Result = false ; } catch(CryptographicException) { Result = true ; } catch(Exception e) { Result = false ; Console.WriteLine(e) ; } //add w/ a empty Oid data2.Oid = new Oid() ; asn = GetCollection(data1) ; try { asn.Add(data2) ; Result = false ; } catch(CryptographicException) { Result = true ; } catch(Exception e) { Result = false ; Console.WriteLine(e) ; } }
public static void Add() { AsnEncodedData a0 = new AsnEncodedData("1.0", Array.Empty<byte>()); AsnEncodedData a1 = new AsnEncodedData("1.1", Array.Empty<byte>()); AsnEncodedData a2 = new AsnEncodedData("1.2", Array.Empty<byte>()); AsnEncodedDataCollection c = new AsnEncodedDataCollection(); 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 AsnEncodedData[] { a0, a1, a2 }); }
public static void CryptographicAttributeObjectMismatch() { Oid oid = new Oid(Oids.DocumentDescription); Oid wrongOid = new Oid(Oids.DocumentName); AsnEncodedDataCollection col = new AsnEncodedDataCollection(); col.Add(new AsnEncodedData(oid, new byte[3])); object ignore; Assert.Throws <InvalidOperationException>(() => ignore = new CryptographicAttributeObject(wrongOid, col)); }
/// <summary> /// Allows encoding data that the production helper does not. /// </summary> private static CryptographicAttributeObject GetCommitmentTypeTestAttribute(params string[] oids) { var commitmentTypeIndication = new Oid(Oids.CommitmentTypeIndication); var values = new AsnEncodedDataCollection(); foreach (var oid in oids) { var value = DerEncoder.ConstructSequence(DerEncoder.SegmentedEncodeOid(oid)); values.Add(new AsnEncodedData(commitmentTypeIndication, value)); } return(new CryptographicAttributeObject(commitmentTypeIndication, values)); }
public static void CopyExceptions() { AsnEncodedData a0 = new AsnEncodedData("1.0", Array.Empty <byte>()); AsnEncodedData a1 = new AsnEncodedData("1.1", Array.Empty <byte>()); AsnEncodedData a2 = new AsnEncodedData("1.2", Array.Empty <byte>()); AsnEncodedDataCollection c = new AsnEncodedDataCollection(); c.Add(a0); c.Add(a1); c.Add(a2); Assert.Throws <ArgumentNullException>(() => c.CopyTo(null, 0)); AsnEncodedData[] a = new AsnEncodedData[3]; Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, 3)); Assert.Throws <ArgumentException>(() => c.CopyTo(a, 1)); // Array has non-zero lower bound ICollection ic = c; Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 }); Assert.Throws <IndexOutOfRangeException>(() => ic.CopyTo(array, 0)); }
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++) { byte[] encodedAttribute = pCryptAttribute->rgValue[i].ToByteArray(); AsnEncodedData attributeObject = PkcsHelpers.CreateBestPkcs9AttributeObjectAvailable(oid, encodedAttribute); attributeCollection.Add(attributeObject); } collection.Add(new CryptographicAttributeObject(oid, attributeCollection)); }
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)); }
/// <summary> /// Get the ANS.1 encoded data from the certificate. /// </summary> /// <param name="certificate">The certificate.</param> /// <returns>The collection of ANS.1 encoded data.</returns> public AsnEncodedDataCollection GetData(X509Certificate2 certificate) { // Create a new AsnEncodedDataCollection object. AsnEncodedDataCollection asncoll = new AsnEncodedDataCollection(); // Display extensions information. foreach (X509Extension extension in certificate.Extensions) { // Create an AsnEncodedData object using the extensions information. AsnEncodedData asndata = new AsnEncodedData(extension.Oid, extension.RawData); // Add the AsnEncodedData object to the AsnEncodedDataCollection object. asncoll.Add(asndata); } // Return the ASN.1 encoded data. return(asncoll); }
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); }
internal static AsnEncodedDataCollection GetAsnEncodedDataCollection(CAPI.CRYPT_ATTRIBUTE cryptAttribute) { AsnEncodedDataCollection encodedDataCollection = new AsnEncodedDataCollection(); Oid oid = new Oid(cryptAttribute.pszObjId); string name = oid.Value; for (uint index = 0U; index < cryptAttribute.cValue; ++index) { IntPtr pBlob = new IntPtr((long)cryptAttribute.rgValue + (long)index * (long)Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB))); Pkcs9AttributeObject pkcs9AttributeObject1 = new Pkcs9AttributeObject(oid, CAPI.BlobToByteArray(pBlob)); Pkcs9AttributeObject pkcs9AttributeObject2 = CryptoConfig.CreateFromName(name) as Pkcs9AttributeObject; if (pkcs9AttributeObject2 != null) { pkcs9AttributeObject2.CopyFrom((AsnEncodedData)pkcs9AttributeObject1); pkcs9AttributeObject1 = pkcs9AttributeObject2; } encodedDataCollection.Add((AsnEncodedData)pkcs9AttributeObject1); } return(encodedDataCollection); }
internal static AsnEncodedDataCollection GetAsnEncodedDataCollection(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE cryptAttribute) { AsnEncodedDataCollection datas = new AsnEncodedDataCollection(); Oid oid = new Oid(cryptAttribute.pszObjId); string name = oid.Value; for (uint i = 0; i < cryptAttribute.cValue; i++) { IntPtr pBlob = new IntPtr(((long)cryptAttribute.rgValue) + (i * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)))); Pkcs9AttributeObject asnEncodedData = new Pkcs9AttributeObject(oid, System.Security.Cryptography.CAPI.BlobToByteArray(pBlob)); Pkcs9AttributeObject obj3 = CryptoConfig.CreateFromName(name) as Pkcs9AttributeObject; if (obj3 != null) { obj3.CopyFrom(asnEncodedData); asnEncodedData = obj3; } datas.Add(asnEncodedData); } return(datas); }
public static void AddNegative() { AsnEncodedDataCollection c = new AsnEncodedDataCollection(); Assert.Throws <ArgumentNullException>(() => c.Add(null)); }
public static void IndexOutOfBounds() { AsnEncodedData a0 = new AsnEncodedData("1.0", Array.Empty<byte>()); AsnEncodedData a1 = new AsnEncodedData("1.1", Array.Empty<byte>()); AsnEncodedData a2 = new AsnEncodedData("1.2", Array.Empty<byte>()); AsnEncodedDataCollection c = new AsnEncodedDataCollection(); c.Add(a0); c.Add(a1); c.Add(a2); Assert.Throws<ArgumentOutOfRangeException>(() => c[-1]); Assert.Throws<ArgumentOutOfRangeException>(() => c[3]); }
public static void AddNegative() { AsnEncodedDataCollection c = new AsnEncodedDataCollection(); Assert.Throws<ArgumentNullException>(() => c.Add(null)); }
public static void CopyExceptions() { AsnEncodedData a0 = new AsnEncodedData("1.0", Array.Empty<byte>()); AsnEncodedData a1 = new AsnEncodedData("1.1", Array.Empty<byte>()); AsnEncodedData a2 = new AsnEncodedData("1.2", Array.Empty<byte>()); AsnEncodedDataCollection c = new AsnEncodedDataCollection(); c.Add(a0); c.Add(a1); c.Add(a2); Assert.Throws<ArgumentNullException>(() => c.CopyTo(null, 0)); AsnEncodedData[] a = new AsnEncodedData[3]; Assert.Throws<ArgumentOutOfRangeException>(() => c.CopyTo(a, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => c.CopyTo(a, 3)); Assert.Throws<ArgumentException>(() => c.CopyTo(a, 1)); // Array has non-zero lower bound ICollection ic = c; Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 }); Assert.Throws<IndexOutOfRangeException>(() => ic.CopyTo(array, 0)); }
static void Main() { //The following example demonstrates the usage the AsnEncodedData classes. // Asn encoded data is read from the extensions of an X509 certificate. try { // Open the certificate store. X509Store store = new X509Store("MY", StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); // Select one or more certificates to display extensions information. X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificate Select", "Select certificates from the following list to get extension information on that certificate", X509SelectionFlag.MultiSelection); // Create a new AsnEncodedDataCollection object. AsnEncodedDataCollection asncoll = new AsnEncodedDataCollection(); for (int i = 0; i < scollection.Count; i++) { // Display certificate information. Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Certificate name: {0}", scollection[i].GetName()); Console.ResetColor(); // Display extensions information. foreach (X509Extension extension in scollection[i].Extensions) { // Create an AsnEncodedData object using the extensions information. AsnEncodedData asndata = new AsnEncodedData(extension.Oid, extension.RawData); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Extension type: {0}", extension.Oid.FriendlyName); Console.WriteLine("Oid value: {0}", asndata.Oid.Value); Console.WriteLine("Raw data length: {0} {1}", asndata.RawData.Length, Environment.NewLine); Console.ResetColor(); Console.WriteLine(asndata.Format(true)); Console.WriteLine(Environment.NewLine); // Add the AsnEncodedData object to the AsnEncodedDataCollection object. asncoll.Add(asndata); } Console.WriteLine(Environment.NewLine); } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Number of AsnEncodedData items in the collection: {0} {1}", asncoll.Count, Environment.NewLine); Console.ResetColor(); store.Close(); //Create an enumerator for moving through the collection. AsnEncodedDataEnumerator asne = asncoll.GetEnumerator(); //You must execute a MoveNext() to get to the first item in the collection. asne.MoveNext(); // Write out AsnEncodedData in the collection. Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("First AsnEncodedData in the collection: {0}", asne.Current.Format(true)); Console.ResetColor(); asne.MoveNext(); Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine("Second AsnEncodedData in the collection: {0}", asne.Current.Format(true)); Console.ResetColor(); //Return index in the collection to the beginning. asne.Reset(); } catch (CryptographicException) { Console.WriteLine("Information could not be written out for this certificate."); } }
public static void CopyExceptions() { AsnEncodedData a0 = new AsnEncodedData("1.0", Array.Empty<byte>()); AsnEncodedData a1 = new AsnEncodedData("1.1", Array.Empty<byte>()); AsnEncodedData a2 = new AsnEncodedData("1.2", Array.Empty<byte>()); AsnEncodedDataCollection c = new AsnEncodedDataCollection(); c.Add(a0); c.Add(a1); c.Add(a2); Assert.Throws<ArgumentNullException>(() => c.CopyTo(null, 0)); AsnEncodedData[] a = new AsnEncodedData[3]; Assert.Throws<ArgumentOutOfRangeException>(() => c.CopyTo(a, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => c.CopyTo(a, 3)); Assert.Throws<ArgumentException>(() => c.CopyTo(a, 1)); }
static void TestAdd() { asn = new AsnEncodedDataCollection(); try { asn.Add(null); Result = false; } catch (ArgumentNullException) { Result = true; } catch (Exception) { Result = false; } //add normal case AsnEncodedData data1 = new AsnEncodedData(oid, new byte[] { 1, 2, 3 }); AsnEncodedData data2 = new AsnEncodedData(oid, new byte[] { 5, 6, 7 }); asn = new AsnEncodedDataCollection(data1); asn.Add(data2); Result = asn.Count == 2; //Add where OIDs don't match data2 = new AsnEncodedData(badOid, new byte[] { 1, 2, 3, 4 }); asn = GetCollection(data1); try { asn.Add(data2); Result = false; } catch (CryptographicException) { Result = true; } catch (Exception e) { Result = false; Console.WriteLine(e); } //add w/ a empty Oid data2.Oid = new Oid(); asn = GetCollection(data1); try { asn.Add(data2); Result = false; } catch (CryptographicException) { Result = true; } catch (Exception e) { Result = false; Console.WriteLine(e); } }