Example: TLS/SSL client
Demonstrates how to establish a SSL connection with ssl module based on OpenSSL.
<?v1
dl ("ssl"); // Load ssl module
host = "v1-script.net";
print ("Connect to ", host);
// Create classic socket
socket = fsockopen (host, 443);
if (socket) {
// Create SSL Context
ctx = SSL_CTX_new ();
SSL_CTX_set_options (ctx, SSL_FLAG("SSL_OP_NO_SSLv3"));
printf ("SSL_CTX_set_options Flags = 0x%.8x", SSL_CTX_get_options (ctx));
// Create SSL
ssl = SSL_new (ctx);
SSL_set_cipher_list (ssl, "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4");
// Set socket
SSL_set_fd (ssl, socket);
// Connect SSL
if (SSL_connect (ssl, host)) {
// Print certificate information
print ("\r\nCertificate:");
cert = SSL_get_peer_certificate (ssl);
foreach (cert as key => data) {
if (!is_array (data))
print (key." = ".data);
else {
foreach (data as key2 => value2) {
print (key.":".key2." = ".value2);
}
}
}
// Print ciphers
print ("\r\nCiphers:");
print (SSL_get_cipher_list (ssl));
// Write HTTP header
SSL_write (ssl, "GET / HTTP/1.1\r\nHost: ".host."\r\nConnection: close\r\n\r\n");
// Read header
print ("\r\nHTTP-Header:");
str = "";
while (SSL_readln (ssl, str)) {
print (str);
if (str=="")
break;
}
}
// Free SSL
SSL_free (ssl);
// Free SSL Context
SSL_CTX_free (ctx);
// Close original socket
fclose (socket);
}
?>