2022-04-24 07:25:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-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.
|
|
|
|
*/
|
|
|
|
|
2021-08-24 20:02:14 +00:00
|
|
|
package signing_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
2022-12-11 12:32:05 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2021-08-24 20:02:14 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2022-04-24 07:25:04 +00:00
|
|
|
"fmt"
|
2021-08-24 20:02:14 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/x509/helper"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/x509/signing"
|
2021-08-24 20:02:14 +00:00
|
|
|
)
|
|
|
|
|
2022-09-18 10:17:27 +00:00
|
|
|
func randomSerial(t *testing.T) *big.Int {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
serial, err := helper.GenerateRandomSerial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return serial
|
|
|
|
}
|
|
|
|
|
2022-12-11 12:32:05 +00:00
|
|
|
func TestProfileUsage_String(t *testing.T) {
|
|
|
|
okValues := []struct {
|
|
|
|
Name string
|
|
|
|
Usage signing.ProfileUsage
|
|
|
|
}{
|
|
|
|
{"invalid", signing.UsageInvalid},
|
|
|
|
{"ocsp", signing.UsageOCSP},
|
|
|
|
{"client", signing.UsageClient},
|
|
|
|
{"code", signing.UsageCode},
|
|
|
|
{"person", signing.UsagePerson},
|
|
|
|
{"server", signing.UsageServer},
|
|
|
|
{"server-client", signing.UsageServerClient},
|
|
|
|
{"org-client", signing.UsageOrgClient},
|
|
|
|
{"org-code", signing.UsageOrgCode},
|
|
|
|
{"org-email", signing.UsageOrgEmail},
|
|
|
|
{"org-person", signing.UsageOrgPerson},
|
|
|
|
{"org-server", signing.UsageOrgServer},
|
|
|
|
{"org-server-client", signing.UsageOrgServerClient},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range okValues {
|
|
|
|
t.Run(v.Name, func(t *testing.T) {
|
|
|
|
str := v.Usage.String()
|
|
|
|
|
|
|
|
assert.NotEmpty(t, str)
|
|
|
|
assert.NotContains(t, str, "unknown profile usage")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("undefined", func(t *testing.T) {
|
|
|
|
str := signing.ProfileUsage(255).String()
|
|
|
|
|
|
|
|
assert.NotEmpty(t, str)
|
|
|
|
assert.Contains(t, str, "unknown profile usage")
|
|
|
|
assert.Contains(t, str, "255")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProfileUsage_Description(t *testing.T) {
|
|
|
|
okValues := []struct {
|
|
|
|
Name string
|
|
|
|
Usage signing.ProfileUsage
|
|
|
|
}{
|
|
|
|
{"invalid", signing.UsageInvalid},
|
|
|
|
{"ocsp", signing.UsageOCSP},
|
|
|
|
{"client", signing.UsageClient},
|
|
|
|
{"code", signing.UsageCode},
|
|
|
|
{"person", signing.UsagePerson},
|
|
|
|
{"server", signing.UsageServer},
|
|
|
|
{"server-client", signing.UsageServerClient},
|
|
|
|
{"org-client", signing.UsageOrgClient},
|
|
|
|
{"org-code", signing.UsageOrgCode},
|
|
|
|
{"org-email", signing.UsageOrgEmail},
|
|
|
|
{"org-person", signing.UsageOrgPerson},
|
|
|
|
{"org-server", signing.UsageOrgServer},
|
|
|
|
{"org-server-client", signing.UsageOrgServerClient},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range okValues {
|
|
|
|
t.Run(v.Name, func(t *testing.T) {
|
|
|
|
str := v.Usage.Description()
|
|
|
|
|
|
|
|
assert.NotEmpty(t, str)
|
|
|
|
assert.NotContains(t, str, "unknown profile usage")
|
|
|
|
assert.Greater(t, len(str), len(v.Name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("undefined", func(t *testing.T) {
|
|
|
|
str := signing.ProfileUsage(255).Description()
|
|
|
|
|
|
|
|
assert.NotEmpty(t, str)
|
|
|
|
assert.Contains(t, str, "unknown profile usage")
|
|
|
|
assert.Contains(t, str, "255")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:02:14 +00:00
|
|
|
type testRepo struct {
|
|
|
|
certs map[string]x509.Certificate
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func (r *testRepo) StoreCertificate(certificate *x509.Certificate) error {
|
|
|
|
r.certs[certificate.SerialNumber.Text(16)] = *certificate
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2021-08-24 20:02:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testSigner struct {
|
2022-04-24 06:03:51 +00:00
|
|
|
t *testing.T
|
2021-08-24 20:02:14 +00:00
|
|
|
key crypto.PrivateKey
|
|
|
|
certificate *x509.Certificate
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func newTestSignerResponse(certificate *x509.Certificate) *signing.SignerResponse {
|
|
|
|
return &signing.SignerResponse{Certificate: certificate}
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func (s *testSigner) SignCertificate(request *signing.SignerRequest) (*signing.SignerResponse, error) {
|
2021-08-24 20:02:14 +00:00
|
|
|
startDate := time.Now().Add(-1 * time.Minute)
|
|
|
|
template := &x509.Certificate{
|
2022-04-23 17:37:42 +00:00
|
|
|
Subject: request.SubjectDN,
|
2022-04-24 06:03:51 +00:00
|
|
|
SerialNumber: randomSerial(s.t),
|
2022-04-23 17:37:42 +00:00
|
|
|
EmailAddresses: request.Emails,
|
2021-08-24 20:02:14 +00:00
|
|
|
NotBefore: startDate,
|
2022-12-11 12:32:05 +00:00
|
|
|
NotAfter: startDate.AddDate(1, 0, 0),
|
2021-08-24 20:02:14 +00:00
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageDataEncipherment,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageEmailProtection},
|
2022-12-11 12:32:05 +00:00
|
|
|
SignatureAlgorithm: s.determineSignatureAlgorithm(request.PreferredHash),
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
certBytes, err := x509.CreateCertificate(rand.Reader, template, s.certificate, request.CSR.PublicKey, s.key)
|
2021-08-24 20:02:14 +00:00
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not sign certificate: %w", err)
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certificate, err := x509.ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not parse certificate: %w", err)
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newTestSignerResponse(certificate), nil
|
|
|
|
}
|
|
|
|
|
2022-12-11 12:32:05 +00:00
|
|
|
func (s *testSigner) determineSignatureAlgorithm(hash crypto.Hash) x509.SignatureAlgorithm {
|
|
|
|
switch hash {
|
|
|
|
case crypto.SHA512:
|
|
|
|
return x509.ECDSAWithSHA512
|
|
|
|
case crypto.SHA384:
|
|
|
|
return x509.ECDSAWithSHA384
|
|
|
|
default:
|
|
|
|
return x509.ECDSAWithSHA384
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:02:14 +00:00
|
|
|
func TestSigning(t *testing.T) {
|
|
|
|
testRepository := testRepo{certs: make(map[string]x509.Certificate)}
|
|
|
|
testSigner := newTestSigner(t)
|
|
|
|
s := signing.NewX509Signing(testSigner, &testRepository)
|
|
|
|
|
|
|
|
csrKey, err := rsa.GenerateKey(rand.Reader, 3072)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not generate key pair")
|
|
|
|
}
|
|
|
|
|
|
|
|
csrTemplate := &x509.CertificateRequest{PublicKey: csrKey.Public()}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-12-11 12:32:05 +00:00
|
|
|
testRequest := &signing.SignerRequest{
|
|
|
|
CSR: csrTemplate,
|
|
|
|
SubjectDN: pkix.Name{CommonName: "Test Subject"},
|
|
|
|
Emails: []string{"test@example.org"},
|
|
|
|
DNSNames: nil,
|
|
|
|
PreferredHash: crypto.SHA384,
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signed, err := s.Sign(testRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-12-11 12:32:05 +00:00
|
|
|
cert := signed.Certificate
|
2021-08-24 20:02:14 +00:00
|
|
|
assert.Contains(t, testRepository.certs, cert.SerialNumber.Text(16))
|
|
|
|
assert.Equal(t, cert.Subject.CommonName, "Test Subject")
|
|
|
|
assert.Contains(t, cert.EmailAddresses, "test@example.org")
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestSigner(t *testing.T) *testSigner {
|
|
|
|
t.Helper()
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-12-11 12:32:05 +00:00
|
|
|
caKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2021-08-24 20:02:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not generate key pair: %v", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-24 06:03:51 +00:00
|
|
|
caTemplate := &x509.Certificate{Subject: pkix.Name{CommonName: "Test CA"}, SerialNumber: randomSerial(t)}
|
2021-08-24 20:02:14 +00:00
|
|
|
|
|
|
|
certificateBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, caKey.Public(), caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not self-sign CA certificate: %v", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2021-08-24 20:02:14 +00:00
|
|
|
caCertificate, err := x509.ParseCertificate(certificateBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create test CA certificate: %v", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-24 06:03:51 +00:00
|
|
|
return &testSigner{key: caKey, certificate: caCertificate, t: t}
|
2021-08-24 20:02:14 +00:00
|
|
|
}
|