cacert-gosigner/internal/hsm/hsm_test.go

203 lines
4.4 KiB
Go
Raw Normal View History

2022-05-01 10:36:17 +00:00
/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hsm_test
import (
"crypto/x509"
"strings"
"testing"
"github.com/sirupsen/logrus"
2022-05-01 10:36:17 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.cacert.org/cacert-gosigner/internal/config"
"git.cacert.org/cacert-gosigner/internal/hsm"
2022-05-01 10:36:17 +00:00
)
func TestEnsureCAKeysAndCertificates_not_in_setup_mode(t *testing.T) {
testConfig := setupSignerConfig(t)
setupSoftHsm(t)
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
acc, err := hsm.NewAccess(logrus.StandardLogger(),
2022-05-01 10:36:17 +00:00
hsm.CaConfigOption(testConfig),
hsm.CADirectoryOption(t.TempDir()))
assert.NoError(t, err)
2022-05-01 10:36:17 +00:00
err = acc.EnsureCAKeysAndCertificates()
2022-05-01 10:36:17 +00:00
t.Cleanup(func() {
err := acc.CloseP11Contexts()
2022-05-01 10:36:17 +00:00
assert.NoError(t, err)
})
assert.ErrorContains(t, err, "not in setup mode")
}
func prepareSoftHSM(t *testing.T) *hsm.Access {
2022-05-01 10:36:17 +00:00
t.Helper()
testConfig := setupSignerConfig(t)
setupSoftHsm(t)
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
acc, err := hsm.NewAccess(logrus.StandardLogger(),
2022-05-01 10:36:17 +00:00
hsm.CaConfigOption(testConfig),
hsm.SetupModeOption(),
hsm.CADirectoryOption(t.TempDir()))
assert.NoError(t, err)
2022-05-01 10:36:17 +00:00
err = acc.EnsureCAKeysAndCertificates()
2022-05-01 10:36:17 +00:00
t.Cleanup(func() {
err := acc.CloseP11Contexts()
2022-05-01 10:36:17 +00:00
assert.NoError(t, err)
})
require.NoError(t, err)
return acc
2022-05-01 10:36:17 +00:00
}
func TestGetRootCACertificate(t *testing.T) {
acc := prepareSoftHSM(t)
2022-05-01 10:36:17 +00:00
testData := map[string]struct {
label, errMsg string
}{
"known root": {
label: "root",
},
"unknown root": {
label: "unknown",
errMsg: "could not get CA definition for label unknown",
},
"known subordinate": {
2022-05-01 10:36:17 +00:00
label: "sub1",
errMsg: "CA definition sub1 is not a root CA definition",
},
}
for name, item := range testData {
t.Run(name, func(t *testing.T) {
root, err := acc.GetRootCACertificate(item.label)
2022-05-01 10:36:17 +00:00
if item.errMsg != "" {
assert.ErrorContains(t, err, item.errMsg)
assert.Nil(t, root)
} else {
assert.NoError(t, err)
assert.NotNil(t, root)
}
})
}
}
func TestGetSubordinateCACertificate(t *testing.T) {
acc := prepareSoftHSM(t)
2022-05-01 10:36:17 +00:00
testData := map[string]struct {
label, errMsg string
}{
"known subordinate": {
2022-05-01 10:36:17 +00:00
label: "sub1",
},
"unknown subordinate": {
2022-05-01 10:36:17 +00:00
label: "unknown",
errMsg: "could not get CA definition for label unknown",
},
"known root": {
label: "root",
errMsg: "CA definition root is a root CA definition, subordinate expected",
2022-05-01 10:36:17 +00:00
},
}
for name, item := range testData {
t.Run(name, func(t *testing.T) {
root, err := acc.GetSubordinateCACertificate(item.label)
2022-05-01 10:36:17 +00:00
if item.errMsg != "" {
assert.ErrorContains(t, err, item.errMsg)
assert.Nil(t, root)
} else {
assert.NoError(t, err)
assert.NotNil(t, root)
}
})
}
}
func TestRSAKeyGeneration(t *testing.T) {
const testRSASignerConfig = `---
Settings:
organization:
organization: ["Acme CAs Ltd."]
validity-years:
root: 30
subordinate: 10
2022-05-01 10:36:17 +00:00
url-patterns:
ocsp: http://ocsp.example.org/
crl: http://crl.example.org/%s.crl
issuer: http://%s.cas.example.org/
serial:
device: /dev/ttyUSB0
2022-05-01 10:36:17 +00:00
CAs:
root:
common-name: "Acme CAs root"
key-info:
algorithm: RSA
rsa-bits: 2048
KeyStorage:
default:
type: softhsm
label: acme-test-hsm
`
2022-05-01 10:36:17 +00:00
testConfig, err := config.LoadConfiguration(strings.NewReader(testRSASignerConfig))
require.NoError(t, err)
setupSoftHsm(t)
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
acc, err := hsm.NewAccess(
logrus.StandardLogger(),
2022-05-01 10:36:17 +00:00
hsm.CaConfigOption(testConfig),
hsm.SetupModeOption(),
hsm.CADirectoryOption(t.TempDir()))
assert.NoError(t, err)
2022-05-01 10:36:17 +00:00
err = acc.EnsureCAKeysAndCertificates()
require.NoError(t, err)
2022-05-01 10:36:17 +00:00
t.Cleanup(func() {
err := acc.CloseP11Contexts()
2022-05-01 10:36:17 +00:00
assert.NoError(t, err)
})
root, err := acc.GetRootCACertificate("root")
2022-05-01 10:36:17 +00:00
assert.NoError(t, err)
assert.NotNil(t, root)
assert.Equal(t, x509.RSA, root.PublicKeyAlgorithm)
}