tls-suites.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Embedded Linux library
  3. *
  4. * Copyright (C) 2018 Intel Corporation. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #define _GNU_SOURCE
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <strings.h>
  28. #include "util.h"
  29. #include "tls.h"
  30. #include "cipher.h"
  31. #include "checksum.h"
  32. #include "cert.h"
  33. #include "tls-private.h"
  34. #include "key.h"
  35. #include "random.h"
  36. #include "ecc.h"
  37. #include "ecdh.h"
  38. #include "missing.h"
  39. static bool tls_rsa_validate_cert_key(struct l_cert *cert)
  40. {
  41. return l_cert_get_pubkey_type(cert) == L_CERT_KEY_RSA;
  42. }
  43. static ssize_t tls_rsa_sign(struct l_tls *tls, uint8_t *out, size_t out_len,
  44. tls_get_hash_t get_hash,
  45. const uint8_t *data, size_t data_len)
  46. {
  47. ssize_t result = -EMSGSIZE;
  48. enum l_checksum_type sign_checksum_type;
  49. uint8_t sign_input[HANDSHAKE_HASH_MAX_SIZE + 36];
  50. size_t sign_input_len;
  51. uint8_t *ptr = out;
  52. if (!tls->priv_key || !tls->priv_key_size) {
  53. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, TLS_ALERT_BAD_CERT,
  54. "No private key loaded");
  55. return -ENOKEY;
  56. }
  57. if (tls->negotiated_version >= L_TLS_V12) {
  58. const struct tls_hash_algorithm *hash_type =
  59. &tls_handshake_hash_data[tls->signature_hash];
  60. /* Build the DigitallySigned struct */
  61. if (out_len < 2) /* Is there space for the algorithm IDs */
  62. goto error;
  63. get_hash(tls, tls->signature_hash, data, data_len,
  64. sign_input, &sign_input_len);
  65. sign_checksum_type = hash_type->l_id;
  66. *ptr++ = hash_type->tls_id;
  67. *ptr++ = 1; /* RSA_sign */
  68. out_len -= 2;
  69. } else {
  70. get_hash(tls, HANDSHAKE_HASH_MD5, data, data_len,
  71. sign_input + 0, NULL);
  72. get_hash(tls, HANDSHAKE_HASH_SHA1, data, data_len,
  73. sign_input + 16, NULL);
  74. sign_checksum_type = L_CHECKSUM_NONE;
  75. sign_input_len = 36;
  76. }
  77. if (out_len < tls->priv_key_size + 2)
  78. goto error;
  79. l_put_be16(tls->priv_key_size, ptr);
  80. result = l_key_sign(tls->priv_key, L_KEY_RSA_PKCS1_V1_5,
  81. sign_checksum_type, sign_input, ptr + 2,
  82. sign_input_len, tls->priv_key_size);
  83. ptr += tls->priv_key_size + 2;
  84. if (result == (ssize_t) tls->priv_key_size)
  85. return ptr - out; /* Success */
  86. error:
  87. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  88. "Signing the hash failed: %s",
  89. strerror(-result));
  90. return result;
  91. }
  92. static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t in_len,
  93. tls_get_hash_t get_hash,
  94. const uint8_t *data, size_t data_len)
  95. {
  96. enum l_checksum_type sign_checksum_type;
  97. uint8_t expected[HANDSHAKE_HASH_MAX_SIZE + 36];
  98. size_t expected_len;
  99. unsigned int offset;
  100. bool success;
  101. /* 2 bytes for SignatureAndHashAlgorithm if version >= 1.2 */
  102. offset = 2;
  103. if (tls->negotiated_version < L_TLS_V12)
  104. offset = 0;
  105. if (in_len < offset + 2 ||
  106. (size_t) l_get_be16(in + offset) + offset + 2 !=
  107. in_len) {
  108. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0, "Signature msg too "
  109. "short (%zi) or signature length doesn't match",
  110. in_len);
  111. return false;
  112. }
  113. /* Only the default hash type supported */
  114. if (in_len != offset + 2 + tls->peer_pubkey_size) {
  115. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  116. "Signature length %zi not equal %zi", in_len,
  117. offset + 2 + tls->peer_pubkey_size);
  118. return false;
  119. }
  120. if (tls->negotiated_version >= L_TLS_V12) {
  121. enum handshake_hash_type hash;
  122. /* Only RSA supported */
  123. if (in[1] != 1 /* RSA_sign */) {
  124. TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
  125. "Unknown signature algorithm %i",
  126. in[1]);
  127. return false;
  128. }
  129. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  130. if (tls_handshake_hash_data[hash].tls_id == in[0])
  131. break;
  132. if (hash == __HANDSHAKE_HASH_COUNT) {
  133. TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
  134. "Unknown hash type %i", in[0]);
  135. return false;
  136. }
  137. get_hash(tls, hash, data, data_len, expected, &expected_len);
  138. sign_checksum_type = tls_handshake_hash_data[hash].l_id;
  139. /*
  140. * Note: Next we let the l_key_verify's underlying kernel
  141. * operation prepend the OID to the hash to build the
  142. * DigestInfo struct. However according to 4.7 we need to
  143. * support at least two forms of the signed content in the
  144. * verification:
  145. * - DigestInfo with NULL AlgorithmIdentifier.parameters,
  146. * - DigestInfo with empty AlgorithmIdentifier.parameters,
  147. *
  148. * while the kernel only understands the former encoding.
  149. * Note PKCS#1 versions 2.0 and later section A.2.4 do
  150. * mandate NULL AlgorithmIdentifier.parameters.
  151. *
  152. * Additionally PKCS#1 v1.5 said BER is used in place of DER
  153. * for DigestInfo encoding which adds more ambiguity in the
  154. * encoding.
  155. */
  156. } else {
  157. get_hash(tls, HANDSHAKE_HASH_MD5, data, data_len,
  158. expected + 0, NULL);
  159. get_hash(tls, HANDSHAKE_HASH_SHA1, data, data_len,
  160. expected + 16, NULL);
  161. expected_len = 36;
  162. sign_checksum_type = L_CHECKSUM_NONE;
  163. /*
  164. * Note: Within the RSA padding for signatures PKCS#1 1.5
  165. * allows the block format to be either 0 or 1, while PKCS#1
  166. * v2.0+ mandates block type 1 making the signatures
  167. * unambiguous. TLS 1.0 doesn't additionally specify which
  168. * block type is to be used (TLS 1.2 does) meaning that both
  169. * PKCS#1 v1.5 types are allowed. The l_key_verify's
  170. * underlying kernel implementation only accepts block type
  171. * 1. If this ever becomes an issue we'd need to go back to
  172. * using L_KEY_RSA_RAW and our own PKCS#1 v1.5 verify logic.
  173. */
  174. }
  175. success = l_key_verify(tls->peer_pubkey, L_KEY_RSA_PKCS1_V1_5,
  176. sign_checksum_type, expected, in + offset + 2,
  177. expected_len, tls->peer_pubkey_size);
  178. if (!success)
  179. TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
  180. "Peer signature verification failed");
  181. else
  182. TLS_DEBUG("Peer signature verified");
  183. return success;
  184. }
  185. static struct tls_signature_algorithm tls_rsa_signature = {
  186. .id = 1, /* SignatureAlgorithm.rsa */
  187. .validate_cert_key_type = tls_rsa_validate_cert_key,
  188. .sign = tls_rsa_sign,
  189. .verify = tls_rsa_verify,
  190. };
  191. static bool tls_send_rsa_client_key_xchg(struct l_tls *tls)
  192. {
  193. uint8_t buf[1024 + 32];
  194. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  195. uint8_t pre_master_secret[48];
  196. ssize_t bytes_encrypted;
  197. if (!tls->peer_pubkey) {
  198. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  199. "Peer public key not received");
  200. return false;
  201. }
  202. /* Must match the version in tls_send_client_hello */
  203. pre_master_secret[0] = (uint8_t) (tls->max_version >> 8);
  204. pre_master_secret[1] = (uint8_t) (tls->max_version >> 0);
  205. l_getrandom(pre_master_secret + 2, 46);
  206. if (tls->peer_pubkey_size + 32 > (int) sizeof(buf)) {
  207. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  208. "Peer public key too big: %zi",
  209. tls->peer_pubkey_size);
  210. return false;
  211. }
  212. l_put_be16(tls->peer_pubkey_size, ptr);
  213. bytes_encrypted = l_key_encrypt(tls->peer_pubkey,
  214. L_KEY_RSA_PKCS1_V1_5, L_CHECKSUM_NONE,
  215. pre_master_secret, ptr + 2, 48,
  216. tls->peer_pubkey_size);
  217. ptr += tls->peer_pubkey_size + 2;
  218. if (bytes_encrypted != (ssize_t) tls->peer_pubkey_size) {
  219. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  220. "Encrypting PreMasterSecret failed: %s",
  221. strerror(-bytes_encrypted));
  222. return false;
  223. }
  224. tls_tx_handshake(tls, TLS_CLIENT_KEY_EXCHANGE, buf, ptr - buf);
  225. tls_generate_master_secret(tls, pre_master_secret, 48);
  226. explicit_bzero(pre_master_secret, 48);
  227. return true;
  228. }
  229. static void tls_handle_rsa_client_key_xchg(struct l_tls *tls,
  230. const uint8_t *buf, size_t len)
  231. {
  232. uint8_t pre_master_secret[48], random_secret[46];
  233. ssize_t bytes_decrypted;
  234. if (!tls->priv_key || !tls->priv_key_size) {
  235. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, TLS_ALERT_BAD_CERT,
  236. "No private key");
  237. return;
  238. }
  239. if (len != tls->priv_key_size + 2) {
  240. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  241. "ClientKeyExchange len %zi not %zi", len,
  242. tls->priv_key_size + 2);
  243. return;
  244. }
  245. len = l_get_be16(buf);
  246. if (len != tls->priv_key_size) {
  247. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  248. "EncryptedPreMasterSecret len %zi not %zi",
  249. len, tls->priv_key_size);
  250. return;
  251. }
  252. bytes_decrypted = l_key_decrypt(tls->priv_key, L_KEY_RSA_PKCS1_V1_5,
  253. L_CHECKSUM_NONE, buf + 2,
  254. pre_master_secret, tls->priv_key_size,
  255. 48);
  256. /*
  257. * Assume correct premaster secret client version which according
  258. * to the TLS1.2 spec is unlikely in client implementations SSLv3
  259. * and prior. Spec suggests either not supporting them or adding
  260. * a configurable override for <= SSLv3 clients. For now we have
  261. * no need to support them.
  262. *
  263. * On any decode error randomise the Pre Master Secret as per the
  264. * countermeasures in 7.4.7.1 and don't generate any alerts.
  265. */
  266. l_getrandom(random_secret, 46);
  267. pre_master_secret[0] = tls->client_version >> 8;
  268. pre_master_secret[1] = tls->client_version >> 0;
  269. if (bytes_decrypted != 48) {
  270. memcpy(pre_master_secret + 2, random_secret, 46);
  271. TLS_DEBUG("Error decrypting PreMasterSecret: %s",
  272. strerror(-bytes_decrypted));
  273. }
  274. tls_generate_master_secret(tls, pre_master_secret, 48);
  275. explicit_bzero(pre_master_secret, 48);
  276. explicit_bzero(random_secret, 46);
  277. }
  278. static struct tls_key_exchange_algorithm tls_rsa_key_xchg = {
  279. .send_client_key_exchange = tls_send_rsa_client_key_xchg,
  280. .handle_client_key_exchange = tls_handle_rsa_client_key_xchg,
  281. };
  282. /* Used by both DHE and ECDHE */
  283. static bool tls_get_dh_params_hash(struct l_tls *tls,
  284. enum handshake_hash_type type,
  285. const uint8_t *data, size_t data_len,
  286. uint8_t *out, size_t *out_len)
  287. {
  288. struct l_checksum *checksum;
  289. ssize_t ret;
  290. /*
  291. * The ServerKeyExchange signature hash input format for RSA_sign is
  292. * not really specified in either RFC 8422 or RFC 5246 explicitly
  293. * but we use this format by analogy to DHE_RSA which uses RSA_sign
  294. * as well. Also matches ecdsa, ed25519 and ed448 formats.
  295. */
  296. struct iovec iov[] = {
  297. { .iov_base = tls->pending.client_random, .iov_len = 32 },
  298. { .iov_base = tls->pending.server_random, .iov_len = 32 },
  299. { .iov_base = (void *) data, .iov_len = data_len },
  300. };
  301. checksum = l_checksum_new(tls_handshake_hash_data[type].l_id);
  302. if (!checksum)
  303. return false;
  304. l_checksum_updatev(checksum, iov, L_ARRAY_SIZE(iov));
  305. ret = l_checksum_get_digest(checksum, out, HANDSHAKE_HASH_MAX_SIZE);
  306. l_checksum_free(checksum);
  307. if (ret < 0)
  308. return false;
  309. if (out_len)
  310. *out_len = ret;
  311. return true;
  312. }
  313. struct tls_ecdhe_params {
  314. const struct l_ecc_curve *curve;
  315. struct l_ecc_scalar *private;
  316. struct l_ecc_point *public;
  317. };
  318. static void tls_free_ecdhe_params(struct l_tls *tls)
  319. {
  320. struct tls_ecdhe_params *params = tls->pending.key_xchg_params;
  321. if (!params)
  322. return;
  323. tls->pending.key_xchg_params = NULL;
  324. l_ecc_scalar_free(params->private);
  325. l_ecc_point_free(params->public);
  326. l_free(params);
  327. }
  328. static size_t tls_write_ecpoint(uint8_t *buf, size_t len,
  329. const struct tls_named_group *curve,
  330. const struct l_ecc_point *point)
  331. {
  332. size_t point_bytes;
  333. /* RFC 8422, Section 5.4.1 */
  334. point_bytes = l_ecc_point_get_data(point, buf + 2, len - 2);
  335. buf[0] = 1 + point_bytes; /* length */
  336. buf[1] = 4; /* form: uncompressed */
  337. return 2 + point_bytes;
  338. }
  339. static size_t tls_write_server_ecdh_params(struct l_tls *tls, uint8_t *buf, size_t len)
  340. {
  341. struct tls_ecdhe_params *params = tls->pending.key_xchg_params;
  342. /* RFC 8422, Section 5.4 */
  343. buf[0] = 3; /* curve_type: named_curve */
  344. l_put_be16(tls->negotiated_curve->id, buf + 1);
  345. return 3 + tls_write_ecpoint(buf + 3, len - 3, tls->negotiated_curve,
  346. params->public);
  347. }
  348. static bool tls_send_ecdhe_server_key_xchg(struct l_tls *tls)
  349. {
  350. uint8_t buf[1024];
  351. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  352. struct tls_ecdhe_params *params;
  353. ssize_t sign_len;
  354. const uint8_t *server_ecdh_params_ptr;
  355. /*
  356. * RFC 8422, Section 5.4
  357. *
  358. * If we're getting here we can assume that tls->pending.key_xchg_params
  359. * is NULL, tls->priv_key is our signing key and tls->negotiated_curve
  360. * is non-NULL.
  361. */
  362. params = l_new(struct tls_ecdhe_params, 1);
  363. params->curve = l_ecc_curve_from_tls_group(tls->negotiated_curve->id);
  364. tls->pending.key_xchg_params = params;
  365. if (!l_ecdh_generate_key_pair(params->curve,
  366. &params->private, &params->public)) {
  367. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  368. "Generating ECDH key pair failed");
  369. return false;
  370. }
  371. server_ecdh_params_ptr = ptr;
  372. ptr += tls_write_server_ecdh_params(tls, ptr, buf + sizeof(buf) - ptr);
  373. if (tls->pending.cipher_suite->signature) {
  374. sign_len = tls->pending.cipher_suite->signature->sign(tls, ptr,
  375. buf + sizeof(buf) - ptr,
  376. tls_get_dh_params_hash,
  377. server_ecdh_params_ptr,
  378. ptr - server_ecdh_params_ptr);
  379. if (sign_len < 0)
  380. return false;
  381. ptr += sign_len;
  382. }
  383. tls_tx_handshake(tls, TLS_SERVER_KEY_EXCHANGE, buf, ptr - buf);
  384. return true;
  385. }
  386. static void tls_handle_ecdhe_server_key_xchg(struct l_tls *tls,
  387. const uint8_t *buf, size_t len)
  388. {
  389. struct tls_ecdhe_params *params;
  390. uint16_t namedcurve;
  391. const uint8_t *server_ecdh_params_ptr = buf;
  392. size_t point_bytes;
  393. /* RFC 8422, Section 5.4 */
  394. if (len < 5)
  395. goto decode_error;
  396. if (*buf != 3) {
  397. TLS_DISCONNECT(TLS_ALERT_ILLEGAL_PARAM, 0,
  398. "Unsupported (deprecated?) ECCurveType %u",
  399. *buf);
  400. return;
  401. }
  402. namedcurve = l_get_be16(buf + 1);
  403. buf += 3;
  404. len -= 3;
  405. tls->negotiated_curve = tls_find_group(namedcurve);
  406. if (!tls->negotiated_curve ||
  407. tls->negotiated_curve->type != TLS_GROUP_TYPE_EC) {
  408. TLS_DISCONNECT(TLS_ALERT_ILLEGAL_PARAM, 0,
  409. "Unsupported NamedCurve %u", namedcurve);
  410. return;
  411. }
  412. TLS_DEBUG("Negotiated %s", tls->negotiated_curve->name);
  413. if (*buf < 1)
  414. goto decode_error;
  415. point_bytes = *buf++ - 1;
  416. if (*buf != 4) { /* uncompressed */
  417. TLS_DISCONNECT(TLS_ALERT_ILLEGAL_PARAM, 0,
  418. "Unsupported (deprecated?) PointConversionForm "
  419. "%u", *buf);
  420. return;
  421. }
  422. buf++;
  423. len -= 2;
  424. if (len < point_bytes)
  425. goto decode_error;
  426. /*
  427. * RFC 8422, Section 5.11: "A receiving party MUST check that the
  428. * x and y parameters from the peer's public value satisfy the
  429. * curve equation, y^2 = x^3 + ax + b mod p."
  430. * This happens in l_ecc_point_from_data when the L_ECC_POINT_TYPE_FULL
  431. * format is used.
  432. */
  433. params = l_new(struct tls_ecdhe_params, 1);
  434. params->curve = l_ecc_curve_from_tls_group(tls->negotiated_curve->id);
  435. params->public = l_ecc_point_from_data(params->curve,
  436. L_ECC_POINT_TYPE_FULL,
  437. buf, len);
  438. tls->pending.key_xchg_params = params;
  439. buf += point_bytes;
  440. len -= point_bytes;
  441. if (!params->public || point_bytes !=
  442. 2 * l_ecc_curve_get_scalar_bytes(params->curve)) {
  443. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  444. "ServerKeyExchange.params.public decode error");
  445. return;
  446. }
  447. if (tls->pending.cipher_suite->signature) {
  448. if (!tls->pending.cipher_suite->signature->verify(tls, buf, len,
  449. tls_get_dh_params_hash,
  450. server_ecdh_params_ptr,
  451. buf - server_ecdh_params_ptr))
  452. return;
  453. } else {
  454. if (len)
  455. goto decode_error;
  456. }
  457. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
  458. return;
  459. decode_error:
  460. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  461. "ServerKeyExchange decode error");
  462. }
  463. static bool tls_send_ecdhe_client_key_xchg(struct l_tls *tls)
  464. {
  465. uint8_t buf[1024];
  466. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  467. uint8_t pre_master_secret[128];
  468. ssize_t pre_master_secret_len;
  469. struct tls_ecdhe_params *params = tls->pending.key_xchg_params;
  470. struct l_ecc_point *our_public;
  471. struct l_ecc_scalar *secret;
  472. /* RFC 8422, Section 5.7 */
  473. if (!l_ecdh_generate_key_pair(params->curve,
  474. &params->private, &our_public)) {
  475. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  476. "Generating ECDH key pair failed");
  477. return false;
  478. }
  479. ptr += tls_write_ecpoint(ptr, buf + sizeof(buf) - ptr,
  480. tls->negotiated_curve, our_public);
  481. l_ecc_point_free(our_public);
  482. /*
  483. * Neither 5.4 or 5.7 "Actions" paragraphs say when the ECDH shared
  484. * secret is calculated but we can either do this in
  485. * tls_handle_ecdhe_server_key_xchg or here. In both cases we only
  486. * need to store the public key in the client's key_xchg_params and
  487. * can free all of the params after sending the ClientKeyExchange.
  488. * By doing this calculation here we're aligned with RSA and also
  489. * with the server mode where the shared secret can only be
  490. * calculated after the ClientKeyExchange is received.
  491. */
  492. if (!l_ecdh_generate_shared_secret(params->private, params->public,
  493. &secret)) {
  494. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  495. "Generating ECDH shared-secret failed");
  496. return false;
  497. }
  498. tls_free_ecdhe_params(tls);
  499. pre_master_secret_len = l_ecc_scalar_get_data(secret,
  500. pre_master_secret,
  501. sizeof(pre_master_secret));
  502. l_ecc_scalar_free(secret);
  503. if (pre_master_secret_len < 0) {
  504. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  505. "l_ecc_scalar_get_data(secret) failed");
  506. return false;
  507. }
  508. tls_tx_handshake(tls, TLS_CLIENT_KEY_EXCHANGE, buf, ptr - buf);
  509. tls_generate_master_secret(tls, pre_master_secret,
  510. pre_master_secret_len);
  511. explicit_bzero(pre_master_secret, pre_master_secret_len);
  512. return true;
  513. }
  514. static void tls_handle_ecdhe_client_key_xchg(struct l_tls *tls,
  515. const uint8_t *buf, size_t len)
  516. {
  517. struct tls_ecdhe_params *params = tls->pending.key_xchg_params;
  518. uint8_t pre_master_secret[128];
  519. ssize_t pre_master_secret_len;
  520. struct l_ecc_point *other_public;
  521. struct l_ecc_scalar *secret;
  522. size_t point_bytes = 2 * l_ecc_curve_get_scalar_bytes(params->curve);
  523. /* RFC 8422, Section 5.7 */
  524. if (len < 2)
  525. goto decode_error;
  526. if (*buf++ != 1 + point_bytes)
  527. goto decode_error;
  528. if (*buf != 4) { /* uncompressed */
  529. TLS_DISCONNECT(TLS_ALERT_ILLEGAL_PARAM, 0,
  530. "Unsupported (deprecated?) PointConversionForm "
  531. "%u", *buf);
  532. return;
  533. }
  534. buf++;
  535. len -= 2;
  536. if (len != point_bytes)
  537. goto decode_error;
  538. /*
  539. * RFC 8422, Section 5.11: "A receiving party MUST check that the
  540. * x and y parameters from the peer's public value satisfy the
  541. * curve equation, y^2 = x^3 + ax + b mod p."
  542. * This happens in l_ecc_point_from_data when the L_ECC_POINT_TYPE_FULL
  543. * format is used.
  544. */
  545. other_public = l_ecc_point_from_data(params->curve,
  546. L_ECC_POINT_TYPE_FULL,
  547. buf, len);
  548. if (!other_public) {
  549. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  550. "ClientKeyExchange.exchange_keys.ecdh_Yc "
  551. "decode error");
  552. return;
  553. }
  554. if (!l_ecdh_generate_shared_secret(params->private, other_public,
  555. &secret)) {
  556. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  557. "Generating ECDH shared-secret failed");
  558. return;
  559. }
  560. tls_free_ecdhe_params(tls);
  561. l_ecc_point_free(other_public);
  562. pre_master_secret_len = l_ecc_scalar_get_data(secret,
  563. pre_master_secret,
  564. sizeof(pre_master_secret));
  565. l_ecc_scalar_free(secret);
  566. if (pre_master_secret_len < 0) {
  567. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  568. "l_ecc_scalar_get_data(secret) failed");
  569. return;
  570. }
  571. tls_generate_master_secret(tls, pre_master_secret,
  572. pre_master_secret_len);
  573. explicit_bzero(pre_master_secret, pre_master_secret_len);
  574. return;
  575. decode_error:
  576. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  577. "ClientKeyExchange decode error");
  578. }
  579. static struct tls_key_exchange_algorithm tls_ecdhe = {
  580. .need_ecc = true,
  581. .send_server_key_exchange = tls_send_ecdhe_server_key_xchg,
  582. .handle_server_key_exchange = tls_handle_ecdhe_server_key_xchg,
  583. .send_client_key_exchange = tls_send_ecdhe_client_key_xchg,
  584. .handle_client_key_exchange = tls_handle_ecdhe_client_key_xchg,
  585. .free_params = tls_free_ecdhe_params,
  586. };
  587. /* Maximum FF DH prime modulus size in bytes */
  588. #define TLS_DHE_MAX_SIZE 1024
  589. struct tls_dhe_params {
  590. size_t prime_len;
  591. struct l_key *prime;
  592. struct l_key *generator;
  593. struct l_key *private;
  594. struct l_key *public;
  595. };
  596. static void tls_free_dhe_params(struct l_tls *tls)
  597. {
  598. struct tls_dhe_params *params = tls->pending.key_xchg_params;
  599. if (!params)
  600. return;
  601. tls->pending.key_xchg_params = NULL;
  602. l_key_free(params->prime);
  603. l_key_free(params->generator);
  604. l_key_free(params->private);
  605. l_key_free(params->public);
  606. l_free(params);
  607. }
  608. static bool tls_send_dhe_server_key_xchg(struct l_tls *tls)
  609. {
  610. uint8_t buf[1024 + TLS_DHE_MAX_SIZE * 3];
  611. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  612. struct tls_dhe_params *params;
  613. const uint8_t *prime_buf;
  614. uint8_t generator_buf = tls->negotiated_ff_group->ff.generator;
  615. uint8_t public_buf[TLS_DHE_MAX_SIZE];
  616. size_t public_len;
  617. unsigned int zeros = 0;
  618. ssize_t sign_len;
  619. const uint8_t *server_dh_params_ptr;
  620. params = l_new(struct tls_dhe_params, 1);
  621. prime_buf = tls->negotiated_ff_group->ff.prime;
  622. params->prime_len = tls->negotiated_ff_group->ff.prime_len;
  623. params->prime = l_key_new(L_KEY_RAW, prime_buf, params->prime_len);
  624. params->generator = l_key_new(L_KEY_RAW, &generator_buf, 1);
  625. if (!params->prime || !params->generator) {
  626. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0, "l_key_new failed");
  627. goto free_params;
  628. }
  629. params->private = l_key_generate_dh_private(prime_buf, params->prime_len);
  630. if (!params->private) {
  631. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  632. "l_key_generate_dh_private failed");
  633. goto free_params;
  634. }
  635. memset(public_buf, 0, sizeof(public_buf));
  636. public_len = params->prime_len;
  637. if (!l_key_compute_dh_public(params->generator, params->private,
  638. params->prime, public_buf,
  639. &public_len)) {
  640. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  641. "l_key_compute_dh_public failed");
  642. goto free_params;
  643. }
  644. while (zeros < public_len && public_buf[zeros] == 0x00)
  645. zeros++;
  646. server_dh_params_ptr = ptr;
  647. /* RFC 5246, Section 7.4.3 */
  648. l_put_be16(params->prime_len, ptr);
  649. memcpy(ptr + 2, prime_buf, params->prime_len);
  650. ptr += 2 + params->prime_len;
  651. l_put_be16(1, ptr);
  652. memcpy(ptr + 2, &generator_buf, 1);
  653. ptr += 2 + 1;
  654. l_put_be16(public_len - zeros, ptr);
  655. memcpy(ptr + 2, public_buf + zeros, public_len - zeros);
  656. ptr += 2 + public_len - zeros;
  657. if (tls->pending.cipher_suite->signature) {
  658. sign_len = tls->pending.cipher_suite->signature->sign(tls, ptr,
  659. buf + sizeof(buf) - ptr,
  660. tls_get_dh_params_hash,
  661. server_dh_params_ptr,
  662. ptr - server_dh_params_ptr);
  663. if (sign_len < 0)
  664. goto free_params;
  665. ptr += sign_len;
  666. }
  667. tls->pending.key_xchg_params = params;
  668. tls_tx_handshake(tls, TLS_SERVER_KEY_EXCHANGE, buf, ptr - buf);
  669. return true;
  670. free_params:
  671. l_key_free(params->prime);
  672. l_key_free(params->generator);
  673. l_key_free(params->private);
  674. l_free(params);
  675. return false;
  676. }
  677. static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
  678. const uint8_t *buf, size_t len)
  679. {
  680. struct tls_dhe_params *params = NULL;
  681. const uint8_t *prime_buf;
  682. const uint8_t *generator_buf;
  683. size_t generator_len;
  684. const uint8_t *public_buf;
  685. size_t public_len;
  686. const uint8_t *server_dh_params_ptr = buf;
  687. if (len < 2)
  688. goto decode_error;
  689. params = l_new(struct tls_dhe_params, 1);
  690. params->prime_len = l_get_be16(buf);
  691. if (len < 2 + params->prime_len + 2)
  692. goto decode_error;
  693. prime_buf = buf + 2;
  694. buf += 2 + params->prime_len;
  695. len -= 2 + params->prime_len;
  696. /* Strip leading zeros for the length checks later */
  697. while (params->prime_len && prime_buf[0] == 0x00) {
  698. prime_buf++;
  699. params->prime_len--;
  700. }
  701. generator_len = l_get_be16(buf);
  702. if (len < 2 + generator_len + 2)
  703. goto decode_error;
  704. generator_buf = buf + 2;
  705. buf += 2 + generator_len;
  706. len -= 2 + generator_len;
  707. public_len = l_get_be16(buf);
  708. if (len < 2 + public_len)
  709. goto decode_error;
  710. public_buf = buf + 2;
  711. buf += 2 + public_len;
  712. len -= 2 + public_len;
  713. /*
  714. * Validate the values received. Without requiring RFC 7919 from
  715. * the server, and there are many servers that don't implement it
  716. * yet, we basically have to blindly accept the provided prime value.
  717. * We have no way to confirm that it's actually prime or that it's a
  718. * "safe prime" or that it forms a group without small sub-groups.
  719. * There's also no way to whitelist all valid values. But we do a
  720. * basic sanity check and require it to be 1536-bit or longer, the
  721. * minimum length required by the Linux kernel for keyctl_dh_compute().
  722. * The generator must also be at least within the min & max interval
  723. * for the private/public values.
  724. */
  725. if (params->prime_len > TLS_DHE_MAX_SIZE || params->prime_len < 192 ||
  726. !(prime_buf[params->prime_len - 1] & 1)) {
  727. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  728. "Server DH prime modulus invalid");
  729. goto free_params;
  730. }
  731. if (!l_key_validate_dh_payload(generator_buf, generator_len,
  732. prime_buf, params->prime_len)) {
  733. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  734. "Server DH generator value invalid");
  735. goto free_params;
  736. }
  737. /*
  738. * Just output a warning if the server sent group parameters not
  739. * offered in our RFC 7919 Supported Groups extension.
  740. */
  741. if (!tls_find_ff_group(prime_buf, params->prime_len,
  742. generator_buf, generator_len))
  743. TLS_DEBUG("Warning: using server's custom %i-bit FF DH group",
  744. (int) (params->prime_len * 8));
  745. /*
  746. * RFC 7919 Section 3.0:
  747. * "the client MUST verify that dh_Ys is in the range
  748. * 1 < dh_Ys < dh_p - 1. If dh_Ys is not in this range, the client
  749. * MUST terminate the connection with a fatal handshake_failure(40)
  750. * alert."
  751. */
  752. if (!l_key_validate_dh_payload(public_buf, public_len,
  753. prime_buf, params->prime_len)) {
  754. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  755. "Server DH public value invalid");
  756. goto free_params;
  757. }
  758. params->prime = l_key_new(L_KEY_RAW, prime_buf, params->prime_len);
  759. params->generator = l_key_new(L_KEY_RAW, generator_buf, generator_len);
  760. params->public = l_key_new(L_KEY_RAW, public_buf, public_len);
  761. if (!params->prime || !params->generator || !params->public) {
  762. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0, "l_key_new failed");
  763. goto free_params;
  764. }
  765. /* Do this now so we don't need prime_buf in send_client_key_xchg */
  766. params->private = l_key_generate_dh_private(prime_buf, params->prime_len);
  767. if (!params->private) {
  768. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  769. "l_key_generate_dh_private failed");
  770. goto free_params;
  771. }
  772. tls->pending.key_xchg_params = params;
  773. if (tls->pending.cipher_suite->signature) {
  774. if (!tls->pending.cipher_suite->signature->verify(tls, buf, len,
  775. tls_get_dh_params_hash,
  776. server_dh_params_ptr,
  777. buf - server_dh_params_ptr))
  778. return;
  779. } else {
  780. if (len)
  781. goto decode_error;
  782. }
  783. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
  784. return;
  785. decode_error:
  786. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  787. "ServerKeyExchange decode error");
  788. free_params:
  789. if (params) {
  790. l_key_free(params->prime);
  791. l_key_free(params->generator);
  792. l_key_free(params->public);
  793. l_free(params);
  794. }
  795. }
  796. static bool tls_send_dhe_client_key_xchg(struct l_tls *tls)
  797. {
  798. struct tls_dhe_params *params = tls->pending.key_xchg_params;
  799. uint8_t buf[128 + params->prime_len];
  800. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  801. uint8_t public_buf[params->prime_len];
  802. size_t public_len;
  803. unsigned int zeros = 0;
  804. uint8_t pre_master_secret[params->prime_len];
  805. size_t pre_master_secret_len;
  806. public_len = params->prime_len;
  807. memset(public_buf, 0, sizeof(public_buf));
  808. if (!l_key_compute_dh_public(params->generator, params->private,
  809. params->prime, public_buf,
  810. &public_len)) {
  811. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  812. "l_key_compute_dh_public failed");
  813. return false;
  814. }
  815. while (zeros < public_len && public_buf[zeros] == 0x00)
  816. zeros++;
  817. l_put_be16(public_len - zeros, ptr);
  818. memcpy(ptr + 2, public_buf + zeros, public_len - zeros);
  819. ptr += 2 + public_len - zeros;
  820. pre_master_secret_len = params->prime_len;
  821. zeros = 0;
  822. if (!l_key_compute_dh_secret(params->public, params->private,
  823. params->prime, pre_master_secret,
  824. &pre_master_secret_len)) {
  825. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  826. "Generating DH shared-secret failed");
  827. return false;
  828. }
  829. while (zeros < pre_master_secret_len &&
  830. pre_master_secret[zeros] == 0x00)
  831. zeros++;
  832. tls_tx_handshake(tls, TLS_CLIENT_KEY_EXCHANGE, buf, ptr - buf);
  833. tls_free_dhe_params(tls);
  834. tls_generate_master_secret(tls, pre_master_secret + zeros,
  835. pre_master_secret_len - zeros);
  836. explicit_bzero(pre_master_secret, pre_master_secret_len);
  837. return true;
  838. }
  839. static void tls_handle_dhe_client_key_xchg(struct l_tls *tls,
  840. const uint8_t *buf, size_t len)
  841. {
  842. struct tls_dhe_params *params = tls->pending.key_xchg_params;
  843. uint8_t pre_master_secret[params->prime_len];
  844. size_t pre_master_secret_len;
  845. size_t public_len;
  846. unsigned int zeros = 0;
  847. if (len < 2)
  848. goto decode_error;
  849. public_len = l_get_be16(buf);
  850. buf += 2;
  851. len -= 2;
  852. if (public_len != len)
  853. goto decode_error;
  854. /*
  855. * RFC 7919 Section 4:
  856. * "the server MUST verify that 1 < dh_Yc < dh_p - 1. If dh_Yc is
  857. * out of range, the server MUST terminate the connection with
  858. * a fatal handshake_failure(40) alert."
  859. */
  860. if (!l_key_validate_dh_payload(buf, public_len,
  861. tls->negotiated_ff_group->ff.prime,
  862. params->prime_len)) {
  863. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  864. "Client DH public value invalid");
  865. return;
  866. }
  867. params->public = l_key_new(L_KEY_RAW, buf, public_len);
  868. if (!params->public) {
  869. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0, "l_key_new failed");
  870. return;
  871. }
  872. pre_master_secret_len = params->prime_len;
  873. if (!l_key_compute_dh_secret(params->public, params->private,
  874. params->prime, pre_master_secret,
  875. &pre_master_secret_len)) {
  876. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  877. "Generating DH shared-secret failed");
  878. return;
  879. }
  880. while (zeros < pre_master_secret_len &&
  881. pre_master_secret[zeros] == 0x00)
  882. zeros++;
  883. tls_free_dhe_params(tls);
  884. tls_generate_master_secret(tls, pre_master_secret + zeros,
  885. pre_master_secret_len - zeros);
  886. explicit_bzero(pre_master_secret, pre_master_secret_len);
  887. return;
  888. decode_error:
  889. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  890. "ClientKeyExchange decode error");
  891. }
  892. static struct tls_key_exchange_algorithm tls_dhe = {
  893. .need_ecc = true,
  894. .send_server_key_exchange = tls_send_dhe_server_key_xchg,
  895. .handle_server_key_exchange = tls_handle_dhe_server_key_xchg,
  896. .send_client_key_exchange = tls_send_dhe_client_key_xchg,
  897. .handle_client_key_exchange = tls_handle_dhe_client_key_xchg,
  898. .free_params = tls_free_dhe_params,
  899. };
  900. static struct tls_bulk_encryption_algorithm tls_aes128 = {
  901. .cipher_type = TLS_CIPHER_BLOCK,
  902. .l_id = L_CIPHER_AES_CBC,
  903. .key_length = 16,
  904. .iv_length = 16,
  905. .block_length = 16,
  906. }, tls_aes256 = {
  907. .cipher_type = TLS_CIPHER_BLOCK,
  908. .l_id = L_CIPHER_AES_CBC,
  909. .key_length = 32,
  910. .iv_length = 16,
  911. .block_length = 16,
  912. }, tls_3des_ede = {
  913. .cipher_type = TLS_CIPHER_BLOCK,
  914. .l_id = L_CIPHER_DES3_EDE_CBC,
  915. .key_length = 24,
  916. .iv_length = 8,
  917. .block_length = 8,
  918. }, tls_aes128_gcm = {
  919. .cipher_type = TLS_CIPHER_AEAD,
  920. .l_aead_id = L_AEAD_CIPHER_AES_GCM,
  921. .key_length = 16,
  922. .iv_length = 12,
  923. .fixed_iv_length = 4,
  924. .auth_tag_length = 16,
  925. }, tls_aes256_gcm = {
  926. .cipher_type = TLS_CIPHER_AEAD,
  927. .l_aead_id = L_AEAD_CIPHER_AES_GCM,
  928. .key_length = 32,
  929. .iv_length = 12,
  930. .fixed_iv_length = 4,
  931. .auth_tag_length = 16,
  932. };
  933. static struct tls_mac_algorithm tls_sha = {
  934. .id = 2,
  935. .hmac_type = L_CHECKSUM_SHA1,
  936. .mac_length = 20,
  937. }, tls_sha256 = {
  938. .id = 4,
  939. .hmac_type = L_CHECKSUM_SHA256,
  940. .mac_length = 32,
  941. }, tls_sha384 = {
  942. .id = 5,
  943. .hmac_type = L_CHECKSUM_SHA384,
  944. .mac_length = 48,
  945. };
  946. static struct tls_cipher_suite tls_rsa_with_3des_ede_cbc_sha = {
  947. .id = { 0x00, 0x0a },
  948. .name = "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
  949. .verify_data_length = 12,
  950. .encryption = &tls_3des_ede,
  951. .mac = &tls_sha,
  952. .signature = &tls_rsa_signature,
  953. .key_xchg = &tls_rsa_key_xchg,
  954. }, tls_dhe_rsa_with_3des_ede_cbc_sha = {
  955. .id = { 0x00, 0x16 },
  956. .name = "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
  957. .verify_data_length = 12,
  958. .encryption = &tls_3des_ede,
  959. .mac = &tls_sha,
  960. .signature = &tls_rsa_signature,
  961. .key_xchg = &tls_dhe,
  962. }, tls_rsa_with_aes_128_cbc_sha = {
  963. .id = { 0x00, 0x2f },
  964. .name = "TLS_RSA_WITH_AES_128_CBC_SHA",
  965. .verify_data_length = 12,
  966. .encryption = &tls_aes128,
  967. .mac = &tls_sha,
  968. .signature = &tls_rsa_signature,
  969. .key_xchg = &tls_rsa_key_xchg,
  970. }, tls_dhe_rsa_with_aes_128_cbc_sha = {
  971. .id = { 0x00, 0x33 },
  972. .name = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
  973. .verify_data_length = 12,
  974. .encryption = &tls_aes128,
  975. .mac = &tls_sha,
  976. .signature = &tls_rsa_signature,
  977. .key_xchg = &tls_dhe,
  978. }, tls_rsa_with_aes_256_cbc_sha = {
  979. .id = { 0x00, 0x35 },
  980. .name = "TLS_RSA_WITH_AES_256_CBC_SHA",
  981. .verify_data_length = 12,
  982. .encryption = &tls_aes256,
  983. .mac = &tls_sha,
  984. .signature = &tls_rsa_signature,
  985. .key_xchg = &tls_rsa_key_xchg,
  986. }, tls_dhe_rsa_with_aes_256_cbc_sha = {
  987. .id = { 0x00, 0x39 },
  988. .name = "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
  989. .verify_data_length = 12,
  990. .encryption = &tls_aes256,
  991. .mac = &tls_sha,
  992. .signature = &tls_rsa_signature,
  993. .key_xchg = &tls_dhe,
  994. }, tls_rsa_with_aes_128_cbc_sha256 = {
  995. .id = { 0x00, 0x3c },
  996. .name = "TLS_RSA_WITH_AES_128_CBC_SHA256",
  997. .verify_data_length = 12,
  998. .encryption = &tls_aes128,
  999. .mac = &tls_sha256,
  1000. .signature = &tls_rsa_signature,
  1001. .key_xchg = &tls_rsa_key_xchg,
  1002. }, tls_rsa_with_aes_256_cbc_sha256 = {
  1003. .id = { 0x00, 0x3d },
  1004. .name = "TLS_RSA_WITH_AES_256_CBC_SHA256",
  1005. .verify_data_length = 12,
  1006. .encryption = &tls_aes256,
  1007. .mac = &tls_sha256,
  1008. .signature = &tls_rsa_signature,
  1009. .key_xchg = &tls_rsa_key_xchg,
  1010. }, tls_dhe_rsa_with_aes_128_cbc_sha256 = {
  1011. .id = { 0x00, 0x67 },
  1012. .name = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
  1013. .verify_data_length = 12,
  1014. .encryption = &tls_aes128,
  1015. .mac = &tls_sha256,
  1016. .signature = &tls_rsa_signature,
  1017. .key_xchg = &tls_dhe,
  1018. }, tls_dhe_rsa_with_aes_256_cbc_sha256 = {
  1019. .id = { 0x00, 0x6b },
  1020. .name = "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
  1021. .verify_data_length = 12,
  1022. .encryption = &tls_aes256,
  1023. .mac = &tls_sha256,
  1024. .signature = &tls_rsa_signature,
  1025. .key_xchg = &tls_dhe,
  1026. }, tls_rsa_with_aes_128_gcm_sha256 = {
  1027. .id = { 0x00, 0x9c },
  1028. .name = "TLS_RSA_WITH_AES_128_GCM_SHA256",
  1029. .verify_data_length = 12,
  1030. .encryption = &tls_aes128_gcm,
  1031. .signature = &tls_rsa_signature,
  1032. .key_xchg = &tls_rsa_key_xchg,
  1033. }, tls_rsa_with_aes_256_gcm_sha384 = {
  1034. .id = { 0x00, 0x9d },
  1035. .name = "TLS_RSA_WITH_AES_256_GCM_SHA384",
  1036. .verify_data_length = 12,
  1037. .encryption = &tls_aes256_gcm,
  1038. .prf_hmac = L_CHECKSUM_SHA384,
  1039. .signature = &tls_rsa_signature,
  1040. .key_xchg = &tls_rsa_key_xchg,
  1041. }, tls_dhe_rsa_with_aes_128_gcm_sha256 = {
  1042. .id = { 0x00, 0x9e },
  1043. .name = "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
  1044. .verify_data_length = 12,
  1045. .encryption = &tls_aes128_gcm,
  1046. .signature = &tls_rsa_signature,
  1047. .key_xchg = &tls_dhe,
  1048. }, tls_dhe_rsa_with_aes_256_gcm_sha384 = {
  1049. .id = { 0x00, 0x9f },
  1050. .name = "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
  1051. .verify_data_length = 12,
  1052. .encryption = &tls_aes256_gcm,
  1053. .prf_hmac = L_CHECKSUM_SHA384,
  1054. .signature = &tls_rsa_signature,
  1055. .key_xchg = &tls_dhe,
  1056. }, tls_ecdhe_rsa_with_3des_ede_cbc_sha = {
  1057. .id = { 0xc0, 0x12 },
  1058. .name = "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
  1059. .verify_data_length = 12,
  1060. .encryption = &tls_3des_ede,
  1061. .mac = &tls_sha,
  1062. .signature = &tls_rsa_signature,
  1063. .key_xchg = &tls_ecdhe,
  1064. }, tls_ecdhe_rsa_with_aes_128_cbc_sha = {
  1065. .id = { 0xc0, 0x13 },
  1066. .name = "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  1067. .verify_data_length = 12,
  1068. .encryption = &tls_aes128,
  1069. .mac = &tls_sha,
  1070. .signature = &tls_rsa_signature,
  1071. .key_xchg = &tls_ecdhe,
  1072. }, tls_ecdhe_rsa_with_aes_256_cbc_sha = {
  1073. .id = { 0xc0, 0x14 },
  1074. .name = "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  1075. .verify_data_length = 12,
  1076. .encryption = &tls_aes256,
  1077. .mac = &tls_sha,
  1078. .signature = &tls_rsa_signature,
  1079. .key_xchg = &tls_ecdhe,
  1080. }, tls_ecdhe_rsa_with_aes_128_cbc_sha256 = {
  1081. .id = { 0xc0, 0x27 },
  1082. .name = "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  1083. .verify_data_length = 12,
  1084. .encryption = &tls_aes128,
  1085. .mac = &tls_sha256,
  1086. .signature = &tls_rsa_signature,
  1087. .key_xchg = &tls_ecdhe,
  1088. }, tls_ecdhe_rsa_with_aes_256_cbc_sha384 = {
  1089. .id = { 0xc0, 0x28 },
  1090. .name = "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
  1091. .verify_data_length = 12,
  1092. .encryption = &tls_aes256,
  1093. .mac = &tls_sha384,
  1094. .prf_hmac = L_CHECKSUM_SHA384,
  1095. .signature = &tls_rsa_signature,
  1096. .key_xchg = &tls_ecdhe,
  1097. }, tls_ecdhe_rsa_with_aes_128_gcm_sha256 = {
  1098. .id = { 0xc0, 0x2f },
  1099. .name = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  1100. .verify_data_length = 12,
  1101. .encryption = &tls_aes128_gcm,
  1102. .signature = &tls_rsa_signature,
  1103. .key_xchg = &tls_ecdhe,
  1104. }, tls_ecdhe_rsa_with_aes_256_gcm_sha384 = {
  1105. .id = { 0xc0, 0x30 },
  1106. .name = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  1107. .verify_data_length = 12,
  1108. .encryption = &tls_aes256_gcm,
  1109. .prf_hmac = L_CHECKSUM_SHA384,
  1110. .signature = &tls_rsa_signature,
  1111. .key_xchg = &tls_ecdhe,
  1112. };
  1113. struct tls_cipher_suite *tls_cipher_suite_pref[] = {
  1114. &tls_ecdhe_rsa_with_aes_256_cbc_sha,
  1115. &tls_ecdhe_rsa_with_aes_128_cbc_sha,
  1116. &tls_dhe_rsa_with_aes_256_cbc_sha,
  1117. &tls_dhe_rsa_with_aes_128_cbc_sha,
  1118. &tls_rsa_with_aes_256_cbc_sha,
  1119. &tls_rsa_with_aes_128_cbc_sha,
  1120. &tls_ecdhe_rsa_with_aes_256_cbc_sha384,
  1121. &tls_ecdhe_rsa_with_aes_128_cbc_sha256,
  1122. &tls_dhe_rsa_with_aes_256_cbc_sha256,
  1123. &tls_dhe_rsa_with_aes_128_cbc_sha256,
  1124. &tls_rsa_with_aes_256_cbc_sha256,
  1125. &tls_rsa_with_aes_128_cbc_sha256,
  1126. &tls_ecdhe_rsa_with_aes_256_gcm_sha384,
  1127. &tls_ecdhe_rsa_with_aes_128_gcm_sha256,
  1128. &tls_dhe_rsa_with_aes_256_gcm_sha384,
  1129. &tls_dhe_rsa_with_aes_128_gcm_sha256,
  1130. &tls_rsa_with_aes_256_gcm_sha384,
  1131. &tls_rsa_with_aes_128_gcm_sha256,
  1132. &tls_ecdhe_rsa_with_3des_ede_cbc_sha,
  1133. &tls_dhe_rsa_with_3des_ede_cbc_sha,
  1134. &tls_rsa_with_3des_ede_cbc_sha,
  1135. NULL,
  1136. };