Exchange.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <LibCrypto/ASN1/DER.h>
  27. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  28. #include <LibTLS/TLSv12.h>
  29. namespace TLS {
  30. bool TLSv12::expand_key()
  31. {
  32. u8 key[192]; // soooooooo many constants
  33. auto key_buffer = Bytes { key, sizeof(key) };
  34. auto is_aead = this->is_aead();
  35. if (m_context.master_key.size() == 0) {
  36. dbgln("expand_key() with empty master key");
  37. return false;
  38. }
  39. auto key_size = key_length();
  40. auto mac_size = mac_length();
  41. auto iv_size = iv_length();
  42. pseudorandom_function(
  43. key_buffer,
  44. m_context.master_key,
  45. (const u8*)"key expansion", 13,
  46. ReadonlyBytes { m_context.remote_random, sizeof(m_context.remote_random) },
  47. ReadonlyBytes { m_context.local_random, sizeof(m_context.local_random) });
  48. size_t offset = 0;
  49. if (is_aead) {
  50. iv_size = 4; // Explicit IV size.
  51. } else {
  52. memcpy(m_context.crypto.local_mac, key + offset, mac_size);
  53. offset += mac_size;
  54. memcpy(m_context.crypto.remote_mac, key + offset, mac_size);
  55. offset += mac_size;
  56. }
  57. auto client_key = key + offset;
  58. offset += key_size;
  59. auto server_key = key + offset;
  60. offset += key_size;
  61. auto client_iv = key + offset;
  62. offset += iv_size;
  63. auto server_iv = key + offset;
  64. offset += iv_size;
  65. #ifdef TLS_DEBUG
  66. dbgln("client key");
  67. print_buffer(client_key, key_size);
  68. dbgln("server key");
  69. print_buffer(server_key, key_size);
  70. dbgln("client iv");
  71. print_buffer(client_iv, iv_size);
  72. dbgln("server iv");
  73. print_buffer(server_iv, iv_size);
  74. if (!is_aead) {
  75. dbgln("client mac key");
  76. print_buffer(m_context.crypto.local_mac, mac_size);
  77. dbgln("server mac key");
  78. print_buffer(m_context.crypto.remote_mac, mac_size);
  79. }
  80. #endif
  81. if (is_aead) {
  82. memcpy(m_context.crypto.local_aead_iv, client_iv, iv_size);
  83. memcpy(m_context.crypto.remote_aead_iv, server_iv, iv_size);
  84. m_aes_local.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ReadonlyBytes { client_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
  85. m_aes_remote.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ReadonlyBytes { server_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
  86. } else {
  87. memcpy(m_context.crypto.local_iv, client_iv, iv_size);
  88. memcpy(m_context.crypto.remote_iv, server_iv, iv_size);
  89. m_aes_local.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ReadonlyBytes { client_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
  90. m_aes_remote.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ReadonlyBytes { server_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
  91. }
  92. m_context.crypto.created = 1;
  93. return true;
  94. }
  95. void TLSv12::pseudorandom_function(Bytes output, ReadonlyBytes secret, const u8* label, size_t label_length, ReadonlyBytes seed, ReadonlyBytes seed_b)
  96. {
  97. if (!secret.size()) {
  98. dbgln("null secret");
  99. return;
  100. }
  101. // RFC 5246: "In this section, we define one PRF, based on HMAC. This PRF with the
  102. // SHA-256 hash function is used for all cipher suites defined in this
  103. // document and in TLS documents published prior to this document when
  104. // TLS 1.2 is negotiated."
  105. // Apparently this PRF _always_ uses SHA256
  106. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac(secret);
  107. auto l_seed_size = label_length + seed.size() + seed_b.size();
  108. u8 l_seed[l_seed_size];
  109. auto label_seed_buffer = Bytes { l_seed, l_seed_size };
  110. label_seed_buffer.overwrite(0, label, label_length);
  111. label_seed_buffer.overwrite(label_length, seed.data(), seed.size());
  112. label_seed_buffer.overwrite(label_length + seed.size(), seed_b.data(), seed_b.size());
  113. auto digest_size = hmac.digest_size();
  114. u8 digest[digest_size];
  115. auto digest_0 = Bytes { digest, digest_size };
  116. digest_0.overwrite(0, hmac.process(label_seed_buffer).immutable_data(), digest_size);
  117. size_t index = 0;
  118. while (index < output.size()) {
  119. hmac.update(digest_0);
  120. hmac.update(label_seed_buffer);
  121. auto digest_1 = hmac.digest();
  122. auto copy_size = min(digest_size, output.size() - index);
  123. output.overwrite(index, digest_1.immutable_data(), copy_size);
  124. index += copy_size;
  125. digest_0.overwrite(0, hmac.process(digest_0).immutable_data(), digest_size);
  126. }
  127. }
  128. bool TLSv12::compute_master_secret(size_t length)
  129. {
  130. if (m_context.premaster_key.size() == 0 || length < 48) {
  131. dbgln("there's no way I can make a master secret like this");
  132. dbgln("I'd like to talk to your manager about this length of {}", length);
  133. return false;
  134. }
  135. m_context.master_key.clear();
  136. m_context.master_key.grow(length);
  137. pseudorandom_function(
  138. m_context.master_key,
  139. m_context.premaster_key,
  140. (const u8*)"master secret", 13,
  141. ReadonlyBytes { m_context.local_random, sizeof(m_context.local_random) },
  142. ReadonlyBytes { m_context.remote_random, sizeof(m_context.remote_random) });
  143. m_context.premaster_key.clear();
  144. #ifdef TLS_DEBUG
  145. dbgln("master key:");
  146. print_buffer(m_context.master_key);
  147. #endif
  148. expand_key();
  149. return true;
  150. }
  151. ByteBuffer TLSv12::build_certificate()
  152. {
  153. PacketBuilder builder { MessageType::Handshake, m_context.version };
  154. Vector<const Certificate*> certificates;
  155. Vector<Certificate>* local_certificates = nullptr;
  156. if (m_context.is_server) {
  157. dbgln("Unsupported: Server mode");
  158. ASSERT_NOT_REACHED();
  159. } else {
  160. local_certificates = &m_context.client_certificates;
  161. }
  162. constexpr size_t der_length_delta = 3;
  163. constexpr size_t certificate_vector_header_size = 3;
  164. size_t total_certificate_size = 0;
  165. for (size_t i = 0; i < local_certificates->size(); ++i) {
  166. auto& certificate = local_certificates->at(i);
  167. if (!certificate.der.is_empty()) {
  168. total_certificate_size += certificate.der.size() + der_length_delta;
  169. // FIXME: Check for and respond with only the requested certificate types.
  170. if (true) {
  171. certificates.append(&certificate);
  172. }
  173. }
  174. }
  175. builder.append((u8)HandshakeType::CertificateMessage);
  176. if (!total_certificate_size) {
  177. #ifdef TLS_DEBUG
  178. dbgln("No certificates, sending empty certificate message");
  179. #endif
  180. builder.append_u24(certificate_vector_header_size);
  181. builder.append_u24(total_certificate_size);
  182. } else {
  183. builder.append_u24(total_certificate_size + certificate_vector_header_size); // 3 bytes for header
  184. builder.append_u24(total_certificate_size);
  185. for (auto& certificate : certificates) {
  186. if (!certificate->der.is_empty()) {
  187. builder.append_u24(certificate->der.size());
  188. builder.append(certificate->der.bytes());
  189. }
  190. }
  191. }
  192. auto packet = builder.build();
  193. update_packet(packet);
  194. return packet;
  195. }
  196. ByteBuffer TLSv12::build_change_cipher_spec()
  197. {
  198. PacketBuilder builder { MessageType::ChangeCipher, m_context.version, 64 };
  199. builder.append((u8)1);
  200. auto packet = builder.build();
  201. update_packet(packet);
  202. m_context.local_sequence_number = 0;
  203. return packet;
  204. }
  205. ByteBuffer TLSv12::build_server_key_exchange()
  206. {
  207. dbgln("FIXME: build_server_key_exchange");
  208. return {};
  209. }
  210. ByteBuffer TLSv12::build_client_key_exchange()
  211. {
  212. PacketBuilder builder { MessageType::Handshake, m_context.version };
  213. builder.append((u8)HandshakeType::ClientKeyExchange);
  214. build_random(builder);
  215. m_context.connection_status = ConnectionStatus::KeyExchange;
  216. auto packet = builder.build();
  217. update_packet(packet);
  218. return packet;
  219. }
  220. ssize_t TLSv12::handle_server_key_exchange(ReadonlyBytes)
  221. {
  222. dbgln("FIXME: parse_server_key_exchange");
  223. return 0;
  224. }
  225. ssize_t TLSv12::handle_verify(ReadonlyBytes)
  226. {
  227. dbgln("FIXME: parse_verify");
  228. return 0;
  229. }
  230. }