internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1AttributeValueAssertion decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1AttributeValueAssertion(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpDescription)) { decoded.Description = tmpDescription; } else { decoded.Description = sequenceReader.ReadOctetString(); } if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpValue)) { decoded.Value = tmpValue; } else { decoded.Value = sequenceReader.ReadOctetString(); } sequenceReader.ThrowIfNotEmpty(); }
public static void TagMustBeCorrect_Custom(PublicEncodingRules ruleSet) { byte[] inputData = { 0x87, 2, 0, 0x80 }; AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); AssertExtensions.Throws <ArgumentException>( "expectedTag", () => reader.TryGetPrimitiveOctetStringBytes(Asn1Tag.Null, out _)); Assert.True(reader.HasData, "HasData after bad universal tag"); Assert.Throws <CryptographicException>(() => reader.TryGetPrimitiveOctetStringBytes(out _)); Assert.True(reader.HasData, "HasData after default tag"); Assert.Throws <CryptographicException>( () => reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.Application, 0), out _)); Assert.True(reader.HasData, "HasData after wrong custom class"); Assert.Throws <CryptographicException>( () => reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 1), out _)); Assert.True(reader.HasData, "HasData after wrong custom tag value"); Assert.True( reader.TryGetPrimitiveOctetStringBytes( new Asn1Tag(TagClass.ContextSpecific, 7), out ReadOnlyMemory <byte> value)); Assert.Equal("0080", value.ByteArrayToHex()); Assert.False(reader.HasData, "HasData after reading value"); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1MatchingRuleAssertion decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1MatchingRuleAssertion(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 1), out ReadOnlyMemory <byte> tmpMatchingRule)) { decoded.MatchingRule = tmpMatchingRule; } else { decoded.MatchingRule = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 1)); } } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 2))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 2), out ReadOnlyMemory <byte> tmpType)) { decoded.Type = tmpType; } else { decoded.Type = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 2)); } } if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 3), out ReadOnlyMemory <byte> tmpValue)) { decoded.Value = tmpValue; } else { decoded.Value = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 3)); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 4))) { decoded.DNAttributes = sequenceReader.ReadBoolean(new Asn1Tag(TagClass.ContextSpecific, 4)); } sequenceReader.ThrowIfNotEmpty(); }
internal static void Decode(AsnReader reader, out Asn1Substring decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1Substring(); Asn1Tag tag = reader.PeekTag(); if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpInitial)) { decoded.Initial = tmpInitial; } else { decoded.Initial = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { if (reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 1), out ReadOnlyMemory <byte> tmpAny)) { decoded.Any = tmpAny; } else { decoded.Any = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 1)); } } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 2))) { if (reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 2), out ReadOnlyMemory <byte> tmpFinal)) { decoded.Final = tmpFinal; } else { decoded.Final = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 2)); } } else { throw new CryptographicException(); } }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out EssCertId decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpHash)) { decoded.Hash = tmpHash; } else { decoded.Hash = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Sequence)) { System.Security.Cryptography.Pkcs.Asn1.CadesIssuerSerial tmpIssuerSerial; System.Security.Cryptography.Pkcs.Asn1.CadesIssuerSerial.Decode(sequenceReader, out tmpIssuerSerial); decoded.IssuerSerial = tmpIssuerSerial; } sequenceReader.ThrowIfNotEmpty(); }
public static void ExpectedTag_IgnoresConstructed( PublicEncodingRules ruleSet, string inputHex, PublicTagClass tagClass, int tagValue) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); Assert.True( reader.TryGetPrimitiveOctetStringBytes( new Asn1Tag((TagClass)tagClass, tagValue, true), out ReadOnlyMemory <byte> val1)); Assert.False(reader.HasData); reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); Assert.True( reader.TryGetPrimitiveOctetStringBytes( new Asn1Tag((TagClass)tagClass, tagValue, false), out ReadOnlyMemory <byte> val2)); Assert.False(reader.HasData); Assert.Equal(val1.ByteArrayToHex(), val2.ByteArrayToHex()); }
internal static void Decode(AsnReader reader, out SignerIdentifierAsn decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; Asn1Tag tag = reader.PeekTag(); if (tag.HasSameClassAndValue(Asn1Tag.Sequence)) { System.Security.Cryptography.Pkcs.Asn1.IssuerAndSerialNumberAsn tmpIssuerAndSerialNumber; System.Security.Cryptography.Pkcs.Asn1.IssuerAndSerialNumberAsn.Decode(reader, out tmpIssuerAndSerialNumber); decoded.IssuerAndSerialNumber = tmpIssuerAndSerialNumber; } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpSubjectKeyIdentifier)) { decoded.SubjectKeyIdentifier = tmpSubjectKeyIdentifier; } else { decoded.SubjectKeyIdentifier = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } else { throw new CryptographicException(); } }
internal static void Decode(AsnReader reader, out Asn1AuthenticationChoice decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1AuthenticationChoice(); Asn1Tag tag = reader.PeekTag(); if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpSimple)) { decoded.Simple = tmpSimple; } else { decoded.Simple = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 3))) { Asn1SaslCredentials tmpSasl; Asn1SaslCredentials.Decode(reader, new Asn1Tag(TagClass.ContextSpecific, 3), out tmpSasl); decoded.Sasl = tmpSasl; } else { throw new CryptographicException(); } }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out KeyTransRecipientInfoAsn decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (!sequenceReader.TryReadInt32(out decoded.Version)) { sequenceReader.ThrowIfNotEmpty(); } System.Security.Cryptography.Pkcs.Asn1.RecipientIdentifierAsn.Decode(sequenceReader, out decoded.Rid); System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.KeyEncryptionAlgorithm); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpEncryptedKey)) { decoded.EncryptedKey = tmpEncryptedKey; } else { decoded.EncryptedKey = sequenceReader.ReadOctetString(); } sequenceReader.ThrowIfNotEmpty(); }
public override byte[] GetSubjectKeyIdentifier(X509Certificate2 certificate) { Debug.Assert(certificate != null); X509Extension extension = certificate.Extensions[Oids.SubjectKeyIdentifier]; if (extension == null) { // Construct the value from the public key info. extension = new X509SubjectKeyIdentifierExtension( certificate.PublicKey, X509SubjectKeyIdentifierHashAlgorithm.CapiSha1, false); } // Certificates are DER encoded. AsnReader reader = new AsnReader(extension.RawData, AsnEncodingRules.DER); if (reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents)) { reader.ThrowIfNotEmpty(); return(contents.ToArray()); } // TryGetPrimitiveOctetStringBytes will have thrown if the next tag wasn't // Universal (primitive) OCTET STRING, since we're in DER mode. // So there's really no way we can get here. Debug.Fail($"TryGetPrimitiveOctetStringBytes returned false in DER mode"); throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out EncryptedContentInfoAsn decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; AsnReader sequenceReader = reader.ReadSequence(expectedTag); decoded.ContentType = sequenceReader.ReadObjectIdentifierAsString(); System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.ContentEncryptionAlgorithm); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpEncryptedContent)) { decoded.EncryptedContent = tmpEncryptedContent; } else { decoded.EncryptedContent = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } sequenceReader.ThrowIfNotEmpty(); }
public static void TryGetOctetStringBytes_Success_CER_MaxLength() { // CER says that the maximum encoding length for an OctetString primitive // is 1000. // // So we need 04 [1000] { 1000 anythings } // 1000 => 0x3E8, so the length encoding is 82 03 E8. // 1000 + 3 + 1 == 1004 byte[] input = new byte[1004]; input[0] = 0x04; input[1] = 0x82; input[2] = 0x03; input[3] = 0xE8; // Contents input[4] = 0x02; input[5] = 0xA0; input[1002] = 0xA5; input[1003] = 0xFC; AsnReader reader = new AsnReader(input, AsnEncodingRules.CER); bool success = reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents); Assert.True(success, "reader.TryGetOctetStringBytes"); Assert.Equal(1000, contents.Length); // Check that it is, in fact, the same memory. No copies with this API. Assert.True( Unsafe.AreSame( ref MemoryMarshal.GetReference(contents.Span), ref input[4])); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1BindRequest decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1BindRequest(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (!sequenceReader.TryReadUInt8(out decoded.Version)) { sequenceReader.ThrowIfNotEmpty(); } if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpName)) { decoded.Name = tmpName; } else { decoded.Name = sequenceReader.ReadOctetString(); } Asn1AuthenticationChoice.Decode(sequenceReader, out decoded.Authentication); sequenceReader.ThrowIfNotEmpty(); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1ModifyDNRequest decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1ModifyDNRequest(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpEntry)) { decoded.Entry = tmpEntry; } else { decoded.Entry = sequenceReader.ReadOctetString(); } if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpNewRDN)) { decoded.NewRDN = tmpNewRDN; } else { decoded.NewRDN = sequenceReader.ReadOctetString(); } decoded.DeleteOldRDN = sequenceReader.ReadBoolean(); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpNewSuperior)) { decoded.NewSuperior = tmpNewSuperior; } else { decoded.NewSuperior = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } sequenceReader.ThrowIfNotEmpty(); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out SignerInfoAsn decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; AsnReader sequenceReader = reader.ReadSequence(expectedTag); AsnReader collectionReader; if (!sequenceReader.TryReadInt32(out decoded.Version)) { sequenceReader.ThrowIfNotEmpty(); } System.Security.Cryptography.Pkcs.Asn1.SignerIdentifierAsn.Decode(sequenceReader, out decoded.Sid); System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.DigestAlgorithm); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { decoded.SignedAttributes = sequenceReader.GetEncodedValue(); } System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.SignatureAlgorithm); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpSignatureValue)) { decoded.SignatureValue = tmpSignatureValue; } else { decoded.SignatureValue = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { // Decode SEQUENCE OF for UnsignedAttributes { collectionReader = sequenceReader.ReadSetOf(new Asn1Tag(TagClass.ContextSpecific, 1)); var tmpList = new List <System.Security.Cryptography.Asn1.AttributeAsn>(); System.Security.Cryptography.Asn1.AttributeAsn tmpItem; while (collectionReader.HasData) { System.Security.Cryptography.Asn1.AttributeAsn.Decode(collectionReader, out tmpItem); tmpList.Add(tmpItem); } decoded.UnsignedAttributes = tmpList.ToArray(); } } sequenceReader.ThrowIfNotEmpty(); }
internal static ReadOnlyMemory <byte> GetContent( ReadOnlyMemory <byte> wrappedContent, string contentType) { // Read the input. // // PKCS7's id-data is written in both PKCS#7 and CMS as an OCTET STRING wrapping // the arbitrary bytes, so the OCTET STRING must always be present. // // For other types, CMS says to always write an OCTET STRING, and to put the properly // encoded data within it. // PKCS#7 originally ommitted the OCTET STRING wrapper for this model, so this is the // dynamic adapter. // // See https://tools.ietf.org/html/rfc5652#section-5.2.1 byte[] rented = null; int bytesWritten = 0; try { AsnReader reader = new AsnReader(wrappedContent, AsnEncodingRules.BER); if (reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> inner)) { return(inner); } rented = ArrayPool <byte> .Shared.Rent(wrappedContent.Length); if (!reader.TryCopyOctetStringBytes(rented, out bytesWritten)) { Debug.Fail($"TryCopyOctetStringBytes failed with an array larger than the encoded value"); throw new CryptographicException(); } return(rented.AsSpan(0, bytesWritten).ToArray()); } catch (Exception) { if (contentType == Oids.Pkcs7Data) { throw; } } finally { if (rented != null) { rented.AsSpan(0, bytesWritten).Clear(); ArrayPool <byte> .Shared.Return(rented); } } // PKCS#7 encoding for something other than id-data. Debug.Assert(contentType != Oids.Pkcs7Data); return(wrappedContent); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1Control decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1Control(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpType)) { decoded.Type = tmpType; } else { decoded.Type = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Boolean)) { decoded.Criticality = sequenceReader.ReadBoolean(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.PrimitiveOctetString)) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpValue)) { decoded.Value = tmpValue; } else { decoded.Value = sequenceReader.ReadOctetString(); } } sequenceReader.ThrowIfNotEmpty(); }
public static void TagMustBeCorrect_Universal(PublicEncodingRules ruleSet) { byte[] inputData = { 4, 1, 0x7E }; AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); AssertExtensions.Throws <ArgumentException>( "expectedTag", () => reader.TryGetPrimitiveOctetStringBytes(Asn1Tag.Null, out _)); Assert.True(reader.HasData, "HasData after bad universal tag"); Assert.Throws <CryptographicException>( () => reader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out _)); Assert.True(reader.HasData, "HasData after wrong tag"); Assert.True(reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> value)); Assert.Equal("7E", value.ByteArrayToHex()); Assert.False(reader.HasData, "HasData after read"); }
public static void TryGetOctetStringBytes_Throws( string description, PublicEncodingRules ruleSet, string inputHex) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); Assert.Throws <CryptographicException>( () => reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents)); }
internal static byte[] DecodeX509SubjectKeyIdentifierExtension(byte[] encoded) { AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER); ReadOnlyMemory <byte> contents; if (!reader.TryGetPrimitiveOctetStringBytes(out contents)) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } reader.ThrowIfNotEmpty(); return(contents.ToArray()); }
public static void TryGetOctetStringBytes_Fails( string description, PublicEncodingRules ruleSet, string inputHex) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); bool didRead = reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents); Assert.False(didRead, "reader.TryGetOctetStringBytes"); Assert.Equal(0, contents.Length); }
public static void TryGetOctetStringBytes_Success( PublicEncodingRules ruleSet, int expectedLength, string inputHex) { byte[] inputData = inputHex.HexToByteArray(); AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); bool didRead = reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents); Assert.True(didRead, "reader.TryGetOctetStringBytes"); Assert.Equal(expectedLength, contents.Length); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1IntermediateResponse decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1IntermediateResponse(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpName)) { decoded.Name = tmpName; } else { decoded.Name = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); } } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 1), out ReadOnlyMemory <byte> tmpValue)) { decoded.Value = tmpValue; } else { decoded.Value = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 1)); } } sequenceReader.ThrowIfNotEmpty(); }
private static void AssertExtension(AsnReader extensions, string oid, bool critical, int index, byte[] bytes) { AsnReader extension = extensions.ReadSequence(); Assert.Equal(oid, extension.ReadObjectIdentifierAsString()); if (critical) { Assert.True(extension.ReadBoolean(), $"{oid} is critical"); } Assert.True(extension.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> extensionBytes)); AssertRefSame(extensionBytes, ref bytes[index], $"{oid} extension value is at byte {index}"); }
private static SymmetricAlgorithm OpenAlgorithm(AlgorithmIdentifierAsn contentEncryptionAlgorithm) { SymmetricAlgorithm alg = OpenAlgorithm(contentEncryptionAlgorithm.Algorithm); if (alg is RC2) { if (contentEncryptionAlgorithm.Parameters == null) { // Windows issues CRYPT_E_BAD_DECODE throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } Rc2CbcParameters.Decode( new AsnReader(contentEncryptionAlgorithm.Parameters.Value, AsnEncodingRules.BER), out Rc2CbcParameters rc2Params); alg.KeySize = rc2Params.GetEffectiveKeyBits(); alg.IV = rc2Params.Iv.ToArray(); } else { if (contentEncryptionAlgorithm.Parameters == null) { // Windows issues CRYPT_E_BAD_DECODE throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } AsnReader reader = new AsnReader(contentEncryptionAlgorithm.Parameters.Value, AsnEncodingRules.BER); if (reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> primitiveBytes)) { alg.IV = primitiveBytes.ToArray(); } else { byte[] iv = new byte[alg.BlockSize / 8]; if (!reader.TryCopyOctetStringBytes(iv, out int bytesWritten) || bytesWritten != iv.Length) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } alg.IV = iv; } } return(alg); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1SaslCredentials decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1SaslCredentials(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpMechanism)) { decoded.Mechanism = tmpMechanism; } else { decoded.Mechanism = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.PrimitiveOctetString)) { if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpCredentials)) { decoded.Credentials = tmpCredentials; } else { decoded.Credentials = sequenceReader.ReadOctetString(); } } sequenceReader.ThrowIfNotEmpty(); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1PartialAttribute decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1PartialAttribute(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); AsnReader collectionReader; if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpType)) { decoded.Type = tmpType; } else { decoded.Type = sequenceReader.ReadOctetString(); } // Decode SEQUENCE OF for Values { collectionReader = sequenceReader.ReadSetOf(); var tmpList = new List <ReadOnlyMemory <byte> >(); ReadOnlyMemory <byte> tmpItem; while (collectionReader.HasData) { if (collectionReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmp)) { tmpItem = tmp; } else { tmpItem = collectionReader.ReadOctetString(); } tmpList.Add(tmpItem); } decoded.Values = tmpList.ToArray(); } sequenceReader.ThrowIfNotEmpty(); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out MacData decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = default; AsnReader sequenceReader = reader.ReadSequence(expectedTag); AsnReader defaultReader; System.Security.Cryptography.Pkcs.Asn1.DigestInfoAsn.Decode(sequenceReader, out decoded.Mac); if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpMacSalt)) { decoded.MacSalt = tmpMacSalt; } else { decoded.MacSalt = sequenceReader.ReadOctetString(); } if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Integer)) { if (!sequenceReader.TryReadInt32(out decoded.IterationCount)) { sequenceReader.ThrowIfNotEmpty(); } } else { defaultReader = new AsnReader(s_defaultIterationCount, AsnEncodingRules.DER); if (!defaultReader.TryReadInt32(out decoded.IterationCount)) { defaultReader.ThrowIfNotEmpty(); } } sequenceReader.ThrowIfNotEmpty(); }
public static void TryGetOctetStringBytes_Throws_CER_TooLong() { // CER says that the maximum encoding length for an OctetString primitive // is 1000. // // So we need 04 [1001] { 1001 0x00s } // 1001 => 0x3E9, so the length encoding is 82 03 E9. // 1001 + 3 + 1 == 1005 byte[] input = new byte[1005]; input[0] = 0x04; input[1] = 0x82; input[2] = 0x03; input[3] = 0xE9; AsnReader reader = new AsnReader(input, AsnEncodingRules.CER); Assert.Throws <CryptographicException>( () => reader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> contents)); }
internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1ModifyRequest decoded) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } decoded = new Asn1ModifyRequest(); AsnReader sequenceReader = reader.ReadSequence(expectedTag); AsnReader collectionReader; if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpObject)) { decoded.Object = tmpObject; } else { decoded.Object = sequenceReader.ReadOctetString(); } // Decode SEQUENCE OF for Changes { collectionReader = sequenceReader.ReadSequence(); var tmpList = new List <Asn1Change>(); Asn1Change tmpItem; while (collectionReader.HasData) { Asn1Change.Decode(collectionReader, out tmpItem); tmpList.Add(tmpItem); } decoded.Changes = tmpList.ToArray(); } sequenceReader.ThrowIfNotEmpty(); }