gunk-dev/the-valley

attestation.go

sourceb115ae7362ffmain's published tip history
8.2 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 policies a slot certificate may state, in the two bytes of its
80// policy extension: the PIN policy first, then the touch policy. These are
81// the values Yubico specifies, and the only ones read. Any other value
82// fails the compilation, because a policy this program cannot interpret
83// says nothing about whether a signature needed a touch.
84const touchAlways = 2
85
86var (
87 pinPolicies = map[byte]string{1: "never", 2: "once", 3: "always"}
88 touchPolicies = map[byte]string{1: "never", touchAlways: "always", 3: "cached"}
89)
90
91// attestation is the block a key entry may carry (schema/identity.cue).
92type attestation struct {
93 Slot string `json:"slot"`
94 F9 string `json:"f9"`
95}
96
97// attested is what a verified attestation says about its key.
98type attested struct {
99 serial *big.Int
100 firmware []byte
101 pin byte
102 touch byte
103}
104
105// hardware is whether the key qualifies for the hardware approval file:
106// every signature it makes needed a touch.
107func (a attested) hardware() bool {
108 return a.touch == touchAlways
109}
110
111// String is the line the compiler reports, for the people reviewing an
112// enrollment.
113func (a attested) String() string {
114 firmware := "unstated"
115 if len(a.firmware) == 3 {
116 firmware = fmt.Sprintf("%d.%d.%d", a.firmware[0], a.firmware[1], a.firmware[2])
117 }
118 serial := "unstated"
119 if a.serial != nil {
120 serial = a.serial.String()
121 }
122 return fmt.Sprintf("YubiKey serial %s, firmware %s, PIN policy %s, touch policy %s",
123 serial, firmware, pinPolicies[a.pin], touchPolicies[a.touch])
124}
125
126// verifyAttestation verifies a key's attestation as of one day, and
127// returns what it says. pub is the key the entry declares. Every failure
128// is an error: an attestation that is present and does not verify fails
129// the compilation, rather than leaving the key a software key.
130func verifyAttestation(a attestation, pub ed25519.PublicKey, day time.Time, t trust) (attested, error) {
131 var out attested
132 slot, err := readCertificate("slot", a.Slot)
133 if err != nil {
134 return out, err
135 }
136 f9, err := readCertificate("f9", a.F9)
137 if err != nil {
138 return out, err
139 }
140
141 // The slot certificate verifies to the pinned root, through the f9
142 // certificate and the pinned intermediates. The extended key usage is
143 // left open: these certificates attest keys and serve no TLS, and Go's
144 // default would ask for a server certificate.
145 intermediates := t.intermediates.Clone()
146 intermediates.AddCert(f9)
147 chains, err := slot.Verify(x509.VerifyOptions{
148 Roots: t.roots,
149 Intermediates: intermediates,
150 CurrentTime: day,
151 KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
152 })
153 if err != nil {
154 return out, fmt.Errorf("the attestation does not verify to Yubico Attestation Root 1: %w", err)
155 }
156 // And the f9 certificate is what signed it: the slot certificate is
157 // the device's statement, not something an intermediate issued.
158 for _, chain := range chains {
159 if len(chain) < 2 || !chain[1].Equal(f9) {
160 return out, fmt.Errorf("the slot certificate verifies without its f9 certificate")
161 }
162 }
163
164 attestedKey, ok := slot.PublicKey.(ed25519.PublicKey)
165 if !ok {
166 return out, fmt.Errorf("the slot certificate attests a key of type %s, not Ed25519", slot.PublicKeyAlgorithm)
167 }
168 if !attestedKey.Equal(pub) {
169 return out, fmt.Errorf("the slot certificate attests a different key from the entry's public key")
170 }
171
172 policy := extension(slot.Extensions, extPolicy)
173 switch {
174 case policy == nil:
175 return out, fmt.Errorf("the slot certificate states no PIN and touch policy")
176 case len(policy) != 2:
177 return out, fmt.Errorf("the slot certificate's PIN and touch policy is %d byte(s), not 2", len(policy))
178 }
179 out.pin, out.touch = policy[0], policy[1]
180 if _, ok := pinPolicies[out.pin]; !ok {
181 return out, fmt.Errorf("the slot certificate states PIN policy 0x%02x, which Yubico does not specify", out.pin)
182 }
183 if _, ok := touchPolicies[out.touch]; !ok {
184 return out, fmt.Errorf("the slot certificate states touch policy 0x%02x, which Yubico does not specify", out.touch)
185 }
186 out.firmware = extension(slot.Extensions, extFirmware)
187 if der := extension(slot.Extensions, extSerial); der != nil {
188 var serial *big.Int
189 if rest, err := asn1.Unmarshal(der, &serial); err == nil && len(rest) == 0 {
190 out.serial = serial
191 }
192 }
193 return out, nil
194}
195
196// readCertificate reads one PEM certificate, and nothing around it.
197func readCertificate(which, text string) (*x509.Certificate, error) {
198 block, rest := pem.Decode([]byte(text))
199 switch {
200 case block == nil:
201 return nil, fmt.Errorf("the %s certificate is not PEM", which)
202 case block.Type != "CERTIFICATE" || len(block.Headers) != 0:
203 return nil, fmt.Errorf("the %s PEM block is not a plain certificate", which)
204 case !bytes.HasPrefix(bytes.TrimSpace([]byte(text)), []byte("-----BEGIN ")) || len(bytes.TrimSpace(rest)) != 0:
205 return nil, fmt.Errorf("the %s certificate holds text beside its one PEM block", which)
206 }
207 cert, err := x509.ParseCertificate(block.Bytes)
208 if err != nil {
209 return nil, fmt.Errorf("the %s certificate: %w", which, err)
210 }
211 return cert, nil
212}
213
214// extension is the value of the extension with the given identifier, or
215// nil.
216func extension(extensions []pkix.Extension, id asn1.ObjectIdentifier) []byte {
217 for _, e := range extensions {
218 if e.Id.Equal(id) {
219 return e.Value
220 }
221 }
222 return nil
223}