| 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 anonymous authentication. Exercise the |
| 24 |
;;; record layer low-level API. |
| 25 |
;;; |
| 26 |
|
| 27 |
(use-modules (gnutls) |
| 28 |
(gnutls build tests) |
| 29 |
(srfi srfi-4)) |
| 30 |
|
| 31 |
|
| 32 |
;; TLS session settings. |
| 33 |
(define priorities |
| 34 |
"NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH") |
| 35 |
|
| 36 |
;; Message sent by the client. |
| 37 |
(define %message (apply u8vector (iota 256))) |
| 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-dh-params file) |
| 47 |
(import-something pkcs3-import-dh-parameters file |
| 48 |
x509-certificate-format/pem)) |
| 49 |
|
| 50 |
;; Debugging. |
| 51 |
;; (set-log-level! 100) |
| 52 |
;; (set-log-procedure! (lambda (level str) |
| 53 |
;; (format #t "[~a|~a] ~a" (getpid) level str))) |
| 54 |
|
| 55 |
(run-test |
| 56 |
(lambda () |
| 57 |
(let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0))) |
| 58 |
(with-child-process pid |
| 59 |
;; server-side |
| 60 |
(let ((server (make-session connection-end/server))) |
| 61 |
(set-session-priorities! server priorities) |
| 62 |
|
| 63 |
(set-session-transport-fd! server (port->fdes (cdr socket-pair))) |
| 64 |
(let ((cred (make-anonymous-server-credentials)) |
| 65 |
(dh-params (import-dh-params "dh-parameters.pem"))) |
| 66 |
;; Note: DH parameter generation can take some time. |
| 67 |
(set-anonymous-server-dh-parameters! cred dh-params) |
| 68 |
(set-session-credentials! server cred)) |
| 69 |
(set-session-dh-prime-bits! server 1024) |
| 70 |
|
| 71 |
(handshake server) |
| 72 |
(let* ((buf (make-u8vector (u8vector-length %message))) |
| 73 |
(amount (record-receive! server buf))) |
| 74 |
(bye server close-request/rdwr) |
| 75 |
(and (zero? (cdr (waitpid pid))) |
| 76 |
(= amount (u8vector-length %message)) |
| 77 |
(equal? buf %message)))) |
| 78 |
|
| 79 |
;; client-side (child process) |
| 80 |
(let ((client (make-session connection-end/client))) |
| 81 |
(set-session-priorities! client priorities) |
| 82 |
(set-session-server-name! client |
| 83 |
server-name-type/dns (gethostname)) |
| 84 |
(set-session-transport-fd! client (port->fdes (car socket-pair))) |
| 85 |
(set-session-credentials! client (make-anonymous-client-credentials)) |
| 86 |
(set-session-dh-prime-bits! client 1024) |
| 87 |
|
| 88 |
(handshake client) |
| 89 |
(record-send client %message) |
| 90 |
(bye client close-request/rdwr) |
| 91 |
|
| 92 |
(primitive-exit)))) |
| 93 |
|
| 94 |
;; This was not exactly the manual example (records API...) |
| 95 |
(let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0))) |
| 96 |
(with-child-process pid |
| 97 |
;; server-side |
| 98 |
(let ((some-socket (cdr socket-pair))) |
| 99 |
(let ((output |
| 100 |
(with-output-to-string |
| 101 |
(lambda () |
| 102 |
(include-from-path "anonymous-server.scm"))))) |
| 103 |
(unless (zero? (cdr (waitpid pid))) |
| 104 |
(error "the server failed.")) |
| 105 |
(unless (equal? output "received the following message: hello, world!\n") |
| 106 |
(error "the server did not receive the message.")))) |
| 107 |
|
| 108 |
;; client-side (child process) |
| 109 |
(let ((some-socket (car socket-pair))) |
| 110 |
(include-from-path "anonymous-client.scm") |
| 111 |
(primitive-exit)))))) |
| 112 |
|
| 113 |
;;; arch-tag: 8c98de24-0a53-4290-974e-4b071ad162a0 |