PublicKeyFactory.cs 11 KB

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