123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #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.Crypto.Parameters;
- 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;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
- {
- public class OcspReqGenerator
- {
- private IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- private GeneralName requestorName = null;
- private X509Extensions requestExtensions = null;
- private class RequestObject
- {
- internal CertificateID certId;
- internal X509Extensions extensions;
- public RequestObject(
- CertificateID certId,
- X509Extensions extensions)
- {
- this.certId = certId;
- this.extensions = extensions;
- }
- public Request ToRequest()
- {
- return new Request(certId.ToAsn1Object(), extensions);
- }
- }
- /**
- * Add a request for the given CertificateID.
- *
- * @param certId certificate ID of interest
- */
- public void AddRequest(
- CertificateID certId)
- {
- list.Add(new RequestObject(certId, null));
- }
- /**
- * Add a request with extensions
- *
- * @param certId certificate ID of interest
- * @param singleRequestExtensions the extensions to attach to the request
- */
- public void AddRequest(
- CertificateID certId,
- X509Extensions singleRequestExtensions)
- {
- list.Add(new RequestObject(certId, singleRequestExtensions));
- }
- /**
- * Set the requestor name to the passed in X509Principal
- *
- * @param requestorName a X509Principal representing the requestor name.
- */
- public void SetRequestorName(
- X509Name requestorName)
- {
- try
- {
- this.requestorName = new GeneralName(GeneralName.DirectoryName, requestorName);
- }
- catch (Exception e)
- {
- throw new ArgumentException("cannot encode principal", e);
- }
- }
- public void SetRequestorName(
- GeneralName requestorName)
- {
- this.requestorName = requestorName;
- }
- public void SetRequestExtensions(
- X509Extensions requestExtensions)
- {
- this.requestExtensions = requestExtensions;
- }
- private OcspReq GenerateRequest(
- DerObjectIdentifier signingAlgorithm,
- AsymmetricKeyParameter privateKey,
- X509Certificate[] chain,
- SecureRandom random)
- {
- Asn1EncodableVector requests = new Asn1EncodableVector();
- foreach (RequestObject reqObj in list)
- {
- try
- {
- requests.Add(reqObj.ToRequest());
- }
- catch (Exception e)
- {
- throw new OcspException("exception creating Request", e);
- }
- }
- TbsRequest tbsReq = new TbsRequest(requestorName, new DerSequence(requests), requestExtensions);
- ISigner sig = null;
- Signature signature = null;
- if (signingAlgorithm != null)
- {
- if (requestorName == null)
- {
- throw new OcspException("requestorName must be specified if request is signed.");
- }
- try
- {
- sig = SignerUtilities.GetSigner(signingAlgorithm.Id);
- if (random != null)
- {
- sig.Init(true, new ParametersWithRandom(privateKey, random));
- }
- else
- {
- sig.Init(true, privateKey);
- }
- }
- catch (Exception e)
- {
- throw new OcspException("exception creating signature: " + e, e);
- }
- DerBitString bitSig = null;
- try
- {
- byte[] encoded = tbsReq.GetEncoded();
- sig.BlockUpdate(encoded, 0, encoded.Length);
- bitSig = new DerBitString(sig.GenerateSignature());
- }
- catch (Exception e)
- {
- throw new OcspException("exception processing TBSRequest: " + e, e);
- }
- AlgorithmIdentifier sigAlgId = new AlgorithmIdentifier(signingAlgorithm, DerNull.Instance);
- 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);
- }
- signature = new Signature(sigAlgId, bitSig, new DerSequence(v));
- }
- else
- {
- signature = new Signature(sigAlgId, bitSig);
- }
- }
- return new OcspReq(new OcspRequest(tbsReq, signature));
- }
- /**
- * Generate an unsigned request
- *
- * @return the OcspReq
- * @throws OcspException
- */
- public OcspReq Generate()
- {
- return GenerateRequest(null, null, null, null);
- }
- public OcspReq Generate(
- string signingAlgorithm,
- AsymmetricKeyParameter privateKey,
- X509Certificate[] chain)
- {
- return Generate(signingAlgorithm, privateKey, chain, null);
- }
- public OcspReq Generate(
- string signingAlgorithm,
- AsymmetricKeyParameter privateKey,
- X509Certificate[] chain,
- SecureRandom random)
- {
- if (signingAlgorithm == null)
- throw new ArgumentException("no signing algorithm specified");
- try
- {
- DerObjectIdentifier oid = OcspUtilities.GetAlgorithmOid(signingAlgorithm);
- return GenerateRequest(oid, privateKey, chain, random);
- }
- catch (ArgumentException)
- {
- throw new ArgumentException("unknown signing algorithm specified: " + signingAlgorithm);
- }
- }
- /**
- * 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
|