Message.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. # Written by Matt Caswell for the OpenSSL project.
  2. # ====================================================================
  3. # Copyright (c) 1998-2015 The OpenSSL Project. 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
  7. # are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # 3. All advertising materials mentioning features or use of this
  18. # software must display the following acknowledgment:
  19. # "This product includes software developed by the OpenSSL Project
  20. # for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. #
  22. # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. # endorse or promote products derived from this software without
  24. # prior written permission. For written permission, please contact
  25. # openssl-core@openssl.org.
  26. #
  27. # 5. Products derived from this software may not be called "OpenSSL"
  28. # nor may "OpenSSL" appear in their names without prior written
  29. # permission of the OpenSSL Project.
  30. #
  31. # 6. Redistributions of any form whatsoever must retain the following
  32. # acknowledgment:
  33. # "This product includes software developed by the OpenSSL Project
  34. # for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. #
  36. # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. # OF THE POSSIBILITY OF SUCH DAMAGE.
  48. # ====================================================================
  49. #
  50. # This product includes cryptographic software written by Eric Young
  51. # (eay@cryptsoft.com). This product includes software written by Tim
  52. # Hudson (tjh@cryptsoft.com).
  53. use strict;
  54. package TLSProxy::Message;
  55. use constant TLS_MESSAGE_HEADER_LENGTH => 4;
  56. #Message types
  57. use constant {
  58. MT_HELLO_REQUEST => 0,
  59. MT_CLIENT_HELLO => 1,
  60. MT_SERVER_HELLO => 2,
  61. MT_NEW_SESSION_TICKET => 4,
  62. MT_CERTIFICATE => 11,
  63. MT_SERVER_KEY_EXCHANGE => 12,
  64. MT_CERTIFICATE_REQUEST => 13,
  65. MT_SERVER_HELLO_DONE => 14,
  66. MT_CERTIFICATE_VERIFY => 15,
  67. MT_CLIENT_KEY_EXCHANGE => 16,
  68. MT_FINISHED => 20,
  69. MT_CERTIFICATE_STATUS => 22,
  70. MT_NEXT_PROTO => 67
  71. };
  72. #Alert levels
  73. use constant {
  74. AL_LEVEL_WARN => 1,
  75. AL_LEVEL_FATAL => 2
  76. };
  77. #Alert descriptions
  78. use constant {
  79. AL_DESC_CLOSE_NOTIFY => 0
  80. };
  81. my %message_type = (
  82. MT_HELLO_REQUEST, "HelloRequest",
  83. MT_CLIENT_HELLO, "ClientHello",
  84. MT_SERVER_HELLO, "ServerHello",
  85. MT_NEW_SESSION_TICKET, "NewSessionTicket",
  86. MT_CERTIFICATE, "Certificate",
  87. MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange",
  88. MT_CERTIFICATE_REQUEST, "CertificateRequest",
  89. MT_SERVER_HELLO_DONE, "ServerHelloDone",
  90. MT_CERTIFICATE_VERIFY, "CertificateVerify",
  91. MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange",
  92. MT_FINISHED, "Finished",
  93. MT_CERTIFICATE_STATUS, "CertificateStatus",
  94. MT_NEXT_PROTO, "NextProto"
  95. );
  96. my $payload = "";
  97. my $messlen = -1;
  98. my $mt;
  99. my $startoffset = -1;
  100. my $server = 0;
  101. my $success = 0;
  102. my $end = 0;
  103. my @message_rec_list = ();
  104. my @message_frag_lens = ();
  105. my $ciphersuite = 0;
  106. sub clear
  107. {
  108. $payload = "";
  109. $messlen = -1;
  110. $startoffset = -1;
  111. $server = 0;
  112. $success = 0;
  113. $end = 0;
  114. @message_rec_list = ();
  115. @message_frag_lens = ();
  116. }
  117. #Class method to extract messages from a record
  118. sub get_messages
  119. {
  120. my $class = shift;
  121. my $serverin = shift;
  122. my $record = shift;
  123. my @messages = ();
  124. my $message;
  125. @message_frag_lens = ();
  126. if ($serverin != $server && length($payload) != 0) {
  127. die "Changed peer, but we still have fragment data\n";
  128. }
  129. $server = $serverin;
  130. if ($record->content_type == TLSProxy::Record::RT_CCS) {
  131. if ($payload ne "") {
  132. #We can't handle this yet
  133. die "CCS received before message data complete\n";
  134. }
  135. if ($server) {
  136. TLSProxy::Record->server_ccs_seen(1);
  137. } else {
  138. TLSProxy::Record->client_ccs_seen(1);
  139. }
  140. } elsif ($record->content_type == TLSProxy::Record::RT_HANDSHAKE) {
  141. if ($record->len == 0 || $record->len_real == 0) {
  142. print " Message truncated\n";
  143. } else {
  144. my $recoffset = 0;
  145. if (length $payload > 0) {
  146. #We are continuing processing a message started in a previous
  147. #record. Add this record to the list associated with this
  148. #message
  149. push @message_rec_list, $record;
  150. if ($messlen <= length($payload)) {
  151. #Shouldn't happen
  152. die "Internal error: invalid messlen: ".$messlen
  153. ." payload length:".length($payload)."\n";
  154. }
  155. if (length($payload) + $record->decrypt_len >= $messlen) {
  156. #We can complete the message with this record
  157. $recoffset = $messlen - length($payload);
  158. $payload .= substr($record->decrypt_data, 0, $recoffset);
  159. push @message_frag_lens, $recoffset;
  160. $message = create_message($server, $mt, $payload,
  161. $startoffset);
  162. push @messages, $message;
  163. $payload = "";
  164. } else {
  165. #This is just part of the total message
  166. $payload .= $record->decrypt_data;
  167. $recoffset = $record->decrypt_len;
  168. push @message_frag_lens, $record->decrypt_len;
  169. }
  170. print " Partial message data read: ".$recoffset." bytes\n";
  171. }
  172. while ($record->decrypt_len > $recoffset) {
  173. #We are at the start of a new message
  174. if ($record->decrypt_len - $recoffset < 4) {
  175. #Whilst technically probably valid we can't cope with this
  176. die "End of record in the middle of a message header\n";
  177. }
  178. @message_rec_list = ($record);
  179. my $lenhi;
  180. my $lenlo;
  181. ($mt, $lenhi, $lenlo) = unpack('CnC',
  182. substr($record->decrypt_data,
  183. $recoffset));
  184. $messlen = ($lenhi << 8) | $lenlo;
  185. print " Message type: $message_type{$mt}\n";
  186. print " Message Length: $messlen\n";
  187. $startoffset = $recoffset;
  188. $recoffset += 4;
  189. $payload = "";
  190. if ($recoffset < $record->decrypt_len) {
  191. #Some payload data is present in this record
  192. if ($record->decrypt_len - $recoffset >= $messlen) {
  193. #We can complete the message with this record
  194. $payload .= substr($record->decrypt_data, $recoffset,
  195. $messlen);
  196. $recoffset += $messlen;
  197. push @message_frag_lens, $messlen;
  198. $message = create_message($server, $mt, $payload,
  199. $startoffset);
  200. push @messages, $message;
  201. $payload = "";
  202. } else {
  203. #This is just part of the total message
  204. $payload .= substr($record->decrypt_data, $recoffset,
  205. $record->decrypt_len - $recoffset);
  206. $recoffset = $record->decrypt_len;
  207. push @message_frag_lens, $recoffset;
  208. }
  209. }
  210. }
  211. }
  212. } elsif ($record->content_type == TLSProxy::Record::RT_APPLICATION_DATA) {
  213. print " [ENCRYPTED APPLICATION DATA]\n";
  214. print " [".$record->decrypt_data."]\n";
  215. } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
  216. my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
  217. #All alerts end the test
  218. $end = 1;
  219. #A CloseNotify from the client indicates we have finished successfully
  220. #(we assume)
  221. if (!$server && $alertlev == AL_LEVEL_WARN
  222. && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
  223. $success = 1;
  224. }
  225. }
  226. return @messages;
  227. }
  228. #Function to work out which sub-class we need to create and then
  229. #construct it
  230. sub create_message
  231. {
  232. my ($server, $mt, $data, $startoffset) = @_;
  233. my $message;
  234. #We only support ClientHello in this version...needs to be extended for
  235. #others
  236. if ($mt == MT_CLIENT_HELLO) {
  237. $message = TLSProxy::ClientHello->new(
  238. $server,
  239. $data,
  240. [@message_rec_list],
  241. $startoffset,
  242. [@message_frag_lens]
  243. );
  244. $message->parse();
  245. } elsif ($mt == MT_SERVER_HELLO) {
  246. $message = TLSProxy::ServerHello->new(
  247. $server,
  248. $data,
  249. [@message_rec_list],
  250. $startoffset,
  251. [@message_frag_lens]
  252. );
  253. $message->parse();
  254. } elsif ($mt == MT_SERVER_KEY_EXCHANGE) {
  255. $message = TLSProxy::ServerKeyExchange->new(
  256. $server,
  257. $data,
  258. [@message_rec_list],
  259. $startoffset,
  260. [@message_frag_lens]
  261. );
  262. $message->parse();
  263. } elsif ($mt == MT_NEW_SESSION_TICKET) {
  264. $message = TLSProxy::NewSessionTicket->new(
  265. $server,
  266. $data,
  267. [@message_rec_list],
  268. $startoffset,
  269. [@message_frag_lens]
  270. );
  271. $message->parse();
  272. } else {
  273. #Unknown message type
  274. $message = TLSProxy::Message->new(
  275. $server,
  276. $mt,
  277. $data,
  278. [@message_rec_list],
  279. $startoffset,
  280. [@message_frag_lens]
  281. );
  282. }
  283. return $message;
  284. }
  285. sub end
  286. {
  287. my $class = shift;
  288. return $end;
  289. }
  290. sub success
  291. {
  292. my $class = shift;
  293. return $success;
  294. }
  295. sub fail
  296. {
  297. my $class = shift;
  298. return !$success && $end;
  299. }
  300. sub new
  301. {
  302. my $class = shift;
  303. my ($server,
  304. $mt,
  305. $data,
  306. $records,
  307. $startoffset,
  308. $message_frag_lens) = @_;
  309. my $self = {
  310. server => $server,
  311. data => $data,
  312. records => $records,
  313. mt => $mt,
  314. startoffset => $startoffset,
  315. message_frag_lens => $message_frag_lens
  316. };
  317. return bless $self, $class;
  318. }
  319. sub ciphersuite
  320. {
  321. my $class = shift;
  322. if (@_) {
  323. $ciphersuite = shift;
  324. }
  325. return $ciphersuite;
  326. }
  327. #Update all the underlying records with the modified data from this message
  328. #Note: Does not currently support re-encrypting
  329. sub repack
  330. {
  331. my $self = shift;
  332. my $msgdata;
  333. my $numrecs = $#{$self->records};
  334. $self->set_message_contents();
  335. my $lenhi;
  336. my $lenlo;
  337. $lenlo = length($self->data) & 0xff;
  338. $lenhi = length($self->data) >> 8;
  339. $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
  340. if ($numrecs == 0) {
  341. #The message is fully contained within one record
  342. my ($rec) = @{$self->records};
  343. my $recdata = $rec->decrypt_data;
  344. my $old_length;
  345. # We use empty message_frag_lens to indicates that pre-repacking,
  346. # the message wasn't present. The first fragment length doesn't include
  347. # the TLS header, so we need to check and compute the right length.
  348. if (@{$self->message_frag_lens}) {
  349. $old_length = ${$self->message_frag_lens}[0] +
  350. TLS_MESSAGE_HEADER_LENGTH;
  351. } else {
  352. $old_length = 0;
  353. }
  354. my $prefix = substr($recdata, 0, $self->startoffset);
  355. my $suffix = substr($recdata, $self->startoffset + $old_length);
  356. $rec->decrypt_data($prefix.($msgdata).($suffix));
  357. # TODO(openssl-team): don't keep explicit lengths.
  358. # (If a length override is ever needed to construct invalid packets,
  359. # use an explicit override field instead.)
  360. $rec->decrypt_len(length($rec->decrypt_data));
  361. $rec->len($rec->len + length($msgdata) - $old_length);
  362. # Don't support re-encryption.
  363. $rec->data($rec->decrypt_data);
  364. #Update the fragment len in case we changed it above
  365. ${$self->message_frag_lens}[0] = length($msgdata)
  366. - TLS_MESSAGE_HEADER_LENGTH;
  367. return;
  368. }
  369. #Note we don't currently support changing a fragmented message length
  370. my $recctr = 0;
  371. my $datadone = 0;
  372. foreach my $rec (@{$self->records}) {
  373. my $recdata = $rec->decrypt_data;
  374. if ($recctr == 0) {
  375. #This is the first record
  376. my $remainlen = length($recdata) - $self->startoffset;
  377. $rec->data(substr($recdata, 0, $self->startoffset)
  378. .substr(($msgdata), 0, $remainlen));
  379. $datadone += $remainlen;
  380. } elsif ($recctr + 1 == $numrecs) {
  381. #This is the last record
  382. $rec->data(substr($msgdata, $datadone));
  383. } else {
  384. #This is a middle record
  385. $rec->data(substr($msgdata, $datadone, length($rec->data)));
  386. $datadone += length($rec->data);
  387. }
  388. $recctr++;
  389. }
  390. }
  391. #To be overridden by sub-classes
  392. sub set_message_contents
  393. {
  394. }
  395. #Read only accessors
  396. sub server
  397. {
  398. my $self = shift;
  399. return $self->{server};
  400. }
  401. #Read/write accessors
  402. sub mt
  403. {
  404. my $self = shift;
  405. if (@_) {
  406. $self->{mt} = shift;
  407. }
  408. return $self->{mt};
  409. }
  410. sub data
  411. {
  412. my $self = shift;
  413. if (@_) {
  414. $self->{data} = shift;
  415. }
  416. return $self->{data};
  417. }
  418. sub records
  419. {
  420. my $self = shift;
  421. if (@_) {
  422. $self->{records} = shift;
  423. }
  424. return $self->{records};
  425. }
  426. sub startoffset
  427. {
  428. my $self = shift;
  429. if (@_) {
  430. $self->{startoffset} = shift;
  431. }
  432. return $self->{startoffset};
  433. }
  434. sub message_frag_lens
  435. {
  436. my $self = shift;
  437. if (@_) {
  438. $self->{message_frag_lens} = shift;
  439. }
  440. return $self->{message_frag_lens};
  441. }
  442. sub encoded_length
  443. {
  444. my $self = shift;
  445. return TLS_MESSAGE_HEADER_LENGTH + length($self->data);
  446. }
  447. 1;