TlsUtilities.cs 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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.Nist;
  8. using Org.BouncyCastle.Asn1.Pkcs;
  9. using Org.BouncyCastle.Asn1.X509;
  10. using Org.BouncyCastle.Crypto.Digests;
  11. using Org.BouncyCastle.Crypto.Macs;
  12. using Org.BouncyCastle.Crypto.Parameters;
  13. using Org.BouncyCastle.Security;
  14. using Org.BouncyCastle.Utilities;
  15. using Org.BouncyCastle.Utilities.Date;
  16. using Org.BouncyCastle.Utilities.IO;
  17. namespace Org.BouncyCastle.Crypto.Tls
  18. {
  19. /// <remarks>Some helper functions for MicroTLS.</remarks>
  20. public abstract class TlsUtilities
  21. {
  22. public static readonly byte[] EmptyBytes = new byte[0];
  23. public static readonly short[] EmptyShorts = new short[0];
  24. public static readonly int[] EmptyInts = new int[0];
  25. public static readonly long[] EmptyLongs = new long[0];
  26. public static void CheckUint8(int i)
  27. {
  28. if (!IsValidUint8(i))
  29. throw new TlsFatalAlert(AlertDescription.internal_error);
  30. }
  31. public static void CheckUint8(long i)
  32. {
  33. if (!IsValidUint8(i))
  34. throw new TlsFatalAlert(AlertDescription.internal_error);
  35. }
  36. public static void CheckUint16(int i)
  37. {
  38. if (!IsValidUint16(i))
  39. throw new TlsFatalAlert(AlertDescription.internal_error);
  40. }
  41. public static void CheckUint16(long i)
  42. {
  43. if (!IsValidUint16(i))
  44. throw new TlsFatalAlert(AlertDescription.internal_error);
  45. }
  46. public static void CheckUint24(int i)
  47. {
  48. if (!IsValidUint24(i))
  49. throw new TlsFatalAlert(AlertDescription.internal_error);
  50. }
  51. public static void CheckUint24(long i)
  52. {
  53. if (!IsValidUint24(i))
  54. throw new TlsFatalAlert(AlertDescription.internal_error);
  55. }
  56. public static void CheckUint32(long i)
  57. {
  58. if (!IsValidUint32(i))
  59. throw new TlsFatalAlert(AlertDescription.internal_error);
  60. }
  61. public static void CheckUint48(long i)
  62. {
  63. if (!IsValidUint48(i))
  64. throw new TlsFatalAlert(AlertDescription.internal_error);
  65. }
  66. public static void CheckUint64(long i)
  67. {
  68. if (!IsValidUint64(i))
  69. throw new TlsFatalAlert(AlertDescription.internal_error);
  70. }
  71. public static bool IsValidUint8(int i)
  72. {
  73. return (i & 0xFF) == i;
  74. }
  75. public static bool IsValidUint8(long i)
  76. {
  77. return (i & 0xFFL) == i;
  78. }
  79. public static bool IsValidUint16(int i)
  80. {
  81. return (i & 0xFFFF) == i;
  82. }
  83. public static bool IsValidUint16(long i)
  84. {
  85. return (i & 0xFFFFL) == i;
  86. }
  87. public static bool IsValidUint24(int i)
  88. {
  89. return (i & 0xFFFFFF) == i;
  90. }
  91. public static bool IsValidUint24(long i)
  92. {
  93. return (i & 0xFFFFFFL) == i;
  94. }
  95. public static bool IsValidUint32(long i)
  96. {
  97. return (i & 0xFFFFFFFFL) == i;
  98. }
  99. public static bool IsValidUint48(long i)
  100. {
  101. return (i & 0xFFFFFFFFFFFFL) == i;
  102. }
  103. public static bool IsValidUint64(long i)
  104. {
  105. return true;
  106. }
  107. public static bool IsSsl(TlsContext context)
  108. {
  109. return context.ServerVersion.IsSsl;
  110. }
  111. public static bool IsTlsV11(ProtocolVersion version)
  112. {
  113. return ProtocolVersion.TLSv11.IsEqualOrEarlierVersionOf(version.GetEquivalentTLSVersion());
  114. }
  115. public static bool IsTlsV11(TlsContext context)
  116. {
  117. return IsTlsV11(context.ServerVersion);
  118. }
  119. public static bool IsTlsV12(ProtocolVersion version)
  120. {
  121. return ProtocolVersion.TLSv12.IsEqualOrEarlierVersionOf(version.GetEquivalentTLSVersion());
  122. }
  123. public static bool IsTlsV12(TlsContext context)
  124. {
  125. return IsTlsV12(context.ServerVersion);
  126. }
  127. public static void WriteUint8(byte i, Stream output)
  128. {
  129. output.WriteByte(i);
  130. }
  131. public static void WriteUint8(byte i, byte[] buf, int offset)
  132. {
  133. buf[offset] = i;
  134. }
  135. public static void WriteUint16(int i, Stream output)
  136. {
  137. output.WriteByte((byte)(i >> 8));
  138. output.WriteByte((byte)i);
  139. }
  140. public static void WriteUint16(int i, byte[] buf, int offset)
  141. {
  142. buf[offset] = (byte)(i >> 8);
  143. buf[offset + 1] = (byte)i;
  144. }
  145. public static void WriteUint24(int i, Stream output)
  146. {
  147. output.WriteByte((byte)(i >> 16));
  148. output.WriteByte((byte)(i >> 8));
  149. output.WriteByte((byte)i);
  150. }
  151. public static void WriteUint24(int i, byte[] buf, int offset)
  152. {
  153. buf[offset] = (byte)(i >> 16);
  154. buf[offset + 1] = (byte)(i >> 8);
  155. buf[offset + 2] = (byte)i;
  156. }
  157. public static void WriteUint32(long i, Stream output)
  158. {
  159. output.WriteByte((byte)(i >> 24));
  160. output.WriteByte((byte)(i >> 16));
  161. output.WriteByte((byte)(i >> 8));
  162. output.WriteByte((byte)i);
  163. }
  164. public static void WriteUint32(long i, byte[] buf, int offset)
  165. {
  166. buf[offset] = (byte)(i >> 24);
  167. buf[offset + 1] = (byte)(i >> 16);
  168. buf[offset + 2] = (byte)(i >> 8);
  169. buf[offset + 3] = (byte)i;
  170. }
  171. public static void WriteUint48(long i, Stream output)
  172. {
  173. output.WriteByte((byte)(i >> 40));
  174. output.WriteByte((byte)(i >> 32));
  175. output.WriteByte((byte)(i >> 24));
  176. output.WriteByte((byte)(i >> 16));
  177. output.WriteByte((byte)(i >> 8));
  178. output.WriteByte((byte)i);
  179. }
  180. public static void WriteUint48(long i, byte[] buf, int offset)
  181. {
  182. buf[offset] = (byte)(i >> 40);
  183. buf[offset + 1] = (byte)(i >> 32);
  184. buf[offset + 2] = (byte)(i >> 24);
  185. buf[offset + 3] = (byte)(i >> 16);
  186. buf[offset + 4] = (byte)(i >> 8);
  187. buf[offset + 5] = (byte)i;
  188. }
  189. public static void WriteUint64(long i, Stream output)
  190. {
  191. output.WriteByte((byte)(i >> 56));
  192. output.WriteByte((byte)(i >> 48));
  193. output.WriteByte((byte)(i >> 40));
  194. output.WriteByte((byte)(i >> 32));
  195. output.WriteByte((byte)(i >> 24));
  196. output.WriteByte((byte)(i >> 16));
  197. output.WriteByte((byte)(i >> 8));
  198. output.WriteByte((byte)i);
  199. }
  200. public static void WriteUint64(long i, byte[] buf, int offset)
  201. {
  202. buf[offset] = (byte)(i >> 56);
  203. buf[offset + 1] = (byte)(i >> 48);
  204. buf[offset + 2] = (byte)(i >> 40);
  205. buf[offset + 3] = (byte)(i >> 32);
  206. buf[offset + 4] = (byte)(i >> 24);
  207. buf[offset + 5] = (byte)(i >> 16);
  208. buf[offset + 6] = (byte)(i >> 8);
  209. buf[offset + 7] = (byte)i;
  210. }
  211. public static void WriteOpaque8(byte[] buf, Stream output)
  212. {
  213. WriteUint8((byte)buf.Length, output);
  214. output.Write(buf, 0, buf.Length);
  215. }
  216. public static void WriteOpaque16(byte[] buf, Stream output)
  217. {
  218. WriteUint16(buf.Length, output);
  219. output.Write(buf, 0, buf.Length);
  220. }
  221. public static void WriteOpaque24(byte[] buf, Stream output)
  222. {
  223. WriteUint24(buf.Length, output);
  224. output.Write(buf, 0, buf.Length);
  225. }
  226. public static void WriteUint8Array(byte[] uints, Stream output)
  227. {
  228. output.Write(uints, 0, uints.Length);
  229. }
  230. public static void WriteUint8Array(byte[] uints, byte[] buf, int offset)
  231. {
  232. for (int i = 0; i < uints.Length; ++i)
  233. {
  234. WriteUint8(uints[i], buf, offset);
  235. ++offset;
  236. }
  237. }
  238. public static void WriteUint8ArrayWithUint8Length(byte[] uints, Stream output)
  239. {
  240. CheckUint8(uints.Length);
  241. WriteUint8((byte)uints.Length, output);
  242. WriteUint8Array(uints, output);
  243. }
  244. public static void WriteUint8ArrayWithUint8Length(byte[] uints, byte[] buf, int offset)
  245. {
  246. CheckUint8(uints.Length);
  247. WriteUint8((byte)uints.Length, buf, offset);
  248. WriteUint8Array(uints, buf, offset + 1);
  249. }
  250. public static void WriteUint16Array(int[] uints, Stream output)
  251. {
  252. for (int i = 0; i < uints.Length; ++i)
  253. {
  254. WriteUint16(uints[i], output);
  255. }
  256. }
  257. public static void WriteUint16Array(int[] uints, byte[] buf, int offset)
  258. {
  259. for (int i = 0; i < uints.Length; ++i)
  260. {
  261. WriteUint16(uints[i], buf, offset);
  262. offset += 2;
  263. }
  264. }
  265. public static void WriteUint16ArrayWithUint16Length(int[] uints, Stream output)
  266. {
  267. int length = 2 * uints.Length;
  268. CheckUint16(length);
  269. WriteUint16(length, output);
  270. WriteUint16Array(uints, output);
  271. }
  272. public static void WriteUint16ArrayWithUint16Length(int[] uints, byte[] buf, int offset)
  273. {
  274. int length = 2 * uints.Length;
  275. CheckUint16(length);
  276. WriteUint16(length, buf, offset);
  277. WriteUint16Array(uints, buf, offset + 2);
  278. }
  279. public static byte[] EncodeOpaque8(byte[] buf)
  280. {
  281. CheckUint8(buf.Length);
  282. return Arrays.Prepend(buf, (byte)buf.Length);
  283. }
  284. public static byte[] EncodeUint8ArrayWithUint8Length(byte[] uints)
  285. {
  286. byte[] result = new byte[1 + uints.Length];
  287. WriteUint8ArrayWithUint8Length(uints, result, 0);
  288. return result;
  289. }
  290. public static byte[] EncodeUint16ArrayWithUint16Length(int[] uints)
  291. {
  292. int length = 2 * uints.Length;
  293. byte[] result = new byte[2 + length];
  294. WriteUint16ArrayWithUint16Length(uints, result, 0);
  295. return result;
  296. }
  297. public static byte ReadUint8(Stream input)
  298. {
  299. int i = input.ReadByte();
  300. if (i < 0)
  301. throw new EndOfStreamException();
  302. return (byte)i;
  303. }
  304. public static byte ReadUint8(byte[] buf, int offset)
  305. {
  306. return buf[offset];
  307. }
  308. public static int ReadUint16(Stream input)
  309. {
  310. int i1 = input.ReadByte();
  311. int i2 = input.ReadByte();
  312. if (i2 < 0)
  313. throw new EndOfStreamException();
  314. return (i1 << 8) | i2;
  315. }
  316. public static int ReadUint16(byte[] buf, int offset)
  317. {
  318. uint n = (uint)buf[offset] << 8;
  319. n |= (uint)buf[++offset];
  320. return (int)n;
  321. }
  322. public static int ReadUint24(Stream input)
  323. {
  324. int i1 = input.ReadByte();
  325. int i2 = input.ReadByte();
  326. int i3 = input.ReadByte();
  327. if (i3 < 0)
  328. throw new EndOfStreamException();
  329. return (i1 << 16) | (i2 << 8) | i3;
  330. }
  331. public static int ReadUint24(byte[] buf, int offset)
  332. {
  333. uint n = (uint)buf[offset] << 16;
  334. n |= (uint)buf[++offset] << 8;
  335. n |= (uint)buf[++offset];
  336. return (int)n;
  337. }
  338. public static long ReadUint32(Stream input)
  339. {
  340. int i1 = input.ReadByte();
  341. int i2 = input.ReadByte();
  342. int i3 = input.ReadByte();
  343. int i4 = input.ReadByte();
  344. if (i4 < 0)
  345. throw new EndOfStreamException();
  346. return (long)(uint)((i1 << 24) | (i2 << 16) | (i3 << 8) | i4);
  347. }
  348. public static long ReadUint32(byte[] buf, int offset)
  349. {
  350. uint n = (uint)buf[offset] << 24;
  351. n |= (uint)buf[++offset] << 16;
  352. n |= (uint)buf[++offset] << 8;
  353. n |= (uint)buf[++offset];
  354. return (long)n;
  355. }
  356. public static long ReadUint48(Stream input)
  357. {
  358. int hi = ReadUint24(input);
  359. int lo = ReadUint24(input);
  360. return ((long)(hi & 0xffffffffL) << 24) | (long)(lo & 0xffffffffL);
  361. }
  362. public static long ReadUint48(byte[] buf, int offset)
  363. {
  364. int hi = ReadUint24(buf, offset);
  365. int lo = ReadUint24(buf, offset + 3);
  366. return ((long)(hi & 0xffffffffL) << 24) | (long)(lo & 0xffffffffL);
  367. }
  368. public static byte[] ReadAllOrNothing(int length, Stream input)
  369. {
  370. if (length < 1)
  371. return EmptyBytes;
  372. byte[] buf = new byte[length];
  373. int read = Streams.ReadFully(input, buf);
  374. if (read == 0)
  375. return null;
  376. if (read != length)
  377. throw new EndOfStreamException();
  378. return buf;
  379. }
  380. public static byte[] ReadFully(int length, Stream input)
  381. {
  382. if (length < 1)
  383. return EmptyBytes;
  384. byte[] buf = new byte[length];
  385. if (length != Streams.ReadFully(input, buf))
  386. throw new EndOfStreamException();
  387. return buf;
  388. }
  389. public static void ReadFully(byte[] buf, Stream input)
  390. {
  391. if (Streams.ReadFully(input, buf, 0, buf.Length) < buf.Length)
  392. throw new EndOfStreamException();
  393. }
  394. public static byte[] ReadOpaque8(Stream input)
  395. {
  396. byte length = ReadUint8(input);
  397. byte[] bytes = new byte[length];
  398. ReadFully(bytes, input);
  399. return bytes;
  400. }
  401. public static byte[] ReadOpaque16(Stream input)
  402. {
  403. int length = ReadUint16(input);
  404. byte[] bytes = new byte[length];
  405. ReadFully(bytes, input);
  406. return bytes;
  407. }
  408. public static byte[] ReadOpaque24(Stream input)
  409. {
  410. int length = ReadUint24(input);
  411. return ReadFully(length, input);
  412. }
  413. public static byte[] ReadUint8Array(int count, Stream input)
  414. {
  415. byte[] uints = new byte[count];
  416. for (int i = 0; i < count; ++i)
  417. {
  418. uints[i] = ReadUint8(input);
  419. }
  420. return uints;
  421. }
  422. public static int[] ReadUint16Array(int count, Stream input)
  423. {
  424. int[] uints = new int[count];
  425. for (int i = 0; i < count; ++i)
  426. {
  427. uints[i] = ReadUint16(input);
  428. }
  429. return uints;
  430. }
  431. public static ProtocolVersion ReadVersion(byte[] buf, int offset)
  432. {
  433. return ProtocolVersion.Get(buf[offset], buf[offset + 1]);
  434. }
  435. public static ProtocolVersion ReadVersion(Stream input)
  436. {
  437. int i1 = input.ReadByte();
  438. int i2 = input.ReadByte();
  439. if (i2 < 0)
  440. throw new EndOfStreamException();
  441. return ProtocolVersion.Get(i1, i2);
  442. }
  443. public static int ReadVersionRaw(byte[] buf, int offset)
  444. {
  445. return (buf[offset] << 8) | buf[offset + 1];
  446. }
  447. public static int ReadVersionRaw(Stream input)
  448. {
  449. int i1 = input.ReadByte();
  450. int i2 = input.ReadByte();
  451. if (i2 < 0)
  452. throw new EndOfStreamException();
  453. return (i1 << 8) | i2;
  454. }
  455. public static Asn1Object ReadAsn1Object(byte[] encoding)
  456. {
  457. MemoryStream input = new MemoryStream(encoding, false);
  458. Asn1InputStream asn1 = new Asn1InputStream(input, encoding.Length);
  459. Asn1Object result = asn1.ReadObject();
  460. if (null == result)
  461. throw new TlsFatalAlert(AlertDescription.decode_error);
  462. if (input.Position != input.Length)
  463. throw new TlsFatalAlert(AlertDescription.decode_error);
  464. return result;
  465. }
  466. public static Asn1Object ReadDerObject(byte[] encoding)
  467. {
  468. /*
  469. * NOTE: The current ASN.1 parsing code can't enforce DER-only parsing, but since DER is
  470. * canonical, we can check it by re-encoding the result and comparing to the original.
  471. */
  472. Asn1Object result = ReadAsn1Object(encoding);
  473. byte[] check = result.GetEncoded(Asn1Encodable.Der);
  474. if (!Arrays.AreEqual(check, encoding))
  475. throw new TlsFatalAlert(AlertDescription.decode_error);
  476. return result;
  477. }
  478. public static void WriteGmtUnixTime(byte[] buf, int offset)
  479. {
  480. int t = (int)(DateTimeUtilities.CurrentUnixMs() / 1000L);
  481. buf[offset] = (byte)(t >> 24);
  482. buf[offset + 1] = (byte)(t >> 16);
  483. buf[offset + 2] = (byte)(t >> 8);
  484. buf[offset + 3] = (byte)t;
  485. }
  486. public static void WriteVersion(ProtocolVersion version, Stream output)
  487. {
  488. output.WriteByte((byte)version.MajorVersion);
  489. output.WriteByte((byte)version.MinorVersion);
  490. }
  491. public static void WriteVersion(ProtocolVersion version, byte[] buf, int offset)
  492. {
  493. buf[offset] = (byte)version.MajorVersion;
  494. buf[offset + 1] = (byte)version.MinorVersion;
  495. }
  496. public static IList GetDefaultDssSignatureAlgorithms()
  497. {
  498. return VectorOfOne(new SignatureAndHashAlgorithm(HashAlgorithm.sha1, SignatureAlgorithm.dsa));
  499. }
  500. public static IList GetDefaultECDsaSignatureAlgorithms()
  501. {
  502. return VectorOfOne(new SignatureAndHashAlgorithm(HashAlgorithm.sha1, SignatureAlgorithm.ecdsa));
  503. }
  504. public static IList GetDefaultRsaSignatureAlgorithms()
  505. {
  506. return VectorOfOne(new SignatureAndHashAlgorithm(HashAlgorithm.sha1, SignatureAlgorithm.rsa));
  507. }
  508. public static byte[] GetExtensionData(IDictionary extensions, int extensionType)
  509. {
  510. return extensions == null ? null : (byte[])extensions[extensionType];
  511. }
  512. public static IList GetDefaultSupportedSignatureAlgorithms()
  513. {
  514. byte[] hashAlgorithms = new byte[]{ HashAlgorithm.sha1, HashAlgorithm.sha224, HashAlgorithm.sha256,
  515. HashAlgorithm.sha384, HashAlgorithm.sha512 };
  516. byte[] signatureAlgorithms = new byte[]{ SignatureAlgorithm.rsa, SignatureAlgorithm.dsa,
  517. SignatureAlgorithm.ecdsa };
  518. IList result = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  519. for (int i = 0; i < signatureAlgorithms.Length; ++i)
  520. {
  521. for (int j = 0; j < hashAlgorithms.Length; ++j)
  522. {
  523. result.Add(new SignatureAndHashAlgorithm(hashAlgorithms[j], signatureAlgorithms[i]));
  524. }
  525. }
  526. return result;
  527. }
  528. public static SignatureAndHashAlgorithm GetSignatureAndHashAlgorithm(TlsContext context,
  529. TlsSignerCredentials signerCredentials)
  530. {
  531. SignatureAndHashAlgorithm signatureAndHashAlgorithm = null;
  532. if (IsTlsV12(context))
  533. {
  534. signatureAndHashAlgorithm = signerCredentials.SignatureAndHashAlgorithm;
  535. if (signatureAndHashAlgorithm == null)
  536. throw new TlsFatalAlert(AlertDescription.internal_error);
  537. }
  538. return signatureAndHashAlgorithm;
  539. }
  540. public static bool HasExpectedEmptyExtensionData(IDictionary extensions, int extensionType,
  541. byte alertDescription)
  542. {
  543. byte[] extension_data = GetExtensionData(extensions, extensionType);
  544. if (extension_data == null)
  545. return false;
  546. if (extension_data.Length != 0)
  547. throw new TlsFatalAlert(alertDescription);
  548. return true;
  549. }
  550. public static TlsSession ImportSession(byte[] sessionID, SessionParameters sessionParameters)
  551. {
  552. return new TlsSessionImpl(sessionID, sessionParameters);
  553. }
  554. public static bool IsSignatureAlgorithmsExtensionAllowed(ProtocolVersion clientVersion)
  555. {
  556. return ProtocolVersion.TLSv12.IsEqualOrEarlierVersionOf(clientVersion.GetEquivalentTLSVersion());
  557. }
  558. /**
  559. * Add a 'signature_algorithms' extension to existing extensions.
  560. *
  561. * @param extensions A {@link Hashtable} to add the extension to.
  562. * @param supportedSignatureAlgorithms {@link Vector} containing at least 1 {@link SignatureAndHashAlgorithm}.
  563. * @throws IOException
  564. */
  565. public static void AddSignatureAlgorithmsExtension(IDictionary extensions, IList supportedSignatureAlgorithms)
  566. {
  567. extensions[ExtensionType.signature_algorithms] = CreateSignatureAlgorithmsExtension(supportedSignatureAlgorithms);
  568. }
  569. /**
  570. * Get a 'signature_algorithms' extension from extensions.
  571. *
  572. * @param extensions A {@link Hashtable} to get the extension from, if it is present.
  573. * @return A {@link Vector} containing at least 1 {@link SignatureAndHashAlgorithm}, or null.
  574. * @throws IOException
  575. */
  576. public static IList GetSignatureAlgorithmsExtension(IDictionary extensions)
  577. {
  578. byte[] extensionData = GetExtensionData(extensions, ExtensionType.signature_algorithms);
  579. return extensionData == null ? null : ReadSignatureAlgorithmsExtension(extensionData);
  580. }
  581. /**
  582. * Create a 'signature_algorithms' extension value.
  583. *
  584. * @param supportedSignatureAlgorithms A {@link Vector} containing at least 1 {@link SignatureAndHashAlgorithm}.
  585. * @return A byte array suitable for use as an extension value.
  586. * @throws IOException
  587. */
  588. public static byte[] CreateSignatureAlgorithmsExtension(IList supportedSignatureAlgorithms)
  589. {
  590. MemoryStream buf = new MemoryStream();
  591. // supported_signature_algorithms
  592. EncodeSupportedSignatureAlgorithms(supportedSignatureAlgorithms, false, buf);
  593. return buf.ToArray();
  594. }
  595. /**
  596. * Read 'signature_algorithms' extension data.
  597. *
  598. * @param extensionData The extension data.
  599. * @return A {@link Vector} containing at least 1 {@link SignatureAndHashAlgorithm}.
  600. * @throws IOException
  601. */
  602. public static IList ReadSignatureAlgorithmsExtension(byte[] extensionData)
  603. {
  604. if (extensionData == null)
  605. throw new ArgumentNullException("extensionData");
  606. MemoryStream buf = new MemoryStream(extensionData, false);
  607. // supported_signature_algorithms
  608. IList supported_signature_algorithms = ParseSupportedSignatureAlgorithms(false, buf);
  609. TlsProtocol.AssertEmpty(buf);
  610. return supported_signature_algorithms;
  611. }
  612. public static void EncodeSupportedSignatureAlgorithms(IList supportedSignatureAlgorithms, bool allowAnonymous,
  613. Stream output)
  614. {
  615. if (supportedSignatureAlgorithms == null)
  616. throw new ArgumentNullException("supportedSignatureAlgorithms");
  617. if (supportedSignatureAlgorithms.Count < 1 || supportedSignatureAlgorithms.Count >= (1 << 15))
  618. throw new ArgumentException("must have length from 1 to (2^15 - 1)", "supportedSignatureAlgorithms");
  619. // supported_signature_algorithms
  620. int length = 2 * supportedSignatureAlgorithms.Count;
  621. CheckUint16(length);
  622. WriteUint16(length, output);
  623. foreach (SignatureAndHashAlgorithm entry in supportedSignatureAlgorithms)
  624. {
  625. if (!allowAnonymous && entry.Signature == SignatureAlgorithm.anonymous)
  626. {
  627. /*
  628. * RFC 5246 7.4.1.4.1 The "anonymous" value is meaningless in this context but used
  629. * in Section 7.4.3. It MUST NOT appear in this extension.
  630. */
  631. throw new ArgumentException(
  632. "SignatureAlgorithm.anonymous MUST NOT appear in the signature_algorithms extension");
  633. }
  634. entry.Encode(output);
  635. }
  636. }
  637. public static IList ParseSupportedSignatureAlgorithms(bool allowAnonymous, Stream input)
  638. {
  639. // supported_signature_algorithms
  640. int length = ReadUint16(input);
  641. if (length < 2 || (length & 1) != 0)
  642. throw new TlsFatalAlert(AlertDescription.decode_error);
  643. int count = length / 2;
  644. IList supportedSignatureAlgorithms = Org.BouncyCastle.Utilities.Platform.CreateArrayList(count);
  645. for (int i = 0; i < count; ++i)
  646. {
  647. SignatureAndHashAlgorithm entry = SignatureAndHashAlgorithm.Parse(input);
  648. if (!allowAnonymous && entry.Signature == SignatureAlgorithm.anonymous)
  649. {
  650. /*
  651. * RFC 5246 7.4.1.4.1 The "anonymous" value is meaningless in this context but used
  652. * in Section 7.4.3. It MUST NOT appear in this extension.
  653. */
  654. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  655. }
  656. supportedSignatureAlgorithms.Add(entry);
  657. }
  658. return supportedSignatureAlgorithms;
  659. }
  660. public static void VerifySupportedSignatureAlgorithm(IList supportedSignatureAlgorithms, SignatureAndHashAlgorithm signatureAlgorithm)
  661. {
  662. if (supportedSignatureAlgorithms == null)
  663. throw new ArgumentNullException("supportedSignatureAlgorithms");
  664. if (supportedSignatureAlgorithms.Count < 1 || supportedSignatureAlgorithms.Count >= (1 << 15))
  665. throw new ArgumentException("must have length from 1 to (2^15 - 1)", "supportedSignatureAlgorithms");
  666. if (signatureAlgorithm == null)
  667. throw new ArgumentNullException("signatureAlgorithm");
  668. if (signatureAlgorithm.Signature != SignatureAlgorithm.anonymous)
  669. {
  670. foreach (SignatureAndHashAlgorithm entry in supportedSignatureAlgorithms)
  671. {
  672. if (entry.Hash == signatureAlgorithm.Hash && entry.Signature == signatureAlgorithm.Signature)
  673. return;
  674. }
  675. }
  676. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  677. }
  678. public static byte[] PRF(TlsContext context, byte[] secret, string asciiLabel, byte[] seed, int size)
  679. {
  680. ProtocolVersion version = context.ServerVersion;
  681. if (version.IsSsl)
  682. throw new InvalidOperationException("No PRF available for SSLv3 session");
  683. byte[] label = Strings.ToByteArray(asciiLabel);
  684. byte[] labelSeed = Concat(label, seed);
  685. int prfAlgorithm = context.SecurityParameters.PrfAlgorithm;
  686. if (prfAlgorithm == PrfAlgorithm.tls_prf_legacy)
  687. return PRF_legacy(secret, label, labelSeed, size);
  688. IDigest prfDigest = CreatePrfHash(prfAlgorithm);
  689. byte[] buf = new byte[size];
  690. HMacHash(prfDigest, secret, labelSeed, buf);
  691. return buf;
  692. }
  693. public static byte[] PRF_legacy(byte[] secret, string asciiLabel, byte[] seed, int size)
  694. {
  695. byte[] label = Strings.ToByteArray(asciiLabel);
  696. byte[] labelSeed = Concat(label, seed);
  697. return PRF_legacy(secret, label, labelSeed, size);
  698. }
  699. internal static byte[] PRF_legacy(byte[] secret, byte[] label, byte[] labelSeed, int size)
  700. {
  701. int s_half = (secret.Length + 1) / 2;
  702. byte[] s1 = new byte[s_half];
  703. byte[] s2 = new byte[s_half];
  704. Array.Copy(secret, 0, s1, 0, s_half);
  705. Array.Copy(secret, secret.Length - s_half, s2, 0, s_half);
  706. byte[] b1 = new byte[size];
  707. byte[] b2 = new byte[size];
  708. HMacHash(CreateHash(HashAlgorithm.md5), s1, labelSeed, b1);
  709. HMacHash(CreateHash(HashAlgorithm.sha1), s2, labelSeed, b2);
  710. for (int i = 0; i < size; i++)
  711. {
  712. b1[i] ^= b2[i];
  713. }
  714. return b1;
  715. }
  716. internal static byte[] Concat(byte[] a, byte[] b)
  717. {
  718. byte[] c = new byte[a.Length + b.Length];
  719. Array.Copy(a, 0, c, 0, a.Length);
  720. Array.Copy(b, 0, c, a.Length, b.Length);
  721. return c;
  722. }
  723. internal static void HMacHash(IDigest digest, byte[] secret, byte[] seed, byte[] output)
  724. {
  725. HMac mac = new HMac(digest);
  726. mac.Init(new KeyParameter(secret));
  727. byte[] a = seed;
  728. int size = digest.GetDigestSize();
  729. int iterations = (output.Length + size - 1) / size;
  730. byte[] buf = new byte[mac.GetMacSize()];
  731. byte[] buf2 = new byte[mac.GetMacSize()];
  732. for (int i = 0; i < iterations; i++)
  733. {
  734. mac.BlockUpdate(a, 0, a.Length);
  735. mac.DoFinal(buf, 0);
  736. a = buf;
  737. mac.BlockUpdate(a, 0, a.Length);
  738. mac.BlockUpdate(seed, 0, seed.Length);
  739. mac.DoFinal(buf2, 0);
  740. Array.Copy(buf2, 0, output, (size * i), System.Math.Min(size, output.Length - (size * i)));
  741. }
  742. }
  743. internal static void ValidateKeyUsage(X509CertificateStructure c, int keyUsageBits)
  744. {
  745. X509Extensions exts = c.TbsCertificate.Extensions;
  746. if (exts != null)
  747. {
  748. X509Extension ext = exts.GetExtension(X509Extensions.KeyUsage);
  749. if (ext != null)
  750. {
  751. DerBitString ku = KeyUsage.GetInstance(ext);
  752. int bits = ku.GetBytes()[0];
  753. if ((bits & keyUsageBits) != keyUsageBits)
  754. throw new TlsFatalAlert(AlertDescription.certificate_unknown);
  755. }
  756. }
  757. }
  758. internal static byte[] CalculateKeyBlock(TlsContext context, int size)
  759. {
  760. SecurityParameters securityParameters = context.SecurityParameters;
  761. byte[] master_secret = securityParameters.MasterSecret;
  762. byte[] seed = Concat(securityParameters.ServerRandom, securityParameters.ClientRandom);
  763. if (IsSsl(context))
  764. return CalculateKeyBlock_Ssl(master_secret, seed, size);
  765. return PRF(context, master_secret, ExporterLabel.key_expansion, seed, size);
  766. }
  767. internal static byte[] CalculateKeyBlock_Ssl(byte[] master_secret, byte[] random, int size)
  768. {
  769. IDigest md5 = CreateHash(HashAlgorithm.md5);
  770. IDigest sha1 = CreateHash(HashAlgorithm.sha1);
  771. int md5Size = md5.GetDigestSize();
  772. byte[] shatmp = new byte[sha1.GetDigestSize()];
  773. byte[] tmp = new byte[size + md5Size];
  774. int i = 0, pos = 0;
  775. while (pos < size)
  776. {
  777. byte[] ssl3Const = SSL3_CONST[i];
  778. sha1.BlockUpdate(ssl3Const, 0, ssl3Const.Length);
  779. sha1.BlockUpdate(master_secret, 0, master_secret.Length);
  780. sha1.BlockUpdate(random, 0, random.Length);
  781. sha1.DoFinal(shatmp, 0);
  782. md5.BlockUpdate(master_secret, 0, master_secret.Length);
  783. md5.BlockUpdate(shatmp, 0, shatmp.Length);
  784. md5.DoFinal(tmp, pos);
  785. pos += md5Size;
  786. ++i;
  787. }
  788. return Arrays.CopyOfRange(tmp, 0, size);
  789. }
  790. internal static byte[] CalculateMasterSecret(TlsContext context, byte[] pre_master_secret)
  791. {
  792. SecurityParameters securityParameters = context.SecurityParameters;
  793. byte[] seed = securityParameters.extendedMasterSecret
  794. ? securityParameters.SessionHash
  795. : Concat(securityParameters.ClientRandom, securityParameters.ServerRandom);
  796. if (IsSsl(context))
  797. return CalculateMasterSecret_Ssl(pre_master_secret, seed);
  798. string asciiLabel = securityParameters.extendedMasterSecret
  799. ? ExporterLabel.extended_master_secret
  800. : ExporterLabel.master_secret;
  801. return PRF(context, pre_master_secret, asciiLabel, seed, 48);
  802. }
  803. internal static byte[] CalculateMasterSecret_Ssl(byte[] pre_master_secret, byte[] random)
  804. {
  805. IDigest md5 = CreateHash(HashAlgorithm.md5);
  806. IDigest sha1 = CreateHash(HashAlgorithm.sha1);
  807. int md5Size = md5.GetDigestSize();
  808. byte[] shatmp = new byte[sha1.GetDigestSize()];
  809. byte[] rval = new byte[md5Size * 3];
  810. int pos = 0;
  811. for (int i = 0; i < 3; ++i)
  812. {
  813. byte[] ssl3Const = SSL3_CONST[i];
  814. sha1.BlockUpdate(ssl3Const, 0, ssl3Const.Length);
  815. sha1.BlockUpdate(pre_master_secret, 0, pre_master_secret.Length);
  816. sha1.BlockUpdate(random, 0, random.Length);
  817. sha1.DoFinal(shatmp, 0);
  818. md5.BlockUpdate(pre_master_secret, 0, pre_master_secret.Length);
  819. md5.BlockUpdate(shatmp, 0, shatmp.Length);
  820. md5.DoFinal(rval, pos);
  821. pos += md5Size;
  822. }
  823. return rval;
  824. }
  825. internal static byte[] CalculateVerifyData(TlsContext context, string asciiLabel, byte[] handshakeHash)
  826. {
  827. if (IsSsl(context))
  828. return handshakeHash;
  829. SecurityParameters securityParameters = context.SecurityParameters;
  830. byte[] master_secret = securityParameters.MasterSecret;
  831. int verify_data_length = securityParameters.VerifyDataLength;
  832. return PRF(context, master_secret, asciiLabel, handshakeHash, verify_data_length);
  833. }
  834. public static IDigest CreateHash(byte hashAlgorithm)
  835. {
  836. switch (hashAlgorithm)
  837. {
  838. case HashAlgorithm.md5:
  839. return new MD5Digest();
  840. case HashAlgorithm.sha1:
  841. return new Sha1Digest();
  842. case HashAlgorithm.sha224:
  843. return new Sha224Digest();
  844. case HashAlgorithm.sha256:
  845. return new Sha256Digest();
  846. case HashAlgorithm.sha384:
  847. return new Sha384Digest();
  848. case HashAlgorithm.sha512:
  849. return new Sha512Digest();
  850. default:
  851. throw new ArgumentException("unknown HashAlgorithm", "hashAlgorithm");
  852. }
  853. }
  854. public static IDigest CreateHash(SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  855. {
  856. return signatureAndHashAlgorithm == null
  857. ? new CombinedHash()
  858. : CreateHash(signatureAndHashAlgorithm.Hash);
  859. }
  860. public static IDigest CloneHash(byte hashAlgorithm, IDigest hash)
  861. {
  862. switch (hashAlgorithm)
  863. {
  864. case HashAlgorithm.md5:
  865. return new MD5Digest((MD5Digest)hash);
  866. case HashAlgorithm.sha1:
  867. return new Sha1Digest((Sha1Digest)hash);
  868. case HashAlgorithm.sha224:
  869. return new Sha224Digest((Sha224Digest)hash);
  870. case HashAlgorithm.sha256:
  871. return new Sha256Digest((Sha256Digest)hash);
  872. case HashAlgorithm.sha384:
  873. return new Sha384Digest((Sha384Digest)hash);
  874. case HashAlgorithm.sha512:
  875. return new Sha512Digest((Sha512Digest)hash);
  876. default:
  877. throw new ArgumentException("unknown HashAlgorithm", "hashAlgorithm");
  878. }
  879. }
  880. public static IDigest CreatePrfHash(int prfAlgorithm)
  881. {
  882. switch (prfAlgorithm)
  883. {
  884. case PrfAlgorithm.tls_prf_legacy:
  885. return new CombinedHash();
  886. default:
  887. return CreateHash(GetHashAlgorithmForPrfAlgorithm(prfAlgorithm));
  888. }
  889. }
  890. public static IDigest ClonePrfHash(int prfAlgorithm, IDigest hash)
  891. {
  892. switch (prfAlgorithm)
  893. {
  894. case PrfAlgorithm.tls_prf_legacy:
  895. return new CombinedHash((CombinedHash)hash);
  896. default:
  897. return CloneHash(GetHashAlgorithmForPrfAlgorithm(prfAlgorithm), hash);
  898. }
  899. }
  900. public static byte GetHashAlgorithmForPrfAlgorithm(int prfAlgorithm)
  901. {
  902. switch (prfAlgorithm)
  903. {
  904. case PrfAlgorithm.tls_prf_legacy:
  905. throw new ArgumentException("legacy PRF not a valid algorithm", "prfAlgorithm");
  906. case PrfAlgorithm.tls_prf_sha256:
  907. return HashAlgorithm.sha256;
  908. case PrfAlgorithm.tls_prf_sha384:
  909. return HashAlgorithm.sha384;
  910. default:
  911. throw new ArgumentException("unknown PrfAlgorithm", "prfAlgorithm");
  912. }
  913. }
  914. public static DerObjectIdentifier GetOidForHashAlgorithm(byte hashAlgorithm)
  915. {
  916. switch (hashAlgorithm)
  917. {
  918. case HashAlgorithm.md5:
  919. return PkcsObjectIdentifiers.MD5;
  920. case HashAlgorithm.sha1:
  921. return X509ObjectIdentifiers.IdSha1;
  922. case HashAlgorithm.sha224:
  923. return NistObjectIdentifiers.IdSha224;
  924. case HashAlgorithm.sha256:
  925. return NistObjectIdentifiers.IdSha256;
  926. case HashAlgorithm.sha384:
  927. return NistObjectIdentifiers.IdSha384;
  928. case HashAlgorithm.sha512:
  929. return NistObjectIdentifiers.IdSha512;
  930. default:
  931. throw new ArgumentException("unknown HashAlgorithm", "hashAlgorithm");
  932. }
  933. }
  934. internal static short GetClientCertificateType(Certificate clientCertificate, Certificate serverCertificate)
  935. {
  936. if (clientCertificate.IsEmpty)
  937. return -1;
  938. X509CertificateStructure x509Cert = clientCertificate.GetCertificateAt(0);
  939. SubjectPublicKeyInfo keyInfo = x509Cert.SubjectPublicKeyInfo;
  940. try
  941. {
  942. AsymmetricKeyParameter publicKey = PublicKeyFactory.CreateKey(keyInfo);
  943. if (publicKey.IsPrivate)
  944. throw new TlsFatalAlert(AlertDescription.internal_error);
  945. /*
  946. * TODO RFC 5246 7.4.6. The certificates MUST be signed using an acceptable hash/
  947. * signature algorithm pair, as described in Section 7.4.4. Note that this relaxes the
  948. * constraints on certificate-signing algorithms found in prior versions of TLS.
  949. */
  950. /*
  951. * RFC 5246 7.4.6. Client Certificate
  952. */
  953. /*
  954. * RSA public key; the certificate MUST allow the key to be used for signing with the
  955. * signature scheme and hash algorithm that will be employed in the certificate verify
  956. * message.
  957. */
  958. if (publicKey is RsaKeyParameters)
  959. {
  960. ValidateKeyUsage(x509Cert, KeyUsage.DigitalSignature);
  961. return ClientCertificateType.rsa_sign;
  962. }
  963. /*
  964. * DSA public key; the certificate MUST allow the key to be used for signing with the
  965. * hash algorithm that will be employed in the certificate verify message.
  966. */
  967. if (publicKey is DsaPublicKeyParameters)
  968. {
  969. ValidateKeyUsage(x509Cert, KeyUsage.DigitalSignature);
  970. return ClientCertificateType.dss_sign;
  971. }
  972. /*
  973. * ECDSA-capable public key; the certificate MUST allow the key to be used for signing
  974. * with the hash algorithm that will be employed in the certificate verify message; the
  975. * public key MUST use a curve and point format supported by the server.
  976. */
  977. if (publicKey is ECPublicKeyParameters)
  978. {
  979. ValidateKeyUsage(x509Cert, KeyUsage.DigitalSignature);
  980. // TODO Check the curve and point format
  981. return ClientCertificateType.ecdsa_sign;
  982. }
  983. // TODO Add support for ClientCertificateType.*_fixed_*
  984. throw new TlsFatalAlert(AlertDescription.unsupported_certificate);
  985. }
  986. catch (Exception e)
  987. {
  988. throw new TlsFatalAlert(AlertDescription.unsupported_certificate, e);
  989. }
  990. }
  991. internal static void TrackHashAlgorithms(TlsHandshakeHash handshakeHash, IList supportedSignatureAlgorithms)
  992. {
  993. if (supportedSignatureAlgorithms != null)
  994. {
  995. foreach (SignatureAndHashAlgorithm signatureAndHashAlgorithm in supportedSignatureAlgorithms)
  996. {
  997. byte hashAlgorithm = signatureAndHashAlgorithm.Hash;
  998. // TODO Support values in the "Reserved for Private Use" range
  999. if (!HashAlgorithm.IsPrivate(hashAlgorithm))
  1000. {
  1001. handshakeHash.TrackHashAlgorithm(hashAlgorithm);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. public static bool HasSigningCapability(byte clientCertificateType)
  1007. {
  1008. switch (clientCertificateType)
  1009. {
  1010. case ClientCertificateType.dss_sign:
  1011. case ClientCertificateType.ecdsa_sign:
  1012. case ClientCertificateType.rsa_sign:
  1013. return true;
  1014. default:
  1015. return false;
  1016. }
  1017. }
  1018. public static TlsSigner CreateTlsSigner(byte clientCertificateType)
  1019. {
  1020. switch (clientCertificateType)
  1021. {
  1022. case ClientCertificateType.dss_sign:
  1023. return new TlsDssSigner();
  1024. case ClientCertificateType.ecdsa_sign:
  1025. return new TlsECDsaSigner();
  1026. case ClientCertificateType.rsa_sign:
  1027. return new TlsRsaSigner();
  1028. default:
  1029. throw new ArgumentException("not a type with signing capability", "clientCertificateType");
  1030. }
  1031. }
  1032. internal static readonly byte[] SSL_CLIENT = {0x43, 0x4C, 0x4E, 0x54};
  1033. internal static readonly byte[] SSL_SERVER = {0x53, 0x52, 0x56, 0x52};
  1034. // SSL3 magic mix constants ("A", "BB", "CCC", ...)
  1035. internal static readonly byte[][] SSL3_CONST = GenSsl3Const();
  1036. private static byte[][] GenSsl3Const()
  1037. {
  1038. int n = 10;
  1039. byte[][] arr = new byte[n][];
  1040. for (int i = 0; i < n; i++)
  1041. {
  1042. byte[] b = new byte[i + 1];
  1043. Arrays.Fill(b, (byte)('A' + i));
  1044. arr[i] = b;
  1045. }
  1046. return arr;
  1047. }
  1048. private static IList VectorOfOne(object obj)
  1049. {
  1050. IList v = Org.BouncyCastle.Utilities.Platform.CreateArrayList(1);
  1051. v.Add(obj);
  1052. return v;
  1053. }
  1054. public static int GetCipherType(int ciphersuite)
  1055. {
  1056. switch (GetEncryptionAlgorithm(ciphersuite))
  1057. {
  1058. case EncryptionAlgorithm.AES_128_CCM:
  1059. case EncryptionAlgorithm.AES_128_CCM_8:
  1060. case EncryptionAlgorithm.AES_128_GCM:
  1061. case EncryptionAlgorithm.AES_128_OCB_TAGLEN96:
  1062. case EncryptionAlgorithm.AES_256_CCM:
  1063. case EncryptionAlgorithm.AES_256_CCM_8:
  1064. case EncryptionAlgorithm.AES_256_GCM:
  1065. case EncryptionAlgorithm.AES_256_OCB_TAGLEN96:
  1066. case EncryptionAlgorithm.CAMELLIA_128_GCM:
  1067. case EncryptionAlgorithm.CAMELLIA_256_GCM:
  1068. case EncryptionAlgorithm.CHACHA20_POLY1305:
  1069. return CipherType.aead;
  1070. case EncryptionAlgorithm.RC2_CBC_40:
  1071. case EncryptionAlgorithm.IDEA_CBC:
  1072. case EncryptionAlgorithm.DES40_CBC:
  1073. case EncryptionAlgorithm.DES_CBC:
  1074. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  1075. case EncryptionAlgorithm.AES_128_CBC:
  1076. case EncryptionAlgorithm.AES_256_CBC:
  1077. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  1078. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  1079. case EncryptionAlgorithm.SEED_CBC:
  1080. return CipherType.block;
  1081. case EncryptionAlgorithm.RC4_40:
  1082. case EncryptionAlgorithm.RC4_128:
  1083. return CipherType.stream;
  1084. default:
  1085. throw new TlsFatalAlert(AlertDescription.internal_error);
  1086. }
  1087. }
  1088. public static int GetEncryptionAlgorithm(int ciphersuite)
  1089. {
  1090. switch (ciphersuite)
  1091. {
  1092. case CipherSuite.TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  1093. case CipherSuite.TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  1094. case CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  1095. case CipherSuite.TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1096. case CipherSuite.TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1097. case CipherSuite.TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA:
  1098. case CipherSuite.TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1099. case CipherSuite.TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  1100. case CipherSuite.TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1101. case CipherSuite.TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1102. case CipherSuite.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1103. case CipherSuite.TLS_PSK_WITH_3DES_EDE_CBC_SHA:
  1104. case CipherSuite.TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA:
  1105. case CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  1106. case CipherSuite.TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
  1107. case CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
  1108. case CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
  1109. return EncryptionAlgorithm.cls_3DES_EDE_CBC;
  1110. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  1111. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  1112. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  1113. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  1114. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  1115. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  1116. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
  1117. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
  1118. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  1119. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  1120. case CipherSuite.TLS_ECDH_anon_WITH_AES_128_CBC_SHA:
  1121. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  1122. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  1123. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  1124. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  1125. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  1126. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  1127. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA:
  1128. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256:
  1129. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  1130. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  1131. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA:
  1132. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256:
  1133. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA:
  1134. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA256:
  1135. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA:
  1136. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256:
  1137. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
  1138. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
  1139. case CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
  1140. return EncryptionAlgorithm.AES_128_CBC;
  1141. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CCM:
  1142. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM:
  1143. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM:
  1144. case CipherSuite.TLS_PSK_WITH_AES_128_CCM:
  1145. case CipherSuite.TLS_RSA_WITH_AES_128_CCM:
  1146. return EncryptionAlgorithm.AES_128_CCM;
  1147. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM_8:
  1148. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  1149. case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
  1150. case CipherSuite.TLS_PSK_WITH_AES_128_CCM_8:
  1151. case CipherSuite.TLS_RSA_WITH_AES_128_CCM_8:
  1152. return EncryptionAlgorithm.AES_128_CCM_8;
  1153. case CipherSuite.TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  1154. case CipherSuite.TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  1155. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  1156. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  1157. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  1158. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  1159. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  1160. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  1161. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  1162. case CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256:
  1163. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_GCM_SHA256:
  1164. case CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256:
  1165. return EncryptionAlgorithm.AES_128_GCM;
  1166. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_128_OCB:
  1167. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_128_OCB:
  1168. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_128_OCB:
  1169. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_128_OCB:
  1170. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_128_OCB:
  1171. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_128_OCB:
  1172. return EncryptionAlgorithm.AES_128_OCB_TAGLEN96;
  1173. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  1174. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  1175. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  1176. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  1177. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  1178. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  1179. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
  1180. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
  1181. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  1182. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  1183. case CipherSuite.TLS_ECDH_anon_WITH_AES_256_CBC_SHA:
  1184. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  1185. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  1186. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  1187. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  1188. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  1189. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  1190. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA:
  1191. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384:
  1192. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  1193. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  1194. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA:
  1195. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA384:
  1196. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA:
  1197. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA384:
  1198. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA:
  1199. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256:
  1200. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
  1201. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
  1202. case CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
  1203. return EncryptionAlgorithm.AES_256_CBC;
  1204. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CCM:
  1205. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM:
  1206. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM:
  1207. case CipherSuite.TLS_PSK_WITH_AES_256_CCM:
  1208. case CipherSuite.TLS_RSA_WITH_AES_256_CCM:
  1209. return EncryptionAlgorithm.AES_256_CCM;
  1210. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM_8:
  1211. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  1212. case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
  1213. case CipherSuite.TLS_PSK_WITH_AES_256_CCM_8:
  1214. case CipherSuite.TLS_RSA_WITH_AES_256_CCM_8:
  1215. return EncryptionAlgorithm.AES_256_CCM_8;
  1216. case CipherSuite.TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  1217. case CipherSuite.TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  1218. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  1219. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  1220. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  1221. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  1222. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  1223. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  1224. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  1225. case CipherSuite.TLS_PSK_WITH_AES_256_GCM_SHA384:
  1226. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_GCM_SHA384:
  1227. case CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384:
  1228. return EncryptionAlgorithm.AES_256_GCM;
  1229. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_256_OCB:
  1230. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_256_OCB:
  1231. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_256_OCB:
  1232. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_256_OCB:
  1233. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_256_OCB:
  1234. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_256_OCB:
  1235. return EncryptionAlgorithm.AES_256_OCB_TAGLEN96;
  1236. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1237. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1238. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1239. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1240. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1241. return EncryptionAlgorithm.CAMELLIA_128_CBC;
  1242. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1243. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1244. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1245. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1246. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1247. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1248. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1249. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1250. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1251. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1252. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1253. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1254. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1255. return EncryptionAlgorithm.CAMELLIA_128_CBC;
  1256. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1257. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1258. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1259. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1260. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1261. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1262. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1263. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1264. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1265. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1266. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1267. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1268. return EncryptionAlgorithm.CAMELLIA_128_GCM;
  1269. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1270. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1271. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1272. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1273. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1274. return EncryptionAlgorithm.CAMELLIA_256_CBC;
  1275. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1276. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1277. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1278. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1279. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1280. return EncryptionAlgorithm.CAMELLIA_256_CBC;
  1281. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1282. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1283. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1284. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1285. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1286. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1287. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1288. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1289. return EncryptionAlgorithm.CAMELLIA_256_CBC;
  1290. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1291. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1292. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1293. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1294. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1295. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1296. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1297. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1298. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1299. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1300. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1301. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1302. return EncryptionAlgorithm.CAMELLIA_256_GCM;
  1303. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1304. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1305. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
  1306. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1307. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1308. case CipherSuite.DRAFT_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1309. case CipherSuite.DRAFT_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1310. return EncryptionAlgorithm.CHACHA20_POLY1305;
  1311. case CipherSuite.TLS_RSA_WITH_NULL_MD5:
  1312. return EncryptionAlgorithm.NULL;
  1313. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA:
  1314. case CipherSuite.TLS_ECDH_anon_WITH_NULL_SHA:
  1315. case CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA:
  1316. case CipherSuite.TLS_ECDH_RSA_WITH_NULL_SHA:
  1317. case CipherSuite.TLS_ECDHE_ECDSA_WITH_NULL_SHA:
  1318. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA:
  1319. case CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA:
  1320. case CipherSuite.TLS_PSK_WITH_NULL_SHA:
  1321. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA:
  1322. case CipherSuite.TLS_RSA_WITH_NULL_SHA:
  1323. return EncryptionAlgorithm.NULL;
  1324. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA256:
  1325. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA256:
  1326. case CipherSuite.TLS_PSK_WITH_NULL_SHA256:
  1327. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA256:
  1328. case CipherSuite.TLS_RSA_WITH_NULL_SHA256:
  1329. return EncryptionAlgorithm.NULL;
  1330. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384:
  1331. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA384:
  1332. case CipherSuite.TLS_PSK_WITH_NULL_SHA384:
  1333. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA384:
  1334. return EncryptionAlgorithm.NULL;
  1335. case CipherSuite.TLS_DH_anon_WITH_RC4_128_MD5:
  1336. case CipherSuite.TLS_RSA_WITH_RC4_128_MD5:
  1337. return EncryptionAlgorithm.RC4_128;
  1338. case CipherSuite.TLS_DHE_PSK_WITH_RC4_128_SHA:
  1339. case CipherSuite.TLS_ECDH_anon_WITH_RC4_128_SHA:
  1340. case CipherSuite.TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  1341. case CipherSuite.TLS_ECDH_RSA_WITH_RC4_128_SHA:
  1342. case CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  1343. case CipherSuite.TLS_ECDHE_PSK_WITH_RC4_128_SHA:
  1344. case CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  1345. case CipherSuite.TLS_PSK_WITH_RC4_128_SHA:
  1346. case CipherSuite.TLS_RSA_WITH_RC4_128_SHA:
  1347. case CipherSuite.TLS_RSA_PSK_WITH_RC4_128_SHA:
  1348. return EncryptionAlgorithm.RC4_128;
  1349. case CipherSuite.TLS_DH_DSS_WITH_SEED_CBC_SHA:
  1350. case CipherSuite.TLS_DH_RSA_WITH_SEED_CBC_SHA:
  1351. case CipherSuite.TLS_DHE_DSS_WITH_SEED_CBC_SHA:
  1352. case CipherSuite.TLS_DHE_RSA_WITH_SEED_CBC_SHA:
  1353. case CipherSuite.TLS_RSA_WITH_SEED_CBC_SHA:
  1354. return EncryptionAlgorithm.SEED_CBC;
  1355. default:
  1356. throw new TlsFatalAlert(AlertDescription.internal_error);
  1357. }
  1358. }
  1359. public static int GetKeyExchangeAlgorithm(int ciphersuite)
  1360. {
  1361. switch (ciphersuite)
  1362. {
  1363. case CipherSuite.TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  1364. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  1365. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  1366. case CipherSuite.TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  1367. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  1368. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  1369. case CipherSuite.TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  1370. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1371. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1372. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1373. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1374. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1375. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1376. case CipherSuite.TLS_DH_DSS_WITH_SEED_CBC_SHA:
  1377. return KeyExchangeAlgorithm.DH_DSS;
  1378. case CipherSuite.TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  1379. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  1380. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  1381. case CipherSuite.TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  1382. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  1383. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  1384. case CipherSuite.TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  1385. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1386. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1387. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1388. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1389. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1390. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1391. case CipherSuite.TLS_DH_RSA_WITH_SEED_CBC_SHA:
  1392. return KeyExchangeAlgorithm.DH_RSA;
  1393. case CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  1394. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  1395. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  1396. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  1397. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  1398. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  1399. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  1400. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1401. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1402. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1403. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1404. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1405. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1406. case CipherSuite.TLS_DHE_DSS_WITH_SEED_CBC_SHA:
  1407. return KeyExchangeAlgorithm.DHE_DSS;
  1408. case CipherSuite.TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1409. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
  1410. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
  1411. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CCM:
  1412. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  1413. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_128_OCB:
  1414. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
  1415. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
  1416. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CCM:
  1417. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  1418. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_256_OCB:
  1419. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1420. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1421. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1422. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1423. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1424. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA:
  1425. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA256:
  1426. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384:
  1427. case CipherSuite.TLS_DHE_PSK_WITH_RC4_128_SHA:
  1428. case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
  1429. case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
  1430. return KeyExchangeAlgorithm.DHE_PSK;
  1431. case CipherSuite.TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1432. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  1433. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  1434. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM:
  1435. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM_8:
  1436. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  1437. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_128_OCB:
  1438. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  1439. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  1440. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM:
  1441. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM_8:
  1442. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  1443. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_256_OCB:
  1444. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1445. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1446. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1447. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1448. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1449. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1450. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1451. case CipherSuite.TLS_DHE_RSA_WITH_SEED_CBC_SHA:
  1452. return KeyExchangeAlgorithm.DHE_RSA;
  1453. case CipherSuite.TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA:
  1454. case CipherSuite.TLS_ECDH_anon_WITH_AES_128_CBC_SHA:
  1455. case CipherSuite.TLS_ECDH_anon_WITH_AES_256_CBC_SHA:
  1456. case CipherSuite.TLS_ECDH_anon_WITH_NULL_SHA:
  1457. case CipherSuite.TLS_ECDH_anon_WITH_RC4_128_SHA:
  1458. return KeyExchangeAlgorithm.ECDH_anon;
  1459. case CipherSuite.TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1460. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  1461. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  1462. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  1463. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_128_OCB:
  1464. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  1465. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  1466. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  1467. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_256_OCB:
  1468. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1469. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1470. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1471. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1472. case CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA:
  1473. case CipherSuite.TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  1474. return KeyExchangeAlgorithm.ECDH_ECDSA;
  1475. case CipherSuite.TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  1476. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  1477. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  1478. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  1479. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  1480. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  1481. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  1482. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1483. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1484. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1485. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1486. case CipherSuite.TLS_ECDH_RSA_WITH_NULL_SHA:
  1487. case CipherSuite.TLS_ECDH_RSA_WITH_RC4_128_SHA:
  1488. return KeyExchangeAlgorithm.ECDH_RSA;
  1489. case CipherSuite.TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1490. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  1491. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  1492. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM:
  1493. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  1494. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  1495. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  1496. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  1497. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM:
  1498. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  1499. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  1500. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1501. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1502. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1503. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1504. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
  1505. case CipherSuite.TLS_ECDHE_ECDSA_WITH_NULL_SHA:
  1506. case CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  1507. return KeyExchangeAlgorithm.ECDHE_ECDSA;
  1508. case CipherSuite.TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1509. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA:
  1510. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256:
  1511. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_128_OCB:
  1512. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA:
  1513. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384:
  1514. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_256_OCB:
  1515. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1516. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1517. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1518. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA:
  1519. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA256:
  1520. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA384:
  1521. case CipherSuite.TLS_ECDHE_PSK_WITH_RC4_128_SHA:
  1522. return KeyExchangeAlgorithm.ECDHE_PSK;
  1523. case CipherSuite.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1524. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  1525. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  1526. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  1527. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_128_OCB:
  1528. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  1529. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  1530. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  1531. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_256_OCB:
  1532. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1533. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1534. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1535. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1536. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1537. case CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA:
  1538. case CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  1539. return KeyExchangeAlgorithm.ECDHE_RSA;
  1540. case CipherSuite.TLS_PSK_WITH_3DES_EDE_CBC_SHA:
  1541. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA:
  1542. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256:
  1543. case CipherSuite.TLS_PSK_WITH_AES_128_CCM:
  1544. case CipherSuite.TLS_PSK_WITH_AES_128_CCM_8:
  1545. case CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256:
  1546. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_128_OCB:
  1547. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA:
  1548. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA384:
  1549. case CipherSuite.TLS_PSK_WITH_AES_256_CCM:
  1550. case CipherSuite.TLS_PSK_WITH_AES_256_CCM_8:
  1551. case CipherSuite.TLS_PSK_WITH_AES_256_GCM_SHA384:
  1552. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_256_OCB:
  1553. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1554. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1555. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1556. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1557. case CipherSuite.DRAFT_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1558. case CipherSuite.TLS_PSK_WITH_NULL_SHA:
  1559. case CipherSuite.TLS_PSK_WITH_NULL_SHA256:
  1560. case CipherSuite.TLS_PSK_WITH_NULL_SHA384:
  1561. case CipherSuite.TLS_PSK_WITH_RC4_128_SHA:
  1562. return KeyExchangeAlgorithm.PSK;
  1563. case CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  1564. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA:
  1565. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256:
  1566. case CipherSuite.TLS_RSA_WITH_AES_128_CCM:
  1567. case CipherSuite.TLS_RSA_WITH_AES_128_CCM_8:
  1568. case CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256:
  1569. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA:
  1570. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256:
  1571. case CipherSuite.TLS_RSA_WITH_AES_256_CCM:
  1572. case CipherSuite.TLS_RSA_WITH_AES_256_CCM_8:
  1573. case CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384:
  1574. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1575. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1576. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1577. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1578. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1579. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1580. case CipherSuite.DRAFT_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1581. case CipherSuite.TLS_RSA_WITH_NULL_MD5:
  1582. case CipherSuite.TLS_RSA_WITH_NULL_SHA:
  1583. case CipherSuite.TLS_RSA_WITH_NULL_SHA256:
  1584. case CipherSuite.TLS_RSA_WITH_RC4_128_MD5:
  1585. case CipherSuite.TLS_RSA_WITH_RC4_128_SHA:
  1586. case CipherSuite.TLS_RSA_WITH_SEED_CBC_SHA:
  1587. return KeyExchangeAlgorithm.RSA;
  1588. case CipherSuite.TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA:
  1589. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA:
  1590. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA256:
  1591. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_GCM_SHA256:
  1592. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA:
  1593. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA384:
  1594. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_GCM_SHA384:
  1595. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1596. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1597. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1598. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1599. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA:
  1600. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA256:
  1601. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA384:
  1602. case CipherSuite.TLS_RSA_PSK_WITH_RC4_128_SHA:
  1603. return KeyExchangeAlgorithm.RSA_PSK;
  1604. case CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
  1605. case CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
  1606. case CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
  1607. return KeyExchangeAlgorithm.SRP;
  1608. case CipherSuite.TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
  1609. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
  1610. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
  1611. return KeyExchangeAlgorithm.SRP_DSS;
  1612. case CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
  1613. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
  1614. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
  1615. return KeyExchangeAlgorithm.SRP_RSA;
  1616. default:
  1617. throw new TlsFatalAlert(AlertDescription.internal_error);
  1618. }
  1619. }
  1620. public static int GetMacAlgorithm(int ciphersuite)
  1621. {
  1622. switch (ciphersuite)
  1623. {
  1624. case CipherSuite.TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  1625. case CipherSuite.TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  1626. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1627. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1628. case CipherSuite.TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  1629. case CipherSuite.TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  1630. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1631. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1632. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  1633. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  1634. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1635. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1636. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CCM:
  1637. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  1638. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_128_OCB:
  1639. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CCM:
  1640. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  1641. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_256_OCB:
  1642. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1643. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1644. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1645. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM:
  1646. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM_8:
  1647. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  1648. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_128_OCB:
  1649. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM:
  1650. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM_8:
  1651. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  1652. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_256_OCB:
  1653. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1654. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1655. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1656. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  1657. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  1658. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1659. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1660. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  1661. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  1662. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1663. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1664. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM:
  1665. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  1666. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  1667. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_128_OCB:
  1668. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM:
  1669. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  1670. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  1671. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_256_OCB:
  1672. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1673. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1674. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
  1675. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_128_OCB:
  1676. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_256_OCB:
  1677. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1678. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  1679. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_128_OCB:
  1680. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  1681. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_256_OCB:
  1682. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1683. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1684. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1685. case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
  1686. case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
  1687. case CipherSuite.TLS_PSK_WITH_AES_128_CCM:
  1688. case CipherSuite.TLS_PSK_WITH_AES_128_CCM_8:
  1689. case CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256:
  1690. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_128_OCB:
  1691. case CipherSuite.TLS_PSK_WITH_AES_256_CCM:
  1692. case CipherSuite.TLS_PSK_WITH_AES_256_CCM_8:
  1693. case CipherSuite.TLS_PSK_WITH_AES_256_GCM_SHA384:
  1694. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_256_OCB:
  1695. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1696. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1697. case CipherSuite.DRAFT_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1698. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_GCM_SHA256:
  1699. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_GCM_SHA384:
  1700. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1701. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1702. case CipherSuite.DRAFT_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1703. case CipherSuite.TLS_RSA_WITH_AES_128_CCM:
  1704. case CipherSuite.TLS_RSA_WITH_AES_128_CCM_8:
  1705. case CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256:
  1706. case CipherSuite.TLS_RSA_WITH_AES_256_CCM:
  1707. case CipherSuite.TLS_RSA_WITH_AES_256_CCM_8:
  1708. case CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384:
  1709. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1710. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1711. return MacAlgorithm.cls_null;
  1712. case CipherSuite.TLS_RSA_WITH_NULL_MD5:
  1713. case CipherSuite.TLS_RSA_WITH_RC4_128_MD5:
  1714. return MacAlgorithm.hmac_md5;
  1715. case CipherSuite.TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  1716. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  1717. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  1718. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1719. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1720. case CipherSuite.TLS_DH_DSS_WITH_SEED_CBC_SHA:
  1721. case CipherSuite.TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  1722. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  1723. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  1724. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1725. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1726. case CipherSuite.TLS_DH_RSA_WITH_SEED_CBC_SHA:
  1727. case CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  1728. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  1729. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  1730. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA:
  1731. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA:
  1732. case CipherSuite.TLS_DHE_DSS_WITH_SEED_CBC_SHA:
  1733. case CipherSuite.TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1734. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
  1735. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
  1736. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA:
  1737. case CipherSuite.TLS_DHE_PSK_WITH_RC4_128_SHA:
  1738. case CipherSuite.TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1739. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  1740. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  1741. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1742. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1743. case CipherSuite.TLS_DHE_RSA_WITH_SEED_CBC_SHA:
  1744. case CipherSuite.TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA:
  1745. case CipherSuite.TLS_ECDH_anon_WITH_AES_128_CBC_SHA:
  1746. case CipherSuite.TLS_ECDH_anon_WITH_AES_256_CBC_SHA:
  1747. case CipherSuite.TLS_ECDH_anon_WITH_NULL_SHA:
  1748. case CipherSuite.TLS_ECDH_anon_WITH_RC4_128_SHA:
  1749. case CipherSuite.TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1750. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  1751. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  1752. case CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA:
  1753. case CipherSuite.TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  1754. case CipherSuite.TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  1755. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  1756. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  1757. case CipherSuite.TLS_ECDH_RSA_WITH_NULL_SHA:
  1758. case CipherSuite.TLS_ECDH_RSA_WITH_RC4_128_SHA:
  1759. case CipherSuite.TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  1760. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  1761. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  1762. case CipherSuite.TLS_ECDHE_ECDSA_WITH_NULL_SHA:
  1763. case CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  1764. case CipherSuite.TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA:
  1765. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA:
  1766. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA:
  1767. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA:
  1768. case CipherSuite.TLS_ECDHE_PSK_WITH_RC4_128_SHA:
  1769. case CipherSuite.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  1770. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  1771. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  1772. case CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA:
  1773. case CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  1774. case CipherSuite.TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA:
  1775. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA:
  1776. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA:
  1777. case CipherSuite.TLS_PSK_WITH_3DES_EDE_CBC_SHA:
  1778. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA:
  1779. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA:
  1780. case CipherSuite.TLS_PSK_WITH_NULL_SHA:
  1781. case CipherSuite.TLS_PSK_WITH_RC4_128_SHA:
  1782. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA:
  1783. case CipherSuite.TLS_RSA_PSK_WITH_RC4_128_SHA:
  1784. case CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  1785. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA:
  1786. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA:
  1787. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA:
  1788. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA:
  1789. case CipherSuite.TLS_RSA_WITH_NULL_SHA:
  1790. case CipherSuite.TLS_RSA_WITH_RC4_128_SHA:
  1791. case CipherSuite.TLS_RSA_WITH_SEED_CBC_SHA:
  1792. case CipherSuite.TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
  1793. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
  1794. case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
  1795. case CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
  1796. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
  1797. case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
  1798. case CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
  1799. case CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
  1800. case CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
  1801. return MacAlgorithm.hmac_sha1;
  1802. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  1803. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  1804. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1805. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1806. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  1807. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  1808. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1809. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1810. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  1811. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  1812. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1813. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1814. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
  1815. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1816. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA256:
  1817. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  1818. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  1819. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1820. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1821. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  1822. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1823. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  1824. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1825. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  1826. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1827. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256:
  1828. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1829. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA256:
  1830. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  1831. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1832. case CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256:
  1833. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1834. case CipherSuite.TLS_PSK_WITH_NULL_SHA256:
  1835. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_CBC_SHA256:
  1836. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256:
  1837. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA256:
  1838. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256:
  1839. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256:
  1840. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1841. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1842. case CipherSuite.TLS_RSA_WITH_NULL_SHA256:
  1843. return MacAlgorithm.hmac_sha256;
  1844. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
  1845. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1846. case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384:
  1847. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  1848. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1849. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  1850. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1851. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  1852. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1853. case CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384:
  1854. case CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1855. case CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA384:
  1856. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  1857. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1858. case CipherSuite.TLS_PSK_WITH_AES_256_CBC_SHA384:
  1859. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1860. case CipherSuite.TLS_PSK_WITH_NULL_SHA384:
  1861. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA384:
  1862. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384:
  1863. case CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA384:
  1864. return MacAlgorithm.hmac_sha384;
  1865. default:
  1866. throw new TlsFatalAlert(AlertDescription.internal_error);
  1867. }
  1868. }
  1869. public static ProtocolVersion GetMinimumVersion(int ciphersuite)
  1870. {
  1871. switch (ciphersuite)
  1872. {
  1873. case CipherSuite.TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256:
  1874. case CipherSuite.TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256:
  1875. case CipherSuite.TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256:
  1876. case CipherSuite.TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384:
  1877. case CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  1878. case CipherSuite.TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  1879. case CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  1880. case CipherSuite.TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  1881. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1882. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1883. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1884. case CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1885. case CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  1886. case CipherSuite.TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  1887. case CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  1888. case CipherSuite.TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  1889. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1890. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1891. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1892. case CipherSuite.TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1893. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  1894. case CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  1895. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  1896. case CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  1897. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256:
  1898. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256:
  1899. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256:
  1900. case CipherSuite.TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384:
  1901. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_CCM:
  1902. case CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  1903. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_128_OCB:
  1904. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CCM:
  1905. case CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  1906. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_AES_256_OCB:
  1907. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1908. case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1909. case CipherSuite.DRAFT_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1910. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  1911. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM:
  1912. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_CCM_8:
  1913. case CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  1914. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_128_OCB:
  1915. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  1916. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM:
  1917. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_CCM_8:
  1918. case CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  1919. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_AES_256_OCB:
  1920. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1921. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1922. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1923. case CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1924. case CipherSuite.DRAFT_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1925. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  1926. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  1927. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  1928. case CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  1929. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1930. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1931. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1932. case CipherSuite.TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1933. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  1934. case CipherSuite.TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  1935. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  1936. case CipherSuite.TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  1937. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1938. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1939. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1940. case CipherSuite.TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1941. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  1942. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM:
  1943. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  1944. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  1945. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_128_OCB:
  1946. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  1947. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM:
  1948. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  1949. case CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  1950. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_AES_256_OCB:
  1951. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256:
  1952. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256:
  1953. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384:
  1954. case CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384:
  1955. case CipherSuite.DRAFT_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
  1956. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_128_OCB:
  1957. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_AES_256_OCB:
  1958. case CipherSuite.DRAFT_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1959. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  1960. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  1961. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_128_OCB:
  1962. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  1963. case CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  1964. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_AES_256_OCB:
  1965. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1966. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1967. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384:
  1968. case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  1969. case CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  1970. case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
  1971. case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
  1972. case CipherSuite.TLS_PSK_WITH_AES_128_CCM:
  1973. case CipherSuite.TLS_PSK_WITH_AES_128_CCM_8:
  1974. case CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256:
  1975. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_128_OCB:
  1976. case CipherSuite.TLS_PSK_WITH_AES_256_CCM:
  1977. case CipherSuite.TLS_PSK_WITH_AES_256_CCM_8:
  1978. case CipherSuite.TLS_PSK_WITH_AES_256_GCM_SHA384:
  1979. case CipherSuite.DRAFT_TLS_PSK_WITH_AES_256_OCB:
  1980. case CipherSuite.TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1981. case CipherSuite.TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1982. case CipherSuite.DRAFT_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1983. case CipherSuite.TLS_RSA_PSK_WITH_AES_128_GCM_SHA256:
  1984. case CipherSuite.TLS_RSA_PSK_WITH_AES_256_GCM_SHA384:
  1985. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256:
  1986. case CipherSuite.TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384:
  1987. case CipherSuite.DRAFT_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256:
  1988. case CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256:
  1989. case CipherSuite.TLS_RSA_WITH_AES_128_CCM:
  1990. case CipherSuite.TLS_RSA_WITH_AES_128_CCM_8:
  1991. case CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256:
  1992. case CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256:
  1993. case CipherSuite.TLS_RSA_WITH_AES_256_CCM:
  1994. case CipherSuite.TLS_RSA_WITH_AES_256_CCM_8:
  1995. case CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384:
  1996. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256:
  1997. case CipherSuite.TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256:
  1998. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256:
  1999. case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384:
  2000. case CipherSuite.TLS_RSA_WITH_NULL_SHA256:
  2001. return ProtocolVersion.TLSv12;
  2002. default:
  2003. return ProtocolVersion.SSLv3;
  2004. }
  2005. }
  2006. public static bool IsAeadCipherSuite(int ciphersuite)
  2007. {
  2008. return CipherType.aead == GetCipherType(ciphersuite);
  2009. }
  2010. public static bool IsBlockCipherSuite(int ciphersuite)
  2011. {
  2012. return CipherType.block == GetCipherType(ciphersuite);
  2013. }
  2014. public static bool IsStreamCipherSuite(int ciphersuite)
  2015. {
  2016. return CipherType.stream == GetCipherType(ciphersuite);
  2017. }
  2018. public static bool IsValidCipherSuiteForVersion(int cipherSuite, ProtocolVersion serverVersion)
  2019. {
  2020. return GetMinimumVersion(cipherSuite).IsEqualOrEarlierVersionOf(serverVersion.GetEquivalentTLSVersion());
  2021. }
  2022. }
  2023. }
  2024. #endif