2
0

ServerHello.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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::ServerHello;
  55. use parent 'TLSProxy::Message';
  56. sub new
  57. {
  58. my $class = shift;
  59. my ($server,
  60. $data,
  61. $records,
  62. $startoffset,
  63. $message_frag_lens) = @_;
  64. my $self = $class->SUPER::new(
  65. $server,
  66. TLSProxy::Message::MT_SERVER_HELLO,
  67. $data,
  68. $records,
  69. $startoffset,
  70. $message_frag_lens);
  71. $self->{server_version} = 0;
  72. $self->{random} = [];
  73. $self->{session_id_len} = 0;
  74. $self->{session} = "";
  75. $self->{ciphersuite} = 0;
  76. $self->{comp_meth} = 0;
  77. $self->{extensions_data} = "";
  78. return $self;
  79. }
  80. sub parse
  81. {
  82. my $self = shift;
  83. my $ptr = 2;
  84. my ($server_version) = unpack('n', $self->data);
  85. my $random = substr($self->data, $ptr, 32);
  86. $ptr += 32;
  87. my $session_id_len = unpack('C', substr($self->data, $ptr));
  88. $ptr++;
  89. my $session = substr($self->data, $ptr, $session_id_len);
  90. $ptr += $session_id_len;
  91. my $ciphersuite = unpack('n', substr($self->data, $ptr));
  92. $ptr += 2;
  93. my $comp_meth = unpack('C', substr($self->data, $ptr));
  94. $ptr++;
  95. my $extensions_len = unpack('n', substr($self->data, $ptr));
  96. $ptr += 2;
  97. #For now we just deal with this as a block of data. In the future we will
  98. #want to parse this
  99. my $extension_data = substr($self->data, $ptr);
  100. if (length($extension_data) != $extensions_len) {
  101. die "Invalid extension length\n";
  102. }
  103. my %extensions = ();
  104. while (length($extension_data) >= 4) {
  105. my ($type, $size) = unpack("nn", $extension_data);
  106. my $extdata = substr($extension_data, 4, $size);
  107. $extension_data = substr($extension_data, 4 + $size);
  108. $extensions{$type} = $extdata;
  109. }
  110. $self->server_version($server_version);
  111. $self->random($random);
  112. $self->session_id_len($session_id_len);
  113. $self->session($session);
  114. $self->ciphersuite($ciphersuite);
  115. $self->comp_meth($comp_meth);
  116. $self->extension_data(\%extensions);
  117. $self->process_data();
  118. print " Server Version:".$server_version."\n";
  119. print " Session ID Len:".$session_id_len."\n";
  120. print " Ciphersuite:".$ciphersuite."\n";
  121. print " Compression Method:".$comp_meth."\n";
  122. print " Extensions Len:".$extensions_len."\n";
  123. }
  124. #Perform any actions necessary based on the data we've seen
  125. sub process_data
  126. {
  127. my $self = shift;
  128. TLSProxy::Message->ciphersuite($self->ciphersuite);
  129. }
  130. #Reconstruct the on-the-wire message data following changes
  131. sub set_message_contents
  132. {
  133. my $self = shift;
  134. my $data;
  135. my $extensions = "";
  136. $data = pack('n', $self->server_version);
  137. $data .= $self->random;
  138. $data .= pack('C', $self->session_id_len);
  139. $data .= $self->session;
  140. $data .= pack('n', $self->ciphersuite);
  141. $data .= pack('C', $self->comp_meth);
  142. foreach my $key (keys %{$self->extension_data}) {
  143. my $extdata = ${$self->extension_data}{$key};
  144. $extensions .= pack("n", $key);
  145. $extensions .= pack("n", length($extdata));
  146. $extensions .= $extdata;
  147. }
  148. $data .= pack('n', length($extensions));
  149. $data .= $extensions;
  150. $self->data($data);
  151. }
  152. #Read/write accessors
  153. sub server_version
  154. {
  155. my $self = shift;
  156. if (@_) {
  157. $self->{client_version} = shift;
  158. }
  159. return $self->{client_version};
  160. }
  161. sub random
  162. {
  163. my $self = shift;
  164. if (@_) {
  165. $self->{random} = shift;
  166. }
  167. return $self->{random};
  168. }
  169. sub session_id_len
  170. {
  171. my $self = shift;
  172. if (@_) {
  173. $self->{session_id_len} = shift;
  174. }
  175. return $self->{session_id_len};
  176. }
  177. sub session
  178. {
  179. my $self = shift;
  180. if (@_) {
  181. $self->{session} = shift;
  182. }
  183. return $self->{session};
  184. }
  185. sub ciphersuite
  186. {
  187. my $self = shift;
  188. if (@_) {
  189. $self->{ciphersuite} = shift;
  190. }
  191. return $self->{ciphersuite};
  192. }
  193. sub comp_meth
  194. {
  195. my $self = shift;
  196. if (@_) {
  197. $self->{comp_meth} = shift;
  198. }
  199. return $self->{comp_meth};
  200. }
  201. sub extension_data
  202. {
  203. my $self = shift;
  204. if (@_) {
  205. $self->{extension_data} = shift;
  206. }
  207. return $self->{extension_data};
  208. }
  209. sub set_extension
  210. {
  211. my ($self, $ext_type, $ext_data) = @_;
  212. $self->{extension_data}{$ext_type} = $ext_data;
  213. }
  214. sub delete_extension
  215. {
  216. my ($self, $ext_type) = @_;
  217. delete $self->{extension_data}{$ext_type};
  218. }
  219. 1;