EncryptedExtensions.pm 2.7 KB

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