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-12-02 11:54:07 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/config"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/handler"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/health"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/hsm"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/serial"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/x509/revoking"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-28 16:10:46 +00:00
|
|
|
commit = "dev"
|
|
|
|
date = "unknown"
|
|
|
|
version = "unknown"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-12-02 11:54:07 +00:00
|
|
|
framer, err := protocol.NewCOBSFramer(logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not create framer")
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
healthHandler := health.New(version, access)
|
2022-04-20 07:03:26 +00:00
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
revokingRepositories, err := configureRepositories(caConfig, logger)
|
2022-11-20 17:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not setup revoking repositories")
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchCRLHandler := revoking.NewFetchCRLHandler(revokingRepositories)
|
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
proto, err := handler.New(
|
2022-11-20 17:59:37 +00:00
|
|
|
logger,
|
2022-11-28 16:39:48 +00:00
|
|
|
handler.RegisterHealthHandler(healthHandler),
|
|
|
|
handler.RegisterFetchCRLHandler(fetchCRLHandler),
|
2022-12-02 17:31:59 +00:00
|
|
|
handler.RegisterCAInfoHandler(access),
|
2022-11-20 17:59:37 +00:00
|
|
|
)
|
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-12-02 11:54:07 +00:00
|
|
|
serialHandler, err := serial.New(caConfig.GetSerial(), logger, framer, 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
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
logger.Info("setup complete, starting signer operation")
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
if err = runSigner(logger, serialHandler); err != nil {
|
2022-12-04 12:47:51 +00:00
|
|
|
logger.WithError(err).Error("error running serial handler")
|
|
|
|
|
|
|
|
os.Exit(1)
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
func runSigner(logger *logrus.Logger, serialHandler *serial.Handler) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2022-12-04 12:47:51 +00:00
|
|
|
defer func() { _ = serialHandler.Close() }()
|
2022-12-02 11:54:07 +00:00
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
|
|
|
|
logger.Info("received shutdown signal")
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := serialHandler.Run(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error from serial handler: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
func configureRepositories(
|
|
|
|
caConfig *config.SignerConfig,
|
2022-11-30 17:47:18 +00:00
|
|
|
logger *logrus.Logger,
|
2022-11-20 17:59:37 +00:00
|
|
|
) (map[string]*revoking.X509Revoking, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
result := make(map[string]*revoking.X509Revoking)
|
|
|
|
|
|
|
|
for _, name := range caConfig.RootCAs() {
|
2022-12-03 10:44:39 +00:00
|
|
|
result[name], err = buildX509Revoking(caConfig, name, logger)
|
2022-11-20 17:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range caConfig.SubordinateCAs() {
|
2022-12-03 10:44:39 +00:00
|
|
|
result[name], err = buildX509Revoking(caConfig, name, logger)
|
2022-11-20 17:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
func buildX509Revoking(
|
|
|
|
caConfig *config.SignerConfig,
|
|
|
|
name string,
|
|
|
|
logger *logrus.Logger,
|
|
|
|
) (*revoking.X509Revoking, error) {
|
2022-11-20 17:59:37 +00:00
|
|
|
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,
|
2022-11-30 17:47:18 +00:00
|
|
|
logger,
|
2022-11-20 17:59:37 +00:00
|
|
|
), 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
|
|
|
}
|