Debian Codegraph @

guile/tests/x509-auth.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2007-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This program is free software: you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation, either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 ;;; Written by Ludovic Courtès <[email protected]>.
20
21
22 ;;;
23 ;;; Test session establishment using X.509 certificate authentication.
24 ;;; Based on `openpgp-auth.scm'.
25 ;;;
26
27 (use-modules (gnutls)
28 (gnutls build tests)
29 (srfi srfi-4))
30
31 ;; TLS session settings.
32 (define priorities
33 "NORMAL")
34
35 ;; Message sent by the client.
36 (define %message
37 (cons "hello, world!" (iota 4444)))
38
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)))
45
46 (define (import-key import-proc file)
47 (import-something import-proc file x509-certificate-format/pem))
48
49 (define (import-dh-params file)
50 (import-something pkcs3-import-dh-parameters file
51 x509-certificate-format/pem))
52
53 ;; Debugging.
54 ;; (set-log-level! 3)
55 ;; (set-log-procedure! (lambda (level str)
56 ;; (format #t "[~a|~a] ~a" (getpid) level str)))
57
58 (run-test
59 (lambda ()
60 (let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0))
61 (pub (import-key import-x509-certificate
62 "x509-certificate.pem"))
63 (sec (import-key import-x509-private-key
64 "x509-key.pem")))
65 (with-child-process pid
66
67 ;; server-side
68 (let ((server (make-session connection-end/server))
69 (dh (import-dh-params "dh-parameters.pem")))
70 (set-session-priorities! server priorities)
71 (set-server-session-certificate-request! server
72 certificate-request/require)
73
74 (set-session-transport-fd! server (port->fdes (cdr socket-pair)))
75 (let ((cred (make-certificate-credentials))
76 (trust-file (search-path %load-path
77 "x509-certificate.pem"))
78 (trust-fmt x509-certificate-format/pem))
79 (set-certificate-credentials-dh-parameters! cred dh)
80 (set-certificate-credentials-x509-keys! cred (list pub) sec)
81 (set-certificate-credentials-x509-trust-file! cred
82 trust-file
83 trust-fmt)
84 (set-session-credentials! server cred))
85 (set-session-dh-prime-bits! server 1024)
86
87 (handshake server)
88 (let ((msg (read (session-record-port server)))
89 (auth-type (session-authentication-type server)))
90 (bye server close-request/rdwr)
91 (and (zero? (cdr (waitpid pid)))
92 (eq? auth-type credentials/certificate)
93 (equal? msg %message))))
94
95 ;; client-side (child process)
96 (let ((client (make-session connection-end/client))
97 (cred (make-certificate-credentials)))
98 (set-session-priorities! client priorities)
99 (set-certificate-credentials-x509-keys! cred (list pub) sec)
100 (set-session-credentials! client cred)
101 (set-session-dh-prime-bits! client 1024)
102
103 (set-session-transport-fd! client (port->fdes (car socket-pair)))
104
105 (handshake client)
106 (write %message (session-record-port client))
107 (bye client close-request/rdwr)
108
109 (primitive-exit))))))
110
111 ;;; arch-tag: 1f88f835-a5c8-4fd6-94b6-5a13571ba03d