CertificateRequestMessage.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf
  9. {
  10. public class CertificateRequestMessage
  11. {
  12. public static readonly int popRaVerified = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf.ProofOfPossession.TYPE_RA_VERIFIED;
  13. public static readonly int popSigningKey = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf.ProofOfPossession.TYPE_SIGNING_KEY;
  14. public static readonly int popKeyEncipherment = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf.ProofOfPossession.TYPE_KEY_ENCIPHERMENT;
  15. public static readonly int popKeyAgreement = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf.ProofOfPossession.TYPE_KEY_AGREEMENT;
  16. private readonly CertReqMsg certReqMsg;
  17. private readonly Controls controls;
  18. private static CertReqMsg ParseBytes(byte[] encoding)
  19. {
  20. return CertReqMsg.GetInstance(encoding);
  21. }
  22. /// <summary>
  23. /// Create a CertificateRequestMessage from the passed in bytes.
  24. /// </summary>
  25. /// <param name="encoded">BER/DER encoding of the CertReqMsg structure.</param>
  26. public CertificateRequestMessage(byte[] encoded)
  27. : this(CertReqMsg.GetInstance(encoded))
  28. {
  29. }
  30. public CertificateRequestMessage(CertReqMsg certReqMsg)
  31. {
  32. this.certReqMsg = certReqMsg;
  33. this.controls = certReqMsg.CertReq.Controls;
  34. }
  35. /// <summary>
  36. /// Return the underlying ASN.1 object defining this CertificateRequestMessage object.
  37. /// </summary>
  38. /// <returns>A CertReqMsg</returns>
  39. public CertReqMsg ToAsn1Structure()
  40. {
  41. return certReqMsg;
  42. }
  43. /// <summary>
  44. /// Return the certificate template contained in this message.
  45. /// </summary>
  46. /// <returns>a CertTemplate structure.</returns>
  47. public CertTemplate GetCertTemplate()
  48. {
  49. return this.certReqMsg.CertReq.CertTemplate;
  50. }
  51. /// <summary>
  52. /// Return whether or not this request has control values associated with it.
  53. /// </summary>
  54. /// <returns>true if there are control values present, false otherwise.</returns>
  55. public bool HasControls
  56. {
  57. get { return controls != null; }
  58. }
  59. /// <summary>
  60. /// Return whether or not this request has a specific type of control value.
  61. /// </summary>
  62. /// <param name="objectIdentifier">the type OID for the control value we are checking for.</param>
  63. /// <returns>true if a control value of type is present, false otherwise.</returns>
  64. public bool HasControl(DerObjectIdentifier objectIdentifier)
  65. {
  66. return FindControl(objectIdentifier) != null;
  67. }
  68. /// <summary>
  69. /// Return a control value of the specified type.
  70. /// </summary>
  71. /// <param name="type">the type OID for the control value we are checking for.</param>
  72. /// <returns>the control value if present, null otherwise.</returns>
  73. public IControl GetControl(DerObjectIdentifier type)
  74. {
  75. AttributeTypeAndValue found = FindControl(type);
  76. if (found != null)
  77. {
  78. if (found.Type.Equals(CrmfObjectIdentifiers.id_regCtrl_pkiArchiveOptions))
  79. {
  80. return new PkiArchiveControl(PkiArchiveOptions.GetInstance(found.Value));
  81. }
  82. if (found.Type.Equals(CrmfObjectIdentifiers.id_regCtrl_regToken))
  83. {
  84. return new RegTokenControl(DerUtf8String.GetInstance(found.Value));
  85. }
  86. if (found.Type.Equals(CrmfObjectIdentifiers.id_regCtrl_authenticator))
  87. {
  88. return new AuthenticatorControl(DerUtf8String.GetInstance(found.Value));
  89. }
  90. }
  91. return null;
  92. }
  93. public AttributeTypeAndValue FindControl(DerObjectIdentifier type)
  94. {
  95. if (controls == null)
  96. {
  97. return null;
  98. }
  99. AttributeTypeAndValue[] tAndV = controls.ToAttributeTypeAndValueArray();
  100. AttributeTypeAndValue found = null;
  101. for (int i = 0; i < tAndV.Length; i++)
  102. {
  103. if (tAndV[i].Type.Equals(type))
  104. {
  105. found = tAndV[i];
  106. break;
  107. }
  108. }
  109. return found;
  110. }
  111. /// <summary>
  112. /// Return whether or not this request message has a proof-of-possession field in it.
  113. /// </summary>
  114. /// <returns>true if proof-of-possession is present, false otherwise.</returns>
  115. public bool HasProofOfPossession
  116. {
  117. get { return certReqMsg.Popo != null; }
  118. }
  119. /// <summary>
  120. /// Return the type of the proof-of-possession this request message provides.
  121. /// </summary>
  122. /// <returns>one of: popRaVerified, popSigningKey, popKeyEncipherment, popKeyAgreement</returns>
  123. public int ProofOfPossession
  124. {
  125. get { return certReqMsg.Popo.Type; }
  126. }
  127. /// <summary>
  128. /// Return whether or not the proof-of-possession (POP) is of the type popSigningKey and
  129. /// it has a public key MAC associated with it.
  130. /// </summary>
  131. /// <returns>true if POP is popSigningKey and a PKMAC is present, false otherwise.</returns>
  132. public bool HasSigningKeyProofOfPossessionWithPkMac
  133. {
  134. get
  135. {
  136. ProofOfPossession pop = certReqMsg.Popo;
  137. if (pop.Type == popSigningKey)
  138. {
  139. PopoSigningKey popoSign = PopoSigningKey.GetInstance(pop.Object);
  140. return popoSign.PoposkInput.PublicKeyMac != null;
  141. }
  142. return false;
  143. }
  144. }
  145. /// <summary>
  146. /// Return whether or not a signing key proof-of-possession (POP) is valid.
  147. /// </summary>
  148. /// <param name="verifierProvider">a provider that can produce content verifiers for the signature contained in this POP.</param>
  149. /// <returns>true if the POP is valid, false otherwise.</returns>
  150. /// <exception cref="InvalidOperationException">if there is a problem in verification or content verifier creation.</exception>
  151. /// <exception cref="InvalidOperationException">if POP not appropriate.</exception>
  152. public bool IsValidSigningKeyPop(IVerifierFactoryProvider verifierProvider)
  153. {
  154. ProofOfPossession pop = certReqMsg.Popo;
  155. if (pop.Type == popSigningKey)
  156. {
  157. PopoSigningKey popoSign = PopoSigningKey.GetInstance(pop.Object);
  158. if (popoSign.PoposkInput != null && popoSign.PoposkInput.PublicKeyMac != null)
  159. {
  160. throw new InvalidOperationException("verification requires password check");
  161. }
  162. return verifySignature(verifierProvider, popoSign);
  163. }
  164. throw new InvalidOperationException("not Signing Key type of proof of possession");
  165. }
  166. private bool verifySignature(IVerifierFactoryProvider verifierFactoryProvider, PopoSigningKey signKey)
  167. {
  168. IVerifierFactory verifer;
  169. IStreamCalculator calculator;
  170. try
  171. {
  172. verifer = verifierFactoryProvider.CreateVerifierFactory(signKey.AlgorithmIdentifier);
  173. calculator = verifer.CreateCalculator();
  174. }
  175. catch (Exception ex)
  176. {
  177. throw new CrmfException("unable to create verifier: " + ex.Message, ex);
  178. }
  179. if (signKey.PoposkInput != null)
  180. {
  181. byte[] b = signKey.GetDerEncoded();
  182. calculator.Stream.Write(b, 0, b.Length);
  183. }
  184. else
  185. {
  186. byte[] b = certReqMsg.CertReq.GetDerEncoded();
  187. calculator.Stream.Write(b, 0, b.Length);
  188. }
  189. DefaultVerifierResult result = (DefaultVerifierResult)calculator.GetResult();
  190. return result.IsVerified(signKey.Signature.GetBytes());
  191. }
  192. /// <summary>
  193. /// Return the ASN.1 encoding of the certReqMsg we wrap.
  194. /// </summary>
  195. /// <returns>a byte array containing the binary encoding of the certReqMsg.</returns>
  196. public byte[] GetEncoded()
  197. {
  198. return certReqMsg.GetEncoded();
  199. }
  200. }
  201. }
  202. #pragma warning restore
  203. #endif