123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections;
- using System.IO;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ocsp;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
- {
- /**
- * Generator for basic OCSP response objects.
- */
- public class BasicOcspRespGenerator
- {
- private readonly IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- private X509Extensions responseExtensions;
- private RespID responderID;
- private class ResponseObject
- {
- internal CertificateID certId;
- internal CertStatus certStatus;
- internal DerGeneralizedTime thisUpdate;
- internal DerGeneralizedTime nextUpdate;
- internal X509Extensions extensions;
- public ResponseObject(
- CertificateID certId,
- CertificateStatus certStatus,
- DateTime thisUpdate,
- X509Extensions extensions)
- : this(certId, certStatus, new DerGeneralizedTime(thisUpdate), null, extensions)
- {
- }
- public ResponseObject(
- CertificateID certId,
- CertificateStatus certStatus,
- DateTime thisUpdate,
- DateTime nextUpdate,
- X509Extensions extensions)
- : this(certId, certStatus, new DerGeneralizedTime(thisUpdate), new DerGeneralizedTime(nextUpdate), extensions)
- {
- }
- private ResponseObject(
- CertificateID certId,
- CertificateStatus certStatus,
- DerGeneralizedTime thisUpdate,
- DerGeneralizedTime nextUpdate,
- X509Extensions extensions)
- {
- this.certId = certId;
- if (certStatus == null)
- {
- this.certStatus = new CertStatus();
- }
- else if (certStatus is UnknownStatus)
- {
- this.certStatus = new CertStatus(2, DerNull.Instance);
- }
- else
- {
- RevokedStatus rs = (RevokedStatus) certStatus;
- CrlReason revocationReason = rs.HasRevocationReason
- ? new CrlReason(rs.RevocationReason)
- : null;
- this.certStatus = new CertStatus(
- new RevokedInfo(new DerGeneralizedTime(rs.RevocationTime), revocationReason));
- }
- this.thisUpdate = thisUpdate;
- this.nextUpdate = nextUpdate;
- this.extensions = extensions;
- }
- public SingleResponse ToResponse()
- {
- return new SingleResponse(certId.ToAsn1Object(), certStatus, thisUpdate, nextUpdate, extensions);
- }
- }
- /**
- * basic constructor
- */
- public BasicOcspRespGenerator(
- RespID responderID)
- {
- this.responderID = responderID;
- }
- /**
- * construct with the responderID to be the SHA-1 keyHash of the passed in public key.
- */
- public BasicOcspRespGenerator(
- AsymmetricKeyParameter publicKey)
- {
- this.responderID = new RespID(publicKey);
- }
- /**
- * Add a response for a particular Certificate ID.
- *
- * @param certID certificate ID details
- * @param certStatus status of the certificate - null if okay
- */
- public void AddResponse(
- CertificateID certID,
- CertificateStatus certStatus)
- {
- list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, null));
- }
- /**
- * Add a response for a particular Certificate ID.
- *
- * @param certID certificate ID details
- * @param certStatus status of the certificate - null if okay
- * @param singleExtensions optional extensions
- */
- public void AddResponse(
- CertificateID certID,
- CertificateStatus certStatus,
- X509Extensions singleExtensions)
- {
- list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, singleExtensions));
- }
- /**
- * Add a response for a particular Certificate ID.
- *
- * @param certID certificate ID details
- * @param nextUpdate date when next update should be requested
- * @param certStatus status of the certificate - null if okay
- * @param singleExtensions optional extensions
- */
- public void AddResponse(
- CertificateID certID,
- CertificateStatus certStatus,
- DateTime nextUpdate,
- X509Extensions singleExtensions)
- {
- list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, nextUpdate, singleExtensions));
- }
- /**
- * Add a response for a particular Certificate ID.
- *
- * @param certID certificate ID details
- * @param thisUpdate date this response was valid on
- * @param nextUpdate date when next update should be requested
- * @param certStatus status of the certificate - null if okay
- * @param singleExtensions optional extensions
- */
- public void AddResponse(
- CertificateID certID,
- CertificateStatus certStatus,
- DateTime thisUpdate,
- DateTime nextUpdate,
- X509Extensions singleExtensions)
- {
- list.Add(new ResponseObject(certID, certStatus, thisUpdate, nextUpdate, singleExtensions));
- }
- /**
- * Set the extensions for the response.
- *
- * @param responseExtensions the extension object to carry.
- */
- public void SetResponseExtensions(
- X509Extensions responseExtensions)
- {
- this.responseExtensions = responseExtensions;
- }
- private BasicOcspResp GenerateResponse(
- ISignatureFactory signatureCalculator,
- X509Certificate[] chain,
- DateTime producedAt)
- {
- AlgorithmIdentifier signingAlgID = (AlgorithmIdentifier)signatureCalculator.AlgorithmDetails;
- DerObjectIdentifier signingAlgorithm = signingAlgID.Algorithm;
- Asn1EncodableVector responses = new Asn1EncodableVector();
- foreach (ResponseObject respObj in list)
- {
- try
- {
- responses.Add(respObj.ToResponse());
- }
- catch (Exception e)
- {
- throw new OcspException("exception creating Request", e);
- }
- }
- ResponseData tbsResp = new ResponseData(responderID.ToAsn1Object(), new DerGeneralizedTime(producedAt), new DerSequence(responses), responseExtensions);
- DerBitString bitSig = null;
- try
- {
- IStreamCalculator streamCalculator = signatureCalculator.CreateCalculator();
- byte[] encoded = tbsResp.GetDerEncoded();
- streamCalculator.Stream.Write(encoded, 0, encoded.Length);
- BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
- bitSig = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
- }
- catch (Exception e)
- {
- throw new OcspException("exception processing TBSRequest: " + e, e);
- }
- AlgorithmIdentifier sigAlgId = OcspUtilities.GetSigAlgID(signingAlgorithm);
- DerSequence chainSeq = null;
- if (chain != null && chain.Length > 0)
- {
- Asn1EncodableVector v = new Asn1EncodableVector();
- try
- {
- for (int i = 0; i != chain.Length; i++)
- {
- v.Add(
- X509CertificateStructure.GetInstance(
- Asn1Object.FromByteArray(chain[i].GetEncoded())));
- }
- }
- catch (IOException e)
- {
- throw new OcspException("error processing certs", e);
- }
- catch (CertificateEncodingException e)
- {
- throw new OcspException("error encoding certs", e);
- }
- chainSeq = new DerSequence(v);
- }
- return new BasicOcspResp(new BasicOcspResponse(tbsResp, sigAlgId, bitSig, chainSeq));
- }
- public BasicOcspResp Generate(
- string signingAlgorithm,
- AsymmetricKeyParameter privateKey,
- X509Certificate[] chain,
- DateTime thisUpdate)
- {
- return Generate(signingAlgorithm, privateKey, chain, thisUpdate, null);
- }
- public BasicOcspResp Generate(
- string signingAlgorithm,
- AsymmetricKeyParameter privateKey,
- X509Certificate[] chain,
- DateTime producedAt,
- SecureRandom random)
- {
- if (signingAlgorithm == null)
- {
- throw new ArgumentException("no signing algorithm specified");
- }
- return GenerateResponse(new Asn1SignatureFactory(signingAlgorithm, privateKey, random), chain, producedAt);
- }
- /// <summary>
- /// Generate the signed response using the passed in signature calculator.
- /// </summary>
- /// <param name="signatureCalculatorFactory">Implementation of signing calculator factory.</param>
- /// <param name="chain">The certificate chain associated with the response signer.</param>
- /// <param name="producedAt">"produced at" date.</param>
- /// <returns></returns>
- public BasicOcspResp Generate(
- ISignatureFactory signatureCalculatorFactory,
- X509Certificate[] chain,
- DateTime producedAt)
- {
- if (signatureCalculatorFactory == null)
- {
- throw new ArgumentException("no signature calculator specified");
- }
- return GenerateResponse(signatureCalculatorFactory, chain, producedAt);
- }
- /**
- * Return an IEnumerable of the signature names supported by the generator.
- *
- * @return an IEnumerable containing recognised names.
- */
- public IEnumerable SignatureAlgNames
- {
- get { return OcspUtilities.AlgNames; }
- }
- }
- }
- #pragma warning restore
- #endif
|