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.
|
|
|
|
*/
|
|
|
|
|
2022-04-13 06:30:20 +00:00
|
|
|
package main
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
import (
|
2022-11-21 07:26:50 +00:00
|
|
|
"context"
|
2022-04-16 20:24:32 +00:00
|
|
|
"flag"
|
2022-11-20 17:59:37 +00:00
|
|
|
"fmt"
|
2022-04-16 20:24:32 +00:00
|
|
|
"os"
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/x509/revoking"
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/config"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/health"
|
2022-04-16 20:24:32 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/hsm"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/seriallink"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
commit string
|
|
|
|
date string
|
|
|
|
version string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-19 14:48:32 +00:00
|
|
|
defaultSignerConfigFile = "config.yaml"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 06:30:20 +00:00
|
|
|
func main() {
|
2022-04-16 20:24:32 +00:00
|
|
|
var (
|
2022-04-20 07:03:26 +00:00
|
|
|
showVersion, setupMode, verbose bool
|
2022-11-20 09:07:02 +00:00
|
|
|
signerConfigFile, logLevel string
|
|
|
|
logger *logrus.Logger
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
logger = logrus.New()
|
|
|
|
logger.SetOutput(os.Stdout)
|
|
|
|
logger.SetLevel(logrus.InfoLevel)
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.Infof("cacert-gosigner %s (%s) - built %s", version, commit, date)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-24 12:49:17 +00:00
|
|
|
flag.StringVar(&signerConfigFile, "config", defaultSignerConfigFile, "signer configuration file")
|
2022-04-16 20:24:32 +00:00
|
|
|
flag.BoolVar(&showVersion, "version", false, "show version")
|
2022-04-19 14:48:32 +00:00
|
|
|
flag.BoolVar(&setupMode, "setup", false, "setup mode")
|
2022-04-20 07:03:26 +00:00
|
|
|
flag.BoolVar(&verbose, "verbose", false, "verbose output")
|
2022-11-20 09:07:02 +00:00
|
|
|
flag.StringVar(&logLevel, "loglevel", "INFO", "log level")
|
2022-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if showVersion {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
parsedLevel, err := logrus.ParseLevel(logLevel)
|
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("could not parse log level")
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetLevel(parsedLevel)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
caConfig := loadConfig(signerConfigFile, logger)
|
2022-08-03 13:45:27 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
access := initializeHSM(caConfig, setupMode, verbose, logger)
|
2022-08-03 13:45:27 +00:00
|
|
|
|
|
|
|
if setupMode {
|
|
|
|
return
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
healthHandler := health.New(version, access)
|
2022-04-20 07:03:26 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
revokingRepositories, err := configureRepositories(caConfig)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not setup revoking repositories")
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchCRLHandler := revoking.NewFetchCRLHandler(revokingRepositories)
|
|
|
|
|
|
|
|
proto, err := protocol.New(
|
|
|
|
logger,
|
|
|
|
protocol.RegisterHealthHandler(healthHandler),
|
|
|
|
protocol.RegisterFetchCRLHandler(fetchCRLHandler),
|
|
|
|
)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("could not setup protocol handler")
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
serialHandler, err := seriallink.New(caConfig.GetSerial(), logger, proto)
|
2022-08-03 13:45:27 +00:00
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("could not setup serial link handler")
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = serialHandler.Close() }()
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
logger.Info("setup complete, starting signer operation")
|
|
|
|
|
|
|
|
if err = serialHandler.Run(context.Background()); err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("error in serial handler")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureRepositories(
|
|
|
|
caConfig *config.SignerConfig,
|
|
|
|
) (map[string]*revoking.X509Revoking, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
result := make(map[string]*revoking.X509Revoking)
|
|
|
|
|
|
|
|
for _, name := range caConfig.RootCAs() {
|
|
|
|
result[fmt.Sprintf("root-%s", name)], err = buildX509Revoking(caConfig, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range caConfig.SubordinateCAs() {
|
|
|
|
result[fmt.Sprintf("sub-%s", name)], err = buildX509Revoking(caConfig, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildX509Revoking(caConfig *config.SignerConfig, name string) (*revoking.X509Revoking, error) {
|
|
|
|
caDef, err := caConfig.GetCADefinition(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get CA definition for %s: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := caConfig.Repository(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get repository for %s: %w", name, err)
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
return revoking.NewX509Revoking(
|
|
|
|
repo,
|
|
|
|
caDef.KeyInfo.CRLSignatureAlgorithm,
|
|
|
|
caDef.Certificate,
|
|
|
|
caDef.KeyPair,
|
|
|
|
), nil
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
func initializeHSM(caConfig *config.SignerConfig, setupMode, verbose bool, logger *logrus.Logger) *hsm.Access {
|
2022-08-03 13:45:27 +00:00
|
|
|
opts := make([]hsm.ConfigOption, 0)
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
opts = append(opts, hsm.CaConfigOption(caConfig))
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if setupMode {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Infof("running in setup mode")
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
opts = append(opts, hsm.SetupModeOption())
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
opts = append(opts, hsm.VerboseLoggingOption())
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
access, err := hsm.NewAccess(logger, opts...)
|
2022-08-03 07:59:26 +00:00
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("could not setup HSM access")
|
2022-08-03 07:59:26 +00:00
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
err = access.EnsureCAKeysAndCertificates()
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatal("could not ensure CA keys and certificates exist")
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
return access
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
func loadConfig(signerConfigFile string, logger *logrus.Logger) *config.SignerConfig {
|
2022-08-03 13:45:27 +00:00
|
|
|
configFile, err := os.Open(signerConfigFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Fatalf("could not open signer configuration file: %s", signerConfigFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
caConfig, err := config.LoadConfiguration(configFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not load CA hierarchy: %v", err)
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
return caConfig
|
2022-04-13 06:30:20 +00:00
|
|
|
}
|