Exemplo n.º 1
0
        public Pkcs12ShroudedKeyBag AddShroudedKey(
            AsymmetricAlgorithm key,
            ReadOnlySpan <char> password,
            PbeParameters pbeParameters)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (IsReadOnly)
            {
                throw new InvalidOperationException(SR.Cryptography_Pkcs12_SafeContentsIsReadOnly);
            }

            byte[] encryptedPkcs8    = key.ExportEncryptedPkcs8PrivateKey(password, pbeParameters);
            Pkcs12ShroudedKeyBag bag = new Pkcs12ShroudedKeyBag(encryptedPkcs8, skipCopy: true);

            AddSafeBag(bag);
            return(bag);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }