示例#1
0
        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.TryReadPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpEncryptedContent))
                {
                    decoded.EncryptedContent = tmpEncryptedContent;
                }
                else
                {
                    decoded.EncryptedContent = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0));
                }
            }


            sequenceReader.ThrowIfNotEmpty();
        }
示例#2
0
        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.TryReadPrimitiveOctetStringBytes(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();
        }
示例#3
0
        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, 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.ReadEncodedValue();
            }

            System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.SignatureAlgorithm);

            if (sequenceReader.TryReadPrimitiveOctetStringBytes(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();
        }
示例#6
0
            public DigestInfo(byte[] asn1der)
            {
                var asnReader = new AsnReader(asn1der, AsnEncodingRules.DER).ReadSequence();

                this.digestAlgorithm = new DigestAlgorithmIdentifier(asnReader);
                this.digest          = asnReader.ReadOctetString();

                asnReader.ThrowIfNotEmpty();
            }
示例#7
0
        public BindRequest TryDecode(AsnReader reader, byte[] input)
        {
            Asn1Tag   bindRequestApplication = new Asn1Tag(TagClass.Application, 0);
            AsnReader subReader = reader.ReadSequence(bindRequestApplication);

            BigInteger version = subReader.ReadInteger();

            string nameString = System.Text.Encoding.ASCII.GetString(subReader.ReadOctetString());

            Asn1Tag authContext = new Asn1Tag(TagClass.ContextSpecific, 0);
            string  authString  = System.Text.Encoding.ASCII.GetString(subReader.ReadOctetString(authContext));

            subReader.ThrowIfNotEmpty();
            reader.ThrowIfNotEmpty();

            BindRequest bindRequest = new BindRequest(version, nameString, authString);

            return(bindRequest);
        }
示例#8
0
        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();
        }
示例#9
0
        public ExtendedRequest TryDecode(AsnReader reader, byte[] input)
        {
            Asn1Tag   bindRequestApplication = new Asn1Tag(TagClass.Application, 23);
            AsnReader subReader  = reader.ReadSequence(bindRequestApplication);
            Asn1Tag   contextTag = new Asn1Tag(TagClass.ContextSpecific, 0);

            string requestName  = System.Text.Encoding.ASCII.GetString(subReader.ReadOctetString(contextTag));
            string?requestValue = null;

            if (subReader.HasData)
            {
                requestValue = System.Text.Encoding.ASCII.GetString(subReader.ReadOctetString(contextTag));
            }

            return(new ExtendedRequest
            {
                RequestName = requestName,
                RequestValue = requestValue,
            });
        }
        public static void TagMustBeCorrect_Custom(AsnEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x87, 2, 0, 0x80 };
            byte[]    output    = new byte[inputData.Length];
            AsnReader reader    = new AsnReader(inputData, ruleSet);

            Asn1Tag wrongTag1  = new Asn1Tag(TagClass.Application, 0);
            Asn1Tag wrongTag2  = new Asn1Tag(TagClass.ContextSpecific, 1);
            Asn1Tag correctTag = new Asn1Tag(TagClass.ContextSpecific, 7);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.TryReadPrimitiveOctetString(out _, Asn1Tag.Null));
            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.TryReadOctetString(output, out _, Asn1Tag.Null));
            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.ReadOctetString(Asn1Tag.Null));

            Assert.True(reader.HasData, "HasData after bad universal tag");

            Assert.Throws <AsnContentException>(() => reader.TryReadPrimitiveOctetString(out _));
            Assert.Throws <AsnContentException>(() => reader.TryReadOctetString(output, out _));
            Assert.Throws <AsnContentException>(() => reader.ReadOctetString());
            Assert.True(reader.HasData, "HasData after default tag");

            Assert.Throws <AsnContentException>(() => reader.TryReadPrimitiveOctetString(out _, wrongTag1));
            Assert.Throws <AsnContentException>(() => reader.TryReadOctetString(output, out _, wrongTag1));
            Assert.Throws <AsnContentException>(() => reader.ReadOctetString(wrongTag1));
            Assert.True(reader.HasData, "HasData after wrong custom class");

            Assert.Throws <AsnContentException>(() => reader.TryReadPrimitiveOctetString(out _, wrongTag2));
            Assert.Throws <AsnContentException>(() => reader.TryReadOctetString(output, out _, wrongTag2));
            Assert.Throws <AsnContentException>(() => reader.ReadOctetString(wrongTag2));
            Assert.True(reader.HasData, "HasData after wrong custom tag value");

            Assert.True(reader.TryReadPrimitiveOctetString(out ReadOnlyMemory <byte> value, correctTag));
            Assert.Equal("0080", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after reading value");

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(reader.TryReadOctetString(output.AsSpan(1), out int written, correctTag));
            Assert.Equal("0080", output.AsSpan(1, written).ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after reading value");

            reader = new AsnReader(inputData, ruleSet);

            byte[] output2 = reader.ReadOctetString(correctTag);
            Assert.Equal("0080", output2.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after reading value");
        }
        private static void ReadOctetStringBytes_Throws_Helper(
            AsnEncodingRules ruleSet,
            byte[] input)
        {
            AsnReader reader = new AsnReader(input, ruleSet);

            Assert.Throws <AsnContentException>(
                () =>
            {
                reader.ReadOctetString();
            });
        }
        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();
        }
        //public Rfc3171Accuracy_ Accuracy { get; private set; }
        //public ReadOnlyMemory<byte> Ordering { get; private set; }
        //public ReadOnlyMemory<byte> Nonce { get; private set; }
        //public ReadOnlyMemory<byte> Tsa { get; private set; }
        //public ReadOnlyMemory<byte> Extensions { get; private set; }


        public static Rfc3161TstInfo From(EncapsulatedContentInfo content)
        {
            /*  EncapsulatedContentInfo ::= SEQUENCE {
             *      eContentType ContentType,
             *      eContent [0] EXPLICIT OCTET STRING OPTIONAL } */
            // note: the content field is read with ReadEncodedValue
            // because it would fail with ReadOctetString when reading the *.p7s.

            // just unwrap OCTET STRING node, [0] OPTIONAL is already stripped inside EncapsulatedContentInfo
            var reader = new AsnReader(content.Content, AsnEncodingRules.DER);
            var data   = reader.ReadOctetString();

            return(Decode(data));
        }
示例#14
0
        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();
        }
示例#16
0
        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 rc2Params = Rc2CbcParameters.Decode(
                    contentEncryptionAlgorithm.Parameters.Value,
                    AsnEncodingRules.BER);

                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);
                }

                try
                {
                    AsnReader reader = new AsnReader(contentEncryptionAlgorithm.Parameters.Value, AsnEncodingRules.BER);
                    alg.IV = reader.ReadOctetString();

                    if (alg.IV.Length != alg.BlockSize / 8)
                    {
                        throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                    }
                }
                catch (AsnContentException e)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
                }
            }

            return(alg);
        }
示例#17
0
        private IFilterChoice DecodeSearchFilter(AsnReader reader)
        {
            switch (reader.PeekTag().TagValue)
            {
            case 0:
                return(new AndFilter {
                    Filters = DecodeRecursiveFilterSets(reader)
                });

            case 1:
                return(new OrFilter {
                    Filters = DecodeRecursiveFilterSets(reader)
                });

            case 2:
                return(new NotFilter {
                    Filter = DecodeSearchFilter(reader)
                });

            case 3:
                return(DecodeAttributeValueAssertionFilter <EqualityMatchFilter>(reader));

            case 4:
                return(DecodeSubstringFilter(reader));

            case 5:
                return(DecodeAttributeValueAssertionFilter <GreaterOrEqualFilter>(reader));

            case 6:
                return(DecodeAttributeValueAssertionFilter <LessOrEqualFilter>(reader));

            case 7:
                return(new PresentFilter {
                    Value = System.Text.Encoding.ASCII.GetString(reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, reader.PeekTag().TagValue)))
                });

            case 8:
                return(DecodeAttributeValueAssertionFilter <ApproxMatchFilter>(reader));

            default:
                throw new NotImplementedException("Cannot decode the tag: " + reader.PeekTag().TagValue);
            }
        }
        public ReadOnlyMemory <byte> GetSubjectKeyIdentifier()
        {
            if (ExtnID != Oids.SubjectKeyIdentifier)
            {
                throw new InvalidOperationException($"Invalid ExtnID: {ExtnID}");
            }

            var reader = new AsnReader(ExtnValue, AsnEncodingRules.DER);

            if (reader.PeekTag().HasSameClassAndValue(Asn1Tag.PrimitiveOctetString))
            {
                var value = reader.ReadOctetString();

                reader.ThrowIfNotEmpty();
                return(value);
            }

            throw new CryptographicException();
        }
示例#19
0
        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.Asn1.DigestInfoAsn.Decode(sequenceReader, out decoded.Mac);

            if (sequenceReader.TryReadPrimitiveOctetStringBytes(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();
        }
        internal static void Decode <T>(AsnReader reader, Asn1Tag expectedTag, out T decoded)
            where T : KrbDHReplyInfo, new()
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = new T();
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);
            AsnReader explicitReader;


            if (sequenceReader.TryReadPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out ReadOnlyMemory <byte> tmpDHSignedData))
            {
                decoded.DHSignedData = tmpDHSignedData;
            }
            else
            {
                decoded.DHSignedData = sequenceReader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0));
            }


            if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1)))
            {
                explicitReader = sequenceReader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 1));

                if (explicitReader.TryReadPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpServerDHNonce))
                {
                    decoded.ServerDHNonce = tmpServerDHNonce;
                }
                else
                {
                    decoded.ServerDHNonce = explicitReader.ReadOctetString();
                }

                explicitReader.ThrowIfNotEmpty();
            }


            sequenceReader.ThrowIfNotEmpty();
        }
        public static void TryReadOctetStringBytes_Success(
            AsnEncodingRules ruleSet,
            string inputHex,
            string expectedHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            byte[]    output    = new byte[expectedHex.Length / 2];
            AsnReader reader    = new AsnReader(inputData, ruleSet);

            bool didRead = reader.TryReadOctetString(
                output,
                out int bytesWritten);

            Assert.True(didRead, "reader.TryReadOctetString");
            Assert.Equal(expectedHex, output.AsSpan(0, bytesWritten).ByteArrayToHex());

            reader = new AsnReader(inputData, ruleSet);
            byte[] output2 = reader.ReadOctetString();
            Assert.Equal(output, output2);
        }
示例#22
0
        public SearchRequest TryDecode(AsnReader reader, byte[] input)
        {
            SearchRequest searchRequest = new SearchRequest
            {
                RawPacket = input,
            };

            Asn1Tag   bindRequestApplication = new Asn1Tag(TagClass.Application, 3);
            AsnReader subReader = reader.ReadSequence(bindRequestApplication);

            searchRequest.BaseObject = System.Text.Encoding.ASCII.GetString(subReader.ReadOctetString());
            SearchRequest.ScopeEnum        scope = subReader.ReadEnumeratedValue <SearchRequest.ScopeEnum>();
            SearchRequest.DerefAliasesEnum deref = subReader.ReadEnumeratedValue <SearchRequest.DerefAliasesEnum>();
            BigInteger sizeLimit = subReader.ReadInteger();
            BigInteger timeLimit = subReader.ReadInteger();
            bool       typesOnly = subReader.ReadBoolean();

            searchRequest.Filter = DecodeSearchFilter(subReader);

            return(searchRequest);
        }
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1SubstringFilter decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = new Asn1SubstringFilter();
            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 Substrings
            {
                collectionReader = sequenceReader.ReadSequence();
                var           tmpList = new List <Asn1Substring>();
                Asn1Substring tmpItem;

                while (collectionReader.HasData)
                {
                    Asn1Substring.Decode(collectionReader, out tmpItem);
                    tmpList.Add(tmpItem);
                }

                decoded.Substrings = tmpList.ToArray();
            }


            sequenceReader.ThrowIfNotEmpty();
        }
示例#24
0
        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();
        }
示例#25
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out MessageImprint decoded)
        {
            if (reader == null)
                throw new ArgumentNullException(nameof(reader));

            decoded = default;
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);
            
            System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.HashAlgorithm);

            if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory<byte> tmpHashedMessage))
            {
                decoded.HashedMessage = tmpHashedMessage;
            }
            else
            {
                decoded.HashedMessage = sequenceReader.ReadOctetString();
            }


            sequenceReader.ThrowIfNotEmpty();
        }
        public static void TryCopyOctetStringBytes_Success_CER_MaxPrimitiveLength()
        {
            // 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;

            // Content
            input[4]    = 0x02;
            input[5]    = 0xA0;
            input[1002] = 0xA5;
            input[1003] = 0xFC;

            byte[] output = new byte[1000];

            AsnReader reader = new AsnReader(input, AsnEncodingRules.CER);

            bool success = reader.TryReadOctetString(
                output,
                out int bytesWritten);

            Assert.True(success, "reader.TryReadOctetString");
            Assert.Equal(1000, bytesWritten);

            Assert.Equal(
                input.AsSpan(4).ByteArrayToHex(),
                output.ByteArrayToHex());

            reader = new AsnReader(input, AsnEncodingRules.CER);
            byte[] output2 = reader.ReadOctetString();
            Assert.Equal(output, output2);
        }
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out RecipientKeyIdentifier decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = default;
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);


            if (sequenceReader.TryReadPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpSubjectKeyIdentifier))
            {
                decoded.SubjectKeyIdentifier = tmpSubjectKeyIdentifier;
            }
            else
            {
                decoded.SubjectKeyIdentifier = sequenceReader.ReadOctetString();
            }


            if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.GeneralizedTime))
            {
                decoded.Date = sequenceReader.ReadGeneralizedTime();
            }


            if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(Asn1Tag.Sequence))
            {
                System.Security.Cryptography.Pkcs.Asn1.OtherKeyAttributeAsn tmpOther;
                System.Security.Cryptography.Pkcs.Asn1.OtherKeyAttributeAsn.Decode(sequenceReader, out tmpOther);
                decoded.Other = tmpOther;
            }


            sequenceReader.ThrowIfNotEmpty();
        }
        public static void TryReadPrimitiveOctetStringBytes_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 <AsnContentException>(
                () => reader.TryReadPrimitiveOctetString(out ReadOnlyMemory <byte> contents));

            Assert.Throws <AsnContentException>(
                () => reader.TryReadOctetString(new byte[input.Length], out _));

            Assert.Throws <AsnContentException>(() => reader.ReadOctetString());
        }
示例#29
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out RecipientEncryptedKeyAsn decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = default;
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);

            System.Security.Cryptography.Pkcs.Asn1.KeyAgreeRecipientIdentifierAsn.Decode(sequenceReader, out decoded.Rid);

            if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpEncryptedKey))
            {
                decoded.EncryptedKey = tmpEncryptedKey;
            }
            else
            {
                decoded.EncryptedKey = sequenceReader.ReadOctetString();
            }


            sequenceReader.ThrowIfNotEmpty();
        }
示例#30
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out DigestInfoAsn decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = default;
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);

            System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(sequenceReader, out decoded.DigestAlgorithm);

            if (sequenceReader.TryReadPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpDigest))
            {
                decoded.Digest = tmpDigest;
            }
            else
            {
                decoded.Digest = sequenceReader.ReadOctetString();
            }


            sequenceReader.ThrowIfNotEmpty();
        }