| 1 | # | ||
| 2 | # $Id: ssl.pm,v 1.10 2003/03/02 11:12:10 dsw Exp $ | ||
| 3 | # | ||
| 4 | # COPYRIGHT AND LICENSE | ||
| 5 | # Copyright (c) 2001-2003, Juniper Networks, Inc. | ||
| 6 | # All rights reserved. | ||
| 7 | # Redistribution and use in source and binary forms, with or without | ||
| 8 | # modification, are permitted provided that the following conditions are | ||
| 9 | # met: | ||
| 10 | # 1. Redistributions of source code must retain the above | ||
| 11 | # copyright notice, this list of conditions and the following | ||
| 12 | # disclaimer. | ||
| 13 | # 2. Redistributions in binary form must reproduce the above | ||
| 14 | # copyright notice, this list of conditions and the following disclaimer | ||
| 15 | # in the documentation and/or other materials provided with the | ||
| 16 | # distribution. | ||
| 17 | # 3. The name of the copyright owner may not be used to | ||
| 18 | # endorse or promote products derived from this software without specific | ||
| 19 | # prior written permission. | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
| 21 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 22 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 23 | # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
| 24 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 25 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 26 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 28 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 29 | # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # | ||
| 32 | |||
| 33 | #========================================================================== | ||
| 34 | # | ||
| 35 | # ssl Access Method for the JUNOS::Access object | ||
| 36 | # | ||
| 37 | # Here are the mandatory input parameter(s) that are needed to | ||
| 38 | # create a connection with the server and optional input parameters | ||
| 39 | # that can change the default behavior of the SSL connection. | ||
| 40 | # | ||
| 41 | # $self->{hostname} = Mandatory. Name of the router to connect to. | ||
| 42 | # | ||
| 43 | # $self->{login} = Conditional on noninteractive. The username to login the server. | ||
| 44 | # If this parameter is not provided by the client application, | ||
| 45 | # this module will prompt for the username unless noninteractive | ||
| 46 | # is specified. | ||
| 47 | # | ||
| 48 | # $self->{password} = Conditional on noninteractive. The password for the username. | ||
| 49 | # If this parameter is not provided by the client | ||
| 50 | # application and it's requested by the server, this | ||
| 51 | # module will prompt for the password unless noninteractive | ||
| 52 | # is specified. | ||
| 53 | # | ||
| 54 | # $self->{noninteractive} = Optional. If this parameter is specified, this module | ||
| 55 | # will not prompt for any information it needs. Instead, | ||
| 56 | # it will fail if any required information is not already | ||
| 57 | # provided by the client application. For example, if | ||
| 58 | # the login value is not provided and noninteractive is | ||
| 59 | # specified, then the SSL connection will fail. | ||
| 60 | # | ||
| 61 | # $self->{ssl_port} = Optional. The port number of the ssl server. It's | ||
| 62 | # JUNOSCRIPT_SSL_PORT by default. | ||
| 63 | # | ||
| 64 | # $self->{ssl_cert_dir} = Optional. The certificate dir storing the CA certs | ||
| 65 | # validation of the server certificate. | ||
| 66 | # | ||
| 67 | # $self->{ssl_verify_method} = Optional. A callback function provided by the | ||
| 68 | # client application to verify the server certificate. | ||
| 69 | # | ||
| 70 | # $self->{ssl_client_cert} = Optional. The file containing the client's | ||
| 71 | # certificate. | ||
| 72 | # | ||
| 73 | # $self->{ssl_client_key} = Conditional on ssl_client_cert. The file | ||
| 74 | # containing client's key. | ||
| 75 | # | ||
| 76 | # $self->{ssl_client_cipher_list} = Optional. The list of ciphers the client | ||
| 77 | # will accept while negotiating with the server on the cipher suite | ||
| 78 | # for the new SSL connection. This is a colon separated list, | ||
| 79 | # its syntax is specified in : | ||
| 80 | # http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT | ||
| 81 | # | ||
| 82 | # States maintained by this access method for each SSL connection, they | ||
| 83 | # can also be read by the client application: | ||
| 84 | # | ||
| 85 | # $self->{xnm_result} = contains the authenticated user if the SSL | ||
| 86 | # connection was successful. Otherwise, it contains the error message. | ||
| 87 | # | ||
| 88 | # $self->{ssl_ctx} = the SSL_CTX object as framework for TLS/SSL enabled | ||
| 89 | # functions. | ||
| 90 | # | ||
| 91 | # $self->{ssl_conn} = the SSL structure for the connection | ||
| 92 | # | ||
| 93 | # $self->{ssl_socket} = the file handle of the socket attached to the SSLeay | ||
| 94 | # layer. | ||
| 95 | # | ||
| 96 | #========================================================================== | ||
| 97 | package JUNOS::Access::ssl; | ||
| 98 | |||
| 99 | use strict; | ||
| 100 | use IO::Socket; | ||
| 101 | use Net::SSLeay; | ||
| 102 | use XML::Parser; | ||
| 103 | use Term::ReadKey; | ||
| 104 | use JUNOS::Trace; | ||
| 105 | use JUNOS::Access; | ||
| 106 | use JUNOS::Access::xnm; | ||
| 107 | use vars qw(@ISA); | ||
| 108 | @ISA = qw(JUNOS::Access::xnm); | ||
| 109 | |||
| 110 | # | ||
| 111 | # JUNOS::Access::ssl::start | ||
| 112 | # | ||
| 113 | # Initialize SSLeay module and create a connection to $self->{hostname} | ||
| 114 | # The ssl port is JUNOSCRIPT_SSL_PORT unless $self->{ssl_port} is | ||
| 115 | # already defined to a port number. | ||
| 116 | # | ||
| 117 | use constant JUNOSCRIPT_SSL_PORT => 3220; | ||
| 118 | my $ssl_initialized = 0; | ||
| 119 | sub start | ||
| 120 | { | ||
| 121 | tracept("IO"); | ||
| 122 | my($self) = @_; | ||
| 123 | |||
| 124 | # Only need to do this once for all SSL connections | ||
| 125 | if ($ssl_initialized == 0) { | ||
| 126 | Net::SSLeay::load_error_strings(); | ||
| 127 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | ||
| 128 | Net::SSLeay::randomize(); | ||
| 129 | $ssl_initialized = 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | # If port has not been defined, take the default port. | ||
| 133 | my $port = $self->{ssl_port} || JUNOSCRIPT_SSL_PORT; | ||
| 134 | |||
| 135 | my $dest_ip = gethostbyname($self->{hostname}); | ||
| 136 | unless ($dest_ip) { | ||
| 137 | $self->{JUNOS_Device}->report_error("Cannot get ip address of $self->{hostname}: $!"); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | my $dest_serv_params = sockaddr_in($port, $dest_ip); | ||
| 141 | unless ($dest_serv_params) { | ||
| 142 | $self->{JUNOS_Device}->report_error("sockaddr_in failed: $!"); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $mysock = IO::Socket::INET->new( | ||
| 147 | PeerAddr => $self->{hostname}, | ||
| 148 | PeerPort => $port, | ||
| 149 | Proto => "tcp", | ||
| 150 | Type => SOCK_STREAM, | ||
| 151 | ); | ||
| 152 | |||
| 153 | unless ($mysock) { | ||
| 154 | $self->{JUNOS_Device}->report_error("cannot create socket: $!"); | ||
| 155 | return; | ||
| 156 | } | ||
| 157 | $self->{ssl_socket} = $mysock; | ||
| 158 | |||
| 159 | # use $|=1 to eleminate buffering on the socket | ||
| 160 | $self->{ssl_socket}->autoflush(1); | ||
| 161 | |||
| 162 | $self->{ssl_ctx} = Net::SSLeay::CTX_new(); | ||
| 163 | unless ($self->{ssl_ctx}) { | ||
| 164 | $self->{JUNOS_Device}->report_error("Failed to create SSL_CTX: $!"); | ||
| 165 | trace("IO", "start: Failed to create SSL_CTX: " . Net::SSLeay::print_errs() . "\n"); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | if ($self->{ssl_client_cipher_list}) { | ||
| 170 | trace("IO", "start: setting cipher list to $self->{ssl_client_cipher_list}\n"); | ||
| 171 | Net::SSLeay::CTX_set_cipher_list($self->{ssl_ctx}, $self->{ssl_client_cipher_list}); | ||
| 172 | } | ||
| 173 | |||
| 174 | if ($self->{ssl_client_cert} && $self->{ssl_client_key}) { | ||
| 175 | unless (Net::SSLeay::set_cert_and_key($self->{ssl_ctx}, | ||
| 176 | $self->{ssl_client_cert}, $self->{ssl_client_key})) { | ||
| 177 | $self->{JUNOS_Device}->report_error("cannot set client certificate and key, $self->{ssl_client_cert}, $self->{ssl_client_key}: $!"); | ||
| 178 | trace("IO", "start: Failed to set cert and key: " . Net::SSLeay::print_errs() . "\n"); | ||
| 179 | return; | ||
| 180 | |||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | if ($self->{ssl_cert_dir}) { | ||
| 185 | Net::SSLeay::CTX_load_verify_locations($self->{ssl_ctx}, '', | ||
| 186 | $self->{ssl_cert_dir}); | ||
| 187 | } | ||
| 188 | |||
| 189 | if ($self->{ssl_verify_method}) { | ||
| 190 | Net::SSLeay::CTX_set_verify($self->{ssl_ctx}, | ||
| 191 | &Net::SSLeay::VERIFY_PEER, $self->{ssl_verify_method}); | ||
| 192 | } | ||
| 193 | |||
| 194 | # The network connection is now open, lets fire up SSL | ||
| 195 | $self->{ssl_conn} = Net::SSLeay::new($self->{ssl_ctx}); | ||
| 196 | unless ($self->{ssl_conn}) { | ||
| 197 | $self->{JUNOS_Device}->report_error("Failed to create SSL: $!"); | ||
| 198 | trace("IO", "start: Failed to create SSL: " . Net::SSLeay::print_errs() . "\n"); | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | # Must attach to SSLeay the underlying file descriptor (fileno) of | ||
| 203 | # the socket | ||
| 204 | Net::SSLeay::set_fd($self->{ssl_conn}, fileno($self->{ssl_socket})); | ||
| 205 | my $res = Net::SSLeay::connect($self->{ssl_conn}); | ||
| 206 | unless ($res) { | ||
| 207 | $self->{JUNOS_Device}->report_error("Failed to create SSL connection: $!"); | ||
| 208 | trace("IO", "start: Failed to create SSL: " . Net::SSLeay::print_errs() . "\n"); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | trace("IO", "start: Connected to $self->{hostname}\n"); | ||
| 213 | trace("IO", "start: " . ssl_cipher_list($self->{ssl_conn}) . "\n"); | ||
| 214 | trace("IO", "start: Ciper '" . Net::SSLeay::get_cipher($self->{ssl_conn}) . "'\n"); | ||
| 215 | my $cert = Net::SSLeay::dump_peer_certificate($self->{ssl_conn}); | ||
| 216 | trace("IO", "start: Server Certificate: $cert\n"); | ||
| 217 | |||
| 218 | return $res; | ||
| 219 | } | ||
| 220 | |||
| 221 | # | ||
| 222 | # private: ssl_cipher_list | ||
| 223 | # | ||
| 224 | # This subroutine puts together all the cipher suites acceptable to both | ||
| 225 | # the client and returns the list as a string. | ||
| 226 | # | ||
| 227 | sub ssl_cipher_list | ||
| 228 | { | ||
| 229 | my $conn = shift; | ||
| 230 | my $i = 0; | ||
| 231 | my $p; | ||
| 232 | my $cipher_list = "My Cipher List: \n"; | ||
| 233 | |||
| 234 | do { | ||
| 235 | $p = Net::SSLeay::get_cipher_list($conn, $i); | ||
| 236 | $cipher_list .= " [$i] " . $p . "\n" if $p; | ||
| 237 | $i++; | ||
| 238 | } while $p; | ||
| 239 | |||
| 240 | return $cipher_list; | ||
| 241 | } | ||
| 242 | |||
| 243 | # | ||
| 244 | # JUNOS::Access::ssl::send | ||
| 245 | # | ||
| 246 | # You can send an array of data buffers, this subroutine just concatenate | ||
| 247 | # them and send them all at once. | ||
| 248 | # | ||
| 249 | sub send | ||
| 250 | { | ||
| 251 | tracept("IO"); | ||
| 252 | my($self, @data) = @_; | ||
| 253 | return unless ($self->{ssl_conn}); | ||
| 254 | my $msg = join("", @data); | ||
| 255 | my $res = Net::SSLeay::write($self->{ssl_conn}, $msg); | ||
| 256 | unless ($res) { | ||
| 257 | $self->{JUNOS_Device}->report_error("Failed to write data: $!"); | ||
| 258 | trace("IO", "send: Failed to write: " . Net::SSLeay::print_errs() . "\n"); | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | $self->{seen_eof} = 1 if $res <= 0; | ||
| 262 | trace("IO", "send: sent $res bytes:\n"); | ||
| 263 | trace("IO", "send: [[[[[$msg]]]]]\n"); | ||
| 264 | return $res; | ||
| 265 | } | ||
| 266 | |||
| 267 | # | ||
| 268 | # JUNOS::Access::ssl::recv | ||
| 269 | # | ||
| 270 | # This subroutine returns the data it receives. | ||
| 271 | # | ||
| 272 | sub recv | ||
| 273 | { | ||
| 274 | tracept("IO"); | ||
| 275 | my $self = shift; | ||
| 276 | return unless ($self->{ssl_conn}); | ||
| 277 | my $res = Net::SSLeay::read($self->{ssl_conn}); | ||
| 278 | my $len = length($res) if $res; | ||
| 279 | $self->{seen_eof} = 1 unless $res; | ||
| 280 | $self->{seen_eof} = 1 if $len == 0 && eof($self->{ssl_conn}); | ||
| 281 | trace("IO", "recv: received $len bytes:\n"); | ||
| 282 | trace("IO", "recv: [[[[[$res]]]]]\n"); | ||
| 283 | return $res; | ||
| 284 | } | ||
| 285 | |||
| 286 | # | ||
| 287 | # JUNOS::Access::ssl::disconnect | ||
| 288 | # | ||
| 289 | # disconnect the SSL socket and free the context | ||
| 290 | # | ||
| 291 | sub disconnect | ||
| 292 | { | ||
| 293 | tracept("IO"); | ||
| 294 | my($self) = @_; | ||
| 295 | |||
| 296 | |||
| 297 | if ($self->{ssl_socket}) { | ||
| 298 | close $self->{ssl_socket}; | ||
| 299 | undef($self->{ssl_socket}); | ||
| 300 | } | ||
| 301 | |||
| 302 | if ($self->{ssl_conn}) { | ||
| 303 | Net::SSLeay::free ($self->{ssl_conn}); # Tear down connection | ||
| 304 | undef($self->{ssl_conn}); | ||
| 305 | } | ||
| 306 | |||
| 307 | if ($self->{ssl_ctx}) { | ||
| 308 | Net::SSLeay::CTX_free ($self->{ssl_ctx}); | ||
| 309 | undef($self->{ssl_ctx}); | ||
| 310 | } | ||
| 311 | |||
| 312 | trace("IO", "disconnect: Disconnected from $self->{hostname}\n"); | ||
| 313 | } | ||
| 314 | |||
| 315 | # | ||
| 316 | # JUNOS::Access::ssl::incoming | ||
| 317 | # | ||
| 318 | sub incoming | ||
| 319 | { | ||
| 320 | $_[0]; | ||
| 321 | } | ||
| 322 | |||
| 323 | 1; | ||
| 324 | |||
| 325 | __END__ |