EncryptedExtensions.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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::EncryptedExtensions;
  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_ENCRYPTED_EXTENSIONS,
  22. $data,
  23. $records,
  24. $startoffset,
  25. $message_frag_lens);
  26. $self->{extension_data} = "";
  27. return $self;
  28. }
  29. sub parse
  30. {
  31. my $self = shift;
  32. my $extensions_len = unpack('n', $self->data);
  33. if (!defined $extensions_len) {
  34. $extensions_len = 0;
  35. }
  36. my $extension_data;
  37. if ($extensions_len != 0) {
  38. $extension_data = substr($self->data, 2);
  39. if (length($extension_data) != $extensions_len) {
  40. die "Invalid extension length\n";
  41. }
  42. } else {
  43. if (length($self->data) != 2) {
  44. die "Invalid extension length\n";
  45. }
  46. $extension_data = "";
  47. }
  48. my %extensions = ();
  49. while (length($extension_data) >= 4) {
  50. my ($type, $size) = unpack("nn", $extension_data);
  51. my $extdata = substr($extension_data, 4, $size);
  52. $extension_data = substr($extension_data, 4 + $size);
  53. $extensions{$type} = $extdata;
  54. }
  55. $self->extension_data(\%extensions);
  56. print " Extensions Len:".$extensions_len."\n";
  57. }
  58. #Reconstruct the on-the-wire message data following changes
  59. sub set_message_contents
  60. {
  61. my $self = shift;
  62. my $data;
  63. my $extensions = "";
  64. foreach my $key (keys %{$self->extension_data}) {
  65. my $extdata = ${$self->extension_data}{$key};
  66. $extensions .= pack("n", $key);
  67. $extensions .= pack("n", length($extdata));
  68. $extensions .= $extdata;
  69. }
  70. $data = pack('n', length($extensions));
  71. $data .= $extensions;
  72. $self->data($data);
  73. }
  74. #Read/write accessors
  75. sub extension_data
  76. {
  77. my $self = shift;
  78. if (@_) {
  79. $self->{extension_data} = shift;
  80. }
  81. return $self->{extension_data};
  82. }
  83. sub set_extension
  84. {
  85. my ($self, $ext_type, $ext_data) = @_;
  86. $self->{extension_data}{$ext_type} = $ext_data;
  87. }
  88. sub delete_extension
  89. {
  90. my ($self, $ext_type) = @_;
  91. delete $self->{extension_data}{$ext_type};
  92. }
  93. 1;