internal static NegTokenInit Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
        internal static Rfc3161TimeStampResp Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out Rfc3161TimeStampResp decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #3
0
 internal static KrbKdcReqBody Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
Example #4
0
        internal static DistributionPointNameAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            try
            {
                AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

                DecodeCore(ref reader, encoded, out DistributionPointNameAsn decoded);
                reader.ThrowIfNotEmpty();
                return(decoded);
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
        }
Example #5
0
        internal static CertificationRequestAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out CertificationRequestAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #6
0
        internal static OaepParamsAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out OaepParamsAsn decoded);
            reader.ThrowIfNotEmpty();
            return decoded;
        }
Example #7
0
 internal static OtherNameAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
        public static EncryptedContentInfoAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

            Decode(ref reader, expectedTag, encoded, out EncryptedContentInfoAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #9
0
 internal static SubjectPublicKeyInfoAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
        internal static KeyTransRecipientInfoAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

            Decode(ref reader, expectedTag, encoded, out KeyTransRecipientInfoAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
 public static EncryptedContentInfoAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
 internal static KeyTransRecipientInfoAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
 internal static X509ExtensionAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
        internal static NegTokenInit Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out NegTokenInit decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
 public static PssParamsAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
Example #16
0
        internal static SubjectPublicKeyInfoAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out SubjectPublicKeyInfoAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #17
0
 internal static OaepParamsAsn Decode(ReadOnlyMemory<byte> encoded, AsnEncodingRules ruleSet)
 {
     return Decode(Asn1Tag.Sequence, encoded, ruleSet);
 }
 internal static EncryptedDataAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
Example #19
0
        internal static KeyAgreeRecipientIdentifierAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

            Decode(ref reader, encoded, out KeyAgreeRecipientIdentifierAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #20
0
        public static void ReadSequenceOf_PreservesOptions(AsnEncodingRules ruleSet)
        {
            // [5] (UtcTime) 500102123456Z
            // UtcTime 120102235959Z
            //
            // They're sorted backwards, though.
            const string PayloadHex =
                "850D3530303130323132333435365A" +
                "170D3132303130323233353935395A";

            byte[] inputData;

            // Build the rule-specific form of SEQUENCE { [PRIVATE 9] SEQUENCE { SET-OF { dates }, NULL } }
            // The outer Set-Of is also invalid, because the NULL should be first.
            if (ruleSet == AsnEncodingRules.DER)
            {
                inputData = ("3024" + "E922" + "A21E" + PayloadHex + "0500").HexToByteArray();
            }
            else
            {
                string inputHex = "3080" + "E980" + "A280" + PayloadHex + "0000" + "0500" + "0000" + "0000";
                inputData = inputHex.HexToByteArray();
            }

            AsnReaderOptions options = new AsnReaderOptions
            {
                SkipSetSortOrderVerification = true,
                UtcTimeTwoDigitYearMax       = 2011,
            };

            AsnReader initial = new AsnReader(inputData, ruleSet, options);
            AsnReader outer   = initial.ReadSequence();

            Assert.False(initial.HasData);
            AsnReader inner = outer.ReadSequence(new Asn1Tag(TagClass.Private, 9));

            Assert.False(outer.HasData);

            Asn1Tag setTag = new Asn1Tag(TagClass.ContextSpecific, 2);

            if (ruleSet != AsnEncodingRules.BER)
            {
                Assert.Throws <AsnContentException>(() => inner.ReadSetOf(false, setTag));
            }

            // This confirms that we've passed SkipSetOrderVerification this far.
            AsnReader setOf = inner.ReadSetOf(setTag);

            Assert.True(inner.HasData);

            Assert.Equal(
                new DateTimeOffset(1950, 1, 2, 12, 34, 56, TimeSpan.Zero),
                setOf.ReadUtcTime(new Asn1Tag(TagClass.ContextSpecific, 5)));

            // This confirms that we've passed UtcTimeTwoDigitYearMax,
            // the default would call this 2012.
            Assert.Equal(
                new DateTimeOffset(1912, 1, 2, 23, 59, 59, TimeSpan.Zero),
                setOf.ReadUtcTime());

            Assert.False(setOf.HasData);

            inner.ReadNull();
            Assert.False(inner.HasData);

            setOf.ThrowIfNotEmpty();
            inner.ThrowIfNotEmpty();
            outer.ThrowIfNotEmpty();
            initial.ThrowIfNotEmpty();
        }
Example #21
0
        internal static CertificatePolicyMappingAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

            Decode(ref reader, expectedTag, encoded, out CertificatePolicyMappingAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #22
0
        /// <summary>
        ///   Reads a NamedBitList from <paramref name="source"/> with a specified tag under
        ///   the specified encoding rules, converting it to the
        ///   [<see cref="FlagsAttribute"/>] enum specified by <paramref name="flagsEnumType"/>.
        /// </summary>
        /// <param name="source">The buffer containing encoded data.</param>
        /// <param name="ruleSet">The encoding constraints to use when interpreting the data.</param>
        /// <param name="flagsEnumType">Type object representing the destination type.</param>
        /// <param name="bytesConsumed">
        ///   When this method returns, the total number of bytes for the encoded value.
        ///   This parameter is treated as uninitialized.
        /// </param>
        /// <param name="expectedTag">
        ///   The tag to check for before reading, or <see langword="null"/> for the default tag (Universal 3).
        /// </param>
        /// <returns>
        ///   The NamedBitList value converted to a <paramref name="flagsEnumType"/>.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///   <paramref name="ruleSet"/> is not defined.
        /// </exception>
        /// <exception cref="AsnContentException">
        ///   the next value does not have the correct tag.
        ///
        ///   -or-
        ///
        ///   the length encoding is not valid under the current encoding rules.
        ///
        ///   -or-
        ///
        ///   the contents are not valid under the current encoding rules.
        ///
        ///   -or-
        ///-
        ///   the encoded value is too big to fit in a <paramref name="flagsEnumType"/> value.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <paramref name="flagsEnumType"/> is not an enum type.
        ///
        ///   -or-
        ///
        ///   <paramref name="flagsEnumType"/> was not declared with <see cref="FlagsAttribute"/>
        ///
        ///   -or-
        ///
        ///   <paramref name="expectedTag"/>.<see cref="Asn1Tag.TagClass"/> is
        ///   <see cref="TagClass.Universal"/>, but
        ///   <paramref name="expectedTag"/>.<see cref="Asn1Tag.TagValue"/> is not correct for
        ///   the method.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="flagsEnumType"/> is <see langword="null" />
        /// </exception>
        /// <seealso cref="ReadNamedBitListValue{TFlagsEnum}"/>
        public static Enum ReadNamedBitListValue(
            ReadOnlySpan <byte> source,
            AsnEncodingRules ruleSet,
            Type flagsEnumType,
            out int bytesConsumed,
            Asn1Tag?expectedTag = null)
        {
            if (flagsEnumType == null)
            {
                throw new ArgumentNullException(nameof(flagsEnumType));
            }

            // This will throw an ArgumentException if TEnum isn't an enum type,
            // so we don't need to validate it.
            Type backingType = flagsEnumType.GetEnumUnderlyingType();

            if (!flagsEnumType.IsDefined(typeof(FlagsAttribute), false))
            {
                throw new ArgumentException(
                          SR.Argument_NamedBitListRequiresFlagsEnum,
                          nameof(flagsEnumType));
            }

            Span <byte> stackSpan = stackalloc byte[sizeof(ulong)];
            int         sizeLimit = Marshal.SizeOf(backingType);

            stackSpan = stackSpan.Slice(0, sizeLimit);

            bool read = TryReadBitString(
                source,
                stackSpan,
                ruleSet,
                out int unusedBitCount,
                out int consumed,
                out int bytesWritten,
                expectedTag);

            if (!read)
            {
                throw new AsnContentException(
                          SR.Format(SR.ContentException_NamedBitListValueTooBig, flagsEnumType.Name));
            }

            Enum ret;

            if (bytesWritten == 0)
            {
                // The mode isn't relevant, zero is always zero.
                ret           = (Enum)Enum.ToObject(flagsEnumType, 0);
                bytesConsumed = consumed;
                return(ret);
            }

            ReadOnlySpan <byte> valueSpan = stackSpan.Slice(0, bytesWritten);

            // Now that the 0-bounds check is out of the way:
            //
            // T-REC-X.690-201508 sec 11.2.2
            if (ruleSet == AsnEncodingRules.DER ||
                ruleSet == AsnEncodingRules.CER)
            {
                byte lastByte = valueSpan[bytesWritten - 1];

                // No unused bits tests 0x01, 1 is 0x02, 2 is 0x04, etc.
                // We already know that TryCopyBitStringBytes checked that the
                // declared unused bits were 0, this checks that the last "used" bit
                // isn't also zero.
                byte testBit = (byte)(1 << unusedBitCount);

                if ((lastByte & testBit) == 0)
                {
                    throw new AsnContentException(SR.ContentException_InvalidUnderCerOrDer_TryBer);
                }
            }

            // Consider a NamedBitList defined as
            //
            //   SomeList ::= BIT STRING {
            //     a(0), b(1), c(2), d(3), e(4), f(5), g(6), h(7), i(8), j(9), k(10)
            //   }
            //
            // The BIT STRING encoding of (a | j) is
            //   unusedBitCount = 6,
            //   contents: 0x80 0x40  (0b10000000_01000000)
            //
            // A the C# exposure of this structure we adhere to is
            //
            // [Flags]
            // enum SomeList
            // {
            //     A = 1,
            //     B = 1 << 1,
            //     C = 1 << 2,
            //     ...
            // }
            //
            // Which happens to be exactly backwards from how the bits are encoded, but the complexity
            // only needs to live here.
            ret           = (Enum)Enum.ToObject(flagsEnumType, InterpretNamedBitListReversed(valueSpan));
            bytesConsumed = consumed;
            return(ret);
        }
Example #23
0
 internal static CertificationRequestAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
 internal static AlgorithmIdentifierAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
 internal static Rfc3161TimeStampResp Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
Example #26
0
 internal static PolicyInformation Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }
Example #27
0
        internal static DistributionPointNameAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, out DistributionPointNameAsn decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
Example #28
0
        internal static PolicyInformation Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            try
            {
                AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

                DecodeCore(ref reader, expectedTag, encoded, out PolicyInformation decoded);
                reader.ThrowIfNotEmpty();
                return(decoded);
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
        }
Example #29
0
        internal static KrbKdcReqBody Decode(Asn1Tag expectedTag, ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
        {
            AsnReader reader = new AsnReader(encoded, ruleSet);

            Decode(reader, expectedTag, out KrbKdcReqBody decoded);
            reader.ThrowIfNotEmpty();
            return(decoded);
        }
 internal static OriginatorPublicKeyAsn Decode(ReadOnlyMemory <byte> encoded, AsnEncodingRules ruleSet)
 {
     return(Decode(Asn1Tag.Sequence, encoded, ruleSet));
 }