| 1 |
;;; GnuTLS --- Guile bindings for GnuTLS. |
| 2 |
;;; Copyright (C) 2023 David Thompson <[email protected]> |
| 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 |
;;; |
| 20 |
;;; Exercise the X.509 key and certificate generation API. |
| 21 |
;;; |
| 22 |
|
| 23 |
(use-modules (gnutls) |
| 24 |
(gnutls build tests) |
| 25 |
(srfi srfi-1) |
| 26 |
(srfi srfi-4) |
| 27 |
(srfi srfi-11) |
| 28 |
(ice-9 format)) |
| 29 |
|
| 30 |
(run-test |
| 31 |
(lambda () |
| 32 |
(let ((key (generate-x509-private-key pk-algorithm/rsa 2048 '())) |
| 33 |
(cert (make-x509-certificate)) |
| 34 |
(cert-format x509-certificate-format/pem) |
| 35 |
(serial #vu8(1 2 3 4)) |
| 36 |
;; Use dates past 2038 if possible |
| 37 |
(activation (if (and=> (getenv "SIZEOF_TIME_T") |
| 38 |
(lambda (n) (string=? "4" n))) |
| 39 |
946702800 ; 2000-01-01 |
| 40 |
2272143600)) ; 2042-01-01 |
| 41 |
(expiration (if (and=> (getenv "SIZEOF_TIME_T") |
| 42 |
(lambda (n) (string=? "4" n))) |
| 43 |
1893474000 ; 2030-01-01 |
| 44 |
2524604400))) ; 2050-01-01 |
| 45 |
(set-x509-certificate-ca-status! cert #f) |
| 46 |
(set-x509-certificate-dn-by-oid! cert oid/x520-country-name "US") |
| 47 |
(set-x509-certificate-dn-by-oid! cert oid/x520-state-or-province-name "MA") |
| 48 |
(set-x509-certificate-dn-by-oid! cert oid/x520-locality-name "Boston") |
| 49 |
(set-x509-certificate-dn-by-oid! cert oid/x520-common-name "Test") |
| 50 |
(set-x509-certificate-dn-by-oid! cert oid/x520-organization-name "Guile") |
| 51 |
(set-x509-certificate-dn-by-oid! cert oid/x520-organizational-unit-name "GnuTLS") |
| 52 |
(set-x509-certificate-version! cert 3) |
| 53 |
(set-x509-certificate-serial! cert serial) |
| 54 |
(set-x509-certificate-activation-time! cert activation) |
| 55 |
(set-x509-certificate-expiration-time! cert expiration) |
| 56 |
(set-x509-certificate-key! cert key) |
| 57 |
(set-x509-certificate-key-usage! cert (list key-usage/digital-signature |
| 58 |
key-usage/key-encipherment)) |
| 59 |
(set-x509-certificate-subject-key-id! cert (x509-certificate-key-id cert)) |
| 60 |
(sign-x509-certificate! cert cert key) ; self-sign |
| 61 |
(and (x509-private-key? key) |
| 62 |
(x509-certificate? cert) |
| 63 |
;; Validate that setter bindings set the right values and getter |
| 64 |
;; bindings return the right values. |
| 65 |
(not (x509-certificate-ca-status cert)) |
| 66 |
(equal? (x509-certificate-dn cert) |
| 67 |
"C=US,ST=MA,L=Boston,CN=Test,O=Guile,OU=GnuTLS") |
| 68 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-country-name) |
| 69 |
"US") |
| 70 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-state-or-province-name) |
| 71 |
"MA") |
| 72 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-locality-name) |
| 73 |
"Boston") |
| 74 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-common-name) |
| 75 |
"Test") |
| 76 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-organization-name) |
| 77 |
"Guile") |
| 78 |
(string=? (x509-certificate-dn-by-oid cert oid/x520-organizational-unit-name) |
| 79 |
"GnuTLS") |
| 80 |
(= (x509-certificate-version cert) 3) |
| 81 |
(equal? (x509-certificate-serial cert) serial) |
| 82 |
(= (x509-certificate-activation-time cert) activation) |
| 83 |
(= (x509-certificate-expiration-time cert) expiration) |
| 84 |
(lset= eq? |
| 85 |
(x509-certificate-key-usage cert) |
| 86 |
(list key-usage/digital-signature |
| 87 |
key-usage/key-encipherment)) |
| 88 |
(equal? (x509-certificate-subject-key-id cert) |
| 89 |
(x509-certificate-key-id cert)) |
| 90 |
;; Round trip export/import. |
| 91 |
(let ((bv (export-x509-private-key key cert-format))) |
| 92 |
(equal? (export-x509-private-key |
| 93 |
(import-x509-private-key bv cert-format) |
| 94 |
cert-format) |
| 95 |
bv)) |
| 96 |
(let ((bv (export-x509-certificate cert cert-format))) |
| 97 |
(equal? (export-x509-certificate |
| 98 |
(import-x509-certificate bv cert-format) |
| 99 |
cert-format) |
| 100 |
bv)))))) |