21
21
namespace Yubico . YubiKey . Piv
22
22
{
23
23
[ Trait ( TraitTypes . Category , TestCategories . Simple ) ]
24
- public class ManagementKeyTests
24
+ public class ManagementKeyTests : PivSessionIntegrationTestBase
25
25
{
26
26
private readonly byte [ ] _currentKey ;
27
27
private readonly byte [ ] _newKey ;
@@ -45,28 +45,27 @@ public ManagementKeyTests(ITestOutputHelper output)
45
45
} ;
46
46
}
47
47
48
- [ Theory ]
48
+ [ SkippableTheory ( typeof ( DeviceNotFoundException ) ) ]
49
49
[ InlineData ( StandardTestDevice . Fw5 ) ]
50
50
[ InlineData ( StandardTestDevice . Fw5Fips ) ]
51
51
public void HasFeature_ReturnsCorrect ( StandardTestDevice device )
52
52
{
53
- var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( device ) ;
53
+ TestDeviceType = device ;
54
54
55
- var expectedResult = testDevice . FirmwareVersion >= new FirmwareVersion ( major : 5 , minor : 4 , patch : 2 ) ;
56
-
57
- var hasFeature = testDevice . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ;
58
-
59
- Assert . Equal ( hasFeature , expectedResult ) ;
55
+ Skip . If ( Device . FirmwareVersion < FirmwareVersion . V5_4_2 ) ;
56
+ var hasFeature = Device . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ;
57
+ Assert . True ( hasFeature ) ;
60
58
}
61
59
62
- [ Theory ]
60
+ [ SkippableTheory ( typeof ( DeviceNotFoundException ) ) ]
63
61
[ InlineData ( StandardTestDevice . Fw5 ) ]
64
62
[ InlineData ( StandardTestDevice . Fw5Fips ) ]
65
63
public void GetManagementAlgorithm_WhenReset_ReturnsCorrectType ( StandardTestDevice device )
66
64
{
67
- var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( device ) ;
68
- var shouldBeAes = testDevice . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
69
- var mustBeAes = shouldBeAes && testDevice . IsFipsSeries ;
65
+ TestDeviceType = device ;
66
+
67
+ var shouldBeAes = Device . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
68
+ var mustBeAes = shouldBeAes && Device . IsFipsSeries ;
70
69
var defaultManagementKeyType = shouldBeAes
71
70
? KeyType . AES192
72
71
: KeyType . TripleDES ;
@@ -75,67 +74,63 @@ public void GetManagementAlgorithm_WhenReset_ReturnsCorrectType(StandardTestDevi
75
74
? KeyType . AES192
76
75
: KeyType . TripleDES ;
77
76
78
- using var session = new PivSession ( testDevice ) ;
79
- session . KeyCollector = TestKeyCollectorDelegate ;
80
-
81
- session . ResetApplication ( ) ;
77
+ Session . KeyCollector = TestKeyCollectorDelegate ;
82
78
83
79
// This must throw for FIPS devices.
84
80
if ( mustBeAes )
85
81
{
86
82
Assert . Throws < InvalidOperationException > (
87
- ( ) => session . ChangeManagementKey ( PivTouchPolicy . None , KeyType . TripleDES . GetPivAlgorithm ( ) ) ) ;
83
+ ( ) => Session . ChangeManagementKey ( PivTouchPolicy . None , KeyType . TripleDES . GetPivAlgorithm ( ) ) ) ;
88
84
}
89
85
else
90
86
{
91
- session . ChangeManagementKey ( PivTouchPolicy . None , alternativeManagementKeyType . GetPivAlgorithm ( ) ) ;
92
- Assert . Equal ( alternativeManagementKeyType . GetPivAlgorithm ( ) , session . ManagementKeyAlgorithm ) ;
87
+ Session . ChangeManagementKey ( PivTouchPolicy . None , alternativeManagementKeyType . GetPivAlgorithm ( ) ) ;
88
+ Assert . Equal ( alternativeManagementKeyType . GetPivAlgorithm ( ) , Session . ManagementKeyAlgorithm ) ;
93
89
94
- session . AuthenticateManagementKey ( ) ;
95
- session . ResetApplication ( ) ;
90
+ Session . AuthenticateManagementKey ( ) ;
91
+ Session . ResetApplication ( ) ;
96
92
97
- Assert . Equal ( defaultManagementKeyType . GetPivAlgorithm ( ) , session . ManagementKeyAlgorithm ) ;
93
+ Assert . Equal ( defaultManagementKeyType . GetPivAlgorithm ( ) , Session . ManagementKeyAlgorithm ) ;
98
94
}
99
95
}
100
96
101
- [ Theory ]
97
+ [ SkippableTheory ( typeof ( DeviceNotFoundException ) ) ]
102
98
[ InlineData ( StandardTestDevice . Fw5 ) ]
103
99
[ InlineData ( StandardTestDevice . Fw5Fips ) ]
104
100
public void ChangeManagementKey_WithDefaultParameters_UsesCorrectTypeForRespectiveVersion ( StandardTestDevice device )
105
101
{
106
- var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( device ) ;
102
+ var Device = IntegrationTestDeviceEnumeration . GetTestDevice ( device ) ;
107
103
108
- var shouldBeAes = testDevice . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
109
- var mustBeAes = shouldBeAes && testDevice . IsFipsSeries ;
104
+ var shouldBeAes = Device . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
105
+ var mustBeAes = shouldBeAes && Device . IsFipsSeries ;
110
106
var defaultManagementKeyType = shouldBeAes || mustBeAes
111
107
? KeyType . AES192
112
108
: KeyType . TripleDES ;
113
109
114
- using var session = new PivSession ( testDevice ) ;
115
- session . KeyCollector = TestKeyCollectorDelegate ;
116
- session . ResetApplication ( ) ;
110
+ using var Session = new PivSession ( Device ) ;
111
+ Session . KeyCollector = TestKeyCollectorDelegate ;
117
112
118
113
// This must not throw. 5.7 FIPS requires management key to be AES192.
119
- session . ChangeManagementKey ( ) ;
120
- Assert . Equal ( defaultManagementKeyType . GetPivAlgorithm ( ) , session . ManagementKeyAlgorithm ) ;
114
+ Session . ChangeManagementKey ( ) ;
115
+ Assert . Equal ( defaultManagementKeyType . GetPivAlgorithm ( ) , Session . ManagementKeyAlgorithm ) ;
121
116
122
117
// This must throw for FIPS devices.
123
118
if ( mustBeAes )
124
119
{
125
120
Assert . Throws < InvalidOperationException > (
126
- ( ) => session . ChangeManagementKey ( PivTouchPolicy . None , KeyType . TripleDES . GetPivAlgorithm ( ) ) ) ;
121
+ ( ) => Session . ChangeManagementKey ( PivTouchPolicy . None , KeyType . TripleDES . GetPivAlgorithm ( ) ) ) ;
127
122
}
128
123
}
129
124
130
- [ Theory ]
125
+ [ SkippableTheory ( typeof ( DeviceNotFoundException ) ) ]
131
126
[ InlineData ( StandardTestDevice . Fw5 ) ]
132
127
[ InlineData ( StandardTestDevice . Fw5Fips ) ]
133
- public void RandomKey_Authenticates ( StandardTestDevice testDeviceType )
128
+ public void RandomKey_Authenticates ( StandardTestDevice DeviceType )
134
129
{
135
- var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( testDeviceType ) ;
136
130
137
- var shouldBeAes = testDevice . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
138
- var mustBeAes = shouldBeAes && testDevice . IsFipsSeries ;
131
+ TestDeviceType = DeviceType ;
132
+ var shouldBeAes = Device . FirmwareVersion >= FirmwareVersion . V5_7_0 ;
133
+ var mustBeAes = shouldBeAes && Device . IsFipsSeries ;
139
134
var defaultManagementKeyType = shouldBeAes || mustBeAes
140
135
? KeyType . AES192
141
136
: KeyType . TripleDES ;
@@ -145,44 +140,38 @@ public void RandomKey_Authenticates(StandardTestDevice testDeviceType)
145
140
for ( var index = 0 ; index < count ; index ++ )
146
141
{
147
142
GetRandomMgmtKey ( ) ;
148
- isValid = ChangeMgmtKey ( testDevice , defaultManagementKeyType ) ;
143
+ isValid = ChangeMgmtKey ( defaultManagementKeyType ) ;
149
144
if ( ! isValid )
150
145
{
151
146
break ;
152
147
}
153
148
154
- isValid = VerifyMgmtKey ( isMutual : false , testDevice ) ;
149
+ isValid = VerifyMgmtKey ( isMutual : false ) ;
155
150
if ( ! isValid )
156
151
{
157
152
break ;
158
153
}
159
154
160
- isValid = VerifyMgmtKey ( isMutual : true , testDevice ) ;
155
+ isValid = VerifyMgmtKey ( isMutual : true ) ;
161
156
if ( ! isValid )
162
157
{
163
158
break ;
164
159
}
165
160
}
166
161
167
- ResetPiv ( testDevice ) ;
168
-
169
162
Assert . True ( isValid ) ;
170
163
}
171
164
172
- private bool VerifyMgmtKey ( bool isMutual , IYubiKeyDevice testDevice )
165
+ private bool VerifyMgmtKey ( bool isMutual )
173
166
{
174
- using ( var pivSession = new PivSession ( testDevice ) )
175
- {
176
- pivSession . KeyCollector = TestKeyCollectorDelegate ;
177
- return pivSession . TryAuthenticateManagementKey ( isMutual ) ;
178
- }
167
+ Session . KeyCollector = TestKeyCollectorDelegate ;
168
+ return Session . TryAuthenticateManagementKey ( isMutual ) ;
179
169
}
180
170
181
- private bool ChangeMgmtKey ( IYubiKeyDevice testDevice , KeyType managementKeyType )
171
+ private bool ChangeMgmtKey ( KeyType managementKeyType )
182
172
{
183
- using var pivSession = new PivSession ( testDevice ) ;
184
- pivSession . KeyCollector = TestKeyCollectorDelegate ;
185
- var isChanged = pivSession . TryChangeManagementKey ( PivTouchPolicy . Default , managementKeyType . GetPivAlgorithm ( ) ) ;
173
+ Session . KeyCollector = TestKeyCollectorDelegate ;
174
+ var isChanged = Session . TryChangeManagementKey ( PivTouchPolicy . Default , managementKeyType . GetPivAlgorithm ( ) ) ;
186
175
if ( isChanged )
187
176
{
188
177
Array . Copy ( _newKey , _currentKey , length : 24 ) ;
@@ -191,10 +180,10 @@ private bool ChangeMgmtKey(IYubiKeyDevice testDevice, KeyType managementKeyType)
191
180
return isChanged ;
192
181
}
193
182
194
- private static void ResetPiv ( IYubiKeyDevice testDevice )
183
+ private static void ResetPiv ( IYubiKeyDevice Device )
195
184
{
196
- using var pivSession = new PivSession ( testDevice ) ;
197
- pivSession . ResetApplication ( ) ;
185
+ using var Session = new PivSession ( Device ) ;
186
+ Session . ResetApplication ( ) ;
198
187
}
199
188
200
189
private void GetRandomMgmtKey ( )
0 commit comments