Debian Codegraph @

guile/tests/x509-certificates.scm

raw
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 ;;; Exercise the X.509 certificate API.
24 ;;;
25
26 (use-modules (gnutls)
27 (gnutls build tests)
28 (srfi srfi-4)
29 (srfi srfi-11)
30 (ice-9 format)
31 (ice-9 regex))
32
33 (define %certificate-file
34 (search-path %load-path "x509-certificate.pem"))
35
36 (define %private-key-file
37 (search-path %load-path "x509-key.pem"))
38
39 (define %first-oid
40 ;; The certificate's first OID.
41 "2.5.4.6")
42
43 (define %signature-algorithm
44 ;; The certificate's signature algorithm.
45 sign-algorithm/rsa-sha1)
46
47 (define %sha1-fingerprint
48 ;; The certificate's SHA-1 fingerprint.
49 "7c55df47de718869d55998ee1e9301331ccd0601")
50
51 (define %sha256-fingerprint
52 ;; The certificate's SHA-256 fingerprint.
53 "0db40a5ee20169d25f090e4d165d87266b1a04722cddec4da36692c81c3096f6")
54
55 (define %certificate-oneline
56 ;; The certificate's information in one line.
57 ;; Don't compare the full string as some part is locale specific
58 ;; and SHA-1 might be trusted.
59 (string-append
60 "subject `CN=GNUTLS TEST SERVER,OU=GNUTLS,O=FSF,C=GR', "
61 "issuer `CN=GNUTLS INTERMEDIATE TEST CA,OU=GNUTLS,O=FSF,C=GR', "
62 "serial 0x02, RSA key 1024 bits, signed using RSA-SHA1 (broken!), "
63 "activated `2004-06-28 22:47:00 UTC', "
64 "expires `2007-03-22 22:47:00 UTC', "
65 "pin-sha256=\"BHqOQartAZ1QqiUxkeaMtSjzgubb011pac+q0M7BkV4=\""))
66
67 (define (file-size file)
68 (stat:size (stat file)))
69
70 (define (u8vector->hex-string u8vector)
71 (string-join (map (lambda (u8) (format #f "~2,'0x" u8))
72 (u8vector->list u8vector))
73 ""))
74
75
76 (run-test
77 (lambda ()
78 (let ((raw-certificate (make-u8vector (file-size %certificate-file)))
79 (raw-privkey (make-u8vector (file-size %private-key-file))))
80
81 (uniform-vector-read! raw-certificate
82 (open-input-file %certificate-file))
83 (uniform-vector-read! raw-privkey
84 (open-input-file %private-key-file))
85
86 (let* ((cert (import-x509-certificate raw-certificate
87 x509-certificate-format/pem))
88 (cert-printed (x509-certificate-print
89 cert certificate-print-format/oneline))
90 (sec (import-x509-private-key raw-privkey
91 x509-certificate-format/pem)))
92
93 (and (x509-certificate? cert)
94 (x509-private-key? sec)
95 (string? (x509-certificate-dn cert))
96 (string? (x509-certificate-issuer-dn cert))
97 (string=? (x509-certificate-dn-oid cert 0) %first-oid)
98 (eq? (x509-certificate-signature-algorithm cert)
99 %signature-algorithm)
100 (string-match
101 "subject `CN=GNUTLS TEST SERVER,OU=GNUTLS,O=FSF,C=GR'"
102 cert-printed)
103 (string-match
104 "issuer `CN=GNUTLS INTERMEDIATE TEST CA,OU=GNUTLS,O=FSF,C=GR'"
105 cert-printed)
106 (string-match "serial 0x02, RSA key 1024 bits"
107 cert-printed)
108 (string-match "activated `2004-06-28 22:47:00 UTC'"
109 cert-printed)
110 (string-match "expires `2007-03-22 22:47:00 UTC'"
111 cert-printed)
112 (string-match
113 "pin-sha256=\"BHqOQartAZ1QqiUxkeaMtSjzgubb011pac\\+q0M7BkV4=\""
114 cert-printed)
115 (x509-certificate-matches-hostname? cert "localhost")
116 (let-values (((type name)
117 (x509-certificate-subject-alternative-name
118 cert 0)))
119 (and (string? name)
120 (string?
121 (x509-subject-alternative-name->string type))))
122 (equal? (u8vector->hex-string
123 (x509-certificate-fingerprint cert digest/sha1))
124 %sha1-fingerprint)
125 (equal? (u8vector->hex-string
126 (x509-certificate-fingerprint cert digest/sha256))
127 %sha256-fingerprint))))))
128
129 ;;; arch-tag: eef09b52-30e8-472a-8b93-cb636434f6eb