Pkcs10CertificationRequestDelaySigned.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Pkcs
  12. {
  13. /// <remarks>
  14. /// A class for creating and verifying Pkcs10 Certification requests (this is an extension on <see cref="Pkcs10CertificationRequest"/>).
  15. /// The requests are made using delay signing. This is useful for situations where
  16. /// the private key is in another environment and not directly accessible (e.g. HSM)
  17. /// So the first step creates the request, then the signing is done outside this
  18. /// object and the signature is then used to complete the request.
  19. /// </remarks>
  20. /// <code>
  21. /// CertificationRequest ::= Sequence {
  22. /// certificationRequestInfo CertificationRequestInfo,
  23. /// signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
  24. /// signature BIT STRING
  25. /// }
  26. ///
  27. /// CertificationRequestInfo ::= Sequence {
  28. /// version Integer { v1(0) } (v1,...),
  29. /// subject Name,
  30. /// subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
  31. /// attributes [0] Attributes{{ CRIAttributes }}
  32. /// }
  33. ///
  34. /// Attributes { ATTRIBUTE:IOSet } ::= Set OF Attr{{ IOSet }}
  35. ///
  36. /// Attr { ATTRIBUTE:IOSet } ::= Sequence {
  37. /// type ATTRIBUTE.&amp;id({IOSet}),
  38. /// values Set SIZE(1..MAX) OF ATTRIBUTE.&amp;Type({IOSet}{\@type})
  39. /// }
  40. /// </code>
  41. /// see <a href="http://www.rsasecurity.com/rsalabs/node.asp?id=2132"/>
  42. public class Pkcs10CertificationRequestDelaySigned : Pkcs10CertificationRequest
  43. {
  44. protected Pkcs10CertificationRequestDelaySigned()
  45. : base()
  46. {
  47. }
  48. public Pkcs10CertificationRequestDelaySigned(
  49. byte[] encoded)
  50. : base(encoded)
  51. {
  52. }
  53. public Pkcs10CertificationRequestDelaySigned(
  54. Asn1Sequence seq)
  55. : base(seq)
  56. {
  57. }
  58. public Pkcs10CertificationRequestDelaySigned(
  59. Stream input)
  60. : base(input)
  61. {
  62. }
  63. public Pkcs10CertificationRequestDelaySigned(
  64. string signatureAlgorithm,
  65. X509Name subject,
  66. AsymmetricKeyParameter publicKey,
  67. Asn1Set attributes,
  68. AsymmetricKeyParameter signingKey)
  69. : base(signatureAlgorithm, subject, publicKey, attributes, signingKey)
  70. {
  71. }
  72. /// <summary>
  73. /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
  74. /// </summary>
  75. /// <param name="signatureAlgorithm">Name of Sig Alg.</param>
  76. /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
  77. /// <param name="publicKey">Public Key to be included in cert reqest.</param>
  78. /// <param name="attributes">ASN1Set of Attributes.</param>
  79. /// <remarks>
  80. /// After the object is constructed use the <see cref="GetDataToSign"/> and finally the
  81. /// SignRequest methods to finalize the request.
  82. /// </remarks>
  83. public Pkcs10CertificationRequestDelaySigned(
  84. string signatureAlgorithm,
  85. X509Name subject,
  86. AsymmetricKeyParameter publicKey,
  87. Asn1Set attributes)
  88. {
  89. if (signatureAlgorithm == null)
  90. throw new ArgumentNullException("signatureAlgorithm");
  91. if (subject == null)
  92. throw new ArgumentNullException("subject");
  93. if (publicKey == null)
  94. throw new ArgumentNullException("publicKey");
  95. if (publicKey.IsPrivate)
  96. throw new ArgumentException("expected public key", "publicKey");
  97. DerObjectIdentifier sigOid = CollectionUtilities.GetValueOrNull(m_algorithms, signatureAlgorithm);
  98. if (sigOid == null)
  99. {
  100. try
  101. {
  102. sigOid = new DerObjectIdentifier(signatureAlgorithm);
  103. }
  104. catch (Exception e)
  105. {
  106. throw new ArgumentException("Unknown signature type requested", e);
  107. }
  108. }
  109. if (m_noParams.Contains(sigOid))
  110. {
  111. this.sigAlgId = new AlgorithmIdentifier(sigOid);
  112. }
  113. else if (m_exParams.TryGetValue(signatureAlgorithm, out var explicitParameters))
  114. {
  115. this.sigAlgId = new AlgorithmIdentifier(sigOid, explicitParameters);
  116. }
  117. else
  118. {
  119. this.sigAlgId = new AlgorithmIdentifier(sigOid, DerNull.Instance);
  120. }
  121. SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
  122. this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
  123. }
  124. public byte[] GetDataToSign()
  125. {
  126. return reqInfo.GetDerEncoded();
  127. }
  128. public void SignRequest(byte[] signedData)
  129. {
  130. //build the signature from the signed data
  131. sigBits = new DerBitString(signedData);
  132. }
  133. public void SignRequest(DerBitString signedData)
  134. {
  135. //build the signature from the signed data
  136. sigBits = signedData;
  137. }
  138. }
  139. }
  140. #pragma warning restore
  141. #endif