예제 #1
0
        internal byte[][] Encode()
        {
            var bytes  = DirectoryName.RawData;
            var reader = DerSequenceReader.CreateForPayload(bytes);

            var tag             = reader.PeekTag();
            var value           = reader.ReadValue((DerSequenceReader.DerTag)tag);
            var lengthByteCount = reader.ContentLength - 1 - value.Length;
            var length          = new byte[lengthByteCount];

            Array.Copy(bytes, sourceIndex: 1, destinationArray: length, destinationIndex: 0, length: length.Length);

            const int contextId = 4;

            return(DerEncoder.ConstructSegmentedContextSpecificValue(
                       contextId,
                       new byte[][]
            {
                new byte[1] {
                    tag
                },
                length,
                value
            }));
        }
예제 #2
0
        public static void NoSupportForMultiByteTags(string caseName, string hexInput)
        {
            byte[]            bytes  = hexInput.HexToByteArray();
            DerSequenceReader reader = DerSequenceReader.CreateForPayload(bytes);

            Assert.Throws <CryptographicException>(() => reader.PeekTag());
            Assert.Throws <CryptographicException>(() => reader.SkipValue());
            Assert.Throws <CryptographicException>(() => reader.ReadNextEncodedValue());
        }
예제 #3
0
        internal static byte[][] SegmentedEncodeSubjectPublicKeyInfo(this PublicKey publicKey)
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            if (publicKey.Oid == null ||
                string.IsNullOrEmpty(publicKey.Oid.Value) ||
                publicKey.EncodedKeyValue == null)
            {
                throw new CryptographicException(SR.Cryptography_InvalidPublicKey_Object);
            }

            // SubjectPublicKeyInfo::= SEQUENCE  {
            //   algorithm AlgorithmIdentifier,
            //   subjectPublicKey     BIT STRING
            // }
            //
            // AlgorithmIdentifier::= SEQUENCE  {
            //   algorithm OBJECT IDENTIFIER,
            //   parameters ANY DEFINED BY algorithm OPTIONAL
            // }

            byte[][] algorithmIdentifier;

            if (publicKey.EncodedParameters == null)
            {
                algorithmIdentifier = DerEncoder.ConstructSegmentedSequence(
                    DerEncoder.SegmentedEncodeOid(publicKey.Oid));
            }
            else
            {
                DerSequenceReader validator =
                    DerSequenceReader.CreateForPayload(publicKey.EncodedParameters.RawData);

                validator.ValidateAndSkipDerValue();

                if (validator.HasData)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
                }

                algorithmIdentifier = DerEncoder.ConstructSegmentedSequence(
                    DerEncoder.SegmentedEncodeOid(publicKey.Oid),
                    publicKey.EncodedParameters.RawData.WrapAsSegmentedForSequence());
            }

            return(DerEncoder.ConstructSegmentedSequence(
                       algorithmIdentifier,
                       DerEncoder.SegmentedEncodeBitString(
                           publicKey.EncodedKeyValue.RawData)));
        }
예제 #4
0
        public static void InvalidLengthSpecified(string hexInput)
        {
            byte[]            bytes  = hexInput.HexToByteArray();
            DerSequenceReader reader = DerSequenceReader.CreateForPayload(bytes);

            // Doesn't throw.
            reader.PeekTag();

            // Since EatTag will have succeeded the reader needs to be reconstructed after each test.
            Assert.Throws <CryptographicException>(() => reader.SkipValue());
            reader = DerSequenceReader.CreateForPayload(bytes);

            Assert.Throws <CryptographicException>(() => reader.ReadOctetString());
            reader = DerSequenceReader.CreateForPayload(bytes);

            Assert.Throws <CryptographicException>(() => reader.ReadNextEncodedValue());
        }
        internal static byte[] GetAsn1IntegerBytes(SafeSharedAsn1IntegerHandle asn1Integer)
        {
            CheckValidOpenSslHandle(asn1Integer);

            // OpenSSL stores negative numbers in their two's complement (positive) form, but
            // sets an internal negative bit.
            //
            // If the number was positive, but could sign-test as negative, DER puts in a leading
            // 0x00 byte, which reading OpenSSL's data directly won't have.
            //
            // So to ensure we're getting a set of bytes compatible with BigInteger (though with the
            // wrong endianness here), DER encode it, then use the DER reader to skip past the tag
            // and length.
            byte[] derEncoded = OpenSslEncode(
                handle => GetAsn1IntegerDerSize(handle),
                (handle, buf) => EncodeAsn1Integer(handle, buf),
                asn1Integer);

            DerSequenceReader reader = DerSequenceReader.CreateForPayload(derEncoded);

            return(reader.ReadIntegerBytes());
        }
예제 #6
0
        public static IssuerSerial Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #7
0
        public static Extensions Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #8
0
        public static PolicyInformation Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #9
0
        public static Accuracy Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #10
0
        public static CommitmentTypeQualifier Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #11
0
        public static AlgorithmIdentifier Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #12
0
        public static NuGetV3ServiceIndexUrl Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #13
0
        public static MessageImprint Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #14
0
        public static GeneralName Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #15
0
        public static EssCertId Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
예제 #16
0
        public static PolicyQualifierInfo Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }
        public static NuGetPackageOwners Read(byte[] bytes)
        {
            var reader = DerSequenceReader.CreateForPayload(bytes);

            return(Read(reader));
        }