Certificate.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Copyright 2016 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::Certificate;
  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_CERTIFICATE,
  22. $data,
  23. $records,
  24. $startoffset,
  25. $message_frag_lens);
  26. $self->{first_certificate} = "";
  27. $self->{extension_data} = "";
  28. $self->{remaining_certdata} = "";
  29. return $self;
  30. }
  31. sub parse
  32. {
  33. my $self = shift;
  34. if (TLSProxy::Proxy->is_tls13()) {
  35. my $context_len = unpack('C', $self->data);
  36. my $context = substr($self->data, 1, $context_len);
  37. my $remdata = substr($self->data, 1 + $context_len);
  38. my ($hicertlistlen, $certlistlen) = unpack('Cn', $remdata);
  39. $certlistlen += ($hicertlistlen << 16);
  40. $remdata = substr($remdata, 3);
  41. die "Invalid Certificate List length"
  42. if length($remdata) != $certlistlen;
  43. my ($hicertlen, $certlen) = unpack('Cn', $remdata);
  44. $certlen += ($hicertlen << 16);
  45. die "Certificate too long" if ($certlen + 3) > $certlistlen;
  46. $remdata = substr($remdata, 3);
  47. my $certdata = substr($remdata, 0, $certlen);
  48. $remdata = substr($remdata, $certlen);
  49. my $extensions_len = unpack('n', $remdata);
  50. $remdata = substr($remdata, 2);
  51. die "Extensions too long"
  52. if ($certlen + 3 + $extensions_len + 2) > $certlistlen;
  53. my $extension_data = "";
  54. if ($extensions_len != 0) {
  55. $extension_data = substr($remdata, 0, $extensions_len);
  56. if (length($extension_data) != $extensions_len) {
  57. die "Invalid extension length\n";
  58. }
  59. }
  60. my %extensions = ();
  61. while (length($extension_data) >= 4) {
  62. my ($type, $size) = unpack("nn", $extension_data);
  63. my $extdata = substr($extension_data, 4, $size);
  64. $extension_data = substr($extension_data, 4 + $size);
  65. $extensions{$type} = $extdata;
  66. }
  67. $remdata = substr($remdata, $extensions_len);
  68. $self->context($context);
  69. $self->first_certificate($certdata);
  70. $self->extension_data(\%extensions);
  71. $self->remaining_certdata($remdata);
  72. print " Context:".$context."\n";
  73. print " Certificate List Len:".$certlistlen."\n";
  74. print " Certificate Len:".$certlen."\n";
  75. print " Extensions Len:".$extensions_len."\n";
  76. } else {
  77. my ($hicertlistlen, $certlistlen) = unpack('Cn', $self->data);
  78. $certlistlen += ($hicertlistlen << 16);
  79. my $remdata = substr($self->data, 3);
  80. die "Invalid Certificate List length"
  81. if length($remdata) != $certlistlen;
  82. my ($hicertlen, $certlen) = unpack('Cn', $remdata);
  83. $certlen += ($hicertlen << 16);
  84. die "Certificate too long" if ($certlen + 3) > $certlistlen;
  85. $remdata = substr($remdata, 3);
  86. my $certdata = substr($remdata, 0, $certlen);
  87. $remdata = substr($remdata, $certlen);
  88. $self->first_certificate($certdata);
  89. $self->remaining_certdata($remdata);
  90. print " Certificate List Len:".$certlistlen."\n";
  91. print " Certificate Len:".$certlen."\n";
  92. }
  93. }
  94. #Reconstruct the on-the-wire message data following changes
  95. sub set_message_contents
  96. {
  97. my $self = shift;
  98. my $data;
  99. my $extensions = "";
  100. if (TLSProxy::Proxy->is_tls13()) {
  101. foreach my $key (keys %{$self->extension_data}) {
  102. my $extdata = ${$self->extension_data}{$key};
  103. $extensions .= pack("n", $key);
  104. $extensions .= pack("n", length($extdata));
  105. $extensions .= $extdata;
  106. }
  107. $data = pack('C', length($self->context()));
  108. $data .= $self->context;
  109. my $certlen = length($self->first_certificate);
  110. my $certlistlen = $certlen + length($extensions)
  111. + length($self->remaining_certdata);
  112. my $hi = $certlistlen >> 16;
  113. $certlistlen = $certlistlen & 0xffff;
  114. $data .= pack('Cn', $hi, $certlistlen);
  115. $hi = $certlen >> 16;
  116. $certlen = $certlen & 0xffff;
  117. $data .= pack('Cn', $hi, $certlen);
  118. $data .= pack('n', length($extensions));
  119. $data .= $extensions;
  120. $data .= $self->remaining_certdata();
  121. $self->data($data);
  122. } else {
  123. my $certlen = length($self->first_certificate);
  124. my $certlistlen = $certlen + length($self->remaining_certdata);
  125. my $hi = $certlistlen >> 16;
  126. $certlistlen = $certlistlen & 0xffff;
  127. $data .= pack('Cn', $hi, $certlistlen);
  128. $hi = $certlen >> 16;
  129. $certlen = $certlen & 0xffff;
  130. $data .= pack('Cn', $hi, $certlen);
  131. $data .= $self->remaining_certdata();
  132. $self->data($data);
  133. }
  134. }
  135. #Read/write accessors
  136. sub context
  137. {
  138. my $self = shift;
  139. if (@_) {
  140. $self->{context} = shift;
  141. }
  142. return $self->{context};
  143. }
  144. sub first_certificate
  145. {
  146. my $self = shift;
  147. if (@_) {
  148. $self->{first_certificate} = shift;
  149. }
  150. return $self->{first_certificate};
  151. }
  152. sub remaining_certdata
  153. {
  154. my $self = shift;
  155. if (@_) {
  156. $self->{remaining_certdata} = shift;
  157. }
  158. return $self->{remaining_certdata};
  159. }
  160. sub extension_data
  161. {
  162. my $self = shift;
  163. if (@_) {
  164. $self->{extension_data} = shift;
  165. }
  166. return $self->{extension_data};
  167. }
  168. sub set_extension
  169. {
  170. my ($self, $ext_type, $ext_data) = @_;
  171. $self->{extension_data}{$ext_type} = $ext_data;
  172. }
  173. sub delete_extension
  174. {
  175. my ($self, $ext_type) = @_;
  176. delete $self->{extension_data}{$ext_type};
  177. }
  178. 1;