[svn-upgrade] Integrating new upstream version, libpoe-component-sslify-perl (0.10)
MartÃn Ferrari
15 years ago
0 | 0 | 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 | |
1 | 8 | |
2 | 9 | * 0.09 |
3 | 10 |
0 | 0 | --- #YAML:1.0 |
1 | 1 | name: POE-Component-SSLify |
2 | version: 0.09 | |
2 | version: 0.10 | |
3 | 3 | abstract: Makes using SSL in the world of POE easy! |
4 | 4 | license: perl |
5 | 5 | 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 | ||
0 | 3 | use POE; |
1 | 4 | use POE::Component::SSLify qw( Server_SSLify SSLify_Options ); |
2 | 5 | use POE::Wheel::ReadWrite; |
4 | 7 | use POE::Driver::SysRW; |
5 | 8 | use POE::Filter::Line; |
6 | 9 | |
7 | # Needs to generate the SSL certs before running this! | |
8 | ||
9 | POE::Session->new( | |
10 | POE::Session->create( | |
10 | 11 | 'inline_states' => { |
11 | 12 | '_start' => sub { |
12 | 13 | # 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' ); | |
14 | 18 | |
15 | 19 | # Create the socketfactory wheel to listen for requests |
16 | 20 | $_[HEAP]->{'SOCKETFACTORY'} = POE::Wheel::SocketFactory->new( |
17 | 21 | 'BindPort' => 5432, |
18 | 'BindAddress' => localhost, | |
22 | 'BindAddress' => 'localhost', | |
19 | 23 | 'Reuse' => 'yes', |
20 | 24 | 'SuccessEvent' => 'Got_Connection', |
21 | 25 | 'FailureEvent' => 'ListenerError', |
22 | 26 | ); |
23 | return; | |
27 | return 1; | |
24 | 28 | }, |
25 | 29 | 'Got_Connection' => sub { |
26 | 30 | # ARG0 = Socket, ARG1 = Remote Address, ARG2 = Remote Port |
41 | 45 | |
42 | 46 | # Store it... |
43 | 47 | $_[HEAP]->{'WHEELS'}->{ $wheel->ID } = $wheel; |
44 | return; | |
48 | return 1; | |
45 | 49 | }, |
46 | 50 | 'ListenerError' => sub { |
47 | 51 | # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID |
48 | 52 | my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ]; |
49 | 53 | warn "SocketFactory Wheel $wheel_id generated $operation error $errnum: $errstr\n"; |
50 | 54 | |
51 | return; | |
55 | return 1; | |
52 | 56 | }, |
53 | 57 | 'Got_Input' => sub { |
54 | 58 | # ARG0: The Line, ARG1: Wheel ID |
55 | 59 | |
56 | 60 | # Send back to the client the line! |
57 | 61 | $_[HEAP]->{'WHEELS'}->{ $_[ARG1] }->put( $_[ARG0] ); |
58 | return; | |
62 | return 1; | |
59 | 63 | }, |
60 | 64 | '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; | |
64 | 67 | }, |
65 | 68 | 'Got_Error' => sub { |
66 | 69 | # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID |
69 | 72 | |
70 | 73 | # Done with a wheel |
71 | 74 | delete $_[HEAP]->{'WHEELS'}->{ $_[ARG0] }; |
72 | return; | |
75 | return 1; | |
73 | 76 | }, |
74 | 77 | }, |
75 | 78 | ); |
5 | 5 | use warnings FATAL => 'all'; # Enable warnings to catch errors |
6 | 6 | |
7 | 7 | # Initialize our version |
8 | # $Revision: 1223 $ | |
8 | # $Revision: 1247 $ | |
9 | 9 | use vars qw( $VERSION ); |
10 | $VERSION = '0.03'; | |
10 | $VERSION = '0.04'; | |
11 | 11 | |
12 | 12 | # Import the SSL death routines |
13 | 13 | use Net::SSLeay qw( die_now die_if_ssl_error ); |
18 | 18 | |
19 | 19 | # Override TIEHANDLE because we create a CTX |
20 | 20 | sub TIEHANDLE { |
21 | my ( $class, $socket ) = @_; | |
21 | my ( $class, $socket, $version, $options ) = @_; | |
22 | 22 | |
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 | ||
24 | 45 | my $ssl = Net::SSLeay::new( $ctx ) or die_now( "Failed to create SSL $!" ); |
25 | 46 | |
26 | 47 | my $fileno = fileno( $socket ); |
57 | 78 | 1; |
58 | 79 | |
59 | 80 | __END__ |
81 | ||
60 | 82 | =head1 NAME |
61 | 83 | |
62 | 84 | POE::Component::SSLify::ClientHandle - client object for POE::Component::SSLify |
93 | 115 | |
94 | 116 | =head1 COPYRIGHT AND LICENSE |
95 | 117 | |
96 | Copyright 2006 by Apocalypse/Rocco Caputo | |
118 | Copyright 2007 by Apocalypse/Rocco Caputo | |
97 | 119 | |
98 | 120 | This library is free software; you can redistribute it and/or modify |
99 | 121 | it under the same terms as Perl itself. |
5 | 5 | use warnings FATAL => 'all'; # Enable warnings to catch errors |
6 | 6 | |
7 | 7 | # Initialize our version |
8 | # $Revision: 1223 $ | |
8 | # $Revision: 1247 $ | |
9 | 9 | use vars qw( $VERSION ); |
10 | 10 | $VERSION = '0.04'; |
11 | 11 | |
160 | 160 | 1; |
161 | 161 | |
162 | 162 | __END__ |
163 | ||
163 | 164 | =head1 NAME |
164 | 165 | |
165 | 166 | POE::Component::SSLify::ServerHandle - server object for POE::Component::SSLify |
5 | 5 | use warnings FATAL => 'all'; # Enable warnings to catch errors |
6 | 6 | |
7 | 7 | # Initialize our version |
8 | # $Revision: 1223 $ | |
9 | our $VERSION = '0.09'; | |
8 | # $Revision: 1248 $ | |
9 | our $VERSION = '0.10'; | |
10 | 10 | |
11 | 11 | # We need Net::SSLeay or all's a failure! |
12 | 12 | BEGIN { |
96 | 96 | |
97 | 97 | # Okay, the main routine here! |
98 | 98 | sub Client_SSLify { |
99 | # Get the socket! | |
100 | my $socket = shift; | |
99 | # Get the socket + version + options | |
100 | my( $socket, $version, $options ) = @_; | |
101 | 101 | |
102 | 102 | # Validation... |
103 | 103 | if ( ! defined $socket ) { |
109 | 109 | |
110 | 110 | # Now, we create the new socket and bind it to our subclass of Net::SSLeay::Handle |
111 | 111 | 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: $!"; | |
113 | 113 | |
114 | 114 | # All done! |
115 | 115 | return $newsock; |
141 | 141 | return $newsock; |
142 | 142 | } |
143 | 143 | |
144 | # Sets the key + certificate | |
145 | 144 | 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' ); | |
151 | 173 | |
152 | 174 | # Following will ask password unless private key is not encrypted |
153 | 175 | Net::SSLeay::CTX_use_RSAPrivateKey_file( $ctx, $key, &Net::SSLeay::FILETYPE_PEM ); |
182 | 204 | 1; |
183 | 205 | |
184 | 206 | __END__ |
207 | ||
185 | 208 | =head1 NAME |
186 | 209 | |
187 | 210 | POE::Component::SSLify - Makes using SSL in the world of POE easy! |
213 | 236 | =head2 Server-side usage |
214 | 237 | |
215 | 238 | # !!! 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 | |
216 | 240 | |
217 | 241 | # 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 ); | |
219 | 243 | |
220 | 244 | # Set the key + certificate file |
221 | eval { SSLify_Options( 'public-key.pem', 'public-cert.pem' ) }; | |
245 | eval { SSLify_Options( 'server.key', 'server.crt' ) }; | |
222 | 246 | if ( $@ ) { |
223 | 247 | # Unable to load key or certificate file... |
224 | 248 | } |
225 | ||
226 | # Ah, I want to set some options ( not required ) | |
227 | # my $ctx = SSLify_GetCTX(); | |
228 | # Net::SSLeay::CTX_set_options( $ctx, foo ); | |
229 | 249 | |
230 | 250 | # Create a normal SocketFactory wheel or something |
231 | 251 | my $factory = POE::Wheel::SocketFactory->new( ... ); |
286 | 306 | |
287 | 307 | Accepts a socket, returns a brand new socket SSLified |
288 | 308 | |
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 | ||
289 | 322 | =head2 Server_SSLify |
290 | 323 | |
291 | 324 | Accepts a socket, returns a brand new socket SSLified |
295 | 328 | =head2 SSLify_Options |
296 | 329 | |
297 | 330 | 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 | |
298 | 344 | |
299 | 345 | =head2 SSLify_GetCTX |
300 | 346 |