Debian lenny version packages
[pkg-perl] / deb-src / libnet-ssleay-perl / libnet-ssleay-perl-1.35 / QuickRef
1 (This quick reference list was contributed by anton@genua.de. 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 # MD5 - hashfunction
186 #----------------------
187
188 $hash = Net::SSLeay:MD5($data);
189         Computes md5 hash over $data. $hash is a binary string! Convert it to
190         a printable with $string = unpack("H32",Net::SSLeay::MD5($data));
191
192
193 #----------------------
194 # TCP-Connection hints
195 #----------------------
196
197 # Make socket unbuffered after connect succeeded.
198 #
199 select(S); $| = 1; select(STDOUT);
200
201 # Close connection by half... from client to server. This signals EOF to
202 # server. (Clear some buffers, too...??)
203 # Use this if finished with sending data to remote side.
204 shutdown S, 1;
205
206 # Finally close connection. Do this after reading everything availlable!
207 #
208 close S;
209
210
211 #------------------
212 # TCP Client
213 #------------------
214
215 # #!/usr/bin/perl -w
216 use strict;
217 use Socket;
218 my ($remote,$port, $iaddr, $paddr, $proto, $line);
219
220 $remote  = shift || 'localhost';
221 $port    = shift || 3000;  # random port
222 if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
223 die "No port" unless $port;
224 $iaddr   = inet_aton($remote)               || die "no host: $remote";
225 $paddr   = sockaddr_in($port, $iaddr);
226
227 $proto   = getprotobyname('tcp');
228 socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
229 connect(SOCK, $paddr)    || die "connect: $!";
230 while (defined($line = <SOCK>)) {
231     print $line;
232 }
233
234 close (SOCK)            || die "close: $!";
235 exit;
236
237
238 #--------------------
239 # TCP Server
240 #--------------------
241
242 # #!/usr/bin/perl -Tw
243 use strict;
244 BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
245 use Socket;
246 use Carp;
247
248 sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
249
250 my $EOL = "\015\012";
251
252 my $port = shift || 3000;
253 my $proto = getprotobyname('tcp');
254 $port = $1 if $port =~ /(\d+)/; # untaint port number
255
256 socket(Server, PF_INET, SOCK_STREAM, $proto)        || die "socket: $!";
257 setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
258                                     pack("l", 1))   || die "setsockopt: $!";
259
260 bind(Server, sockaddr_in($port, INADDR_ANY))        || die "bind: $!";
261 listen(Server,SOMAXCONN)                            || die "listen: $!";
262
263 logmsg "server started on port $port";
264
265 my $paddr;
266
267 for ( ; $paddr = accept(Client,Server); close Client) {
268     my($port,$iaddr) = sockaddr_in($paddr);
269     my $name = gethostbyaddr($iaddr,AF_INET);
270
271     logmsg "connection from $name [",
272             inet_ntoa($iaddr), "]
273             at port $port";
274
275     print Client "Hello there, $name, it's now ",
276                     scalar localtime, $EOL;
277 }
278