2022-04-24 07:25:04 +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 handles hardware security modules.
|
2022-04-16 20:24:32 +00:00
|
|
|
package hsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/asn1"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2022-05-01 10:36:17 +00:00
|
|
|
"path"
|
2022-04-16 20:24:32 +00:00
|
|
|
"syscall"
|
2022-04-23 16:34:51 +00:00
|
|
|
"time"
|
2022-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
"github.com/ThalesIgnite/crypto11"
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/cainfo"
|
2022-11-28 16:39:48 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/config"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/health"
|
2022-12-02 17:31:59 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 09:52:54 +00:00
|
|
|
var (
|
|
|
|
// 1.3.6.1.4.1.18506.2.3.1 Class3 Policy Version 1
|
|
|
|
oidCAcertClass3PolicyV1 = []int{1, 3, 6, 1, 4, 1, 18506, 2, 3, 1}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
errKeyGenerationRefused = errors.New("not in setup mode, refusing key generation")
|
|
|
|
errCertificateGenerationRefused = errors.New("not in setup mode, refusing certificate generation")
|
2022-04-19 09:52:54 +00:00
|
|
|
)
|
|
|
|
|
2022-05-01 10:36:17 +00:00
|
|
|
type caFile struct {
|
|
|
|
sc *config.SignerConfig
|
|
|
|
label string
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
func (c *caFile) buildCertificatePath(caDirectory string) string {
|
|
|
|
fileName := c.sc.CertificateFileName(c.label)
|
|
|
|
|
|
|
|
if caDirectory == "" {
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(caDirectory, fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *caFile) loadCertificate(caDirectory string) (*x509.Certificate, error) {
|
|
|
|
certFile := c.buildCertificatePath(caDirectory)
|
|
|
|
|
|
|
|
certFileInfo, err := os.Stat(certFile)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, syscall.ENOENT) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not get info for %s: %w", certFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !certFileInfo.Mode().IsRegular() {
|
|
|
|
return nil, fmt.Errorf("certificate file %s is not a regular file", certFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
certData, err := os.ReadFile(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read %s: %w", certFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certificate, err := x509.ParseCertificate(certData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse certificate from %s: %w", certFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *caFile) storeCertificate(caDirectory string, certificate []byte) error {
|
|
|
|
certFile := c.buildCertificatePath(caDirectory)
|
|
|
|
|
|
|
|
err := os.WriteFile(certFile, certificate, 0o600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write certificate file %s: %w", certFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
type Access struct {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger *logrus.Logger
|
2022-08-03 07:59:26 +00:00
|
|
|
caDirectory string
|
|
|
|
signerConfig *config.SignerConfig
|
|
|
|
p11Contexts map[string]*crypto11.Context
|
|
|
|
setupMode bool
|
|
|
|
verbose bool
|
|
|
|
}
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
func (a *Access) Healthy() (*health.Info, error) {
|
|
|
|
healthy := true
|
|
|
|
|
|
|
|
moreInfo := make(map[string]string)
|
|
|
|
|
|
|
|
for _, ca := range a.signerConfig.RootCAs() {
|
2022-12-02 17:31:59 +00:00
|
|
|
_, err := a.GetRootCACertificate(ca)
|
2022-08-03 13:45:27 +00:00
|
|
|
if err != nil {
|
|
|
|
healthy = false
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusFailed)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
continue
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusOk)
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 14:01:06 +00:00
|
|
|
for _, ca := range a.signerConfig.SubordinateCAs() {
|
2022-12-02 17:31:59 +00:00
|
|
|
_, err := a.GetSubordinateCACertificate(ca)
|
2022-08-03 13:45:27 +00:00
|
|
|
if err != nil {
|
|
|
|
healthy = false
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusFailed)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
continue
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def, err := a.signerConfig.GetCADefinition(ca)
|
|
|
|
if err != nil {
|
|
|
|
healthy = false
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusFailed)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
continue
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = a.getKeyPair(ca, def.KeyInfo)
|
|
|
|
if err != nil {
|
|
|
|
healthy = false
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusFailed)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
continue
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
moreInfo[ca] = string(messages.CertStatusOk)
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &health.Info{
|
|
|
|
Healthy: healthy,
|
|
|
|
Source: "HSM",
|
|
|
|
MoreInfo: moreInfo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
func NewAccess(logger *logrus.Logger, options ...ConfigOption) (*Access, error) {
|
|
|
|
access := &Access{logger: logger}
|
2022-08-03 07:59:26 +00:00
|
|
|
access.setupContext(options...)
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
return access, nil
|
|
|
|
}
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
func (a *Access) GetCAInfo(name string) (*cainfo.Result, error) {
|
|
|
|
sc := a.GetSignerConfig()
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
caFile := &caFile{sc: sc, label: name}
|
2022-05-01 10:36:17 +00:00
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
def, err := sc.GetCADefinition(name)
|
2022-05-01 10:36:17 +00:00
|
|
|
if err != nil {
|
2022-12-02 17:31:59 +00:00
|
|
|
return nil, fmt.Errorf("certificate %s unknown: %w", name, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
certificate, err := caFile.loadCertificate(a.caDirectory)
|
2022-05-01 10:36:17 +00:00
|
|
|
if err != nil {
|
2022-12-02 17:31:59 +00:00
|
|
|
return nil, fmt.Errorf("certificate %s not available: %w", name, err)
|
2022-05-01 10:36:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
return &cainfo.Result{
|
|
|
|
Certificate: certificate.Raw,
|
2022-12-11 12:32:05 +00:00
|
|
|
Profiles: cainfo.MapProfiles(def.Profiles),
|
2022-12-02 17:31:59 +00:00
|
|
|
}, nil
|
2022-05-01 10:36:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
func (a *Access) GetRootCACertificate(label string) (*x509.Certificate, error) {
|
2022-04-19 14:48:32 +00:00
|
|
|
var (
|
|
|
|
certificate *x509.Certificate
|
|
|
|
keyPair crypto.Signer
|
|
|
|
)
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
sc := a.GetSignerConfig()
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
caCert, err := sc.GetCADefinition(label)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not get CA definition for label %s: %w", label, err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if !caCert.IsRoot() {
|
|
|
|
return nil, fmt.Errorf("CA definition %s is not a root CA definition", label)
|
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-05-01 10:36:17 +00:00
|
|
|
caFile := &caFile{sc: sc, label: label}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
certificate, err = caFile.loadCertificate(a.caDirectory)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if certificate != nil && !a.IsSetupMode() {
|
2022-04-19 14:48:32 +00:00
|
|
|
caCert.Certificate = certificate
|
|
|
|
|
|
|
|
return certificate, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:12:26 +00:00
|
|
|
if a.IsSetupMode() {
|
|
|
|
keyPair, err = a.getKeyPair(label, caCert.KeyInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if certificate != nil {
|
|
|
|
err := certificateMatches(certificate, keyPair)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-11-30 19:12:26 +00:00
|
|
|
caCert.Certificate, caCert.KeyPair = certificate, keyPair
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return certificate, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if !a.IsSetupMode() {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, errCertificateGenerationRefused
|
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
notBefore, notAfter := sc.CalculateValidity(caCert, time.Now())
|
2022-04-19 14:48:32 +00:00
|
|
|
subject := sc.CalculateSubject(caCert)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
certificate, err = a.generateRootCACertificate(
|
2022-05-01 10:36:17 +00:00
|
|
|
caFile,
|
2022-04-16 20:24:32 +00:00
|
|
|
keyPair,
|
|
|
|
&x509.Certificate{
|
|
|
|
Subject: subject,
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
MaxPathLen: 0,
|
|
|
|
MaxPathLenZero: false,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
KeyUsage: x509.KeyUsageCRLSign | x509.KeyUsageCertSign,
|
|
|
|
IsCA: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
p11Context, err := a.GetP11Context(caCert)
|
2022-04-19 14:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
err = addCertificate(p11Context, label, certificate)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
caCert.Certificate, caCert.KeyPair = certificate, keyPair
|
|
|
|
|
|
|
|
return certificate, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 14:01:06 +00:00
|
|
|
func (a *Access) GetSubordinateCACertificate(certLabel string) (*x509.Certificate, error) {
|
2022-04-19 14:48:32 +00:00
|
|
|
var (
|
|
|
|
certificate *x509.Certificate
|
|
|
|
keyPair crypto.Signer
|
|
|
|
)
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
sc := a.GetSignerConfig()
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
caCert, err := sc.GetCADefinition(certLabel)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not get CA definition for label %s: %w", certLabel, err)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if caCert.IsRoot() {
|
|
|
|
return nil, fmt.Errorf(
|
2022-08-03 14:01:06 +00:00
|
|
|
"CA definition %s is a root CA definition, subordinate expected",
|
2022-04-19 14:48:32 +00:00
|
|
|
certLabel,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
keyPair, err = a.getKeyPair(certLabel, caCert.KeyInfo)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 10:36:17 +00:00
|
|
|
certFile := &caFile{sc: sc, label: certLabel}
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
certificate, err = certFile.loadCertificate(a.caDirectory)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if certificate != nil {
|
|
|
|
err := certificateMatches(certificate, keyPair)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
caCert.Certificate, caCert.KeyPair = certificate, keyPair
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return certificate, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if !a.IsSetupMode() {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, errCertificateGenerationRefused
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
notBefore, notAfter := sc.CalculateValidity(caCert, time.Now())
|
2022-04-19 14:48:32 +00:00
|
|
|
subject := sc.CalculateSubject(caCert)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-08-03 14:01:06 +00:00
|
|
|
certificate, err = a.generateSubordinateCACertificate(
|
2022-05-01 10:36:17 +00:00
|
|
|
certFile,
|
2022-04-19 14:48:32 +00:00
|
|
|
sc,
|
|
|
|
certLabel,
|
2022-04-16 20:24:32 +00:00
|
|
|
keyPair.Public(),
|
|
|
|
&x509.Certificate{
|
|
|
|
Subject: subject,
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
MaxPathLen: caCert.MaxPathLen,
|
|
|
|
MaxPathLenZero: true,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
|
|
ExtKeyUsage: caCert.ExtKeyUsage,
|
2022-04-19 14:48:32 +00:00
|
|
|
IssuingCertificateURL: []string{sc.BuildIssuerURL(caCert)},
|
|
|
|
OCSPServer: []string{sc.BuildOCSPURL(caCert)},
|
2022-11-30 19:12:26 +00:00
|
|
|
CRLDistributionPoints: []string{sc.BuildCRLUrl(certLabel)},
|
2022-04-16 20:24:32 +00:00
|
|
|
PolicyIdentifiers: []asn1.ObjectIdentifier{
|
|
|
|
// use policy identifiers from http://wiki.cacert.org/OidAllocation
|
2022-04-19 09:52:54 +00:00
|
|
|
oidCAcertClass3PolicyV1,
|
2022-04-16 20:24:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
p11Context, err := a.GetP11Context(caCert)
|
2022-04-19 14:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
err = addCertificate(p11Context, certLabel, certificate)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
caCert.Certificate, caCert.KeyPair = certificate, keyPair
|
|
|
|
|
|
|
|
return certificate, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 14:01:06 +00:00
|
|
|
func (a *Access) generateSubordinateCACertificate(
|
2022-05-01 10:36:17 +00:00
|
|
|
certFile *caFile,
|
2022-04-24 07:25:04 +00:00
|
|
|
config *config.SignerConfig,
|
|
|
|
certLabel string,
|
|
|
|
publicKey crypto.PublicKey,
|
|
|
|
template *x509.Certificate,
|
|
|
|
) (*x509.Certificate, error) {
|
2022-04-19 14:48:32 +00:00
|
|
|
parent, err := config.GetParentCA(certLabel)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not get parent CA for label %s: %w", certLabel, err)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
serial, err := randomSerialNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
template.SerialNumber = serial
|
2022-04-19 14:48:32 +00:00
|
|
|
template.SignatureAlgorithm, err = determineSignatureAlgorithm(parent.KeyPair)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
template,
|
2022-04-19 14:48:32 +00:00
|
|
|
parent.Certificate,
|
2022-04-16 20:24:32 +00:00
|
|
|
publicKey,
|
2022-04-19 14:48:32 +00:00
|
|
|
parent.KeyPair,
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-11-30 19:12:26 +00:00
|
|
|
return nil, fmt.Errorf("could not create subordinate CA certificate %s: %w", certLabel, err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:12:26 +00:00
|
|
|
err = certFile.storeCertificate(a.caDirectory, certBytes)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-05-01 10:36:17 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certificate, err := x509.ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse generated certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addCertificate(p11Context *crypto11.Context, label string, certificate *x509.Certificate) error {
|
2022-04-24 07:25:04 +00:00
|
|
|
objectID, err := randomObjectID()
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
err = p11Context.ImportCertificateWithLabel(objectID, []byte(label), certificate)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not import certificate into token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
func (a *Access) getKeyPair(label string, keyInfo *config.PrivateKeyInfo) (crypto.Signer, error) {
|
|
|
|
sc := a.GetSignerConfig()
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
cert, err := sc.GetCADefinition(label)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not get CA definition for label %s: %w", label, err)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cert.KeyPair != nil {
|
|
|
|
return cert.KeyPair, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
p11Context, err := a.GetP11Context(cert)
|
2022-04-19 14:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
keyPair, err := p11Context.FindKeyPair(nil, []byte(label))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find requested key pair: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyPair != nil {
|
|
|
|
return keyPair, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
if !a.IsSetupMode() {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, errKeyGenerationRefused
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
switch keyInfo.Algorithm {
|
|
|
|
case x509.RSA:
|
|
|
|
keyPair, err = generateRSAKeyPair(p11Context, label, keyInfo)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
case x509.ECDSA:
|
|
|
|
keyPair, err = generateECDSAKeyPair(p11Context, label, keyInfo)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
default:
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"could not generate private key with label %s with unsupported key algorithm %s",
|
|
|
|
label,
|
|
|
|
keyInfo.Algorithm,
|
|
|
|
)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return keyPair, nil
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
func generateECDSAKeyPair(
|
|
|
|
p11Context *crypto11.Context,
|
|
|
|
label string,
|
|
|
|
keyInfo *config.PrivateKeyInfo,
|
|
|
|
) (crypto11.Signer, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
newObjectID []byte
|
|
|
|
signer crypto11.Signer
|
|
|
|
)
|
|
|
|
|
|
|
|
newObjectID, err = randomObjectID()
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
signer, err = p11Context.GenerateECDSAKeyPairWithLabel(newObjectID, []byte(label), keyInfo.EccCurve)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate ECDSA key pair: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signer, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
func generateRSAKeyPair(
|
|
|
|
p11Context *crypto11.Context,
|
|
|
|
label string,
|
|
|
|
keyInfo *config.PrivateKeyInfo,
|
|
|
|
) (crypto11.Signer, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
newObjectID []byte
|
|
|
|
signer crypto11.Signer
|
|
|
|
)
|
|
|
|
|
|
|
|
newObjectID, err = randomObjectID()
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
signer, err = p11Context.GenerateRSAKeyPairWithLabel(newObjectID, []byte(label), keyInfo.RSABits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate RSA key pair: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signer, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
func randomObjectID() ([]byte, error) {
|
|
|
|
const objectIDSize = 20
|
|
|
|
|
|
|
|
result := make([]byte, objectIDSize)
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
_, err := rand.Read(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create new random object id: %w", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
func (a *Access) generateRootCACertificate(
|
2022-05-01 10:36:17 +00:00
|
|
|
certFile *caFile,
|
2022-04-24 07:25:04 +00:00
|
|
|
keyPair crypto.Signer,
|
|
|
|
template *x509.Certificate,
|
|
|
|
) (*x509.Certificate, error) {
|
2022-04-16 20:24:32 +00:00
|
|
|
serial, err := randomSerialNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
template.SerialNumber = serial
|
|
|
|
template.SignatureAlgorithm, err = determineSignatureAlgorithm(keyPair)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
template,
|
|
|
|
template,
|
|
|
|
keyPair.Public(),
|
|
|
|
keyPair,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create root certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:12:26 +00:00
|
|
|
if err = certFile.storeCertificate(a.caDirectory, certBytes); err != nil {
|
2022-05-01 10:36:17 +00:00
|
|
|
return nil, err
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certificate, err := x509.ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse generated certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func determineSignatureAlgorithm(keyPair crypto.Signer) (x509.SignatureAlgorithm, error) {
|
|
|
|
switch keyPair.Public().(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
return x509.ECDSAWithSHA256, nil
|
|
|
|
case *rsa.PublicKey:
|
|
|
|
return x509.SHA256WithRSA, nil
|
|
|
|
default:
|
|
|
|
return x509.UnknownSignatureAlgorithm,
|
|
|
|
fmt.Errorf("could not determine signature algorithm for key of type %T", keyPair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
func certificateMatches(certificate *x509.Certificate, key crypto.Signer) error {
|
2022-04-16 20:24:32 +00:00
|
|
|
switch v := certificate.PublicKey.(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
if pub, ok := key.Public().(*ecdsa.PublicKey); ok {
|
|
|
|
if v.Equal(pub) {
|
2022-08-03 07:59:26 +00:00
|
|
|
return nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case *rsa.PublicKey:
|
|
|
|
if pub, ok := key.Public().(*rsa.PublicKey); ok {
|
|
|
|
if v.Equal(pub) {
|
2022-08-03 07:59:26 +00:00
|
|
|
return nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2022-08-03 07:59:26 +00:00
|
|
|
return fmt.Errorf("unsupported public key %v", v)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
return fmt.Errorf(
|
2022-04-16 20:24:32 +00:00
|
|
|
"public key from certificate does not match private key: %s != %s",
|
|
|
|
certificate.PublicKey,
|
|
|
|
key.Public(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomSerialNumber() (*big.Int, error) {
|
2022-04-24 07:25:04 +00:00
|
|
|
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate serial number: %w", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
return serialNumber, nil
|
|
|
|
}
|