X509SignatureUtil.cs 3.4 KB

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