Skip to content

Commit a322a29

Browse files
committed
Extracted common logic into helper function
1 parent 817a434 commit a322a29

File tree

2 files changed

+53
-73
lines changed

2 files changed

+53
-73
lines changed

YubiKit/YubiKit/PIV/PIVDataTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public enum PIV {
177177

178178
/// PIV management key type.
179179
public enum ManagementKeyType: UInt8, Sendable {
180-
/// 3-des (default)
180+
/// 3-des
181181
case tripleDES = 0x03
182182
/// AES-128
183183
case AES128 = 0x08

YubiKit/YubiKit/PIV/PIVSession.swift

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ public final actor PIVSession: Session {
276276
Logger.piv.debug("\(String(describing: self).lastComponent), \(#function)")
277277
let rsaKeyType = PIV.RSAKey.rsa(key.size)
278278
let keyType = PIV.KeyType.rsa(key.size)
279-
try await checkKeyFeatures(keyType: keyType, pinPolicy: pinPolicy, touchPolicy: touchPolicy, generateKey: false)
280279

281280
var data = Data()
282281
let primeOne = key.p
@@ -291,21 +290,13 @@ public final actor PIVSession: Session {
291290
data.append(TKBERTLVRecord(tag: 0x04, value: exponentTwo.padOrTrim(to: length)).data)
292291
data.append(TKBERTLVRecord(tag: 0x05, value: coefficient.padOrTrim(to: length)).data)
293292

294-
if pinPolicy != .`defaultPolicy` {
295-
data.append(TKBERTLVRecord(tag: tagPinPolicy, value: pinPolicy.rawValue.data).data)
296-
}
297-
if touchPolicy != .`defaultPolicy` {
298-
data.append(TKBERTLVRecord(tag: tagTouchPolicy, value: touchPolicy.rawValue.data).data)
299-
}
300-
let apdu = APDU(
301-
cla: 0,
302-
ins: insImportKey,
303-
p1: keyType.rawValue,
304-
p2: slot.rawValue,
305-
command: data,
306-
type: .extended
293+
try await importKey(
294+
keyType: keyType,
295+
keyData: data,
296+
slot: slot,
297+
pinPolicy: pinPolicy,
298+
touchPolicy: touchPolicy
307299
)
308-
try await send(apdu: apdu)
309300
return rsaKeyType
310301
}
311302

@@ -333,27 +324,14 @@ public final actor PIVSession: Session {
333324
Logger.piv.debug("\(String(describing: self).lastComponent), \(#function)")
334325
let eccKeyType = PIV.ECCKey.ecc(key.curve)
335326
let keyType = PIV.KeyType.ecc(key.curve)
336-
try await checkKeyFeatures(keyType: keyType, pinPolicy: pinPolicy, touchPolicy: touchPolicy, generateKey: false)
337327

338-
var data = Data()
339-
let privateKeyData = key.k
340-
data.append(TKBERTLVRecord(tag: 0x06, value: privateKeyData).data)
341-
342-
if pinPolicy != .`defaultPolicy` {
343-
data.append(TKBERTLVRecord(tag: tagPinPolicy, value: pinPolicy.rawValue.data).data)
344-
}
345-
if touchPolicy != .`defaultPolicy` {
346-
data.append(TKBERTLVRecord(tag: tagTouchPolicy, value: touchPolicy.rawValue.data).data)
347-
}
348-
let apdu = APDU(
349-
cla: 0,
350-
ins: insImportKey,
351-
p1: keyType.rawValue,
352-
p2: slot.rawValue,
353-
command: data,
354-
type: .extended
328+
try await importKey(
329+
keyType: keyType,
330+
keyData: TKBERTLVRecord(tag: 0x06, value: key.k).data,
331+
slot: slot,
332+
pinPolicy: pinPolicy,
333+
touchPolicy: touchPolicy
355334
)
356-
try await send(apdu: apdu)
357335
return eccKeyType
358336
}
359337

@@ -381,27 +359,14 @@ public final actor PIVSession: Session {
381359
Logger.piv.debug("\(String(describing: self).lastComponent), \(#function)")
382360
let ed25519KeyType = PIV.Ed25519Key.ed25519
383361
let keyType = PIV.KeyType.ed25519
384-
try await checkKeyFeatures(keyType: keyType, pinPolicy: pinPolicy, touchPolicy: touchPolicy, generateKey: false)
385362

386-
var data = Data()
387-
let privateKeyData = key.seed
388-
data.append(TKBERTLVRecord(tag: 0x07, value: privateKeyData).data)
389-
390-
if pinPolicy != .`defaultPolicy` {
391-
data.append(TKBERTLVRecord(tag: tagPinPolicy, value: pinPolicy.rawValue.data).data)
392-
}
393-
if touchPolicy != .`defaultPolicy` {
394-
data.append(TKBERTLVRecord(tag: tagTouchPolicy, value: touchPolicy.rawValue.data).data)
395-
}
396-
let apdu = APDU(
397-
cla: 0,
398-
ins: insImportKey,
399-
p1: keyType.rawValue,
400-
p2: slot.rawValue,
401-
command: data,
402-
type: .extended
363+
try await importKey(
364+
keyType: keyType,
365+
keyData: TKBERTLVRecord(tag: 0x07, value: key.seed).data,
366+
slot: slot,
367+
pinPolicy: pinPolicy,
368+
touchPolicy: touchPolicy
403369
)
404-
try await send(apdu: apdu)
405370
return ed25519KeyType
406371
}
407372

@@ -429,27 +394,14 @@ public final actor PIVSession: Session {
429394
Logger.piv.debug("\(String(describing: self).lastComponent), \(#function)")
430395
let x25519KeyType = PIV.X25519Key.x25519
431396
let keyType = PIV.KeyType.x25519
432-
try await checkKeyFeatures(keyType: keyType, pinPolicy: pinPolicy, touchPolicy: touchPolicy, generateKey: false)
433397

434-
var data = Data()
435-
let privateKeyData = key.scalar
436-
data.append(TKBERTLVRecord(tag: 0x08, value: privateKeyData).data)
437-
438-
if pinPolicy != .`defaultPolicy` {
439-
data.append(TKBERTLVRecord(tag: tagPinPolicy, value: pinPolicy.rawValue.data).data)
440-
}
441-
if touchPolicy != .`defaultPolicy` {
442-
data.append(TKBERTLVRecord(tag: tagTouchPolicy, value: touchPolicy.rawValue.data).data)
443-
}
444-
let apdu = APDU(
445-
cla: 0,
446-
ins: insImportKey,
447-
p1: keyType.rawValue,
448-
p2: slot.rawValue,
449-
command: data,
450-
type: .extended
398+
try await importKey(
399+
keyType: keyType,
400+
keyData: TKBERTLVRecord(tag: 0x08, value: key.scalar).data,
401+
slot: slot,
402+
pinPolicy: pinPolicy,
403+
touchPolicy: touchPolicy
451404
)
452-
try await send(apdu: apdu)
453405
return x25519KeyType
454406
}
455407

@@ -950,6 +902,34 @@ public final actor PIVSession: Session {
950902

951903
extension PIVSession {
952904

905+
private func importKey(
906+
keyType: PIV.KeyType,
907+
keyData: Data,
908+
slot: PIV.Slot,
909+
pinPolicy: PIV.PinPolicy,
910+
touchPolicy: PIV.TouchPolicy
911+
) async throws {
912+
try await checkKeyFeatures(keyType: keyType, pinPolicy: pinPolicy, touchPolicy: touchPolicy, generateKey: false)
913+
914+
var data = keyData
915+
if pinPolicy != .`defaultPolicy` {
916+
data.append(TKBERTLVRecord(tag: tagPinPolicy, value: pinPolicy.rawValue.data).data)
917+
}
918+
if touchPolicy != .`defaultPolicy` {
919+
data.append(TKBERTLVRecord(tag: tagTouchPolicy, value: touchPolicy.rawValue.data).data)
920+
}
921+
922+
let apdu = APDU(
923+
cla: 0,
924+
ins: insImportKey,
925+
p1: keyType.rawValue,
926+
p2: slot.rawValue,
927+
command: data,
928+
type: .extended
929+
)
930+
try await send(apdu: apdu)
931+
}
932+
953933
private func usePrivateKeyInSlot(
954934
slot: PIV.Slot,
955935
keyType: PIV.KeyType,

0 commit comments

Comments
 (0)