TlsDHUtilities.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Crypto.Agreement;
  6. using Org.BouncyCastle.Crypto.Generators;
  7. using Org.BouncyCastle.Crypto.Parameters;
  8. using Org.BouncyCastle.Math;
  9. using Org.BouncyCastle.Security;
  10. using Org.BouncyCastle.Utilities;
  11. using Org.BouncyCastle.Utilities.Encoders;
  12. namespace Org.BouncyCastle.Crypto.Tls
  13. {
  14. public abstract class TlsDHUtilities
  15. {
  16. internal static readonly BigInteger Two = BigInteger.Two;
  17. /*
  18. * TODO[draft-ietf-tls-negotiated-ff-dhe-01] Move these groups to DHStandardGroups once reaches RFC
  19. */
  20. private static BigInteger FromHex(String hex)
  21. {
  22. return new BigInteger(1, Hex.Decode(hex));
  23. }
  24. private static DHParameters FromSafeP(String hexP)
  25. {
  26. BigInteger p = FromHex(hexP), q = p.ShiftRight(1);
  27. return new DHParameters(p, Two, q);
  28. }
  29. private static readonly string draft_ffdhe2432_p =
  30. "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1"
  31. + "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9"
  32. + "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561"
  33. + "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935"
  34. + "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735"
  35. + "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB"
  36. + "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19"
  37. + "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61"
  38. + "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73"
  39. + "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA"
  40. + "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238"
  41. + "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C"
  42. + "AEFE13098533C8B3FFFFFFFFFFFFFFFF";
  43. internal static readonly DHParameters draft_ffdhe2432 = FromSafeP(draft_ffdhe2432_p);
  44. private static readonly string draft_ffdhe3072_p =
  45. "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1"
  46. + "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9"
  47. + "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561"
  48. + "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935"
  49. + "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735"
  50. + "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB"
  51. + "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19"
  52. + "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61"
  53. + "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73"
  54. + "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA"
  55. + "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238"
  56. + "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C"
  57. + "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3"
  58. + "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D"
  59. + "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF"
  60. + "3C1B20EE3FD59D7C25E41D2B66C62E37FFFFFFFFFFFFFFFF";
  61. internal static readonly DHParameters draft_ffdhe3072 = FromSafeP(draft_ffdhe3072_p);
  62. private static readonly string draft_ffdhe4096_p =
  63. "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1"
  64. + "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9"
  65. + "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561"
  66. + "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935"
  67. + "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735"
  68. + "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB"
  69. + "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19"
  70. + "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61"
  71. + "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73"
  72. + "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA"
  73. + "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238"
  74. + "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C"
  75. + "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3"
  76. + "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D"
  77. + "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF"
  78. + "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB"
  79. + "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004"
  80. + "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832"
  81. + "A907600A918130C46DC778F971AD0038092999A333CB8B7A"
  82. + "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF"
  83. + "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E655F6A"
  84. + "FFFFFFFFFFFFFFFF";
  85. internal static readonly DHParameters draft_ffdhe4096 = FromSafeP(draft_ffdhe4096_p);
  86. private static readonly string draft_ffdhe6144_p =
  87. "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1"
  88. + "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9"
  89. + "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561"
  90. + "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935"
  91. + "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735"
  92. + "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB"
  93. + "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19"
  94. + "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61"
  95. + "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73"
  96. + "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA"
  97. + "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238"
  98. + "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C"
  99. + "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3"
  100. + "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D"
  101. + "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF"
  102. + "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB"
  103. + "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004"
  104. + "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832"
  105. + "A907600A918130C46DC778F971AD0038092999A333CB8B7A"
  106. + "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF"
  107. + "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E0DD902"
  108. + "0BFD64B645036C7A4E677D2C38532A3A23BA4442CAF53EA6"
  109. + "3BB454329B7624C8917BDD64B1C0FD4CB38E8C334C701C3A"
  110. + "CDAD0657FCCFEC719B1F5C3E4E46041F388147FB4CFDB477"
  111. + "A52471F7A9A96910B855322EDB6340D8A00EF092350511E3"
  112. + "0ABEC1FFF9E3A26E7FB29F8C183023C3587E38DA0077D9B4"
  113. + "763E4E4B94B2BBC194C6651E77CAF992EEAAC0232A281BF6"
  114. + "B3A739C1226116820AE8DB5847A67CBEF9C9091B462D538C"
  115. + "D72B03746AE77F5E62292C311562A846505DC82DB854338A"
  116. + "E49F5235C95B91178CCF2DD5CACEF403EC9D1810C6272B04"
  117. + "5B3B71F9DC6B80D63FDD4A8E9ADB1E6962A69526D43161C1"
  118. + "A41D570D7938DAD4A40E329CD0E40E65FFFFFFFFFFFFFFFF";
  119. internal static readonly DHParameters draft_ffdhe6144 = FromSafeP(draft_ffdhe6144_p);
  120. private static readonly string draft_ffdhe8192_p =
  121. "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1"
  122. + "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9"
  123. + "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561"
  124. + "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935"
  125. + "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735"
  126. + "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB"
  127. + "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19"
  128. + "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61"
  129. + "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73"
  130. + "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA"
  131. + "886B4238611FCFDCDE355B3B6519035BBC34F4DEF99C0238"
  132. + "61B46FC9D6E6C9077AD91D2691F7F7EE598CB0FAC186D91C"
  133. + "AEFE130985139270B4130C93BC437944F4FD4452E2D74DD3"
  134. + "64F2E21E71F54BFF5CAE82AB9C9DF69EE86D2BC522363A0D"
  135. + "ABC521979B0DEADA1DBF9A42D5C4484E0ABCD06BFA53DDEF"
  136. + "3C1B20EE3FD59D7C25E41D2B669E1EF16E6F52C3164DF4FB"
  137. + "7930E9E4E58857B6AC7D5F42D69F6D187763CF1D55034004"
  138. + "87F55BA57E31CC7A7135C886EFB4318AED6A1E012D9E6832"
  139. + "A907600A918130C46DC778F971AD0038092999A333CB8B7A"
  140. + "1A1DB93D7140003C2A4ECEA9F98D0ACC0A8291CDCEC97DCF"
  141. + "8EC9B55A7F88A46B4DB5A851F44182E1C68A007E5E0DD902"
  142. + "0BFD64B645036C7A4E677D2C38532A3A23BA4442CAF53EA6"
  143. + "3BB454329B7624C8917BDD64B1C0FD4CB38E8C334C701C3A"
  144. + "CDAD0657FCCFEC719B1F5C3E4E46041F388147FB4CFDB477"
  145. + "A52471F7A9A96910B855322EDB6340D8A00EF092350511E3"
  146. + "0ABEC1FFF9E3A26E7FB29F8C183023C3587E38DA0077D9B4"
  147. + "763E4E4B94B2BBC194C6651E77CAF992EEAAC0232A281BF6"
  148. + "B3A739C1226116820AE8DB5847A67CBEF9C9091B462D538C"
  149. + "D72B03746AE77F5E62292C311562A846505DC82DB854338A"
  150. + "E49F5235C95B91178CCF2DD5CACEF403EC9D1810C6272B04"
  151. + "5B3B71F9DC6B80D63FDD4A8E9ADB1E6962A69526D43161C1"
  152. + "A41D570D7938DAD4A40E329CCFF46AAA36AD004CF600C838"
  153. + "1E425A31D951AE64FDB23FCEC9509D43687FEB69EDD1CC5E"
  154. + "0B8CC3BDF64B10EF86B63142A3AB8829555B2F747C932665"
  155. + "CB2C0F1CC01BD70229388839D2AF05E454504AC78B758282"
  156. + "2846C0BA35C35F5C59160CC046FD8251541FC68C9C86B022"
  157. + "BB7099876A460E7451A8A93109703FEE1C217E6C3826E52C"
  158. + "51AA691E0E423CFC99E9E31650C1217B624816CDAD9A95F9"
  159. + "D5B8019488D9C0A0A1FE3075A577E23183F81D4A3F2FA457"
  160. + "1EFC8CE0BA8A4FE8B6855DFE72B0A66EDED2FBABFBE58A30"
  161. + "FAFABE1C5D71A87E2F741EF8C1FE86FEA6BBFDE530677F0D"
  162. + "97D11D49F7A8443D0822E506A9F4614E011E2A94838FF88C"
  163. + "D68C8BB7C5C6424CFFFFFFFFFFFFFFFF";
  164. internal static readonly DHParameters draft_ffdhe8192 = FromSafeP(draft_ffdhe8192_p);
  165. public static void AddNegotiatedDheGroupsClientExtension(IDictionary extensions, byte[] dheGroups)
  166. {
  167. extensions[ExtensionType.negotiated_ff_dhe_groups] = CreateNegotiatedDheGroupsClientExtension(dheGroups);
  168. }
  169. public static void AddNegotiatedDheGroupsServerExtension(IDictionary extensions, byte dheGroup)
  170. {
  171. extensions[ExtensionType.negotiated_ff_dhe_groups] = CreateNegotiatedDheGroupsServerExtension(dheGroup);
  172. }
  173. public static byte[] GetNegotiatedDheGroupsClientExtension(IDictionary extensions)
  174. {
  175. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.negotiated_ff_dhe_groups);
  176. return extensionData == null ? null : ReadNegotiatedDheGroupsClientExtension(extensionData);
  177. }
  178. public static short GetNegotiatedDheGroupsServerExtension(IDictionary extensions)
  179. {
  180. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.negotiated_ff_dhe_groups);
  181. return extensionData == null ? (short)-1 : (short)ReadNegotiatedDheGroupsServerExtension(extensionData);
  182. }
  183. public static byte[] CreateNegotiatedDheGroupsClientExtension(byte[] dheGroups)
  184. {
  185. if (dheGroups == null || dheGroups.Length < 1 || dheGroups.Length > 255)
  186. throw new TlsFatalAlert(AlertDescription.internal_error);
  187. return TlsUtilities.EncodeUint8ArrayWithUint8Length(dheGroups);
  188. }
  189. public static byte[] CreateNegotiatedDheGroupsServerExtension(byte dheGroup)
  190. {
  191. return new byte[]{ dheGroup };
  192. }
  193. public static byte[] ReadNegotiatedDheGroupsClientExtension(byte[] extensionData)
  194. {
  195. if (extensionData == null)
  196. throw new ArgumentNullException("extensionData");
  197. MemoryStream buf = new MemoryStream(extensionData, false);
  198. byte length = TlsUtilities.ReadUint8(buf);
  199. if (length < 1)
  200. throw new TlsFatalAlert(AlertDescription.decode_error);
  201. byte[] dheGroups = TlsUtilities.ReadUint8Array(length, buf);
  202. TlsProtocol.AssertEmpty(buf);
  203. return dheGroups;
  204. }
  205. public static byte ReadNegotiatedDheGroupsServerExtension(byte[] extensionData)
  206. {
  207. if (extensionData == null)
  208. throw new ArgumentNullException("extensionData");
  209. if (extensionData.Length != 1)
  210. throw new TlsFatalAlert(AlertDescription.decode_error);
  211. return extensionData[0];
  212. }
  213. public static DHParameters GetParametersForDHEGroup(short dheGroup)
  214. {
  215. switch (dheGroup)
  216. {
  217. case FiniteFieldDheGroup.ffdhe2432:
  218. return draft_ffdhe2432;
  219. case FiniteFieldDheGroup.ffdhe3072:
  220. return draft_ffdhe3072;
  221. case FiniteFieldDheGroup.ffdhe4096:
  222. return draft_ffdhe4096;
  223. case FiniteFieldDheGroup.ffdhe6144:
  224. return draft_ffdhe6144;
  225. case FiniteFieldDheGroup.ffdhe8192:
  226. return draft_ffdhe8192;
  227. default:
  228. return null;
  229. }
  230. }
  231. public static bool ContainsDheCipherSuites(int[] cipherSuites)
  232. {
  233. for (int i = 0; i < cipherSuites.Length; ++i)
  234. {
  235. if (IsDheCipherSuite(cipherSuites[i]))
  236. return true;
  237. }
  238. return false;
  239. }
  240. public static bool IsDheCipherSuite(int cipherSuite)
  241. {
  242. switch (cipherSuite)
  243. {
  244. /*
  245. * RFC 2246
  246. */
  247. case CipherSuite.TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA:
  248. case CipherSuite.TLS_DHE_DSS_WITH_DES_CBC_SHA:
  249. case CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  250. case CipherSuite.TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA:
  251. case CipherSuite.TLS_DHE_RSA_WITH_DES_CBC_SHA:
  252. case CipherSuite.TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  253. /*
  254. * RFC 3268
  255. */
  256. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  257. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  258. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  259. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  260. /*
  261. * RFC 5932
  262. */
  263. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA:
  264. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA:
  265. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA:
  266. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA:
  267. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  268. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  269. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  270. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  271. /*
  272. * RFC 4162
  273. */
  274. case CipherSuite.TLS_DHE_DSS_WITH_SEED_CBC_SHA:
  275. case CipherSuite.TLS_DHE_RSA_WITH_SEED_CBC_SHA:
  276. /*
  277. * RFC 4279
  278. */
  279. case CipherSuite.TLS_DHE_PSK_WITH_RC4_128_SHA:
  280. case CipherSuite.TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
  281. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
  282. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
  283. /*
  284. * RFC 4785
  285. */
  286. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA:
  287. /*
  288. * RFC 5246
  289. */
  290. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  291. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  292. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  293. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  294. /*
  295. * RFC 5288
  296. */
  297. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  298. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  299. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  300. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  301. /*
  302. * RFC 5487
  303. */
  304. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  305. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  306. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
  307. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
  308. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA256:
  309. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384:
  310. /*
  311. * RFC 6367
  312. */
  313. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  314. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  315. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  316. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  317. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  318. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  319. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  320. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  321. /*
  322. * RFC 6655
  323. */
  324. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM:
  325. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM:
  326. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM_8:
  327. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM_8:
  328. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CCM:
  329. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CCM:
  330. case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
  331. case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
  332. /*
  333. * draft-ietf-tls-chacha20-poly1305-04
  334. */
  335. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  336. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  337. /*
  338. * draft-zauner-tls-aes-ocb-04
  339. */
  340. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_128_OCB:
  341. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_256_OCB:
  342. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_128_OCB:
  343. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_256_OCB:
  344. return true;
  345. default:
  346. return false;
  347. }
  348. }
  349. public static bool AreCompatibleParameters(DHParameters a, DHParameters b)
  350. {
  351. return a.P.Equals(b.P) && a.G.Equals(b.G)
  352. && (a.Q == null || b.Q == null || a.Q.Equals(b.Q));
  353. }
  354. public static byte[] CalculateDHBasicAgreement(DHPublicKeyParameters publicKey,
  355. DHPrivateKeyParameters privateKey)
  356. {
  357. DHBasicAgreement basicAgreement = new DHBasicAgreement();
  358. basicAgreement.Init(privateKey);
  359. BigInteger agreementValue = basicAgreement.CalculateAgreement(publicKey);
  360. /*
  361. * RFC 5246 8.1.2. Leading bytes of Z that contain all zero bits are stripped before it is
  362. * used as the pre_master_secret.
  363. */
  364. return BigIntegers.AsUnsignedByteArray(agreementValue);
  365. }
  366. public static AsymmetricCipherKeyPair GenerateDHKeyPair(SecureRandom random, DHParameters dhParams)
  367. {
  368. DHBasicKeyPairGenerator dhGen = new DHBasicKeyPairGenerator();
  369. dhGen.Init(new DHKeyGenerationParameters(random, dhParams));
  370. return dhGen.GenerateKeyPair();
  371. }
  372. public static DHPrivateKeyParameters GenerateEphemeralClientKeyExchange(SecureRandom random,
  373. DHParameters dhParams, Stream output)
  374. {
  375. AsymmetricCipherKeyPair kp = GenerateDHKeyPair(random, dhParams);
  376. DHPublicKeyParameters dhPublic = (DHPublicKeyParameters)kp.Public;
  377. WriteDHParameter(dhPublic.Y, output);
  378. return (DHPrivateKeyParameters)kp.Private;
  379. }
  380. public static DHPrivateKeyParameters GenerateEphemeralServerKeyExchange(SecureRandom random,
  381. DHParameters dhParams, Stream output)
  382. {
  383. AsymmetricCipherKeyPair kp = GenerateDHKeyPair(random, dhParams);
  384. DHPublicKeyParameters dhPublic = (DHPublicKeyParameters)kp.Public;
  385. new ServerDHParams(dhPublic).Encode(output);
  386. return (DHPrivateKeyParameters)kp.Private;
  387. }
  388. public static DHParameters ValidateDHParameters(DHParameters parameters)
  389. {
  390. BigInteger p = parameters.P;
  391. BigInteger g = parameters.G;
  392. if (!p.IsProbablePrime(2))
  393. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  394. if (g.CompareTo(Two) < 0 || g.CompareTo(p.Subtract(Two)) > 0)
  395. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  396. return parameters;
  397. }
  398. public static DHPublicKeyParameters ValidateDHPublicKey(DHPublicKeyParameters key)
  399. {
  400. DHParameters parameters = ValidateDHParameters(key.Parameters);
  401. BigInteger Y = key.Y;
  402. if (Y.CompareTo(Two) < 0 || Y.CompareTo(parameters.P.Subtract(Two)) > 0)
  403. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  404. // TODO See RFC 2631 for more discussion of Diffie-Hellman validation
  405. return key;
  406. }
  407. public static BigInteger ReadDHParameter(Stream input)
  408. {
  409. return new BigInteger(1, TlsUtilities.ReadOpaque16(input));
  410. }
  411. public static void WriteDHParameter(BigInteger x, Stream output)
  412. {
  413. TlsUtilities.WriteOpaque16(BigIntegers.AsUnsignedByteArray(x), output);
  414. }
  415. }
  416. }
  417. #endif