X509SignatureUtil.cs 3.7 KB

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