You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
2 years ago
|
/*
|
||
|
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.
|
||
|
*/
|
||
|
|
||
2 years ago
|
package hsm
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"syscall"
|
||
|
|
||
|
"github.com/ThalesIgnite/crypto11"
|
||
2 years ago
|
"github.com/sirupsen/logrus"
|
||
2 years ago
|
"golang.org/x/term"
|
||
|
)
|
||
|
|
||
2 years ago
|
func (a *Access) prepareCrypto11Context(label string) (*crypto11.Context, error) {
|
||
2 years ago
|
var (
|
||
|
err error
|
||
|
p11Context *crypto11.Context
|
||
|
)
|
||
|
|
||
2 years ago
|
storage, err := a.GetSignerConfig().GetKeyStorage(label)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, fmt.Errorf("key storage %s not available: %w", label, err)
|
||
|
}
|
||
|
|
||
2 years ago
|
p11Config := &crypto11.Config{
|
||
|
Path: storage.Module,
|
||
|
TokenLabel: storage.Label,
|
||
|
}
|
||
|
|
||
2 years ago
|
a.logger.Infof("using PKCS#11 module %s", p11Config.Path)
|
||
|
a.logger.Infof("looking for token with label %s", p11Config.TokenLabel)
|
||
2 years ago
|
|
||
2 years ago
|
p11Config.Pin, err = getPin(p11Config, a.logger)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
p11Context, err = crypto11.Configure(p11Config)
|
||
|
if err != nil {
|
||
2 years ago
|
return nil, fmt.Errorf("could not configure PKCS#11 library: %w", err)
|
||
2 years ago
|
}
|
||
|
|
||
|
return p11Context, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func getPin(p11Config *crypto11.Config, logger *logrus.Logger) (string, error) {
|
||
2 years ago
|
var err error
|
||
|
|
||
2 years ago
|
tokenPinEnv := strings.NewReplacer(
|
||
|
"-", "_",
|
||
|
" ", "_",
|
||
|
"(", "_",
|
||
|
")", "_",
|
||
|
).Replace(p11Config.TokenLabel)
|
||
2 years ago
|
tokenPinEnv = strings.ToUpper(tokenPinEnv)
|
||
|
tokenPinEnv = fmt.Sprintf("TOKEN_PIN_%s", tokenPinEnv)
|
||
|
|
||
2 years ago
|
pin, found := os.LookupEnv(tokenPinEnv)
|
||
|
if !found {
|
||
2 years ago
|
logger.Warnf("environment variable %s has not been set", tokenPinEnv)
|
||
2 years ago
|
|
||
|
if !term.IsTerminal(syscall.Stdin) {
|
||
|
return "", errors.New("stdin is not a terminal")
|
||
|
}
|
||
|
|
||
2 years ago
|
_, 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)
|
||
|
}
|
||
2 years ago
|
|
||
|
bytePin, err := term.ReadPassword(syscall.Stdin)
|
||
|
if err != nil {
|
||
2 years ago
|
return "", fmt.Errorf("could not read PIN: %w", err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
_, err = fmt.Fprintln(os.Stdout)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("could not write to stdout: %w", err)
|
||
|
}
|
||
2 years ago
|
|
||
|
pin = string(bytePin)
|
||
|
}
|
||
|
|
||
|
return strings.TrimSpace(pin), nil
|
||
|
}
|