corrected copyright notices
[gnutls.git] / guile / tests / openpgp-auth.scm
blobd4f242efb2175b4892366752a37eb59f9933ac9b
1 ;;; GnuTLS-extra --- Guile bindings for GnuTLS-EXTRA.
2 ;;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
3 ;;;
4 ;;; GnuTLS-extra is free software; you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation; either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; GnuTLS-extra is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with GnuTLS-EXTRA; if not, write to the Free Software
16 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 ;;; USA.
19 ;;; Written by Ludovic Courtès <ludo@chbouib.org>.
22 ;;;
23 ;;; Test session establishment using OpenPGP certificate authentication.
24 ;;;
26 (use-modules (gnutls)
27              (gnutls build tests)
28              (srfi srfi-4))
31 ;; TLS session settings.
32 (define priorities
33   "NONE:+VERS-TLS-ALL:+CTYPE-OPENPGP:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+DHE-RSA:+DHE-DSS:+COMP-ALL")
35 ;; Message sent by the client.
36 (define %message
37   (cons "hello, world!" (iota 4444)))
39 (define (import-something import-proc file fmt)
40   (let* ((path (search-path %load-path file))
41          (size (stat:size (stat path)))
42          (raw  (make-u8vector size)))
43     (uniform-vector-read! raw (open-input-file path))
44     (import-proc raw fmt)))
46 (define (import-key import-proc file)
47   (import-something import-proc file openpgp-certificate-format/base64))
49 (define (import-rsa-params file)
50   (import-something pkcs1-import-rsa-parameters file
51                     x509-certificate-format/pem))
53 (define (import-dh-params file)
54   (import-something pkcs3-import-dh-parameters file
55                     x509-certificate-format/pem))
57 ;; Debugging.
58 ;; (set-log-level! 3)
59 ;; (set-log-procedure! (lambda (level str)
60 ;;                       (format #t "[~a|~a] ~a" (getpid) level str)))
62 (run-test
63     (lambda ()
64       (let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0))
65             (pub         (import-key import-openpgp-certificate
66                                      "openpgp-pub.asc"))
67             (sec         (import-key import-openpgp-private-key
68                                      "openpgp-sec.asc")))
69         (let ((pid (primitive-fork)))
70           (if (= 0 pid)
72               (let ((client (make-session connection-end/client))
73                     (cred   (make-certificate-credentials)))
74                 ;; client-side (child process)
75                 (set-session-priorities! client priorities)
77                 (set-certificate-credentials-openpgp-keys! cred pub sec)
78                 (set-session-credentials! client cred)
79                 (set-session-dh-prime-bits! client 1024)
81                 (set-session-transport-fd! client (fileno (car socket-pair)))
83                 (handshake client)
84                 (write %message (session-record-port client))
85                 (bye client close-request/rdwr)
87                 (primitive-exit))
89               (let ((server (make-session connection-end/server))
90                     (rsa    (import-rsa-params "rsa-parameters.pem"))
91                     (dh     (import-dh-params "dh-parameters.pem")))
92                 ;; server-side
93                 (set-session-priorities! server priorities)
94                 (set-server-session-certificate-request! server
95                          certificate-request/require)
97                 (set-session-transport-fd! server (fileno (cdr socket-pair)))
98                 (let ((cred (make-certificate-credentials)))
99                   (set-certificate-credentials-dh-parameters! cred dh)
100                   (set-certificate-credentials-rsa-export-parameters! cred rsa)
101                   (set-certificate-credentials-openpgp-keys! cred pub sec)
102                   (set-session-credentials! server cred))
103                 (set-session-dh-prime-bits! server 1024)
105                 (handshake server)
106                 (let ((msg (read (session-record-port server)))
107                       (auth-type (session-authentication-type server)))
108                   (bye server close-request/rdwr)
109                   (and (eq? auth-type credentials/certificate)
110                        (equal? msg %message)))))))))
112 ;;; arch-tag: 1a973ed5-f45d-45a4-8160-900b6a8c27ff