Debian Codegraph @

attributecert/sign_test.go

raw
1 package attributecert
2
3 import (
4 "crypto"
5 "crypto/rand"
6 "crypto/rsa"
7 "crypto/x509"
8 "crypto/x509/pkix"
9 "math/big"
10 "testing"
11 "time"
12
13 "github.com/google/go-cmp/cmp"
14 )
15
16 func bigIntComparer(x, y *big.Int) bool {
17 if x == nil || y == nil {
18 return x == y
19 }
20 return x.Cmp(y) == 0
21 }
22
23 // createKeyAndCert creates a key and self-signed certificate for the given template.
24 func createKeyAndCert(t *testing.T, template *x509.Certificate, parentTemplate *x509.Certificate) (crypto.Signer, *x509.Certificate) {
25 t.Helper()
26 // Generate a certificate for the attribute cert signer
27 key, err := rsa.GenerateKey(rand.Reader, 2048)
28 if err != nil {
29 t.Fatalf("rsa.GenerateKey() failed: %v", err)
30 }
31 certBytes, err := x509.CreateCertificate(rand.Reader, template, parentTemplate, &key.PublicKey, key)
32 if err != nil {
33 t.Fatalf("x509.CreateCertificate() failed: %v", err)
34 }
35 cert, err := x509.ParseCertificate(certBytes)
36 if err != nil {
37 t.Fatalf("x509.ParseCertificate() failed: %v", err)
38 }
39 return key, cert
40 }
41
42 // signCert creates a certificate for the given template signed by the parent template.
43 func signCert(t *testing.T, template *x509.Certificate, parentTemplate *x509.Certificate, signer any) (crypto.Signer, *x509.Certificate) {
44 t.Helper()
45 // Generate a certificate for the attribute cert signer
46 key, err := rsa.GenerateKey(rand.Reader, 2048)
47 if err != nil {
48 t.Fatalf("rsa.GenerateKey() failed: %v", err)
49 }
50 certBytes, err := x509.CreateCertificate(rand.Reader, template, parentTemplate, &key.PublicKey, signer)
51 if err != nil {
52 t.Fatalf("x509.CreateCertificate() failed: %v", err)
53 }
54 cert, err := x509.ParseCertificate(certBytes)
55 if err != nil {
56 t.Fatalf("x509.ParseCertificate() failed: %v", err)
57 }
58 return key, cert
59 }
60
61 func TestCreateAttributeCertificate(t *testing.T) {
62 // Generate a certificate for the attribute cert signer
63 ACSignerCertTemplate := &x509.Certificate{
64 SerialNumber: big.NewInt(1),
65 Subject: pkix.Name{
66 CommonName: "Test Attribute Cert Signer",
67 Organization: []string{"Test Org"},
68 },
69 NotBefore: time.Now().Add(-time.Hour),
70 NotAfter: time.Now().Add(time.Hour),
71 IsCA: true,
72 BasicConstraintsValid: true,
73 KeyUsage: x509.KeyUsageCertSign,
74 }
75 ACSignerKey, ACSignerCert := createKeyAndCert(t, ACSignerCertTemplate, ACSignerCertTemplate)
76
77 // Generate a certificate for the EK cert signer
78 EKSignerCertTemplate := &x509.Certificate{
79 SerialNumber: big.NewInt(1),
80 Subject: pkix.Name{
81 CommonName: "Test EK Signer",
82 Organization: []string{"Test Org"},
83 },
84 NotBefore: time.Now().Add(-time.Hour),
85 NotAfter: time.Now().Add(time.Hour),
86 IsCA: true,
87 BasicConstraintsValid: true,
88 KeyUsage: x509.KeyUsageCertSign,
89 }
90 EKSignerKey, EKSignerCert := createKeyAndCert(t, EKSignerCertTemplate, EKSignerCertTemplate)
91
92 testCases := []struct {
93 name string
94 subjectCert *x509.Certificate
95 signer crypto.Signer
96 expectError bool
97 addAttributes bool
98 }{
99 {
100 name: "Basic Test",
101 subjectCert: &x509.Certificate{
102 SerialNumber: big.NewInt(2),
103 Subject: pkix.Name{
104 CommonName: "Test EK",
105 Organization: []string{"Test Org"},
106 },
107 NotBefore: time.Now().Add(-time.Hour),
108 NotAfter: time.Now().Add(time.Hour),
109 },
110 signer: ACSignerKey,
111 expectError: false,
112 },
113 {
114 name: "Invalid Signer",
115 subjectCert: &x509.Certificate{
116 SerialNumber: big.NewInt(4),
117 Subject: pkix.Name{
118 CommonName: "Test EK",
119 Organization: []string{"Test Org"},
120 },
121 NotBefore: time.Now().Add(-time.Hour),
122 NotAfter: time.Now().Add(time.Hour),
123 },
124 signer: nil,
125 expectError: true,
126 },
127 {
128 name: "Invalid Subject Cert",
129 subjectCert: &x509.Certificate{
130 SerialNumber: big.NewInt(0),
131 },
132 signer: ACSignerKey,
133 expectError: true,
134 },
135 }
136
137 for _, tc := range testCases {
138 t.Run(tc.name, func(t *testing.T) {
139
140 // Generate a certificate for the test case
141 _, subjectCert := signCert(t, tc.subjectCert, EKSignerCert, EKSignerKey)
142
143 notBefore := time.Now().Add(-time.Hour)
144 notAfter := time.Now().Add(time.Hour)
145 attributeCertBytes, err := CreateAttributeCertificateFor(subjectCert, notBefore, notAfter, ACSignerCert, tc.signer)
146 if tc.expectError {
147 if err == nil {
148 t.Fatalf("CreateAttributeCertificate expected an error but got none")
149 }
150 return
151 }
152 if err != nil {
153 t.Fatalf("CreateAttributeCertificate failed: %v", err)
154 }
155
156 parsedCert, err := ParseAttributeCertificate(attributeCertBytes)
157 if err != nil {
158 t.Fatalf("ParseAttributeCertificate failed: %v", err)
159 }
160
161 if parsedCert.SerialNumber.Cmp(big.NewInt(0)) == 0 {
162 t.Errorf("Parsed certificate serial number is 0")
163 }
164 if !parsedCert.NotBefore.Before(time.Now()) {
165 t.Errorf("Parsed certificate not before is in the past")
166 }
167 if !parsedCert.NotAfter.After(time.Now()) {
168 t.Errorf("Parsed certificate not after is in the future")
169 }
170 if diff := cmp.Diff(parsedCert.Holder.Issuer, EKSignerCert.Subject); diff != "" {
171 t.Errorf("Holder Issuer mismatch (-want +got):\n%s", diff)
172 }
173 if diff := cmp.Diff(parsedCert.Holder.Serial, tc.subjectCert.SerialNumber, cmp.Comparer(bigIntComparer)); diff != "" {
174 t.Errorf("Holder Serial mismatch (-want +got):\n%s", diff)
175 }
176 if diff := cmp.Diff(parsedCert.Issuer, ACSignerCert.Subject); diff != "" {
177 t.Errorf("Issuer mismatch (-want +got):\n%s", diff)
178 }
179 })
180 }
181 }