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.
10 @c Unify some of the indices.
14 @comment %**end of header
17 This manual is last updated @value{UPDATED} for version
18 @value{VERSION} of GnuTLS.
20 Copyright @copyright{} 2001-2012 Free Software Foundation, Inc.
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''.
32 @dircategory Software libraries
34 * GnuTLS-Guile: (gnutls-guile). GNU Transport Layer Security Library. Guile bindings.
39 @subtitle Guile binding for GNU TLS
40 @subtitle for version @value{VERSION}, @value{UPDATED}
42 @image{gnutls-logo,6cm,6cm}
44 @vskip 0pt plus 1filll
49 [\ref\] (@pxref{Bibliography})
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.
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
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
96 guile> (use-modules (gnutls))
97 <unnamed port>: no code for module (gnutls)
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:
107 $ ./configure --with-guile-site-dir=no
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:
118 $ ./configure --prefix=/usr
121 If you want to specify the path to install the Guile bindings you can
122 also specify the path directly:
125 $ ./configure --with-guile-site-dir=/opt/guile/share/guile/site
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:
134 $ GUILE_LOAD_PATH="/usr/local/share/guile/site:$GUILE_LOAD_PATH" guile
135 guile> (use-modules (gnutls))
139 Alternatively, you can modify Guile's @code{%load-path} variable
140 (@pxref{Build Config, Guile's run-time options,, guile, The GNU Guile
143 At this point, you might get an error regarding
144 @file{libguile-gnutls-v-0} similar to:
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"
151 In this case, you will need to modify the run-time linker path, for
155 $ LD_LIBRARY_PATH=/usr/local/lib GUILE_LOAD_PATH=/usr/local/share/guile/site guile
156 guile> (use-modules (gnutls))
160 To check that you got the intended GnuTLS library version, you may
161 print the version number of the loaded library as follows:
165 guile> (use-modules (gnutls))
166 guile> (gnutls-version)
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.
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.
187 @node Enumerates and Constants
188 @section Enumerates and Constants
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:
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.
213 Consider for instance this C-side enumerate:
218 GNUTLS_CRD_CERTIFICATE = 1,
222 @} gnutls_credentials_type_t;
225 The corresponding Scheme values are bound to the following variables
226 exported by the @code{(gnutls)} module:
229 credentials/certificate
230 credentials/anonymous
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
244 (let ((session (make-session connection-end/client)))
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")))
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
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
307 (let* ((rsa-params (make-rsa-parameters 1024))
309 (pkcs1-export-rsa-parameters rsa-params
310 x509-certificate-format/pem)))
311 (uniform-vector-write raw-data (open-output-file "some-file.pem")))
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:
339 (let ((socket (socket PF_INET SOCK_STREAM 0))
340 (session (make-session connection-end/client)))
343 ;; Establish a TCP connection...
346 ;; Use the file descriptor that underlies SOCKET.
347 (set-session-transport-fd! session (fileno socket)))
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}:
357 (let ((session (make-session connection-end/client)))
360 ;; Initialize the various parameters of SESSION, set up
361 ;; a network connection, etc...
364 (let ((i/o (session-record-port session)))
365 (write "Hello peer!" i/o)
366 (let ((greetings (read i/o)))
370 (bye session close-request/rdwr))))
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
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}
401 The following examples illustrates how GnuTLS exceptions can be
405 (let ((session (make-session connection-end/server)))
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)))))
420 Again, error values can be compared using @code{eq?}:
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))))
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,
439 @c *********************************************************************
441 @chapter Guile Examples
443 This chapter provides examples that illustrate common use cases.
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.
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
463 @vindex close-request/rdwr
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.
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))
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
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.
521 ;; Receive data over the TLS record layer.
522 (let ((message (read (session-record-port server))))
523 (format #t "received the following message: ~a~%"
526 (bye server close-request/rdwr)))
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
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))
568 (write "hello, world!" (session-record-port client))
569 (bye client close-request/rdwr))
572 Similarly, server-side code would be along these lines:
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))
597 (let ((msg (read (session-record-port server))))
598 (format #t "received: ~a~%" msg)
600 (bye server close-request/rdwr)))
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}
616 @vindex openpgp-certificate-format/base64
617 @vindex openpgp-certificate-format/raw
620 (use-modules (srfi srfi-4)
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
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))
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}).
662 @c ispell-local-dictionary: "american"
665 @include cha-copying.texi