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