guile: Rename to `libguile-gnutls-v-2'.
[gnutls.git] / doc / cha-library.texi
blobde34820d9fb30e3dd393f2de226b5f0224eae4f8
1 @node The Library
2 @chapter The Library
4 In brief @acronym{GnuTLS} can be described as a library which offers an API
5 to access secure communication protocols. These protocols provide
6 privacy over insecure lines, and were designed to prevent
7 eavesdropping, tampering, or message forgery.
9 Technically @acronym{GnuTLS} is a portable ANSI C based library which
10 implements the protocols ranging from SSL 3.0 to TLS 1.2 (see @ref{Introduction to TLS}, 
11 for a detailed description of the protocols), accompanied
12 with the required framework for authentication and public key
13 infrastructure.  Important features of the @acronym{GnuTLS} library
14 include:
16 @itemize
18 @item Support for TLS 1.2, TLS 1.1, TLS 1.0 and SSL 3.0 protocols.
20 @item Support for Datagram TLS 1.0.
22 @item Support for both @acronym{X.509} and @acronym{OpenPGP} certificates.
24 @item Support for handling and verification of certificates.
26 @item Support for password authentication using @acronym{TLS-SRP}.
28 @item Support for keyed authentication using @acronym{TLS-PSK}.
30 @item Support for @acronym{PKCS} #11 tokens and smart-cards.
32 @end itemize
34 The @acronym{GnuTLS} library consists of three independent parts, namely the ``TLS
35 protocol part'', the ``Certificate part'', and the ``Cryptographic
36 back-end'' part.  The ``TLS protocol part'' is the actual protocol
37 implementation, and is entirely implemented within the
38 @acronym{GnuTLS} library.  The ``Certificate part'' consists of the
39 certificate parsing, and verification functions and it uses
40 functionality from the
41 libtasn1@footnote{@url{http://www.gnu.org/software/libtasn1/}} library.
42 The ``Cryptographic back-end'' is provided by the nettle@footnote{@url{http://www.lysator.liu.se/~nisse/nettle/}}
43 library. 
44 @menu
45 * Downloading and installing::
46 * General idea::
47 * Error handling::
48 * Thread safety::
49 * Callback functions::
50 @end menu
52 @node Downloading and installing
53 @section Downloading and installing
54 @cindex installation
55 @cindex download
57 GnuTLS is available for download at:
58 @url{http://www.gnutls.org/download.html}
60 GnuTLS uses a development cycle where even minor version numbers
61 indicate a stable release and a odd minor version number indicate a
62 development release.  For example, GnuTLS 1.6.3 denote a stable
63 release since 6 is even, and GnuTLS 1.7.11 denote a development
64 release since 7 is odd.
66 GnuTLS depends on Libnettle, and you will need to install it
67 before installing GnuTLS.  Libnettle is available from
68 @url{http://www.lysator.liu.se/~nisse/nettle/}.  
69 Don't forget to verify the cryptographic signature after downloading
70 source code packages.
72 The package is then extracted, configured and built like many other
73 packages that use Autoconf.  For detailed information on configuring
74 and building it, refer to the @file{INSTALL} file that is part of the
75 distribution archive.  Typically you invoke @code{./configure} and
76 then @code{make check install}.  There are a number of compile-time
77 parameters, as discussed below.
79 The compression library, libz, as well as p11-kit are a optional dependencies.
80 You can get libz from @url{http://www.zlib.net/} and p11-kit from @url{http://p11-glue.freedesktop.org/}.
82 The X.509 part of GnuTLS needs ASN.1 functionality, from a library
83 called libtasn1.  A copy of libtasn1 is included in GnuTLS.  If you
84 want to install it separately (e.g., to make it possibly to use
85 libtasn1 in other programs), you can get it from
86 @url{http://www.gnu.org/software/gnutls/download.html}.
88 A few @code{configure} options may be relevant, summarized below.
89 They disable or enable particular features,
90 to create a smaller library with only the required features.
92 @verbatim
93 --disable-srp-authentication
94 --disable-psk-authentication
95 --disable-anon-authentication
96 --disable-extra-pki
97 --disable-openpgp-authentication
98 --disable-openssl-compatibility
99 --without-p11-kit
100 @end verbatim
102 For the complete list, refer to the output from @code{configure --help}.
104 @node General idea
105 @section General idea
107 A brief description of how @acronym{GnuTLS} works internally is shown
108 at @ref{fig:gnutls-design}. This section may be easier to understand after
109 having seen the examples at @ref{examples}.
110 As shown in the figure, there is a read-only global state that is
111 initialized once by the global initialization function.  This global
112 structure, among others, contains the memory allocation functions
113 used, and structures needed for the @acronym{ASN.1} parser.  This
114 structure is never modified by any @acronym{GnuTLS} function, except
115 for the deinitialization function which frees all allocated memory
116 and is called after the program has permanently
117 finished using @acronym{GnuTLS}.
119 @float Figure,fig:gnutls-design
120 @image{gnutls-internals,12cm}
121 @caption{High level design of GnuTLS.}
122 @end float
124 The credentials structures are used by the authentication methods, such
125 as certificate authentication. They store certificates, privates keys,
126 and other information that is needed to prove the identity to the peer,
127 and/or verify the indentity of the peer. The information stored in
128 the credentials structures is initialized once and then can be 
129 shared by many @acronym{TLS} sessions.
131 A @acronym{GnuTLS} session contains all the required information
132 to handle one secure connection. The session communicates with the
133 peers using the provided functions of the transport layer.
134 Every session has a unique session ID shared with the peer.
136 Since TLS sessions can be resumed, servers need a
137 database back-end to hold the session's parameters.  Every
138 @acronym{GnuTLS} session after a successful handshake calls the
139 appropriate back-end function (see @ref{resume})
140 to store the newly negotiated session. The session
141 database is examined by the server just after having received the
142 client hello@footnote{The first message in a @acronym{TLS} handshake},
143 and if the session ID sent by the client, matches a stored session,
144 the stored session will be retrieved, and the new session will be a
145 resumed one, and will share the same session ID with the previous one.
147 @node Error handling
148 @section Error handling
149 @subsection Conventions
151 In @acronym{GnuTLS} most functions return an integer type as a result.
152 In almost all cases a zero or a positive number means success, and a
153 negative number indicates failure, or a situation that some action has
154 to be taken. Thus negative error codes may be fatal or not.
156 Fatal errors terminate the connection immediately and further sends
157 and receives will be disallowed.  Such an example is
158 @code{GNUTLS_@-E_@-DECRYPTION_@-FAILED}. Non-fatal errors may warn about
159 something, i.e., a warning alert was received, or indicate the some
160 action has to be taken. This is the case with the error code
161 @code{GNUTLS_@-E_@-REHANDSHAKE} returned by @funcref{gnutls_record_recv}.
162 This error code indicates that the server requests a re-handshake. The
163 client may ignore this request, or may reply with an alert.  You can
164 test if an error code is a fatal one by using the
165 @funcref{gnutls_error_is_fatal}.
167 If any non fatal errors, that require an action, are to be returned by
168 a function, these error codes will be documented in the function's
169 reference.  See @ref{Error codes}, for a description of the available 
170 error codes.
172 @subsection Debugging and auditing
174 In many cases things may not go as expected and further information,
175 to assist debugging, from @acronym{GnuTLS} is desired. 
176 Those are the cases where the @funcref{gnutls_global_set_log_level} and
177 @funcref{gnutls_global_set_log_function} are to be used. Those will print
178 verbose information on the @acronym{GnuTLS} functions internal flow.
180 @showfuncB{gnutls_global_set_log_level,gnutls_global_set_log_function}
182 When debugging is not required, important issues, such as detected
183 attacks on the protocol still need to be logged. This is provided
184 by the logging function set by
185 @funcref{gnutls_global_set_audit_log_function}. The provided function
186 will receive an message and the corresponding
187 TLS session. The session information might be used to derive IP addresses
188 or other information about the peer involved.
190 @showfuncdesc{gnutls_global_set_audit_log_function}
192 @node Thread safety
193 @section Thread safety
195 The @acronym{GnuTLS} library is thread safe by design, meaning that
196 objects of the library such as TLS sessions, can be safely divided across
197 threads as long as a single thread accesses a single object. This is
198 sufficient to support a server which handles several sessions per thread.
199 If, however, an object needs to be shared across threads then access must be 
200 protected with a mutex. Read-only access to objects, for example the
201 credentials holding structures (see @ref{Authentication methods}), is also thread-safe. 
203 The random generator of the cryptographic back-end, is not thread safe and requires
204 mutex locks which are setup by @acronym{GnuTLS}.
205 Applications can either call @funcref{gnutls_global_init} which will initialize the default
206 operating system provided locks (i.e. @code{pthreads} on GNU/Linux and
207 @code{CriticalSection} on Windows), or specify manually the locking system using 
208 the function @funcref{gnutls_global_set_mutex} before calling @funcref{gnutls_global_init}. 
209 Setting manually mutexes is recommended
210 only to applications that have full control of the underlying libraries. If this
211 is not the case, the use of the operating system defaults is recommended. An example of 
212 non-native thread usage is shown below.
214 @example
215 #include <gnutls.h>
217 /* Other thread packages
218  */
220 int main()
222    gnutls_global_set_mutex (mutex_init, mutex_deinit, 
223                             mutex_lock, mutex_unlock);
224    gnutls_global_init();
226 @end example
228 @showfuncdesc{gnutls_global_set_mutex}
230 @node Callback functions
231 @section Callback functions
232 @cindex callback functions
234 There are several cases where @acronym{GnuTLS} may need out of
235 band input from your program. This is now implemented using some
236 callback functions, which your program is expected to register.
238 An example of this type of functions are the push and pull callbacks
239 which are used to specify the functions that will retrieve and send
240 data to the transport layer.
242 @showfuncB{gnutls_transport_set_push_function,gnutls_transport_set_pull_function}
244 Other callback functions may require more complicated input and data
245 to be allocated. Such an example is 
246 @funcref{gnutls_srp_set_server_credentials_function}.
247 All callbacks should allocate and free memory using the functions shown below.
249 @showfuncB{gnutls_malloc,gnutls_free}