Hi!
I have written a
server in PERL and I would like to know how to implement SSL connection in this
server...
The windows client
is ready to receive SSL connections...
The part where it
gets stuck is when I want to receive text in server...
Here is the code for
the SERVER part where it gets the connection ...
--------------- SNIP
------------------- <-- NON-SSL
$port =
2345;
($name, $aliases,
$protocol) = getprotobyname('tcp');
if ($port !~
/^\d+$/) {
($name, $aliases, $port) = getservbyport($port, 'tcp');
}
($name, $aliases, $port) = getservbyport($port, 'tcp');
}
socket(S,AF_INET,SOCK_STREAM,$protocol) || die "socket : $!";
$sockaddr = 'S n a4
x8';
$this = pack($sockaddr, AF_INET, $port, "\0\0\0\0");
$this = pack($sockaddr, AF_INET, $port, "\0\0\0\0");
bind(S, $this) ||
die "bind : $!";
listen(S,1) || die
"listen: $!";
select(S);
$|=1;
select(STDOUT);
$|=1;
select(STDOUT);
for ($con = 1; ;
$con++) {
open (LOG, ">>$log_file");
open (LOG, ">>$log_file");
($addr = accept(NS,S)) || die
$!;
select(NS);
$| = 1;
select(STDOUT);
if (($child = fork()) == 0)
{
getTime();
($af,$port, $inetaddr) = unpack($sockaddr, $addr);
@inetaddr = unpack('C4', $inetaddr);
writeLog("Serving connection @ Internet address @inetaddr");
getTime();
($af,$port, $inetaddr) = unpack($sockaddr, $addr);
@inetaddr = unpack('C4', $inetaddr);
writeLog("Serving connection @ Internet address @inetaddr");
while (<NS>) {
$recstr = $_;
process();
}
$recstr = $_;
process();
}
close(NS);
writeLog("Client has disconnected...");
writeLog("Client has disconnected...");
exit;
}
close(NS);
close(LOG);
}
}
close(NS);
close(LOG);
}
---------------SNIP-----------------
-------------- SNIP
--------------- <-- implemented some SSL stuff
$our_ip =
"\0\0\0\0";
$our_serv_params = pack('S n a4 x8', &AF_INET, $port, $our_ip);
socket(S,&AF_INET,&SOCK_STREAM,0) || die "socket : $!";
bind(S, $our_serv_params) || die "bind : $!";
listen(S,5) || die "listen: $!";
$our_serv_params = pack('S n a4 x8', &AF_INET, $port, $our_ip);
socket(S,&AF_INET,&SOCK_STREAM,0) || die "socket : $!";
bind(S, $our_serv_params) || die "bind : $!";
listen(S,5) || die "listen: $!";
# Prepare
SSLeay
Net::SSLeay::load_error_strings();
Net::SSLeay::ERR_load_crypto_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();
Net::SSLeay::randomize();
close LOG;
select(S);
$|=1;
select(STDOUT);
Net::SSLeay::load_error_strings();
Net::SSLeay::ERR_load_crypto_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();
Net::SSLeay::randomize();
close LOG;
select(S);
$|=1;
select(STDOUT);
for ($con = 1; ;
$con++) {
($addr = accept(NS,S)) || die
$!;
select(NS); $| = 1;
select(STDOUT);
if (($child = fork()) == 0)
{
open (LOG, ">>$log_file");
writeLog("Creating SSL context...");
$ctx=Net::SSLeay::CTX_new() or die_now("CTX_new ($ctx): $!\n");
writeLog("Setting cert and RSA key...");
Net::SSLeay::CTX_set_cipher_list($ctx,'ALL');
Net::SSLeay::set_server_cert_and_key($ctx, $cert_file, $key_file) or die "key";
getTime();
($af,$c_port, $c_inetaddr) = unpack('S n a4 x8', $addr);
@inetaddr = unpack('C4', $c_inetaddr);
writeLog("Serving connection @ Internet address @inetaddr");
$conn = FALSE;
$auth = FALSE;
$user = "";
$pass = "";
##SSL NEGOTIATION
writeLog("Creating SSL session (ctx='$ctx')...");
$ssl = Net::SSLeay::new($ctx) or die_now("ssl new ($ssl): $!");
writeLog("Setting fd (ctx $ctx, con $ssl)...");
Net::SSLeay::set_fd($ssl, fileno(NS));
writeLog("Entering SSL negotiation phase...");
Net::SSLeay::accept($ssl);
die_if_ssl_error("ERROR: ssl accept: ($!)");
writeLog("SSL Cipher: ".Net::SSLeay::get_cipher($ssl));
open (LOG, ">>$log_file");
writeLog("Creating SSL context...");
$ctx=Net::SSLeay::CTX_new() or die_now("CTX_new ($ctx): $!\n");
writeLog("Setting cert and RSA key...");
Net::SSLeay::CTX_set_cipher_list($ctx,'ALL');
Net::SSLeay::set_server_cert_and_key($ctx, $cert_file, $key_file) or die "key";
getTime();
($af,$c_port, $c_inetaddr) = unpack('S n a4 x8', $addr);
@inetaddr = unpack('C4', $c_inetaddr);
writeLog("Serving connection @ Internet address @inetaddr");
$conn = FALSE;
$auth = FALSE;
$user = "";
$pass = "";
##SSL NEGOTIATION
writeLog("Creating SSL session (ctx='$ctx')...");
$ssl = Net::SSLeay::new($ctx) or die_now("ssl new ($ssl): $!");
writeLog("Setting fd (ctx $ctx, con $ssl)...");
Net::SSLeay::set_fd($ssl, fileno(NS));
writeLog("Entering SSL negotiation phase...");
Net::SSLeay::accept($ssl);
die_if_ssl_error("ERROR: ssl accept: ($!)");
writeLog("SSL Cipher: ".Net::SSLeay::get_cipher($ssl));
while (<NS>)
{
$recstr = Net::SSLeay::ssl_read_until($ssl);
process();
}
Net::SSLeay::CTX_free($ctx);
Net::SSLeay::free($ssl);
close(NS);
writeLog("Client has disconnected...");
$recstr = Net::SSLeay::ssl_read_until($ssl);
process();
}
Net::SSLeay::CTX_free($ctx);
Net::SSLeay::free($ssl);
close(NS);
writeLog("Client has disconnected...");
exit;
close(LOG);
}
Net::SSLeay::CTX_free($ctx);
close(NS);
}
close(LOG);
}
Net::SSLeay::CTX_free($ctx);
close(NS);
}
-------------------
SNIP ------------------
Here it gets stuck
where it should call the process(); it never get's the CRLF from the client
since the data is encrypted...
Please
HELP!
Thank
you!
Uroš Gaber
PowerCom Gaber & Globočnik
d.n.o.
http://www.powercom-si.com
eMail: [EMAIL PROTECTED]
Tel:
01/724-84-26 -- +386-1-7248426
Fax:
01/724-84-27 --
+386-1-7248427