public Pkcs12SafeContentsBag AddNestedContents(Pkcs12SafeContents safeContents) { if (safeContents == null) { throw new ArgumentNullException(nameof(safeContents)); } if (safeContents.ConfidentialityMode != Pkcs12ConfidentialityMode.None) { throw new ArgumentException(SR.Cryptography_Pkcs12_CannotProcessEncryptedSafeContents, nameof(safeContents)); } if (IsReadOnly) { throw new InvalidOperationException(SR.Cryptography_Pkcs12_SafeContentsIsReadOnly); } Pkcs12SafeContentsBag bag = Pkcs12SafeContentsBag.Create(safeContents); AddSafeBag(bag); return(bag); }
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); }
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); }