doc: Fix pkg-config recommendation.
[gnutls.git] / doc / gnutls.texi
blobe761c68e7c538a7b75f9716817661b445b51a038
1 \input texinfo   @c -*-texinfo-*-
2 @comment %**start of header
3 @setfilename gnutls.info
4 @include version.texi
5 @settitle GNU TLS @value{VERSION}
7 @c don't indent the paragraphs.
8 @paragraphindent 0
10 @c Unify some of the indices.
11 @syncodeindex tp fn
12 @syncodeindex pg cp
14 @comment %**end of header
15 @finalout
16 @copying
17 This manual is last updated @value{UPDATED} for version
18 @value{VERSION} of GNU TLS.
20 Copyright @copyright{} 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
22 @quotation
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.3 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.  A
27 copy of the license is included in the section entitled ``GNU Free
28 Documentation License''.
29 @end quotation
30 @end copying
32 @dircategory Software libraries
33 @direntry
34 * GnuTLS: (gnutls).             GNU Transport Layer Security Library.
35 @end direntry
37 @dircategory System Administration
38 @direntry
39 * certtool: (gnutls)Invoking certtool.  Manipulate certificates and keys.
40 * gnutls-serv: (gnutls)Invoking gnutls-serv.    GNU TLS test server.
41 * gnutls-cli: (gnutls)Invoking gnutls-cli.      GNU TLS test client.
42 * gnutls-cli-debug: (gnutls)Invoking gnutls-cli-debug.  GNU TLS debug client.
43 * psktool: (gnutls)Invoking psktool.    Simple TLS-Pre-Shared-Keys manager.
44 * srptool: (gnutls)Invoking srptool.    Simple SRP password tool.
45 @end direntry
47 @titlepage
48 @title GNU TLS
49 @subtitle Transport Layer Security Library for the GNU system
50 @subtitle for version @value{VERSION}, @value{UPDATED}
51 @sp 7
52 @image{gnutls-logo,6cm,6cm}
53 @author Nikos Mavrogiannopoulos
54 @author Simon Josefsson (@email{bug-gnutls@@gnu.org})
55 @page
56 @vskip 0pt plus 1filll
57 @insertcopying
58 @end titlepage
60 @macro xcite{ref}
61 [\ref\] (@pxref{Bibliography})
62 @end macro
64 @contents
66 @ifnottex
67 @node Top
68 @top GNU TLS
70 @insertcopying
71 @end ifnottex
73 @menu
74 * Preface::
75 * The Library::
76 * Introduction to TLS::
77 * Authentication methods::
78 * More on certificate authentication::
79 * How to use TLS in application protocols::
80 * How to use GnuTLS in applications::
81 * Included programs::
82 * Function reference::
83 * All the supported ciphersuites in GnuTLS::
84 * Guile Bindings::
85 * Internal architecture of GnuTLS::
86 * Copying Information::
87 * Concept Index::
88 * Function and Data Index::
89 @c * @mybibnode{}::
90 * Bibliography::
91 @end menu
94 @node Preface
95 @chapter Preface
97 This document tries to demonstrate and explain the @acronym{GnuTLS}
98 library API.  A brief introduction to the protocols and the technology
99 involved, is also included so that an application programmer can
100 better understand the @acronym{GnuTLS} purpose and actual offerings.
101 Even if @acronym{GnuTLS} is a typical library software, it operates
102 over several security and cryptographic protocols, which require the
103 programmer to make careful and correct usage of them, otherwise he
104 risks to offer just a false sense of security. Security and the
105 network security terms are very general terms even for computer
106 software thus cannot be easily restricted to a single cryptographic
107 library.  For that reason, do not consider a program secure just
108 because it uses @acronym{GnuTLS}; there are several ways to compromise
109 a program or a communication line and @acronym{GnuTLS} only helps with
110 some of them.
112 Although this document tries to be self contained, basic network
113 programming and PKI knowlegde is assumed in most of it. A good
114 introduction to networking can be found in @xcite{STEVENS} and for
115 Public Key Infrastructure in @xcite{GUTPKI}.
117 @anchor{Availability}
119 Updated versions of the @acronym{GnuTLS} software and this document
120 will be available from @url{http://www.gnutls.org/} and
121 @url{http://www.gnu.org/software/gnutls/}.
123 @menu
124 * Getting help::
125 * Commercial Support::
126 * Downloading and Installing::
127 * Bug Reports::
128 * Contributing::
129 @end menu
131 @node Getting help
132 @section Getting Help
134 A mailing list where users may help each other exists, and you can
135 reach it by sending e-mail to @email{help-gnutls@@gnu.org}.  Archives
136 of the mailing list discussions, and an interface to manage
137 subscriptions, is available through the World Wide Web at
138 @url{http://lists.gnu.org/mailman/listinfo/help-gnutls}.
140 A mailing list for developers are also available, see
141 @url{http://www.gnu.org/software/gnutls/lists.html}.
143 Bug reports should be sent to @email{bug-gnutls@@gnu.org}, see
144 @xref{Bug Reports}.
146 @node Commercial Support
147 @section Commercial Support
149 Commercial support is available for users of GnuTLS.  The kind of
150 support that can be purchased may include:
152 @itemize
154 @item Implement new features.
155 Such as a new TLS extension.
157 @item Port GnuTLS to new platforms.
158 This could include porting to an embedded platforms that may need
159 memory or size optimization.
161 @item Integrating TLS as a security environment in your existing project.
163 @item System design of components related to TLS.
165 @end itemize
167 If you are interested, please write to:
169 @verbatim
170 Simon Josefsson Datakonsult
171 Hagagatan 24
172 113 47 Stockholm
173 Sweden
175 E-mail: simon@josefsson.org
176 @end verbatim
178 If your company provides support related to GnuTLS and would like to
179 be mentioned here, contact the author (@pxref{Bug Reports}).
181 @node Downloading and Installing
182 @section Downloading and Installing
183 @cindex Installation
184 @cindex Download
186 GnuTLS is available for download from the following URL:
188 @url{http://www.gnutls.org/download.html}
190 The latest version is stored in a file, e.g.,
191 @samp{gnutls-@value{VERSION}.tar.gz} where the @samp{@value{VERSION}}
192 value is the highest version number in the directory.
194 GnuTLS uses a Linux-like development cycle: even minor version numbers
195 indicate a stable release and a odd minor version number indicates a
196 development release.  For example, GnuTLS 1.6.3 denote a stable
197 release since 6 is even, and GnuTLS 1.7.11 denote a development
198 release since 7 is odd.
200 GnuTLS depends on Libgcrypt,
201 and you will need to install Libgcrypt
202 before installing GnuTLS.  Libgcrypt is available from
203 @url{ftp://ftp.gnupg.org/gcrypt/libgcrypt}.  Libgcrypt needs another
204 library, libgpg-error, and you need to install libgpg-error before
205 installing Libgcrypt.  Libgpg-error is available from
206 @url{ftp://ftp.gnupg.org/gcrypt/libgpg-error}.
208 Don't forget to verify the cryptographic signature after downloading
209 source code packages.
211 The package is then extracted, configured and built like many other
212 packages that use Autoconf.  For detailed information on configuring
213 and building it, refer to the @file{INSTALL} file that is part of the
214 distribution archive.  Typically you invoke @code{./configure} and
215 then @code{make check install}.  There are a number of compile-time
216 parameters, as discussed below.
218 The compression libraries (libz and lzo) are optional dependencies.
219 You can get libz from @url{http://www.zlib.net/}.  You can get lzo
220 from @url{http://www.oberhumer.com/opensource/lzo/}.
222 The X.509 part of GnuTLS needs ASN.1 functionality, from a library
223 called libtasn1.  A copy of libtasn1 is included in GnuTLS.  If you
224 want to install it separately (e.g., to make it possibly to use
225 libtasn1 in other programs), you can get it from
226 @url{http://www.gnu.org/software/gnutls/download.html}.
228 The OpenPGP part of GnuTLS uses a stripped down version of OpenCDK for
229 parsing OpenPGP packets.  It is included GnuTLS.  Use parameter
230 @code{--disable-openpgp-authentication} to disable the OpenPGP
231 functionality in GnuTLS.  Unfortunately, we didn't have resources to
232 maintain the code in a separate library.
234 Regarding the Guile bindings, there are additional installation
235 considerations, see @xref{Guile Preparations}.
237 A few @code{configure} options may be relevant, summarized in the
238 table.
240 @table @code
242 @item --disable-srp-authentication
243 @itemx --disable-psk-authentication
244 @itemx --disable-anon-authentication
245 @itemx --disable-extra-pki
246 @itemx --disable-openpgp-authentication
247 @itemx --disable-openssl-compatibility
248 Disable or enable particular features.  Generally not recommended.
250 @end table
252 For the complete list, refer to the output from @code{configure
253 --help}.
255 @node Bug Reports
256 @section Bug Reports
257 @cindex Reporting Bugs
259 If you think you have found a bug in GnuTLS, please investigate it and
260 report it.
262 @itemize @bullet
264 @item Please make sure that the bug is really in GnuTLS, and
265 preferably also check that it hasn't already been fixed in the latest
266 version.
268 @item You have to send us a test case that makes it possible for us to
269 reproduce the bug.
271 @item You also have to explain what is wrong; if you get a crash, or
272 if the results printed are not good and in that case, in what way.
273 Make sure that the bug report includes all information you would need
274 to fix this kind of bug for someone else.
276 @end itemize
278 Please make an effort to produce a self-contained report, with
279 something definite that can be tested or debugged.  Vague queries or
280 piecemeal messages are difficult to act on and don't help the
281 development effort.
283 If your bug report is good, we will do our best to help you to get a
284 corrected version of the software; if the bug report is poor, we won't
285 do anything about it (apart from asking you to send better bug
286 reports).
288 If you think something in this manual is unclear, or downright
289 incorrect, or if the language needs to be improved, please also send a
290 note.
292 Send your bug report to:
294 @center @samp{bug-gnutls@@gnu.org}
296 @node Contributing
297 @section Contributing
298 @cindex Contributing
299 @cindex Hacking
301 If you want to submit a patch for inclusion -- from solve a typo you
302 discovered, up to adding support for a new feature -- you should
303 submit it as a bug report (@pxref{Bug Reports}).  There are some
304 things that you can do to increase the chances for it to be included
305 in the official package.
307 Unless your patch is very small (say, under 10 lines) we require that
308 you assign the copyright of your work to the Free Software Foundation.
309 This is to protect the freedom of the project.  If you have not
310 already signed papers, we will send you the necessary information when
311 you submit your contribution.
313 For contributions that doesn't consist of actual programming code, the
314 only guidelines are common sense.  Use it.
316 For code contributions, a number of style guides will help you:
318 @itemize @bullet
320 @item Coding Style.
321 Follow the GNU Standards document (@pxref{top, GNU Coding Standards,,
322 standards}).
324 If you normally code using another coding standard, there is no
325 problem, but you should use @samp{indent} to reformat the code
326 (@pxref{top, GNU Indent,, indent}) before submitting your work.
328 @item Use the unified diff format @samp{diff -u}.
330 @item Return errors.
331 No reason whatsoever should abort the execution of the library.  Even
332 memory allocation errors, e.g. when malloc return NULL, should work
333 although result in an error code.
335 @item Design with thread safety in mind.
336 Don't use global variables.  Don't even write to per-handle global
337 variables unless the documented behaviour of the function you write is
338 to write to the per-handle global variable.
340 @item Avoid using the C math library.
341 It causes problems for embedded implementations, and in most
342 situations it is very easy to avoid using it.
344 @item Document your functions.
345 Use comments before each function headers, that, if properly
346 formatted, are extracted into Texinfo manuals and GTK-DOC web pages.
348 @item Supply a ChangeLog and NEWS entries, where appropriate.
350 @end itemize
351 @node The Library
352 @chapter The Library
354 In brief @acronym{GnuTLS} can be described as a library which offers an API
355 to access secure communication protocols. These protocols provide
356 privacy over insecure lines, and were designed to prevent
357 eavesdropping, tampering, or message forgery.
359 Technically @acronym{GnuTLS} is a portable ANSI C based library which
360 implements the TLS 1.1 and SSL 3.0 protocols (@xref{Introduction to
361 TLS}, for a more detailed description of the protocols), accompanied
362 with the required framework for authentication and public key
363 infrastructure.  Important features of the @acronym{GnuTLS} library
364 include:
366 @itemize
368 @item Support for TLS 1.0, TLS 1.1, and SSL 3.0 protocols.
370 @item Support for both @acronym{X.509} and @acronym{OpenPGP} certificates.
372 @item Support for handling and verification of certificates.
374 @item Support for @acronym{SRP} for TLS authentication.
376 @item Support for @acronym{PSK} for TLS authentication.
378 @item Support for TLS Extension mechanism.
380 @item Support for TLS Compression Methods.
382 @end itemize
384 Additionally @acronym{GnuTLS} provides a limited emulation API for the
385 widely used OpenSSL@footnote{@url{http://www.openssl.org/}} library,
386 to ease integration with existing applications.
388 @acronym{GnuTLS} consists of three independent parts, namely the ``TLS
389 protocol part'', the ``Certificate part'', and the ``Cryptographic
390 backend'' part.  The `TLS protocol part' is the actual protocol
391 implementation, and is entirely implemented within the
392 @acronym{GnuTLS} library.  The `Certificate part' consists of the
393 certificate parsing, and verification functions which is partially
394 implemented in the @acronym{GnuTLS} library.  The
395 @acronym{Libtasn1}@footnote{@url{ftp://ftp.gnupg.org/gcrypt/alpha/gnutls/libtasn1/}},
396 a library which offers @acronym{ASN.1} parsing capabilities, is used
397 for the @acronym{X.509} certificate parsing functions.  A smaller
398 version of
399 @acronym{OpenCDK}@footnote{@url{ftp://ftp.gnupg.org/gcrypt/alpha/gnutls/opencdk/}}
400 is used for the @acronym{OpenPGP} key support in @acronym{GnuTLS}.
401 The ``Cryptographic backend'' is provided by the
402 @acronym{Libgcrypt}@footnote{@url{ftp://ftp.gnupg.org/gcrypt/alpha/libgcrypt/}}
403 library@footnote{On current versions of GnuTLS it is possible to
404 override the default crypto backend. Check @pxref{Cryptographic
405 Backend} for details}.
407 In order to ease integration in embedded systems, parts of the
408 @acronym{GnuTLS} library can be disabled at compile time. That way a
409 small library, with the required features, can be generated.
411 @menu
412 * General Idea::
413 * Error handling::
414 * Memory handling::
415 * Callback functions::
416 @end menu
418 @node General Idea
419 @section General Idea
421 A brief description of how @acronym{GnuTLS} works internally is shown
422 at the figure below. This section may be easier to understand after
423 having seen the examples (@pxref{examples}).
425 @image{gnutls-internals,12cm,8cm}
427 As shown in the figure, there is a read-only global state that is
428 initialized once by the global initialization function.  This global
429 structure, among others, contains the memory allocation functions
430 used, and some structures needed for the @acronym{ASN.1} parser.  This
431 structure is never modified by any @acronym{GnuTLS} function, except
432 for the deinitialization function which frees all memory allocated in
433 the global structure and is called after the program has permanently
434 finished using @acronym{GnuTLS}.
436 The credentials structure is used by some authentication methods, such
437 as certificate authentication (@pxref{Certificate Authentication}).  A
438 credentials structure may contain certificates, private keys,
439 temporary parameters for Diffie-Hellman or RSA key exchange, and other
440 stuff that may be shared between several TLS sessions.
442 This structure should be initialized using the appropriate
443 initialization functions. For example an application which uses
444 certificate authentication would probably initialize the credentials,
445 using the appropriate functions, and put its trusted certificates in
446 this structure. The next step is to associate the credentials
447 structure with each @acronym{TLS} session.
449 A @acronym{GnuTLS} session contains all the required stuff for a
450 session to handle one secure connection. This session calls directly
451 to the transport layer functions, in order to communicate with the
452 peer.  Every session has a unique session ID shared with the peer.
454 Since TLS sessions can be resumed, servers would probably need a
455 database backend to hold the session's parameters.  Every
456 @acronym{GnuTLS} session after a successful handshake calls the
457 appropriate backend function (@xref{resume}, for information on
458 initialization) to store the newly negotiated session. The session
459 database is examined by the server just after having received the
460 client hello@footnote{The first message in a @acronym{TLS} handshake},
461 and if the session ID sent by the client, matches a stored session,
462 the stored session will be retrieved, and the new session will be a
463 resumed one, and will share the same session ID with the previous one.
465 @node Error handling
466 @section Error Handling
468 In @acronym{GnuTLS} most functions return an integer type as a result.
469 In almost all cases a zero or a positive number means success, and a
470 negative number indicates failure, or a situation that some action has
471 to be taken. Thus negative error codes may be fatal or not.
473 Fatal errors terminate the connection immediately and further sends
474 and receives will be disallowed. An example of a fatal error code is
475 @code{GNUTLS_E_DECRYPTION_FAILED}. Non-fatal errors may warn about
476 something, i.e., a warning alert was received, or indicate the some
477 action has to be taken. This is the case with the error code
478 @code{GNUTLS_E_REHANDSHAKE} returned by @ref{gnutls_record_recv}.
479 This error code indicates that the server requests a re-handshake. The
480 client may ignore this request, or may reply with an alert.  You can
481 test if an error code is a fatal one by using the
482 @ref{gnutls_error_is_fatal}.
484 If any non fatal errors, that require an action, are to be returned by
485 a function, these error codes will be documented in the function's
486 reference.  @xref{Error Codes}, for all the error codes.
488 @node Memory handling
489 @section Memory Handling
491 @acronym{GnuTLS} internally handles heap allocated objects
492 differently, depending on the sensitivity of the data they
493 contain. However for performance reasons, the default memory functions
494 do not overwrite sensitive data from memory, nor protect such objects
495 from being written to the swap.  In order to change the default
496 behavior the @ref{gnutls_global_set_mem_functions} function is
497 available which can be used to set other memory handlers than the
498 defaults.
500 The @acronym{Libgcrypt} library on which @acronym{GnuTLS} depends, has
501 such secure memory allocation functions available. These should be
502 used in cases where even the system's swap memory is not considered
503 secure. See the documentation of @acronym{Libgcrypt} for more
504 information.
506 @node Callback functions
507 @section Callback Functions
508 @cindex Callback functions
510 There are several cases where @acronym{GnuTLS} may need some out of
511 band input from your program. This is now implemented using some
512 callback functions, which your program is expected to register.
514 An example of this type of functions are the push and pull callbacks
515 which are used to specify the functions that will retrieve and send
516 data to the transport layer.
518 @itemize
520 @item @ref{gnutls_transport_set_push_function}
522 @item @ref{gnutls_transport_set_pull_function}
524 @end itemize
526 Other callback functions such as the one set by
527 @ref{gnutls_srp_set_server_credentials_function}, may require more
528 complicated input, including data to be allocated.  These callbacks
529 should allocate and free memory using the functions shown below.
531 @itemize
533 @item @ref{gnutls_malloc}
535 @item @ref{gnutls_free}
537 @end itemize
539 @node Introduction to TLS
540 @chapter Introduction to @acronym{TLS}
542 @acronym{TLS} stands for ``Transport Layer Security'' and is the
543 successor of SSL, the Secure Sockets Layer protocol @xcite{SSL3}
544 designed by Netscape.  @acronym{TLS} is an Internet protocol, defined
545 by @acronym{IETF}@footnote{IETF, or Internet Engineering Task Force,
546 is a large open international community of network designers,
547 operators, vendors, and researchers concerned with the evolution of
548 the Internet architecture and the smooth operation of the Internet.
549 It is open to any interested individual.}, described in @acronym{RFC}
550 4346 and also in @xcite{RESCORLA}.  The protocol provides
551 confidentiality, and authentication layers over any reliable transport
552 layer.  The description, below, refers to @acronym{TLS} 1.0 but also
553 applies to @acronym{TLS} 1.1 @xcite{RFC4346} and @acronym{SSL} 3.0,
554 since the differences of these protocols are minor.  Older protocols
555 such as @acronym{SSL} 2.0 are not discussed nor implemented in
556 @acronym{GnuTLS} since they are not considered secure today.  GnuTLS
557 also supports @acronym{X.509} and @acronym{OpenPGP} @xcite{RFC4880}.
559 @menu
560 * TLS layers::
561 * The transport layer::
562 * The TLS record protocol::
563 * The TLS Alert Protocol::
564 * The TLS Handshake Protocol::
565 * TLS Extensions::
566 * Selecting cryptographic key sizes::
567 * On SSL 2 and older protocols::
568 * On Record Padding::
569 @end menu
571 @node TLS layers
572 @section TLS Layers
573 @cindex TLS Layers
575 @acronym{TLS} is a layered protocol, and consists of the Record
576 Protocol, the Handshake Protocol and the Alert Protocol. The Record
577 Protocol is to serve all other protocols and is above the transport
578 layer.  The Record protocol offers symmetric encryption, data
579 authenticity, and optionally compression.
581 The Alert protocol offers some signaling to the other protocols. It
582 can help informing the peer for the cause of failures and other error
583 conditions.  @xref{The Alert Protocol}, for more information.  The
584 alert protocol is above the record protocol.
586 The Handshake protocol is responsible for the security parameters'
587 negotiation, the initial key exchange and authentication.  @xref{The
588 Handshake Protocol}, for more information about the handshake
589 protocol.  The protocol layering in TLS is shown in the figure below.
591 @image{gnutls-layers,12cm,8cm}
593 @node The transport layer
594 @section The Transport Layer
595 @cindex Transport protocol
597 @acronym{TLS} is not limited to one transport layer, it can be used
598 above any transport layer, as long as it is a reliable one.  A set of
599 functions is provided and their purpose is to load to @acronym{GnuTLS} the
600 required callbacks to access the transport layer.
602 @itemize
603 @item @ref{gnutls_transport_set_push_function}
604 @item @ref{gnutls_transport_set_pull_function}
605 @item @ref{gnutls_transport_set_ptr}
606 @item @ref{gnutls_transport_set_lowat}
607 @item @ref{gnutls_transport_set_errno}
608 @end itemize
610 These functions accept a callback function as a parameter.  The
611 callback functions should return the number of bytes written, or -1 on
612 error and should set @code{errno} appropriately.
614 In some environments, setting @code{errno} is unreliable, for example
615 Windows have several errno variables in different CRTs, or it may be
616 that errno is not a thread-local variable.  If this is a concern to
617 you, call @code{gnutls_transport_set_errno} with the intended errno
618 value instead of setting @code{errno} directly.
620 @acronym{GnuTLS} currently only interprets the EINTR and EAGAIN errno
621 values and returns the corresponding @acronym{GnuTLS} error codes
622 @code{GNUTLS_E_INTERRUPTED} and @code{GNUTLS_E_AGAIN}.  These values
623 are usually returned by interrupted system calls, or when non blocking
624 IO is used.  All @acronym{GnuTLS} functions can be resumed (called
625 again), if any of these error codes is returned.  The error codes
626 above refer to the system call, not the @acronym{GnuTLS} function,
627 since signals do not interrupt @acronym{GnuTLS}' functions.
629 For non blocking sockets or other custom made pull/push functions
630 the @ref{gnutls_transport_set_lowat} must be called, with a zero
631 low water mark value.
633 By default, if the transport functions are not set, @acronym{GnuTLS}
634 will use the Berkeley Sockets functions.  In this case
635 @acronym{GnuTLS} will use some hacks in order for @code{select} to
636 work, thus making it easy to add @acronym{TLS} support to existing
637 TCP/IP servers.
639 @node The TLS record protocol
640 @section The TLS Record Protocol
641 @cindex Record protocol
643 The Record protocol is the secure communications provider. Its purpose
644 is to encrypt, authenticate and ---optionally--- compress packets.
645 The following functions are available:
647 @table @asis
649 @item @ref{gnutls_record_send}:
650 To send a record packet (with application data).
652 @item @ref{gnutls_record_recv}:
653 To receive a record packet (with application data).
655 @item @ref{gnutls_record_get_direction}:
656 To get the direction of the last interrupted function call.
657 @end table
659 As you may have already noticed, the functions which access the Record
660 protocol, are quite limited, given the importance of this protocol in
661 @acronym{TLS}.  This is because the Record protocol's parameters are
662 all set by the Handshake protocol.
664 The Record protocol initially starts with NULL parameters, which means
665 no encryption, and no MAC is used. Encryption and authentication begin
666 just after the handshake protocol has finished.
668 @menu
669 * Encryption algorithms used in the record layer::
670 * Compression algorithms used in the record layer::
671 * Weaknesses and countermeasures::
672 @end menu
674 @node Encryption algorithms used in the record layer
675 @subsection Encryption Algorithms Used in the Record Layer
676 @cindex Symmetric encryption algorithms
678 Confidentiality in the record layer is achieved by using symmetric
679 block encryption algorithms like @code{3DES}, @code{AES}@footnote{AES,
680 or Advanced Encryption Standard, is actually the RIJNDAEL algorithm.
681 This is the algorithm that replaced DES.}, or stream algorithms like
682 @code{ARCFOUR_128}@footnote{@code{ARCFOUR_128} is a compatible
683 algorithm with RSA's RC4 algorithm, which is considered to be a trade
684 secret.}. Ciphers are encryption algorithms that use a single, secret,
685 key to encrypt and decrypt data. Block algorithms in TLS also provide
686 protection against statistical analysis of the data.  Thus, if you're
687 using the @acronym{TLS} protocol, a random number of blocks will be
688 appended to data, to prevent eavesdroppers from guessing the actual
689 data size.
691 Supported cipher algorithms:
693 @table @code
694 @item 3DES_CBC
695 @code{3DES_CBC} is the DES block cipher algorithm used with triple
696 encryption (EDE). Has 64 bits block size and is used in CBC mode.
698 @item ARCFOUR_128
699 ARCFOUR is a fast stream cipher.
701 @item ARCFOUR_40
702 This is the ARCFOUR cipher that is fed with a 40 bit key,
703 which is considered weak.
705 @item AES_CBC
706 AES or RIJNDAEL is the block cipher algorithm that replaces the old
707 DES algorithm.  Has 128 bits block size and is used in CBC mode. This
708 is not officially supported in TLS.
709 @end table
711 Supported MAC algorithms:
713 @table @code
714 @item MAC_MD5
715 MD5 is a cryptographic hash algorithm designed by Ron Rivest. Outputs
716 128 bits of data.
718 @item MAC_SHA
719 SHA is a cryptographic hash algorithm designed by NSA. Outputs 160
720 bits of data.
722 @end table
724 @node Compression algorithms used in the record layer
725 @subsection Compression Algorithms Used in the Record Layer
726 @cindex Compression algorithms
728 The TLS record layer also supports compression.  The algorithms
729 implemented in @acronym{GnuTLS} can be found in the table below.
730 All the algorithms except for DEFLATE which is
731 referenced in @xcite{RFC3749}, should be considered as
732 @acronym{GnuTLS}' extensions@footnote{You should use
733 @ref{gnutls_handshake_set_private_extensions} to enable private
734 extensions.}, and should be advertised only when the peer is known to
735 have a compliant client, to avoid interoperability problems.
737 The included algorithms perform really good when text, or other
738 compressible data are to be transfered, but offer nothing on already
739 compressed data, such as compressed images, zipped archives etc.
740 These compression algorithms, may be useful in high bandwidth TLS
741 tunnels, and in cases where network usage has to be minimized. As a
742 drawback, compression increases latency.
744 The record layer compression in @acronym{GnuTLS} is implemented based
745 on the proposal @xcite{RFC3749}.
746 The supported compression algorithms are:
748 @table @code
749 @item DEFLATE
750 Zlib compression, using the deflate algorithm.
752 @item LZO
753 LZO is a very fast compression algorithm.  This algorithm is only
754 available if the @acronym{GnuTLS-extra} library has been initialized
755 and the private extensions are enabled, and if GnuTLS was built with
756 LZO support.
758 @end table
760 @node Weaknesses and countermeasures
761 @subsection Weaknesses and Countermeasures
763 Some weaknesses that may affect the security of the Record layer have
764 been found in @acronym{TLS} 1.0 protocol. These weaknesses can be
765 exploited by active attackers, and exploit the facts that
767 @enumerate
769 @item
770 @acronym{TLS} has separate alerts for ``decryption_failed'' and
771 ``bad_record_mac''
773 @item
774 The decryption failure reason can be detected by timing the response
775 time.
777 @item
778 The IV for CBC encrypted packets is the last block of the previous
779 encrypted packet.
781 @end enumerate
783 Those weaknesses were solved in @acronym{TLS} 1.1 @xcite{RFC4346}
784 which is implemented in @acronym{GnuTLS}. For a detailed discussion
785 see the archives of the TLS Working Group mailing list and the paper
786 @xcite{CBCATT}.
788 @node The TLS Alert Protocol
789 @section The TLS Alert Protocol
790 @anchor{The Alert Protocol}
791 @cindex Alert protocol
793 The Alert protocol is there to allow signals to be sent between peers.
794 These signals are mostly used to inform the peer about the cause of a
795 protocol failure. Some of these signals are used internally by the
796 protocol and the application protocol does not have to cope with them
797 (see @code{GNUTLS_A_CLOSE_NOTIFY}), and others refer to the
798 application protocol solely (see @code{GNUTLS_A_USER_CANCELLED}).  An
799 alert signal includes a level indication which may be either fatal or
800 warning. Fatal alerts always terminate the current connection, and
801 prevent future renegotiations using the current session ID.
803 The alert messages are protected by the record protocol, thus the
804 information that is included does not leak. You must take extreme care
805 for the alert information not to leak to a possible attacker, via
806 public log files etc.
808 @table @asis
809 @item @ref{gnutls_alert_send}:
810 To send an alert signal.
812 @item @ref{gnutls_error_to_alert}:
813 To map a gnutls error number to an alert signal.
815 @item @ref{gnutls_alert_get}:
816 Returns the last received alert.
818 @item @ref{gnutls_alert_get_name}:
819 Returns the name, in a character array, of the given alert.
821 @end table
823 @node The TLS Handshake Protocol
824 @section The TLS Handshake Protocol
825 @anchor{The Handshake Protocol}
826 @cindex Handshake protocol
828 The Handshake protocol is responsible for the ciphersuite negotiation,
829 the initial key exchange, and the authentication of the two peers.
830 This is fully controlled by the application layer, thus your program
831 has to set up the required parameters. Available functions to control
832 the handshake protocol include:
834 @table @asis
835 @item @ref{gnutls_priority_init}:
836 To initialize a priority set of ciphers.
838 @item @ref{gnutls_priority_deinit}:
839 To deinitialize a priority set of ciphers.
841 @item @ref{gnutls_priority_set}:
842 To associate a priority set with a @acronym{TLS} session.
844 @item @ref{gnutls_priority_set_direct}:
845 To directly associate a session with a given priority string.
847 @item @ref{gnutls_credentials_set}:
848 To set the appropriate credentials structures.
850 @item @ref{gnutls_certificate_server_set_request}:
851 To set whether client certificate is required or not.
853 @item @ref{gnutls_handshake}:
854 To initiate the handshake.
855 @end table
857 @subsection TLS Cipher Suites
859 The Handshake Protocol of @acronym{TLS} negotiates cipher suites of
860 the form @code{TLS_DHE_RSA_WITH_3DES_CBC_SHA}.  The usual cipher
861 suites contain these parameters:
863 @itemize
865 @item The key exchange algorithm.
866 @code{DHE_RSA} in the example.
868 @item The Symmetric encryption algorithm and mode
869 @code{3DES_CBC} in this example.
871 @item The MAC@footnote{MAC stands for Message Authentication Code. It can be described as a keyed hash algorithm. See RFC2104.} algorithm used for authentication.
872 @code{MAC_SHA} is used in the above example.
874 @end itemize
876 The cipher suite negotiated in the handshake protocol will affect the
877 Record Protocol, by enabling encryption and data authentication.  Note
878 that you should not over rely on @acronym{TLS} to negotiate the
879 strongest available cipher suite. Do not enable ciphers and algorithms
880 that you consider weak.
882 The priority functions, dicussed above, allow the application layer to
883 enable and set priorities on the individual ciphers. It may imply that
884 all combinations of ciphersuites are allowed, but this is not
885 true. For several reasons, not discussed here, some combinations were
886 not defined in the @acronym{TLS} protocol.  The supported ciphersuites
887 are shown in @ref{ciphersuites}.
889 @subsection Client Authentication
890 @cindex Client Certificate authentication
892 In the case of ciphersuites that use certificate authentication, the
893 authentication of the client is optional in @acronym{TLS}.  A server
894 may request a certificate from the client --- using the
895 @ref{gnutls_certificate_server_set_request} function. If a certificate
896 is to be requested from the client during the handshake, the server
897 will send a certificate request message that contains a list of
898 acceptable certificate signers. In @acronym{GnuTLS} the certificate
899 signers list is constructed using the trusted Certificate Authorities
900 by the server. That is the ones set using
901 @itemize
902 @item @ref{gnutls_certificate_set_x509_trust_file}
903 @item @ref{gnutls_certificate_set_x509_trust_mem}
904 @end itemize
906 Sending of the names of the CAs can be controlled using
907 @ref{gnutls_certificate_send_x509_rdn_sequence}. The client, then, may
908 send a certificate, signed by one of the server's acceptable signers.
910 @subsection Resuming Sessions
911 @anchor{resume}
912 @cindex Resuming sessions
914 The @ref{gnutls_handshake} function, is expensive since a lot of
915 calculations are performed. In order to support many fast connections
916 to the same server a client may use session resuming. @strong{Session
917 resuming} is a feature of the @acronym{TLS} protocol which allows a
918 client to connect to a server, after a successful handshake, without
919 the expensive calculations.  This is achieved by using the previously
920 established keys. @acronym{GnuTLS} supports this feature, and the
921 example (@pxref{ex:resume-client}) illustrates a typical use of it.
923 Keep in mind that sessions are expired after some time, for security
924 reasons, thus it may be normal for a server not to resume a session
925 even if you requested that.  Also note that you must enable, using the
926 priority functions, at least the algorithms used in the last session.
928 @subsection Resuming Internals
930 The resuming capability, mostly in the server side, is one of the
931 problems of a thread-safe TLS implementations. The problem is that all
932 threads must share information in order to be able to resume
933 sessions. The gnutls approach is, in case of a client, to leave all
934 the burden of resuming to the client. I.e., copy and keep the
935 necessary parameters. See the functions:
937 @itemize
939 @item @ref{gnutls_session_get_data}
941 @item @ref{gnutls_session_get_id}
943 @item @ref{gnutls_session_set_data}
945 @end itemize
947 The server side is different. A server has to specify some callback
948 functions which store, retrieve and delete session data. These can be
949 registered with:
951 @itemize
953 @item @ref{gnutls_db_set_remove_function}
955 @item @ref{gnutls_db_set_store_function}
957 @item @ref{gnutls_db_set_retrieve_function}
959 @item @ref{gnutls_db_set_ptr}
961 @end itemize
963 It might also be useful to be able to check for expired sessions in
964 order to remove them, and save space. The function
965 @ref{gnutls_db_check_entry} is provided for that reason.
967 @node TLS Extensions
968 @section TLS Extensions
969 @cindex TLS Extensions
971 A number of extensions to the @acronym{TLS} protocol have been
972 proposed mainly in @xcite{TLSEXT}. The extensions supported
973 in @acronym{GnuTLS} are:
975 @itemize
976 @item Maximum fragment length negotiation
977 @item Server name indication
978 @item Session tickets
979 @end itemize
981 and they will be discussed in the subsections that follow.
983 @subsection Maximum Fragment Length Negotiation
984 @cindex TLS Extensions
985 @cindex Maximum fragment length
987 This extension allows a @acronym{TLS} implementation to negotiate a
988 smaller value for record packet maximum length. This extension may be
989 useful to clients with constrained capabilities. See the
990 @ref{gnutls_record_set_max_size} and the
991 @ref{gnutls_record_get_max_size} functions.
993 @subsection Server Name Indication
994 @anchor{serverind}
995 @cindex TLS Extensions
996 @cindex Server name indication
998 A common problem in @acronym{HTTPS} servers is the fact that the
999 @acronym{TLS} protocol is not aware of the hostname that a client
1000 connects to, when the handshake procedure begins. For that reason the
1001 @acronym{TLS} server has no way to know which certificate to send.
1003 This extension solves that problem within the @acronym{TLS} protocol,
1004 and allows a client to send the HTTP hostname before the handshake
1005 begins within the first handshake packet.  The functions
1006 @ref{gnutls_server_name_set} and @ref{gnutls_server_name_get} can be
1007 used to enable this extension, or to retrieve the name sent by a
1008 client.
1010 @subsection Session Tickets
1011 @cindex TLS Extensions
1012 @cindex Session Tickets
1013 @cindex Ticket
1015 To resume a TLS session the server normally store some state.  This
1016 complicates deployment, and typical situations the client can cache
1017 information and send it to the server instead.  The Session Ticket
1018 extension implements this idea, and it is documented in
1019 RFC 5077 @xcite{TLSTKT}.
1021 Clients can enable support for TLS tickets with
1022 @ref{gnutls_session_ticket_enable_client} and servers use
1023 @ref{gnutls_session_ticket_key_generate} to generate a key and
1024 @ref{gnutls_session_ticket_enable_server} to enable the extension.
1025 Clients resume sessions using the ticket using the normal session
1026 resume functions, @ref{resume}.
1028 @node Selecting cryptographic key sizes
1029 @section Selecting Cryptographic Key Sizes
1030 @cindex key sizes
1032 In TLS, since a lot of algorithms are involved, it is not easy to set
1033 a consistent security level.  For this reason this section will
1034 present some correspondance between key sizes of symmetric algorithms
1035 and public key algorithms based on the most conservative values of
1036 @xcite{SELKEY}.  Those can be used to generate certificates with
1037 appropriate key sizes as well as parameters for Diffie-Hellman and SRP
1038 authentication.
1040 @multitable @columnfractions .15 .20 .20 .20
1042 @item Year
1043 @tab Symmetric key size
1044 @tab RSA key size, DH and SRP prime size
1045 @tab ECC key size
1047 @item 1982
1048 @tab 56
1049 @tab 417
1050 @tab 105
1052 @item 1988
1053 @tab 61
1054 @tab 566
1055 @tab 114
1057 @item 2002
1058 @tab 72
1059 @tab 1028
1060 @tab 139
1062 @item 2015
1063 @tab 82
1064 @tab 1613
1065 @tab 173
1067 @item 2028
1068 @tab 92
1069 @tab 2362
1070 @tab 210
1072 @item 2040
1073 @tab 101
1074 @tab 3214
1075 @tab 244
1077 @item 2050
1078 @tab 109
1079 @tab 4047
1080 @tab 272
1082 @end multitable
1084 The first column provides an estimation of the year until these
1085 parameters are considered safe and the rest of the columns list the
1086 parameters for the various algorithms.
1088 Note however that the values suggested here are nothing more than an
1089 educated guess that is valid today. There are no guarrantees that an
1090 algorithm will remain unbreakable or that these values will remain
1091 constant in time. There could be scientific breakthroughs that cannot
1092 be predicted or total failure of the current public key systems by
1093 quantum computers. On the other hand though the cryptosystems used in
1094 TLS are selected in a conservative way and such catastrophic
1095 breakthroughs or failures are believed to be unlikely.
1097 NIST publication SP 800-57 @xcite{NISTSP80057} contains a similar
1098 table that extends beyond the key sizes given above.
1100 @multitable @columnfractions .15 .20 .20 .20
1102 @item Bits of security
1103 @tab Symmetric key algorithms
1104 @tab RSA key size, DSA, DH and SRP prime size
1105 @tab ECC key size
1107 @item 80
1108 @tab 2TDEA
1109 @tab 1024
1110 @tab 160-223
1112 @item 112
1113 @tab 3DES
1114 @tab 2048
1115 @tab 224-255
1117 @item 128
1118 @tab AES-128
1119 @tab 3072
1120 @tab 256-383
1122 @item 192
1123 @tab AES-192
1124 @tab 7680
1125 @tab 384-511
1127 @item 256
1128 @tab AES-256
1129 @tab 15360
1130 @tab 512+
1132 @end multitable
1134 The recommendations are fairly consistent.
1136 @node On SSL 2 and older protocols
1137 @section On SSL 2 and Older Protocols
1138 @cindex SSL 2
1140 One of the initial decisions in the @acronym{GnuTLS} development was
1141 to implement the known security protocols for the transport layer.
1142 Initially @acronym{TLS} 1.0 was implemented since it was the latest at
1143 that time, and was considered to be the most advanced in security
1144 properties.  Later the @acronym{SSL} 3.0 protocol was implemented
1145 since it is still the only protocol supported by several servers and
1146 there are no serious security vulnerabilities known.
1148 One question that may arise is why we didn't implement @acronym{SSL}
1149 2.0 in the library.  There are several reasons, most important being
1150 that it has serious security flaws, unacceptable for a modern security
1151 library.  Other than that, this protocol is barely used by anyone
1152 these days since it has been deprecated since 1996.  The security
1153 problems in @acronym{SSL} 2.0 include:
1155 @itemize
1157 @item Message integrity compromised.
1158 The @acronym{SSLv2} message authentication uses the MD5 function, and
1159 is insecure.
1161 @item Man-in-the-middle attack.
1162 There is no protection of the handshake in @acronym{SSLv2}, which
1163 permits a man-in-the-middle attack.
1165 @item Truncation attack.
1166 @acronym{SSLv2} relies on TCP FIN to close the session, so the
1167 attacker can forge a TCP FIN, and the peer cannot tell if it was a
1168 legitimate end of data or not.
1170 @item Weak message integrity for export ciphers.
1171 The cryptographic keys in @acronym{SSLv2} are used for both message
1172 authentication and encryption, so if weak encryption schemes are
1173 negotiated (say 40-bit keys) the message authentication code use the
1174 same weak key, which isn't necessary.
1176 @end itemize
1178 @cindex PCT
1179 Other protocols such as Microsoft's @acronym{PCT} 1 and @acronym{PCT}
1180 2 were not implemented because they were also abandoned and deprecated
1181 by @acronym{SSL} 3.0 and later @acronym{TLS} 1.0.
1183 @node On Record Padding
1184 @section On Record Padding
1185 @cindex Record padding
1186 @cindex Bad record MAC
1188 The TLS protocol allows for random padding of records, to make it more
1189 difficult to perform analysis on the length of exchanged messages.
1190 (In RFC 4346 this is specified in section 6.2.3.2.)  GnuTLS appears to
1191 be one of few implementation that take advantage of this text, and pad
1192 records by a random length.
1194 The TLS implementation in the Symbian operating system, frequently
1195 used by Nokia and Sony-Ericsson mobile phones, cannot handle
1196 non-minimal record padding.  What happens when one of these clients
1197 handshake with a GnuTLS server is that the client will fail to compute
1198 the correct MAC for the record.  The client sends a TLS alert
1199 (@code{bad_record_mac}) and disconnects.  Typically this will result
1200 in error messages such as 'A TLS fatal alert has been received', 'Bad
1201 record MAC', or both, on the GnuTLS server side.
1203 GnuTLS implements a work around for this problem.  However, it has to
1204 be enabled specifically.  It can be enabled by using
1205 @ref{gnutls_record_disable_padding}, or @ref{gnutls_priority_set} with
1206 the @code{%COMPAT} priority string.
1208 If you implement an application that have a configuration file, we
1209 recommend that you make it possible for users or administrators to
1210 specify a GnuTLS protocol priority string, which is used by your
1211 application via @ref{gnutls_priority_set}.  To allow the best
1212 flexibility, make it possible to have a different priority string for
1213 different incoming IP addresses.
1215 To enable the workaround in the @code{gnutls-cli} client or the
1216 @code{gnutls-serv} server, for testing of other implementations, use
1217 the following parameter: @code{--priority "%COMPAT"}.
1219 This problem has been discussed on mailing lists and in bug reports.
1220 This section tries to collect all pieces of information that we know
1221 about the problem.  If you wish to go back to the old discussions,
1222 here are some links:
1224 @url{http://bugs.debian.org/390712}
1226 @url{http://bugs.debian.org/402861}
1228 @url{http://bugs.debian.org/438137}
1230 @url{http://thread.gmane.org/gmane.ietf.tls/3079}
1232 @node Authentication methods
1233 @chapter Authentication Methods
1235 The @acronym{TLS} protocol provides confidentiality and encryption,
1236 but also offers authentication, which is a prerequisite for a secure
1237 connection. The available authentication methods in @acronym{GnuTLS}
1238 are:
1240 @itemize
1242 @item Certificate authentication
1244 @item Anonymous authentication
1246 @item @acronym{SRP} authentication
1248 @item @acronym{PSK} authentication
1250 @end itemize
1252 @menu
1253 * Certificate authentication::
1254 * Anonymous authentication::
1255 * Authentication using SRP::
1256 * Authentication using PSK::
1257 * Authentication and credentials::
1258 * Parameters stored in credentials::
1259 @end menu
1261 @node Certificate authentication
1262 @section Certificate Authentication
1264 @subsection Authentication Using @acronym{X.509} Certificates
1265 @cindex @acronym{X.509} certificates
1267 @acronym{X.509} certificates contain the public parameters, of a
1268 public key algorithm, and an authority's signature, which proves the
1269 authenticity of the parameters.  @xref{The X.509 trust model}, for
1270 more information on @acronym{X.509} protocols.
1272 @subsection Authentication Using @acronym{OpenPGP} Keys
1273 @cindex @acronym{OpenPGP} Keys
1275 @acronym{OpenPGP} keys also contain public parameters of a public key
1276 algorithm, and signatures from several other parties. Depending on
1277 whether a signer is trusted the key is considered trusted or not.
1278 @acronym{GnuTLS}'s @acronym{OpenPGP} authentication implementation is
1279 based on the @xcite{TLSPGP} proposal.
1281 @xref{The OpenPGP trust model}, for more information about the
1282 @acronym{OpenPGP} trust model.  For a more detailed introduction to
1283 @acronym{OpenPGP} and @acronym{GnuPG} see @xcite{GPGH}.
1285 @subsection Using Certificate Authentication
1287 In @acronym{GnuTLS} both the @acronym{OpenPGP} and @acronym{X.509}
1288 certificates are part of the certificate authentication and thus are
1289 handled using a common API.
1291 When using certificates the server is required to have at least one
1292 certificate and private key pair. A client may or may not have such a
1293 pair. The certificate and key pair should be loaded, before any
1294 @acronym{TLS} session is initialized, in a certificate credentials
1295 structure. This should be done by using
1296 @ref{gnutls_certificate_set_x509_key_file} or
1297 @ref{gnutls_certificate_set_openpgp_key_file} depending on the
1298 certificate type.  In the @acronym{X.509} case, the functions will
1299 also accept and use a certificate list that leads to a trusted
1300 authority. The certificate list must be ordered in such way that every
1301 certificate certifies the one before it. The trusted authority's
1302 certificate need not to be included, since the peer should possess it
1303 already.
1305 As an alternative, a callback may be used so the server or the client
1306 specify the certificate and the key at the handshake time.  That
1307 callback can be set using the functions:
1309 @itemize
1311 @item @ref{gnutls_certificate_server_set_retrieve_function}
1313 @item @ref{gnutls_certificate_client_set_retrieve_function}
1315 @end itemize
1317 Clients and servers that will select certificates using callback
1318 functions should select a certificate according the peer's signature
1319 algorithm preferences. To get those preferences use
1320 @ref{gnutls_sign_algorithm_get_requested}.
1322 Certificate verification is possible by loading the trusted
1323 authorities into the credentials structure by using
1324 @ref{gnutls_certificate_set_x509_trust_file} or
1325 @ref{gnutls_certificate_set_openpgp_keyring_file} for openpgp
1326 keys. Note however that the peer's certificate is not automatically
1327 verified, you should call @ref{gnutls_certificate_verify_peers2},
1328 after a successful handshake, to verify the signatures of the
1329 certificate.  An alternative way, which reports a more detailed
1330 verification output, is to use @ref{gnutls_certificate_get_peers} to
1331 obtain the raw certificate of the peer and verify it using the
1332 functions discussed in @ref{The X.509 trust model}.
1334 In a handshake, the negotiated cipher suite depends on the
1335 certificate's parameters, so not all key exchange methods will be
1336 available with some certificates. @acronym{GnuTLS} will disable
1337 ciphersuites that are not compatible with the key, or the enabled
1338 authentication methods.  For example keys marked as sign-only, will
1339 not be able to access the plain RSA ciphersuites, but only the
1340 @code{DHE_RSA} ones. It is recommended not to use RSA keys for both
1341 signing and encryption. If possible use the same key for the
1342 @code{DHE_RSA} and @code{RSA_EXPORT} ciphersuites, which use signing,
1343 and a different key for the plain RSA ciphersuites, which use
1344 encryption.  All the key exchange methods shown below are available in
1345 certificate authentication.
1347 Note that the DHE key exchange methods are generally
1348 slower@footnote{It really depends on the group used.  Primes with
1349 lesser bits are always faster, but also easier to break.  Values less
1350 than 768 should not be used today} than plain RSA and require Diffie
1351 Hellman parameters to be generated and associated with a credentials
1352 structure, by the server.  The @code{RSA-EXPORT} method also requires
1353 512 bit RSA parameters, that should also be generated and associated
1354 with the credentials structure.  See the functions:
1356 @itemize
1358 @item @ref{gnutls_dh_params_generate2}
1360 @item @ref{gnutls_certificate_set_dh_params}
1362 @item @ref{gnutls_rsa_params_generate2}
1364 @item @ref{gnutls_certificate_set_rsa_export_params}
1366 @end itemize
1368 Sometimes in order to avoid bottlenecks in programs it is usefull to
1369 store and read parameters from formats that can be generated by
1370 external programs such as @code{certtool}. This is possible with
1371 @acronym{GnuTLS} by using the following functions:
1373 @itemize
1375 @item @ref{gnutls_dh_params_import_pkcs3}
1377 @item @ref{gnutls_rsa_params_import_pkcs1}
1379 @item @ref{gnutls_dh_params_export_pkcs3}
1381 @item @ref{gnutls_rsa_params_export_pkcs1}
1383 @end itemize
1385 Key exchange algorithms for @acronym{OpenPGP} and @acronym{X.509}
1386 certificates:
1388 @table @code
1390 @item RSA:
1391 The RSA algorithm is used to encrypt a key and send it to the peer.
1392 The certificate must allow the key to be used for encryption.
1394 @item RSA_EXPORT:
1395 The RSA algorithm is used to encrypt a key and send it to the peer.
1396 In the EXPORT algorithm, the server signs temporary RSA parameters of
1397 512 bits --- which are considered weak --- and sends them to the
1398 client.
1400 @item DHE_RSA:
1401 The RSA algorithm is used to sign Ephemeral Diffie-Hellman parameters
1402 which are sent to the peer. The key in the certificate must allow the
1403 key to be used for signing. Note that key exchange algorithms which
1404 use Ephemeral Diffie-Hellman parameters, offer perfect forward
1405 secrecy. That means that even if the private key used for signing is
1406 compromised, it cannot be used to reveal past session data.
1408 @item DHE_DSS:
1409 The DSS algorithm is used to sign Ephemeral Diffie-Hellman parameters
1410 which are sent to the peer. The certificate must contain DSA
1411 parameters to use this key exchange algorithm. DSS stands for Digital
1412 Signature Standard.
1414 @end table
1416 @node Anonymous authentication
1417 @section Anonymous Authentication
1418 @cindex Anonymous authentication
1420 The anonymous key exchange performs encryption but there is no
1421 indication of the identity of the peer.  This kind of authentication
1422 is vulnerable to a man in the middle attack, but this protocol can be
1423 used even if there is no prior communication and trusted parties with
1424 the peer, or when full anonymity is required.  Unless really required,
1425 do not use anonymous authentication.  Available key exchange methods
1426 are shown below.
1428 Note that the key exchange methods for anonymous authentication
1429 require Diffie-Hellman parameters to be generated by the server and
1430 associated with an anonymous credentials structure.
1432 Supported anonymous key exchange algorithms:
1434 @table @code
1436 @item ANON_DH:
1437 This algorithm exchanges Diffie-Hellman parameters.
1439 @end table
1441 @node Authentication using SRP
1442 @section Authentication using @acronym{SRP}
1443 @cindex @acronym{SRP} authentication
1445 Authentication via the Secure Remote Password protocol,
1446 @acronym{SRP}@footnote{@acronym{SRP} is described in @xcite{RFC2945}},
1447 is supported.  The @acronym{SRP} key exchange is an extension to the
1448 @acronym{TLS} protocol, and it is a password based authentication
1449 (unlike @acronym{X.509} or @acronym{OpenPGP} that use certificates).
1450 The two peers can be identified using a single password, or there can
1451 be combinations where the client is authenticated using @acronym{SRP}
1452 and the server using a certificate.
1454 The advantage of @acronym{SRP} authentication, over other proposed
1455 secure password authentication schemes, is that @acronym{SRP} does not
1456 require the server to hold the user's password.  This kind of
1457 protection is similar to the one used traditionally in the @emph{UNIX}
1458 @file{/etc/passwd} file, where the contents of this file did not cause
1459 harm to the system security if they were revealed.  The @acronym{SRP}
1460 needs instead of the plain password something called a verifier, which
1461 is calculated using the user's password, and if stolen cannot be used
1462 to impersonate the user. Check @xcite{TOMSRP} for a detailed
1463 description of the @acronym{SRP} protocol and the Stanford
1464 @acronym{SRP} libraries, which includes a PAM module that synchronizes
1465 the system's users passwords with the @acronym{SRP} password
1466 files. That way @acronym{SRP} authentication could be used for all the
1467 system's users.
1469 The implementation in @acronym{GnuTLS} is based on paper
1470 @xcite{TLSSRP}.  The supported @acronym{SRP} key exchange methods are:
1472 @table @code
1474 @item SRP:
1475 Authentication using the @acronym{SRP} protocol.
1477 @item SRP_DSS:
1478 Client authentication using the @acronym{SRP} protocol. Server is
1479 authenticated using a certificate with DSA parameters.
1481 @item SRP_RSA:
1482 Client authentication using the @acronym{SRP} protocol. Server is
1483 authenticated using a certificate with RSA parameters.
1485 @end table
1487 If clients supporting @acronym{SRP} know the username and password
1488 before the connection, should initialize the client credentials and
1489 call the function @ref{gnutls_srp_set_client_credentials}.
1490 Alternatively they could specify a callback function by using the
1491 function @ref{gnutls_srp_set_client_credentials_function}.  This has
1492 the advantage that allows probing the server for @acronym{SRP}
1493 support.  In that case the callback function will be called twice per
1494 handshake.  The first time is before the ciphersuite is negotiated,
1495 and if the callback returns a negative error code, the callback will
1496 be called again if @acronym{SRP} has been negotiated.  This uses a
1497 special @acronym{TLS}-@acronym{SRP} handshake idiom in order to avoid,
1498 in interactive applications, to ask the user for @acronym{SRP}
1499 password and username if the server does not negotiate an
1500 @acronym{SRP} ciphersuite.
1502 In server side the default behaviour of @acronym{GnuTLS} is to read
1503 the usernames and @acronym{SRP} verifiers from password files. These
1504 password files are the ones used by the @emph{Stanford srp libraries}
1505 and can be specified using the
1506 @ref{gnutls_srp_set_server_credentials_file}.  If a different
1507 password file format is to be used, then the function
1508 @ref{gnutls_srp_set_server_credentials_function}, should be called,
1509 in order to set an appropriate callback.
1511 Some helper functions such as
1513 @itemize
1515 @item @ref{gnutls_srp_verifier}
1517 @item @ref{gnutls_srp_base64_encode}
1519 @item @ref{gnutls_srp_base64_decode}
1521 @end itemize
1523 are included in @acronym{GnuTLS}, and can be used to generate and
1524 maintain @acronym{SRP} verifiers and password files.  A program to
1525 manipulate the required parameters for @acronym{SRP} authentication is
1526 also included.  @xref{srptool}, for more information.
1529 @node Authentication using PSK
1530 @section Authentication using @acronym{PSK}
1531 @cindex @acronym{PSK} authentication
1533 Authentication using Pre-shared keys is a method to authenticate using
1534 usernames and binary keys. This protocol avoids making use of public
1535 key infrastructure and expensive calculations, thus it is suitable for
1536 constraint clients.
1538 The implementation in @acronym{GnuTLS} is based on paper
1539 @xcite{TLSPSK}.  The supported @acronym{PSK} key exchange methods are:
1541 @table @code
1543 @item PSK:
1544 Authentication using the @acronym{PSK} protocol.
1546 @item DHE-PSK:
1547 Authentication using the @acronym{PSK} protocol and Diffie-Hellman key
1548 exchange.  This method offers perfect forward secrecy.
1550 @end table
1552 Clients supporting @acronym{PSK} should supply the username and key
1553 before the connection to the client credentials by calling the
1554 function @ref{gnutls_psk_set_client_credentials}.  Alternatively they
1555 could specify a callback function by using the function
1556 @ref{gnutls_psk_set_client_credentials_function}.  This has the
1557 advantage that the callback will be called only if @acronym{PSK} has
1558 been negotiated.
1560 In server side the default behaviour of @acronym{GnuTLS} is to read
1561 the usernames and @acronym{PSK} keys from a password file. The
1562 password file should contain usernames and keys in hexadecimal
1563 format. The name of the password file can be stored to the credentials
1564 structure by calling @ref{gnutls_psk_set_server_credentials_file}.  If
1565 a different password file format is to be used, then the function
1566 @ref{gnutls_psk_set_server_credentials_function}, should be used
1567 instead.
1569 The server can help the client chose a suitable username and password,
1570 by sending a hint.  In the server, specify the hint by calling
1571 @ref{gnutls_psk_set_server_credentials_hint}.  The client can retrieve
1572 the hint, for example in the callback function, using
1573 @ref{gnutls_psk_client_get_hint}.
1575 There is no standard mechanism to derive a PSK key from a password
1576 specified by the TLS PSK document.  However, GnuTLS provides
1577 @ref{gnutls_psk_netconf_derive_key} which follows the algorithm
1578 specified in @file{draft-ietf-netconf-tls-02.txt}.
1580 Some helper functions such as:
1582 @itemize
1584 @item @ref{gnutls_hex_encode}
1586 @item @ref{gnutls_hex_decode}
1588 @end itemize
1590 are included in @acronym{GnuTLS}, and may be used to generate and
1591 maintain @acronym{PSK} keys.
1594 @node Authentication and credentials
1595 @section Authentication and Credentials
1597 In @acronym{GnuTLS} every key exchange method is associated with a
1598 credentials type. So in order to enable to enable a specific method,
1599 the corresponding credentials type should be initialized and set using
1600 @ref{gnutls_credentials_set}.  A mapping is shown below.
1602 Key exchange algorithms and the corresponding credential types:
1604 @multitable @columnfractions .3 .3 .3
1606 @headitem Key exchange @tab Client credentials @tab Server credentials
1608 @item @code{KX_RSA}
1609 @item @code{KX_DHE_RSA}
1610 @item @code{KX_DHE_DSS}
1611 @item @code{KX_RSA_EXPORT}
1612 @tab @code{CRD_CERTIFICATE}
1613 @tab @code{CRD_CERTIFICATE}
1615 @item @code{KX_SRP_RSA}
1616 @tab @code{CRD_SRP}
1617 @tab @code{CRD_SRP}
1618 @item @code{KX_SRP_DSS}
1619 @tab
1620 @tab @code{CRD_CERTIFICATE}
1622 @item @code{KX_SRP}
1623 @tab @code{CRD_SRP}
1624 @tab @code{CRD_SRP}
1626 @item @code{KX_ANON_DH}
1627 @tab @code{CRD_ANON}
1628 @tab @code{CRD_ANON}
1630 @item @code{KX_PSK}
1631 @tab @code{CRD_PSK}
1632 @tab @code{CRD_PSK}
1634 @end multitable
1636 @node Parameters stored in credentials
1637 @section Parameters Stored in Credentials
1639 Several parameters such as the ones used for Diffie-Hellman
1640 authentication are stored within the credentials structures, so all
1641 sessions can access them. Those parameters are stored in structures
1642 such as @code{gnutls_dh_params_t} and @code{gnutls_rsa_params_t}, and
1643 functions like @ref{gnutls_certificate_set_dh_params} and
1644 @ref{gnutls_certificate_set_rsa_export_params} can be used to
1645 associate those parameters with the given credentials structure.
1647 Since those parameters need to be renewed from time to time and a
1648 global structure such as the credentials, may not be easy to modify
1649 since it is accessible by all sessions, an alternative interface is
1650 available using a callback function.  This can be set using the
1651 @ref{gnutls_certificate_set_params_function}.  An example is shown
1652 below.
1654 @example
1655 #include <gnutls.h>
1657 gnutls_rsa_params_t rsa_params;
1658 gnutls_dh_params_t dh_params;
1660 /* This function will be called once a session requests DH
1661  * or RSA parameters. The parameters returned (if any) will
1662  * be used for the first handshake only.
1663  */
1664 static int get_params( gnutls_session_t session,
1665         gnutls_params_type_t type,
1666         gnutls_params_st *st)
1668    if (type == GNUTLS_PARAMS_RSA_EXPORT)
1669       st->params.rsa_export = rsa_params;
1670    else if (type == GNUTLS_PARAMS_DH)
1671       st->params.dh = dh_params;
1672    else return -1;
1674    st->type = type;
1675    /* do not deinitialize those parameters.
1676     */
1677    st->deinit = 0;
1679    return 0;
1682 int main()
1684    gnutls_certificate_credentials_t cert_cred;
1686    initialize_params();
1688    /* ...
1689     */
1691    gnutls_certificate_set_params_function( cert_cred, get_params);
1693 @end example
1695 @node More on certificate authentication
1696 @chapter More on Certificate Authentication
1697 @anchor{Certificate Authentication}
1698 @cindex Certificate authentication
1700 @menu
1701 * The X.509 trust model::
1702 * The OpenPGP trust model::
1703 * Digital signatures::
1704 @end menu
1706 @node The X.509 trust model
1707 @section The @acronym{X.509} Trust Model
1708 @cindex @acronym{X.509} certificates
1710 The @acronym{X.509} protocols rely on a hierarchical trust model. In
1711 this trust model Certification Authorities (CAs) are used to certify
1712 entities.  Usually more than one certification authorities exist, and
1713 certification authorities may certify other authorities to issue
1714 certificates as well, following a hierarchical model.
1716 @image{gnutls-x509,7cm,9.5cm}
1718 One needs to trust one or more CAs for his secure communications. In
1719 that case only the certificates issued by the trusted authorities are
1720 acceptable.  See the figure above for a typical example.  The API for
1721 handling @acronym{X.509} certificates is described at section
1722 @ref{sec:x509api}.  Some examples are listed below.
1724 @menu
1725 * X.509 certificates::
1726 * Verifying X.509 certificate paths::
1727 * PKCS #10 certificate requests::
1728 * PKCS #12 structures::
1729 @end menu
1731 @node X.509 certificates
1732 @subsection @acronym{X.509} Certificates
1734 An @acronym{X.509} certificate usually contains information about the
1735 certificate holder, the signer, a unique serial number, expiration
1736 dates and some other fields @xcite{PKIX} as shown in the table below.
1738 @table @code
1740 @item version:
1741 The field that indicates the version of the certificate.
1743 @item serialNumber:
1744 This field holds a unique serial number per certificate.
1746 @item issuer:
1747 Holds the issuer's distinguished name.
1749 @item validity:
1750 The activation and expiration dates.
1752 @item subject:
1753 The subject's distinguished name of the certificate.
1755 @item extensions:
1756 The extensions are fields only present in version 3 certificates.
1758 @end table
1760 The certificate's @emph{subject or issuer name} is not just a single
1761 string.  It is a Distinguished name and in the @acronym{ASN.1}
1762 notation is a sequence of several object IDs with their corresponding
1763 values. Some of available OIDs to be used in an @acronym{X.509}
1764 distinguished name are defined in @file{gnutls/x509.h}.
1766 The @emph{Version} field in a certificate has values either 1 or 3 for
1767 version 3 certificates.  Version 1 certificates do not support the
1768 extensions field so it is not possible to distinguish a CA from a
1769 person, thus their usage should be avoided.
1771 The @emph{validity} dates are there to indicate the date that the
1772 specific certificate was activated and the date the certificate's key
1773 would be considered invalid.
1775 Certificate @emph{extensions} are there to include information about
1776 the certificate's subject that did not fit in the typical certificate
1777 fields. Those may be e-mail addresses, flags that indicate whether the
1778 belongs to a CA etc.  All the supported @acronym{X.509} version 3
1779 extensions are shown in the table below.
1781 @table @code
1783 @item subject key id (2.5.29.14):
1784 An identifier of the key of the subject.
1786 @item authority key id (2.5.29.35):
1787 An identifier of the authority's key used to sign the certificate.
1789 @item subject alternative name (2.5.29.17):
1790 Alternative names to subject's distinguished name.
1792 @item key usage (2.5.29.15):
1793 Constraints the key's usage of the certificate.
1795 @item extended key usage (2.5.29.37):
1796 Constraints the purpose of the certificate.
1798 @item basic constraints (2.5.29.19):
1799 Indicates whether this is a CA certificate or not, and specify the
1800 maximum path lengths of certificate chains.
1802 @item CRL distribution points (2.5.29.31):
1803 This extension is set by the CA, in order to inform about the issued
1804 CRLs.
1806 @item Proxy Certification Information (1.3.6.1.5.5.7.1.14):
1807 Proxy Certificates includes this extension that contains the OID of
1808 the proxy policy language used, and can specify limits on the maximum
1809 lengths of proxy chains.  Proxy Certificates are specified in
1810 @xcite{RFC3820}.
1812 @end table
1814 In @acronym{GnuTLS} the @acronym{X.509} certificate structures are
1815 handled using the @code{gnutls_x509_crt_t} type and the corresponding
1816 private keys with the @code{gnutls_x509_privkey_t} type.  All the
1817 available functions for @acronym{X.509} certificate handling have
1818 their prototypes in @file{gnutls/x509.h}. An example program to
1819 demonstrate the @acronym{X.509} parsing capabilities can be found at
1820 section @ref{ex:x509-info}.
1822 @node Verifying X.509 certificate paths
1823 @subsection Verifying @acronym{X.509} Certificate Paths
1824 @cindex Verifying certificate paths
1826 Verifying certificate paths is important in @acronym{X.509}
1827 authentication. For this purpose the function
1828 @ref{gnutls_x509_crt_verify} is provided. The output of this function
1829 is the bitwise OR of the elements of the
1830 @code{gnutls_certificate_status_t} enumeration.  A detailed
1831 description of these elements can be found in figure below.  The
1832 function @ref{gnutls_certificate_verify_peers2} is equivalent to the
1833 previous one, and will verify the peer's certificate in a TLS session.
1835 @table @code
1837 @item GNUTLS_CERT_INVALID:
1838 The certificate is not signed by one of the known authorities, or
1839 the signature is invalid.
1841 @item GNUTLS_CERT_REVOKED:
1842 The certificate has been revoked by its CA.
1844 @item GNUTLS_CERT_SIGNER_NOT_FOUND:
1845 The certificate's issuer is not known. This is the case when the
1846 issuer is not in the trusted certificates list.
1848 @item GNUTLS_CERT_SIGNER_NOT_CA:
1849 The certificate's signer was not a CA. This may happen if
1850 this was a version 1 certificate, which is common with some CAs, or
1851 a version 3 certificate without the basic constrains extension.
1853 @anchor{GNUTLS_CERT_INSECURE_ALGORITHM}
1854 @item GNUTLS_CERT_INSECURE_ALGORITHM:
1855 The certificate was signed using an insecure algorithm such as MD2 or
1856 MD5.  These algorithms have been broken and should not be trusted.
1858 @end table
1860 There is also to possibility to pass some input to the verification
1861 functions in the form of flags. For @ref{gnutls_x509_crt_verify} the
1862 flags are passed straightforward, but
1863 @ref{gnutls_certificate_verify_peers2} depends on the flags set by
1864 calling @ref{gnutls_certificate_set_verify_flags}.  All the available
1865 flags are part of the enumeration
1866 @ref{gnutls_certificate_verify_flags} and are explained in the table
1867 below.
1869 @anchor{gnutls_certificate_verify_flags}
1870 @tindex gnutls_certificate_verify_flags
1871 @table @code
1872 @item GNUTLS_VERIFY_DISABLE_CA_SIGN:
1873 If set a signer does not have to be a certificate authority. This
1874 flag should normaly be disabled, unless you know what this means.
1876 @item GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT:
1877 Allow only trusted CA certificates that have version 1.  This is
1878 safer than GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT, and should be
1879 used instead. That way only signers in your trusted list will be
1880 allowed to have certificates of version 1.
1882 @item GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT:
1883 Allow CA certificates that have version 1 (both root and
1884 intermediate). This is dangerous since those haven't the
1885 basicConstraints extension. Must be used in combination with
1886 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT.
1888 @item GNUTLS_VERIFY_DO_NOT_ALLOW_SAME:
1889 If a certificate is not signed by anyone trusted but exists in
1890 the trusted CA list do not treat it as trusted.
1892 @item GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2:
1893 Allow certificates to be signed using the old MD2 algorithm.
1895 @item GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5:
1896 Allow certificates to be signed using the broken MD5 algorithm.
1897 @end table
1899 Although the verification of a certificate path indicates that the
1900 certificate is signed by trusted authority, does not reveal anything
1901 about the peer's identity. It is required to verify if the
1902 certificate's owner is the one you expect. For more information
1903 consult @xcite{RFC2818} and section @ref{ex:verify} for an example.
1905 @node PKCS #10 certificate requests
1906 @subsection @acronym{PKCS} #10 Certificate Requests
1907 @cindex Certificate requests
1908 @cindex @acronym{PKCS} #10
1910 A certificate request is a structure, which contain information about
1911 an applicant of a certificate service.  It usually contains a private
1912 key, a distinguished name and secondary data such as a challenge
1913 password. @acronym{GnuTLS} supports the requests defined in
1914 @acronym{PKCS} #10 @xcite{RFC2986}. Other certificate request's format
1915 such as PKIX's @xcite{RFC4211} are not currently supported.
1917 In @acronym{GnuTLS} the @acronym{PKCS} #10 structures are handled
1918 using the @code{gnutls_x509_crq_t} type.  An example of a certificate
1919 request generation can be found at section @ref{ex:crq}.
1921 @node PKCS #12 structures
1922 @subsection @acronym{PKCS} #12 Structures
1923 @cindex @acronym{PKCS} #12
1925 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
1926 private keys and certificates. It is commonly used in browsers to
1927 export and import the user's identities.
1929 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
1930 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
1931 may hold several @code{gnutls_pkcs12_bag_t} types.  The Bag types are
1932 the holders of the actual data, which may be certificates, private
1933 keys or encrypted data.  An Bag of type encrypted should be decrypted
1934 in order for its data to be accessed.
1936 An example of a @acronym{PKCS} #12 structure generation can be found
1937 at section @ref{ex:pkcs12}.
1939 @node The OpenPGP trust model
1940 @section The @acronym{OpenPGP} Trust Model
1941 @cindex @acronym{OpenPGP} Keys
1943 The @acronym{OpenPGP} key authentication relies on a distributed trust
1944 model, called the ``web of trust''. The ``web of trust'' uses a
1945 decentralized system of trusted introducers, which are the same as a
1946 CA. @acronym{OpenPGP} allows anyone to sign anyone's else public
1947 key. When Alice signs Bob's key, she is introducing Bob's key to
1948 anyone who trusts Alice. If someone trusts Alice to introduce keys,
1949 then Alice is a trusted introducer in the mind of that observer.
1951 @image{gnutls-pgp,11cm,9cm}
1953 For example: If David trusts Alice to be an introducer, and Alice
1954 signed Bob's key, Dave also trusts Bob's key to be the real one.
1956 There are some key points that are important in that model. In the
1957 example Alice has to sign Bob's key, only if she is sure that the key
1958 belongs to Bob. Otherwise she may also make Dave falsely believe that
1959 this is Bob's key. Dave has also the responsibility to know who to
1960 trust.  This model is similar to real life relations.
1962 Just see how Charlie behaves in the previous example. Although he has
1963 signed Bob's key - because he knows, somehow, that it belongs to Bob -
1964 he does not trust Bob to be an introducer. Charlie decided to trust
1965 only Kevin, for some reason. A reason could be that Bob is lazy
1966 enough, and signs other people's keys without being sure that they
1967 belong to the actual owner.
1969 @subsection @acronym{OpenPGP} Keys
1971 In @acronym{GnuTLS} the @acronym{OpenPGP} key structures
1972 @xcite{RFC2440} are handled using the @code{gnutls_openpgp_crt_t} type
1973 and the corresponding private keys with the
1974 @code{gnutls_openpgp_privkey_t} type. All the prototypes for the key
1975 handling functions can be found at @file{gnutls/openpgp.h}.
1977 @subsection Verifying an @acronym{OpenPGP} Key
1979 The verification functions of @acronym{OpenPGP} keys, included in
1980 @acronym{GnuTLS}, are simple ones, and do not use the features of the
1981 ``web of trust''.  For that reason, if the verification needs are
1982 complex, the assistance of external tools like @acronym{GnuPG} and
1983 GPGME (@url{http://www.gnupg.org/related_software/gpgme/}) is
1984 recommended.
1986 There is one verification function in @acronym{GnuTLS}, the
1987 @ref{gnutls_openpgp_crt_verify_ring}.  This checks an
1988 @acronym{OpenPGP} key against a given set of public keys (keyring) and
1989 returns the key status. The key verification status is the same as in
1990 @acronym{X.509} certificates, although the meaning and interpretation
1991 are different. For example an @acronym{OpenPGP} key may be valid, if
1992 the self signature is ok, even if no signers were found.  The meaning
1993 of verification status is shown in the figure below.
1995 @table @code
1997 @item CERT_INVALID:
1998 A signature on the key is invalid. That means that the key was
1999 modified by somebody, or corrupted during transport.
2001 @item CERT_REVOKED:
2002 The key has been revoked by its owner.
2004 @item CERT_SIGNER_NOT_FOUND:
2005 The key was not signed by a known signer.
2007 @item GNUTLS_CERT_INSECURE_ALGORITHM:
2008 The certificate was signed using an insecure algorithm such as MD2 or
2009 MD5.  These algorithms have been broken and should not be trusted.
2011 @end table
2013 @node Digital signatures
2014 @section Digital Signatures
2015 @cindex Digital signatures
2017 In this section we will provide some information about digital
2018 signatures, how they work, and give the rationale for disabling some
2019 of the algorithms used.
2021 Digital signatures work by using somebody's secret key to sign some
2022 arbitrary data.  Then anybody else could use the public key of that
2023 person to verify the signature.  Since the data may be arbitrary it is
2024 not suitable input to a cryptographic digital signature algorithm. For
2025 this reason and also for performance cryptographic hash algorithms are
2026 used to preprocess the input to the signature algorithm. This works as
2027 long as it is difficult enough to generate two different messages with
2028 the same hash algorithm output. In that case the same signature could
2029 be used as a proof for both messages. Nobody wants to sign an innocent
2030 message of donating 1 @euro{} to Greenpeace and find out that he
2031 donated 1.000.000 @euro{} to Bad Inc.
2033 For a hash algorithm to be called cryptographic the following three
2034 requirements must hold:
2036 @enumerate
2037 @item Preimage resistance.
2038 That means the algorithm must be one way and given the output of the
2039 hash function @math{H(x)}, it is impossible to calculate @math{x}.
2041 @item 2nd preimage resistance.
2042 That means that given a pair @math{x,y} with @math{y=H(x)} it is
2043 impossible to calculate an @math{x'} such that @math{y=H(x')}.
2045 @item Collision resistance.
2046 That means that it is impossible to calculate random @math{x} and
2047 @math{x'} such @math{H(x')=H(x)}.
2048 @end enumerate
2050 The last two requirements in the list are the most important in
2051 digital signatures. These protect against somebody who would like to
2052 generate two messages with the same hash output. When an algorithm is
2053 considered broken usually it means that the Collision resistance of
2054 the algorithm is less than brute force. Using the birthday paradox the
2055 brute force attack takes
2056 @iftex
2057 @math{2^{(\rm{hash\ size}) / 2}}
2058 @end iftex
2059 @ifnottex
2060 @math{2^{((hash size) / 2)}}
2061 @end ifnottex
2062 operations. Today colliding certificates using the MD5 hash algorithm
2063 have been generated as shown in @xcite{WEGER}.
2065 There has been cryptographic results for the SHA-1 hash algorithms as
2066 well, although they are not yet critical.  Before 2004, MD5 had a
2067 presumed collision strength of @math{2^{64}}, but it has been showed
2068 to have a collision strength well under @math{2^{50}}.  As of November
2069 2005, it is believed that SHA-1's collision strength is around
2070 @math{2^{63}}.  We consider this sufficiently hard so that we still
2071 support SHA-1.  We anticipate that SHA-256/386/512 will be used in
2072 publicly-distributed certificates in the future.  When @math{2^{63}}
2073 can be considered too weak compared to the computer power available
2074 sometime in the future, SHA-1 will be disabled as well.  The collision
2075 attacks on SHA-1 may also get better, given the new interest in tools
2076 for creating them.
2078 @subsection Trading Security for Interoperability
2080 If you connect to a server and use GnuTLS' functions to verify the
2081 certificate chain, and get a @ref{GNUTLS_CERT_INSECURE_ALGORITHM}
2082 validation error (@pxref{Verifying X.509 certificate paths}), it means
2083 that somewhere in the certificate chain there is a certificate signed
2084 using @code{RSA-MD2} or @code{RSA-MD5}.  These two digital signature
2085 algorithms are considered broken, so GnuTLS fail when attempting to
2086 verify the certificate.  In some situations, it may be useful to be
2087 able to verify the certificate chain anyway, assuming an attacker did
2088 not utilize the fact that these signatures algorithms are broken.
2089 This section will give help on how to achieve that.
2091 First, it is important to know that you do not have to enable any of
2092 the flags discussed here to be able to use trusted root CA
2093 certificates signed using @code{RSA-MD2} or @code{RSA-MD5}.  The only
2094 attack today is that it is possible to generate certificates with
2095 colliding signatures (collision resistance); you cannot generate a
2096 certificate that has the same signature as an already existing
2097 signature (2nd preimage resistance).
2099 If you are using @ref{gnutls_certificate_verify_peers2} to verify the
2100 certificate chain, you can call
2101 @ref{gnutls_certificate_set_verify_flags} with the
2102 @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2} or
2103 @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5} flag, as in:
2105 @example
2106   gnutls_certificate_set_verify_flags (x509cred,
2107                                        GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
2108 @end example
2110 This will tell the verifier algorithm to enable @code{RSA-MD5} when
2111 verifying the certificates.
2113 If you are using @ref{gnutls_x509_crt_verify} or
2114 @ref{gnutls_x509_crt_list_verify}, you can pass the
2115 @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5} parameter directly in the
2116 @code{flags} parameter.
2118 If you are using these flags, it may also be a good idea to warn the
2119 user when verification failure occur for this reason.  The simplest is
2120 to not use the flags by default, and only fall back to using them
2121 after warning the user.  If you wish to inspect the certificate chain
2122 yourself, you can use @ref{gnutls_certificate_get_peers} to extract
2123 the raw server's certificate chain, then use
2124 @ref{gnutls_x509_crt_import} to parse each of the certificates, and
2125 then use @ref{gnutls_x509_crt_get_signature_algorithm} to find out the
2126 signing algorithm used for each certificate.  If any of the
2127 intermediary certificates are using @code{GNUTLS_SIGN_RSA_MD2} or
2128 @code{GNUTLS_SIGN_RSA_MD5}, you could present a warning.
2132 @node How to use TLS in application protocols
2133 @chapter How To Use @acronym{TLS} in Application Protocols
2135 This chapter is intended to provide some hints on how to use the
2136 @acronym{TLS} over simple custom made application protocols.  The
2137 discussion below mainly refers to the @emph{TCP/IP} transport layer
2138 but may be extended to other ones too.
2140 @menu
2141 * Separate ports::
2142 * Upward negotiation::
2143 @end menu
2145 @node Separate ports
2146 @section Separate Ports
2148 Traditionally @acronym{SSL} was used in application protocols by
2149 assigning a new port number for the secure services. That way two
2150 separate ports were assigned, one for the non secure sessions, and one
2151 for the secured ones. This has the benefit that if a user requests a
2152 secure session then the client will try to connect to the secure port
2153 and fail otherwise. The only possible attack with this method is a
2154 denial of service one. The most famous example of this method is the
2155 famous ``HTTP over TLS'' or @acronym{HTTPS} protocol @xcite{RFC2818}.
2157 Despite its wide use, this method is not as good as it seems.  This
2158 approach starts the @acronym{TLS} Handshake procedure just after the
2159 client connects on the ---so called--- secure port.  That way the
2160 @acronym{TLS} protocol does not know anything about the client, and
2161 popular methods like the host advertising in HTTP do not
2162 work@footnote{See also the Server Name Indication extension on
2163 @ref{serverind}.}.  There is no way for the client to say ``I
2164 connected to YYY server'' before the Handshake starts, so the server
2165 cannot possibly know which certificate to use.
2167 Other than that it requires two separate ports to run a single
2168 service, which is unnecessary complication. Due to the fact that there
2169 is a limitation on the available privileged ports, this approach was
2170 soon obsoleted.
2172 @node Upward negotiation
2173 @section Upward Negotiation
2175 Other application protocols@footnote{See LDAP, IMAP etc.}  use a
2176 different approach to enable the secure layer.  They use something
2177 called the ``TLS upgrade'' method. This method is quite tricky but it
2178 is more flexible. The idea is to extend the application protocol to
2179 have a ``STARTTLS'' request, whose purpose it to start the TLS
2180 protocols just after the client requests it.  This is a really neat
2181 idea and does not require an extra port.
2183 This method is used by almost all modern protocols and there is even
2184 the @xcite{RFC2817} paper which proposes extensions to HTTP to support
2187 The tricky part, in this method, is that the ``STARTTLS'' request is
2188 sent in the clear, thus is vulnerable to modifications.  A typical
2189 attack is to modify the messages in a way that the client is fooled
2190 and thinks that the server does not have the ``STARTTLS'' capability.
2191 See a typical conversation of a hypothetical protocol:
2193 @quotation
2194 (client connects to the server)
2196 CLIENT: HELLO I'M MR. XXX
2198 SERVER: NICE TO MEET YOU XXX
2200 CLIENT: PLEASE START TLS
2202 SERVER: OK
2204 *** TLS STARTS
2206 CLIENT: HERE ARE SOME CONFIDENTIAL DATA
2207 @end quotation
2209 And see an example of a conversation where someone is acting
2210 in between:
2212 @quotation
2213 (client connects to the server)
2215 CLIENT: HELLO I'M MR. XXX
2217 SERVER: NICE TO MEET YOU XXX
2219 CLIENT: PLEASE START TLS
2221 (here someone inserts this message)
2223 SERVER: SORRY I DON'T HAVE THIS CAPABILITY
2225 CLIENT: HERE ARE SOME CONFIDENTIAL DATA
2226 @end quotation
2228 As you can see above the client was fooled, and was dummy enough to
2229 send the confidential data in the clear.
2231 How to avoid the above attack? As you may have already thought this
2232 one is easy to avoid. The client has to ask the user before it
2233 connects whether the user requests @acronym{TLS} or not. If the user
2234 answered that he certainly wants the secure layer the last
2235 conversation should be:
2237 @quotation
2238 (client connects to the server)
2240 CLIENT: HELLO I'M MR. XXX
2242 SERVER: NICE TO MEET YOU XXX
2244 CLIENT: PLEASE START TLS
2246 (here someone inserts this message)
2248 SERVER: SORRY I DON'T HAVE THIS CAPABILITY
2250 CLIENT: BYE
2252 (the client notifies the user that the secure connection was not possible)
2253 @end quotation
2255 This method, if implemented properly, is far better than the
2256 traditional method, and the security properties remain the same, since
2257 only denial of service is possible. The benefit is that the server may
2258 request additional data before the @acronym{TLS} Handshake protocol
2259 starts, in order to send the correct certificate, use the correct
2260 password file@footnote{in @acronym{SRP} authentication}, or anything
2261 else!
2263 @node How to use GnuTLS in applications
2264 @chapter How To Use @acronym{GnuTLS} in Applications
2265 @anchor{examples}
2266 @cindex Example programs
2268 @menu
2269 * Preparation::
2270 * Multi-threaded applications::
2271 * Client examples::
2272 * Server examples::
2273 * Miscellaneous examples::
2274 * Compatibility with the OpenSSL library::
2275 * Opaque PRF Input TLS Extension::
2276 * Keying Material Exporters::
2277 @end menu
2279 @node Preparation
2280 @section Preparation
2282 To use @acronym{GnuTLS}, you have to perform some changes to your
2283 sources and your build system. The necessary changes are explained in
2284 the following subsections.
2286 @menu
2287 * Headers::
2288 * Initialization::
2289 * Version check::
2290 * Debugging::
2291 * Building the source::
2292 @end menu
2294 @node Headers
2295 @subsection Headers
2297 All the data types and functions of the @acronym{GnuTLS} library are
2298 defined in the header file @file{gnutls/gnutls.h}.  This must be
2299 included in all programs that make use of the @acronym{GnuTLS}
2300 library.
2302 The extra functionality of the @acronym{GnuTLS-extra} library is
2303 available by including the header file @file{gnutls/extra.h} in your
2304 programs.
2306 @node Initialization
2307 @subsection Initialization
2309 GnuTLS must be initialized before it can be used.  The library is
2310 initialized by calling @ref{gnutls_global_init}.  The resources
2311 allocated by the initialization process can be released if the
2312 application no longer has a need to call GnuTLS functions, this is
2313 done by calling @ref{gnutls_global_deinit}.
2315 The extra functionality of the @acronym{GnuTLS-extra} library is
2316 available after calling @ref{gnutls_global_init_extra}.
2318 In order to take advantage of the internationalisation features in
2319 GnuTLS, such as translated error messages, the application must set
2320 the current locale using @code{setlocale} before initializing GnuTLS.
2322 @node Version check
2323 @subsection Version Check
2325 It is often desirable to check that the version of `gnutls' used is
2326 indeed one which fits all requirements.  Even with binary
2327 compatibility new features may have been introduced but due to problem
2328 with the dynamic linker an old version is actually used.  So you may
2329 want to check that the version is okay right after program startup.
2330 See the function @ref{gnutls_check_version}.
2332 @node Debugging
2333 @subsection Debugging
2335 In many cases things may not go as expected and further information,
2336 to assist debugging, from @acronym{GnuTLS} is desired. Those are the
2337 case where the @ref{gnutls_global_set_log_level} and
2338 @ref{gnutls_global_set_log_function} are to be used. Those will print
2339 verbose information on the @acronym{GnuTLS} functions internal flow.
2341 @node Building the source
2342 @subsection Building the Source
2344 If you want to compile a source file including the
2345 @file{gnutls/gnutls.h} header file, you must make sure that the
2346 compiler can find it in the directory hierarchy.  This is accomplished
2347 by adding the path to the directory in which the header file is
2348 located to the compilers include file search path (via the @option{-I}
2349 option).
2351 However, the path to the include file is determined at the time the
2352 source is configured.  To solve this problem, the library uses the
2353 external package @command{pkg-config} that knows the path to the
2354 include file and other configuration options.  The options that need
2355 to be added to the compiler invocation at compile time are output by
2356 the @option{--cflags} option to @command{pkg-config gnutls}.  The
2357 following example shows how it can be used at the command line:
2359 @example
2360 gcc -c foo.c `pkg-config gnutls --cflags`
2361 @end example
2363 Adding the output of @samp{pkg-config gnutls --cflags} to the
2364 compilers command line will ensure that the compiler can find the
2365 @file{gnutls/gnutls.h} header file.
2367 A similar problem occurs when linking the program with the library.
2368 Again, the compiler has to find the library files.  For this to work,
2369 the path to the library files has to be added to the library search
2370 path (via the @option{-L} option).  For this, the option
2371 @option{--libs} to @command{pkg-config gnutls} can be used.  For
2372 convenience, this option also outputs all other options that are
2373 required to link the program with the libarary (for instance, the
2374 @samp{-ltasn1} option).  The example shows how to link @file{foo.o}
2375 with the library to a program @command{foo}.
2377 @example
2378 gcc -o foo foo.o `pkg-config gnutls --libs`
2379 @end example
2381 Of course you can also combine both examples to a single command by
2382 specifying both options to @command{pkg-config}:
2384 @example
2385 gcc -o foo foo.c `pkg-config gnutls --cflags --libs`
2386 @end example
2388 @node Multi-threaded applications
2389 @section Multi-Threaded Applications
2391 Although the @acronym{GnuTLS} library is thread safe by design, some
2392 parts of Libgcrypt, such as the random generator, are not.
2393 Applications have to register callback functions to ensure proper
2394 locking in the sensitive parts of @emph{libgcrypt}.
2396 There are helper macros to help you properly initialize the libraries.
2397 Examples are shown below.
2399 @itemize
2401 @item POSIX threads
2402 @example
2403 #include <gnutls.h>
2404 #include <gcrypt.h>
2405 #include <errno.h>
2406 #include <pthread.h>
2407 GCRY_THREAD_OPTION_PTHREAD_IMPL;
2409 int main() 
2411    /* The order matters.
2412     */
2413    gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
2414    gnutls_global_init();
2416 @end example
2418 @item GNU PTH threads
2419 @example
2420 #include <gnutls.h>
2421 #include <gcrypt.h>
2422 #include <errno.h>
2423 #include <pth.h>
2424 GCRY_THREAD_OPTION_PTH_IMPL;
2426 int main() 
2428    gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
2429    gnutls_global_init();
2431 @end example
2433 @item Other thread packages
2434 @example
2435 /* The gcry_thread_cbs structure must have been
2436  * initialized.
2437  */
2438 static struct gcry_thread_cbs gcry_threads_other = @{ ... @};
2440 int main()
2442    gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_other);
2444 @end example
2445 @end itemize
2447 @node Client examples
2448 @section Client Examples
2450 This section contains examples of @acronym{TLS} and @acronym{SSL}
2451 clients, using @acronym{GnuTLS}.  Note that these examples contain
2452 little or no error checking.  Some of the examples require functions
2453 implemented by another example.
2455 @menu
2456 * Simple client example with anonymous authentication::
2457 * Simple client example with X.509 certificate support::
2458 * Obtaining session information::
2459 * Verifying peer's certificate::
2460 * Using a callback to select the certificate to use::
2461 * Client with Resume capability example::
2462 * Simple client example with SRP authentication::
2463 * Simple client example with TLS/IA support::
2464 * Simple client example in C++::
2465 * Helper function for TCP connections::
2466 @end menu
2468 @node Simple client example with anonymous authentication
2469 @subsection Simple Client Example with Anonymous Authentication
2471 The simplest client using TLS is the one that doesn't do any
2472 authentication.  This means no external certificates or passwords are
2473 needed to set up the connection.  As could be expected, the connection
2474 is vulnerable to man-in-the-middle (active or redirection) attacks.
2475 However, the data is integrity and privacy protected.
2477 @verbatiminclude examples/ex-client1.c
2479 @node Simple client example with X.509 certificate support
2480 @subsection Simple Client Example with @acronym{X.509} Certificate Support
2482 Let's assume now that we want to create a TCP client which
2483 communicates with servers that use @acronym{X.509} or
2484 @acronym{OpenPGP} certificate authentication. The following client is
2485 a very simple @acronym{TLS} client, it does not support session
2486 resuming, not even certificate verification. The TCP functions defined
2487 in this example are used in most of the other examples below, without
2488 redefining them.
2490 @verbatiminclude examples/ex-client2.c
2492 @node Obtaining session information
2493 @subsection Obtaining Session Information
2495 Most of the times it is desirable to know the security properties of
2496 the current established session.  This includes the underlying ciphers
2497 and the protocols involved.  That is the purpose of the following
2498 function.  Note that this function will print meaningful values only
2499 if called after a successful @ref{gnutls_handshake}.
2501 @verbatiminclude examples/ex-session-info.c
2503 @node Verifying peer's certificate
2504 @subsection Verifying Peer's Certificate
2505 @anchor{ex:verify}
2507 A @acronym{TLS} session is not secure just after the handshake
2508 procedure has finished.  It must be considered secure, only after the
2509 peer's certificate and identity have been verified. That is, you have
2510 to verify the signature in peer's certificate, the hostname in the
2511 certificate, and expiration dates.  Just after this step you should
2512 treat the connection as being a secure one.
2514 @verbatiminclude examples/ex-rfc2818.c
2516 An other example is listed below which provides a more detailed
2517 verification output.
2519 @verbatiminclude examples/ex-verify.c
2521 @node Using a callback to select the certificate to use
2522 @subsection Using a Callback to Select the Certificate to Use
2524 There are cases where a client holds several certificate and key
2525 pairs, and may not want to load all of them in the credentials
2526 structure.  The following example demonstrates the use of the
2527 certificate selection callback.
2529 @verbatiminclude examples/ex-cert-select.c
2531 @node Client with Resume capability example
2532 @subsection Client with Resume Capability Example
2533 @anchor{ex:resume-client}
2535 This is a modification of the simple client example. Here we
2536 demonstrate the use of session resumption. The client tries to connect
2537 once using @acronym{TLS}, close the connection and then try to
2538 establish a new connection using the previously negotiated data.
2540 @verbatiminclude examples/ex-client-resume.c
2542 @node Simple client example with SRP authentication
2543 @subsection Simple Client Example with @acronym{SRP} Authentication
2545 The following client is a very simple @acronym{SRP} @acronym{TLS}
2546 client which connects to a server and authenticates using a
2547 @emph{username} and a @emph{password}. The server may authenticate
2548 itself using a certificate, and in that case it has to be verified.
2550 @verbatiminclude examples/ex-client-srp.c
2552 @node Simple client example with TLS/IA support
2553 @subsection Simple Client Example with @acronym{TLS/IA} Support
2555 The following client is a simple client which uses the
2556 @acronym{TLS/IA} extension to authenticate with the server.
2558 @verbatiminclude examples/ex-client-tlsia.c
2560 @node Simple client example in C++
2561 @subsection Simple Client Example using the C++ API
2563 The following client is a simple example of a client client utilizing
2564 the GnuTLS C++ API.
2566 @verbatiminclude examples/ex-cxx.cpp
2568 @node Helper function for TCP connections
2569 @subsection Helper Function for TCP Connections
2571 This helper function abstracts away TCP connection handling from the
2572 other examples.  It is required to build some examples.
2574 @verbatiminclude examples/tcp.c
2576 @node Server examples
2577 @section Server Examples
2579 This section contains examples of @acronym{TLS} and @acronym{SSL}
2580 servers, using @acronym{GnuTLS}.
2582 @menu
2583 * Echo Server with X.509 authentication::
2584 * Echo Server with X.509 authentication II::
2585 * Echo Server with OpenPGP authentication::
2586 * Echo Server with SRP authentication::
2587 * Echo Server with anonymous authentication::
2588 @end menu
2590 @node Echo Server with X.509 authentication
2591 @subsection Echo Server with @acronym{X.509} Authentication
2593 This example is a very simple echo server which supports
2594 @acronym{X.509} authentication, using the RSA ciphersuites.
2596 @verbatiminclude examples/ex-serv1.c
2598 @node Echo Server with X.509 authentication II
2599 @subsection Echo Server with @acronym{X.509} Authentication II
2601 The following example is a server which supports @acronym{X.509}
2602 authentication.  This server supports the export-grade cipher suites,
2603 the DHE ciphersuites and session resuming.
2605 @verbatiminclude examples/ex-serv-export.c
2607 @node Echo Server with OpenPGP authentication
2608 @subsection Echo Server with @acronym{OpenPGP} Authentication
2609 @cindex @acronym{OpenPGP} Server
2611 The following example is an echo server which supports
2612 @acronym{@acronym{OpenPGP}} key authentication. You can easily combine
2613 this functionality ---that is have a server that supports both
2614 @acronym{X.509} and @acronym{OpenPGP} certificates--- but we separated
2615 them to keep these examples as simple as possible.
2617 @verbatiminclude examples/ex-serv-pgp.c
2619 @node Echo Server with SRP authentication
2620 @subsection Echo Server with @acronym{SRP} Authentication
2622 This is a server which supports @acronym{SRP} authentication. It is
2623 also possible to combine this functionality with a certificate
2624 server. Here it is separate for simplicity.
2626 @verbatiminclude examples/ex-serv-srp.c
2628 @node Echo Server with anonymous authentication
2629 @subsection Echo Server with Anonymous Authentication
2631 This example server support anonymous authentication, and could be
2632 used to serve the example client for anonymous authentication.
2634 @verbatiminclude examples/ex-serv-anon.c
2636 @node Miscellaneous examples
2637 @section Miscellaneous Examples
2639 @menu
2640 * Checking for an alert::
2641 * X.509 certificate parsing example::
2642 * Certificate request generation::
2643 * PKCS #12 structure generation::
2644 @end menu
2646 @node Checking for an alert
2647 @subsection Checking for an Alert
2649 This is a function that checks if an alert has been received in the
2650 current session.
2652 @verbatiminclude examples/ex-alert.c
2654 @node X.509 certificate parsing example
2655 @subsection @acronym{X.509} Certificate Parsing Example
2656 @anchor{ex:x509-info}
2658 To demonstrate the @acronym{X.509} parsing capabilities an example program is
2659 listed below.  That program reads the peer's certificate, and prints
2660 information about it.
2662 @verbatiminclude examples/ex-x509-info.c
2664 @node Certificate request generation
2665 @subsection Certificate Request Generation
2666 @anchor{ex:crq}
2668 The following example is about generating a certificate request, and a
2669 private key. A certificate request can be later be processed by a CA,
2670 which should return a signed certificate.
2672 @verbatiminclude examples/ex-crq.c
2674 @node PKCS #12 structure generation
2675 @subsection @acronym{PKCS} #12 Structure Generation
2676 @anchor{ex:pkcs12}
2678 The following example is about generating a @acronym{PKCS} #12
2679 structure.
2681 @verbatiminclude examples/ex-pkcs12.c
2683 @node Compatibility with the OpenSSL library
2684 @section Compatibility with the OpenSSL Library
2685 @cindex OpenSSL
2687 To ease @acronym{GnuTLS}' integration with existing applications, a
2688 compatibility layer with the widely used OpenSSL library is included
2689 in the @code{gnutls-openssl} library. This compatibility layer is not
2690 complete and it is not intended to completely reimplement the OpenSSL
2691 API with @acronym{GnuTLS}.  It only provides source-level
2692 compatibility. There is currently no attempt to make it
2693 binary-compatible with OpenSSL.
2695 The prototypes for the compatibility functions are in the
2696 @file{gnutls/openssl.h} header file.
2698 Current limitations imposed by the compatibility layer include:
2700 @itemize
2702 @item Error handling is not thread safe.
2704 @end itemize
2706 @node Opaque PRF Input TLS Extension
2707 @section Opaque PRF Input TLS Extension
2708 @cindex Opaque PRF Input
2710 GnuTLS supports the Opaque PRF Input TLS extension
2711 (@code{draft-rescorla-tls-opaque-prf-input-00.txt}).  The API consists
2712 of one API for use in the client, @ref{gnutls_oprfi_enable_client},
2713 and one API for use in the server, @ref{gnutls_oprfi_enable_server}.
2714 You must invoke both functions before calling @ref{gnutls_handshake}.
2715 The server utilizes a callback function into the application.  The
2716 callback can look at the random string provided by the client, and
2717 also set the server string.  The string lengths must be equal
2718 according to the protocol.
2720 @node Keying Material Exporters
2721 @section Keying Material Exporters
2722 @cindex Keying Material Exporters
2723 @cindex Exporting Keying Material
2725 The TLS PRF can be used by other protocols to derive data.  The API to
2726 use is @ref{gnutls_prf}.  The function needs to be provided with the
2727 label in the parameter @code{label}, and the extra data to mix in the
2728 @code{extra} parameter.  Depending on whether you want to mix in the
2729 client or server random data first, you can set the
2730 @code{server_random_first} parameter.
2732 For example, after establishing a TLS session using
2733 @ref{gnutls_handshake}, you can invoke the TLS PRF with this call:
2735 @smallexample
2736 #define MYLABEL "EXPORTER-FOO"
2737 #define MYCONTEXT "some context data"
2738 char out[32];
2739 rc = gnutls_prf (session, strlen (MYLABEL), MYLABEL, 0,
2740                  strlen (MYCONTEXT), MYCONTEXT, 32, out);
2741 @end smallexample
2743 If you don't want to mix in the client/server random, there is a more
2744 low-level TLS PRF interface called @ref{gnutls_prf_raw}.
2746 @node Included programs
2747 @chapter Included Programs
2749 Included with @acronym{GnuTLS} are also a few command line tools that
2750 let you use the library for common tasks without writing an
2751 application.  The applications are discussed in this chapter.
2753 @menu
2754 * Invoking certtool::
2755 * Invoking gnutls-cli::
2756 * Invoking gnutls-cli-debug::
2757 * Invoking gnutls-serv::
2758 * Invoking psktool::
2759 * Invoking srptool::
2760 @end menu
2762 @node Invoking certtool
2763 @section Invoking certtool
2764 @cindex certtool
2766 This is a program to generate @acronym{X.509} certificates, certificate
2767 requests, CRLs and private keys.
2769 @verbatim
2770 Certtool help
2771 Usage: certtool [options]
2772      -s, --generate-self-signed
2773                               Generate a self-signed certificate.
2774      -c, --generate-certificate
2775                               Generate a signed certificate.
2776      --generate-proxy         Generate a proxy certificate.
2777      --generate-crl           Generate a CRL.
2778      -u, --update-certificate
2779                               Update a signed certificate.
2780      -p, --generate-privkey   Generate a private key.
2781      -q, --generate-request   Generate a PKCS #10 certificate
2782                               request.
2783      -e, --verify-chain       Verify a PEM encoded certificate chain.
2784                               The last certificate in the chain must
2785                               be a self signed one.
2786      --verify-crl             Verify a CRL.
2787      --generate-dh-params     Generate PKCS #3 encoded Diffie-Hellman
2788                               parameters.
2789      --get-dh-params          Get the included PKCS #3 encoded Diffie
2790                               Hellman parameters.
2791      --load-privkey FILE      Private key file to use.
2792      --load-request FILE      Certificate request file to use.
2793      --load-certificate FILE
2794                               Certificate file to use.
2795      --load-ca-privkey FILE   Certificate authority's private key
2796                               file to use.
2797      --load-ca-certificate FILE
2798                               Certificate authority's certificate
2799                               file to use.
2800      --password PASSWORD      Password to use.
2801      -i, --certificate-info   Print information on a certificate.
2802      -l, --crl-info           Print information on a CRL.
2803      --p12-info               Print information on a PKCS #12
2804                               structure.
2805      --p7-info                Print information on a PKCS #7
2806                               structure.
2807      --smime-to-p7            Convert S/MIME to PKCS #7 structure.
2808      -k, --key-info           Print information on a private key.
2809      --fix-key                Regenerate the parameters in a private
2810                               key.
2811      --to-p12                 Generate a PKCS #12 structure.
2812      -8, --pkcs8              Use PKCS #8 format for private keys.
2813      --dsa                    Use DSA keys.
2814      --hash STR               Hash algorithm to use for signing
2815                               (MD5,SHA1,RMD160).
2816      --export-ciphers         Use weak encryption algorithms.
2817      --inder                  Use DER format for input certificates
2818                               and private keys.
2819      --outder                 Use DER format for output certificates
2820                               and private keys.
2821      --bits BITS              specify the number of bits for key
2822                               generation.
2823      --outfile FILE           Output file.
2824      --infile FILE            Input file.
2825      --template FILE          Template file to use for non
2826                               interactive operation.
2827      -d, --debug LEVEL        specify the debug level. Default is 1.
2828      -h, --help               shows this help text
2829      -v, --version            shows the program's version
2830 @end verbatim
2832 The program can be used interactively or non interactively by
2833 specifying the @code{--template} command line option. See below for an
2834 example of a template file.
2836 How to use certtool interactively:
2838 @itemize
2839 @item
2840 To generate parameters for Diffie-Hellman key exchange, use the command:
2841 @example
2842 $ certtool --generate-dh-params --outfile dh.pem
2843 @end example
2845 @item
2846 To generate parameters for the RSA-EXPORT key exchange, use the command:
2847 @example
2848 $ certtool --generate-privkey --bits 512 --outfile rsa.pem
2849 @end example
2851 @end itemize
2853 @itemize
2855 @item
2856 To create a self signed certificate, use the command:
2857 @example
2858 $ certtool --generate-privkey --outfile ca-key.pem
2859 $ certtool --generate-self-signed --load-privkey ca-key.pem \
2860    --outfile ca-cert.pem
2861 @end example
2863 Note that a self-signed certificate usually belongs to a certificate
2864 authority, that signs other certificates.
2866 @item
2867 To create a private key (RSA by default), run:
2869 @example
2870 $ certtool --generate-privkey --outfile key.pem
2871 @end example
2873 To create a DSA private key, run:
2875 @example
2876 $ certtool --dsa --generate-privkey --outfile key-dsa.pem
2877 @end example
2879 @item
2880 To generate a certificate using the private key, use the command:
2882 @example
2883 $ certtool --generate-certificate --load-privkey key.pem \
2884    --outfile cert.pem --load-ca-certificate ca-cert.pem \
2885    --load-ca-privkey ca-key.pem
2886 @end example
2888 @item
2889 To create a certificate request (needed when the certificate is issued by
2890 another party), run:
2892 @example
2893 $ certtool --generate-request --load-privkey key.pem \
2894   --outfile request.pem
2895 @end example
2897 @item
2898 To generate a certificate using the previous request, use the command:
2900 @example
2901 $ certtool --generate-certificate --load-request request.pem \
2902    --outfile cert.pem \
2903    --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem
2904 @end example
2906 @item
2907 To view the certificate information, use:
2909 @example
2910 $ certtool --certificate-info --infile cert.pem
2911 @end example
2913 @item
2914 To generate a @acronym{PKCS} #12 structure using the previous key and
2915 certificate, use the command:
2917 @example
2918 $ certtool --load-certificate cert.pem --load-privkey key.pem \
2919   --to-p12 --outder --outfile key.p12
2920 @end example
2922 Some tools (reportedly web browsers) have problems with that file
2923 because it does not contain the CA certificate for the certificate.
2924 To work around that problem in the tool, you can use the
2925 @samp{--load-ca-certificate} parameter as follows:
2927 @example
2928 $ certtool --load-ca-certificate ca.pem \
2929   --load-certificate cert.pem --load-privkey key.pem \
2930   --to-p12 --outder --outfile key.p12
2931 @end example
2933 @item
2934 Proxy certificate can be used to delegate your credential to a
2935 temporary, typically short-lived, certificate.  To create one from the
2936 previously created certificate, first create a temporary key and then
2937 generate a proxy certificate for it, using the commands:
2939 @example
2940 $ certtool --generate-privkey > proxy-key.pem
2941 $ certtool --generate-proxy --load-ca-privkey key.pem \
2942   --load-privkey proxy-key.pem --load-certificate cert.pem \
2943   --outfile proxy-cert.pem
2944 @end example
2946 @item
2947 To create an empty Certificate Revocation List (CRL) do:
2949 @example
2950 $ certtool --generate-crl --load-ca-privkey x509-ca-key.pem --load-ca-certificate x509-ca.pem
2951 @end example
2953 To create a CRL that contains some revoked certificates, place the
2954 certificates in a file and use @code{--load-certificate} as follows:
2956 @example
2957 $ certtool --generate-crl --load-ca-privkey x509-ca-key.pem --load-ca-certificate x509-ca.pem --load-certificate revoked-certs.pem
2958 @end example
2960 @item
2961 To verify a Certificate Revocation List (CRL) do:
2963 @example
2964 $ certtool --verify-crl --load-ca-certificate x509-ca.pem < crl.pem
2965 @end example
2967 @end itemize
2969 Certtool's template file format:
2971 @itemize
2973 @item
2974 Firstly create a file named 'cert.cfg' that contains the information
2975 about the certificate. An example file is listed below.
2977 @item
2978 Then execute:
2980 @example
2981 $ certtool --generate-certificate cert.pem --load-privkey key.pem  \
2982    --template cert.cfg \
2983    --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem
2984 @end example
2986 @end itemize
2988 An example certtool template file:
2990 @example
2991 # X.509 Certificate options
2993 # DN options
2995 # The organization of the subject.
2996 organization = "Koko inc."
2998 # The organizational unit of the subject.
2999 unit = "sleeping dept."
3001 # The locality of the subject.
3002 # locality =
3004 # The state of the certificate owner.
3005 state = "Attiki"
3007 # The country of the subject. Two letter code.
3008 country = GR
3010 # The common name of the certificate owner.
3011 cn = "Cindy Lauper"
3013 # A user id of the certificate owner.
3014 #uid = "clauper"
3016 # If the supported DN OIDs are not adequate you can set
3017 # any OID here.
3018 # For example set the X.520 Title and the X.520 Pseudonym
3019 # by using OID and string pairs.
3020 #dn_oid = "2.5.4.12" "Dr." "2.5.4.65" "jackal"
3022 # This is deprecated and should not be used in new
3023 # certificates.
3024 # pkcs9_email = "none@@none.org"
3026 # The serial number of the certificate
3027 serial = 007
3029 # In how many days, counting from today, this certificate will expire.
3030 expiration_days = 700
3032 # X.509 v3 extensions
3034 # A dnsname in case of a WWW server.
3035 #dns_name = "www.none.org"
3036 #dns_name = "www.morethanone.org"
3038 # An IP address in case of a server.
3039 #ip_address = "192.168.1.1"
3041 # An email in case of a person
3042 email = "none@@none.org"
3044 # An URL that has CRLs (certificate revocation lists)
3045 # available. Needed in CA certificates.
3046 #crl_dist_points = "http://www.getcrl.crl/getcrl/"
3048 # Whether this is a CA certificate or not
3051 # Whether this certificate will be used for a TLS client
3052 #tls_www_client
3054 # Whether this certificate will be used for a TLS server
3055 #tls_www_server
3057 # Whether this certificate will be used to sign data (needed
3058 # in TLS DHE ciphersuites).
3059 signing_key
3061 # Whether this certificate will be used to encrypt data (needed
3062 # in TLS RSA ciphersuites). Note that it is preferred to use different
3063 # keys for encryption and signing.
3064 #encryption_key
3066 # Whether this key will be used to sign other certificates.
3067 #cert_signing_key
3069 # Whether this key will be used to sign CRLs.
3070 #crl_signing_key
3072 # Whether this key will be used to sign code.
3073 #code_signing_key
3075 # Whether this key will be used to sign OCSP data.
3076 #ocsp_signing_key
3078 # Whether this key will be used for time stamping.
3079 #time_stamping_key
3080 @end example
3082 @node Invoking gnutls-cli
3083 @section Invoking gnutls-cli
3084 @cindex gnutls-cli
3086 Simple client program to set up a TLS connection to some other
3087 computer.  It sets up a TLS connection and forwards data from the
3088 standard input to the secured socket and vice versa.
3090 @verbatim
3091 GNU TLS test client
3092 Usage:  gnutls-cli [options] hostname
3094      -d, --debug integer      Enable debugging
3095      -r, --resume             Connect, establish a session. Connect
3096                               again and resume this session.
3097      -s, --starttls           Connect, establish a plain session and
3098                               start TLS when EOF or a SIGALRM is
3099                               received.
3100      --crlf                   Send CR LF instead of LF.
3101      --x509fmtder             Use DER format for certificates to read
3102                               from.
3103      -f, --fingerprint        Send the openpgp fingerprint, instead
3104                               of the key.
3105      --disable-extensions     Disable all the TLS extensions.
3106      --print-cert             Print the certificate in PEM format.
3107      --recordsize integer     The maximum record size to advertize.
3108      -V, --verbose            More verbose output.
3109      --ciphers cipher1 cipher2...
3110                               Ciphers to enable.
3111      --protocols protocol1 protocol2...
3112                               Protocols to enable.
3113      --comp comp1 comp2...    Compression methods to enable.
3114      --macs mac1 mac2...      MACs to enable.
3115      --kx kx1 kx2...          Key exchange methods to enable.
3116      --ctypes certType1 certType2...
3117                               Certificate types to enable.
3118      --priority PRIORITY STRING
3119                               Priorities string.
3120      --x509cafile FILE        Certificate file to use.
3121      --x509crlfile FILE       CRL file to use.
3122      --pgpkeyfile FILE        PGP Key file to use.
3123      --pgpkeyring FILE        PGP Key ring file to use.
3124      --pgpcertfile FILE       PGP Public Key (certificate) file to
3125                               use.
3126      --pgpsubkey HEX|auto     PGP subkey to use.
3127      --x509keyfile FILE       X.509 key file to use.
3128      --x509certfile FILE      X.509 Certificate file to use.
3129      --srpusername NAME       SRP username to use.
3130      --srppasswd PASSWD       SRP password to use.
3131      --pskusername NAME       PSK username to use.
3132      --pskkey KEY             PSK key (in hex) to use.
3133      --opaque-prf-input DATA
3134                               Use Opaque PRF Input DATA.
3135      -p, --port PORT          The port to connect to.
3136      --insecure               Don't abort program if server
3137                               certificate can't be validated.
3138      -l, --list               Print a list of the supported
3139                               algorithms and modes.
3140      -h, --help               prints this help
3141      -v, --version            prints the program's version number
3142 @end verbatim
3144 To connect to a server using PSK authentication, you may use something
3145 like:
3147 @smallexample
3148 $ gnutls-cli -p 5556 test.gnutls.org --pskusername jas --pskkey 9e32cf7786321a828ef7668f09fb35db --priority NORMAL:+PSK:-RSA:-DHE-RSA -d 4711
3149 @end smallexample
3151 @menu
3152 * Example client PSK connection::
3153 @end menu
3155 @node Example client PSK connection
3156 @subsection Example client PSK connection
3157 @cindex PSK client
3159 If your server only supports the PSK ciphersuite, connecting to it
3160 should be as simple as connecting to the server:
3162 @smallexample
3163 $ ./gnutls-cli -p 5556 localhost
3164 Resolving 'localhost'...
3165 Connecting to '127.0.0.1:5556'...
3166 - PSK client callback. PSK hint 'psk_identity_hint'
3167 Enter PSK identity: psk_identity
3168 Enter password: 
3169 - PSK authentication. PSK hint 'psk_identity_hint'
3170 - Version: TLS1.1
3171 - Key Exchange: PSK
3172 - Cipher: AES-128-CBC
3173 - MAC: SHA1
3174 - Compression: NULL
3175 - Handshake was completed
3177 - Simple Client Mode:
3178 @end smallexample
3180 If the server supports several cipher suites, you may need to force it
3181 to chose PSK by using a cipher priority parameter such as
3182 @code{--priority NORMAL:+PSK:-RSA:-DHE-RSA:-DHE-PSK}.
3184 @cindex Netconf
3185 Instead of using the Netconf-way to derive the PSK key from a
3186 password, you can also give the PSK username and key directly on the
3187 command line:
3189 @smallexample
3190 $ ./gnutls-cli -p 5556 localhost --pskusername psk_identity --pskkey 88f3824b3e5659f52d00e959bacab954b6540344 
3191 Resolving 'localhost'...
3192 Connecting to '127.0.0.1:5556'...
3193 - PSK authentication. PSK hint 'psk_identity_hint'
3194 - Version: TLS1.1
3195 - Key Exchange: PSK
3196 - Cipher: AES-128-CBC
3197 - MAC: SHA1
3198 - Compression: NULL
3199 - Handshake was completed
3201 - Simple Client Mode:
3202 @end smallexample
3204 By keeping the @code{--pskusername} parameter and removing the
3205 @code{--pskkey} parameter, it will query only for the password during
3206 the handshake.
3208 @node Invoking gnutls-cli-debug
3209 @section Invoking gnutls-cli-debug
3210 @cindex gnutls-cli-debug
3212 This program was created to assist in debugging @acronym{GnuTLS}, but
3213 it might be useful to extract a @acronym{TLS} server's capabilities.
3214 It's purpose is to connect onto a @acronym{TLS} server, perform some
3215 tests and print the server's capabilities. If called with the `-v'
3216 parameter a more checks will be performed. An example output is:
3218 @smallexample
3219 crystal:/cvs/gnutls/src$ ./gnutls-cli-debug localhost -p 5556
3220 Resolving 'localhost'...
3221 Connecting to '127.0.0.1:5556'...
3222 Checking for TLS 1.1 support... yes
3223 Checking fallback from TLS 1.1 to... N/A
3224 Checking for TLS 1.0 support... yes
3225 Checking for SSL 3.0 support... yes
3226 Checking for version rollback bug in RSA PMS... no
3227 Checking for version rollback bug in Client Hello... no
3228 Checking whether we need to disable TLS 1.0... N/A
3229 Checking whether the server ignores the RSA PMS version... no
3230 Checking whether the server can accept Hello Extensions... yes
3231 Checking whether the server can accept cipher suites not in SSL 3.0 spec... yes
3232 Checking whether the server can accept a bogus TLS record version in the client hello... yes
3233 Checking for certificate information... N/A
3234 Checking for trusted CAs... N/A
3235 Checking whether the server understands TLS closure alerts... yes
3236 Checking whether the server supports session resumption... yes
3237 Checking for export-grade ciphersuite support... no
3238 Checking RSA-export ciphersuite info... N/A
3239 Checking for anonymous authentication support... no
3240 Checking anonymous Diffie-Hellman group info... N/A
3241 Checking for ephemeral Diffie-Hellman support... no
3242 Checking ephemeral Diffie-Hellman group info... N/A
3243 Checking for AES cipher support (TLS extension)... yes
3244 Checking for 3DES cipher support... yes
3245 Checking for ARCFOUR 128 cipher support... yes
3246 Checking for ARCFOUR 40 cipher support... no
3247 Checking for MD5 MAC support... yes
3248 Checking for SHA1 MAC support... yes
3249 Checking for ZLIB compression support (TLS extension)... yes
3250 Checking for LZO compression support (GnuTLS extension)... yes
3251 Checking for max record size (TLS extension)... yes
3252 Checking for SRP authentication support (TLS extension)... yes
3253 Checking for OpenPGP authentication support (TLS extension)... no
3254 @end smallexample
3256 @node Invoking gnutls-serv
3257 @section Invoking gnutls-serv
3258 @cindex gnutls-serv
3260 Simple server program that listens to incoming TLS connections.
3262 @verbatim
3263 GNU TLS test server
3264 Usage: gnutls-serv [options]
3266      -d, --debug integer      Enable debugging
3267      -g, --generate           Generate Diffie-Hellman Parameters.
3268      -p, --port integer       The port to connect to.
3269      -q, --quiet              Suppress some messages.
3270      --nodb                   Does not use the resume database.
3271      --http                   Act as an HTTP Server.
3272      --echo                   Act as an Echo Server.
3273      --dhparams FILE          DH params file to use.
3274      --x509fmtder             Use DER format for certificates
3275      --x509cafile FILE        Certificate file to use.
3276      --x509crlfile FILE       CRL file to use.
3277      --pgpkeyring FILE        PGP Key ring file to use.
3278      --pgpkeyfile FILE        PGP Key file to use.
3279      --pgpcertfile FILE       PGP Public Key (certificate) file to
3280                               use.
3281      --pgpsubkey HEX|auto     PGP subkey to use.
3282      --x509keyfile FILE       X.509 key file to use.
3283      --x509certfile FILE      X.509 Certificate file to use.
3284      --x509dsakeyfile FILE    Alternative X.509 key file to use.
3285      --x509dsacertfile FILE   Alternative X.509 certificate file to
3286                               use.
3287      -r, --require-cert       Require a valid certificate.
3288      -a, --disable-client-cert
3289                               Disable request for a client
3290                               certificate.
3291      --pskpasswd FILE         PSK password file to use.
3292      --pskhint HINT           PSK identity hint to use.
3293      --srppasswd FILE         SRP password file to use.
3294      --srppasswdconf FILE     SRP password conf file to use.
3295      --opaque-prf-input DATA
3296                               Use Opaque PRF Input DATA.
3297      --ciphers cipher1 cipher2...
3298                               Ciphers to enable.
3299      --protocols protocol1 protocol2...
3300                               Protocols to enable.
3301      --comp comp1 comp2...    Compression methods to enable.
3302      --macs mac1 mac2...      MACs to enable.
3303      --kx kx1 kx2...          Key exchange methods to enable.
3304      --ctypes certType1 certType2...
3305                               Certificate types to enable.
3306      --priority PRIORITY STRING
3307                               Priorities string.
3308      -l, --list               Print a list of the supported
3309                               algorithms  and modes.
3310      -h, --help               prints this help
3311      -v, --version            prints the program's version number
3312 @end verbatim
3314 @subsection Setting Up a Test HTTPS Server
3315 @cindex HTTPS server
3316 @cindex debug server
3318 Running your own TLS server based on GnuTLS can be useful when
3319 debugging clients and/or GnuTLS itself.  This section describes how to
3320 use @code{gnutls-serv} as a simple HTTPS server.
3322 The most basic server can be started as:
3324 @example
3325 gnutls-serv --http
3326 @end example
3328 It will only support anonymous ciphersuites, which many TLS clients
3329 refuse to use.
3331 The next step is to add support for X.509.  First we generate a CA:
3333 @example
3334 certtool --generate-privkey > x509-ca-key.pem
3335 echo 'cn = GnuTLS test CA' > ca.tmpl
3336 echo 'ca' >> ca.tmpl
3337 echo 'cert_signing_key' >> ca.tmpl
3338 certtool --generate-self-signed --load-privkey x509-ca-key.pem \
3339   --template ca.tmpl --outfile x509-ca.pem
3341 @end example
3343 Then generate a server certificate.  Remember to change the dns_name
3344 value to the name of your server host, or skip that command to avoid
3345 the field.
3347 @example
3348 certtool --generate-privkey > x509-server-key.pem
3349 echo 'organization = GnuTLS test server' > server.tmpl
3350 echo 'cn = test.gnutls.org' >> server.tmpl
3351 echo 'tls_www_server' >> server.tmpl
3352 echo 'encryption_key' >> server.tmpl
3353 echo 'signing_key' >> server.tmpl
3354 echo 'dns_name = test.gnutls.org' >> server.tmpl
3355 certtool --generate-certificate --load-privkey x509-server-key.pem \
3356   --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
3357   --template server.tmpl --outfile x509-server.pem
3359 @end example
3361 For use in the client, you may want to generate a client certificate
3362 as well.
3364 @example
3365 certtool --generate-privkey > x509-client-key.pem
3366 echo 'cn = GnuTLS test client' > client.tmpl
3367 echo 'tls_www_client' >> client.tmpl
3368 echo 'encryption_key' >> client.tmpl
3369 echo 'signing_key' >> client.tmpl
3370 certtool --generate-certificate --load-privkey x509-client-key.pem \
3371   --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
3372   --template client.tmpl --outfile x509-client.pem
3374 @end example
3376 To be able to import the client key/certificate into some
3377 applications, you will need to convert them into a PKCS#12 structure.
3378 This also encrypts the security sensitive key with a password.
3380 @example
3381 certtool --to-p12 --load-ca-certificate x509-ca.pem --load-privkey x509-client-key.pem --load-certificate x509-client.pem --outder --outfile x509-client.p12
3382 @end example
3384 For icing, we'll create a proxy certificate for the client too.
3386 @example
3387 certtool --generate-privkey > x509-proxy-key.pem
3388 echo 'cn = GnuTLS test client proxy' > proxy.tmpl
3389 certtool --generate-proxy --load-privkey x509-proxy-key.pem \
3390   --load-ca-certificate x509-client.pem --load-ca-privkey x509-client-key.pem \
3391   --load-certificate x509-client.pem --template proxy.tmpl \
3392   --outfile x509-proxy.pem
3394 @end example
3396 Then start the server again:
3398 @example
3399 gnutls-serv --http \
3400             --x509cafile x509-ca.pem \
3401             --x509keyfile x509-server-key.pem \
3402             --x509certfile x509-server.pem
3403 @end example
3405 Try connecting to the server using your web browser.  Note that the
3406 server listens to port 5556 by default.
3408 While you are at it, to allow connections using DSA, you can also
3409 create a DSA key and certificate for the server.  These credentials
3410 will be used in the final example below.
3412 @example
3413 certtool --generate-privkey --dsa > x509-server-key-dsa.pem
3414 certtool --generate-certificate --load-privkey x509-server-key-dsa.pem \
3415   --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
3416   --template server.tmpl --outfile x509-server-dsa.pem
3418 @end example
3420 The next step is to create OpenPGP credentials for the server.
3422 @example
3423 gpg --gen-key
3424 ...enter whatever details you want, use 'test.gnutls.org' as name...
3425 @end example
3427 Make a note of the OpenPGP key identifier of the newly generated key,
3428 here it was @code{5D1D14D8}.  You will need to export the key for
3429 GnuTLS to be able to use it.
3431 @example
3432 gpg -a --export 5D1D14D8 > openpgp-server.txt
3433 gpg --export 5D1D14D8 > openpgp-server.bin
3434 gpg --export-secret-keys 5D1D14D8 > openpgp-server-key.bin
3435 gpg -a --export-secret-keys 5D1D14D8 > openpgp-server-key.txt
3436 @end example
3438 Let's start the server with support for OpenPGP credentials:
3440 @example
3441 gnutls-serv --http \
3442             --pgpkeyfile openpgp-server-key.txt \
3443             --pgpcertfile openpgp-server.txt
3444 @end example
3446 The next step is to add support for SRP authentication.
3448 @example
3449 srptool --create-conf srp-tpasswd.conf
3450 srptool --passwd-conf srp-tpasswd.conf --username jas --passwd srp-passwd.txt
3451 Enter password: [TYPE "foo"]
3452 @end example
3454 Start the server with SRP support:
3456 @example
3457 gnutls-serv --http \
3458             --srppasswdconf srp-tpasswd.conf \
3459             --srppasswd srp-passwd.txt
3460 @end example
3462 Let's also add support for PSK.
3464 @example
3465 $ psktool --passwd psk-passwd.txt
3466 @end example
3468 Start the server with PSK support:
3470 @example
3471 gnutls-serv --http \
3472             --pskpasswd psk-passwd.txt
3473 @end example
3475 Finally, we start the server with all the earlier parameters and you
3476 get this command:
3478 @example
3479 gnutls-serv --http \
3480             --x509cafile x509-ca.pem \
3481             --x509keyfile x509-server-key.pem \
3482             --x509certfile x509-server.pem \
3483             --x509dsakeyfile x509-server-key-dsa.pem \
3484             --x509dsacertfile x509-server-dsa.pem \
3485             --pgpkeyfile openpgp-server-key.txt \
3486             --pgpcertfile openpgp-server.txt \
3487             --srppasswdconf srp-tpasswd.conf \
3488             --srppasswd srp-passwd.txt \
3489             --pskpasswd psk-passwd.txt
3490 @end example
3492 @menu
3493 * Example server PSK connection::
3494 @end menu
3496 @node Example server PSK connection
3497 @subsection Example server PSK connection
3498 @cindex PSK server
3500 To set up a PSK server with @code{gnutls-serv} you need to create PSK
3501 password file (@pxref{Invoking psktool}).  In the example below, I
3502 type @code{password} at the prompt.
3504 @smallexample
3505 $ ./psktool -u psk_identity -p psks.txt -n psk_identity_hint
3506 Enter password:
3507 Key stored to psks.txt
3508 $ cat psks.txt
3509 psk_identity:88f3824b3e5659f52d00e959bacab954b6540344
3511 @end smallexample
3513 After this, start the server pointing to the password file.  We
3514 disable DHE-PSK.
3516 @smallexample
3517 $ ./gnutls-serv --pskpasswd psks.txt  --pskhint psk_identity_hint --priority NORMAL:-DHE-PSK
3518 Set static Diffie-Hellman parameters, consider --dhparams.
3519 Echo Server ready. Listening to port '5556'.
3520 @end smallexample
3522 You can now connect to the server using a PSK client (@pxref{Example
3523 client PSK connection}).
3525 @node Invoking psktool
3526 @section Invoking psktool
3527 @cindex psktool
3529 This is a program to manage @acronym{PSK} username and keys.
3531 @verbatim
3532 PSKtool help
3533 Usage : psktool [options]
3534      -u, --username username
3535                               specify username.
3536      -p, --passwd FILE        specify a password file.
3537      -n, --netconf-hint HINT
3538                               derive key from Netconf password, using 
3539                               HINT as the psk_identity_hint.
3540      -s, --keysize SIZE       specify the key size in bytes.
3541      -v, --version            prints the program's version number
3542      -h, --help               shows this help text
3543 @end verbatim
3545 Normally the file will generate random keys for the indicate username.
3546 You may also derive PSK keys from passwords, using the algorithm
3547 specified in @file{draft-ietf-netconf-tls-02.txt}.  The algorithm
3548 needs a PSK identity hint, which you specify using
3549 @code{--netconf-hint}.  To derive a PSK key from a password with an
3550 empty PSK identity hint, using @code{--netconf-hint ""}.
3552 @node Invoking srptool
3553 @section Invoking srptool
3554 @anchor{srptool}
3555 @cindex srptool
3557 The @file{srptool} is a very simple program that emulates the programs
3558 in the @emph{Stanford SRP libraries}, see
3559 @url{http://srp.stanford.edu/}.  It is intended for use in places
3560 where you don't expect @acronym{SRP} authentication to be the used for
3561 system users.
3563 Traditionally @emph{libsrp} used two files. One called @code{tpasswd}
3564 which holds usernames and verifiers, and @code{tpasswd.conf} which
3565 holds generators and primes.
3567 How to use srptool:
3569 @itemize
3571 @item
3572 To create tpasswd.conf which holds the g and n values for
3573 @acronym{SRP} protocol (generator and a large prime), run:
3575 @example
3576 $ srptool --create-conf /etc/tpasswd.conf
3577 @end example
3579 @item
3580 This command will create /etc/tpasswd and will add user 'test' (you
3581 will also be prompted for a password).  Verifiers are stored by
3582 default in the way libsrp expects.
3584 @example
3585 $ srptool --passwd /etc/tpasswd \
3586     --passwd-conf /etc/tpasswd.conf -u test
3587 @end example
3589 @item
3590 This command will check against a password.  If the password matches
3591 the one in /etc/tpasswd you will get an ok.
3593 @example
3594 $ srptool --passwd /etc/tpasswd \
3595     --passwd-conf /etc/tpasswd.conf --verify -u test
3596 @end example
3598 @end itemize
3600 @node Function reference
3601 @chapter Function Reference
3602 @cindex Function reference
3604 @menu
3605 * Core functions::
3606 * X.509 certificate functions::
3607 * GnuTLS-extra functions::
3608 * OpenPGP functions::
3609 * TLS Inner Application (TLS/IA) functions::
3610 * Error codes and descriptions::
3611 @end menu
3613 @node Core functions
3614 @section Core Functions
3616 The prototypes for the following functions lie in
3617 @file{gnutls/gnutls.h}.
3619 @include gnutls-api.texi
3621 @node X.509 certificate functions
3622 @section @acronym{X.509} Certificate Functions
3623 @anchor{sec:x509api}
3624 @cindex @acronym{X.509} Functions
3626 The following functions are to be used for @acronym{X.509} certificate handling.
3627 Their prototypes lie in @file{gnutls/x509.h}.
3629 @include x509-api.texi
3631 @node GnuTLS-extra functions
3632 @section @acronym{GnuTLS-extra} Functions
3633 @cindex @acronym{GnuTLS-extra} functions
3635 These functions are only available in the GPLv3+ version of the
3636 library called @code{gnutls-extra}. The prototypes for this library
3637 lie in @file{gnutls/extra.h}.
3639 @include extra-api.texi
3641 @node OpenPGP functions
3642 @section @acronym{OpenPGP} Functions
3643 @cindex @acronym{OpenPGP} functions
3644 @anchor{sec:openpgpapi}
3646 The following functions are to be used for @acronym{OpenPGP}
3647 certificate handling.  Their prototypes lie in
3648 @file{gnutls/openpgp.h}.
3650 @include pgp-api.texi
3652 @node TLS Inner Application (TLS/IA) functions
3653 @section @acronym{TLS} Inner Application (@acronym{TLS/IA}) Functions
3654 @cindex @acronym{TLS} Inner Application (@acronym{TLS/IA}) functions
3655 @cindex Inner Application (@acronym{TLS/IA}) functions
3657 The following functions are used for @acronym{TLS} Inner Application
3658 (@acronym{TLS/IA}).  Their prototypes lie in @file{gnutls/extra.h}.
3659 You need to link with @file{libgnutls-extra} to be able to use these
3660 functions (@pxref{GnuTLS-extra functions}).
3662 The typical control flow in an TLS/IA client (that would not require
3663 an Application Phase for resumed sessions) would be similar to the
3664 following:
3666 @example
3667 int client_avp (gnuls_session_t *session, void *ptr,
3668                 const char *last, size_t lastlen,
3669                 char **new, size_t *newlen)
3674 int main ()
3676   gnutls_ia_client_credentials_t iacred;
3678   gnutls_init (&session, GNUTLS_CLIENT);
3680   /* Enable TLS/IA. */
3681   gnutls_ia_allocate_client_credentials(&iacred);
3682   gnutls_ia_set_client_avp_function(iacred, client_avp);
3683   gnutls_credentials_set (session, GNUTLS_CRD_IA, iacred);
3685   ret = gnutls_handshake (session);
3686   // Error handling...
3688   if (gnutls_ia_handshake_p (session))
3689     @{
3690       ret = gnutls_ia_handshake (session);
3691       // Error handling...
3693 @end example
3695 See below for detailed descriptions of all the functions used above.
3697 The function @code{client_avp} would have to be implemented by your
3698 application.  The function is responsible for handling the AVP data.
3699 See @code{gnutls_ia_set_client_avp_function} below for more
3700 information on how that function should be implemented.
3702 The control flow in a typical server is similar to the above, use
3703 @code{gnutls_ia_server_credentials_t} instead of
3704 @code{gnutls_ia_client_credentials_t}, and replace the call to the
3705 client functions with the corresponding server functions.
3707 @include ia-api.texi
3709 @node Error codes and descriptions
3710 @section Error Codes and Descriptions
3711 @anchor{Error Codes}
3712 @cindex Error codes
3714 The error codes used throughout the library are described below.  The
3715 return code @code{GNUTLS_E_SUCCESS} indicate successful operation, and
3716 is guaranteed to have the value 0, so you can use it in logical
3717 expressions.
3719 @include error_codes.texi
3721 @node All the supported ciphersuites in GnuTLS
3722 @chapter All the Supported Ciphersuites in @acronym{GnuTLS}
3723 @anchor{ciphersuites}
3724 @cindex Ciphersuites
3726 @include algorithms.texi
3728 Some additional information regarding some of the algorithms:
3730 @table @code
3731 @item RSA
3732 RSA is public key cryptosystem designed by Ronald Rivest, Adi Shamir
3733 and Leonard Adleman.  It can be used with any hash functions.
3735 @item DSA
3736 DSA is the USA's Digital Signature Standard.  It uses only the SHA-1
3737 hash algorithm.
3739 @item MD2
3740 MD2 is a cryptographic hash algorithm designed by Ron Rivest.  It is
3741 optimized for 8-bit processors. Outputs 128 bits of data.  There are
3742 no known weaknesses of this algorithm but since this algorithm is
3743 rarely used and not really studied it should not be used today.
3745 @item MD5
3746 MD5 is a cryptographic hash algorithm designed by Ron Rivest. Outputs
3747 128 bits of data.  It is considered to be broken.
3749 @item SHA-1
3750 SHA is a cryptographic hash algorithm designed by NSA. Outputs 160
3751 bits of data.  It is also considered to be broken, though no practical
3752 attacks have been found.
3754 @item RMD160
3755 RIPEMD is a cryptographic hash algorithm developed in the framework of
3756 the EU project RIPE.  Outputs 160 bits of data.
3758 @end table
3761 @c Guile Bindings
3764 @include guile.texi
3767 @node Internal architecture of GnuTLS
3768 @chapter Internal Architecture of GnuTLS
3769 @cindex Internal architecture
3771 This chapter is to give a brief description of the
3772 way @acronym{GnuTLS} works. The focus is to give an idea
3773 to potential developers and those who want to know what
3774 happens inside the black box.
3776 @menu
3777 * The TLS Protocol::
3778 * TLS Handshake Protocol::
3779 * TLS Authentication Methods::
3780 * TLS Extension Handling::
3781 * Cryptographic Backend::
3782 @end menu
3784 @node The TLS Protocol
3785 @section The TLS Protocol
3786 The main needs for the TLS protocol to be used are
3787 shown in the image below.
3789 @image{gnutls-client-server-use-case,9cm}
3791 This is being accomplished by the following object diagram.
3792 Note that since @acronym{GnuTLS} is being developed in C
3793 object are just structures with attributes. The operations listed
3794 are functions that require the first parameter to be that object.
3795 @image{gnutls-objects,15cm}
3797 @node TLS Handshake Protocol
3798 @section TLS Handshake Protocol
3799 The @acronym{GnuTLS} handshake protocol is implemented as a state
3800 machine that waits for input or returns immediately when the non-blocking
3801 transport layer functions are used. The main idea is shown in the following
3802 figure.
3804 @image{gnutls-handshake-state,9cm}
3806 Also the way the input is processed varies per ciphersuite. Several 
3807 implementations of the internal handlers are available and 
3808 @ref{gnutls_handshake} only multiplexes the input to the appropriate 
3809 handler. For example a @acronym{PSK} ciphersuite has a different 
3810 implementation of the @code{process_client_key_exchange} than a
3811 certificate ciphersuite.
3813 @image{gnutls-handshake-sequence,12cm}
3815 @node TLS Authentication Methods
3816 @section TLS Authentication Methods
3817 In @acronym{GnuTLS} authentication methods can be implemented quite
3818 easily.  Since the required changes to add a new authentication method
3819 affect only the handshake protocol, a simple interface is used. An
3820 authentication method needs only to implement the functions as seen in
3821 the figure below.
3823 @image{gnutls-mod_auth_st,12cm}
3825 The functions that need to be implemented are the ones responsible for
3826 interpreting the handshake protocol messages. It is common for such
3827 functions to read data from one or more @code{credentials_t}
3828 structures@footnote{such as the
3829 @code{gnutls_certificate_credentials_t} structures} and write data,
3830 such as certificates, usernames etc. to @code{auth_info_t} structures.
3832 Simple examples of existing authentication methods can be seen in
3833 @code{auth_psk.c} for PSK ciphersuites and @code{auth_srp.c} for SRP
3834 ciphersuites. After implementing these functions the structure holding
3835 its pointers has to be registered in @code{gnutls_algorithms.c} in the
3836 @code{_gnutls_kx_algorithms} structure.
3838 @node TLS Extension Handling
3839 @section TLS Extension Handling
3840 As with authentication methods, the TLS extensions handlers can be
3841 implemented using the following interface.
3843 @image{gnutls-extensions_st,12cm}
3845 Here there are two functions, one for receiving the extension data
3846 and one for sending. These functions have to check internally whether
3847 they operate in client or server side. 
3849 A simple example of an extension handler can be seen in
3850 @code{ext_srp.c} After implementing these functions, together with the
3851 extension number they handle, they have to be registered in
3852 @code{gnutls_extensions.c} in the @code{_gnutls_extensions} structure.
3854 @subsection Adding a New TLS Extension
3856 Adding support for a new TLS extension is done from time to time, and
3857 the process to do so is not difficult.  Here are the steps you need to
3858 follow if you wish to do this yourself.  For sake of discussion, let's
3859 consider adding support for the hypothetical TLS extension
3860 @code{foobar}.
3862 @enumerate
3864 @item Add @code{configure} option like @code{--enable-foobar} or @code{--disable-foobar}.
3866 Which to chose depends on whether you intend to make the extension be
3867 enabled by default.  Look at existing checks (i.e., SRP, authz) for
3868 how to model the code.  For example:
3870 @example
3871 AC_MSG_CHECKING([whether to disable foobar support])
3872 AC_ARG_ENABLE(foobar,
3873         AS_HELP_STRING([--disable-foobar],
3874                 [disable foobar support]),
3875         ac_enable_foobar=no)
3876 if test x$ac_enable_foobar != xno; then
3877  AC_MSG_RESULT(no)
3878  AC_DEFINE(ENABLE_FOOBAR, 1, [enable foobar])
3879 else
3880  ac_full=0
3881  AC_MSG_RESULT(yes)
3883 AM_CONDITIONAL(ENABLE_FOOBAR, test "$ac_enable_foobar" != "no")
3884 @end example
3886 These lines should go in @code{lib/m4/hooks.m4}.
3888 @item Add IANA extension value to @code{extensions_t} in @code{gnutls_int.h}.
3890 A good name for the value would be GNUTLS_EXTENSION_FOOBAR.  Check
3891 with @url{http://www.iana.org/assignments/tls-extensiontype-values}
3892 for allocated values.  For experiments, you could pick a number but
3893 remember that some consider it a bad idea to deploy such modified
3894 version since it will lead to interoperability problems in the future
3895 when the IANA allocates that number to someone else, or when the
3896 foobar protocol is allocated another number.
3898 @item Add an entry to @code{_gnutls_extensions} in @code{gnutls_extensions.c}.
3900 A typical entry would be:
3902 @example
3903   int ret;
3905   /* ...
3906    */
3908 #if ENABLE_FOOBAR
3909   ret = gnutls_ext_register (GNUTLS_EXTENSION_FOOBAR,
3910                              "FOOBAR",
3911                              GNUTLS_EXT_TLS,
3912                              _gnutls_foobar_recv_params,
3913                              _gnutls_foobar_send_params);
3914   if (ret != GNUTLS_E_SUCCESS)
3915     return ret;
3916 #endif
3917 @end example
3919 The GNUTLS_EXTENSION_FOOBAR is the integer value you added to
3920 @code{gnutls_int.h} earlier.  The two functions are new functions that
3921 you will need to implement, most likely you'll need to add an
3922 @code{#include "ext_foobar.h"} as well.
3924 @item Add new files @code{ext_foobar.c} and @code{ext_foobar.h} that implements the extension.
3926 The functions you are responsible to add are those mentioned in the
3927 previous step.  As a starter, you could add this:
3929 @example
3931 _gnutls_foobar_recv_params (gnutls_session_t session,
3932                             const opaque * data,
3933                             size_t data_size)
3935   return 0;
3939 _gnutls_foobar_send_params (gnutls_session_t session,
3940                             opaque * data,
3941                             size_t _data_size)
3943   return 0;
3945 @end example
3947 The @code{_gnutls_foobar_recv_params} function is responsible for
3948 parsing incoming extension data (both in the client and server).
3950 The @code{_gnutls_foobar_send_params} function is responsible for
3951 sending extension data (both in the client and server).
3953 If you receive length fields that doesn't match, return
3954 @code{GNUTLS_E_UNEXPECTED_PACKET_LENGTH}.  If you receive invalid
3955 data, return @code{GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER}.  You can use
3956 other error codes too.  Return 0 on success.
3958 The function typically store some information in the @code{session}
3959 variable for later usage.  If you need to add new fields there, check
3960 @code{tls_ext_st} in @code{gnutls_int.h} and compare with existing TLS
3961 extension specific variables.
3963 Recall that both the client and server both send and receives
3964 parameters, and your code most likely will need to do different things
3965 depending on which mode it is in.  It may be useful to make this
3966 distinction explicit in the code.  Thus, for example, a better
3967 template than above would be:
3969 @example
3971 _gnutls_foobar_recv_params (gnutls_session_t session,
3972                             const opaque * data,
3973                             size_t data_size)
3975   if (session->security_parameters.entity == GNUTLS_CLIENT)
3976     return foobar_recv_client (session, data, data_size);
3977   else
3978     return foobar_recv_server (session, data, data_size);
3982 _gnutls_foobar_send_params (gnutls_session_t session,
3983                             opaque * data,
3984                             size_t data_size)
3986   if (session->security_parameters.entity == GNUTLS_CLIENT)
3987     return foobar_send_client (session, data, data_size);
3988   else
3989     return foobar_send_server (session, data, data_size);
3991 @end example
3993 The functions used would be declared as @code{static} functions, of
3994 the appropriate prototype, in the same file.
3996 When adding the files, you'll need to add them to @code{Makefile.am}
3997 as well, for example:
3999 @example
4000 if ENABLE_FOOBAR
4001 COBJECTS += ext_foobar.c
4002 HFILES += ext_foobar.h
4003 endif
4004 @end example
4006 @item Add API functions to enable/disable the extension.
4008 Normally the client will have one API to request use of the extension,
4009 and setting some extension specific data.  The server will have one
4010 API to let the library know that it is willing to accept the
4011 extension, often this is implemented through a callback but it doesn't
4012 have to.
4014 The APIs need to be added to @code{includes/gnutls/gnutls.h} or
4015 @code{includes/gnutls/extra.h} as appropriate.  It is recommended that
4016 if you don't have a requirement to use the LGPLv2.1+ license for your
4017 extension, that you place your work under the GPLv3+ license and thus
4018 in the libgnutls-extra library.
4020 You can implement the API function in the @code{ext_foobar.c} file, or
4021 if that file ends up becoming rather larger, add a
4022 @code{gnutls_foobar.c} file.
4024 @end enumerate
4026 @section Certificate Handling
4027 What is provided by the certificate handling functions
4028 is summarized in the following diagram.
4030 @image{gnutls-certificate-user-use-case,12cm}
4032 @node Cryptographic Backend
4033 @section Cryptographic Backend
4034 Several new systems provide hardware assisted cryptographic algorithm
4035 implementations that offer implementations some orders of magnitude
4036 faster than the software. For this reason GnuTLS supports by default
4037 the /dev/crypto device usually found in FreeBSD and OpenBSD system, to
4038 take advantage of installed hardware. 
4040 In addition it is possible to override parts of the crypto backend or the
4041 whole. It is possible to override them both at runtime and compile
4042 time, however here we will discuss the runtime possibility. The API
4043 available for this functionality is in @code{gnutls/crypto.h} header
4044 file.
4046 @subsection Override specific algorithms
4047 When an optimized implementation of a single algorithm is available,
4048 say a hardware assisted version of @acronym{AES-CBC} then the
4049 following functions can be used to register those algorithms.
4051 @itemize
4053 @item @ref{gnutls_crypto_single_cipher_register2}
4054 To register a cipher algorithm.
4056 @ref{gnutls_crypto_single_digest_register2}
4057 To register a hash (digest) or MAC algorithm.
4059 @end itemize
4061 Those registration functions will only replace the specified algorithm
4062 and leave the rest of subsystem intact.
4064 @subsection Override parts of the backend
4065 In some systems, such as embedded ones, it might be desirable to
4066 override big parts of the cryptographic backend, or even all of
4067 them. For this reason the following functions are provided.
4069 @itemize
4071 @item @ref{gnutls_crypto_cipher_register2}
4072 To override the cryptographic algorithms backend.
4074 @item @ref{gnutls_crypto_digest_register2}
4075 To override the digest algorithms backend.
4077 @item @ref{gnutls_crypto_rnd_register2}
4078 To override the random number generator backend.
4080 @item @ref{gnutls_crypto_bigint_register2}
4081 To override the big number number operations backend.
4083 @item @ref{gnutls_crypto_pk_register2}
4084 To override the public key encryption backend. This is tight to the
4085 big number operations so either both of them should be updated or care
4086 must be taken to use the same format.
4088 @end itemize
4090 If all of them are used then GnuTLS will no longer use libgcrypt.
4092 @node Copying Information
4093 @appendix Copying Information
4095 @menu
4096 * GNU Free Documentation License::   License for copying this manual.
4097 * GNU LGPL::                     License for copying the core GnuTLS library.
4098 * GNU GPL::                      License for copying GNUTLS extra and tools.
4099 @end menu
4101 @node GNU Free Documentation License
4102 @appendixsec GNU Free Documentation License
4104 @cindex FDL, GNU Free Documentation License
4106 @include fdl-1.3.texi
4108 @node GNU LGPL
4109 @appendixsec GNU Lesser General Public License
4110 @cindex LGPL, GNU Lesser General Public License
4111 @cindex License, GNU LGPL
4113 @include lgpl-2.1.texi
4115 @node GNU GPL
4116 @appendixsec GNU General Public License
4117 @cindex GPL, GNU General Public License
4118 @cindex License, GNU GPL
4120 @include gpl-3.0.texi
4122 @node Bibliography
4123 @unnumbered Bibliography
4125 @table @asis
4127 @item @anchor{CBCATT}[CBCATT]
4128 Bodo Moeller, "Security of CBC Ciphersuites in SSL/TLS: Problems and
4129 Countermeasures", 2002, available from
4130 @url{http://www.openssl.org/~bodo/tls-cbc.txt}.
4132 @item @anchor{GPGH}[GPGH]
4133 Mike Ashley, "The GNU Privacy Handbook", 2002, available from
4134 @url{http://www.gnupg.org/gph/en/manual.pdf}.
4136 @item @anchor{GUTPKI}[GUTPKI]
4137 Peter Gutmann, "Everything you never wanted to know about PKI but were
4138 forced to find out", Available from
4139 @url{http://www.cs.auckland.ac.nz/~pgut001/}.
4141 @item @anchor{NISTSP80057}[NISTSP80057]
4142 NIST Special Publication 800-57, "Recommendation for Key Management -
4143 Part 1: General (Revised)", March 2007, available from
4144 @url{http://csrc.nist.gov/publications/nistpubs/800-57/sp800-57-Part1-revised2_Mar08-2007.pdf}.
4146 @item @anchor{RFC2246}[RFC2246]
4147 Tim Dierks and Christopher Allen, "The TLS Protocol Version 1.0",
4148 January 1999, Available from
4149 @url{http://www.ietf.org/rfc/rfc2246.txt}.
4151 @item @anchor{RFC4346}[RFC4346]
4152 Tim Dierks and Eric Rescorla, "The TLS Protocol Version 1.1", Match
4153 2006, Available from @url{http://www.ietf.org/rfc/rfc4346.txt}.
4155 @item @anchor{RFC2440}[RFC2440]
4156 Jon Callas, Lutz Donnerhacke, Hal Finney and Rodney Thayer, "OpenPGP
4157 Message Format", November 1998, Available from
4158 @url{http://www.ietf.org/rfc/rfc2440.txt}.
4160 @item @anchor{RFC4880}[RFC4880]
4161 Jon Callas, Lutz Donnerhacke, Hal Finney, David Shaw and Rodney
4162 Thayer, "OpenPGP Message Format", November 2007, Available from
4163 @url{http://www.ietf.org/rfc/rfc4880.txt}.
4165 @item @anchor{RFC4211}[RFC4211]
4166 J. Schaad, "Internet X.509 Public Key Infrastructure Certificate
4167 Request Message Format (CRMF)", September 2005, Available from
4168 @url{http://www.ietf.org/rfc/rfc4211.txt}.
4170 @item @anchor{RFC2817}[RFC2817]
4171 Rohit Khare and Scott Lawrence, "Upgrading to TLS Within HTTP/1.1",
4172 May 2000, Available from @url{http://www.ietf.org/rfc/rfc2817.txt}
4174 @item @anchor{RFC2818}[RFC2818]
4175 Eric Rescorla, "HTTP Over TLS", May 2000, Available from
4176 @url{http://www.ietf/rfc/rfc2818.txt}.
4178 @item @anchor{RFC2945}[RFC2945]
4179 Tom Wu, "The SRP Authentication and Key Exchange System", September
4180 2000, Available from @url{http://www.ietf.org/rfc/rfc2945.txt}.
4182 @item @anchor{RFC2986}[RFC2986]
4183 Magnus Nystrom and Burt Kaliski, "PKCS 10 v1.7: Certification Request
4184 Syntax Specification", November 2000, Available from
4185 @url{http://www.ietf.org/rfc/rfc2986.txt}.
4187 @item @anchor{PKIX}[PKIX]
4188 D. Cooper, S. Santesson, S. Farrel, S. Boeyen, R. Housley, W. Polk,
4189 "Internet X.509 Public Key Infrastructure Certificate and Certificate
4190 Revocation List (CRL) Profile", May 2008, available from
4191 @url{http://www.ietf.org/rfc/rfc5280.txt}.
4193 @item @anchor{RFC3749}[RFC3749]
4194 Scott Hollenbeck, "Transport Layer Security Protocol Compression
4195 Methods", May 2004, available from
4196 @url{http://www.ietf.org/rfc/rfc3749.txt}.
4198 @item @anchor{RFC3820}[RFC3820]
4199 Steven Tuecke, Von Welch, Doug Engert, Laura Pearlman, and Mary
4200 Thompson, "Internet X.509 Public Key Infrastructure (PKI) Proxy
4201 Certificate Profile", June 2004, available from
4202 @url{http://www.ietf.org/rfc/rfc3820}.
4204 @item @anchor{TLSTKT}[TLSTKT]
4205 Joseph Salowey, Hao Zhou, Pasi Eronen, Hannes Tschofenig, "Transport
4206 Layer Security (TLS) Session Resumption without Server-Side State",
4207 January 2008, available from @url{http://www.ietf.org/rfc/rfc5077}.
4209 @item @anchor{PKCS12}[PKCS12]
4210 RSA Laboratories, "PKCS 12 v1.0: Personal Information Exchange
4211 Syntax", June 1999, Available from @url{http://www.rsa.com}.
4213 @item @anchor{RESCORLA}[RESCORLA]
4214 Eric Rescorla, "SSL and TLS: Designing and Building Secure Systems",
4215 2001
4217 @item @anchor{SELKEY}[SELKEY]
4218 Arjen Lenstra and Eric Verheul, "Selecting Cryptographic Key Sizes",
4219 2003, available from @url{http://www.win.tue.nl/~klenstra/key.pdf}.
4221 @item @anchor{SSL3}[SSL3]
4222 Alan Freier, Philip Karlton and Paul Kocher, "The SSL Protocol Version
4223 3.0", November 1996, Available from
4224 @url{http://wp.netscape.com/eng/ssl3/draft302.txt}.
4226 @item @anchor{STEVENS}[STEVENS]
4227 Richard Stevens, "UNIX Network Programming, Volume 1", Prentice Hall
4228 PTR, January 1998
4230 @item @anchor{TLSEXT}[TLSEXT]
4231 Simon Blake-Wilson, Magnus Nystrom, David Hopwood, Jan Mikkelsen and
4232 Tim Wright, "Transport Layer Security (TLS) Extensions", June 2003,
4233 Available from @url{http://www.ietf.org/rfc/rfc3546.txt}.
4235 @item @anchor{TLSPGP}[TLSPGP]
4236 Nikos Mavrogiannopoulos, "Using OpenPGP keys for TLS authentication",
4237 April 2004, November 2007. Available from
4238 @url{http://www.ietf.org/rfc/rfc5081.txt}.
4240 @item @anchor{TLSSRP}[TLSSRP]
4241 David Taylor, Trevor Perrin, Tom Wu and Nikos Mavrogiannopoulos,
4242 "Using SRP for TLS Authentication", November 2007. Available from
4243 @url{http://www.ietf.org/rfc/rfc5054.txt}.
4245 @item @anchor{TLSPSK}[TLSPSK]
4246 Pasi Eronen and Hannes Tschofenig, "Pre-shared key Ciphersuites for
4247 TLS", December 2005, Available from
4248 @url{http://www.ietf.org/rfc/rfc4279.txt}.
4250 @item @anchor{TOMSRP}[TOMSRP]
4251 Tom Wu, "The Stanford SRP Authentication Project", Available at
4252 @url{http://srp.stanford.edu/}.
4254 @item @anchor{WEGER}[WEGER]
4255 Arjen Lenstra and Xiaoyun Wang and Benne de Weger, "Colliding X.509
4256 Certificates", Cryptology ePrint Archive, Report 2005/067, Available
4257 at @url{http://eprint.iacr.org/}.
4259 @end table
4261 @node Function and Data Index
4262 @unnumbered Function and Data Index
4264 @printindex fn
4266 @node Concept Index
4267 @unnumbered Concept Index
4269 @printindex cp
4271 @bye