| 1 | package VOMS::Lite::MyProxy; | ||
| 2 | |||
| 3 | use VOMS::Lite::PEMHelper qw(readCert readAC readPrivateKey writeCert writeKey); | ||
| 4 | use VOMS::Lite::PROXY; | ||
| 5 | use VOMS::Lite::REQ; | ||
| 6 | use IO::Socket::SSL; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 10 | @ISA = qw(Exporter); | ||
| 11 | |||
| 12 | $VERSION = '0.20'; | ||
| 13 | |||
| 14 | END{ | ||
| 15 | if ( -e "/tmp/mpcertfile.$<.$$" ) { unlink("/tmp/mpcertfile.$<.$$") }; | ||
| 16 | if ( -e "/tmp/mpkeyfile.$<.$$" ) { unlink("/tmp/mpkeyfile.$<.$$") }; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub Get { | ||
| 20 | my %context=%{ $_[0]}; | ||
| 21 | my @error; my @warning; | ||
| 22 | |||
| 23 | if ( ! defined $context{'Server'} ) { push @error, "VOMS::Lite::MyProxy::Get: Server not Specified"; } | ||
| 24 | if ( ! defined $context{'Username'} ) { push @error, "VOMS::Lite::MyProxy::Get: Username not specified"; } | ||
| 25 | if ( ! defined $context{'Password'} ) { push @error, "VOMS::Lite::MyProxy::Get: Password not Specified"; } | ||
| 26 | if ( $#error > 0 ) { return { Errors =>\@error }; } | ||
| 27 | |||
| 28 | my $Server = (($context{'Server'} =~ /^([a-z0-9_.-]+)$/) ? $& : undef); | ||
| 29 | my $Port = (($context{'Port'} =~ /^([0-9]{1,5})$/ && $context{'Port'} < 65536) ? $& : 7512); | ||
| 30 | # my $Username = (($context{'Username'} =~ /^([\/A-Za-z0-9_.= \@-]+)$/) ? $& : undef); # [^\n]+ | ||
| 31 | my $Username = (($context{'Username'} =~ /^([^\n]+)$/) ? $& : undef); # Protocol does not specify what can be a username except implicit \n | ||
| 32 | my $Password = (($context{'Password'} =~ /^([^\n]{6,})$/) ? $& : undef); | ||
| 33 | my $Lifetime = 43200; # default 12 hours | ||
| 34 | my $Bits = 512; # default 512 bits | ||
| 35 | if (defined $context{'Lifetime'}) { | ||
| 36 | $Lifetime = ( ( $context{'Lifetime'} =~ /^([0-9]{1,10})$/ && $context{'Lifetime'} < 1000000001 ) ? $& : undef); | ||
| 37 | } | ||
| 38 | if (defined $context{'Bits'}) { | ||
| 39 | $Bits = (($context{'Bits'} =~ /^(512|1024|2048|4096)$/) ? $1 : undef); | ||
| 40 | } | ||
| 41 | my $Quiet = ((defined $context{'Quiet'}) ? 1 : undef); | ||
| 42 | |||
| 43 | # IO::SOCKET::SSL may optionally use a cert and key on the file system; | ||
| 44 | if ( defined $context{'Cert'} && ! defined $context{'CertFile'} ) { | ||
| 45 | writeCert("/tmp/mpcertfile.$<.$$",$context{'Cert'}); | ||
| 46 | $context{'CertFile'} = "/tmp/mpcertfile.$<.$$"; } | ||
| 47 | if ( defined $context{'Key'} && ! defined $context{'KeyFile'} ) { | ||
| 48 | writeKey("/tmp/mpkeyfile.$<.$$",$context{'Key'},""); | ||
| 49 | $context{'KeyFile'} = "/tmp/mpkeyfile.$<.$$"; } | ||
| 50 | |||
| 51 | # Barf if data is not good | ||
| 52 | if ( ! defined $Server ) { push @error, "VOMS::Lite::MyProxy::Get: Bad MyProxy server string"; } | ||
| 53 | if ( ! defined $Username ) { push @error, "VOMS::Lite::MyProxy::Get: Bad Username"; } | ||
| 54 | if ( ! defined $Password ) { push @error, "VOMS::Lite::MyProxy::Get: Password too short"; } | ||
| 55 | if ( ! defined $Lifetime ) { push @error, "VOMS::Lite::MyProxy::Get: Lifetime is bad"; } | ||
| 56 | if ( ! defined $Bits ) { push @error, "VOMS::Lite::MyProxy::Get: Keysize is bad"; } | ||
| 57 | |||
| 58 | if ( defined $context{'CertFile'} && ! defined $context{'KeyFile'} ) { push @error, "VOMS::Lite::MyProxy::Get: SSL Certificate specified without Key"; } | ||
| 59 | if ( defined $context{'KeyFile'} && ! defined $context{'CertFile'} ) { push @error, "VOMS::Lite::MyProxy::Get: SSL Key specified without Certificate"; } | ||
| 60 | if ( defined $context{'CertFile'} && ! -r $context{'CertFile'} ) { push @error, "VOMS::Lite::MyProxy::Get: SSL Certificate specified but file not readable"; } | ||
| 61 | if ( defined $context{'KeyFile'} && ! -r $context{'KeyFile'} ) { push @error, "VOMS::Lite::MyProxy::Get: SSL Key specified but file not readable"; } | ||
| 62 | |||
| 63 | if ( @error > 0 ) { return { Errors =>\@error }; } | ||
| 64 | |||
| 65 | ### Open SSL connection to MyProxy Server ####################################### | ||
| 66 | |||
| 67 | my $client; | ||
| 68 | if ( defined $context{'CertFile'} && defined $context{'KeyFile'} ) { | ||
| 69 | $client = IO::Socket::SSL->new(PeerAddr => "$Server:$Port", | ||
| 70 | SSL_version => 'SSLv3', | ||
| 71 | SSL_cert_file => $context{'CertFile'}, | ||
| 72 | SSL_key_file => $context{'KeyFile'}, | ||
| 73 | SSL_use_cert => 1, | ||
| 74 | SSL_ca_path => "/etc/grid-security/certificates", | ||
| 75 | SSL_verify_mode => 0x07); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $client = IO::Socket::SSL->new(PeerAddr => "$Server:$Port", | ||
| 79 | SSL_version => 'SSLv3', | ||
| 80 | SSL_ca_path => "/etc/grid-security/certificates", | ||
| 81 | SSL_verify_mode => 0x07); | ||
| 82 | } | ||
| 83 | |||
| 84 | # Barf if connection failed | ||
| 85 | if ( ! $client ) { return { Errors =>[ "VOMS::Lite::MyProxy::Get: Couldn't establish secure connection with MyProxy Server $Server:$Port" ] }; } | ||
| 86 | |||
| 87 | # Barf if server is not the authentic server | ||
| 88 | if ( $client->peer_certificate("subject") !~ /\/CN=(?:host\/|myproxy\/)?$Server(?:\/|$)/ ) { | ||
| 89 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: MyProxy Server $Server doesn't match certificate name ".$client->peer_certificate("subject") ] }; | ||
| 90 | } | ||
| 91 | |||
| 92 | #### Send put request ########################################################### | ||
| 93 | $client->print("0VERSION=MYPROXYv2\nCOMMAND=0\nUSERNAME=$Username\nPASSPHRASE=$Password\nLIFETIME=$Lifetime\n"); | ||
| 94 | $client->print("\0"); | ||
| 95 | |||
| 96 | #### Read Response ############################################################## | ||
| 97 | my $response=""; | ||
| 98 | my $max=0; | ||
| 99 | my $expect="VERSION=MYPROXYv2\n"; | ||
| 100 | while ( $expect =~ /^$response/ && $response ne $expect ) { | ||
| 101 | $response.=$client->getc(); | ||
| 102 | } | ||
| 103 | if ( $expect !~ /^$response/ ) { | ||
| 104 | for($i=length($response);$i<20;$i++) {$response.=$client->getc();} | ||
| 105 | close $client; | ||
| 106 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: MyProxy server Version is not 2. The server responded: \"$response\"" ] } | ||
| 107 | }; | ||
| 108 | |||
| 109 | until ( substr($response,-1,1) eq "\0" || $max++ > 10000) { | ||
| 110 | $response.=$client->getc(); | ||
| 111 | } | ||
| 112 | if ( $response !~ /VERSION=MYPROXYv2\n/ ) { | ||
| 113 | close $client; | ||
| 114 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: MyProxy server Version is not 2: $response" ] }; | ||
| 115 | } elsif ( $response !~ /VERSION=MYPROXYv2\nRESPONSE=0/ ) { | ||
| 116 | close $client; | ||
| 117 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: MyProxy didn't like me: $response" ] }; | ||
| 118 | } | ||
| 119 | |||
| 120 | #### Create Request ############################################################## | ||
| 121 | my %Req; | ||
| 122 | if ( ! defined $Quiet ) { %Req = %{ VOMS::Lite::REQ::Create( { Bits => $Bits, DN => ["CN=NO NAME SPECIFIED"] } ) }; } | ||
| 123 | else { %Req = %{ VOMS::Lite::REQ::Create( { Bits => $Bits, DN => ["CN=NO NAME SPECIFIED"], Quiet => 1 } ) }; } | ||
| 124 | |||
| 125 | if ( defined $Req{'Errors'} ) { | ||
| 126 | close $client; | ||
| 127 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: Failed to create a certificate request: @{ $Req{'Errors'} }" ] }; | ||
| 128 | } | ||
| 129 | |||
| 130 | #### Delegate Proxy ############################################################# | ||
| 131 | my $data=$Req{'Req'}; | ||
| 132 | $response=""; | ||
| 133 | $client->print("$data"); | ||
| 134 | $client->print("\x00"); | ||
| 135 | |||
| 136 | ### Wait for cert chain ######################################################### | ||
| 137 | my $chainlen=ord($client->getc()); # first byte is the number of certs to expect (0-255) | ||
| 138 | my $chainindex=0; | ||
| 139 | my @chain=(); | ||
| 140 | while ( $chainlen > $chainindex ) { # loop through returned certs | ||
| 141 | my $len=0; my $sublen; my $cert=""; | ||
| 142 | while ($len==0) { # Loop to get length of DER Cert | ||
| 143 | $cert.=$client->getc(); | ||
| 144 | if ( $cert =~ /^\x30([\0-\x7f])$/s ) { $len=ord($1);} # length < 128 !unlikely! | ||
| 145 | elsif ( !defined($sublen) && $cert =~ /^\x30([\x80-\xff])$/s ) { $sublen=(ord($1)-128); } # length > 127, we get length-header length | ||
| 146 | elsif ( $cert !~ /^\x30/ ) { # This is an error all DER certs start with \x30 | ||
| 147 | $client->read($response,1024); | ||
| 148 | close $client; | ||
| 149 | return { Errors =>[ "VOMS::Lite::MyProxy::Get: Expecting DER encoded cert recieved something else: \"$cert$response\"" ] }; } | ||
| 150 | if ( defined $sublen && $cert =~ /^\x30[\x80-\xff](.{$sublen})$/s ) { | ||
| 151 | $_=$1; s/./$len+=(256**(--$sublen)*ord($&))/ges; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | my $buffer; | ||
| 155 | $client->read($buffer,$len); | ||
| 156 | $cert.=$buffer; | ||
| 157 | $chain[$chainindex]=$cert; | ||
| 158 | $chainindex++; | ||
| 159 | } | ||
| 160 | |||
| 161 | $client->read($response,1024); | ||
| 162 | close $client; | ||
| 163 | |||
| 164 | if ( $response eq "" ) { push @warning,"VOMS::Lite::MyProxy::Get: No Response message after receiving $chainlen certificates"; } | ||
| 165 | elsif ( $response !~ /RESPONSE=0/ ) { return { Errors =>[ "VOMS::Lite::MyProxy::Get: Failed to get cert from MyProxy" ] }; } | ||
| 166 | |||
| 167 | return { CertChain=>\@chain, Key=>$Req{Key}, Warnings=>\@warning }; | ||
| 168 | } | ||
| 169 | |||
| 170 | ################################################################################# | ||
| 171 | |||
| 172 | sub Init { | ||
| 173 | my %context=%{ $_[0]}; | ||
| 174 | my @error; my @warning; | ||
| 175 | |||
| 176 | #### Check Input ################################################################ | ||
| 177 | if ( ! defined $context{'Server'} ) { push @error, "VOMS::Lite::MyProxy::Init: Server not Specified"; } | ||
| 178 | if ( ! defined $context{'CertFile'} && ! defined $context{'Cert'}) | ||
| 179 | { push @error, "VOMS::Lite::MyProxy::Init: Certificate not Specified"; } | ||
| 180 | if ( ! defined $context{'KeyFile'} && ! defined $context{'Key'}) | ||
| 181 | { push @error, "VOMS::Lite::MyProxy::Init: Key not Specified"; } | ||
| 182 | if ( ! defined $context{'Username'} ) { push @error, "VOMS::Lite::MyProxy::Init: Username not specified"; } | ||
| 183 | if ( ! defined $context{'Password'} ) { push @error, "VOMS::Lite::MyProxy::Init: Password not Specified"; } | ||
| 184 | |||
| 185 | if ( @error > 0 ) { return { Errors => \@error }; } | ||
| 186 | |||
| 187 | my $Lifetime = (($context{'Lifetime'} =~ /^([0-9]+)$/) ? $& : 86400); | ||
| 188 | my $ReleaseLife = (($context{'ReleaseLifetime'} =~ /^([0-9]+)$/) ? $& : 32400); | ||
| 189 | my $Server = (($context{'Server'} =~ /^([a-z0-9_.-]+)$/) ? $& : undef); | ||
| 190 | my $Port = (($context{'Port'} =~ /^([0-9]{1,5})$/ && $context{'Port'} < 65536) ? $& : 7512); | ||
| 191 | # my $Username = (($context{'Username'} =~ /^([A-Za-z0-9_.\%\@-]+)$/) ? $& : undef); | ||
| 192 | my $Username = (($context{'Username'} =~ /^([^\n]+)$/) ? $& : undef); | ||
| 193 | my $Password = (($context{'Password'} =~ /^([^\n]{6,})$/) ? $& : undef); | ||
| 194 | my $pathlen = ((( defined $context{'PathLength'} && $context{'PathLength'} =~ /^([0-9]+)$/s) ) ? $& : undef); | ||
| 195 | my $AC = (($context{'AC'} =~ /^(\060.+)$/s) ? $& : undef); | ||
| 196 | if ( ! defined $context{'Username'} ) { $Username = ${VOMS::Lite::PROXY::Examine($context{'Cert'},{SubjectDN=>""})}{'SubjectDN'}; } | ||
| 197 | |||
| 198 | # IO::SOCKET::SSL needs a cert on the file system; VOMS::Lite needs a cert in memory | ||
| 199 | if ( ! defined $context{'CertFile'} ) { writeCert("/tmp/mpcertfile.$<.$$",$context{'Cert'}); | ||
| 200 | $context{'CertFile'} = "/tmp/mpcertfile.$<.$$"; } | ||
| 201 | if ( ! defined $context{'KeyFile'} ) { writeKey("/tmp/mpkeyfile.$<.$$",$context{'Key'},""); | ||
| 202 | $context{'KeyFile'} = "/tmp/mpkeyfile.$<.$$"; } | ||
| 203 | if ( ! defined $context{'Cert'} ) { $context{'Cert'} = readCert($context{'CertFile'}); } | ||
| 204 | if ( ! defined $context{'Key'} ) { $context{'Key'} = readPrivateKey($context{'KeyFile'}); } | ||
| 205 | |||
| 206 | # Barf if data is not good | ||
| 207 | if ( ! -r $context{'CertFile'} ) { push @error, "VOMS::Lite::MyProxy::Init: Certificate file not readable"; } | ||
| 208 | if ( ! -r $context{'KeyFile'} ) { push @error, "VOMS::Lite::MyProxy::Init: Key file not readable"; } | ||
| 209 | if ( ! defined $Server ) { push @error, "VOMS::Lite::MyProxy::Init: Bad MyProxy server string"; } | ||
| 210 | # if ( ! defined $Username ) { push @error, "VOMS::Lite::MyProxy::Init: Bad Username"; } | ||
| 211 | if ( ! defined $Password ) { push @error, "VOMS::Lite::MyProxy::Init: Password too short"; } | ||
| 212 | if ( $context{'Key'} !~ /^(\060.+)$/s ) | ||
| 213 | { push @error, "VOMS::Lite::MyProxy::Init: Key is not in correct (DER) format"; } | ||
| 214 | if ( $context{'Cert'} !~ /^(\060.+)$/s ) | ||
| 215 | { push @error, "VOMS::Lite::MyProxy::Init: Cert is not in correct (DER) format"; } | ||
| 216 | if ( @error > 0 ) { return { Errors => \@error }; } | ||
| 217 | |||
| 218 | # Some warnings | ||
| 219 | if ( $ReleaseLife > $Lifetime ) { push @warning, "VOMS::Lite::MyProxy::Init: The requested release policy lifetime (${ReleaseLife}s) is greater than the delegated proxy lifetime (${Lifetime}s)."; } | ||
| 220 | |||
| 221 | ### Open SSL connection to MyProxy Server ####################################### | ||
| 222 | my $client = IO::Socket::SSL->new(PeerAddr => "$Server:$Port", | ||
| 223 | SSL_version => 'SSLv3', | ||
| 224 | SSL_cert_file => $context{'CertFile'}, | ||
| 225 | SSL_key_file => $context{'KeyFile'}, | ||
| 226 | SSL_use_cert => 1, | ||
| 227 | SSL_ca_path => "/etc/grid-security/certificates", | ||
| 228 | SSL_verify_mode => 0x07); | ||
| 229 | |||
| 230 | # Barf if connection failed | ||
| 231 | if ( ! $client ) { return { Errors => [ "VOMS::Lite::MyProxy::Init: Couldn't establish secure connection with MyProxy Server $Server:$Port $context{'CertFile'} $context{'KeyFile'}" ] }; } | ||
| 232 | |||
| 233 | # Barf if server is not the authentic server | ||
| 234 | if ( $client->peer_certificate("subject") !~ /\/CN=(?:host\/|myproxy\/)?$Server(?:\/|$)/ ) { | ||
| 235 | return { Errors => [ "VOMS::Lite::MyProxy::Init: MyProxy Server $Server doesn't match certificate name ".$client->peer_certificate("subject") ] }; | ||
| 236 | } | ||
| 237 | |||
| 238 | #### Send put request ########################################################### | ||
| 239 | $client->print("0VERSION=MYPROXYv2\nCOMMAND=1\nUSERNAME=$Username\nPASSPHRASE=$Password\nLIFETIME=$ReleaseLife\n"); | ||
| 240 | $client->print("\0"); | ||
| 241 | |||
| 242 | #### Read Response ############################################################## | ||
| 243 | my $response=""; | ||
| 244 | my $req=""; | ||
| 245 | until ($response =~ /\0/ ) { $response.=$client->getc(); } | ||
| 246 | if ( $response =~ /VERSION=MYPROXYv2\nRESPONSE=0/ ) { | ||
| 247 | my $len=0; my $sublen; | ||
| 248 | while ($len==0) { # Loop to get length of DER Request | ||
| 249 | $req.=$client->getc(); | ||
| 250 | if ( $req =~ /^\x30([\0-\x7f])$/ ) { $len=ord($1);} | ||
| 251 | elsif ( $req =~ /^\x30([\x80-\xff])$/ ) { $sublen=(ord($1)-128); } | ||
| 252 | if ( defined $sublen && $req =~ /^\x30[\x80-\xff](.{$sublen})$/ ) { | ||
| 253 | $_=$1; s/./$len+=(256**(--$sublen)*ord($&))/ges; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | my $buffer; | ||
| 257 | $client->read($buffer,$len); | ||
| 258 | $req.=$buffer; | ||
| 259 | } elsif ( $response !~ /VERSION=MYPROXYv2\n/ ) { | ||
| 260 | close $client; | ||
| 261 | return { Errors => [ "VOMS::Lite::MyProxy::Init: MyProxy server Version is not 2: $response" ] } ; | ||
| 262 | } else { | ||
| 263 | close $client; | ||
| 264 | return { Errors => [ "VOMS::Lite::MyProxy::Init: MyProxy didn't like me: $response" ] }; | ||
| 265 | } | ||
| 266 | |||
| 267 | #### Parse the Certificate Request ############################################## | ||
| 268 | my $ref=VOMS::Lite::REQ::Examine($req,{SubjectDN=>"", KeypublicExponent=>"", KeypublicModulus=>""}); | ||
| 269 | |||
| 270 | #### Create A proxy ############################################################# | ||
| 271 | my %proxyinit=( Cert=>$context{'Cert'}, | ||
| 272 | Key=>$context{'Key'}, | ||
| 273 | Quiet=>1, | ||
| 274 | KeypublicExponent=>${ $ref }{'KeypublicExponent'}, | ||
| 275 | KeypublicModulus=>${ $ref }{'Keymodulus'}, | ||
| 276 | Lifetime=>$Lifetime, | ||
| 277 | PathLength=>$pathlen | ||
| 278 | ); | ||
| 279 | |||
| 280 | if ( defined $AC ) { $proxyinit{AC} = $AC; } | ||
| 281 | |||
| 282 | my %proxy = %{ VOMS::Lite::PROXY::Create( \%proxyinit ) }; | ||
| 283 | |||
| 284 | if ( defined $proxy{'Errors'} ) { | ||
| 285 | close $client; | ||
| 286 | return { Errors => [ "VOMS::Lite::MyProxy::Init: Failed to create a proxy: @{ $proxy{'Errors'} }" ] }; | ||
| 287 | } | ||
| 288 | |||
| 289 | #### Delegate Proxy ############################################################# | ||
| 290 | my $data="\x02".$proxy{'ProxyCert'}.$context{'Cert'}; | ||
| 291 | $response=""; | ||
| 292 | $client->print("$data"); | ||
| 293 | $client->read($response,1024); | ||
| 294 | close $client; | ||
| 295 | |||
| 296 | if ( $response !~ /RESPONSE=0/ ) { return { Errors => [ "VOMS::Lite::MyProxy::Init: Delegation failed:$response" ] }; } | ||
| 297 | return { Username => $Username, Password => $Password, Lifetime => $Lifetime, ReleaseLifetime => $ReleaseLife, Server => $Server, Port => $Port, ProxyCert => $proxy{'ProxyCert'} }; | ||
| 298 | } | ||
| 299 | |||
| 300 | 1; | ||
| 301 | |||
| 302 | __END__ |