released 3.1.2
[gnutls.git] / doc / gnutls-guile.texi
blob1e28c8dc40adfbf4f06c13592feaca0acc092ee9
1 \input texinfo   @c -*-texinfo-*-
2 @comment %**start of header
3 @setfilename gnutls-guile.info
4 @include version-guile.texi
5 @settitle GnuTLS-Guile @value{VERSION}
7 @c don't indent the paragraphs.
8 @paragraphindent 0
10 @c Unify some of the indices.
11 @syncodeindex tp fn
12 @syncodeindex pg cp
14 @comment %**end of header
15 @finalout
16 @copying
17 This manual is last updated @value{UPDATED} for version
18 @value{VERSION} of GnuTLS.
20 Copyright @copyright{} 2001-2012 Free Software Foundation, Inc.
22 @quotation
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.3 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.  A
27 copy of the license is included in the section entitled ``GNU Free
28 Documentation License''.
29 @end quotation
30 @end copying
32 @dircategory Software libraries
33 @direntry
34 * GnuTLS-Guile: (gnutls-guile).         GNU Transport Layer Security Library. Guile bindings.
35 @end direntry
37 @titlepage
38 @title GnuTLS-Guile
39 @subtitle Guile binding for GNU TLS
40 @subtitle for version @value{VERSION}, @value{UPDATED}
41 @sp 7
42 @image{gnutls-logo,6cm,6cm}
43 @page
44 @vskip 0pt plus 1filll
45 @insertcopying
46 @end titlepage
48 @macro xcite{ref}
49 [\ref\] (@pxref{Bibliography})
50 @end macro
52 @contents
54 @ifnottex
55 @node Top
56 @top GnuTLS-Guile
58 @insertcopying
59 @end ifnottex
61 @menu
62 * Preface::                     Preface.
63 * Guile Preparations::          Note on installation and environment.
64 * Guile API Conventions::       Naming conventions and other idiosyncrasies.
65 * Guile Examples::              Quick start.
66 * Guile Reference::             The Scheme GnuTLS programming interface.
67 * Copying Information::         Copying information.
68 @end menu
70 @node Preface
71 @chapter Preface
73 This manual describes the @uref{http://www.gnu.org/software/guile/,
74 GNU Guile} Scheme programming interface to GnuTLS.  The reader is
75 assumed to have basic knowledge of the protocol and library.  Details
76 missing from this chapter may be found in Function reference,
77 of the C API reference.
79 At this stage, not all the C functions are available from Scheme, but
80 a large subset thereof is available.
82 @c *********************************************************************
83 @node Guile Preparations
84 @chapter Guile Preparations
86 The GnuTLS Guile bindings are available for both the 1.8 and 2.0 stable
87 series of Guile.
89 By default they are installed under the GnuTLS installation directory,
90 typically @file{/usr/local/share/guile/site/}).  Normally Guile
91 will not find the module there without help.  You may experience
92 something like this:
94 @example
95 $ guile
96 guile> (use-modules (gnutls))
97 <unnamed port>: no code for module (gnutls)
98 guile>
99 @end example
101 There are two ways to solve this.  The first is to make sure that when
102 building GnuTLS, the Guile bindings will be installed in the same
103 place where Guile looks.  You may do this by using the
104 @code{--with-guile-site-dir} parameter as follows:
106 @example
107 $ ./configure --with-guile-site-dir=no
108 @end example
110 This will instruct GnuTLS to attempt to install the Guile bindings
111 where Guile will look for them.  It will use @code{guile-config info
112 pkgdatadir} to learn the path to use.
114 If Guile was installed into @code{/usr}, you may also install GnuTLS
115 using the same prefix:
117 @example
118 $ ./configure --prefix=/usr
119 @end example
121 If you want to specify the path to install the Guile bindings you can
122 also specify the path directly:
124 @example
125 $ ./configure --with-guile-site-dir=/opt/guile/share/guile/site
126 @end example
128 The second solution requires some more work but may be easier to use
129 if you do not have system administrator rights to your machine.  You
130 need to instruct Guile so that it finds the GnuTLS Guile bindings.
131 Either use the @code{GUILE_LOAD_PATH} environment variable as follows:
133 @example
134 $ GUILE_LOAD_PATH="/usr/local/share/guile/site:$GUILE_LOAD_PATH" guile
135 guile> (use-modules (gnutls))
136 guile>
137 @end example
139 Alternatively, you can modify Guile's @code{%load-path} variable
140 (@pxref{Build Config, Guile's run-time options,, guile, The GNU Guile
141 Reference Manual}).
143 At this point, you might get an error regarding
144 @file{libguile-gnutls-v-0} similar to:
146 @example
147 gnutls.scm:361:1: In procedure dynamic-link in expression (load-extension "libguile-gnutls-v-0" "scm_init_gnutls"):
148 gnutls.scm:361:1: file: "libguile-gnutls-v-0", message: "libguile-gnutls-v-0.so: cannot open shared object file: No such file or directory"
149 @end example
151 In this case, you will need to modify the run-time linker path, for
152 example as follows:
154 @example
155 $ LD_LIBRARY_PATH=/usr/local/lib GUILE_LOAD_PATH=/usr/local/share/guile/site guile
156 guile> (use-modules (gnutls))
157 guile>
158 @end example
160 To check that you got the intended GnuTLS library version, you may
161 print the version number of the loaded library as follows:
163 @example
164 $ guile
165 guile> (use-modules (gnutls))
166 guile> (gnutls-version)
167 "@value{VERSION}"
168 guile> 
169 @end example
172 @c *********************************************************************
173 @node Guile API Conventions
174 @chapter Guile API Conventions
176 This chapter details the conventions used by Guile API, as well as
177 specificities of the mapping of the C API to Scheme.
179 @menu
180 * Enumerates and Constants::      Representation of C-side constants.
181 * Procedure Names::               Naming conventions.
182 * Representation of Binary Data:: Binary data buffers.
183 * Input and Output::              Input and output.
184 * Exception Handling::            Exceptions.
185 @end menu
187 @node Enumerates and Constants
188 @section Enumerates and Constants
190 @cindex enumerate
191 @cindex constant
193 Lots of enumerates and constants are used in the GnuTLS C API.  For
194 each C enumerate type, a disjoint Scheme type is used---thus,
195 enumerate values and constants are not represented by Scheme symbols
196 nor by integers.  This makes it impossible to use an enumerate value
197 of the wrong type on the Scheme side: such errors are automatically
198 detected by type-checking.
200 The enumerate values are bound to variables exported by the
201 @code{(gnutls)} module.  These variables
202 are named according to the following convention:
204 @itemize
205 @item All variable names are lower-case; the underscore @code{_}
206 character used in the C API is replaced by hyphen @code{-}.
207 @item All variable names are prepended by the name of the enumerate
208 type and the slash @code{/} character.
209 @item In some cases, the variable name is made more explicit than the
210 one of the C API, e.g., by avoid abbreviations.
211 @end itemize
213 Consider for instance this C-side enumerate:
215 @example
216 typedef enum
218   GNUTLS_CRD_CERTIFICATE = 1,
219   GNUTLS_CRD_ANON,
220   GNUTLS_CRD_SRP,
221   GNUTLS_CRD_PSK
222 @} gnutls_credentials_type_t;
223 @end example
225 The corresponding Scheme values are bound to the following variables
226 exported by the @code{(gnutls)} module:
228 @example
229 credentials/certificate
230 credentials/anonymous
231 credentials/srp
232 credentials/psk
233 @end example
235 Hopefully, most variable names can be deduced from this convention.
237 Scheme-side ``enumerate'' values can be compared using @code{eq?}
238 (@pxref{Equality, equality predicates,, guile, The GNU Guile Reference
239 Manual}).  Consider the following example:
241 @findex session-cipher
243 @example
244 (let ((session (make-session connection-end/client)))
246   ;;
247   ;; ...
248   ;;
250   ;; Check the ciphering algorithm currently used by SESSION.
251   (if (eq? cipher/arcfour (session-cipher session))
252       (format #t "We're using the ARCFOUR algorithm")))
253 @end example
255 In addition, all enumerate values can be converted to a human-readable
256 string, in a type-specific way.  For instance, @code{(cipher->string
257 cipher/arcfour)} yields @code{"ARCFOUR 128"}, while
258 @code{(key-usage->string key-usage/digital-signature)} yields
259 @code{"digital-signature"}.  Note that these strings may not be
260 sufficient for use in a user interface since they are fairly concise
261 and not internationalized.
264 @node Procedure Names
265 @section Procedure Names
267 Unlike C functions in GnuTLS, the corresponding Scheme procedures are
268 named in a way that is close to natural English.  Abbreviations are
269 also avoided.  For instance, the Scheme procedure corresponding to
270 @code{gnutls_certificate_set_dh_params} is named
271 @code{set-certificate-credentials-dh-parameters!}.  The @code{gnutls_}
272 prefix is always omitted from variable names since a similar effect
273 can be achieved using Guile's nifty binding renaming facilities,
274 should it be needed (@pxref{Using Guile Modules,,, guile, The GNU
275 Guile Reference Manual}).
277 Often Scheme procedure names differ from C function names in a way
278 that makes it clearer what objects they operate on.  For example, the
279 Scheme procedure named @code{set-session-transport-port!} corresponds
280 to @code{gnutls_transport_set_ptr}, making it clear that this
281 procedure applies to session.
283 @node Representation of Binary Data
284 @section Representation of Binary Data
286 Many procedures operate on binary data.  For instance,
287 @code{pkcs3-import-dh-parameters} expects binary data as input and,
288 similarly, procedures like @code{pkcs1-export-rsa-parameters} return
289 binary data.
291 @cindex SRFI-4
292 @cindex homogeneous vector
294 Binary data is represented on the Scheme side using SRFI-4 homogeneous
295 vectors (@pxref{SRFI-4,,, guile, The GNU Guile Reference Manual}).
296 Although any type of homogeneous vector may be used, @code{u8vector}s
297 (i.e., vectors of bytes) are highly recommended.
299 As an example, generating and then exporting RSA parameters in the PEM
300 format can be done as follows:
302 @findex make-rsa-parameters
303 @findex pkcs1-export-rsa-parameters
304 @vindex x509-certificate-format/pem
306 @example
307 (let* ((rsa-params (make-rsa-parameters 1024))
308        (raw-data
309         (pkcs1-export-rsa-parameters rsa-params
310                                      x509-certificate-format/pem)))
311   (uniform-vector-write raw-data (open-output-file "some-file.pem")))
312 @end example
314 For an example of OpenPGP key import from a file, see @ref{Importing
315 OpenPGP Keys Guile Example}.
318 @node Input and Output
319 @section Input and Output
321 @findex set-session-transport-port!
322 @findex set-session-transport-fd!
324 The underlying transport of a TLS session can be any Scheme
325 input/output port (@pxref{Ports and File Descriptors,,, guile, The GNU
326 Guile Reference Manual}).  This has to be specified using
327 @code{set-session-transport-port!}.
329 However, for better performance, a raw file descriptor can be
330 specified, using @code{set-session-transport-fd!}.  For instance, if
331 the transport layer is a socket port over an OS-provided socket, you
332 can use the @code{port->fdes} or @code{fileno} procedure to obtain the
333 underlying file descriptor and pass it to
334 @code{set-session-transport-fd!}  (@pxref{Ports and File Descriptors,
335 @code{port->fdes} and @code{fileno},, guile, The GNU Guile Reference
336 Manual}).  This would work as follows:
338 @example
339 (let ((socket (socket PF_INET SOCK_STREAM 0))
340       (session (make-session connection-end/client)))
342   ;;
343   ;; Establish a TCP connection...
344   ;;
346   ;; Use the file descriptor that underlies SOCKET.
347   (set-session-transport-fd! session (fileno socket)))
348 @end example
350 @findex session-record-port
352 Once a TLS session is established, data can be communicated through it
353 (i.e., @emph{via} the TLS record layer) using the port returned by
354 @code{session-record-port}:
356 @example
357 (let ((session (make-session connection-end/client)))
359   ;;
360   ;; Initialize the various parameters of SESSION, set up
361   ;; a network connection, etc...
362   ;;
364   (let ((i/o (session-record-port session)))
365     (write "Hello peer!" i/o)
366     (let ((greetings (read i/o)))
368       ;; ...
370       (bye session close-request/rdwr))))
371 @end example
373 @findex record-send
374 @findex record-receive!
376 A lower-level I/O API is provided by @code{record-send} and
377 @code{record-receive!} which take an SRFI-4 vector to represent the
378 data sent or received.  While it might improve performance, it is much
379 less convenient than the above and should rarely be needed.
382 @node Exception Handling
383 @section Exception Handling
385 @cindex exceptions
386 @cindex errors
387 @cindex @code{gnutls-error}
388 @findex error->string
390 GnuTLS errors are implemented as Scheme exceptions (@pxref{Exceptions,
391 exceptions in Guile,, guile, The GNU Guile Reference Manual}).  Each
392 time a GnuTLS function returns an error, an exception with key
393 @code{gnutls-error} is raised.  The additional arguments that are
394 thrown include an error code and the name of the GnuTLS procedure that
395 raised the exception.  The error code is pretty much like an enumerate
396 value: it is one of the @code{error/} variables exported by the
397 @code{(gnutls)} module (@pxref{Enumerates and Constants}).  Exceptions
398 can be turned into error messages using the @code{error->string}
399 procedure.
401 The following examples illustrates how GnuTLS exceptions can be
402 handled:
404 @example
405 (let ((session (make-session connection-end/server)))
407   ;;
408   ;; ...
409   ;;
411   (catch 'gnutls-error
412     (lambda ()
413       (handshake session))
414     (lambda (key err function . currently-unused)
415       (format (current-error-port)
416               "a GnuTLS error was raised by `~a': ~a~%"
417               function (error->string err)))))
418 @end example
420 Again, error values can be compared using @code{eq?}:
422 @example
423     ;; `gnutls-error' handler.
424     (lambda (key err function . currently-unused)
425       (if (eq? err error/fatal-alert-received)
426           (format (current-error-port)
427                   "a fatal alert was caught!~%")
428           (format (current-error-port)
429                   "something bad happened: ~a~%"
430                   (error->string err))))
431 @end example
433 Note that the @code{catch} handler is currently passed only 3
434 arguments but future versions might provide it with additional
435 arguments.  Thus, it must be prepared to handle more than 3 arguments,
436 as in this example.
439 @c *********************************************************************
440 @node Guile Examples
441 @chapter Guile Examples
443 This chapter provides examples that illustrate common use cases.
445 @menu
446 * Anonymous Authentication Guile Example::    Simplest client and server.
447 * OpenPGP Authentication Guile Example::      Using OpenPGP-based authentication.
448 * Importing OpenPGP Keys Guile Example::      Importing keys from files.
449 @end menu
451 @node Anonymous Authentication Guile Example
452 @section Anonymous Authentication Guile Example
454 @dfn{Anonymous authentication} is very easy to use.  No certificates
455 are needed by the communicating parties.  Yet, it allows them to
456 benefit from end-to-end encryption and integrity checks.
458 The client-side code would look like this (assuming @var{some-socket}
459 is bound to an open socket port):
461 @vindex connection-end/client
462 @vindex kx/anon-dh
463 @vindex close-request/rdwr
465 @example
466 ;; Client-side.
468 (let ((client (make-session connection-end/client)))
469   ;; Use the default settings.
470   (set-session-default-priority! client)
472   ;; Don't use certificate-based authentication.
473   (set-session-certificate-type-priority! client '())
475   ;; Request the "anonymous Diffie-Hellman" key exchange method.
476   (set-session-kx-priority! client (list kx/anon-dh))
478   ;; Specify the underlying socket.
479   (set-session-transport-fd! client (fileno some-socket))
481   ;; Create anonymous credentials.
482   (set-session-credentials! client
483                             (make-anonymous-client-credentials))
485   ;; Perform the TLS handshake with the server.
486   (handshake client)
488   ;; Send data over the TLS record layer.
489   (write "hello, world!" (session-record-port client))
491   ;; Terminate the TLS session.
492   (bye client close-request/rdwr))
493 @end example
495 The corresponding server would look like this (again, assuming
496 @var{some-socket} is bound to a socket port):
498 @vindex connection-end/server
500 @example
501 ;; Server-side.
503 (let ((server (make-session connection-end/server)))
504   (set-session-default-priority! server)
505   (set-session-certificate-type-priority! server '())
506   (set-session-kx-priority! server (list kx/anon-dh))
508   ;; Specify the underlying transport socket.
509   (set-session-transport-fd! server (fileno some-socket))
511   ;; Create anonymous credentials.
512   (let ((cred (make-anonymous-server-credentials))
513         (dh-params (make-dh-parameters 1024)))
514     ;; Note: DH parameter generation can take some time.
515     (set-anonymous-server-dh-parameters! cred dh-params)
516     (set-session-credentials! server cred))
518   ;; Perform the TLS handshake with the client.
519   (handshake server)
521   ;; Receive data over the TLS record layer.
522   (let ((message (read (session-record-port server))))
523     (format #t "received the following message: ~a~%"
524             message)
526     (bye server close-request/rdwr)))
527 @end example
529 This is it!
532 @node OpenPGP Authentication Guile Example
533 @section OpenPGP Authentication Guile Example
535 GnuTLS allows users to authenticate using OpenPGP certificates.
536 Using OpenPGP-based authentication is not more complicated than using
537 anonymous authentication.  It requires a bit of extra work, though, to
538 import the OpenPGP public and private key of the client/server.  Key
539 import is omitted here and is left as an exercise to the reader
540 (@pxref{Importing OpenPGP Keys Guile Example}).
542 Assuming @var{some-socket} is bound to an open socket port and
543 @var{pub} and @var{sec} are bound to the client's OpenPGP public and
544 secret key, respectively, client-side code would look like this:
546 @vindex certificate-type/openpgp
548 @example
549 ;; Client-side.
551 (define %certs (list certificate-type/openpgp))
553 (let ((client (make-session connection-end/client))
554       (cred   (make-certificate-credentials)))
555   (set-session-default-priority! client)
557   ;; Choose OpenPGP certificates.
558   (set-session-certificate-type-priority! client %certs)
560   ;; Prepare appropriate client credentials.
561   (set-certificate-credentials-openpgp-keys! cred pub sec)
562   (set-session-credentials! client cred)
564   ;; Specify the underlying transport socket.
565   (set-session-transport-fd! client (fileno some-socket))
567   (handshake client)
568   (write "hello, world!" (session-record-port client))
569   (bye client close-request/rdwr))
570 @end example
572 Similarly, server-side code would be along these lines:
574 @example
575 ;; Server-side.
577 (define %certs (list certificate-type/openpgp))
579 (let ((server (make-session connection-end/server))
580       (rsa    (make-rsa-parameters 1024))
581       (dh     (make-dh-parameters 1024)))
582   (set-session-default-priority! server)
584   ;; Choose OpenPGP certificates.
585   (set-session-certificate-type-priority! server %certs)
587   (let ((cred (make-certificate-credentials)))
588     ;; Prepare credentials with RSA and Diffie-Hellman parameters.
589     (set-certificate-credentials-dh-parameters! cred dh)
590     (set-certificate-credentials-rsa-export-parameters! cred rsa)
591     (set-certificate-credentials-openpgp-keys! cred pub sec)
592     (set-session-credentials! server cred))
594   (set-session-transport-fd! server (fileno some-socket))
596   (handshake server)
597   (let ((msg (read (session-record-port server))))
598     (format #t "received: ~a~%" msg)
600     (bye server close-request/rdwr)))
601 @end example
603 In practice, generating RSA parameters (and Diffie-Hellman parameters)
604 can time a long time.  Thus, you may want to generate them once and
605 store them in a file for future re-use (@pxref{Guile Reference,
606 @code{pkcs1-export-rsa-parameters} and @code{pkcs1-import-rsa-parameters}}).
608 @node Importing OpenPGP Keys Guile Example
609 @section Importing OpenPGP Keys Guile Example
611 The following example provides a simple way of importing
612 ``ASCII-armored'' OpenPGP keys from files, using the
613 @code{import-openpgp-certificate} and @code{import-openpgp-private-key}
614 procedures.
616 @vindex openpgp-certificate-format/base64
617 @vindex openpgp-certificate-format/raw
619 @example
620 (use-modules (srfi srfi-4)
621              (gnutls))
623 (define (import-key-from-file import-proc file)
624   ;; Import OpenPGP key from FILE using IMPORT-PROC.
626   ;; Prepare a u8vector large enough to hold the raw
627   ;; key contents.
628   (let* ((size (stat:size (stat path)))
629          (raw  (make-u8vector size)))
631     ;; Fill in the u8vector with the contents of FILE.
632     (uniform-vector-read! raw (open-input-file file))
634     ;; Pass the u8vector to the import procedure.
635     (import-proc raw openpgp-certificate-format/base64)))
638 (define (import-public-key-from-file file)
639   (import-key-from-file import-openpgp-certificate file))
641 (define (import-private-key-from-file file)
642   (import-key-from-file import-openpgp-private-key file))
643 @end example
645 The procedures @code{import-public-key-from-file} and
646 @code{import-private-key-from-file} can be passed a file name.  They
647 return an OpenPGP public key and private key object, respectively
648 (@pxref{Guile Reference, OpenPGP key objects}).
651 @c *********************************************************************
652 @node Guile Reference
653 @chapter Guile Reference
655 This chapter lists the GnuTLS Scheme procedures exported by the
656 @code{(gnutls)} module (@pxref{The Guile module system,,, guile, The
657 GNU Guile Reference Manual}).
659 @include core.c.texi
661 @c Local Variables:
662 @c ispell-local-dictionary: "american"
663 @c End:
665 @include cha-copying.texi
667 @bye