-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.go
146 lines (130 loc) · 3.63 KB
/
generate.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
package main
import (
"crypto"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"github.com/cashapp/pivit/cmd/pivit/utils"
"github.com/go-piv/piv-go/piv"
"github.com/pkg/errors"
)
// commandGenerate generates a new keypair and certificate signing request
func commandGenerate() error {
cards, err := piv.Cards()
if err != nil {
return errors.Wrap(err, "enumerate smart cards")
}
if len(cards) != 1 {
return errors.New("no smart card found")
}
yk, err := piv.Open(cards[0])
if err != nil {
return errors.Wrap(err, "open smart card")
}
defer func() {
_ = yk.Close()
}()
pin, err := utils.GetPin()
if err != nil {
return errors.Wrap(err, "get pin")
}
managementKey := deriveManagementKey(pin)
key := piv.Key{
Algorithm: piv.AlgorithmEC384,
PINPolicy: piv.PINPolicyNever,
TouchPolicy: piv.TouchPolicyAlways,
}
publicKey, err := yk.GenerateKey(*managementKey, piv.SlotCardAuthentication, key)
if err != nil {
return errors.Wrap(err, "generate new key")
}
deviceCert, err := yk.AttestationCertificate()
if err != nil {
return errors.Wrap(err, "device cert")
}
fmt.Println("Printing Yubikey device attestation certificate:")
printCertificate(deviceCert)
keyCert, err := yk.Attest(piv.SlotCardAuthentication)
if err != nil {
return errors.Wrap(err, "attest key")
}
fmt.Println("Printing generated key certificate:")
printCertificate(keyCert)
err = yk.SetCertificate(*managementKey, piv.SlotCardAuthentication, keyCert)
if err != nil {
return errors.Wrap(err, "set yubikey certificate")
}
auth := piv.KeyAuth{
PINPolicy: piv.PINPolicyOnce,
PINPrompt: func() (string, error) {
fmt.Println("Touch Yubikey now to sign your key...")
return pin, nil
},
}
privateKey, err := yk.PrivateKey(piv.SlotCardAuthentication, publicKey, auth)
if err != nil {
return errors.Wrap(err, "access private key")
}
attestation, err := piv.Verify(deviceCert, keyCert)
if err != nil {
return errors.Wrap(err, "verify device certificate")
}
certRequest, err := certificateRequest(strconv.FormatUint(uint64(attestation.Serial), 10), privateKey)
fmt.Println("Printing certificate signing request:")
printCsr(certRequest)
_ = yk.Close()
return nil
}
func deriveManagementKey(pin string) *[24]byte {
hash := crypto.SHA256.New()
sha1 := hash.Sum([]byte(pin))
var mk [24]byte
copy(mk[:], sha1[:24])
return &mk
}
func certificateRequest(serialNumber string, privateKey crypto.PrivateKey) ([]byte, error) {
emailAddress := os.Getenv("PIVIT_EMAIL")
pivitOrg := strings.Split(os.Getenv("PIVIT_ORG"), ",")
pivitOrgUnit := strings.Split(os.Getenv("PIVIT_ORG_UNIT"), ",")
subject := pkix.Name{
Organization: pivitOrg,
OrganizationalUnit: pivitOrgUnit,
SerialNumber: serialNumber,
CommonName: emailAddress,
}
certRequest := &x509.CertificateRequest{
SignatureAlgorithm: x509.ECDSAWithSHA256,
Subject: subject,
DNSNames: []string{},
EmailAddresses: []string{emailAddress},
IPAddresses: []net.IP{},
URIs: []*url.URL{},
ExtraExtensions: []pkix.Extension{},
}
csr, err := x509.CreateCertificateRequest(rand.Reader, certRequest, privateKey)
if err != nil {
return nil, errors.Wrap(err, "create certificate signing request")
}
return csr, nil
}
func printCertificate(certificate *x509.Certificate) {
bytes := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certificate.Raw,
})
fmt.Println(string(bytes))
}
func printCsr(csr []byte) {
csrBytes := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csr,
})
fmt.Println(string(csrBytes))
}