Skip to content

Commit

Permalink
Moved data types into seperate file for PIVSession.
Browse files Browse the repository at this point in the history
  • Loading branch information
jensutbult committed Feb 21, 2024
1 parent e3a0294 commit bc9e34a
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 188 deletions.
6 changes: 5 additions & 1 deletion YubiKit/YubiKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
B4F937682B55105B0007D394 /* PIVPaddingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4F937672B55105B0007D394 /* PIVPaddingTests.swift */; };
B4F9376F2B557E740007D394 /* SecKey+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4F9376E2B557E740007D394 /* SecKey+Extensions.swift */; };
B4F937782B593A6B0007D394 /* EncryptDecryptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4F937762B5939960007D394 /* EncryptDecryptTests.swift */; };
B4FF44A32B862BCE0070750D /* PIVDataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4FF44A22B862BCE0070750D /* PIVDataTypes.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -94,6 +95,7 @@
B4F937672B55105B0007D394 /* PIVPaddingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIVPaddingTests.swift; sourceTree = "<group>"; };
B4F9376E2B557E740007D394 /* SecKey+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SecKey+Extensions.swift"; sourceTree = "<group>"; };
B4F937762B5939960007D394 /* EncryptDecryptTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncryptDecryptTests.swift; sourceTree = "<group>"; };
B4FF44A22B862BCE0070750D /* PIVDataTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PIVDataTypes.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -208,9 +210,10 @@
isa = PBXGroup;
children = (
B4F937612B51A44E0007D394 /* PIVSession.swift */,
B4F937652B51EBAF0007D394 /* PIVPadding.swift */,
B4FF44A22B862BCE0070750D /* PIVDataTypes.swift */,
B4F9376E2B557E740007D394 /* SecKey+Extensions.swift */,
B424CF5A2B7A2A1600AC8EBF /* PIVSessionFeature.swift */,
B4F937652B51EBAF0007D394 /* PIVPadding.swift */,
);
path = PIV;
sourceTree = "<group>";
Expand Down Expand Up @@ -364,6 +367,7 @@
B401F7762B17B8DD00C541D1 /* Logger+Extensions.swift in Sources */,
B47FDD9B293A15AE00AFF70A /* NSLock+Extensions.swift in Sources */,
B456E213274D2403004471DE /* NFCConnection.swift in Sources */,
B4FF44A32B862BCE0070750D /* PIVDataTypes.swift in Sources */,
B408BA8F2948FA2100001B2F /* Stream+Extensions.swift in Sources */,
B4BE3AB3292E1E6D00CC30CB /* TKTLVRecord+Extensions.swift in Sources */,
B456E215274D2453004471DE /* LightningConnection.swift in Sources */,
Expand Down
189 changes: 189 additions & 0 deletions YubiKit/YubiKit/PIV/PIVDataTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import CommonCrypto


/// Touch policy for PIV application.
public enum PIVTouchPolicy: UInt8 {
case defaultPolicy = 0x0
case never = 0x1
case always = 0x2
case cached = 0x3
}

/// Pin policy for PIV application.
public enum PIVPinPolicy: UInt8 {
case defaultPolicy = 0x0
case never = 0x1
case once = 0x2
case always = 0x3
};

public enum PIVSlot: UInt8 {
case authentication = 0x9a
case signature = 0x9c
case keyManagement = 0x9d
case cardAuth = 0x9e
case attestation = 0xf9

var objectId: Data {
switch self {
case .authentication:
return Data([0x5f, 0xc1, 0x05])
case .signature:
return Data([0x5f, 0xc1, 0x0a])
case .keyManagement:
return Data([0x5f, 0xc1, 0x0b])
case .cardAuth:
return Data([0x5f, 0xc1, 0x01])
case .attestation:
return Data([0x5f, 0xff, 0x01])
}
}
}

public enum PIVKeyType: UInt8 {
case RSA1024 = 0x06
case RSA2048 = 0x07
case ECCP256 = 0x11
case ECCP384 = 0x14
case unknown = 0x00

public init?(_ secKey: SecKey) {
guard let dict = SecKeyCopyAttributes(secKey) else { return nil }
let attributes = dict as NSDictionary
guard let size = attributes[kSecAttrKeySizeInBits] as? Int else { return nil }
guard let type = attributes[kSecAttrKeyType] as? String else { return nil }
let secAttrKeyTypeRSA = kSecAttrKeyTypeRSA as String
let secAttrKeyTypeEC = kSecAttrKeyTypeEC as String
switch type {
case secAttrKeyTypeRSA:
switch size {
case 1024:
self = .RSA1024
case 2048:
self = .RSA2048
default:
return nil
}
case secAttrKeyTypeEC:
switch size {
case 256:
self = .ECCP256
case 384:
self = .ECCP384
default:
return nil
}
default:
return nil
}
}

var size: UInt {
switch (self) {
case .ECCP256:
return 256 / 8;
case .ECCP384:
return 384 / 8;
case .RSA1024:
return 1024 / 8;
case .RSA2048:
return 2048 / 8;
default:
return 0;
}
}
}

public enum PIVVerifyPinResult: Equatable {
case success(Int)
case fail(Int)
case pinLocked
}

public enum PIVSessionError: Error {
case invalidCipherTextLength
case unsupportedOperation
case dataParseError
case unknownKeyType
case invalidPin
case pinLocked
case invalidResponse
case authenticationFailed
case responseDataNotTLVFormatted
case failedCreatingCertificate
case badKeyLength
case invalidInput
case unsupportedKeyType
}

public struct PIVManagementKeyMetadata {

public let isDefault: Bool
public let keyType: PIVManagementKeyType
public let touchPolicy: PIVTouchPolicy
}

public struct PIVSlotMetadata {
public let keyType: PIVKeyType
public let pinPolicy: PIVPinPolicy
public let touchPolicy: PIVTouchPolicy
public let generated: Bool
public let publicKey: Data
}

public struct PIVPinPukMetadata {
public let isDefault: Bool
public let retriesTotal: Int
public let retriesRemaining: Int
}

public enum PIVManagementKeyType: UInt8 {
case tripleDES = 0x03
case AES128 = 0x08
case AES192 = 0x0a
case AES256 = 0x0c

var keyLength: Int {
switch self {
case .tripleDES, .AES192:
return 24
case .AES128:
return 16
case .AES256:
return 32
}
}

var challengeLength: Int {
switch self {
case .tripleDES:
return 8
case .AES128, .AES192, .AES256:
return 16
}
}

var ccAlgorithm: UInt32 {
switch self {
case .tripleDES:
return UInt32(kCCAlgorithm3DES)
case .AES128, .AES192, .AES256:
return UInt32(kCCAlgorithmAES)
}
}
}

0 comments on commit bc9e34a

Please sign in to comment.