diff options
Diffstat (limited to 'crypto/src/asn1/ocsp/BasicOCSPResponse.cs')
-rw-r--r-- | crypto/src/asn1/ocsp/BasicOCSPResponse.cs | 110 |
1 files changed, 43 insertions, 67 deletions
diff --git a/crypto/src/asn1/ocsp/BasicOCSPResponse.cs b/crypto/src/asn1/ocsp/BasicOCSPResponse.cs index 2637d36bc..fc491f78e 100644 --- a/crypto/src/asn1/ocsp/BasicOCSPResponse.cs +++ b/crypto/src/asn1/ocsp/BasicOCSPResponse.cs @@ -1,91 +1,66 @@ using System; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Ocsp { public class BasicOcspResponse : Asn1Encodable { - private readonly ResponseData tbsResponseData; - private readonly AlgorithmIdentifier signatureAlgorithm; - private readonly DerBitString signature; - private readonly Asn1Sequence certs; - - public static BasicOcspResponse GetInstance( - Asn1TaggedObject obj, - bool explicitly) - { - return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); - } - - public static BasicOcspResponse GetInstance( - object obj) - { - if (obj == null || obj is BasicOcspResponse) - { - return (BasicOcspResponse)obj; - } + public static BasicOcspResponse GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is BasicOcspResponse basicOcspResponse) + return basicOcspResponse; + return new BasicOcspResponse(Asn1Sequence.GetInstance(obj)); + } - if (obj is Asn1Sequence) - { - return new BasicOcspResponse((Asn1Sequence)obj); - } + public static BasicOcspResponse GetInstance(Asn1TaggedObject obj, bool explicitly) + { + return new BasicOcspResponse(Asn1Sequence.GetInstance(obj, explicitly)); + } - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + private readonly ResponseData m_tbsResponseData; + private readonly AlgorithmIdentifier m_signatureAlgorithm; + private readonly DerBitString m_signature; + private readonly Asn1Sequence m_certs; - public BasicOcspResponse( - ResponseData tbsResponseData, - AlgorithmIdentifier signatureAlgorithm, - DerBitString signature, - Asn1Sequence certs) + public BasicOcspResponse(ResponseData tbsResponseData, AlgorithmIdentifier signatureAlgorithm, + DerBitString signature, Asn1Sequence certs) { - this.tbsResponseData = tbsResponseData; - this.signatureAlgorithm = signatureAlgorithm; - this.signature = signature; - this.certs = certs; + m_tbsResponseData = tbsResponseData ?? throw new ArgumentNullException(nameof(tbsResponseData)); + m_signatureAlgorithm = signatureAlgorithm ?? throw new ArgumentNullException(nameof(signatureAlgorithm)); + m_signature = signature ?? throw new ArgumentNullException(nameof(signature)); + m_certs = certs; } - private BasicOcspResponse( - Asn1Sequence seq) + private BasicOcspResponse(Asn1Sequence seq) { - this.tbsResponseData = ResponseData.GetInstance(seq[0]); - this.signatureAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]); - this.signature = (DerBitString)seq[2]; + int count = seq.Count; + if (count < 3 || count > 4) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + int pos = 0; + + m_tbsResponseData = ResponseData.GetInstance(seq[pos++]); + m_signatureAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); + m_signature = DerBitString.GetInstance(seq[pos++]); + m_certs = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1Sequence.GetInstance); - if (seq.Count > 3) - { - this.certs = Asn1Sequence.GetInstance((Asn1TaggedObject)seq[3], true); - } + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - public ResponseData TbsResponseData - { - get { return tbsResponseData; } - } + public ResponseData TbsResponseData => m_tbsResponseData; - public AlgorithmIdentifier SignatureAlgorithm - { - get { return signatureAlgorithm; } - } + public AlgorithmIdentifier SignatureAlgorithm => m_signatureAlgorithm; - public DerBitString Signature - { - get { return signature; } - } + public DerBitString Signature => m_signature; - public byte[] GetSignatureOctets() - { - return signature.GetOctets(); - } + public byte[] GetSignatureOctets() => m_signature.GetOctets(); - public Asn1Sequence Certs - { - get { return certs; } - } + public Asn1Sequence Certs => m_certs; /** * Produce an object suitable for an Asn1OutputStream. @@ -99,8 +74,9 @@ namespace Org.BouncyCastle.Asn1.Ocsp */ public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector(tbsResponseData, signatureAlgorithm, signature); - v.AddOptionalTagged(true, 0, certs); + Asn1EncodableVector v = new Asn1EncodableVector(4); + v.Add(m_tbsResponseData, m_signatureAlgorithm, m_signature); + v.AddOptionalTagged(true, 0, m_certs); return new DerSequence(v); } } |