TSPUtil.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM;
  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.Rosstandart;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Cms;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  20. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  21. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tsp
  22. {
  23. public class TspUtil
  24. {
  25. private static ISet EmptySet = CollectionUtilities.ReadOnly(new HashSet());
  26. private static IList EmptyList = CollectionUtilities.ReadOnly(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList());
  27. private static readonly IDictionary digestLengths = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  28. private static readonly IDictionary digestNames = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  29. static TspUtil()
  30. {
  31. digestLengths.Add(PkcsObjectIdentifiers.MD5.Id, 16);
  32. digestLengths.Add(OiwObjectIdentifiers.IdSha1.Id, 20);
  33. digestLengths.Add(NistObjectIdentifiers.IdSha224.Id, 28);
  34. digestLengths.Add(NistObjectIdentifiers.IdSha256.Id, 32);
  35. digestLengths.Add(NistObjectIdentifiers.IdSha384.Id, 48);
  36. digestLengths.Add(NistObjectIdentifiers.IdSha512.Id, 64);
  37. digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, 16);
  38. digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, 20);
  39. digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, 32);
  40. digestLengths.Add(CryptoProObjectIdentifiers.GostR3411.Id, 32);
  41. digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, 32);
  42. digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, 64);
  43. digestLengths.Add(GMObjectIdentifiers.sm3.Id, 32);
  44. digestNames.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
  45. digestNames.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
  46. digestNames.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
  47. digestNames.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
  48. digestNames.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
  49. digestNames.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
  50. digestNames.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption.Id, "MD5");
  51. digestNames.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id, "SHA1");
  52. digestNames.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id, "SHA224");
  53. digestNames.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, "SHA256");
  54. digestNames.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id, "SHA384");
  55. digestNames.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id, "SHA512");
  56. digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
  57. digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
  58. digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
  59. digestNames.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
  60. digestNames.Add(OiwObjectIdentifiers.DsaWithSha1.Id, "SHA1");
  61. digestNames.Add(OiwObjectIdentifiers.Sha1WithRsa.Id, "SHA1");
  62. digestNames.Add(OiwObjectIdentifiers.MD5WithRsa.Id, "MD5");
  63. digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
  64. digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
  65. digestNames.Add(GMObjectIdentifiers.sm3.Id, "SM3");
  66. }
  67. /**
  68. * Fetches the signature time-stamp attributes from a SignerInformation object.
  69. * Checks that the MessageImprint for each time-stamp matches the signature field.
  70. * (see RFC 3161 Appendix A).
  71. *
  72. * @param signerInfo a SignerInformation to search for time-stamps
  73. * @return a collection of TimeStampToken objects
  74. * @throws TSPValidationException
  75. */
  76. public static ICollection GetSignatureTimestamps(
  77. SignerInformation signerInfo)
  78. {
  79. IList timestamps = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  80. Asn1.Cms.AttributeTable unsignedAttrs = signerInfo.UnsignedAttributes;
  81. if (unsignedAttrs != null)
  82. {
  83. foreach (Asn1.Cms.Attribute tsAttr in unsignedAttrs.GetAll(
  84. PkcsObjectIdentifiers.IdAASignatureTimeStampToken))
  85. {
  86. foreach (Asn1Encodable asn1 in tsAttr.AttrValues)
  87. {
  88. try
  89. {
  90. Asn1.Cms.ContentInfo contentInfo = Asn1.Cms.ContentInfo.GetInstance(
  91. asn1.ToAsn1Object());
  92. TimeStampToken timeStampToken = new TimeStampToken(contentInfo);
  93. TimeStampTokenInfo tstInfo = timeStampToken.TimeStampInfo;
  94. byte[] expectedDigest = DigestUtilities.CalculateDigest(
  95. GetDigestAlgName(tstInfo.MessageImprintAlgOid),
  96. signerInfo.GetSignature());
  97. if (!Arrays.ConstantTimeAreEqual(expectedDigest, tstInfo.GetMessageImprintDigest()))
  98. throw new TspValidationException("Incorrect digest in message imprint");
  99. timestamps.Add(timeStampToken);
  100. }
  101. catch (SecurityUtilityException)
  102. {
  103. throw new TspValidationException("Unknown hash algorithm specified in timestamp");
  104. }
  105. catch (Exception)
  106. {
  107. throw new TspValidationException("Timestamp could not be parsed");
  108. }
  109. }
  110. }
  111. }
  112. return timestamps;
  113. }
  114. /**
  115. * Validate the passed in certificate as being of the correct type to be used
  116. * for time stamping. To be valid it must have an ExtendedKeyUsage extension
  117. * which has a key purpose identifier of id-kp-timeStamping.
  118. *
  119. * @param cert the certificate of interest.
  120. * @throws TspValidationException if the certicate fails on one of the check points.
  121. */
  122. public static void ValidateCertificate(
  123. X509Certificate cert)
  124. {
  125. if (cert.Version != 3)
  126. throw new ArgumentException("Certificate must have an ExtendedKeyUsage extension.");
  127. Asn1OctetString ext = cert.GetExtensionValue(X509Extensions.ExtendedKeyUsage);
  128. if (ext == null)
  129. throw new TspValidationException("Certificate must have an ExtendedKeyUsage extension.");
  130. if (!cert.GetCriticalExtensionOids().Contains(X509Extensions.ExtendedKeyUsage.Id))
  131. throw new TspValidationException("Certificate must have an ExtendedKeyUsage extension marked as critical.");
  132. try
  133. {
  134. ExtendedKeyUsage extKey = ExtendedKeyUsage.GetInstance(
  135. Asn1Object.FromByteArray(ext.GetOctets()));
  136. if (!extKey.HasKeyPurposeId(KeyPurposeID.IdKPTimeStamping) || extKey.Count != 1)
  137. throw new TspValidationException("ExtendedKeyUsage not solely time stamping.");
  138. }
  139. catch (IOException)
  140. {
  141. throw new TspValidationException("cannot process ExtendedKeyUsage extension");
  142. }
  143. }
  144. /// <summary>
  145. /// Return the digest algorithm using one of the standard JCA string
  146. /// representations rather than the algorithm identifier (if possible).
  147. /// </summary>
  148. internal static string GetDigestAlgName(
  149. string digestAlgOID)
  150. {
  151. string digestName = (string) digestNames[digestAlgOID];
  152. return digestName != null ? digestName : digestAlgOID;
  153. }
  154. internal static int GetDigestLength(
  155. string digestAlgOID)
  156. {
  157. if (!digestLengths.Contains(digestAlgOID))
  158. throw new TspException("digest algorithm cannot be found.");
  159. return (int)digestLengths[digestAlgOID];
  160. }
  161. internal static IDigest CreateDigestInstance(
  162. String digestAlgOID)
  163. {
  164. string digestName = GetDigestAlgName(digestAlgOID);
  165. return DigestUtilities.GetDigest(digestName);
  166. }
  167. internal static ISet GetCriticalExtensionOids(X509Extensions extensions)
  168. {
  169. if (extensions == null)
  170. return EmptySet;
  171. return CollectionUtilities.ReadOnly(new HashSet(extensions.GetCriticalExtensionOids()));
  172. }
  173. internal static ISet GetNonCriticalExtensionOids(X509Extensions extensions)
  174. {
  175. if (extensions == null)
  176. return EmptySet;
  177. // TODO: should probably produce a set that imposes correct ordering
  178. return CollectionUtilities.ReadOnly(new HashSet(extensions.GetNonCriticalExtensionOids()));
  179. }
  180. internal static IList GetExtensionOids(X509Extensions extensions)
  181. {
  182. if (extensions == null)
  183. return EmptyList;
  184. return CollectionUtilities.ReadOnly(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.GetExtensionOids()));
  185. }
  186. }
  187. }
  188. #pragma warning restore
  189. #endif