-
Notifications
You must be signed in to change notification settings - Fork 1
/
pin_protected_test.go
156 lines (127 loc) · 3.64 KB
/
pin_protected_test.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
// SPDX-FileCopyrightText: 2020 Google LLC
// SPDX-License-Identifier: Apache-2.0
package piv
import (
"encoding/hex"
"testing"
"cunicu.li/go-iso7816/encoding/tlv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPinProtected(t *testing.T) {
withCard(t, true, true, nil, func(t *testing.T, c *Card) {
_, err := c.PinProtectedData(DefaultPIN)
assert.ErrorIs(t, err, ErrNotFound, "A card should return no PPD after reset")
wantKey := ManagementKey{
0x09, 0xd9, 0x87, 0x81, 0xfb, 0xdc, 0xc9, 0xb6,
0x91, 0xa2, 0x05, 0x80, 0x6e, 0xc0, 0xba, 0x84,
0x31, 0xac, 0x0d, 0x9f, 0x59, 0xa5, 0x00, 0xad,
}
ppd := &PinProtectedData{}
err = ppd.SetManagementKey(wantKey)
require.NoError(t, err)
err = c.SetPinProtectedData(DefaultManagementKey, ppd)
require.NoError(t, err, "Failed to set metadata")
got, err := c.PinProtectedData(DefaultPIN)
require.NoError(t, err, "Failed to get PIN protected data")
gotKey, err := got.ManagementKey()
require.NoError(t, err)
require.Equal(t, wantKey, gotKey, "Wanted management key=0x%x, got=0x%x", wantKey, gotKey)
})
}
func TestPinProtectedUnmarshal(t *testing.T) {
data, _ := hex.DecodeString("881a891809d98781fbdcc9b691a205806ec0ba8431ac0d9f59a500ad")
wantKey := ManagementKey{
0x09, 0xd9, 0x87, 0x81, 0xfb, 0xdc, 0xc9, 0xb6,
0x91, 0xa2, 0x05, 0x80, 0x6e, 0xc0, 0xba, 0x84,
0x31, 0xac, 0x0d, 0x9f, 0x59, 0xa5, 0x00, 0xad,
}
tvs, err := tlv.DecodeBER(data)
require.NoError(t, err)
ppd := PinProtectedData{tvs}
gotKey, err := ppd.ManagementKey()
require.NoError(t, err)
assert.Equal(t, wantKey, gotKey)
}
func TestPinProtectedMarshal(t *testing.T) {
key := ManagementKey{
0x09, 0xd9, 0x87, 0x81, 0xfb, 0xdc, 0xc9, 0xb6,
0x91, 0xa2, 0x05, 0x80, 0x6e, 0xc0, 0xba, 0x84,
0x31, 0xac, 0x0d, 0x9f, 0x59, 0xa5, 0x00, 0xad,
}
want := append([]byte{
0x88,
26,
0x89,
24,
}, key[:]...)
ppd := PinProtectedData{}
err := ppd.SetManagementKey(key)
require.NoError(t, err)
got, err := tlv.EncodeBER(ppd.TagValues...)
require.NoError(t, err, "Failed to marshal key")
assert.Equal(t, want, got)
}
func TestPinProtectedUpdate(t *testing.T) {
key := ManagementKey{
0x09, 0xd9, 0x87, 0x81, 0xfb, 0xdc, 0xc9, 0xb6,
0x91, 0xa2, 0x05, 0x80, 0x6e, 0xc0, 0xba, 0x84,
0x31, 0xac, 0x0d, 0x9f, 0x59, 0xa5, 0x00, 0xad,
}
want := append([]byte{
0x88,
26,
0x89,
24,
}, key[:]...)
ppd := PinProtectedData{}
err := ppd.SetManagementKey(DefaultManagementKey)
require.NoError(t, err)
k1, err := ppd.ManagementKey()
require.NoError(t, err)
require.Equal(t, DefaultManagementKey, k1)
err = ppd.SetManagementKey(key)
require.NoError(t, err)
k2, err := ppd.ManagementKey()
require.NoError(t, err)
require.Equal(t, key, k2)
got, err := tlv.EncodeBER(ppd.TagValues...)
require.NoError(t, err)
require.Equal(t, want, got)
}
// func TestPinProtectedAdditionalFields(t *testing.T) {
// key := ManagementKey{
// 0x09, 0xd9, 0x87, 0x81, 0xfb, 0xdc, 0xc9, 0xb6,
// 0x91, 0xa2, 0x05, 0x80, 0x6e, 0xc0, 0xba, 0x84,
// 0x31, 0xac, 0x0d, 0x9f, 0x59, 0xa5, 0x00, 0xad,
// }
// raw := []byte{
// 0x88,
// 4,
// // Unrecognized sub-object. The key should be added, but this object
// // shouldn't be impacted.
// 0x87,
// 2,
// 0x00,
// 0x01,
// }
// want := append([]byte{
// 0x88,
// 30,
// // Unrecognized sub-object.
// 0x87,
// 2,
// 0x00,
// 0x01,
// // Added management key.
// 0x89,
// 24,
// }, key[:]...)
// m := PinProtectedData{
// ManagementKey: &key,
// raw: raw,
// }
// got, err := m.marshal()
// require.NoError(t, err, "Failed to marshal updated metadata")
// assert.Equal(t, want, got)
// }