-
Notifications
You must be signed in to change notification settings - Fork 38
/
cac.go
179 lines (158 loc) · 4.75 KB
/
cac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cli
import (
"fmt"
"os"
"path"
"regexp"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tcnksm/go-input"
"pault.ag/go/pksigner"
)
// ErrInvalidPath is an invalid path error
type ErrInvalidPath struct {
Path string
}
// Error is an error return
func (e *ErrInvalidPath) Error() string {
return fmt.Sprintf("invalid path %q", e.Path)
}
// ErrInvalidLabel is an invalid label error
type ErrInvalidLabel struct {
Cert string
Key string
}
// Error is an error return
func (e *ErrInvalidLabel) Error() string {
return fmt.Sprintf("invalid cert label %q or key label %q", e.Cert, e.Key)
}
const (
// CACFlag indicates that a CAC should be used
CACFlag string = "cac"
// PKCS11ModuleFlag is the location of the PCKS11 module to use with the smart card
PKCS11ModuleFlag string = "pkcs11module"
// TokenLabelFlag is the Token Label to use with the smart card
TokenLabelFlag string = "tokenlabel"
// CertLabelFlag is the Certificate Label to use with the smart card
CertLabelFlag string = "certlabel"
// KeyLabelFlag is the Key Label to use with the smart card
KeyLabelFlag string = "keylabel"
)
var pkcs11Modules = []string{
"opensc-pkcs11.so",
"cackey.dylib",
}
// InitCACFlags initializes the CAC Flags
func InitCACFlags(flag *pflag.FlagSet) {
flag.Bool(CACFlag, false, "Use a CAC for authentication")
flag.String(PKCS11ModuleFlag, "/usr/local/lib/opensc-pkcs11.so", "Smart card: Path to the PKCS11 module to use")
flag.String(TokenLabelFlag, "", "Smart card: name of the token to use")
flag.String(CertLabelFlag, "Certificate for PIV Authentication", "Smart card: label of the public cert")
flag.String(KeyLabelFlag, "PIV AUTH key", "Smart card: label of the private key")
}
// CheckCAC validates CAC command line flags
func CheckCAC(v *viper.Viper) error {
if v.GetBool(CACFlag) {
pkcs11ModulePath := v.GetString(PKCS11ModuleFlag)
if pkcs11ModulePath == "" {
return fmt.Errorf("%q is invalid: %w", PKCS11ModuleFlag, &ErrInvalidPath{Path: pkcs11ModulePath})
} else if _, err := os.Stat(pkcs11ModulePath); err != nil {
return fmt.Errorf("%q is invalid: %w", PKCS11ModuleFlag, &ErrInvalidPath{Path: pkcs11ModulePath})
}
if pkcs11Base := path.Base(pkcs11ModulePath); !stringSliceContains(pkcs11Modules, pkcs11Base) {
return fmt.Errorf("invalid PKCS11 module %s, expecting one of %q", pkcs11ModulePath, pkcs11Modules)
}
certLabel := v.GetString(CertLabelFlag)
keyLabel := v.GetString(KeyLabelFlag)
if certLabel == "" || keyLabel == "" {
return fmt.Errorf("%q or %q is invalid: %w", CertLabelFlag, KeyLabelFlag, &ErrInvalidLabel{Cert: certLabel, Key: keyLabel})
}
}
return nil
}
// GetCACStore retrieves the CAC store
// Call 'defer store.Close()' after retrieving the store
func GetCACStore(v *viper.Viper) (*pksigner.Store, error) {
pkcs11ModulePath := v.GetString(PKCS11ModuleFlag)
tokenLabel := v.GetString(TokenLabelFlag)
certLabel := v.GetString(CertLabelFlag)
keyLabel := v.GetString(KeyLabelFlag)
pkcsConfig := pksigner.Config{
Module: pkcs11ModulePath,
CertificateLabel: certLabel,
PrivateKeyLabel: keyLabel,
TokenLabel: tokenLabel,
}
store, errPKCS11New := pksigner.New(pkcsConfig)
if errPKCS11New != nil {
return nil, errPKCS11New
}
inputUI := &input.UI{
Writer: os.Stderr,
Reader: os.Stdin,
}
pin, errUIAsk := inputUI.Ask("CAC PIN", &input.Options{
Default: "",
HideOrder: true,
HideDefault: true,
Required: true,
Loop: true,
Mask: true,
ValidateFunc: func(input string) error {
matched, matchErr := regexp.Match("^\\d+$", []byte(input))
if matchErr != nil {
return matchErr
}
if !matched {
return errors.New("Invalid PIN format")
}
return nil
},
})
if errUIAsk != nil {
return nil, errUIAsk
}
errLogin := store.Login(pin)
if errLogin != nil {
return nil, errLogin
}
return store, nil
}
// CACStoreLogin login to existing CAC store
// Call 'defer store.Close()' after retrieving the store
func CACStoreLogin(_ *viper.Viper, store *pksigner.Store) (*pksigner.Store, error) {
if store == nil {
return nil, errors.New("Do not have an existing CAC store. Use GetCACStore()")
}
inputUI := &input.UI{
Writer: os.Stderr,
Reader: os.Stdin,
}
pin, errUIAsk := inputUI.Ask("CAC PIN", &input.Options{
Default: "",
HideOrder: true,
HideDefault: true,
Required: true,
Loop: true,
Mask: true,
ValidateFunc: func(input string) error {
matched, matchErr := regexp.Match("^\\d+$", []byte(input))
if matchErr != nil {
return matchErr
}
if !matched {
return errors.New("Invalid PIN format")
}
return nil
},
})
if errUIAsk != nil {
return nil, errUIAsk
}
errLogin := store.Login(pin)
if errLogin != nil {
return nil, errLogin
}
return store, nil
}