| 1 |
;;; GnuTLS --- Guile bindings for GnuTLS |
| 2 |
;;; Copyright (C) 2011-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 |
;; Check the asymmetric cryptography implementation. |
| 20 |
|
| 21 |
(use-modules (gnutls) |
| 22 |
(gnutls build tests) |
| 23 |
(rnrs bytevectors) |
| 24 |
(ice-9 match) |
| 25 |
(ice-9 receive) |
| 26 |
(srfi srfi-1) |
| 27 |
(srfi srfi-26)) |
| 28 |
|
| 29 |
(define (version>? a b) |
| 30 |
(match `(,a ,b) |
| 31 |
((() ()) #f) |
| 32 |
((_ ()) #t) |
| 33 |
((() _) #f) |
| 34 |
(((leading-a a ...) |
| 35 |
(leading-b b ...)) |
| 36 |
(cond |
| 37 |
((> leading-a leading-b) |
| 38 |
#t) |
| 39 |
((< leading-a leading-b) |
| 40 |
#f) |
| 41 |
(else |
| 42 |
(version>? a b)))))) |
| 43 |
|
| 44 |
(define (version<? a b) |
| 45 |
(version>? b a)) |
| 46 |
|
| 47 |
(define (version>=? a b) |
| 48 |
(not (version<? a b))) |
| 49 |
|
| 50 |
(define (check-import-rsa-private-key-opt m e d p q u e1 e2) |
| 51 |
;; The RSA private key import accepts #f for d only after 3.6.14. |
| 52 |
(receive (at-least-3.7.0? before-3.6.14?) |
| 53 |
(let ((version-digits |
| 54 |
(let ((canonical |
| 55 |
(match (string-split (gnutls-version) #\-) |
| 56 |
((before-dash . _) before-dash)))) |
| 57 |
(map string->number (string-split canonical #\.))))) |
| 58 |
(values |
| 59 |
(version>=? version-digits '(3 7 0)) |
| 60 |
(version<? version-digits '(3 6 14)))) |
| 61 |
;; In some old versions of gnutls, having exactly u, e1 and e2 missing |
| 62 |
;; leads to a gnutls internal error exception. So I avoid the situation |
| 63 |
;; here and either have all optional parameters absent, or all optional |
| 64 |
;; parameters present. |
| 65 |
(cond |
| 66 |
(at-least-3.7.0? |
| 67 |
(import-raw-rsa-private-key m e #f p q #f #f #f)) |
| 68 |
(before-3.6.14? |
| 69 |
(begin |
| 70 |
;; Importing without d should fail |
| 71 |
(when (false-if-exception |
| 72 |
(import-raw-rsa-private-key m e #f p q #f #f #f)) |
| 73 |
(error "this should not work")) |
| 74 |
(import-raw-rsa-private-key m e d p q u e1 e2))) |
| 75 |
(else |
| 76 |
;; This is a grey zone, maybe we’re before the commit that made d |
| 77 |
;; optional, maybe we’re after. |
| 78 |
(import-raw-rsa-private-key m e d p q u e1 e2))))) |
| 79 |
|
| 80 |
(run-test |
| 81 |
(lambda () |
| 82 |
;; Check that I can export an RSA key and that I can sign with PSS |
| 83 |
(receive (m e d p q u e1 e2) |
| 84 |
(private-key-export-raw-rsa |
| 85 |
(generate-private-key pk-algorithm/rsa 2048)) |
| 86 |
(let ((private-key (check-import-rsa-private-key-opt m e d p q u e1 e2)) |
| 87 |
(public-key (import-raw-rsa-public-key m e)) |
| 88 |
(message (string->utf8 "Hello, world!"))) |
| 89 |
(receive (signature pss-signature algo) |
| 90 |
(let ((sign-algo |
| 91 |
(pk-algorithm->sign-algorithm |
| 92 |
(private-key-pk-algorithm private-key) |
| 93 |
digest/sha256))) |
| 94 |
(values |
| 95 |
(private-key-sign-data private-key sign-algo message '()) |
| 96 |
(private-key-sign-data private-key sign-algo message |
| 97 |
(list privkey/sign-flag-rsa-pss)) |
| 98 |
sign-algo)) |
| 99 |
(public-key-verify-data public-key algo message signature) |
| 100 |
(public-key-verify-data public-key algo message pss-signature) |
| 101 |
(receive (incorrect-signature incorrect-pss-signature) |
| 102 |
(let ((corrupt |
| 103 |
(let ((+1/8bits |
| 104 |
(lambda (number) |
| 105 |
(remainder (+ 1 number) 256)))) |
| 106 |
(lambda (bv) |
| 107 |
(u8-list->bytevector |
| 108 |
(map (lambda (byte) (+1/8bits byte)) |
| 109 |
(bytevector->u8-list bv))))))) |
| 110 |
(values (corrupt signature) (corrupt pss-signature))) |
| 111 |
(when (false-if-exception |
| 112 |
(public-key-verify-data |
| 113 |
public-key algo message incorrect-signature)) |
| 114 |
(error "This should have failed (I).")) |
| 115 |
(when (false-if-exception |
| 116 |
(public-key-verify-data |
| 117 |
public-key algo message incorrect-pss-signature)) |
| 118 |
(error "This should have failed (II).")))))) |
| 119 |
;; This is the example in the manual. |
| 120 |
(receive (curve x y k) |
| 121 |
(let ((raw-output |
| 122 |
(call-with-output-string |
| 123 |
(lambda (port) |
| 124 |
(with-input-from-port |
| 125 |
(open-input-string "") |
| 126 |
(lambda () |
| 127 |
(with-output-to-port port |
| 128 |
(lambda () |
| 129 |
(load-from-path "generate-private-key.scm"))))))))) |
| 130 |
(match (read (open-input-string raw-output)) |
| 131 |
(`((curve . ,curve) |
| 132 |
(x . ,x) |
| 133 |
(y . ,y) |
| 134 |
(k . ,k)) |
| 135 |
(values curve x y k)))) |
| 136 |
(let ((input |
| 137 |
(call-with-output-string |
| 138 |
(lambda (port) |
| 139 |
;; curve: |
| 140 |
(format port "~a\n" curve) |
| 141 |
;; x: |
| 142 |
(format port "~a\n" x) |
| 143 |
;; y: |
| 144 |
(format port "~a\n" y) |
| 145 |
;; k: |
| 146 |
(format port "~a\n" k)))) |
| 147 |
(expected-output |
| 148 |
(call-with-output-string |
| 149 |
(lambda (port) |
| 150 |
(format port "curve:\n") |
| 151 |
(format port "x:\n") |
| 152 |
(format port "y:\n") |
| 153 |
(format port "k:\n") |
| 154 |
(format port "I could sign a message with that key.\n"))))) |
| 155 |
(let ((true-output |
| 156 |
(call-with-output-string |
| 157 |
(lambda (output-port) |
| 158 |
(with-input-from-port |
| 159 |
(open-input-string input) |
| 160 |
(lambda () |
| 161 |
(with-output-to-port output-port |
| 162 |
(lambda () |
| 163 |
(load-from-path "use-private-key.scm"))))))))) |
| 164 |
(unless (equal? expected-output true-output) |
| 165 |
(error "The manual example failed."))))))) |