diff options
Diffstat (limited to 'crypto/src/asn1/crmf')
-rw-r--r-- | crypto/src/asn1/crmf/EncryptedKey.cs | 58 | ||||
-rw-r--r-- | crypto/src/asn1/crmf/EncryptedValue.cs | 62 |
2 files changed, 50 insertions, 70 deletions
diff --git a/crypto/src/asn1/crmf/EncryptedKey.cs b/crypto/src/asn1/crmf/EncryptedKey.cs index 850fbd219..d4ff250c5 100644 --- a/crypto/src/asn1/crmf/EncryptedKey.cs +++ b/crypto/src/asn1/crmf/EncryptedKey.cs @@ -1,58 +1,44 @@ -using System; - -using Org.BouncyCastle.Asn1.Cms; +using Org.BouncyCastle.Asn1.Cms; namespace Org.BouncyCastle.Asn1.Crmf { public class EncryptedKey : Asn1Encodable, IAsn1Choice { - private readonly EnvelopedData envelopedData; - private readonly EncryptedValue encryptedValue; - - public static EncryptedKey GetInstance(object o) + public static EncryptedKey GetInstance(object obj) { - if (o is EncryptedKey) - { - return (EncryptedKey)o; - } - else if (o is Asn1TaggedObject) - { - return new EncryptedKey(EnvelopedData.GetInstance((Asn1TaggedObject)o, false)); - } - else if (o is EncryptedValue) - { - return new EncryptedKey((EncryptedValue)o); - } - else - { - return new EncryptedKey(EncryptedValue.GetInstance(o)); - } + if (obj is EncryptedKey encryptedKey) + return encryptedKey; + + if (obj is Asn1TaggedObject taggedObject) + return new EncryptedKey(EnvelopedData.GetInstance(taggedObject, false)); + + return new EncryptedKey(EncryptedValue.GetInstance(obj)); } + private readonly EnvelopedData m_envelopedData; + private readonly EncryptedValue m_encryptedValue; + public EncryptedKey(EnvelopedData envelopedData) { - this.envelopedData = envelopedData; + m_envelopedData = envelopedData; } public EncryptedKey(EncryptedValue encryptedValue) { - this.encryptedValue = encryptedValue; + m_encryptedValue = encryptedValue; } - public virtual bool IsEncryptedValue - { - get { return encryptedValue != null; } - } + public virtual bool IsEncryptedValue => m_encryptedValue != null; public virtual Asn1Encodable Value { get { - if (encryptedValue != null) - return encryptedValue; + if (m_encryptedValue != null) + return m_encryptedValue; - return envelopedData; + return m_envelopedData; } } @@ -67,12 +53,10 @@ namespace Org.BouncyCastle.Asn1.Crmf */ public override Asn1Object ToAsn1Object() { - if (encryptedValue != null) - { - return encryptedValue.ToAsn1Object(); - } + if (m_encryptedValue != null) + return m_encryptedValue.ToAsn1Object(); - return new DerTaggedObject(false, 0, envelopedData); + return new DerTaggedObject(false, 0, m_envelopedData); } } } diff --git a/crypto/src/asn1/crmf/EncryptedValue.cs b/crypto/src/asn1/crmf/EncryptedValue.cs index 7c5cf18b4..ad3a4c3f7 100644 --- a/crypto/src/asn1/crmf/EncryptedValue.cs +++ b/crypto/src/asn1/crmf/EncryptedValue.cs @@ -7,6 +7,17 @@ namespace Org.BouncyCastle.Asn1.Crmf public class EncryptedValue : Asn1Encodable { + public static EncryptedValue GetInstance(object obj) + { + if (obj is EncryptedValue) + return (EncryptedValue)obj; + + if (obj != null) + return new EncryptedValue(Asn1Sequence.GetInstance(obj)); + + return null; + } + private readonly AlgorithmIdentifier intendedAlg; private readonly AlgorithmIdentifier symmAlg; private readonly DerBitString encSymmKey; @@ -17,45 +28,31 @@ namespace Org.BouncyCastle.Asn1.Crmf private EncryptedValue(Asn1Sequence seq) { int index = 0; - while (seq[index] is Asn1TaggedObject) + while (seq[index++] is Asn1TaggedObject tObj) { - Asn1TaggedObject tObj = (Asn1TaggedObject)seq[index]; - switch (tObj.TagNo) { - case 0: - intendedAlg = AlgorithmIdentifier.GetInstance(tObj, false); - break; - case 1: - symmAlg = AlgorithmIdentifier.GetInstance(tObj, false); - break; - case 2: - encSymmKey = DerBitString.GetInstance(tObj, false); - break; - case 3: - keyAlg = AlgorithmIdentifier.GetInstance(tObj, false); - break; - case 4: - valueHint = Asn1OctetString.GetInstance(tObj, false); - break; + case 0: + intendedAlg = AlgorithmIdentifier.GetInstance(tObj, false); + break; + case 1: + symmAlg = AlgorithmIdentifier.GetInstance(tObj, false); + break; + case 2: + encSymmKey = DerBitString.GetInstance(tObj, false); + break; + case 3: + keyAlg = AlgorithmIdentifier.GetInstance(tObj, false); + break; + case 4: + valueHint = Asn1OctetString.GetInstance(tObj, false); + break; } - ++index; } encValue = DerBitString.GetInstance(seq[index]); } - public static EncryptedValue GetInstance(object obj) - { - if (obj is EncryptedValue) - return (EncryptedValue)obj; - - if (obj != null) - return new EncryptedValue(Asn1Sequence.GetInstance(obj)); - - return null; - } - public EncryptedValue( AlgorithmIdentifier intendedAlg, AlgorithmIdentifier symmAlg, @@ -65,9 +62,7 @@ namespace Org.BouncyCastle.Asn1.Crmf DerBitString encValue) { if (encValue == null) - { - throw new ArgumentNullException("encValue"); - } + throw new ArgumentNullException(nameof(encValue)); this.intendedAlg = intendedAlg; this.symmAlg = symmAlg; @@ -109,6 +104,7 @@ namespace Org.BouncyCastle.Asn1.Crmf /** * <pre> + * (IMPLICIT TAGS) * EncryptedValue ::= SEQUENCE { * intendedAlg [0] AlgorithmIdentifier OPTIONAL, * -- the intended algorithm for which the value will be used |