TLSv12.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #ifdef TLS_DEBUG
  161. dbg() << "could not read the certificate";
  162. #endif
  163. return position;
  164. }
  165. position += octets;
  166. if (size - position < length) {
  167. #ifdef TLS_DEBUG
  168. dbg() << "not enough data for sequence";
  169. #endif
  170. return (i8)Error::NeedMoreData;
  171. }
  172. if (length && constructed) {
  173. switch (type) {
  174. case 0x03:
  175. break;
  176. case 0x10:
  177. if (level == 2 && index == 1) {
  178. cert_length = length + position - start_position;
  179. cert_data = buffer + start_position;
  180. }
  181. // public key data
  182. if (!cert.version && _asn1_is_field_present(fields, Constants::priv_der_id)) {
  183. temp = length + position - start_position;
  184. if (cert.der.size() < temp) {
  185. cert.der.grow(temp);
  186. } else {
  187. cert.der.trim(temp);
  188. }
  189. cert.der.overwrite(0, buffer + start_position, temp);
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. local_has_key = false;
  196. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  197. if ((local_has_key && (!context.is_server || client_cert)) || (client_cert || _asn1_is_field_present(fields, Constants::pk_id))) {
  198. temp = length + position - start_position;
  199. if (cert.der.size() < temp) {
  200. cert.der.grow(temp);
  201. } else {
  202. cert.der.trim(temp);
  203. }
  204. cert.der.overwrite(0, buffer + start_position, temp);
  205. }
  206. } else {
  207. switch (type) {
  208. case 0x00:
  209. return position;
  210. break;
  211. case 0x01:
  212. temp = buffer[position];
  213. break;
  214. case 0x02:
  215. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  216. if (has_key)
  217. *has_key = true;
  218. if (index == 1)
  219. cert.public_key.set(
  220. Crypto::UnsignedBigInteger::import_data(buffer + position, length),
  221. cert.public_key.public_exponent());
  222. else if (index == 2)
  223. cert.public_key.set(
  224. cert.public_key.modulus(),
  225. Crypto::UnsignedBigInteger::import_data(buffer + position, length));
  226. } else if (_asn1_is_field_present(fields, Constants::serial_id)) {
  227. cert.serial_number = Crypto::UnsignedBigInteger::import_data(buffer + position, length);
  228. }
  229. if (_asn1_is_field_present(fields, Constants::version_id)) {
  230. if (length == 1)
  231. cert.version = buffer[position];
  232. }
  233. // print_buffer(ByteBuffer::wrap(buffer + position, length));
  234. break;
  235. case 0x03:
  236. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  237. if (has_key)
  238. *has_key = true;
  239. }
  240. if (_asn1_is_field_present(fields, Constants::sign_id)) {
  241. auto* value = buffer + position;
  242. auto len = length;
  243. if (!value[0] && len % 2) {
  244. ++value;
  245. --len;
  246. }
  247. cert.sign_key = ByteBuffer::copy(value, len);
  248. } else {
  249. if (buffer[position] == 0 && length > 256) {
  250. _parse_asn1(context, cert, buffer + position + 1, length - 1, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  251. } else {
  252. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  253. }
  254. }
  255. break;
  256. case 0x04:
  257. _parse_asn1(context, cert, buffer + position, length, level + 1, fields, &local_has_key, client_cert, root_oid, &local_chain);
  258. break;
  259. case 0x05:
  260. break;
  261. case 0x06:
  262. if (_asn1_is_field_present(fields, Constants::pk_id)) {
  263. _set_algorithm(cert.key_algorithm, buffer + position, length);
  264. }
  265. if (_asn1_is_field_present(fields, Constants::algorithm_id)) {
  266. _set_algorithm(cert.algorithm, buffer + position, length);
  267. }
  268. if (length < 16)
  269. memcpy(oid, buffer + position, length);
  270. else
  271. memcpy(oid, buffer + position, 16);
  272. if (root_oid)
  273. memcpy(root_oid, oid, 16);
  274. break;
  275. case 0x09:
  276. break;
  277. case 0x17:
  278. case 0x018:
  279. // time
  280. // ignore
  281. break;
  282. case 0x013:
  283. case 0x0c:
  284. case 0x14:
  285. case 0x15:
  286. case 0x16:
  287. case 0x19:
  288. case 0x1a:
  289. case 0x1b:
  290. case 0x1c:
  291. case 0x1d:
  292. case 0x1e:
  293. // printable string and such
  294. if (_asn1_is_field_present(fields, Constants::issurer_id)) {
  295. if (_asn1_is_oid(oid, Constants::country_oid)) {
  296. cert.issuer_country = String { (const char*)buffer + position, length };
  297. } else if (_asn1_is_oid(oid, Constants::state_oid)) {
  298. cert.issuer_state = String { (const char*)buffer + position, length };
  299. } else if (_asn1_is_oid(oid, Constants::location_oid)) {
  300. cert.issuer_location = String { (const char*)buffer + position, length };
  301. } else if (_asn1_is_oid(oid, Constants::entity_oid)) {
  302. cert.issuer_entity = String { (const char*)buffer + position, length };
  303. } else if (_asn1_is_oid(oid, Constants::subject_oid)) {
  304. cert.issuer_subject = String { (const char*)buffer + position, length };
  305. }
  306. } else if (_asn1_is_field_present(fields, Constants::owner_id)) {
  307. if (_asn1_is_oid(oid, Constants::country_oid)) {
  308. cert.country = String { (const char*)buffer + position, length };
  309. } else if (_asn1_is_oid(oid, Constants::state_oid)) {
  310. cert.state = String { (const char*)buffer + position, length };
  311. } else if (_asn1_is_oid(oid, Constants::location_oid)) {
  312. cert.location = String { (const char*)buffer + position, length };
  313. } else if (_asn1_is_oid(oid, Constants::entity_oid)) {
  314. cert.entity = String { (const char*)buffer + position, length };
  315. } else if (_asn1_is_oid(oid, Constants::subject_oid)) {
  316. cert.subject = String { (const char*)buffer + position, length };
  317. }
  318. }
  319. break;
  320. default:
  321. // dbg() << "unused field " << type;
  322. break;
  323. }
  324. }
  325. position += length;
  326. }
  327. if (level == 2 && cert.sign_key.size() && cert_length && cert_data) {
  328. cert.fingerprint.clear();
  329. Crypto::Hash::Manager hash;
  330. switch (cert.key_algorithm) {
  331. case CertificateKeyAlgorithm::RSA_MD5:
  332. hash.initialize(Crypto::Hash::HashKind::MD5);
  333. break;
  334. case CertificateKeyAlgorithm::RSA_SHA1:
  335. hash.initialize(Crypto::Hash::HashKind::SHA1);
  336. break;
  337. case CertificateKeyAlgorithm::RSA_SHA256:
  338. hash.initialize(Crypto::Hash::HashKind::SHA256);
  339. break;
  340. case CertificateKeyAlgorithm::RSA_SHA512:
  341. hash.initialize(Crypto::Hash::HashKind::SHA512);
  342. break;
  343. default:
  344. #ifdef TLS_DEBUG
  345. dbg() << "Unsupported hash mode " << (u32)cert.key_algorithm;
  346. #endif
  347. // fallback to md5, it will fail later
  348. hash.initialize(Crypto::Hash::HashKind::MD5);
  349. break;
  350. }
  351. hash.update(cert_data, cert_length);
  352. auto fingerprint = hash.digest();
  353. cert.fingerprint.grow(fingerprint.data_length());
  354. cert.fingerprint.overwrite(0, fingerprint.immutable_data(), fingerprint.data_length());
  355. #ifdef TLS_DEBUG
  356. dbg() << "Certificate fingerprint:";
  357. print_buffer(cert.fingerprint);
  358. #endif
  359. }
  360. return position;
  361. }
  362. }
  363. Optional<Certificate> TLSv12::parse_asn1(const ByteBuffer& buffer, bool) const
  364. {
  365. // FIXME: Our ASN.1 parser is not quite up to the task of
  366. // parsing this X.509 certificate, so for the
  367. // time being, we will "parse" the certificate
  368. // manually right here.
  369. Certificate cert;
  370. u32 fields[0xff];
  371. _parse_asn1(m_context, cert, buffer.data(), buffer.size(), 1, fields, nullptr, 0, nullptr, nullptr);
  372. #ifdef TLS_DEBUG
  373. dbg() << "Certificate issued for " << cert.subject << " by " << cert.issuer_subject;
  374. #endif
  375. return cert;
  376. }
  377. ssize_t TLSv12::handle_certificate(const ByteBuffer& buffer)
  378. {
  379. ssize_t res = 0;
  380. if (buffer.size() < 3) {
  381. #ifdef TLS_DEBUG
  382. dbg() << "not enough certificate header data";
  383. #endif
  384. return (i8)Error::NeedMoreData;
  385. }
  386. u32 certificate_total_length = buffer[0] * 0x10000 + buffer[1] * 0x100 + buffer[2];
  387. #ifdef TLS_DEBUG
  388. dbg() << "total length: " << certificate_total_length;
  389. #endif
  390. if (certificate_total_length <= 4)
  391. return 3 * certificate_total_length;
  392. res += 3;
  393. if (certificate_total_length > buffer.size() - res) {
  394. #ifdef TLS_DEBUG
  395. dbg() << "not enough data for claimed total cert length";
  396. #endif
  397. return (i8)Error::NeedMoreData;
  398. }
  399. size_t size = certificate_total_length;
  400. size_t index = 0;
  401. bool valid_certificate = false;
  402. while (size > 0) {
  403. ++index;
  404. if (buffer.size() - res < 3) {
  405. #ifdef TLS_DEBUG
  406. dbg() << "not enough data for certificate length";
  407. #endif
  408. return (i8)Error::NeedMoreData;
  409. }
  410. size_t certificate_size = buffer[res] * 0x10000 + buffer[res + 1] * 0x100 + buffer[res + 2];
  411. res += 3;
  412. if (buffer.size() - res < certificate_size) {
  413. #ifdef TLS_DEBUG
  414. dbg() << "not enough data for certificate body";
  415. #endif
  416. return (i8)Error::NeedMoreData;
  417. }
  418. auto res_cert = res;
  419. auto remaining = certificate_size;
  420. size_t certificates_in_chain = 0;
  421. do {
  422. if (remaining <= 3) {
  423. dbg() << "Ran out of data";
  424. break;
  425. }
  426. ++certificates_in_chain;
  427. if (buffer.size() < (size_t)res_cert + 3) {
  428. dbg() << "not enough data to read cert size (" << buffer.size() << " < " << res_cert + 3 << ")";
  429. break;
  430. }
  431. size_t certificate_size_specific = buffer[res_cert] * 0x10000 + buffer[res_cert + 1] * 0x100 + buffer[res_cert + 2];
  432. res_cert += 3;
  433. remaining -= 3;
  434. if (certificate_size_specific > remaining) {
  435. dbg() << "invalid certificate size (expected " << remaining << " but got " << certificate_size_specific << ")";
  436. break;
  437. }
  438. remaining -= certificate_size_specific;
  439. auto certificate = parse_asn1(buffer.slice_view(res_cert, certificate_size_specific), false);
  440. if (certificate.has_value()) {
  441. m_context.certificates.append(certificate.value());
  442. valid_certificate = true;
  443. }
  444. res_cert += certificate_size_specific;
  445. } while (remaining > 0);
  446. if (remaining) {
  447. dbg() << "extraneous " << remaining << " bytes left over after parsing certificates";
  448. }
  449. size -= certificate_size + 3;
  450. res += certificate_size;
  451. }
  452. if (!valid_certificate)
  453. return (i8)Error::UnsupportedCertificate;
  454. if ((size_t)res != buffer.size())
  455. dbg() << "some data left unread: " << (size_t)res << " bytes out of " << buffer.size();
  456. return res;
  457. }
  458. void TLSv12::consume(const ByteBuffer& record)
  459. {
  460. if (m_context.critical_error) {
  461. dbg() << "There has been a critical error (" << (i8)m_context.critical_error << "), refusing to continue";
  462. return;
  463. }
  464. if (record.size() == 0) {
  465. return;
  466. }
  467. #ifdef TLS_DEBUG
  468. dbg() << "Consuming " << record.size() << " bytes";
  469. #endif
  470. m_context.message_buffer.append(record.data(), record.size());
  471. size_t index { 0 };
  472. size_t buffer_length = m_context.message_buffer.size();
  473. size_t size_offset { 3 }; // read the common record header
  474. size_t header_size { 5 };
  475. #ifdef TLS_DEBUG
  476. dbg() << "message buffer length " << buffer_length;
  477. #endif
  478. while (buffer_length >= 5) {
  479. auto length = convert_between_host_and_network(*(u16*)m_context.message_buffer.offset_pointer(index + size_offset)) + header_size;
  480. if (length > buffer_length) {
  481. #ifdef TLS_DEBUG
  482. dbg() << "Need more data: " << length << " | " << buffer_length;
  483. #endif
  484. break;
  485. }
  486. auto consumed = handle_message(m_context.message_buffer.slice_view(index, length));
  487. #ifdef TLS_DEBUG
  488. if (consumed > 0)
  489. dbg() << "consumed " << (size_t)consumed << " bytes";
  490. else
  491. dbg() << "error: " << (int)consumed;
  492. #endif
  493. if (consumed != (i8)Error::NeedMoreData) {
  494. if (consumed < 0) {
  495. dbg() << "Consumed an error: " << (int)consumed;
  496. if (!m_context.critical_error)
  497. m_context.critical_error = (i8)consumed;
  498. m_context.error_code = (Error)consumed;
  499. break;
  500. }
  501. } else {
  502. continue;
  503. }
  504. index += length;
  505. buffer_length -= length;
  506. if (m_context.critical_error) {
  507. dbg() << "Broken connection";
  508. m_context.error_code = Error::BrokenConnection;
  509. break;
  510. }
  511. }
  512. if (m_context.error_code != Error::NoError && m_context.error_code != Error::NeedMoreData) {
  513. dbg() << "consume error: " << (i8)m_context.error_code;
  514. m_context.message_buffer.clear();
  515. return;
  516. }
  517. if (index) {
  518. m_context.message_buffer = m_context.message_buffer.slice(index, m_context.message_buffer.size() - index);
  519. }
  520. }
  521. void TLSv12::ensure_hmac(size_t digest_size, bool local)
  522. {
  523. if (local && m_hmac_local)
  524. return;
  525. if (!local && m_hmac_remote)
  526. return;
  527. auto hash_kind = Crypto::Hash::HashKind::None;
  528. switch (digest_size) {
  529. case Crypto::Hash::SHA1::DigestSize:
  530. hash_kind = Crypto::Hash::HashKind::SHA1;
  531. break;
  532. case Crypto::Hash::SHA256::DigestSize:
  533. hash_kind = Crypto::Hash::HashKind::SHA256;
  534. break;
  535. case Crypto::Hash::SHA512::DigestSize:
  536. hash_kind = Crypto::Hash::HashKind::SHA512;
  537. break;
  538. default:
  539. dbg() << "Failed to find a suitable hash for size " << digest_size;
  540. break;
  541. }
  542. 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);
  543. if (local)
  544. m_hmac_local = move(hmac);
  545. else
  546. m_hmac_remote = move(hmac);
  547. }
  548. bool Certificate::is_valid() const
  549. {
  550. auto now = Core::DateTime::now();
  551. if (!not_before.is_empty()) {
  552. if (now.is_before(not_before)) {
  553. dbg() << "certificate expired (not yet valid, signed for " << not_before << ")";
  554. return false;
  555. }
  556. }
  557. if (!not_after.is_empty()) {
  558. if (!now.is_before(not_after)) {
  559. dbg() << "certificate expired (expiry date " << not_after << ")";
  560. return false;
  561. }
  562. }
  563. return true;
  564. }
  565. void TLSv12::try_disambiguate_error() const
  566. {
  567. dbg() << "Possible failure cause(s): ";
  568. switch ((AlertDescription)m_context.critical_error) {
  569. case AlertDescription::HandshakeFailure:
  570. if (!m_context.cipher_spec_set) {
  571. dbg() << "- No cipher suite in common with " << m_context.SNI;
  572. } else {
  573. dbg() << "- Unknown internal issue";
  574. }
  575. break;
  576. case AlertDescription::InsufficientSecurity:
  577. dbg() << "- No cipher suite in common with " << m_context.SNI << " (the server is oh so secure)";
  578. break;
  579. case AlertDescription::ProtocolVersion:
  580. dbg() << "- The server refused to negotiate with TLS 1.2 :(";
  581. break;
  582. case AlertDescription::UnexpectedMessage:
  583. dbg() << "- We sent an invalid message for the state we're in.";
  584. break;
  585. case AlertDescription::BadRecordMAC:
  586. dbg() << "- Bad MAC record from our side.";
  587. dbg() << "- Ciphertext wasn't an even multiple of the block length.";
  588. dbg() << "- Bad block cipher padding.";
  589. dbg() << "- If both sides are compliant, the only cause is messages being corrupted in the network.";
  590. break;
  591. case AlertDescription::RecordOverflow:
  592. dbg() << "- Sent a ciphertext record which has a length bigger than 18432 bytes.";
  593. dbg() << "- Sent record decrypted to a compressed record that has a length bigger than 18432 bytes.";
  594. dbg() << "- If both sides are compliant, the only cause is messages being corrupted in the network.";
  595. break;
  596. case AlertDescription::DecompressionFailure:
  597. dbg() << "- We sent invalid input for decompression (e.g. data that would expand to excessive length)";
  598. break;
  599. case AlertDescription::IllegalParameter:
  600. dbg() << "- We sent a parameter in the handshake that is out of range or inconsistent with the other parameters.";
  601. break;
  602. case AlertDescription::DecodeError:
  603. dbg() << "- The message we sent cannot be decoded because a field was out of range or the length was incorrect.";
  604. dbg() << "- If both sides are compliant, the only cause is messages being corrupted in the network.";
  605. break;
  606. case AlertDescription::DecryptError:
  607. dbg() << "- A handshake crypto operation failed. This includes signature verification and validating Finished.";
  608. break;
  609. case AlertDescription::AccessDenied:
  610. dbg() << "- The certificate is valid, but once access control was applied, the sender decided to stop negotiation.";
  611. break;
  612. case AlertDescription::InternalError:
  613. dbg() << "- No one knows, but it isn't a protocol failure.";
  614. break;
  615. case AlertDescription::DecryptionFailed:
  616. case AlertDescription::NoCertificate:
  617. case AlertDescription::ExportRestriction:
  618. dbg() << "- No one knows, the server sent a non-compliant alert.";
  619. break;
  620. default:
  621. dbg() << "- No one knows.";
  622. break;
  623. }
  624. }
  625. TLSv12::TLSv12(Core::Object* parent, Version version)
  626. : Core::Socket(Core::Socket::Type::TCP, parent)
  627. {
  628. m_context.version = version;
  629. m_context.is_server = false;
  630. m_context.tls_buffer = ByteBuffer::create_uninitialized(0);
  631. int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  632. if (fd < 0) {
  633. set_error(errno);
  634. } else {
  635. set_fd(fd);
  636. set_mode(IODevice::ReadWrite);
  637. set_error(0);
  638. }
  639. }
  640. }