PublicKeyFactory.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cryptlib;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Gnu;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  16. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  17. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  18. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  19. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  20. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  21. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Security
  22. {
  23. public static class PublicKeyFactory
  24. {
  25. public static AsymmetricKeyParameter CreateKey(
  26. byte[] keyInfoData)
  27. {
  28. return CreateKey(
  29. SubjectPublicKeyInfo.GetInstance(
  30. Asn1Object.FromByteArray(keyInfoData)));
  31. }
  32. public static AsymmetricKeyParameter CreateKey(
  33. Stream inStr)
  34. {
  35. return CreateKey(
  36. SubjectPublicKeyInfo.GetInstance(
  37. Asn1Object.FromStream(inStr)));
  38. }
  39. public static AsymmetricKeyParameter CreateKey(
  40. SubjectPublicKeyInfo keyInfo)
  41. {
  42. AlgorithmIdentifier algID = keyInfo.AlgorithmID;
  43. DerObjectIdentifier algOid = algID.Algorithm;
  44. // TODO See RSAUtil.isRsaOid in Java build
  45. if (algOid.Equals(PkcsObjectIdentifiers.RsaEncryption)
  46. || algOid.Equals(X509ObjectIdentifiers.IdEARsa)
  47. || algOid.Equals(PkcsObjectIdentifiers.IdRsassaPss)
  48. || algOid.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
  49. {
  50. RsaPublicKeyStructure pubKey = RsaPublicKeyStructure.GetInstance(
  51. keyInfo.ParsePublicKey());
  52. return new RsaKeyParameters(false, pubKey.Modulus, pubKey.PublicExponent);
  53. }
  54. else if (algOid.Equals(X9ObjectIdentifiers.DHPublicNumber))
  55. {
  56. Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());
  57. DHPublicKey dhPublicKey = DHPublicKey.GetInstance(keyInfo.ParsePublicKey());
  58. BigInteger y = dhPublicKey.Y.Value;
  59. if (IsPkcsDHParam(seq))
  60. return ReadPkcsDHParam(algOid, y, seq);
  61. DHDomainParameters dhParams = DHDomainParameters.GetInstance(seq);
  62. BigInteger p = dhParams.P.Value;
  63. BigInteger g = dhParams.G.Value;
  64. BigInteger q = dhParams.Q.Value;
  65. BigInteger j = null;
  66. if (dhParams.J != null)
  67. {
  68. j = dhParams.J.Value;
  69. }
  70. DHValidationParameters validation = null;
  71. DHValidationParms dhValidationParms = dhParams.ValidationParms;
  72. if (dhValidationParms != null)
  73. {
  74. byte[] seed = dhValidationParms.Seed.GetBytes();
  75. BigInteger pgenCounter = dhValidationParms.PgenCounter.Value;
  76. // TODO Check pgenCounter size?
  77. validation = new DHValidationParameters(seed, pgenCounter.IntValue);
  78. }
  79. return new DHPublicKeyParameters(y, new DHParameters(p, g, q, j, validation));
  80. }
  81. else if (algOid.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
  82. {
  83. Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());
  84. DerInteger derY = (DerInteger)keyInfo.ParsePublicKey();
  85. return ReadPkcsDHParam(algOid, derY.Value, seq);
  86. }
  87. else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
  88. {
  89. ElGamalParameter para = new ElGamalParameter(
  90. Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
  91. DerInteger derY = (DerInteger)keyInfo.ParsePublicKey();
  92. return new ElGamalPublicKeyParameters(
  93. derY.Value,
  94. new ElGamalParameters(para.P, para.G));
  95. }
  96. else if (algOid.Equals(X9ObjectIdentifiers.IdDsa)
  97. || algOid.Equals(OiwObjectIdentifiers.DsaWithSha1))
  98. {
  99. DerInteger derY = (DerInteger)keyInfo.ParsePublicKey();
  100. Asn1Encodable ae = algID.Parameters;
  101. DsaParameters parameters = null;
  102. if (ae != null)
  103. {
  104. DsaParameter para = DsaParameter.GetInstance(ae.ToAsn1Object());
  105. parameters = new DsaParameters(para.P, para.Q, para.G);
  106. }
  107. return new DsaPublicKeyParameters(derY.Value, parameters);
  108. }
  109. else if (algOid.Equals(X9ObjectIdentifiers.IdECPublicKey))
  110. {
  111. X962Parameters para = X962Parameters.GetInstance(algID.Parameters.ToAsn1Object());
  112. X9ECParameters x9;
  113. if (para.IsNamedCurve)
  114. {
  115. x9 = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)para.Parameters);
  116. }
  117. else
  118. {
  119. x9 = new X9ECParameters((Asn1Sequence)para.Parameters);
  120. }
  121. Asn1OctetString key = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
  122. X9ECPoint derQ = new X9ECPoint(x9.Curve, key);
  123. ECPoint q = derQ.Point;
  124. if (para.IsNamedCurve)
  125. {
  126. return new ECPublicKeyParameters("EC", q, (DerObjectIdentifier)para.Parameters);
  127. }
  128. ECDomainParameters dParams = new ECDomainParameters(x9);
  129. return new ECPublicKeyParameters(q, dParams);
  130. }
  131. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
  132. {
  133. Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
  134. DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet;
  135. X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
  136. if (ecP == null)
  137. return null;
  138. Asn1OctetString key;
  139. try
  140. {
  141. key = (Asn1OctetString)keyInfo.ParsePublicKey();
  142. }
  143. catch (IOException e)
  144. {
  145. throw new ArgumentException("error recovering GOST3410_2001 public key", e);
  146. }
  147. int fieldSize = 32;
  148. int keySize = 2 * fieldSize;
  149. byte[] keyEnc = key.GetOctets();
  150. if (keyEnc.Length != keySize)
  151. throw new ArgumentException("invalid length for GOST3410_2001 public key");
  152. byte[] x9Encoding = new byte[1 + keySize];
  153. x9Encoding[0] = 0x04;
  154. for (int i = 1; i <= fieldSize; ++i)
  155. {
  156. x9Encoding[i] = keyEnc[fieldSize - i];
  157. x9Encoding[i + fieldSize] = keyEnc[keySize - i];
  158. }
  159. ECPoint q = ecP.Curve.DecodePoint(x9Encoding);
  160. return new ECPublicKeyParameters("ECGOST3410", q, publicKeyParamSet);
  161. }
  162. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
  163. {
  164. Gost3410PublicKeyAlgParameters algParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
  165. Asn1OctetString key;
  166. try
  167. {
  168. key = (Asn1OctetString)keyInfo.ParsePublicKey();
  169. }
  170. catch (IOException e)
  171. {
  172. throw new ArgumentException("error recovering GOST3410_94 public key", e);
  173. }
  174. byte[] keyBytes = Arrays.Reverse(key.GetOctets()); // was little endian
  175. BigInteger y = new BigInteger(1, keyBytes);
  176. return new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet);
  177. }
  178. else if (algOid.Equals(EdECObjectIdentifiers.id_X25519)
  179. || algOid.Equals(CryptlibObjectIdentifiers.curvey25519))
  180. {
  181. return new X25519PublicKeyParameters(GetRawKey(keyInfo));
  182. }
  183. else if (algOid.Equals(EdECObjectIdentifiers.id_X448))
  184. {
  185. return new X448PublicKeyParameters(GetRawKey(keyInfo));
  186. }
  187. else if (algOid.Equals(EdECObjectIdentifiers.id_Ed25519)
  188. || algOid.Equals(GnuObjectIdentifiers.Ed25519))
  189. {
  190. return new Ed25519PublicKeyParameters(GetRawKey(keyInfo));
  191. }
  192. else if (algOid.Equals(EdECObjectIdentifiers.id_Ed448))
  193. {
  194. return new Ed448PublicKeyParameters(GetRawKey(keyInfo));
  195. }
  196. else if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256)
  197. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)
  198. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256)
  199. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512))
  200. {
  201. Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
  202. DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet;
  203. ECGost3410Parameters ecDomainParameters =new ECGost3410Parameters(
  204. new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOid(publicKeyParamSet)),
  205. publicKeyParamSet,
  206. gostParams.DigestParamSet,
  207. gostParams.EncryptionParamSet);
  208. Asn1OctetString key;
  209. try
  210. {
  211. key = (Asn1OctetString)keyInfo.ParsePublicKey();
  212. }
  213. catch (IOException e)
  214. {
  215. throw new ArgumentException("error recovering GOST3410_2012 public key", e);
  216. }
  217. int fieldSize = 32;
  218. if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
  219. {
  220. fieldSize = 64;
  221. }
  222. int keySize = 2 * fieldSize;
  223. byte[] keyEnc = key.GetOctets();
  224. if (keyEnc.Length != keySize)
  225. throw new ArgumentException("invalid length for GOST3410_2012 public key");
  226. byte[] x9Encoding = new byte[1 + keySize];
  227. x9Encoding[0] = 0x04;
  228. for (int i = 1; i <= fieldSize; ++i)
  229. {
  230. x9Encoding[i] = keyEnc[fieldSize - i];
  231. x9Encoding[i + fieldSize] = keyEnc[keySize - i];
  232. }
  233. ECPoint q = ecDomainParameters.Curve.DecodePoint(x9Encoding);
  234. return new ECPublicKeyParameters(q, ecDomainParameters);
  235. }
  236. else
  237. {
  238. throw new SecurityUtilityException("algorithm identifier in public key not recognised: " + algOid);
  239. }
  240. }
  241. private static byte[] GetRawKey(SubjectPublicKeyInfo keyInfo)
  242. {
  243. /*
  244. * TODO[RFC 8422]
  245. * - Require keyInfo.Algorithm.Parameters == null?
  246. */
  247. return keyInfo.PublicKeyData.GetOctets();
  248. }
  249. private static bool IsPkcsDHParam(Asn1Sequence seq)
  250. {
  251. if (seq.Count == 2)
  252. return true;
  253. if (seq.Count > 3)
  254. return false;
  255. DerInteger l = DerInteger.GetInstance(seq[2]);
  256. DerInteger p = DerInteger.GetInstance(seq[0]);
  257. return l.Value.CompareTo(BigInteger.ValueOf(p.Value.BitLength)) <= 0;
  258. }
  259. private static DHPublicKeyParameters ReadPkcsDHParam(DerObjectIdentifier algOid,
  260. BigInteger y, Asn1Sequence seq)
  261. {
  262. DHParameter para = new DHParameter(seq);
  263. BigInteger lVal = para.L;
  264. int l = lVal == null ? 0 : lVal.IntValue;
  265. DHParameters dhParams = new DHParameters(para.P, para.G, null, l);
  266. return new DHPublicKeyParameters(y, dhParams, algOid);
  267. }
  268. }
  269. }
  270. #pragma warning restore
  271. #endif