summary refs log tree commit diff
path: root/crypto/src/tls/CertificateStatusRequest.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-07-12 15:15:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-07-12 15:15:36 +0700
commit68c795fe81277f73aeb90d8ad4c6f4305f32c906 (patch)
tree59643344aafef91bbd4c4a3a7973deba3d837a00 /crypto/src/tls/CertificateStatusRequest.cs
parentTLS test tweaks (diff)
downloadBouncyCastle.NET-ed25519-68c795fe81277f73aeb90d8ad4c6f4305f32c906.tar.xz
Port of new TLS API from bc-java
Diffstat (limited to 'crypto/src/tls/CertificateStatusRequest.cs')
-rw-r--r--crypto/src/tls/CertificateStatusRequest.cs91
1 files changed, 91 insertions, 0 deletions
diff --git a/crypto/src/tls/CertificateStatusRequest.cs b/crypto/src/tls/CertificateStatusRequest.cs
new file mode 100644
index 000000000..4731316cd
--- /dev/null
+++ b/crypto/src/tls/CertificateStatusRequest.cs
@@ -0,0 +1,91 @@
+using System;
+using System.IO;
+
+namespace Org.BouncyCastle.Tls
+{
+    /// <summary>Implementation of the RFC 3546 3.6. CertificateStatusRequest.</summary>
+    public sealed class CertificateStatusRequest
+    {
+        private short m_statusType;
+        private object m_request;
+
+        public CertificateStatusRequest(short statusType, object request)
+        {
+            if (!IsCorrectType(statusType, request))
+                throw new ArgumentException("not an instance of the correct type", "request");
+
+            this.m_statusType = statusType;
+            this.m_request = request;
+        }
+
+        public short StatusType
+        {
+            get { return m_statusType; }
+        }
+
+        public object Request
+        {
+            get { return m_request; }
+        }
+
+        public OcspStatusRequest OcspStatusRequest
+        {
+            get
+            {
+                if (!IsCorrectType(CertificateStatusType.ocsp, m_request))
+                    throw new InvalidOperationException("'request' is not an OCSPStatusRequest");
+
+                return (OcspStatusRequest)m_request;
+            }
+        }
+
+        /// <summary>Encode this <see cref="CertificateStatusRequest"/> to a <see cref="Stream"/>.</summary>
+        /// <param name="output">the <see cref="Stream"/> to encode to.</param>
+        /// <exception cref="IOException"/>
+        public void Encode(Stream output)
+        {
+            TlsUtilities.WriteUint8(m_statusType, output);
+
+            switch (m_statusType)
+            {
+            case CertificateStatusType.ocsp:
+                ((OcspStatusRequest)m_request).Encode(output);
+                break;
+            default:
+                throw new TlsFatalAlert(AlertDescription.internal_error);
+            }
+        }
+
+        /// <summary>Parse a <see cref="CertificateStatusRequest"/> from a <see cref="Stream"/>.</summary>
+        /// <param name="input">the <see cref="Stream"/> to parse from.</param>
+        /// <returns>a <see cref="CertificateStatusRequest"/> object.</returns>
+        /// <exception cref="IOException"/>
+        public static CertificateStatusRequest Parse(Stream input)
+        {
+            short status_type = TlsUtilities.ReadUint8(input);
+            object request;
+
+            switch (status_type)
+            {
+            case CertificateStatusType.ocsp:
+                request = OcspStatusRequest.Parse(input);
+                break;
+            default:
+                throw new TlsFatalAlert(AlertDescription.decode_error);
+            }
+
+            return new CertificateStatusRequest(status_type, request);
+        }
+
+        private static bool IsCorrectType(short statusType, object request)
+        {
+            switch (statusType)
+            {
+            case CertificateStatusType.ocsp:
+                return request is OcspStatusRequest;
+            default:
+                throw new ArgumentException("unsupported CertificateStatusType", "statusType");
+            }
+        }
+    }
+}