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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package nssdb
import (
"context"
"crypto/x509"
"database/sql"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestObject_AsX509Certificate(t *testing.T) {
t.Run("ok", func(t *testing.T) {
obj := &Object{
ULongAttributes: map[string]uint32{
"CKA_CLASS": CKO_CERTIFICATE,
"CKA_CERTIFICATE_TYPE": CKC_X_509,
},
Attributes: map[string][]byte{
"CKA_VALUE": leafCrt.Raw,
},
}
got, err := obj.ToX509Certificate()
require.NoError(t, err)
assert.True(t, got.Equal(leafCrt))
})
t.Run("invalid class", func(t *testing.T) {
obj := &Object{}
_, err := obj.ToX509Certificate()
assert.ErrorContains(t, err, "CKA_CLASS")
})
t.Run("invalid certificate type", func(t *testing.T) {
obj := &Object{
ULongAttributes: map[string]uint32{
"CKA_CLASS": CKO_CERTIFICATE,
},
}
_, err := obj.ToX509Certificate()
assert.ErrorContains(t, err, "CKA_CERTIFICATE_TYPE")
})
t.Run("missing CKA_VALUE", func(t *testing.T) {
obj := &Object{
ULongAttributes: map[string]uint32{
"CKA_CLASS": CKO_CERTIFICATE,
"CKA_CERTIFICATE_TYPE": CKC_X_509,
},
}
_, err := obj.ToX509Certificate()
assert.ErrorContains(t, err, "missing attribute CKA_VALUE")
})
t.Run("invalid CKA_VALUE", func(t *testing.T) {
obj := &Object{
ULongAttributes: map[string]uint32{
"CKA_CLASS": CKO_CERTIFICATE,
"CKA_CERTIFICATE_TYPE": CKC_X_509,
},
Attributes: map[string][]byte{
"CKA_VALUE": {1, 2, 3},
},
}
_, err := obj.ToX509Certificate()
assert.ErrorContains(t, err, "parse CKA_VALUE")
})
}
func TestX509CertToObject(t *testing.T) {
t.Run("ok", func(t *testing.T) {
got, err := x509CertToObject(leafCrt, "My Client Access")
require.NoError(t, err)
assert.NoError(t, got.ValidateULong("CKA_CLASS", CKO_CERTIFICATE))
assert.NoError(t, got.ValidateULong("CKA_CERTIFICATE_TYPE", CKC_X_509))
assert.Equal(t, []byte("My Client Access"), got.Attributes["CKA_LABEL"])
assert.Equal(t, leafCrt.Raw, got.Attributes["CKA_VALUE"])
assert.Equal(t, leafCrt.SubjectKeyId, got.Attributes["CKA_ID"])
assert.NotEmpty(t, got.Attributes["CKA_SUBJECT"])
assert.NotEmpty(t, got.Attributes["CKA_ISSUER"])
assert.NotEmpty(t, got.Attributes["CKA_SERIAL_NUMBER"])
assert.Equal(t, []byte{0}, got.Attributes["CKA_PRIVATE"])
assert.Equal(t, []byte{1}, got.Attributes["CKA_MODIFIABLE"])
assert.Equal(t, []byte{1}, got.Attributes["CKA_TOKEN"])
assert.Empty(t, got.EncryptedAttributes)
})
t.Run("bad serial", func(t *testing.T) {
_, err := x509CertToObject(&x509.Certificate{}, "x")
assert.ErrorContains(t, err, "invalid serial number")
})
}
func TestNSSDB_AddCertificate(t *testing.T) {
ctx := context.Background()
for _, v := range nssVersions {
db := v.connect(t)
t.Run(v.name("ok"), func(t *testing.T) {
oldCertObj, err := db.GetObjectPublic(ctx, v.certID)
require.NoError(t, err)
oldPubKeyObj, err := db.GetObjectPublic(ctx, v.pubKeyID)
require.NoError(t, err)
require.NoError(t, db.Reset(ctx))
certID, pubKeyID, err := db.AddCertificate(ctx, leafCrt, "leaf")
require.NoError(t, err)
assert.NotEmpty(t, certID)
assert.NotEmpty(t, pubKeyID)
certObj, err := db.GetObjectPublic(ctx, certID)
require.NoError(t, err)
assert.EqualValues(t, CKO_CERTIFICATE, certObj.ULongAttributes["CKA_CLASS"])
pubKeyObj, err := db.GetObjectPublic(ctx, pubKeyID)
require.NoError(t, err)
assert.EqualValues(t, CKO_PUBLIC_KEY, pubKeyObj.ULongAttributes["CKA_CLASS"])
// verify the certificate imported by this library matches the certificate
// imported with pk12util in everything besides the id
oldCertObj.ID = 0
certObj.ID = 0
assert.Equal(t, oldCertObj, certObj)
// verify the public key imported by this library matches the public key
// imported with pk12util for every attribute except the id
oldPubKeyObj.ID = 0
pubKeyObj.ID = 0
assert.Equal(t, oldPubKeyObj, pubKeyObj)
})
t.Run(v.name("ok replace"), func(t *testing.T) {
certID, pubKeyID, err := db.AddCertificate(ctx, leafCrt, "name")
require.NoError(t, err)
assert.NotEqual(t, v.certID, certID)
assert.NotEqual(t, v.pubKeyID, pubKeyID)
_, err = db.GetObjectPublic(ctx, v.certID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPublic(ctx, v.pubKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPrivate(ctx, v.privateKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
})
}
}
func TestNSSDB_Import(t *testing.T) {
ctx := context.Background()
for _, v := range nssVersions {
t.Run(v.name("ok"), func(t *testing.T) {
db := v.connect(t)
require.NoError(t, db.Reset(ctx))
certID, pubKeyID, privKeyID, err := db.Import(ctx, "My Leaf", leafCrt, leafKey)
require.NoError(t, err)
certObj, err := db.GetObjectPublic(ctx, certID)
require.NoError(t, err)
assert.NoError(t, certObj.ValidateULong("CKA_CLASS", CKO_CERTIFICATE))
assert.NoError(t, certObj.Validate("CKA_ID", leafCrt.SubjectKeyId))
pubKeyObj, err := db.GetObjectPublic(ctx, pubKeyID)
require.NoError(t, err)
require.NoError(t, err)
assert.NoError(t, pubKeyObj.ValidateULong("CKA_CLASS", CKO_PUBLIC_KEY))
assert.NoError(t, pubKeyObj.Validate("CKA_ID", leafCrt.SubjectKeyId))
privKeyObj, err := db.GetObjectPrivate(ctx, privKeyID)
require.NoError(t, err)
require.NoError(t, err)
assert.NoError(t, privKeyObj.ValidateULong("CKA_CLASS", CKO_PRIVATE_KEY))
assert.NoError(t, privKeyObj.Validate("CKA_ID", leafCrt.SubjectKeyId))
})
// If the nss tools are installed sign some data with pk1sign to validate
// the imported private key and cert are compatible.
t.Run(v.name("pk1sign"), func(t *testing.T) {
p, err := exec.LookPath("pk1sign")
if err != nil {
t.Skip("pk1sign not installed")
}
db, dir := v.connectDir(t)
require.NoError(t, db.Reset(ctx))
_, _, _, err = db.Import(ctx, "leaf", leafCrt, leafKey)
require.NoError(t, err)
cmd := exec.Command(p, "-d", dir, "-k", "leaf", "-i", "README.md")
output, err := cmd.CombinedOutput()
assert.NoError(t, err, string(output))
})
}
}
func TestNSSDB_ListCertificateObjects(t *testing.T) {
for _, v := range nssVersions {
db := v.connect(t)
t.Run(v.name("ok"), func(t *testing.T) {
objs, err := db.ListCertificateObjects(context.Background())
require.NoError(t, err)
assert.Len(t, objs, 1)
for _, obj := range objs {
_, err = obj.ToX509Certificate()
assert.NoError(t, err)
}
})
}
}
func TestNSSDB_DeleteCertificatesByName(t *testing.T) {
ctx := context.Background()
for _, v := range nssVersions {
t.Run(v.name("ok"), func(t *testing.T) {
db := v.connect(t)
err := db.DeleteCertificatesByName(ctx, "leaf")
require.NoError(t, err)
_, err = db.GetObjectPublic(ctx, v.certID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPublic(ctx, v.pubKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPrivate(ctx, v.privateKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
})
t.Run(v.name("ok nothing to do"), func(t *testing.T) {
db := v.connect(t)
err := db.DeleteCertificatesByName(ctx, "leafx")
require.NoError(t, err)
_, err = db.GetObjectPublic(ctx, v.certID)
assert.NoError(t, err)
_, err = db.GetObjectPublic(ctx, v.pubKeyID)
assert.NoError(t, err)
_, err = db.GetObjectPrivate(ctx, v.privateKeyID)
assert.NoError(t, err)
})
}
}
func TestNSSDB_DeleteCertificate(t *testing.T) {
ctx := context.Background()
for _, v := range nssVersions {
t.Run(v.name("ok"), func(t *testing.T) {
db := v.connect(t)
err := db.DeleteCertificate(ctx, v.certID)
require.NoError(t, err)
_, err = db.GetObjectPublic(ctx, v.certID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPublic(ctx, v.pubKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
_, err = db.GetObjectPrivate(ctx, v.privateKeyID)
assert.ErrorIs(t, err, sql.ErrNoRows)
})
t.Run(v.name("ok nothing to do"), func(t *testing.T) {
db := v.connect(t)
err := db.DeleteCertificate(ctx, 7)
assert.ErrorIs(t, err, sql.ErrNoRows)
})
t.Run(v.name("fail not a cert"), func(t *testing.T) {
db := v.connect(t)
err := db.DeleteCertificate(ctx, v.pubKeyID)
assert.Error(t, err)
})
}
}
|