Debian Codegraph @

guile/tests/reauth.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2019-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 TLS 1.3 re-authentication requests.
24 ;;;
25
26 (use-modules (gnutls)
27 (gnutls build tests)
28 (srfi srfi-4))
29
30
31 ;; TLS session settings.
32 (define priorities
33 "NORMAL:+VERS-TLS1.3")
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! 5)
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 connection-flag/post-handshake-auth))
70 (dh (import-dh-params "dh-parameters.pem")))
71 (set-session-priorities! server "NORMAL:+VERS-TLS1.3")
72 (set-session-transport-fd! server (port->fdes (cdr socket-pair)))
73 (let ((cred (make-certificate-credentials))
74 (trust-file (search-path %load-path
75 "x509-certificate.pem"))
76 (trust-fmt x509-certificate-format/pem))
77 (set-certificate-credentials-dh-parameters! cred dh)
78 (set-certificate-credentials-x509-keys! cred (list pub) sec)
79 (set-certificate-credentials-x509-trust-file! cred
80 trust-file
81 trust-fmt)
82 (set-session-credentials! server cred))
83
84 (handshake server)
85 (let ((msg (read (session-record-port server)))
86 (auth-type (session-authentication-type server)))
87 (set-server-session-certificate-request! server
88 certificate-request/request)
89
90 ;; Request a post-handshake reauthentication.
91 (reauthenticate server)
92
93 (write msg (session-record-port server))
94 (bye server close-request/rdwr)
95 (and (zero? (cdr (waitpid pid)))
96 (eq? auth-type credentials/certificate)
97 (equal? msg %message))))
98
99 ;; client-side (child process)
100 (let ((client (make-session connection-end/client
101 connection-flag/post-handshake-auth
102 connection-flag/auto-reauth))
103 (cred (make-certificate-credentials)))
104 (set-session-priorities! client
105 "NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.0")
106 (set-certificate-credentials-x509-keys! cred (list pub) sec)
107 (set-session-credentials! client cred)
108
109 (set-session-transport-fd! client (port->fdes (car socket-pair)))
110
111 (handshake client)
112 (write %message (session-record-port client))
113
114 ;; In the middle of the 'read' call, we receive a post-handshake
115 ;; reauthentication request that should be automatically handled,
116 ;; thanks to CONNECTION-FLAG/AUTO-REAUTH.
117 (let ((msg (read (session-record-port client))))
118 (unless (equal? msg %message)
119 (error "wrong message" msg)))
120 (bye client close-request/rdwr)
121
122 (primitive-exit))))))