CertificateRequestMessageBuilder.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf
  13. {
  14. public class CertificateRequestMessageBuilder
  15. {
  16. private readonly BigInteger _certReqId;
  17. private X509ExtensionsGenerator _extGenerator;
  18. private CertTemplateBuilder _templateBuilder;
  19. private IList _controls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  20. private ISignatureFactory _popSigner;
  21. private PKMacBuilder _pkMacBuilder;
  22. private char[] _password;
  23. private GeneralName _sender;
  24. private int _popoType = ProofOfPossession.TYPE_KEY_ENCIPHERMENT;
  25. private PopoPrivKey _popoPrivKey;
  26. private Asn1Null _popRaVerified;
  27. private PKMacValue _agreeMac;
  28. public CertificateRequestMessageBuilder(BigInteger certReqId)
  29. {
  30. this._certReqId = certReqId;
  31. this._extGenerator = new X509ExtensionsGenerator();
  32. this._templateBuilder = new CertTemplateBuilder();
  33. }
  34. public CertificateRequestMessageBuilder SetPublicKey(SubjectPublicKeyInfo publicKeyInfo)
  35. {
  36. if (publicKeyInfo != null)
  37. {
  38. _templateBuilder.SetPublicKey(publicKeyInfo);
  39. }
  40. return this;
  41. }
  42. public CertificateRequestMessageBuilder SetIssuer(X509Name issuer)
  43. {
  44. if (issuer != null)
  45. {
  46. _templateBuilder.SetIssuer(issuer);
  47. }
  48. return this;
  49. }
  50. public CertificateRequestMessageBuilder SetSubject(X509Name subject)
  51. {
  52. if (subject != null)
  53. {
  54. _templateBuilder.SetSubject(subject);
  55. }
  56. return this;
  57. }
  58. public CertificateRequestMessageBuilder SetSerialNumber(BigInteger serialNumber)
  59. {
  60. if (serialNumber != null)
  61. {
  62. _templateBuilder.SetSerialNumber(new DerInteger(serialNumber));
  63. }
  64. return this;
  65. }
  66. public CertificateRequestMessageBuilder SetValidity(Time notBefore, Time notAfter)
  67. {
  68. _templateBuilder.SetValidity(new OptionalValidity(notBefore, notAfter));
  69. return this;
  70. }
  71. public CertificateRequestMessageBuilder AddExtension(DerObjectIdentifier oid, bool critical,
  72. Asn1Encodable value)
  73. {
  74. _extGenerator.AddExtension(oid, critical, value);
  75. return this;
  76. }
  77. public CertificateRequestMessageBuilder AddExtension(DerObjectIdentifier oid, bool critical,
  78. byte[] value)
  79. {
  80. _extGenerator.AddExtension(oid, critical, value);
  81. return this;
  82. }
  83. public CertificateRequestMessageBuilder AddControl(IControl control)
  84. {
  85. _controls.Add(control);
  86. return this;
  87. }
  88. public CertificateRequestMessageBuilder SetProofOfPossessionSignKeySigner(ISignatureFactory popoSignatureFactory)
  89. {
  90. if (_popoPrivKey != null || _popRaVerified != null || _agreeMac != null)
  91. {
  92. throw new InvalidOperationException("only one proof of possession is allowed.");
  93. }
  94. this._popSigner = popoSignatureFactory;
  95. return this;
  96. }
  97. public CertificateRequestMessageBuilder SetProofOfPossessionSubsequentMessage(SubsequentMessage msg)
  98. {
  99. if (_popoPrivKey != null || _popRaVerified != null || _agreeMac != null)
  100. {
  101. throw new InvalidOperationException("only one proof of possession is allowed.");
  102. }
  103. this._popoType = ProofOfPossession.TYPE_KEY_ENCIPHERMENT;
  104. this._popoPrivKey = new PopoPrivKey(msg);
  105. return this;
  106. }
  107. public CertificateRequestMessageBuilder SetProofOfPossessionSubsequentMessage(int type, SubsequentMessage msg)
  108. {
  109. if (_popoPrivKey != null || _popRaVerified != null || _agreeMac != null)
  110. {
  111. throw new InvalidOperationException("only one proof of possession is allowed.");
  112. }
  113. if (type != ProofOfPossession.TYPE_KEY_ENCIPHERMENT && type != ProofOfPossession.TYPE_KEY_AGREEMENT)
  114. {
  115. throw new ArgumentException("type must be ProofOfPossession.TYPE_KEY_ENCIPHERMENT || ProofOfPossession.TYPE_KEY_AGREEMENT");
  116. }
  117. this._popoType = type;
  118. this._popoPrivKey = new PopoPrivKey(msg);
  119. return this;
  120. }
  121. public CertificateRequestMessageBuilder SetProofOfPossessionAgreeMac(PKMacValue macValue)
  122. {
  123. if (_popSigner != null || _popRaVerified != null || _popoPrivKey != null)
  124. {
  125. throw new InvalidOperationException("only one proof of possession allowed");
  126. }
  127. this._agreeMac = macValue;
  128. return this;
  129. }
  130. public CertificateRequestMessageBuilder SetProofOfPossessionRaVerified()
  131. {
  132. if (_popSigner != null || _popoPrivKey != null)
  133. {
  134. throw new InvalidOperationException("only one proof of possession allowed");
  135. }
  136. this._popRaVerified = DerNull.Instance;
  137. return this;
  138. }
  139. public CertificateRequestMessageBuilder SetAuthInfoPKMAC(PKMacBuilder pkmacFactory, char[] password)
  140. {
  141. this._pkMacBuilder = pkmacFactory;
  142. this._password = password;
  143. return this;
  144. }
  145. public CertificateRequestMessageBuilder SetAuthInfoSender(X509Name sender)
  146. {
  147. return SetAuthInfoSender(new GeneralName(sender));
  148. }
  149. public CertificateRequestMessageBuilder SetAuthInfoSender(GeneralName sender)
  150. {
  151. this._sender = sender;
  152. return this;
  153. }
  154. public CertificateRequestMessage Build()
  155. {
  156. Asn1EncodableVector v = new Asn1EncodableVector(new DerInteger(this._certReqId));
  157. if (!this._extGenerator.IsEmpty)
  158. {
  159. this._templateBuilder.SetExtensions(_extGenerator.Generate());
  160. }
  161. v.Add(_templateBuilder.Build());
  162. if (_controls.Count > 0)
  163. {
  164. Asn1EncodableVector controlV = new Asn1EncodableVector();
  165. foreach (object item in _controls)
  166. {
  167. IControl control = (IControl)item;
  168. controlV.Add(new AttributeTypeAndValue(control.Type, control.Value));
  169. }
  170. v.Add(new DerSequence(controlV));
  171. }
  172. CertRequest request = CertRequest.GetInstance(new DerSequence(v));
  173. v = new Asn1EncodableVector(request);
  174. if (_popSigner != null)
  175. {
  176. CertTemplate template = request.CertTemplate;
  177. if (template.Subject == null || template.PublicKey == null)
  178. {
  179. SubjectPublicKeyInfo pubKeyInfo = request.CertTemplate.PublicKey;
  180. ProofOfPossessionSigningKeyBuilder builder = new ProofOfPossessionSigningKeyBuilder(pubKeyInfo);
  181. if (_sender != null)
  182. {
  183. builder.SetSender(_sender);
  184. }
  185. else
  186. {
  187. //PKMACValueGenerator pkmacGenerator = new PKMACValueGenerator(_pkmacBuilder);
  188. builder.SetPublicKeyMac(_pkMacBuilder, _password);
  189. }
  190. v.Add(new ProofOfPossession(builder.Build(_popSigner)));
  191. }
  192. else
  193. {
  194. ProofOfPossessionSigningKeyBuilder builder = new ProofOfPossessionSigningKeyBuilder(request);
  195. v.Add(new ProofOfPossession(builder.Build(_popSigner)));
  196. }
  197. }
  198. else if (_popoPrivKey != null)
  199. {
  200. v.Add(new ProofOfPossession(_popoType, _popoPrivKey));
  201. }
  202. else if (_agreeMac != null)
  203. {
  204. v.Add(new ProofOfPossession(ProofOfPossession.TYPE_KEY_AGREEMENT,
  205. PopoPrivKey.GetInstance(new DerTaggedObject(false, PopoPrivKey.agreeMAC, _agreeMac), true)));
  206. }
  207. else if (_popRaVerified != null)
  208. {
  209. v.Add(new ProofOfPossession());
  210. }
  211. return new CertificateRequestMessage(CertReqMsg.GetInstance(new DerSequence(v)));
  212. }
  213. }
  214. }
  215. #pragma warning restore
  216. #endif