1 @c Documentation of the GNU Guile bindings.
4 @chapter Guile Bindings
6 This chapter describes the @uref{http://www.gnu.org/software/guile/,
7 GNU Guile} Scheme programming interface to GnuTLS. The reader is
8 assumed to have basic knowledge of the protocol and library. Details
9 missing from this chapter may be found in @ref{Function reference,
12 At this stage, not all the C functions are available from Scheme, but
13 a large subset thereof is available.
17 * Guile Preparations:: Note on installation and environment.
18 * Guile API Conventions:: Naming conventions and other idiosyncrasies.
19 * Guile Examples:: Quick start.
20 * Guile Reference:: The Scheme GnuTLS programming interface.
23 @c *********************************************************************
24 @node Guile Preparations
25 @section Guile Preparations
27 The GnuTLS Guile bindings are by default installed under the GnuTLS
28 installation directory (e.g., typically
29 @file{/usr/local/share/guile/site/}). Normally Guile will not find
30 the module there without help. You may experience something like
35 guile> (use-modules (gnutls))
36 <unnamed port>: no code for module (gnutls)
40 There are two ways to solve this. The first is to make sure that when
41 building GnuTLS, the Guile bindings will be installed in the same
42 place where Guile looks. You may do this by using the
43 @code{--with-guile-site-dir} parameter as follows:
46 $ ./configure --with-guile-site-dir=no
49 This will instruct GnuTLS to attempt to install the Guile bindings
50 where Guile will look for them. It will use @code{guile-config info
51 pkgdatadir} to learn the path to use.
53 If Guile was installed into @code{/usr}, you may also install GnuTLS
54 using the same prefix:
57 $ ./configure --prefix=/usr
60 If you want to specify the path to install the Guile bindings you can
61 also specify the path directly:
64 $ ./configure --with-guile-site-dir=/opt/guile/share/guile/site
67 The second solution requires some more work but may be easier to use
68 if you do not have system administrator rights to your machine. You
69 need to instruct Guile so that it finds the GnuTLS Guile bindings.
70 Either use the @code{GUILE_LOAD_PATH} environment variable as follows:
73 $ GUILE_LOAD_PATH="/usr/local/share/guile/site:$GUILE_LOAD_PATH" guile
74 guile> (use-modules (gnutls))
78 Alternatively, you can modify Guile's @code{%load-path} variable
79 (@pxref{Build Config, Guile's run-time options,, guile, The GNU Guile
82 At this point, you might get an error regarding
83 @file{libguile-gnutls-v-0} similar to:
86 gnutls.scm:361:1: In procedure dynamic-link in expression (load-extension "libguile-gnutls-v-0" "scm_init_gnutls"):
87 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"
90 In this case, you will need to modify the run-time linker path, for
94 $ LD_LIBRARY_PATH=/usr/local/lib GUILE_LOAD_PATH=/usr/local/share/guile/site guile
95 guile> (use-modules (gnutls))
99 To check that you got the intended GnuTLS library version, you may
100 print the version number of the loaded library as follows:
104 guile> (use-modules (gnutls))
105 guile> (gnutls-version)
111 @c *********************************************************************
112 @node Guile API Conventions
113 @section Guile API Conventions
115 This chapter details the conventions used by Guile API, as well as
116 specificities of the mapping of the C API to Scheme.
119 * Enumerates and Constants:: Representation of C-side constants.
120 * Procedure Names:: Naming conventions.
121 * Representation of Binary Data:: Binary data buffers.
122 * Input and Output:: Input and output.
123 * Exception Handling:: Exceptions.
126 @node Enumerates and Constants
127 @subsection Enumerates and Constants
132 Lots of enumerates and constants are used in the GnuTLS C API. For
133 each C enumerate type, a disjoint Scheme type is used---thus,
134 enumerate values and constants are not represented by Scheme symbols
135 nor by integers. This makes it impossible to use an enumerate value
136 of the wrong type on the Scheme side: such errors are automatically
137 detected by type-checking.
139 The enumerate values are bound to variables exported by the
140 @code{(gnutls)} and @code{(gnutls extra)} modules. These variables
141 are named according to the following convention:
144 @item All variable names are lower-case; the underscore @code{_}
145 character used in the C API is replaced by hyphen @code{-}.
146 @item All variable names are prepended by the name of the enumerate
147 type and the slash @code{/} character.
148 @item In some cases, the variable name is made more explicit than the
149 one of the C API, e.g., by avoid abbreviations.
152 Consider for instance this C-side enumerate:
157 GNUTLS_CRD_CERTIFICATE = 1,
162 @} gnutls_credentials_type_t;
165 The corresponding Scheme values are bound to the following variables
166 exported by the @code{(gnutls)} module:
169 credentials/certificate
170 credentials/anonymous
176 Hopefully, most variable names can be deduced from this convention.
178 Scheme-side ``enumerate'' values can be compared using @code{eq?}
179 (@pxref{Equality, equality predicates,, guile, The GNU Guile Reference
180 Manual}). Consider the following example:
182 @findex session-cipher
185 (let ((session (make-session connection-end/client)))
191 ;; Check the ciphering algorithm currently used by SESSION.
192 (if (eq? cipher/arcfour (session-cipher session))
193 (format #t "We're using the ARCFOUR algorithm")))
196 In addition, all enumerate values can be converted to a human-readable
197 string, in a type-specific way. For instance, @code{(cipher->string
198 cipher/arcfour)} yields @code{"ARCFOUR 128"}, while
199 @code{(key-usage->string key-usage/digital-signature)} yields
200 @code{"digital-signature"}. Note that these strings may not be
201 sufficient for use in a user interface since they are fairly concise
202 and not internationalized.
205 @node Procedure Names
206 @subsection Procedure Names
208 Unlike C functions in GnuTLS, the corresponding Scheme procedures are
209 named in a way that is close to natural English. Abbreviations are
210 also avoided. For instance, the Scheme procedure corresponding to
211 @code{gnutls_certificate_set_dh_params} is named
212 @code{set-certificate-credentials-dh-parameters!}. The @code{gnutls_}
213 prefix is always omitted from variable names since a similar effect
214 can be achieved using Guile's nifty binding renaming facilities,
215 should it be needed (@pxref{Using Guile Modules,,, guile, The GNU
216 Guile Reference Manual}).
218 Often Scheme procedure names differ from C function names in a way
219 that makes it clearer what objects they operate on. For example, the
220 Scheme procedure named @code{set-session-transport-port!} corresponds
221 to @code{gnutls_transport_set_ptr}, making it clear that this
222 procedure applies to session.
224 @node Representation of Binary Data
225 @subsection Representation of Binary Data
227 Many procedures operate on binary data. For instance,
228 @code{pkcs3-import-dh-parameters} expects binary data as input and,
229 similarly, procedures like @code{pkcs1-export-rsa-parameters} return
233 @cindex homogeneous vector
235 Binary data is represented on the Scheme side using SRFI-4 homogeneous
236 vectors (@pxref{SRFI-4,,, guile, The GNU Guile Reference Manual}).
237 Although any type of homogeneous vector may be used, @code{u8vector}s
238 (i.e., vectors of bytes) are highly recommended.
240 As an example, generating and then exporting RSA parameters in the PEM
241 format can be done as follows:
243 @findex make-rsa-parameters
244 @findex pkcs1-export-rsa-parameters
245 @vindex x509-certificate-format/pem
248 (let* ((rsa-params (make-rsa-parameters 1024))
250 (pkcs1-export-rsa-parameters rsa-params
251 x509-certificate-format/pem)))
252 (uniform-vector-write raw-data (open-output-file "some-file.pem")))
255 For an example of OpenPGP key import from a file, see @ref{Importing
256 OpenPGP Keys Guile Example}.
259 @node Input and Output
260 @subsection Input and Output
262 @findex set-session-transport-port!
263 @findex set-session-transport-fd!
265 The underlying transport of a TLS session can be any Scheme
266 input/output port (@pxref{Ports and File Descriptors,,, guile, The GNU
267 Guile Reference Manual}). This has to be specified using
268 @code{set-session-transport-port!}.
270 However, for better performance, a raw file descriptor can be
271 specified, using @code{set-session-transport-fd!}. For instance, if
272 the transport layer is a socket port over an OS-provided socket, you
273 can use the @code{port->fdes} or @code{fileno} procedure to obtain the
274 underlying file descriptor and pass it to
275 @code{set-session-transport-fd!} (@pxref{Ports and File Descriptors,
276 @code{port->fdes} and @code{fileno},, guile, The GNU Guile Reference
277 Manual}). This would work as follows:
280 (let ((socket (socket PF_INET SOCK_STREAM 0))
281 (session (make-session connection-end/client)))
284 ;; Establish a TCP connection...
287 ;; Use the file descriptor that underlies SOCKET.
288 (set-session-transport-fd! session (fileno socket)))
291 @findex session-record-port
293 Once a TLS session is established, data can be communicated through it
294 (i.e., @emph{via} the TLS record layer) using the port returned by
295 @code{session-record-port}:
298 (let ((session (make-session connection-end/client)))
301 ;; Initialize the various parameters of SESSION, set up
302 ;; a network connection, etc...
305 (let ((i/o (session-record-port session)))
306 (write "Hello peer!" i/o)
307 (let ((greetings (read i/o)))
311 (bye session close-request/rdwr))))
315 @findex record-receive!
317 A lower-level I/O API is provided by @code{record-send} and
318 @code{record-receive!} which take an SRFI-4 vector to represent the
319 data sent or received. While it might improve performance, it is much
320 less convenient than the above and should rarely be needed.
323 @node Exception Handling
324 @subsection Exception Handling
328 @cindex @code{gnutls-error}
329 @findex error->string
331 GnuTLS errors are implemented as Scheme exceptions (@pxref{Exceptions,
332 exceptions in Guile,, guile, The GNU Guile Reference Manual}). Each
333 time a GnuTLS function returns an error, an exception with key
334 @code{gnutls-error} is raised. The additional arguments that are
335 thrown include an error code and the name of the GnuTLS procedure that
336 raised the exception. The error code is pretty much like an enumerate
337 value: it is one of the @code{error/} variables exported by the
338 @code{(gnutls)} module (@pxref{Enumerates and Constants}). Exceptions
339 can be turned into error messages using the @code{error->string}
342 The following examples illustrates how GnuTLS exceptions can be
346 (let ((session (make-session connection-end/server)))
355 (lambda (key err function . currently-unused)
356 (format (current-error-port)
357 "a GnuTLS error was raised by `~a': ~a~%"
358 function (error->string err)))))
361 Again, error values can be compared using @code{eq?}:
364 ;; `gnutls-error' handler.
365 (lambda (key err function . currently-unused)
366 (if (eq? err error/fatal-alert-received)
367 (format (current-error-port)
368 "a fatal alert was caught!~%")
369 (format (current-error-port)
370 "something bad happened: ~a~%"
371 (error->string err))))
374 Note that the @code{catch} handler is currently passed only 3
375 arguments but future versions might provide it with additional
376 arguments. Thus, it must be prepared to handle more than 3 arguments,
380 @c *********************************************************************
382 @section Guile Examples
384 This chapter provides examples that illustrate common use cases.
387 * Anonymous Authentication Guile Example:: Simplest client and server.
388 * OpenPGP Authentication Guile Example:: Using OpenPGP-based authentication.
389 * Importing OpenPGP Keys Guile Example:: Importing keys from files.
392 @node Anonymous Authentication Guile Example
393 @subsection Anonymous Authentication Guile Example
395 @dfn{Anonymous authentication} is very easy to use. No certificates
396 are needed by the communicating parties. Yet, it allows them to
397 benefit from end-to-end encryption and integrity checks.
399 The client-side code would look like this (assuming @var{some-socket}
400 is bound to an open socket port):
402 @vindex connection-end/client
404 @vindex close-request/rdwr
409 (let ((client (make-session connection-end/client)))
410 ;; Use the default settings.
411 (set-session-default-priority! client)
413 ;; Don't use certificate-based authentication.
414 (set-session-certificate-type-priority! client '())
416 ;; Request the "anonymous Diffie-Hellman" key exchange method.
417 (set-session-kx-priority! client (list kx/anon-dh))
419 ;; Specify the underlying socket.
420 (set-session-transport-fd! client (fileno some-socket))
422 ;; Create anonymous credentials.
423 (set-session-credentials! client
424 (make-anonymous-client-credentials))
426 ;; Perform the TLS handshake with the server.
429 ;; Send data over the TLS record layer.
430 (write "hello, world!" (session-record-port client))
432 ;; Terminate the TLS session.
433 (bye client close-request/rdwr))
436 The corresponding server would look like this (again, assuming
437 @var{some-socket} is bound to a socket port):
439 @vindex connection-end/server
444 (let ((server (make-session connection-end/server)))
445 (set-session-default-priority! server)
446 (set-session-certificate-type-priority! server '())
447 (set-session-kx-priority! server (list kx/anon-dh))
449 ;; Specify the underlying transport socket.
450 (set-session-transport-fd! server (fileno some-socket))
452 ;; Create anonymous credentials.
453 (let ((cred (make-anonymous-server-credentials))
454 (dh-params (make-dh-parameters 1024)))
455 ;; Note: DH parameter generation can take some time.
456 (set-anonymous-server-dh-parameters! cred dh-params)
457 (set-session-credentials! server cred))
459 ;; Perform the TLS handshake with the client.
462 ;; Receive data over the TLS record layer.
463 (let ((message (read (session-record-port server))))
464 (format #t "received the following message: ~a~%"
467 (bye server close-request/rdwr)))
473 @node OpenPGP Authentication Guile Example
474 @subsection OpenPGP Authentication Guile Example
476 GnuTLS allows users to authenticate using OpenPGP certificates. The
477 relevant procedures are provided by the @code{(gnutls extra)} module.
478 Using OpenPGP-based authentication is not more complicated than using
479 anonymous authentication. It requires a bit of extra work, though, to
480 import the OpenPGP public and private key of the client/server. Key
481 import is omitted here and is left as an exercise to the reader
482 (@pxref{Importing OpenPGP Keys Guile Example}).
484 Assuming @var{some-socket} is bound to an open socket port and
485 @var{pub} and @var{sec} are bound to the client's OpenPGP public and
486 secret key, respectively, client-side code would look like this:
488 @vindex certificate-type/openpgp
493 (define %certs (list certificate-type/openpgp))
495 (let ((client (make-session connection-end/client))
496 (cred (make-certificate-credentials)))
497 (set-session-default-priority! client)
499 ;; Choose OpenPGP certificates.
500 (set-session-certificate-type-priority! client %certs)
502 ;; Prepare appropriate client credentials.
503 (set-certificate-credentials-openpgp-keys! cred pub sec)
504 (set-session-credentials! client cred)
506 ;; Specify the underlying transport socket.
507 (set-session-transport-fd! client (fileno some-socket))
510 (write "hello, world!" (session-record-port client))
511 (bye client close-request/rdwr))
514 Similarly, server-side code would be along these lines:
519 (define %certs (list certificate-type/openpgp))
521 (let ((server (make-session connection-end/server))
522 (rsa (make-rsa-parameters 1024))
523 (dh (make-dh-parameters 1024)))
524 (set-session-default-priority! server)
526 ;; Choose OpenPGP certificates.
527 (set-session-certificate-type-priority! server %certs)
529 (let ((cred (make-certificate-credentials)))
530 ;; Prepare credentials with RSA and Diffie-Hellman parameters.
531 (set-certificate-credentials-dh-parameters! cred dh)
532 (set-certificate-credentials-rsa-export-parameters! cred rsa)
533 (set-certificate-credentials-openpgp-keys! cred pub sec)
534 (set-session-credentials! server cred))
536 (set-session-transport-fd! server (fileno some-socket))
539 (let ((msg (read (session-record-port server))))
540 (format #t "received: ~a~%" msg)
542 (bye server close-request/rdwr)))
545 In practice, generating RSA parameters (and Diffie-Hellman parameters)
546 can time a long time. Thus, you may want to generate them once and
547 store them in a file for future re-use (@pxref{Core Interface,
548 @code{pkcs1-export-rsa-parameters} and
549 @code{pkcs1-import-rsa-parameters}}).
551 @node Importing OpenPGP Keys Guile Example
552 @subsection Importing OpenPGP Keys Guile Example
554 The following example provides a simple way of importing
555 ``ASCII-armored'' OpenPGP keys from files, using the
556 @code{import-openpgp-certificate} and @code{import-openpgp-private-key}
557 procedures provided by the @code{(gnutls extra)} module.
559 @vindex openpgp-certificate-format/base64
560 @vindex openpgp-certificate-format/raw
563 (use-modules (srfi srfi-4)
566 (define (import-key-from-file import-proc file)
567 ;; Import OpenPGP key from FILE using IMPORT-PROC.
569 ;; Prepare a u8vector large enough to hold the raw
571 (let* ((size (stat:size (stat path)))
572 (raw (make-u8vector size)))
574 ;; Fill in the u8vector with the contents of FILE.
575 (uniform-vector-read! raw (open-input-file file))
577 ;; Pass the u8vector to the import procedure.
578 (import-proc raw openpgp-certificate-format/base64)))
581 (define (import-public-key-from-file file)
582 (import-key-from-file import-openpgp-certificate file))
584 (define (import-private-key-from-file file)
585 (import-key-from-file import-openpgp-private-key file))
588 The procedures @code{import-public-key-from-file} and
589 @code{import-private-key-from-file} can be passed a file name. They
590 return an OpenPGP public key and private key object, respectively
591 (@pxref{Extra Interface, OpenPGP key objects}).
594 @c *********************************************************************
595 @node Guile Reference
596 @section Guile Reference
598 This chapter documents GnuTLS Scheme procedures available to Guile
602 * Core Interface:: Bindings for core GnuTLS.
603 * Extra Interface:: Bindings for GnuTLS-Extra.
607 @subsection Core Interface
609 This section lists the Scheme procedures exported by the
610 @code{(gnutls)} module (@pxref{The Guile module system,,, guile, The
611 GNU Guile Reference Manual}). This module is licenced under the GNU
612 Lesser General Public Licence, version 2.1 or later.
616 @node Extra Interface
617 @subsection Extra Interface
619 This section lists the Scheme procedures exported by the @code{(gnutls
620 extra)} module. This module is licenced under the GNU General Public
621 Licence, version 3 or later.
623 @include extra.c.texi
628 ;;; arch-tag: ee5f2081-9153-48fc-b4ee-2024381c65d7
632 @c ispell-local-dictionary: "american"