PublicKeyFactory.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Text;
  6. using Org.BouncyCastle.Asn1;
  7. using Org.BouncyCastle.Asn1.CryptoPro;
  8. using Org.BouncyCastle.Asn1.Oiw;
  9. using Org.BouncyCastle.Asn1.Pkcs;
  10. using Org.BouncyCastle.Asn1.Sec;
  11. using Org.BouncyCastle.Asn1.X509;
  12. using Org.BouncyCastle.Asn1.X9;
  13. using Org.BouncyCastle.Crypto;
  14. using Org.BouncyCastle.Crypto.Generators;
  15. using Org.BouncyCastle.Crypto.Parameters;
  16. using Org.BouncyCastle.Math;
  17. using Org.BouncyCastle.Math.EC;
  18. namespace Org.BouncyCastle.Security
  19. {
  20. public sealed class PublicKeyFactory
  21. {
  22. private PublicKeyFactory()
  23. {
  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.GetPublicKey());
  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.GetPublicKey());
  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.GetPublicKey();
  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.GetPublicKey();
  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.GetPublicKey();
  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 = new X962Parameters(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.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
  129. return new ECPublicKeyParameters(q, dParams);
  130. }
  131. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
  132. {
  133. Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
  134. (Asn1Sequence) algID.Parameters);
  135. Asn1OctetString key;
  136. try
  137. {
  138. key = (Asn1OctetString) keyInfo.GetPublicKey();
  139. }
  140. catch (IOException)
  141. {
  142. throw new ArgumentException("invalid info structure in GOST3410 public key");
  143. }
  144. byte[] keyEnc = key.GetOctets();
  145. byte[] x = new byte[32];
  146. byte[] y = new byte[32];
  147. for (int i = 0; i != y.Length; i++)
  148. {
  149. x[i] = keyEnc[32 - 1 - i];
  150. }
  151. for (int i = 0; i != x.Length; i++)
  152. {
  153. y[i] = keyEnc[64 - 1 - i];
  154. }
  155. ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);
  156. if (ecP == null)
  157. return null;
  158. ECPoint q = ecP.Curve.CreatePoint(new BigInteger(1, x), new BigInteger(1, y));
  159. return new ECPublicKeyParameters("ECGOST3410", q, gostParams.PublicKeyParamSet);
  160. }
  161. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
  162. {
  163. Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
  164. (Asn1Sequence) algID.Parameters);
  165. DerOctetString derY;
  166. try
  167. {
  168. derY = (DerOctetString) keyInfo.GetPublicKey();
  169. }
  170. catch (IOException)
  171. {
  172. throw new ArgumentException("invalid info structure in GOST3410 public key");
  173. }
  174. byte[] keyEnc = derY.GetOctets();
  175. byte[] keyBytes = new byte[keyEnc.Length];
  176. for (int i = 0; i != keyEnc.Length; i++)
  177. {
  178. keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // was little endian
  179. }
  180. BigInteger y = new BigInteger(1, keyBytes);
  181. return new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet);
  182. }
  183. else
  184. {
  185. throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algOid);
  186. }
  187. }
  188. private static bool IsPkcsDHParam(Asn1Sequence seq)
  189. {
  190. if (seq.Count == 2)
  191. return true;
  192. if (seq.Count > 3)
  193. return false;
  194. DerInteger l = DerInteger.GetInstance(seq[2]);
  195. DerInteger p = DerInteger.GetInstance(seq[0]);
  196. return l.Value.CompareTo(BigInteger.ValueOf(p.Value.BitLength)) <= 0;
  197. }
  198. private static DHPublicKeyParameters ReadPkcsDHParam(DerObjectIdentifier algOid,
  199. BigInteger y, Asn1Sequence seq)
  200. {
  201. DHParameter para = new DHParameter(seq);
  202. BigInteger lVal = para.L;
  203. int l = lVal == null ? 0 : lVal.IntValue;
  204. DHParameters dhParams = new DHParameters(para.P, para.G, null, l);
  205. return new DHPublicKeyParameters(y, dhParams, algOid);
  206. }
  207. }
  208. }
  209. #endif