Skip to content

Commit 4c4cad8

Browse files
authored
Merge pull request #146 from smlx/setup-slots-tweaks
Setup slots tweaks
2 parents a4d09fa + 63058a8 commit 4c4cad8

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: test
22
test: mod-tidy generate
3-
go test -v ./...
3+
CGO_ENABLED=1 go test -v ./...
44

55
.PHONY: generate
66
generate: mod-tidy
@@ -12,4 +12,4 @@ mod-tidy:
1212

1313
.PHONY: build
1414
build: test
15-
go build ./cmd/piv-agent
15+
CGO_ENABLED=1 go build ./cmd/piv-agent

cmd/piv-agent/setup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type SetupCmd struct {
2121
DecryptingKeys []string `kong:"default='cached,always,never',enum='cached,always,never',help='Generate decrypting keys with various touch policies (possible values: cached,always,never)'"`
2222
}
2323

24-
func interactivePIN() (uint64, error) {
24+
// interactiveNewPIN prompts twice for a new PIN.
25+
func interactiveNewPIN() (uint64, error) {
2526
fmt.Print("Enter a new PIN/PUK (6-8 digits): ")
2627
rawPIN, err := terminal.ReadPassword(int(os.Stdin.Fd()))
2728
fmt.Println()
@@ -49,7 +50,7 @@ func (cmd *SetupCmd) Run() error {
4950
// if PIN has not been specified, ask interactively
5051
var err error
5152
if cmd.PIN == 0 {
52-
cmd.PIN, err = interactivePIN()
53+
cmd.PIN, err = interactiveNewPIN()
5354
if err != nil {
5455
return err
5556
}

cmd/piv-agent/setupslots.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,44 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"strconv"
78

89
"github.com/smlx/piv-agent/internal/pinentry"
910
"github.com/smlx/piv-agent/internal/securitykey"
11+
"golang.org/x/crypto/ssh/terminal"
1012
)
1113

1214
// SetupSlotsCmd represents the setup command.
1315
type SetupSlotsCmd struct {
1416
Card string `kong:"help='Specify a smart card device'"`
1517
ResetSlots bool `kong:"help='Overwrite existing keys in the targeted slots'"`
1618
PIN uint64 `kong:"help='The PIN/PUK of the device (6-8 digits). Will be prompted interactively if not provided.'"`
17-
SigningKeys []string `kong:"required,enum='cached,always,never',help='Set up slots for signing keys with various touch policies (possible values cached,always,never)'"`
18-
DecryptingKeys []string `kong:"required,enum='cached,always,never',help='Set up slots for a decrypting keys with various touch polcies (possible values cached,always,never)'"`
19+
SigningKeys []string `kong:"enum='cached,always,never',help='Set up slots for signing keys with various touch policies (possible values cached,always,never)'"`
20+
DecryptingKeys []string `kong:"enum='cached,always,never',help='Set up slots for a decrypting keys with various touch polcies (possible values cached,always,never)'"`
21+
}
22+
23+
// interactivePIN prompts once for an existing PIN.
24+
func interactivePIN() (uint64, error) {
25+
fmt.Print("Enter the PIN/PUK (6-8 digits): ")
26+
rawPIN, err := terminal.ReadPassword(int(os.Stdin.Fd()))
27+
fmt.Println()
28+
if err != nil {
29+
return 0, fmt.Errorf("couldn't read PIN/PUK: %w", err)
30+
}
31+
pin, err := strconv.ParseUint(string(rawPIN), 10, 64)
32+
if err != nil {
33+
return 0, fmt.Errorf("invalid characters: %w", err)
34+
}
35+
return pin, nil
1936
}
2037

2138
// Run the setup-slot command to configure a slot on a security key.
2239
func (cmd *SetupSlotsCmd) Run() error {
40+
// validate keys specified
41+
if len(cmd.SigningKeys) == 0 && len(cmd.DecryptingKeys) == 0 {
42+
return fmt.Errorf("at least one key slot must be specified via --signing-keys=... or --decrypting-keys=... ")
43+
}
2344
// if PIN has not been specified, ask interactively
2445
var err error
2546
if cmd.PIN == 0 {

0 commit comments

Comments
 (0)