X509SignatureUtil.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.CryptoPro;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  15. {
  16. internal class X509SignatureUtilities
  17. {
  18. private static readonly Asn1Null derNull = DerNull.Instance;
  19. internal static void SetSignatureParameters(
  20. ISigner signature,
  21. Asn1Encodable parameters)
  22. {
  23. if (parameters != null && !derNull.Equals(parameters))
  24. {
  25. // TODO Put back in
  26. // AlgorithmParameters sigParams = AlgorithmParameters.GetInstance(signature.getAlgorithm());
  27. //
  28. // try
  29. // {
  30. // sigParams.Init(parameters.ToAsn1Object().GetDerEncoded());
  31. // }
  32. // catch (IOException e)
  33. // {
  34. // throw new SignatureException("IOException decoding parameters: " + e.Message);
  35. // }
  36. //
  37. // if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(signature.getAlgorithm(), "MGF1"))
  38. // {
  39. // try
  40. // {
  41. // signature.setParameter(sigParams.getParameterSpec(PSSParameterSpec.class));
  42. // }
  43. // catch (GeneralSecurityException e)
  44. // {
  45. // throw new SignatureException("Exception extracting parameters: " + e.Message);
  46. // }
  47. // }
  48. }
  49. }
  50. internal static string GetSignatureName(
  51. AlgorithmIdentifier sigAlgId)
  52. {
  53. Asn1Encodable parameters = sigAlgId.Parameters;
  54. if (parameters != null && !derNull.Equals(parameters))
  55. {
  56. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  57. {
  58. RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
  59. return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
  60. }
  61. if (sigAlgId.Algorithm.Equals(X9ObjectIdentifiers.ECDsaWithSha2))
  62. {
  63. Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
  64. return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
  65. }
  66. }
  67. string sigName = SignerUtilities.GetEncodingName(sigAlgId.Algorithm);
  68. if (null != sigName)
  69. {
  70. return sigName;
  71. }
  72. return sigAlgId.Algorithm.Id;
  73. }
  74. /**
  75. * Return the digest algorithm using one of the standard JCA string
  76. * representations rather than the algorithm identifier (if possible).
  77. */
  78. private static string GetDigestAlgName(
  79. DerObjectIdentifier digestAlgOID)
  80. {
  81. if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
  82. {
  83. return "MD5";
  84. }
  85. else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
  86. {
  87. return "SHA1";
  88. }
  89. else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
  90. {
  91. return "SHA224";
  92. }
  93. else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
  94. {
  95. return "SHA256";
  96. }
  97. else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
  98. {
  99. return "SHA384";
  100. }
  101. else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
  102. {
  103. return "SHA512";
  104. }
  105. else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
  106. {
  107. return "RIPEMD128";
  108. }
  109. else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
  110. {
  111. return "RIPEMD160";
  112. }
  113. else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
  114. {
  115. return "RIPEMD256";
  116. }
  117. else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
  118. {
  119. return "GOST3411";
  120. }
  121. else
  122. {
  123. return digestAlgOID.Id;
  124. }
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif