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 (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"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
|
|
|
|
signerConfigFile string
|
2022-08-03 07:59:26 +00:00
|
|
|
infoLog, errorLog *log.Logger
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
infoLog = log.New(os.Stdout, "INFO ", log.Ldate|log.Lmicroseconds|log.LUTC)
|
|
|
|
errorLog = log.New(os.Stderr, "ERROR ", log.Ldate|log.Lmicroseconds|log.LUTC)
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-08-03 07:59:26 +00:00
|
|
|
infoLog.Printf("cacert-gosigner %s (%s) - built %s\n", 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-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if showVersion {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
caConfig := LoadConfig(signerConfigFile, errorLog)
|
|
|
|
|
|
|
|
access := initializeHSM(caConfig, setupMode, verbose, infoLog, errorLog)
|
|
|
|
|
|
|
|
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-08-03 13:45:27 +00:00
|
|
|
proto, err := protocol.New(infoLog, errorLog, protocol.RegisterHealthHandler(healthHandler))
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-08-03 13:45:27 +00:00
|
|
|
errorLog.Fatalf("could not setup protocol handler: %v", err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
serialHandler, err := seriallink.New(caConfig.GetSerial(), proto)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatalf("could not setup serial link handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = serialHandler.Close() }()
|
|
|
|
|
|
|
|
if err = serialHandler.Run(); err != nil {
|
|
|
|
errorLog.Fatalf("error in serial handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
infoLog.Print("setup complete, starting signer operation")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeHSM(caConfig *config.SignerConfig, setupMode, verbose bool, infoLog, errorLog *log.Logger) *hsm.Access {
|
|
|
|
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-08-03 07:59:26 +00:00
|
|
|
infoLog.Print("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-08-03 13:45:27 +00:00
|
|
|
access, err := hsm.NewAccess(infoLog, opts...)
|
2022-08-03 07:59:26 +00:00
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatalf("could not setup HSM access: %v", err)
|
|
|
|
}
|
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-08-03 07:59:26 +00:00
|
|
|
errorLog.Fatalf("could not ensure CA keys and certificates exist: %v", err)
|
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-08-03 13:45:27 +00:00
|
|
|
func LoadConfig(signerConfigFile string, errorLog *log.Logger) *config.SignerConfig {
|
|
|
|
configFile, err := os.Open(signerConfigFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
2022-08-03 13:45:27 +00:00
|
|
|
errorLog.Fatalf("could not open signer configuration file %s: %v", signerConfigFile, err)
|
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-08-03 13:45:27 +00:00
|
|
|
errorLog.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
|
|
|
}
|