| 1 | |||||||||||
| 2 | package Net::SSLeay::OO::Context; | ||||||||||
| 3 | |||||||||||
| 4 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use Net::SSLeay; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Net::SSLeay::OO::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Net::SSLeay::OO::Context - OO interface to Net::SSLeay CTX_ methods | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Net::SSLeay::OO::Constants qw(OP_ALL FILETYPE_PEM OP_NO_SSLv2); | ||||||||||
| 16 | use Net::SSLeay::OO::Context; | ||||||||||
| 17 | |||||||||||
| 18 | # create an SSL object, disable SSLv2 | ||||||||||
| 19 | my $ctx = Net::SSLeay::OO::Context->new; | ||||||||||
| 20 | $ctx->set_options(OP_ALL & OP_NO_SSLv2); | ||||||||||
| 21 | |||||||||||
| 22 | # specify path to your CA certificates for verifying peer | ||||||||||
| 23 | $ctx->load_verify_locations($ca_filename, $db_dir); | ||||||||||
| 24 | |||||||||||
| 25 | # optional for clients - load our own certificate/key | ||||||||||
| 26 | $ctx->use_certificate_chain_file($cert_filename); | ||||||||||
| 27 | $ctx->use_PrivateKey_file($key_filename, FILETYPE_PEM); | ||||||||||
| 28 | |||||||||||
| 29 | # optional for servers - require peer certificates | ||||||||||
| 30 | $ctx->set_verify(VERIFY_PEER & VERIFY_FAIL_IF_NO_PEER_CERT); | ||||||||||
| 31 | |||||||||||
| 32 | # now make SSL objects with these options! | ||||||||||
| 33 | use Net::SSLeay::OO::SSL; | ||||||||||
| 34 | my $ssl = Net::SSLeay::OO::SSL->new( ctx => $ctx ); | ||||||||||
| 35 | |||||||||||
| 36 | # convenience method for the above, plus attach to a socket | ||||||||||
| 37 | my $ssl = $ctx->new_ssl($socket); | ||||||||||
| 38 | |||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||
| 40 | |||||||||||
| 41 | Every SSL connection has a context, which specifies various options. | ||||||||||
| 42 | You can also specify these options on Net::SSLeay::OO::SSL objects, but | ||||||||||
| 43 | you would normally want to set up as much as possible early on, then | ||||||||||
| 44 | re-use the context to create new SSL handles. | ||||||||||
| 45 | |||||||||||
| 46 | The OpenSSL library initialization functions are called the first time | ||||||||||
| 47 | that a Net::SSLeay::OO::Context object is instantiated. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | has 'ctx' => ( | ||||||||||
| 52 | is => "ro", | ||||||||||
| 53 | isa => "Int", | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | =head1 ATTRIBUTES | ||||||||||
| 57 | |||||||||||
| 58 | =over | ||||||||||
| 59 | |||||||||||
| 60 | =item ctx : Int | ||||||||||
| 61 | |||||||||||
| 62 | The raw ctx object. Use at your own risk. | ||||||||||
| 63 | |||||||||||
| 64 | =item ssl_version: ( undef | 2 | 3 | 10 ) | ||||||||||
| 65 | |||||||||||
| 66 | Specify the SSL version to allow. 10 means TLSv1, 2 and 3 mean SSLv2 | ||||||||||
| 67 | and SSLv3, respectively. No options means 'SSLv23'; if you want to | ||||||||||
| 68 | permit the secure protocols only (SSLv3 and TLSv1) you need to use: | ||||||||||
| 69 | |||||||||||
| 70 | use Net::SSLeay::OO::Constants qw(OP_NO_SSLv2); | ||||||||||
| 71 | my $ctx = Net::SSLeay::OO::Context->new(); | ||||||||||
| 72 | $ctx->set_options( OP_NO_SSLv2 ) | ||||||||||
| 73 | |||||||||||
| 74 | This option must be specified at object creation time. | ||||||||||
| 75 | |||||||||||
| 76 | =back | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | has 'ssl_version' => ( | ||||||||||
| 81 | is => "ro", | ||||||||||
| 82 | isa => "Int", | ||||||||||
| 83 | ); | ||||||||||
| 84 | |||||||||||
| 85 | our $INITIALIZED; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=24.8
| |||||||||||
| 88 | 0 | my $self = shift; | |||||||||
| 89 | 0 | ▶ | if ( !$INITIALIZED++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | Net::SSLeay::load_error_strings(); | |||||||||
| 91 | 0 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | |||||||||
| 92 | 0 | Net::SSLeay::randomize(); | |||||||||
| 93 | } | ||||||||||
| 94 | 0 | ▶ | if ( !$self->ctx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | my $ctx = Net::SSLeay::new_x_ctx( $self->ssl_version ); | |||||||||
| 96 | 0 | $self->{ctx} = $ctx; | |||||||||
| 97 | 0 | $self->set_default_verify_paths; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||
| 102 | 0 | my $self = shift; | |||||||||
| 103 | 0 | ▶ | if ( $self->ctx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $self->free; | |||||||||
| 105 | 0 | delete $self->{ctx}; | |||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | =head1 METHODS | ||||||||||
| 110 | |||||||||||
| 111 | All of the CTX_ methods in Net::SSLeay are converted to methods of | ||||||||||
| 112 | the Net::SSLeay::OO::Context class. | ||||||||||
| 113 | |||||||||||
| 114 | The documentation that follows is a core set, sufficient for running | ||||||||||
| 115 | up a server and verifying client certificates. However most functions | ||||||||||
| 116 | from the OpenSSL library are actually imported. | ||||||||||
| 117 | |||||||||||
| 118 | =head2 Handshake configuration methods | ||||||||||
| 119 | |||||||||||
| 120 | =over | ||||||||||
| 121 | |||||||||||
| 122 | =item B<set_options(OP_XXX | OP_XXX ...)> | ||||||||||
| 123 | |||||||||||
| 124 | Set options that apply to this Context. The valid values and | ||||||||||
| 125 | descriptions can be found on L<SSL_CTX_set_options(3ssl)>; for this | ||||||||||
| 126 | module they must be imported from L<Net::SSLeay::OO::Constants>. | ||||||||||
| 127 | |||||||||||
| 128 | =item B<get_options()> | ||||||||||
| 129 | |||||||||||
| 130 | Returns the current options bitmask; mask with the option you're | ||||||||||
| 131 | interested in to see if it is set: | ||||||||||
| 132 | |||||||||||
| 133 | unless ($ctx->get_options & OP_NO_SSLv2) { | ||||||||||
| 134 | die "SSL v2 was not disabled!"; | ||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | =item B<load_verify_locations($filename, $path)> | ||||||||||
| 138 | |||||||||||
| 139 | Specify where CA certificates in PEM format are to be found. | ||||||||||
| 140 | C<$filename> is a single file containing one or more certificates. | ||||||||||
| 141 | C<$path> refers to a directory with C<9d66eef0.1> etc files as would | ||||||||||
| 142 | be made by L<c_rehash>. See L<SSL_CTX_load_verify_locations(3ssl)>. | ||||||||||
| 143 | |||||||||||
| 144 | =item B<set_default_verify_paths()> | ||||||||||
| 145 | |||||||||||
| 146 | Sets up system-dependent certificate store location. This is probably | ||||||||||
| 147 | quite a good default. | ||||||||||
| 148 | |||||||||||
| 149 | =item B<set_verify($mode, [$verify_callback])> | ||||||||||
| 150 | |||||||||||
| 151 | Mode should be either VERIFY_NONE, or a combination of VERIFY_PEER, | ||||||||||
| 152 | VERIFY_CLIENT_ONCE and/or VERIFY_FAIL_IF_NO_PEER_CERT. If you don't | ||||||||||
| 153 | set this as a server, you cannot later call | ||||||||||
| 154 | C<-E<gt>get_peer_certificate> to find out if the client configured a | ||||||||||
| 155 | certificate (though there are references to repeating SSL negotiation, | ||||||||||
| 156 | eg in L<SSL_read(3ssl)>, not sure how this is performed though). | ||||||||||
| 157 | |||||||||||
| 158 | During the handshake phase, the $verify_callback is called once for | ||||||||||
| 159 | every certificate in the chain of the peer, starting with the root | ||||||||||
| 160 | certificate. Each time, it is passed two arguments: the first a | ||||||||||
| 161 | boolean (1 or 0) which indicates whether the in-built certificate | ||||||||||
| 162 | verification passed, and the second argument is the actual | ||||||||||
| 163 | B<certficate> which is being verified (a L<Net::SSLeay::OO::X509> object). | ||||||||||
| 164 | Note this is different to the calling convention of OpenSSL and | ||||||||||
| 165 | Net::SSLeay, which instead (logically, anyway) pass a | ||||||||||
| 166 | L<Net::SSLeay::OO::X509::Context> object. However there is little of | ||||||||||
| 167 | interest in this other object, so for convenience the current | ||||||||||
| 168 | certificate is passed instead as the second object. The | ||||||||||
| 169 | L<Net::SSLeay::OO::X509::Context> is passed as a third argument should you | ||||||||||
| 170 | need it. | ||||||||||
| 171 | |||||||||||
| 172 | The passed L<Net::SSLeay::OO::X509> object will not work outside of the | ||||||||||
| 173 | callback; get everything out of it that you need inside it, or use the | ||||||||||
| 174 | C<get_peer_certificate> method of L<Net::SSLeay::OO::SSL> later. | ||||||||||
| 175 | |||||||||||
| 176 | Example: | ||||||||||
| 177 | |||||||||||
| 178 | my @names; | ||||||||||
| 179 | $ctx->set_verify(VERIFY_PEER, sub { | ||||||||||
| 180 | my ($ok, $x509) = @_; | ||||||||||
| 181 | push @names, $x509->subject_name->cn; | ||||||||||
| 182 | return $ok; | ||||||||||
| 183 | }); | ||||||||||
| 184 | |||||||||||
| 185 | $ssl = $ctx->new_ssl($fd); | ||||||||||
| 186 | $ssl->accept(); | ||||||||||
| 187 | |||||||||||
| 188 | print "Client identity chain: @names\n"; | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | 5 | ▶ | use Net::SSLeay::OO::Constants qw(VERIFY_NONE); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 193 | |||||||||||
| 194 | has 'verify_cb', is => "ro"; | ||||||||||
| 195 | |||||||||||
| 196 | sub set_verify { | ||||||||||
| 197 | my $self = shift; | ||||||||||
| 198 | my $mode = shift; | ||||||||||
| 199 | my $callback = shift; | ||||||||||
| 200 | require Net::SSLeay::OO::X509::Context; | ||||||||||
| 201 | |||||||||||
| 202 | # always set a callback, unless VERIFY_NONE "is set" | ||||||||||
| 203 | my $real_cb = $mode == VERIFY_NONE ? undef : sub { | ||||||||||
| 204 | my ( $preverify_ok, $x509_store_ctx ) = @_; | ||||||||||
| 205 | my $x509_ctx = | ||||||||||
| 206 | Net::SSLeay::OO::X509::Context->new( | ||||||||||
| 207 | x509_store_ctx => $x509_store_ctx,); | ||||||||||
| 208 | my $cert = $x509_ctx->get_current_cert; | ||||||||||
| 209 | my $ok; | ||||||||||
| 210 | if ($callback) { | ||||||||||
| 211 | $ok = $callback->( $preverify_ok, $cert, $x509_ctx ); | ||||||||||
| 212 | } | ||||||||||
| 213 | else { | ||||||||||
| 214 | $ok = $preverify_ok; | ||||||||||
| 215 | } | ||||||||||
| 216 | $cert->free; | ||||||||||
| 217 | $ok; | ||||||||||
| 218 | }; | ||||||||||
| 219 | $self->_set_verify( $mode, $real_cb ); | ||||||||||
| 220 | &Net::SSLeay::OO::Error::die_if_ssl_error("set_verify"); | ||||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | sub _set_verify { | ||||||||||
| 224 | my $self = shift; | ||||||||||
| 225 | my $mode = shift; | ||||||||||
| 226 | my $real_cb = shift; | ||||||||||
| 227 | my $ctx = $self->ctx; | ||||||||||
| 228 | $self->{verify_cb} = $real_cb; | ||||||||||
| 229 | Net::SSLeay::CTX_set_verify( $ctx, $mode, $real_cb ); | ||||||||||
| 230 | } | ||||||||||
| 231 | |||||||||||
| 232 | =item use_certificate_file($filename, $type) | ||||||||||
| 233 | |||||||||||
| 234 | C<$filename> is the name of a local file. This becomes your local | ||||||||||
| 235 | cert - client or server. | ||||||||||
| 236 | |||||||||||
| 237 | C<$type> may be FILETYPE_PEM or FILETYPE_ASN1. | ||||||||||
| 238 | |||||||||||
| 239 | =item use_certificate_chain_file($filename) | ||||||||||
| 240 | |||||||||||
| 241 | C<$filename> is the name of a local PEM file, containing a chain of | ||||||||||
| 242 | certificates which lead back to a valid root certificate. In general, | ||||||||||
| 243 | this is the more useful method of loading a certificate. | ||||||||||
| 244 | |||||||||||
| 245 | =item use_PrivateKey_file($filename, $type); | ||||||||||
| 246 | |||||||||||
| 247 | If using a certificate, you need to specify the private key of the end | ||||||||||
| 248 | of the chain. Specify it here; set C<$type> as with | ||||||||||
| 249 | C<use_certificate_file> | ||||||||||
| 250 | |||||||||||
| 251 | =back | ||||||||||
| 252 | |||||||||||
| 253 | =head2 Setup methods | ||||||||||
| 254 | |||||||||||
| 255 | =over | ||||||||||
| 256 | |||||||||||
| 257 | =item B<set_mode($mode)> | ||||||||||
| 258 | |||||||||||
| 259 | =item B<get_mode> | ||||||||||
| 260 | |||||||||||
| 261 | Sets/gets the mode of SSL objects created from this context. See | ||||||||||
| 262 | L<SSL_set_mode(3ssl)>. This is documented more fully at | ||||||||||
| 263 | L<Net::SSLeay::OO::SSL/set_mode> | ||||||||||
| 264 | |||||||||||
| 265 | =back | ||||||||||
| 266 | |||||||||||
| 267 | =head2 Handshake/SSL session methods | ||||||||||
| 268 | |||||||||||
| 269 | =over | ||||||||||
| 270 | |||||||||||
| 271 | =item B<new_ssl($socket)> | ||||||||||
| 272 | |||||||||||
| 273 | Makes a new L<Net::SSLeay::OO::SSL> object using this Context, and attach | ||||||||||
| 274 | it to the given socket (if passed). | ||||||||||
| 275 | |||||||||||
| 276 | =cut | ||||||||||
| 277 | |||||||||||
| 278 | sub new_ssl { | ||||||||||
| 279 | my $self = shift; | ||||||||||
| 280 | my $socket = shift; | ||||||||||
| 281 | my $ssl = Net::SSLeay::OO::SSL->new( ctx => $self ); | ||||||||||
| 282 | if ($socket) { | ||||||||||
| 283 | $ssl->set_fd( fileno($socket) ); | ||||||||||
| 284 | } | ||||||||||
| 285 | $ssl; | ||||||||||
| 286 | } | ||||||||||
| 287 | |||||||||||
| 288 | =item B<connect($socket)> | ||||||||||
| 289 | |||||||||||
| 290 | =item B<accept($socket)> | ||||||||||
| 291 | |||||||||||
| 292 | Further convenience methods, which create a new L<Net::SSLeay::OO::SSL> | ||||||||||
| 293 | object, wire it up to the passed socket, then call either C<connect> | ||||||||||
| 294 | or C<accept>. Returns the L<Net::SSLeay::OO::SSL> object. | ||||||||||
| 295 | |||||||||||
| 296 | =cut | ||||||||||
| 297 | |||||||||||
| 298 | sub connect { | ||||||||||
| 299 | my $self = shift; | ||||||||||
| 300 | my $ssl = $self->new_ssl(@_); | ||||||||||
| 301 | $ssl->connect(); | ||||||||||
| 302 | $ssl; | ||||||||||
| 303 | } | ||||||||||
| 304 | |||||||||||
| 305 | sub accept { | ||||||||||
| 306 | my $self = shift; | ||||||||||
| 307 | my $ssl = $self->new_ssl(@_); | ||||||||||
| 308 | $ssl->accept(); | ||||||||||
| 309 | $ssl; | ||||||||||
| 310 | } | ||||||||||
| 311 | |||||||||||
| 312 | =back | ||||||||||
| 313 | |||||||||||
| 314 | =head1 Informative methods | ||||||||||
| 315 | |||||||||||
| 316 | =over | ||||||||||
| 317 | |||||||||||
| 318 | =item B<get_cert_store()> | ||||||||||
| 319 | |||||||||||
| 320 | Returns the L<Net::SSLeay::OO::X509::Store> associated with this context. | ||||||||||
| 321 | |||||||||||
| 322 | =cut | ||||||||||
| 323 | |||||||||||
| 324 | sub get_cert_store { | ||||||||||
| 325 | my $self = shift; | ||||||||||
| 326 | require Net::SSLeay::OO::X509::Store; | ||||||||||
| 327 | my $store = Net::SSLeay::CTX_get_cert_store( $self->ctx ), | ||||||||||
| 328 | &Net::SSLeay::OO::Error::die_if_ssl_error("get_cert_store"); | ||||||||||
| 329 | Net::SSLeay::OO::X509::Store->new( x509_store => $store ); | ||||||||||
| 330 | } | ||||||||||
| 331 | |||||||||||
| 332 | =back | ||||||||||
| 333 | |||||||||||
| 334 | =cut | ||||||||||
| 335 | |||||||||||
| 336 | use Net::SSLeay::OO::Functions "ctx", | ||||||||||
| 337 | -include => { set_cert_and_key => "set_cert_and_key" }; | ||||||||||
| 338 | |||||||||||
| 339 | 1; | ||||||||||
| 340 | |||||||||||
| 341 | __END__ | ||||||||||