gunk-dev/the-valley

attestation.go

sourcedee3830391efmain's published tip history
7.7 KiB
1package main
2
3// A key's attestation: the evidence that its private half was generated
4// on a YubiKey, in a PIV slot, and cannot leave it. The registry carries
5// two certificates per attested key, as PEM:
6//
7// - slot: the certificate the YubiKey writes for the slot holding the
8// key (`yubico-piv-tool -a attest -s 9c`). It holds the public key and
9// says, in Yubico's extensions, which device made it and how the slot
10// may be used: whether signing needs the PIN, and whether it needs a
11// touch.
12// - f9: the YubiKey's own attestation certificate, read from slot f9
13// (`yubico-piv-tool -a read-certificate -s f9`). It signed the slot
14// certificate, and Yubico signed it at manufacture.
15//
16// The compiler, not the registry, decides which issuers count. It carries
17// Yubico Attestation Root 1 and the PIV intermediates beneath it
18// (yubico/), and no registry entry can add another. A key counts as a
19// hardware key when its slot certificate verifies, through its f9
20// certificate and those intermediates, to that root; when the key it
21// attests is the entry's public key; and when its touch policy is always.
22
23import (
24 "bytes"
25 "crypto/ed25519"
26 "crypto/x509"
27 "crypto/x509/pkix"
28 _ "embed"
29 "encoding/asn1"
30 "encoding/pem"
31 "fmt"
32 "math/big"
33 "time"
34)
35
36// The pinned certificates, from https://developers.yubico.com/PKI/.
37// attestation-root-1.pem is yubico-ca-1.pem, byte for byte: Yubico
38// Attestation Root 1, which signs the attestations of YubiKeys from
39// firmware 5.7.4 on, and whose SHA-256 fingerprint Yubico publishes and
40// the unit tests pin. piv-intermediates.pem is the five certificates of
41// yubico-intermediate.pem on a PIV path beneath it: Yubico Attestation
42// Intermediate A 1 and B 1, and Yubico PIV Attestation A 1, B 1 and B2 1.
43// The FIDO, OpenPGP, SD and YubiHSM intermediates are left out, so a
44// certificate issued under another application of the device attests no
45// PIV key.
46var (
47 //go:embed yubico/attestation-root-1.pem
48 yubicoRootPEM []byte
49 //go:embed yubico/piv-intermediates.pem
50 yubicoIntermediatesPEM []byte
51)
52
53// trust is the issuers an attestation must verify to.
54type trust struct {
55 roots *x509.CertPool
56 intermediates *x509.CertPool
57}
58
59// yubico is the trust every compilation uses. The tests replace it to
60// sign attestations of their own.
61var yubico = mustTrust(yubicoRootPEM, yubicoIntermediatesPEM)
62
63func mustTrust(roots, intermediates []byte) trust {
64 t := trust{roots: x509.NewCertPool(), intermediates: x509.NewCertPool()}
65 if !t.roots.AppendCertsFromPEM(roots) || !t.intermediates.AppendCertsFromPEM(intermediates) {
66 panic("identity: the pinned Yubico certificates do not parse")
67 }
68 return t
69}
70
71// The extensions Yubico writes into a slot certificate, under its arc
72// 1.3.6.1.4.1.41482.3 (https://developers.yubico.com/PIV/Introduction/PIV_attestation.html).
73var (
74 extFirmware = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 3}
75 extSerial = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 7}
76 extPolicy = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 8}
77)
78
79// The touch policy a slot certificate states, in the second byte of its
80// policy extension. The first byte is the PIN policy.
81const touchAlways = 2
82
83var (
84 pinPolicies = map[byte]string{1: "never", 2: "once", 3: "always"}
85 touchPolicies = map[byte]string{1: "never", touchAlways: "always", 3: "cached"}
86)
87
88// attestation is the block a key entry may carry (schema/identity.cue).
89type attestation struct {
90 Slot string `json:"slot"`
91 F9 string `json:"f9"`
92}
93
94// attested is what a verified attestation says about its key.
95type attested struct {
96 serial *big.Int
97 firmware []byte
98 pin byte
99 touch byte
100}
101
102// hardware is whether the key qualifies for the hardware approval file:
103// every signature it makes needed a touch.
104func (a attested) hardware() bool {
105 return a.touch == touchAlways
106}
107
108// String is the line the compiler reports, for the people reviewing an
109// enrollment.
110func (a attested) String() string {
111 firmware := "unstated"
112 if len(a.firmware) == 3 {
113 firmware = fmt.Sprintf("%d.%d.%d", a.firmware[0], a.firmware[1], a.firmware[2])
114 }
115 serial := "unstated"
116 if a.serial != nil {
117 serial = a.serial.String()
118 }
119 return fmt.Sprintf("YubiKey serial %s, firmware %s, PIN policy %s, touch policy %s",
120 serial, firmware, policyName(pinPolicies, a.pin), policyName(touchPolicies, a.touch))
121}
122
123func policyName(names map[byte]string, b byte) string {
124 if name, ok := names[b]; ok {
125 return name
126 }
127 return fmt.Sprintf("%#02x", b)
128}
129
130// verifyAttestation verifies a key's attestation as of one day, and
131// returns what it says. pub is the key the entry declares. Every failure
132// is an error: an attestation that is present and does not verify fails
133// the compilation, rather than leaving the key a software key.
134func verifyAttestation(a attestation, pub ed25519.PublicKey, day time.Time, t trust) (attested, error) {
135 var out attested
136 slot, err := readCertificate("slot", a.Slot)
137 if err != nil {
138 return out, err
139 }
140 f9, err := readCertificate("f9", a.F9)
141 if err != nil {
142 return out, err
143 }
144
145 // The slot certificate verifies to the pinned root, through the f9
146 // certificate and the pinned intermediates. The extended key usage is
147 // left open: these certificates attest keys and serve no TLS, and Go's
148 // default would ask for a server certificate.
149 intermediates := t.intermediates.Clone()
150 intermediates.AddCert(f9)
151 chains, err := slot.Verify(x509.VerifyOptions{
152 Roots: t.roots,
153 Intermediates: intermediates,
154 CurrentTime: day,
155 KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
156 })
157 if err != nil {
158 return out, fmt.Errorf("the attestation does not verify to Yubico Attestation Root 1: %w", err)
159 }
160 // And the f9 certificate is what signed it: the slot certificate is
161 // the device's statement, not something an intermediate issued.
162 for _, chain := range chains {
163 if len(chain) < 2 || !chain[1].Equal(f9) {
164 return out, fmt.Errorf("the slot certificate verifies without its f9 certificate")
165 }
166 }
167
168 attestedKey, ok := slot.PublicKey.(ed25519.PublicKey)
169 if !ok {
170 return out, fmt.Errorf("the slot certificate attests a %s key, not an ed25519 one", slot.PublicKeyAlgorithm)
171 }
172 if !attestedKey.Equal(pub) {
173 return out, fmt.Errorf("the slot certificate attests a different key from the entry's public key")
174 }
175
176 policy := extension(slot.Extensions, extPolicy)
177 if len(policy) != 2 {
178 return out, fmt.Errorf("the slot certificate states no PIN and touch policy")
179 }
180 out.pin, out.touch = policy[0], policy[1]
181 out.firmware = extension(slot.Extensions, extFirmware)
182 if der := extension(slot.Extensions, extSerial); der != nil {
183 var serial *big.Int
184 if rest, err := asn1.Unmarshal(der, &serial); err == nil && len(rest) == 0 {
185 out.serial = serial
186 }
187 }
188 return out, nil
189}
190
191// readCertificate reads one PEM certificate, and nothing around it.
192func readCertificate(which, text string) (*x509.Certificate, error) {
193 block, rest := pem.Decode([]byte(text))
194 switch {
195 case block == nil:
196 return nil, fmt.Errorf("the %s certificate is not PEM", which)
197 case block.Type != "CERTIFICATE" || len(block.Headers) != 0:
198 return nil, fmt.Errorf("the %s PEM block is not a plain certificate", which)
199 case !bytes.HasPrefix(bytes.TrimSpace([]byte(text)), []byte("-----BEGIN ")) || len(bytes.TrimSpace(rest)) != 0:
200 return nil, fmt.Errorf("the %s certificate holds text beside its one PEM block", which)
201 }
202 cert, err := x509.ParseCertificate(block.Bytes)
203 if err != nil {
204 return nil, fmt.Errorf("the %s certificate: %w", which, err)
205 }
206 return cert, nil
207}
208
209// extension is the value of the extension with the given identifier, or
210// nil.
211func extension(extensions []pkix.Extension, id asn1.ObjectIdentifier) []byte {
212 for _, e := range extensions {
213 if e.Id.Equal(id) {
214 return e.Value
215 }
216 }
217 return nil
218}