TLSv12.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/DateTime.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibCrypto/ASN1/DER.h>
  29. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  30. #include <LibTLS/TLSv12.h>
  31. //#define TLS_DEBUG
  32. namespace {
  33. struct OIDChain {
  34. void* root { nullptr };
  35. u8* oid { nullptr };
  36. };
  37. }
  38. namespace TLS {
  39. // "for now" q&d implementation of ASN1
  40. namespace {
  41. static bool _asn1_is_field_present(const u32* fields, const u32* prefix)
  42. {
  43. size_t i = 0;
  44. while (prefix[i]) {
  45. if (fields[i] != prefix[i])
  46. return false;
  47. ++i;
  48. }
  49. return true;
  50. }
  51. static bool _asn1_is_oid(const u8* oid, const u8* compare, size_t length = 3)
  52. {
  53. size_t i = 0;
  54. while (oid[i] && i < length) {
  55. if (oid[i] != compare[i])
  56. return false;
  57. ++i;
  58. }
  59. return true;
  60. }
  61. static bool _set_algorithm(CertificateKeyAlgorithm& algorithm, const u8* value, size_t length)
  62. {
  63. if (length == 7) {
  64. // Elliptic Curve pubkey
  65. dbg() << "Cert.algorithm: EC, unsupported";
  66. return false;
  67. }
  68. if (length == 8) {
  69. // named EC key
  70. dbg() << "Cert.algorithm: Named EC (" << *value << "), unsupported";
  71. return false;
  72. }
  73. if (length == 5) {
  74. // named EC SECP key
  75. dbg() << "Cert.algorithm: Named EC secp (" << *value << "), unsupported";
  76. return false;
  77. }
  78. if (length != 9) {
  79. dbg() << "Invalid certificate algorithm";
  80. return false;
  81. }
  82. if (_asn1_is_oid(value, Constants::RSA_SIGN_RSA_OID, 9)) {
  83. algorithm = CertificateKeyAlgorithm::RSA_RSA;
  84. return true;
  85. }
  86. if (_asn1_is_oid(value, Constants::RSA_SIGN_SHA256_OID, 9)) {
  87. algorithm = CertificateKeyAlgorithm::RSA_SHA256;
  88. return true;
  89. }
  90. if (_asn1_is_oid(value, Constants::RSA_SIGN_SHA512_OID, 9)) {
  91. algorithm = CertificateKeyAlgorithm::RSA_SHA512;
  92. return true;
  93. }
  94. if (_asn1_is_oid(value, Constants::RSA_SIGN_SHA1_OID, 9)) {
  95. algorithm = CertificateKeyAlgorithm::RSA_SHA1;
  96. return true;
  97. }
  98. if (_asn1_is_oid(value, Constants::RSA_SIGN_MD5_OID, 9)) {
  99. algorithm = CertificateKeyAlgorithm::RSA_MD5;
  100. return true;
  101. }
  102. dbg() << "Unsupported RSA Signature mode " << value[8];
  103. return false;
  104. }
  105. static size_t _get_asn1_length(const u8* buffer, size_t length, size_t& octets)
  106. {
  107. octets = 0;
  108. if (length < 1)
  109. return 0;
  110. u8 size = buffer[0];
  111. if (size & 0x80) {
  112. octets = size & 0x7f;
  113. if (octets > length - 1) {
  114. return 0;
  115. }
  116. auto reference_octets = octets;
  117. if (octets > 4)
  118. reference_octets = 4;
  119. size_t long_size = 0, coeff = 1;
  120. for (auto i = reference_octets; i > 0; --i) {
  121. long_size += buffer[i] * coeff;
  122. coeff *= 0x100;
  123. }
  124. ++octets;
  125. return long_size;
  126. }
  127. ++octets;
  128. return size;
  129. }
  130. static ssize_t _parse_asn1(const Context& context, Certificate& cert, const u8* buffer, size_t size, int level, u32* fields, u8* has_key, int client_cert, u8* root_oid, OIDChain* chain)
  131. {
  132. OIDChain local_chain;
  133. local_chain.root = chain;
  134. size_t position = 0;
  135. // parse DER...again
  136. size_t index = 0;
  137. u8 oid[16] { 0 };
  138. local_chain.oid = oid;
  139. if (has_key)
  140. *has_key = 0;
  141. u8 local_has_key = 0;
  142. const u8* cert_data = nullptr;
  143. size_t cert_length = 0;
  144. while (position < size) {
  145. size_t start_position = position;
  146. if (size - position < 2) {
  147. dbg() << "not enough data for certificate size";
  148. return (i8)Error::NeedMoreData;
  149. }
  150. u8 first = buffer[position++];
  151. u8 type = first & 0x1f;
  152. u8 constructed = first & 0x20;
  153. size_t octets = 0;
  154. u32 temp;
  155. index++;
  156. if (level <= 0xff)
  157. fields[level - 1] = index;
  158. size_t length = _get_asn1_length((const u8*)&buffer[position], size - position, octets);
  159. if (octets > 4 || octets > size - position) {
  160. dbg() << "could not read the certificate";
  161. return position;
  162. }
  163. position += octets;
  164. if (size - position < length) {
  165. dbg() << "not enough data for sequence";
  166. return (i8)Error::NeedMoreData;
  167. }
  168. if (length && constructed) {
  169. switch (type) {
  170. case 0x03:
  171. break;
  172. case 0x10:
  173. if (level == 2 && index == 1) {
  174. cert_length = length + position - start_position;
  175. cert_data = buffer + start_position;
  176. }
  177. // public key data
  178. if (!cert.version && _asn1_is_field_present(fields, Constants::priv_der_id)) {
  179. temp = length + position - start_position;
  180. if (cert.der.size() < temp) {
  181. cert.der.grow(temp);
  182. } else {
  183. cert.der.trim(temp);
  184. }
  185. cert.der.overwrite(0, buffer + start_position, temp);
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. local_has_key = false;
  192. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  193. if ((local_has_key && (!context.is_server || client_cert)) || (client_cert || _asn1_is_field_present(fields, Constants::pk_id))) {
  194. temp = length + position - start_position;
  195. if (cert.der.size() < temp) {
  196. cert.der.grow(temp);
  197. } else {
  198. cert.der.trim(temp);
  199. }
  200. cert.der.overwrite(0, buffer + start_position, temp);
  201. }
  202. } else {
  203. switch (type) {
  204. case 0x00:
  205. return position;
  206. break;
  207. case 0x01:
  208. temp = buffer[position];
  209. break;
  210. case 0x02:
  211. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  212. if (has_key)
  213. *has_key = true;
  214. if (index == 1)
  215. cert.public_key.set(
  216. Crypto::UnsignedBigInteger::import_data(buffer + position, length),
  217. cert.public_key.public_exponent());
  218. else if (index == 2)
  219. cert.public_key.set(
  220. cert.public_key.modulus(),
  221. Crypto::UnsignedBigInteger::import_data(buffer + position, length));
  222. } else if (_asn1_is_field_present(fields, Constants::serial_id)) {
  223. cert.serial_number = Crypto::UnsignedBigInteger::import_data(buffer + position, length);
  224. }
  225. if (_asn1_is_field_present(fields, Constants::version_id)) {
  226. if (length == 1)
  227. cert.version = buffer[position];
  228. }
  229. // print_buffer(ByteBuffer::wrap(buffer + position, length));
  230. break;
  231. case 0x03:
  232. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  233. if (has_key)
  234. *has_key = true;
  235. }
  236. if (_asn1_is_field_present(fields, Constants::sign_id)) {
  237. auto* value = buffer + position;
  238. auto len = length;
  239. if (!value[0] && len % 2) {
  240. ++value;
  241. --len;
  242. }
  243. cert.sign_key = ByteBuffer::copy(value, len);
  244. } else {
  245. if (buffer[position] == 0 && length > 256) {
  246. _parse_asn1(context, cert, buffer + position + 1, length - 1, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  247. } else {
  248. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  249. }
  250. }
  251. break;
  252. case 0x04:
  253. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  254. break;
  255. case 0x05:
  256. break;
  257. case 0x06:
  258. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  259. _set_algorithm(cert.key_algorithm, buffer + position, length);
  260. }
  261. if (_asn1_is_field_present(fields, Constants::algorithm_id)) {
  262. _set_algorithm(cert.algorithm, buffer + position, length);
  263. }
  264. if (length < 16)
  265. memcpy(oid, buffer + position, length);
  266. else
  267. memcpy(oid, buffer + position, 16);
  268. if (root_oid)
  269. memcpy(root_oid, oid, 16);
  270. break;
  271. case 0x09:
  272. break;
  273. case 0x17:
  274. case 0x018:
  275. // time
  276. // ignore
  277. break;
  278. case 0x013:
  279. case 0x0c:
  280. case 0x14:
  281. case 0x15:
  282. case 0x16:
  283. case 0x19:
  284. case 0x1a:
  285. case 0x1b:
  286. case 0x1c:
  287. case 0x1d:
  288. case 0x1e:
  289. // printable string and such
  290. if (_asn1_is_field_present(fields, Constants::issurer_id)) {
  291. if (_asn1_is_oid(oid, Constants::country_oid)) {
  292. cert.issuer_country = String { (const char*)buffer + position, length };
  293. } else if (_asn1_is_oid(oid, Constants::state_oid)) {
  294. cert.issuer_state = String { (const char*)buffer + position, length };
  295. } else if (_asn1_is_oid(oid, Constants::location_oid)) {
  296. cert.issuer_location = String { (const char*)buffer + position, length };
  297. } else if (_asn1_is_oid(oid, Constants::entity_oid)) {
  298. cert.issuer_entity = String { (const char*)buffer + position, length };
  299. } else if (_asn1_is_oid(oid, Constants::subject_oid)) {
  300. cert.issuer_subject = String { (const char*)buffer + position, length };
  301. }
  302. } else if (_asn1_is_field_present(fields, Constants::owner_id)) {
  303. if (_asn1_is_oid(oid, Constants::country_oid)) {
  304. cert.country = String { (const char*)buffer + position, length };
  305. } else if (_asn1_is_oid(oid, Constants::state_oid)) {
  306. cert.state = String { (const char*)buffer + position, length };
  307. } else if (_asn1_is_oid(oid, Constants::location_oid)) {
  308. cert.location = String { (const char*)buffer + position, length };
  309. } else if (_asn1_is_oid(oid, Constants::entity_oid)) {
  310. cert.entity = String { (const char*)buffer + position, length };
  311. } else if (_asn1_is_oid(oid, Constants::subject_oid)) {
  312. cert.subject = String { (const char*)buffer + position, length };
  313. }
  314. }
  315. break;
  316. default:
  317. // dbg() << "unused field " << type;
  318. break;
  319. }
  320. }
  321. position += length;
  322. }
  323. if (level == 2 && cert.sign_key.size() && cert_length && cert_data) {
  324. cert.fingerprint.clear();
  325. Crypto::Hash::Manager hash;
  326. switch (cert.key_algorithm) {
  327. case CertificateKeyAlgorithm::RSA_MD5:
  328. hash.initialize(Crypto::Hash::HashKind::MD5);
  329. break;
  330. case CertificateKeyAlgorithm::RSA_SHA1:
  331. hash.initialize(Crypto::Hash::HashKind::SHA1);
  332. break;
  333. case CertificateKeyAlgorithm::RSA_SHA256:
  334. hash.initialize(Crypto::Hash::HashKind::SHA256);
  335. break;
  336. case CertificateKeyAlgorithm::RSA_SHA512:
  337. hash.initialize(Crypto::Hash::HashKind::SHA512);
  338. break;
  339. default:
  340. dbg() << "Unsupported hash mode " << (u32)cert.key_algorithm;
  341. // fallback to md5, it will fail later
  342. hash.initialize(Crypto::Hash::HashKind::MD5);
  343. break;
  344. }
  345. hash.update(cert_data, cert_length);
  346. auto fingerprint = hash.digest();
  347. cert.fingerprint.grow(fingerprint.data_length());
  348. cert.fingerprint.overwrite(0, fingerprint.immutable_data(), fingerprint.data_length());
  349. #ifdef TLS_DEBUG
  350. dbg() << "Certificate fingerprint:";
  351. print_buffer(cert.fingerprint);
  352. #endif
  353. }
  354. return position;
  355. }
  356. }
  357. Optional<Certificate> TLSv12::parse_asn1(const ByteBuffer& buffer, bool) const
  358. {
  359. // FIXME: Our ASN.1 parser is not quite up to the task of
  360. // parsing this X.509 certificate, so for the
  361. // time being, we will "parse" the certificate
  362. // manually right here.
  363. Certificate cert;
  364. u32 fields[0xff];
  365. _parse_asn1(m_context, cert, buffer.data(), buffer.size(), 1, fields, nullptr, 0, nullptr, nullptr);
  366. #ifdef TLS_DEBUG
  367. dbg() << "Certificate issued for " << cert.subject << " by " << cert.issuer_subject;
  368. #endif
  369. return cert;
  370. }
  371. ssize_t TLSv12::handle_certificate(const ByteBuffer& buffer)
  372. {
  373. ssize_t res = 0;
  374. if (buffer.size() < 3) {
  375. dbg() << "not enough certificate header data";
  376. return (i8)Error::NeedMoreData;
  377. }
  378. u32 certificate_total_length = buffer[0] * 0x10000 + buffer[1] * 0x100 + buffer[2];
  379. dbg() << "total length: " << certificate_total_length;
  380. if (certificate_total_length <= 4)
  381. return 3 * certificate_total_length;
  382. res += 3;
  383. if (certificate_total_length > buffer.size() - res) {
  384. dbg() << "not enough data for claimed total cert length";
  385. return (i8)Error::NeedMoreData;
  386. }
  387. size_t size = certificate_total_length;
  388. size_t index = 0;
  389. bool valid_certificate = false;
  390. while (size > 0) {
  391. ++index;
  392. if (buffer.size() - res < 3) {
  393. dbg() << "not enough data for certificate length";
  394. return (i8)Error::NeedMoreData;
  395. }
  396. size_t certificate_size = buffer[res] * 0x10000 + buffer[res + 1] * 0x100 + buffer[res + 2];
  397. res += 3;
  398. if (buffer.size() - res < certificate_size) {
  399. dbg() << "not enough data for certificate body";
  400. return (i8)Error::NeedMoreData;
  401. }
  402. auto res_cert = res;
  403. auto remaining = certificate_size;
  404. size_t certificates_in_chain = 0;
  405. do {
  406. if (remaining <= 3) {
  407. dbg() << "Ran out of data";
  408. break;
  409. }
  410. ++certificates_in_chain;
  411. if (buffer.size() < (size_t)res_cert + 3) {
  412. dbg() << "not enough data to read cert size (" << buffer.size() << " < " << res_cert + 3 << ")";
  413. break;
  414. }
  415. size_t certificate_size_specific = buffer[res_cert] * 0x10000 + buffer[res_cert + 1] * 0x100 + buffer[res_cert + 2];
  416. res_cert += 3;
  417. remaining -= 3;
  418. if (certificate_size_specific > remaining) {
  419. dbg() << "invalid certificate size (expected " << remaining << " but got " << certificate_size_specific << ")";
  420. break;
  421. }
  422. remaining -= certificate_size_specific;
  423. auto certificate = parse_asn1(buffer.slice_view(res_cert, certificate_size_specific), false);
  424. if (certificate.has_value()) {
  425. m_context.certificates.append(certificate.value());
  426. valid_certificate = true;
  427. }
  428. res_cert += certificate_size_specific;
  429. } while (remaining > 0);
  430. if (remaining) {
  431. dbg() << "extraneous " << remaining << " bytes left over after parsing certificates";
  432. }
  433. size -= certificate_size + 3;
  434. res += certificate_size;
  435. }
  436. if (!valid_certificate)
  437. return (i8)Error::UnsupportedCertificate;
  438. if ((size_t)res != buffer.size())
  439. dbg() << "some data left unread: " << (size_t)res << " bytes out of " << buffer.size();
  440. return res;
  441. }
  442. void TLSv12::consume(const ByteBuffer& record)
  443. {
  444. if (m_context.critical_error) {
  445. dbg() << "There has been a critical error (" << (i8)m_context.critical_error << "), refusing to continue";
  446. return;
  447. }
  448. if (record.size() == 0) {
  449. return;
  450. }
  451. #ifdef TLS_DEBUG
  452. dbg() << "Consuming " << record.size() << " bytes";
  453. #endif
  454. m_context.message_buffer.append(record.data(), record.size());
  455. size_t index { 0 };
  456. size_t buffer_length = m_context.message_buffer.size();
  457. size_t size_offset { 3 }; // read the common record header
  458. size_t header_size { 5 };
  459. #ifdef TLS_DEBUG
  460. dbg() << "message buffer length " << buffer_length;
  461. #endif
  462. while (buffer_length >= 5) {
  463. auto length = convert_between_host_and_network(*(u16*)m_context.message_buffer.offset_pointer(index + size_offset)) + header_size;
  464. if (length > buffer_length) {
  465. #ifdef TLS_DEBUG
  466. dbg() << "Need more data: " << length << " | " << buffer_length;
  467. #endif
  468. break;
  469. }
  470. auto consumed = handle_message(m_context.message_buffer.slice_view(index, length));
  471. #ifdef TLS_DEBUG
  472. if (consumed > 0)
  473. dbg() << "consumed " << (size_t)consumed << " bytes";
  474. else
  475. dbg() << "error: " << (int)consumed;
  476. #endif
  477. if (consumed != (i8)Error::NeedMoreData) {
  478. if (consumed < 0) {
  479. dbg() << "Consumed an error: " << (int)consumed;
  480. if (!m_context.critical_error)
  481. m_context.critical_error = (i8)consumed;
  482. m_context.error_code = (Error)consumed;
  483. break;
  484. }
  485. } else {
  486. continue;
  487. }
  488. index += length;
  489. buffer_length -= length;
  490. if (m_context.critical_error) {
  491. dbg() << "Broken connection";
  492. m_context.error_code = Error::BrokenConnection;
  493. break;
  494. }
  495. }
  496. if (m_context.error_code != Error::NoError && m_context.error_code != Error::NeedMoreData) {
  497. dbg() << "consume error: " << (i8)m_context.error_code;
  498. m_context.message_buffer.clear();
  499. return;
  500. }
  501. if (index) {
  502. m_context.message_buffer = m_context.message_buffer.slice(index, m_context.message_buffer.size() - index);
  503. }
  504. }
  505. void TLSv12::ensure_hmac(size_t digest_size, bool local)
  506. {
  507. if (local && m_hmac_local)
  508. return;
  509. if (!local && m_hmac_remote)
  510. return;
  511. auto hash_kind = Crypto::Hash::HashKind::None;
  512. switch (digest_size) {
  513. case Crypto::Hash::SHA1::DigestSize:
  514. hash_kind = Crypto::Hash::HashKind::SHA1;
  515. break;
  516. case Crypto::Hash::SHA256::DigestSize:
  517. hash_kind = Crypto::Hash::HashKind::SHA256;
  518. break;
  519. case Crypto::Hash::SHA512::DigestSize:
  520. hash_kind = Crypto::Hash::HashKind::SHA512;
  521. break;
  522. default:
  523. dbg() << "Failed to find a suitable hash for size " << digest_size;
  524. break;
  525. }
  526. auto hmac = make<Crypto::Authentication::HMAC<Crypto::Hash::Manager>>(ByteBuffer::wrap(local ? m_context.crypto.local_mac : m_context.crypto.remote_mac, digest_size), hash_kind);
  527. if (local)
  528. m_hmac_local = move(hmac);
  529. else
  530. m_hmac_remote = move(hmac);
  531. }
  532. bool Certificate::is_valid() const
  533. {
  534. auto now = Core::DateTime::now();
  535. if (!not_before.is_empty()) {
  536. if (now.is_before(not_before)) {
  537. dbg() << "certificate expired (not yet valid, signed for " << not_before << ")";
  538. return false;
  539. }
  540. }
  541. if (!not_after.is_empty()) {
  542. if (!now.is_before(not_after)) {
  543. dbg() << "certificate expired (expiry date " << not_after << ")";
  544. return false;
  545. }
  546. }
  547. return true;
  548. }
  549. void TLSv12::try_disambiguate_error() const
  550. {
  551. dbg() << "Possible failure cause: ";
  552. switch ((AlertDescription)m_context.critical_error) {
  553. case AlertDescription::HandshakeFailure:
  554. if (!m_context.cipher_spec_set) {
  555. dbg() << "- No cipher suite in common with " << m_context.SNI;
  556. } else {
  557. dbg() << "- Unknown internal issue";
  558. }
  559. break;
  560. case AlertDescription::InsufficientSecurity:
  561. dbg() << "- No cipher suite in common with " << m_context.SNI << " (the server is oh so secure)";
  562. break;
  563. case AlertDescription::DecryptionFailed:
  564. dbg() << "- Bad MAC record from our side";
  565. dbg() << "- Bad block cipher padding";
  566. break;
  567. default:
  568. dbg() << "- No one knows";
  569. break;
  570. }
  571. }
  572. TLSv12::TLSv12(Core::Object* parent, Version version)
  573. : Core::Socket(Core::Socket::Type::TCP, parent)
  574. {
  575. m_context.version = version;
  576. m_context.is_server = false;
  577. m_context.tls_buffer = ByteBuffer::create_uninitialized(0);
  578. int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  579. if (fd < 0) {
  580. set_error(errno);
  581. } else {
  582. set_fd(fd);
  583. set_mode(IODevice::ReadWrite);
  584. set_error(0);
  585. }
  586. }
  587. }