116 lines
2.7 KiB
Go
116 lines
2.7 KiB
Go
/*
|
|
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
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/ThalesIgnite/crypto11"
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/config"
|
|
)
|
|
|
|
type ctxKey int
|
|
|
|
const (
|
|
ctxP11Contexts ctxKey = iota
|
|
ctxSetupMode
|
|
ctxSignerConfig
|
|
ctxVerboseLogging
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// SetupContext sets global context for HSM operations.
|
|
func SetupContext(options ...ConfigOption) context.Context {
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, ctxP11Contexts, make(map[string]*crypto11.Context))
|
|
|
|
for _, opt := range options {
|
|
ctx = opt(ctx)
|
|
}
|
|
|
|
return ctx
|
|
}
|
|
|
|
func GetSignerConfig(ctx context.Context) *config.SignerConfig {
|
|
signerConfig, ok := ctx.Value(ctxSignerConfig).(*config.SignerConfig)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return signerConfig
|
|
}
|
|
|
|
func IsSetupMode(ctx context.Context) bool {
|
|
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
|
|
}
|
|
|
|
func GetP11Context(ctx context.Context, entry *config.CaCertificateEntry) (*crypto11.Context, error) {
|
|
contexts, ok := ctx.Value(ctxP11Contexts).(map[string]*crypto11.Context)
|
|
if !ok {
|
|
return nil, errors.New("type assertion failed")
|
|
}
|
|
|
|
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
|
|
}
|