示例#1
0
        public static Pkcs8PrivateKeyInfo Decode(
            ReadOnlyMemory <byte> source,
            out int bytesRead,
            bool skipCopy = false)
        {
            AsnValueReader reader = new AsnValueReader(source.Span, AsnEncodingRules.BER);
            // By using the default/empty ReadOnlyMemory value, the Decode method will have to
            // make copies of the data when assigning ReadOnlyMemory fields.
            ReadOnlyMemory <byte> rebind = skipCopy ? source : default;

            int localRead = reader.PeekEncodedValue().Length;

            PrivateKeyInfoAsn.Decode(ref reader, rebind, out PrivateKeyInfoAsn privateKeyInfo);
            bytesRead = localRead;

            return(new Pkcs8PrivateKeyInfo(
                       privateKeyInfo.PrivateKeyAlgorithm.Algorithm,
                       privateKeyInfo.PrivateKeyAlgorithm.Parameters,
                       privateKeyInfo.PrivateKey,
                       SignerInfo.MakeAttributeCollection(privateKeyInfo.Attributes)));
        }
示例#2
0
        public static Pkcs8PrivateKeyInfo Decode(
            ReadOnlyMemory <byte> source,
            out int bytesRead,
            bool skipCopy = false)
        {
            AsnReader reader = new AsnReader(source, AsnEncodingRules.BER);

            if (!skipCopy)
            {
                reader = new AsnReader(reader.ReadEncodedValue().ToArray(), AsnEncodingRules.BER);
            }

            int localRead = reader.PeekEncodedValue().Length;

            PrivateKeyInfoAsn.Decode(reader, out PrivateKeyInfoAsn privateKeyInfo);
            bytesRead = localRead;

            return(new Pkcs8PrivateKeyInfo(
                       privateKeyInfo.PrivateKeyAlgorithm.Algorithm,
                       privateKeyInfo.PrivateKeyAlgorithm.Parameters,
                       privateKeyInfo.PrivateKey,
                       SignerInfo.MakeAttributeCollection(privateKeyInfo.Attributes)));
        }
示例#3
0
        private static List <Pkcs12SafeBag> ReadBags(ReadOnlyMemory <byte> serialized)
        {
            List <SafeBagAsn> serializedBags = new List <SafeBagAsn>();
            AsnReader         reader         = new AsnReader(serialized, AsnEncodingRules.BER);
            AsnReader         sequenceReader = reader.ReadSequence();

            reader.ThrowIfNotEmpty();
            while (sequenceReader.HasData)
            {
                SafeBagAsn.Decode(sequenceReader, out SafeBagAsn serializedBag);
                serializedBags.Add(serializedBag);
            }

            if (serializedBags.Count == 0)
            {
                return(new List <Pkcs12SafeBag>(0));
            }

            List <Pkcs12SafeBag> bags = new List <Pkcs12SafeBag>(serializedBags.Count);

            for (int i = 0; i < serializedBags.Count; i++)
            {
                ReadOnlyMemory <byte> bagValue = serializedBags[i].BagValue;
                Pkcs12SafeBag         bag      = null;

                try
                {
                    switch (serializedBags[i].BagId)
                    {
                    case Oids.Pkcs12KeyBag:
                        bag = new Pkcs12KeyBag(bagValue);
                        break;

                    case Oids.Pkcs12ShroudedKeyBag:
                        bag = new Pkcs12ShroudedKeyBag(bagValue);
                        break;

                    case Oids.Pkcs12CertBag:
                        bag = Pkcs12CertBag.DecodeValue(bagValue);
                        break;

                    case Oids.Pkcs12CrlBag:
                        // Known, but no first-class support currently.
                        break;

                    case Oids.Pkcs12SecretBag:
                        bag = Pkcs12SecretBag.DecodeValue(bagValue);
                        break;

                    case Oids.Pkcs12SafeContentsBag:
                        bag = Pkcs12SafeContentsBag.Decode(bagValue);
                        break;
                    }
                }
                catch (CryptographicException)
                {
                }

                if (bag == null)
                {
                    bag = new Pkcs12SafeBag.UnknownBag(serializedBags[i].BagId, bagValue);
                }

                bag.Attributes = SignerInfo.MakeAttributeCollection(serializedBags[i].BagAttributes);
                bags.Add(bag);
            }

            return(bags);
        }
示例#4
0
        private static List <Pkcs12SafeBag> ReadBags(ReadOnlyMemory <byte> serialized)
        {
            SafeBagAsn[] serializedBags =
                AsnSerializer.Deserialize <SafeBagAsn[]>(serialized, AsnEncodingRules.BER);

            if (serializedBags.Length == 0)
            {
                return(new List <Pkcs12SafeBag>(0));
            }

            List <Pkcs12SafeBag> bags = new List <Pkcs12SafeBag>(serializedBags.Length);

            for (int i = 0; i < serializedBags.Length; i++)
            {
                ReadOnlyMemory <byte> bagValue = serializedBags[i].BagValue;
                Pkcs12SafeBag         bag      = null;

                try
                {
                    switch (serializedBags[i].BagId)
                    {
                    case Oids.Pkcs12KeyBag:
                        bag = new Pkcs12KeyBag(bagValue);
                        break;

                    case Oids.Pkcs12ShroudedKeyBag:
                        bag = new Pkcs12ShroudedKeyBag(bagValue);
                        break;

                    case Oids.Pkcs12CertBag:
                        bag = Pkcs12CertBag.DecodeValue(bagValue);
                        break;

                    case Oids.Pkcs12CrlBag:
                        // Known, but no first-class support currently.
                        break;

                    case Oids.Pkcs12SecretBag:
                        bag = Pkcs12SecretBag.DecodeValue(bagValue);
                        break;

                    case Oids.Pkcs12SafeContentsBag:
                        bag = Pkcs12SafeContentsBag.Decode(bagValue);
                        break;
                    }
                }
                catch (CryptographicException)
                {
                }

                if (bag == null)
                {
                    bag = new Pkcs12SafeBag.UnknownBag(serializedBags[i].BagId, bagValue);
                }

                bag.Attributes = SignerInfo.MakeAttributeCollection(serializedBags[i].BagAttributes);
                bags.Add(bag);
            }

            return(bags);
        }