コード例 #1
0
            static bool Method(
                ReadOnlySpan <byte> source,
                Span <byte> destination,
                AsnEncodingRules ruleSet,
                out int consumed,
                out int written)
            {
                bool ret = AsnDecoder.TryReadBitString(
                    source,
                    destination,
                    ruleSet,
                    out int unusedBitCount,
                    out consumed,
                    out written);

                if (ret)
                {
                    Assert.Equal(2, unusedBitCount);
                }

                return(ret);
            }
コード例 #2
0
        public static ReadOnlyMemory <byte> DecodeOctetStringAsMemory(ReadOnlyMemory <byte> encodedOctetString)
        {
            try
            {
                ReadOnlySpan <byte> input = encodedOctetString.Span;

                if (AsnDecoder.TryReadPrimitiveOctetString(
                        input,
                        AsnEncodingRules.BER,
                        out ReadOnlySpan <byte> primitive,
                        out int consumed))
                {
                    if (consumed != input.Length)
                    {
                        throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                    }

                    if (input.Overlaps(primitive, out int offset))
                    {
                        return(encodedOctetString.Slice(offset, primitive.Length));
                    }

                    Debug.Fail("input.Overlaps(primitive) failed after TryReadPrimitiveOctetString succeeded");
                }

                byte[] ret = AsnDecoder.ReadOctetString(input, AsnEncodingRules.BER, out consumed);

                if (consumed != input.Length)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                }

                return(ret);
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
        }
コード例 #3
0
        public static DateTime DecodeUtcTime(byte[] encodedUtcTime)
        {
            // Read using BER because the CMS specification says the encoding is BER.
            try
            {
                DateTimeOffset value = AsnDecoder.ReadUtcTime(
                    encodedUtcTime,
                    AsnEncodingRules.BER,
                    out int consumed);

                if (consumed != encodedUtcTime.Length)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                }

                return(value.UtcDateTime);
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
        }
コード例 #4
0
        public static void PeekEncodedValue_InvalidLength()
        {
            byte[] badLength = "04040203".HexToByteArray();

            AsnReader reader = new AsnReader(badLength, AsnEncodingRules.BER);

            Assert.Throws <AsnContentException>(() => reader.PeekEncodedValue());
            Assert.Throws <AsnContentException>(() => reader.ReadEncodedValue());

            Assert.False(
                AsnDecoder.TryReadEncodedValue(
                    badLength,
                    AsnEncodingRules.BER,
                    out Asn1Tag tag,
                    out int contentOffset,
                    out int contentLength,
                    out int bytesConsumed));

            Assert.Equal(0, contentOffset);
            Assert.Equal(0, contentLength);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(default(Asn1Tag), tag);
        }
コード例 #5
0
ファイル: ECKey.cs プロジェクト: 1hub/springburg
        protected static ECParameters ReadOpenPgpECParameters(ReadOnlySpan <byte> source, out int bytesRead)
        {
            ECParameters ecParameters = new ECParameters();

            int oidLength = source[0];
            // TODO: Validate oidLength
            var oidBytes = new byte[oidLength + 2];

            oidBytes[0] = 6;
            oidBytes[1] = (byte)oidLength;
            source.Slice(1, oidLength).CopyTo(oidBytes.AsSpan(2));
            var oid = new Oid(AsnDecoder.ReadObjectIdentifier(oidBytes, AsnEncodingRules.DER, out _));

            ecParameters.Curve = ECCurve.CreateFromOid(oid);

            var pointBytes = MPInteger.ReadInteger(source.Slice(oidLength + 1), out bytesRead);

            bytesRead += oidLength + 1;

            ecParameters.Q = DecodePoint(pointBytes);

            return(ecParameters);
        }
コード例 #6
0
        internal static byte[] DecodeX509SubjectKeyIdentifierExtension(byte[] encoded)
        {
            ReadOnlySpan <byte> contents;

            try
            {
                bool gotContents = AsnDecoder.TryReadPrimitiveOctetString(
                    encoded,
                    AsnEncodingRules.BER,
                    out contents,
                    out int consumed);

                if (!gotContents || consumed != encoded.Length)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                }
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }

            return(contents.ToArray());
        }
コード例 #7
0
ファイル: ParseTag.cs プロジェクト: layomia/dotnet_runtime
        public static void ParseCorruptTag(string description, string inputHex)
        {
            _ = description;
            byte[] inputBytes = inputHex.HexToByteArray();

            Assert.False(Asn1Tag.TryDecode(inputBytes, out Asn1Tag tag, out var bytesRead));

            Assert.Equal(default(Asn1Tag), tag);
            Assert.Equal(0, bytesRead);

            Assert.False(
                AsnDecoder.TryReadEncodedValue(
                    inputBytes,
                    AsnEncodingRules.BER,
                    out tag,
                    out int contentOffset,
                    out int contentLength,
                    out int bytesConsumed));

            Assert.Equal(0, contentOffset);
            Assert.Equal(0, contentLength);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(default(Asn1Tag), tag);
        }
コード例 #8
0
        internal static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(
            ReadOnlySpan <char> password,
            ReadOnlySpan <byte> source,
            out int bytesRead)
        {
            try
            {
                AsnDecoder.ReadEncodedValue(
                    source,
                    AsnEncodingRules.BER,
                    out _,
                    out _,
                    out int len);

                source = source.Slice(0, len);

                fixed(byte *ptr = &MemoryMarshal.GetReference(source))
                {
                    using (MemoryManager <byte> manager = new PointerMemoryManager <byte>(ptr, source.Length))
                    {
                        try
                        {
                            bytesRead = len;
                            return(ImportPkcs8(source, password));
                        }
                        catch (CryptographicException)
                        {
                        }

                        ArraySegment <byte> decrypted = KeyFormatHelper.DecryptPkcs8(
                            password,
                            manager.Memory.Slice(0, len),
                            out int innerRead);

                        Span <byte> decryptedSpan = decrypted;

                        try
                        {
                            if (innerRead != len)
                            {
                                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                            }

                            bytesRead = len;
                            return(ImportPkcs8(decryptedSpan));
                        }
                        catch (CryptographicException e)
                        {
                            AsnWriter?pkcs8ZeroPublicKey = RewritePkcs8ECPrivateKeyWithZeroPublicKey(decryptedSpan);

                            if (pkcs8ZeroPublicKey == null)
                            {
                                throw new CryptographicException(SR.Cryptography_Pkcs8_EncryptedReadFailed, e);
                            }

                            try
                            {
                                bytesRead = len;
                                return(ImportPkcs8(pkcs8ZeroPublicKey));
                            }
                            catch (CryptographicException)
                            {
                                throw new CryptographicException(SR.Cryptography_Pkcs8_EncryptedReadFailed, e);
                            }
                        }
                        finally
                        {
                            CryptographicOperations.ZeroMemory(decryptedSpan);
                            CryptoPool.Return(decrypted.Array !, clearSize: 0);
                        }
                    }
                }
            }
            catch (AsnContentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
        }
コード例 #9
0
 /// <summary>
 ///   Reads the next value as a NULL with a specified tag.
 /// </summary>
 /// <param name="expectedTag">
 ///   The tag to check for before reading, or <see langword="null"/> for the default tag (Universal 5).
 /// </param>
 /// <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.
 /// </exception>
 /// <exception cref="ArgumentException">
 ///   <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>
 public void ReadNull(Asn1Tag?expectedTag = null)
 {
     AsnDecoder.ReadNull(_data.Span, RuleSet, out int consumed, expectedTag);
     _data = _data.Slice(consumed);
 }