ServerKeyExchange.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  3. # Licensed under the OpenSSL license (the "License"). You may not use
  4. # this file except in compliance with the License. You can obtain a copy
  5. # in the file LICENSE in the source distribution or at
  6. # https://www.openssl.org/source/license.html
  7. use strict;
  8. package TLSProxy::ServerKeyExchange;
  9. use vars '@ISA';
  10. push @ISA, 'TLSProxy::Message';
  11. sub new
  12. {
  13. my $class = shift;
  14. my ($server,
  15. $data,
  16. $records,
  17. $startoffset,
  18. $message_frag_lens) = @_;
  19. my $self = $class->SUPER::new(
  20. $server,
  21. TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
  22. $data,
  23. $records,
  24. $startoffset,
  25. $message_frag_lens);
  26. #DHE
  27. $self->{p} = "";
  28. $self->{g} = "";
  29. $self->{pub_key} = "";
  30. $self->{sigalg} = -1;
  31. $self->{sig} = "";
  32. return $self;
  33. }
  34. sub parse
  35. {
  36. my $self = shift;
  37. my $sigalg = -1;
  38. #Minimal SKE parsing. Only supports one known DHE ciphersuite at the moment
  39. return if TLSProxy::Proxy->ciphersuite()
  40. != TLSProxy::Message::CIPHER_ADH_AES_128_SHA
  41. && TLSProxy::Proxy->ciphersuite()
  42. != TLSProxy::Message::CIPHER_DHE_RSA_AES_128_SHA;
  43. my $p_len = unpack('n', $self->data);
  44. my $ptr = 2;
  45. my $p = substr($self->data, $ptr, $p_len);
  46. $ptr += $p_len;
  47. my $g_len = unpack('n', substr($self->data, $ptr));
  48. $ptr += 2;
  49. my $g = substr($self->data, $ptr, $g_len);
  50. $ptr += $g_len;
  51. my $pub_key_len = unpack('n', substr($self->data, $ptr));
  52. $ptr += 2;
  53. my $pub_key = substr($self->data, $ptr, $pub_key_len);
  54. $ptr += $pub_key_len;
  55. #We assume its signed
  56. my $record = ${$self->records}[0];
  57. if (TLSProxy::Proxy->is_tls13()
  58. || $record->version() == TLSProxy::Record::VERS_TLS_1_2) {
  59. $sigalg = unpack('n', substr($self->data, $ptr));
  60. $ptr += 2;
  61. }
  62. my $sig = "";
  63. if (defined $sigalg) {
  64. my $sig_len = unpack('n', substr($self->data, $ptr));
  65. if (defined $sig_len) {
  66. $ptr += 2;
  67. $sig = substr($self->data, $ptr, $sig_len);
  68. $ptr += $sig_len;
  69. }
  70. }
  71. $self->p($p);
  72. $self->g($g);
  73. $self->pub_key($pub_key);
  74. $self->sigalg($sigalg) if defined $sigalg;
  75. $self->signature($sig);
  76. }
  77. #Reconstruct the on-the-wire message data following changes
  78. sub set_message_contents
  79. {
  80. my $self = shift;
  81. my $data;
  82. $data = pack('n', length($self->p));
  83. $data .= $self->p;
  84. $data .= pack('n', length($self->g));
  85. $data .= $self->g;
  86. $data .= pack('n', length($self->pub_key));
  87. $data .= $self->pub_key;
  88. $data .= pack('n', $self->sigalg) if ($self->sigalg != -1);
  89. if (length($self->signature) > 0) {
  90. $data .= pack('n', length($self->signature));
  91. $data .= $self->signature;
  92. }
  93. $self->data($data);
  94. }
  95. #Read/write accessors
  96. #DHE
  97. sub p
  98. {
  99. my $self = shift;
  100. if (@_) {
  101. $self->{p} = shift;
  102. }
  103. return $self->{p};
  104. }
  105. sub g
  106. {
  107. my $self = shift;
  108. if (@_) {
  109. $self->{g} = shift;
  110. }
  111. return $self->{g};
  112. }
  113. sub pub_key
  114. {
  115. my $self = shift;
  116. if (@_) {
  117. $self->{pub_key} = shift;
  118. }
  119. return $self->{pub_key};
  120. }
  121. sub sigalg
  122. {
  123. my $self = shift;
  124. if (@_) {
  125. $self->{sigalg} = shift;
  126. }
  127. return $self->{sigalg};
  128. }
  129. sub signature
  130. {
  131. my $self = shift;
  132. if (@_) {
  133. $self->{sig} = shift;
  134. }
  135. return $self->{sig};
  136. }
  137. 1;