forked from go-piv/go-ykpiv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpivman.go
186 lines (166 loc) · 5.86 KB
/
pivman.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
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2017
//
// 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
/*
#include <ykpiv.h>
#include <stdlib.h>
*/
import "C"
import (
"crypto"
"encoding/asn1"
"golang.org/x/crypto/pbkdf2"
"github.com/t33m/go-ykpiv/internal/bytearray"
)
var (
pivmanObjData = 0x5FFF00
pivmanProtectedData = 0x5FC109
/* pivman's source defines this as 0x80, but since we're using an actual
* der decoder, we'll see the tag value, which would just be 1 */
pivmanTagFlags1 = 0x01
pivmanTagSalt = 0x02
pivmanTagTimestamp = 0x03
pivmanTagFlags1PUKBlocked = 0x01
)
// Get the salt off the Yubikey PIV token, which is stored in a DER encoded
// array of arrays. This salt is a couple of bytes of calming entropy.
func (y Yubikey) GetSalt() ([]byte, error) {
attributes, err := y.getPIVMANAttributes(pivmanObjData)
if err != nil {
return nil, err
}
return attributes[pivmanTagSalt], nil
}
func (y Yubikey) SetSalt(salt []byte) (err error) {
attributes, err := y.getPIVMANAttributes(pivmanObjData)
if err != nil {
// TODO: handle APDUError if PIVMANAttributes is not yet set
attributes = map[int][]byte{}
}
attributes[pivmanTagSalt] = salt
values := make([]asn1.RawValue, len(attributes))
values = append(values, asn1.RawValue{Tag: pivmanTagSalt, IsCompound: false, Class: 0x01, Bytes: salt})
if _, ok := attributes[pivmanTagFlags1]; !ok {
values = append(values, asn1.RawValue{Tag: pivmanTagFlags1, IsCompound: false, Class: 0x01, Bytes: []byte{0}})
}
if _, ok := attributes[pivmanTagTimestamp]; !ok {
values = append(values,
asn1.RawValue{Tag: pivmanTagTimestamp, IsCompound: false, Class: 0x01, Bytes: []byte{0, 0, 0, 0}},
)
}
byteArray, err := bytearray.Encode(values)
if err != nil {
return
}
if byteArray, err = bytearray.Encode(
[]asn1.RawValue{{Tag: 0x80, IsCompound: true, Class: 0x01, Bytes: byteArray}},
); err != nil {
return
}
return y.SaveObject(int32(pivmanObjData), byteArray)
}
// Compute the PIVMAN Management Key using 10000 rounds of PBKDF2 SHA1
// utilizing the salt off the Yubikey to derive the 3DES management key.
func (y Yubikey) DeriveManagementKey() ([]byte, error) {
// Description of the Management key derivation can be found on the
// Yubikey website:
// https://developers.yubico.com/yubikey-piv-manager/PIN_and_Management_Key.html
//
// Technical description of Key derivation from PIN
//
// When choosing to use a Management Key derived from the PIN, the following takes place:
//
// A random 8-byte SALT value is generated and stored on the YubiKey.
//
// The derived Management Key is calculated as PBKDF2(PIN, SALT, 24, 10000).
//
// The PBKDF2 function (described in RFC 2898) is run using the PIN
// (encoded using UTF-8) as the password, for 10000 rounds, to produce a 24
// byte key, which is used as the management key. Whenever the user changes
// the PIN this process is repeated, using a new SALT and the new PIN.
pin, err := y.Options.GetPIN()
if err != nil {
return nil, err
}
salt, err := y.GetSalt()
if err != nil {
return nil, err
}
return pbkdf2.Key([]byte(pin), salt, 10000, 24, crypto.SHA1.New), nil
}
func (y Yubikey) SetProtectedMGMKey(key []byte) (err error) {
if err = y.SetMGMKey(key); err != nil {
return
}
byteArray, err := bytearray.Encode(
[]asn1.RawValue{asn1.RawValue{Tag: pivmanTagFlags1, IsCompound: false, Class: 0x02, Bytes: []byte{2}}},
)
if err != nil {
return
}
if byteArray, err = bytearray.Encode(
[]asn1.RawValue{{Tag: 0x00, IsCompound: false, Class: 0x02, Bytes: byteArray}},
); err != nil {
return
}
if err = y.SaveObject(int32(pivmanObjData), byteArray); err != nil {
return
}
byteArray, err = bytearray.Encode([]asn1.RawValue{{Tag: 0x09, IsCompound: false, Class: 0x02, Bytes: key}})
if err != nil {
return
}
if byteArray, err = bytearray.Encode(
[]asn1.RawValue{{Tag: 0x08, IsCompound: false, Class: 0x02, Bytes: byteArray}},
); err != nil {
return
}
return y.SaveObject(int32(pivmanProtectedData), byteArray)
}
func (y Yubikey) GetProtectedMGMKey() ([]byte, error) {
attributes, err := y.getPIVMANAttributes(pivmanObjData)
if err != nil {
return nil, err
}
attributes, err = y.getPIVMANAttributes(pivmanProtectedData)
if err != nil {
return nil, err
}
return attributes[0x09], nil
}
// Return a mapping of pivmanTags -> byte arrays. The exact semantics
// of this byte array is defined entirely by the tag, and should be treated
// as semantically opaque to the user, unless specific parsing code is in place.
func (y Yubikey) getPIVMANAttributes(id int) (map[int][]byte, error) {
attributes := map[int][]byte{}
bytes, err := y.GetObject(id)
if err != nil {
return nil, err
}
byteArray, err := bytearray.DERDecode(bytes)
if err != nil {
return nil, err
}
for _, rawValue := range byteArray {
attributes[rawValue.Tag] = rawValue.Bytes
}
return attributes, nil
}
// vim: foldmethod=marker