diff options
Diffstat (limited to 'crypto/src/asn1/cms/PasswordRecipientInfo.cs')
-rw-r--r-- | crypto/src/asn1/cms/PasswordRecipientInfo.cs | 79 |
1 files changed, 29 insertions, 50 deletions
diff --git a/crypto/src/asn1/cms/PasswordRecipientInfo.cs b/crypto/src/asn1/cms/PasswordRecipientInfo.cs index 5fac434ca..41ab686ef 100644 --- a/crypto/src/asn1/cms/PasswordRecipientInfo.cs +++ b/crypto/src/asn1/cms/PasswordRecipientInfo.cs @@ -25,68 +25,46 @@ namespace Org.BouncyCastle.Asn1.Cms #pragma warning restore CS0618 // Type or member is obsolete } - private readonly DerInteger version; - private readonly AlgorithmIdentifier keyDerivationAlgorithm; - private readonly AlgorithmIdentifier keyEncryptionAlgorithm; - private readonly Asn1OctetString encryptedKey; + private readonly DerInteger m_version; + private readonly AlgorithmIdentifier m_keyDerivationAlgorithm; + private readonly AlgorithmIdentifier m_keyEncryptionAlgorithm; + private readonly Asn1OctetString m_encryptedKey; - public PasswordRecipientInfo( - AlgorithmIdentifier keyEncryptionAlgorithm, - Asn1OctetString encryptedKey) + public PasswordRecipientInfo(AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey) + : this(keyDerivationAlgorithm: null, keyEncryptionAlgorithm, encryptedKey) { - this.version = DerInteger.Zero; - this.keyEncryptionAlgorithm = keyEncryptionAlgorithm; - this.encryptedKey = encryptedKey; } - public PasswordRecipientInfo( - AlgorithmIdentifier keyDerivationAlgorithm, - AlgorithmIdentifier keyEncryptionAlgorithm, - Asn1OctetString encryptedKey) + public PasswordRecipientInfo(AlgorithmIdentifier keyDerivationAlgorithm, + AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey) { - this.version = DerInteger.Zero; - this.keyDerivationAlgorithm = keyDerivationAlgorithm; - this.keyEncryptionAlgorithm = keyEncryptionAlgorithm; - this.encryptedKey = encryptedKey; + m_version = DerInteger.Zero; + m_keyDerivationAlgorithm = keyDerivationAlgorithm; + m_keyEncryptionAlgorithm = keyEncryptionAlgorithm ?? throw new ArgumentNullException(nameof(keyEncryptionAlgorithm)); + m_encryptedKey = encryptedKey ?? throw new ArgumentNullException(nameof(encryptedKey)); } [Obsolete("Use 'GetInstance' instead")] public PasswordRecipientInfo(Asn1Sequence seq) { - version = (DerInteger)seq[0]; + int count = seq.Count, pos = 0; + if (count < 3 || count > 4) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - if (seq[1] is Asn1TaggedObject taggedObject) - { - keyDerivationAlgorithm = AlgorithmIdentifier.GetInstance(taggedObject, false); - keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]); - encryptedKey = (Asn1OctetString)seq[3]; - } - else - { - keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]); - encryptedKey = (Asn1OctetString)seq[2]; - } + m_version = DerInteger.GetInstance(seq[pos++]); + m_keyDerivationAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, + AlgorithmIdentifier.GetInstance); + m_keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); + m_encryptedKey = Asn1OctetString.GetInstance(seq[pos++]); } - public DerInteger Version - { - get { return version; } - } + public DerInteger Version => m_version; - public AlgorithmIdentifier KeyDerivationAlgorithm - { - get { return keyDerivationAlgorithm; } - } + public AlgorithmIdentifier KeyDerivationAlgorithm => m_keyDerivationAlgorithm; - public AlgorithmIdentifier KeyEncryptionAlgorithm - { - get { return keyEncryptionAlgorithm; } - } + public AlgorithmIdentifier KeyEncryptionAlgorithm => m_keyEncryptionAlgorithm; - public Asn1OctetString EncryptedKey - { - get { return encryptedKey; } - } + public Asn1OctetString EncryptedKey => m_encryptedKey; /** * Produce an object suitable for an Asn1OutputStream. @@ -101,10 +79,11 @@ namespace Org.BouncyCastle.Asn1.Cms */ public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector(version); - v.AddOptionalTagged(false, 0, keyDerivationAlgorithm); - v.Add(keyEncryptionAlgorithm, encryptedKey); - return new DerSequence(v); + if (m_keyDerivationAlgorithm == null) + return new DerSequence(m_version, m_keyEncryptionAlgorithm, m_encryptedKey); + + return new DerSequence(m_version, new DerTaggedObject(false, 0, m_keyDerivationAlgorithm), + m_keyEncryptionAlgorithm, m_encryptedKey); } } } |