An argument to myrproxy_logon was not properly set."; }
return false;
}
if(! $myproxy_server) {
$myproxy_server = 'localhost';
}
if(!$myproxy_port) {
$myproxy_port = 7512;
}
//hostname of the myproxy server. should be in form 'tcp://myproxy.teragrid.org'
$MYPROXY_SERVER = 'tcp://' . $myproxy_server;
$PORT = $myproxy_port; //default myproxy port is 7512
$USERNAME = $username;
$PASSPHRASE = $passphrase;
$OUTFILE = $outfile;
//assemble the myproxy get command
$CERT_OUT = "";
$CMD_GET =
"VERSION=MYPROXYv2\n" .
"COMMAND=0\n" .
"USERNAME=$USERNAME\n" .
"PASSPHRASE=$PASSPHRASE\n" .
"LIFETIME=$lifetime\n";
//Distinguished Name, this info does not matter, as the myproxy server will replace it with
//the correct info in the certificate it returns
$DN=array(
"countryName" => "US",
"stateOrProvinceName" => "Unknown",
"localityName" => "Springfield",
"organizationName" => "Springfield Nuclear Power Plant",
"organizationalUnitName" => "Sector 7-G Safety Inspection",
"commonName" => "Homer Simpson",
"emailAddress" => "homer@springfieldnuclear.com"
);
//OpenSSL configuration for generating a private key
$SSL_CONFIG = array (
"private_key_bits"=>"1024",
"digest_alg"=>"md5",
"encrypt_key" => false,
"private_key_type"=>"OPENSSL_KEYTYPE_RSA"
);
//generate private key
$privkey=openssl_pkey_new();
//exports text to $privkey_string
openssl_pkey_export($privkey,$privkey_string);
//generate new CSR(certificate signing request) using privkey, DN, and config
$csr = openssl_csr_new($DN,$privkey,$SSL_CONFIG);
//export the CSR to $csr_data as text
$publickeyString = openssl_csr_export($csr,$csr_data);
//convert the CSR to DER format that myproxy expects
$der_csr = pem2der($csr_data);
//open a normal socket connection to the server
$fd=fsockopen( $MYPROXY_SERVER, $PORT, $errno, $errstr);
if(!$fd) {
if($DEBUG) {
echo "\n
Could not create socket connection to $MYPROXY_SERVER: ". $errno.":".$errstr;
}
return false;
}
//convert normal socket to an SSL v3 Client socket connection
if(stream_socket_enable_crypto( $fd, true, STREAM_CRYPTO_METHOD_SSLv3_CLIENT ) === false) {
fclose($fd);
if ($DEBUG) { echo "
Unable to establish SSLv3 Connection with $MYPROXY_SERVER"; }
return false;
}
if($DEBUG) { echo "\n
SSLv3 connection established with $MYPROXY_SERVER.
"; }
if(fwrite($fd,'0') == true) { //send Globus Compatibility Zero byte
if(fwrite($fd, $CMD_GET) == true) { //send GET COMMAND
$dat = "";
$dat .= fgets($fd); //read two lines
$dat .= fgets($fd);
if(strpos($dat,"RESPONSE=0") === false) {
if($DEBUG) { echo "\n
Server reponse: $dat"; }
fclose($fd);
return false;
}
if($DEBUG) { echo "\n
read $dat from myproxy server
"; }
fread($fd,1); //get null termination
//send the cert request
$csr_send = fwrite($fd,$der_csr);
if($DEBUG) { echo "\n
sent $csr_send bytes
"; }
//receive response containing the certs
while(!feof($fd)) {
$buf = "";
$buf = fread($fd,8192);
if(strpos($buf,'VERSION=MYPROX') === false) {
$CERT_OUT = $CERT_OUT . $buf;
} else {
$buf = "";
}
}
//set umask super restrictive so file gets created with user read/write only
$oldmask = umask(0177);
if(strlen($CERT_OUT) > 0) {
$pemArray = der2pem($CERT_OUT);
$fh = fopen($OUTFILE,'w') or die("\n
Could not open proxy file for writing.");
fwrite($fh, $pemArray[0]);
fwrite($fh,$privkey_string);
for($n=1; $n < count($pemArray); $n++) {
fwrite($fh,$pemArray[$n]);
}
fclose($fh);
umask($oldmask);
return true;
} else {
fclose($fd);
if($DEBUG) { echo "\n
No certificate recieved from $MYPROXY_SERVER"; }
return false;
}
}
} else {
fclose($fd);
if($DEBUG) { echo "\n
Could not write to SSL socket at $MYPROXY_SERVER"; }
return false;
}
fclose($fd);
return true;
}
//converts PEM cert info to DER cert info
function pem2der($pem_data) {
$begin = "CERTIFICATE REQUEST-----";
$end = "-----END";
$pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin));
$pem_data = substr($pem_data, 0, strpos($pem_data, $end));
$der = base64_decode($pem_data);
return $der;
}
// unpacks certificates from myproxy server response and
// converts them to PEM format
// Arg : -string containing the myproxy server response
// Returns : -array of strings each containing a certificate
function der2pem($der_data) {
$pems = array();
$num_array = unpack('C',substr($der_data,0,1)); //C* converts to unsigned char
$num_certs = $num_array[1]; //why does unpack start at index 1? why!?!?!
$der_data = substr($der_data,1); //trim off the number of certs from first byte
//now bytes 1 and 2 mark the beginning of the cert
for($i=0; $i < $num_certs; $i++) {
$pem = "";
$index = 0;
//bytes 3 and 4 tell how long the cert is
$l1 = ord(substr($der_data,$index+2,$index+3));
$l2 = ord(substr($der_data,$index+3,$index+4));
$len = (256*$l1) + $l2;
$thisCertData = substr($der_data,$index,$index+$len+4);
$pem = $pem. "-----BEGIN CERTIFICATE-----\n" . chunk_split(base64_encode($thisCertData), 64, "\n")
. "-----END CERTIFICATE-----\n";
$der_data = substr($der_data,$index+$len+4);
array_push($pems, $pem);
}
return $pems;
}
?>