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"
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-05-01 10:36:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
"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")
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
acc, err := hsm.NewAccess(logrus.StandardLogger(),
|
2022-05-01 10:36:17 +00:00
|
|
|
hsm.CaConfigOption(testConfig),
|
|
|
|
hsm.CADirectoryOption(t.TempDir()))
|
2022-08-03 07:59:26 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
err = acc.EnsureCAKeysAndCertificates()
|
2022-05-01 10:36:17 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2022-08-03 07:59:26 +00:00
|
|
|
err := acc.CloseP11Contexts()
|
2022-05-01 10:36:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.ErrorContains(t, err, "not in setup mode")
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
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")
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
acc, err := hsm.NewAccess(logrus.StandardLogger(),
|
2022-05-01 10:36:17 +00:00
|
|
|
hsm.CaConfigOption(testConfig),
|
|
|
|
hsm.SetupModeOption(),
|
|
|
|
hsm.CADirectoryOption(t.TempDir()))
|
2022-08-03 07:59:26 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
err = acc.EnsureCAKeysAndCertificates()
|
2022-05-01 10:36:17 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2022-08-03 07:59:26 +00:00
|
|
|
err := acc.CloseP11Contexts()
|
2022-05-01 10:36:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
return acc
|
2022-05-01 10:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRootCACertificate(t *testing.T) {
|
2022-08-03 07:59:26 +00:00
|
|
|
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",
|
|
|
|
},
|
2022-08-03 14:01:06 +00:00
|
|
|
"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) {
|
2022-08-03 07:59:26 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 14:01:06 +00:00
|
|
|
func TestGetSubordinateCACertificate(t *testing.T) {
|
2022-08-03 07:59:26 +00:00
|
|
|
acc := prepareSoftHSM(t)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
|
|
|
testData := map[string]struct {
|
|
|
|
label, errMsg string
|
|
|
|
}{
|
2022-08-03 14:01:06 +00:00
|
|
|
"known subordinate": {
|
2022-05-01 10:36:17 +00:00
|
|
|
label: "sub1",
|
|
|
|
},
|
2022-08-03 14:01:06 +00:00
|
|
|
"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",
|
2022-08-03 14:01:06 +00:00
|
|
|
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) {
|
2022-08-03 14:01:06 +00:00
|
|
|
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
|
2022-08-03 14:01:06 +00:00
|
|
|
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/
|
2022-08-03 12:38:36 +00:00
|
|
|
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-08-03 07:59:26 +00:00
|
|
|
|
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")
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
acc, err := hsm.NewAccess(
|
2022-11-20 09:07:02 +00:00
|
|
|
logrus.StandardLogger(),
|
2022-05-01 10:36:17 +00:00
|
|
|
hsm.CaConfigOption(testConfig),
|
|
|
|
hsm.SetupModeOption(),
|
|
|
|
hsm.CADirectoryOption(t.TempDir()))
|
2022-08-03 07:59:26 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
err = acc.EnsureCAKeysAndCertificates()
|
|
|
|
require.NoError(t, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2022-08-03 07:59:26 +00:00
|
|
|
err := acc.CloseP11Contexts()
|
2022-05-01 10:36:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
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)
|
|
|
|
}
|