Debian Codegraph @

ietf-cms/sign_test.go

raw symbols SCIP
1 package cms
2
3 import (
4 "crypto/x509"
5 "encoding/pem"
6 "io/ioutil"
7 "os"
8 "os/exec"
9 "testing"
10 "time"
11 )
12
13 var (
14 examplePrivateKey = leaf.PrivateKey
15 exampleChain = leaf.Chain()
16 )
17
18 func TestSign(t *testing.T) {
19 data := []byte("hello, world!")
20
21 ci, err := Sign(data, leaf.Chain(), leaf.PrivateKey)
22 if err != nil {
23 t.Fatal(err)
24 }
25
26 sd2, err := ParseSignedData(ci)
27 if err != nil {
28 t.Fatal(err)
29 }
30
31 if _, err = sd2.Verify(rootOpts); err != nil {
32 t.Fatal(err)
33 }
34
35 // test that we're including whole chain in sd
36 sdCerts, err := sd2.psd.X509Certificates()
37 if err != nil {
38 t.Fatal(err)
39 }
40 for _, chainCert := range leaf.Chain() {
41 var found bool
42 for _, sdCert := range sdCerts {
43 if sdCert.Equal(chainCert) {
44 if found == true {
45 t.Fatal("duplicate cert in sd")
46 }
47 found = true
48 }
49 }
50 if !found {
51 t.Fatal("missing cert in sd")
52 }
53 }
54
55 // check that we're including signing time attribute
56 st, err := sd2.psd.SignerInfos[0].GetSigningTimeAttribute()
57 if st.After(time.Now().Add(time.Second)) || st.Before(time.Now().Add(-time.Second)) {
58 t.Fatal("expected SigningTime to be now. Difference was", st.Sub(time.Now()))
59 }
60 }
61
62 func TestSignDetached(t *testing.T) {
63 data := []byte("hello, world!")
64
65 ci, err := SignDetached(data, leaf.Chain(), leaf.PrivateKey)
66 if err != nil {
67 t.Fatal(err)
68 }
69
70 sd2, err := ParseSignedData(ci)
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 if _, err = sd2.VerifyDetached(data, rootOpts); err != nil {
76 t.Fatal(err)
77 }
78
79 // test that we're including whole chain in sd
80 sdCerts, err := sd2.psd.X509Certificates()
81 if err != nil {
82 t.Fatal(err)
83 }
84 for _, chainCert := range leaf.Chain() {
85 var found bool
86 for _, sdCert := range sdCerts {
87 if sdCert.Equal(chainCert) {
88 if found == true {
89 t.Fatal("duplicate cert in sd")
90 }
91 found = true
92 }
93 }
94 if !found {
95 t.Fatal("missing cert in sd")
96 }
97 }
98
99 // check that we're including signing time attribute
100 st, err := sd2.psd.SignerInfos[0].GetSigningTimeAttribute()
101 if st.After(time.Now().Add(time.Second)) || st.Before(time.Now().Add(-time.Second)) {
102 t.Fatal("expected SigningTime to be now. Difference was", st.Sub(time.Now()))
103 }
104 }
105
106 func TestSignDetachedWithOpenSSL(t *testing.T) {
107 // Do not require this test to pass if openssl is not in the path
108 opensslPath, err := exec.LookPath("openssl")
109 if err != nil {
110 t.Skip("could not find openssl in path")
111 }
112
113 content := []byte("hello, world!")
114
115 signatureDER, err := SignDetached(content, leaf.Chain(), leaf.PrivateKey)
116 if err != nil {
117 t.Fatal(err)
118 }
119
120 signatureFile, err := ioutil.TempFile("", "TestSignatureOpenSSL_signatureFile_*")
121 if err != nil {
122 t.Fatal(err)
123 }
124
125 _, err = signatureFile.Write(signatureDER)
126 if err != nil {
127 t.Fatal(err)
128 }
129
130 signatureFile.Close()
131
132 // write content to a temp file
133 contentFile, err := ioutil.TempFile("", "TestSignatureOpenSSL_contentFile_*")
134 if err != nil {
135 t.Fatal(err)
136 }
137
138 _, err = contentFile.Write(content)
139 if err != nil {
140 t.Fatal(err)
141 }
142
143 contentFile.Close()
144
145 // write CA cert to a temp file
146 certsFile, err := ioutil.TempFile("", "TestSignatureOpenSSL_certsFile_*")
147 if err != nil {
148 t.Fatal(err)
149 }
150
151 for _, cert := range leaf.Chain() {
152 // write leaf as PEM
153 certBlock := &pem.Block{
154 Type: "CERTIFICATE",
155 Bytes: cert.Raw,
156 }
157
158 certPEM := pem.EncodeToMemory(certBlock)
159
160 _, err = certsFile.Write(certPEM)
161 if err != nil {
162 t.Fatal(err)
163 }
164 }
165
166 certsFile.Close()
167
168 cmd := exec.Command(opensslPath, "cms", "-verify",
169 "-content", contentFile.Name(), "-binary",
170 "-in", signatureFile.Name(), "-inform", "DER",
171 "-CAfile", certsFile.Name())
172
173 _, err = cmd.CombinedOutput()
174 if err != nil {
175 t.Fatal(err)
176 }
177
178 //
179 // Remove temporary files if test was successful.
180 // Intentionally leave the temp files if test fails.
181 //
182 os.Remove(contentFile.Name())
183 os.Remove(signatureFile.Name())
184 os.Remove(certsFile.Name())
185 }
186
187 func TestSignRemoveHeaders(t *testing.T) {
188 sd, err := NewSignedData([]byte("hello, world"))
189 if err != nil {
190 t.Fatal(err)
191 }
192 if err = sd.Sign(leaf.Chain(), leaf.PrivateKey); err != nil {
193 t.Fatal(err)
194 }
195 if err = sd.SetCertificates([]*x509.Certificate{}); err != nil {
196 t.Fatal(err)
197 }
198 if certs, err := sd.GetCertificates(); err != nil {
199 t.Fatal(err)
200 } else if len(certs) != 0 {
201 t.Fatal("expected 0 certs")
202 }
203
204 der, err := sd.ToDER()
205 if err != nil {
206 t.Fatal(err)
207 }
208 if sd, err = ParseSignedData(der); err != nil {
209 t.Fatal(err)
210 }
211 sd.SetCertificates([]*x509.Certificate{leaf.Certificate})
212
213 opts := x509.VerifyOptions{
214 Roots: root.ChainPool(),
215 Intermediates: leaf.ChainPool(),
216 }
217
218 if _, err := sd.Verify(opts); err != nil {
219 t.Fatal(err)
220 }
221 }