Guile: Fix `x509-certificate-dn-oid' and related functions.
[gnutls.git] / doc / guile.texi
blob53c0e50f765497ac3184e9ee99a656fd43122642
1 @c Documentation of the GNU Guile bindings.
3 @node 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,
10 the C API reference}.
12 At this stage, not all the C functions are available from Scheme, but
13 a large subset thereof is available.
16 @menu
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.
21 @end menu
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
31 this:
33 @example
34 $ guile
35 guile> (use-modules (gnutls))
36 <unnamed port>: no code for module (gnutls)
37 guile>
38 @end example
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:
45 @example
46 $ ./configure --with-guile-site-dir=no
47 @end example
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:
56 @example
57 $ ./configure --prefix=/usr
58 @end example
60 If you want to specify the path to install the Guile bindings you can
61 also specify the path directly:
63 @example
64 $ ./configure --with-guile-site-dir=/opt/guile/share/guile/site
65 @end example
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:
72 @example
73 $ GUILE_LOAD_PATH="/usr/local/share/guile/site:$GUILE_LOAD_PATH" guile
74 guile> (use-modules (gnutls))
75 guile>
76 @end example
78 Alternatively, you can modify Guile's @code{%load-path} variable
79 (@pxref{Build Config, Guile's run-time options,, guile, The GNU Guile
80 Reference Manual}).
82 At this point, you might get an error regarding
83 @file{libguile-gnutls-v-0} similar to:
85 @example
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"
88 @end example
90 In this case, you will need to modify the run-time linker path, for
91 example as follows:
93 @example
94 $ LD_LIBRARY_PATH=/usr/local/lib GUILE_LOAD_PATH=/usr/local/share/guile/site guile
95 guile> (use-modules (gnutls))
96 guile>
97 @end example
100 @c *********************************************************************
101 @node Guile API Conventions
102 @section Guile API Conventions
104 This chapter details the conventions used by Guile API, as well as
105 specificities of the mapping of the C API to Scheme.
107 @menu
108 * Enumerates and Constants::      Representation of C-side constants.
109 * Procedure Names::               Naming conventions.
110 * Representation of Binary Data:: Binary data buffers.
111 * Input and Output::              Input and output.
112 * Exception Handling::            Exceptions.
113 @end menu
115 @node Enumerates and Constants
116 @subsection Enumerates and Constants
118 @cindex enumerate
119 @cindex constant
121 Lots of enumerates and constants are used in the GnuTLS C API.  For
122 each C enumerate type, a disjoint Scheme type is used---thus,
123 enumerate values and constants are not represented by Scheme symbols
124 nor by integers.  This makes it impossible to use an enumerate value
125 of the wrong type on the Scheme side: such errors are automatically
126 detected by type-checking.
128 The enumerate values are bound to variables exported by the
129 @code{(gnutls)} and @code{(gnutls extra)} modules.  These variables
130 are named according to the following convention:
132 @itemize
133 @item All variable names are lower-case; the underscore @code{_}
134 character used in the C API is replaced by hyphen @code{-}.
135 @item All variable names are prepended by the name of the enumerate
136 type and the slash @code{/} character.
137 @item In some cases, the variable name is made more explicit than the
138 one of the C API, e.g., by avoid abbreviations.
139 @end itemize
141 Consider for instance this C-side enumerate:
143 @example
144 typedef enum
146   GNUTLS_CRD_CERTIFICATE = 1,
147   GNUTLS_CRD_ANON,
148   GNUTLS_CRD_SRP,
149   GNUTLS_CRD_PSK,
150   GNUTLS_CRD_IA
151 @} gnutls_credentials_type_t;
152 @end example
154 The corresponding Scheme values are bound to the following variables
155 exported by the @code{(gnutls)} module:
157 @example
158 credentials/certificate
159 credentials/anonymous
160 credentials/srp
161 credentials/psk
162 credentials/ia
163 @end example
165 Hopefully, most variable names can be deduced from this convention.
167 Scheme-side ``enumerate'' values can be compared using @code{eq?}
168 (@pxref{Equality, equality predicates,, guile, The GNU Guile Reference
169 Manual}).  Consider the following example:
171 @findex session-cipher
173 @example
174 (let ((session (make-session connection-end/client)))
176   ;;
177   ;; ...
178   ;;
180   ;; Check the ciphering algorithm currently used by SESSION.
181   (if (eq? cipher/arcfour (session-cipher session))
182       (format #t "We're using the ARCFOUR algorithm")))
183 @end example
185 In addition, all enumerate values can be converted to a human-readable
186 string, in a type-specific way.  For instance, @code{(cipher->string
187 cipher/arcfour)} yields @code{"ARCFOUR 128"}, while
188 @code{(key-usage->string key-usage/digital-signature)} yields
189 @code{"digital-signature"}.  Note that these strings may not be
190 sufficient for use in a user interface since they are fairly concise
191 and not internationalized.
194 @node Procedure Names
195 @subsection Procedure Names
197 Unlike C functions in GnuTLS, the corresponding Scheme procedures are
198 named in a way that is close to natural English.  Abbreviations are
199 also avoided.  For instance, the Scheme procedure corresponding to
200 @code{gnutls_certificate_set_dh_params} is named
201 @code{set-certificate-credentials-dh-parameters!}.  The @code{gnutls_}
202 prefix is always omitted from variable names since a similar effect
203 can be achieved using Guile's nifty binding renaming facilities,
204 should it be needed (@pxref{Using Guile Modules,,, guile, The GNU
205 Guile Reference Manual}).
207 Often Scheme procedure names differ from C function names in a way
208 that makes it clearer what objects they operate on.  For example, the
209 Scheme procedure named @code{set-session-transport-port!} corresponds
210 to @code{gnutls_transport_set_ptr}, making it clear that this
211 procedure applies to session.
213 @node Representation of Binary Data
214 @subsection Representation of Binary Data
216 Many procedures operate on binary data.  For instance,
217 @code{pkcs3-import-dh-parameters} expects binary data as input and,
218 similarly, procedures like @code{pkcs1-export-rsa-parameters} return
219 binary data.
221 @cindex SRFI-4
222 @cindex homogeneous vector
224 Binary data is represented on the Scheme side using SRFI-4 homogeneous
225 vectors (@pxref{SRFI-4,,, guile, The GNU Guile Reference Manual}).
226 Although any type of homogeneous vector may be used, @code{u8vector}s
227 (i.e., vectors of bytes) are highly recommended.
229 As an example, generating and then exporting RSA parameters in the PEM
230 format can be done as follows:
232 @findex make-rsa-parameters
233 @findex pkcs1-export-rsa-parameters
234 @vindex x509-certificate-format/pem
236 @example
237 (let* ((rsa-params (make-rsa-parameters 1024))
238        (raw-data
239         (pkcs1-export-rsa-parameters rsa-params
240                                      x509-certificate-format/pem)))
241   (uniform-vector-write raw-data (open-output-file "some-file.pem")))
242 @end example
244 For an example of OpenPGP key import from a file, see @ref{Importing
245 OpenPGP Keys Guile Example}.
248 @node Input and Output
249 @subsection Input and Output
251 @findex set-session-transport-port!
252 @findex set-session-transport-fd!
254 The underlying transport of a TLS session can be any Scheme
255 input/output port (@pxref{Ports and File Descriptors,,, guile, The GNU
256 Guile Reference Manual}).  This has to be specified using
257 @code{set-session-transport-port!}.
259 However, for better performance, a raw file descriptor can be
260 specified, using @code{set-session-transport-fd!}.  For instance, if
261 the transport layer is a socket port over an OS-provided socket, you
262 can use the @code{port->fdes} or @code{fileno} procedure to obtain the
263 underlying file descriptor and pass it to
264 @code{set-session-transport-fd!}  (@pxref{Ports and File Descriptors,
265 @code{port->fdes} and @code{fileno},, guile, The GNU Guile Reference
266 Manual}).  This would work as follows:
268 @example
269 (let ((socket (socket PF_INET SOCK_STREAM 0))
270       (session (make-session connection-end/client)))
272   ;;
273   ;; Establish a TCP connection...
274   ;;
276   ;; Use the file descriptor that underlies SOCKET.
277   (set-session-transport-fd! session (fileno socket)))
278 @end example
280 @findex session-record-port
282 Once a TLS session is established, data can be communicated through it
283 (i.e., @emph{via} the TLS record layer) using the port returned by
284 @code{session-record-port}:
286 @example
287 (let ((session (make-session connection-end/client)))
289   ;;
290   ;; Initialize the various parameters of SESSION, set up
291   ;; a network connection, etc...
292   ;;
294   (let ((i/o (session-record-port session)))
295     (write "Hello peer!" i/o)
296     (let ((greetings (read i/o)))
298       ;; ...
300       (bye session close-request/rdwr))))
301 @end example
303 @findex record-send
304 @findex record-receive!
306 A lower-level I/O API is provided by @code{record-send} and
307 @code{record-receive!} which take an SRFI-4 vector to represent the
308 data sent or received.  While it might improve performance, it is much
309 less convenient than the above and should rarely be needed.
312 @node Exception Handling
313 @subsection Exception Handling
315 @cindex exceptions
316 @cindex errors
317 @cindex @code{gnutls-error}
318 @findex error->string
320 GnuTLS errors are implemented as Scheme exceptions (@pxref{Exceptions,
321 exceptions in Guile,, guile, The GNU Guile Reference Manual}).  Each
322 time a GnuTLS function returns an error, an exception with key
323 @code{gnutls-error} is raised.  The additional arguments that are
324 thrown include an error code and the name of the GnuTLS procedure that
325 raised the exception.  The error code is pretty much like an enumerate
326 value: it is one of the @code{error/} variables exported by the
327 @code{(gnutls)} module (@pxref{Enumerates and Constants}).  Exceptions
328 can be turned into error messages using the @code{error->string}
329 procedure.
331 The following examples illustrates how GnuTLS exceptions can be
332 handled:
334 @example
335 (let ((session (make-session connection-end/server)))
337   ;;
338   ;; ...
339   ;;
341   (catch 'gnutls-error
342     (lambda ()
343       (handshake session))
344     (lambda (key err function . currently-unused)
345       (format (current-error-port)
346               "a GnuTLS error was raised by `~a': ~a~%"
347               function (error->string err)))))
348 @end example
350 Again, error values can be compared using @code{eq?}:
352 @example
353     ;; `gnutls-error' handler.
354     (lambda (key err function . currently-unused)
355       (if (eq? err error/fatal-alert-received)
356           (format (current-error-port)
357                   "a fatal alert was caught!~%")
358           (format (current-error-port)
359                   "something bad happened: ~a~%"
360                   (error->string err))))
361 @end example
363 Note that the @code{catch} handler is currently passed only 3
364 arguments but future versions might provide it with additional
365 arguments.  Thus, it must be prepared to handle more than 3 arguments,
366 as in this example.
369 @c *********************************************************************
370 @node Guile Examples
371 @section Guile Examples
373 This chapter provides examples that illustrate common use cases.
375 @menu
376 * Anonymous Authentication Guile Example::    Simplest client and server.
377 * OpenPGP Authentication Guile Example::      Using OpenPGP-based authentication.
378 * Importing OpenPGP Keys Guile Example::      Importing keys from files.
379 @end menu
381 @node Anonymous Authentication Guile Example
382 @subsection Anonymous Authentication Guile Example
384 @dfn{Anonymous authentication} is very easy to use.  No certificates
385 are needed by the communicating parties.  Yet, it allows them to
386 benefit from end-to-end encryption and integrity checks.
388 The client-side code would look like this (assuming @var{some-socket}
389 is bound to an open socket port):
391 @vindex connection-end/client
392 @vindex kx/anon-dh
393 @vindex close-request/rdwr
395 @example
396 ;; Client-side.
398 (let ((client (make-session connection-end/client)))
399   ;; Use the default settings.
400   (set-session-default-priority! client)
402   ;; Don't use certificate-based authentication.
403   (set-session-certificate-type-priority! client '())
405   ;; Request the "anonymous Diffie-Hellman" key exchange method.
406   (set-session-kx-priority! client (list kx/anon-dh))
408   ;; Specify the underlying socket.
409   (set-session-transport-fd! client (fileno some-socket))
411   ;; Create anonymous credentials.
412   (set-session-credentials! client
413                             (make-anonymous-client-credentials))
415   ;; Perform the TLS handshake with the server.
416   (handshake client)
418   ;; Send data over the TLS record layer.
419   (write "hello, world!" (session-record-port client))
421   ;; Terminate the TLS session.
422   (bye client close-request/rdwr))
423 @end example
425 The corresponding server would look like this (again, assuming
426 @var{some-socket} is bound to a socket port):
428 @vindex connection-end/server
430 @example
431 ;; Server-side.
433 (let ((server (make-session connection-end/server)))
434   (set-session-default-priority! server)
435   (set-session-certificate-type-priority! server '())
436   (set-session-kx-priority! server (list kx/anon-dh))
438   ;; Specify the underlying transport socket.
439   (set-session-transport-fd! server (fileno some-socket))
441   ;; Create anonymous credentials.
442   (let ((cred (make-anonymous-server-credentials))
443         (dh-params (make-dh-parameters 1024)))
444     ;; Note: DH parameter generation can take some time.
445     (set-anonymous-server-dh-parameters! cred dh-params)
446     (set-session-credentials! server cred))
448   ;; Perform the TLS handshake with the client.
449   (handshake server)
451   ;; Receive data over the TLS record layer.
452   (let ((message (read (session-record-port server))))
453     (format #t "received the following message: ~a~%"
454             message)
456     (bye server close-request/rdwr)))
457 @end example
459 This is it!
462 @node OpenPGP Authentication Guile Example
463 @subsection OpenPGP Authentication Guile Example
465 GnuTLS allows users to authenticate using OpenPGP certificates.  The
466 relevant procedures are provided by the @code{(gnutls extra)} module.
467 Using OpenPGP-based authentication is not more complicated than using
468 anonymous authentication.  It requires a bit of extra work, though, to
469 import the OpenPGP public and private key of the client/server.  Key
470 import is omitted here and is left as an exercise to the reader
471 (@pxref{Importing OpenPGP Keys Guile Example}).
473 Assuming @var{some-socket} is bound to an open socket port and
474 @var{pub} and @var{sec} are bound to the client's OpenPGP public and
475 secret key, respectively, client-side code would look like this:
477 @vindex certificate-type/openpgp
479 @example
480 ;; Client-side.
482 (define %certs (list certificate-type/openpgp))
484 (let ((client (make-session connection-end/client))
485       (cred   (make-certificate-credentials)))
486   (set-session-default-priority! client)
488   ;; Choose OpenPGP certificates.
489   (set-session-certificate-type-priority! client %certs)
491   ;; Prepare appropriate client credentials.
492   (set-certificate-credentials-openpgp-keys! cred pub sec)
493   (set-session-credentials! client cred)
495   ;; Specify the underlying transport socket.
496   (set-session-transport-fd! client (fileno some-socket))
498   (handshake client)
499   (write "hello, world!" (session-record-port client))
500   (bye client close-request/rdwr))
501 @end example
503 Similarly, server-side code would be along these lines:
505 @example
506 ;; Server-side.
508 (define %certs (list certificate-type/openpgp))
510 (let ((server (make-session connection-end/server))
511       (rsa    (make-rsa-parameters 1024))
512       (dh     (make-dh-parameters 1024)))
513   (set-session-default-priority! server)
515   ;; Choose OpenPGP certificates.
516   (set-session-certificate-type-priority! server %certs)
518   (let ((cred (make-certificate-credentials)))
519     ;; Prepare credentials with RSA and Diffie-Hellman parameters.
520     (set-certificate-credentials-dh-parameters! cred dh)
521     (set-certificate-credentials-rsa-export-parameters! cred rsa)
522     (set-certificate-credentials-openpgp-keys! cred pub sec)
523     (set-session-credentials! server cred))
525   (set-session-transport-fd! server (fileno some-socket))
527   (handshake server)
528   (let ((msg (read (session-record-port server))))
529     (format #t "received: ~a~%" msg)
531     (bye server close-request/rdwr)))
532 @end example
534 In practice, generating RSA parameters (and Diffie-Hellman parameters)
535 can time a long time.  Thus, you may want to generate them once and
536 store them in a file for future re-use (@pxref{Core Interface,
537 @code{pkcs1-export-rsa-parameters} and
538 @code{pkcs1-import-rsa-parameters}}).
540 @node Importing OpenPGP Keys Guile Example
541 @subsection Importing OpenPGP Keys Guile Example
543 The following example provides a simple way of importing
544 ``ASCII-armored'' OpenPGP keys from files, using the
545 @code{import-openpgp-public-key} and @code{import-openpgp-private-key}
546 procedures provided by the @code{(gnutls extra)} module.
548 @vindex openpgp-key-format/base64
549 @vindex openpgp-key-format/raw
551 @example
552 (use-modules (srfi srfi-4)
553              (gnutls extra))
555 (define (import-key-from-file import-proc file)
556   ;; Import OpenPGP key from FILE using IMPORT-PROC.
558   ;; Prepare a u8vector large enough to hold the raw
559   ;; key contents.
560   (let* ((size (stat:size (stat path)))
561          (raw  (make-u8vector size)))
563     ;; Fill in the u8vector with the contents of FILE.
564     (uniform-vector-read! raw (open-input-file file))
566     ;; Pass the u8vector to the import procedure.
567     (import-proc raw openpgp-key-format/base64)))
570 (define (import-public-key-from-file file)
571   (import-key-from-file import-openpgp-public-key file))
573 (define (import-private-key-from-file file)
574   (import-key-from-file import-openpgp-private-key file))
575 @end example
577 The procedures @code{import-public-key-from-file} and
578 @code{import-private-key-from-file} can be passed a file name.  They
579 return an OpenPGP public key and private key object, respectively
580 (@pxref{Extra Interface, OpenPGP key objects}).
583 @c *********************************************************************
584 @node Guile Reference
585 @section Guile Reference
587 This chapter documents GnuTLS Scheme procedures available to Guile
588 programmers.
590 @menu
591 * Core Interface::              Bindings for core GnuTLS.
592 * Extra Interface::             Bindings for GnuTLS-Extra.
593 @end menu
595 @node Core Interface
596 @subsection Core Interface
598 This section lists the Scheme procedures exported by the
599 @code{(gnutls)} module (@pxref{The Guile module system,,, guile, The
600 GNU Guile Reference Manual}).  This module is licenced under the GNU
601 Lesser General Public Licence, version 2.1 or later.
603 @include core.c.texi
605 @node Extra Interface
606 @subsection Extra Interface
608 This section lists the Scheme procedures exported by the @code{(gnutls
609 extra)} module.  This module is licenced under the GNU General Public
610 Licence, version 2 or later.
612 @include extra.c.texi
616 @ignore
617 ;;; arch-tag: ee5f2081-9153-48fc-b4ee-2024381c65d7
618 @end ignore
620 @c Local Variables:
621 @c ispell-local-dictionary: "american"
622 @c End: