Pkcs10CertificationRequestDelaySigned.cs 5.5 KB

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