Tizen 2.0 Release
[external/libgnutls26.git] / doc / gnutls.info-1
1 This is gnutls.info, produced by makeinfo version 4.13.90 from
2 gnutls.texi.
3
4 This manual is last updated 6 January 2012 for version 2.12.20 of
5 GnuTLS.
6
7 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
8 Free Software Foundation, Inc.
9
10      Permission is granted to copy, distribute and/or modify this
11      document under the terms of the GNU Free Documentation License,
12      Version 1.3 or any later version published by the Free Software
13      Foundation; with no Invariant Sections, no Front-Cover Texts, and
14      no Back-Cover Texts.  A copy of the license is included in the
15      section entitled "GNU Free Documentation License".
16 INFO-DIR-SECTION Software libraries
17 START-INFO-DIR-ENTRY
18 * GnuTLS: (gnutls).             GNU Transport Layer Security Library.
19 END-INFO-DIR-ENTRY
20
21 INFO-DIR-SECTION System Administration
22 START-INFO-DIR-ENTRY
23 * certtool: (gnutls)Invoking certtool.  Manipulate certificates and keys.
24 * gnutls-serv: (gnutls)Invoking gnutls-serv.    GnuTLS test server.
25 * gnutls-cli: (gnutls)Invoking gnutls-cli.      GnuTLS test client.
26 * gnutls-cli-debug: (gnutls)Invoking gnutls-cli-debug.  GnuTLS debug client.
27 * psktool: (gnutls)Invoking psktool.    Simple TLS-Pre-Shared-Keys manager.
28 * srptool: (gnutls)Invoking srptool.    Simple SRP password tool.
29 END-INFO-DIR-ENTRY
30
31 \1f
32 File: gnutls.info,  Node: Top,  Next: Preface,  Up: (dir)
33
34 GnuTLS
35 ******
36
37 This manual is last updated 6 January 2012 for version 2.12.20 of
38 GnuTLS.
39
40 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
41 Free Software Foundation, Inc.
42
43      Permission is granted to copy, distribute and/or modify this
44      document under the terms of the GNU Free Documentation License,
45      Version 1.3 or any later version published by the Free Software
46      Foundation; with no Invariant Sections, no Front-Cover Texts, and
47      no Back-Cover Texts.  A copy of the license is included in the
48      section entitled "GNU Free Documentation License".
49
50 * Menu:
51
52 * Preface::
53 * The Library::
54 * Introduction to TLS::
55 * Authentication methods::
56 * More on certificate authentication::
57 * How to use TLS in application protocols::
58 * How to use GnuTLS in applications::
59 * Included programs::
60 * Function reference::
61 * All the supported ciphersuites in GnuTLS::
62 * Guile Bindings::
63 * Internal architecture of GnuTLS::
64 * Copying Information::
65 * Concept Index::
66 * Function and Data Index::
67 * Bibliography::
68
69 \1f
70 File: gnutls.info,  Node: Preface,  Next: The Library,  Prev: Top,  Up: Top
71
72 1 Preface
73 *********
74
75 This document tries to demonstrate and explain the GnuTLS library API. A
76 brief introduction to the protocols and the technology involved, is also
77 included so that an application programmer can better understand the
78 GnuTLS purpose and actual offerings.  Even if GnuTLS is a typical
79 library software, it operates over several security and cryptographic
80 protocols, which require the programmer to make careful and correct
81 usage of them, otherwise he risks to offer just a false sense of
82 security.  Security and the network security terms are very general
83 terms even for computer software thus cannot be easily restricted to a
84 single cryptographic library.  For that reason, do not consider a
85 program secure just because it uses GnuTLS; there are several ways to
86 compromise a program or a communication line and GnuTLS only helps with
87 some of them.
88
89 Although this document tries to be self contained, basic network
90 programming and PKI knowlegde is assumed in most of it.  A good
91 introduction to networking can be found in [STEVENS] (*note
92 Bibliography::) and for Public Key Infrastructure in [GUTPKI] (*note
93 Bibliography::).
94
95 Updated versions of the GnuTLS software and this document will be
96 available from 'http://www.gnutls.org/' and
97 'http://www.gnu.org/software/gnutls/'.
98
99 * Menu:
100
101 * Getting help::
102 * Commercial Support::
103 * Downloading and Installing::
104 * Bug Reports::
105 * Contributing::
106
107 \1f
108 File: gnutls.info,  Node: Getting help,  Next: Commercial Support,  Up: Preface
109
110 1.1 Getting Help
111 ================
112
113 A mailing list where users may help each other exists, and you can reach
114 it by sending e-mail to <help-gnutls@gnu.org>.  Archives of the mailing
115 list discussions, and an interface to manage subscriptions, is available
116 through the World Wide Web at
117 'http://lists.gnu.org/mailman/listinfo/help-gnutls'.
118
119 A mailing list for developers are also available, see
120 'http://www.gnu.org/software/gnutls/lists.html'.
121
122 Bug reports should be sent to <bug-gnutls@gnu.org>, see *Note Bug
123 Reports::.
124
125 \1f
126 File: gnutls.info,  Node: Commercial Support,  Next: Downloading and Installing,  Prev: Getting help,  Up: Preface
127
128 1.2 Commercial Support
129 ======================
130
131 Commercial support is available for users of GnuTLS. The kind of support
132 that can be purchased may include:
133
134    * Implement new features.  Such as a new TLS extension.
135
136    * Port GnuTLS to new platforms.  This could include porting to an
137      embedded platforms that may need memory or size optimization.
138
139    * Integrating TLS as a security environment in your existing project.
140
141    * System design of components related to TLS.
142
143 If you are interested, please write to:
144
145 Simon Josefsson Datakonsult
146 Hagagatan 24
147 113 47 Stockholm
148 Sweden
149
150 E-mail: simon@josefsson.org
151
152 If your company provides support related to GnuTLS and would like to be
153 mentioned here, contact the author (*note Bug Reports::).
154
155 \1f
156 File: gnutls.info,  Node: Downloading and Installing,  Next: Bug Reports,  Prev: Commercial Support,  Up: Preface
157
158 1.3 Downloading and Installing
159 ==============================
160
161 GnuTLS is available for download from the following URL:
162
163 'http://www.gnutls.org/download.html'
164
165 The latest version is stored in a file, e.g., 'gnutls-2.12.20.tar.gz'
166 where the '2.12.20' value is the highest version number in the
167 directory.
168
169 GnuTLS uses a Linux-like development cycle: even minor version numbers
170 indicate a stable release and a odd minor version number indicates a
171 development release.  For example, GnuTLS 1.6.3 denote a stable release
172 since 6 is even, and GnuTLS 1.7.11 denote a development release since 7
173 is odd.
174
175 GnuTLS depends on Libgcrypt, and you will need to install Libgcrypt
176 before installing GnuTLS. Libgcrypt is available from
177 'ftp://ftp.gnupg.org/gcrypt/libgcrypt'.  Libgcrypt needs another
178 library, libgpg-error, and you need to install libgpg-error before
179 installing Libgcrypt.  Libgpg-error is available from
180 'ftp://ftp.gnupg.org/gcrypt/libgpg-error'.
181
182 Don't forget to verify the cryptographic signature after downloading
183 source code packages.
184
185 The package is then extracted, configured and built like many other
186 packages that use Autoconf.  For detailed information on configuring and
187 building it, refer to the 'INSTALL' file that is part of the
188 distribution archive.  Typically you invoke './configure' and then 'make
189 check install'.  There are a number of compile-time parameters, as
190 discussed below.
191
192 The compression libraries (libz and lzo) are optional dependencies.  You
193 can get libz from 'http://www.zlib.net/'.  You can get lzo from
194 'http://www.oberhumer.com/opensource/lzo/'.
195
196 The X.509 part of GnuTLS needs ASN.1 functionality, from a library
197 called libtasn1.  A copy of libtasn1 is included in GnuTLS. If you want
198 to install it separately (e.g., to make it possibly to use libtasn1 in
199 other programs), you can get it from
200 'http://www.gnu.org/software/gnutls/download.html'.
201
202 The OpenPGP part of GnuTLS uses a stripped down version of OpenCDK for
203 parsing OpenPGP packets.  It is included GnuTLS. Use parameter
204 '--disable-openpgp-authentication' to disable the OpenPGP functionality
205 in GnuTLS. Unfortunately, we didn't have resources to maintain the code
206 in a separate library.
207
208 Regarding the Guile bindings, there are additional installation
209 considerations, see *Note Guile Preparations::.
210
211 A few 'configure' options may be relevant, summarized in the table.
212
213 '--disable-srp-authentication'
214 '--disable-psk-authentication'
215 '--disable-anon-authentication'
216 '--disable-extra-pki'
217 '--disable-openpgp-authentication'
218 '--disable-openssl-compatibility'
219      Disable or enable particular features.  Generally not recommended.
220
221 For the complete list, refer to the output from 'configure --help'.
222
223 \1f
224 File: gnutls.info,  Node: Bug Reports,  Next: Contributing,  Prev: Downloading and Installing,  Up: Preface
225
226 1.4 Bug Reports
227 ===============
228
229 If you think you have found a bug in GnuTLS, please investigate it and
230 report it.
231
232    * Please make sure that the bug is really in GnuTLS, and preferably
233      also check that it hasn't already been fixed in the latest version.
234
235    * You have to send us a test case that makes it possible for us to
236      reproduce the bug.
237
238    * You also have to explain what is wrong; if you get a crash, or if
239      the results printed are not good and in that case, in what way.
240      Make sure that the bug report includes all information you would
241      need to fix this kind of bug for someone else.
242
243 Please make an effort to produce a self-contained report, with something
244 definite that can be tested or debugged.  Vague queries or piecemeal
245 messages are difficult to act on and don't help the development effort.
246
247 If your bug report is good, we will do our best to help you to get a
248 corrected version of the software; if the bug report is poor, we won't
249 do anything about it (apart from asking you to send better bug reports).
250
251 If you think something in this manual is unclear, or downright
252 incorrect, or if the language needs to be improved, please also send a
253 note.
254
255 Send your bug report to:
256
257                          'bug-gnutls@gnu.org'
258
259 \1f
260 File: gnutls.info,  Node: Contributing,  Prev: Bug Reports,  Up: Preface
261
262 1.5 Contributing
263 ================
264
265 If you want to submit a patch for inclusion - from solve a typo you
266 discovered, up to adding support for a new feature - you should submit
267 it as a bug report (*note Bug Reports::).  There are some things that
268 you can do to increase the chances for it to be included in the official
269 package.
270
271 Unless your patch is very small (say, under 10 lines) we require that
272 you assign the copyright of your work to the Free Software Foundation.
273 This is to protect the freedom of the project.  If you have not already
274 signed papers, we will send you the necessary information when you
275 submit your contribution.
276
277 For contributions that doesn't consist of actual programming code, the
278 only guidelines are common sense.  Use it.
279
280 For code contributions, a number of style guides will help you:
281
282    * Coding Style.  Follow the GNU Standards document.
283
284      If you normally code using another coding standard, there is no
285      problem, but you should use 'indent' to reformat the code before
286      submitting your work.
287
288    * Use the unified diff format 'diff -u'.
289
290    * Return errors.  No reason whatsoever should abort the execution of
291      the library.  Even memory allocation errors, e.g.  when malloc
292      return NULL, should work although result in an error code.
293
294    * Design with thread safety in mind.  Don't use global variables.
295      Don't even write to per-handle global variables unless the
296      documented behaviour of the function you write is to write to the
297      per-handle global variable.
298
299    * Avoid using the C math library.  It causes problems for embedded
300      implementations, and in most situations it is very easy to avoid
301      using it.
302
303    * Document your functions.  Use comments before each function
304      headers, that, if properly formatted, are extracted into Texinfo
305      manuals and GTK-DOC web pages.
306
307    * Supply a ChangeLog and NEWS entries, where appropriate.
308
309 \1f
310 File: gnutls.info,  Node: The Library,  Next: Introduction to TLS,  Prev: Preface,  Up: Top
311
312 2 The Library
313 *************
314
315 In brief GnuTLS can be described as a library which offers an API to
316 access secure communication protocols.  These protocols provide privacy
317 over insecure lines, and were designed to prevent eavesdropping,
318 tampering, or message forgery.
319
320 Technically GnuTLS is a portable ANSI C based library which implements
321 the protocols ranging from SSL 3.0 to TLS 1.2 (*Note Introduction to
322 TLS::, for a more detailed description of the protocols), accompanied
323 with the required framework for authentication and public key
324 infrastructure.  Important features of the GnuTLS library include:
325
326    * Support for TLS 1.2, TLS 1.1, TLS 1.0 and SSL 3.0 protocols.
327
328    * Support for both X.509 and OpenPGP certificates.
329
330    * Support for handling and verification of certificates.
331
332    * Support for SRP for TLS authentication.
333
334    * Support for PSK for TLS authentication.
335
336    * Support for TLS Extension mechanism.
337
338    * Support for TLS Compression Methods.
339
340 Additionally GnuTLS provides a limited emulation API for the widely used
341 OpenSSL(1) library, to ease integration with existing applications.
342
343 GnuTLS consists of three independent parts, namely the "TLS protocol
344 part", the "Certificate part", and the "Cryptographic backend" part.
345 The 'TLS protocol part' is the actual protocol implementation, and is
346 entirely implemented within the GnuTLS library.  The 'Certificate part'
347 consists of the certificate parsing, and verification functions which is
348 partially implemented in the GnuTLS library.  The Libtasn1(2), a library
349 which offers ASN.1 parsing capabilities, is used for the X.509
350 certificate parsing functions.  A smaller version of OpenCDK(3) is used
351 for the OpenPGP key support in GnuTLS.  The "Cryptographic backend" is
352 provided by the Libgcrypt(4) library(5).
353
354 In order to ease integration in embedded systems, parts of the GnuTLS
355 library can be disabled at compile time.  That way a small library, with
356 the required features, can be generated.
357
358 * Menu:
359
360 * General Idea::
361 * Error handling::
362 * Memory handling::
363 * Callback functions::
364
365    ---------- Footnotes ----------
366
367    (1) 'http://www.openssl.org/'
368
369    (2) 'ftp://ftp.gnupg.org/gcrypt/alpha/gnutls/libtasn1/'
370
371    (3) 'ftp://ftp.gnupg.org/gcrypt/alpha/gnutls/opencdk/'
372
373    (4) 'ftp://ftp.gnupg.org/gcrypt/alpha/libgcrypt/'
374
375    (5) On current versions of GnuTLS it is possible to override the
376 default crypto backend.  Check *note Cryptographic Backend:: for details
377
378 \1f
379 File: gnutls.info,  Node: General Idea,  Next: Error handling,  Up: The Library
380
381 2.1 General Idea
382 ================
383
384 A brief description of how GnuTLS works internally is shown at the
385 figure below.  This section may be easier to understand after having
386 seen the examples (*note examples::).
387
388 \0\b[image src="gnutls-internals.png"\0\b]
389
390 As shown in the figure, there is a read-only global state that is
391 initialized once by the global initialization function.  This global
392 structure, among others, contains the memory allocation functions used,
393 and some structures needed for the ASN.1 parser.  This structure is
394 never modified by any GnuTLS function, except for the deinitialization
395 function which frees all memory allocated in the global structure and is
396 called after the program has permanently finished using GnuTLS.
397
398 The credentials structure is used by some authentication methods, such
399 as certificate authentication (*note Certificate Authentication::).  A
400 credentials structure may contain certificates, private keys, temporary
401 parameters for Diffie-Hellman or RSA key exchange, and other stuff that
402 may be shared between several TLS sessions.
403
404 This structure should be initialized using the appropriate
405 initialization functions.  For example an application which uses
406 certificate authentication would probably initialize the credentials,
407 using the appropriate functions, and put its trusted certificates in
408 this structure.  The next step is to associate the credentials structure
409 with each TLS session.
410
411 A GnuTLS session contains all the required stuff for a session to handle
412 one secure connection.  This session calls directly to the transport
413 layer functions, in order to communicate with the peer.  Every session
414 has a unique session ID shared with the peer.
415
416 Since TLS sessions can be resumed, servers would probably need a
417 database backend to hold the session's parameters.  Every GnuTLS session
418 after a successful handshake calls the appropriate backend function
419 (*Note resume::, for information on initialization) to store the newly
420 negotiated session.  The session database is examined by the server just
421 after having received the client hello(1), and if the session ID sent by
422 the client, matches a stored session, the stored session will be
423 retrieved, and the new session will be a resumed one, and will share the
424 same session ID with the previous one.
425
426    ---------- Footnotes ----------
427
428    (1) The first message in a TLS handshake
429
430 \1f
431 File: gnutls.info,  Node: Error handling,  Next: Memory handling,  Prev: General Idea,  Up: The Library
432
433 2.2 Error Handling
434 ==================
435
436 In GnuTLS most functions return an integer type as a result.  In almost
437 all cases a zero or a positive number means success, and a negative
438 number indicates failure, or a situation that some action has to be
439 taken.  Thus negative error codes may be fatal or not.
440
441 Fatal errors terminate the connection immediately and further sends and
442 receives will be disallowed.  An example of a fatal error code is
443 'GNUTLS_E_DECRYPTION_FAILED'.  Non-fatal errors may warn about
444 something, i.e., a warning alert was received, or indicate the some
445 action has to be taken.  This is the case with the error code
446 'GNUTLS_E_REHANDSHAKE' returned by *note gnutls_record_recv::.  This
447 error code indicates that the server requests a re-handshake.  The
448 client may ignore this request, or may reply with an alert.  You can
449 test if an error code is a fatal one by using the *note
450 gnutls_error_is_fatal::.
451
452 If any non fatal errors, that require an action, are to be returned by a
453 function, these error codes will be documented in the function's
454 reference.  *Note Error Codes::, for all the error codes.
455
456 \1f
457 File: gnutls.info,  Node: Memory handling,  Next: Callback functions,  Prev: Error handling,  Up: The Library
458
459 2.3 Memory Handling
460 ===================
461
462 GnuTLS internally handles heap allocated objects differently, depending
463 on the sensitivity of the data they contain.  However for performance
464 reasons, the default memory functions do not overwrite sensitive data
465 from memory, nor protect such objects from being written to the swap.
466 In order to change the default behavior the *note
467 gnutls_global_set_mem_functions:: function is available which can be
468 used to set other memory handlers than the defaults.
469
470 The Libgcrypt library on which GnuTLS depends, has such secure memory
471 allocation functions available.  These should be used in cases where
472 even the system's swap memory is not considered secure.  See the
473 documentation of Libgcrypt for more information.
474
475 \1f
476 File: gnutls.info,  Node: Callback functions,  Prev: Memory handling,  Up: The Library
477
478 2.4 Callback Functions
479 ======================
480
481 There are several cases where GnuTLS may need some out of band input
482 from your program.  This is now implemented using some callback
483 functions, which your program is expected to register.
484
485 An example of this type of functions are the push and pull callbacks
486 which are used to specify the functions that will retrieve and send data
487 to the transport layer.
488
489    * *note gnutls_transport_set_push_function::
490
491    * *note gnutls_transport_set_pull_function::
492
493 Other callback functions such as the one set by *note
494 gnutls_srp_set_server_credentials_function::, may require more
495 complicated input, including data to be allocated.  These callbacks
496 should allocate and free memory using the functions shown below.
497
498    * *note gnutls_malloc::
499
500    * *note gnutls_free::
501
502 \1f
503 File: gnutls.info,  Node: Introduction to TLS,  Next: Authentication methods,  Prev: The Library,  Up: Top
504
505 3 Introduction to TLS
506 *********************
507
508 TLS stands for "Transport Layer Security" and is the successor of SSL,
509 the Secure Sockets Layer protocol [SSL3] (*note Bibliography::) designed
510 by Netscape.  TLS is an Internet protocol, defined by IETF(1), described
511 in RFC 4346 and also in [RESCORLA] (*note Bibliography::).  The protocol
512 provides confidentiality, and authentication layers over any reliable
513 transport layer.  The description, below, refers to TLS 1.0 but also
514 applies to TLS 1.1 [RFC4346] (*note Bibliography::) and SSL 3.0, since
515 the differences of these protocols are minor.  Older protocols such as
516 SSL 2.0 are not discussed nor implemented in GnuTLS since they are not
517 considered secure today.  GnuTLS also supports X.509 and OpenPGP
518 [RFC4880] (*note Bibliography::).
519
520 * Menu:
521
522 * TLS layers::
523 * The transport layer::
524 * The TLS record protocol::
525 * The TLS Alert Protocol::
526 * The TLS Handshake Protocol::
527 * TLS Extensions::
528 * Selecting cryptographic key sizes::
529 * On SSL 2 and older protocols::
530
531    ---------- Footnotes ----------
532
533    (1) IETF, or Internet Engineering Task Force, is a large open
534 international community of network designers, operators, vendors, and
535 researchers concerned with the evolution of the Internet architecture
536 and the smooth operation of the Internet.  It is open to any interested
537 individual.
538
539 \1f
540 File: gnutls.info,  Node: TLS layers,  Next: The transport layer,  Up: Introduction to TLS
541
542 3.1 TLS Layers
543 ==============
544
545 TLS is a layered protocol, and consists of the Record Protocol, the
546 Handshake Protocol and the Alert Protocol.  The Record Protocol is to
547 serve all other protocols and is above the transport layer.  The Record
548 protocol offers symmetric encryption, data authenticity, and optionally
549 compression.
550
551 The Alert protocol offers some signaling to the other protocols.  It can
552 help informing the peer for the cause of failures and other error
553 conditions.  *Note The Alert Protocol::, for more information.  The
554 alert protocol is above the record protocol.
555
556 The Handshake protocol is responsible for the security parameters'
557 negotiation, the initial key exchange and authentication.  *Note The
558 Handshake Protocol::, for more information about the handshake protocol.
559 The protocol layering in TLS is shown in the figure below.
560
561 \0\b[image src="gnutls-layers.png"\0\b]
562
563 \1f
564 File: gnutls.info,  Node: The transport layer,  Next: The TLS record protocol,  Prev: TLS layers,  Up: Introduction to TLS
565
566 3.2 The Transport Layer
567 =======================
568
569 TLS is not limited to one transport layer, it can be used above any
570 transport layer, as long as it is a reliable one.  A set of functions is
571 provided and their purpose is to load to GnuTLS the required callbacks
572 to access the transport layer.
573
574    * *note gnutls_transport_set_push_function::
575    * *note gnutls_transport_set_vec_push_function::
576    * *note gnutls_transport_set_pull_function::
577    * *note gnutls_transport_set_ptr::
578    * *note gnutls_transport_set_lowat::
579    * *note gnutls_transport_set_errno::
580
581 These functions accept a callback function as a parameter.  The callback
582 functions should return the number of bytes written, or -1 on error and
583 should set 'errno' appropriately.
584
585 In some environments, setting 'errno' is unreliable, for example Windows
586 have several errno variables in different CRTs, or it may be that errno
587 is not a thread-local variable.  If this is a concern to you, call
588 'gnutls_transport_set_errno' with the intended errno value instead of
589 setting 'errno' directly.
590
591 GnuTLS currently only interprets the EINTR and EAGAIN errno values and
592 returns the corresponding GnuTLS error codes 'GNUTLS_E_INTERRUPTED' and
593 'GNUTLS_E_AGAIN'.  These values are usually returned by interrupted
594 system calls, or when non blocking IO is used.  All GnuTLS functions can
595 be resumed (called again), if any of these error codes is returned.  The
596 error codes above refer to the system call, not the GnuTLS function,
597 since signals do not interrupt GnuTLS' functions.
598
599 For non blocking sockets or other custom made pull/push functions the
600 *note gnutls_transport_set_lowat:: must be called, with a zero low water
601 mark value.
602
603 By default, if the transport functions are not set, GnuTLS will use the
604 Berkeley Sockets functions.
605
606 \1f
607 File: gnutls.info,  Node: The TLS record protocol,  Next: The TLS Alert Protocol,  Prev: The transport layer,  Up: Introduction to TLS
608
609 3.3 The TLS Record Protocol
610 ===========================
611
612 The Record protocol is the secure communications provider.  Its purpose
613 is to encrypt, authenticate and --optionally-- compress packets.  The
614 following functions are available:
615
616 *note gnutls_record_send:::
617      To send a record packet (with application data).
618
619 *note gnutls_record_recv:::
620      To receive a record packet (with application data).
621
622 *note gnutls_record_get_direction:::
623      To get the direction of the last interrupted function call.
624
625 As you may have already noticed, the functions which access the Record
626 protocol, are quite limited, given the importance of this protocol in
627 TLS.  This is because the Record protocol's parameters are all set by
628 the Handshake protocol.
629
630 The Record protocol initially starts with NULL parameters, which means
631 no encryption, and no MAC is used.  Encryption and authentication begin
632 just after the handshake protocol has finished.
633
634 * Menu:
635
636 * Encryption algorithms used in the record layer::
637 * Compression algorithms used in the record layer::
638 * Weaknesses and countermeasures::
639 * On Record Padding::
640
641 \1f
642 File: gnutls.info,  Node: Encryption algorithms used in the record layer,  Next: Compression algorithms used in the record layer,  Up: The TLS record protocol
643
644 3.3.1 Encryption Algorithms Used in the Record Layer
645 ----------------------------------------------------
646
647 Confidentiality in the record layer is achieved by using symmetric block
648 encryption algorithms like '3DES', 'AES'(1), or stream algorithms like
649 'ARCFOUR_128'(2).  Ciphers are encryption algorithms that use a single,
650 secret, key to encrypt and decrypt data.  Block algorithms in TLS also
651 provide protection against statistical analysis of the data.  Thus, if
652 you're using the TLS protocol, a random number of blocks will be
653 appended to data, to prevent eavesdroppers from guessing the actual data
654 size.
655
656 Supported cipher algorithms:
657
658 '3DES_CBC'
659      '3DES_CBC' is the DES block cipher algorithm used with triple
660      encryption (EDE). Has 64 bits block size and is used in CBC mode.
661
662 'ARCFOUR_128'
663      ARCFOUR is a fast stream cipher.
664
665 'ARCFOUR_40'
666      This is the ARCFOUR cipher that is fed with a 40 bit key, which is
667      considered weak.
668
669 'AES_CBC'
670      AES or RIJNDAEL is the block cipher algorithm that replaces the old
671      DES algorithm.  Has 128 bits block size and is used in CBC mode.
672
673 Supported MAC algorithms:
674
675 'MAC_MD5'
676      MD5 is a cryptographic hash algorithm designed by Ron Rivest.
677      Outputs 128 bits of data.
678
679 'MAC_SHA'
680      SHA is a cryptographic hash algorithm designed by NSA. Outputs 160
681      bits of data.
682
683    ---------- Footnotes ----------
684
685    (1) AES, or Advanced Encryption Standard, is actually the RIJNDAEL
686 algorithm.  This is the algorithm that replaced DES.
687
688    (2) 'ARCFOUR_128' is a compatible algorithm with RSA's RC4 algorithm,
689 which is considered to be a trade secret.
690
691 \1f
692 File: gnutls.info,  Node: Compression algorithms used in the record layer,  Next: Weaknesses and countermeasures,  Prev: Encryption algorithms used in the record layer,  Up: The TLS record protocol
693
694 3.3.2 Compression Algorithms Used in the Record Layer
695 -----------------------------------------------------
696
697 The TLS record layer also supports compression.  The algorithms
698 implemented in GnuTLS can be found in the table below.  All the
699 algorithms except for DEFLATE which is referenced in [RFC3749] (*note
700 Bibliography::), should be considered as GnuTLS' extensions(1), and
701 should be advertised only when the peer is known to have a compliant
702 client, to avoid interoperability problems.
703
704 The included algorithms perform really good when text, or other
705 compressible data are to be transfered, but offer nothing on already
706 compressed data, such as compressed images, zipped archives etc.  These
707 compression algorithms, may be useful in high bandwidth TLS tunnels, and
708 in cases where network usage has to be minimized.  As a drawback,
709 compression increases latency.
710
711 The record layer compression in GnuTLS is implemented based on the
712 proposal [RFC3749] (*note Bibliography::).  The supported compression
713 algorithms are:
714
715 'DEFLATE'
716      Zlib compression, using the deflate algorithm.
717
718 'LZO'
719      LZO is a very fast compression algorithm.  This algorithm is only
720      available if the GnuTLS-extra library has been initialized and the
721      private extensions are enabled, and if GnuTLS was built with LZO
722      support.
723
724    ---------- Footnotes ----------
725
726    (1) You should use *note gnutls_handshake_set_private_extensions:: to
727 enable private extensions.
728
729 \1f
730 File: gnutls.info,  Node: Weaknesses and countermeasures,  Next: On Record Padding,  Prev: Compression algorithms used in the record layer,  Up: The TLS record protocol
731
732 3.3.3 Weaknesses and Countermeasures
733 ------------------------------------
734
735 Some weaknesses that may affect the security of the Record layer have
736 been found in TLS 1.0 protocol.  These weaknesses can be exploited by
737 active attackers, and exploit the facts that
738
739   1. TLS has separate alerts for "decryption_failed" and
740      "bad_record_mac"
741
742   2. The decryption failure reason can be detected by timing the
743      response time.
744
745   3. The IV for CBC encrypted packets is the last block of the previous
746      encrypted packet.
747
748 Those weaknesses were solved in TLS 1.1 [RFC4346] (*note Bibliography::)
749 which is implemented in GnuTLS.  For a detailed discussion see the
750 archives of the TLS Working Group mailing list and the paper [CBCATT]
751 (*note Bibliography::).
752
753 \1f
754 File: gnutls.info,  Node: On Record Padding,  Prev: Weaknesses and countermeasures,  Up: The TLS record protocol
755
756 3.3.4 On Record Padding
757 -----------------------
758
759 The TLS protocol allows for random padding of records, to make it more
760 difficult to perform analysis on the length of exchanged messages (RFC
761 5246 6.2.3.2).  GnuTLS appears to be one of few implementation that take
762 advantage of this text, and pad records by a random length.
763
764 The TLS implementation in the Symbian operating system, frequently used
765 by Nokia and Sony-Ericsson mobile phones, cannot handle non-minimal
766 record padding.  What happens when one of these clients handshake with a
767 GnuTLS server is that the client will fail to compute the correct MAC
768 for the record.  The client sends a TLS alert ('bad_record_mac') and
769 disconnects.  Typically this will result in error messages such as 'A
770 TLS fatal alert has been received', 'Bad record MAC', or both, on the
771 GnuTLS server side.
772
773 GnuTLS implements a work around for this problem.  However, it has to be
774 enabled specifically.  It can be enabled by using *note
775 gnutls_record_disable_padding::, or *note gnutls_priority_set:: with the
776 '%COMPAT' priority string.
777
778 If you implement an application that have a configuration file, we
779 recommend that you make it possible for users or administrators to
780 specify a GnuTLS protocol priority string, which is used by your
781 application via *note gnutls_priority_set::.  To allow the best
782 flexibility, make it possible to have a different priority string for
783 different incoming IP addresses.
784
785 To enable the workaround in the 'gnutls-cli' client or the 'gnutls-serv'
786 server, for testing of other implementations, use the following
787 parameter: '--priority "NORMAL:%COMPAT"'.
788
789 \1f
790 File: gnutls.info,  Node: The TLS Alert Protocol,  Next: The TLS Handshake Protocol,  Prev: The TLS record protocol,  Up: Introduction to TLS
791
792 3.4 The TLS Alert Protocol
793 ==========================
794
795 The Alert protocol is there to allow signals to be sent between peers.
796 These signals are mostly used to inform the peer about the cause of a
797 protocol failure.  Some of these signals are used internally by the
798 protocol and the application protocol does not have to cope with them
799 (see 'GNUTLS_A_CLOSE_NOTIFY'), and others refer to the application
800 protocol solely (see 'GNUTLS_A_USER_CANCELLED').  An alert signal
801 includes a level indication which may be either fatal or warning.  Fatal
802 alerts always terminate the current connection, and prevent future
803 renegotiations using the current session ID.
804
805 The alert messages are protected by the record protocol, thus the
806 information that is included does not leak.  You must take extreme care
807 for the alert information not to leak to a possible attacker, via public
808 log files etc.
809
810 *note gnutls_alert_send:::
811      To send an alert signal.
812
813 *note gnutls_error_to_alert:::
814      To map a gnutls error number to an alert signal.
815
816 *note gnutls_alert_get:::
817      Returns the last received alert.
818
819 *note gnutls_alert_get_name:::
820      Returns the name, in a character array, of the given alert.
821
822 \1f
823 File: gnutls.info,  Node: The TLS Handshake Protocol,  Next: TLS Extensions,  Prev: The TLS Alert Protocol,  Up: Introduction to TLS
824
825 3.5 The TLS Handshake Protocol
826 ==============================
827
828 The Handshake protocol is responsible for the ciphersuite negotiation,
829 the initial key exchange, and the authentication of the two peers.  This
830 is fully controlled by the application layer, thus your program has to
831 set up the required parameters.  Available functions to control the
832 handshake protocol include:
833
834 *note gnutls_priority_init:::
835      To initialize a priority set of ciphers.
836
837 *note gnutls_priority_deinit:::
838      To deinitialize a priority set of ciphers.
839
840 *note gnutls_priority_set:::
841      To associate a priority set with a TLS session.
842
843 *note gnutls_priority_set_direct:::
844      To directly associate a session with a given priority string.
845
846 *note gnutls_credentials_set:::
847      To set the appropriate credentials structures.
848
849 *note gnutls_certificate_server_set_request:::
850      To set whether client certificate is required or not.
851
852 *note gnutls_handshake:::
853      To initiate the handshake.
854
855 * Menu:
856
857 * TLS Cipher Suites::           TLS session parameters.
858 * Priority Strings::            Defining how parameters are negotiated.
859 * Client Authentication::       Requesting a certificate from the client.
860 * Resuming Sessions::           Reusing previously established keys.
861 * Resuming Internals::          More information on reusing previously established keys.
862 * Interoperability::            About interoperability with other implementations.
863
864 \1f
865 File: gnutls.info,  Node: TLS Cipher Suites,  Next: Priority Strings,  Up: The TLS Handshake Protocol
866
867 3.5.1 TLS Cipher Suites
868 -----------------------
869
870 The Handshake Protocol of TLS negotiates cipher suites of the form
871 'TLS_DHE_RSA_WITH_3DES_CBC_SHA'.  The usual cipher suites contain these
872 parameters:
873
874    * The key exchange algorithm.  'DHE_RSA' in the example.
875
876    * The Symmetric encryption algorithm and mode '3DES_CBC' in this
877      example.
878
879    * The MAC(1) algorithm used for authentication.  'MAC_SHA' is used in
880      the above example.
881
882 The cipher suite negotiated in the handshake protocol will affect the
883 Record Protocol, by enabling encryption and data authentication.  Note
884 that you should not over rely on TLS to negotiate the strongest
885 available cipher suite.  Do not enable ciphers and algorithms that you
886 consider weak.
887
888 All the supported ciphersuites are shown in *note ciphersuites::.
889
890    ---------- Footnotes ----------
891
892    (1) MAC stands for Message Authentication Code.  It can be described
893 as a keyed hash algorithm.  See RFC2104.
894
895 \1f
896 File: gnutls.info,  Node: Priority Strings,  Next: Client Authentication,  Prev: TLS Cipher Suites,  Up: The TLS Handshake Protocol
897
898 3.5.2 Priority Strings
899 ----------------------
900
901 In order to specify cipher suite preferences, the previously shown
902 priority functions accept a string that specifies the algorithms to be
903 enabled in a TLS handshake.  That string may contain some high level
904 keyword such as:
905
906 PERFORMANCE:
907      All the "secure" ciphersuites are enabled, limited to 128 bit
908      ciphers and sorted by terms of speed performance.
909
910 NORMAL:
911      Means all "secure" ciphersuites.  The 256-bit ciphers are included
912      as a fallback only.  The ciphers are sorted by security margin.
913
914 SECURE128:
915      Means all "secure" ciphersuites with ciphers up to 128 bits, sorted
916      by security margin.
917
918 SECURE256:
919      Means all "secure" ciphersuites including the 256 bit ciphers,
920      sorted by security margin.
921
922 EXPORT:
923      Means all ciphersuites are enabled, including the low-security 40
924      bit ciphers.
925
926 NONE:
927      Means nothing is enabled.  This disables even protocols and
928      compression methods.  It should be followed by the algorithms to be
929      enabled.
930
931 or it might contain special keywords, that will be explained later on.
932
933 Unless the first keyword is "NONE" the defaults (in preference order)
934 are for TLS protocols TLS 1.2, TLS1.1, TLS1.0, SSL3.0; for compression
935 NULL; for certificate types X.509, OpenPGP. For key exchange algorithms
936 when in NORMAL or SECURE levels the perfect forward secrecy algorithms
937 take precedence of the other protocols.  In all cases all the supported
938 key exchange algorithms are enabled (except for the RSA-EXPORT which is
939 only enabled in EXPORT level).
940
941 The NONE keyword is followed by the algorithms to be enabled, and is
942 used to provide the exact list of requested algorithms(1).  The order
943 with which every algorithm is specified is significant.  Similar
944 algorithms specified before others will take precedence.
945
946 Keywords prepended to individual algorithms:
947
948 '!' or '-'
949      appended with an algorithm will remove this algorithm.
950
951 "+"
952      appended with an algorithm will add this algorithm.
953
954 Individual algorithms:
955 Ciphers:
956      AES-128-CBC, AES-256-CBC, CAMELLIA-128-CBC, CAMELLIA-256-CBC,
957      ARCFOUR-128, 3DES-CBC ARCFOUR-40.  Catch all name is CIPHER-ALL
958      which will add all the algorithms from NORMAL priority.
959
960 Key exchange:
961      RSA, DHE-RSA, DHE-DSS, SRP, SRP-RSA, SRP-DSS, PSK, DHE-PSK,
962      ANON-DH, RSA-EXPORT. The key exchange methods do not have a catch
963      all.
964
965 MAC:
966      MD5, SHA1, SHA256.  All algorithms from NORMAL priority can be
967      accessed with MAC-ALL.
968
969 Compression algorithms:
970      COMP-NULL, COMP-DEFLATE. Catch all is COMP-ALL.
971
972 TLS versions:
973      VERS-SSL3.0, VERS-TLS1.0, VERS-TLS1.1, VERS-TLS1.2.  Catch all is
974      VERS-TLS-ALL.
975
976 Signature algorithms:
977      SIGN-RSA-SHA1, SIGN-RSA-SHA224, SIGN-RSA-SHA256, SIGN-RSA-SHA384,
978      SIGN-RSA-SHA512, SIGN-DSA-SHA1, SIGN-DSA-SHA224, SIGN-DSA-SHA256,
979      SIGN-RSA-MD5.  Catch all is SIGN-ALL. This is only valid for TLS
980      1.2 and later.
981
982 Special keywords:
983
984 %COMPAT:
985      will enable compatibility mode.  It might mean that violations of
986      the protocols are allowed as long as maximum compatibility with
987      problematic clients and servers is achieved.
988
989 %DISABLE_SAFE_RENEGOTIATION:
990      will disable safe renegotiation completely.  Do not use unless you
991      know what you are doing.  Testing purposes only.
992
993 %UNSAFE_RENEGOTIATION:
994      will allow handshakes and rehandshakes without the safe
995      renegotiation extension.  Note that for clients this mode is
996      insecure (you may be under attack), and for servers it will allow
997      insecure clients to connect (which could be fooled by an attacker).
998      Do not use unless you know what you are doing and want maximum
999      compatibility.
1000
1001 %PARTIAL_RENEGOTIATION:
1002      will allow initial handshakes to proceed, but not rehandshakes.
1003      This leaves the client vulnerable to attack, and servers will be
1004      compatible with non-upgraded clients for initial handshakes.  This
1005      is currently the default for clients and servers, for compatibility
1006      reasons.
1007
1008 %SAFE_RENEGOTIATION:
1009      will enforce safe renegotiation.  Clients and servers will refuse
1010      to talk to an insecure peer.  Currently this causes operability
1011      problems, but is required for full protection.
1012
1013 %SSL3_RECORD_VERSION:
1014      will use SSL3.0 record version in client hello.  This is the
1015      default.
1016
1017 %LATEST_RECORD_VERSION:
1018      will use the latest TLS version record version in client hello.
1019
1020 %VERIFY_ALLOW_SIGN_RSA_MD5:
1021      will allow RSA-MD5 signatures in certificate chains.
1022
1023 %VERIFY_ALLOW_X509_V1_CA_CRT:
1024      will allow V1 CAs in chains.
1025
1026    ---------- Footnotes ----------
1027
1028    (1) To avoid collisions in order to specify a compression algorithm
1029 in this string you have to prefix it with "COMP-", protocol versions
1030 with "VERS-", signature algorithms with "SIGN-" and certificate types
1031 with "CTYPE-".  All other algorithms don't need a prefix.
1032
1033 \1f
1034 File: gnutls.info,  Node: Client Authentication,  Next: Resuming Sessions,  Prev: Priority Strings,  Up: The TLS Handshake Protocol
1035
1036 3.5.3 Client Authentication
1037 ---------------------------
1038
1039 In the case of ciphersuites that use certificate authentication, the
1040 authentication of the client is optional in TLS.  A server may request a
1041 certificate from the client -- using the *note
1042 gnutls_certificate_server_set_request:: function.  If a certificate is
1043 to be requested from the client during the handshake, the server will
1044 send a certificate request message that contains a list of acceptable
1045 certificate signers.  In GnuTLS the certificate signers list is
1046 constructed using the trusted Certificate Authorities by the server.
1047 That is the ones set using
1048    * *note gnutls_certificate_set_x509_trust_file::
1049    * *note gnutls_certificate_set_x509_trust_mem::
1050
1051 Sending of the names of the CAs can be controlled using *note
1052 gnutls_certificate_send_x509_rdn_sequence::.  The client, then, may send
1053 a certificate, signed by one of the server's acceptable signers.
1054
1055 \1f
1056 File: gnutls.info,  Node: Resuming Sessions,  Next: Resuming Internals,  Prev: Client Authentication,  Up: The TLS Handshake Protocol
1057
1058 3.5.4 Resuming Sessions
1059 -----------------------
1060
1061 The *note gnutls_handshake:: function, is expensive since a lot of
1062 calculations are performed.  In order to support many fast connections
1063 to the same server a client may use session resuming.  *Session
1064 resuming* is a feature of the TLS protocol which allows a client to
1065 connect to a server, after a successful handshake, without the expensive
1066 calculations.  This is achieved by using the previously established
1067 keys.  GnuTLS supports this feature, and the example (*note
1068 ex:resume-client::) illustrates a typical use of it.
1069
1070 Keep in mind that sessions are expired after some time, for security
1071 reasons, thus it may be normal for a server not to resume a session even
1072 if you requested that.  Also note that you must enable, using the
1073 priority functions, at least the algorithms used in the last session.
1074
1075 \1f
1076 File: gnutls.info,  Node: Resuming Internals,  Next: Interoperability,  Prev: Resuming Sessions,  Up: The TLS Handshake Protocol
1077
1078 3.5.5 Resuming Internals
1079 ------------------------
1080
1081 The resuming capability, mostly in the server side, is one of the
1082 problems of a thread-safe TLS implementations.  The problem is that all
1083 threads must share information in order to be able to resume sessions.
1084 The gnutls approach is, in case of a client, to leave all the burden of
1085 resuming to the client.  I.e., copy and keep the necessary parameters.
1086 See the functions:
1087
1088    * *note gnutls_session_get_data::
1089
1090    * *note gnutls_session_get_id::
1091
1092    * *note gnutls_session_set_data::
1093
1094 The server side is different.  A server has to specify some callback
1095 functions which store, retrieve and delete session data.  These can be
1096 registered with:
1097
1098    * *note gnutls_db_set_remove_function::
1099
1100    * *note gnutls_db_set_store_function::
1101
1102    * *note gnutls_db_set_retrieve_function::
1103
1104    * *note gnutls_db_set_ptr::
1105
1106 It might also be useful to be able to check for expired sessions in
1107 order to remove them, and save space.  The function *note
1108 gnutls_db_check_entry:: is provided for that reason.
1109
1110 \1f
1111 File: gnutls.info,  Node: Interoperability,  Prev: Resuming Internals,  Up: The TLS Handshake Protocol
1112
1113 3.5.6 Interoperability
1114 ----------------------
1115
1116 The TLS handshake is a complex procedure that negotiates all required
1117 parameters for a secure session.  GnuTLS supports several TLS
1118 extensions, as well as the latest TLS protocol version 1.2.  However few
1119 implementations are not able to properly interoperate once faced with
1120 extensions or version protocols they do not support and understand.  The
1121 TLS protocol allows for a graceful downgrade to the commonly supported
1122 options, but practice shows it is not always implemented correctly.
1123
1124 Because there is no way to achieve maximum interoperability with broken
1125 peers without sacrificing security, GnuTLS ignores such peers by
1126 default.  This might not be acceptable in cases where maximum
1127 compatibility is required.  Thus we allow enabling compatibility with
1128 broken peers using priority strings (see *note Priority Strings::).  An
1129 example priority string that is known to provide wide compatibility even
1130 with broken peers is shown below:
1131      NORMAL:-VERS-TLS-ALL:+VERS-TLS1.0:+VERS-SSL3.0:%COMPAT
1132 This priority string will only enable SSL 3.0 and TLS 1.0 as protocols
1133 and will disable, via the '%COMPAT' keyword, several TLS protocol
1134 options that are known to cause compatibility problems.  We suggest
1135 however only to use this mode if compatibility issues occur.
1136
1137 \1f
1138 File: gnutls.info,  Node: TLS Extensions,  Next: Selecting cryptographic key sizes,  Prev: The TLS Handshake Protocol,  Up: Introduction to TLS
1139
1140 3.6 TLS Extensions
1141 ==================
1142
1143 A number of extensions to the TLS protocol have been proposed mainly in
1144 [TLSEXT] (*note Bibliography::).  The extensions supported in GnuTLS
1145 are:
1146
1147    * Maximum fragment length negotiation
1148    * Server name indication
1149    * Session tickets
1150    * Safe Renegotiation
1151
1152 and they will be discussed in the subsections that follow.
1153
1154 3.6.1 Maximum Fragment Length Negotiation
1155 -----------------------------------------
1156
1157 This extension allows a TLS implementation to negotiate a smaller value
1158 for record packet maximum length.  This extension may be useful to
1159 clients with constrained capabilities.  See the *note
1160 gnutls_record_set_max_size:: and the *note gnutls_record_get_max_size::
1161 functions.
1162
1163 3.6.2 Server Name Indication
1164 ----------------------------
1165
1166 A common problem in HTTPS servers is the fact that the TLS protocol is
1167 not aware of the hostname that a client connects to, when the handshake
1168 procedure begins.  For that reason the TLS server has no way to know
1169 which certificate to send.
1170
1171 This extension solves that problem within the TLS protocol, and allows a
1172 client to send the HTTP hostname before the handshake begins within the
1173 first handshake packet.  The functions *note gnutls_server_name_set::
1174 and *note gnutls_server_name_get:: can be used to enable this extension,
1175 or to retrieve the name sent by a client.
1176
1177 3.6.3 Session Tickets
1178 ---------------------
1179
1180 To resume a TLS session the server normally store some state.  This
1181 complicates deployment, and typical situations the client can cache
1182 information and send it to the server instead.  The Session Ticket
1183 extension implements this idea, and it is documented in RFC 5077
1184 [TLSTKT] (*note Bibliography::).
1185
1186 Clients can enable support for TLS tickets with *note
1187 gnutls_session_ticket_enable_client:: and servers use *note
1188 gnutls_session_ticket_key_generate:: to generate a key and *note
1189 gnutls_session_ticket_enable_server:: to enable the extension.  Clients
1190 resume sessions using the ticket using the normal session resume
1191 functions, *note resume::.
1192
1193 3.6.4 Safe Renegotiation
1194 ------------------------
1195
1196 TLS gives the option to two communicating parties to renegotiate and
1197 update their security parameters.  One useful example of this feature
1198 was for a client to initially connect using anonymous negotiation to a
1199 server, and the renegotiate using some authenticated ciphersuite.  This
1200 occured to avoid having the client sending its credentials in the clear.
1201
1202 However this renegotiation, as initially designed would not ensure that
1203 the party one is renegotiating is the same as the one in the initial
1204 negotiation.  For example one server could forward all renegotiation
1205 traffic to an other server who will see this traffic as an initial
1206 negotiation attempt.
1207
1208 This might be seen as a valid design decision, but it seems it was not
1209 widely known or understood, thus today some application protocols the
1210 TLS renegotiation feature in a manner that enables a malicious server to
1211 insert content of his choice in the beginning of a TLS session.
1212
1213 The most prominent vulnerability was with HTTPS. There servers request a
1214 renegotiation to enforce an anonymous user to use a certificate in order
1215 to access certain parts of a web site.  The attack works by having the
1216 attacker simulate a client and connect to a server, with server-only
1217 authentication, and send some data intended to cause harm.  The server
1218 will then require renegotiation from him in order to perform the
1219 request.  When the proper client attempts to contact the server, the
1220 attacker hijacks that connection and forwards traffic to the initial
1221 server that requested renegotiation.  The attacker will not be able to
1222 read the data exchanged between the client and the server.  However, the
1223 server will (incorrectly) assume that the initial request sent by the
1224 attacker was sent by the now authenticated client.  The result is a
1225 prefix plain-text injection attack.
1226
1227 The above is just one example.  Other vulnerabilities exists that do not
1228 rely on the TLS renegotiation to change the client's authenticated
1229 status (either TLS or application layer).
1230
1231 While fixing these application protocols and implementations would be
1232 one natural reaction, an extension to TLS has been designed that
1233 cryptographically binds together any renegotiated handshakes with the
1234 initial negotiation.  When the extension is used, the attack is detected
1235 and the session can be terminated.  The extension is specified in
1236 [RFC5746] (*note Bibliography::).
1237
1238 GnuTLS supports the safe renegotiation extension.  The default behavior
1239 is as follows.  Clients will attempt to negotiate the safe renegotiation
1240 extension when talking to servers.  Servers will accept the extension
1241 when presented by clients.  Clients and servers will permit an initial
1242 handshake to complete even when the other side does not support the safe
1243 renegotiation extension.  Clients and servers will refuse renegotiation
1244 attempts when the extension has not been negotiated.
1245
1246 Note that permitting clients to connect to servers when the safe
1247 renegotiation extension is not enabled, is open up for attacks.
1248 Changing this default behaviour would prevent interoperability against
1249 the majority of deployed servers out there.  We will reconsider this
1250 default behaviour in the future when more servers have been upgraded.
1251 Note that it is easy to configure clients to always require the safe
1252 renegotiation extension from servers (see below on the
1253 '%SAFE_RENEGOTIATION' priority string).
1254
1255 To modify the default behaviour, we have introduced some new priority
1256 strings.  The priority strings can be used by applications (*note
1257 gnutls_priority_set::) and end users (e.g., '--priority' parameter to
1258 'gnutls-cli' and 'gnutls-serv').
1259
1260 The '%UNSAFE_RENEGOTIATION' priority string permits (re-)handshakes even
1261 when the safe renegotiation extension was not negotiated.  The default
1262 behavior is '%PARTIAL_RENEGOTIATION' that will prevent renegotiation
1263 with clients and servers not supporting the extension.  This is secure
1264 for servers but leaves clients vulnerable to some attacks, but this is a
1265 tradeoff between security and compatibility with old servers.  The
1266 '%SAFE_RENEGOTIATION' priority string makes clients and servers require
1267 the extension for every handshake.  The latter is the most secure option
1268 for clients, at the cost of not being able to connect to legacy servers.
1269 Servers will also deny clients that do not support the extension from
1270 connecting.
1271
1272 It is possible to disable use of the extension completely, in both
1273 clients and servers, by using the '%DISABLE_SAFE_RENEGOTIATION' priority
1274 string however we strongly recommend you to only do this for debugging
1275 and test purposes.
1276
1277 The default values if the flags above are not specified are:
1278
1279 'Server:'
1280      %PARTIAL_RENEGOTIATION
1281
1282 'Client:'
1283      %PARTIAL_RENEGOTIATION
1284
1285 For applications we have introduced a new API related to safe
1286 renegotiation.  The *note gnutls_safe_renegotiation_status:: function is
1287 used to check if the extension has been negotiated on a session, and can
1288 be used both by clients and servers.
1289
1290 \1f
1291 File: gnutls.info,  Node: Selecting cryptographic key sizes,  Next: On SSL 2 and older protocols,  Prev: TLS Extensions,  Up: Introduction to TLS
1292
1293 3.7 Selecting Cryptographic Key Sizes
1294 =====================================
1295
1296 In TLS, since a lot of algorithms are involved, it is not easy to set a
1297 consistent security level.  For this reason this section will present
1298 some correspondance between key sizes of symmetric algorithms and public
1299 key algorithms based on the "ECRYPT II Yearly Report on Algorithms and
1300 Keysizes (2009-2010)" in [ECRYPT] (*note Bibliography::).  Those can be
1301 used to generate certificates with appropriate key sizes as well as
1302 parameters for Diffie-Hellman and SRP authentication.
1303
1304 SecurityRSA, DH     ECC     'gnutls_sec_param_t'Description
1305 bits    and SRP     key                    
1306         parameter   size
1307         size
1308 64      816         128     'WEAK'         Very short term
1309                                            protection against
1310                                            small organizations
1311                                            
1312 80      1248        160     'LOW'          Very short term
1313                                            protection against
1314                                            agencies
1315                                            
1316 112     2432        224     'NORMAL'       Medium-term protection
1317                                            
1318 128     3248        256     'HIGH'         Long term protection
1319                                            
1320 256     15424       512     'ULTRA'        Foreseeable future
1321                                            
1322
1323 The first column provides a security parameter in a number of bits.
1324 This gives an indication of the number of combinations to be tried by an
1325 adversary to brute force a key.  For example to test all possible keys
1326 in a 112 bit security parameter 2^{112} combinations have to be tried.
1327 For today's technology this is infeasible.  The next two columns
1328 correlate the security parameter with actual bit sizes of parameters for
1329 DH, RSA, SRP and ECC algorithms.  A mapping to 'gnutls_sec_param_t'
1330 value is given for each security parameter, on the next column, and
1331 finally a brief description of the level.
1332
1333 Note however that the values suggested here are nothing more than an
1334 educated guess that is valid today.  There are no guarrantees that an
1335 algorithm will remain unbreakable or that these values will remain
1336 constant in time.  There could be scientific breakthroughs that cannot
1337 be predicted or total failure of the current public key systems by
1338 quantum computers.  On the other hand though the cryptosystems used in
1339 TLS are selected in a conservative way and such catastrophic
1340 breakthroughs or failures are believed to be unlikely.
1341
1342 NIST publication SP 800-57 [NISTSP80057] (*note Bibliography::) contains
1343 a similar table.
1344
1345 When using GnuTLS and a decision on bit sizes for a public key algorithm
1346 is required, use of the following functions is recommended:
1347
1348    * *note gnutls_pk_bits_to_sec_param::
1349
1350    * *note gnutls_sec_param_to_pk_bits::
1351
1352 Those functions will convert a human understandable security parameter
1353 of 'gnutls_sec_param_t' type, to a number of bits suitable for a public
1354 key algorithm.
1355
1356 \1f
1357 File: gnutls.info,  Node: On SSL 2 and older protocols,  Prev: Selecting cryptographic key sizes,  Up: Introduction to TLS
1358
1359 3.8 On SSL 2 and Older Protocols
1360 ================================
1361
1362 One of the initial decisions in the GnuTLS development was to implement
1363 the known security protocols for the transport layer.  Initially TLS 1.0
1364 was implemented since it was the latest at that time, and was considered
1365 to be the most advanced in security properties.  Later the SSL 3.0
1366 protocol was implemented since it is still the only protocol supported
1367 by several servers and there are no serious security vulnerabilities
1368 known.
1369
1370 One question that may arise is why we didn't implement SSL 2.0 in the
1371 library.  There are several reasons, most important being that it has
1372 serious security flaws, unacceptable for a modern security library.
1373 Other than that, this protocol is barely used by anyone these days since
1374 it has been deprecated since 1996.  The security problems in SSL 2.0
1375 include:
1376
1377    * Message integrity compromised.  The SSLv2 message authentication
1378      uses the MD5 function, and is insecure.
1379
1380    * Man-in-the-middle attack.  There is no protection of the handshake
1381      in SSLv2, which permits a man-in-the-middle attack.
1382
1383    * Truncation attack.  SSLv2 relies on TCP FIN to close the session,
1384      so the attacker can forge a TCP FIN, and the peer cannot tell if it
1385      was a legitimate end of data or not.
1386
1387    * Weak message integrity for export ciphers.  The cryptographic keys
1388      in SSLv2 are used for both message authentication and encryption,
1389      so if weak encryption schemes are negotiated (say 40-bit keys) the
1390      message authentication code use the same weak key, which isn't
1391      necessary.
1392
1393 Other protocols such as Microsoft's PCT 1 and PCT 2 were not implemented
1394 because they were also abandoned and deprecated by SSL 3.0 and later TLS
1395 1.0.
1396
1397 \1f
1398 File: gnutls.info,  Node: Authentication methods,  Next: More on certificate authentication,  Prev: Introduction to TLS,  Up: Top
1399
1400 4 Authentication Methods
1401 ************************
1402
1403 The TLS protocol provides confidentiality and encryption, but also
1404 offers authentication, which is a prerequisite for a secure connection.
1405 The available authentication methods in GnuTLS are:
1406
1407    * Certificate authentication
1408
1409    * Anonymous authentication
1410
1411    * SRP authentication
1412
1413    * PSK authentication
1414
1415 * Menu:
1416
1417 * Certificate authentication::
1418 * Anonymous authentication::
1419 * Authentication using SRP::
1420 * Authentication using PSK::
1421 * Authentication and credentials::
1422 * Parameters stored in credentials::
1423
1424 \1f
1425 File: gnutls.info,  Node: Certificate authentication,  Next: Anonymous authentication,  Up: Authentication methods
1426
1427 4.1 Certificate Authentication
1428 ==============================
1429
1430 4.1.1 Authentication Using X.509 Certificates
1431 ---------------------------------------------
1432
1433 X.509 certificates contain the public parameters, of a public key
1434 algorithm, and an authority's signature, which proves the authenticity
1435 of the parameters.  *Note The X.509 trust model::, for more information
1436 on X.509 protocols.
1437
1438 4.1.2 Authentication Using OpenPGP Keys
1439 ---------------------------------------
1440
1441 OpenPGP keys also contain public parameters of a public key algorithm,
1442 and signatures from several other parties.  Depending on whether a
1443 signer is trusted the key is considered trusted or not.  GnuTLS's
1444 OpenPGP authentication implementation is based on the [TLSPGP] (*note
1445 Bibliography::) proposal.
1446
1447 *Note The OpenPGP trust model::, for more information about the OpenPGP
1448 trust model.  For a more detailed introduction to OpenPGP and GnuPG see
1449 [GPGH] (*note Bibliography::).
1450
1451 4.1.3 Using Certificate Authentication
1452 --------------------------------------
1453
1454 In GnuTLS both the OpenPGP and X.509 certificates are part of the
1455 certificate authentication and thus are handled using a common API.
1456
1457 When using certificates the server is required to have at least one
1458 certificate and private key pair.  A client may or may not have such a
1459 pair.  The certificate and key pair should be loaded, before any TLS
1460 session is initialized, in a certificate credentials structure.  This
1461 should be done by using *note gnutls_certificate_set_x509_key_file:: or
1462 *note gnutls_certificate_set_openpgp_key_file:: depending on the
1463 certificate type.  In the X.509 case, the functions will also accept and
1464 use a certificate list that leads to a trusted authority.  The
1465 certificate list must be ordered in such way that every certificate
1466 certifies the one before it.  The trusted authority's certificate need
1467 not to be included, since the peer should possess it already.
1468
1469 As an alternative, a callback may be used so the server or the client
1470 specify the certificate and the key at the handshake time.  That
1471 callback can be set using the functions:
1472
1473    * *note gnutls_certificate_server_set_retrieve_function::
1474
1475    * *note gnutls_certificate_client_set_retrieve_function::
1476
1477 Clients and servers that will select certificates using callback
1478 functions should select a certificate according the peer's signature
1479 algorithm preferences.  To get those preferences use *note
1480 gnutls_sign_algorithm_get_requested::.
1481
1482 Certificate verification is possible by loading the trusted authorities
1483 into the credentials structure by using *note
1484 gnutls_certificate_set_x509_trust_file:: or *note
1485 gnutls_certificate_set_openpgp_keyring_file:: for openpgp keys.  Note
1486 however that the peer's certificate is not automatically verified, you
1487 should call *note gnutls_certificate_verify_peers2::, after a successful
1488 handshake, to verify the signatures of the certificate.  An alternative
1489 way, which reports a more detailed verification output, is to use *note
1490 gnutls_certificate_get_peers:: to obtain the raw certificate of the peer
1491 and verify it using the functions discussed in *note The X.509 trust
1492 model::.
1493
1494 In a handshake, the negotiated cipher suite depends on the certificate's
1495 parameters, so not all key exchange methods will be available with some
1496 certificates.  GnuTLS will disable ciphersuites that are not compatible
1497 with the key, or the enabled authentication methods.  For example keys
1498 marked as sign-only, will not be able to access the plain RSA
1499 ciphersuites, but only the 'DHE_RSA' ones.  It is recommended not to use
1500 RSA keys for both signing and encryption.  If possible use the same key
1501 for the 'DHE_RSA' and 'RSA_EXPORT' ciphersuites, which use signing, and
1502 a different key for the plain RSA ciphersuites, which use encryption.
1503 All the key exchange methods shown below are available in certificate
1504 authentication.
1505
1506 Note that the DHE key exchange methods are generally slower(1) than
1507 plain RSA and require Diffie Hellman parameters to be generated and
1508 associated with a credentials structure, by the server.  The
1509 'RSA-EXPORT' method also requires 512 bit RSA parameters, that should
1510 also be generated and associated with the credentials structure.  See
1511 the functions:
1512
1513    * *note gnutls_dh_params_generate2::
1514
1515    * *note gnutls_certificate_set_dh_params::
1516
1517    * *note gnutls_rsa_params_generate2::
1518
1519    * *note gnutls_certificate_set_rsa_export_params::
1520
1521 Sometimes in order to avoid bottlenecks in programs it is useful to
1522 store and read parameters from formats that can be generated by external
1523 programs such as 'certtool'.  This is possible with GnuTLS by using the
1524 following functions:
1525
1526    * *note gnutls_dh_params_import_pkcs3::
1527
1528    * *note gnutls_rsa_params_import_pkcs1::
1529
1530    * *note gnutls_dh_params_export_pkcs3::
1531
1532    * *note gnutls_rsa_params_export_pkcs1::
1533
1534 Key exchange algorithms for OpenPGP and X.509 certificates:
1535
1536 'RSA:'
1537      The RSA algorithm is used to encrypt a key and send it to the peer.
1538      The certificate must allow the key to be used for encryption.
1539
1540 'RSA_EXPORT:'
1541      The RSA algorithm is used to encrypt a key and send it to the peer.
1542      In the EXPORT algorithm, the server signs temporary RSA parameters
1543      of 512 bits -- which are considered weak -- and sends them to the
1544      client.
1545
1546 'DHE_RSA:'
1547      The RSA algorithm is used to sign Ephemeral Diffie-Hellman
1548      parameters which are sent to the peer.  The key in the certificate
1549      must allow the key to be used for signing.  Note that key exchange
1550      algorithms which use Ephemeral Diffie-Hellman parameters, offer
1551      perfect forward secrecy.  That means that even if the private key
1552      used for signing is compromised, it cannot be used to reveal past
1553      session data.
1554
1555 'DHE_DSS:'
1556      The DSS algorithm is used to sign Ephemeral Diffie-Hellman
1557      parameters which are sent to the peer.  The certificate must
1558      contain DSA parameters to use this key exchange algorithm.  DSS
1559      stands for Digital Signature Standard.
1560
1561    ---------- Footnotes ----------
1562
1563    (1) It really depends on the group used.  Primes with lesser bits are
1564 always faster, but also easier to break.  Values less than 768 should
1565 not be used today
1566
1567 \1f
1568 File: gnutls.info,  Node: Anonymous authentication,  Next: Authentication using SRP,  Prev: Certificate authentication,  Up: Authentication methods
1569
1570 4.2 Anonymous Authentication
1571 ============================
1572
1573 The anonymous key exchange performs encryption but there is no
1574 indication of the identity of the peer.  This kind of authentication is
1575 vulnerable to a man in the middle attack, but this protocol can be used
1576 even if there is no prior communication and trusted parties with the
1577 peer, or when full anonymity is required.  Unless really required, do
1578 not use anonymous authentication.  Available key exchange methods are
1579 shown below.
1580
1581 Note that the key exchange methods for anonymous authentication require
1582 Diffie-Hellman parameters to be generated by the server and associated
1583 with an anonymous credentials structure.
1584
1585 Supported anonymous key exchange algorithms:
1586
1587 'ANON_DH:'
1588      This algorithm exchanges Diffie-Hellman parameters.
1589
1590 \1f
1591 File: gnutls.info,  Node: Authentication using SRP,  Next: Authentication using PSK,  Prev: Anonymous authentication,  Up: Authentication methods
1592
1593 4.3 Authentication using SRP
1594 ============================
1595
1596 Authentication via the Secure Remote Password protocol, SRP(1), is
1597 supported.  The SRP key exchange is an extension to the TLS protocol,
1598 and it is a password based authentication (unlike X.509 or OpenPGP that
1599 use certificates).  The two peers can be identified using a single
1600 password, or there can be combinations where the client is authenticated
1601 using SRP and the server using a certificate.
1602
1603 The advantage of SRP authentication, over other proposed secure password
1604 authentication schemes, is that SRP does not require the server to hold
1605 the user's password.  This kind of protection is similar to the one used
1606 traditionally in the _UNIX_ '/etc/passwd' file, where the contents of
1607 this file did not cause harm to the system security if they were
1608 revealed.  The SRP needs instead of the plain password something called
1609 a verifier, which is calculated using the user's password, and if stolen
1610 cannot be used to impersonate the user.  Check [TOMSRP] (*note
1611 Bibliography::) for a detailed description of the SRP protocol and the
1612 Stanford SRP libraries, which includes a PAM module that synchronizes
1613 the system's users passwords with the SRP password files.  That way SRP
1614 authentication could be used for all the system's users.
1615
1616 The implementation in GnuTLS is based on paper [TLSSRP] (*note
1617 Bibliography::).  The supported SRP key exchange methods are:
1618
1619 'SRP:'
1620      Authentication using the SRP protocol.
1621
1622 'SRP_DSS:'
1623      Client authentication using the SRP protocol.  Server is
1624      authenticated using a certificate with DSA parameters.
1625
1626 'SRP_RSA:'
1627      Client authentication using the SRP protocol.  Server is
1628      authenticated using a certificate with RSA parameters.
1629
1630 If clients supporting SRP know the username and password before the
1631 connection, should initialize the client credentials and call the
1632 function *note gnutls_srp_set_client_credentials::.  Alternatively they
1633 could specify a callback function by using the function *note
1634 gnutls_srp_set_client_credentials_function::.  This has the advantage
1635 that allows probing the server for SRP support.  In that case the
1636 callback function will be called twice per handshake.  The first time is
1637 before the ciphersuite is negotiated, and if the callback returns a
1638 negative error code, the callback will be called again if SRP has been
1639 negotiated.  This uses a special TLS-SRP handshake idiom in order to
1640 avoid, in interactive applications, to ask the user for SRP password and
1641 username if the server does not negotiate an SRP ciphersuite.
1642
1643 In server side the default behaviour of GnuTLS is to read the usernames
1644 and SRP verifiers from password files.  These password files are the
1645 ones used by the _Stanford srp libraries_ and can be specified using the
1646 *note gnutls_srp_set_server_credentials_file::.  If a different password
1647 file format is to be used, then the function *note
1648 gnutls_srp_set_server_credentials_function::, should be called, in order
1649 to set an appropriate callback.
1650
1651 Some helper functions such as
1652
1653    * *note gnutls_srp_verifier::
1654
1655    * *note gnutls_srp_base64_encode::
1656
1657    * *note gnutls_srp_base64_decode::
1658
1659 are included in GnuTLS, and can be used to generate and maintain SRP
1660 verifiers and password files.  A program to manipulate the required
1661 parameters for SRP authentication is also included.  *Note srptool::,
1662 for more information.
1663
1664    ---------- Footnotes ----------
1665
1666    (1) SRP is described in [RFC2945] (*note Bibliography::)
1667
1668 \1f
1669 File: gnutls.info,  Node: Authentication using PSK,  Next: Authentication and credentials,  Prev: Authentication using SRP,  Up: Authentication methods
1670
1671 4.4 Authentication using PSK
1672 ============================
1673
1674 Authentication using Pre-shared keys is a method to authenticate using
1675 usernames and binary keys.  This protocol avoids making use of public
1676 key infrastructure and expensive calculations, thus it is suitable for
1677 constraint clients.
1678
1679 The implementation in GnuTLS is based on paper [TLSPSK] (*note
1680 Bibliography::).  The supported PSK key exchange methods are:
1681
1682 'PSK:'
1683      Authentication using the PSK protocol.
1684
1685 'DHE-PSK:'
1686      Authentication using the PSK protocol and Diffie-Hellman key
1687      exchange.  This method offers perfect forward secrecy.
1688
1689 Clients supporting PSK should supply the username and key before the
1690 connection to the client credentials by calling the function *note
1691 gnutls_psk_set_client_credentials::.  Alternatively they could specify a
1692 callback function by using the function *note
1693 gnutls_psk_set_client_credentials_function::.  This has the advantage
1694 that the callback will be called only if PSK has been negotiated.
1695
1696 In server side the default behaviour of GnuTLS is to read the usernames
1697 and PSK keys from a password file.  The password file should contain
1698 usernames and keys in hexadecimal format.  The name of the password file
1699 can be stored to the credentials structure by calling *note
1700 gnutls_psk_set_server_credentials_file::.  If a different password file
1701 format is to be used, then the function *note
1702 gnutls_psk_set_server_credentials_function::, should be used instead.
1703
1704 The server can help the client chose a suitable username and password,
1705 by sending a hint.  In the server, specify the hint by calling *note
1706 gnutls_psk_set_server_credentials_hint::.  The client can retrieve the
1707 hint, for example in the callback function, using *note
1708 gnutls_psk_client_get_hint::.
1709
1710 There is no mechanism to derive a PSK key from a password specified by
1711 the TLS PSK document(1).  For password-based authentication check *note
1712 Authentication using SRP::.
1713
1714 Some helper functions such as:
1715
1716    * *note gnutls_hex_encode::
1717
1718    * *note gnutls_hex_decode::
1719
1720 are included in GnuTLS, and may be used to generate and maintain PSK
1721 keys.
1722
1723    ---------- Footnotes ----------
1724
1725    (1) GnuTLS used to provide 'gnutls_psk_netconf_derive_key' which
1726 follows the algorithm specified in 'draft-ietf-netconf-tls-02.txt'.
1727 This method is deprecated and might be removed in later versions of
1728 GnuTLS.
1729
1730 \1f
1731 File: gnutls.info,  Node: Authentication and credentials,  Next: Parameters stored in credentials,  Prev: Authentication using PSK,  Up: Authentication methods
1732
1733 4.5 Authentication and Credentials
1734 ==================================
1735
1736 In GnuTLS every key exchange method is associated with a credentials
1737 type.  So in order to enable to enable a specific method, the
1738 corresponding credentials type should be initialized and set using *note
1739 gnutls_credentials_set::.  A mapping is shown below.
1740
1741 Key exchange algorithms and the corresponding credential types:
1742
1743 Key exchange           Client credentials     Server credentials
1744                                               
1745 ---------------------------------------------------------------------
1746 'KX_RSA'
1747 'KX_DHE_RSA'
1748 'KX_DHE_DSS'
1749 'KX_RSA_EXPORT'        'CRD_CERTIFICATE'      'CRD_CERTIFICATE'
1750                                               
1751 'KX_SRP_RSA'           'CRD_SRP'              'CRD_SRP'
1752 'KX_SRP_DSS'                                  'CRD_CERTIFICATE'
1753                                               
1754 'KX_SRP'               'CRD_SRP'              'CRD_SRP'
1755                                               
1756 'KX_ANON_DH'           'CRD_ANON'             'CRD_ANON'
1757                                               
1758 'KX_PSK'               'CRD_PSK'              'CRD_PSK'
1759                                               
1760
1761 \1f
1762 File: gnutls.info,  Node: Parameters stored in credentials,  Prev: Authentication and credentials,  Up: Authentication methods
1763
1764 4.6 Parameters Stored in Credentials
1765 ====================================
1766
1767 Several parameters such as the ones used for Diffie-Hellman
1768 authentication are stored within the credentials structures, so all
1769 sessions can access them.  Those parameters are stored in structures
1770 such as 'gnutls_dh_params_t' and 'gnutls_rsa_params_t', and functions
1771 like *note gnutls_certificate_set_dh_params:: and *note
1772 gnutls_certificate_set_rsa_export_params:: can be used to associate
1773 those parameters with the given credentials structure.
1774
1775 Since those parameters need to be renewed from time to time and a global
1776 structure such as the credentials, may not be easy to modify since it is
1777 accessible by all sessions, an alternative interface is available using
1778 a callback function.  This can be set using the *note
1779 gnutls_certificate_set_params_function::.  An example is shown below.
1780
1781      #include <gnutls.h>
1782
1783      gnutls_rsa_params_t rsa_params;
1784      gnutls_dh_params_t dh_params;
1785
1786      /* This function will be called once a session requests DH
1787       * or RSA parameters. The parameters returned (if any) will
1788       * be used for the first handshake only.
1789       */
1790      static int get_params( gnutls_session_t session,
1791              gnutls_params_type_t type,
1792              gnutls_params_st *st)
1793      {
1794         if (type == GNUTLS_PARAMS_RSA_EXPORT)
1795            st->params.rsa_export = rsa_params;
1796         else if (type == GNUTLS_PARAMS_DH)
1797            st->params.dh = dh_params;
1798         else return -1;
1799
1800         st->type = type;
1801         /* do not deinitialize those parameters.
1802          */
1803         st->deinit = 0;
1804
1805         return 0;
1806      }
1807
1808      int main()
1809      {
1810         gnutls_certificate_credentials_t cert_cred;
1811
1812         initialize_params();
1813
1814         /* ...
1815          */
1816
1817         gnutls_certificate_set_params_function( cert_cred, get_params);
1818      }
1819
1820 \1f
1821 File: gnutls.info,  Node: More on certificate authentication,  Next: How to use TLS in application protocols,  Prev: Authentication methods,  Up: Top
1822
1823 5 More on Certificate Authentication
1824 ************************************
1825
1826 * Menu:
1827
1828 * The X.509 trust model::
1829 * The OpenPGP trust model::
1830 * PKCS #11 tokens::
1831 * Abstract data types::
1832 * Digital signatures::
1833
1834 \1f
1835 File: gnutls.info,  Node: The X.509 trust model,  Next: The OpenPGP trust model,  Up: More on certificate authentication
1836
1837 5.1 The X.509 Trust Model
1838 =========================
1839
1840 The X.509 protocols rely on a hierarchical trust model.  In this trust
1841 model Certification Authorities (CAs) are used to certify entities.
1842 Usually more than one certification authorities exist, and certification
1843 authorities may certify other authorities to issue certificates as well,
1844 following a hierarchical model.
1845
1846 \0\b[image src="gnutls-x509.png"\0\b]
1847
1848 One needs to trust one or more CAs for his secure communications.  In
1849 that case only the certificates issued by the trusted authorities are
1850 acceptable.  See the figure above for a typical example.  The API for
1851 handling X.509 certificates is described at section *note sec:x509api::.
1852 Some examples are listed below.
1853
1854 * Menu:
1855
1856 * X.509 certificates::
1857 * Verifying X.509 certificate paths::
1858 * PKCS #10 certificate requests::
1859 * PKCS #12 structures::
1860
1861 \1f
1862 File: gnutls.info,  Node: X.509 certificates,  Next: Verifying X.509 certificate paths,  Up: The X.509 trust model
1863
1864 5.1.1 X.509 Certificates
1865 ------------------------
1866
1867 An X.509 certificate usually contains information about the certificate
1868 holder, the signer, a unique serial number, expiration dates and some
1869 other fields [PKIX] (*note Bibliography::) as shown in the table below.
1870
1871 'version:'
1872      The field that indicates the version of the certificate.
1873
1874 'serialNumber:'
1875      This field holds a unique serial number per certificate.
1876
1877 'issuer:'
1878      Holds the issuer's distinguished name.
1879
1880 'validity:'
1881      The activation and expiration dates.
1882
1883 'subject:'
1884      The subject's distinguished name of the certificate.
1885
1886 'extensions:'
1887      The extensions are fields only present in version 3 certificates.
1888
1889 The certificate's _subject or issuer name_ is not just a single string.
1890 It is a Distinguished name and in the ASN.1 notation is a sequence of
1891 several object IDs with their corresponding values.  Some of available
1892 OIDs to be used in an X.509 distinguished name are defined in
1893 'gnutls/x509.h'.
1894
1895 The _Version_ field in a certificate has values either 1 or 3 for
1896 version 3 certificates.  Version 1 certificates do not support the
1897 extensions field so it is not possible to distinguish a CA from a
1898 person, thus their usage should be avoided.
1899
1900 The _validity_ dates are there to indicate the date that the specific
1901 certificate was activated and the date the certificate's key would be
1902 considered invalid.
1903
1904 Certificate _extensions_ are there to include information about the
1905 certificate's subject that did not fit in the typical certificate
1906 fields.  Those may be e-mail addresses, flags that indicate whether the
1907 belongs to a CA etc.  All the supported X.509 version 3 extensions are
1908 shown in the table below.
1909
1910 'subject key id (2.5.29.14):'
1911      An identifier of the key of the subject.
1912
1913 'authority key id (2.5.29.35):'
1914      An identifier of the authority's key used to sign the certificate.
1915
1916 'subject alternative name (2.5.29.17):'
1917      Alternative names to subject's distinguished name.
1918
1919 'key usage (2.5.29.15):'
1920      Constraints the key's usage of the certificate.
1921
1922 'extended key usage (2.5.29.37):'
1923      Constraints the purpose of the certificate.
1924
1925 'basic constraints (2.5.29.19):'
1926      Indicates whether this is a CA certificate or not, and specify the
1927      maximum path lengths of certificate chains.
1928
1929 'CRL distribution points (2.5.29.31):'
1930      This extension is set by the CA, in order to inform about the
1931      issued CRLs.
1932
1933 'Proxy Certification Information (1.3.6.1.5.5.7.1.14):'
1934      Proxy Certificates includes this extension that contains the OID of
1935      the proxy policy language used, and can specify limits on the
1936      maximum lengths of proxy chains.  Proxy Certificates are specified
1937      in [RFC3820] (*note Bibliography::).
1938
1939 In GnuTLS the X.509 certificate structures are handled using the
1940 'gnutls_x509_crt_t' type and the corresponding private keys with the
1941 'gnutls_x509_privkey_t' type.  All the available functions for X.509
1942 certificate handling have their prototypes in 'gnutls/x509.h'.  An
1943 example program to demonstrate the X.509 parsing capabilities can be
1944 found at section *note ex:x509-info::.
1945
1946 \1f
1947 File: gnutls.info,  Node: Verifying X.509 certificate paths,  Next: PKCS #10 certificate requests,  Prev: X.509 certificates,  Up: The X.509 trust model
1948
1949 5.1.2 Verifying X.509 Certificate Paths
1950 ---------------------------------------
1951
1952 Verifying certificate paths is important in X.509 authentication.  For
1953 this purpose the function *note gnutls_x509_crt_verify:: is provided.
1954 The output of this function is the bitwise OR of the elements of the
1955 'gnutls_certificate_status_t' enumeration.  A detailed description of
1956 these elements can be found in figure below.  The function *note
1957 gnutls_certificate_verify_peers2:: is equivalent to the previous one,
1958 and will verify the peer's certificate in a TLS session.
1959
1960 'GNUTLS_CERT_INVALID:'
1961      The certificate is not signed by one of the known authorities, or
1962      the signature is invalid.
1963
1964 'GNUTLS_CERT_REVOKED:'
1965      The certificate has been revoked by its CA.
1966
1967 'GNUTLS_CERT_SIGNER_NOT_FOUND:'
1968      The certificate's issuer is not known.  This is the case when the
1969      issuer is not in the trusted certificates list.
1970
1971 'GNUTLS_CERT_SIGNER_NOT_CA:'
1972      The certificate's signer was not a CA. This may happen if this was
1973      a version 1 certificate, which is common with some CAs, or a
1974      version 3 certificate without the basic constrains extension.
1975
1976 'GNUTLS_CERT_INSECURE_ALGORITHM:'
1977      The certificate was signed using an insecure algorithm such as MD2
1978      or MD5.  These algorithms have been broken and should not be
1979      trusted.
1980
1981 There is also to possibility to pass some input to the verification
1982 functions in the form of flags.  For *note gnutls_x509_crt_verify:: the
1983 flags are passed straightforward, but *note
1984 gnutls_certificate_verify_peers2:: depends on the flags set by calling
1985 *note gnutls_certificate_set_verify_flags::.  All the available flags
1986 are part of the enumeration *note gnutls_certificate_verify_flags:: and
1987 are explained in the table below.
1988
1989 'GNUTLS_VERIFY_DISABLE_CA_SIGN:'
1990      If set a signer does not have to be a certificate authority.  This
1991      flag should normaly be disabled, unless you know what this means.
1992
1993 'GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT:'
1994      Allow only trusted CA certificates that have version 1.  This is
1995      safer than GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT, and should be
1996      used instead.  That way only signers in your trusted list will be
1997      allowed to have certificates of version 1.
1998
1999 'GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT:'
2000      Allow CA certificates that have version 1 (both root and
2001      intermediate).  This is dangerous since those haven't the
2002      basicConstraints extension.  Must be used in combination with
2003      GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT.
2004
2005 'GNUTLS_VERIFY_DO_NOT_ALLOW_SAME:'
2006      If a certificate is not signed by anyone trusted but exists in the
2007      trusted CA list do not treat it as trusted.
2008
2009 'GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2:'
2010      Allow certificates to be signed using the old MD2 algorithm.
2011
2012 'GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5:'
2013      Allow certificates to be signed using the broken MD5 algorithm.
2014
2015 Although the verification of a certificate path indicates that the
2016 certificate is signed by trusted authority, does not reveal anything
2017 about the peer's identity.  It is required to verify if the
2018 certificate's owner is the one you expect.  For more information consult
2019 [RFC2818] (*note Bibliography::) and section *note ex:verify:: for an
2020 example.
2021
2022 \1f
2023 File: gnutls.info,  Node: PKCS #10 certificate requests,  Next: PKCS #12 structures,  Prev: Verifying X.509 certificate paths,  Up: The X.509 trust model
2024
2025 5.1.3 PKCS #10 Certificate Requests
2026 -----------------------------------
2027
2028 A certificate request is a structure, which contain information about an
2029 applicant of a certificate service.  It usually contains a private key,
2030 a distinguished name and secondary data such as a challenge password.
2031 GnuTLS supports the requests defined in PKCS #10 [RFC2986] (*note
2032 Bibliography::).  Other certificate request's format such as PKIX's
2033 [RFC4211] (*note Bibliography::) are not currently supported.
2034
2035 In GnuTLS the PKCS #10 structures are handled using the
2036 'gnutls_x509_crq_t' type.  An example of a certificate request
2037 generation can be found at section *note ex:crq::.
2038
2039 \1f
2040 File: gnutls.info,  Node: PKCS #12 structures,  Prev: PKCS #10 certificate requests,  Up: The X.509 trust model
2041
2042 5.1.4 PKCS #12 Structures
2043 -------------------------
2044
2045 A PKCS #12 structure [PKCS12] (*note Bibliography::) usually contains a
2046 user's private keys and certificates.  It is commonly used in browsers
2047 to export and import the user's identities.
2048
2049 In GnuTLS the PKCS #12 structures are handled using the
2050 'gnutls_pkcs12_t' type.  This is an abstract type that may hold several
2051 'gnutls_pkcs12_bag_t' types.  The Bag types are the holders of the
2052 actual data, which may be certificates, private keys or encrypted data.
2053 An Bag of type encrypted should be decrypted in order for its data to be
2054 accessed.
2055
2056 An example of a PKCS #12 structure generation can be found at section
2057 *note ex:pkcs12::.
2058
2059 \1f
2060 File: gnutls.info,  Node: The OpenPGP trust model,  Next: PKCS #11 tokens,  Prev: The X.509 trust model,  Up: More on certificate authentication
2061
2062 5.2 The OpenPGP Trust Model
2063 ===========================
2064
2065 The OpenPGP key authentication relies on a distributed trust model,
2066 called the "web of trust".  The "web of trust" uses a decentralized
2067 system of trusted introducers, which are the same as a CA. OpenPGP
2068 allows anyone to sign anyone's else public key.  When Alice signs Bob's
2069 key, she is introducing Bob's key to anyone who trusts Alice.  If
2070 someone trusts Alice to introduce keys, then Alice is a trusted
2071 introducer in the mind of that observer.
2072
2073 \0\b[image src="gnutls-pgp.png"\0\b]
2074
2075 For example: If David trusts Alice to be an introducer, and Alice signed
2076 Bob's key, Dave also trusts Bob's key to be the real one.
2077
2078 There are some key points that are important in that model.  In the
2079 example Alice has to sign Bob's key, only if she is sure that the key
2080 belongs to Bob.  Otherwise she may also make Dave falsely believe that
2081 this is Bob's key.  Dave has also the responsibility to know who to
2082 trust.  This model is similar to real life relations.
2083
2084 Just see how Charlie behaves in the previous example.  Although he has
2085 signed Bob's key - because he knows, somehow, that it belongs to Bob -
2086 he does not trust Bob to be an introducer.  Charlie decided to trust
2087 only Kevin, for some reason.  A reason could be that Bob is lazy enough,
2088 and signs other people's keys without being sure that they belong to the
2089 actual owner.
2090
2091 5.2.1 OpenPGP Keys
2092 ------------------
2093
2094 In GnuTLS the OpenPGP key structures [RFC2440] (*note Bibliography::)
2095 are handled using the 'gnutls_openpgp_crt_t' type and the corresponding
2096 private keys with the 'gnutls_openpgp_privkey_t' type.  All the
2097 prototypes for the key handling functions can be found at
2098 'gnutls/openpgp.h'.
2099
2100 5.2.2 Verifying an OpenPGP Key
2101 ------------------------------
2102
2103 The verification functions of OpenPGP keys, included in GnuTLS, are
2104 simple ones, and do not use the features of the "web of trust".  For
2105 that reason, if the verification needs are complex, the assistance of
2106 external tools like GnuPG and GPGME
2107 ('http://www.gnupg.org/related_software/gpgme/') is recommended.
2108
2109 There is one verification function in GnuTLS, the *note
2110 gnutls_openpgp_crt_verify_ring::.  This checks an OpenPGP key against a
2111 given set of public keys (keyring) and returns the key status.  The key
2112 verification status is the same as in X.509 certificates, although the
2113 meaning and interpretation are different.  For example an OpenPGP key
2114 may be valid, if the self signature is ok, even if no signers were
2115 found.  The meaning of verification status is shown in the figure below.
2116
2117 'CERT_INVALID:'
2118      A signature on the key is invalid.  That means that the key was
2119      modified by somebody, or corrupted during transport.
2120
2121 'CERT_REVOKED:'
2122      The key has been revoked by its owner.
2123
2124 'CERT_SIGNER_NOT_FOUND:'
2125      The key was not signed by a known signer.
2126
2127 'GNUTLS_CERT_INSECURE_ALGORITHM:'
2128      The certificate was signed using an insecure algorithm such as MD2
2129      or MD5.  These algorithms have been broken and should not be
2130      trusted.
2131
2132 \1f
2133 File: gnutls.info,  Node: PKCS #11 tokens,  Next: Abstract data types,  Prev: The OpenPGP trust model,  Up: More on certificate authentication
2134
2135 5.3 PKCS #11 tokens
2136 ===================
2137
2138 5.3.1 Introduction
2139 ------------------
2140
2141 This section copes with the PKCS #11 [PKCS11] (*note Bibliography::)
2142 support in GnuTLS.  PKCS #11 is plugin API allowing applications to
2143 access cryptographic operations on a token, as well as to objects
2144 residing on the token.  A token can be a real hardware token such as a
2145 smart card, or it can be a software component such as Gnome Keyring.
2146 The objects residing on such token can be certificates, public keys,
2147 private keys or even plain data or secret keys.  Of those certificates
2148 and public/private key pairs can be used with GnuTLS.  Its main
2149 advantage is that it allows operations on private key objects such as
2150 decryption and signing without accessing the key itself.
2151
2152 Moreover it can be used to allow all applications in the same operating
2153 system to access shared cryptographic keys and certificates in a uniform
2154 way, as in the following picture.
2155
2156 \0\b[image src="pkcs11-vision.png"\0\b]
2157
2158 5.3.2 Initialization
2159 --------------------
2160
2161 To allow all the GnuTLS applications to access PKCS #11 tokens you can
2162 use a configuration per module, such as
2163 '/etc/pkcs11/modules/mymodule.conf'.  This file has the following
2164 format:
2165
2166      module: /usr/lib/opensc-pkcs11.so
2167
2168 If you use this file, then there is no need for other initialization in
2169 GnuTLS, except for the PIN and token functions.  Those allow retrieving
2170 a PIN when accessing a protected object, such as a private key, as well
2171 as probe the user to insert the token.  All the initialization functions
2172 are below.
2173
2174    * *note gnutls_pkcs11_init::: Global initialization
2175
2176    * *note gnutls_pkcs11_deinit::: Global deinitialization
2177
2178    * *note gnutls_pkcs11_set_token_function::: Sets the token insertion
2179      function
2180
2181    * *note gnutls_pkcs11_set_pin_function::: Sets the PIN request
2182      function
2183
2184    * *note gnutls_pkcs11_add_provider::: Adds an additional PKCS #11
2185      provider
2186
2187 Note that due to limitations of PKCS #11 there might be issues when
2188 multiple libraries are sharing a module.  If this is the case we suggest
2189 to use p11-kit(1) that provides an intermediate module to control access
2190 to resources over the multiple users.
2191
2192 5.3.3 Reading Objects
2193 ---------------------
2194
2195 All PKCS #11 objects are referenced by GnuTLS functions by URLs as
2196 described in 'draft-pechanec-pkcs11uri-03'.  For example a public key on
2197 a smart card may be referenced as:
2198
2199      pkcs11:token=Nikos;serial=307521161601031;model=PKCS%2315; \
2200      manufacturer=EnterSafe;object=test1;objecttype=public;\
2201      id=32:f1:53:f3:e3:79:90:b0:86:24:14:10:77:ca:5d:ec:2d:15:fa:ed
2202
2203 while the smart card itself can be referenced as:
2204      pkcs11:token=Nikos;serial=307521161601031;model=PKCS%2315;manufacturer=EnterSafe
2205
2206 Objects can be accessed with the following functions
2207
2208    * *note gnutls_pkcs11_obj_init::: Initializes an object
2209
2210    * *note gnutls_pkcs11_obj_import_url::: To import an object from a
2211      url
2212
2213    * *note gnutls_pkcs11_obj_export_url::: To export the URL of the
2214      object
2215
2216    * *note gnutls_pkcs11_obj_deinit::: To deinitialize an object
2217
2218    * *note gnutls_pkcs11_obj_export::: To export data associated with
2219      object
2220
2221    * *note gnutls_pkcs11_obj_get_info::: To obtain information about an
2222      object
2223
2224    * *note gnutls_pkcs11_obj_list_import_url::: To mass load of objects
2225
2226    * *note gnutls_x509_crt_import_pkcs11::: Import a certificate object
2227
2228    * *note gnutls_x509_crt_import_pkcs11_url::: Helper function to
2229      directly import a URL into a certificate
2230
2231    * *note gnutls_x509_crt_list_import_pkcs11::: Mass import of
2232      certificates
2233
2234 Functions that relate to token handling are shown below
2235
2236    * *note gnutls_pkcs11_token_init::: Initializes a token
2237
2238    * *note gnutls_pkcs11_token_set_pin::: Sets the token user's PIN
2239
2240    * *note gnutls_pkcs11_token_get_url::: Returns the URL of a token
2241
2242    * *note gnutls_pkcs11_token_get_info::: Obtain information about a
2243      token
2244
2245    * *note gnutls_pkcs11_token_get_flags::: Returns flags about a token
2246      (i.e.  hardware or software)
2247
2248 The following example will list all tokens.
2249 int i;
2250 char* url;
2251
2252 gnutls_global_init();
2253
2254 for (i=0;;i++) {
2255         ret = gnutls_pkcs11_token_get_url(i, &url);
2256         if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
2257                 break;
2258
2259         if (ret < 0)
2260                 exit(1);
2261
2262         fprintf(stdout, "Token[%d]: URL: %s\n", i, url);
2263         gnutls_free(url);
2264 }
2265 gnutls_global_deinit();
2266
2267 The next one will list all certificates in a token, that have a
2268 corresponding private key:
2269 gnutls_pkcs11_obj_t *obj_list;
2270 unsigned int obj_list_size = 0;
2271 gnutls_datum_t cinfo;
2272 int i;
2273
2274 obj_list_size = 0;
2275 ret = gnutls_pkcs11_obj_list_import_url( obj_list, NULL, url, \
2276                         GNUTLS_PKCS11_OBJ_ATTR_CRT_WITH_PRIVKEY);
2277 if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
2278         exit(1);
2279
2280 /* no error checking from now on */
2281 obj_list = malloc(sizeof(*obj_list)*obj_list_size);
2282
2283 gnutls_pkcs11_obj_list_import_url( obj_list, &obj_list_size, url, flags);
2284
2285 /* now all certificates are in obj_list */
2286 for (i=0;i<obj_list_size;i++) {
2287
2288         gnutls_x509_crt_init(&xcrt);
2289
2290         gnutls_x509_crt_import_pkcs11(xcrt, obj_list[i]);
2291
2292         gnutls_x509_crt_print (xcrt, GNUTLS_CRT_PRINT_FULL, &cinfo);
2293
2294         fprintf(stdout, "cert[%d]:\n %s\n\n", cinfo.data);
2295
2296         gnutls_free(cinfo.data);
2297         gnutls_x509_crt_deinit(&xcrt);
2298 }
2299
2300 5.3.4 Writing Objects
2301 ---------------------
2302
2303 With GnuTLS you can copy existing private keys and certificates to a
2304 token.  This can be achieved with the following functions
2305
2306    * *note gnutls_pkcs11_delete_url::: To delete an object
2307
2308    * *note gnutls_pkcs11_copy_x509_privkey::: To copy a private key to a
2309      token
2310
2311    * *note gnutls_pkcs11_copy_x509_crt::: To copy a certificate to a
2312      token
2313
2314 5.3.5 Using a PKCS #11 token with TLS
2315 -------------------------------------
2316
2317 It is possible to use a PKCS #11 token to a TLS session, as shown in
2318 *note ex:pkcs11-client::.  In addition the following functions can be
2319 used to load PKCS #11 key and certificates.
2320
2321    * *note gnutls_certificate_set_x509_trust_file::: If given a PKCS #11
2322      URL will load the trusted certificates from it.
2323
2324    * *note gnutls_certificate_set_x509_key_file::: Will also load PKCS
2325      #11 URLs for keys and certificates.
2326
2327    ---------- Footnotes ----------
2328
2329    (1) http://p11-glue.freedesktop.org/
2330
2331 \1f
2332 File: gnutls.info,  Node: Abstract data types,  Next: Digital signatures,  Prev: PKCS #11 tokens,  Up: More on certificate authentication
2333
2334 5.4 Abstract data types
2335 =======================
2336
2337 Since there are many forms of a public or private keys supported by
2338 GnuTLS such as X.509, OpenPGP, or PKCS #11 it is desirable to allow
2339 common operations on them.  For these reasons the abstract
2340 'gnutls_privkey_t' and 'gnutls_pubkey_t' were introduced in
2341 'gnutls/abstract.h' header.  Those types are initialized using a
2342 specific type of key and then can be used to perform operations in an
2343 abstract way.  For example in order for someone to sign an X.509
2344 certificate with a key that resides in a smart he has to follow the
2345 steps below:
2346
2347 #inlude <gnutls/abstract.h>
2348 #inlude <gnutls/pkcs11.h>
2349
2350 void sign_cert( gnutls_x509_crt_t to_be_signed)
2351 {
2352 gnutls_pkcs11_privkey_t ca_key;
2353 gnutls_x509_crt_t ca_cert;
2354 gnutls_privkey_t abs_key;
2355
2356         /* load the PKCS #11 key and certificates */
2357         gnutls_pkcs11_privkey_init(&ca_key);
2358         gnutls_pkcs11_privkey_import_url(ca_key, key_url);
2359
2360         gnutls_x509_crt_init(&ca_cert);
2361         gnutls_x509_crt_import_pkcs11_url(&ca_cert, cert_url);
2362
2363         /* initialize the abstract key */
2364         gnutls_privkey_init(&abs_key);
2365         gnutls_privkey_import_pkcs11(abs_key, ca_key);
2366
2367         /* sign the certificate to be signed */
2368         gnutls_x509_crt_privkey_sign(to_be_signed, ca_cert, ca_key, GNUTLS_DIG_SHA1, 0);
2369 }
2370
2371 \1f
2372 File: gnutls.info,  Node: Digital signatures,  Prev: Abstract data types,  Up: More on certificate authentication
2373
2374 5.5 Digital Signatures
2375 ======================
2376
2377 In this section we will provide some information about digital
2378 signatures, how they work, and give the rationale for disabling some of
2379 the algorithms used.
2380
2381 Digital signatures work by using somebody's secret key to sign some
2382 arbitrary data.  Then anybody else could use the public key of that
2383 person to verify the signature.  Since the data may be arbitrary it is
2384 not suitable input to a cryptographic digital signature algorithm.  For
2385 this reason and also for performance cryptographic hash algorithms are
2386 used to preprocess the input to the signature algorithm.  This works as
2387 long as it is difficult enough to generate two different messages with
2388 the same hash algorithm output.  In that case the same signature could
2389 be used as a proof for both messages.  Nobody wants to sign an innocent
2390 message of donating 1 euro to Greenpeace and find out that he donated
2391 1.000.000 euros to Bad Inc.
2392
2393 For a hash algorithm to be called cryptographic the following three
2394 requirements must hold:
2395
2396   1. Preimage resistance.  That means the algorithm must be one way and
2397      given the output of the hash function H(x), it is impossible to
2398      calculate x.
2399
2400   2. 2nd preimage resistance.  That means that given a pair x,y with
2401      y=H(x) it is impossible to calculate an x' such that y=H(x').
2402
2403   3. Collision resistance.  That means that it is impossible to
2404      calculate random x and x' such H(x')=H(x).
2405
2406 The last two requirements in the list are the most important in digital
2407 signatures.  These protect against somebody who would like to generate
2408 two messages with the same hash output.  When an algorithm is considered
2409 broken usually it means that the Collision resistance of the algorithm
2410 is less than brute force.  Using the birthday paradox the brute force
2411 attack takes 2^{((hash size) / 2)} operations.  Today colliding
2412 certificates using the MD5 hash algorithm have been generated as shown
2413 in [WEGER] (*note Bibliography::).
2414
2415 There has been cryptographic results for the SHA-1 hash algorithms as
2416 well, although they are not yet critical.  Before 2004, MD5 had a
2417 presumed collision strength of 2^{64}, but it has been showed to have a
2418 collision strength well under 2^{50}.  As of November 2005, it is
2419 believed that SHA-1's collision strength is around 2^{63}.  We consider
2420 this sufficiently hard so that we still support SHA-1.  We anticipate
2421 that SHA-256/386/512 will be used in publicly-distributed certificates
2422 in the future.  When 2^{63} can be considered too weak compared to the
2423 computer power available sometime in the future, SHA-1 will be disabled
2424 as well.  The collision attacks on SHA-1 may also get better, given the
2425 new interest in tools for creating them.
2426
2427 5.5.1 Trading Security for Interoperability
2428 -------------------------------------------
2429
2430 If you connect to a server and use GnuTLS' functions to verify the
2431 certificate chain, and get a *note GNUTLS_CERT_INSECURE_ALGORITHM::
2432 validation error (*note Verifying X.509 certificate paths::), it means
2433 that somewhere in the certificate chain there is a certificate signed
2434 using 'RSA-MD2' or 'RSA-MD5'.  These two digital signature algorithms
2435 are considered broken, so GnuTLS fail when attempting to verify the
2436 certificate.  In some situations, it may be useful to be able to verify
2437 the certificate chain anyway, assuming an attacker did not utilize the
2438 fact that these signatures algorithms are broken.  This section will
2439 give help on how to achieve that.
2440
2441 First, it is important to know that you do not have to enable any of the
2442 flags discussed here to be able to use trusted root CA certificates
2443 signed using 'RSA-MD2' or 'RSA-MD5'.  The only attack today is that it
2444 is possible to generate certificates with colliding signatures
2445 (collision resistance); you cannot generate a certificate that has the
2446 same signature as an already existing signature (2nd preimage
2447 resistance).
2448
2449 If you are using *note gnutls_certificate_verify_peers2:: to verify the
2450 certificate chain, you can call *note
2451 gnutls_certificate_set_verify_flags:: with the
2452 'GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2' or 'GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5'
2453 flag, as in:
2454
2455        gnutls_certificate_set_verify_flags (x509cred,
2456                                             GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
2457
2458 This will tell the verifier algorithm to enable 'RSA-MD5' when verifying
2459 the certificates.
2460
2461 If you are using *note gnutls_x509_crt_verify:: or *note
2462 gnutls_x509_crt_list_verify::, you can pass the
2463 'GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5' parameter directly in the 'flags'
2464 parameter.
2465
2466 If you are using these flags, it may also be a good idea to warn the
2467 user when verification failure occur for this reason.  The simplest is
2468 to not use the flags by default, and only fall back to using them after
2469 warning the user.  If you wish to inspect the certificate chain
2470 yourself, you can use *note gnutls_certificate_get_peers:: to extract
2471 the raw server's certificate chain, then use *note
2472 gnutls_x509_crt_import:: to parse each of the certificates, and then use
2473 *note gnutls_x509_crt_get_signature_algorithm:: to find out the signing
2474 algorithm used for each certificate.  If any of the intermediary
2475 certificates are using 'GNUTLS_SIGN_RSA_MD2' or 'GNUTLS_SIGN_RSA_MD5',
2476 you could present a warning.
2477
2478 \1f
2479 File: gnutls.info,  Node: How to use TLS in application protocols,  Next: How to use GnuTLS in applications,  Prev: More on certificate authentication,  Up: Top
2480
2481 6 How To Use TLS in Application Protocols
2482 *****************************************
2483
2484 This chapter is intended to provide some hints on how to use the TLS
2485 over simple custom made application protocols.  The discussion below
2486 mainly refers to the _TCP/IP_ transport layer but may be extended to
2487 other ones too.
2488
2489 * Menu:
2490
2491 * Separate ports::
2492 * Upward negotiation::
2493
2494 \1f
2495 File: gnutls.info,  Node: Separate ports,  Next: Upward negotiation,  Up: How to use TLS in application protocols
2496
2497 6.1 Separate Ports
2498 ==================
2499
2500 Traditionally SSL was used in application protocols by assigning a new
2501 port number for the secure services.  That way two separate ports were
2502 assigned, one for the non secure sessions, and one for the secured ones.
2503 This has the benefit that if a user requests a secure session then the
2504 client will try to connect to the secure port and fail otherwise.  The
2505 only possible attack with this method is a denial of service one.  The
2506 most famous example of this method is the famous "HTTP over TLS" or
2507 HTTPS protocol [RFC2818] (*note Bibliography::).
2508
2509 Despite its wide use, this method is not as good as it seems.  This
2510 approach starts the TLS Handshake procedure just after the client
2511 connects on the --so called-- secure port.  That way the TLS protocol
2512 does not know anything about the client, and popular methods like the
2513 host advertising in HTTP do not work(1).  There is no way for the client
2514 to say "I connected to YYY server" before the Handshake starts, so the
2515 server cannot possibly know which certificate to use.
2516
2517 Other than that it requires two separate ports to run a single service,
2518 which is unnecessary complication.  Due to the fact that there is a
2519 limitation on the available privileged ports, this approach was soon
2520 obsoleted.
2521
2522    ---------- Footnotes ----------
2523
2524    (1) See also the Server Name Indication extension on *note
2525 serverind::.
2526
2527 \1f
2528 File: gnutls.info,  Node: Upward negotiation,  Prev: Separate ports,  Up: How to use TLS in application protocols
2529
2530 6.2 Upward Negotiation
2531 ======================
2532
2533 Other application protocols(1) use a different approach to enable the
2534 secure layer.  They use something called the "TLS upgrade" method.  This
2535 method is quite tricky but it is more flexible.  The idea is to extend
2536 the application protocol to have a "STARTTLS" request, whose purpose it
2537 to start the TLS protocols just after the client requests it.  This is a
2538 really neat idea and does not require an extra port.
2539
2540 This method is used by almost all modern protocols and there is even the
2541 [RFC2817] (*note Bibliography::) paper which proposes extensions to HTTP
2542 to support it.
2543
2544 The tricky part, in this method, is that the "STARTTLS" request is sent
2545 in the clear, thus is vulnerable to modifications.  A typical attack is
2546 to modify the messages in a way that the client is fooled and thinks
2547 that the server does not have the "STARTTLS" capability.  See a typical
2548 conversation of a hypothetical protocol:
2549
2550      (client connects to the server)
2551
2552      CLIENT: HELLO I'M MR. XXX
2553
2554      SERVER: NICE TO MEET YOU XXX
2555
2556      CLIENT: PLEASE START TLS
2557
2558      SERVER: OK
2559
2560      *** TLS STARTS
2561
2562      CLIENT: HERE ARE SOME CONFIDENTIAL DATA
2563
2564 And see an example of a conversation where someone is acting in between:
2565
2566      (client connects to the server)
2567
2568      CLIENT: HELLO I'M MR. XXX
2569
2570      SERVER: NICE TO MEET YOU XXX
2571
2572      CLIENT: PLEASE START TLS
2573
2574      (here someone inserts this message)
2575
2576      SERVER: SORRY I DON'T HAVE THIS CAPABILITY
2577
2578      CLIENT: HERE ARE SOME CONFIDENTIAL DATA
2579
2580 As you can see above the client was fooled, and was dummy enough to send
2581 the confidential data in the clear.
2582
2583 How to avoid the above attack?  As you may have already thought this one
2584 is easy to avoid.  The client has to ask the user before it connects
2585 whether the user requests TLS or not.  If the user answered that he
2586 certainly wants the secure layer the last conversation should be:
2587
2588      (client connects to the server)
2589
2590      CLIENT: HELLO I'M MR. XXX
2591
2592      SERVER: NICE TO MEET YOU XXX
2593
2594      CLIENT: PLEASE START TLS
2595
2596      (here someone inserts this message)
2597
2598      SERVER: SORRY I DON'T HAVE THIS CAPABILITY
2599
2600      CLIENT: BYE
2601
2602      (the client notifies the user that the secure connection was not
2603      possible)
2604
2605 This method, if implemented properly, is far better than the traditional
2606 method, and the security properties remain the same, since only denial
2607 of service is possible.  The benefit is that the server may request
2608 additional data before the TLS Handshake protocol starts, in order to
2609 send the correct certificate, use the correct password file(2), or
2610 anything else!
2611
2612    ---------- Footnotes ----------
2613
2614    (1) See LDAP, IMAP etc.
2615
2616    (2) in SRP authentication
2617
2618 \1f
2619 File: gnutls.info,  Node: How to use GnuTLS in applications,  Next: Included programs,  Prev: How to use TLS in application protocols,  Up: Top
2620
2621 7 How To Use GnuTLS in Applications
2622 ***********************************
2623
2624 * Menu:
2625
2626 * Preparation::
2627 * Multi-threaded applications::
2628 * Client examples::
2629 * Server examples::
2630 * Miscellaneous examples::
2631 * Parameter generation::
2632 * Keying Material Exporters::
2633 * Channel Bindings::
2634 * Compatibility with the OpenSSL library::
2635
2636 \1f
2637 File: gnutls.info,  Node: Preparation,  Next: Multi-threaded applications,  Up: How to use GnuTLS in applications
2638
2639 7.1 Preparation
2640 ===============
2641
2642 To use GnuTLS, you have to perform some changes to your sources and your
2643 build system.  The necessary changes are explained in the following
2644 subsections.
2645
2646 * Menu:
2647
2648 * Headers::
2649 * Initialization::
2650 * Version check::
2651 * Debugging::
2652 * Building the source::
2653
2654 \1f
2655 File: gnutls.info,  Node: Headers,  Next: Initialization,  Up: Preparation
2656
2657 7.1.1 Headers
2658 -------------
2659
2660 All the data types and functions of the GnuTLS library are defined in
2661 the header file 'gnutls/gnutls.h'.  This must be included in all
2662 programs that make use of the GnuTLS library.
2663
2664 The extra functionality of the GnuTLS-extra library is available by
2665 including the header file 'gnutls/extra.h' in your programs.
2666
2667 \1f
2668 File: gnutls.info,  Node: Initialization,  Next: Version check,  Prev: Headers,  Up: Preparation
2669
2670 7.1.2 Initialization
2671 --------------------
2672
2673 GnuTLS must be initialized before it can be used.  The library is
2674 initialized by calling *note gnutls_global_init::.  The resources
2675 allocated by the initialization process can be released if the
2676 application no longer has a need to call GnuTLS functions, this is done
2677 by calling *note gnutls_global_deinit::.
2678
2679 The extra functionality of the GnuTLS-extra library is available after
2680 calling *note gnutls_global_init_extra::.
2681
2682 In order to take advantage of the internationalisation features in
2683 GnuTLS, such as translated error messages, the application must set the
2684 current locale using 'setlocale' before initializing GnuTLS.
2685
2686 \1f
2687 File: gnutls.info,  Node: Version check,  Next: Debugging,  Prev: Initialization,  Up: Preparation
2688
2689 7.1.3 Version Check
2690 -------------------
2691
2692 It is often desirable to check that the version of 'gnutls' used is
2693 indeed one which fits all requirements.  Even with binary compatibility
2694 new features may have been introduced but due to problem with the
2695 dynamic linker an old version is actually used.  So you may want to
2696 check that the version is okay right after program startup.  See the
2697 function *note gnutls_check_version::.
2698
2699 \1f
2700 File: gnutls.info,  Node: Debugging,  Next: Building the source,  Prev: Version check,  Up: Preparation
2701
2702 7.1.4 Debugging
2703 ---------------
2704
2705 In many cases things may not go as expected and further information, to
2706 assist debugging, from GnuTLS is desired.  Those are the case where the
2707 *note gnutls_global_set_log_level:: and *note
2708 gnutls_global_set_log_function:: are to be used.  Those will print
2709 verbose information on the GnuTLS functions internal flow.
2710
2711 \1f
2712 File: gnutls.info,  Node: Building the source,  Prev: Debugging,  Up: Preparation
2713
2714 7.1.5 Building the Source
2715 -------------------------
2716
2717 If you want to compile a source file including the 'gnutls/gnutls.h'
2718 header file, you must make sure that the compiler can find it in the
2719 directory hierarchy.  This is accomplished by adding the path to the
2720 directory in which the header file is located to the compilers include
2721 file search path (via the '-I' option).
2722
2723 However, the path to the include file is determined at the time the
2724 source is configured.  To solve this problem, the library uses the
2725 external package 'pkg-config' that knows the path to the include file
2726 and other configuration options.  The options that need to be added to
2727 the compiler invocation at compile time are output by the '--cflags'
2728 option to 'pkg-config gnutls'.  The following example shows how it can
2729 be used at the command line:
2730
2731      gcc -c foo.c `pkg-config gnutls --cflags`
2732
2733 Adding the output of 'pkg-config gnutls --cflags' to the compilers
2734 command line will ensure that the compiler can find the
2735 'gnutls/gnutls.h' header file.
2736
2737 A similar problem occurs when linking the program with the library.
2738 Again, the compiler has to find the library files.  For this to work,
2739 the path to the library files has to be added to the library search path
2740 (via the '-L' option).  For this, the option '--libs' to 'pkg-config
2741 gnutls' can be used.  For convenience, this option also outputs all
2742 other options that are required to link the program with the libarary
2743 (for instance, the '-ltasn1' option).  The example shows how to link
2744 'foo.o' with the library to a program 'foo'.
2745
2746      gcc -o foo foo.o `pkg-config gnutls --libs`
2747
2748 Of course you can also combine both examples to a single command by
2749 specifying both options to 'pkg-config':
2750
2751      gcc -o foo foo.c `pkg-config gnutls --cflags --libs`
2752
2753 \1f
2754 File: gnutls.info,  Node: Multi-threaded applications,  Next: Client examples,  Prev: Preparation,  Up: How to use GnuTLS in applications
2755
2756 7.2 Multi-Threaded Applications
2757 ===============================
2758
2759 Although the GnuTLS library is thread safe by design, some parts of the
2760 cryptographic backend, such as the random generator, are not.
2761 Applications can either call *note gnutls_global_init:: and use the
2762 default operating system provided locks (i.e.  'pthreads' on GNU/Linux),
2763 or specify manualy the locking system using the function *note
2764 gnutls_global_set_mutex:: before calling *note gnutls_global_init::.
2765 Setting manually mutexes is recommended only to applications that have
2766 full control of the underlying libraries.  If this is not the case, the
2767 use of the operating system defaults is recommended.
2768
2769 There are helper macros to help you properly initialize the libraries.
2770 Examples are shown below.
2771
2772    * POSIX threads in GNU/Linux
2773           #include <gnutls.h>
2774           #include <errno.h>
2775           #include <pthread.h>
2776
2777           int main()
2778           {
2779              gnutls_global_init();
2780           }
2781
2782    * Other thread packages
2783
2784           int main()
2785           {
2786              gnutls_global_mutex_set (mutex_init, mutex_deinit, mutex_lock, mutex_unlock);
2787              gnutls_global_init();
2788           }
2789
2790 \1f
2791 File: gnutls.info,  Node: Client examples,  Next: Server examples,  Prev: Multi-threaded applications,  Up: How to use GnuTLS in applications
2792
2793 7.3 Client Examples
2794 ===================
2795
2796 This section contains examples of TLS and SSL clients, using GnuTLS.
2797 Note that these examples contain little or no error checking.  Some of
2798 the examples require functions implemented by another example.
2799
2800 * Menu:
2801
2802 * Simple client example with anonymous authentication::
2803 * Simple client example with X.509 certificate support::
2804 * Obtaining session information::
2805 * Verifying peer's certificate::
2806 * Using a callback to select the certificate to use::
2807 * Client using a PKCS #11 token with TLS::
2808 * Client with Resume capability example::
2809 * Simple client example with SRP authentication::
2810 * Simple client example in C++::
2811 * Helper function for TCP connections::
2812
2813 \1f
2814 File: gnutls.info,  Node: Simple client example with anonymous authentication,  Next: Simple client example with X.509 certificate support,  Up: Client examples
2815
2816 7.3.1 Simple Client Example with Anonymous Authentication
2817 ---------------------------------------------------------
2818
2819 The simplest client using TLS is the one that doesn't do any
2820 authentication.  This means no external certificates or passwords are
2821 needed to set up the connection.  As could be expected, the connection
2822 is vulnerable to man-in-the-middle (active or redirection) attacks.
2823 However, the data is integrity and privacy protected.
2824
2825 /* This example code is placed in the public domain. */
2826
2827 #ifdef HAVE_CONFIG_H
2828 #include <config.h>
2829 #endif
2830
2831 #include <stdio.h>
2832 #include <stdlib.h>
2833 #include <string.h>
2834 #include <sys/types.h>
2835 #include <sys/socket.h>
2836 #include <arpa/inet.h>
2837 #include <unistd.h>
2838 #include <gnutls/gnutls.h>
2839
2840 /* A very basic TLS client, with anonymous authentication.
2841  */
2842
2843 #define MAX_BUF 1024
2844 #define MSG "GET / HTTP/1.0\r\n\r\n"
2845
2846 extern int tcp_connect (void);
2847 extern void tcp_close (int sd);
2848
2849 int
2850 main (void)
2851 {
2852   int ret, sd, ii;
2853   gnutls_session_t session;
2854   char buffer[MAX_BUF + 1];
2855   gnutls_anon_client_credentials_t anoncred;
2856   /* Need to enable anonymous KX specifically. */
2857
2858   gnutls_global_init ();
2859
2860   gnutls_anon_allocate_client_credentials (&anoncred);
2861
2862   /* Initialize TLS session
2863    */
2864   gnutls_init (&session, GNUTLS_CLIENT);
2865
2866   /* Use default priorities */
2867   gnutls_priority_set_direct (session, "PERFORMANCE:+ANON-DH:!ARCFOUR-128",
2868                               NULL);
2869
2870   /* put the anonymous credentials to the current session
2871    */
2872   gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
2873
2874   /* connect to the peer
2875    */
2876   sd = tcp_connect ();
2877
2878   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
2879
2880   /* Perform the TLS handshake
2881    */
2882   ret = gnutls_handshake (session);
2883
2884   if (ret < 0)
2885     {
2886       fprintf (stderr, "*** Handshake failed\n");
2887       gnutls_perror (ret);
2888       goto end;
2889     }
2890   else
2891     {
2892       printf ("- Handshake was completed\n");
2893     }
2894
2895   gnutls_record_send (session, MSG, strlen (MSG));
2896
2897   ret = gnutls_record_recv (session, buffer, MAX_BUF);
2898   if (ret == 0)
2899     {
2900       printf ("- Peer has closed the TLS connection\n");
2901       goto end;
2902     }
2903   else if (ret < 0)
2904     {
2905       fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
2906       goto end;
2907     }
2908
2909   printf ("- Received %d bytes: ", ret);
2910   for (ii = 0; ii < ret; ii++)
2911     {
2912       fputc (buffer[ii], stdout);
2913     }
2914   fputs ("\n", stdout);
2915
2916   gnutls_bye (session, GNUTLS_SHUT_RDWR);
2917
2918 end:
2919
2920   tcp_close (sd);
2921
2922   gnutls_deinit (session);
2923
2924   gnutls_anon_free_client_credentials (anoncred);
2925
2926   gnutls_global_deinit ();
2927
2928   return 0;
2929 }
2930
2931 \1f
2932 File: gnutls.info,  Node: Simple client example with X.509 certificate support,  Next: Obtaining session information,  Prev: Simple client example with anonymous authentication,  Up: Client examples
2933
2934 7.3.2 Simple Client Example with X.509 Certificate Support
2935 ----------------------------------------------------------
2936
2937 Let's assume now that we want to create a TCP client which communicates
2938 with servers that use X.509 or OpenPGP certificate authentication.  The
2939 following client is a very simple TLS client, it does not support
2940 session resuming, not even certificate verification.  The TCP functions
2941 defined in this example are used in most of the other examples below,
2942 without redefining them.
2943
2944 /* This example code is placed in the public domain. */
2945
2946 #ifdef HAVE_CONFIG_H
2947 #include <config.h>
2948 #endif
2949
2950 #include <stdio.h>
2951 #include <stdlib.h>
2952 #include <string.h>
2953 #include <sys/types.h>
2954 #include <sys/socket.h>
2955 #include <arpa/inet.h>
2956 #include <unistd.h>
2957 #include <gnutls/gnutls.h>
2958
2959 /* A very basic TLS client, with X.509 authentication.
2960  */
2961
2962 #define MAX_BUF 1024
2963 #define CAFILE "ca.pem"
2964 #define MSG "GET / HTTP/1.0\r\n\r\n"
2965
2966 extern int tcp_connect (void);
2967 extern void tcp_close (int sd);
2968
2969 int
2970 main (void)
2971 {
2972   int ret, sd, ii;
2973   gnutls_session_t session;
2974   char buffer[MAX_BUF + 1];
2975   const char *err;
2976   gnutls_certificate_credentials_t xcred;
2977
2978   gnutls_global_init ();
2979
2980   /* X509 stuff */
2981   gnutls_certificate_allocate_credentials (&xcred);
2982
2983   /* sets the trusted cas file
2984    */
2985   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
2986
2987   /* Initialize TLS session
2988    */
2989   gnutls_init (&session, GNUTLS_CLIENT);
2990
2991   /* Use default priorities */
2992   ret = gnutls_priority_set_direct (session, "PERFORMANCE", &err);
2993   if (ret < 0)
2994     {
2995       if (ret == GNUTLS_E_INVALID_REQUEST)
2996         {
2997           fprintf (stderr, "Syntax error at: %s\n", err);
2998         }
2999       exit (1);
3000     }
3001
3002   /* put the x509 credentials to the current session
3003    */
3004   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
3005
3006   /* connect to the peer
3007    */
3008   sd = tcp_connect ();
3009
3010   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
3011
3012   /* Perform the TLS handshake
3013    */
3014   ret = gnutls_handshake (session);
3015
3016   if (ret < 0)
3017     {
3018       fprintf (stderr, "*** Handshake failed\n");
3019       gnutls_perror (ret);
3020       goto end;
3021     }
3022   else
3023     {
3024       printf ("- Handshake was completed\n");
3025     }
3026
3027   gnutls_record_send (session, MSG, strlen (MSG));
3028
3029   ret = gnutls_record_recv (session, buffer, MAX_BUF);
3030   if (ret == 0)
3031     {
3032       printf ("- Peer has closed the TLS connection\n");
3033       goto end;
3034     }
3035   else if (ret < 0)
3036     {
3037       fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
3038       goto end;
3039     }
3040
3041   printf ("- Received %d bytes: ", ret);
3042   for (ii = 0; ii < ret; ii++)
3043     {
3044       fputc (buffer[ii], stdout);
3045     }
3046   fputs ("\n", stdout);
3047
3048   gnutls_bye (session, GNUTLS_SHUT_RDWR);
3049
3050 end:
3051
3052   tcp_close (sd);
3053
3054   gnutls_deinit (session);
3055
3056   gnutls_certificate_free_credentials (xcred);
3057
3058   gnutls_global_deinit ();
3059
3060   return 0;
3061 }
3062
3063 \1f
3064 File: gnutls.info,  Node: Obtaining session information,  Next: Verifying peer's certificate,  Prev: Simple client example with X.509 certificate support,  Up: Client examples
3065
3066 7.3.3 Obtaining Session Information
3067 -----------------------------------
3068
3069 Most of the times it is desirable to know the security properties of the
3070 current established session.  This includes the underlying ciphers and
3071 the protocols involved.  That is the purpose of the following function.
3072 Note that this function will print meaningful values only if called
3073 after a successful *note gnutls_handshake::.
3074
3075 /* This example code is placed in the public domain. */
3076
3077 #ifdef HAVE_CONFIG_H
3078 #include <config.h>
3079 #endif
3080
3081 #include <stdio.h>
3082 #include <stdlib.h>
3083 #include <gnutls/gnutls.h>
3084 #include <gnutls/x509.h>
3085
3086 #include "examples.h"
3087
3088 /* This function will print some details of the
3089  * given session.
3090  */
3091 int
3092 print_info (gnutls_session_t session)
3093 {
3094   const char *tmp;
3095   gnutls_credentials_type_t cred;
3096   gnutls_kx_algorithm_t kx;
3097
3098   /* print the key exchange's algorithm name
3099    */
3100   kx = gnutls_kx_get (session);
3101   tmp = gnutls_kx_get_name (kx);
3102   printf ("- Key Exchange: %s\n", tmp);
3103
3104   /* Check the authentication type used and switch
3105    * to the appropriate.
3106    */
3107   cred = gnutls_auth_get_type (session);
3108   switch (cred)
3109     {
3110     case GNUTLS_CRD_IA:
3111       printf ("- TLS/IA session\n");
3112       break;
3113
3114
3115 #ifdef ENABLE_SRP
3116     case GNUTLS_CRD_SRP:
3117       printf ("- SRP session with username %s\n",
3118               gnutls_srp_server_get_username (session));
3119       break;
3120 #endif
3121
3122     case GNUTLS_CRD_PSK:
3123       /* This returns NULL in server side.
3124        */
3125       if (gnutls_psk_client_get_hint (session) != NULL)
3126         printf ("- PSK authentication. PSK hint '%s'\n",
3127                 gnutls_psk_client_get_hint (session));
3128       /* This returns NULL in client side.
3129        */
3130       if (gnutls_psk_server_get_username (session) != NULL)
3131         printf ("- PSK authentication. Connected as '%s'\n",
3132                 gnutls_psk_server_get_username (session));
3133       break;
3134
3135     case GNUTLS_CRD_ANON:      /* anonymous authentication */
3136
3137       printf ("- Anonymous DH using prime of %d bits\n",
3138               gnutls_dh_get_prime_bits (session));
3139       break;
3140
3141     case GNUTLS_CRD_CERTIFICATE:       /* certificate authentication */
3142
3143       /* Check if we have been using ephemeral Diffie-Hellman.
3144        */
3145       if (kx == GNUTLS_KX_DHE_RSA || kx == GNUTLS_KX_DHE_DSS)
3146         {
3147           printf ("\n- Ephemeral DH using prime of %d bits\n",
3148                   gnutls_dh_get_prime_bits (session));
3149         }
3150
3151       /* if the certificate list is available, then
3152        * print some information about it.
3153        */
3154       print_x509_certificate_info (session);
3155
3156     }                           /* switch */
3157
3158   /* print the protocol's name (ie TLS 1.0)
3159    */
3160   tmp = gnutls_protocol_get_name (gnutls_protocol_get_version (session));
3161   printf ("- Protocol: %s\n", tmp);
3162
3163   /* print the certificate type of the peer.
3164    * ie X.509
3165    */
3166   tmp =
3167     gnutls_certificate_type_get_name (gnutls_certificate_type_get (session));
3168
3169   printf ("- Certificate Type: %s\n", tmp);
3170
3171   /* print the compression algorithm (if any)
3172    */
3173   tmp = gnutls_compression_get_name (gnutls_compression_get (session));
3174   printf ("- Compression: %s\n", tmp);
3175
3176   /* print the name of the cipher used.
3177    * ie 3DES.
3178    */
3179   tmp = gnutls_cipher_get_name (gnutls_cipher_get (session));
3180   printf ("- Cipher: %s\n", tmp);
3181
3182   /* Print the MAC algorithms name.
3183    * ie SHA1
3184    */
3185   tmp = gnutls_mac_get_name (gnutls_mac_get (session));
3186   printf ("- MAC: %s\n", tmp);
3187
3188   return 0;
3189 }
3190
3191 \1f
3192 File: gnutls.info,  Node: Verifying peer's certificate,  Next: Using a callback to select the certificate to use,  Prev: Obtaining session information,  Up: Client examples
3193
3194 7.3.4 Verifying Peer's Certificate
3195 ----------------------------------
3196
3197 A TLS session is not secure just after the handshake procedure has
3198 finished.  It must be considered secure, only after the peer's
3199 certificate and identity have been verified.  That is, you have to
3200 verify the signature in peer's certificate, the hostname in the
3201 certificate, and expiration dates.  Just after this step you should
3202 treat the connection as being a secure one.
3203
3204 /* This example code is placed in the public domain. */
3205
3206 #ifdef HAVE_CONFIG_H
3207 #include <config.h>
3208 #endif
3209
3210 #include <stdio.h>
3211 #include <stdlib.h>
3212 #include <string.h>
3213 #include <gnutls/gnutls.h>
3214 #include <gnutls/x509.h>
3215 #include "examples.h"
3216
3217 /* A very basic TLS client, with X.509 authentication and server certificate
3218  * verification.
3219  */
3220
3221 #define MAX_BUF 1024
3222 #define CAFILE "ca.pem"
3223 #define MSG "GET / HTTP/1.0\r\n\r\n"
3224
3225 extern int tcp_connect (void);
3226 extern void tcp_close (int sd);
3227
3228 /* This function will try to verify the peer's certificate, and
3229  * also check if the hostname matches, and the activation, expiration dates.
3230  */
3231 static int
3232 verify_certificate_callback (gnutls_session_t session)
3233 {
3234   unsigned int status;
3235   const gnutls_datum_t *cert_list;
3236   unsigned int cert_list_size;
3237   int ret;
3238   gnutls_x509_crt_t cert;
3239   const char *hostname;
3240
3241   /* read hostname */
3242   hostname = gnutls_session_get_ptr (session);
3243
3244   /* This verification function uses the trusted CAs in the credentials
3245    * structure. So you must have installed one or more CA certificates.
3246    */
3247   ret = gnutls_certificate_verify_peers2 (session, &status);
3248   if (ret < 0)
3249     {
3250       printf ("Error\n");
3251       return GNUTLS_E_CERTIFICATE_ERROR;
3252     }
3253
3254   if (status & GNUTLS_CERT_INVALID)
3255     printf ("The certificate is not trusted.\n");
3256
3257   if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
3258     printf ("The certificate hasn't got a known issuer.\n");
3259
3260   if (status & GNUTLS_CERT_REVOKED)
3261     printf ("The certificate has been revoked.\n");
3262
3263   if (status & GNUTLS_CERT_EXPIRED)
3264     printf ("The certificate has expired\n");
3265
3266   if (status & GNUTLS_CERT_NOT_ACTIVATED)
3267     printf ("The certificate is not yet activated\n");
3268
3269   /* Up to here the process is the same for X.509 certificates and
3270    * OpenPGP keys. From now on X.509 certificates are assumed. This can
3271    * be easily extended to work with openpgp keys as well.
3272    */
3273   if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509)
3274     return GNUTLS_E_CERTIFICATE_ERROR;
3275
3276   if (gnutls_x509_crt_init (&cert) < 0)
3277     {
3278       printf ("error in initialization\n");
3279       return GNUTLS_E_CERTIFICATE_ERROR;
3280     }
3281
3282   cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
3283   if (cert_list == NULL)
3284     {
3285       printf ("No certificate was found!\n");
3286       return GNUTLS_E_CERTIFICATE_ERROR;
3287     }
3288
3289   /* This is not a real world example, since we only check the first
3290    * certificate in the given chain.
3291    */
3292   if (gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
3293     {
3294       printf ("error parsing certificate\n");
3295       return GNUTLS_E_CERTIFICATE_ERROR;
3296     }
3297
3298
3299   if (!gnutls_x509_crt_check_hostname (cert, hostname))
3300     {
3301       printf ("The certificate's owner does not match hostname '%s'\n",
3302               hostname);
3303       return GNUTLS_E_CERTIFICATE_ERROR;
3304     }
3305
3306   gnutls_x509_crt_deinit (cert);
3307
3308   /* notify gnutls to continue handshake normally */
3309   return 0;
3310 }
3311
3312
3313 int
3314 main (void)
3315 {
3316   int ret, sd, ii;
3317   gnutls_session_t session;
3318   char buffer[MAX_BUF + 1];
3319   const char *err;
3320   gnutls_certificate_credentials_t xcred;
3321
3322   gnutls_global_init ();
3323
3324   /* X509 stuff */
3325   gnutls_certificate_allocate_credentials (&xcred);
3326
3327   /* sets the trusted cas file
3328    */
3329   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
3330   gnutls_certificate_set_verify_function (xcred, verify_certificate_callback);
3331   gnutls_certificate_set_verify_flags (xcred,
3332                                        GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
3333
3334   /* Initialize TLS session
3335    */
3336   gnutls_init (&session, GNUTLS_CLIENT);
3337
3338   gnutls_session_set_ptr (session, (void *) "my_host_name");
3339
3340   /* Use default priorities */
3341   ret = gnutls_priority_set_direct (session, "PERFORMANCE", &err);
3342   if (ret < 0)
3343     {
3344       if (ret == GNUTLS_E_INVALID_REQUEST)
3345         {
3346           fprintf (stderr, "Syntax error at: %s\n", err);
3347         }
3348       exit (1);
3349     }
3350
3351   /* put the x509 credentials to the current session
3352    */
3353   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
3354
3355   /* connect to the peer
3356    */
3357   sd = tcp_connect ();
3358
3359   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
3360
3361   /* Perform the TLS handshake
3362    */
3363   ret = gnutls_handshake (session);
3364
3365   if (ret < 0)
3366     {
3367       fprintf (stderr, "*** Handshake failed\n");
3368       gnutls_perror (ret);
3369       goto end;
3370     }
3371   else
3372     {
3373       printf ("- Handshake was completed\n");
3374     }
3375
3376   gnutls_record_send (session, MSG, strlen (MSG));
3377
3378   ret = gnutls_record_recv (session, buffer, MAX_BUF);
3379   if (ret == 0)
3380     {
3381       printf ("- Peer has closed the TLS connection\n");
3382       goto end;
3383     }
3384   else if (ret < 0)
3385     {
3386       fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
3387       goto end;
3388     }
3389
3390   printf ("- Received %d bytes: ", ret);
3391   for (ii = 0; ii < ret; ii++)
3392     {
3393       fputc (buffer[ii], stdout);
3394     }
3395   fputs ("\n", stdout);
3396
3397   gnutls_bye (session, GNUTLS_SHUT_RDWR);
3398
3399 end:
3400
3401   tcp_close (sd);
3402
3403   gnutls_deinit (session);
3404
3405   gnutls_certificate_free_credentials (xcred);
3406
3407   gnutls_global_deinit ();
3408
3409   return 0;
3410 }
3411
3412 Another example is listed below which provides more detailed
3413 verification output, for applications that need it.
3414
3415 /* This example code is placed in the public domain. */
3416
3417 #ifdef HAVE_CONFIG_H
3418 #include <config.h>
3419 #endif
3420
3421 #include <stdio.h>
3422 #include <stdlib.h>
3423 #include <gnutls/gnutls.h>
3424 #include <gnutls/x509.h>
3425
3426 #include "examples.h"
3427
3428 /* All the available CRLs
3429  */
3430 gnutls_x509_crl_t *crl_list;
3431 int crl_list_size;
3432
3433 /* All the available trusted CAs
3434  */
3435 gnutls_x509_crt_t *ca_list;
3436 int ca_list_size;
3437
3438 static void verify_cert2 (gnutls_x509_crt_t crt,
3439                           gnutls_x509_crt_t issuer,
3440                           gnutls_x509_crl_t * crl_list, int crl_list_size);
3441 static void verify_last_cert (gnutls_x509_crt_t crt,
3442                               gnutls_x509_crt_t * ca_list, int ca_list_size,
3443                               gnutls_x509_crl_t * crl_list,
3444                               int crl_list_size);
3445
3446
3447 /* This function will try to verify the peer's certificate chain, and
3448  * also check if the hostname matches, and the activation, expiration dates.
3449  */
3450 void
3451 verify_certificate_chain (gnutls_session_t session,
3452                           const char *hostname,
3453                           const gnutls_datum_t * cert_chain,
3454                           int cert_chain_length)
3455 {
3456   int i;
3457   gnutls_x509_crt_t *cert;
3458
3459   cert = malloc (sizeof (*cert) * cert_chain_length);
3460
3461   /* Import all the certificates in the chain to
3462    * native certificate format.
3463    */
3464   for (i = 0; i < cert_chain_length; i++)
3465     {
3466       gnutls_x509_crt_init (&cert[i]);
3467       gnutls_x509_crt_import (cert[i], &cert_chain[i], GNUTLS_X509_FMT_DER);
3468     }
3469
3470   /* If the last certificate in the chain is self signed ignore it.
3471    * That is because we want to check against our trusted certificate
3472    * list.
3473    */
3474   if (gnutls_x509_crt_check_issuer (cert[cert_chain_length - 1],
3475                                     cert[cert_chain_length - 1]) > 0
3476       && cert_chain_length > 0)
3477     {
3478       cert_chain_length--;
3479     }
3480
3481   /* Now verify the certificates against their issuers
3482    * in the chain.
3483    */
3484   for (i = 1; i < cert_chain_length; i++)
3485     {
3486       verify_cert2 (cert[i - 1], cert[i], crl_list, crl_list_size);
3487     }
3488
3489   /* Here we must verify the last certificate in the chain against
3490    * our trusted CA list.
3491    */
3492   verify_last_cert (cert[cert_chain_length - 1],
3493                     ca_list, ca_list_size, crl_list, crl_list_size);
3494
3495   /* Check if the name in the first certificate matches our destination!
3496    */
3497   if (!gnutls_x509_crt_check_hostname (cert[0], hostname))
3498     {
3499       printf ("The certificate's owner does not match hostname '%s'\n",
3500               hostname);
3501     }
3502
3503   for (i = 0; i < cert_chain_length; i++)
3504     gnutls_x509_crt_deinit (cert[i]);
3505
3506   return;
3507 }
3508
3509
3510 /* Verifies a certificate against an other certificate
3511  * which is supposed to be it's issuer. Also checks the
3512  * crl_list if the certificate is revoked.
3513  */
3514 static void
3515 verify_cert2 (gnutls_x509_crt_t crt, gnutls_x509_crt_t issuer,
3516               gnutls_x509_crl_t * crl_list, int crl_list_size)
3517 {
3518   unsigned int output;
3519   int ret;
3520   size_t name_size;
3521   char name[64];
3522
3523   /* Print information about the certificates to
3524    * be checked.
3525    */
3526   name_size = sizeof (name);
3527   gnutls_x509_crt_get_dn (crt, name, &name_size);
3528
3529   fprintf (stderr, "\nCertificate: %s\n", name);
3530
3531   name_size = sizeof (name);
3532   gnutls_x509_crt_get_issuer_dn (crt, name, &name_size);
3533
3534   fprintf (stderr, "Issued by: %s\n", name);
3535
3536   /* Get the DN of the issuer cert.
3537    */
3538   name_size = sizeof (name);
3539   gnutls_x509_crt_get_dn (issuer, name, &name_size);
3540
3541   fprintf (stderr, "Checking against: %s\n", name);
3542
3543   /* Do the actual verification.
3544    */
3545   gnutls_x509_crt_verify (crt, &issuer, 1, 0, &output);
3546
3547   if (output & GNUTLS_CERT_INVALID)
3548     {
3549       fprintf (stderr, "Not trusted");
3550
3551       if (output & GNUTLS_CERT_SIGNER_NOT_FOUND)
3552         fprintf (stderr, ": no issuer was found");
3553       if (output & GNUTLS_CERT_SIGNER_NOT_CA)
3554         fprintf (stderr, ": issuer is not a CA");
3555       if (output & GNUTLS_CERT_NOT_ACTIVATED)
3556         fprintf (stderr, ": not yet activated\n");
3557       if (output & GNUTLS_CERT_EXPIRED)
3558         fprintf (stderr, ": expired\n");
3559
3560       fprintf (stderr, "\n");
3561     }
3562   else
3563     fprintf (stderr, "Trusted\n");
3564
3565   /* Check if the certificate is revoked.
3566    */
3567   ret = gnutls_x509_crt_check_revocation (crt, crl_list, crl_list_size);
3568   if (ret == 1)
3569     {                           /* revoked */
3570       fprintf (stderr, "Revoked\n");
3571     }
3572 }
3573
3574
3575 /* Verifies a certificate against our trusted CA list.
3576  * Also checks the crl_list if the certificate is revoked.
3577  */
3578 static void
3579 verify_last_cert (gnutls_x509_crt_t crt,
3580                   gnutls_x509_crt_t * ca_list, int ca_list_size,
3581                   gnutls_x509_crl_t * crl_list, int crl_list_size)
3582 {
3583   unsigned int output;
3584   int ret;
3585   size_t name_size;
3586   char name[64];
3587
3588   /* Print information about the certificates to
3589    * be checked.
3590    */
3591   name_size = sizeof (name);
3592   gnutls_x509_crt_get_dn (crt, name, &name_size);
3593
3594   fprintf (stderr, "\nCertificate: %s\n", name);
3595
3596   name_size = sizeof (name);
3597   gnutls_x509_crt_get_issuer_dn (crt, name, &name_size);
3598
3599   fprintf (stderr, "Issued by: %s\n", name);
3600
3601   /* Do the actual verification.
3602    */
3603   gnutls_x509_crt_verify (crt, ca_list, ca_list_size,
3604                           GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT, &output);
3605
3606   if (output & GNUTLS_CERT_INVALID)
3607     {
3608       fprintf (stderr, "Not trusted");
3609
3610       if (output & GNUTLS_CERT_SIGNER_NOT_CA)
3611         fprintf (stderr, ": Issuer is not a CA\n");
3612       if (output & GNUTLS_CERT_NOT_ACTIVATED)
3613         fprintf (stderr, ": Not yet activated\n");
3614       if (output & GNUTLS_CERT_EXPIRED)
3615         fprintf (stderr, ": Expired\n");
3616       fprintf (stderr, "\n");
3617     }
3618   else
3619     fprintf (stderr, "Trusted\n");
3620
3621
3622   /* Check if the certificate is revoked.
3623    */
3624   ret = gnutls_x509_crt_check_revocation (crt, crl_list, crl_list_size);
3625   if (ret == 1)
3626     {                           /* revoked */
3627       fprintf (stderr, "Revoked\n");
3628     }
3629 }
3630
3631 \1f
3632 File: gnutls.info,  Node: Using a callback to select the certificate to use,  Next: Client using a PKCS #11 token with TLS,  Prev: Verifying peer's certificate,  Up: Client examples
3633
3634 7.3.5 Using a Callback to Select the Certificate to Use
3635 -------------------------------------------------------
3636
3637 There are cases where a client holds several certificate and key pairs,
3638 and may not want to load all of them in the credentials structure.  The
3639 following example demonstrates the use of the certificate selection
3640 callback.
3641
3642 /* This example code is placed in the public domain. */
3643
3644 #ifdef HAVE_CONFIG_H
3645 #include <config.h>
3646 #endif
3647
3648 #include <stdio.h>
3649 #include <stdlib.h>
3650 #include <string.h>
3651 #include <sys/types.h>
3652 #include <sys/socket.h>
3653 #include <arpa/inet.h>
3654 #include <unistd.h>
3655 #include <gnutls/gnutls.h>
3656 #include <gnutls/x509.h>
3657 #include <sys/types.h>
3658 #include <sys/stat.h>
3659 #include <fcntl.h>
3660
3661 /* A TLS client that loads the certificate and key.
3662  */
3663
3664 #define MAX_BUF 1024
3665 #define MSG "GET / HTTP/1.0\r\n\r\n"
3666
3667 #define CERT_FILE "cert.pem"
3668 #define KEY_FILE "key.pem"
3669 #define CAFILE "ca.pem"
3670
3671 extern int tcp_connect (void);
3672 extern void tcp_close (int sd);
3673
3674 static int cert_callback (gnutls_session_t session,
3675                           const gnutls_datum_t * req_ca_rdn, int nreqs,
3676                           const gnutls_pk_algorithm_t * sign_algos,
3677                           int sign_algos_length, gnutls_retr2_st * st);
3678
3679 gnutls_x509_crt_t crt;
3680 gnutls_x509_privkey_t key;
3681
3682 /* Helper functions to load a certificate and key
3683  * files into memory.
3684  */
3685 static gnutls_datum_t
3686 load_file (const char *file)
3687 {
3688   FILE *f;
3689   gnutls_datum_t loaded_file = { NULL, 0 };
3690   long filelen;
3691   void *ptr;
3692
3693   if (!(f = fopen (file, "r"))
3694       || fseek (f, 0, SEEK_END) != 0
3695       || (filelen = ftell (f)) < 0
3696       || fseek (f, 0, SEEK_SET) != 0
3697       || !(ptr = malloc ((size_t) filelen))
3698       || fread (ptr, 1, (size_t) filelen, f) < (size_t) filelen)
3699     {
3700       return loaded_file;
3701     }
3702
3703   loaded_file.data = ptr;
3704   loaded_file.size = (unsigned int) filelen;
3705   return loaded_file;
3706 }
3707
3708 static void
3709 unload_file (gnutls_datum_t data)
3710 {
3711   free (data.data);
3712 }
3713
3714 /* Load the certificate and the private key.
3715  */
3716 static void
3717 load_keys (void)
3718 {
3719   int ret;
3720   gnutls_datum_t data;
3721
3722   data = load_file (CERT_FILE);
3723   if (data.data == NULL)
3724     {
3725       fprintf (stderr, "*** Error loading cert file.\n");
3726       exit (1);
3727     }
3728   gnutls_x509_crt_init (&crt);
3729
3730   ret = gnutls_x509_crt_import (crt, &data, GNUTLS_X509_FMT_PEM);
3731   if (ret < 0)
3732     {
3733       fprintf (stderr, "*** Error loading key file: %s\n",
3734                gnutls_strerror (ret));
3735       exit (1);
3736     }
3737
3738   unload_file (data);
3739
3740   data = load_file (KEY_FILE);
3741   if (data.data == NULL)
3742     {
3743       fprintf (stderr, "*** Error loading key file.\n");
3744       exit (1);
3745     }
3746
3747   gnutls_x509_privkey_init (&key);
3748
3749   ret = gnutls_x509_privkey_import (key, &data, GNUTLS_X509_FMT_PEM);
3750   if (ret < 0)
3751     {
3752       fprintf (stderr, "*** Error loading key file: %s\n",
3753                gnutls_strerror (ret));
3754       exit (1);
3755     }
3756
3757   unload_file (data);
3758
3759 }
3760
3761 int
3762 main (void)
3763 {
3764   int ret, sd, ii;
3765   gnutls_session_t session;
3766   gnutls_priority_t priorities_cache;
3767   char buffer[MAX_BUF + 1];
3768   gnutls_certificate_credentials_t xcred;
3769   /* Allow connections to servers that have OpenPGP keys as well.
3770    */
3771
3772   gnutls_global_init ();
3773
3774   load_keys ();
3775
3776   /* X509 stuff */
3777   gnutls_certificate_allocate_credentials (&xcred);
3778
3779   /* priorities */
3780   gnutls_priority_init (&priorities_cache, "NORMAL", NULL);
3781
3782
3783   /* sets the trusted cas file
3784    */
3785   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
3786
3787   gnutls_certificate_set_retrieve_function (xcred, cert_callback);
3788
3789   /* Initialize TLS session
3790    */
3791   gnutls_init (&session, GNUTLS_CLIENT);
3792
3793   /* Use default priorities */
3794   gnutls_priority_set (session, priorities_cache);
3795
3796   /* put the x509 credentials to the current session
3797    */
3798   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
3799
3800   /* connect to the peer
3801    */
3802   sd = tcp_connect ();
3803
3804   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
3805
3806   /* Perform the TLS handshake
3807    */
3808   ret = gnutls_handshake (session);
3809
3810   if (ret < 0)
3811     {
3812       fprintf (stderr, "*** Handshake failed\n");
3813       gnutls_perror (ret);
3814       goto end;
3815     }
3816   else
3817     {
3818       printf ("- Handshake was completed\n");
3819     }
3820
3821   gnutls_record_send (session, MSG, strlen (MSG));
3822
3823   ret = gnutls_record_recv (session, buffer, MAX_BUF);
3824   if (ret == 0)
3825     {
3826       printf ("- Peer has closed the TLS connection\n");
3827       goto end;
3828     }
3829   else if (ret < 0)
3830     {
3831       fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
3832       goto end;
3833     }
3834
3835   printf ("- Received %d bytes: ", ret);
3836   for (ii = 0; ii < ret; ii++)
3837     {
3838       fputc (buffer[ii], stdout);
3839     }
3840   fputs ("\n", stdout);
3841
3842   gnutls_bye (session, GNUTLS_SHUT_RDWR);
3843
3844 end:
3845
3846   tcp_close (sd);
3847
3848   gnutls_deinit (session);
3849
3850   gnutls_certificate_free_credentials (xcred);
3851   gnutls_priority_deinit (priorities_cache);
3852
3853   gnutls_global_deinit ();
3854
3855   return 0;
3856 }
3857
3858
3859
3860 /* This callback should be associated with a session by calling
3861  * gnutls_certificate_client_set_retrieve_function( session, cert_callback),
3862  * before a handshake.
3863  */
3864
3865 static int
3866 cert_callback (gnutls_session_t session,
3867                const gnutls_datum_t * req_ca_rdn, int nreqs,
3868                const gnutls_pk_algorithm_t * sign_algos,
3869                int sign_algos_length, gnutls_retr2_st * st)
3870 {
3871   char issuer_dn[256];
3872   int i, ret;
3873   size_t len;
3874   gnutls_certificate_type_t type;
3875
3876   /* Print the server's trusted CAs
3877    */
3878   if (nreqs > 0)
3879     printf ("- Server's trusted authorities:\n");
3880   else
3881     printf ("- Server did not send us any trusted authorities names.\n");
3882
3883   /* print the names (if any) */
3884   for (i = 0; i < nreqs; i++)
3885     {
3886       len = sizeof (issuer_dn);
3887       ret = gnutls_x509_rdn_get (&req_ca_rdn[i], issuer_dn, &len);
3888       if (ret >= 0)
3889         {
3890           printf ("   [%d]: ", i);
3891           printf ("%s\n", issuer_dn);
3892         }
3893     }
3894
3895   /* Select a certificate and return it.
3896    * The certificate must be of any of the "sign algorithms"
3897    * supported by the server.
3898    */
3899
3900   type = gnutls_certificate_type_get (session);
3901   if (type == GNUTLS_CRT_X509)
3902     {
3903       /* check if the certificate we are sending is signed
3904        * with an algorithm that the server accepts */
3905       gnutls_sign_algorithm_t cert_algo, req_algo;
3906       int i, match = 0;
3907
3908       ret = gnutls_x509_crt_get_signature_algorithm (crt);
3909       if (ret < 0)
3910         {
3911           /* error reading signature algorithm
3912            */
3913           return -1;
3914         }
3915       cert_algo = ret;
3916
3917       i = 0;
3918       do
3919         {
3920           ret = gnutls_sign_algorithm_get_requested (session, i, &req_algo);
3921           if (ret >= 0 && cert_algo == req_algo)
3922             {
3923               match = 1;
3924               break;
3925             }
3926
3927           /* server has not requested anything specific */
3928           if (i == 0 && ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
3929             {
3930               match = 1;
3931               break;
3932             }
3933           i++;
3934         }
3935       while (ret >= 0);
3936
3937       if (match == 0)
3938         {
3939           printf
3940             ("- Could not find a suitable certificate to send to server\n");
3941           return -1;
3942         }
3943
3944       st->cert_type = type;
3945       st->ncerts = 1;
3946
3947       st->cert.x509 = &crt;
3948       st->key.x509 = key;
3949       st->key_type = GNUTLS_PRIVKEY_X509;
3950
3951       st->deinit_all = 0;
3952     }
3953   else
3954     {
3955       return -1;
3956     }
3957
3958   return 0;
3959
3960 }
3961
3962 \1f
3963 File: gnutls.info,  Node: Client using a PKCS #11 token with TLS,  Next: Client with Resume capability example,  Prev: Using a callback to select the certificate to use,  Up: Client examples
3964
3965 7.3.6 Using a PKCS #11 token with TLS
3966 -------------------------------------
3967
3968 This example will demonstrate how to load keys and certificates from a
3969 PKCS #11 token, and use it with a TLS connection.
3970
3971 /* This example code is placed in the public domain. */
3972
3973 #ifdef HAVE_CONFIG_H
3974 #include <config.h>
3975 #endif
3976
3977 #include <getpass.h>
3978
3979 #include <stdio.h>
3980 #include <stdlib.h>
3981 #include <string.h>
3982 #include <sys/types.h>
3983 #include <sys/socket.h>
3984 #include <arpa/inet.h>
3985 #include <unistd.h>
3986 #include <gnutls/gnutls.h>
3987 #include <gnutls/x509.h>
3988 #include <gnutls/pkcs11.h>
3989 #include <sys/types.h>
3990 #include <sys/stat.h>
3991 #include <fcntl.h>
3992
3993 /* A TLS client that loads the certificate and key.
3994  */
3995
3996 #define MAX_BUF 1024
3997 #define MSG "GET / HTTP/1.0\r\n\r\n"
3998 #define MIN(x,y) (((x)<(y))?(x):(y))
3999
4000 #define CAFILE "ca.pem"
4001 #define KEY_URL "pkcs11:manufacturer=SomeManufacturer;object=Private%20Key" \
4002   ";objecttype=private;id=db:5b:3e:b5:72:33"
4003 #define CERT_URL "pkcs11:manufacturer=SomeManufacturer;object=Certificate;" \
4004   "objecttype=cert;id=db:5b:3e:b5:72:33"
4005
4006 extern int tcp_connect (void);
4007 extern void tcp_close (int sd);
4008
4009 static int cert_callback (gnutls_session_t session,
4010                           const gnutls_datum_t * req_ca_rdn, int nreqs,
4011                           const gnutls_pk_algorithm_t * sign_algos,
4012                           int sign_algos_length, gnutls_retr2_st * st);
4013
4014 gnutls_x509_crt_t crt;
4015 gnutls_pkcs11_privkey_t key;
4016
4017 /* Load the certificate and the private key.
4018  */
4019 static void
4020 load_keys (void)
4021 {
4022   int ret;
4023
4024   gnutls_x509_crt_init (&crt);
4025
4026   ret = gnutls_x509_crt_import_pkcs11_url (crt, CERT_URL, 0);
4027
4028   /* some tokens require login to read data */
4029   if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
4030     ret = gnutls_x509_crt_import_pkcs11_url (crt, CERT_URL,
4031                                              GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
4032
4033   if (ret < 0)
4034     {
4035       fprintf (stderr, "*** Error loading key file: %s\n",
4036                gnutls_strerror (ret));
4037       exit (1);
4038     }
4039
4040   gnutls_pkcs11_privkey_init (&key);
4041
4042   ret = gnutls_pkcs11_privkey_import_url (key, KEY_URL, 0);
4043   if (ret < 0)
4044     {
4045       fprintf (stderr, "*** Error loading key file: %s\n",
4046                gnutls_strerror (ret));
4047       exit (1);
4048     }
4049
4050 }
4051
4052 static int
4053 pin_callback (void *user, int attempt, const char *token_url,
4054               const char *token_label, unsigned int flags, char *pin,
4055               size_t pin_max)
4056 {
4057   const char *password;
4058   int len;
4059
4060   printf ("PIN required for token '%s' with URL '%s'\n", token_label,
4061           token_url);
4062   if (flags & GNUTLS_PKCS11_PIN_FINAL_TRY)
4063     printf ("*** This is the final try before locking!\n");
4064   if (flags & GNUTLS_PKCS11_PIN_COUNT_LOW)
4065     printf ("*** Only few tries left before locking!\n");
4066
4067   password = getpass ("Enter pin: ");
4068   if (password == NULL || password[0] == 0)
4069     {
4070       fprintf (stderr, "No password given\n");
4071       exit (1);
4072     }
4073
4074   len = MIN (pin_max, strlen (password));
4075   memcpy (pin, password, len);
4076   pin[len] = 0;
4077
4078   return 0;
4079 }
4080
4081 int
4082 main (void)
4083 {
4084   int ret, sd, ii;
4085   gnutls_session_t session;
4086   gnutls_priority_t priorities_cache;
4087   char buffer[MAX_BUF + 1];
4088   gnutls_certificate_credentials_t xcred;
4089   /* Allow connections to servers that have OpenPGP keys as well.
4090    */
4091
4092   gnutls_global_init ();
4093   /* PKCS11 private key operations might require PIN.
4094    * Register a callback.
4095    */
4096   gnutls_pkcs11_set_pin_function (pin_callback, NULL);
4097
4098   load_keys ();
4099
4100   /* X509 stuff */
4101   gnutls_certificate_allocate_credentials (&xcred);
4102
4103   /* priorities */
4104   gnutls_priority_init (&priorities_cache, "NORMAL", NULL);
4105
4106
4107   /* sets the trusted cas file
4108    */
4109   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
4110
4111   gnutls_certificate_set_retrieve_function (xcred, cert_callback);
4112
4113   /* Initialize TLS session
4114    */
4115   gnutls_init (&session, GNUTLS_CLIENT);
4116
4117   /* Use default priorities */
4118   gnutls_priority_set (session, priorities_cache);
4119
4120   /* put the x509 credentials to the current session
4121    */
4122   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
4123
4124   /* connect to the peer
4125    */
4126   sd = tcp_connect ();
4127
4128   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
4129
4130   /* Perform the TLS handshake
4131    */
4132   ret = gnutls_handshake (session);
4133
4134   if (ret < 0)
4135     {
4136       fprintf (stderr, "*** Handshake failed\n");
4137       gnutls_perror (ret);
4138       goto end;
4139     }
4140   else
4141     {
4142       printf ("- Handshake was completed\n");
4143     }
4144
4145   gnutls_record_send (session, MSG, strlen (MSG));
4146
4147   ret = gnutls_record_recv (session, buffer, MAX_BUF);
4148   if (ret == 0)
4149     {
4150       printf ("- Peer has closed the TLS connection\n");
4151       goto end;
4152     }
4153   else if (ret < 0)
4154     {
4155       fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
4156       goto end;
4157     }
4158
4159   printf ("- Received %d bytes: ", ret);
4160   for (ii = 0; ii < ret; ii++)
4161     {
4162       fputc (buffer[ii], stdout);
4163     }
4164   fputs ("\n", stdout);
4165
4166   gnutls_bye (session, GNUTLS_SHUT_RDWR);
4167
4168 end:
4169
4170   tcp_close (sd);
4171
4172   gnutls_deinit (session);
4173
4174   gnutls_certificate_free_credentials (xcred);
4175   gnutls_priority_deinit (priorities_cache);
4176
4177   gnutls_global_deinit ();
4178
4179   return 0;
4180 }
4181
4182
4183
4184 /* This callback should be associated with a session by calling
4185  * gnutls_certificate_client_set_retrieve_function( session, cert_callback),
4186  * before a handshake.
4187  */
4188
4189 static int
4190 cert_callback (gnutls_session_t session,
4191                const gnutls_datum_t * req_ca_rdn, int nreqs,
4192                const gnutls_pk_algorithm_t * sign_algos,
4193                int sign_algos_length, gnutls_retr2_st * st)
4194 {
4195   char issuer_dn[256];
4196   int i, ret;
4197   size_t len;
4198   gnutls_certificate_type_t type;
4199
4200   /* Print the server's trusted CAs
4201    */
4202   if (nreqs > 0)
4203     printf ("- Server's trusted authorities:\n");
4204   else
4205     printf ("- Server did not send us any trusted authorities names.\n");
4206
4207   /* print the names (if any) */
4208   for (i = 0; i < nreqs; i++)
4209     {
4210       len = sizeof (issuer_dn);
4211       ret = gnutls_x509_rdn_get (&req_ca_rdn[i], issuer_dn, &len);
4212       if (ret >= 0)
4213         {
4214           printf ("   [%d]: ", i);
4215           printf ("%s\n", issuer_dn);
4216         }
4217     }
4218
4219   /* Select a certificate and return it.
4220    * The certificate must be of any of the "sign algorithms"
4221    * supported by the server.
4222    */
4223
4224   type = gnutls_certificate_type_get (session);
4225   if (type == GNUTLS_CRT_X509)
4226     {
4227       /* check if the certificate we are sending is signed
4228        * with an algorithm that the server accepts */
4229       gnutls_sign_algorithm_t cert_algo, req_algo;
4230       int i, match = 0;
4231
4232       ret = gnutls_x509_crt_get_signature_algorithm (crt);
4233       if (ret < 0)
4234         {
4235           /* error reading signature algorithm
4236            */
4237           return -1;
4238         }
4239       cert_algo = ret;
4240
4241       i = 0;
4242       do
4243         {
4244           ret = gnutls_sign_algorithm_get_requested (session, i, &req_algo);
4245           if (ret >= 0 && cert_algo == req_algo)
4246             {
4247               match = 1;
4248               break;
4249             }
4250
4251           /* server has not requested anything specific */
4252           if (i == 0 && ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
4253             {
4254               match = 1;
4255               break;
4256             }
4257           i++;
4258         }
4259       while (ret >= 0);
4260
4261       if (match == 0)
4262         {
4263           printf
4264             ("- Could not find a suitable certificate to send to server\n");
4265           return -1;
4266         }
4267
4268       st->cert_type = type;
4269       st->ncerts = 1;
4270
4271       st->cert.x509 = &crt;
4272       st->key.pkcs11 = key;
4273       st->key_type = GNUTLS_PRIVKEY_PKCS11;
4274
4275       st->deinit_all = 0;
4276     }
4277   else
4278     {
4279       return -1;
4280     }
4281
4282   return 0;
4283
4284 }
4285
4286 \1f
4287 File: gnutls.info,  Node: Client with Resume capability example,  Next: Simple client example with SRP authentication,  Prev: Client using a PKCS #11 token with TLS,  Up: Client examples
4288
4289 7.3.7 Client with Resume Capability Example
4290 -------------------------------------------
4291
4292 This is a modification of the simple client example.  Here we
4293 demonstrate the use of session resumption.  The client tries to connect
4294 once using TLS, close the connection and then try to establish a new
4295 connection using the previously negotiated data.
4296
4297 /* This example code is placed in the public domain. */
4298
4299 #ifdef HAVE_CONFIG_H
4300 #include <config.h>
4301 #endif
4302
4303 #include <string.h>
4304 #include <stdio.h>
4305 #include <stdlib.h>
4306 #include <gnutls/gnutls.h>
4307
4308 /* Those functions are defined in other examples.
4309  */
4310 extern void check_alert (gnutls_session_t session, int ret);
4311 extern int tcp_connect (void);
4312 extern void tcp_close (int sd);
4313
4314 #define MAX_BUF 1024
4315 #define CAFILE "ca.pem"
4316 #define MSG "GET / HTTP/1.0\r\n\r\n"
4317
4318 int
4319 main (void)
4320 {
4321   int ret;
4322   int sd, ii;
4323   gnutls_session_t session;
4324   char buffer[MAX_BUF + 1];
4325   gnutls_certificate_credentials_t xcred;
4326
4327   /* variables used in session resuming
4328    */
4329   int t;
4330   char *session_data = NULL;
4331   size_t session_data_size = 0;
4332
4333   gnutls_global_init ();
4334
4335   /* X509 stuff */
4336   gnutls_certificate_allocate_credentials (&xcred);
4337
4338   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
4339
4340   for (t = 0; t < 2; t++)
4341     {                           /* connect 2 times to the server */
4342
4343       sd = tcp_connect ();
4344
4345       gnutls_init (&session, GNUTLS_CLIENT);
4346
4347       gnutls_priority_set_direct (session, "PERFORMANCE:!ARCFOUR-128", NULL);
4348
4349       gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
4350
4351       if (t > 0)
4352         {
4353           /* if this is not the first time we connect */
4354           gnutls_session_set_data (session, session_data, session_data_size);
4355           free (session_data);
4356         }
4357
4358       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
4359
4360       /* Perform the TLS handshake
4361        */
4362       ret = gnutls_handshake (session);
4363
4364       if (ret < 0)
4365         {
4366           fprintf (stderr, "*** Handshake failed\n");
4367           gnutls_perror (ret);
4368           goto end;
4369         }
4370       else
4371         {
4372           printf ("- Handshake was completed\n");
4373         }
4374
4375       if (t == 0)
4376         {                       /* the first time we connect */
4377           /* get the session data size */
4378           gnutls_session_get_data (session, NULL, &session_data_size);
4379           session_data = malloc (session_data_size);
4380
4381           /* put session data to the session variable */
4382           gnutls_session_get_data (session, session_data, &session_data_size);
4383
4384         }
4385       else
4386         {                       /* the second time we connect */
4387
4388           /* check if we actually resumed the previous session */
4389           if (gnutls_session_is_resumed (session) != 0)
4390             {
4391               printf ("- Previous session was resumed\n");
4392             }
4393           else
4394             {
4395               fprintf (stderr, "*** Previous session was NOT resumed\n");
4396             }
4397         }
4398
4399       /* This function was defined in a previous example
4400        */
4401       /* print_info(session); */
4402
4403       gnutls_record_send (session, MSG, strlen (MSG));
4404
4405       ret = gnutls_record_recv (session, buffer, MAX_BUF);
4406       if (ret == 0)
4407         {
4408           printf ("- Peer has closed the TLS connection\n");
4409           goto end;
4410         }
4411       else if (ret < 0)
4412         {
4413           fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
4414           goto end;
4415         }
4416
4417       printf ("- Received %d bytes: ", ret);
4418       for (ii = 0; ii < ret; ii++)
4419         {
4420           fputc (buffer[ii], stdout);
4421         }
4422       fputs ("\n", stdout);
4423
4424       gnutls_bye (session, GNUTLS_SHUT_RDWR);
4425
4426     end:
4427
4428       tcp_close (sd);
4429
4430       gnutls_deinit (session);
4431
4432     }                           /* for() */
4433
4434   gnutls_certificate_free_credentials (xcred);
4435
4436   gnutls_global_deinit ();
4437
4438   return 0;
4439 }
4440
4441 \1f
4442 File: gnutls.info,  Node: Simple client example with SRP authentication,  Next: Simple client example in C++,  Prev: Client with Resume capability example,  Up: Client examples
4443
4444 7.3.8 Simple Client Example with SRP Authentication
4445 ---------------------------------------------------
4446
4447 The following client is a very simple SRP TLS client which connects to a
4448 server and authenticates using a _username_ and a _password_.  The
4449 server may authenticate itself using a certificate, and in that case it
4450 has to be verified.
4451
4452 /* This example code is placed in the public domain. */
4453
4454 #ifdef HAVE_CONFIG_H
4455 #include <config.h>
4456 #endif
4457
4458 #include <stdio.h>
4459 #include <stdlib.h>
4460 #include <string.h>
4461 #include <gnutls/gnutls.h>
4462 #include <gnutls/extra.h>
4463
4464 /* Those functions are defined in other examples.
4465  */
4466 extern void check_alert (gnutls_session_t session, int ret);
4467 extern int tcp_connect (void);
4468 extern void tcp_close (int sd);
4469
4470 #define MAX_BUF 1024
4471 #define USERNAME "user"
4472 #define PASSWORD "pass"
4473 #define CAFILE "ca.pem"
4474 #define MSG "GET / HTTP/1.0\r\n\r\n"
4475
4476 int
4477 main (void)
4478 {
4479   int ret;
4480   int sd, ii;
4481   gnutls_session_t session;
4482   char buffer[MAX_BUF + 1];
4483   gnutls_srp_client_credentials_t srp_cred;
4484   gnutls_certificate_credentials_t cert_cred;
4485
4486   gnutls_global_init ();
4487
4488   /* now enable the gnutls-extra library which contains the
4489    * SRP stuff.
4490    */
4491   gnutls_global_init_extra ();
4492
4493   gnutls_srp_allocate_client_credentials (&srp_cred);
4494   gnutls_certificate_allocate_credentials (&cert_cred);
4495
4496   gnutls_certificate_set_x509_trust_file (cert_cred, CAFILE,
4497                                           GNUTLS_X509_FMT_PEM);
4498   gnutls_srp_set_client_credentials (srp_cred, USERNAME, PASSWORD);
4499
4500   /* connects to server
4501    */
4502   sd = tcp_connect ();
4503
4504   /* Initialize TLS session
4505    */
4506   gnutls_init (&session, GNUTLS_CLIENT);
4507
4508
4509   /* Set the priorities.
4510    */
4511   gnutls_priority_set_direct (session, "NORMAL:+SRP:+SRP-RSA:+SRP-DSS", NULL);
4512
4513   /* put the SRP credentials to the current session
4514    */
4515   gnutls_credentials_set (session, GNUTLS_CRD_SRP, srp_cred);
4516   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cert_cred);
4517
4518   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
4519
4520   /* Perform the TLS handshake
4521    */
4522   ret = gnutls_handshake (session);
4523
4524   if (ret < 0)
4525     {
4526       fprintf (stderr, "*** Handshake failed\n");
4527       gnutls_perror (ret);
4528       goto end;
4529     }
4530   else
4531     {
4532       printf ("- Handshake was completed\n");
4533     }
4534
4535   gnutls_record_send (session, MSG, strlen (MSG));
4536
4537   ret = gnutls_record_recv (session, buffer, MAX_BUF);
4538   if (gnutls_error_is_fatal (ret) == 1 || ret == 0)
4539     {
4540       if (ret == 0)
4541         {
4542           printf ("- Peer has closed the GnuTLS connection\n");
4543           goto end;
4544         }
4545       else
4546         {
4547           fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
4548           goto end;
4549         }
4550     }
4551   else
4552     check_alert (session, ret);
4553
4554   if (ret > 0)
4555     {
4556       printf ("- Received %d bytes: ", ret);
4557       for (ii = 0; ii < ret; ii++)
4558         {
4559           fputc (buffer[ii], stdout);
4560         }
4561       fputs ("\n", stdout);
4562     }
4563   gnutls_bye (session, GNUTLS_SHUT_RDWR);
4564
4565 end:
4566
4567   tcp_close (sd);
4568
4569   gnutls_deinit (session);
4570
4571   gnutls_srp_free_client_credentials (srp_cred);
4572   gnutls_certificate_free_credentials (cert_cred);
4573
4574   gnutls_global_deinit ();
4575
4576   return 0;
4577 }
4578
4579 \1f
4580 File: gnutls.info,  Node: Simple client example in C++,  Next: Helper function for TCP connections,  Prev: Simple client example with SRP authentication,  Up: Client examples
4581
4582 7.3.9 Simple Client Example using the C++ API
4583 ---------------------------------------------
4584
4585 The following client is a simple example of a client client utilizing
4586 the GnuTLS C++ API.
4587
4588 #include <iostream>
4589 #include <stdexcept>
4590 #include <gnutls/gnutls.h>
4591 #include <gnutls/gnutlsxx.h>
4592 #include <cstring> /* for strlen */
4593
4594 /* A very basic TLS client, with anonymous authentication.
4595  * written by Eduardo Villanueva Che.
4596  */
4597
4598 #define MAX_BUF 1024
4599 #define SA struct sockaddr
4600
4601 #define CAFILE "ca.pem"
4602 #define MSG "GET / HTTP/1.0\r\n\r\n"
4603
4604 extern "C"
4605 {
4606     int tcp_connect(void);
4607     void tcp_close(int sd);
4608 }
4609
4610
4611 int main(void)
4612 {
4613     int sd = -1;
4614     gnutls_global_init();
4615
4616     try
4617     {
4618
4619         /* Allow connections to servers that have OpenPGP keys as well.
4620          */
4621         gnutls::client_session session;
4622
4623         /* X509 stuff */
4624         gnutls::certificate_credentials credentials;
4625
4626
4627         /* sets the trusted cas file
4628          */
4629         credentials.set_x509_trust_file(CAFILE, GNUTLS_X509_FMT_PEM);
4630         /* put the x509 credentials to the current session
4631          */
4632         session.set_credentials(credentials);
4633
4634         /* Use default priorities */
4635         session.set_priority ("NORMAL", NULL);
4636
4637         /* connect to the peer
4638          */
4639         sd = tcp_connect();
4640         session.set_transport_ptr((gnutls_transport_ptr_t) sd);
4641
4642         /* Perform the TLS handshake
4643          */
4644         int ret = session.handshake();
4645         if (ret < 0)
4646         {
4647             throw std::runtime_error("Handshake failed");
4648         }
4649         else
4650         {
4651             std::cout << "- Handshake was completed" << std::endl;
4652         }
4653
4654         session.send(MSG, strlen(MSG));
4655         char buffer[MAX_BUF + 1];
4656         ret = session.recv(buffer, MAX_BUF);
4657         if (ret == 0)
4658         {
4659             throw std::runtime_error("Peer has closed the TLS connection");
4660         }
4661         else if (ret < 0)
4662         {
4663             throw std::runtime_error(gnutls_strerror(ret));
4664         }
4665
4666         std::cout << "- Received " << ret << " bytes:" << std::endl;
4667         std::cout.write(buffer, ret);
4668         std::cout << std::endl;
4669
4670         session.bye(GNUTLS_SHUT_RDWR);
4671     }
4672     catch (std::exception &ex)
4673     {
4674         std::cerr << "Exception caught: " << ex.what() << std::endl;
4675     }
4676
4677     if (sd != -1)
4678         tcp_close(sd);
4679
4680     gnutls_global_deinit();
4681
4682     return 0;
4683 }
4684
4685 \1f
4686 File: gnutls.info,  Node: Helper function for TCP connections,  Prev: Simple client example in C++,  Up: Client examples
4687
4688 7.3.10 Helper Function for TCP Connections
4689 ------------------------------------------
4690
4691 This helper function abstracts away TCP connection handling from the
4692 other examples.  It is required to build some examples.
4693
4694 /* This example code is placed in the public domain. */
4695
4696 #ifdef HAVE_CONFIG_H
4697 #include <config.h>
4698 #endif
4699
4700 #include <stdio.h>
4701 #include <stdlib.h>
4702 #include <string.h>
4703 #include <sys/types.h>
4704 #include <sys/socket.h>
4705 #include <arpa/inet.h>
4706 #include <netinet/in.h>
4707 #include <unistd.h>
4708
4709 #define SA struct sockaddr
4710
4711 /* tcp.c */
4712 int tcp_connect (void);
4713 void tcp_close (int sd);
4714
4715 /* Connects to the peer and returns a socket
4716  * descriptor.
4717  */
4718 extern int
4719 tcp_connect (void)
4720 {
4721   const char *PORT = "5556";
4722   const char *SERVER = "127.0.0.1";
4723   int err, sd;
4724   struct sockaddr_in sa;
4725
4726   /* connects to server
4727    */
4728   sd = socket (AF_INET, SOCK_STREAM, 0);
4729
4730   memset (&sa, '\0', sizeof (sa));
4731   sa.sin_family = AF_INET;
4732   sa.sin_port = htons (atoi (PORT));
4733   inet_pton (AF_INET, SERVER, &sa.sin_addr);
4734
4735   err = connect (sd, (SA *) & sa, sizeof (sa));
4736   if (err < 0)
4737     {
4738       fprintf (stderr, "Connect error\n");
4739       exit (1);
4740     }
4741
4742   return sd;
4743 }
4744
4745 /* closes the given socket descriptor.
4746  */
4747 extern void
4748 tcp_close (int sd)
4749 {
4750   shutdown (sd, SHUT_RDWR);     /* no more receptions */
4751   close (sd);
4752 }
4753
4754 \1f
4755 File: gnutls.info,  Node: Server examples,  Next: Miscellaneous examples,  Prev: Client examples,  Up: How to use GnuTLS in applications
4756
4757 7.4 Server Examples
4758 ===================
4759
4760 This section contains examples of TLS and SSL servers, using GnuTLS.
4761
4762 * Menu:
4763
4764 * Echo Server with X.509 authentication::
4765 * Echo Server with OpenPGP authentication::
4766 * Echo Server with SRP authentication::
4767 * Echo Server with anonymous authentication::
4768
4769 \1f
4770 File: gnutls.info,  Node: Echo Server with X.509 authentication,  Next: Echo Server with OpenPGP authentication,  Up: Server examples
4771
4772 7.4.1 Echo Server with X.509 Authentication
4773 -------------------------------------------
4774
4775 This example is a very simple echo server which supports X.509
4776 authentication, using the RSA ciphersuites.
4777
4778 /* This example code is placed in the public domain. */
4779
4780 #ifdef HAVE_CONFIG_H
4781 #include <config.h>
4782 #endif
4783
4784 #include <stdio.h>
4785 #include <stdlib.h>
4786 #include <errno.h>
4787 #include <sys/types.h>
4788 #include <sys/socket.h>
4789 #include <arpa/inet.h>
4790 #include <netinet/in.h>
4791 #include <string.h>
4792 #include <unistd.h>
4793 #include <gnutls/gnutls.h>
4794
4795 #define KEYFILE "key.pem"
4796 #define CERTFILE "cert.pem"
4797 #define CAFILE "ca.pem"
4798 #define CRLFILE "crl.pem"
4799
4800 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
4801  */
4802
4803
4804 #define SA struct sockaddr
4805 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
4806 #define MAX_BUF 1024
4807 #define PORT 5556               /* listen to 5556 port */
4808 #define DH_BITS 1024
4809
4810 /* These are global */
4811 gnutls_certificate_credentials_t x509_cred;
4812 gnutls_priority_t priority_cache;
4813
4814 static gnutls_session_t
4815 initialize_tls_session (void)
4816 {
4817   gnutls_session_t session;
4818
4819   gnutls_init (&session, GNUTLS_SERVER);
4820
4821   gnutls_priority_set (session, priority_cache);
4822
4823   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
4824
4825   /* request client certificate if any.
4826    */
4827   gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
4828
4829   /* Set maximum compatibility mode. This is only suggested on public webservers
4830    * that need to trade security for compatibility
4831    */
4832   gnutls_session_enable_compatibility_mode (session);
4833
4834   return session;
4835 }
4836
4837 static gnutls_dh_params_t dh_params;
4838
4839 static int
4840 generate_dh_params (void)
4841 {
4842
4843   /* Generate Diffie-Hellman parameters - for use with DHE
4844    * kx algorithms. When short bit length is used, it might
4845    * be wise to regenerate parameters.
4846    *
4847    * Check the ex-serv-export.c example for using static
4848    * parameters.
4849    */
4850   gnutls_dh_params_init (&dh_params);
4851   gnutls_dh_params_generate2 (dh_params, DH_BITS);
4852
4853   return 0;
4854 }
4855
4856 int
4857 main (void)
4858 {
4859   int err, listen_sd;
4860   int sd, ret;
4861   struct sockaddr_in sa_serv;
4862   struct sockaddr_in sa_cli;
4863   int client_len;
4864   char topbuf[512];
4865   gnutls_session_t session;
4866   char buffer[MAX_BUF + 1];
4867   int optval = 1;
4868
4869   /* this must be called once in the program
4870    */
4871   gnutls_global_init ();
4872
4873   gnutls_certificate_allocate_credentials (&x509_cred);
4874   gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE,
4875                                           GNUTLS_X509_FMT_PEM);
4876
4877   gnutls_certificate_set_x509_crl_file (x509_cred, CRLFILE,
4878                                         GNUTLS_X509_FMT_PEM);
4879
4880   gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
4881                                         GNUTLS_X509_FMT_PEM);
4882
4883   generate_dh_params ();
4884
4885   gnutls_priority_init (&priority_cache, "NORMAL", NULL);
4886
4887
4888   gnutls_certificate_set_dh_params (x509_cred, dh_params);
4889
4890   /* Socket operations
4891    */
4892   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
4893   SOCKET_ERR (listen_sd, "socket");
4894
4895   memset (&sa_serv, '\0', sizeof (sa_serv));
4896   sa_serv.sin_family = AF_INET;
4897   sa_serv.sin_addr.s_addr = INADDR_ANY;
4898   sa_serv.sin_port = htons (PORT);      /* Server Port number */
4899
4900   setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
4901               sizeof (int));
4902
4903   err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
4904   SOCKET_ERR (err, "bind");
4905   err = listen (listen_sd, 1024);
4906   SOCKET_ERR (err, "listen");
4907
4908   printf ("Server ready. Listening to port '%d'.\n\n", PORT);
4909
4910   client_len = sizeof (sa_cli);
4911   for (;;)
4912     {
4913       session = initialize_tls_session ();
4914
4915       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
4916
4917       printf ("- connection from %s, port %d\n",
4918               inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
4919                          sizeof (topbuf)), ntohs (sa_cli.sin_port));
4920
4921       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
4922       ret = gnutls_handshake (session);
4923       if (ret < 0)
4924         {
4925           close (sd);
4926           gnutls_deinit (session);
4927           fprintf (stderr, "*** Handshake has failed (%s)\n\n",
4928                    gnutls_strerror (ret));
4929           continue;
4930         }
4931       printf ("- Handshake was completed\n");
4932
4933       /* see the Getting peer's information example */
4934       /* print_info(session); */
4935
4936       for (;;)
4937         {
4938           memset (buffer, 0, MAX_BUF + 1);
4939           ret = gnutls_record_recv (session, buffer, MAX_BUF);
4940
4941           if (ret == 0)
4942             {
4943               printf ("\n- Peer has closed the GnuTLS connection\n");
4944               break;
4945             }
4946           else if (ret < 0)
4947             {
4948               fprintf (stderr, "\n*** Received corrupted "
4949                        "data(%d). Closing the connection.\n\n", ret);
4950               break;
4951             }
4952           else if (ret > 0)
4953             {
4954               /* echo data back to the client
4955                */
4956               gnutls_record_send (session, buffer, strlen (buffer));
4957             }
4958         }
4959       printf ("\n");
4960       /* do not wait for the peer to close the connection.
4961        */
4962       gnutls_bye (session, GNUTLS_SHUT_WR);
4963
4964       close (sd);
4965       gnutls_deinit (session);
4966
4967     }
4968   close (listen_sd);
4969
4970   gnutls_certificate_free_credentials (x509_cred);
4971   gnutls_priority_deinit (priority_cache);
4972
4973   gnutls_global_deinit ();
4974
4975   return 0;
4976
4977 }
4978
4979 \1f
4980 File: gnutls.info,  Node: Echo Server with OpenPGP authentication,  Next: Echo Server with SRP authentication,  Prev: Echo Server with X.509 authentication,  Up: Server examples
4981
4982 7.4.2 Echo Server with OpenPGP Authentication
4983 ---------------------------------------------
4984
4985 The following example is an echo server which supports OpenPGP key
4986 authentication.  You can easily combine this functionality --that is
4987 have a server that supports both X.509 and OpenPGP certificates-- but we
4988 separated them to keep these examples as simple as possible.
4989
4990 /* This example code is placed in the public domain. */
4991
4992 #ifdef HAVE_CONFIG_H
4993 #include <config.h>
4994 #endif
4995
4996 #include <stdio.h>
4997 #include <stdlib.h>
4998 #include <errno.h>
4999 #include <sys/types.h>
5000 #include <sys/socket.h>
5001 #include <arpa/inet.h>
5002 #include <netinet/in.h>
5003 #include <string.h>
5004 #include <unistd.h>
5005 #include <gnutls/gnutls.h>
5006 #include <gnutls/openpgp.h>
5007
5008 #define KEYFILE "secret.asc"
5009 #define CERTFILE "public.asc"
5010 #define RINGFILE "ring.gpg"
5011
5012 /* This is a sample TLS 1.0-OpenPGP echo server.
5013  */
5014
5015
5016 #define SA struct sockaddr
5017 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
5018 #define MAX_BUF 1024
5019 #define PORT 5556               /* listen to 5556 port */
5020 #define DH_BITS 1024
5021
5022 /* These are global */
5023 gnutls_certificate_credentials_t cred;
5024 gnutls_dh_params_t dh_params;
5025
5026 static int
5027 generate_dh_params (void)
5028 {
5029
5030   /* Generate Diffie-Hellman parameters - for use with DHE
5031    * kx algorithms. These should be discarded and regenerated
5032    * once a day, once a week or once a month. Depending on the
5033    * security requirements.
5034    */
5035   gnutls_dh_params_init (&dh_params);
5036   gnutls_dh_params_generate2 (dh_params, DH_BITS);
5037
5038   return 0;
5039 }
5040
5041 static gnutls_session_t
5042 initialize_tls_session (void)
5043 {
5044   gnutls_session_t session;
5045
5046   gnutls_init (&session, GNUTLS_SERVER);
5047
5048   gnutls_priority_set_direct (session, "NORMAL", NULL);
5049
5050   /* request client certificate if any.
5051    */
5052   gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
5053
5054   gnutls_dh_set_prime_bits (session, DH_BITS);
5055
5056   return session;
5057 }
5058
5059 int
5060 main (void)
5061 {
5062   int err, listen_sd;
5063   int sd, ret;
5064   struct sockaddr_in sa_serv;
5065   struct sockaddr_in sa_cli;
5066   int client_len;
5067   char topbuf[512];
5068   gnutls_session_t session;
5069   char buffer[MAX_BUF + 1];
5070   int optval = 1;
5071   char name[256];
5072
5073   strcpy (name, "Echo Server");
5074
5075   /* this must be called once in the program
5076    */
5077   gnutls_global_init ();
5078
5079   gnutls_certificate_allocate_credentials (&cred);
5080   gnutls_certificate_set_openpgp_keyring_file (cred, RINGFILE,
5081                                                GNUTLS_OPENPGP_FMT_BASE64);
5082
5083   gnutls_certificate_set_openpgp_key_file (cred, CERTFILE, KEYFILE,
5084                                            GNUTLS_OPENPGP_FMT_BASE64);
5085
5086   generate_dh_params ();
5087
5088   gnutls_certificate_set_dh_params (cred, dh_params);
5089
5090   /* Socket operations
5091    */
5092   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
5093   SOCKET_ERR (listen_sd, "socket");
5094
5095   memset (&sa_serv, '\0', sizeof (sa_serv));
5096   sa_serv.sin_family = AF_INET;
5097   sa_serv.sin_addr.s_addr = INADDR_ANY;
5098   sa_serv.sin_port = htons (PORT);      /* Server Port number */
5099
5100   setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
5101               sizeof (int));
5102
5103   err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
5104   SOCKET_ERR (err, "bind");
5105   err = listen (listen_sd, 1024);
5106   SOCKET_ERR (err, "listen");
5107
5108   printf ("%s ready. Listening to port '%d'.\n\n", name, PORT);
5109
5110   client_len = sizeof (sa_cli);
5111   for (;;)
5112     {
5113       session = initialize_tls_session ();
5114
5115       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
5116
5117       printf ("- connection from %s, port %d\n",
5118               inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
5119                          sizeof (topbuf)), ntohs (sa_cli.sin_port));
5120
5121       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
5122       ret = gnutls_handshake (session);
5123       if (ret < 0)
5124         {
5125           close (sd);
5126           gnutls_deinit (session);
5127           fprintf (stderr, "*** Handshake has failed (%s)\n\n",
5128                    gnutls_strerror (ret));
5129           continue;
5130         }
5131       printf ("- Handshake was completed\n");
5132
5133       /* see the Getting peer's information example */
5134       /* print_info(session); */
5135
5136       for (;;)
5137         {
5138           memset (buffer, 0, MAX_BUF + 1);
5139           ret = gnutls_record_recv (session, buffer, MAX_BUF);
5140
5141           if (ret == 0)
5142             {
5143               printf ("\n- Peer has closed the GnuTLS connection\n");
5144               break;
5145             }
5146           else if (ret < 0)
5147             {
5148               fprintf (stderr, "\n*** Received corrupted "
5149                        "data(%d). Closing the connection.\n\n", ret);
5150               break;
5151             }
5152           else if (ret > 0)
5153             {
5154               /* echo data back to the client
5155                */
5156               gnutls_record_send (session, buffer, strlen (buffer));
5157             }
5158         }
5159       printf ("\n");
5160       /* do not wait for the peer to close the connection.
5161        */
5162       gnutls_bye (session, GNUTLS_SHUT_WR);
5163
5164       close (sd);
5165       gnutls_deinit (session);
5166
5167     }
5168   close (listen_sd);
5169
5170   gnutls_certificate_free_credentials (cred);
5171
5172   gnutls_global_deinit ();
5173
5174   return 0;
5175
5176 }
5177
5178 \1f
5179 File: gnutls.info,  Node: Echo Server with SRP authentication,  Next: Echo Server with anonymous authentication,  Prev: Echo Server with OpenPGP authentication,  Up: Server examples
5180
5181 7.4.3 Echo Server with SRP Authentication
5182 -----------------------------------------
5183
5184 This is a server which supports SRP authentication.  It is also possible
5185 to combine this functionality with a certificate server.  Here it is
5186 separate for simplicity.
5187
5188 /* This example code is placed in the public domain. */
5189
5190 #ifdef HAVE_CONFIG_H
5191 #include <config.h>
5192 #endif
5193
5194 #include <stdio.h>
5195 #include <stdlib.h>
5196 #include <errno.h>
5197 #include <sys/types.h>
5198 #include <sys/socket.h>
5199 #include <arpa/inet.h>
5200 #include <netinet/in.h>
5201 #include <string.h>
5202 #include <unistd.h>
5203 #include <gnutls/gnutls.h>
5204 #include <gnutls/extra.h>
5205
5206 #define SRP_PASSWD "tpasswd"
5207 #define SRP_PASSWD_CONF "tpasswd.conf"
5208
5209 #define KEYFILE "key.pem"
5210 #define CERTFILE "cert.pem"
5211 #define CAFILE "ca.pem"
5212
5213 /* This is a sample TLS-SRP echo server.
5214  */
5215
5216 #define SA struct sockaddr
5217 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
5218 #define MAX_BUF 1024
5219 #define PORT 5556               /* listen to 5556 port */
5220
5221 /* These are global */
5222 gnutls_srp_server_credentials_t srp_cred;
5223 gnutls_certificate_credentials_t cert_cred;
5224
5225 static gnutls_session_t
5226 initialize_tls_session (void)
5227 {
5228   gnutls_session_t session;
5229
5230   gnutls_init (&session, GNUTLS_SERVER);
5231
5232   gnutls_priority_set_direct (session, "NORMAL:+SRP:+SRP-DSS:+SRP-RSA", NULL);
5233
5234   gnutls_credentials_set (session, GNUTLS_CRD_SRP, srp_cred);
5235   /* for the certificate authenticated ciphersuites.
5236    */
5237   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cert_cred);
5238
5239   /* request client certificate if any.
5240    */
5241   gnutls_certificate_server_set_request (session, GNUTLS_CERT_IGNORE);
5242
5243   return session;
5244 }
5245
5246 int
5247 main (void)
5248 {
5249   int err, listen_sd;
5250   int sd, ret;
5251   struct sockaddr_in sa_serv;
5252   struct sockaddr_in sa_cli;
5253   int client_len;
5254   char topbuf[512];
5255   gnutls_session_t session;
5256   char buffer[MAX_BUF + 1];
5257   int optval = 1;
5258   char name[256];
5259
5260   strcpy (name, "Echo Server");
5261
5262   /* these must be called once in the program
5263    */
5264   gnutls_global_init ();
5265   gnutls_global_init_extra ();  /* for SRP */
5266
5267   /* SRP_PASSWD a password file (created with the included srptool utility)
5268    */
5269   gnutls_srp_allocate_server_credentials (&srp_cred);
5270   gnutls_srp_set_server_credentials_file (srp_cred, SRP_PASSWD,
5271                                           SRP_PASSWD_CONF);
5272
5273   gnutls_certificate_allocate_credentials (&cert_cred);
5274   gnutls_certificate_set_x509_trust_file (cert_cred, CAFILE,
5275                                           GNUTLS_X509_FMT_PEM);
5276   gnutls_certificate_set_x509_key_file (cert_cred, CERTFILE, KEYFILE,
5277                                         GNUTLS_X509_FMT_PEM);
5278
5279   /* TCP socket operations
5280    */
5281   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
5282   SOCKET_ERR (listen_sd, "socket");
5283
5284   memset (&sa_serv, '\0', sizeof (sa_serv));
5285   sa_serv.sin_family = AF_INET;
5286   sa_serv.sin_addr.s_addr = INADDR_ANY;
5287   sa_serv.sin_port = htons (PORT);      /* Server Port number */
5288
5289   setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
5290               sizeof (int));
5291
5292   err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
5293   SOCKET_ERR (err, "bind");
5294   err = listen (listen_sd, 1024);
5295   SOCKET_ERR (err, "listen");
5296
5297   printf ("%s ready. Listening to port '%d'.\n\n", name, PORT);
5298
5299   client_len = sizeof (sa_cli);
5300   for (;;)
5301     {
5302       session = initialize_tls_session ();
5303
5304       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
5305
5306       printf ("- connection from %s, port %d\n",
5307               inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
5308                          sizeof (topbuf)), ntohs (sa_cli.sin_port));
5309
5310       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
5311       ret = gnutls_handshake (session);
5312       if (ret < 0)
5313         {
5314           close (sd);
5315           gnutls_deinit (session);
5316           fprintf (stderr, "*** Handshake has failed (%s)\n\n",
5317                    gnutls_strerror (ret));
5318           continue;
5319         }
5320       printf ("- Handshake was completed\n");
5321
5322       /* print_info(session); */
5323
5324       for (;;)
5325         {
5326           memset (buffer, 0, MAX_BUF + 1);
5327           ret = gnutls_record_recv (session, buffer, MAX_BUF);
5328
5329           if (ret == 0)
5330             {
5331               printf ("\n- Peer has closed the GnuTLS connection\n");
5332               break;
5333             }
5334           else if (ret < 0)
5335             {
5336               fprintf (stderr, "\n*** Received corrupted "
5337                        "data(%d). Closing the connection.\n\n", ret);
5338               break;
5339             }
5340           else if (ret > 0)
5341             {
5342               /* echo data back to the client
5343                */
5344               gnutls_record_send (session, buffer, strlen (buffer));
5345             }
5346         }
5347       printf ("\n");
5348       /* do not wait for the peer to close the connection. */
5349       gnutls_bye (session, GNUTLS_SHUT_WR);
5350
5351       close (sd);
5352       gnutls_deinit (session);
5353
5354     }
5355   close (listen_sd);
5356
5357   gnutls_srp_free_server_credentials (srp_cred);
5358   gnutls_certificate_free_credentials (cert_cred);
5359
5360   gnutls_global_deinit ();
5361
5362   return 0;
5363
5364 }
5365
5366 \1f
5367 File: gnutls.info,  Node: Echo Server with anonymous authentication,  Prev: Echo Server with SRP authentication,  Up: Server examples
5368
5369 7.4.4 Echo Server with Anonymous Authentication
5370 -----------------------------------------------
5371
5372 This example server support anonymous authentication, and could be used
5373 to serve the example client for anonymous authentication.
5374
5375 /* This example code is placed in the public domain. */
5376
5377 #ifdef HAVE_CONFIG_H
5378 #include <config.h>
5379 #endif
5380
5381 #include <stdio.h>
5382 #include <stdlib.h>
5383 #include <errno.h>
5384 #include <sys/types.h>
5385 #include <sys/socket.h>
5386 #include <arpa/inet.h>
5387 #include <netinet/in.h>
5388 #include <string.h>
5389 #include <unistd.h>
5390 #include <gnutls/gnutls.h>
5391
5392 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
5393  */
5394
5395
5396 #define SA struct sockaddr
5397 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
5398 #define MAX_BUF 1024
5399 #define PORT 5556               /* listen to 5556 port */
5400 #define DH_BITS 1024
5401
5402 /* These are global */
5403 gnutls_anon_server_credentials_t anoncred;
5404
5405 static gnutls_session_t
5406 initialize_tls_session (void)
5407 {
5408   gnutls_session_t session;
5409
5410   gnutls_init (&session, GNUTLS_SERVER);
5411
5412   gnutls_priority_set_direct (session, "NORMAL:+ANON-DH", NULL);
5413
5414   gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
5415
5416   gnutls_dh_set_prime_bits (session, DH_BITS);
5417
5418   return session;
5419 }
5420
5421 static gnutls_dh_params_t dh_params;
5422
5423 static int
5424 generate_dh_params (void)
5425 {
5426
5427   /* Generate Diffie-Hellman parameters - for use with DHE
5428    * kx algorithms. These should be discarded and regenerated
5429    * once a day, once a week or once a month. Depending on the
5430    * security requirements.
5431    */
5432   gnutls_dh_params_init (&dh_params);
5433   gnutls_dh_params_generate2 (dh_params, DH_BITS);
5434
5435   return 0;
5436 }
5437
5438 int
5439 main (void)
5440 {
5441   int err, listen_sd;
5442   int sd, ret;
5443   struct sockaddr_in sa_serv;
5444   struct sockaddr_in sa_cli;
5445   int client_len;
5446   char topbuf[512];
5447   gnutls_session_t session;
5448   char buffer[MAX_BUF + 1];
5449   int optval = 1;
5450
5451   /* this must be called once in the program
5452    */
5453   gnutls_global_init ();
5454
5455   gnutls_anon_allocate_server_credentials (&anoncred);
5456
5457   generate_dh_params ();
5458
5459   gnutls_anon_set_server_dh_params (anoncred, dh_params);
5460
5461   /* Socket operations
5462    */
5463   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
5464   SOCKET_ERR (listen_sd, "socket");
5465
5466   memset (&sa_serv, '\0', sizeof (sa_serv));
5467   sa_serv.sin_family = AF_INET;
5468   sa_serv.sin_addr.s_addr = INADDR_ANY;
5469   sa_serv.sin_port = htons (PORT);      /* Server Port number */
5470
5471   setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
5472               sizeof (int));
5473
5474   err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
5475   SOCKET_ERR (err, "bind");
5476   err = listen (listen_sd, 1024);
5477   SOCKET_ERR (err, "listen");
5478
5479   printf ("Server ready. Listening to port '%d'.\n\n", PORT);
5480
5481   client_len = sizeof (sa_cli);
5482   for (;;)
5483     {
5484       session = initialize_tls_session ();
5485
5486       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
5487
5488       printf ("- connection from %s, port %d\n",
5489               inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
5490                          sizeof (topbuf)), ntohs (sa_cli.sin_port));
5491
5492       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
5493       ret = gnutls_handshake (session);
5494       if (ret < 0)
5495         {
5496           close (sd);
5497           gnutls_deinit (session);
5498           fprintf (stderr, "*** Handshake has failed (%s)\n\n",
5499                    gnutls_strerror (ret));
5500           continue;
5501         }
5502       printf ("- Handshake was completed\n");
5503
5504       /* see the Getting peer's information example */
5505       /* print_info(session); */
5506
5507       for (;;)
5508         {
5509           memset (buffer, 0, MAX_BUF + 1);
5510           ret = gnutls_record_recv (session, buffer, MAX_BUF);
5511
5512           if (ret == 0)
5513             {
5514               printf ("\n- Peer has closed the GnuTLS connection\n");
5515               break;
5516             }
5517           else if (ret < 0)
5518             {
5519               fprintf (stderr, "\n*** Received corrupted "
5520                        "data(%d). Closing the connection.\n\n", ret);
5521               break;
5522             }
5523           else if (ret > 0)
5524             {
5525               /* echo data back to the client
5526                */
5527               gnutls_record_send (session, buffer, strlen (buffer));
5528             }
5529         }
5530       printf ("\n");
5531       /* do not wait for the peer to close the connection.
5532        */
5533       gnutls_bye (session, GNUTLS_SHUT_WR);
5534
5535       close (sd);
5536       gnutls_deinit (session);
5537
5538     }
5539   close (listen_sd);
5540
5541   gnutls_anon_free_server_credentials (anoncred);
5542
5543   gnutls_global_deinit ();
5544
5545   return 0;
5546
5547 }
5548
5549 \1f
5550 File: gnutls.info,  Node: Miscellaneous examples,  Next: Parameter generation,  Prev: Server examples,  Up: How to use GnuTLS in applications
5551
5552 7.5 Miscellaneous Examples
5553 ==========================
5554
5555 * Menu:
5556
5557 * Checking for an alert::
5558 * X.509 certificate parsing example::
5559 * Certificate request generation::
5560 * PKCS #12 structure generation::
5561
5562 \1f
5563 File: gnutls.info,  Node: Checking for an alert,  Next: X.509 certificate parsing example,  Up: Miscellaneous examples
5564
5565 7.5.1 Checking for an Alert
5566 ---------------------------
5567
5568 This is a function that checks if an alert has been received in the
5569 current session.
5570
5571 /* This example code is placed in the public domain. */
5572
5573 #ifdef HAVE_CONFIG_H
5574 #include <config.h>
5575 #endif
5576
5577 #include <stdio.h>
5578 #include <stdlib.h>
5579 #include <gnutls/gnutls.h>
5580
5581 #include "examples.h"
5582
5583 /* This function will check whether the given return code from
5584  * a gnutls function (recv/send), is an alert, and will print
5585  * that alert.
5586  */
5587 void
5588 check_alert (gnutls_session_t session, int ret)
5589 {
5590   int last_alert;
5591
5592   if (ret == GNUTLS_E_WARNING_ALERT_RECEIVED
5593       || ret == GNUTLS_E_FATAL_ALERT_RECEIVED)
5594     {
5595       last_alert = gnutls_alert_get (session);
5596
5597       /* The check for renegotiation is only useful if we are
5598        * a server, and we had requested a rehandshake.
5599        */
5600       if (last_alert == GNUTLS_A_NO_RENEGOTIATION &&
5601           ret == GNUTLS_E_WARNING_ALERT_RECEIVED)
5602         printf ("* Received NO_RENEGOTIATION alert. "
5603                 "Client Does not support renegotiation.\n");
5604       else
5605         printf ("* Received alert '%d': %s.\n", last_alert,
5606                 gnutls_alert_get_name (last_alert));
5607     }
5608 }
5609
5610 \1f
5611 File: gnutls.info,  Node: X.509 certificate parsing example,  Next: Certificate request generation,  Prev: Checking for an alert,  Up: Miscellaneous examples
5612
5613 7.5.2 X.509 Certificate Parsing Example
5614 ---------------------------------------
5615
5616 To demonstrate the X.509 parsing capabilities an example program is
5617 listed below.  That program reads the peer's certificate, and prints
5618 information about it.
5619
5620 /* This example code is placed in the public domain. */
5621
5622 #ifdef HAVE_CONFIG_H
5623 #include <config.h>
5624 #endif
5625
5626 #include <stdio.h>
5627 #include <stdlib.h>
5628 #include <gnutls/gnutls.h>
5629 #include <gnutls/x509.h>
5630
5631 #include "examples.h"
5632
5633 static const char *
5634 bin2hex (const void *bin, size_t bin_size)
5635 {
5636   static char printable[110];
5637   const unsigned char *_bin = bin;
5638   char *print;
5639   size_t i;
5640
5641   if (bin_size > 50)
5642     bin_size = 50;
5643
5644   print = printable;
5645   for (i = 0; i < bin_size; i++)
5646     {
5647       sprintf (print, "%.2x ", _bin[i]);
5648       print += 2;
5649     }
5650
5651   return printable;
5652 }
5653
5654 /* This function will print information about this session's peer
5655  * certificate.
5656  */
5657 void
5658 print_x509_certificate_info (gnutls_session_t session)
5659 {
5660   char serial[40];
5661   char dn[256];
5662   size_t size;
5663   unsigned int algo, bits;
5664   time_t expiration_time, activation_time;
5665   const gnutls_datum_t *cert_list;
5666   unsigned int cert_list_size = 0;
5667   gnutls_x509_crt_t cert;
5668   gnutls_datum_t cinfo;
5669
5670   /* This function only works for X.509 certificates.
5671    */
5672   if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509)
5673     return;
5674
5675   cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
5676
5677   printf ("Peer provided %d certificates.\n", cert_list_size);
5678
5679   if (cert_list_size > 0)
5680     {
5681       int ret;
5682
5683       /* we only print information about the first certificate.
5684        */
5685       gnutls_x509_crt_init (&cert);
5686
5687       gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER);
5688
5689       printf ("Certificate info:\n");
5690
5691       /* This is the preferred way of printing short information about
5692          a certificate. */
5693
5694       ret = gnutls_x509_crt_print (cert, GNUTLS_CRT_PRINT_ONELINE, &cinfo);
5695       if (ret == 0)
5696         {
5697           printf ("\t%s\n", cinfo.data);
5698           gnutls_free (cinfo.data);
5699         }
5700
5701       /* If you want to extract fields manually for some other reason,
5702          below are popular example calls. */
5703
5704       expiration_time = gnutls_x509_crt_get_expiration_time (cert);
5705       activation_time = gnutls_x509_crt_get_activation_time (cert);
5706
5707       printf ("\tCertificate is valid since: %s", ctime (&activation_time));
5708       printf ("\tCertificate expires: %s", ctime (&expiration_time));
5709
5710       /* Print the serial number of the certificate.
5711        */
5712       size = sizeof (serial);
5713       gnutls_x509_crt_get_serial (cert, serial, &size);
5714
5715       printf ("\tCertificate serial number: %s\n", bin2hex (serial, size));
5716
5717       /* Extract some of the public key algorithm's parameters
5718        */
5719       algo = gnutls_x509_crt_get_pk_algorithm (cert, &bits);
5720
5721       printf ("Certificate public key: %s",
5722               gnutls_pk_algorithm_get_name (algo));
5723
5724       /* Print the version of the X.509
5725        * certificate.
5726        */
5727       printf ("\tCertificate version: #%d\n",
5728               gnutls_x509_crt_get_version (cert));
5729
5730       size = sizeof (dn);
5731       gnutls_x509_crt_get_dn (cert, dn, &size);
5732       printf ("\tDN: %s\n", dn);
5733
5734       size = sizeof (dn);
5735       gnutls_x509_crt_get_issuer_dn (cert, dn, &size);
5736       printf ("\tIssuer's DN: %s\n", dn);
5737
5738       gnutls_x509_crt_deinit (cert);
5739
5740     }
5741 }
5742
5743 \1f
5744 File: gnutls.info,  Node: Certificate request generation,  Next: PKCS #12 structure generation,  Prev: X.509 certificate parsing example,  Up: Miscellaneous examples
5745
5746 7.5.3 Certificate Request Generation
5747 ------------------------------------
5748
5749 The following example is about generating a certificate request, and a
5750 private key.  A certificate request can be later be processed by a CA,
5751 which should return a signed certificate.
5752
5753 /* This example code is placed in the public domain. */
5754
5755 #ifdef HAVE_CONFIG_H
5756 #include <config.h>
5757 #endif
5758
5759 #include <stdio.h>
5760 #include <stdlib.h>
5761 #include <string.h>
5762 #include <gnutls/gnutls.h>
5763 #include <gnutls/x509.h>
5764 #include <gnutls/abstract.h>
5765 #include <time.h>
5766
5767 /* This example will generate a private key and a certificate
5768  * request.
5769  */
5770
5771 int
5772 main (void)
5773 {
5774   gnutls_x509_crq_t crq;
5775   gnutls_x509_privkey_t key;
5776   gnutls_privkey_t pkey; /* object used for signing */
5777   unsigned char buffer[10 * 1024];
5778   size_t buffer_size = sizeof (buffer);
5779   unsigned int bits;
5780
5781   gnutls_global_init ();
5782
5783   /* Initialize an empty certificate request, and
5784    * an empty private key.
5785    */
5786   gnutls_x509_crq_init (&crq);
5787
5788   gnutls_x509_privkey_init (&key);
5789   gnutls_privkey_init (&pkey);
5790
5791   /* Generate an RSA key of moderate security.
5792    */
5793   bits = gnutls_sec_param_to_pk_bits (GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_NORMAL);
5794   gnutls_x509_privkey_generate (key, GNUTLS_PK_RSA, bits, 0);
5795
5796   /* Add stuff to the distinguished name
5797    */
5798   gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COUNTRY_NAME,
5799                                  0, "GR", 2);
5800
5801   gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COMMON_NAME,
5802                                  0, "Nikos", strlen ("Nikos"));
5803
5804   /* Set the request version.
5805    */
5806   gnutls_x509_crq_set_version (crq, 1);
5807
5808   /* Set a challenge password.
5809    */
5810   gnutls_x509_crq_set_challenge_password (crq, "something to remember here");
5811
5812   /* Associate the request with the private key
5813    */
5814   gnutls_x509_crq_set_key (crq, key);
5815
5816   /* Self sign the certificate request.
5817    */
5818   gnutls_privkey_import_x509( pkey, key, 0);
5819   gnutls_x509_crq_privkey_sign (crq, pkey, GNUTLS_DIG_SHA1, 0);
5820
5821   /* Export the PEM encoded certificate request, and
5822    * display it.
5823    */
5824   gnutls_x509_crq_export (crq, GNUTLS_X509_FMT_PEM, buffer, &buffer_size);
5825
5826   printf ("Certificate Request: \n%s", buffer);
5827
5828
5829   /* Export the PEM encoded private key, and
5830    * display it.
5831    */
5832   buffer_size = sizeof (buffer);
5833   gnutls_x509_privkey_export (key, GNUTLS_X509_FMT_PEM, buffer, &buffer_size);
5834
5835   printf ("\n\nPrivate key: \n%s", buffer);
5836
5837   gnutls_x509_crq_deinit (crq);
5838   gnutls_x509_privkey_deinit (key);
5839
5840   return 0;
5841
5842 }
5843
5844 \1f
5845 File: gnutls.info,  Node: PKCS #12 structure generation,  Prev: Certificate request generation,  Up: Miscellaneous examples
5846
5847 7.5.4 PKCS #12 Structure Generation
5848 -----------------------------------
5849
5850 The following example is about generating a PKCS #12 structure.
5851
5852 /* This example code is placed in the public domain. */
5853
5854 #ifdef HAVE_CONFIG_H
5855 #include <config.h>
5856 #endif
5857
5858 #include <stdio.h>
5859 #include <stdlib.h>
5860 #include <gnutls/gnutls.h>
5861 #include <gnutls/pkcs12.h>
5862
5863 #include "examples.h"
5864
5865 #define OUTFILE "out.p12"
5866
5867 /* This function will write a pkcs12 structure into a file.
5868  * cert: is a DER encoded certificate
5869  * pkcs8_key: is a PKCS #8 encrypted key (note that this must be
5870  *  encrypted using a PKCS #12 cipher, or some browsers will crash)
5871  * password: is the password used to encrypt the PKCS #12 packet.
5872  */
5873 int
5874 write_pkcs12 (const gnutls_datum_t * cert,
5875               const gnutls_datum_t * pkcs8_key, const char *password)
5876 {
5877   gnutls_pkcs12_t pkcs12;
5878   int ret, bag_index;
5879   gnutls_pkcs12_bag_t bag, key_bag;
5880   char pkcs12_struct[10 * 1024];
5881   size_t pkcs12_struct_size;
5882   FILE *fd;
5883
5884   /* A good idea might be to use gnutls_x509_privkey_get_key_id()
5885    * to obtain a unique ID.
5886    */
5887   gnutls_datum_t key_id = { (char *) "\x00\x00\x07", 3 };
5888
5889   gnutls_global_init ();
5890
5891   /* Firstly we create two helper bags, which hold the certificate,
5892    * and the (encrypted) key.
5893    */
5894
5895   gnutls_pkcs12_bag_init (&bag);
5896   gnutls_pkcs12_bag_init (&key_bag);
5897
5898   ret = gnutls_pkcs12_bag_set_data (bag, GNUTLS_BAG_CERTIFICATE, cert);
5899   if (ret < 0)
5900     {
5901       fprintf (stderr, "ret: %s\n", gnutls_strerror (ret));
5902       return 1;
5903     }
5904
5905   /* ret now holds the bag's index.
5906    */
5907   bag_index = ret;
5908
5909   /* Associate a friendly name with the given certificate. Used
5910    * by browsers.
5911    */
5912   gnutls_pkcs12_bag_set_friendly_name (bag, bag_index, "My name");
5913
5914   /* Associate the certificate with the key using a unique key
5915    * ID.
5916    */
5917   gnutls_pkcs12_bag_set_key_id (bag, bag_index, &key_id);
5918
5919   /* use weak encryption for the certificate.
5920    */
5921   gnutls_pkcs12_bag_encrypt (bag, password, GNUTLS_PKCS_USE_PKCS12_RC2_40);
5922
5923   /* Now the key.
5924    */
5925
5926   ret = gnutls_pkcs12_bag_set_data (key_bag,
5927                                     GNUTLS_BAG_PKCS8_ENCRYPTED_KEY,
5928                                     pkcs8_key);
5929   if (ret < 0)
5930     {
5931       fprintf (stderr, "ret: %s\n", gnutls_strerror (ret));
5932       return 1;
5933     }
5934
5935   /* Note that since the PKCS #8 key is already encrypted we don't
5936    * bother encrypting that bag.
5937    */
5938   bag_index = ret;
5939
5940   gnutls_pkcs12_bag_set_friendly_name (key_bag, bag_index, "My name");
5941
5942   gnutls_pkcs12_bag_set_key_id (key_bag, bag_index, &key_id);
5943
5944
5945   /* The bags were filled. Now create the PKCS #12 structure.
5946    */
5947   gnutls_pkcs12_init (&pkcs12);
5948
5949   /* Insert the two bags in the PKCS #12 structure.
5950    */
5951
5952   gnutls_pkcs12_set_bag (pkcs12, bag);
5953   gnutls_pkcs12_set_bag (pkcs12, key_bag);
5954
5955
5956   /* Generate a message authentication code for the PKCS #12
5957    * structure.
5958    */
5959   gnutls_pkcs12_generate_mac (pkcs12, password);
5960
5961   pkcs12_struct_size = sizeof (pkcs12_struct);
5962   ret =
5963     gnutls_pkcs12_export (pkcs12, GNUTLS_X509_FMT_DER, pkcs12_struct,
5964                           &pkcs12_struct_size);
5965   if (ret < 0)
5966     {
5967       fprintf (stderr, "ret: %s\n", gnutls_strerror (ret));
5968       return 1;
5969     }
5970
5971   fd = fopen (OUTFILE, "w");
5972   if (fd == NULL)
5973     {
5974       fprintf (stderr, "cannot open file\n");
5975       return 1;
5976     }
5977   fwrite (pkcs12_struct, 1, pkcs12_struct_size, fd);
5978   fclose (fd);
5979
5980   gnutls_pkcs12_bag_deinit (bag);
5981   gnutls_pkcs12_bag_deinit (key_bag);
5982   gnutls_pkcs12_deinit (pkcs12);
5983
5984   return 0;
5985 }
5986
5987 \1f
5988 File: gnutls.info,  Node: Parameter generation,  Next: Keying Material Exporters,  Prev: Miscellaneous examples,  Up: How to use GnuTLS in applications
5989
5990 7.6 Parameter generation
5991 ========================
5992
5993 Several TLS ciphersuites require additional parameters that need to be
5994 generated or provided by the application.  The Diffie-Hellman based
5995 ciphersuites (ANON-DH or DHE), require the group information to be
5996 provided.  This information can be either be generated on the fly using
5997 *note gnutls_dh_params_generate2:: or imported from some pregenerated
5998 value using *note gnutls_dh_params_import_pkcs3::.  The parameters can
5999 be used in a session by calling *note gnutls_certificate_set_dh_params::
6000 or *note gnutls_anon_set_server_dh_params:: for anonymous sessions.
6001
6002 Due to the time-consuming calculations required for the generation of
6003 Diffie-Hellman parameters we suggest against performing generation of
6004 them within an application.  The 'certtool' tool can be used to generate
6005 or export known safe values that can be stored in code or in a
6006 configuration file to provide the ability to replace.  We also recommend
6007 the usage of *note gnutls_sec_param_to_pk_bits:: to determine the bit
6008 size of the parameters to be generated.
6009
6010 The ciphersuites that involve the RSA-EXPORT key exchange require
6011 additional parameters.  Those ciphersuites are rarely used today because
6012 they are by design insecure, thus if you have no requirement for them,
6013 this section should be skipped.  The RSA-EXPORT key exchange requires
6014 512-bit RSA keys to be generated.  It is recommended those parameters to
6015 be refreshed (regenerated) in short intervals.  The following functions
6016 can be used for these parameters.
6017
6018    * *note gnutls_rsa_params_generate2::
6019
6020    * *note gnutls_certificate_set_rsa_export_params::
6021
6022    * *note gnutls_rsa_params_import_pkcs1::
6023
6024    * *note gnutls_rsa_params_export_pkcs1::
6025
6026 \1f
6027 File: gnutls.info,  Node: Keying Material Exporters,  Next: Channel Bindings,  Prev: Parameter generation,  Up: How to use GnuTLS in applications
6028
6029 7.7 Keying Material Exporters
6030 =============================
6031
6032 The TLS PRF can be used by other protocols to derive data.  The API to
6033 use is *note gnutls_prf::.  The function needs to be provided with the
6034 label in the parameter 'label', and the extra data to mix in the 'extra'
6035 parameter.  Depending on whether you want to mix in the client or server
6036 random data first, you can set the 'server_random_first' parameter.
6037
6038 For example, after establishing a TLS session using *note
6039 gnutls_handshake::, you can invoke the TLS PRF with this call:
6040
6041      #define MYLABEL "EXPORTER-FOO"
6042      #define MYCONTEXT "some context data"
6043      char out[32];
6044      rc = gnutls_prf (session, strlen (MYLABEL), MYLABEL, 0,
6045                       strlen (MYCONTEXT), MYCONTEXT, 32, out);
6046
6047 If you don't want to mix in the client/server random, there is a more
6048 low-level TLS PRF interface called *note gnutls_prf_raw::.
6049
6050 \1f
6051 File: gnutls.info,  Node: Channel Bindings,  Next: Compatibility with the OpenSSL library,  Prev: Keying Material Exporters,  Up: How to use GnuTLS in applications
6052
6053 7.8 Channel Bindings
6054 ====================
6055
6056 In user authentication protocols (e.g., EAP or SASL mechanisms) it is
6057 useful to have a unique string that identifies the secure channel that
6058 is used, to bind together the user authentication with the secure
6059 channel.  This can protect against man-in-the-middle attacks in some
6060 situations.  The unique strings is a "channel bindings".  For background
6061 and more discussion see [RFC5056] (*note Bibliography::).
6062
6063 You can extract a channel bindings using the *note
6064 gnutls_session_channel_binding:: function.  Currently only the
6065 'GNUTLS_CB_TLS_UNIQUE' type is supported, which corresponds to the
6066 'tls-unique' channel bindings for TLS defined in [RFC5929] (*note
6067 Bibliography::).
6068
6069 The following example describes how to print the channel binding data.
6070 Note that it must be run after a successful TLS handshake.
6071
6072      {
6073        gnutls_datum cb;
6074        int rc;
6075
6076        rc = gnutls_session_channel_binding (session,
6077                                             GNUTLS_CB_TLS_UNIQUE,
6078                                             &cb);
6079        if (rc)
6080          fprintf (stderr, "Channel binding error: %s\n",
6081                   gnutls_strerror (rc));
6082        else
6083          {
6084            size_t i;
6085            printf ("- Channel binding 'tls-unique': ");
6086            for (i = 0; i < cb.size; i++)
6087              printf ("%02x", cb.data[i]);
6088            printf ("\n");
6089          }
6090      }
6091
6092 \1f
6093 File: gnutls.info,  Node: Compatibility with the OpenSSL library,  Prev: Channel Bindings,  Up: How to use GnuTLS in applications
6094
6095 7.9 Compatibility with the OpenSSL Library
6096 ==========================================
6097
6098 To ease GnuTLS' integration with existing applications, a compatibility
6099 layer with the widely used OpenSSL library is included in the
6100 'gnutls-openssl' library.  This compatibility layer is not complete and
6101 it is not intended to completely reimplement the OpenSSL API with
6102 GnuTLS.  It only provides source-level compatibility.  There is
6103 currently no attempt to make it binary-compatible with OpenSSL.
6104
6105 The prototypes for the compatibility functions are in the
6106 'gnutls/openssl.h' header file.
6107
6108 Current limitations imposed by the compatibility layer include:
6109
6110    * Error handling is not thread safe.
6111
6112 \1f
6113 File: gnutls.info,  Node: Included programs,  Next: Function reference,  Prev: How to use GnuTLS in applications,  Up: Top
6114
6115 8 Included Programs
6116 *******************
6117
6118 Included with GnuTLS are also a few command line tools that let you use
6119 the library for common tasks without writing an application.  The
6120 applications are discussed in this chapter.
6121
6122 * Menu:
6123
6124 * Invoking certtool::
6125 * Invoking gnutls-cli::
6126 * Invoking gnutls-cli-debug::
6127 * Invoking gnutls-serv::
6128 * Invoking psktool::
6129 * Invoking srptool::
6130 * Invoking p11tool::
6131
6132 \1f
6133 File: gnutls.info,  Node: Invoking certtool,  Next: Invoking gnutls-cli,  Up: Included programs
6134
6135 8.1 Invoking certtool
6136 =====================
6137
6138 This is a program to generate X.509 certificates, certificate requests,
6139 CRLs and private keys.
6140
6141 Certtool help
6142 Usage: certtool [options]
6143      -s, --generate-self-signed
6144                               Generate a self-signed certificate.
6145      -c, --generate-certificate
6146                               Generate a signed certificate.
6147      --generate-proxy         Generate a proxy certificate.
6148      --generate-crl           Generate a CRL.
6149      -u, --update-certificate
6150                               Update a signed certificate.
6151      -p, --generate-privkey   Generate a private key.
6152      -q, --generate-request   Generate a PKCS #10 certificate
6153                               request.
6154      -e, --verify-chain       Verify a PEM encoded certificate chain.
6155                               The last certificate in the chain must
6156                               be a self signed one.
6157      --verify-crl             Verify a CRL.
6158      --generate-dh-params     Generate PKCS #3 encoded Diffie-Hellman
6159                               parameters.
6160      --get-dh-params          Get the included PKCS #3 encoded Diffie
6161                               Hellman parameters.
6162      --load-privkey FILE      Private key file to use.
6163      --load-request FILE      Certificate request file to use.
6164      --load-certificate FILE
6165                               Certificate file to use.
6166      --load-ca-privkey FILE   Certificate authority's private key
6167                               file to use.
6168      --load-ca-certificate FILE
6169                               Certificate authority's certificate
6170                               file to use.
6171      --password PASSWORD      Password to use.
6172      -i, --certificate-info   Print information on a certificate.
6173      -l, --crl-info           Print information on a CRL.
6174      --p12-info               Print information on a PKCS #12
6175                               structure.
6176      --p7-info                Print information on a PKCS #7
6177                               structure.
6178      --smime-to-p7            Convert S/MIME to PKCS #7 structure.
6179      -k, --key-info           Print information on a private key.
6180      --fix-key                Regenerate the parameters in a private
6181                               key.
6182      --to-p12                 Generate a PKCS #12 structure.
6183      -8, --pkcs8              Use PKCS #8 format for private keys.
6184      --dsa                    Use DSA keys.
6185      --hash STR               Hash algorithm to use for signing
6186                               (MD5,SHA1,RMD160).
6187      --export-ciphers         Use weak encryption algorithms.
6188      --inder                  Use DER format for input certificates
6189                               and private keys.
6190      --outder                 Use DER format for output certificates
6191                               and private keys.
6192      --bits BITS              specify the number of bits for key
6193                               generation.
6194      --outfile FILE           Output file.
6195      --infile FILE            Input file.
6196      --template FILE          Template file to use for non
6197                               interactive operation.
6198      -d, --debug LEVEL        specify the debug level. Default is 1.
6199      -h, --help               shows this help text
6200      -v, --version            shows the program's version
6201
6202 The program can be used interactively or non interactively by specifying
6203 the '--template' command line option.  See below for an example of a
6204 template file.
6205
6206 How to use certtool interactively:
6207
6208    * To generate parameters for Diffie-Hellman key exchange, use the
6209      command:
6210           $ certtool --generate-dh-params --outfile dh.pem
6211
6212    * To generate parameters for the RSA-EXPORT key exchange, use the
6213      command:
6214           $ certtool --generate-privkey --bits 512 --outfile rsa.pem
6215
6216    * To create a self signed certificate, use the command:
6217           $ certtool --generate-privkey --outfile ca-key.pem
6218           $ certtool --generate-self-signed --load-privkey ca-key.pem \
6219              --outfile ca-cert.pem
6220
6221      Note that a self-signed certificate usually belongs to a
6222      certificate authority, that signs other certificates.
6223
6224    * To create a private key (RSA by default), run:
6225
6226           $ certtool --generate-privkey --outfile key.pem
6227
6228      To create a DSA private key, run:
6229
6230           $ certtool --dsa --generate-privkey --outfile key-dsa.pem
6231
6232    * To generate a certificate using the private key, use the command:
6233
6234           $ certtool --generate-certificate --load-privkey key.pem \
6235              --outfile cert.pem --load-ca-certificate ca-cert.pem \
6236              --load-ca-privkey ca-key.pem
6237
6238    * To create a certificate request (needed when the certificate is
6239      issued by another party), run:
6240
6241           $ certtool --generate-request --load-privkey key.pem \
6242             --outfile request.pem
6243
6244    * To generate a certificate using the previous request, use the
6245      command:
6246
6247           $ certtool --generate-certificate --load-request request.pem \
6248              --outfile cert.pem \
6249              --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem
6250
6251    * To view the certificate information, use:
6252
6253           $ certtool --certificate-info --infile cert.pem
6254
6255    * To generate a PKCS #12 structure using the previous key and
6256      certificate, use the command:
6257
6258           $ certtool --load-certificate cert.pem --load-privkey key.pem \
6259             --to-p12 --outder --outfile key.p12
6260
6261      Some tools (reportedly web browsers) have problems with that file
6262      because it does not contain the CA certificate for the certificate.
6263      To work around that problem in the tool, you can use the
6264      '--load-ca-certificate' parameter as follows:
6265
6266           $ certtool --load-ca-certificate ca.pem \
6267             --load-certificate cert.pem --load-privkey key.pem \
6268             --to-p12 --outder --outfile key.p12
6269
6270    * Proxy certificate can be used to delegate your credential to a
6271      temporary, typically short-lived, certificate.  To create one from
6272      the previously created certificate, first create a temporary key
6273      and then generate a proxy certificate for it, using the commands:
6274
6275           $ certtool --generate-privkey > proxy-key.pem
6276           $ certtool --generate-proxy --load-ca-privkey key.pem \
6277             --load-privkey proxy-key.pem --load-certificate cert.pem \
6278             --outfile proxy-cert.pem
6279
6280    * To create an empty Certificate Revocation List (CRL) do:
6281
6282           $ certtool --generate-crl --load-ca-privkey x509-ca-key.pem --load-ca-certificate x509-ca.pem
6283
6284      To create a CRL that contains some revoked certificates, place the
6285      certificates in a file and use '--load-certificate' as follows:
6286
6287           $ certtool --generate-crl --load-ca-privkey x509-ca-key.pem --load-ca-certificate x509-ca.pem --load-certificate revoked-certs.pem
6288
6289    * To verify a Certificate Revocation List (CRL) do:
6290
6291           $ certtool --verify-crl --load-ca-certificate x509-ca.pem < crl.pem
6292
6293 Certtool's template file format:
6294
6295    * Firstly create a file named 'cert.cfg' that contains the
6296      information about the certificate.  An example file is listed
6297      below.
6298
6299    * Then execute:
6300
6301           $ certtool --generate-certificate cert.pem --load-privkey key.pem  \
6302              --template cert.cfg \
6303              --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem
6304
6305 An example certtool template file:
6306
6307      # X.509 Certificate options
6308      #
6309      # DN options
6310
6311      # The organization of the subject.
6312      organization = "Koko inc."
6313
6314      # The organizational unit of the subject.
6315      unit = "sleeping dept."
6316
6317      # The locality of the subject.
6318      # locality =
6319
6320      # The state of the certificate owner.
6321      state = "Attiki"
6322
6323      # The country of the subject. Two letter code.
6324      country = GR
6325
6326      # The common name of the certificate owner.
6327      cn = "Cindy Lauper"
6328
6329      # A user id of the certificate owner.
6330      #uid = "clauper"
6331
6332      # If the supported DN OIDs are not adequate you can set
6333      # any OID here.
6334      # For example set the X.520 Title and the X.520 Pseudonym
6335      # by using OID and string pairs.
6336      #dn_oid = "2.5.4.12" "Dr." "2.5.4.65" "jackal"
6337
6338      # This is deprecated and should not be used in new
6339      # certificates.
6340      # pkcs9_email = "none@none.org"
6341
6342      # The serial number of the certificate
6343      serial = 007
6344
6345      # In how many days, counting from today, this certificate will expire.
6346      expiration_days = 700
6347
6348      # X.509 v3 extensions
6349
6350      # A dnsname in case of a WWW server.
6351      #dns_name = "www.none.org"
6352      #dns_name = "www.morethanone.org"
6353
6354      # An IP address in case of a server.
6355      #ip_address = "192.168.1.1"
6356
6357      # An email in case of a person
6358      email = "none@none.org"
6359
6360      # An URL that has CRLs (certificate revocation lists)
6361      # available. Needed in CA certificates.
6362      #crl_dist_points = "http://www.getcrl.crl/getcrl/"
6363
6364      # Whether this is a CA certificate or not
6365      #ca
6366
6367      # Whether this certificate will be used for a TLS client
6368      #tls_www_client
6369
6370      # Whether this certificate will be used for a TLS server
6371      #tls_www_server
6372
6373      # Whether this certificate will be used to sign data (needed
6374      # in TLS DHE ciphersuites).
6375      signing_key
6376
6377      # Whether this certificate will be used to encrypt data (needed
6378      # in TLS RSA ciphersuites). Note that it is preferred to use different
6379      # keys for encryption and signing.
6380      #encryption_key
6381
6382      # Whether this key will be used to sign other certificates.
6383      #cert_signing_key
6384
6385      # Whether this key will be used to sign CRLs.
6386      #crl_signing_key
6387
6388      # Whether this key will be used to sign code.
6389      #code_signing_key
6390
6391      # Whether this key will be used to sign OCSP data.
6392      #ocsp_signing_key
6393
6394      # Whether this key will be used for time stamping.
6395      #time_stamping_key
6396
6397      # Whether this key will be used for IPsec IKE operations.
6398      #ipsec_ike_key
6399
6400 \1f
6401 File: gnutls.info,  Node: Invoking gnutls-cli,  Next: Invoking gnutls-cli-debug,  Prev: Invoking certtool,  Up: Included programs
6402
6403 8.2 Invoking gnutls-cli
6404 =======================
6405
6406 Simple client program to set up a TLS connection to some other computer.
6407 It sets up a TLS connection and forwards data from the standard input to
6408 the secured socket and vice versa.
6409
6410 GnuTLS test client
6411 Usage:  gnutls-cli [options] hostname
6412
6413      -d, --debug integer      Enable debugging
6414      -r, --resume             Connect, establish a session. Connect
6415                               again and resume this session.
6416      -s, --starttls           Connect, establish a plain session and
6417                               start TLS when EOF or a SIGALRM is
6418                               received.
6419      --crlf                   Send CR LF instead of LF.
6420      --x509fmtder             Use DER format for certificates to read
6421                               from.
6422      -f, --fingerprint        Send the openpgp fingerprint, instead
6423                               of the key.
6424      --disable-extensions     Disable all the TLS extensions.
6425      --print-cert             Print the certificate in PEM format.
6426      --recordsize integer     The maximum record size to advertize.
6427      -V, --verbose            More verbose output.
6428      --ciphers cipher1 cipher2...
6429                               Ciphers to enable.
6430      --protocols protocol1 protocol2...
6431                               Protocols to enable.
6432      --comp comp1 comp2...    Compression methods to enable.
6433      --macs mac1 mac2...      MACs to enable.
6434      --kx kx1 kx2...          Key exchange methods to enable.
6435      --ctypes certType1 certType2...
6436                               Certificate types to enable.
6437      --priority PRIORITY STRING
6438                               Priorities string.
6439      --x509cafile FILE        Certificate file to use.
6440      --x509crlfile FILE       CRL file to use.
6441      --pgpkeyfile FILE        PGP Key file to use.
6442      --pgpkeyring FILE        PGP Key ring file to use.
6443      --pgpcertfile FILE       PGP Public Key (certificate) file to
6444                               use.
6445      --pgpsubkey HEX|auto     PGP subkey to use.
6446      --x509keyfile FILE       X.509 key file to use.
6447      --x509certfile FILE      X.509 Certificate file to use.
6448      --srpusername NAME       SRP username to use.
6449      --srppasswd PASSWD       SRP password to use.
6450      --pskusername NAME       PSK username to use.
6451      --pskkey KEY             PSK key (in hex) to use.
6452      --opaque-prf-input DATA
6453                               Use Opaque PRF Input DATA.
6454      -p, --port PORT          The port to connect to.
6455      --insecure               Don't abort program if server
6456                               certificate can't be validated.
6457      -l, --list               Print a list of the supported
6458                               algorithms and modes.
6459      -h, --help               prints this help
6460      -v, --version            prints the program's version number
6461
6462 To connect to a server using PSK authentication, you may use something
6463 like:
6464
6465      $ gnutls-cli -p 5556 test.gnutls.org --pskusername jas --pskkey 9e32cf7786321a828ef7668f09fb35db --priority NORMAL:+DHE-PSK:+PSK:-RSA:-DHE-RSA -d 4711
6466
6467 * Menu:
6468
6469 * Example client PSK connection::
6470
6471 \1f
6472 File: gnutls.info,  Node: Example client PSK connection,  Up: Invoking gnutls-cli
6473
6474 8.2.1 Example client PSK connection
6475 -----------------------------------
6476
6477 If your server only supports the PSK ciphersuite, connecting to it
6478 should be as simple as connecting to the server:
6479
6480      $ ./gnutls-cli -p 5556 localhost
6481      Resolving 'localhost'...
6482      Connecting to '127.0.0.1:5556'...
6483      - PSK client callback.
6484      Enter PSK identity: psk_identity
6485      Enter password:
6486      - PSK authentication.
6487      - Version: TLS1.1
6488      - Key Exchange: PSK
6489      - Cipher: AES-128-CBC
6490      - MAC: SHA1
6491      - Compression: NULL
6492      - Handshake was completed
6493
6494      - Simple Client Mode:
6495
6496 If the server supports several cipher suites, you may need to force it
6497 to chose PSK by using a cipher priority parameter such as '--priority
6498 NORMAL:+PSK:-RSA:-DHE-RSA:-DHE-PSK'.
6499
6500 Instead of using the Netconf-way to derive the PSK key from a password,
6501 you can also give the PSK username and key directly on the command line:
6502
6503      $ ./gnutls-cli -p 5556 localhost --pskusername psk_identity --pskkey 88f3824b3e5659f52d00e959bacab954b6540344  --priority NORMAL:+DHE-PSK:+PSK
6504      Resolving 'localhost'...
6505      Connecting to '127.0.0.1:5556'...
6506      - PSK authentication.
6507      - Version: TLS1.1
6508      - Key Exchange: PSK
6509      - Cipher: AES-128-CBC
6510      - MAC: SHA1
6511      - Compression: NULL
6512      - Handshake was completed
6513
6514      - Simple Client Mode:
6515
6516 By keeping the '--pskusername' parameter and removing the '--pskkey'
6517 parameter, it will query only for the password during the handshake.
6518
6519 \1f
6520 File: gnutls.info,  Node: Invoking gnutls-cli-debug,  Next: Invoking gnutls-serv,  Prev: Invoking gnutls-cli,  Up: Included programs
6521
6522 8.3 Invoking gnutls-cli-debug
6523 =============================
6524
6525 This program was created to assist in debugging GnuTLS, but it might be
6526 useful to extract a TLS server's capabilities.  It's purpose is to
6527 connect onto a TLS server, perform some tests and print the server's
6528 capabilities.  If called with the '-v' parameter a more checks will be
6529 performed.  An example output is:
6530
6531      crystal:/cvs/gnutls/src$ ./gnutls-cli-debug localhost -p 5556
6532      Resolving 'localhost'...
6533      Connecting to '127.0.0.1:5556'...
6534      Checking for TLS 1.1 support... yes
6535      Checking fallback from TLS 1.1 to... N/A
6536      Checking for TLS 1.0 support... yes
6537      Checking for SSL 3.0 support... yes
6538      Checking for version rollback bug in RSA PMS... no
6539      Checking for version rollback bug in Client Hello... no
6540      Checking whether we need to disable TLS 1.0... N/A
6541      Checking whether the server ignores the RSA PMS version... no
6542      Checking whether the server can accept Hello Extensions... yes
6543      Checking whether the server can accept cipher suites not in SSL 3.0 spec... yes
6544      Checking whether the server can accept a bogus TLS record version in the client hello... yes
6545      Checking for certificate information... N/A
6546      Checking for trusted CAs... N/A
6547      Checking whether the server understands TLS closure alerts... yes
6548      Checking whether the server supports session resumption... yes
6549      Checking for export-grade ciphersuite support... no
6550      Checking RSA-export ciphersuite info... N/A
6551      Checking for anonymous authentication support... no
6552      Checking anonymous Diffie-Hellman group info... N/A
6553      Checking for ephemeral Diffie-Hellman support... no
6554      Checking ephemeral Diffie-Hellman group info... N/A
6555      Checking for AES cipher support (TLS extension)... yes
6556      Checking for 3DES cipher support... yes
6557      Checking for ARCFOUR 128 cipher support... yes
6558      Checking for ARCFOUR 40 cipher support... no
6559      Checking for MD5 MAC support... yes
6560      Checking for SHA1 MAC support... yes
6561      Checking for ZLIB compression support (TLS extension)... yes
6562      Checking for LZO compression support (GnuTLS extension)... yes
6563      Checking for max record size (TLS extension)... yes
6564      Checking for SRP authentication support (TLS extension)... yes
6565      Checking for OpenPGP authentication support (TLS extension)... no
6566
6567 \1f
6568 File: gnutls.info,  Node: Invoking gnutls-serv,  Next: Invoking psktool,  Prev: Invoking gnutls-cli-debug,  Up: Included programs
6569
6570 8.4 Invoking gnutls-serv
6571 ========================
6572
6573 Simple server program that listens to incoming TLS connections.
6574
6575 GnuTLS test server
6576 Usage: gnutls-serv [options]
6577
6578      -d, --debug integer      Enable debugging
6579      -g, --generate           Generate Diffie-Hellman Parameters.
6580      -p, --port integer       The port to connect to.
6581      -q, --quiet              Suppress some messages.
6582      --nodb                   Does not use the resume database.
6583      --http                   Act as an HTTP Server.
6584      --echo                   Act as an Echo Server.
6585      --dhparams FILE          DH params file to use.
6586      --x509fmtder             Use DER format for certificates
6587      --x509cafile FILE        Certificate file to use.
6588      --x509crlfile FILE       CRL file to use.
6589      --pgpkeyring FILE        PGP Key ring file to use.
6590      --pgpkeyfile FILE        PGP Key file to use.
6591      --pgpcertfile FILE       PGP Public Key (certificate) file to
6592                               use.
6593      --pgpsubkey HEX|auto     PGP subkey to use.
6594      --x509keyfile FILE       X.509 key file to use.
6595      --x509certfile FILE      X.509 Certificate file to use.
6596      --x509dsakeyfile FILE    Alternative X.509 key file to use.
6597      --x509dsacertfile FILE   Alternative X.509 certificate file to
6598                               use.
6599      -r, --require-cert       Require a valid certificate.
6600      -a, --disable-client-cert
6601                               Disable request for a client
6602                               certificate.
6603      --pskpasswd FILE         PSK password file to use.
6604      --pskhint HINT           PSK identity hint to use.
6605      --srppasswd FILE         SRP password file to use.
6606      --srppasswdconf FILE     SRP password conf file to use.
6607      --opaque-prf-input DATA
6608                               Use Opaque PRF Input DATA.
6609      --ciphers cipher1 cipher2...
6610                               Ciphers to enable.
6611      --protocols protocol1 protocol2...
6612                               Protocols to enable.
6613      --comp comp1 comp2...    Compression methods to enable.
6614      --macs mac1 mac2...      MACs to enable.
6615      --kx kx1 kx2...          Key exchange methods to enable.
6616      --ctypes certType1 certType2...
6617                               Certificate types to enable.
6618      --priority PRIORITY STRING
6619                               Priorities string.
6620      -l, --list               Print a list of the supported
6621                               algorithms  and modes.
6622      -h, --help               prints this help
6623      -v, --version            prints the program's version number
6624
6625 8.4.1 Setting Up a Test HTTPS Server
6626 ------------------------------------
6627
6628 Running your own TLS server based on GnuTLS can be useful when debugging
6629 clients and/or GnuTLS itself.  This section describes how to use
6630 'gnutls-serv' as a simple HTTPS server.
6631
6632 The most basic server can be started as:
6633
6634      gnutls-serv --http
6635
6636 It will only support anonymous ciphersuites, which many TLS clients
6637 refuse to use.
6638
6639 The next step is to add support for X.509.  First we generate a CA:
6640
6641      certtool --generate-privkey > x509-ca-key.pem
6642      echo 'cn = GnuTLS test CA' > ca.tmpl
6643      echo 'ca' >> ca.tmpl
6644      echo 'cert_signing_key' >> ca.tmpl
6645      certtool --generate-self-signed --load-privkey x509-ca-key.pem \
6646        --template ca.tmpl --outfile x509-ca.pem
6647      ...
6648
6649 Then generate a server certificate.  Remember to change the dns_name
6650 value to the name of your server host, or skip that command to avoid the
6651 field.
6652
6653      certtool --generate-privkey > x509-server-key.pem
6654      echo 'organization = GnuTLS test server' > server.tmpl
6655      echo 'cn = test.gnutls.org' >> server.tmpl
6656      echo 'tls_www_server' >> server.tmpl
6657      echo 'encryption_key' >> server.tmpl
6658      echo 'signing_key' >> server.tmpl
6659      echo 'dns_name = test.gnutls.org' >> server.tmpl
6660      certtool --generate-certificate --load-privkey x509-server-key.pem \
6661        --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
6662        --template server.tmpl --outfile x509-server.pem
6663      ...
6664
6665 For use in the client, you may want to generate a client certificate as
6666 well.
6667
6668      certtool --generate-privkey > x509-client-key.pem
6669      echo 'cn = GnuTLS test client' > client.tmpl
6670      echo 'tls_www_client' >> client.tmpl
6671      echo 'encryption_key' >> client.tmpl
6672      echo 'signing_key' >> client.tmpl
6673      certtool --generate-certificate --load-privkey x509-client-key.pem \
6674        --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
6675        --template client.tmpl --outfile x509-client.pem
6676      ...
6677
6678 To be able to import the client key/certificate into some applications,
6679 you will need to convert them into a PKCS#12 structure.  This also
6680 encrypts the security sensitive key with a password.
6681
6682      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
6683
6684 For icing, we'll create a proxy certificate for the client too.
6685
6686      certtool --generate-privkey > x509-proxy-key.pem
6687      echo 'cn = GnuTLS test client proxy' > proxy.tmpl
6688      certtool --generate-proxy --load-privkey x509-proxy-key.pem \
6689        --load-ca-certificate x509-client.pem --load-ca-privkey x509-client-key.pem \
6690        --load-certificate x509-client.pem --template proxy.tmpl \
6691        --outfile x509-proxy.pem
6692      ...
6693
6694 Then start the server again:
6695
6696      gnutls-serv --http \
6697                  --x509cafile x509-ca.pem \
6698                  --x509keyfile x509-server-key.pem \
6699                  --x509certfile x509-server.pem
6700
6701 Try connecting to the server using your web browser.  Note that the
6702 server listens to port 5556 by default.
6703
6704 While you are at it, to allow connections using DSA, you can also create
6705 a DSA key and certificate for the server.  These credentials will be
6706 used in the final example below.
6707
6708      certtool --generate-privkey --dsa > x509-server-key-dsa.pem
6709      certtool --generate-certificate --load-privkey x509-server-key-dsa.pem \
6710        --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
6711        --template server.tmpl --outfile x509-server-dsa.pem
6712      ...
6713
6714 The next step is to create OpenPGP credentials for the server.
6715
6716      gpg --gen-key
6717      ...enter whatever details you want, use 'test.gnutls.org' as name...
6718
6719 Make a note of the OpenPGP key identifier of the newly generated key,
6720 here it was '5D1D14D8'.  You will need to export the key for GnuTLS to
6721 be able to use it.
6722
6723      gpg -a --export 5D1D14D8 > openpgp-server.txt
6724      gpg --export 5D1D14D8 > openpgp-server.bin
6725      gpg --export-secret-keys 5D1D14D8 > openpgp-server-key.bin
6726      gpg -a --export-secret-keys 5D1D14D8 > openpgp-server-key.txt
6727
6728 Let's start the server with support for OpenPGP credentials:
6729
6730      gnutls-serv --http \
6731                  --pgpkeyfile openpgp-server-key.txt \
6732                  --pgpcertfile openpgp-server.txt
6733
6734 The next step is to add support for SRP authentication.
6735
6736      srptool --create-conf srp-tpasswd.conf
6737      srptool --passwd-conf srp-tpasswd.conf --username jas --passwd srp-passwd.txt
6738      Enter password: [TYPE "foo"]
6739
6740 Start the server with SRP support:
6741
6742      gnutls-serv --http \
6743                  --srppasswdconf srp-tpasswd.conf \
6744                  --srppasswd srp-passwd.txt
6745
6746 Let's also add support for PSK.
6747
6748      $ psktool --passwd psk-passwd.txt
6749
6750 Start the server with PSK support:
6751
6752      gnutls-serv --http \
6753                  --pskpasswd psk-passwd.txt
6754
6755 Finally, we start the server with all the earlier parameters and you get
6756 this command:
6757
6758      gnutls-serv --http \
6759                  --x509cafile x509-ca.pem \
6760                  --x509keyfile x509-server-key.pem \
6761                  --x509certfile x509-server.pem \
6762                  --x509dsakeyfile x509-server-key-dsa.pem \
6763                  --x509dsacertfile x509-server-dsa.pem \
6764                  --pgpkeyfile openpgp-server-key.txt \
6765                  --pgpcertfile openpgp-server.txt \
6766                  --srppasswdconf srp-tpasswd.conf \
6767                  --srppasswd srp-passwd.txt \
6768                  --pskpasswd psk-passwd.txt
6769
6770 * Menu:
6771
6772 * Example server PSK connection::
6773
6774 \1f
6775 File: gnutls.info,  Node: Example server PSK connection,  Up: Invoking gnutls-serv
6776
6777 8.4.2 Example server PSK connection
6778 -----------------------------------
6779
6780 To set up a PSK server with 'gnutls-serv' you need to create PSK
6781 password file (*note Invoking psktool::).  In the example below, I type
6782 'password' at the prompt.
6783
6784      $ ./psktool -u psk_identity -p psks.txt
6785      Enter password:
6786      Key stored to psks.txt
6787      $ cat psks.txt
6788      psk_identity:88f3824b3e5659f52d00e959bacab954b6540344
6789      $
6790
6791 After this, start the server pointing to the password file.  We disable
6792 DHE-PSK.
6793
6794      $ ./gnutls-serv --pskpasswd psks.txt  --pskhint psk_identity_hint --priority NORMAL:-DHE-PSK
6795      Set static Diffie-Hellman parameters, consider --dhparams.
6796      Echo Server ready. Listening to port '5556'.
6797
6798 You can now connect to the server using a PSK client (*note Example
6799 client PSK connection::).
6800
6801 \1f
6802 File: gnutls.info,  Node: Invoking psktool,  Next: Invoking srptool,  Prev: Invoking gnutls-serv,  Up: Included programs
6803
6804 8.5 Invoking psktool
6805 ====================
6806
6807 This is a program to manage PSK username and keys.
6808
6809 PSKtool help
6810 Usage : psktool [options]
6811      -u, --username username
6812                               specify username.
6813      -p, --passwd FILE        specify a password file.
6814      -n, --netconf-hint HINT
6815                               derive key from Netconf password, using
6816                               HINT as the psk_identity_hint.
6817      -s, --keysize SIZE       specify the key size in bytes.
6818      -v, --version            prints the program's version number
6819      -h, --help               shows this help text
6820
6821 Normally the file will generate random keys for the indicate username.
6822 You may also derive PSK keys from passwords, using the algorithm
6823 specified in 'draft-ietf-netconf-tls-02.txt'.  The algorithm needs a PSK
6824 identity hint, which you specify using '--netconf-hint'.  To derive a
6825 PSK key from a password with an empty PSK identity hint, using
6826 '--netconf-hint ""'.
6827
6828 \1f
6829 File: gnutls.info,  Node: Invoking srptool,  Next: Invoking p11tool,  Prev: Invoking psktool,  Up: Included programs
6830
6831 8.6 Invoking srptool
6832 ====================
6833
6834 The 'srptool' is a very simple program that emulates the programs in the
6835 _Stanford SRP libraries_, see 'http://srp.stanford.edu/'.  It is
6836 intended for use in places where you don't expect SRP authentication to
6837 be the used for system users.
6838
6839 Traditionally _libsrp_ used two files.  One called 'tpasswd' which holds
6840 usernames and verifiers, and 'tpasswd.conf' which holds generators and
6841 primes.
6842
6843 How to use srptool:
6844
6845    * To create tpasswd.conf which holds the g and n values for SRP
6846      protocol (generator and a large prime), run:
6847
6848           $ srptool --create-conf /etc/tpasswd.conf
6849
6850    * This command will create /etc/tpasswd and will add user 'test' (you
6851      will also be prompted for a password).  Verifiers are stored by
6852      default in the way libsrp expects.
6853
6854           $ srptool --passwd /etc/tpasswd \
6855               --passwd-conf /etc/tpasswd.conf -u test
6856
6857    * This command will check against a password.  If the password
6858      matches the one in /etc/tpasswd you will get an ok.
6859
6860           $ srptool --passwd /etc/tpasswd \
6861               --passwd-conf /etc/tpasswd.conf --verify -u test
6862
6863 \1f
6864 File: gnutls.info,  Node: Invoking p11tool,  Prev: Invoking srptool,  Up: Included programs
6865
6866 8.7 Invoking p11tool
6867 ====================
6868
6869 The 'p11tool' is a program that helps with accessing tokens and security
6870 modules that support the PKCS #11 API. It requires the individual PKCS
6871 #11 modules to be loaded either with the '--provider' option, or by
6872 setting up the GnuTLS configuration file for PKCS #11 as in *note
6873 sec:pkcs11::.
6874
6875 p11tool help
6876 Usage: p11tool [options]
6877
6878      --export URL             Export an object specified by a pkcs11
6879                               URL
6880      --list-tokens            List all available tokens
6881      --list-mechanisms URL    List all available mechanisms in token.
6882      --list-all               List all objects specified by a PKCS#11
6883                               URL
6884      --list-all-certs         List all certificates specified by a
6885                               PKCS#11 URL
6886      --list-certs             List certificates that have a private
6887                               key specified by a PKCS#11 URL
6888      --list-privkeys          List private keys specified by a
6889                               PKCS#11 URL
6890      --list-trusted           List certificates marked as trusted,
6891                               specified by a PKCS#11 URL
6892      --initialize URL         Initializes a PKCS11 token.
6893      --write URL              Writes loaded certificates, private or
6894                               secret keys to a PKCS11 token.
6895      --delete URL             Deletes objects matching the URL.
6896      --label label            Sets a label for the write operation.
6897      --trusted                Marks the certificate to be imported as
6898                               trusted.
6899      --login                  Force login to token
6900      --detailed-url           Export detailed URLs.
6901      --no-detailed-url        Export less detailed URLs.
6902      --secret-key HEX_KEY     Provide a hex encoded secret key.
6903      --load-privkey FILE      Private key file to use.
6904      --load-pubkey FILE       Private key file to use.
6905      --load-certificate FILE
6906                               Certificate file to use.
6907      -8, --pkcs8              Use PKCS #8 format for private keys.
6908      --inder                  Use DER format for input certificates
6909                               and private keys.
6910      --inraw                  Use RAW/DER format for input
6911                               certificates and private keys.
6912      --provider Library       Specify the pkcs11 provider library
6913      --outfile FILE           Output file.
6914      -d, --debug LEVEL        specify the debug level. Default is 1.
6915      -h, --help               shows this help text
6916
6917 After being provided the available PKCS #11 modules, it can list all
6918 tokens available in your system, the objects on the tokens, and perform
6919 operations on them.
6920
6921 Some examples on how to use p11tool:
6922
6923    * List all tokens
6924           $ p11tool --list-tokens
6925
6926    * List all objects
6927           $ p11tool --login --list-all
6928
6929    * To export an object
6930           $ p11tool --login --export pkcs11:(OBJECT URL)
6931
6932    * To copy an object to a token
6933           $ p11tool --login --write pkcs11:(TOKEN URL) --load-certificate (certificate file) --label "my_cert"
6934
6935 Note that typically PKCS #11 private key objects are not allowed to be
6936 extracted from the token.
6937
6938 \1f
6939 File: gnutls.info,  Node: Function reference,  Next: All the supported ciphersuites in GnuTLS,  Prev: Included programs,  Up: Top
6940
6941 9 Function Reference
6942 ********************
6943
6944 * Menu:
6945
6946 * Core functions::
6947 * X.509 certificate functions::
6948 * GnuTLS-extra functions::
6949 * OpenPGP functions::
6950 * TLS Inner Application (TLS/IA) functions::
6951 * Error codes and descriptions::
6952
6953 \1f
6954 File: gnutls.info,  Node: Core functions,  Next: X.509 certificate functions,  Up: Function reference
6955
6956 9.1 Core Functions
6957 ==================
6958
6959 The prototypes for the following functions lie in 'gnutls/gnutls.h'.
6960
6961 gnutls_alert_get_name
6962 ---------------------
6963
6964  -- Function: const char * gnutls_alert_get_name
6965           (gnutls_alert_description_t ALERT)
6966      ALERT: is an alert number 'gnutls_session_t' structure.
6967
6968      This function will return a string that describes the given alert
6969      number, or 'NULL'.  See 'gnutls_alert_get()'.
6970
6971      *Returns:* string corresponding to 'gnutls_alert_description_t'
6972      value.
6973
6974 gnutls_alert_get
6975 ----------------
6976
6977  -- Function: gnutls_alert_description_t gnutls_alert_get
6978           (gnutls_session_t SESSION)
6979      SESSION: is a 'gnutls_session_t' structure.
6980
6981      This function will return the last alert number received.  This
6982      function should be called if 'GNUTLS_E_WARNING_ALERT_RECEIVED' or
6983      'GNUTLS_E_FATAL_ALERT_RECEIVED' has been returned by a gnutls
6984      function.  The peer may send alerts if he thinks some things were
6985      not right.  Check gnutls.h for the available alert descriptions.
6986
6987      If no alert has been received the returned value is undefined.
6988
6989      *Returns:* returns the last alert received, a
6990      'gnutls_alert_description_t' value.
6991
6992 gnutls_alert_send_appropriate
6993 -----------------------------
6994
6995  -- Function: int gnutls_alert_send_appropriate (gnutls_session_t
6996           SESSION, int ERR)
6997      SESSION: is a 'gnutls_session_t' structure.
6998
6999      ERR: is an integer
7000
7001      Sends an alert to the peer depending on the error code returned by
7002      a gnutls function.  This function will call
7003      'gnutls_error_to_alert()' to determine the appropriate alert to
7004      send.
7005
7006      This function may also return 'GNUTLS_E_AGAIN', or
7007      'GNUTLS_E_INTERRUPTED'.
7008
7009      If the return value is 'GNUTLS_E_INVALID_REQUEST', then no alert
7010      has been sent to the peer.
7011
7012      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
7013      otherwise an error code is returned.
7014
7015 gnutls_alert_send
7016 -----------------
7017
7018  -- Function: int gnutls_alert_send (gnutls_session_t SESSION,
7019           gnutls_alert_level_t LEVEL, gnutls_alert_description_t DESC)
7020      SESSION: is a 'gnutls_session_t' structure.
7021
7022      LEVEL: is the level of the alert
7023
7024      DESC: is the alert description
7025
7026      This function will send an alert to the peer in order to inform him
7027      of something important (eg.  his Certificate could not be
7028      verified).  If the alert level is Fatal then the peer is expected
7029      to close the connection, otherwise he may ignore the alert and
7030      continue.
7031
7032      The error code of the underlying record send function will be
7033      returned, so you may also receive 'GNUTLS_E_INTERRUPTED' or
7034      'GNUTLS_E_AGAIN' as well.
7035
7036      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
7037      otherwise an error code is returned.
7038
7039 gnutls_anon_allocate_client_credentials
7040 ---------------------------------------
7041
7042  -- Function: int gnutls_anon_allocate_client_credentials
7043           (gnutls_anon_client_credentials_t * SC)
7044      SC: is a pointer to a 'gnutls_anon_client_credentials_t' structure.
7045
7046      This structure is complex enough to manipulate directly thus this
7047      helper function is provided in order to allocate it.
7048
7049      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7050
7051 gnutls_anon_allocate_server_credentials
7052 ---------------------------------------
7053
7054  -- Function: int gnutls_anon_allocate_server_credentials
7055           (gnutls_anon_server_credentials_t * SC)
7056      SC: is a pointer to a 'gnutls_anon_server_credentials_t' structure.
7057
7058      This structure is complex enough to manipulate directly thus this
7059      helper function is provided in order to allocate it.
7060
7061      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7062
7063 gnutls_anon_free_client_credentials
7064 -----------------------------------
7065
7066  -- Function: void gnutls_anon_free_client_credentials
7067           (gnutls_anon_client_credentials_t SC)
7068      SC: is a 'gnutls_anon_client_credentials_t' structure.
7069
7070      This structure is complex enough to manipulate directly thus this
7071      helper function is provided in order to free (deallocate) it.
7072
7073 gnutls_anon_free_server_credentials
7074 -----------------------------------
7075
7076  -- Function: void gnutls_anon_free_server_credentials
7077           (gnutls_anon_server_credentials_t SC)
7078      SC: is a 'gnutls_anon_server_credentials_t' structure.
7079
7080      This structure is complex enough to manipulate directly thus this
7081      helper function is provided in order to free (deallocate) it.
7082
7083 gnutls_anon_set_params_function
7084 -------------------------------
7085
7086  -- Function: void gnutls_anon_set_params_function
7087           (gnutls_anon_server_credentials_t RES, gnutls_params_function
7088           * FUNC)
7089      RES: is a gnutls_anon_server_credentials_t structure
7090
7091      FUNC: is the function to be called
7092
7093      This function will set a callback in order for the server to get
7094      the Diffie-Hellman or RSA parameters for anonymous authentication.
7095      The callback should return zero on success.
7096
7097 gnutls_anon_set_server_dh_params
7098 --------------------------------
7099
7100  -- Function: void gnutls_anon_set_server_dh_params
7101           (gnutls_anon_server_credentials_t RES, gnutls_dh_params_t
7102           DH_PARAMS)
7103      RES: is a gnutls_anon_server_credentials_t structure
7104
7105      DH_PARAMS: is a structure that holds Diffie-Hellman parameters.
7106
7107      This function will set the Diffie-Hellman parameters for an
7108      anonymous server to use.  These parameters will be used in
7109      Anonymous Diffie-Hellman cipher suites.
7110
7111 gnutls_anon_set_server_params_function
7112 --------------------------------------
7113
7114  -- Function: void gnutls_anon_set_server_params_function
7115           (gnutls_anon_server_credentials_t RES, gnutls_params_function
7116           * FUNC)
7117      RES: is a gnutls_certificate_credentials_t structure
7118
7119      FUNC: is the function to be called
7120
7121      This function will set a callback in order for the server to get
7122      the Diffie-Hellman parameters for anonymous authentication.  The
7123      callback should return zero on success.
7124
7125 gnutls_auth_client_get_type
7126 ---------------------------
7127
7128  -- Function: gnutls_credentials_type_t gnutls_auth_client_get_type
7129           (gnutls_session_t SESSION)
7130      SESSION: is a 'gnutls_session_t' structure.
7131
7132      Returns the type of credentials that were used for client
7133      authentication.  The returned information is to be used to
7134      distinguish the function used to access authentication data.
7135
7136      *Returns:* The type of credentials for the client authentication
7137      schema, a 'gnutls_credentials_type_t' type.
7138
7139 gnutls_auth_get_type
7140 --------------------
7141
7142  -- Function: gnutls_credentials_type_t gnutls_auth_get_type
7143           (gnutls_session_t SESSION)
7144      SESSION: is a 'gnutls_session_t' structure.
7145
7146      Returns type of credentials for the current authentication schema.
7147      The returned information is to be used to distinguish the function
7148      used to access authentication data.
7149
7150      Eg.  for CERTIFICATE ciphersuites (key exchange algorithms:
7151      'GNUTLS_KX_RSA', 'GNUTLS_KX_DHE_RSA'), the same function are to be
7152      used to access the authentication data.
7153
7154      *Returns:* The type of credentials for the current authentication
7155      schema, a 'gnutls_credentials_type_t' type.
7156
7157 gnutls_auth_server_get_type
7158 ---------------------------
7159
7160  -- Function: gnutls_credentials_type_t gnutls_auth_server_get_type
7161           (gnutls_session_t SESSION)
7162      SESSION: is a 'gnutls_session_t' structure.
7163
7164      Returns the type of credentials that were used for server
7165      authentication.  The returned information is to be used to
7166      distinguish the function used to access authentication data.
7167
7168      *Returns:* The type of credentials for the server authentication
7169      schema, a 'gnutls_credentials_type_t' type.
7170
7171 gnutls_bye
7172 ----------
7173
7174  -- Function: int gnutls_bye (gnutls_session_t SESSION,
7175           gnutls_close_request_t HOW)
7176      SESSION: is a 'gnutls_session_t' structure.
7177
7178      HOW: is an integer
7179
7180      Terminates the current TLS/SSL connection.  The connection should
7181      have been initiated using 'gnutls_handshake()'.  'how' should be
7182      one of 'GNUTLS_SHUT_RDWR', 'GNUTLS_SHUT_WR'.
7183
7184      In case of 'GNUTLS_SHUT_RDWR' then the TLS connection gets
7185      terminated and further receives and sends will be disallowed.  If
7186      the return value is zero you may continue using the connection.
7187      'GNUTLS_SHUT_RDWR' actually sends an alert containing a close
7188      request and waits for the peer to reply with the same message.
7189
7190      In case of 'GNUTLS_SHUT_WR' then the TLS connection gets terminated
7191      and further sends will be disallowed.  In order to reuse the
7192      connection you should wait for an EOF from the peer.
7193      'GNUTLS_SHUT_WR' sends an alert containing a close request.
7194
7195      Note that not all implementations will properly terminate a TLS
7196      connection.  Some of them, usually for performance reasons, will
7197      terminate only the underlying transport layer, thus causing a
7198      transmission error to the peer.  This error cannot be distinguished
7199      from a malicious party prematurely terminating the session, thus
7200      this behavior is not recommended.
7201
7202      This function may also return 'GNUTLS_E_AGAIN' or
7203      'GNUTLS_E_INTERRUPTED'; cf.  'gnutls_record_get_direction()'.
7204
7205      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code, see
7206      function documentation for entire semantics.
7207
7208 gnutls_certificate_activation_time_peers
7209 ----------------------------------------
7210
7211  -- Function: time_t gnutls_certificate_activation_time_peers
7212           (gnutls_session_t SESSION)
7213      SESSION: is a gnutls session
7214
7215      This function will return the peer's certificate activation time.
7216      This is the creation time for openpgp keys.
7217
7218      *Returns:* (time_t)-1 on error.
7219
7220      *Deprecated:* 'gnutls_certificate_verify_peers2()' now verifies
7221      activation times.
7222
7223 gnutls_certificate_allocate_credentials
7224 ---------------------------------------
7225
7226  -- Function: int gnutls_certificate_allocate_credentials
7227           (gnutls_certificate_credentials_t * RES)
7228      RES: is a pointer to a 'gnutls_certificate_credentials_t'
7229      structure.
7230
7231      This structure is complex enough to manipulate directly thus this
7232      helper function is provided in order to allocate it.
7233
7234      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7235
7236 gnutls_certificate_client_get_request_status
7237 --------------------------------------------
7238
7239  -- Function: int gnutls_certificate_client_get_request_status
7240           (gnutls_session_t SESSION)
7241      SESSION: is a gnutls session
7242
7243      Get whether client certificate is requested or not.
7244
7245      *Returns:* 0 if the peer (server) did not request client
7246      authentication or 1 otherwise, or a negative value in case of
7247      error.
7248
7249 gnutls_certificate_client_set_retrieve_function
7250 -----------------------------------------------
7251
7252  -- Function: void gnutls_certificate_client_set_retrieve_function
7253           (gnutls_certificate_credentials_t CRED,
7254           gnutls_certificate_client_retrieve_function * FUNC)
7255      CRED: is a 'gnutls_certificate_credentials_t' structure.
7256
7257      FUNC: is the callback function
7258
7259      This function sets a callback to be called in order to retrieve the
7260      certificate to be used in the handshake.
7261
7262      The callback's function prototype is: int
7263      (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int
7264      nreqs, const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length,
7265      gnutls_retr_st* st);
7266
7267      'req_ca_cert' is only used in X.509 certificates.  Contains a list
7268      with the CA names that the server considers trusted.  Normally we
7269      should send a certificate that is signed by one of these CAs.
7270      These names are DER encoded.  To get a more meaningful value use
7271      the function 'gnutls_x509_rdn_get()'.
7272
7273      'pk_algos' contains a list with server's acceptable signature
7274      algorithms.  The certificate returned should support the server's
7275      given algorithms.
7276
7277      'st' should contain the certificates and private keys.
7278
7279      If the callback function is provided then gnutls will call it, in
7280      the handshake, after the certificate request message has been
7281      received.
7282
7283      The callback function should set the certificate list to be sent,
7284      and return 0 on success.  If no certificate was selected then the
7285      number of certificates should be set to zero.  The value (-1)
7286      indicates error and the handshake will be terminated.
7287
7288 gnutls_certificate_expiration_time_peers
7289 ----------------------------------------
7290
7291  -- Function: time_t gnutls_certificate_expiration_time_peers
7292           (gnutls_session_t SESSION)
7293      SESSION: is a gnutls session
7294
7295      This function will return the peer's certificate expiration time.
7296
7297      *Returns:* (time_t)-1 on error.
7298
7299      *Deprecated:* 'gnutls_certificate_verify_peers2()' now verifies
7300      expiration times.
7301
7302 gnutls_certificate_free_ca_names
7303 --------------------------------
7304
7305  -- Function: void gnutls_certificate_free_ca_names
7306           (gnutls_certificate_credentials_t SC)
7307      SC: is a 'gnutls_certificate_credentials_t' structure.
7308
7309      This function will delete all the CA name in the given credentials.
7310      Clients may call this to save some memory since in client side the
7311      CA names are not used.  Servers might want to use this function if
7312      a large list of trusted CAs is present and sending the names of it
7313      would just consume bandwidth without providing information to
7314      client.
7315
7316      CA names are used by servers to advertize the CAs they support to
7317      clients.
7318
7319 gnutls_certificate_free_cas
7320 ---------------------------
7321
7322  -- Function: void gnutls_certificate_free_cas
7323           (gnutls_certificate_credentials_t SC)
7324      SC: is a 'gnutls_certificate_credentials_t' structure.
7325
7326      This function will delete all the CAs associated with the given
7327      credentials.  Servers that do not use
7328      'gnutls_certificate_verify_peers2()' may call this to save some
7329      memory.
7330
7331 gnutls_certificate_free_credentials
7332 -----------------------------------
7333
7334  -- Function: void gnutls_certificate_free_credentials
7335           (gnutls_certificate_credentials_t SC)
7336      SC: is a 'gnutls_certificate_credentials_t' structure.
7337
7338      This structure is complex enough to manipulate directly thus this
7339      helper function is provided in order to free (deallocate) it.
7340
7341      This function does not free any temporary parameters associated
7342      with this structure (ie RSA and DH parameters are not freed by this
7343      function).
7344
7345 gnutls_certificate_free_crls
7346 ----------------------------
7347
7348  -- Function: void gnutls_certificate_free_crls
7349           (gnutls_certificate_credentials_t SC)
7350      SC: is a 'gnutls_certificate_credentials_t' structure.
7351
7352      This function will delete all the CRLs associated with the given
7353      credentials.
7354
7355 gnutls_certificate_free_keys
7356 ----------------------------
7357
7358  -- Function: void gnutls_certificate_free_keys
7359           (gnutls_certificate_credentials_t SC)
7360      SC: is a 'gnutls_certificate_credentials_t' structure.
7361
7362      This function will delete all the keys and the certificates
7363      associated with the given credentials.  This function must not be
7364      called when a TLS negotiation that uses the credentials is in
7365      progress.
7366
7367 gnutls_certificate_get_issuer
7368 -----------------------------
7369
7370  -- Function: int gnutls_certificate_get_issuer
7371           (gnutls_certificate_credentials_t SC, gnutls_x509_crt_t CERT,
7372           gnutls_x509_crt_t* ISSUER, unsigned int FLAGS)
7373      SC: is a 'gnutls_certificate_credentials_t' structure.
7374
7375      CERT: is the certificate to find issuer for
7376
7377      ISSUER: Will hold the issuer if any.  Should be treated as
7378      constant.
7379
7380      FLAGS: Use zero.
7381
7382      This function will return the issuer of a given certificate.
7383
7384      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
7385      negative error value.
7386
7387 gnutls_certificate_get_openpgp_keyring
7388 --------------------------------------
7389
7390  -- Function: void gnutls_certificate_get_openpgp_keyring
7391           (gnutls_certificate_credentials_t SC, gnutls_openpgp_keyring_t
7392           * KEYRING)
7393      SC: is a 'gnutls_certificate_credentials_t' structure.
7394
7395      KEYRING: the exported keyring.  Should be treated as constant
7396
7397      This function will export the OpenPGP keyring associated with the
7398      given credentials.
7399
7400      *Since:* 2.4.0
7401
7402 gnutls_certificate_get_ours
7403 ---------------------------
7404
7405  -- Function: const gnutls_datum_t * gnutls_certificate_get_ours
7406           (gnutls_session_t SESSION)
7407      SESSION: is a gnutls session
7408
7409      Get the certificate as sent to the peer, in the last handshake.
7410      These certificates are in raw format.  In X.509 this is a
7411      certificate list.  In OpenPGP this is a single certificate.
7412
7413      *Returns:* return a pointer to a 'gnutls_datum_t' containing our
7414      certificates, or 'NULL' in case of an error or if no certificate
7415      was used.
7416
7417 gnutls_certificate_get_peers
7418 ----------------------------
7419
7420  -- Function: const gnutls_datum_t * gnutls_certificate_get_peers
7421           (gnutls_session_t SESSION, unsigned int * LIST_SIZE)
7422      SESSION: is a gnutls session
7423
7424      LIST_SIZE: is the length of the certificate list
7425
7426      Get the peer's raw certificate (chain) as sent by the peer.  These
7427      certificates are in raw format (DER encoded for X.509).  In case of
7428      a X.509 then a certificate list may be present.  The first
7429      certificate in the list is the peer's certificate, following the
7430      issuer's certificate, then the issuer's issuer etc.
7431
7432      In case of OpenPGP keys a single key will be returned in raw
7433      format.
7434
7435      *Returns:* return a pointer to a 'gnutls_datum_t' containing our
7436      certificates, or 'NULL' in case of an error or if no certificate
7437      was used.
7438
7439 gnutls_certificate_get_x509_cas
7440 -------------------------------
7441
7442  -- Function: void gnutls_certificate_get_x509_cas
7443           (gnutls_certificate_credentials_t SC, gnutls_x509_crt_t **
7444           X509_CA_LIST, unsigned int * NCAS)
7445      SC: is a 'gnutls_certificate_credentials_t' structure.
7446
7447      X509_CA_LIST: will point to the CA list.  Should be treated as
7448      constant
7449
7450      NCAS: the number of CAs
7451
7452      This function will export all the CAs associated with the given
7453      credentials.
7454
7455      *Since:* 2.4.0
7456
7457 gnutls_certificate_get_x509_crls
7458 --------------------------------
7459
7460  -- Function: void gnutls_certificate_get_x509_crls
7461           (gnutls_certificate_credentials_t SC, gnutls_x509_crl_t **
7462           X509_CRL_LIST, unsigned int * NCRLS)
7463      SC: is a 'gnutls_certificate_credentials_t' structure.
7464
7465      X509_CRL_LIST: the exported CRL list.  Should be treated as
7466      constant
7467
7468      NCRLS: the number of exported CRLs
7469
7470      This function will export all the CRLs associated with the given
7471      credentials.
7472
7473      *Since:* 2.4.0
7474
7475 gnutls_certificate_send_x509_rdn_sequence
7476 -----------------------------------------
7477
7478  -- Function: void gnutls_certificate_send_x509_rdn_sequence
7479           (gnutls_session_t SESSION, int STATUS)
7480      SESSION: is a pointer to a 'gnutls_session_t' structure.
7481
7482      STATUS: is 0 or 1
7483
7484      If status is non zero, this function will order gnutls not to send
7485      the rdnSequence in the certificate request message.  That is the
7486      server will not advertize it's trusted CAs to the peer.  If status
7487      is zero then the default behaviour will take effect, which is to
7488      advertize the server's trusted CAs.
7489
7490      This function has no effect in clients, and in authentication
7491      methods other than certificate with X.509 certificates.
7492
7493 gnutls_certificate_server_set_request
7494 -------------------------------------
7495
7496  -- Function: void gnutls_certificate_server_set_request
7497           (gnutls_session_t SESSION, gnutls_certificate_request_t REQ)
7498      SESSION: is a 'gnutls_session_t' structure.
7499
7500      REQ: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
7501
7502      This function specifies if we (in case of a server) are going to
7503      send a certificate request message to the client.  If 'req' is
7504      GNUTLS_CERT_REQUIRE then the server will return an error if the
7505      peer does not provide a certificate.  If you do not call this
7506      function then the client will not be asked to send a certificate.
7507
7508 gnutls_certificate_server_set_retrieve_function
7509 -----------------------------------------------
7510
7511  -- Function: void gnutls_certificate_server_set_retrieve_function
7512           (gnutls_certificate_credentials_t CRED,
7513           gnutls_certificate_server_retrieve_function * FUNC)
7514      CRED: is a 'gnutls_certificate_credentials_t' structure.
7515
7516      FUNC: is the callback function
7517
7518      This function sets a callback to be called in order to retrieve the
7519      certificate to be used in the handshake.
7520
7521      The callback's function prototype is: int
7522      (*callback)(gnutls_session_t, gnutls_retr_st* st);
7523
7524      'st' should contain the certificates and private keys.
7525
7526      If the callback function is provided then gnutls will call it, in
7527      the handshake, after the certificate request message has been
7528      received.
7529
7530      The callback function should set the certificate list to be sent,
7531      and return 0 on success.  The value (-1) indicates error and the
7532      handshake will be terminated.
7533
7534 gnutls_certificate_set_dh_params
7535 --------------------------------
7536
7537  -- Function: void gnutls_certificate_set_dh_params
7538           (gnutls_certificate_credentials_t RES, gnutls_dh_params_t
7539           DH_PARAMS)
7540      RES: is a gnutls_certificate_credentials_t structure
7541
7542      DH_PARAMS: is a structure that holds Diffie-Hellman parameters.
7543
7544      This function will set the Diffie-Hellman parameters for a
7545      certificate server to use.  These parameters will be used in
7546      Ephemeral Diffie-Hellman cipher suites.  Note that only a pointer
7547      to the parameters are stored in the certificate handle, so if you
7548      deallocate the parameters before the certificate is deallocated,
7549      you must change the parameters stored in the certificate first.
7550
7551 gnutls_certificate_set_params_function
7552 --------------------------------------
7553
7554  -- Function: void gnutls_certificate_set_params_function
7555           (gnutls_certificate_credentials_t RES, gnutls_params_function
7556           * FUNC)
7557      RES: is a gnutls_certificate_credentials_t structure
7558
7559      FUNC: is the function to be called
7560
7561      This function will set a callback in order for the server to get
7562      the Diffie-Hellman or RSA parameters for certificate
7563      authentication.  The callback should return zero on success.
7564
7565 gnutls_certificate_set_retrieve_function
7566 ----------------------------------------
7567
7568  -- Function: void gnutls_certificate_set_retrieve_function
7569           (gnutls_certificate_credentials_t CRED,
7570           gnutls_certificate_retrieve_function * FUNC)
7571      CRED: is a 'gnutls_certificate_credentials_t' structure.
7572
7573      FUNC: is the callback function
7574
7575      This function sets a callback to be called in order to retrieve the
7576      certificate to be used in the handshake.
7577
7578      The callback's function prototype is: int
7579      (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int
7580      nreqs, const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length,
7581      gnutls_retr2_st* st);
7582
7583      'req_ca_cert' is only used in X.509 certificates.  Contains a list
7584      with the CA names that the server considers trusted.  Normally we
7585      should send a certificate that is signed by one of these CAs.
7586      These names are DER encoded.  To get a more meaningful value use
7587      the function 'gnutls_x509_rdn_get()'.
7588
7589      'pk_algos' contains a list with server's acceptable signature
7590      algorithms.  The certificate returned should support the server's
7591      given algorithms.
7592
7593      'st' should contain the certificates and private keys.
7594
7595      If the callback function is provided then gnutls will call it, in
7596      the handshake, after the certificate request message has been
7597      received.
7598
7599      In server side pk_algos and req_ca_dn are NULL.
7600
7601      The callback function should set the certificate list to be sent,
7602      and return 0 on success.  If no certificate was selected then the
7603      number of certificates should be set to zero.  The value (-1)
7604      indicates error and the handshake will be terminated.
7605
7606 gnutls_certificate_set_rsa_export_params
7607 ----------------------------------------
7608
7609  -- Function: void gnutls_certificate_set_rsa_export_params
7610           (gnutls_certificate_credentials_t RES, gnutls_rsa_params_t
7611           RSA_PARAMS)
7612      RES: is a gnutls_certificate_credentials_t structure
7613
7614      RSA_PARAMS: is a structure that holds temporary RSA parameters.
7615
7616      This function will set the temporary RSA parameters for a
7617      certificate server to use.  These parameters will be used in
7618      RSA-EXPORT cipher suites.
7619
7620 gnutls_certificate_set_verify_flags
7621 -----------------------------------
7622
7623  -- Function: void gnutls_certificate_set_verify_flags
7624           (gnutls_certificate_credentials_t RES, unsigned int FLAGS)
7625      RES: is a gnutls_certificate_credentials_t structure
7626
7627      FLAGS: are the flags
7628
7629      This function will set the flags to be used at verification of the
7630      certificates.  Flags must be OR of the
7631      'gnutls_certificate_verify_flags' enumerations.
7632
7633 gnutls_certificate_set_verify_function
7634 --------------------------------------
7635
7636  -- Function: void gnutls_certificate_set_verify_function
7637           (gnutls_certificate_credentials_t CRED,
7638           gnutls_certificate_verify_function * FUNC)
7639      CRED: is a 'gnutls_certificate_credentials_t' structure.
7640
7641      FUNC: is the callback function
7642
7643      This function sets a callback to be called when peer's certificate
7644      has been received in order to verify it on receipt rather than
7645      doing after the handshake is completed.
7646
7647      The callback's function prototype is: int
7648      (*callback)(gnutls_session_t);
7649
7650      If the callback function is provided then gnutls will call it, in
7651      the handshake, just after the certificate message has been
7652      received.  To verify or obtain the certificate the
7653      'gnutls_certificate_verify_peers2()',
7654      'gnutls_certificate_type_get()', 'gnutls_certificate_get_peers()'
7655      functions can be used.
7656
7657      The callback function should return 0 for the handshake to continue
7658      or non-zero to terminate.
7659
7660      *Since:* 2.10.0
7661
7662 gnutls_certificate_set_verify_limits
7663 ------------------------------------
7664
7665  -- Function: void gnutls_certificate_set_verify_limits
7666           (gnutls_certificate_credentials_t RES, unsigned int MAX_BITS,
7667           unsigned int MAX_DEPTH)
7668      RES: is a gnutls_certificate_credentials structure
7669
7670      MAX_BITS: is the number of bits of an acceptable certificate
7671      (default 8200)
7672
7673      MAX_DEPTH: is maximum depth of the verification of a certificate
7674      chain (default 5)
7675
7676      This function will set some upper limits for the default
7677      verification function, 'gnutls_certificate_verify_peers2()', to
7678      avoid denial of service attacks.  You can set them to zero to
7679      disable limits.
7680
7681 gnutls_certificate_set_x509_crl_file
7682 ------------------------------------
7683
7684  -- Function: int gnutls_certificate_set_x509_crl_file
7685           (gnutls_certificate_credentials_t RES, const char * CRLFILE,
7686           gnutls_x509_crt_fmt_t TYPE)
7687      RES: is a 'gnutls_certificate_credentials_t' structure.
7688
7689      CRLFILE: is a file containing the list of verified CRLs (DER or PEM
7690      list)
7691
7692      TYPE: is PEM or DER
7693
7694      This function adds the trusted CRLs in order to verify client or
7695      server certificates.  In case of a client this is not required to
7696      be called if the certificates are not verified using
7697      'gnutls_certificate_verify_peers2()'.  This function may be called
7698      multiple times.
7699
7700      *Returns:* number of CRLs processed or a negative value on error.
7701
7702 gnutls_certificate_set_x509_crl_mem
7703 -----------------------------------
7704
7705  -- Function: int gnutls_certificate_set_x509_crl_mem
7706           (gnutls_certificate_credentials_t RES, const gnutls_datum_t *
7707           CRL, gnutls_x509_crt_fmt_t TYPE)
7708      RES: is a 'gnutls_certificate_credentials_t' structure.
7709
7710      CRL: is a list of trusted CRLs.  They should have been verified
7711      before.
7712
7713      TYPE: is DER or PEM
7714
7715      This function adds the trusted CRLs in order to verify client or
7716      server certificates.  In case of a client this is not required to
7717      be called if the certificates are not verified using
7718      'gnutls_certificate_verify_peers2()'.  This function may be called
7719      multiple times.
7720
7721      *Returns:* number of CRLs processed, or a negative value on error.
7722
7723 gnutls_certificate_set_x509_crl
7724 -------------------------------
7725
7726  -- Function: int gnutls_certificate_set_x509_crl
7727           (gnutls_certificate_credentials_t RES, gnutls_x509_crl_t *
7728           CRL_LIST, int CRL_LIST_SIZE)
7729      RES: is a 'gnutls_certificate_credentials_t' structure.
7730
7731      CRL_LIST: is a list of trusted CRLs.  They should have been
7732      verified before.
7733
7734      CRL_LIST_SIZE: holds the size of the crl_list
7735
7736      This function adds the trusted CRLs in order to verify client or
7737      server certificates.  In case of a client this is not required to
7738      be called if the certificates are not verified using
7739      'gnutls_certificate_verify_peers2()'.  This function may be called
7740      multiple times.
7741
7742      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7743
7744      *Since:* 2.4.0
7745
7746 gnutls_certificate_set_x509_key_file
7747 ------------------------------------
7748
7749  -- Function: int gnutls_certificate_set_x509_key_file
7750           (gnutls_certificate_credentials_t RES, const char * CERTFILE,
7751           const char * KEYFILE, gnutls_x509_crt_fmt_t TYPE)
7752      RES: is a 'gnutls_certificate_credentials_t' structure.
7753
7754      CERTFILE: is a file that containing the certificate list (path) for
7755      the specified private key, in PKCS7 format, or a list of
7756      certificates
7757
7758      KEYFILE: is a file that contains the private key
7759
7760      TYPE: is PEM or DER
7761
7762      This function sets a certificate/private key pair in the
7763      gnutls_certificate_credentials_t structure.  This function may be
7764      called more than once (in case multiple keys/certificates exist for
7765      the server).  For clients that wants to send more than its own end
7766      entity certificate (e.g., also an intermediate CA cert) then put
7767      the certificate chain in 'certfile'.
7768
7769      Currently only PKCS-1 encoded RSA and DSA private keys are accepted
7770      by this function.
7771
7772      This function can also accept PKCS '11' URLs.  In that case it will
7773      import the private key and certificate indicated by the urls.
7774
7775      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7776
7777 gnutls_certificate_set_x509_key_mem
7778 -----------------------------------
7779
7780  -- Function: int gnutls_certificate_set_x509_key_mem
7781           (gnutls_certificate_credentials_t RES, const gnutls_datum_t *
7782           CERT, const gnutls_datum_t * KEY, gnutls_x509_crt_fmt_t TYPE)
7783      RES: is a 'gnutls_certificate_credentials_t' structure.
7784
7785      CERT: contains a certificate list (path) for the specified private
7786      key
7787
7788      KEY: is the private key, or 'NULL'
7789
7790      TYPE: is PEM or DER
7791
7792      This function sets a certificate/private key pair in the
7793      gnutls_certificate_credentials_t structure.  This function may be
7794      called more than once (in case multiple keys/certificates exist for
7795      the server).
7796
7797      *Currently are supported:* RSA PKCS-1 encoded private keys, DSA
7798      private keys.
7799
7800      DSA private keys are encoded the OpenSSL way, which is an ASN.1 DER
7801      sequence of 6 INTEGERs - version, p, q, g, pub, priv.
7802
7803      Note that the keyUsage (2.5.29.15) PKIX extension in X.509
7804      certificates is supported.  This means that certificates intended
7805      for signing cannot be used for ciphersuites that require
7806      encryption.
7807
7808      If the certificate and the private key are given in PEM encoding
7809      then the strings that hold their values must be null terminated.
7810
7811      The 'key' may be 'NULL' if you are using a sign callback, see
7812      'gnutls_sign_callback_set()'.
7813
7814      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7815
7816 gnutls_certificate_set_x509_key
7817 -------------------------------
7818
7819  -- Function: int gnutls_certificate_set_x509_key
7820           (gnutls_certificate_credentials_t RES, gnutls_x509_crt_t *
7821           CERT_LIST, int CERT_LIST_SIZE, gnutls_x509_privkey_t KEY)
7822      RES: is a 'gnutls_certificate_credentials_t' structure.
7823
7824      CERT_LIST: contains a certificate list (path) for the specified
7825      private key
7826
7827      CERT_LIST_SIZE: holds the size of the certificate list
7828
7829      KEY: is a gnutls_x509_privkey_t key
7830
7831      This function sets a certificate/private key pair in the
7832      gnutls_certificate_credentials_t structure.  This function may be
7833      called more than once (in case multiple keys/certificates exist for
7834      the server).  For clients that wants to send more than its own end
7835      entity certificate (e.g., also an intermediate CA cert) then put
7836      the certificate chain in 'cert_list'.
7837
7838      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7839
7840      *Since:* 2.4.0
7841
7842 gnutls_certificate_set_x509_simple_pkcs12_file
7843 ----------------------------------------------
7844
7845  -- Function: int gnutls_certificate_set_x509_simple_pkcs12_file
7846           (gnutls_certificate_credentials_t RES, const char *
7847           PKCS12FILE, gnutls_x509_crt_fmt_t TYPE, const char * PASSWORD)
7848      RES: is a 'gnutls_certificate_credentials_t' structure.
7849
7850      PKCS12FILE: filename of file containing PKCS'12' blob.
7851
7852      TYPE: is PEM or DER of the 'pkcs12file'.
7853
7854      PASSWORD: optional password used to decrypt PKCS'12' file, bags and
7855      keys.
7856
7857      This function sets a certificate/private key pair and/or a CRL in
7858      the gnutls_certificate_credentials_t structure.  This function may
7859      be called more than once (in case multiple keys/certificates exist
7860      for the server).
7861
7862      *MAC:* ed PKCS'12' files are supported.  Encrypted PKCS'12' bags
7863      are supported.  Encrypted PKCS'8' private keys are supported.
7864      However, only password based security, and the same password for
7865      all operations, are supported.
7866
7867      The private keys may be RSA PKCS'1' or DSA private keys encoded in
7868      the OpenSSL way.
7869
7870      PKCS'12' file may contain many keys and/or certificates, and there
7871      is no way to identify which key/certificate pair you want.  You
7872      should make sure the PKCS'12' file only contain one key/certificate
7873      pair and/or one CRL.
7874
7875      It is believed that the limitations of this function is acceptable
7876      for most usage, and that any more flexibility would introduce
7877      complexity that would make it harder to use this functionality at
7878      all.
7879
7880      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7881
7882 gnutls_certificate_set_x509_simple_pkcs12_mem
7883 ---------------------------------------------
7884
7885  -- Function: int gnutls_certificate_set_x509_simple_pkcs12_mem
7886           (gnutls_certificate_credentials_t RES, const gnutls_datum_t *
7887           P12BLOB, gnutls_x509_crt_fmt_t TYPE, const char * PASSWORD)
7888      RES: is a 'gnutls_certificate_credentials_t' structure.
7889
7890      P12BLOB: the PKCS'12' blob.
7891
7892      TYPE: is PEM or DER of the 'pkcs12file'.
7893
7894      PASSWORD: optional password used to decrypt PKCS'12' file, bags and
7895      keys.
7896
7897      This function sets a certificate/private key pair and/or a CRL in
7898      the gnutls_certificate_credentials_t structure.  This function may
7899      be called more than once (in case multiple keys/certificates exist
7900      for the server).
7901
7902      *MAC:* ed PKCS'12' files are supported.  Encrypted PKCS'12' bags
7903      are supported.  Encrypted PKCS'8' private keys are supported.
7904      However, only password based security, and the same password for
7905      all operations, are supported.
7906
7907      The private keys may be RSA PKCS'1' or DSA private keys encoded in
7908      the OpenSSL way.
7909
7910      PKCS'12' file may contain many keys and/or certificates, and there
7911      is no way to identify which key/certificate pair you want.  You
7912      should make sure the PKCS'12' file only contain one key/certificate
7913      pair and/or one CRL.
7914
7915      It is believed that the limitations of this function is acceptable
7916      for most usage, and that any more flexibility would introduce
7917      complexity that would make it harder to use this functionality at
7918      all.
7919
7920      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
7921
7922      *Since:* 2.8.0
7923
7924 gnutls_certificate_set_x509_trust_file
7925 --------------------------------------
7926
7927  -- Function: int gnutls_certificate_set_x509_trust_file
7928           (gnutls_certificate_credentials_t RES, const char * CAFILE,
7929           gnutls_x509_crt_fmt_t TYPE)
7930      RES: is a 'gnutls_certificate_credentials_t' structure.
7931
7932      CAFILE: is a file containing the list of trusted CAs (DER or PEM
7933      list)
7934
7935      TYPE: is PEM or DER
7936
7937      This function adds the trusted CAs in order to verify client or
7938      server certificates.  In case of a client this is not required to
7939      be called if the certificates are not verified using
7940      'gnutls_certificate_verify_peers2()'.  This function may be called
7941      multiple times.
7942
7943      In case of a server the names of the CAs set here will be sent to
7944      the client if a certificate request is sent.  This can be disabled
7945      using 'gnutls_certificate_send_x509_rdn_sequence()'.
7946
7947      This function can also accept PKCS '11' URLs.  In that case it will
7948      import all certificates that are marked as trusted.
7949
7950      *Returns:* number of certificates processed, or a negative value on
7951      error.
7952
7953 gnutls_certificate_set_x509_trust_mem
7954 -------------------------------------
7955
7956  -- Function: int gnutls_certificate_set_x509_trust_mem
7957           (gnutls_certificate_credentials_t RES, const gnutls_datum_t *
7958           CA, gnutls_x509_crt_fmt_t TYPE)
7959      RES: is a 'gnutls_certificate_credentials_t' structure.
7960
7961      CA: is a list of trusted CAs or a DER certificate
7962
7963      TYPE: is DER or PEM
7964
7965      This function adds the trusted CAs in order to verify client or
7966      server certificates.  In case of a client this is not required to
7967      be called if the certificates are not verified using
7968      'gnutls_certificate_verify_peers2()'.  This function may be called
7969      multiple times.
7970
7971      In case of a server the CAs set here will be sent to the client if
7972      a certificate request is sent.  This can be disabled using
7973      'gnutls_certificate_send_x509_rdn_sequence()'.
7974
7975      *Returns:* the number of certificates processed or a negative value
7976      on error.
7977
7978 gnutls_certificate_set_x509_trust
7979 ---------------------------------
7980
7981  -- Function: int gnutls_certificate_set_x509_trust
7982           (gnutls_certificate_credentials_t RES, gnutls_x509_crt_t *
7983           CA_LIST, int CA_LIST_SIZE)
7984      RES: is a 'gnutls_certificate_credentials_t' structure.
7985
7986      CA_LIST: is a list of trusted CAs
7987
7988      CA_LIST_SIZE: holds the size of the CA list
7989
7990      This function adds the trusted CAs in order to verify client or
7991      server certificates.  In case of a client this is not required to
7992      be called if the certificates are not verified using
7993      'gnutls_certificate_verify_peers2()'.  This function may be called
7994      multiple times.
7995
7996      In case of a server the CAs set here will be sent to the client if
7997      a certificate request is sent.  This can be disabled using
7998      'gnutls_certificate_send_x509_rdn_sequence()'.
7999
8000      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
8001
8002      *Since:* 2.4.0
8003
8004 gnutls_certificate_type_get_id
8005 ------------------------------
8006
8007  -- Function: gnutls_certificate_type_t gnutls_certificate_type_get_id
8008           (const char * NAME)
8009      NAME: is a certificate type name
8010
8011      The names are compared in a case insensitive way.
8012
8013      *Returns:* a 'gnutls_certificate_type_t' for the specified in a
8014      string certificate type, or 'GNUTLS_CRT_UNKNOWN' on error.
8015
8016 gnutls_certificate_type_get_name
8017 --------------------------------
8018
8019  -- Function: const char * gnutls_certificate_type_get_name
8020           (gnutls_certificate_type_t TYPE)
8021      TYPE: is a certificate type
8022
8023      Convert a 'gnutls_certificate_type_t' type to a string.
8024
8025      *Returns:* a string that contains the name of the specified
8026      certificate type, or 'NULL' in case of unknown types.
8027
8028 gnutls_certificate_type_get
8029 ---------------------------
8030
8031  -- Function: gnutls_certificate_type_t gnutls_certificate_type_get
8032           (gnutls_session_t SESSION)
8033      SESSION: is a 'gnutls_session_t' structure.
8034
8035      The certificate type is by default X.509, unless it is negotiated
8036      as a TLS extension.
8037
8038      *Returns:* the currently used 'gnutls_certificate_type_t'
8039      certificate type.
8040
8041 gnutls_certificate_type_list
8042 ----------------------------
8043
8044  -- Function: const gnutls_certificate_type_t *
8045           gnutls_certificate_type_list ( VOID)
8046
8047      Get a list of certificate types.  Note that to be able to use
8048      OpenPGP certificates, you must link to libgnutls-extra and call
8049      'gnutls_global_init_extra()'.
8050
8051      *Returns:* a zero-terminated list of 'gnutls_certificate_type_t'
8052      integers indicating the available certificate types.
8053
8054 gnutls_certificate_type_set_priority
8055 ------------------------------------
8056
8057  -- Function: int gnutls_certificate_type_set_priority (gnutls_session_t
8058           SESSION, const int * LIST)
8059      SESSION: is a 'gnutls_session_t' structure.
8060
8061      LIST: is a 0 terminated list of gnutls_certificate_type_t elements.
8062
8063      Sets the priority on the certificate types supported by gnutls.
8064      Priority is higher for elements specified before others.  After
8065      specifying the types you want, you must append a 0.  Note that the
8066      certificate type priority is set on the client.  The server does
8067      not use the cert type priority except for disabling types that were
8068      not specified.
8069
8070      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
8071
8072 gnutls_certificate_verify_peers2
8073 --------------------------------
8074
8075  -- Function: int gnutls_certificate_verify_peers2 (gnutls_session_t
8076           SESSION, unsigned int * STATUS)
8077      SESSION: is a gnutls session
8078
8079      STATUS: is the output of the verification
8080
8081      This function will try to verify the peer's certificate and return
8082      its status (trusted, invalid etc.).  The value of 'status' should
8083      be one or more of the gnutls_certificate_status_t enumerated
8084      elements bitwise or'd.  To avoid denial of service attacks some
8085      default upper limits regarding the certificate key size and chain
8086      size are set.  To override them use
8087      'gnutls_certificate_set_verify_limits()'.
8088
8089      Note that you must also check the peer's name in order to check if
8090      the verified certificate belongs to the actual peer.
8091
8092      This function uses 'gnutls_x509_crt_list_verify()' with the CAs in
8093      the credentials as trusted CAs.
8094
8095      *Returns:* a negative error code on error and zero on success.
8096
8097 gnutls_certificate_verify_peers
8098 -------------------------------
8099
8100  -- Function: int gnutls_certificate_verify_peers (gnutls_session_t
8101           SESSION)
8102      SESSION: is a gnutls session
8103
8104      This function will try to verify the peer's certificate and return
8105      its status (trusted, invalid etc.).  However you must also check
8106      the peer's name in order to check if the verified certificate
8107      belongs to the actual peer.
8108
8109      This function uses 'gnutls_x509_crt_list_verify()'.
8110
8111      *Returns:* one or more of the 'gnutls_certificate_status_t'
8112      enumerated elements bitwise or'd, or a negative value on error.
8113
8114      *Deprecated:* Use 'gnutls_certificate_verify_peers2()' instead.
8115
8116 gnutls_check_version
8117 --------------------
8118
8119  -- Function: const char * gnutls_check_version (const char *
8120           REQ_VERSION)
8121      REQ_VERSION: version string to compare with, or 'NULL'.
8122
8123      Check GnuTLS Library version.
8124
8125      See 'GNUTLS_VERSION' for a suitable 'req_version' string.
8126
8127      *Return value:* Check that the version of the library is at minimum
8128      the one given as a string in 'req_version' and return the actual
8129      version string of the library; return 'NULL' if the condition is
8130      not met.  If 'NULL' is passed to this function no check is done and
8131      only the version string is returned.
8132
8133 gnutls_cipher_decrypt2
8134 ----------------------
8135
8136  -- Function: int gnutls_cipher_decrypt2 (gnutls_cipher_hd_t HANDLE,
8137           const void * CIPHERTEXT, size_t CIPHERTEXTLEN, void * TEXT,
8138           size_t TEXTLEN)
8139      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8140
8141      CIPHERTEXT: the data to encrypt
8142
8143      CIPHERTEXTLEN: The length of data to encrypt
8144
8145      TEXT: the decrypted data
8146
8147      TEXTLEN: The available length for decrypted data
8148
8149      This function will decrypt the given data using the algorithm
8150      specified by the context.
8151
8152      *Returns:* Zero or a negative value on error.
8153
8154      *Since:* 2.10.0
8155
8156 gnutls_cipher_decrypt
8157 ---------------------
8158
8159  -- Function: int gnutls_cipher_decrypt (gnutls_cipher_hd_t HANDLE, void
8160           * CIPHERTEXT, size_t CIPHERTEXTLEN)
8161      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8162
8163      CIPHERTEXT: the data to encrypt
8164
8165      CIPHERTEXTLEN: The length of data to encrypt
8166
8167      This function will decrypt the given data using the algorithm
8168      specified by the context.
8169
8170      *Returns:* Zero or a negative value on error.
8171
8172      *Since:* 2.10.0
8173
8174 gnutls_cipher_deinit
8175 --------------------
8176
8177  -- Function: void gnutls_cipher_deinit (gnutls_cipher_hd_t HANDLE)
8178      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8179
8180      This function will deinitialize all resources occupied by the given
8181      encryption context.
8182
8183      *Since:* 2.10.0
8184
8185 gnutls_cipher_encrypt2
8186 ----------------------
8187
8188  -- Function: int gnutls_cipher_encrypt2 (gnutls_cipher_hd_t HANDLE,
8189           void * TEXT, size_t TEXTLEN, void * CIPHERTEXT, size_t
8190           CIPHERTEXTLEN)
8191      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8192
8193      TEXT: the data to encrypt
8194
8195      TEXTLEN: The length of data to encrypt
8196
8197      CIPHERTEXT: the encrypted data
8198
8199      CIPHERTEXTLEN: The available length for encrypted data
8200
8201      This function will encrypt the given data using the algorithm
8202      specified by the context.
8203
8204      *Returns:* Zero or a negative value on error.
8205
8206      *Since:* 2.10.0
8207
8208 gnutls_cipher_encrypt
8209 ---------------------
8210
8211  -- Function: int gnutls_cipher_encrypt (gnutls_cipher_hd_t HANDLE, void
8212           * TEXT, size_t TEXTLEN)
8213      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8214
8215      TEXT: the data to encrypt
8216
8217      TEXTLEN: The length of data to encrypt
8218
8219      This function will encrypt the given data using the algorithm
8220      specified by the context.
8221
8222      *Returns:* Zero or a negative value on error.
8223
8224      *Since:* 2.10.0
8225
8226 gnutls_cipher_get_block_size
8227 ----------------------------
8228
8229  -- Function: int gnutls_cipher_get_block_size
8230           (gnutls_cipher_algorithm_t ALGORITHM)
8231      ALGORITHM: is an encryption algorithm
8232
8233      Get block size for encryption algorithm.
8234
8235      *Returns:* block size for encryption algorithm.
8236
8237      *Since:* 2.10.0
8238
8239 gnutls_cipher_get_id
8240 --------------------
8241
8242  -- Function: gnutls_cipher_algorithm_t gnutls_cipher_get_id (const char
8243           * NAME)
8244      NAME: is a MAC algorithm name
8245
8246      The names are compared in a case insensitive way.
8247
8248      *Returns:* return a 'gnutls_cipher_algorithm_t' value corresponding
8249      to the specified cipher, or 'GNUTLS_CIPHER_UNKNOWN' on error.
8250
8251 gnutls_cipher_get_key_size
8252 --------------------------
8253
8254  -- Function: size_t gnutls_cipher_get_key_size
8255           (gnutls_cipher_algorithm_t ALGORITHM)
8256      ALGORITHM: is an encryption algorithm
8257
8258      Get key size for cipher.
8259
8260      *Returns:* length (in bytes) of the given cipher's key size, or 0
8261      if the given cipher is invalid.
8262
8263 gnutls_cipher_get_name
8264 ----------------------
8265
8266  -- Function: const char * gnutls_cipher_get_name
8267           (gnutls_cipher_algorithm_t ALGORITHM)
8268      ALGORITHM: is an encryption algorithm
8269
8270      Convert a 'gnutls_cipher_algorithm_t' type to a string.
8271
8272      *Returns:* a pointer to a string that contains the name of the
8273      specified cipher, or 'NULL'.
8274
8275 gnutls_cipher_get
8276 -----------------
8277
8278  -- Function: gnutls_cipher_algorithm_t gnutls_cipher_get
8279           (gnutls_session_t SESSION)
8280      SESSION: is a 'gnutls_session_t' structure.
8281
8282      Get currently used cipher.
8283
8284      *Returns:* the currently used cipher, a 'gnutls_cipher_algorithm_t'
8285      type.
8286
8287 gnutls_cipher_init
8288 ------------------
8289
8290  -- Function: int gnutls_cipher_init (gnutls_cipher_hd_t * HANDLE,
8291           gnutls_cipher_algorithm_t CIPHER, const gnutls_datum_t * KEY,
8292           const gnutls_datum_t * IV)
8293      HANDLE: is a 'gnutls_cipher_hd_t' structure.
8294
8295      CIPHER: the encryption algorithm to use
8296
8297      KEY: The key to be used for encryption
8298
8299      IV: The IV to use (if not applicable set NULL)
8300
8301      This function will initialize an context that can be used for
8302      encryption/decryption of data.  This will effectively use the
8303      current crypto backend in use by gnutls or the cryptographic
8304      accelerator in use.
8305
8306      *Returns:* Zero or a negative value on error.
8307
8308      *Since:* 2.10.0
8309
8310 gnutls_cipher_list
8311 ------------------
8312
8313  -- Function: const gnutls_cipher_algorithm_t * gnutls_cipher_list (
8314           VOID)
8315
8316      Get a list of supported cipher algorithms.  Note that not
8317      necessarily all ciphers are supported as TLS cipher suites.  For
8318      example, DES is not supported as a cipher suite, but is supported
8319      for other purposes (e.g., PKCS'8' or similar).
8320
8321      *Returns:* a zero-terminated list of 'gnutls_cipher_algorithm_t'
8322      integers indicating the available ciphers.
8323
8324 gnutls_cipher_set_priority
8325 --------------------------
8326
8327  -- Function: int gnutls_cipher_set_priority (gnutls_session_t SESSION,
8328           const int * LIST)
8329      SESSION: is a 'gnutls_session_t' structure.
8330
8331      LIST: is a 0 terminated list of gnutls_cipher_algorithm_t elements.
8332
8333      Sets the priority on the ciphers supported by gnutls.  Priority is
8334      higher for elements specified before others.  After specifying the
8335      ciphers you want, you must append a 0.  Note that the priority is
8336      set on the client.  The server does not use the algorithm's
8337      priority except for disabling algorithms that were not specified.
8338
8339      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
8340
8341 gnutls_cipher_suite_get_name
8342 ----------------------------
8343
8344  -- Function: const char * gnutls_cipher_suite_get_name
8345           (gnutls_kx_algorithm_t KX_ALGORITHM, gnutls_cipher_algorithm_t
8346           CIPHER_ALGORITHM, gnutls_mac_algorithm_t MAC_ALGORITHM)
8347      KX_ALGORITHM: is a Key exchange algorithm
8348
8349      CIPHER_ALGORITHM: is a cipher algorithm
8350
8351      MAC_ALGORITHM: is a MAC algorithm
8352
8353      Note that the full cipher suite name must be prepended by TLS or
8354      SSL depending of the protocol in use.
8355
8356      *Returns:* a string that contains the name of a TLS cipher suite,
8357      specified by the given algorithms, or 'NULL'.
8358
8359 gnutls_cipher_suite_info
8360 ------------------------
8361
8362  -- Function: const char * gnutls_cipher_suite_info (size_t IDX, char *
8363           CS_ID, gnutls_kx_algorithm_t * KX, gnutls_cipher_algorithm_t *
8364           CIPHER, gnutls_mac_algorithm_t * MAC, gnutls_protocol_t *
8365           MIN_VERSION)
8366      IDX: index of cipher suite to get information about, starts on 0.
8367
8368      CS_ID: output buffer with room for 2 bytes, indicating cipher suite
8369      value
8370
8371      KX: output variable indicating key exchange algorithm, or 'NULL'.
8372
8373      CIPHER: output variable indicating cipher, or 'NULL'.
8374
8375      MAC: output variable indicating MAC algorithm, or 'NULL'.
8376
8377      Get information about supported cipher suites.  Use the function
8378      iteratively to get information about all supported cipher suites.
8379      Call with idx=0 to get information about first cipher suite, then
8380      idx=1 and so on until the function returns NULL.
8381
8382      *Returns:* the name of 'idx' cipher suite, and set the information
8383      about the cipher suite in the output variables.  If 'idx' is out of
8384      bounds, 'NULL' is returned.
8385
8386 gnutls_compression_get_id
8387 -------------------------
8388
8389  -- Function: gnutls_compression_method_t gnutls_compression_get_id
8390           (const char * NAME)
8391      NAME: is a compression method name
8392
8393      The names are compared in a case insensitive way.
8394
8395      *Returns:* an id of the specified in a string compression method,
8396      or 'GNUTLS_COMP_UNKNOWN' on error.
8397
8398 gnutls_compression_get_name
8399 ---------------------------
8400
8401  -- Function: const char * gnutls_compression_get_name
8402           (gnutls_compression_method_t ALGORITHM)
8403      ALGORITHM: is a Compression algorithm
8404
8405      Convert a 'gnutls_compression_method_t' value to a string.
8406
8407      *Returns:* a pointer to a string that contains the name of the
8408      specified compression algorithm, or 'NULL'.
8409
8410 gnutls_compression_get
8411 ----------------------
8412
8413  -- Function: gnutls_compression_method_t gnutls_compression_get
8414           (gnutls_session_t SESSION)
8415      SESSION: is a 'gnutls_session_t' structure.
8416
8417      Get currently used compression algorithm.
8418
8419      *Returns:* the currently used compression method, a
8420      'gnutls_compression_method_t' value.
8421
8422 gnutls_compression_list
8423 -----------------------
8424
8425  -- Function: const gnutls_compression_method_t *
8426           gnutls_compression_list ( VOID)
8427
8428      Get a list of compression methods.  Note that to be able to use LZO
8429      compression, you must link to libgnutls-extra and call
8430      'gnutls_global_init_extra()'.
8431
8432      *Returns:* a zero-terminated list of 'gnutls_compression_method_t'
8433      integers indicating the available compression methods.
8434
8435 gnutls_compression_set_priority
8436 -------------------------------
8437
8438  -- Function: int gnutls_compression_set_priority (gnutls_session_t
8439           SESSION, const int * LIST)
8440      SESSION: is a 'gnutls_session_t' structure.
8441
8442      LIST: is a 0 terminated list of gnutls_compression_method_t
8443      elements.
8444
8445      Sets the priority on the compression algorithms supported by
8446      gnutls.  Priority is higher for elements specified before others.
8447      After specifying the algorithms you want, you must append a 0.
8448      Note that the priority is set on the client.  The server does not
8449      use the algorithm's priority except for disabling algorithms that
8450      were not specified.
8451
8452      TLS 1.0 does not define any compression algorithms except NULL.
8453      Other compression algorithms are to be considered as gnutls
8454      extensions.
8455
8456      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
8457
8458 gnutls_credentials_clear
8459 ------------------------
8460
8461  -- Function: void gnutls_credentials_clear (gnutls_session_t SESSION)
8462      SESSION: is a 'gnutls_session_t' structure.
8463
8464      Clears all the credentials previously set in this session.
8465
8466 gnutls_credentials_set
8467 ----------------------
8468
8469  -- Function: int gnutls_credentials_set (gnutls_session_t SESSION,
8470           gnutls_credentials_type_t TYPE, void * CRED)
8471      SESSION: is a 'gnutls_session_t' structure.
8472
8473      TYPE: is the type of the credentials
8474
8475      CRED: is a pointer to a structure.
8476
8477      Sets the needed credentials for the specified type.  Eg username,
8478      password - or public and private keys etc.  The 'cred' parameter is
8479      a structure that depends on the specified type and on the current
8480      session (client or server).
8481
8482      In order to minimize memory usage, and share credentials between
8483      several threads gnutls keeps a pointer to cred, and not the whole
8484      cred structure.  Thus you will have to keep the structure allocated
8485      until you call 'gnutls_deinit()'.
8486
8487      For 'GNUTLS_CRD_ANON', 'cred' should be
8488      'gnutls_anon_client_credentials_t' in case of a client.  In case of
8489      a server it should be 'gnutls_anon_server_credentials_t'.
8490
8491      For 'GNUTLS_CRD_SRP', 'cred' should be
8492      'gnutls_srp_client_credentials_t' in case of a client, and
8493      'gnutls_srp_server_credentials_t', in case of a server.
8494
8495      For 'GNUTLS_CRD_CERTIFICATE', 'cred' should be
8496      'gnutls_certificate_credentials_t'.
8497
8498      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
8499      otherwise an error code is returned.
8500
8501 gnutls_crypto_bigint_register2
8502 ------------------------------
8503
8504  -- Function: int gnutls_crypto_bigint_register2 (int PRIORITY, int
8505           VERSION, const gnutls_crypto_bigint_st * S)
8506      PRIORITY: is the priority of the interface
8507
8508      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8509
8510      S: is a structure holding new interface's data
8511
8512      This function will register an interface for gnutls to operate on
8513      big integers.  Any interface registered will override the included
8514      interface.  The interface with the lowest priority will be used by
8515      gnutls.
8516
8517      Note that the bigint interface must interoperate with the public
8518      key interface.  Thus if this interface is updated the
8519      'gnutls_crypto_pk_register()' should also be used.
8520
8521      This function should be called before 'gnutls_global_init()'.
8522
8523      For simplicity you can use the convenience
8524      'gnutls_crypto_bigint_register()' macro.
8525
8526      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8527
8528      *Since:* 2.6.0
8529
8530 gnutls_crypto_cipher_register2
8531 ------------------------------
8532
8533  -- Function: int gnutls_crypto_cipher_register2 (int PRIORITY, int
8534           VERSION, const gnutls_crypto_cipher_st * S)
8535      PRIORITY: is the priority of the cipher interface
8536
8537      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8538
8539      S: is a structure holding new interface's data
8540
8541      This function will register a cipher interface to be used by
8542      gnutls.  Any interface registered will override the included engine
8543      and by convention kernel implemented interfaces should have
8544      priority of 90.  The interface with the lowest priority will be
8545      used by gnutls.
8546
8547      This function should be called before 'gnutls_global_init()'.
8548
8549      For simplicity you can use the convenience
8550      'gnutls_crypto_cipher_register()' macro.
8551
8552      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8553
8554      *Since:* 2.6.0
8555
8556 gnutls_crypto_digest_register2
8557 ------------------------------
8558
8559  -- Function: int gnutls_crypto_digest_register2 (int PRIORITY, int
8560           VERSION, const gnutls_crypto_digest_st * S)
8561      PRIORITY: is the priority of the digest interface
8562
8563      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8564
8565      S: is a structure holding new interface's data
8566
8567      This function will register a digest interface to be used by
8568      gnutls.  Any interface registered will override the included engine
8569      and by convention kernel implemented interfaces should have
8570      priority of 90.  The interface with the lowest priority will be
8571      used by gnutls.
8572
8573      This function should be called before 'gnutls_global_init()'.
8574
8575      For simplicity you can use the convenience
8576      'gnutls_crypto_digest_register()' macro.
8577
8578      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8579
8580      *Since:* 2.6.0
8581
8582 gnutls_crypto_mac_register2
8583 ---------------------------
8584
8585  -- Function: int gnutls_crypto_mac_register2 (int PRIORITY, int
8586           VERSION, const gnutls_crypto_mac_st * S)
8587      PRIORITY: is the priority of the mac interface
8588
8589      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8590
8591      S: is a structure holding new interface's data
8592
8593      This function will register a mac interface to be used by gnutls.
8594      Any interface registered will override the included engine and by
8595      convention kernel implemented interfaces should have priority of
8596      90.  The interface with the lowest priority will be used by gnutls.
8597
8598      This function should be called before 'gnutls_global_init()'.
8599
8600      For simplicity you can use the convenience
8601      'gnutls_crypto_digest_register()' macro.
8602
8603      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8604
8605      *Since:* 2.6.0
8606
8607 gnutls_crypto_pk_register2
8608 --------------------------
8609
8610  -- Function: int gnutls_crypto_pk_register2 (int PRIORITY, int VERSION,
8611           const gnutls_crypto_pk_st * S)
8612      PRIORITY: is the priority of the interface
8613
8614      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8615
8616      S: is a structure holding new interface's data
8617
8618      This function will register an interface for gnutls to operate on
8619      public key operations.  Any interface registered will override the
8620      included interface.  The interface with the lowest priority will be
8621      used by gnutls.
8622
8623      Note that the bigint interface must interoperate with the bigint
8624      interface.  Thus if this interface is updated the
8625      'gnutls_crypto_bigint_register()' should also be used.
8626
8627      This function should be called before 'gnutls_global_init()'.
8628
8629      For simplicity you can use the convenience
8630      'gnutls_crypto_pk_register()' macro.
8631
8632      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8633
8634      *Since:* 2.6.0
8635
8636 gnutls_crypto_rnd_register2
8637 ---------------------------
8638
8639  -- Function: int gnutls_crypto_rnd_register2 (int PRIORITY, int
8640           VERSION, const gnutls_crypto_rnd_st * S)
8641      PRIORITY: is the priority of the generator
8642
8643      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8644
8645      S: is a structure holding new generator's data
8646
8647      This function will register a random generator to be used by
8648      gnutls.  Any generator registered will override the included
8649      generator and by convention kernel implemented generators have
8650      priority of 90.  The generator with the lowest priority will be
8651      used by gnutls.
8652
8653      This function should be called before 'gnutls_global_init()'.
8654
8655      For simplicity you can use the convenience
8656      'gnutls_crypto_rnd_register()' macro.
8657
8658      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8659
8660      *Since:* 2.6.0
8661
8662 gnutls_crypto_single_cipher_register2
8663 -------------------------------------
8664
8665  -- Function: int gnutls_crypto_single_cipher_register2
8666           (gnutls_cipher_algorithm_t ALGORITHM, int PRIORITY, int
8667           VERSION, const gnutls_crypto_cipher_st * S)
8668      ALGORITHM: is the gnutls algorithm identifier
8669
8670      PRIORITY: is the priority of the algorithm
8671
8672      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8673
8674      S: is a structure holding new cipher's data
8675
8676      This function will register a cipher algorithm to be used by
8677      gnutls.  Any algorithm registered will override the included
8678      algorithms and by convention kernel implemented algorithms have
8679      priority of 90.  The algorithm with the lowest priority will be
8680      used by gnutls.
8681
8682      This function should be called before 'gnutls_global_init()'.
8683
8684      For simplicity you can use the convenience
8685      'gnutls_crypto_single_cipher_register()' macro.
8686
8687      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8688
8689      *Since:* 2.6.0
8690
8691 gnutls_crypto_single_digest_register2
8692 -------------------------------------
8693
8694  -- Function: int gnutls_crypto_single_digest_register2
8695           (gnutls_digest_algorithm_t ALGORITHM, int PRIORITY, int
8696           VERSION, const gnutls_crypto_digest_st * S)
8697      ALGORITHM: is the gnutls algorithm identifier
8698
8699      PRIORITY: is the priority of the algorithm
8700
8701      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8702
8703      S: is a structure holding new algorithms's data
8704
8705      This function will register a digest (hash) algorithm to be used by
8706      gnutls.  Any algorithm registered will override the included
8707      algorithms and by convention kernel implemented algorithms have
8708      priority of 90.  The algorithm with the lowest priority will be
8709      used by gnutls.
8710
8711      This function should be called before 'gnutls_global_init()'.
8712
8713      For simplicity you can use the convenience
8714      'gnutls_crypto_single_digest_register()' macro.
8715
8716      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8717
8718      *Since:* 2.6.0
8719
8720 gnutls_crypto_single_mac_register2
8721 ----------------------------------
8722
8723  -- Function: int gnutls_crypto_single_mac_register2
8724           (gnutls_mac_algorithm_t ALGORITHM, int PRIORITY, int VERSION,
8725           const gnutls_crypto_mac_st * S)
8726      ALGORITHM: is the gnutls algorithm identifier
8727
8728      PRIORITY: is the priority of the algorithm
8729
8730      VERSION: should be set to 'GNUTLS_CRYPTO_API_VERSION'
8731
8732      S: is a structure holding new algorithms's data
8733
8734      This function will register a MAC algorithm to be used by gnutls.
8735      Any algorithm registered will override the included algorithms and
8736      by convention kernel implemented algorithms have priority of 90.
8737      The algorithm with the lowest priority will be used by gnutls.
8738
8739      This function should be called before 'gnutls_global_init()'.
8740
8741      For simplicity you can use the convenience
8742      'gnutls_crypto_single_mac_register()' macro.
8743
8744      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
8745
8746      *Since:* 2.6.0
8747
8748 gnutls_db_check_entry
8749 ---------------------
8750
8751  -- Function: int gnutls_db_check_entry (gnutls_session_t SESSION,
8752           gnutls_datum_t SESSION_ENTRY)
8753      SESSION: is a 'gnutls_session_t' structure.
8754
8755      SESSION_ENTRY: is the session data (not key)
8756
8757      Check if database entry has expired.  This function is to be used
8758      when you want to clear unnesessary session which occupy space in
8759      your backend.
8760
8761      *Returns:* Returns 'GNUTLS_E_EXPIRED', if the database entry has
8762      expired or 0 otherwise.
8763
8764 gnutls_db_get_ptr
8765 -----------------
8766
8767  -- Function: void * gnutls_db_get_ptr (gnutls_session_t SESSION)
8768      SESSION: is a 'gnutls_session_t' structure.
8769
8770      Get db function pointer.
8771
8772      *Returns:* the pointer that will be sent to db store, retrieve and
8773      delete functions, as the first argument.
8774
8775 gnutls_db_remove_session
8776 ------------------------
8777
8778  -- Function: void gnutls_db_remove_session (gnutls_session_t SESSION)
8779      SESSION: is a 'gnutls_session_t' structure.
8780
8781      This function will remove the current session data from the session
8782      database.  This will prevent future handshakes reusing these
8783      session data.  This function should be called if a session was
8784      terminated abnormally, and before 'gnutls_deinit()' is called.
8785
8786      Normally 'gnutls_deinit()' will remove abnormally terminated
8787      sessions.
8788
8789 gnutls_db_set_cache_expiration
8790 ------------------------------
8791
8792  -- Function: void gnutls_db_set_cache_expiration (gnutls_session_t
8793           SESSION, int SECONDS)
8794      SESSION: is a 'gnutls_session_t' structure.
8795
8796      SECONDS: is the number of seconds.
8797
8798      Set the expiration time for resumed sessions.  The default is 3600
8799      (one hour) at the time writing this.
8800
8801 gnutls_db_set_ptr
8802 -----------------
8803
8804  -- Function: void gnutls_db_set_ptr (gnutls_session_t SESSION, void *
8805           PTR)
8806      SESSION: is a 'gnutls_session_t' structure.
8807
8808      PTR: is the pointer
8809
8810      Sets the pointer that will be provided to db store, retrieve and
8811      delete functions, as the first argument.
8812
8813 gnutls_db_set_remove_function
8814 -----------------------------
8815
8816  -- Function: void gnutls_db_set_remove_function (gnutls_session_t
8817           SESSION, gnutls_db_remove_func REM_FUNC)
8818      SESSION: is a 'gnutls_session_t' structure.
8819
8820      REM_FUNC: is the function.
8821
8822      Sets the function that will be used to remove data from the resumed
8823      sessions database.  This function must return 0 on success.
8824
8825      The first argument to 'rem_func' will be null unless
8826      'gnutls_db_set_ptr()' has been called.
8827
8828 gnutls_db_set_retrieve_function
8829 -------------------------------
8830
8831  -- Function: void gnutls_db_set_retrieve_function (gnutls_session_t
8832           SESSION, gnutls_db_retr_func RETR_FUNC)
8833      SESSION: is a 'gnutls_session_t' structure.
8834
8835      RETR_FUNC: is the function.
8836
8837      Sets the function that will be used to retrieve data from the
8838      resumed sessions database.  This function must return a
8839      gnutls_datum_t containing the data on success, or a gnutls_datum_t
8840      containing null and 0 on failure.
8841
8842      The datum's data must be allocated using the function
8843      'gnutls_malloc()'.
8844
8845      The first argument to 'retr_func' will be null unless
8846      'gnutls_db_set_ptr()' has been called.
8847
8848 gnutls_db_set_store_function
8849 ----------------------------
8850
8851  -- Function: void gnutls_db_set_store_function (gnutls_session_t
8852           SESSION, gnutls_db_store_func STORE_FUNC)
8853      SESSION: is a 'gnutls_session_t' structure.
8854
8855      STORE_FUNC: is the function
8856
8857      Sets the function that will be used to store data from the resumed
8858      sessions database.  This function must remove 0 on success.
8859
8860      The first argument to 'store_func()' will be null unless
8861      'gnutls_db_set_ptr()' has been called.
8862
8863 gnutls_deinit
8864 -------------
8865
8866  -- Function: void gnutls_deinit (gnutls_session_t SESSION)
8867      SESSION: is a 'gnutls_session_t' structure.
8868
8869      This function clears all buffers associated with the 'session'.
8870      This function will also remove session data from the session
8871      database if the session was terminated abnormally.
8872
8873 gnutls_dh_get_group
8874 -------------------
8875
8876  -- Function: int gnutls_dh_get_group (gnutls_session_t SESSION,
8877           gnutls_datum_t * RAW_GEN, gnutls_datum_t * RAW_PRIME)
8878      SESSION: is a gnutls session
8879
8880      RAW_GEN: will hold the generator.
8881
8882      RAW_PRIME: will hold the prime.
8883
8884      This function will return the group parameters used in the last
8885      Diffie-Hellman key exchange with the peer.  These are the prime and
8886      the generator used.  This function should be used for both
8887      anonymous and ephemeral Diffie-Hellman.  The output parameters must
8888      be freed with 'gnutls_free()'.
8889
8890      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
8891      otherwise an error code is returned.
8892
8893 gnutls_dh_get_peers_public_bits
8894 -------------------------------
8895
8896  -- Function: int gnutls_dh_get_peers_public_bits (gnutls_session_t
8897           SESSION)
8898      SESSION: is a gnutls session
8899
8900      Get the Diffie-Hellman public key bit size.  Can be used for both
8901      anonymous and ephemeral Diffie-Hellman.
8902
8903      *Returns:* the public key bit size used in the last Diffie-Hellman
8904      key exchange with the peer, or a negative value in case of error.
8905
8906 gnutls_dh_get_prime_bits
8907 ------------------------
8908
8909  -- Function: int gnutls_dh_get_prime_bits (gnutls_session_t SESSION)
8910      SESSION: is a gnutls session
8911
8912      This function will return the bits of the prime used in the last
8913      Diffie-Hellman key exchange with the peer.  Should be used for both
8914      anonymous and ephemeral Diffie-Hellman.  Note that some ciphers,
8915      like RSA and DSA without DHE, does not use a Diffie-Hellman key
8916      exchange, and then this function will return 0.
8917
8918      *Returns:* The Diffie-Hellman bit strength is returned, or 0 if no
8919      Diffie-Hellman key exchange was done, or a negative error code on
8920      failure.
8921
8922 gnutls_dh_get_pubkey
8923 --------------------
8924
8925  -- Function: int gnutls_dh_get_pubkey (gnutls_session_t SESSION,
8926           gnutls_datum_t * RAW_KEY)
8927      SESSION: is a gnutls session
8928
8929      RAW_KEY: will hold the public key.
8930
8931      This function will return the peer's public key used in the last
8932      Diffie-Hellman key exchange.  This function should be used for both
8933      anonymous and ephemeral Diffie-Hellman.  The output parameters must
8934      be freed with 'gnutls_free()'.
8935
8936      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
8937      otherwise an error code is returned.
8938
8939 gnutls_dh_get_secret_bits
8940 -------------------------
8941
8942  -- Function: int gnutls_dh_get_secret_bits (gnutls_session_t SESSION)
8943      SESSION: is a gnutls session
8944
8945      This function will return the bits used in the last Diffie-Hellman
8946      key exchange with the peer.  Should be used for both anonymous and
8947      ephemeral Diffie-Hellman.
8948
8949      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
8950      otherwise an error code is returned.
8951
8952 gnutls_dh_params_cpy
8953 --------------------
8954
8955  -- Function: int gnutls_dh_params_cpy (gnutls_dh_params_t DST,
8956           gnutls_dh_params_t SRC)
8957      DST: Is the destination structure, which should be initialized.
8958
8959      SRC: Is the source structure
8960
8961      This function will copy the DH parameters structure from source to
8962      destination.
8963
8964      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
8965      otherwise an error code is returned.
8966
8967 gnutls_dh_params_deinit
8968 -----------------------
8969
8970  -- Function: void gnutls_dh_params_deinit (gnutls_dh_params_t
8971           DH_PARAMS)
8972      DH_PARAMS: Is a structure that holds the prime numbers
8973
8974      This function will deinitialize the DH parameters structure.
8975
8976 gnutls_dh_params_export_pkcs3
8977 -----------------------------
8978
8979  -- Function: int gnutls_dh_params_export_pkcs3 (gnutls_dh_params_t
8980           PARAMS, gnutls_x509_crt_fmt_t FORMAT, unsigned char *
8981           PARAMS_DATA, size_t * PARAMS_DATA_SIZE)
8982      PARAMS: Holds the DH parameters
8983
8984      FORMAT: the format of output params.  One of PEM or DER.
8985
8986      PARAMS_DATA: will contain a PKCS3 DHParams structure PEM or DER
8987      encoded
8988
8989      PARAMS_DATA_SIZE: holds the size of params_data (and will be
8990      replaced by the actual size of parameters)
8991
8992      This function will export the given dh parameters to a PKCS3
8993      DHParams structure.  This is the format generated by "openssl
8994      dhparam" tool.  If the buffer provided is not long enough to hold
8995      the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
8996
8997      If the structure is PEM encoded, it will have a header of "BEGIN DH
8998      PARAMETERS".
8999
9000      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9001      otherwise an error code is returned.
9002
9003 gnutls_dh_params_export_raw
9004 ---------------------------
9005
9006  -- Function: int gnutls_dh_params_export_raw (gnutls_dh_params_t
9007           PARAMS, gnutls_datum_t * PRIME, gnutls_datum_t * GENERATOR,
9008           unsigned int * BITS)
9009      PARAMS: Holds the DH parameters
9010
9011      PRIME: will hold the new prime
9012
9013      GENERATOR: will hold the new generator
9014
9015      BITS: if non null will hold is the prime's number of bits
9016
9017      This function will export the pair of prime and generator for use
9018      in the Diffie-Hellman key exchange.  The new parameters will be
9019      allocated using 'gnutls_malloc()' and will be stored in the
9020      appropriate datum.
9021
9022      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9023      otherwise an error code is returned.
9024
9025 gnutls_dh_params_generate2
9026 --------------------------
9027
9028  -- Function: int gnutls_dh_params_generate2 (gnutls_dh_params_t PARAMS,
9029           unsigned int BITS)
9030      PARAMS: Is the structure that the DH parameters will be stored
9031
9032      BITS: is the prime's number of bits
9033
9034      This function will generate a new pair of prime and generator for
9035      use in the Diffie-Hellman key exchange.  The new parameters will be
9036      allocated using 'gnutls_malloc()' and will be stored in the
9037      appropriate datum.  This function is normally slow.
9038
9039      Do not set the number of bits directly, use
9040      'gnutls_sec_param_to_pk_bits()' to get bits for 'GNUTLS_PK_DSA'.
9041      Also note that the DH parameters are only useful to servers.  Since
9042      clients use the parameters sent by the server, it's of no use to
9043      call this in client side.
9044
9045      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9046      otherwise an error code is returned.
9047
9048 gnutls_dh_params_import_pkcs3
9049 -----------------------------
9050
9051  -- Function: int gnutls_dh_params_import_pkcs3 (gnutls_dh_params_t
9052           PARAMS, const gnutls_datum_t * PKCS3_PARAMS,
9053           gnutls_x509_crt_fmt_t FORMAT)
9054      PARAMS: A structure where the parameters will be copied to
9055
9056      PKCS3_PARAMS: should contain a PKCS3 DHParams structure PEM or DER
9057      encoded
9058
9059      FORMAT: the format of params.  PEM or DER.
9060
9061      This function will extract the DHParams found in a PKCS3 formatted
9062      structure.  This is the format generated by "openssl dhparam" tool.
9063
9064      If the structure is PEM encoded, it should have a header of "BEGIN
9065      DH PARAMETERS".
9066
9067      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9068      otherwise an error code is returned.
9069
9070 gnutls_dh_params_import_raw
9071 ---------------------------
9072
9073  -- Function: int gnutls_dh_params_import_raw (gnutls_dh_params_t
9074           DH_PARAMS, const gnutls_datum_t * PRIME, const gnutls_datum_t
9075           * GENERATOR)
9076      DH_PARAMS: Is a structure that will hold the prime numbers
9077
9078      PRIME: holds the new prime
9079
9080      GENERATOR: holds the new generator
9081
9082      This function will replace the pair of prime and generator for use
9083      in the Diffie-Hellman key exchange.  The new parameters should be
9084      stored in the appropriate gnutls_datum.
9085
9086      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9087      otherwise an error code is returned.
9088
9089 gnutls_dh_params_init
9090 ---------------------
9091
9092  -- Function: int gnutls_dh_params_init (gnutls_dh_params_t * DH_PARAMS)
9093      DH_PARAMS: Is a structure that will hold the prime numbers
9094
9095      This function will initialize the DH parameters structure.
9096
9097      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9098      otherwise an error code is returned.
9099
9100 gnutls_dh_set_prime_bits
9101 ------------------------
9102
9103  -- Function: void gnutls_dh_set_prime_bits (gnutls_session_t SESSION,
9104           unsigned int BITS)
9105      SESSION: is a 'gnutls_session_t' structure.
9106
9107      BITS: is the number of bits
9108
9109      This function sets the number of bits, for use in an Diffie-Hellman
9110      key exchange.  This is used both in DH ephemeral and DH anonymous
9111      cipher suites.  This will set the minimum size of the prime that
9112      will be used for the handshake.
9113
9114      In the client side it sets the minimum accepted number of bits.  If
9115      a server sends a prime with less bits than that
9116      'GNUTLS_E_DH_PRIME_UNACCEPTABLE' will be returned by the handshake.
9117
9118      This function has no effect in server side.
9119
9120 gnutls_error_is_fatal
9121 ---------------------
9122
9123  -- Function: int gnutls_error_is_fatal (int ERROR)
9124      ERROR: is a GnuTLS error code, a negative value
9125
9126      If a GnuTLS function returns a negative value you may feed that
9127      value to this function to see if the error condition is fatal.
9128
9129      Note that you may want to check the error code manually, since some
9130      non-fatal errors to the protocol may be fatal for you program.
9131
9132      This function is only useful if you are dealing with errors from
9133      the record layer or the handshake layer.
9134
9135      *Returns:* 1 if the error code is fatal, for positive 'error'
9136      values, 0 is returned.  For unknown 'error' values, -1 is returned.
9137
9138 gnutls_error_to_alert
9139 ---------------------
9140
9141  -- Function: int gnutls_error_to_alert (int ERR, int * LEVEL)
9142      ERR: is a negative integer
9143
9144      LEVEL: the alert level will be stored there
9145
9146      Get an alert depending on the error code returned by a gnutls
9147      function.  All alerts sent by this function should be considered
9148      fatal.  The only exception is when 'err' is 'GNUTLS_E_REHANDSHAKE',
9149      where a warning alert should be sent to the peer indicating that no
9150      renegotiation will be performed.
9151
9152      If there is no mapping to a valid alert the alert to indicate
9153      internal error is returned.
9154
9155      *Returns:* the alert code to use for a particular error code.
9156
9157 gnutls_ext_register
9158 -------------------
9159
9160  -- Function: int gnutls_ext_register (int TYPE, const char * NAME,
9161           gnutls_ext_parse_type_t PARSE_TYPE, gnutls_ext_recv_func
9162           RECV_FUNC, gnutls_ext_send_func SEND_FUNC)
9163      TYPE: the 16-bit integer referring to the extension type
9164
9165      NAME: human printable name of the extension used for debugging
9166
9167      PARSE_TYPE: either 'GNUTLS_EXT_TLS' or 'GNUTLS_EXT_APPLICATION'.
9168
9169      RECV_FUNC: a function to receive extension data
9170
9171      SEND_FUNC: a function to send extension data
9172
9173      This function is used to register a new TLS extension handler.
9174
9175      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
9176
9177      *Deprecated in:* 2.12.0
9178
9179 gnutls_fingerprint
9180 ------------------
9181
9182  -- Function: int gnutls_fingerprint (gnutls_digest_algorithm_t ALGO,
9183           const gnutls_datum_t * DATA, void * RESULT, size_t *
9184           RESULT_SIZE)
9185      ALGO: is a digest algorithm
9186
9187      DATA: is the data
9188
9189      RESULT: is the place where the result will be copied (may be null).
9190
9191      RESULT_SIZE: should hold the size of the result.  The actual size
9192      of the returned result will also be copied there.
9193
9194      This function will calculate a fingerprint (actually a hash), of
9195      the given data.  The result is not printable data.  You should
9196      convert it to hex, or to something else printable.
9197
9198      This is the usual way to calculate a fingerprint of an X.509 DER
9199      encoded certificate.  Note however that the fingerprint of an
9200      OpenPGP is not just a hash and cannot be calculated with this
9201      function.
9202
9203      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
9204      otherwise an error code is returned.
9205
9206 gnutls_free
9207 -----------
9208
9209  -- Function: void gnutls_free (void * PTR)
9210      This function will free data pointed by ptr.
9211
9212      The deallocation function used is the one set by
9213      'gnutls_global_set_mem_functions()'.
9214
9215 gnutls_global_deinit
9216 --------------------
9217
9218  -- Function: void gnutls_global_deinit ( VOID)
9219
9220      This function deinitializes the global data, that were initialized
9221      using 'gnutls_global_init()'.
9222
9223      Note!  This function is not thread safe.  See the discussion for
9224      'gnutls_global_init()' for more information.
9225
9226 gnutls_global_init
9227 ------------------
9228
9229  -- Function: int gnutls_global_init ( VOID)
9230
9231      This function initializes the global data to defaults.  Every
9232      gnutls application has a global data which holds common parameters
9233      shared by gnutls session structures.  You should call
9234      'gnutls_global_deinit()' when gnutls usage is no longer needed
9235
9236      Note that this function will also initialize the underlying crypto
9237      backend, if it has not been initialized before.
9238
9239      This function increment a global counter, so that
9240      'gnutls_global_deinit()' only releases resources when it has been
9241      called as many times as 'gnutls_global_init()'.  This is useful
9242      when GnuTLS is used by more than one library in an application.
9243      This function can be called many times, but will only do something
9244      the first time.
9245
9246      Note!  This function is not thread safe.  If two threads call this
9247      function simultaneously, they can cause a race between checking the
9248      global counter and incrementing it, causing both threads to execute
9249      the library initialization code.  That would lead to a memory leak.
9250      To handle this, your application could invoke this function after
9251      aquiring a thread mutex.  To ignore the potential memory leak is
9252      also an option.
9253
9254      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
9255      otherwise an error code is returned.
9256
9257 gnutls_global_set_log_function
9258 ------------------------------
9259
9260  -- Function: void gnutls_global_set_log_function (gnutls_log_func
9261           LOG_FUNC)
9262      LOG_FUNC: it's a log function
9263
9264      This is the function where you set the logging function gnutls is
9265      going to use.  This function only accepts a character array.
9266      Normally you may not use this function since it is only used for
9267      debugging purposes.
9268
9269      gnutls_log_func is of the form, void (*gnutls_log_func)( int level,
9270      const char*);
9271
9272 gnutls_global_set_log_level
9273 ---------------------------
9274
9275  -- Function: void gnutls_global_set_log_level (int LEVEL)
9276      LEVEL: it's an integer from 0 to 9.
9277
9278      This is the function that allows you to set the log level.  The
9279      level is an integer between 0 and 9.  Higher values mean more
9280      verbosity.  The default value is 0.  Larger values should only be
9281      used with care, since they may reveal sensitive information.
9282
9283      Use a log level over 10 to enable all debugging options.
9284
9285 gnutls_global_set_mem_functions
9286 -------------------------------
9287
9288  -- Function: void gnutls_global_set_mem_functions
9289           (gnutls_alloc_function ALLOC_FUNC, gnutls_alloc_function
9290           SECURE_ALLOC_FUNC, gnutls_is_secure_function IS_SECURE_FUNC,
9291           gnutls_realloc_function REALLOC_FUNC, gnutls_free_function
9292           FREE_FUNC)
9293      ALLOC_FUNC: it's the default memory allocation function.  Like
9294      'malloc()'.
9295
9296      SECURE_ALLOC_FUNC: This is the memory allocation function that will
9297      be used for sensitive data.
9298
9299      IS_SECURE_FUNC: a function that returns 0 if the memory given is
9300      not secure.  May be NULL.
9301
9302      REALLOC_FUNC: A realloc function
9303
9304      FREE_FUNC: The function that frees allocated data.  Must accept a
9305      NULL pointer.
9306
9307      This is the function were you set the memory allocation functions
9308      gnutls is going to use.  By default the libc's allocation functions
9309      ('malloc()', 'free()'), are used by gnutls, to allocate both
9310      sensitive and not sensitive data.  This function is provided to set
9311      the memory allocation functions to something other than the
9312      defaults
9313
9314      This function must be called before 'gnutls_global_init()' is
9315      called.  This function is not thread safe.
9316
9317 gnutls_global_set_mutex
9318 -----------------------
9319
9320  -- Function: void gnutls_global_set_mutex (mutex_init_func INIT,
9321           mutex_deinit_func DEINIT, mutex_lock_func LOCK,
9322           mutex_unlock_func UNLOCK)
9323      INIT: mutex initialization function
9324
9325      DEINIT: mutex deinitialization function
9326
9327      LOCK: mutex locking function
9328
9329      UNLOCK: mutex unlocking function
9330
9331      With this function you are allowed to override the default mutex
9332      locks used in some parts of gnutls and dependent libraries.  This
9333      function should be used if you have complete control of your
9334      program and libraries.  Do not call this function from a library.
9335      Instead only initialize gnutls and the default OS mutex locks will
9336      be used.
9337
9338      This function must be called before 'gnutls_global_init()'.
9339
9340 gnutls_global_set_time_function
9341 -------------------------------
9342
9343  -- Function: void gnutls_global_set_time_function (gnutls_time_func
9344           TIME_FUNC)
9345      TIME_FUNC: it's the system time function
9346
9347      This is the function where you can override the default system time
9348      function.
9349
9350      gnutls_time_func is of the form, time_t (*gnutls_time_func)(
9351      time*);
9352
9353 gnutls_handshake_get_last_in
9354 ----------------------------
9355
9356  -- Function: gnutls_handshake_description_t
9357           gnutls_handshake_get_last_in (gnutls_session_t SESSION)
9358      SESSION: is a 'gnutls_session_t' structure.
9359
9360      This function is only useful to check where the last performed
9361      handshake failed.  If the previous handshake succeed or was not
9362      performed at all then no meaningful value will be returned.
9363
9364      Check 'gnutls_handshake_description_t' in gnutls.h for the
9365      available handshake descriptions.
9366
9367      *Returns:* the last handshake message type received, a
9368      'gnutls_handshake_description_t'.
9369
9370 gnutls_handshake_get_last_out
9371 -----------------------------
9372
9373  -- Function: gnutls_handshake_description_t
9374           gnutls_handshake_get_last_out (gnutls_session_t SESSION)
9375      SESSION: is a 'gnutls_session_t' structure.
9376
9377      This function is only useful to check where the last performed
9378      handshake failed.  If the previous handshake succeed or was not
9379      performed at all then no meaningful value will be returned.
9380
9381      Check 'gnutls_handshake_description_t' in gnutls.h for the
9382      available handshake descriptions.
9383
9384      *Returns:* the last handshake message type sent, a
9385      'gnutls_handshake_description_t'.
9386
9387 gnutls_handshake_set_max_packet_length
9388 --------------------------------------
9389
9390  -- Function: void gnutls_handshake_set_max_packet_length
9391           (gnutls_session_t SESSION, size_t MAX)
9392      SESSION: is a 'gnutls_session_t' structure.
9393
9394      MAX: is the maximum number.
9395
9396      This function will set the maximum size of all handshake messages.
9397      Handshakes over this size are rejected with
9398      'GNUTLS_E_HANDSHAKE_TOO_LARGE' error code.  The default value is
9399      48kb which is typically large enough.  Set this to 0 if you do not
9400      want to set an upper limit.
9401
9402      The reason for restricting the handshake message sizes are to limit
9403      Denial of Service attacks.
9404
9405 gnutls_handshake_set_post_client_hello_function
9406 -----------------------------------------------
9407
9408  -- Function: void gnutls_handshake_set_post_client_hello_function
9409           (gnutls_session_t SESSION,
9410           gnutls_handshake_post_client_hello_func FUNC)
9411      SESSION: is a 'gnutls_session_t' structure.
9412
9413      FUNC: is the function to be called
9414
9415      This function will set a callback to be called after the client
9416      hello has been received (callback valid in server side only).  This
9417      allows the server to adjust settings based on received extensions.
9418
9419      Those settings could be ciphersuites, requesting certificate, or
9420      anything else except for version negotiation (this is done before
9421      the hello message is parsed).
9422
9423      This callback must return 0 on success or a gnutls error code to
9424      terminate the handshake.
9425
9426      *Warning:* You should not use this function to terminate the
9427      handshake based on client input unless you know what you are doing.
9428      Before the handshake is finished there is no way to know if there
9429      is a man-in-the-middle attack being performed.
9430
9431 gnutls_handshake_set_private_extensions
9432 ---------------------------------------
9433
9434  -- Function: void gnutls_handshake_set_private_extensions
9435           (gnutls_session_t SESSION, int ALLOW)
9436      SESSION: is a 'gnutls_session_t' structure.
9437
9438      ALLOW: is an integer (0 or 1)
9439
9440      This function will enable or disable the use of private cipher
9441      suites (the ones that start with 0xFF). By default or if 'allow' is
9442      0 then these cipher suites will not be advertized nor used.
9443
9444      Unless this function is called with the option to allow (1), then
9445      no compression algorithms, like LZO. That is because these
9446      algorithms are not yet defined in any RFC or even internet draft.
9447
9448      Enabling the private ciphersuites when talking to other than gnutls
9449      servers and clients may cause interoperability problems.
9450
9451 gnutls_handshake
9452 ----------------
9453
9454  -- Function: int gnutls_handshake (gnutls_session_t SESSION)
9455      SESSION: is a 'gnutls_session_t' structure.
9456
9457      This function does the handshake of the TLS/SSL protocol, and
9458      initializes the TLS connection.
9459
9460      This function will fail if any problem is encountered, and will
9461      return a negative error code.  In case of a client, if the client
9462      has asked to resume a session, but the server couldn't, then a full
9463      handshake will be performed.
9464
9465      The non-fatal errors such as 'GNUTLS_E_AGAIN' and
9466      'GNUTLS_E_INTERRUPTED' interrupt the handshake procedure, which
9467      should be later be resumed.  Call this function again, until it
9468      returns 0; cf.  'gnutls_record_get_direction()' and
9469      'gnutls_error_is_fatal()'.
9470
9471      If this function is called by a server after a rehandshake request
9472      then 'GNUTLS_E_GOT_APPLICATION_DATA' or
9473      'GNUTLS_E_WARNING_ALERT_RECEIVED' may be returned.  Note that these
9474      are non fatal errors, only in the specific case of a rehandshake.
9475      Their meaning is that the client rejected the rehandshake request
9476      or in the case of 'GNUTLS_E_GOT_APPLICATION_DATA' it might also
9477      mean that some data were pending.
9478
9479      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
9480
9481 gnutls_hash_deinit
9482 ------------------
9483
9484  -- Function: void gnutls_hash_deinit (gnutls_hash_hd_t HANDLE, void *
9485           DIGEST)
9486      HANDLE: is a 'gnutls_hash_hd_t' structure.
9487
9488      DIGEST: is the output value of the hash
9489
9490      This function will deinitialize all resources occupied by the given
9491      hash context.
9492
9493      *Since:* 2.10.0
9494
9495 gnutls_hash_fast
9496 ----------------
9497
9498  -- Function: int gnutls_hash_fast (gnutls_digest_algorithm_t ALGORITHM,
9499           const void * TEXT, size_t TEXTLEN, void * DIGEST)
9500      ALGORITHM: the hash algorithm to use
9501
9502      TEXT: the data to hash
9503
9504      TEXTLEN: The length of data to hash
9505
9506      DIGEST: is the output value of the hash
9507
9508      This convenience function will hash the given data and return
9509      output on a single call.
9510
9511      *Returns:* Zero or a negative value on error.
9512
9513      *Since:* 2.10.0
9514
9515 gnutls_hash_get_len
9516 -------------------
9517
9518  -- Function: int gnutls_hash_get_len (gnutls_digest_algorithm_t
9519           ALGORITHM)
9520      ALGORITHM: the hash algorithm to use
9521
9522      This function will return the length of the output data of the
9523      given hash algorithm.
9524
9525      *Returns:* The length or zero on error.
9526
9527      *Since:* 2.10.0
9528
9529 gnutls_hash_init
9530 ----------------
9531
9532  -- Function: int gnutls_hash_init (gnutls_hash_hd_t * DIG,
9533           gnutls_digest_algorithm_t ALGORITHM)
9534      DIG: is a 'gnutls_hash_hd_t' structure.
9535
9536      ALGORITHM: the hash algorithm to use
9537
9538      This function will initialize an context that can be used to
9539      produce a Message Digest of data.  This will effectively use the
9540      current crypto backend in use by gnutls or the cryptographic
9541      accelerator in use.
9542
9543      *Returns:* Zero or a negative value on error.
9544
9545      *Since:* 2.10.0
9546
9547 gnutls_hash_output
9548 ------------------
9549
9550  -- Function: void gnutls_hash_output (gnutls_hash_hd_t HANDLE, void *
9551           DIGEST)
9552      HANDLE: is a 'gnutls_hash_hd_t' structure.
9553
9554      DIGEST: is the output value of the hash
9555
9556      This function will output the current hash value.
9557
9558      *Since:* 2.10.0
9559
9560 gnutls_hash
9561 -----------
9562
9563  -- Function: int gnutls_hash (gnutls_hash_hd_t HANDLE, const void *
9564           TEXT, size_t TEXTLEN)
9565      HANDLE: is a 'gnutls_cipher_hd_t' structure.
9566
9567      TEXT: the data to hash
9568
9569      TEXTLEN: The length of data to hash
9570
9571      This function will hash the given data using the algorithm
9572      specified by the context.
9573
9574      *Returns:* Zero or a negative value on error.
9575
9576      *Since:* 2.10.0
9577
9578 gnutls_hex2bin
9579 --------------
9580
9581  -- Function: int gnutls_hex2bin (const char * HEX_DATA, size_t
9582           HEX_SIZE, char * BIN_DATA, size_t * BIN_SIZE)
9583      HEX_DATA: string with data in hex format
9584
9585      HEX_SIZE: size of hex data
9586
9587      BIN_DATA: output array with binary data
9588
9589      BIN_SIZE: when calling *'bin_size' should hold size of 'bin_data',
9590      on return will hold actual size of 'bin_data'.
9591
9592      Convert a buffer with hex data to binary data.
9593
9594      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
9595
9596      *Since:* 2.4.0
9597
9598 gnutls_hex_decode
9599 -----------------
9600
9601  -- Function: int gnutls_hex_decode (const gnutls_datum_t * HEX_DATA,
9602           char * RESULT, size_t * RESULT_SIZE)
9603      HEX_DATA: contain the encoded data
9604
9605      RESULT: the place where decoded data will be copied
9606
9607      RESULT_SIZE: holds the size of the result
9608
9609      This function will decode the given encoded data, using the hex
9610      encoding used by PSK password files.
9611
9612      Note that hex_data should be null terminated.
9613
9614      *Returns:* 'GNUTLS_E_SHORT_MEMORY_BUFFER' if the buffer given is
9615      not long enough, or 0 on success.
9616
9617 gnutls_hex_encode
9618 -----------------
9619
9620  -- Function: int gnutls_hex_encode (const gnutls_datum_t * DATA, char *
9621           RESULT, size_t * RESULT_SIZE)
9622      DATA: contain the raw data
9623
9624      RESULT: the place where hex data will be copied
9625
9626      RESULT_SIZE: holds the size of the result
9627
9628      This function will convert the given data to printable data, using
9629      the hex encoding, as used in the PSK password files.
9630
9631      *Returns:* 'GNUTLS_E_SHORT_MEMORY_BUFFER' if the buffer given is
9632      not long enough, or 0 on success.
9633
9634 gnutls_hmac_deinit
9635 ------------------
9636
9637  -- Function: void gnutls_hmac_deinit (gnutls_hmac_hd_t HANDLE, void *
9638           DIGEST)
9639      HANDLE: is a 'gnutls_hmac_hd_t' structure.
9640
9641      DIGEST: is the output value of the MAC
9642
9643      This function will deinitialize all resources occupied by the given
9644      hmac context.
9645
9646      *Since:* 2.10.0
9647
9648 gnutls_hmac_fast
9649 ----------------
9650
9651  -- Function: int gnutls_hmac_fast (gnutls_mac_algorithm_t ALGORITHM,
9652           const void * KEY, size_t KEYLEN, const void * TEXT, size_t
9653           TEXTLEN, void * DIGEST)
9654      ALGORITHM: the hash algorithm to use
9655
9656      KEY: the key to use
9657
9658      KEYLEN: The length of the key
9659
9660      TEXT: the data to hash
9661
9662      TEXTLEN: The length of data to hash
9663
9664      DIGEST: is the output value of the hash
9665
9666      This convenience function will hash the given data and return
9667      output on a single call.
9668
9669      *Returns:* Zero or a negative value on error.
9670
9671      *Since:* 2.10.0
9672
9673 gnutls_hmac_get_len
9674 -------------------
9675
9676  -- Function: int gnutls_hmac_get_len (gnutls_mac_algorithm_t ALGORITHM)
9677      ALGORITHM: the hmac algorithm to use
9678
9679      This function will return the length of the output data of the
9680      given hmac algorithm.
9681
9682      *Returns:* The length or zero on error.
9683
9684      *Since:* 2.10.0
9685
9686 gnutls_hmac_init
9687 ----------------
9688
9689  -- Function: int gnutls_hmac_init (gnutls_hmac_hd_t * DIG,
9690           gnutls_digest_algorithm_t ALGORITHM, const void * KEY, size_t
9691           KEYLEN)
9692      DIG: is a 'gnutls_hmac_hd_t' structure.
9693
9694      ALGORITHM: the HMAC algorithm to use
9695
9696      KEY: The key to be used for encryption
9697
9698      KEYLEN: The length of the key
9699
9700      This function will initialize an context that can be used to
9701      produce a Message Authentication Code (MAC) of data.  This will
9702      effectively use the current crypto backend in use by gnutls or the
9703      cryptographic accelerator in use.
9704
9705      *Returns:* Zero or a negative value on error.
9706
9707      *Since:* 2.10.0
9708
9709 gnutls_hmac_output
9710 ------------------
9711
9712  -- Function: void gnutls_hmac_output (gnutls_hmac_hd_t HANDLE, void *
9713           DIGEST)
9714      HANDLE: is a 'gnutls_hmac_hd_t' structure.
9715
9716      DIGEST: is the output value of the MAC
9717
9718      This function will output the current MAC value.
9719
9720      *Since:* 2.10.0
9721
9722 gnutls_hmac
9723 -----------
9724
9725  -- Function: int gnutls_hmac (gnutls_hmac_hd_t HANDLE, const void *
9726           TEXT, size_t TEXTLEN)
9727      HANDLE: is a 'gnutls_cipher_hd_t' structure.
9728
9729      TEXT: the data to hash
9730
9731      TEXTLEN: The length of data to hash
9732
9733      This function will hash the given data using the algorithm
9734      specified by the context.
9735
9736      *Returns:* Zero or a negative value on error.
9737
9738      *Since:* 2.10.0
9739
9740 gnutls_init
9741 -----------
9742
9743  -- Function: int gnutls_init (gnutls_session_t * SESSION,
9744           gnutls_connection_end_t CON_END)
9745      SESSION: is a pointer to a 'gnutls_session_t' structure.
9746
9747      CON_END: indicate if this session is to be used for server or
9748      client.
9749
9750      This function initializes the current session to null.  Every
9751      session must be initialized before use, so internal structures can
9752      be allocated.  This function allocates structures which can only be
9753      free'd by calling 'gnutls_deinit()'.  Returns zero on success.
9754
9755      'con_end' can be one of 'GNUTLS_CLIENT' and 'GNUTLS_SERVER'.
9756
9757      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
9758
9759 gnutls_kx_get_id
9760 ----------------
9761
9762  -- Function: gnutls_kx_algorithm_t gnutls_kx_get_id (const char * NAME)
9763      NAME: is a KX name
9764
9765      Convert a string to a 'gnutls_kx_algorithm_t' value.  The names are
9766      compared in a case insensitive way.
9767
9768      *Returns:* an id of the specified KX algorithm, or
9769      'GNUTLS_KX_UNKNOWN' on error.
9770
9771 gnutls_kx_get_name
9772 ------------------
9773
9774  -- Function: const char * gnutls_kx_get_name (gnutls_kx_algorithm_t
9775           ALGORITHM)
9776      ALGORITHM: is a key exchange algorithm
9777
9778      Convert a 'gnutls_kx_algorithm_t' value to a string.
9779
9780      *Returns:* a pointer to a string that contains the name of the
9781      specified key exchange algorithm, or 'NULL'.
9782
9783 gnutls_kx_get
9784 -------------
9785
9786  -- Function: gnutls_kx_algorithm_t gnutls_kx_get (gnutls_session_t
9787           SESSION)
9788      SESSION: is a 'gnutls_session_t' structure.
9789
9790      Get currently used key exchange algorithm.
9791
9792      *Returns:* the key exchange algorithm used in the last handshake, a
9793      'gnutls_kx_algorithm_t' value.
9794
9795 gnutls_kx_list
9796 --------------
9797
9798  -- Function: const gnutls_kx_algorithm_t * gnutls_kx_list ( VOID)
9799
9800      Get a list of supported key exchange algorithms.
9801
9802      *Returns:* a zero-terminated list of 'gnutls_kx_algorithm_t'
9803      integers indicating the available key exchange algorithms.
9804
9805 gnutls_kx_set_priority
9806 ----------------------
9807
9808  -- Function: int gnutls_kx_set_priority (gnutls_session_t SESSION,
9809           const int * LIST)
9810      SESSION: is a 'gnutls_session_t' structure.
9811
9812      LIST: is a 0 terminated list of gnutls_kx_algorithm_t elements.
9813
9814      Sets the priority on the key exchange algorithms supported by
9815      gnutls.  Priority is higher for elements specified before others.
9816      After specifying the algorithms you want, you must append a 0.
9817      Note that the priority is set on the client.  The server does not
9818      use the algorithm's priority except for disabling algorithms that
9819      were not specified.
9820
9821      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
9822
9823 gnutls_mac_get_id
9824 -----------------
9825
9826  -- Function: gnutls_mac_algorithm_t gnutls_mac_get_id (const char *
9827           NAME)
9828      NAME: is a MAC algorithm name
9829
9830      Convert a string to a 'gnutls_mac_algorithm_t' value.  The names
9831      are compared in a case insensitive way.
9832
9833      *Returns:* a 'gnutls_mac_algorithm_t' id of the specified MAC
9834      algorithm string, or 'GNUTLS_MAC_UNKNOWN' on failures.
9835
9836 gnutls_mac_get_key_size
9837 -----------------------
9838
9839  -- Function: size_t gnutls_mac_get_key_size (gnutls_mac_algorithm_t
9840           ALGORITHM)
9841      ALGORITHM: is an encryption algorithm
9842
9843      Get size of MAC key.
9844
9845      *Returns:* length (in bytes) of the given MAC key size, or 0 if the
9846      given MAC algorithm is invalid.
9847
9848 gnutls_mac_get_name
9849 -------------------
9850
9851  -- Function: const char * gnutls_mac_get_name (gnutls_mac_algorithm_t
9852           ALGORITHM)
9853      ALGORITHM: is a MAC algorithm
9854
9855      Convert a 'gnutls_mac_algorithm_t' value to a string.
9856
9857      *Returns:* a string that contains the name of the specified MAC
9858      algorithm, or 'NULL'.
9859
9860 gnutls_mac_get
9861 --------------
9862
9863  -- Function: gnutls_mac_algorithm_t gnutls_mac_get (gnutls_session_t
9864           SESSION)
9865      SESSION: is a 'gnutls_session_t' structure.
9866
9867      Get currently used MAC algorithm.
9868
9869      *Returns:* the currently used mac algorithm, a
9870      'gnutls_mac_algorithm_t' value.
9871
9872 gnutls_mac_list
9873 ---------------
9874
9875  -- Function: const gnutls_mac_algorithm_t * gnutls_mac_list ( VOID)
9876
9877      Get a list of hash algorithms for use as MACs.  Note that not
9878      necessarily all MACs are supported in TLS cipher suites.  For
9879      example, MD2 is not supported as a cipher suite, but is supported
9880      for other purposes (e.g., X.509 signature verification or similar).
9881
9882      *Returns:* Return a zero-terminated list of
9883      'gnutls_mac_algorithm_t' integers indicating the available MACs.
9884
9885 gnutls_mac_set_priority
9886 -----------------------
9887
9888  -- Function: int gnutls_mac_set_priority (gnutls_session_t SESSION,
9889           const int * LIST)
9890      SESSION: is a 'gnutls_session_t' structure.
9891
9892      LIST: is a 0 terminated list of gnutls_mac_algorithm_t elements.
9893
9894      Sets the priority on the mac algorithms supported by gnutls.
9895      Priority is higher for elements specified before others.  After
9896      specifying the algorithms you want, you must append a 0.  Note that
9897      the priority is set on the client.  The server does not use the
9898      algorithm's priority except for disabling algorithms that were not
9899      specified.
9900
9901      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
9902
9903 gnutls_malloc
9904 -------------
9905
9906  -- Function: void * gnutls_malloc (size_t S)
9907
9908      This function will allocate 's' bytes data, and return a pointer to
9909      memory.  This function is supposed to be used by callbacks.
9910
9911      The allocation function used is the one set by
9912      'gnutls_global_set_mem_functions()'.
9913
9914 gnutls_openpgp_send_cert
9915 ------------------------
9916
9917  -- Function: void gnutls_openpgp_send_cert (gnutls_session_t SESSION,
9918           gnutls_openpgp_crt_status_t STATUS)
9919      SESSION: is a pointer to a 'gnutls_session_t' structure.
9920
9921      STATUS: is one of GNUTLS_OPENPGP_CERT, or
9922      GNUTLS_OPENPGP_CERT_FINGERPRINT
9923
9924      This function will order gnutls to send the key fingerprint instead
9925      of the key in the initial handshake procedure.  This should be used
9926      with care and only when there is indication or knowledge that the
9927      server can obtain the client's key.
9928
9929 gnutls_pem_base64_decode_alloc
9930 ------------------------------
9931
9932  -- Function: int gnutls_pem_base64_decode_alloc (const char * HEADER,
9933           const gnutls_datum_t * B64_DATA, gnutls_datum_t * RESULT)
9934      HEADER: The PEM header (eg.  CERTIFICATE)
9935
9936      B64_DATA: contains the encoded data
9937
9938      RESULT: the place where decoded data lie
9939
9940      This function will decode the given encoded data.  The decoded data
9941      will be allocated, and stored into result.  If the header given is
9942      non null this function will search for "---BEGIN header" and decode
9943      only this part.  Otherwise it will decode the first PEM packet
9944      found.
9945
9946      You should use 'gnutls_free()' to free the returned data.
9947
9948      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
9949      otherwise an error code is returned.
9950
9951 gnutls_pem_base64_decode
9952 ------------------------
9953
9954  -- Function: int gnutls_pem_base64_decode (const char * HEADER, const
9955           gnutls_datum_t * B64_DATA, unsigned char * RESULT, size_t *
9956           RESULT_SIZE)
9957      HEADER: A null terminated string with the PEM header (eg.
9958      CERTIFICATE)
9959
9960      B64_DATA: contain the encoded data
9961
9962      RESULT: the place where decoded data will be copied
9963
9964      RESULT_SIZE: holds the size of the result
9965
9966      This function will decode the given encoded data.  If the header
9967      given is non null this function will search for "---BEGIN header"
9968      and decode only this part.  Otherwise it will decode the first PEM
9969      packet found.
9970
9971      *Returns:* On success 'GNUTLS_E_SUCCESS' (0) is returned,
9972      'GNUTLS_E_SHORT_MEMORY_BUFFER' is returned if the buffer given is
9973      not long enough, or 0 on success.
9974
9975 gnutls_pem_base64_encode_alloc
9976 ------------------------------
9977
9978  -- Function: int gnutls_pem_base64_encode_alloc (const char * MSG,
9979           const gnutls_datum_t * DATA, gnutls_datum_t * RESULT)
9980      MSG: is a message to be put in the encoded header
9981
9982      DATA: contains the raw data
9983
9984      RESULT: will hold the newly allocated encoded data
9985
9986      This function will convert the given data to printable data, using
9987      the base64 encoding.  This is the encoding used in PEM messages.
9988      This function will allocate the required memory to hold the encoded
9989      data.
9990
9991      You should use 'gnutls_free()' to free the returned data.
9992
9993      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
9994      otherwise an error code is returned.
9995
9996 gnutls_pem_base64_encode
9997 ------------------------
9998
9999  -- Function: int gnutls_pem_base64_encode (const char * MSG, const
10000           gnutls_datum_t * DATA, char * RESULT, size_t * RESULT_SIZE)
10001      MSG: is a message to be put in the header
10002
10003      DATA: contain the raw data
10004
10005      RESULT: the place where base64 data will be copied
10006
10007      RESULT_SIZE: holds the size of the result
10008
10009      This function will convert the given data to printable data, using
10010      the base64 encoding.  This is the encoding used in PEM messages.
10011
10012      The output string will be null terminated, although the size will
10013      not include the terminating null.
10014
10015      *Returns:* On success 'GNUTLS_E_SUCCESS' (0) is returned,
10016      'GNUTLS_E_SHORT_MEMORY_BUFFER' is returned if the buffer given is
10017      not long enough, or 0 on success.
10018
10019 gnutls_perror
10020 -------------
10021
10022  -- Function: void gnutls_perror (int ERROR)
10023      ERROR: is a GnuTLS error code, a negative value
10024
10025      This function is like 'perror()'.  The only difference is that it
10026      accepts an error number returned by a gnutls function.
10027
10028 gnutls_pk_algorithm_get_name
10029 ----------------------------
10030
10031  -- Function: const char * gnutls_pk_algorithm_get_name
10032           (gnutls_pk_algorithm_t ALGORITHM)
10033      ALGORITHM: is a pk algorithm
10034
10035      Convert a 'gnutls_pk_algorithm_t' value to a string.
10036
10037      *Returns:* a string that contains the name of the specified public
10038      key algorithm, or 'NULL'.
10039
10040 gnutls_pk_bits_to_sec_param
10041 ---------------------------
10042
10043  -- Function: gnutls_sec_param_t gnutls_pk_bits_to_sec_param
10044           (gnutls_pk_algorithm_t ALGO, unsigned int BITS)
10045      ALGO: is a public key algorithm
10046
10047      BITS: is the number of bits
10048
10049      This is the inverse of 'gnutls_sec_param_to_pk_bits()'.  Given an
10050      algorithm and the number of bits, it will return the security
10051      parameter.  This is a rough indication.
10052
10053      *Returns:* The security parameter.
10054
10055 gnutls_pk_get_id
10056 ----------------
10057
10058  -- Function: gnutls_pk_algorithm_t gnutls_pk_get_id (const char * NAME)
10059      NAME: is a string containing a public key algorithm name.
10060
10061      Convert a string to a 'gnutls_pk_algorithm_t' value.  The names are
10062      compared in a case insensitive way.  For example,
10063      gnutls_pk_get_id("RSA") will return 'GNUTLS_PK_RSA'.
10064
10065      *Returns:* a 'gnutls_pk_algorithm_t' id of the specified public key
10066      algorithm string, or 'GNUTLS_PK_UNKNOWN' on failures.
10067
10068      *Since:* 2.6.0
10069
10070 gnutls_pk_get_name
10071 ------------------
10072
10073  -- Function: const char * gnutls_pk_get_name (gnutls_pk_algorithm_t
10074           ALGORITHM)
10075      ALGORITHM: is a public key algorithm
10076
10077      Convert a 'gnutls_pk_algorithm_t' value to a string.
10078
10079      *Returns:* a pointer to a string that contains the name of the
10080      specified public key algorithm, or 'NULL'.
10081
10082      *Since:* 2.6.0
10083
10084 gnutls_pk_list
10085 --------------
10086
10087  -- Function: const gnutls_pk_algorithm_t * gnutls_pk_list ( VOID)
10088
10089      Get a list of supported public key algorithms.
10090
10091      *Returns:* a zero-terminated list of 'gnutls_pk_algorithm_t'
10092      integers indicating the available ciphers.
10093
10094      *Since:* 2.6.0
10095
10096 gnutls_pkcs11_add_provider
10097 --------------------------
10098
10099  -- Function: int gnutls_pkcs11_add_provider (const char * NAME, const
10100           char * PARAMS)
10101      NAME: The filename of the module
10102
10103      PARAMS: should be NULL
10104
10105      This function will load and add a PKCS 11 module to the module list
10106      used in gnutls.  After this function is called the module will be
10107      used for PKCS 11 operations.
10108
10109      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10110      negative error value.
10111
10112 gnutls_pkcs11_copy_secret_key
10113 -----------------------------
10114
10115  -- Function: int gnutls_pkcs11_copy_secret_key (const char * TOKEN_URL,
10116           gnutls_datum_t * KEY, const char * LABEL, unsigned int
10117           KEY_USAGE, unsigned int FLAGS)
10118      TOKEN_URL: A PKCS '11' URL specifying a token
10119
10120      KEY: The raw key
10121
10122      LABEL: A name to be used for the stored data
10123
10124      KEY_USAGE: One of GNUTLS_KEY_*
10125
10126      FLAGS: One of GNUTLS_PKCS11_OBJ_FLAG_*
10127
10128      This function will copy a raw secret (symmetric) key into a PKCS
10129      '11' token specified by a URL. The key can be marked as sensitive
10130      or not.
10131
10132      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10133      negative error value.
10134
10135 gnutls_pkcs11_copy_x509_crt
10136 ---------------------------
10137
10138  -- Function: int gnutls_pkcs11_copy_x509_crt (const char * TOKEN_URL,
10139           gnutls_x509_crt_t CRT, const char * LABEL, unsigned int FLAGS)
10140      TOKEN_URL: A PKCS '11' URL specifying a token
10141
10142      CRT: A certificate
10143
10144      LABEL: A name to be used for the stored data
10145
10146      FLAGS: One of GNUTLS_PKCS11_OBJ_FLAG_*
10147
10148      This function will copy a certificate into a PKCS '11' token
10149      specified by a URL. The certificate can be marked as trusted or
10150      not.
10151
10152      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10153      negative error value.
10154
10155 gnutls_pkcs11_copy_x509_privkey
10156 -------------------------------
10157
10158  -- Function: int gnutls_pkcs11_copy_x509_privkey (const char *
10159           TOKEN_URL, gnutls_x509_privkey_t KEY, const char * LABEL,
10160           unsigned int KEY_USAGE, unsigned int FLAGS)
10161      TOKEN_URL: A PKCS '11' URL specifying a token
10162
10163      KEY: A private key
10164
10165      LABEL: A name to be used for the stored data
10166
10167      KEY_USAGE: One of GNUTLS_KEY_*
10168
10169      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
10170
10171      This function will copy a private key into a PKCS '11' token
10172      specified by a URL. It is highly recommended flags to contain
10173      'GNUTLS_PKCS11_OBJ_FLAG_MARK_SENSITIVE' unless there is a strong
10174      reason not to.
10175
10176      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10177      negative error value.
10178
10179 gnutls_pkcs11_deinit
10180 --------------------
10181
10182  -- Function: void gnutls_pkcs11_deinit ( VOID)
10183
10184      This function will deinitialize the PKCS 11 subsystem in gnutls.
10185
10186 gnutls_pkcs11_delete_url
10187 ------------------------
10188
10189  -- Function: int gnutls_pkcs11_delete_url (const char * OBJECT_URL,
10190           unsigned int FLAGS)
10191      OBJECT_URL: The URL of the object to delete.
10192
10193      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
10194
10195      This function will delete objects matching the given URL.
10196
10197      *Returns:* On success, the number of objects deleted is returned,
10198      otherwise a negative error value.
10199
10200 gnutls_pkcs11_init
10201 ------------------
10202
10203  -- Function: int gnutls_pkcs11_init (unsigned int FLAGS, const char *
10204           DEPRECATED_CONFIG_FILE)
10205      FLAGS: 'GNUTLS_PKCS11_FLAG_MANUAL' or 'GNUTLS_PKCS11_FLAG_AUTO'
10206
10207      DEPRECATED_CONFIG_FILE: either NULL or the location of a deprecated
10208      configuration file
10209
10210      This function will initialize the PKCS 11 subsystem in gnutls.  It
10211      will read configuration files if 'GNUTLS_PKCS11_FLAG_AUTO' is used
10212      or allow you to independently load PKCS 11 modules using
10213      'gnutls_pkcs11_add_provider()' if 'GNUTLS_PKCS11_FLAG_MANUAL' is
10214      specified.
10215
10216      Using a custom configfile is deprecated and will not be supported
10217      in future versions of gnutls.
10218
10219      Normally you don't need to call this function since it is being
10220      called by 'gnutls_global_init()' using the
10221      'GNUTLS_PKCS11_FLAG_AUTO'.  If you need to call this function, you
10222      must call it before 'gnutls_global_init()'.
10223
10224      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10225      negative error value.
10226
10227 gnutls_pkcs11_obj_deinit
10228 ------------------------
10229
10230  -- Function: void gnutls_pkcs11_obj_deinit (gnutls_pkcs11_obj_t OBJ)
10231      OBJ: The structure to be initialized
10232
10233      This function will deinitialize a certificate structure.
10234
10235 gnutls_pkcs11_obj_export_url
10236 ----------------------------
10237
10238  -- Function: int gnutls_pkcs11_obj_export_url (gnutls_pkcs11_obj_t OBJ,
10239           gnutls_pkcs11_url_type_t DETAILED, char ** URL)
10240      OBJ: Holds the PKCS 11 certificate
10241
10242      DETAILED: non zero if a detailed URL is required
10243
10244      URL: will contain an allocated url
10245
10246      This function will export a URL identifying the given certificate.
10247
10248      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10249      negative error value.
10250
10251 gnutls_pkcs11_obj_export
10252 ------------------------
10253
10254  -- Function: int gnutls_pkcs11_obj_export (gnutls_pkcs11_obj_t OBJ,
10255           void * OUTPUT_DATA, size_t * OUTPUT_DATA_SIZE)
10256      OBJ: Holds the object
10257
10258      OUTPUT_DATA: will contain a certificate PEM or DER encoded
10259
10260      OUTPUT_DATA_SIZE: holds the size of output_data (and will be
10261      replaced by the actual size of parameters)
10262
10263      This function will export the pkcs11 object data.  It is normal for
10264      PKCS '11' data to be inaccesible and in that case
10265      'GNUTLS_E_INVALID_REQUEST' will be returned.
10266
10267      If the buffer provided is not long enough to hold the output, then
10268      *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
10269      be returned.
10270
10271      If the structure is PEM encoded, it will have a header of "BEGIN
10272      CERTIFICATE".
10273
10274      *Return value:* In case of failure a negative value will be
10275      returned, and 0 on success.
10276
10277 gnutls_pkcs11_obj_get_info
10278 --------------------------
10279
10280  -- Function: int gnutls_pkcs11_obj_get_info (gnutls_pkcs11_obj_t CRT,
10281           gnutls_pkcs11_obj_info_t ITYPE, void * OUTPUT, size_t *
10282           OUTPUT_SIZE)
10283      CRT: should contain a 'gnutls_pkcs11_obj_t' structure
10284
10285      ITYPE: Denotes the type of information requested
10286
10287      OUTPUT: where output will be stored
10288
10289      OUTPUT_SIZE: contains the maximum size of the output and will be
10290      overwritten with actual
10291
10292      This function will return information about the PKCS 11
10293      certificatesuch as the label, id as well as token information where
10294      the key is stored.  When output is text it returns null terminated
10295      string although 'output_size' contains the size of the actual data
10296      only.
10297
10298      *Returns:* zero on success or a negative value on error.
10299
10300 gnutls_pkcs11_obj_get_type
10301 --------------------------
10302
10303  -- Function: gnutls_pkcs11_obj_type_t gnutls_pkcs11_obj_get_type
10304           (gnutls_pkcs11_obj_t OBJ)
10305      This function will return the type of the certificate being stored
10306      in the structure.
10307
10308      *Returns:* The type of the certificate.
10309
10310 gnutls_pkcs11_obj_import_url
10311 ----------------------------
10312
10313  -- Function: int gnutls_pkcs11_obj_import_url (gnutls_pkcs11_obj_t
10314           CERT, const char * URL, unsigned int FLAGS)
10315      CERT: The structure to store the parsed certificate
10316
10317      URL: a PKCS 11 url identifying the key
10318
10319      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
10320
10321      This function will "import" a PKCS 11 URL identifying a certificate
10322      key to the 'gnutls_pkcs11_obj_t' structure.  This does not involve
10323      any parsing (such as X.509 or OpenPGP) since the
10324      'gnutls_pkcs11_obj_t' is format agnostic.  Only data are
10325      transferred.
10326
10327      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10328      negative error value.
10329
10330 gnutls_pkcs11_obj_init
10331 ----------------------
10332
10333  -- Function: int gnutls_pkcs11_obj_init (gnutls_pkcs11_obj_t * OBJ)
10334      OBJ: The structure to be initialized
10335
10336      This function will initialize a pkcs11 certificate structure.
10337
10338      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10339      negative error value.
10340
10341 gnutls_pkcs11_obj_list_import_url
10342 ---------------------------------
10343
10344  -- Function: int gnutls_pkcs11_obj_list_import_url (gnutls_pkcs11_obj_t
10345           * P_LIST, unsigned int * N_LIST, const char * URL,
10346           gnutls_pkcs11_obj_attr_t ATTRS, unsigned int FLAGS)
10347      P_LIST: An uninitialized object list (may be NULL)
10348
10349      N_LIST: initially should hold the maximum size of the list.  Will
10350      contain the actual size.
10351
10352      URL: A PKCS 11 url identifying a set of objects
10353
10354      ATTRS: Attributes of type 'gnutls_pkcs11_obj_attr_t' that can be
10355      used to limit output
10356
10357      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
10358
10359      This function will initialize and set values to an object list by
10360      using all objects identified by a PKCS 11 URL.
10361
10362      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10363      negative error value.
10364
10365 gnutls_pkcs11_privkey_deinit
10366 ----------------------------
10367
10368  -- Function: void gnutls_pkcs11_privkey_deinit (gnutls_pkcs11_privkey_t
10369           KEY)
10370      KEY: The structure to be initialized
10371
10372      This function will deinitialize a private key structure.
10373
10374 gnutls_pkcs11_privkey_export_url
10375 --------------------------------
10376
10377  -- Function: int gnutls_pkcs11_privkey_export_url
10378           (gnutls_pkcs11_privkey_t KEY, gnutls_pkcs11_url_type_t
10379           DETAILED, char ** URL)
10380      KEY: Holds the PKCS 11 key
10381
10382      DETAILED: non zero if a detailed URL is required
10383
10384      URL: will contain an allocated url
10385
10386      This function will export a URL identifying the given key.
10387
10388      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10389      negative error value.
10390
10391 gnutls_pkcs11_privkey_get_info
10392 ------------------------------
10393
10394  -- Function: int gnutls_pkcs11_privkey_get_info
10395           (gnutls_pkcs11_privkey_t PKEY, gnutls_pkcs11_obj_info_t ITYPE,
10396           void * OUTPUT, size_t * OUTPUT_SIZE)
10397      PKEY: should contain a 'gnutls_pkcs11_privkey_t' structure
10398
10399      ITYPE: Denotes the type of information requested
10400
10401      OUTPUT: where output will be stored
10402
10403      OUTPUT_SIZE: contains the maximum size of the output and will be
10404      overwritten with actual
10405
10406      This function will return information about the PKCS 11 private key
10407      such as the label, id as well as token information where the key is
10408      stored.  When output is text it returns null terminated string
10409      although 'output_size' contains the size of the actual data only.
10410
10411      *Returns:* zero on success or a negative value on error.
10412
10413 gnutls_pkcs11_privkey_get_pk_algorithm
10414 --------------------------------------
10415
10416  -- Function: int gnutls_pkcs11_privkey_get_pk_algorithm
10417           (gnutls_pkcs11_privkey_t KEY, unsigned int * BITS)
10418      KEY: should contain a 'gnutls_pkcs11_privkey_t' structure
10419
10420      This function will return the public key algorithm of a private
10421      key.
10422
10423      *Returns:* a member of the 'gnutls_pk_algorithm_t' enumeration on
10424      success, or a negative value on error.
10425
10426 gnutls_pkcs11_privkey_import_url
10427 --------------------------------
10428
10429  -- Function: int gnutls_pkcs11_privkey_import_url
10430           (gnutls_pkcs11_privkey_t PKEY, const char * URL, unsigned int
10431           FLAGS)
10432      PKEY: The structure to store the parsed key
10433
10434      URL: a PKCS 11 url identifying the key
10435
10436      FLAGS: sequence of GNUTLS_PKCS_PRIVKEY_*
10437
10438      This function will "import" a PKCS 11 URL identifying a private key
10439      to the 'gnutls_pkcs11_privkey_t' structure.  In reality since in
10440      most cases keys cannot be exported, the private key structure is
10441      being associated with the available operations on the token.
10442
10443      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10444      negative error value.
10445
10446 gnutls_pkcs11_privkey_init
10447 --------------------------
10448
10449  -- Function: int gnutls_pkcs11_privkey_init (gnutls_pkcs11_privkey_t *
10450           KEY)
10451      KEY: The structure to be initialized
10452
10453      This function will initialize an private key structure.
10454
10455      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10456      negative error value.
10457
10458 gnutls_pkcs11_reinit
10459 --------------------
10460
10461  -- Function: int gnutls_pkcs11_reinit ( VOID)
10462
10463      This function will reinitialize the PKCS 11 subsystem in gnutls.
10464      This is required by PKCS 11 when an application uses 'fork()'.  The
10465      reinitialization function must be called on the child.
10466
10467      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
10468      otherwise a negative error value.
10469
10470      *Since:* 3.0.0
10471
10472 gnutls_pkcs11_set_pin_function
10473 ------------------------------
10474
10475  -- Function: void gnutls_pkcs11_set_pin_function
10476           (gnutls_pkcs11_pin_callback_t FN, void * USERDATA)
10477      FN: The PIN callback
10478
10479      USERDATA: data to be supplied to callback
10480
10481      This function will set a callback function to be used when a PIN is
10482      required for PKCS 11 operations.
10483
10484      Callback for PKCS'11' PIN entry.  The callback provides the PIN
10485      code to unlock the token with label 'token_label', specified by the
10486      URL 'token_url'.
10487
10488      The PIN code, as a NUL-terminated ASCII string, should be copied
10489      into the 'pin' buffer (of maximum size pin_max), and return 0 to
10490      indicate success.  Alternatively, the callback may return a
10491      negative gnutls error code to indicate failure and cancel PIN entry
10492      (in which case, the contents of the 'pin' parameter are ignored).
10493
10494      When a PIN is required, the callback will be invoked repeatedly
10495      (and indefinitely) until either the returned PIN code is correct,
10496      the callback returns failure, or the token refuses login (e.g.
10497      when the token is locked due to too many incorrect PINs!).  For the
10498      first such invocation, the 'attempt' counter will have value zero;
10499      it will increase by one for each subsequent attempt.
10500
10501      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10502      negative error value.
10503
10504 gnutls_pkcs11_set_token_function
10505 --------------------------------
10506
10507  -- Function: void gnutls_pkcs11_set_token_function
10508           (gnutls_pkcs11_token_callback_t FN, void * USERDATA)
10509      FN: The token callback
10510
10511      USERDATA: data to be supplied to callback
10512
10513      This function will set a callback function to be used when a token
10514      needs to be inserted to continue PKCS 11 operations.
10515
10516      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10517      negative error value.
10518
10519 gnutls_pkcs11_token_get_flags
10520 -----------------------------
10521
10522  -- Function: int gnutls_pkcs11_token_get_flags (const char * URL,
10523           unsigned int * FLAGS)
10524      URL: should contain a PKCS 11 URL
10525
10526      FLAGS: The output flags (GNUTLS_PKCS11_TOKEN_*)
10527
10528      This function will return information about the PKCS 11 token
10529      flags.
10530
10531      *Returns:* zero on success or a negative value on error.
10532
10533 gnutls_pkcs11_token_get_info
10534 ----------------------------
10535
10536  -- Function: int gnutls_pkcs11_token_get_info (const char * URL,
10537           gnutls_pkcs11_token_info_t TTYPE, void * OUTPUT, size_t *
10538           OUTPUT_SIZE)
10539      URL: should contain a PKCS 11 URL
10540
10541      TTYPE: Denotes the type of information requested
10542
10543      OUTPUT: where output will be stored
10544
10545      OUTPUT_SIZE: contains the maximum size of the output and will be
10546      overwritten with actual
10547
10548      This function will return information about the PKCS 11 token such
10549      as the label, id as well as token information where the key is
10550      stored.
10551
10552      *Returns:* zero on success or a negative value on error.
10553
10554 gnutls_pkcs11_token_get_mechanism
10555 ---------------------------------
10556
10557  -- Function: int gnutls_pkcs11_token_get_mechanism (const char * URL,
10558           int IDX, unsigned long * MECHANISM)
10559      URL: should contain a PKCS 11 URL
10560
10561      IDX: The index of the mechanism
10562
10563      MECHANISM: The PKCS '11' mechanism ID
10564
10565      This function will return the names of the supported mechanisms by
10566      the token.  It should be called with an increasing index until it
10567      return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE.
10568
10569      *Returns:* zero on success or a negative value on error.
10570
10571 gnutls_pkcs11_token_get_url
10572 ---------------------------
10573
10574  -- Function: int gnutls_pkcs11_token_get_url (unsigned int SEQ,
10575           gnutls_pkcs11_url_type_t DETAILED, char ** URL)
10576      SEQ: sequence number starting from 0
10577
10578      DETAILED: non zero if a detailed URL is required
10579
10580      URL: will contain an allocated url
10581
10582      This function will return the URL for each token available in
10583      system.  The url has to be released using 'gnutls_free()'
10584
10585      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned,
10586      'GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE' if the sequence number
10587      exceeds the available tokens, otherwise a negative error value.
10588
10589 gnutls_pkcs11_token_init
10590 ------------------------
10591
10592  -- Function: int gnutls_pkcs11_token_init (const char * TOKEN_URL,
10593           const char * SO_PIN, const char * LABEL)
10594      TOKEN_URL: A PKCS '11' URL specifying a token
10595
10596      SO_PIN: Security Officer's PIN
10597
10598      LABEL: A name to be used for the token
10599
10600      This function will initialize (format) a token.  If the token is at
10601      a factory defaults state the security officer's PIN given will be
10602      set to be the default.  Otherwise it should match the officer's
10603      PIN.
10604
10605      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10606      negative error value.
10607
10608 gnutls_pkcs11_token_set_pin
10609 ---------------------------
10610
10611  -- Function: int gnutls_pkcs11_token_set_pin (const char * TOKEN_URL,
10612           const char * OLDPIN, const char * NEWPIN, unsigned int FLAGS)
10613      TOKEN_URL: A PKCS '11' URL specifying a token
10614
10615      OLDPIN: old user's PIN
10616
10617      NEWPIN: new user's PIN
10618
10619      FLAGS: one of gnutls_pkcs11_pin_flag_t
10620
10621      This function will modify or set a user's PIN for the given token.
10622      If it is called to set a user pin for first time the oldpin must be
10623      NULL.
10624
10625      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10626      negative error value.
10627
10628 gnutls_prf_raw
10629 --------------
10630
10631  -- Function: int gnutls_prf_raw (gnutls_session_t SESSION, size_t
10632           LABEL_SIZE, const char * LABEL, size_t SEED_SIZE, const char *
10633           SEED, size_t OUTSIZE, char * OUT)
10634      SESSION: is a 'gnutls_session_t' structure.
10635
10636      LABEL_SIZE: length of the 'label' variable.
10637
10638      LABEL: label used in PRF computation, typically a short string.
10639
10640      SEED_SIZE: length of the 'seed' variable.
10641
10642      SEED: optional extra data to seed the PRF with.
10643
10644      OUTSIZE: size of pre-allocated output buffer to hold the output.
10645
10646      OUT: pre-allocate buffer to hold the generated data.
10647
10648      Apply the TLS Pseudo-Random-Function (PRF) using the master secret
10649      on some data.
10650
10651      The 'label' variable usually contain a string denoting the purpose
10652      for the generated data.  The 'seed' usually contain data such as
10653      the client and server random, perhaps together with some additional
10654      data that is added to guarantee uniqueness of the output for a
10655      particular purpose.
10656
10657      Because the output is not guaranteed to be unique for a particular
10658      session unless 'seed' include the client random and server random
10659      fields (the PRF would output the same data on another connection
10660      resumed from the first one), it is not recommended to use this
10661      function directly.  The 'gnutls_prf()' function seed the PRF with
10662      the client and server random fields directly, and is recommended if
10663      you want to generate pseudo random data unique for each session.
10664
10665      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
10666
10667 gnutls_prf
10668 ----------
10669
10670  -- Function: int gnutls_prf (gnutls_session_t SESSION, size_t
10671           LABEL_SIZE, const char * LABEL, int SERVER_RANDOM_FIRST,
10672           size_t EXTRA_SIZE, const char * EXTRA, size_t OUTSIZE, char *
10673           OUT)
10674      SESSION: is a 'gnutls_session_t' structure.
10675
10676      LABEL_SIZE: length of the 'label' variable.
10677
10678      LABEL: label used in PRF computation, typically a short string.
10679
10680      SERVER_RANDOM_FIRST: non-0 if server random field should be first
10681      in seed
10682
10683      EXTRA_SIZE: length of the 'extra' variable.
10684
10685      EXTRA: optional extra data to seed the PRF with.
10686
10687      OUTSIZE: size of pre-allocated output buffer to hold the output.
10688
10689      OUT: pre-allocate buffer to hold the generated data.
10690
10691      Apply the TLS Pseudo-Random-Function (PRF) using the master secret
10692      on some data, seeded with the client and server random fields.
10693
10694      The 'label' variable usually contain a string denoting the purpose
10695      for the generated data.  The 'server_random_first' indicate whether
10696      the client random field or the server random field should be first
10697      in the seed.  Non-0 indicate that the server random field is first,
10698      0 that the client random field is first.
10699
10700      The 'extra' variable can be used to add more data to the seed,
10701      after the random variables.  It can be used to tie make sure the
10702      generated output is strongly connected to some additional data
10703      (e.g., a string used in user authentication).
10704
10705      The output is placed in *'OUT', which must be pre-allocated.
10706
10707      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
10708
10709 gnutls_priority_deinit
10710 ----------------------
10711
10712  -- Function: void gnutls_priority_deinit (gnutls_priority_t
10713           PRIORITY_CACHE)
10714      PRIORITY_CACHE: is a 'gnutls_prioritity_t' structure.
10715
10716      Deinitializes the priority cache.
10717
10718 gnutls_priority_init
10719 --------------------
10720
10721  -- Function: int gnutls_priority_init (gnutls_priority_t *
10722           PRIORITY_CACHE, const char * PRIORITIES, const char **
10723           ERR_POS)
10724      PRIORITY_CACHE: is a 'gnutls_prioritity_t' structure.
10725
10726      PRIORITIES: is a string describing priorities
10727
10728      ERR_POS: In case of an error this will have the position in the
10729      string the error occured
10730
10731      Sets priorities for the ciphers, key exchange methods, macs and
10732      compression methods.
10733
10734      The 'priorities' option allows you to specify a colon separated
10735      list of the cipher priorities to enable.
10736
10737      *Common keywords:* Some keywords are defined to provide quick
10738      access to common preferences.
10739
10740      "PERFORMANCE" means all the "secure" ciphersuites are enabled,
10741      limited to 128 bit ciphers and sorted by terms of speed
10742      performance.
10743
10744      "NORMAL" means all "secure" ciphersuites.  The 256-bit ciphers are
10745      included as a fallback only.  The ciphers are sorted by security
10746      margin.
10747
10748      "SECURE128" means all "secure" ciphersuites with ciphers up to 128
10749      bits, sorted by security margin.
10750
10751      "SECURE256" means all "secure" ciphersuites including the 256 bit
10752      ciphers, sorted by security margin.
10753
10754      "EXPORT" means all ciphersuites are enabled, including the
10755      low-security 40 bit ciphers.
10756
10757      "NONE" means nothing is enabled.  This disables even protocols and
10758      compression methods.
10759
10760      *Special keywords:* "!"  or "-" appended with an algorithm will
10761      remove this algorithm.
10762
10763      "+" appended with an algorithm will add this algorithm.
10764
10765      Check the GnuTLS manual section "Priority strings" for detailed
10766      information.
10767
10768      *Examples:*
10769      "NONE:+VERS-TLS-ALL:+MAC-ALL:+RSA:+AES-128-CBC:+SIGN-ALL:+COMP-NULL"
10770
10771      "NORMAL:-ARCFOUR-128" means normal ciphers except for ARCFOUR-128.
10772
10773      "SECURE:-VERS-SSL3.0:+COMP-DEFLATE" means that only secure ciphers
10774      are enabled, SSL3.0 is disabled, and libz compression enabled.
10775
10776      "NONE:+VERS-TLS-ALL:+AES-128-CBC:+RSA:+SHA1:+COMP-NULL:+SIGN-RSA-SHA1",
10777
10778      "NORMAL:'COMPAT'" is the most compatible mode.
10779
10780      *Returns:* On syntax error 'GNUTLS_E_INVALID_REQUEST' is returned,
10781      'GNUTLS_E_SUCCESS' on success, or an error code.
10782
10783 gnutls_priority_set_direct
10784 --------------------------
10785
10786  -- Function: int gnutls_priority_set_direct (gnutls_session_t SESSION,
10787           const char * PRIORITIES, const char ** ERR_POS)
10788      SESSION: is a 'gnutls_session_t' structure.
10789
10790      PRIORITIES: is a string describing priorities
10791
10792      ERR_POS: In case of an error this will have the position in the
10793      string the error occured
10794
10795      Sets the priorities to use on the ciphers, key exchange methods,
10796      macs and compression methods.  This function avoids keeping a
10797      priority cache and is used to directly set string priorities to a
10798      TLS session.  For documentation check the 'gnutls_priority_init()'.
10799
10800      *Returns:* On syntax error 'GNUTLS_E_INVALID_REQUEST' is returned,
10801      'GNUTLS_E_SUCCESS' on success, or an error code.
10802
10803 gnutls_priority_set
10804 -------------------
10805
10806  -- Function: int gnutls_priority_set (gnutls_session_t SESSION,
10807           gnutls_priority_t PRIORITY)
10808      SESSION: is a 'gnutls_session_t' structure.
10809
10810      PRIORITY: is a 'gnutls_priority_t' structure.
10811
10812      Sets the priorities to use on the ciphers, key exchange methods,
10813      macs and compression methods.
10814
10815      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
10816
10817 gnutls_privkey_decrypt_data
10818 ---------------------------
10819
10820  -- Function: int gnutls_privkey_decrypt_data (gnutls_privkey_t KEY,
10821           unsigned int FLAGS, const gnutls_datum_t * CIPHERTEXT,
10822           gnutls_datum_t * PLAINTEXT)
10823      KEY: Holds the key
10824
10825      FLAGS: zero for now
10826
10827      CIPHERTEXT: holds the data to be decrypted
10828
10829      PLAINTEXT: will contain the decrypted data, allocated with
10830      'gnutls_malloc()'
10831
10832      This function will decrypt the given data using the algorithm
10833      supported by the private key.
10834
10835      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10836      negative error value.
10837
10838 gnutls_privkey_deinit
10839 ---------------------
10840
10841  -- Function: void gnutls_privkey_deinit (gnutls_privkey_t KEY)
10842      KEY: The structure to be deinitialized
10843
10844      This function will deinitialize a private key structure.
10845
10846 gnutls_privkey_get_pk_algorithm
10847 -------------------------------
10848
10849  -- Function: int gnutls_privkey_get_pk_algorithm (gnutls_privkey_t KEY,
10850           unsigned int * BITS)
10851      KEY: should contain a 'gnutls_privkey_t' structure
10852
10853      BITS: If set will return the number of bits of the parameters (may
10854      be NULL)
10855
10856      This function will return the public key algorithm of a private key
10857      and if possible will return a number of bits that indicates the
10858      security parameter of the key.
10859
10860      *Returns:* a member of the 'gnutls_pk_algorithm_t' enumeration on
10861      success, or a negative value on error.
10862
10863 gnutls_privkey_get_type
10864 -----------------------
10865
10866  -- Function: gnutls_privkey_type_t gnutls_privkey_get_type
10867           (gnutls_privkey_t KEY)
10868      KEY: should contain a 'gnutls_privkey_t' structure
10869
10870      This function will return the type of the private key.  This is
10871      actually the type of the subsystem used to set this private key.
10872
10873      *Returns:* a member of the 'gnutls_privkey_type_t' enumeration on
10874      success, or a negative value on error.
10875
10876 gnutls_privkey_import_openpgp
10877 -----------------------------
10878
10879  -- Function: int gnutls_privkey_import_openpgp (gnutls_privkey_t PKEY,
10880           gnutls_openpgp_privkey_t KEY, unsigned int FLAGS)
10881      PKEY: The private key
10882
10883      KEY: The private key to be imported
10884
10885      FLAGS: should be zero
10886
10887      This function will import the given private key to the abstract
10888      'gnutls_privkey_t' structure.
10889
10890      The 'gnutls_openpgp_privkey_t' object must not be deallocated
10891      during the lifetime of this structure.  The subkey set as preferred
10892      will be used, or the master key otherwise.
10893
10894      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10895      negative error value.
10896
10897 gnutls_privkey_import_pkcs11
10898 ----------------------------
10899
10900  -- Function: int gnutls_privkey_import_pkcs11 (gnutls_privkey_t PKEY,
10901           gnutls_pkcs11_privkey_t KEY, unsigned int FLAGS)
10902      PKEY: The private key
10903
10904      KEY: The private key to be imported
10905
10906      FLAGS: should be zero
10907
10908      This function will import the given private key to the abstract
10909      'gnutls_privkey_t' structure.
10910
10911      The 'gnutls_pkcs11_privkey_t' object must not be deallocated during
10912      the lifetime of this structure.
10913
10914      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10915      negative error value.
10916
10917 gnutls_privkey_import_x509
10918 --------------------------
10919
10920  -- Function: int gnutls_privkey_import_x509 (gnutls_privkey_t PKEY,
10921           gnutls_x509_privkey_t KEY, unsigned int FLAGS)
10922      PKEY: The private key
10923
10924      KEY: The private key to be imported
10925
10926      FLAGS: should be zero
10927
10928      This function will import the given private key to the abstract
10929      'gnutls_privkey_t' structure.
10930
10931      The 'gnutls_x509_privkey_t' object must not be deallocated during
10932      the lifetime of this structure.
10933
10934      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10935      negative error value.
10936
10937 gnutls_privkey_init
10938 -------------------
10939
10940  -- Function: int gnutls_privkey_init (gnutls_privkey_t * KEY)
10941      KEY: The structure to be initialized
10942
10943      This function will initialize an private key structure.
10944
10945      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10946      negative error value.
10947
10948 gnutls_privkey_sign_data
10949 ------------------------
10950
10951  -- Function: int gnutls_privkey_sign_data (gnutls_privkey_t SIGNER,
10952           gnutls_digest_algorithm_t HASH, unsigned int FLAGS, const
10953           gnutls_datum_t * DATA, gnutls_datum_t * SIGNATURE)
10954      SIGNER: Holds the key
10955
10956      HASH: should be a digest algorithm
10957
10958      FLAGS: should be 0 for now
10959
10960      DATA: holds the data to be signed
10961
10962      SIGNATURE: will contain the signature allocate with
10963      'gnutls_malloc()'
10964
10965      This function will sign the given data using a signature algorithm
10966      supported by the private key.  Signature algorithms are always used
10967      together with a hash functions.  Different hash functions may be
10968      used for the RSA algorithm, but only SHA-1 for the DSA keys.
10969
10970      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
10971      negative error value.
10972
10973      *Since:* 2.12.0
10974
10975 gnutls_privkey_sign_hash
10976 ------------------------
10977
10978  -- Function: int gnutls_privkey_sign_hash (gnutls_privkey_t SIGNER,
10979           gnutls_digest_algorithm_t HASH_ALGO, unsigned int FLAGS, const
10980           gnutls_datum_t * HASH_DATA, gnutls_datum_t * SIGNATURE)
10981      SIGNER: Holds the signer's key
10982
10983      HASH_ALGO: The hash algorithm used
10984
10985      FLAGS: zero for now
10986
10987      HASH_DATA: holds the data to be signed
10988
10989      SIGNATURE: will contain newly allocated signature
10990
10991      This function will sign the given hashed data using a signature
10992      algorithm supported by the private key.  Signature algorithms are
10993      always used together with a hash functions.  Different hash
10994      functions may be used for the RSA algorithm, but only SHA-XXX for
10995      the DSA keys.
10996
10997      Use 'gnutls_x509_crt_get_preferred_hash_algorithm()' to determine
10998      the hash algorithm.
10999
11000      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11001      negative error value.
11002
11003      *Since:* 2.12.0
11004
11005 gnutls_protocol_get_id
11006 ----------------------
11007
11008  -- Function: gnutls_protocol_t gnutls_protocol_get_id (const char *
11009           NAME)
11010      NAME: is a protocol name
11011
11012      The names are compared in a case insensitive way.
11013
11014      *Returns:* an id of the specified protocol, or
11015      'GNUTLS_VERSION_UNKNOWN' on error.
11016
11017 gnutls_protocol_get_name
11018 ------------------------
11019
11020  -- Function: const char * gnutls_protocol_get_name (gnutls_protocol_t
11021           VERSION)
11022      VERSION: is a (gnutls) version number
11023
11024      Convert a 'gnutls_protocol_t' value to a string.
11025
11026      *Returns:* a string that contains the name of the specified TLS
11027      version (e.g., "TLS1.0"), or 'NULL'.
11028
11029 gnutls_protocol_get_version
11030 ---------------------------
11031
11032  -- Function: gnutls_protocol_t gnutls_protocol_get_version
11033           (gnutls_session_t SESSION)
11034      SESSION: is a 'gnutls_session_t' structure.
11035
11036      Get TLS version, a 'gnutls_protocol_t' value.
11037
11038      *Returns:* the version of the currently used protocol.
11039
11040 gnutls_protocol_list
11041 --------------------
11042
11043  -- Function: const gnutls_protocol_t * gnutls_protocol_list ( VOID)
11044
11045      Get a list of supported protocols, e.g.  SSL 3.0, TLS 1.0 etc.
11046
11047      *Returns:* a zero-terminated list of 'gnutls_protocol_t' integers
11048      indicating the available protocols.
11049
11050 gnutls_protocol_set_priority
11051 ----------------------------
11052
11053  -- Function: int gnutls_protocol_set_priority (gnutls_session_t
11054           SESSION, const int * LIST)
11055      SESSION: is a 'gnutls_session_t' structure.
11056
11057      LIST: is a 0 terminated list of gnutls_protocol_t elements.
11058
11059      Sets the priority on the protocol versions supported by gnutls.
11060      This function actually enables or disables protocols.  Newer
11061      protocol versions always have highest priority.
11062
11063      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11064
11065 gnutls_psk_allocate_client_credentials
11066 --------------------------------------
11067
11068  -- Function: int gnutls_psk_allocate_client_credentials
11069           (gnutls_psk_client_credentials_t * SC)
11070      SC: is a pointer to a 'gnutls_psk_server_credentials_t' structure.
11071
11072      This structure is complex enough to manipulate directly thus this
11073      helper function is provided in order to allocate it.
11074
11075      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11076
11077 gnutls_psk_allocate_server_credentials
11078 --------------------------------------
11079
11080  -- Function: int gnutls_psk_allocate_server_credentials
11081           (gnutls_psk_server_credentials_t * SC)
11082      SC: is a pointer to a 'gnutls_psk_server_credentials_t' structure.
11083
11084      This structure is complex enough to manipulate directly thus this
11085      helper function is provided in order to allocate it.
11086
11087      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11088
11089 gnutls_psk_client_get_hint
11090 --------------------------
11091
11092  -- Function: const char * gnutls_psk_client_get_hint (gnutls_session_t
11093           SESSION)
11094      SESSION: is a gnutls session
11095
11096      The PSK identity hint may give the client help in deciding which
11097      username to use.  This should only be called in case of PSK
11098      authentication and in case of a client.
11099
11100      *Returns:* the identity hint of the peer, or 'NULL' in case of an
11101      error.
11102
11103      *Since:* 2.4.0
11104
11105 gnutls_psk_free_client_credentials
11106 ----------------------------------
11107
11108  -- Function: void gnutls_psk_free_client_credentials
11109           (gnutls_psk_client_credentials_t SC)
11110      SC: is a 'gnutls_psk_client_credentials_t' structure.
11111
11112      This structure is complex enough to manipulate directly thus this
11113      helper function is provided in order to free (deallocate) it.
11114
11115 gnutls_psk_free_server_credentials
11116 ----------------------------------
11117
11118  -- Function: void gnutls_psk_free_server_credentials
11119           (gnutls_psk_server_credentials_t SC)
11120      SC: is a 'gnutls_psk_server_credentials_t' structure.
11121
11122      This structure is complex enough to manipulate directly thus this
11123      helper function is provided in order to free (deallocate) it.
11124
11125 gnutls_psk_netconf_derive_key
11126 -----------------------------
11127
11128  -- Function: int gnutls_psk_netconf_derive_key (const char * PASSWORD,
11129           const char * PSK_IDENTITY, const char * PSK_IDENTITY_HINT,
11130           gnutls_datum_t * OUTPUT_KEY)
11131      PASSWORD: zero terminated string containing password.
11132
11133      PSK_IDENTITY: zero terminated string with PSK identity.
11134
11135      PSK_IDENTITY_HINT: zero terminated string with PSK identity hint.
11136
11137      OUTPUT_KEY: output variable, contains newly allocated *data
11138      pointer.
11139
11140      This function will derive a PSK key from a password, for use with
11141      the Netconf protocol.
11142
11143      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11144
11145      *Since:* 2.4.0
11146
11147      *Deprecated:* The need for this interface was dropped from the
11148      standard on publication as a RFC. The function works now but will
11149      return a hard failure in a future release.
11150
11151 gnutls_psk_server_get_username
11152 ------------------------------
11153
11154  -- Function: const char * gnutls_psk_server_get_username
11155           (gnutls_session_t SESSION)
11156      SESSION: is a gnutls session
11157
11158      This should only be called in case of PSK authentication and in
11159      case of a server.
11160
11161      *Returns:* the username of the peer, or 'NULL' in case of an error.
11162
11163 gnutls_psk_set_client_credentials_function
11164 ------------------------------------------
11165
11166  -- Function: void gnutls_psk_set_client_credentials_function
11167           (gnutls_psk_client_credentials_t CRED,
11168           gnutls_psk_client_credentials_function * FUNC)
11169      CRED: is a 'gnutls_psk_server_credentials_t' structure.
11170
11171      FUNC: is the callback function
11172
11173      This function can be used to set a callback to retrieve the
11174      username and password for client PSK authentication.  The
11175      callback's function form is: int (*callback)(gnutls_session_t,
11176      char** username, gnutls_datum_t* key);
11177
11178      The 'username' and 'key'->data must be allocated using
11179      'gnutls_malloc()'.  'username' should be ASCII strings or UTF-8
11180      strings prepared using the "SASLprep" profile of "stringprep".
11181
11182      The callback function will be called once per handshake.
11183
11184      The callback function should return 0 on success.  -1 indicates an
11185      error.
11186
11187 gnutls_psk_set_client_credentials
11188 ---------------------------------
11189
11190  -- Function: int gnutls_psk_set_client_credentials
11191           (gnutls_psk_client_credentials_t RES, const char * USERNAME,
11192           const gnutls_datum_t * KEY, gnutls_psk_key_flags FLAGS)
11193      RES: is a 'gnutls_psk_client_credentials_t' structure.
11194
11195      USERNAME: is the user's zero-terminated userid
11196
11197      KEY: is the user's key
11198
11199      This function sets the username and password, in a
11200      gnutls_psk_client_credentials_t structure.  Those will be used in
11201      PSK authentication.  'username' should be an ASCII string or UTF-8
11202      strings prepared using the "SASLprep" profile of "stringprep".  The
11203      key can be either in raw byte format or in Hex format (without the
11204      0x prefix).
11205
11206      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11207
11208 gnutls_psk_set_params_function
11209 ------------------------------
11210
11211  -- Function: void gnutls_psk_set_params_function
11212           (gnutls_psk_server_credentials_t RES, gnutls_params_function *
11213           FUNC)
11214      RES: is a gnutls_psk_server_credentials_t structure
11215
11216      FUNC: is the function to be called
11217
11218      This function will set a callback in order for the server to get
11219      the Diffie-Hellman or RSA parameters for PSK authentication.  The
11220      callback should return zero on success.
11221
11222 gnutls_psk_set_server_credentials_file
11223 --------------------------------------
11224
11225  -- Function: int gnutls_psk_set_server_credentials_file
11226           (gnutls_psk_server_credentials_t RES, const char *
11227           PASSWORD_FILE)
11228      RES: is a 'gnutls_psk_server_credentials_t' structure.
11229
11230      PASSWORD_FILE: is the PSK password file (passwd.psk)
11231
11232      This function sets the password file, in a
11233      'gnutls_psk_server_credentials_t' structure.  This password file
11234      holds usernames and keys and will be used for PSK authentication.
11235
11236      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11237
11238 gnutls_psk_set_server_credentials_function
11239 ------------------------------------------
11240
11241  -- Function: void gnutls_psk_set_server_credentials_function
11242           (gnutls_psk_server_credentials_t CRED,
11243           gnutls_psk_server_credentials_function * FUNC)
11244      CRED: is a 'gnutls_psk_server_credentials_t' structure.
11245
11246      FUNC: is the callback function
11247
11248      This function can be used to set a callback to retrieve the user's
11249      PSK credentials.  The callback's function form is: int
11250      (*callback)(gnutls_session_t, const char* username, gnutls_datum_t*
11251      key);
11252
11253      'username' contains the actual username.  The 'key' must be filled
11254      in using the 'gnutls_malloc()'.
11255
11256      In case the callback returned a negative number then gnutls will
11257      assume that the username does not exist.
11258
11259      The callback function will only be called once per handshake.  The
11260      callback function should return 0 on success, while -1 indicates an
11261      error.
11262
11263 gnutls_psk_set_server_credentials_hint
11264 --------------------------------------
11265
11266  -- Function: int gnutls_psk_set_server_credentials_hint
11267           (gnutls_psk_server_credentials_t RES, const char * HINT)
11268      RES: is a 'gnutls_psk_server_credentials_t' structure.
11269
11270      HINT: is the PSK identity hint string
11271
11272      This function sets the identity hint, in a
11273      'gnutls_psk_server_credentials_t' structure.  This hint is sent to
11274      the client to help it chose a good PSK credential (i.e., username
11275      and password).
11276
11277      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
11278
11279      *Since:* 2.4.0
11280
11281 gnutls_psk_set_server_dh_params
11282 -------------------------------
11283
11284  -- Function: void gnutls_psk_set_server_dh_params
11285           (gnutls_psk_server_credentials_t RES, gnutls_dh_params_t
11286           DH_PARAMS)
11287      RES: is a gnutls_psk_server_credentials_t structure
11288
11289      DH_PARAMS: is a structure that holds Diffie-Hellman parameters.
11290
11291      This function will set the Diffie-Hellman parameters for an
11292      anonymous server to use.  These parameters will be used in
11293      Diffie-Hellman exchange with PSK cipher suites.
11294
11295 gnutls_psk_set_server_params_function
11296 -------------------------------------
11297
11298  -- Function: void gnutls_psk_set_server_params_function
11299           (gnutls_psk_server_credentials_t RES, gnutls_params_function *
11300           FUNC)
11301      RES: is a 'gnutls_certificate_credentials_t' structure
11302
11303      FUNC: is the function to be called
11304
11305      This function will set a callback in order for the server to get
11306      the Diffie-Hellman parameters for PSK authentication.  The callback
11307      should return zero on success.
11308
11309 gnutls_pubkey_deinit
11310 --------------------
11311
11312  -- Function: void gnutls_pubkey_deinit (gnutls_pubkey_t KEY)
11313      KEY: The structure to be deinitialized
11314
11315      This function will deinitialize a public key structure.
11316
11317 gnutls_pubkey_export
11318 --------------------
11319
11320  -- Function: int gnutls_pubkey_export (gnutls_pubkey_t KEY,
11321           gnutls_x509_crt_fmt_t FORMAT, void * OUTPUT_DATA, size_t *
11322           OUTPUT_DATA_SIZE)
11323      KEY: Holds the certificate
11324
11325      FORMAT: the format of output params.  One of PEM or DER.
11326
11327      OUTPUT_DATA: will contain a certificate PEM or DER encoded
11328
11329      OUTPUT_DATA_SIZE: holds the size of output_data (and will be
11330      replaced by the actual size of parameters)
11331
11332      This function will export the certificate to DER or PEM format.
11333
11334      If the buffer provided is not long enough to hold the output, then
11335      *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
11336      be returned.
11337
11338      If the structure is PEM encoded, it will have a header of "BEGIN
11339      CERTIFICATE".
11340
11341      *Return value:* In case of failure a negative value will be
11342      returned, and 0 on success.
11343
11344 gnutls_pubkey_get_key_id
11345 ------------------------
11346
11347  -- Function: int gnutls_pubkey_get_key_id (gnutls_pubkey_t KEY,
11348           unsigned int FLAGS, unsigned char * OUTPUT_DATA, size_t *
11349           OUTPUT_DATA_SIZE)
11350      KEY: Holds the public key
11351
11352      FLAGS: should be 0 for now
11353
11354      OUTPUT_DATA: will contain the key ID
11355
11356      OUTPUT_DATA_SIZE: holds the size of output_data (and will be
11357      replaced by the actual size of parameters)
11358
11359      This function will return a unique ID the depends on the public key
11360      parameters.  This ID can be used in checking whether a certificate
11361      corresponds to the given public key.
11362
11363      If the buffer provided is not long enough to hold the output, then
11364      *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
11365      be returned.  The output will normally be a SHA-1 hash output,
11366      which is 20 bytes.
11367
11368      *Return value:* In case of failure a negative value will be
11369      returned, and 0 on success.
11370
11371 gnutls_pubkey_get_key_usage
11372 ---------------------------
11373
11374  -- Function: int gnutls_pubkey_get_key_usage (gnutls_pubkey_t KEY,
11375           unsigned int * USAGE)
11376      KEY: should contain a 'gnutls_pubkey_t' structure
11377
11378      USAGE: If set will return the number of bits of the parameters (may
11379      be NULL)
11380
11381      This function will return the key usage of the public key.
11382
11383      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11384      negative error value.
11385
11386 gnutls_pubkey_get_pk_algorithm
11387 ------------------------------
11388
11389  -- Function: int gnutls_pubkey_get_pk_algorithm (gnutls_pubkey_t KEY,
11390           unsigned int * BITS)
11391      KEY: should contain a 'gnutls_pubkey_t' structure
11392
11393      BITS: If set will return the number of bits of the parameters (may
11394      be NULL)
11395
11396      This function will return the public key algorithm of a public key
11397      and if possible will return a number of bits that indicates the
11398      security parameter of the key.
11399
11400      *Returns:* a member of the 'gnutls_pk_algorithm_t' enumeration on
11401      success, or a negative value on error.
11402
11403 gnutls_pubkey_get_pk_dsa_raw
11404 ----------------------------
11405
11406  -- Function: int gnutls_pubkey_get_pk_dsa_raw (gnutls_pubkey_t KEY,
11407           gnutls_datum_t * P, gnutls_datum_t * Q, gnutls_datum_t * G,
11408           gnutls_datum_t * Y)
11409      KEY: Holds the public key
11410
11411      P: will hold the p
11412
11413      Q: will hold the q
11414
11415      G: will hold the g
11416
11417      Y: will hold the y
11418
11419      This function will export the DSA public key's parameters found in
11420      the given certificate.  The new parameters will be allocated using
11421      'gnutls_malloc()' and will be stored in the appropriate datum.
11422
11423      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
11424
11425 gnutls_pubkey_get_pk_rsa_raw
11426 ----------------------------
11427
11428  -- Function: int gnutls_pubkey_get_pk_rsa_raw (gnutls_pubkey_t KEY,
11429           gnutls_datum_t * M, gnutls_datum_t * E)
11430      KEY: Holds the certificate
11431
11432      M: will hold the modulus
11433
11434      E: will hold the public exponent
11435
11436      This function will export the RSA public key's parameters found in
11437      the given structure.  The new parameters will be allocated using
11438      'gnutls_malloc()' and will be stored in the appropriate datum.
11439
11440      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
11441
11442 gnutls_pubkey_get_preferred_hash_algorithm
11443 ------------------------------------------
11444
11445  -- Function: int gnutls_pubkey_get_preferred_hash_algorithm
11446           (gnutls_pubkey_t KEY, gnutls_digest_algorithm_t * HASH,
11447           unsigned int * MAND)
11448      KEY: Holds the certificate
11449
11450      HASH: The result of the call with the hash algorithm used for
11451      signature
11452
11453      MAND: If non zero it means that the algorithm MUST use this hash.
11454      May be NULL.
11455
11456      This function will read the certifcate and return the appropriate
11457      digest algorithm to use for signing with this certificate.  Some
11458      certificates (i.e.  DSA might not be able to sign without the
11459      preferred algorithm).
11460
11461      *Returns:* the 0 if the hash algorithm is found.  A negative value
11462      is returned on error.
11463
11464      *Since:* 2.11.0
11465
11466 gnutls_pubkey_get_verify_algorithm
11467 ----------------------------------
11468
11469  -- Function: int gnutls_pubkey_get_verify_algorithm (gnutls_pubkey_t
11470           KEY, const gnutls_datum_t * SIGNATURE,
11471           gnutls_digest_algorithm_t * HASH)
11472      KEY: Holds the certificate
11473
11474      SIGNATURE: contains the signature
11475
11476      HASH: The result of the call with the hash algorithm used for
11477      signature
11478
11479      This function will read the certifcate and the signed data to
11480      determine the hash algorithm used to generate the signature.
11481
11482      *Returns:* the 0 if the hash algorithm is found.  A negative value
11483      is returned on error.
11484
11485 gnutls_pubkey_import_dsa_raw
11486 ----------------------------
11487
11488  -- Function: int gnutls_pubkey_import_dsa_raw (gnutls_pubkey_t KEY,
11489           const gnutls_datum_t * P, const gnutls_datum_t * Q, const
11490           gnutls_datum_t * G, const gnutls_datum_t * Y)
11491      KEY: The structure to store the parsed key
11492
11493      P: holds the p
11494
11495      Q: holds the q
11496
11497      G: holds the g
11498
11499      Y: holds the y
11500
11501      This function will convert the given DSA raw parameters to the
11502      native 'gnutls_pubkey_t' format.  The output will be stored in
11503      'key'.
11504
11505      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11506      negative error value.
11507
11508 gnutls_pubkey_import_openpgp
11509 ----------------------------
11510
11511  -- Function: int gnutls_pubkey_import_openpgp (gnutls_pubkey_t KEY,
11512           gnutls_openpgp_crt_t CRT, unsigned int FLAGS)
11513      KEY: The public key
11514
11515      CRT: The certificate to be imported
11516
11517      FLAGS: should be zero
11518
11519      This function will import the given public key to the abstract
11520      'gnutls_pubkey_t' structure.  The subkey set as preferred will be
11521      imported or the master key otherwise.
11522
11523      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11524      negative error value.
11525
11526 gnutls_pubkey_import_pkcs11_url
11527 -------------------------------
11528
11529  -- Function: int gnutls_pubkey_import_pkcs11_url (gnutls_pubkey_t KEY,
11530           const char * URL, unsigned int FLAGS)
11531      KEY: A key of type 'gnutls_pubkey_t'
11532
11533      URL: A PKCS 11 url
11534
11535      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
11536
11537      This function will import a PKCS 11 certificate to a
11538      'gnutls_pubkey_t' structure.
11539
11540      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11541      negative error value.
11542
11543 gnutls_pubkey_import_pkcs11
11544 ---------------------------
11545
11546  -- Function: int gnutls_pubkey_import_pkcs11 (gnutls_pubkey_t KEY,
11547           gnutls_pkcs11_obj_t OBJ, unsigned int FLAGS)
11548      KEY: The public key
11549
11550      OBJ: The parameters to be imported
11551
11552      FLAGS: should be zero
11553
11554      This function will import the given public key to the abstract
11555      'gnutls_pubkey_t' structure.
11556
11557      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11558      negative error value.
11559
11560 gnutls_pubkey_import_privkey
11561 ----------------------------
11562
11563  -- Function: int gnutls_pubkey_import_privkey (gnutls_pubkey_t KEY,
11564           gnutls_privkey_t PKEY, unsigned int USAGE, unsigned int FLAGS)
11565      KEY: The public key
11566
11567      PKEY: The private key
11568
11569      USAGE: GNUTLS_KEY_* key usage flags.
11570
11571      FLAGS: should be zero
11572
11573      This function will import the given public key to the abstract
11574      'gnutls_pubkey_t' structure.
11575
11576      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11577      negative error value.
11578
11579      *Since:* 2.12.0
11580
11581 gnutls_pubkey_import_rsa_raw
11582 ----------------------------
11583
11584  -- Function: int gnutls_pubkey_import_rsa_raw (gnutls_pubkey_t KEY,
11585           const gnutls_datum_t * M, const gnutls_datum_t * E)
11586      KEY: Is a structure will hold the parameters
11587
11588      M: holds the modulus
11589
11590      E: holds the public exponent
11591
11592      This function will replace the parameters in the given structure.
11593      The new parameters should be stored in the appropriate
11594      gnutls_datum.
11595
11596      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
11597      code.
11598
11599 gnutls_pubkey_import_x509
11600 -------------------------
11601
11602  -- Function: int gnutls_pubkey_import_x509 (gnutls_pubkey_t KEY,
11603           gnutls_x509_crt_t CRT, unsigned int FLAGS)
11604      KEY: The public key
11605
11606      CRT: The certificate to be imported
11607
11608      FLAGS: should be zero
11609
11610      This function will import the given public key to the abstract
11611      'gnutls_pubkey_t' structure.
11612
11613      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11614      negative error value.
11615
11616 gnutls_pubkey_import
11617 --------------------
11618
11619  -- Function: int gnutls_pubkey_import (gnutls_pubkey_t KEY, const
11620           gnutls_datum_t * DATA, gnutls_x509_crt_fmt_t FORMAT)
11621      KEY: The structure to store the parsed public key.
11622
11623      DATA: The DER or PEM encoded certificate.
11624
11625      FORMAT: One of DER or PEM
11626
11627      This function will convert the given DER or PEM encoded Public key
11628      to the native gnutls_pubkey_t format.The output will be stored * in
11629       key.  If the Certificate is PEM encoded it should have a header of
11630      "PUBLIC KEY".
11631
11632      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11633      negative error value.
11634
11635 gnutls_pubkey_init
11636 ------------------
11637
11638  -- Function: int gnutls_pubkey_init (gnutls_pubkey_t * KEY)
11639      KEY: The structure to be initialized
11640
11641      This function will initialize an public key structure.
11642
11643      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11644      negative error value.
11645
11646 gnutls_pubkey_set_key_usage
11647 ---------------------------
11648
11649  -- Function: int gnutls_pubkey_set_key_usage (gnutls_pubkey_t KEY,
11650           unsigned int USAGE)
11651      KEY: a certificate of type 'gnutls_x509_crt_t'
11652
11653      USAGE: an ORed sequence of the GNUTLS_KEY_* elements.
11654
11655      This function will set the key usage flags of the public key.  This
11656      is only useful if the key is to be exported to a certificate or
11657      certificate request.
11658
11659      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
11660      negative error value.
11661
11662 gnutls_pubkey_verify_data
11663 -------------------------
11664
11665  -- Function: int gnutls_pubkey_verify_data (gnutls_pubkey_t PUBKEY,
11666           unsigned int FLAGS, const gnutls_datum_t * DATA, const
11667           gnutls_datum_t * SIGNATURE)
11668      PUBKEY: Holds the public key
11669
11670      FLAGS: should be 0 for now
11671
11672      DATA: holds the data to be signed
11673
11674      SIGNATURE: contains the signature
11675
11676      This function will verify the given signed data, using the
11677      parameters from the certificate.
11678
11679      *Returns:* In case of a verification failure
11680      'GNUTLS_E_PK_SIG_VERIFY_FAILED' is returned, and a positive code on
11681      success.
11682
11683      *Since:* 2.12.0
11684
11685 gnutls_pubkey_verify_hash
11686 -------------------------
11687
11688  -- Function: int gnutls_pubkey_verify_hash (gnutls_pubkey_t KEY,
11689           unsigned int FLAGS, const gnutls_datum_t * HASH, const
11690           gnutls_datum_t * SIGNATURE)
11691      KEY: Holds the certificate
11692
11693      FLAGS: should be 0 for now
11694
11695      HASH: holds the hash digest to be verified
11696
11697      SIGNATURE: contains the signature
11698
11699      This function will verify the given signed digest, using the
11700      parameters from the certificate.
11701
11702      *Returns:* In case of a verification failure
11703      'GNUTLS_E_PK_SIG_VERIFY_FAILED' is returned, and a positive code on
11704      success.
11705
11706 gnutls_record_check_pending
11707 ---------------------------
11708
11709  -- Function: size_t gnutls_record_check_pending (gnutls_session_t
11710           SESSION)
11711      SESSION: is a 'gnutls_session_t' structure.
11712
11713      This function checks if there are any data to receive in the gnutls
11714      buffers.
11715
11716      *Returns:* the size of that data or 0.
11717
11718 gnutls_record_disable_padding
11719 -----------------------------
11720
11721  -- Function: void gnutls_record_disable_padding (gnutls_session_t
11722           SESSION)
11723      SESSION: is a 'gnutls_session_t' structure.
11724
11725      Used to disabled padding in TLS 1.0 and above.  Normally you do not
11726      need to use this function, but there are buggy clients that
11727      complain if a server pads the encrypted data.  This of course will
11728      disable protection against statistical attacks on the data.
11729
11730      Normally only servers that require maximum compatibility with
11731      everything out there, need to call this function.
11732
11733 gnutls_record_get_direction
11734 ---------------------------
11735
11736  -- Function: int gnutls_record_get_direction (gnutls_session_t SESSION)
11737      SESSION: is a 'gnutls_session_t' structure.
11738
11739      This function provides information about the internals of the
11740      record protocol and is only useful if a prior gnutls function call
11741      (e.g.  'gnutls_handshake()') was interrupted for some reason, that
11742      is, if a function returned 'GNUTLS_E_INTERRUPTED' or
11743      'GNUTLS_E_AGAIN'.  In such a case, you might want to call
11744      'select()' or 'poll()' before calling the interrupted gnutls
11745      function again.  To tell you whether a file descriptor should be
11746      selected for either reading or writing,
11747      'gnutls_record_get_direction()' returns 0 if the interrupted
11748      function was trying to read data, and 1 if it was trying to write
11749      data.
11750
11751      *Returns:* 0 if trying to read data, 1 if trying to write data.
11752
11753 gnutls_record_get_max_size
11754 --------------------------
11755
11756  -- Function: size_t gnutls_record_get_max_size (gnutls_session_t
11757           SESSION)
11758      SESSION: is a 'gnutls_session_t' structure.
11759
11760      Get the record size.  The maximum record size is negotiated by the
11761      client after the first handshake message.
11762
11763      *Returns:* The maximum record packet size in this connection.
11764
11765 gnutls_record_recv
11766 ------------------
11767
11768  -- Function: ssize_t gnutls_record_recv (gnutls_session_t SESSION, void
11769           * DATA, size_t SIZEOFDATA)
11770      SESSION: is a 'gnutls_session_t' structure.
11771
11772      DATA: the buffer that the data will be read into
11773
11774      SIZEOFDATA: the number of requested bytes
11775
11776      This function has the similar semantics with 'recv()'.  The only
11777      difference is that it accepts a GnuTLS session, and uses different
11778      error codes.
11779
11780      In the special case that a server requests a renegotiation, the
11781      client may receive an error code of 'GNUTLS_E_REHANDSHAKE'.  This
11782      message may be simply ignored, replied with an alert
11783      'GNUTLS_A_NO_RENEGOTIATION', or replied with a new handshake,
11784      depending on the client's will.
11785
11786      If 'EINTR' is returned by the internal push function (the default
11787      is 'recv()') then 'GNUTLS_E_INTERRUPTED' will be returned.  If
11788      'GNUTLS_E_INTERRUPTED' or 'GNUTLS_E_AGAIN' is returned, you must
11789      call this function again to get the data.  See also
11790      'gnutls_record_get_direction()'.
11791
11792      A server may also receive 'GNUTLS_E_REHANDSHAKE' when a client has
11793      initiated a handshake.  In that case the server can only initiate a
11794      handshake or terminate the connection.
11795
11796      *Returns:* the number of bytes received and zero on EOF. A negative
11797      error code is returned in case of an error.  The number of bytes
11798      received might be less than 'sizeofdata'.
11799
11800 gnutls_record_send
11801 ------------------
11802
11803  -- Function: ssize_t gnutls_record_send (gnutls_session_t SESSION,
11804           const void * DATA, size_t SIZEOFDATA)
11805      SESSION: is a 'gnutls_session_t' structure.
11806
11807      DATA: contains the data to send
11808
11809      SIZEOFDATA: is the length of the data
11810
11811      This function has the similar semantics with 'send()'.  The only
11812      difference is that it accepts a GnuTLS session, and uses different
11813      error codes.
11814
11815      Note that if the send buffer is full, 'send()' will block this
11816      function.  See the 'send()' documentation for full information.
11817      You can replace the default push function by using
11818      'gnutls_transport_set_ptr2()' with a call to 'send()' with a
11819      MSG_DONTWAIT flag if blocking is a problem.
11820
11821      If the EINTR is returned by the internal push function (the default
11822      is 'send()'} then 'GNUTLS_E_INTERRUPTED' will be returned.  If
11823      'GNUTLS_E_INTERRUPTED' or 'GNUTLS_E_AGAIN' is returned, you must
11824      call this function again, with the same parameters; alternatively
11825      you could provide a 'NULL' pointer for data, and 0 for size.  cf.
11826      'gnutls_record_get_direction()'.
11827
11828      *Returns:* the number of bytes sent, or a negative error code.  The
11829      number of bytes sent might be less than 'sizeofdata'.  The maximum
11830      number of bytes this function can send in a single call depends on
11831      the negotiated maximum record size.
11832
11833 gnutls_record_set_max_size
11834 --------------------------
11835
11836  -- Function: ssize_t gnutls_record_set_max_size (gnutls_session_t
11837           SESSION, size_t SIZE)
11838      SESSION: is a 'gnutls_session_t' structure.
11839
11840      SIZE: is the new size
11841
11842      This function sets the maximum record packet size in this
11843      connection.  This property can only be set to clients.  The server
11844      may choose not to accept the requested size.
11845
11846      Acceptable values are 512(=2^9), 1024(=2^10), 2048(=2^11) and
11847      4096(=2^12).  The requested record size does get in effect
11848      immediately only while sending data.  The receive part will take
11849      effect after a successful handshake.
11850
11851      This function uses a TLS extension called 'max record size'.  Not
11852      all TLS implementations use or even understand this extension.
11853
11854      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
11855      otherwise an error code is returned.
11856
11857 gnutls_rehandshake
11858 ------------------
11859
11860  -- Function: int gnutls_rehandshake (gnutls_session_t SESSION)
11861      SESSION: is a 'gnutls_session_t' structure.
11862
11863      This function will renegotiate security parameters with the client.
11864      This should only be called in case of a server.
11865
11866      This message informs the peer that we want to renegotiate
11867      parameters (perform a handshake).
11868
11869      If this function succeeds (returns 0), you must call the
11870      'gnutls_handshake()' function in order to negotiate the new
11871      parameters.
11872
11873      Since TLS is full duplex some application data might have been sent
11874      during peer's processing of this message.  In that case one should
11875      call 'gnutls_record_recv()' until GNUTLS_E_REHANDSHAKE is returned
11876      to clear any pending data.  Care must be taken if rehandshake is
11877      mandatory to terminate if it does not start after some threshold.
11878
11879      If the client does not wish to renegotiate parameters he will
11880      should with an alert message, thus the return code will be
11881      'GNUTLS_E_WARNING_ALERT_RECEIVED' and the alert will be
11882      'GNUTLS_A_NO_RENEGOTIATION'.  A client may also choose to ignore
11883      this message.
11884
11885      *Returns:* 'GNUTLS_E_SUCCESS' on success, otherwise an error.
11886
11887 gnutls_rnd
11888 ----------
11889
11890  -- Function: int gnutls_rnd (gnutls_rnd_level_t LEVEL, void * DATA,
11891           size_t LEN)
11892      LEVEL: a security level
11893
11894      DATA: place to store random bytes
11895
11896      LEN: The requested size
11897
11898      This function will generate random data and store it to output
11899      buffer.
11900
11901      *Returns:* Zero or a negative value on error.
11902
11903 gnutls_rsa_export_get_modulus_bits
11904 ----------------------------------
11905
11906  -- Function: int gnutls_rsa_export_get_modulus_bits (gnutls_session_t
11907           SESSION)
11908      SESSION: is a gnutls session
11909
11910      Get the export RSA parameter's modulus size.
11911
11912      *Returns:* the bits used in the last RSA-EXPORT key exchange with
11913      the peer, or a negative value in case of error.
11914
11915 gnutls_rsa_export_get_pubkey
11916 ----------------------------
11917
11918  -- Function: int gnutls_rsa_export_get_pubkey (gnutls_session_t
11919           SESSION, gnutls_datum_t * EXPONENT, gnutls_datum_t * MODULUS)
11920      SESSION: is a gnutls session
11921
11922      EXPONENT: will hold the exponent.
11923
11924      MODULUS: will hold the modulus.
11925
11926      This function will return the peer's public key exponent and
11927      modulus used in the last RSA-EXPORT authentication.  The output
11928      parameters must be freed with 'gnutls_free()'.
11929
11930      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
11931      otherwise an error code is returned.
11932
11933 gnutls_rsa_params_cpy
11934 ---------------------
11935
11936  -- Function: int gnutls_rsa_params_cpy (gnutls_rsa_params_t DST,
11937           gnutls_rsa_params_t SRC)
11938      DST: Is the destination structure, which should be initialized.
11939
11940      SRC: Is the source structure
11941
11942      This function will copy the RSA parameters structure from source to
11943      destination.
11944
11945      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
11946      code.
11947
11948 gnutls_rsa_params_deinit
11949 ------------------------
11950
11951  -- Function: void gnutls_rsa_params_deinit (gnutls_rsa_params_t
11952           RSA_PARAMS)
11953      RSA_PARAMS: Is a structure that holds the parameters
11954
11955      This function will deinitialize the RSA parameters structure.
11956
11957 gnutls_rsa_params_export_pkcs1
11958 ------------------------------
11959
11960  -- Function: int gnutls_rsa_params_export_pkcs1 (gnutls_rsa_params_t
11961           PARAMS, gnutls_x509_crt_fmt_t FORMAT, unsigned char *
11962           PARAMS_DATA, size_t * PARAMS_DATA_SIZE)
11963      PARAMS: Holds the RSA parameters
11964
11965      FORMAT: the format of output params.  One of PEM or DER.
11966
11967      PARAMS_DATA: will contain a PKCS1 RSAPublicKey structure PEM or DER
11968      encoded
11969
11970      PARAMS_DATA_SIZE: holds the size of params_data (and will be
11971      replaced by the actual size of parameters)
11972
11973      This function will export the given RSA parameters to a PKCS1
11974      RSAPublicKey structure.  If the buffer provided is not long enough
11975      to hold the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be
11976      returned.
11977
11978      If the structure is PEM encoded, it will have a header of "BEGIN
11979      RSA PRIVATE KEY".
11980
11981      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
11982      code.
11983
11984 gnutls_rsa_params_export_raw
11985 ----------------------------
11986
11987  -- Function: int gnutls_rsa_params_export_raw (gnutls_rsa_params_t
11988           PARAMS, gnutls_datum_t * M, gnutls_datum_t * E, gnutls_datum_t
11989           * D, gnutls_datum_t * P, gnutls_datum_t * Q, gnutls_datum_t *
11990           U, unsigned int * BITS)
11991      PARAMS: a structure that holds the rsa parameters
11992
11993      M: will hold the modulus
11994
11995      E: will hold the public exponent
11996
11997      D: will hold the private exponent
11998
11999      P: will hold the first prime (p)
12000
12001      Q: will hold the second prime (q)
12002
12003      U: will hold the coefficient
12004
12005      BITS: if non null will hold the prime's number of bits
12006
12007      This function will export the RSA parameters found in the given
12008      structure.  The new parameters will be allocated using
12009      'gnutls_malloc()' and will be stored in the appropriate datum.
12010
12011      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
12012      code.
12013
12014 gnutls_rsa_params_generate2
12015 ---------------------------
12016
12017  -- Function: int gnutls_rsa_params_generate2 (gnutls_rsa_params_t
12018           PARAMS, unsigned int BITS)
12019      PARAMS: The structure where the parameters will be stored
12020
12021      BITS: is the prime's number of bits
12022
12023      This function will generate new temporary RSA parameters for use in
12024      RSA-EXPORT ciphersuites.  This function is normally slow.
12025
12026      Note that if the parameters are to be used in export cipher suites
12027      the bits value should be 512 or less.  Also note that the
12028      generation of new RSA parameters is only useful to servers.
12029      Clients use the parameters sent by the server, thus it's no use
12030      calling this in client side.
12031
12032      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
12033      code.
12034
12035 gnutls_rsa_params_import_pkcs1
12036 ------------------------------
12037
12038  -- Function: int gnutls_rsa_params_import_pkcs1 (gnutls_rsa_params_t
12039           PARAMS, const gnutls_datum_t * PKCS1_PARAMS,
12040           gnutls_x509_crt_fmt_t FORMAT)
12041      PARAMS: A structure where the parameters will be copied to
12042
12043      PKCS1_PARAMS: should contain a PKCS1 RSAPublicKey structure PEM or
12044      DER encoded
12045
12046      FORMAT: the format of params.  PEM or DER.
12047
12048      This function will extract the RSAPublicKey found in a PKCS1
12049      formatted structure.
12050
12051      If the structure is PEM encoded, it should have a header of "BEGIN
12052      RSA PRIVATE KEY".
12053
12054      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
12055      code.
12056
12057 gnutls_rsa_params_import_raw
12058 ----------------------------
12059
12060  -- Function: int gnutls_rsa_params_import_raw (gnutls_rsa_params_t
12061           RSA_PARAMS, const gnutls_datum_t * M, const gnutls_datum_t *
12062           E, const gnutls_datum_t * D, const gnutls_datum_t * P, const
12063           gnutls_datum_t * Q, const gnutls_datum_t * U)
12064      RSA_PARAMS: Is a structure will hold the parameters
12065
12066      M: holds the modulus
12067
12068      E: holds the public exponent
12069
12070      D: holds the private exponent
12071
12072      P: holds the first prime (p)
12073
12074      Q: holds the second prime (q)
12075
12076      U: holds the coefficient
12077
12078      This function will replace the parameters in the given structure.
12079      The new parameters should be stored in the appropriate
12080      gnutls_datum.
12081
12082      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
12083      code.
12084
12085 gnutls_rsa_params_init
12086 ----------------------
12087
12088  -- Function: int gnutls_rsa_params_init (gnutls_rsa_params_t *
12089           RSA_PARAMS)
12090      RSA_PARAMS: Is a structure that will hold the parameters
12091
12092      This function will initialize the temporary RSA parameters
12093      structure.
12094
12095      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an negative error
12096      code.
12097
12098 gnutls_safe_renegotiation_status
12099 --------------------------------
12100
12101  -- Function: int gnutls_safe_renegotiation_status (gnutls_session_t
12102           SESSION)
12103      SESSION: is a 'gnutls_session_t' structure.
12104
12105      Can be used to check whether safe renegotiation is being used in
12106      the current session.
12107
12108      *Returns:* 0 when safe renegotiation is not used and non zero when
12109      safe renegotiation is used.
12110
12111      *Since:* 2.10.0
12112
12113 gnutls_sec_param_get_name
12114 -------------------------
12115
12116  -- Function: const char * gnutls_sec_param_get_name (gnutls_sec_param_t
12117           PARAM)
12118      PARAM: is a security parameter
12119
12120      Convert a 'gnutls_sec_param_t' value to a string.
12121
12122      *Returns:* a pointer to a string that contains the name of the
12123      specified public key algorithm, or 'NULL'.
12124
12125 gnutls_sec_param_to_pk_bits
12126 ---------------------------
12127
12128  -- Function: unsigned int gnutls_sec_param_to_pk_bits
12129           (gnutls_pk_algorithm_t ALGO, gnutls_sec_param_t PARAM)
12130      ALGO: is a public key algorithm
12131
12132      PARAM: is a security parameter
12133
12134      When generating private and public key pairs a difficult question
12135      is which size of "bits" the modulus will be in RSA and the group
12136      size in DSA. The easy answer is 1024, which is also wrong.  This
12137      function will convert a human understandable security parameter to
12138      an appropriate size for the specific algorithm.
12139
12140      *Returns:* The number of bits, or zero.
12141
12142 gnutls_server_name_get
12143 ----------------------
12144
12145  -- Function: int gnutls_server_name_get (gnutls_session_t SESSION, void
12146           * DATA, size_t * DATA_LENGTH, unsigned int * TYPE, unsigned
12147           int INDX)
12148      SESSION: is a 'gnutls_session_t' structure.
12149
12150      DATA: will hold the data
12151
12152      DATA_LENGTH: will hold the data length.  Must hold the maximum size
12153      of data.
12154
12155      TYPE: will hold the server name indicator type
12156
12157      INDX: is the index of the server_name
12158
12159      This function will allow you to get the name indication (if any), a
12160      client has sent.  The name indication may be any of the enumeration
12161      gnutls_server_name_type_t.
12162
12163      If 'type' is GNUTLS_NAME_DNS, then this function is to be used by
12164      servers that support virtual hosting, and the data will be a null
12165      terminated UTF-8 string.
12166
12167      If 'data' has not enough size to hold the server name
12168      GNUTLS_E_SHORT_MEMORY_BUFFER is returned, and 'data_length' will
12169      hold the required size.
12170
12171      'index' is used to retrieve more than one server names (if sent by
12172      the client).  The first server name has an index of 0, the second 1
12173      and so on.  If no name with the given index exists
12174      GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
12175
12176      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
12177      otherwise an error code is returned.
12178
12179 gnutls_server_name_set
12180 ----------------------
12181
12182  -- Function: int gnutls_server_name_set (gnutls_session_t SESSION,
12183           gnutls_server_name_type_t TYPE, const void * NAME, size_t
12184           NAME_LENGTH)
12185      SESSION: is a 'gnutls_session_t' structure.
12186
12187      TYPE: specifies the indicator type
12188
12189      NAME: is a string that contains the server name.
12190
12191      NAME_LENGTH: holds the length of name
12192
12193      This function is to be used by clients that want to inform (via a
12194      TLS extension mechanism) the server of the name they connected to.
12195      This should be used by clients that connect to servers that do
12196      virtual hosting.
12197
12198      The value of 'name' depends on the 'type' type.  In case of
12199      'GNUTLS_NAME_DNS', an ASCII zero-terminated domain name string,
12200      without the trailing dot, is expected.  IPv4 or IPv6 addresses are
12201      not permitted.
12202
12203      *Returns:* On success, 'GNUTLS_E_SUCCESS' (zero) is returned,
12204      otherwise an error code is returned.
12205
12206 gnutls_session_channel_binding
12207 ------------------------------
12208
12209  -- Function: int gnutls_session_channel_binding (gnutls_session_t
12210           SESSION, gnutls_channel_binding_t CBTYPE, gnutls_datum_t * CB)
12211      SESSION: is a 'gnutls_session_t' structure.
12212
12213      CBTYPE: an 'gnutls_channel_binding_t' enumeration type
12214
12215      CB: output buffer array with data
12216
12217      Extract given channel binding data of the 'cbtype' (e.g.,
12218      'GNUTLS_CB_TLS_UNIQUE') type.
12219
12220      *Returns:* 'GNUTLS_E_SUCCESS' on success,
12221      'GNUTLS_E_UNIMPLEMENTED_FEATURE' if the 'cbtype' is unsupported,
12222      'GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE' if the data is not
12223      currently available, or an error code.
12224
12225      *Since:* 2.12.0
12226
12227 gnutls_session_enable_compatibility_mode
12228 ----------------------------------------
12229
12230  -- Function: void gnutls_session_enable_compatibility_mode
12231           (gnutls_session_t SESSION)
12232      SESSION: is a 'gnutls_session_t' structure.
12233
12234      This function can be used to disable certain (security) features in
12235      TLS in order to maintain maximum compatibility with buggy clients.
12236      It is equivalent to calling: 'gnutls_record_disable_padding()'
12237
12238      Normally only servers that require maximum compatibility with
12239      everything out there, need to call this function.
12240
12241 gnutls_session_get_data2
12242 ------------------------
12243
12244  -- Function: int gnutls_session_get_data2 (gnutls_session_t SESSION,
12245           gnutls_datum_t * DATA)
12246      SESSION: is a 'gnutls_session_t' structure.
12247
12248      DATA: is a pointer to a datum that will hold the session.
12249
12250      Returns all session parameters, in order to support resuming.  The
12251      client should call this, and keep the returned session, if he wants
12252      to resume that current version later by calling
12253      'gnutls_session_set_data()'.  This function must be called after a
12254      successful handshake.  The returned datum must be freed with
12255      'gnutls_free()'.
12256
12257      Resuming sessions is really useful and speedups connections after a
12258      successful one.
12259
12260      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
12261      otherwise an error code is returned.
12262
12263 gnutls_session_get_data
12264 -----------------------
12265
12266  -- Function: int gnutls_session_get_data (gnutls_session_t SESSION,
12267           void * SESSION_DATA, size_t * SESSION_DATA_SIZE)
12268      SESSION: is a 'gnutls_session_t' structure.
12269
12270      SESSION_DATA: is a pointer to space to hold the session.
12271
12272      SESSION_DATA_SIZE: is the session_data's size, or it will be set by
12273      the function.
12274
12275      Returns all session parameters, in order to support resuming.  The
12276      client should call this, and keep the returned session, if he wants
12277      to resume that current version later by calling
12278      'gnutls_session_set_data()' This function must be called after a
12279      successful handshake.
12280
12281      Resuming sessions is really useful and speedups connections after a
12282      successful one.
12283
12284      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
12285      otherwise an error code is returned.
12286
12287 gnutls_session_get_id
12288 ---------------------
12289
12290  -- Function: int gnutls_session_get_id (gnutls_session_t SESSION, void
12291           * SESSION_ID, size_t * SESSION_ID_SIZE)
12292      SESSION: is a 'gnutls_session_t' structure.
12293
12294      SESSION_ID: is a pointer to space to hold the session id.
12295
12296      SESSION_ID_SIZE: is the session id's size, or it will be set by the
12297      function.
12298
12299      Returns the current session id.  This can be used if you want to
12300      check if the next session you tried to resume was actually resumed.
12301      This is because resumed sessions have the same sessionID with the
12302      original session.
12303
12304      Session id is some data set by the server, that identify the
12305      current session.  In TLS 1.0 and SSL 3.0 session id is always less
12306      than 32 bytes.
12307
12308      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
12309      otherwise an error code is returned.
12310
12311 gnutls_session_get_ptr
12312 ----------------------
12313
12314  -- Function: void * gnutls_session_get_ptr (gnutls_session_t SESSION)
12315      SESSION: is a 'gnutls_session_t' structure.
12316
12317      Get user pointer for session.  Useful in callbacks.  This is the
12318      pointer set with 'gnutls_session_set_ptr()'.
12319
12320      *Returns:* the user given pointer from the session structure, or
12321      'NULL' if it was never set.
12322
12323 gnutls_session_is_resumed
12324 -------------------------
12325
12326  -- Function: int gnutls_session_is_resumed (gnutls_session_t SESSION)
12327      SESSION: is a 'gnutls_session_t' structure.
12328
12329      Check whether session is resumed or not.
12330
12331      *Returns:* non zero if this session is resumed, or a zero if this
12332      is a new session.
12333
12334 gnutls_session_set_data
12335 -----------------------
12336
12337  -- Function: int gnutls_session_set_data (gnutls_session_t SESSION,
12338           const void * SESSION_DATA, size_t SESSION_DATA_SIZE)
12339      SESSION: is a 'gnutls_session_t' structure.
12340
12341      SESSION_DATA: is a pointer to space to hold the session.
12342
12343      SESSION_DATA_SIZE: is the session's size
12344
12345      Sets all session parameters, in order to resume a previously
12346      established session.  The session data given must be the one
12347      returned by 'gnutls_session_get_data()'.  This function should be
12348      called before 'gnutls_handshake()'.
12349
12350      Keep in mind that session resuming is advisory.  The server may
12351      choose not to resume the session, thus a full handshake will be
12352      performed.
12353
12354      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
12355      otherwise an error code is returned.
12356
12357 gnutls_session_set_ptr
12358 ----------------------
12359
12360  -- Function: void gnutls_session_set_ptr (gnutls_session_t SESSION,
12361           void * PTR)
12362      SESSION: is a 'gnutls_session_t' structure.
12363
12364      PTR: is the user pointer
12365
12366      This function will set (associate) the user given pointer 'ptr' to
12367      the session structure.  This is pointer can be accessed with
12368      'gnutls_session_get_ptr()'.
12369
12370 gnutls_session_ticket_enable_client
12371 -----------------------------------
12372
12373  -- Function: int gnutls_session_ticket_enable_client (gnutls_session_t
12374           SESSION)
12375      SESSION: is a 'gnutls_session_t' structure.
12376
12377      Request that the client should attempt session resumption using
12378      SessionTicket.
12379
12380      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12381      error code.
12382
12383      *Since:* 2.10.0
12384
12385 gnutls_session_ticket_enable_server
12386 -----------------------------------
12387
12388  -- Function: int gnutls_session_ticket_enable_server (gnutls_session_t
12389           SESSION, const gnutls_datum_t * KEY)
12390      SESSION: is a 'gnutls_session_t' structure.
12391
12392      KEY: key to encrypt session parameters.
12393
12394      Request that the server should attempt session resumption using
12395      SessionTicket.  'key' must be initialized with
12396      'gnutls_session_ticket_key_generate()'.
12397
12398      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12399      error code.
12400
12401      *Since:* 2.10.0
12402
12403 gnutls_session_ticket_key_generate
12404 ----------------------------------
12405
12406  -- Function: int gnutls_session_ticket_key_generate (gnutls_datum_t *
12407           KEY)
12408      KEY: is a pointer to a 'gnutls_datum_t' which will contain a newly
12409      created key.
12410
12411      Generate a random key to encrypt security parameters within
12412      SessionTicket.
12413
12414      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12415      error code.
12416
12417      *Since:* 2.10.0
12418
12419 gnutls_set_default_export_priority
12420 ----------------------------------
12421
12422  -- Function: int gnutls_set_default_export_priority (gnutls_session_t
12423           SESSION)
12424      SESSION: is a 'gnutls_session_t' structure.
12425
12426      Sets some default priority on the ciphers, key exchange methods,
12427      macs and compression methods.  This function also includes weak
12428      algorithms.
12429
12430      *This is the same as calling:* gnutls_priority_set_direct (session,
12431      "EXPORT", NULL);
12432
12433      This function is kept around for backwards compatibility, but
12434      because of its wide use it is still fully supported.  If you wish
12435      to allow users to provide a string that specify which ciphers to
12436      use (which is recommended), you should use
12437      'gnutls_priority_set_direct()' or 'gnutls_priority_set()' instead.
12438
12439      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
12440
12441 gnutls_set_default_priority
12442 ---------------------------
12443
12444  -- Function: int gnutls_set_default_priority (gnutls_session_t SESSION)
12445      SESSION: is a 'gnutls_session_t' structure.
12446
12447      Sets some default priority on the ciphers, key exchange methods,
12448      macs and compression methods.
12449
12450      *This is the same as calling:* gnutls_priority_set_direct (session,
12451      "NORMAL", NULL);
12452
12453      This function is kept around for backwards compatibility, but
12454      because of its wide use it is still fully supported.  If you wish
12455      to allow users to provide a string that specify which ciphers to
12456      use (which is recommended), you should use
12457      'gnutls_priority_set_direct()' or 'gnutls_priority_set()' instead.
12458
12459      *Returns:* 'GNUTLS_E_SUCCESS' on success, or an error code.
12460
12461 gnutls_sign_algorithm_get_name
12462 ------------------------------
12463
12464  -- Function: const char * gnutls_sign_algorithm_get_name
12465           (gnutls_sign_algorithm_t SIGN)
12466      SIGN: is a sign algorithm
12467
12468      Convert a 'gnutls_sign_algorithm_t' value to a string.
12469
12470      *Returns:* a string that contains the name of the specified sign
12471      algorithm, or 'NULL'.
12472
12473 gnutls_sign_algorithm_get_requested
12474 -----------------------------------
12475
12476  -- Function: int gnutls_sign_algorithm_get_requested (gnutls_session_t
12477           SESSION, size_t INDX, gnutls_sign_algorithm_t * ALGO)
12478      SESSION: is a 'gnutls_session_t' structure.
12479
12480      INDX: is an index of the signature algorithm to return
12481
12482      ALGO: the returned certificate type will be stored there
12483
12484      Returns the signature algorithm specified by index that was
12485      requested by the peer.  If the specified index has no data
12486      available this function returns
12487      'GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE'.  If the negotiated TLS
12488      version does not support signature algorithms then
12489      'GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE' will be returned even for
12490      the first index.  The first index is 0.
12491
12492      This function is useful in the certificate callback functions to
12493      assist in selecting the correct certificate.
12494
12495      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
12496      otherwise an error code is returned.
12497
12498      *Since:* 2.10.0
12499
12500 gnutls_sign_callback_get
12501 ------------------------
12502
12503  -- Function: gnutls_sign_func gnutls_sign_callback_get
12504           (gnutls_session_t SESSION, void ** USERDATA)
12505      SESSION: is a gnutls session
12506
12507      USERDATA: if non-'NULL', will be set to abstract callback pointer.
12508
12509      Retrieve the callback function, and its userdata pointer.
12510
12511      *Returns:* The function pointer set by
12512      'gnutls_sign_callback_set()', or if not set, 'NULL'.
12513
12514      *Deprecated:* Use the PKCS 11 interfaces instead.
12515
12516 gnutls_sign_callback_set
12517 ------------------------
12518
12519  -- Function: void gnutls_sign_callback_set (gnutls_session_t SESSION,
12520           gnutls_sign_func SIGN_FUNC, void * USERDATA)
12521      SESSION: is a gnutls session
12522
12523      SIGN_FUNC: function pointer to application's sign callback.
12524
12525      USERDATA: void pointer that will be passed to sign callback.
12526
12527      Set the callback function.  The function must have this prototype:
12528
12529      typedef int (*gnutls_sign_func) (gnutls_session_t session, void
12530      *userdata, gnutls_certificate_type_t cert_type, const
12531      gnutls_datum_t * cert, const gnutls_datum_t * hash, gnutls_datum_t
12532      * signature);
12533
12534      The 'userdata' parameter is passed to the 'sign_func' verbatim, and
12535      can be used to store application-specific data needed in the
12536      callback function.  See also 'gnutls_sign_callback_get()'.
12537
12538      *Deprecated:* Use the PKCS 11 interfaces instead.
12539
12540 gnutls_sign_get_id
12541 ------------------
12542
12543  -- Function: gnutls_sign_algorithm_t gnutls_sign_get_id (const char *
12544           NAME)
12545      NAME: is a MAC algorithm name
12546
12547      The names are compared in a case insensitive way.
12548
12549      *Returns:* return a 'gnutls_sign_algorithm_t' value corresponding
12550      to the specified cipher, or 'GNUTLS_SIGN_UNKNOWN' on error.
12551
12552 gnutls_sign_get_name
12553 --------------------
12554
12555  -- Function: const char * gnutls_sign_get_name (gnutls_sign_algorithm_t
12556           ALGORITHM)
12557      ALGORITHM: is a public key signature algorithm
12558
12559      Convert a 'gnutls_sign_algorithm_t' value to a string.
12560
12561      *Returns:* a pointer to a string that contains the name of the
12562      specified public key signature algorithm, or 'NULL'.
12563
12564      *Since:* 2.6.0
12565
12566 gnutls_sign_list
12567 ----------------
12568
12569  -- Function: const gnutls_sign_algorithm_t * gnutls_sign_list ( VOID)
12570
12571      Get a list of supported public key signature algorithms.
12572
12573      *Returns:* a zero-terminated list of 'gnutls_sign_algorithm_t'
12574      integers indicating the available ciphers.
12575
12576 gnutls_srp_allocate_client_credentials
12577 --------------------------------------
12578
12579  -- Function: int gnutls_srp_allocate_client_credentials
12580           (gnutls_srp_client_credentials_t * SC)
12581      SC: is a pointer to a 'gnutls_srp_server_credentials_t' structure.
12582
12583      This structure is complex enough to manipulate directly thus this
12584      helper function is provided in order to allocate it.
12585
12586      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12587      error code.
12588
12589 gnutls_srp_allocate_server_credentials
12590 --------------------------------------
12591
12592  -- Function: int gnutls_srp_allocate_server_credentials
12593           (gnutls_srp_server_credentials_t * SC)
12594      SC: is a pointer to a 'gnutls_srp_server_credentials_t' structure.
12595
12596      This structure is complex enough to manipulate directly thus this
12597      helper function is provided in order to allocate it.
12598
12599      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12600      error code.
12601
12602 gnutls_srp_base64_decode_alloc
12603 ------------------------------
12604
12605  -- Function: int gnutls_srp_base64_decode_alloc (const gnutls_datum_t *
12606           B64_DATA, gnutls_datum_t * RESULT)
12607      B64_DATA: contains the encoded data
12608
12609      RESULT: the place where decoded data lie
12610
12611      This function will decode the given encoded data.  The decoded data
12612      will be allocated, and stored into result.  It will decode using
12613      the base64 algorithm as used in libsrp.
12614
12615      You should use 'gnutls_free()' to free the returned data.
12616
12617      Warning!  This base64 encoding is not the "standard" encoding, so
12618      do not use it for non-SRP purposes.
12619
12620      *Returns:* 0 on success, or an error code.
12621
12622 gnutls_srp_base64_decode
12623 ------------------------
12624
12625  -- Function: int gnutls_srp_base64_decode (const gnutls_datum_t *
12626           B64_DATA, char * RESULT, size_t * RESULT_SIZE)
12627      B64_DATA: contain the encoded data
12628
12629      RESULT: the place where decoded data will be copied
12630
12631      RESULT_SIZE: holds the size of the result
12632
12633      This function will decode the given encoded data, using the base64
12634      encoding found in libsrp.
12635
12636      Note that 'b64_data' should be null terminated.
12637
12638      Warning!  This base64 encoding is not the "standard" encoding, so
12639      do not use it for non-SRP purposes.
12640
12641      *Returns:* 'GNUTLS_E_SHORT_MEMORY_BUFFER' if the buffer given is
12642      not long enough, or 0 on success.
12643
12644 gnutls_srp_base64_encode_alloc
12645 ------------------------------
12646
12647  -- Function: int gnutls_srp_base64_encode_alloc (const gnutls_datum_t *
12648           DATA, gnutls_datum_t * RESULT)
12649      DATA: contains the raw data
12650
12651      RESULT: will hold the newly allocated encoded data
12652
12653      This function will convert the given data to printable data, using
12654      the base64 encoding.  This is the encoding used in SRP password
12655      files.  This function will allocate the required memory to hold the
12656      encoded data.
12657
12658      You should use 'gnutls_free()' to free the returned data.
12659
12660      Warning!  This base64 encoding is not the "standard" encoding, so
12661      do not use it for non-SRP purposes.
12662
12663      *Returns:* 0 on success, or an error code.
12664
12665 gnutls_srp_base64_encode
12666 ------------------------
12667
12668  -- Function: int gnutls_srp_base64_encode (const gnutls_datum_t * DATA,
12669           char * RESULT, size_t * RESULT_SIZE)
12670      DATA: contain the raw data
12671
12672      RESULT: the place where base64 data will be copied
12673
12674      RESULT_SIZE: holds the size of the result
12675
12676      This function will convert the given data to printable data, using
12677      the base64 encoding, as used in the libsrp.  This is the encoding
12678      used in SRP password files.  If the provided buffer is not long
12679      enough GNUTLS_E_SHORT_MEMORY_BUFFER is returned.
12680
12681      Warning!  This base64 encoding is not the "standard" encoding, so
12682      do not use it for non-SRP purposes.
12683
12684      *Returns:* 'GNUTLS_E_SHORT_MEMORY_BUFFER' if the buffer given is
12685      not long enough, or 0 on success.
12686
12687 gnutls_srp_free_client_credentials
12688 ----------------------------------
12689
12690  -- Function: void gnutls_srp_free_client_credentials
12691           (gnutls_srp_client_credentials_t SC)
12692      SC: is a 'gnutls_srp_client_credentials_t' structure.
12693
12694      This structure is complex enough to manipulate directly thus this
12695      helper function is provided in order to free (deallocate) it.
12696
12697 gnutls_srp_free_server_credentials
12698 ----------------------------------
12699
12700  -- Function: void gnutls_srp_free_server_credentials
12701           (gnutls_srp_server_credentials_t SC)
12702      SC: is a 'gnutls_srp_server_credentials_t' structure.
12703
12704      This structure is complex enough to manipulate directly thus this
12705      helper function is provided in order to free (deallocate) it.
12706
12707 gnutls_srp_server_get_username
12708 ------------------------------
12709
12710  -- Function: const char * gnutls_srp_server_get_username
12711           (gnutls_session_t SESSION)
12712      SESSION: is a gnutls session
12713
12714      This function will return the username of the peer.  This should
12715      only be called in case of SRP authentication and in case of a
12716      server.  Returns NULL in case of an error.
12717
12718      *Returns:* SRP username of the peer, or NULL in case of error.
12719
12720 gnutls_srp_set_client_credentials_function
12721 ------------------------------------------
12722
12723  -- Function: void gnutls_srp_set_client_credentials_function
12724           (gnutls_srp_client_credentials_t CRED,
12725           gnutls_srp_client_credentials_function * FUNC)
12726      CRED: is a 'gnutls_srp_server_credentials_t' structure.
12727
12728      FUNC: is the callback function
12729
12730      This function can be used to set a callback to retrieve the
12731      username and password for client SRP authentication.  The
12732      callback's function form is:
12733
12734      int (*callback)(gnutls_session_t, char** username, char**password);
12735
12736      The 'username' and 'password' must be allocated using
12737      'gnutls_malloc()'.  'username' and 'password' should be ASCII
12738      strings or UTF-8 strings prepared using the "SASLprep" profile of
12739      "stringprep".
12740
12741      The callback function will be called once per handshake before the
12742      initial hello message is sent.
12743
12744      The callback should not return a negative error code the second
12745      time called, since the handshake procedure will be aborted.
12746
12747      The callback function should return 0 on success.  -1 indicates an
12748      error.
12749
12750 gnutls_srp_set_client_credentials
12751 ---------------------------------
12752
12753  -- Function: int gnutls_srp_set_client_credentials
12754           (gnutls_srp_client_credentials_t RES, const char * USERNAME,
12755           const char * PASSWORD)
12756      RES: is a 'gnutls_srp_client_credentials_t' structure.
12757
12758      USERNAME: is the user's userid
12759
12760      PASSWORD: is the user's password
12761
12762      This function sets the username and password, in a
12763      'gnutls_srp_client_credentials_t' structure.  Those will be used in
12764      SRP authentication.  'username' and 'password' should be ASCII
12765      strings or UTF-8 strings prepared using the "SASLprep" profile of
12766      "stringprep".
12767
12768      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12769      error code.
12770
12771 gnutls_srp_set_prime_bits
12772 -------------------------
12773
12774  -- Function: void gnutls_srp_set_prime_bits (gnutls_session_t SESSION,
12775           unsigned int BITS)
12776      SESSION: is a 'gnutls_session_t' structure.
12777
12778      BITS: is the number of bits
12779
12780      This function sets the minimum accepted number of bits, for use in
12781      an SRP key exchange.  If zero, the default 2048 bits will be used.
12782
12783      In the client side it sets the minimum accepted number of bits.  If
12784      a server sends a prime with less bits than that
12785      'GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER' will be returned by the
12786      handshake.
12787
12788      This function has no effect in server side.
12789
12790      *Since:* 2.6.0
12791
12792 gnutls_srp_set_server_credentials_file
12793 --------------------------------------
12794
12795  -- Function: int gnutls_srp_set_server_credentials_file
12796           (gnutls_srp_server_credentials_t RES, const char *
12797           PASSWORD_FILE, const char * PASSWORD_CONF_FILE)
12798      RES: is a 'gnutls_srp_server_credentials_t' structure.
12799
12800      PASSWORD_FILE: is the SRP password file (tpasswd)
12801
12802      PASSWORD_CONF_FILE: is the SRP password conf file (tpasswd.conf)
12803
12804      This function sets the password files, in a
12805      'gnutls_srp_server_credentials_t' structure.  Those password files
12806      hold usernames and verifiers and will be used for SRP
12807      authentication.
12808
12809      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12810      error code.
12811
12812 gnutls_srp_set_server_credentials_function
12813 ------------------------------------------
12814
12815  -- Function: void gnutls_srp_set_server_credentials_function
12816           (gnutls_srp_server_credentials_t CRED,
12817           gnutls_srp_server_credentials_function * FUNC)
12818      CRED: is a 'gnutls_srp_server_credentials_t' structure.
12819
12820      FUNC: is the callback function
12821
12822      This function can be used to set a callback to retrieve the user's
12823      SRP credentials.  The callback's function form is:
12824
12825      int (*callback)(gnutls_session_t, const char* username,
12826      gnutls_datum_t* salt, gnutls_datum_t *verifier, gnutls_datum_t* g,
12827      gnutls_datum_t* n);
12828
12829      'username' contains the actual username.  The 'salt', 'verifier',
12830      'generator' and 'prime' must be filled in using the
12831      'gnutls_malloc()'.  For convenience 'prime' and 'generator' may
12832      also be one of the static parameters defined in extra.h.
12833
12834      In case the callback returned a negative number then gnutls will
12835      assume that the username does not exist.
12836
12837      In order to prevent attackers from guessing valid usernames, if a
12838      user does not exist, g and n values should be filled in using a
12839      random user's parameters.  In that case the callback must return
12840      the special value (1).
12841
12842      The callback function will only be called once per handshake.  The
12843      callback function should return 0 on success, while -1 indicates an
12844      error.
12845
12846 gnutls_srp_verifier
12847 -------------------
12848
12849  -- Function: int gnutls_srp_verifier (const char * USERNAME, const char
12850           * PASSWORD, const gnutls_datum_t * SALT, const gnutls_datum_t
12851           * GENERATOR, const gnutls_datum_t * PRIME, gnutls_datum_t *
12852           RES)
12853      USERNAME: is the user's name
12854
12855      PASSWORD: is the user's password
12856
12857      SALT: should be some randomly generated bytes
12858
12859      GENERATOR: is the generator of the group
12860
12861      PRIME: is the group's prime
12862
12863      RES: where the verifier will be stored.
12864
12865      This function will create an SRP verifier, as specified in RFC2945.
12866      The 'prime' and 'generator' should be one of the static parameters
12867      defined in gnutls/extra.h or may be generated using the libgcrypt
12868      functions 'gcry_prime_generate()' and
12869      'gcry_prime_group_generator()'.
12870
12871      The verifier will be allocated with 'malloc' and will be stored in
12872      'res' using binary format.
12873
12874      *Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned, or an
12875      error code.
12876
12877 gnutls_strerror_name
12878 --------------------
12879
12880  -- Function: const char * gnutls_strerror_name (int ERROR)
12881      ERROR: is an error returned by a gnutls function.
12882
12883      Return the GnuTLS error code define as a string.  For example,
12884      gnutls_strerror_name (GNUTLS_E_DH_PRIME_UNACCEPTABLE) will return
12885      the string "GNUTLS_E_DH_PRIME_UNACCEPTABLE".
12886
12887      *Returns:* A string corresponding to the symbol name of the error
12888      code.
12889
12890      *Since:* 2.6.0
12891
12892 gnutls_strerror
12893 ---------------
12894
12895  -- Function: const char * gnutls_strerror (int ERROR)
12896      ERROR: is a GnuTLS error code, a negative value
12897
12898      This function is similar to strerror.  The difference is that it
12899      accepts an error number returned by a gnutls function; In case of
12900      an unknown error a descriptive string is sent instead of 'NULL'.
12901
12902      Error codes are always a negative value.
12903
12904      *Returns:* A string explaining the GnuTLS error message.
12905
12906 gnutls_supplemental_get_name
12907 ----------------------------
12908
12909  -- Function: const char * gnutls_supplemental_get_name
12910           (gnutls_supplemental_data_format_type_t TYPE)
12911      TYPE: is a supplemental data format type
12912
12913      Convert a 'gnutls_supplemental_data_format_type_t' value to a
12914      string.
12915
12916      *Returns:* a string that contains the name of the specified
12917      supplemental data format type, or 'NULL' for unknown types.
12918
12919 gnutls_transport_get_ptr2
12920 -------------------------
12921
12922  -- Function: void gnutls_transport_get_ptr2 (gnutls_session_t SESSION,
12923           gnutls_transport_ptr_t * RECV_PTR, gnutls_transport_ptr_t *
12924           SEND_PTR)
12925      SESSION: is a 'gnutls_session_t' structure.
12926
12927      RECV_PTR: will hold the value for the pull function
12928
12929      SEND_PTR: will hold the value for the push function
12930
12931      Used to get the arguments of the transport functions (like PUSH and
12932      PULL). These should have been set using
12933      'gnutls_transport_set_ptr2()'.
12934
12935 gnutls_transport_get_ptr
12936 ------------------------
12937
12938  -- Function: gnutls_transport_ptr_t gnutls_transport_get_ptr
12939           (gnutls_session_t SESSION)
12940      SESSION: is a 'gnutls_session_t' structure.
12941
12942      Used to get the first argument of the transport function (like PUSH
12943      and PULL). This must have been set using
12944      'gnutls_transport_set_ptr()'.
12945
12946      *Returns:* first argument of the transport function.
12947
12948 gnutls_transport_set_errno_function
12949 -----------------------------------
12950
12951  -- Function: void gnutls_transport_set_errno_function (gnutls_session_t
12952           SESSION, gnutls_errno_func ERRNO_FUNC)
12953      SESSION: is a 'gnutls_session_t' structure.
12954
12955      ERRNO_FUNC: a callback function similar to 'write()'
12956
12957      This is the function where you set a function to retrieve errno
12958      after a failed push or pull operation.
12959
12960      errno_func is of the form, int
12961      (*gnutls_errno_func)(gnutls_transport_ptr_t); and should return the
12962      errno.
12963
12964 gnutls_transport_set_errno
12965 --------------------------
12966
12967  -- Function: void gnutls_transport_set_errno (gnutls_session_t SESSION,
12968           int ERR)
12969      SESSION: is a 'gnutls_session_t' structure.
12970
12971      ERR: error value to store in session-specific errno variable.
12972
12973      Store 'err' in the session-specific errno variable.  Useful values
12974      for 'err' is EAGAIN and EINTR, other values are treated will be
12975      treated as real errors in the push/pull function.
12976
12977      This function is useful in replacement push/pull functions set by
12978      gnutls_transport_set_push_function and
12979      gnutls_transport_set_pullpush_function under Windows, where the
12980      replacement push/pull may not have access to the same 'errno'
12981      variable that is used by GnuTLS (e.g., the application is linked to
12982      msvcr71.dll and gnutls is linked to msvcrt.dll).
12983
12984      If you don't have the 'session' variable easily accessible from the
12985      push/pull function, and don't worry about thread conflicts, you can
12986      also use 'gnutls_transport_set_global_errno()'.
12987
12988 gnutls_transport_set_global_errno
12989 ---------------------------------
12990
12991  -- Function: void gnutls_transport_set_global_errno (int ERR)
12992      ERR: error value to store in global errno variable.
12993
12994      Store 'err' in the global errno variable.  Useful values for 'err'
12995      is EAGAIN and EINTR, other values are treated will be treated as
12996      real errors in the push/pull function.
12997
12998      This function is useful in replacement push/pull functions set by
12999      gnutls_transport_set_push_function and
13000      gnutls_transport_set_pullpush_function under Windows, where the
13001      replacement push/pull may not have access to the same 'errno'
13002      variable that is used by GnuTLS (e.g., the application is linked to
13003      msvcr71.dll and gnutls is linked to msvcrt.dll).
13004
13005      Whether this function is thread safe or not depends on whether the
13006      global variable errno is thread safe, some system libraries make it
13007      a thread-local variable.  When feasible, using the guaranteed
13008      thread-safe 'gnutls_transport_set_errno()' may be better.
13009
13010 gnutls_transport_set_lowat
13011 --------------------------
13012
13013  -- Function: void gnutls_transport_set_lowat (gnutls_session_t SESSION,
13014           int NUM)
13015      SESSION: is a 'gnutls_session_t' structure.
13016
13017      NUM: is the low water value.
13018
13019      Used to set the lowat value in order for select to check if there
13020      are pending data to socket buffer.  Used only if you have changed
13021      the default low water value (default is 1).  Normally you will not
13022      need that function.  This function is only useful if using berkeley
13023      style sockets.  Otherwise it must be called and set lowat to zero.
13024
13025 gnutls_transport_set_ptr2
13026 -------------------------
13027
13028  -- Function: void gnutls_transport_set_ptr2 (gnutls_session_t SESSION,
13029           gnutls_transport_ptr_t RECV_PTR, gnutls_transport_ptr_t
13030           SEND_PTR)
13031      SESSION: is a 'gnutls_session_t' structure.
13032
13033      RECV_PTR: is the value for the pull function
13034
13035      SEND_PTR: is the value for the push function
13036
13037      Used to set the first argument of the transport function (like PUSH
13038      and PULL). In berkeley style sockets this function will set the
13039      connection handle.  With this function you can use two different
13040      pointers for receiving and sending.
13041
13042 gnutls_transport_set_ptr
13043 ------------------------
13044
13045  -- Function: void gnutls_transport_set_ptr (gnutls_session_t SESSION,
13046           gnutls_transport_ptr_t PTR)
13047      SESSION: is a 'gnutls_session_t' structure.
13048
13049      PTR: is the value.
13050
13051      Used to set the first argument of the transport function (like PUSH
13052      and PULL). In berkeley style sockets this function will set the
13053      connection handle.
13054
13055 gnutls_transport_set_pull_function
13056 ----------------------------------
13057
13058  -- Function: void gnutls_transport_set_pull_function (gnutls_session_t
13059           SESSION, gnutls_pull_func PULL_FUNC)
13060      SESSION: is a 'gnutls_session_t' structure.
13061
13062      PULL_FUNC: a callback function similar to 'read()'
13063
13064      This is the function where you set a function for gnutls to receive
13065      data.  Normally, if you use berkeley style sockets, do not need to
13066      use this function since the default (recv(2)) will probably be ok.
13067
13068      PULL_FUNC is of the form, ssize_t
13069      (*gnutls_pull_func)(gnutls_transport_ptr_t, void*, size_t);
13070
13071 gnutls_transport_set_push_function
13072 ----------------------------------
13073
13074  -- Function: void gnutls_transport_set_push_function (gnutls_session_t
13075           SESSION, gnutls_push_func PUSH_FUNC)
13076      SESSION: is a 'gnutls_session_t' structure.
13077
13078      PUSH_FUNC: a callback function similar to 'write()'
13079
13080      This is the function where you set a push function for gnutls to
13081      use in order to send data.  If you are going to use berkeley style
13082      sockets, you do not need to use this function since the default
13083      (send(2)) will probably be ok.  Otherwise you should specify this
13084      function for gnutls to be able to send data.
13085
13086      PUSH_FUNC is of the form, ssize_t
13087      (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
13088
13089 gnutls_transport_set_vec_push_function
13090 --------------------------------------
13091
13092  -- Function: void gnutls_transport_set_vec_push_function
13093           (gnutls_session_t SESSION, gnutls_vec_push_func VEC_FUNC)
13094      SESSION: is a 'gnutls_session_t' structure.
13095
13096      VEC_FUNC: a callback function similar to 'writev()'
13097
13098      This is the function where you set a push function for gnutls to
13099      use in order to send data.  If you are going to use berkeley style
13100      sockets, you do not need to use this function since the default
13101      (send(2)) will probably be ok.  Otherwise you should specify this
13102      function for gnutls to be able to send data.
13103
13104      PUSH_FUNC is of the form, ssize_t
13105      (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
13106
13107 gnutls_x509_crq_set_pubkey
13108 --------------------------
13109
13110  -- Function: int gnutls_x509_crq_set_pubkey (gnutls_x509_crq_t CRQ,
13111           gnutls_pubkey_t KEY)
13112      CRQ: should contain a 'gnutls_x509_crq_t' structure
13113
13114      KEY: holds a public key
13115
13116      This function will set the public parameters from the given public
13117      key to the request.
13118
13119      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
13120      negative error value.
13121
13122 gnutls_x509_crt_import_pkcs11_url
13123 ---------------------------------
13124
13125  -- Function: int gnutls_x509_crt_import_pkcs11_url (gnutls_x509_crt_t
13126           CRT, const char * URL, unsigned int FLAGS)
13127      CRT: A certificate of type 'gnutls_x509_crt_t'
13128
13129      URL: A PKCS 11 url
13130
13131      FLAGS: One of GNUTLS_PKCS11_OBJ_* flags
13132
13133      This function will import a PKCS 11 certificate directly from a
13134      token without involving the 'gnutls_pkcs11_obj_t' structure.  This
13135      function will fail if the certificate stored is not of X.509 type.
13136
13137      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
13138      negative error value.
13139
13140 gnutls_x509_crt_import_pkcs11
13141 -----------------------------
13142
13143  -- Function: int gnutls_x509_crt_import_pkcs11 (gnutls_x509_crt_t CRT,
13144           gnutls_pkcs11_obj_t PKCS11_CRT)
13145      CRT: A certificate of type 'gnutls_x509_crt_t'
13146
13147      PKCS11_CRT: A PKCS 11 object that contains a certificate
13148
13149      This function will import a PKCS 11 certificate to a
13150      'gnutls_x509_crt_t' structure.
13151
13152      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
13153      negative error value.
13154
13155 gnutls_x509_crt_list_import_pkcs11
13156 ----------------------------------
13157
13158  -- Function: int gnutls_x509_crt_list_import_pkcs11 (gnutls_x509_crt_t
13159           * CERTS, unsigned int CERT_MAX, gnutls_pkcs11_obj_t * const
13160           OBJS, unsigned int FLAGS)
13161      CERTS: A list of certificates of type 'gnutls_x509_crt_t'
13162
13163      CERT_MAX: The maximum size of the list
13164
13165      OBJS: A list of PKCS 11 objects
13166
13167      FLAGS: 0 for now
13168
13169      This function will import a PKCS 11 certificate list to a list of
13170      'gnutls_x509_crt_t' structure.  These must not be initialized.
13171
13172      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
13173      negative error value.
13174
13175 gnutls_x509_crt_set_pubkey
13176 --------------------------
13177
13178  -- Function: int gnutls_x509_crt_set_pubkey (gnutls_x509_crt_t CRT,
13179           gnutls_pubkey_t KEY)
13180      CRT: should contain a 'gnutls_x509_crt_t' structure
13181
13182      KEY: holds a public key
13183
13184      This function will set the public parameters from the given public
13185      key to the request.
13186
13187      *Returns:* On success, 'GNUTLS_E_SUCCESS' is returned, otherwise a
13188      negative error value.