2022-04-19 14:48:32 +00:00
|
|
|
package hsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ThalesIgnite/crypto11"
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ctxKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxP11Contexts ctxKey = iota
|
|
|
|
ctxSetupMode
|
|
|
|
ctxSignerConfig
|
2022-04-20 07:03:26 +00:00
|
|
|
ctxVerboseLogging
|
2022-04-19 14:48:32 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
type ConfigOption func(ctx context.Context) context.Context
|
|
|
|
|
|
|
|
func CaConfigOption(signerConfig *config.SignerConfig) func(context.Context) context.Context {
|
|
|
|
return func(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, ctxSignerConfig, signerConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupModeOption() func(context.Context) context.Context {
|
|
|
|
return func(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, ctxSetupMode, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerboseLoggingOption() func(ctx context.Context) context.Context {
|
|
|
|
return func(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, ctxVerboseLogging, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
// SetupContext sets global context for HSM operations.
|
2022-04-20 07:03:26 +00:00
|
|
|
func SetupContext(options ...ConfigOption) context.Context {
|
2022-04-19 14:48:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, ctxP11Contexts, make(map[string]*crypto11.Context))
|
2022-04-20 07:03:26 +00:00
|
|
|
|
|
|
|
for _, opt := range options {
|
|
|
|
ctx = opt(ctx)
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSignerConfig(ctx context.Context) *config.SignerConfig {
|
2022-04-20 07:03:26 +00:00
|
|
|
signerConfig, ok := ctx.Value(ctxSignerConfig).(*config.SignerConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return signerConfig
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsSetupMode(ctx context.Context) bool {
|
2022-04-20 07:03:26 +00:00
|
|
|
setupMode, ok := ctx.Value(ctxSetupMode).(bool)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return setupMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsVerbose(ctx context.Context) bool {
|
|
|
|
verbose, ok := ctx.Value(ctxVerboseLogging).(bool)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return verbose
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetP11Context(ctx context.Context, entry *config.CaCertificateEntry) (*crypto11.Context, error) {
|
|
|
|
contexts := ctx.Value(ctxP11Contexts).(map[string]*crypto11.Context)
|
|
|
|
|
|
|
|
if p11Context, ok := contexts[entry.Storage]; ok {
|
|
|
|
return p11Context, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p11Context, err := prepareCrypto11Context(ctx, entry.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contexts[entry.Storage] = p11Context
|
|
|
|
|
|
|
|
return p11Context, nil
|
|
|
|
}
|