Debian Codegraph @

guile/tests/premature-termination.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2022-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 handling of premature session termination on the client side while
24 ;;; reading from a session record port.
25 ;;;
26
27 (use-modules (gnutls)
28 (gnutls build tests)
29 (srfi srfi-4))
30
31 ;; TLS session settings.
32 (define priorities
33 "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH")
34
35 ;; Message sent by the client.
36 (define %message (apply u8vector (iota 256)))
37
38 (define (import-something import-proc file fmt)
39 (let* ((path (search-path %load-path file))
40 (size (stat:size (stat path)))
41 (raw (make-u8vector size)))
42 (uniform-vector-read! raw (open-input-file path))
43 (import-proc raw fmt)))
44
45 (define (import-dh-params file)
46 (import-something pkcs3-import-dh-parameters file
47 x509-certificate-format/pem))
48
49 ;; Debugging.
50 ;; (set-log-level! 100)
51 ;; (set-log-procedure! (lambda (level str)
52 ;; (format #t "[~a|~a] ~a" (getpid) level str)))
53
54 (run-test
55 (lambda ()
56 (let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0)))
57 (with-child-process pid
58 ;; server-side
59 (let ((server (make-session connection-end/server)))
60 (close-port (car socket-pair)) ;close the client end
61 (set-session-priorities! server priorities)
62 (set-session-transport-fd! server (fileno (cdr socket-pair)))
63 (let ((cred (make-anonymous-server-credentials))
64 (dh-params (import-dh-params "dh-parameters.pem")))
65 ;; Note: DH parameter generation can take some time.
66 (set-anonymous-server-dh-parameters! cred dh-params)
67 (set-session-credentials! server cred))
68 (set-session-dh-prime-bits! server 1024)
69
70 (handshake server)
71
72 (alarm 60) ;time out after a while
73 (close-port (cdr socket-pair)) ;close prematurely
74 (zero? (cdr (waitpid pid))))
75
76 ;; client-side (child process)
77 (let ((client (make-session connection-end/client)))
78 (close-port (cdr socket-pair)) ;close the server end
79 (set-session-priorities! client priorities)
80 (set-session-server-name! client
81 server-name-type/dns (gethostname))
82 (set-session-transport-fd! client (port->fdes (car socket-pair)))
83 (set-session-credentials! client (make-anonymous-client-credentials))
84 (set-session-dh-prime-bits! client 1024)
85
86 (handshake client)
87
88 ;; Read from the session record port: instead of getting an
89 ;; 'error/premature-termination' exception, we expect to get EOF.
90 (let* ((port (session-record-port client))
91 (read (read port)))
92 (format #t "client received ~s~%" read)
93 (primitive-exit (if (eof-object? read) 0 1))))))))