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-19 14:48:32 +00:00
|
|
|
package hsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/ThalesIgnite/crypto11"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareCrypto11Context(ctx context.Context, label string) (*crypto11.Context, error) {
|
|
|
|
storage, ok := GetSignerConfig(ctx).KeyStorage[label]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not find storage definition with label %s", label)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
p11Context *crypto11.Context
|
|
|
|
)
|
|
|
|
|
|
|
|
p11Config := &crypto11.Config{
|
|
|
|
Path: storage.Module,
|
|
|
|
TokenLabel: storage.Label,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("using PKCS#11 module %s", p11Config.Path)
|
|
|
|
log.Printf("looking for token with label %s", p11Config.TokenLabel)
|
|
|
|
|
|
|
|
p11Config.Pin, err = getPin(p11Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p11Context, err = crypto11.Configure(p11Config)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not configure PKCS#11 library: %w", err)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p11Context, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPin(p11Config *crypto11.Config) (string, error) {
|
2022-04-24 07:25:04 +00:00
|
|
|
var err error
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
tokenPinEnv := strings.ReplaceAll(p11Config.TokenLabel, "-", "_")
|
|
|
|
tokenPinEnv = strings.ReplaceAll(tokenPinEnv, " ", "_")
|
|
|
|
tokenPinEnv = strings.ToUpper(tokenPinEnv)
|
|
|
|
tokenPinEnv = fmt.Sprintf("TOKEN_PIN_%s", tokenPinEnv)
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
pin, found := os.LookupEnv(tokenPinEnv)
|
|
|
|
if !found {
|
|
|
|
log.Printf("environment variable %s has not been set", tokenPinEnv)
|
|
|
|
|
|
|
|
if !term.IsTerminal(syscall.Stdin) {
|
|
|
|
return "", errors.New("stdin is not a terminal")
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
_, err = fmt.Fprintf(os.Stdout, "Enter PIN for token %s: ", p11Config.TokenLabel)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not write PIN prompt: %w", err)
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
bytePin, err := term.ReadPassword(syscall.Stdin)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return "", fmt.Errorf("could not read PIN: %w", err)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
_, err = fmt.Fprintln(os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not write to stdout: %w", err)
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
pin = string(bytePin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(pin), nil
|
|
|
|
}
|