SignerUtilities.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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.Bsi;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Eac;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  20. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  21. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  22. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  23. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Security
  24. {
  25. /// <summary>
  26. /// Signer Utility class contains methods that can not be specifically grouped into other classes.
  27. /// </summary>
  28. public sealed class SignerUtilities
  29. {
  30. private SignerUtilities()
  31. {
  32. }
  33. internal static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  34. internal static readonly IDictionary oids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  35. static SignerUtilities()
  36. {
  37. algorithms["MD2WITHRSA"] = "MD2withRSA";
  38. algorithms["MD2WITHRSAENCRYPTION"] = "MD2withRSA";
  39. algorithms[PkcsObjectIdentifiers.MD2WithRsaEncryption.Id] = "MD2withRSA";
  40. algorithms["MD4WITHRSA"] = "MD4withRSA";
  41. algorithms["MD4WITHRSAENCRYPTION"] = "MD4withRSA";
  42. algorithms[PkcsObjectIdentifiers.MD4WithRsaEncryption.Id] = "MD4withRSA";
  43. algorithms[OiwObjectIdentifiers.MD4WithRsa.Id] = "MD4withRSA";
  44. algorithms[OiwObjectIdentifiers.MD4WithRsaEncryption.Id] = "MD4withRSA";
  45. algorithms["MD5WITHRSA"] = "MD5withRSA";
  46. algorithms["MD5WITHRSAENCRYPTION"] = "MD5withRSA";
  47. algorithms[PkcsObjectIdentifiers.MD5WithRsaEncryption.Id] = "MD5withRSA";
  48. algorithms[OiwObjectIdentifiers.MD5WithRsa.Id] = "MD5withRSA";
  49. algorithms["SHA1WITHRSA"] = "SHA-1withRSA";
  50. algorithms["SHA1WITHRSAENCRYPTION"] = "SHA-1withRSA";
  51. algorithms["SHA-1WITHRSA"] = "SHA-1withRSA";
  52. algorithms[PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id] = "SHA-1withRSA";
  53. algorithms[OiwObjectIdentifiers.Sha1WithRsa.Id] = "SHA-1withRSA";
  54. algorithms["SHA224WITHRSA"] = "SHA-224withRSA";
  55. algorithms["SHA224WITHRSAENCRYPTION"] = "SHA-224withRSA";
  56. algorithms[PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id] = "SHA-224withRSA";
  57. algorithms["SHA-224WITHRSA"] = "SHA-224withRSA";
  58. algorithms["SHA256WITHRSA"] = "SHA-256withRSA";
  59. algorithms["SHA256WITHRSAENCRYPTION"] = "SHA-256withRSA";
  60. algorithms[PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id] = "SHA-256withRSA";
  61. algorithms["SHA-256WITHRSA"] = "SHA-256withRSA";
  62. algorithms["SHA384WITHRSA"] = "SHA-384withRSA";
  63. algorithms["SHA384WITHRSAENCRYPTION"] = "SHA-384withRSA";
  64. algorithms[PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id] = "SHA-384withRSA";
  65. algorithms["SHA-384WITHRSA"] = "SHA-384withRSA";
  66. algorithms["SHA512WITHRSA"] = "SHA-512withRSA";
  67. algorithms["SHA512WITHRSAENCRYPTION"] = "SHA-512withRSA";
  68. algorithms[PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id] = "SHA-512withRSA";
  69. algorithms["SHA-512WITHRSA"] = "SHA-512withRSA";
  70. algorithms["PSSWITHRSA"] = "PSSwithRSA";
  71. algorithms["RSASSA-PSS"] = "PSSwithRSA";
  72. algorithms[PkcsObjectIdentifiers.IdRsassaPss.Id] = "PSSwithRSA";
  73. algorithms["RSAPSS"] = "PSSwithRSA";
  74. algorithms["SHA1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
  75. algorithms["SHA-1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
  76. algorithms["SHA1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
  77. algorithms["SHA-1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
  78. algorithms["SHA224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
  79. algorithms["SHA-224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
  80. algorithms["SHA224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
  81. algorithms["SHA-224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
  82. algorithms["SHA256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
  83. algorithms["SHA-256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
  84. algorithms["SHA256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
  85. algorithms["SHA-256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
  86. algorithms["SHA384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
  87. algorithms["SHA-384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
  88. algorithms["SHA384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
  89. algorithms["SHA-384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
  90. algorithms["SHA512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
  91. algorithms["SHA-512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
  92. algorithms["SHA512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
  93. algorithms["SHA-512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
  94. algorithms["RIPEMD128WITHRSA"] = "RIPEMD128withRSA";
  95. algorithms["RIPEMD128WITHRSAENCRYPTION"] = "RIPEMD128withRSA";
  96. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128.Id] = "RIPEMD128withRSA";
  97. algorithms["RIPEMD160WITHRSA"] = "RIPEMD160withRSA";
  98. algorithms["RIPEMD160WITHRSAENCRYPTION"] = "RIPEMD160withRSA";
  99. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160.Id] = "RIPEMD160withRSA";
  100. algorithms["RIPEMD256WITHRSA"] = "RIPEMD256withRSA";
  101. algorithms["RIPEMD256WITHRSAENCRYPTION"] = "RIPEMD256withRSA";
  102. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256.Id] = "RIPEMD256withRSA";
  103. algorithms["NONEWITHRSA"] = "RSA";
  104. algorithms["RSAWITHNONE"] = "RSA";
  105. algorithms["RAWRSA"] = "RSA";
  106. algorithms["RAWRSAPSS"] = "RAWRSASSA-PSS";
  107. algorithms["NONEWITHRSAPSS"] = "RAWRSASSA-PSS";
  108. algorithms["NONEWITHRSASSA-PSS"] = "RAWRSASSA-PSS";
  109. algorithms["NONEWITHDSA"] = "NONEwithDSA";
  110. algorithms["DSAWITHNONE"] = "NONEwithDSA";
  111. algorithms["RAWDSA"] = "NONEwithDSA";
  112. algorithms["DSA"] = "SHA-1withDSA";
  113. algorithms["DSAWITHSHA1"] = "SHA-1withDSA";
  114. algorithms["DSAWITHSHA-1"] = "SHA-1withDSA";
  115. algorithms["SHA/DSA"] = "SHA-1withDSA";
  116. algorithms["SHA1/DSA"] = "SHA-1withDSA";
  117. algorithms["SHA-1/DSA"] = "SHA-1withDSA";
  118. algorithms["SHA1WITHDSA"] = "SHA-1withDSA";
  119. algorithms["SHA-1WITHDSA"] = "SHA-1withDSA";
  120. algorithms[X9ObjectIdentifiers.IdDsaWithSha1.Id] = "SHA-1withDSA";
  121. algorithms[OiwObjectIdentifiers.DsaWithSha1.Id] = "SHA-1withDSA";
  122. algorithms["DSAWITHSHA224"] = "SHA-224withDSA";
  123. algorithms["DSAWITHSHA-224"] = "SHA-224withDSA";
  124. algorithms["SHA224/DSA"] = "SHA-224withDSA";
  125. algorithms["SHA-224/DSA"] = "SHA-224withDSA";
  126. algorithms["SHA224WITHDSA"] = "SHA-224withDSA";
  127. algorithms["SHA-224WITHDSA"] = "SHA-224withDSA";
  128. algorithms[NistObjectIdentifiers.DsaWithSha224.Id] = "SHA-224withDSA";
  129. algorithms["DSAWITHSHA256"] = "SHA-256withDSA";
  130. algorithms["DSAWITHSHA-256"] = "SHA-256withDSA";
  131. algorithms["SHA256/DSA"] = "SHA-256withDSA";
  132. algorithms["SHA-256/DSA"] = "SHA-256withDSA";
  133. algorithms["SHA256WITHDSA"] = "SHA-256withDSA";
  134. algorithms["SHA-256WITHDSA"] = "SHA-256withDSA";
  135. algorithms[NistObjectIdentifiers.DsaWithSha256.Id] = "SHA-256withDSA";
  136. algorithms["DSAWITHSHA384"] = "SHA-384withDSA";
  137. algorithms["DSAWITHSHA-384"] = "SHA-384withDSA";
  138. algorithms["SHA384/DSA"] = "SHA-384withDSA";
  139. algorithms["SHA-384/DSA"] = "SHA-384withDSA";
  140. algorithms["SHA384WITHDSA"] = "SHA-384withDSA";
  141. algorithms["SHA-384WITHDSA"] = "SHA-384withDSA";
  142. algorithms[NistObjectIdentifiers.DsaWithSha384.Id] = "SHA-384withDSA";
  143. algorithms["DSAWITHSHA512"] = "SHA-512withDSA";
  144. algorithms["DSAWITHSHA-512"] = "SHA-512withDSA";
  145. algorithms["SHA512/DSA"] = "SHA-512withDSA";
  146. algorithms["SHA-512/DSA"] = "SHA-512withDSA";
  147. algorithms["SHA512WITHDSA"] = "SHA-512withDSA";
  148. algorithms["SHA-512WITHDSA"] = "SHA-512withDSA";
  149. algorithms[NistObjectIdentifiers.DsaWithSha512.Id] = "SHA-512withDSA";
  150. algorithms["NONEWITHECDSA"] = "NONEwithECDSA";
  151. algorithms["ECDSAWITHNONE"] = "NONEwithECDSA";
  152. algorithms["ECDSA"] = "SHA-1withECDSA";
  153. algorithms["SHA1/ECDSA"] = "SHA-1withECDSA";
  154. algorithms["SHA-1/ECDSA"] = "SHA-1withECDSA";
  155. algorithms["ECDSAWITHSHA1"] = "SHA-1withECDSA";
  156. algorithms["ECDSAWITHSHA-1"] = "SHA-1withECDSA";
  157. algorithms["SHA1WITHECDSA"] = "SHA-1withECDSA";
  158. algorithms["SHA-1WITHECDSA"] = "SHA-1withECDSA";
  159. algorithms[X9ObjectIdentifiers.ECDsaWithSha1.Id] = "SHA-1withECDSA";
  160. algorithms[TeleTrusTObjectIdentifiers.ECSignWithSha1.Id] = "SHA-1withECDSA";
  161. algorithms["SHA224/ECDSA"] = "SHA-224withECDSA";
  162. algorithms["SHA-224/ECDSA"] = "SHA-224withECDSA";
  163. algorithms["ECDSAWITHSHA224"] = "SHA-224withECDSA";
  164. algorithms["ECDSAWITHSHA-224"] = "SHA-224withECDSA";
  165. algorithms["SHA224WITHECDSA"] = "SHA-224withECDSA";
  166. algorithms["SHA-224WITHECDSA"] = "SHA-224withECDSA";
  167. algorithms[X9ObjectIdentifiers.ECDsaWithSha224.Id] = "SHA-224withECDSA";
  168. algorithms["SHA256/ECDSA"] = "SHA-256withECDSA";
  169. algorithms["SHA-256/ECDSA"] = "SHA-256withECDSA";
  170. algorithms["ECDSAWITHSHA256"] = "SHA-256withECDSA";
  171. algorithms["ECDSAWITHSHA-256"] = "SHA-256withECDSA";
  172. algorithms["SHA256WITHECDSA"] = "SHA-256withECDSA";
  173. algorithms["SHA-256WITHECDSA"] = "SHA-256withECDSA";
  174. algorithms[X9ObjectIdentifiers.ECDsaWithSha256.Id] = "SHA-256withECDSA";
  175. algorithms["SHA384/ECDSA"] = "SHA-384withECDSA";
  176. algorithms["SHA-384/ECDSA"] = "SHA-384withECDSA";
  177. algorithms["ECDSAWITHSHA384"] = "SHA-384withECDSA";
  178. algorithms["ECDSAWITHSHA-384"] = "SHA-384withECDSA";
  179. algorithms["SHA384WITHECDSA"] = "SHA-384withECDSA";
  180. algorithms["SHA-384WITHECDSA"] = "SHA-384withECDSA";
  181. algorithms[X9ObjectIdentifiers.ECDsaWithSha384.Id] = "SHA-384withECDSA";
  182. algorithms["SHA512/ECDSA"] = "SHA-512withECDSA";
  183. algorithms["SHA-512/ECDSA"] = "SHA-512withECDSA";
  184. algorithms["ECDSAWITHSHA512"] = "SHA-512withECDSA";
  185. algorithms["ECDSAWITHSHA-512"] = "SHA-512withECDSA";
  186. algorithms["SHA512WITHECDSA"] = "SHA-512withECDSA";
  187. algorithms["SHA-512WITHECDSA"] = "SHA-512withECDSA";
  188. algorithms[X9ObjectIdentifiers.ECDsaWithSha512.Id] = "SHA-512withECDSA";
  189. algorithms["RIPEMD160/ECDSA"] = "RIPEMD160withECDSA";
  190. algorithms["ECDSAWITHRIPEMD160"] = "RIPEMD160withECDSA";
  191. algorithms["RIPEMD160WITHECDSA"] = "RIPEMD160withECDSA";
  192. algorithms[TeleTrusTObjectIdentifiers.ECSignWithRipeMD160.Id] = "RIPEMD160withECDSA";
  193. algorithms["NONEWITHCVC-ECDSA"] = "NONEwithCVC-ECDSA";
  194. algorithms["CVC-ECDSAWITHNONE"] = "NONEwithCVC-ECDSA";
  195. algorithms["SHA1/CVC-ECDSA"] = "SHA-1withCVC-ECDSA";
  196. algorithms["SHA-1/CVC-ECDSA"] = "SHA-1withCVC-ECDSA";
  197. algorithms["CVC-ECDSAWITHSHA1"] = "SHA-1withCVC-ECDSA";
  198. algorithms["CVC-ECDSAWITHSHA-1"] = "SHA-1withCVC-ECDSA";
  199. algorithms["SHA1WITHCVC-ECDSA"] = "SHA-1withCVC-ECDSA";
  200. algorithms["SHA-1WITHCVC-ECDSA"] = "SHA-1withCVC-ECDSA";
  201. algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_1.Id] = "SHA-1withCVC-ECDSA";
  202. algorithms["SHA224/CVC-ECDSA"] = "SHA-224withCVC-ECDSA";
  203. algorithms["SHA-224/CVC-ECDSA"] = "SHA-224withCVC-ECDSA";
  204. algorithms["CVC-ECDSAWITHSHA224"] = "SHA-224withCVC-ECDSA";
  205. algorithms["CVC-ECDSAWITHSHA-224"] = "SHA-224withCVC-ECDSA";
  206. algorithms["SHA224WITHCVC-ECDSA"] = "SHA-224withCVC-ECDSA";
  207. algorithms["SHA-224WITHCVC-ECDSA"] = "SHA-224withCVC-ECDSA";
  208. algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_224.Id] = "SHA-224withCVC-ECDSA";
  209. algorithms["SHA256/CVC-ECDSA"] = "SHA-256withCVC-ECDSA";
  210. algorithms["SHA-256/CVC-ECDSA"] = "SHA-256withCVC-ECDSA";
  211. algorithms["CVC-ECDSAWITHSHA256"] = "SHA-256withCVC-ECDSA";
  212. algorithms["CVC-ECDSAWITHSHA-256"] = "SHA-256withCVC-ECDSA";
  213. algorithms["SHA256WITHCVC-ECDSA"] = "SHA-256withCVC-ECDSA";
  214. algorithms["SHA-256WITHCVC-ECDSA"] = "SHA-256withCVC-ECDSA";
  215. algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_256.Id] = "SHA-256withCVC-ECDSA";
  216. algorithms["SHA384/CVC-ECDSA"] = "SHA-384withCVC-ECDSA";
  217. algorithms["SHA-384/CVC-ECDSA"] = "SHA-384withCVC-ECDSA";
  218. algorithms["CVC-ECDSAWITHSHA384"] = "SHA-384withCVC-ECDSA";
  219. algorithms["CVC-ECDSAWITHSHA-384"] = "SHA-384withCVC-ECDSA";
  220. algorithms["SHA384WITHCVC-ECDSA"] = "SHA-384withCVC-ECDSA";
  221. algorithms["SHA-384WITHCVC-ECDSA"] = "SHA-384withCVC-ECDSA";
  222. algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_384.Id] = "SHA-384withCVC-ECDSA";
  223. algorithms["SHA512/CVC-ECDSA"] = "SHA-512withCVC-ECDSA";
  224. algorithms["SHA-512/CVC-ECDSA"] = "SHA-512withCVC-ECDSA";
  225. algorithms["CVC-ECDSAWITHSHA512"] = "SHA-512withCVC-ECDSA";
  226. algorithms["CVC-ECDSAWITHSHA-512"] = "SHA-512withCVC-ECDSA";
  227. algorithms["SHA512WITHCVC-ECDSA"] = "SHA-512withCVC-ECDSA";
  228. algorithms["SHA-512WITHCVC-ECDSA"] = "SHA-512withCVC-ECDSA";
  229. algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_512.Id] = "SHA-512withCVC-ECDSA";
  230. algorithms["NONEWITHPLAIN-ECDSA"] = "NONEwithPLAIN-ECDSA";
  231. algorithms["PLAIN-ECDSAWITHNONE"] = "NONEwithPLAIN-ECDSA";
  232. algorithms["SHA1/PLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
  233. algorithms["SHA-1/PLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
  234. algorithms["PLAIN-ECDSAWITHSHA1"] = "SHA-1withPLAIN-ECDSA";
  235. algorithms["PLAIN-ECDSAWITHSHA-1"] = "SHA-1withPLAIN-ECDSA";
  236. algorithms["SHA1WITHPLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
  237. algorithms["SHA-1WITHPLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
  238. algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA1.Id] = "SHA-1withPLAIN-ECDSA";
  239. algorithms["SHA224/PLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
  240. algorithms["SHA-224/PLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
  241. algorithms["PLAIN-ECDSAWITHSHA224"] = "SHA-224withPLAIN-ECDSA";
  242. algorithms["PLAIN-ECDSAWITHSHA-224"] = "SHA-224withPLAIN-ECDSA";
  243. algorithms["SHA224WITHPLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
  244. algorithms["SHA-224WITHPLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
  245. algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA224.Id] = "SHA-224withPLAIN-ECDSA";
  246. algorithms["SHA256/PLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
  247. algorithms["SHA-256/PLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
  248. algorithms["PLAIN-ECDSAWITHSHA256"] = "SHA-256withPLAIN-ECDSA";
  249. algorithms["PLAIN-ECDSAWITHSHA-256"] = "SHA-256withPLAIN-ECDSA";
  250. algorithms["SHA256WITHPLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
  251. algorithms["SHA-256WITHPLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
  252. algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA256.Id] = "SHA-256withPLAIN-ECDSA";
  253. algorithms["SHA384/PLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
  254. algorithms["SHA-384/PLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
  255. algorithms["PLAIN-ECDSAWITHSHA384"] = "SHA-384withPLAIN-ECDSA";
  256. algorithms["PLAIN-ECDSAWITHSHA-384"] = "SHA-384withPLAIN-ECDSA";
  257. algorithms["SHA384WITHPLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
  258. algorithms["SHA-384WITHPLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
  259. algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA384.Id] = "SHA-384withPLAIN-ECDSA";
  260. algorithms["SHA512/PLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
  261. algorithms["SHA-512/PLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
  262. algorithms["PLAIN-ECDSAWITHSHA512"] = "SHA-512withPLAIN-ECDSA";
  263. algorithms["PLAIN-ECDSAWITHSHA-512"] = "SHA-512withPLAIN-ECDSA";
  264. algorithms["SHA512WITHPLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
  265. algorithms["SHA-512WITHPLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
  266. algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA512.Id] = "SHA-512withPLAIN-ECDSA";
  267. algorithms["RIPEMD160/PLAIN-ECDSA"] = "RIPEMD160withPLAIN-ECDSA";
  268. algorithms["PLAIN-ECDSAWITHRIPEMD160"] = "RIPEMD160withPLAIN-ECDSA";
  269. algorithms["RIPEMD160WITHPLAIN-ECDSA"] = "RIPEMD160withPLAIN-ECDSA";
  270. algorithms[BsiObjectIdentifiers.ecdsa_plain_RIPEMD160.Id] = "RIPEMD160withPLAIN-ECDSA";
  271. algorithms["SHA1WITHECNR"] = "SHA-1withECNR";
  272. algorithms["SHA-1WITHECNR"] = "SHA-1withECNR";
  273. algorithms["SHA224WITHECNR"] = "SHA-224withECNR";
  274. algorithms["SHA-224WITHECNR"] = "SHA-224withECNR";
  275. algorithms["SHA256WITHECNR"] = "SHA-256withECNR";
  276. algorithms["SHA-256WITHECNR"] = "SHA-256withECNR";
  277. algorithms["SHA384WITHECNR"] = "SHA-384withECNR";
  278. algorithms["SHA-384WITHECNR"] = "SHA-384withECNR";
  279. algorithms["SHA512WITHECNR"] = "SHA-512withECNR";
  280. algorithms["SHA-512WITHECNR"] = "SHA-512withECNR";
  281. algorithms["GOST-3410"] = "GOST3410";
  282. algorithms["GOST-3410-94"] = "GOST3410";
  283. algorithms["GOST3411WITHGOST3410"] = "GOST3410";
  284. algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94.Id] = "GOST3410";
  285. algorithms["ECGOST-3410"] = "ECGOST3410";
  286. algorithms["ECGOST-3410-2001"] = "ECGOST3410";
  287. algorithms["GOST3411WITHECGOST3410"] = "ECGOST3410";
  288. algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001.Id] = "ECGOST3410";
  289. algorithms["ED25519"] = "Ed25519";
  290. algorithms[EdECObjectIdentifiers.id_Ed25519.Id] = "Ed25519";
  291. algorithms["ED25519CTX"] = "Ed25519ctx";
  292. algorithms["ED25519PH"] = "Ed25519ph";
  293. algorithms["ED448"] = "Ed448";
  294. algorithms[EdECObjectIdentifiers.id_Ed448.Id] = "Ed448";
  295. algorithms["ED448PH"] = "Ed448ph";
  296. oids["MD2withRSA"] = PkcsObjectIdentifiers.MD2WithRsaEncryption;
  297. oids["MD4withRSA"] = PkcsObjectIdentifiers.MD4WithRsaEncryption;
  298. oids["MD5withRSA"] = PkcsObjectIdentifiers.MD5WithRsaEncryption;
  299. oids["SHA-1withRSA"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  300. oids["SHA-224withRSA"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  301. oids["SHA-256withRSA"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  302. oids["SHA-384withRSA"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  303. oids["SHA-512withRSA"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  304. oids["PSSwithRSA"] = PkcsObjectIdentifiers.IdRsassaPss;
  305. oids["SHA-1withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  306. oids["SHA-224withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  307. oids["SHA-256withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  308. oids["SHA-384withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  309. oids["SHA-512withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  310. oids["RIPEMD128withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128;
  311. oids["RIPEMD160withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160;
  312. oids["RIPEMD256withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256;
  313. oids["SHA-1withDSA"] = X9ObjectIdentifiers.IdDsaWithSha1;
  314. oids["SHA-1withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha1;
  315. oids["SHA-224withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha224;
  316. oids["SHA-256withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha256;
  317. oids["SHA-384withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha384;
  318. oids["SHA-512withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha512;
  319. oids["RIPEMD160withECDSA"] = TeleTrusTObjectIdentifiers.ECSignWithRipeMD160;
  320. oids["SHA-1withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_1;
  321. oids["SHA-224withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_224;
  322. oids["SHA-256withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_256;
  323. oids["SHA-384withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_384;
  324. oids["SHA-512withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_512;
  325. oids["SHA-1withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA1;
  326. oids["SHA-224withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA224;
  327. oids["SHA-256withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA256;
  328. oids["SHA-384withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA384;
  329. oids["SHA-512withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA512;
  330. oids["RIPEMD160withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_RIPEMD160;
  331. oids["GOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94;
  332. oids["ECGOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
  333. oids["Ed25519"] = EdECObjectIdentifiers.id_Ed25519;
  334. oids["Ed448"] = EdECObjectIdentifiers.id_Ed448;
  335. }
  336. /// <summary>
  337. /// Returns an ObjectIdentifier for a given encoding.
  338. /// </summary>
  339. /// <param name="mechanism">A string representation of the encoding.</param>
  340. /// <returns>A DerObjectIdentifier, null if the OID is not available.</returns>
  341. // TODO Don't really want to support this
  342. public static DerObjectIdentifier GetObjectIdentifier(
  343. string mechanism)
  344. {
  345. if (mechanism == null)
  346. throw new ArgumentNullException("mechanism");
  347. mechanism = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(mechanism);
  348. string aliased = (string) algorithms[mechanism];
  349. if (aliased != null)
  350. mechanism = aliased;
  351. return (DerObjectIdentifier) oids[mechanism];
  352. }
  353. public static ICollection Algorithms
  354. {
  355. get { return oids.Keys; }
  356. }
  357. public static Asn1Encodable GetDefaultX509Parameters(
  358. DerObjectIdentifier id)
  359. {
  360. return GetDefaultX509Parameters(id.Id);
  361. }
  362. public static Asn1Encodable GetDefaultX509Parameters(
  363. string algorithm)
  364. {
  365. if (algorithm == null)
  366. throw new ArgumentNullException("algorithm");
  367. algorithm = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  368. string mechanism = (string) algorithms[algorithm];
  369. if (mechanism == null)
  370. mechanism = algorithm;
  371. if (mechanism == "PSSwithRSA")
  372. {
  373. // TODO The Sha1Digest here is a default. In JCE version, the actual digest
  374. // to be used can be overridden by subsequent parameter settings.
  375. return GetPssX509Parameters("SHA-1");
  376. }
  377. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withRSAandMGF1"))
  378. {
  379. string digestName = mechanism.Substring(0, mechanism.Length - "withRSAandMGF1".Length);
  380. return GetPssX509Parameters(digestName);
  381. }
  382. return DerNull.Instance;
  383. }
  384. private static Asn1Encodable GetPssX509Parameters(
  385. string digestName)
  386. {
  387. AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
  388. DigestUtilities.GetObjectIdentifier(digestName), DerNull.Instance);
  389. // TODO Is it possible for the MGF hash alg to be different from the PSS one?
  390. AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
  391. PkcsObjectIdentifiers.IdMgf1, hashAlgorithm);
  392. int saltLen = DigestUtilities.GetDigest(digestName).GetDigestSize();
  393. return new RsassaPssParameters(hashAlgorithm, maskGenAlgorithm,
  394. new DerInteger(saltLen), new DerInteger(1));
  395. }
  396. public static ISigner GetSigner(
  397. DerObjectIdentifier id)
  398. {
  399. return GetSigner(id.Id);
  400. }
  401. public static ISigner GetSigner(
  402. string algorithm)
  403. {
  404. if (algorithm == null)
  405. throw new ArgumentNullException("algorithm");
  406. algorithm = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  407. string mechanism = (string) algorithms[algorithm];
  408. if (mechanism == null)
  409. mechanism = algorithm;
  410. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "Ed"))
  411. {
  412. if (mechanism.Equals("Ed25519"))
  413. {
  414. return new Ed25519Signer();
  415. }
  416. if (mechanism.Equals("Ed25519ctx"))
  417. {
  418. return new Ed25519ctxSigner(Arrays.EmptyBytes);
  419. }
  420. if (mechanism.Equals("Ed25519ph"))
  421. {
  422. return new Ed25519phSigner(Arrays.EmptyBytes);
  423. }
  424. if (mechanism.Equals("Ed448"))
  425. {
  426. return new Ed448Signer(Arrays.EmptyBytes);
  427. }
  428. if (mechanism.Equals("Ed448ph"))
  429. {
  430. return new Ed448phSigner(Arrays.EmptyBytes);
  431. }
  432. }
  433. if (mechanism.Equals("RSA"))
  434. {
  435. return (new RsaDigestSigner(new NullDigest(), (AlgorithmIdentifier)null));
  436. }
  437. if (mechanism.Equals("RAWRSASSA-PSS"))
  438. {
  439. // TODO Add support for other parameter settings
  440. return PssSigner.CreateRawSigner(new RsaBlindedEngine(), new Sha1Digest());
  441. }
  442. if (mechanism.Equals("PSSwithRSA"))
  443. {
  444. // TODO The Sha1Digest here is a default. In JCE version, the actual digest
  445. // to be used can be overridden by subsequent parameter settings.
  446. return (new PssSigner(new RsaBlindedEngine(), new Sha1Digest()));
  447. }
  448. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withRSA"))
  449. {
  450. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  451. IDigest digest = DigestUtilities.GetDigest(digestName);
  452. return new RsaDigestSigner(digest);
  453. }
  454. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withRSAandMGF1"))
  455. {
  456. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  457. IDigest digest = DigestUtilities.GetDigest(digestName);
  458. return new PssSigner(new RsaBlindedEngine(), digest);
  459. }
  460. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withDSA"))
  461. {
  462. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  463. IDigest digest = DigestUtilities.GetDigest(digestName);
  464. return new DsaDigestSigner(new DsaSigner(), digest);
  465. }
  466. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withECDSA"))
  467. {
  468. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  469. IDigest digest = DigestUtilities.GetDigest(digestName);
  470. return new DsaDigestSigner(new ECDsaSigner(), digest);
  471. }
  472. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withCVC-ECDSA")
  473. || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withPLAIN-ECDSA"))
  474. {
  475. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  476. IDigest digest = DigestUtilities.GetDigest(digestName);
  477. return new DsaDigestSigner(new ECDsaSigner(), digest, PlainDsaEncoding.Instance);
  478. }
  479. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withECNR"))
  480. {
  481. string digestName = mechanism.Substring(0, mechanism.LastIndexOf("with"));
  482. IDigest digest = DigestUtilities.GetDigest(digestName);
  483. return new DsaDigestSigner(new ECNRSigner(), digest);
  484. }
  485. if (mechanism.Equals("GOST3410"))
  486. {
  487. return new Gost3410DigestSigner(new Gost3410Signer(), new Gost3411Digest());
  488. }
  489. if (mechanism.Equals("ECGOST3410"))
  490. {
  491. return new Gost3410DigestSigner(new ECGost3410Signer(), new Gost3411Digest());
  492. }
  493. if (mechanism.Equals("SHA1WITHRSA/ISO9796-2"))
  494. {
  495. return new Iso9796d2Signer(new RsaBlindedEngine(), new Sha1Digest(), true);
  496. }
  497. if (mechanism.Equals("MD5WITHRSA/ISO9796-2"))
  498. {
  499. return new Iso9796d2Signer(new RsaBlindedEngine(), new MD5Digest(), true);
  500. }
  501. if (mechanism.Equals("RIPEMD160WITHRSA/ISO9796-2"))
  502. {
  503. return new Iso9796d2Signer(new RsaBlindedEngine(), new RipeMD160Digest(), true);
  504. }
  505. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "/X9.31"))
  506. {
  507. string x931 = mechanism.Substring(0, mechanism.Length - "/X9.31".Length);
  508. int withPos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.IndexOf(x931, "WITH");
  509. if (withPos > 0)
  510. {
  511. int endPos = withPos + "WITH".Length;
  512. string digestName = x931.Substring(0, withPos);
  513. IDigest digest = DigestUtilities.GetDigest(digestName);
  514. string cipherName = x931.Substring(endPos, x931.Length - endPos);
  515. if (cipherName.Equals("RSA"))
  516. {
  517. IAsymmetricBlockCipher cipher = new RsaBlindedEngine();
  518. return new X931Signer(cipher, digest);
  519. }
  520. }
  521. }
  522. throw new SecurityUtilityException("Signer " + algorithm + " not recognised.");
  523. }
  524. public static string GetEncodingName(
  525. DerObjectIdentifier oid)
  526. {
  527. return (string) algorithms[oid.Id];
  528. }
  529. public static ISigner InitSigner(DerObjectIdentifier algorithmOid, bool forSigning, AsymmetricKeyParameter privateKey, SecureRandom random)
  530. {
  531. return InitSigner(algorithmOid.Id, forSigning, privateKey, random);
  532. }
  533. public static ISigner InitSigner(string algorithm, bool forSigning, AsymmetricKeyParameter privateKey, SecureRandom random)
  534. {
  535. ISigner signer = SignerUtilities.GetSigner(algorithm);
  536. signer.Init(forSigning, ParameterUtilities.WithRandom(privateKey, random));
  537. return signer;
  538. }
  539. }
  540. }
  541. #pragma warning restore
  542. #endif