| 1 |
(This quick reference list was contributed by [email protected]. Thanks, --Sampo) |
| 2 |
|
| 3 |
Net::SSLeay - useful function prototypes |
| 4 |
|
| 5 |
|
| 6 |
#---------------------------------- |
| 7 |
# Import frequently used functions |
| 8 |
#---------------------------------- |
| 9 |
|
| 10 |
use Net::SSLeay qw(die_now die_if_ssl_error); |
| 11 |
|
| 12 |
$errs = die_if_ssl_error($msg); |
| 13 |
Program dies with $msg if print_errs() was able to find and print |
| 14 |
some errors. |
| 15 |
$errs is 0 if no error occurred. |
| 16 |
|
| 17 |
die_now($msg); |
| 18 |
Program dies unconditionally! print_errs($msg) is used to print out |
| 19 |
errors before dying. |
| 20 |
|
| 21 |
|
| 22 |
#-------------------------- |
| 23 |
# Unsorted prototypes |
| 24 |
#-------------------------- |
| 25 |
|
| 26 |
$count = Net::SSLeay::print_errs($msg); |
| 27 |
Prints SSLeay-error stack with included $msg via 'warn'. Number of |
| 28 |
printed errors is returned (->$count). |
| 29 |
|
| 30 |
void Net::SSLeay::randomize($seed_file,$seed_string); |
| 31 |
void Net::SSLeay::randomize(); |
| 32 |
Load random bytes from $seed_file and from string $seed_string. |
| 33 |
Also uses $Net::SSLeay::random_device and $Net::SSLeay::how_random |
| 34 |
(Bits!) if used without parameters. |
| 35 |
|
| 36 |
void Net::SSLeay::RAND_seed($seed_string); |
| 37 |
Seeds randomgenerator with $seed_string. |
| 38 |
|
| 39 |
$bytes_read = Net::SSLeay::RAND_load_file($file_name, $how_much); |
| 40 |
Loads $how_much bytes randomness from file $file_name. |
| 41 |
|
| 42 |
$bytes_written = Net::SSLeay::RAND_write_file($file_name); |
| 43 |
Writes randomness to $file_name. |
| 44 |
|
| 45 |
void Net::SSLeay::load_error_strings(); |
| 46 |
Load SSL error messages to make error output more informative. |
| 47 |
|
| 48 |
void Net::SSLeay::ERR_load_crypto_strings(); |
| 49 |
Load crypto-API related error messages. |
| 50 |
|
| 51 |
void Net::SSLeay::SSLeay_add_ssl_algorithms(); |
| 52 |
Add support for supported ciphers. |
| 53 |
|
| 54 |
void Net::SSLeay::ENGINE_load_builtin_engines |
| 55 |
Load any built-in SSL engines suported by the underlybing OpenSSL |
| 56 |
|
| 57 |
void Net::SSLeay::ENGINE_register_all_complete |
| 58 |
Register any built-in SSL engines |
| 59 |
|
| 60 |
$ctx = Net::SSLeay::CTX_new(); |
| 61 |
Creates SSL-context. |
| 62 |
|
| 63 |
int Net::SSLeay::CTX_set_default_verify_paths($ctx); |
| 64 |
Load default location where to find certificates to verify |
| 65 |
remote certificates. This value is precompiled in SSLeay-Toolkit. |
| 66 |
|
| 67 |
int Net::SSLeay::CTX_load_verify_locations($ctx, $cert_file, $cert_dir); |
| 68 |
Set verify location. File with certificates or hashed directory. |
| 69 |
|
| 70 |
void Net::SSLeay::CTX_set_verify($ctx, $mode , \&verify_cb); |
| 71 |
Set mode and callback what to do with remote certificates. |
| 72 |
$mode: |
| 73 |
&Net::SSLeay::VERIFY_NONE |
| 74 |
&Net::SSLeay::VERIFY_PEER |
| 75 |
&Net::SSLeay::VERIFY_FAIL_IF_NO_PEER_CERT |
| 76 |
&Net::SSLeay::VERIFY_CLIENT_ONCE |
| 77 |
\&verify_cb: |
| 78 |
$ok = verify_cb($ok,$x509_store_ctx); |
| 79 |
Callback gets info if SSL-toolkit verified certificate ($ok) |
| 80 |
and certificate store location. |
| 81 |
|
| 82 |
void Net::SSLeay::CTX_set_default_passwd_cb($ctx,\&passwd_cb); |
| 83 |
If our RSA private key is passphrase protected and this callback is |
| 84 |
defined, then do not ask on the terminal but call the function. |
| 85 |
\&passwd_cb: |
| 86 |
$passwd = verify_cb($verify); |
| 87 |
If $verify is true, then the callback is supposed to make sure |
| 88 |
the returned password has been verified. |
| 89 |
|
| 90 |
$bool = Net::SSLeay::CTX_use_certificate_file($ctx,$cert,$type); |
| 91 |
$bool = Net::SSLeay::CTX_use_PrivateKey_file($ctx,$key,$type); |
| 92 |
Functions to load cert/key from filename ($cert/$key) with filetype |
| 93 |
$type into SSL-context. |
| 94 |
Filetypes are: |
| 95 |
&Net::SSLeay::FILETYPE_PEM |
| 96 |
|
| 97 |
$ssl = Net::SSLeay::new($ctx) |
| 98 |
Creates a SSL-session in context $ctx. Returns 0 on failure. |
| 99 |
|
| 100 |
$bool = Net::SSLeay::use_certificate_file($ssl,$cert,$type); |
| 101 |
$bool = Net::SSLeay::use_RSAPrivateKey_file($ssl,$key,$type); |
| 102 |
Functions to load cert/key from filename ($cert/$key) with filetype |
| 103 |
$type into SSL-session. |
| 104 |
Filetypes are: |
| 105 |
&Net::SSLeay::FILETYPE_PEM |
| 106 |
|
| 107 |
$bool = Net::SSLeay::set_fd($ssl, fileno(S)); |
| 108 |
Connect SSL-Toolkit with TCP-connection. |
| 109 |
$ssl SSL-Session |
| 110 |
S open socket |
| 111 |
$bool 0-failure 1-success |
| 112 |
|
| 113 |
$bool = Net::SSLeay::accept($ssl); |
| 114 |
Make SSL-handshake on hot connection. I am server! |
| 115 |
$ssl SSL-session |
| 116 |
$bool 0-failure 1-success |
| 117 |
|
| 118 |
$bool = Net::SSLeay::connect($ssl); |
| 119 |
Make SSL-handshake on hot connection. I am client! |
| 120 |
$ssl SSL-session |
| 121 |
$bool 0-failure 1-success |
| 122 |
|
| 123 |
$x509 = Net::SSLeay::get_peer_certificate($ssl); |
| 124 |
Get X509 certificate from SSL_session. |
| 125 |
|
| 126 |
$x509 = Net::SSLeay::X509_STORE_CTX_get_current_cert($x509_store_ctx) |
| 127 |
Extract current certificate from cert-store. Cert-store is |
| 128 |
used in callbacks! |
| 129 |
|
| 130 |
$asn1_utctime = Net::SSLeay::X509_get_notBefore($x509); |
| 131 |
$asn1_utctime = Net::SSLeay::X509_get_notAfter($x509); |
| 132 |
$x509_name = Net::SSLeay::X509_get_subject_name($x509); |
| 133 |
$x509_name = Net::SSLeay::X509_get_issuer_name($x509); |
| 134 |
($type1, $subject1, $type2, $subject2, ...) = Net::SSLeay::X509_get_subjectAltNames($x509) |
| 135 |
subjectAltName types as per x509v3.h GEN_* for example: |
| 136 |
GEN_DNS == 2 |
| 137 |
GEN_IPADD == 7 |
| 138 |
Return information from a certificate. |
| 139 |
|
| 140 |
$string = Net::SSLeay::P_ASN1_UTCTIME_put2string($asn1_utctime); |
| 141 |
Convert a asn1_utctime structure to a printable string. |
| 142 |
|
| 143 |
$string = Net::SSLeay::X509_NAME_oneline($x509_name); |
| 144 |
Convert a x509_name structure to a printable string. |
| 145 |
|
| 146 |
$string = Net::SSLeay::get_cipher($ssl) |
| 147 |
Return the active cipher from SSL-session $ssl. |
| 148 |
|
| 149 |
$string = Net::SSLeay::dump_peer_certificate($ssl) |
| 150 |
Return Subject/Issuer from peer-certificate in printable string. |
| 151 |
|
| 152 |
$string = Net::SSLeay::PEM_get_string_X509($x509); |
| 153 |
Returns a printable string containing the X509 certificate PEM encoded |
| 154 |
from $x509. |
| 155 |
|
| 156 |
$mode = Net::SSLeay::CTX_get_verify_mode($ctx) |
| 157 |
Return verify-mode previously set with CTX_set_verify in SSL-context. |
| 158 |
|
| 159 |
$mode = Net::SSLeay::get_verify_mode($ssl) |
| 160 |
Return verify-mode in SSL-session. |
| 161 |
|
| 162 |
$written_bytes = Net::SSLeay::ssl_write_all($ssl,$string); |
| 163 |
Write $string to SSL-session. This call returns undef if write failed. |
| 164 |
The whole string gets written! |
| 165 |
|
| 166 |
$written_bytes = $Net::SSLeay::write($ssl,$string); |
| 167 |
Write $string to SSL-session. This call returns immediately. SSL maybe |
| 168 |
wrote the string not completely - check yourself or use ssl_write_all! |
| 169 |
|
| 170 |
$string = Net::SSLeay::ssl_read_all($ssl,$how_much); |
| 171 |
Read everything available from the SSL-session and return it. Read a |
| 172 |
maximum of $how_much Bytes (default: 2000000000). |
| 173 |
|
| 174 |
$string = Net::SSLeay::read($ssl); |
| 175 |
Read one bunch of data from the SSL-session and return. |
| 176 |
|
| 177 |
void Net::SSLeay::free ($ssl); |
| 178 |
Free ressources from the SSL-session. |
| 179 |
|
| 180 |
void Net::SSLeay::CTX_free ($ctx); |
| 181 |
Free ressources from the SSL-context. |
| 182 |
|
| 183 |
|
| 184 |
#---------------------- |
| 185 |
# Hash functions |
| 186 |
#---------------------- |
| 187 |
|
| 188 |
# Several cryptographic digests (hash functions) are supported, possibly |
| 189 |
# including MD2, MD4, MD5, and RIPEMD160. |
| 190 |
|
| 191 |
$hash = Net::SSLeay:MD5($data); |
| 192 |
Computes md5 hash over $data. $hash is a binary string! Convert it to |
| 193 |
a printable with $string = unpack("H32",Net::SSLeay::MD5($data)); |
| 194 |
|
| 195 |
$hash = Net::SSLeay:RIPEMD160($data); |
| 196 |
Computes RIPEMD160 hash over $data. $hash is a binary string! Convert it to |
| 197 |
a printable with $string = unpack("H40",Net::SSLeay::RIPEMD160($data)); |
| 198 |
|
| 199 |
Note that some digests may not be available, depending on the version |
| 200 |
of OpenSSL used. |
| 201 |
|
| 202 |
|
| 203 |
#---------------------- |
| 204 |
# TCP-Connection hints |
| 205 |
#---------------------- |
| 206 |
|
| 207 |
# Make socket unbuffered after connect succeeded. |
| 208 |
# |
| 209 |
select(S); $| = 1; select(STDOUT); |
| 210 |
|
| 211 |
# Close connection by half... from client to server. This signals EOF to |
| 212 |
# server. (Clear some buffers, too...??) |
| 213 |
# Use this if finished with sending data to remote side. |
| 214 |
shutdown S, 1; |
| 215 |
|
| 216 |
# Finally close connection. Do this after reading everything availlable! |
| 217 |
# |
| 218 |
close S; |
| 219 |
|
| 220 |
|
| 221 |
#------------------ |
| 222 |
# TCP Client |
| 223 |
#------------------ |
| 224 |
|
| 225 |
# #!/usr/bin/perl -w |
| 226 |
use strict; |
| 227 |
use Socket; |
| 228 |
my ($remote,$port, $iaddr, $paddr, $proto, $line); |
| 229 |
|
| 230 |
$remote = shift || 'localhost'; |
| 231 |
$port = shift || 3000; # random port |
| 232 |
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } |
| 233 |
die "No port" unless $port; |
| 234 |
$iaddr = inet_aton($remote) || die "no host: $remote"; |
| 235 |
$paddr = sockaddr_in($port, $iaddr); |
| 236 |
|
| 237 |
$proto = getprotobyname('tcp'); |
| 238 |
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; |
| 239 |
connect(SOCK, $paddr) || die "connect: $!"; |
| 240 |
while (defined($line = <SOCK>)) { |
| 241 |
print $line; |
| 242 |
} |
| 243 |
|
| 244 |
close (SOCK) || die "close: $!"; |
| 245 |
exit; |
| 246 |
|
| 247 |
|
| 248 |
#-------------------- |
| 249 |
# TCP Server |
| 250 |
#-------------------- |
| 251 |
|
| 252 |
# #!/usr/bin/perl -Tw |
| 253 |
use strict; |
| 254 |
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } |
| 255 |
use Socket; |
| 256 |
use Carp; |
| 257 |
|
| 258 |
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } |
| 259 |
|
| 260 |
my $EOL = "\015\012"; |
| 261 |
|
| 262 |
my $port = shift || 3000; |
| 263 |
my $proto = getprotobyname('tcp'); |
| 264 |
$port = $1 if $port =~ /(\d+)/; # untaint port number |
| 265 |
|
| 266 |
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; |
| 267 |
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, |
| 268 |
pack("l", 1)) || die "setsockopt: $!"; |
| 269 |
|
| 270 |
bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; |
| 271 |
listen(Server,SOMAXCONN) || die "listen: $!"; |
| 272 |
|
| 273 |
logmsg "server started on port $port"; |
| 274 |
|
| 275 |
my $paddr; |
| 276 |
|
| 277 |
for ( ; $paddr = accept(Client,Server); close Client) { |
| 278 |
my($port,$iaddr) = sockaddr_in($paddr); |
| 279 |
my $name = gethostbyaddr($iaddr,AF_INET); |
| 280 |
|
| 281 |
logmsg "connection from $name [", |
| 282 |
inet_ntoa($iaddr), "] |
| 283 |
at port $port"; |
| 284 |
|
| 285 |
print Client "Hello there, $name, it's now ", |
| 286 |
scalar localtime, $EOL; |
| 287 |
} |
| 288 |
|