-
Notifications
You must be signed in to change notification settings - Fork 26
/
attestation.go
197 lines (177 loc) · 7.23 KB
/
attestation.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2019
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
package ykpiv
import (
"bytes"
"crypto/x509"
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fqyi4+JE014cSgR57Jcu3dZiehB6UtAPgad9L5cNvua/IWRmm+ANy3O2LH++Pyl8
SREzU8onbBsjMg9QDiSf5oJLKvd/Ren+zGY7
-----END CERTIFICATE-----
`),
}
var (
// OIDs of some Yubikey specific fields. These are present on the forms
// of a Yubikey Attestion Certificate.
oidFirmwareVersion = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 3}
oidSerialNumber = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 7}
oidPolicy = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 8}
// This is not implemented; I don't have hardware that returns a Certificate
// that has this Extension. This can be implemented once we get an example.
oidFormFactor = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 9}
)
// Verify that the attestation certificate is correctly signed by the roots in `options`
//
// Verify that Attested Certificate is signed by that Attestation Certificate
//
// The `attestationCert` is the Certificate from the Yubikey Attestation slot,
// signed by the provided roots. It should not be a CA Certificate in most cases.
//
// The `attestedCert` is the Certificate signed by the Attestation slot asserting
// that the public key was generated on-chip.
//
// The `options` is the set of roots and verification assertions to use when checking
// the `attestationCert` and `attestedCert`.
func VerifyAttestationWithOptions(attestationCert, attestedCert *x509.Certificate, options x509.VerifyOptions) (verifiedChains [][]*x509.Certificate, err error) {
var attestationChains [][]*x509.Certificate
// Verify Attestation Cert against using the verify options
if attestationChains, err = attestationCert.Verify(options); err != nil {
return
}
// Initialize intermediate chains if necessary
if options.Intermediates == nil {
options.Intermediates = x509.NewCertPool()
}
if attestationCert.IsCA {
// Add Attestation Certificate to Trust Store
options.Intermediates.AddCert(attestationCert)
verifiedChains, err = attestedCert.Verify(options)
} else {
// Note we cannot use CheckSignatureFrom because the parent is not a CA
err = attestationCert.CheckSignature(attestedCert.SignatureAlgorithm,
attestedCert.RawTBSCertificate,
attestedCert.Signature)
// Build chains from the verified attestationChains extending each with
// the attested certificate
if err == nil {
for i := 0; i < len(attestationChains); i++ {
attestationChains[i] = append([]*x509.Certificate{attestedCert}, attestationChains[i]...)
}
verifiedChains = attestationChains
}
}
return
}
// Verify that the attestation certificate is correctly signed by the Yubikey
// root (provided with this package), as well as verifying that the Attestation
// Certificate is signed by that slot correctly.
//
// The `attestationCert` is the Certificate from the Yubikey Attestation slot, signed by
// the Yubico roots. It is not a CA certificate from Yubico.
//
// The `attestedCert` is the Certificate signed by the Attestation slot asserting
// that the public key was generated on-chip.
func VerifyAttestation(attestationCert, attestedCert *x509.Certificate) ([][]*x509.Certificate, error) {
options := x509.VerifyOptions{
Roots: x509.NewCertPool(),
Intermediates: x509.NewCertPool(),
}
for _, yubicoPivAttestationCA := range yubicoPivAttestationCAs {
block, _ := pem.Decode(yubicoPivAttestationCA)
caCert, err := x509.ParseCertificate(block.Bytes)
if err != nil || !caCert.IsCA {
return nil, fmt.Errorf("ykpiv: INTERNAL ERROR: Attestation Root PEM is wrong!")
}
if bytes.Equal(caCert.RawIssuer, caCert.RawSubject) {
options.Roots.AddCert(caCert)
} else {
options.Intermediates.AddCert(caCert)
}
}
return VerifyAttestationWithOptions(attestationCert, attestedCert, options)
}
// Struct with an anonymous member (`x509.Certificate`) that allows you to
// both access the regular fields for ease of use, as well as handle the
// Yubikey particular fields.
type AttestionCertificate struct {
x509.Certificate
// Version of the Yubikey Firmware that generated this key.
FirmwareVersion *[3]byte
// Serial Number of the Yubikey in question.
SerialNumber *int
//
PinPolicy *byte
TouchPolicy *byte
}
// Parse the Yubikey specific Extensions from the x509.Certificate, and place
// them into a struct for use by end users.
func NewAttestionCertificate(cert *x509.Certificate) (*AttestionCertificate, error) {
aC := AttestionCertificate{Certificate: *cert}
for _, extension := range aC.Extensions {
switch {
case extension.Id.Equal(oidFirmwareVersion):
firmware := [3]byte{
extension.Value[0],
extension.Value[1],
extension.Value[2],
}
aC.FirmwareVersion = &firmware
break
case extension.Id.Equal(oidSerialNumber):
var serial int = 0
if _, err := asn1.Unmarshal(extension.Value, &serial); err != nil {
return nil, err
}
aC.SerialNumber = &serial
break
case extension.Id.Equal(oidPolicy):
var (
pin byte = extension.Value[0]
touch byte = extension.Value[1]
)
aC.PinPolicy = &pin
aC.TouchPolicy = &touch
break
}
}
return &aC, nil
}
// vim: foldmethod=marker