Codebase list libpoe-component-sslify-perl / 1308bba
[svn-upgrade] Integrating new upstream version, libpoe-component-sslify-perl (0.10) Martín Ferrari 16 years ago
8 changed file(s) with 220 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
00 Revision history for Perl extension POE::Component::SSLify.
1
2 * 0.10
3
4 More tweaks of POD - finally close RT #31238
5 Added SSL version support - thanks RT #31492
6 Added SSL CTX option support as a side effect
7 Added client.pl example with ReadLine support
18
29 * 0.09
310
88 META.yml
99 Changes
1010 examples/server.pl
11 examples/client.pl
00 --- #YAML:1.0
11 name: POE-Component-SSLify
2 version: 0.09
2 version: 0.10
33 abstract: Makes using SSL in the world of POE easy!
44 license: perl
55 generated_by: ExtUtils::MakeMaker version 6.31
0 #!/usr/bin/perl
1 use strict; use warnings;
2
3 use POE;
4 use POE::Component::SSLify qw( Client_SSLify );
5 use POE::Wheel::ReadWrite;
6 use POE::Wheel::SocketFactory;
7 use POE::Driver::SysRW;
8 use POE::Filter::Line;
9 use POE::Wheel::ReadLine;
10
11 POE::Session->create(
12 'inline_states' => {
13 '_start' => sub {
14 # Set the alias
15 $_[KERNEL]->alias_set( 'main' );
16
17 # Setup our ReadLine stuff
18 $_[HEAP]->{'RL'} = POE::Wheel::ReadLine->new(
19 'InputEvent' => 'Got_ReadLine',
20 );
21
22 # Connect to the server!
23 $_[KERNEL]->yield( 'do_connect' );
24 return 1;
25 },
26 'do_connect' => sub {
27 # Create the socketfactory wheel to listen for requests
28 $_[HEAP]->{'SOCKETFACTORY'} = POE::Wheel::SocketFactory->new(
29 'RemotePort' => 5432,
30 'RemoteAddress' => 'localhost',
31 'Reuse' => 'yes',
32 'SuccessEvent' => 'Got_Connection',
33 'FailureEvent' => 'ConnectError',
34 );
35 return 1;
36 },
37 'Got_ReadLine' => sub {
38 if ( defined $_[ARG0] ) {
39 if ( exists $_[HEAP]->{'WHEEL'} ) {
40 $_[HEAP]->{'WHEEL'}->put( $_[ARG0] );
41 }
42 } else {
43 if ( $_[ARG1] eq 'interrupt' ) {
44 die 'stopped';
45 }
46 }
47 },
48 'Got_Connection' => sub {
49 # ARG0 = Socket, ARG1 = Remote Address, ARG2 = Remote Port
50 my $socket = $_[ ARG0 ];
51
52 # SSLify it!
53 $socket = Client_SSLify( $socket );
54
55 # Hand it off to ReadWrite
56 my $wheel = POE::Wheel::ReadWrite->new(
57 'Handle' => $socket,
58 'Driver' => POE::Driver::SysRW->new(),
59 'Filter' => POE::Filter::Line->new(),
60 'InputEvent' => 'Got_Input',
61 'ErrorEvent' => 'Got_Error',
62 );
63
64 # Store it...
65 $_[HEAP]->{'WHEEL'} = $wheel;
66 $_[HEAP]->{'RL'}->put( 'Connected to SSL server' );
67 $_[HEAP]->{'RL'}->get( 'Input: ' );
68
69 return 1;
70 },
71 'ConnectError' => sub {
72 # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID
73 my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ];
74 warn "SocketFactory Wheel $wheel_id generated $operation error $errnum: $errstr\n";
75 delete $_[HEAP]->{'SOCKETFACTORY'};
76 $_[HEAP]->{'RL'}->put( 'Unable to connect to SSL server...' );
77 $_[KERNEL]->delay_set( 'do_connect', 5 );
78 return 1;
79 },
80 'Got_Input' => sub {
81 # ARG0: The Line, ARG1: Wheel ID
82
83 # Send back to the client the line!
84 $_[HEAP]->{'RL'}->put( 'Got Reply: ' . $_[ARG0] );
85 $_[HEAP]->{'RL'}->get( 'Input: ' );
86 return 1;
87 },
88 'Got_Error' => sub {
89 # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID
90 my ( $operation, $errnum, $errstr, $id ) = @_[ ARG0 .. ARG3 ];
91 warn "Wheel $id generated $operation error $errnum: $errstr\n";
92 delete $_[HEAP]->{'WHEEL'};
93 $_[HEAP]->{'RL'}->put( 'Disconnected from SSL server...' );
94 $_[KERNEL]->delay_set( 'do_connect', 5 );
95 return 1;
96 },
97 },
98 );
99
100 # Start POE!
101 POE::Kernel->run();
102 exit 0;
0 #!/usr/bin/perl
1 use strict; use warnings;
2
03 use POE;
14 use POE::Component::SSLify qw( Server_SSLify SSLify_Options );
25 use POE::Wheel::ReadWrite;
47 use POE::Driver::SysRW;
58 use POE::Filter::Line;
69
7 # Needs to generate the SSL certs before running this!
8
9 POE::Session->new(
10 POE::Session->create(
1011 'inline_states' => {
1112 '_start' => sub {
1213 # Okay, set the SSL options
13 SSLify_Options( 'public-key.pem', 'public-cert.pem' );
14 SSLify_Options( 'server.key', 'server.crt', 'default' );
15
16 # Set the alias
17 $_[KERNEL]->alias_set( 'main' );
1418
1519 # Create the socketfactory wheel to listen for requests
1620 $_[HEAP]->{'SOCKETFACTORY'} = POE::Wheel::SocketFactory->new(
1721 'BindPort' => 5432,
18 'BindAddress' => localhost,
22 'BindAddress' => 'localhost',
1923 'Reuse' => 'yes',
2024 'SuccessEvent' => 'Got_Connection',
2125 'FailureEvent' => 'ListenerError',
2226 );
23 return;
27 return 1;
2428 },
2529 'Got_Connection' => sub {
2630 # ARG0 = Socket, ARG1 = Remote Address, ARG2 = Remote Port
4145
4246 # Store it...
4347 $_[HEAP]->{'WHEELS'}->{ $wheel->ID } = $wheel;
44 return;
48 return 1;
4549 },
4650 'ListenerError' => sub {
4751 # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID
4852 my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ];
4953 warn "SocketFactory Wheel $wheel_id generated $operation error $errnum: $errstr\n";
5054
51 return;
55 return 1;
5256 },
5357 'Got_Input' => sub {
5458 # ARG0: The Line, ARG1: Wheel ID
5559
5660 # Send back to the client the line!
5761 $_[HEAP]->{'WHEELS'}->{ $_[ARG1] }->put( $_[ARG0] );
58 return;
62 return 1;
5963 },
6064 'Got_Flush' => sub {
61 # Done with a wheel
62 delete $_[HEAP]->{'WHEELS'}->{ $_[ARG0] };
63 return;
65 # We don't care about this event
66 return 1;
6467 },
6568 'Got_Error' => sub {
6669 # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID
6972
7073 # Done with a wheel
7174 delete $_[HEAP]->{'WHEELS'}->{ $_[ARG0] };
72 return;
75 return 1;
7376 },
7477 },
7578 );
55 use warnings FATAL => 'all'; # Enable warnings to catch errors
66
77 # Initialize our version
8 # $Revision: 1223 $
8 # $Revision: 1247 $
99 use vars qw( $VERSION );
10 $VERSION = '0.03';
10 $VERSION = '0.04';
1111
1212 # Import the SSL death routines
1313 use Net::SSLeay qw( die_now die_if_ssl_error );
1818
1919 # Override TIEHANDLE because we create a CTX
2020 sub TIEHANDLE {
21 my ( $class, $socket ) = @_;
21 my ( $class, $socket, $version, $options ) = @_;
2222
23 my $ctx = Net::SSLeay::CTX_new() or die_now( "Failed to create SSL_CTX $!" );
23 my $ctx;
24 if ( defined $version and ! ref $version ) {
25 if ( $version eq 'sslv2' ) {
26 $ctx = Net::SSLeay::CTX_v2_new();
27 } elsif ( $version eq 'sslv3' ) {
28 $ctx = Net::SSLeay::CTX_v3_new();
29 } elsif ( $version eq 'tlsv1' ) {
30 $ctx = Net::SSLeay::CTX_tlsv1_new();
31 } elsif ( $version eq 'default' ) {
32 $ctx = Net::SSLeay::CTX_new();
33 } else {
34 die "unknown SSL version: $version";
35 }
36 } else {
37 $ctx = Net::SSLeay::CTX_new();
38 }
39 $ctx || die_now( "Failed to create SSL_CTX $!" );
40
41 if ( defined $options ) {
42 Net::SSLeay::CTX_set_options( $ctx, $options ) and die_if_ssl_error( 'ssl ctx set options' );
43 }
44
2445 my $ssl = Net::SSLeay::new( $ctx ) or die_now( "Failed to create SSL $!" );
2546
2647 my $fileno = fileno( $socket );
5778 1;
5879
5980 __END__
81
6082 =head1 NAME
6183
6284 POE::Component::SSLify::ClientHandle - client object for POE::Component::SSLify
93115
94116 =head1 COPYRIGHT AND LICENSE
95117
96 Copyright 2006 by Apocalypse/Rocco Caputo
118 Copyright 2007 by Apocalypse/Rocco Caputo
97119
98120 This library is free software; you can redistribute it and/or modify
99121 it under the same terms as Perl itself.
55 use warnings FATAL => 'all'; # Enable warnings to catch errors
66
77 # Initialize our version
8 # $Revision: 1223 $
8 # $Revision: 1247 $
99 use vars qw( $VERSION );
1010 $VERSION = '0.04';
1111
160160 1;
161161
162162 __END__
163
163164 =head1 NAME
164165
165166 POE::Component::SSLify::ServerHandle - server object for POE::Component::SSLify
55 use warnings FATAL => 'all'; # Enable warnings to catch errors
66
77 # Initialize our version
8 # $Revision: 1223 $
9 our $VERSION = '0.09';
8 # $Revision: 1248 $
9 our $VERSION = '0.10';
1010
1111 # We need Net::SSLeay or all's a failure!
1212 BEGIN {
9696
9797 # Okay, the main routine here!
9898 sub Client_SSLify {
99 # Get the socket!
100 my $socket = shift;
99 # Get the socket + version + options
100 my( $socket, $version, $options ) = @_;
101101
102102 # Validation...
103103 if ( ! defined $socket ) {
109109
110110 # Now, we create the new socket and bind it to our subclass of Net::SSLeay::Handle
111111 my $newsock = gensym();
112 tie( *$newsock, 'POE::Component::SSLify::ClientHandle', $socket ) or die "Unable to tie to our subclass: $!";
112 tie( *$newsock, 'POE::Component::SSLify::ClientHandle', $socket, $version, $options ) or die "Unable to tie to our subclass: $!";
113113
114114 # All done!
115115 return $newsock;
141141 return $newsock;
142142 }
143143
144 # Sets the key + certificate
145144 sub SSLify_Options {
146 # Get the key + cert
147 my( $key, $cert ) = @_;
148
149 $ctx = Net::SSLeay::CTX_new() or die_now( "CTX_new($ctx): $!" );
150 Net::SSLeay::CTX_set_options( $ctx, &Net::SSLeay::OP_ALL ) and die_if_ssl_error( 'ssl ctx set options' );
145 # Get the key + cert + version + options
146 my( $key, $cert, $version, $options ) = @_;
147
148 if ( defined $version and ! ref $version ) {
149 if ( $version eq 'sslv2' ) {
150 $ctx = Net::SSLeay::CTX_v2_new();
151 } elsif ( $version eq 'sslv3' ) {
152 $ctx = Net::SSLeay::CTX_v3_new();
153 } elsif ( $version eq 'tlsv1' ) {
154 $ctx = Net::SSLeay::CTX_tlsv1_new();
155 } elsif ( $version eq 'default' ) {
156 $ctx = Net::SSLeay::CTX_new();
157 } else {
158 die "unknown SSL version: $version";
159 }
160 } else {
161 $ctx = Net::SSLeay::CTX_new();
162 }
163 if ( ! defined $ctx ) {
164 die_now( "Failed to create SSL_CTX $!" );
165 }
166
167 # Set the default
168 if ( ! defined $options ) {
169 $options = &Net::SSLeay::OP_ALL;
170 }
171
172 Net::SSLeay::CTX_set_options( $ctx, $options ) and die_if_ssl_error( 'ssl ctx set options' );
151173
152174 # Following will ask password unless private key is not encrypted
153175 Net::SSLeay::CTX_use_RSAPrivateKey_file( $ctx, $key, &Net::SSLeay::FILETYPE_PEM );
182204 1;
183205
184206 __END__
207
185208 =head1 NAME
186209
187210 POE::Component::SSLify - Makes using SSL in the world of POE easy!
213236 =head2 Server-side usage
214237
215238 # !!! Make sure you have a public key + certificate generated via Net::SSLeay's makecert.pl
239 # excellent howto: http://www.akadia.com/services/ssh_test_certificate.html
216240
217241 # Import the module
218 use POE::Component::SSLify qw( Server_SSLify SSLify_Options SSLify_GetCTX );
242 use POE::Component::SSLify qw( Server_SSLify SSLify_Options );
219243
220244 # Set the key + certificate file
221 eval { SSLify_Options( 'public-key.pem', 'public-cert.pem' ) };
245 eval { SSLify_Options( 'server.key', 'server.crt' ) };
222246 if ( $@ ) {
223247 # Unable to load key or certificate file...
224248 }
225
226 # Ah, I want to set some options ( not required )
227 # my $ctx = SSLify_GetCTX();
228 # Net::SSLeay::CTX_set_options( $ctx, foo );
229249
230250 # Create a normal SocketFactory wheel or something
231251 my $factory = POE::Wheel::SocketFactory->new( ... );
286306
287307 Accepts a socket, returns a brand new socket SSLified
288308
309 Optionally accepts the SSL version + CTX options
310 Client_SSLify( $socket, $version, $options );
311
312 Known versions:
313 * sslv2
314 * sslv3
315 * tlsv1
316 * default
317
318 By default we use the version: default
319
320 By default we don't set any options
321
289322 =head2 Server_SSLify
290323
291324 Accepts a socket, returns a brand new socket SSLified
295328 =head2 SSLify_Options
296329
297330 Accepts the location of the SSL key + certificate files and does it's job
331
332 Optionally accepts the SSL version + CTX options
333 SSLify_Options( $key, $cert, $version, $options );
334
335 Known versions:
336 * sslv2
337 * sslv3
338 * tlsv1
339 * default
340
341 By default we use the version: default
342
343 By default we use the options: &Net::SSLeay::OP_ALL
298344
299345 =head2 SSLify_GetCTX
300346