| 1 |
package POE::Filter::SSL; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Net::SSLeay; |
| 5 |
use POE qw (Filter::HTTPD Filter::Stackable Wheel::ReadWrite); |
| 6 |
use Scalar::Util qw(blessed); |
| 7 |
use Carp qw(carp confess); |
| 8 |
use POE; |
| 9 |
|
| 10 |
use vars qw($VERSION); |
| 11 |
$VERSION = '0.41'; |
| 12 |
sub DOSENDBACK () { 1 } |
| 13 |
|
| 14 |
our $globalinfos = {}; |
| 15 |
|
| 16 |
BEGIN { |
| 17 |
our $HANDSHAKE = 19; |
| 18 |
our $EVENT_FLUSHED = 20; |
| 19 |
our $EVENT_INPUT = 21; |
| 20 |
eval { |
| 21 |
require Net::SSLeay; |
| 22 |
Net::SSLeay->import( 1.30 ); |
| 23 |
}; |
| 24 |
Net::SSLeay::load_error_strings(); |
| 25 |
Net::SSLeay::SSLeay_add_ssl_algorithms(); |
| 26 |
Net::SSLeay::randomize(); |
| 27 |
|
| 28 |
no warnings 'redefine'; |
| 29 |
my $old_new = \&POE::Wheel::ReadWrite::new; |
| 30 |
*POE::Wheel::ReadWrite::new = sub { |
| 31 |
my $class = shift; |
| 32 |
my %arg = @_; |
| 33 |
my $self = $old_new->($class,%arg); |
| 34 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID]; |
| 35 |
$self->[$EVENT_INPUT] = $self->[POE::Wheel::ReadWrite::EVENT_INPUT]; |
| 36 |
$self->[POE::Wheel::ReadWrite::EVENT_INPUT] = ref($self) . "($unique_id) -> ssl handshake"; |
| 37 |
my $flushed_event = \$self->[POE::Wheel::ReadWrite::EVENT_FLUSHED]; |
| 38 |
my $temp_flushed_event = \$self->[$EVENT_FLUSHED]; |
| 39 |
my $temp_event_input = \$self->[$EVENT_INPUT]; |
| 40 |
my $filter_output = \$self->[POE::Wheel::ReadWrite::FILTER_OUTPUT]; |
| 41 |
my $driver = \$self->[POE::Wheel::ReadWrite::DRIVER_BOTH]; |
| 42 |
my $handle_output = \$self->[POE::Wheel::ReadWrite::HANDLE_OUTPUT]; |
| 43 |
$poe_kernel->state( |
| 44 |
$self->[$HANDSHAKE] = ref($self) . "($unique_id) -> ssl handshake", |
| 45 |
sub { |
| 46 |
if (checkForDoSendback($_[ARG0])) { |
| 47 |
unless (defined($$temp_flushed_event)) { |
| 48 |
$$temp_flushed_event = $$flushed_event; |
| 49 |
$$flushed_event = undef; |
| 50 |
} |
| 51 |
$$driver->put($$filter_output->put([$_[ARG0]])); |
| 52 |
$poe_kernel->select_resume_write($$handle_output); |
| 53 |
} else { |
| 54 |
$poe_kernel->call($_[SESSION], $$temp_event_input, $_[ARG0], $_[ARG1]); |
| 55 |
} |
| 56 |
} |
| 57 |
); |
| 58 |
return $self; |
| 59 |
}; |
| 60 |
my $old_rw_put = \&POE::Wheel::ReadWrite::put; |
| 61 |
*POE::Wheel::ReadWrite::put = sub { |
| 62 |
my $self = shift; |
| 63 |
my $unique_id = $self->[POE::Wheel::ReadWrite::UNIQUE_ID()]; |
| 64 |
if (defined($self->[$EVENT_FLUSHED])) { |
| 65 |
$self->[POE::Wheel::ReadWrite::EVENT_FLUSHED] = $self->[$EVENT_FLUSHED]; |
| 66 |
$self->[$EVENT_FLUSHED] = undef; |
| 67 |
} |
| 68 |
$old_rw_put->($self, @_); |
| 69 |
}; |
| 70 |
my $old_destroy = \&POE::Wheel::ReadWrite::DESTROY; |
| 71 |
*POE::Wheel::ReadWrite::DESTROY = sub { |
| 72 |
my $self = shift; |
| 73 |
if ($self->[$HANDSHAKE]) { |
| 74 |
$poe_kernel->state($self->[$HANDSHAKE]); |
| 75 |
$self->[$HANDSHAKE] = undef; |
| 76 |
} |
| 77 |
return $old_destroy->($self, @_); |
| 78 |
}; |
| 79 |
my $old_get_one = \&POE::Filter::Stackable::get_one; |
| 80 |
*POE::Filter::Stackable::get_one = sub { |
| 81 |
my ($self) = @_; |
| 82 |
my $return = [ ]; |
| 83 |
while (!@$return) { |
| 84 |
my $exchanged = 0; |
| 85 |
foreach my $filter (@{$self->[POE::Filter::Stackable::FILTERS]}) { |
| 86 |
# If we have something to input to the next filter, do that. |
| 87 |
if (@$return) { |
| 88 |
$filter->get_one_start($return); |
| 89 |
$exchanged++; |
| 90 |
} |
| 91 |
# Get what we can from the current filter. |
| 92 |
$return = $filter->get_one(); |
| 93 |
# This is the only inserted line: |
| 94 |
return $return if (checkForDoSendback($return) && ($return->[0] eq $filter)); |
| 95 |
} |
| 96 |
last unless $exchanged; |
| 97 |
} |
| 98 |
return $return; |
| 99 |
}; |
| 100 |
my $old_get_one_start = \&POE::Filter::Stackable::get_one_start; |
| 101 |
*POE::Filter::Stackable::get_one_start = sub { |
| 102 |
my $self = shift; |
| 103 |
(exists($self->[POE::Filter::Stackable::FILTERS]->[0])) ? $old_get_one_start->($self, @_) : [] |
| 104 |
}; |
| 105 |
my $old_put = \&POE::Filter::Stackable::put; |
| 106 |
*POE::Filter::Stackable::put = sub { |
| 107 |
my $self = shift; |
| 108 |
my $data = shift; |
| 109 |
my $found = 0; |
| 110 |
if (checkForDoSendback($data)) { |
| 111 |
foreach my $filter (@{$self->[POE::Filter::Stackable::FILTERS]}) { |
| 112 |
if ($data->[0] eq $filter) { |
| 113 |
$found++; |
| 114 |
last; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
if ($found) { |
| 119 |
my $ok = 0; |
| 120 |
foreach my $filter (reverse @{$self->[POE::Filter::Stackable::FILTERS]}) { |
| 121 |
next unless ($ok || (($filter eq $data->[0]) && checkForDoSendback($data))); |
| 122 |
$ok++; |
| 123 |
$data = $filter->put($data); |
| 124 |
last unless @$data; |
| 125 |
} |
| 126 |
$data; |
| 127 |
} else { |
| 128 |
$old_put->($self, $data, @_); |
| 129 |
} |
| 130 |
}; |
| 131 |
use warnings 'redefine'; |
| 132 |
} |
| 133 |
|
| 134 |
require XSLoader; |
| 135 |
XSLoader::load('POE::Filter::SSL', $VERSION); |
| 136 |
|
| 137 |
sub checkForDoSendback { |
| 138 |
my $chunks = shift; |
| 139 |
$chunks = $chunks->[0] if ((ref($chunks) eq "ARRAY") && |
| 140 |
(scalar(@$chunks))); |
| 141 |
return 1 if (blessed($chunks) && |
| 142 |
($chunks->can("DOSENDBACK")) && |
| 143 |
($chunks->DOSENDBACK())); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
sub PEMdataToX509 { |
| 148 |
my $unblessed = shift; |
| 149 |
my $x509 = shift; |
| 150 |
my $bio = dataToBio($unblessed, $x509); |
| 151 |
my $x509result = undef; |
| 152 |
die "Error using x509: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 153 |
unless ($x509result = Net::SSLeay::PEM_read_bio_X509($bio)); |
| 154 |
Net::SSLeay::BIO_free($bio); |
| 155 |
return $x509result; |
| 156 |
} |
| 157 |
|
| 158 |
sub PEMdataToEVP_PKEY { |
| 159 |
my $unblessed = shift; |
| 160 |
my $crt = shift; |
| 161 |
my $bio = dataToBio($unblessed, $crt); |
| 162 |
my $evp_pkey = undef; |
| 163 |
die "Error using cacrt: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 164 |
unless ($evp_pkey = Net::SSLeay::PEM_read_bio_PrivateKey($bio)); |
| 165 |
Net::SSLeay::BIO_free($bio); |
| 166 |
return $evp_pkey; |
| 167 |
} |
| 168 |
|
| 169 |
sub CTX_add_client_CA { |
| 170 |
my $unblessed = shift; |
| 171 |
my $ctx = shift; |
| 172 |
my $x509 = shift; |
| 173 |
my $err = Net::SSLeay::X509_STORE_add_cert(Net::SSLeay::CTX_get_cert_store($ctx), PEMdataToX509($unblessed, $x509)); |
| 174 |
die "Error using cacrt: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 175 |
if ($err && ($err != 1)); |
| 176 |
$err = Net::SSLeay::CTX_add_client_CA($ctx, PEMdataToX509($unblessed, $x509)); |
| 177 |
die "Error using cacrt: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 178 |
if ($err && ($err != 1)); |
| 179 |
} |
| 180 |
|
| 181 |
sub dataToBio { |
| 182 |
my $unblessed = shift; |
| 183 |
my $data = shift; |
| 184 |
my $bio = Net::SSLeay::BIO_new(Net::SSLeay::BIO_s_mem()); |
| 185 |
my $sent = Net::SSLeay::BIO_write($bio, $data); |
| 186 |
print "Wrote ".$sent." of ".length($data)." bytes.\n" |
| 187 |
if $unblessed->{debug}; |
| 188 |
die "Cannot write to bio!" |
| 189 |
if (($sent) != length($data)); |
| 190 |
return $bio; |
| 191 |
} |
| 192 |
|
| 193 |
sub new { |
| 194 |
my $type = shift; |
| 195 |
|
| 196 |
my $params = {@_}; |
| 197 |
my $self = bless({}, $type); |
| 198 |
|
| 199 |
$self->{unblessed} = {}; |
| 200 |
$self->{buffer} = ''; |
| 201 |
$self->{unblessed}->{debug} = $params->{debug} || 0; |
| 202 |
$self->{cacrl} = $params->{cacrl} |
| 203 |
if $self->{cacrl}; |
| 204 |
$self->{client} = $params->{client} || 0; |
| 205 |
$self->{errorhandler} = $params->{errorhandler}; |
| 206 |
$self->{params} = $params; |
| 207 |
|
| 208 |
$self->{context} = |
| 209 |
($params->{tls} || $params->{tls1_2}) ? |
| 210 |
($params->{tls1_2} ? |
| 211 |
Net::SSLeay::CTX_tlsv1_2_new() : |
| 212 |
Net::SSLeay::CTX_tlsv1_new()) : |
| 213 |
Net::SSLeay::CTX_new(); |
| 214 |
|
| 215 |
Net::SSLeay::CTX_set_options($self->{context}, 0x00400000) # SSL_OP_CIPHER_SERVER_PREFERENCE # Beim Apache: SSLHonorCipherOrder |
| 216 |
if ((!$self->{client}) && (!$params->{"nohonor"})); |
| 217 |
|
| 218 |
my $err = undef; |
| 219 |
if ($params->{chain}) { |
| 220 |
$err = Net::SSLeay::CTX_use_certificate_chain_file($self->{context}, $params->{chain}); |
| 221 |
die "Error using chain: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 222 |
if ($err && ($err != 1)); |
| 223 |
} |
| 224 |
if ($params->{keymem} || $params->{key}) { |
| 225 |
if ($params->{keymem}) { |
| 226 |
$err = Net::SSLeay::CTX_use_PrivateKey($self->{context}, PEMdataToEVP_PKEY($self->{unblessed}, $params->{keymem})); |
| 227 |
print "Loaded keymem(".length($params->{keymem})." Bytes) with result ".$err."\n" |
| 228 |
if $self->{unblessed}->{debug}; |
| 229 |
} else { |
| 230 |
$err = Net::SSLeay::CTX_use_PrivateKey_file($self->{context}, $params->{key}, &Net::SSLeay::FILETYPE_PEM); |
| 231 |
print "Loaded key from file ".$params->{key}." with result ".$err."\n" |
| 232 |
if $self->{unblessed}->{debug}; |
| 233 |
} |
| 234 |
die "Error using keymem: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 235 |
if ($err && ($err != 1)); |
| 236 |
} |
| 237 |
if ($params->{crtmem} || $params->{crt}) { |
| 238 |
if ($params->{crtmem}) { |
| 239 |
my $crt = PEMdataToX509($self->{unblessed}, $params->{crtmem}); |
| 240 |
$err = Net::SSLeay::CTX_use_certificate($self->{context}, $crt); |
| 241 |
print "Loaded crtmem(".length($params->{crtmem})." Bytes/".$crt.") with result ".$err."\n" |
| 242 |
if $self->{unblessed}->{debug}; |
| 243 |
} else { |
| 244 |
# TODO:XXX:FIXME: Errorchecking! |
| 245 |
$err = Net::SSLeay::CTX_use_certificate_file($self->{context}, $params->{crt}, &Net::SSLeay::FILETYPE_PEM); |
| 246 |
print "Loaded crt from file ".$params->{crt}." with result ".$err."\n" |
| 247 |
if $self->{unblessed}->{debug}; |
| 248 |
} |
| 249 |
die "Error using crtmem: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 250 |
if ($err && ($err != 1)); |
| 251 |
} |
| 252 |
|
| 253 |
$err = undef; |
| 254 |
if ($params->{cacrt}|| |
| 255 |
$params->{cacrtmem}) { |
| 256 |
if ($params->{cacrtmem}) { |
| 257 |
if (ref($params->{cacrtmem}) eq "ARRAY") { |
| 258 |
foreach my $curcert (@{$params->{cacrtmem}}) { |
| 259 |
$err = CTX_add_client_CA($self->{unblessed}, $self->{context}, $curcert); |
| 260 |
last |
| 261 |
unless $err; |
| 262 |
} |
| 263 |
} else { |
| 264 |
$err = CTX_add_client_CA($self->{unblessed}, $self->{context}, $params->{cacrtmem}); |
| 265 |
print "Loaded cacrtmem(".length($params->{cacrtmem})." Bytes) with result ".$err."\n" |
| 266 |
if $self->{unblessed}->{debug}; |
| 267 |
} |
| 268 |
} else { |
| 269 |
$err = Net::SSLeay::CTX_load_verify_locations($self->{context}, $params->{cacrt}, ''); |
| 270 |
print "Loaded cacrt from file ".$params->{cacrt}." with result ".$err."\n" |
| 271 |
if $self->{unblessed}->{debug}; |
| 272 |
$err = Net::SSLeay::CTX_set_client_CA_list($self->{context}, Net::SSLeay::load_client_CA_file($params->{cacrt})) |
| 273 |
unless ($err && ($err == 1)); |
| 274 |
print "Set client cacrt from file ".$params->{cacrt}." with result ".$err."\n" |
| 275 |
if $self->{unblessed}->{debug}; |
| 276 |
} |
| 277 |
$err = Net::SSLeay::CTX_set_verify_depth($self->{context}, $params->{caverifydepth} || 5) |
| 278 |
unless ($err && ($err == 1)); |
| 279 |
} |
| 280 |
die "Error using cacrt: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 281 |
if ($err && ($err != 1)); |
| 282 |
|
| 283 |
$err = undef; |
| 284 |
$err = Net::SSLeay::CTX_set_cipher_list($self->{context}, $params->{cipher}) |
| 285 |
if ($params->{cipher}); |
| 286 |
die "Error setting cipher: ".Net::SSLeay::ERR_error_string(ERR_get_error()) |
| 287 |
if ($err && ($err != 1)); |
| 288 |
|
| 289 |
$err = undef; |
| 290 |
$self->{rbio} = Net::SSLeay::BIO_new(Net::SSLeay::BIO_s_mem()) |
| 291 |
or die("Error creating r BIO: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error())); |
| 292 |
$self->{wbio} = Net::SSLeay::BIO_new(Net::SSLeay::BIO_s_mem()) |
| 293 |
or die("Error creating w BIO: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error())); |
| 294 |
$self->{ssl} = Net::SSLeay::new($self->{context}); |
| 295 |
$globalinfos->{int($self->{ssl})} = $self->{unblessed}; |
| 296 |
$err = Net::SSLeay::set_bio($self->{ssl}, $self->{rbio}, $self->{wbio}); |
| 297 |
die "Error setting r/w BIOs: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 298 |
if ($err && ($err != 1)); |
| 299 |
|
| 300 |
if ($params->{dhcert} || |
| 301 |
$params->{dhcertmem}) { |
| 302 |
my $dhbio = undef; |
| 303 |
if ($params->{dhcertmem}) { |
| 304 |
$dhbio = dataToBio($self->{unblessed}, $params->{dhcertmem}); |
| 305 |
} else { |
| 306 |
die "Cannot open dhcert file!" |
| 307 |
unless ((-s $params->{dhcert}) && ($dhbio = Net::SSLeay::BIO_new_file($params->{dhcert}, "r"))); |
| 308 |
} |
| 309 |
# TODO:XXX:FIXME: Errorchecking! |
| 310 |
my $dhret = Net::SSLeay::PEM_read_bio_DHparams($dhbio); |
| 311 |
print "Loaded dhcert with result ".$err."\n" |
| 312 |
if $self->{unblessed}->{debug}; |
| 313 |
Net::SSLeay::BIO_free($dhbio); |
| 314 |
die "Couldn't set DH parameters!" |
| 315 |
if (POE_FILTER_SSL_set_tmp_dh($self->{ssl}, $dhret) < 0); |
| 316 |
print "Set dhcert params with result ".$err."\n" |
| 317 |
if $self->{unblessed}->{debug}; |
| 318 |
#die "Couldn't set CTX DH parameters!" |
| 319 |
# if (POE_FILTER_SSL_CTX_set_tmp_dh($self->{context}, $dhret) < 0); |
| 320 |
# TODO:XXX:FIXME: Errorchecking! |
| 321 |
my $rsa = Net::SSLeay::RSA_generate_key(2048, 73); |
| 322 |
#die "Couldn't set RSA key!" |
| 323 |
# if (!Net::SSLeay::set_tmp_rsa($self->{ssl}, $rsa)); |
| 324 |
die "Couldn't set RSA key!" |
| 325 |
if (!POE_FILTER_SSL_CTX_set_tmp_rsa($self->{context}, $rsa)); |
| 326 |
print "Set dhrsa with result ".$err."\n" |
| 327 |
if $self->{unblessed}->{debug}; |
| 328 |
Net::SSLeay::RSA_free($rsa); |
| 329 |
} |
| 330 |
my $orfilter = 0; |
| 331 |
$orfilter |= &Net::SSLeay::VERIFY_PEER | |
| 332 |
&Net::SSLeay::VERIFY_CLIENT_ONCE |
| 333 |
if $params->{clientcert}; |
| 334 |
$orfilter |= &Net::SSLeay::VERIFY_FAIL_IF_NO_PEER_CERT |
| 335 |
if $params->{clientcert} && |
| 336 |
$params->{blockbadclientcert}; |
| 337 |
# TODO:XXX:FIXME: Errorchecking! |
| 338 |
#Net::SSLeay::CTX_set_verify($self->{context}, $orfilter, \&VERIFY); |
| 339 |
Net::SSLeay::set_verify($self->{ssl}, $orfilter, \&VERIFY); |
| 340 |
print "Set verify ".($params->{blockbadclientcert} ? "FORCE" : "")." ".$orfilter."\n" |
| 341 |
if $self->{unblessed}->{debug}; |
| 342 |
if ($params->{sni}) { |
| 343 |
my $err = Net::SSLeay::set_tlsext_host_name($self->{ssl}, $params->{sni}); |
| 344 |
print "Set sni with result ".$err."\n" |
| 345 |
if $self->{unblessed}->{debug}; |
| 346 |
die "Error setting sni:".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) |
| 347 |
if ($err && ($err != 1)); |
| 348 |
} |
| 349 |
$self->{unblessed}->{ignoreVerifyErrors} = $params->{ignoreVerifyErrors} |
| 350 |
if ($params->{ignoreVerifyErrors} && |
| 351 |
(ref($params->{ignoreVerifyErrors}) eq "ARRAY")); |
| 352 |
|
| 353 |
$self |
| 354 |
} |
| 355 |
|
| 356 |
sub VERIFY { |
| 357 |
my ($ok, $x509_store_ctx) = @_; |
| 358 |
my $ssl = Net::SSLeay::X509_STORE_CTX_get_ex_data($x509_store_ctx, POE_FILTER_SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 359 |
my $unblessed = $globalinfos->{int($ssl)} || die; |
| 360 |
print "VERIFY ".$ok |
| 361 |
if $unblessed->{debug}; |
| 362 |
my $errcode = Net::SSLeay::X509_STORE_CTX_get_error($x509_store_ctx); |
| 363 |
if ($unblessed->{ignoreVerifyErrors} && |
| 364 |
(ref($unblessed->{ignoreVerifyErrors}) eq "ARRAY") && (scalar(grep { $errcode == $_ } |
| 365 |
@{$unblessed->{ignoreVerifyErrors}}))) { |
| 366 |
$ok = 1; |
| 367 |
print " -> ".$ok." (Ignoring error ".$errcode.")" |
| 368 |
if $unblessed->{debug}; |
| 369 |
} |
| 370 |
print "\n" |
| 371 |
if $unblessed->{debug}; |
| 372 |
$unblessed->{ok} = $ok ? 1 : 2 |
| 373 |
if (!defined($unblessed->{ok}) || |
| 374 |
($unblessed->{ok} != 2)); |
| 375 |
$unblessed->{chaincount}++; |
| 376 |
# TODO:XXX:FIXME: Chainlength check |
| 377 |
#X509_STORE_CTX_set_error($x509_store_ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG) |
| 378 |
# if (X509_STORE_CTX_get_error_depth(ctx) > uuu); |
| 379 |
# TODO:XXX:FIXME: No globalconfig |
| 380 |
# ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 381 |
# mydata = SSL_get_ex_data(ssl, mydata_index); |
| 382 |
#push(@{$unblessed->{ssls}}, int($x509_store_ctx)); |
| 383 |
if (my $x = Net::SSLeay::X509_STORE_CTX_get_current_cert($x509_store_ctx)) { |
| 384 |
push(@{$unblessed->{chain}},[Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($x)), |
| 385 |
Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($x)), |
| 386 |
POE_FILTER_X509_get_serialNumber($x), |
| 387 |
$errcode]); |
| 388 |
} |
| 389 |
Net::SSLeay::X509_STORE_CTX_set_error($x509_store_ctx, 0); |
| 390 |
return 1; # $ok; # 1=accept cert, 0=reject |
| 391 |
} |
| 392 |
|
| 393 |
sub clone { |
| 394 |
my $self = shift; |
| 395 |
return POE::Filter::SSL->new(%{$self->{params}}); |
| 396 |
} |
| 397 |
|
| 398 |
sub get_one_start { |
| 399 |
my ($self, $data) = @_; |
| 400 |
print "GETONESTART: NETWORK -> SSL -> POE: ".$self->hexdump(join("", @$data))."\n" |
| 401 |
if $self->{unblessed}->{debug}; |
| 402 |
$self->writeToSSLBIO(join("", @$data), $self->{accepted} ? 0 : 1); |
| 403 |
[] |
| 404 |
} |
| 405 |
|
| 406 |
sub get_one { |
| 407 |
my $self = shift; |
| 408 |
print "GETONE: BEGIN\n" |
| 409 |
if $self->{unblessed}->{debug}; |
| 410 |
my @return = (); |
| 411 |
push(@return, $self) if ($self->doSSL() || $self->{buffer}); |
| 412 |
my $data = Net::SSLeay::read($self->{ssl}); |
| 413 |
push(@return, $data) |
| 414 |
if $data; |
| 415 |
print "GETONE: END: ".scalar(@return)."\n" |
| 416 |
if $self->{unblessed}->{debug}; |
| 417 |
[@return] |
| 418 |
} |
| 419 |
|
| 420 |
sub get { |
| 421 |
my ($self, $chunks) = @_; |
| 422 |
print "GET: BEGIN\n" |
| 423 |
if $self->{unblessed}->{debug}; |
| 424 |
my @return = (); |
| 425 |
#print "GET:\n" |
| 426 |
# if $self->{unblessed}->{debug}; |
| 427 |
push(@return, $self) if ($self->doSSL() || $self->{buffer}); |
| 428 |
foreach my $data (@$chunks) { |
| 429 |
print "GET: NETWORK -> SSL -> POE: ".join("", @$data)."\n" |
| 430 |
if $self->{unblessed}->{debug}; |
| 431 |
$self->writeToSSLBIO(join("", @$data)); |
| 432 |
my $data = Net::SSLeay::read($self->{ssl}); |
| 433 |
print "GET: Read ".length($data)." bytes.\n" |
| 434 |
if $self->{unblessed}->{debug}; |
| 435 |
push(@return, $data); |
| 436 |
} |
| 437 |
[@return] |
| 438 |
} |
| 439 |
|
| 440 |
sub put { |
| 441 |
my ($self, $chunks) = @_; |
| 442 |
print "PUT: BEGIN\n" |
| 443 |
if $self->{unblessed}->{debug}; |
| 444 |
my @return = (); |
| 445 |
$self->doSSL(); |
| 446 |
if ($self->{accepted}) { |
| 447 |
if (defined($self->{sendbuf})) { |
| 448 |
foreach my $cdata (@{$self->{sendbuf}}) { |
| 449 |
$self->writeToSSL($cdata); |
| 450 |
} |
| 451 |
delete($self->{sendbuf}); |
| 452 |
} |
| 453 |
} |
| 454 |
foreach my $data (@$chunks) { |
| 455 |
next if (ref($data) eq "POE::Filter::SSL"); |
| 456 |
print "PUT: POE -> SSL -> NETWORK: ".$self->hexdump($data)."\r\n" |
| 457 |
if $self->{unblessed}->{debug}; |
| 458 |
if ($self->{accepted}) { |
| 459 |
$self->writeToSSL($data); |
| 460 |
} else { |
| 461 |
push(@{$self->{sendbuf}}, $data) |
| 462 |
if ($data); |
| 463 |
} |
| 464 |
} |
| 465 |
push(@return, $self->{buffer}) |
| 466 |
if $self->{buffer}; |
| 467 |
$self->{buffer} = ''; |
| 468 |
[@return] |
| 469 |
} |
| 470 |
|
| 471 |
sub writeToSSL { |
| 472 |
my $self = shift; |
| 473 |
my $data = shift; |
| 474 |
if ((my $sent = Net::SSLeay::write($self->{ssl}, $data)) != length($data)) { |
| 475 |
my $err2 = Net::SSLeay::get_error($self->{ssl}, $sent); |
| 476 |
#die("PUT: Not all data given to SSL(".$err2."): ".$sent." != ".length($data)) if ($sent); |
| 477 |
} |
| 478 |
$self->doSSL(); |
| 479 |
} |
| 480 |
|
| 481 |
sub writeToSSLBIO { |
| 482 |
my $self = shift; |
| 483 |
my $data = shift; |
| 484 |
my $nodoSSL = shift; |
| 485 |
if ((my $sent = Net::SSLeay::BIO_write($self->{rbio}, $data)) != length($data)) { |
| 486 |
my $err2 = Net::SSLeay::get_error($self->{ssl}, $sent); |
| 487 |
#die("GET: Not all data given to BIO SSL(".$err2."): ".$sent." != ".length($data)) if ($sent); |
| 488 |
} |
| 489 |
$self->doSSL() unless $nodoSSL; |
| 490 |
} |
| 491 |
|
| 492 |
sub get_pending { |
| 493 |
return undef; |
| 494 |
} |
| 495 |
|
| 496 |
sub doSSL { |
| 497 |
my $self = shift; |
| 498 |
my $ret = 0; |
| 499 |
print "SSLing..." |
| 500 |
if $self->{unblessed}->{debug}; |
| 501 |
unless ($self->{accepted}) { |
| 502 |
my $err = $self->{client} ? |
| 503 |
Net::SSLeay::connect($self->{ssl}) : |
| 504 |
Net::SSLeay::accept($self->{ssl}); |
| 505 |
if ($err == 1) { |
| 506 |
$self->{accepted}++; |
| 507 |
$ret++; |
| 508 |
} else { |
| 509 |
my $errtext = $!; |
| 510 |
my $err2 = Net::SSLeay::get_error($self->{ssl}, $err); |
| 511 |
unless ($err2 == Net::SSLeay::ERROR_WANT_READ()) { |
| 512 |
my $tmp = "POE::Filter::SSL: ".($self->{client} ? "connect" : "accept").": "; |
| 513 |
my $err3 = undef; |
| 514 |
if ($err3 = Net::SSLeay::ERR_get_error()) { |
| 515 |
$tmp .= Net::SSLeay::ERR_error_string($err3)."(".$err3.", ".$err2.")"; |
| 516 |
} else { |
| 517 |
$tmp .= "No error (return=".$err2.")"; |
| 518 |
} |
| 519 |
if (defined($self->{errorhandler})) { |
| 520 |
if (ref($self->{errorhandler}) eq "CODE") { |
| 521 |
$self->{errorhandler}($self, { |
| 522 |
ssl => $self->{ssl}, |
| 523 |
msg => $tmp, |
| 524 |
ret => $err, |
| 525 |
get_error => $err2, |
| 526 |
error => $err3, |
| 527 |
}); |
| 528 |
} elsif(lc($self->{errorhandler}) eq "ignore") { |
| 529 |
} elsif(lc($self->{errorhandler}) eq "carp") { |
| 530 |
carp($tmp); |
| 531 |
} elsif(lc($self->{errorhandler}) eq "confess") { |
| 532 |
confess($tmp); |
| 533 |
} elsif(lc($self->{errorhandler}) eq "carponetime") { |
| 534 |
carp($tmp) |
| 535 |
unless $self->{errorstat}->{$err||"-"}->{$err2||"-"}->{$err3||"-"}++; |
| 536 |
} |
| 537 |
} else { |
| 538 |
carp($tmp); |
| 539 |
} |
| 540 |
$ret++ |
| 541 |
unless $self->{accepted}++; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
while (my $data = Net::SSLeay::BIO_read($self->{wbio})) { |
| 546 |
$self->{buffer} .= $data; |
| 547 |
} |
| 548 |
print $ret."\n" |
| 549 |
if $self->{unblessed}->{debug}; |
| 550 |
return $ret; |
| 551 |
} |
| 552 |
|
| 553 |
sub getCipher { |
| 554 |
my $self = shift; |
| 555 |
return Net::SSLeay::get_cipher($self->{ssl}); |
| 556 |
} |
| 557 |
|
| 558 |
sub clientCertExists { |
| 559 |
my $self = shift; |
| 560 |
return ((ref($self->{unblessed}->{chain}) eq "ARRAY") && ($self->{unblessed}->{chaincount})); |
| 561 |
} |
| 562 |
|
| 563 |
sub clientCertValid { |
| 564 |
my $self = shift; |
| 565 |
my $valid = 1; |
| 566 |
if (defined($self->{cacrl})) { |
| 567 |
$valid = $self->clientCertNotOnCRL($self->{cacrl}) ? 1 : 0; |
| 568 |
} |
| 569 |
return $self->clientCertExists() ? (($self->{unblessed}->{ok} ne "2") && scalar(@{$self->{unblessed}->{chain}}) && $valid) : undef; |
| 570 |
} |
| 571 |
|
| 572 |
sub clientCertIds { |
| 573 |
my $self = shift; |
| 574 |
return $self->clientCertExists ? @{$self->{unblessed}->{chain}} : undef; |
| 575 |
} |
| 576 |
|
| 577 |
sub clientCertNotOnCRL { |
| 578 |
my $self = shift; |
| 579 |
my $crlfilename = shift; |
| 580 |
my @certids = $self->clientCertIds(); |
| 581 |
if (scalar(@certids)) { |
| 582 |
my $found = 0; |
| 583 |
my $badcrls = 0; |
| 584 |
my $jump = 0; |
| 585 |
print("----- SSL Infos BEGIN ---------------"."\n") |
| 586 |
if $self->{unblessed}->{debug}; |
| 587 |
foreach (@{$self->{unblessed}->{chain}}) { |
| 588 |
my $crlstatus = POE_FILTER_verify_serial_against_crl_file($crlfilename, $_->[2]); |
| 589 |
$badcrls++ if $crlstatus; |
| 590 |
$crlstatus = $crlstatus ? "INVALID (".($crlstatus !~ m,^CRL:, ? $self->hexdump($crlstatus) : $crlstatus).")" : "VALID"; |
| 591 |
my $t = (" " x $jump++); |
| 592 |
if (ref($_) eq "ARRAY") { |
| 593 |
if ($self->{unblessed}->{debug}){ |
| 594 |
print(" ".$t." |---[ Subcertificate ]---\n") if $t; |
| 595 |
print(" ".$t." | Subject Name: ".$_->[0]."\n"); |
| 596 |
print(" ".$t." | Issuer Name : ".$_->[1]."\n"); |
| 597 |
print(" ".$t." | Serial : ".$self->hexdump($_->[2])."\n"); |
| 598 |
print(" ".$t." | CRL Status : ".$crlstatus."\n"); |
| 599 |
} |
| 600 |
} else { |
| 601 |
print(" NOCERTINFOS!"."\n") |
| 602 |
if $self->{unblessed}->{debug}; |
| 603 |
return 0; |
| 604 |
} |
| 605 |
} |
| 606 |
print("----- SSL Infos END -----------------"."\n") |
| 607 |
if $self->{unblessed}->{debug}; |
| 608 |
return 1 unless $badcrls; |
| 609 |
} |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
sub handshakeDone { |
| 614 |
my $self = shift; |
| 615 |
my $params = {@_}; |
| 616 |
return ($self->{accepted} && (($params->{ignorebuf}) || ((!$self->{sendbuf}) && (!$self->{buffer})))) || 0; |
| 617 |
} |
| 618 |
|
| 619 |
sub DESTROY { |
| 620 |
my $self = shift; |
| 621 |
#print "DESTROY: ".int($self->{ssl})."\n"; |
| 622 |
delete $globalinfos->{int($self->{ssl})}; |
| 623 |
Net::SSLeay::free($self->{ssl}) |
| 624 |
if $self->{ssl}; |
| 625 |
Net::SSLeay::CTX_free($self->{context}) |
| 626 |
if $self->{context}; |
| 627 |
#Net::SSLeay::BIO_free($self->{bio}) # CTX_free automatically frees BIO!!! |
| 628 |
# if $self->{bio}; |
| 629 |
} |
| 630 |
|
| 631 |
sub hexdump { my $self = shift; join ':', map { sprintf "%02X", $_ } unpack "C*", $_[0]; } |
| 632 |
|
| 633 |
1; |
| 634 |
|
| 635 |
__END__ |
| 636 |
|
| 637 |
=head1 NAME |
| 638 |
|
| 639 |
POE::Filter::SSL - The easiest and flexiblest way to SSL in POE! |
| 640 |
|
| 641 |
=head1 VERSION |
| 642 |
|
| 643 |
Version 0.41 |
| 644 |
|
| 645 |
=head1 DESCRIPTION |
| 646 |
|
| 647 |
This module allows one to secure connections of I<POE::Wheel::ReadWrite> with OpenSSL by a |
| 648 |
I<POE::Filter> object, and behaves (beside of SSLing) as I<POE::Filter::Stream>. |
| 649 |
|
| 650 |
I<POE::Filter::SSL> can be added, switched and removed during runtime, for example if you |
| 651 |
want to initiate SSL (see the I<SSL on an established connection> example in I<SYNOPSIS>) on an already established connection. You are able to combine |
| 652 |
I<POE::Filter::SSL> with other filters, for example have a HTTPS server together |
| 653 |
with I<POE::Filter::HTTPD> (see the I<HTTPS-Server> example in I<SYNOPSIS>). |
| 654 |
|
| 655 |
I<POE::Filter::SSL> is based on I<Net::SSLeay>, but got two XS functions which I<Net::SSLeay> is missing. |
| 656 |
|
| 657 |
=over 4 |
| 658 |
|
| 659 |
=item B<Features> |
| 660 |
|
| 661 |
=over 2 |
| 662 |
|
| 663 |
Full non-blocking processing |
| 664 |
|
| 665 |
No use of sockets at all |
| 666 |
|
| 667 |
Server and client mode |
| 668 |
|
| 669 |
Optional client certificate verification |
| 670 |
|
| 671 |
Allows one to accept connections with invalid or missing client certificate and return custom error data |
| 672 |
|
| 673 |
CRL check of client certificates |
| 674 |
|
| 675 |
Retrieve client certificate details (subject name, issuer name, certificate serial) |
| 676 |
|
| 677 |
=back |
| 678 |
|
| 679 |
=back |
| 680 |
|
| 681 |
=over 4 |
| 682 |
|
| 683 |
=item B<Upcoming Features> |
| 684 |
|
| 685 |
=over 2 |
| 686 |
|
| 687 |
Direct cipher encryption without SSL or TLS protocol, for example with static AES encryption |
| 688 |
|
| 689 |
=back |
| 690 |
|
| 691 |
=back |
| 692 |
|
| 693 |
=head1 SYNOPSIS |
| 694 |
|
| 695 |
By default I<POE::Filter::SSL> acts as a SSL server. To use it in client mode you just have to set the I<client> option of I<new()>. |
| 696 |
|
| 697 |
=over 2 |
| 698 |
|
| 699 |
=item TCP-Client |
| 700 |
|
| 701 |
#!perl |
| 702 |
|
| 703 |
use warnings; |
| 704 |
use strict; |
| 705 |
|
| 706 |
use POE qw(Component::Client::TCP Filter::SSL); |
| 707 |
|
| 708 |
POE::Component::Client::TCP->new( |
| 709 |
RemoteAddress => "yahoo.com", |
| 710 |
RemotePort => 443, |
| 711 |
Filter => [ "POE::Filter::SSL", client => 1 ], |
| 712 |
Connected => sub { |
| 713 |
$_[HEAP]{server}->put("HEAD /\r\n\r\n"); |
| 714 |
}, |
| 715 |
ServerInput => sub { |
| 716 |
print "from server: ".$_[ARG0]."\n"; |
| 717 |
}, |
| 718 |
); |
| 719 |
|
| 720 |
POE::Kernel->run(); |
| 721 |
exit; |
| 722 |
|
| 723 |
=item TCP-Server |
| 724 |
|
| 725 |
#!perl |
| 726 |
|
| 727 |
use warnings; |
| 728 |
use strict; |
| 729 |
|
| 730 |
use POE qw(Component::Server::TCP); |
| 731 |
|
| 732 |
POE::Component::Server::TCP->new( |
| 733 |
Port => 443, |
| 734 |
ClientFilter => [ "POE::Filter::SSL", crt => 'server.crt', key => 'server.key' ], |
| 735 |
ClientConnected => sub { |
| 736 |
print "got a connection from $_[HEAP]{remote_ip}\n"; |
| 737 |
$_[HEAP]{client}->put("Smile from the server!\r\n"); |
| 738 |
}, |
| 739 |
Alias => "tcp", |
| 740 |
ClientInput => sub { |
| 741 |
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
| 742 |
$_[HEAP]{client}->put("You sent:\r\n".$_[ARG0]); |
| 743 |
$_[KERNEL]->yield("shutdown"); |
| 744 |
}, |
| 745 |
); |
| 746 |
|
| 747 |
POE::Kernel->run; |
| 748 |
exit; |
| 749 |
|
| 750 |
=item HTTPS-Server |
| 751 |
|
| 752 |
use POE::Filter::SSL::PreFilter |
| 753 |
use POE::Filter::SSL; |
| 754 |
use POE::Component::Server::HTTP; |
| 755 |
use HTTP::Status; |
| 756 |
my $aliases = POE::Component::Server::HTTP->new( |
| 757 |
Port => 443, |
| 758 |
ContentHandler => { |
| 759 |
'/' => \&handler, |
| 760 |
'/dir/' => sub { return; }, |
| 761 |
'/file' => sub { return; } |
| 762 |
}, |
| 763 |
Headers => { Server => 'My Server' }, |
| 764 |
PreFilter => POE::Filter::SSL->new( |
| 765 |
crt => 'server.crt', |
| 766 |
key => 'server.key', |
| 767 |
cacrt => 'ca.crt' |
| 768 |
) |
| 769 |
); |
| 770 |
|
| 771 |
sub handler { |
| 772 |
my ($request, $response) = @_; |
| 773 |
$response->code(RC_OK); |
| 774 |
$response->content("Hi, you fetched ". $request->uri); |
| 775 |
return RC_OK; |
| 776 |
} |
| 777 |
|
| 778 |
POE::Kernel->run(); |
| 779 |
POE::Kernel->call($aliases->{httpd}, "shutdown"); |
| 780 |
# next line isn't really needed |
| 781 |
POE::Kernel->call($aliases->{tcp}, "shutdown"); |
| 782 |
|
| 783 |
=back |
| 784 |
|
| 785 |
=head2 SSL on an established connection |
| 786 |
|
| 787 |
=over 2 |
| 788 |
|
| 789 |
=item Advanced Example |
| 790 |
|
| 791 |
This example is an IMAP-Relay which forwards the connections to a IMAP server |
| 792 |
by username. It allows one the unencrypted transfer on port 143, with the option |
| 793 |
of SSL on the established connection (STARTTLS). On port 993 it allows one to do |
| 794 |
direct SSL. |
| 795 |
|
| 796 |
Tested with Thunderbird version 3.0.5. |
| 797 |
|
| 798 |
#!perl |
| 799 |
|
| 800 |
use warnings; |
| 801 |
use strict; |
| 802 |
|
| 803 |
use POE qw(Component::Server::TCP Component::Client::TCP Filter::SSL Filter::Stream); |
| 804 |
|
| 805 |
my $defaultImapServer = "not.existing.de"; |
| 806 |
my $usernameToImapServer = { |
| 807 |
user1 => 'mailserver1.domain.de', |
| 808 |
user2 => 'mailserver2.domain.de', |
| 809 |
# ... |
| 810 |
}; |
| 811 |
|
| 812 |
POE::Component::Server::TCP->new( |
| 813 |
Port => 143, |
| 814 |
ClientFilter => "POE::Filter::Stream", |
| 815 |
ClientDisconnected => \&disconnect, |
| 816 |
ClientConnected => \&connected, |
| 817 |
ClientInput => \&handleInput, |
| 818 |
InlineStates => { |
| 819 |
send_stuff => \&send_stuff, |
| 820 |
_child => \&child |
| 821 |
} |
| 822 |
); |
| 823 |
|
| 824 |
POE::Component::Server::TCP->new( |
| 825 |
Port => 993, |
| 826 |
ClientFilter => [ "POE::Filter::SSL", crt => 'server.crt', key => 'server.key' ], |
| 827 |
ClientConnected => \&connected, |
| 828 |
ClientDisconnected => \&disconnect, |
| 829 |
ClientInput => \&handleInput, |
| 830 |
InlineStates => { |
| 831 |
send_stuff => \&send_stuff, |
| 832 |
_child => \&child |
| 833 |
} |
| 834 |
); |
| 835 |
|
| 836 |
sub disconnect { |
| 837 |
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
| 838 |
logevent('server got disconnect', $session); |
| 839 |
$kernel->post($heap->{client_id} => "shutdown"); |
| 840 |
} |
| 841 |
|
| 842 |
sub connected { |
| 843 |
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
| 844 |
logevent("got a connection from ".$heap->{remote_ip}, $session); |
| 845 |
$heap->{client}->put("* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS] IMAP Relay v0.1 ready.\r\n"); |
| 846 |
} |
| 847 |
|
| 848 |
sub send_stuff { |
| 849 |
my ($heap, $stuff, $session) = @_[HEAP, ARG0, SESSION]; |
| 850 |
logevent("-> ".length($stuff)." Bytes", $session); |
| 851 |
(defined($heap->{client})) && (ref($heap->{client}) eq "POE::Wheel::ReadWrite") && |
| 852 |
$heap->{client}->put($stuff); |
| 853 |
} |
| 854 |
|
| 855 |
sub child { |
| 856 |
my ($heap, $child_op, $child) = @_[HEAP, ARG0, ARG1]; |
| 857 |
if ($child_op eq "create") { |
| 858 |
$heap->{client_id} = $child->ID; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
sub handleInput { |
| 863 |
my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0]; |
| 864 |
if($heap->{forwarding}) { |
| 865 |
return $kernel->yield("shutdown") unless (defined($heap->{client_id})); |
| 866 |
$kernel->post($heap->{client_id} => send_stuff => $input); |
| 867 |
} elsif ($input =~ /^(\d+)\s+STARTTLS[\r\n]+/i) { |
| 868 |
$_[HEAP]{client}->put($1." OK Begin SSL/TLS negotiation now.\r\n"); |
| 869 |
logevent("SSLing now...", $session); |
| 870 |
$_[HEAP]{client}->set_filter(POE::Filter::SSL->new(crt => 'server.crt', key => 'server.key')); |
| 871 |
} elsif ($input =~ /^(\d+)\s+CAPABILITY[\r\n]+/i) { |
| 872 |
$_[HEAP]{client}->put("* CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS\r\n"); |
| 873 |
$_[HEAP]{client}->put($1." OK CAPABILITY completed\r\n"); |
| 874 |
} elsif ($input =~ /^(\d+)\s+login\s+\"(\S+)\"\s+\"(\S+)\"[\r\n]+/i) { |
| 875 |
my $username = $2; |
| 876 |
my $pass = $3; |
| 877 |
logevent("login of user ".$username, $session); |
| 878 |
spawn_client_side($username, $input); |
| 879 |
$heap->{forwarding}++; |
| 880 |
} else { |
| 881 |
logevent("unknown command before login, disconnecting.", $session); |
| 882 |
return $kernel->yield("shutdown"); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
sub spawn_client_side { |
| 887 |
my $username = shift; |
| 888 |
POE::Component::Client::TCP->new( |
| 889 |
RemoteAddress => $usernameToImapServer->{$username} || $defaultImapServer, |
| 890 |
RemotePort => 143, |
| 891 |
Filter => "POE::Filter::Stream", |
| 892 |
Started => sub { |
| 893 |
$_[HEAP]->{server_id} = $_[SENDER]->ID; |
| 894 |
$_[HEAP]->{buf} = $_[ARG0]; |
| 895 |
$_[HEAP]->{skip} = 0; |
| 896 |
}, |
| 897 |
Connected => sub { |
| 898 |
my ($heap, $session) = @_[HEAP, SESSION]; |
| 899 |
logevent('client connected', $session); |
| 900 |
$heap->{server}->put($heap->{buf}); |
| 901 |
delete $heap->{buf}; |
| 902 |
}, |
| 903 |
ServerInput => sub { |
| 904 |
my ($kernel, $heap, $session, $input) = @_[KERNEL, HEAP, SESSION, ARG0]; |
| 905 |
#logevent('client got input', $session, $input); |
| 906 |
$kernel->post($heap->{server_id} => send_stuff => $input) if ($heap->{skip}++); |
| 907 |
}, |
| 908 |
Disconnected => sub { |
| 909 |
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; |
| 910 |
logevent('client disconnected', $session); |
| 911 |
$kernel->post($heap->{server_id} => 'shutdown'); |
| 912 |
}, |
| 913 |
InlineStates => { |
| 914 |
send_stuff => sub { |
| 915 |
my ($heap, $stuff, $session) = @_[HEAP, ARG0, SESSION]; |
| 916 |
logevent("<- ".length($stuff)." Bytes", $session); |
| 917 |
(defined($heap->{server})) && (ref($heap->{server}) eq "POE::Wheel::ReadWrite") && |
| 918 |
$heap->{server}->put($stuff); |
| 919 |
}, |
| 920 |
}, |
| 921 |
Args => [ shift ] |
| 922 |
); |
| 923 |
} |
| 924 |
|
| 925 |
sub logevent { |
| 926 |
my ($state, $session, $arg) = @_; |
| 927 |
my $id = $session->ID(); |
| 928 |
print "session $id $state "; |
| 929 |
print ": $arg" if (defined $arg); |
| 930 |
print "\n"; |
| 931 |
} |
| 932 |
|
| 933 |
POE::Kernel->run; |
| 934 |
|
| 935 |
=back |
| 936 |
|
| 937 |
=head2 Client certificate verification |
| 938 |
|
| 939 |
=over 2 |
| 940 |
|
| 941 |
=item Advanced Example |
| 942 |
|
| 943 |
The following example implements a HTTPS server with client certificate verification, which shows details about the verified client certificate. |
| 944 |
|
| 945 |
#!perl |
| 946 |
|
| 947 |
use strict; |
| 948 |
use warnings; |
| 949 |
use Socket; |
| 950 |
use POE qw( |
| 951 |
Wheel::SocketFactory |
| 952 |
Wheel::ReadWrite |
| 953 |
Driver::SysRW |
| 954 |
Filter::SSL |
| 955 |
Filter::Stackable |
| 956 |
Filter::HTTPD |
| 957 |
); |
| 958 |
|
| 959 |
POE::Session->create( |
| 960 |
inline_states => { |
| 961 |
_start => sub { |
| 962 |
my $heap = $_[HEAP]; |
| 963 |
$heap->{listener} = POE::Wheel::SocketFactory->new( |
| 964 |
BindAddress => '0.0.0.0', |
| 965 |
BindPort => 443, |
| 966 |
Reuse => 'yes', |
| 967 |
SuccessEvent => 'socket_birth', |
| 968 |
FailureEvent => '_stop', |
| 969 |
); |
| 970 |
}, |
| 971 |
_stop => sub { |
| 972 |
delete $_[HEAP]->{listener}; |
| 973 |
}, |
| 974 |
socket_birth => sub { |
| 975 |
my ($socket) = $_[ARG0]; |
| 976 |
POE::Session->create( |
| 977 |
inline_states => { |
| 978 |
_start => sub { |
| 979 |
my ($heap, $kernel, $connected_socket, $address, $port) = @_[HEAP, KERNEL, ARG0, ARG1, ARG2]; |
| 980 |
$heap->{sslfilter} = POE::Filter::SSL->new( |
| 981 |
crt => 'server.crt', |
| 982 |
key => 'server.key', |
| 983 |
cacrt => 'ca.crt', |
| 984 |
cipher => 'DHE-RSA-AES256-GCM-SHA384:AES256-SHA', |
| 985 |
#cacrl => 'ca.crl', # Uncomment this, if you have a CRL file. |
| 986 |
debug => 1, |
| 987 |
clientcert => 1 |
| 988 |
); |
| 989 |
$heap->{socket_wheel} = POE::Wheel::ReadWrite->new( |
| 990 |
Handle => $connected_socket, |
| 991 |
Driver => POE::Driver::SysRW->new(), |
| 992 |
Filter => POE::Filter::Stackable->new(Filters => [ |
| 993 |
$heap->{sslfilter}, |
| 994 |
POE::Filter::HTTPD->new() |
| 995 |
]), |
| 996 |
InputEvent => 'socket_input', |
| 997 |
ErrorEvent => '_stop', |
| 998 |
); |
| 999 |
}, |
| 1000 |
socket_input => sub { |
| 1001 |
my ($kernel, $heap, $buf) = @_[KERNEL, HEAP, ARG0]; |
| 1002 |
my (@certid) = ($heap->{sslfilter}->clientCertIds()); |
| 1003 |
my $content = ''; |
| 1004 |
if ($heap->{sslfilter}->clientCertValid()) { |
| 1005 |
$content .= "Hello <font color=green>valid</font> client Certifcate:"; |
| 1006 |
} else { |
| 1007 |
$content .= "None or <font color=red>invalid</font> client certificate:"; |
| 1008 |
} |
| 1009 |
$content .= "<hr>"; |
| 1010 |
foreach my $certid (@certid) { |
| 1011 |
$certid = $certid ? $certid->[0]."<br>".$certid->[1]."<br>SERIAL=".$heap->{sslfilter}->hexdump($certid->[2]) : 'No client certificate'; |
| 1012 |
$content .= $certid."<hr>"; |
| 1013 |
} |
| 1014 |
$content .= "Your URL was: ".$buf->uri."<hr>" |
| 1015 |
if (ref($buf) eq "HTTP::Request"); |
| 1016 |
$content .= localtime(time()); |
| 1017 |
my $response = HTTP::Response->new(200); |
| 1018 |
$response->push_header('Content-type', 'text/html'); |
| 1019 |
$response->content($content); |
| 1020 |
$heap->{socket_wheel}->put($response); |
| 1021 |
$kernel->delay(_stop => 1); |
| 1022 |
}, |
| 1023 |
_stop => sub { |
| 1024 |
delete $_[HEAP]->{socket_wheel}; |
| 1025 |
} |
| 1026 |
}, |
| 1027 |
args => [$socket], |
| 1028 |
); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
); |
| 1032 |
|
| 1033 |
$poe_kernel->run(); |
| 1034 |
|
| 1035 |
=back |
| 1036 |
|
| 1037 |
=head1 FUNCTIONS |
| 1038 |
|
| 1039 |
=over 4 |
| 1040 |
|
| 1041 |
=item B<new(option => value, option => value, option...)> |
| 1042 |
|
| 1043 |
Returns a new I<POE::Filter::SSL> object. It accepts the following options: |
| 1044 |
|
| 1045 |
=over 2 |
| 1046 |
|
| 1047 |
=item client |
| 1048 |
|
| 1049 |
By default I<POE::Filter::SSL> acts as a SSL server. To use it in client mode, you have to set this option. |
| 1050 |
|
| 1051 |
=item crt{mem} |
| 1052 |
|
| 1053 |
The certificate file (.crt) for the server, a client certificate in client mode. |
| 1054 |
|
| 1055 |
You are able to pass the already inmemory crt file as scalar via I<crtmem>. |
| 1056 |
|
| 1057 |
=item key{mem} |
| 1058 |
|
| 1059 |
The key file (.key) of the certificate (see I<crt> above). |
| 1060 |
|
| 1061 |
You are able to pass the already inmemory key file as scalar via I<keymem>. |
| 1062 |
|
| 1063 |
=item cacrt{mem} |
| 1064 |
|
| 1065 |
The ca certificate file (ca.crt), which is used to verificate the client certificates against a CA. You can store multiple ca in one file, all of them gets imported. |
| 1066 |
|
| 1067 |
You are able to pass the already inmemory cacrt file as scalar via I<cacrtmem> or as an array ref of scalars, if you have multiple ca. |
| 1068 |
|
| 1069 |
=item caverifydepth |
| 1070 |
|
| 1071 |
By default the ca verify depth is 5, you can override this via this option. |
| 1072 |
|
| 1073 |
=item chain |
| 1074 |
|
| 1075 |
Chain certificate, you need it for example for startssl.org which needs a intermedia certificates. Here you can configure it. You can generate this the following way: |
| 1076 |
|
| 1077 |
cat client.crt intermediate.crt ca.crt > chain.pem |
| 1078 |
|
| 1079 |
In this case, you normalyly have no I<key> and I<crt> option. Currently it is not possible to pass this inmemory, only by file. |
| 1080 |
|
| 1081 |
=item cacrl |
| 1082 |
|
| 1083 |
Configures a CRL (ca.crl) against the client certificate is verified by I<clientCertValid()>. |
| 1084 |
|
| 1085 |
=item dhcert{mem} |
| 1086 |
|
| 1087 |
If you want to enable perfect forward secrecy, here you can enable Diffie-Hellman. You just have to create a dhparam file and there here the path to the path/to/FILENAME.pem where your Diffie-Hellman (pem format) stays. |
| 1088 |
|
| 1089 |
openssl dhparam -check -text -5 2048 -out path/to/FILENAME.pem |
| 1090 |
|
| 1091 |
You are able to pass the already inmemory dhparam file as scalar(string) via I<dhcertmem>. |
| 1092 |
|
| 1093 |
=item clientcert |
| 1094 |
|
| •1095 |
Only in server mode: Request during ssl handshake from the client a client certificate. |
| 1096 |
|
| 1097 |
B<WARNING:> If the client provides an untrusted or no client certificate, the connection is B<not> failing. You have to ask I<clientCertValid()> if the certificate is valid! |
| 1098 |
|
| 1099 |
=item sni |
| 1100 |
|
| 1101 |
Allows one to set the SNI hostname indication in first packet of handshake. See https://de.wikipedia.org/wiki/Server_Name_Indication |
| 1102 |
|
| 1103 |
=item tls |
| 1104 |
|
| 1105 |
Force in the handshake the use of tls, disables support for the obsolete SSL handshake. |
| 1106 |
|
| 1107 |
=item tls1_2 |
| 1108 |
|
| 1109 |
Force in the handshake the use of tls in version 1.2, disables support for the obsolete SSL handshake. |
| 1110 |
|
| 1111 |
=item nohonor |
| 1112 |
|
| 1113 |
By default, as server, I<POE::Filter:SSL> sets the option I<SSL_OP_CIPHER_SERVER_PREFERENCE>. For more information you may google the pendant of apache I<SSLHonorCipherOrder>. |
| 1114 |
|
| 1115 |
To flip back to the old behaviour, not setting this option, you can set nohonor. |
| 1116 |
|
| 1117 |
=item cipher |
| 1118 |
|
| 1119 |
Specify which ciphers are allowed for the synchronous encrypted transfer of the data over the ssl connection. |
| 1120 |
|
| 1121 |
Example: |
| 1122 |
|
| 1123 |
cipher => 'DHE-RSA-AES256-GCM-SHA384:AES256-SHA' |
| 1124 |
|
| 1125 |
=item blockbadclientcert |
| 1126 |
|
| 1127 |
Let OpenSSL deny the connection if there is no client certificate. |
| 1128 |
|
| 1129 |
B<WARNING:> If the client is listed in the CRL file or an invalid client certifiate has been sent, the connection will be established! You have to ask I<clientCertValid()> if you have the I<crl> option set on I<new()>, otherwise to ask I<clientCertNotOnCRL()> if the certificate is listed on your CRL file! |
| 1130 |
|
| 1131 |
=item ignoreVerifyErrors |
| 1132 |
|
| 1133 |
B<WARNING:> Before using this option, you should be realy sure that you know what you are doing! |
| 1134 |
|
| 1135 |
Specify to ignore specific errors on verifying the certificate chain: This is for example useful to be able to fetch the time from via secure and trusted TLS connection. In this case, your time is wrong, so must ignore time errors, which are 9: X509_V_ERR_CERT_NOT_YET_VALID (certificate is not yet valid) and 10: X509_V_ERR_CERT_HAS_EXPIRED (certificate has expired). |
| 1136 |
|
| 1137 |
The list of errors you can ignore can be found on the documentation: |
| 1138 |
|
| 1139 |
L<https://wiki.openssl.org/index.php/Manual:Verify(1)> |
| 1140 |
|
| 1141 |
Example: |
| 1142 |
|
| 1143 |
ignoreVerifyErrors => [ 9, 10, ] |
| 1144 |
|
| 1145 |
=back |
| 1146 |
|
| 1147 |
=item handshakeDone(options) |
| 1148 |
|
| 1149 |
Returns I<true> if the handshake is done and all data for handshake has been written out. It accepts the following options: |
| 1150 |
|
| 1151 |
=over 2 |
| 1152 |
|
| 1153 |
=item ignorebuf |
| 1154 |
|
| 1155 |
Returns I<true> if OpenSSL has established the connection, regardless if all data has been written out. This is needed if you want to exchange the Filter of I<POE::Wheel::ReadWrite> before the first data comes in. This option have been only used by I<doHandshake()> to be able to add new filters before first cleartext data to be processed gets in. |
| 1156 |
|
| 1157 |
=back |
| 1158 |
|
| 1159 |
=item clientCertNotOnCRL($file) |
| 1160 |
|
| 1161 |
Verifies if the serial of the client certificate is not contained in the CRL $file. No file caching is done, each call opens the file again. |
| 1162 |
|
| 1163 |
B<WARNING:> If your CRL file is missing, can not be opened is empty or has no blocked certificate at all in it, then every call will get blocked! |
| 1164 |
|
| 1165 |
=item clientCertIds() |
| 1166 |
|
| 1167 |
Returns an array of every certificate found by OpenSSL. Each element |
| 1168 |
is again a array. The first element is the value of I<X509_get_subject_name>, |
| 1169 |
second is the value of I<X509_get_issuer_name> and third element is the |
| 1170 |
serial of the certificate in binary form. You have to use I<split()> and |
| 1171 |
I<ord()>, or the I<hexdump()> function, to convert it to a readable form. |
| 1172 |
|
| 1173 |
Example: |
| 1174 |
|
| 1175 |
my ($certid) = ($heap->{sslfilter}->clientCertIds()); |
| 1176 |
$certid = $certid ? $certid->[0]."<br>".$certid->[1]."<br>SERIAL=".$heap->{sslfilter}->hexdump($certid->[2]) : 'No client certificate'; |
| 1177 |
|
| 1178 |
=item getCipher() |
| 1179 |
|
| 1180 |
Returns the used cryptographic algorithm and length. |
| 1181 |
|
| 1182 |
Example: |
| 1183 |
|
| 1184 |
$sslfilter->getCipher() |
| 1185 |
|
| 1186 |
=item clientCertValid() |
| 1187 |
|
| 1188 |
Returns I<true> if there is a client certificate that is valid. It |
| 1189 |
also tests against the CRL, if you have the I<cacrl> option set on I<new()>. |
| 1190 |
|
| 1191 |
=item doHandshake($readWrite, $filter, $filter, ...) !!!REMOVED!!! |
| 1192 |
|
| 1193 |
B<WARNING:> POE::Filter:SSL now is able to do the ssh handshake now without any helpers. Because of this, this function has been removed! |
| 1194 |
|
| 1195 |
Allows one to add filters after the ssl handshake. It has to be called in the input handler, and needs the passing of the I<POE::Wheel::ReadWhile> object. If it returns false, you have to return from the input handler. |
| 1196 |
|
| 1197 |
See the I<HTTPS-Server>, I<SSL on an established connection> and I<Client certificate verification> examples in I<SYNOPSIS> |
| 1198 |
|
| 1199 |
=item clientCertExists() |
| 1200 |
|
| 1201 |
Returns I<true> if there is a client certificate, that might be untrusted. |
| 1202 |
|
| 1203 |
B<WARNING:> If the client provides an untrusted client certificate a client certificate that is listed in CRL, this function returns I<true>. You have to ask I<clientCertValid()> if the certificate is valid! |
| 1204 |
|
| 1205 |
=item errorhandler |
| 1206 |
|
| 1207 |
By default, every ssl error is escalated via carp. You may change this behaviour via this option to: |
| 1208 |
|
| 1209 |
=over 2 |
| 1210 |
|
| 1211 |
=item "ignore" |
| 1212 |
|
| 1213 |
Do not report any error. |
| 1214 |
|
| 1215 |
=item I<CODE> |
| 1216 |
|
| 1217 |
Setting errorhandler to a reference of a function allows one to be called it callback function with the following options: |
| 1218 |
|
| 1219 |
ARG1: POE:SSL::Filter instance |
| 1220 |
|
| 1221 |
ARG2: Ref on a Hash with the following keys: |
| 1222 |
|
| 1223 |
ret The return code of Net::SSLeay::connect (client) or Net::SSLeay::accept (server) |
| 1224 |
ssl The SSL context (SSL_CTX) |
| 1225 |
msg The error message as text, as normally reported via carp |
| 1226 |
get_error The error code of get_error the ssl context |
| 1227 |
error The error code of get_error without context |
| 1228 |
|
| 1229 |
=item "carp" (or undef) |
| 1230 |
|
| 1231 |
Do Carp/carp on error. |
| 1232 |
|
| 1233 |
=item "confess" |
| 1234 |
|
| 1235 |
Do Carp/confess (stacktrace) on error. |
| 1236 |
|
| 1237 |
=item "carponetime" |
| 1238 |
|
| 1239 |
Report carp for one occurrence only one time - over all! |
| 1240 |
|
| 1241 |
=back |
| 1242 |
|
| 1243 |
=item debug |
| 1244 |
|
| 1245 |
Shows debug messages of I<clientCertNotOnCRL()>. |
| 1246 |
|
| 1247 |
=item hexdump($string) |
| 1248 |
|
| 1249 |
Returns string data in hex format. |
| 1250 |
|
| 1251 |
Example: |
| 1252 |
|
| 1253 |
perl -e 'use POE::Filter::SSL; print POE::Filter::SSL->hexdump("test")."\n";' |
| 1254 |
74:65:73:74 |
| 1255 |
|
| 1256 |
=back |
| 1257 |
|
| 1258 |
=head2 Internal functions and POE::Filter handler |
| 1259 |
|
| 1260 |
=over 2 |
| 1261 |
|
| 1262 |
=item VERIFY() |
| 1263 |
|
| 1264 |
=item POE_FILTER_X509_get_serialNumber() |
| 1265 |
|
| 1266 |
=item POE_FILTER_SSL_CTX_set_tmp_dh() |
| 1267 |
|
| 1268 |
=item POE_FILTER_SSL_CTX_set_tmp_rsa() |
| 1269 |
|
| 1270 |
=item POE_FILTER_SSL_set_tmp_dh() |
| 1271 |
|
| 1272 |
=item clone() |
| 1273 |
|
| 1274 |
=item doSSL() |
| 1275 |
|
| 1276 |
=item get() |
| 1277 |
|
| 1278 |
=item get_one() |
| 1279 |
|
| 1280 |
=item get_one_start() |
| 1281 |
|
| 1282 |
=item get_pending() |
| 1283 |
|
| 1284 |
=item writeToSSLBIO() |
| 1285 |
|
| 1286 |
=item writeToSSL() |
| 1287 |
|
| 1288 |
=item put() |
| 1289 |
|
| 1290 |
=item verify_serial_against_crl_file() |
| 1291 |
|
| 1292 |
=item DOSENDBACK() |
| 1293 |
|
| 1294 |
=item checkForDoSendback() |
| 1295 |
|
| 1296 |
=item CTX_add_client_CA() |
| 1297 |
|
| 1298 |
=item PEMdataToEVP_PKEY |
| 1299 |
|
| 1300 |
=item PEMdataToX509 |
| 1301 |
|
| 1302 |
=item dataToBio |
| 1303 |
|
| 1304 |
=back |
| 1305 |
|
| 1306 |
=head1 AUTHOR |
| 1307 |
|
| 1308 |
Markus Schraeder, C<< <privi at cpan.org> >> |
| 1309 |
|
| 1310 |
=head1 BUGS |
| 1311 |
|
| 1312 |
Please report any bugs or feature requests to C<bug-poe-filter-ssl at rt.cpan.org>, or through |
| 1313 |
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Filter-SSL>. I will be notified, and then you'll |
| 1314 |
automatically be notified of progress on your bug as I make changes. |
| 1315 |
|
| 1316 |
=head1 SUPPORT |
| 1317 |
|
| 1318 |
You can find documentation for this module with the perldoc command. |
| 1319 |
|
| 1320 |
perldoc POE::Filter::SSL |
| 1321 |
|
| 1322 |
You can also look for information at: |
| 1323 |
|
| 1324 |
=over 4 |
| 1325 |
|
| 1326 |
=item * RT: CPAN's request tracker |
| 1327 |
|
| 1328 |
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Filter-SSL> |
| 1329 |
|
| 1330 |
=item * AnnoCPAN: Annotated CPAN documentation |
| 1331 |
|
| 1332 |
L<http://annocpan.org/dist/POE-Filter-SSL> |
| 1333 |
|
| 1334 |
=item * CPAN Ratings |
| 1335 |
|
| 1336 |
L<http://cpanratings.perl.org/d/POE-Filter-SSL> |
| 1337 |
|
| 1338 |
=item * Search CPAN |
| 1339 |
|
| 1340 |
L<http://search.cpan.org/dist/POE-Filter-SSL> |
| 1341 |
|
| 1342 |
=back |
| 1343 |
|
| 1344 |
=head1 Commercial support |
| 1345 |
|
| 1346 |
Commercial support can be gained at <sslsupport at cryptomagic.eu>. |
| 1347 |
|
| 1348 |
Used in our products, you can find on L<https://www.cryptomagic.eu/> |
| 1349 |
|
| 1350 |
=head1 COPYRIGHT & LICENSE |
| 1351 |
|
| 1352 |
Copyright 2010-2017 Markus Schraeder, CryptoMagic GmbH, all rights reserved. |
| 1353 |
|
| 1354 |
This program is free software; you can redistribute it and/or modify it |
| 1355 |
under the same terms as Perl itself. |
| 1356 |
|
| 1357 |
=cut |
| 1358 |
|
| 1359 |
1; # End of POE::Filter::SSL |