ServerKeyExchange.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License 2.0 (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 ($isdtls,
  15. $server,
  16. $msgseq,
  17. $msgfrag,
  18. $msgfragoffs,
  19. $data,
  20. $records,
  21. $startoffset,
  22. $message_frag_lens) = @_;
  23. my $self = $class->SUPER::new(
  24. $isdtls,
  25. $server,
  26. TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
  27. $msgseq,
  28. $msgfrag,
  29. $msgfragoffs,
  30. $data,
  31. $records,
  32. $startoffset,
  33. $message_frag_lens);
  34. #DHE
  35. $self->{p} = "";
  36. $self->{g} = "";
  37. $self->{pub_key} = "";
  38. $self->{sigalg} = -1;
  39. $self->{sig} = "";
  40. return $self;
  41. }
  42. sub parse
  43. {
  44. my $self = shift;
  45. my $sigalg = -1;
  46. #Minimal SKE parsing. Only supports one known DHE ciphersuite at the moment
  47. return if TLSProxy::Proxy->ciphersuite()
  48. != TLSProxy::Message::CIPHER_ADH_AES_128_SHA
  49. && TLSProxy::Proxy->ciphersuite()
  50. != TLSProxy::Message::CIPHER_DHE_RSA_AES_128_SHA;
  51. my $p_len = unpack('n', $self->data);
  52. my $ptr = 2;
  53. my $p = substr($self->data, $ptr, $p_len);
  54. $ptr += $p_len;
  55. my $g_len = unpack('n', substr($self->data, $ptr));
  56. $ptr += 2;
  57. my $g = substr($self->data, $ptr, $g_len);
  58. $ptr += $g_len;
  59. my $pub_key_len = unpack('n', substr($self->data, $ptr));
  60. $ptr += 2;
  61. my $pub_key = substr($self->data, $ptr, $pub_key_len);
  62. $ptr += $pub_key_len;
  63. #We assume its signed
  64. my $record = ${$self->records}[0];
  65. if (TLSProxy::Proxy->is_tls13()
  66. || $record->version() == TLSProxy::Record::VERS_TLS_1_2) {
  67. $sigalg = unpack('n', substr($self->data, $ptr));
  68. $ptr += 2;
  69. }
  70. my $sig = "";
  71. if (defined $sigalg) {
  72. my $sig_len = unpack('n', substr($self->data, $ptr));
  73. if (defined $sig_len) {
  74. $ptr += 2;
  75. $sig = substr($self->data, $ptr, $sig_len);
  76. $ptr += $sig_len;
  77. }
  78. }
  79. $self->p($p);
  80. $self->g($g);
  81. $self->pub_key($pub_key);
  82. $self->sigalg($sigalg) if defined $sigalg;
  83. $self->signature($sig);
  84. }
  85. #Reconstruct the on-the-wire message data following changes
  86. sub set_message_contents
  87. {
  88. my $self = shift;
  89. my $data;
  90. $data = pack('n', length($self->p));
  91. $data .= $self->p;
  92. $data .= pack('n', length($self->g));
  93. $data .= $self->g;
  94. $data .= pack('n', length($self->pub_key));
  95. $data .= $self->pub_key;
  96. $data .= pack('n', $self->sigalg) if ($self->sigalg != -1);
  97. if (length($self->signature) > 0) {
  98. $data .= pack('n', length($self->signature));
  99. $data .= $self->signature;
  100. }
  101. $self->data($data);
  102. }
  103. #Read/write accessors
  104. #DHE
  105. sub p
  106. {
  107. my $self = shift;
  108. if (@_) {
  109. $self->{p} = shift;
  110. }
  111. return $self->{p};
  112. }
  113. sub g
  114. {
  115. my $self = shift;
  116. if (@_) {
  117. $self->{g} = shift;
  118. }
  119. return $self->{g};
  120. }
  121. sub pub_key
  122. {
  123. my $self = shift;
  124. if (@_) {
  125. $self->{pub_key} = shift;
  126. }
  127. return $self->{pub_key};
  128. }
  129. sub sigalg
  130. {
  131. my $self = shift;
  132. if (@_) {
  133. $self->{sigalg} = shift;
  134. }
  135. return $self->{sigalg};
  136. }
  137. sub signature
  138. {
  139. my $self = shift;
  140. if (@_) {
  141. $self->{sig} = shift;
  142. }
  143. return $self->{sig};
  144. }
  145. 1;