Skip to content

Commit

Permalink
Add key and certificate labels in ui, and slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mwielgoszewski committed Feb 28, 2021
1 parent 918fffd commit c1d15f1
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 120 deletions.
4 changes: 4 additions & 0 deletions SecureEnclaveToken.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.mwielgoszewski.SecureEnclaveToken;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -468,6 +469,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.mwielgoszewski.SecureEnclaveToken;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -488,6 +490,7 @@
"@executable_path/../Frameworks",
"@executable_path/../../../../Frameworks",
);
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.mwielgoszewski.SecureEnclaveToken.SecureEnclaveTokenExtension;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -509,6 +512,7 @@
"@executable_path/../Frameworks",
"@executable_path/../../../../Frameworks",
);
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.mwielgoszewski.SecureEnclaveToken.SecureEnclaveTokenExtension;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
28 changes: 23 additions & 5 deletions SecureEnclaveToken/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import CertificateSigningRequest

struct ContentView: View {
@State private var keysIsEmpty = false
@State private var loadButton = "Query Token Configuration"
@State private var loadButton = "Query Token"
@State private var keysLoaded = 0
@State private var certificateLabel = ""
@State private var keyLabel = ""
@State private var generateKeyDescription = ""
@State private var showDeleteConfirmation = false
@State private var commonName = ""
Expand Down Expand Up @@ -76,7 +78,6 @@ struct ContentView: View {

var body: some View {
let tag = "com.mwielgoszewski.SecureEnclaveToken.Key".data(using: .utf8)!
var keysLoaded = tokenConfig.keychainItems.count

VStack(alignment: .leading, spacing: 5) {
HStack {
Expand All @@ -90,16 +91,33 @@ struct ContentView: View {
let certificate = panel.url!.absoluteURL
_ = loadCertificateForTagIntoTokenConfig(certificatePath: certificate, tag: tag, tokenConfig: tokenConfig)
}
} else if self.loadButton == "Unload SE Keys" {
} else if self.loadButton == "Unload Token" {
tokenConfig.keychainItems.removeAll()
}
self.loadButton = tokenConfig.keychainItems.isEmpty ? "Load SE Keys" : "Unload SE Keys"
self.loadButton = tokenConfig.keychainItems.isEmpty ? "Load Token" : "Unload Token"
keysLoaded = tokenConfig.keychainItems.count

if keysLoaded > 0 {
do {
let tkcert = try tokenConfig.certificate(for: tag)
let tkkey = try tokenConfig.key(for: tag)
certificateLabel = "\(tkcert.label ?? "")"
keyLabel = "\(tkkey.label ?? "")"
} catch {
}
} else {
certificateLabel = ""
keyLabel = ""
}

}) {
Text(loadButton)
}
Text("\(keysLoaded) token keychain items loaded")
VStack(alignment: .leading) {
Text("\(keysLoaded) token keychain items loaded")
Text(certificateLabel)
Text(keyLabel)
}
}

HStack(alignment: .top) {
Expand Down
4 changes: 3 additions & 1 deletion SecureEnclaveToken/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Marcin Wielgoszewski</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
Expand Down
22 changes: 16 additions & 6 deletions SecureEnclaveToken/SecureEnclaveTokenUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import Security
import CryptoKit
import CryptoTokenKit

func generateKeyInEnclave(tag: Data, accessibility: CFString, accessControlFlags: Int) -> SecKey {
Expand Down Expand Up @@ -95,7 +96,7 @@ func deleteSecureEnclaveKey(tag: Data) -> Bool {
return status == errSecSuccess
}

func loadCertificateForTagIntoTokenConfig(certificatePath: URL, tag: Data, tokenConfig: TKToken.Configuration) -> Bool {
func loadCertificateForTagIntoTokenConfig(certificatePath: URL, tag: Data, tokenConfig: TKToken.Configuration) -> SecCertificate? {

if FileManager.default.fileExists(atPath: certificatePath.path) {
do {
Expand All @@ -122,27 +123,36 @@ func loadCertificateForTagIntoTokenConfig(certificatePath: URL, tag: Data, token
throw NSError()
}

let publicKeyHash = Insecure.SHA1.hash(data: bytes)

var commonName: CFString?
_ = SecCertificateCopyCommonName(certificate, &commonName)

let tokenCertificate = TKTokenKeychainCertificate(certificate: certificate, objectID: tag)
tokenCertificate?.label = "se certificate"
tokenCertificate?.label = "Certificate for PIV Authentication (\(commonName ?? "Secure Enclave" as CFString))"

let tokenKey = TKTokenKeychainKey(certificate: certificate, objectID: tag)
tokenKey?.label = "se key"
tokenKey?.label = "PIV AUTH key"
tokenKey?.canSign = true
tokenKey?.canPerformKeyExchange = true
tokenKey?.isSuitableForLogin = true
tokenKey?.canDecrypt = false
tokenKey?.applicationTag = tag
tokenKey?.keySizeInBits = 256
tokenKey?.keyType = kSecAttrKeyTypeECSECPrimeRandom as String
tokenKey?.publicKeyData = bytes
tokenKey?.publicKeyHash = publicKeyHash.data

tokenConfig.keychainItems.append(tokenKey!)
tokenConfig.keychainItems.append(tokenCertificate!)
return true
return certificate
} catch {
print("Failed to create cert??")
return false
}
} else {
print("Certificate is not a file")
}
return false
return nil
}

func importCertificateAndCreateSecIdentity(key: SecKey, certificatePath: URL, tag: Data) -> SecIdentity? {
Expand Down
2 changes: 1 addition & 1 deletion SecureEnclaveTokenExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
7 changes: 7 additions & 0 deletions SecureEnclaveTokenExtension/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class Token: TKSmartCardToken, TKTokenDelegate {
NSLog("Got instanceID: \(configuration.instanceID)")
super.init(smartCard: smartCard, aid: nil, instanceID: configuration.instanceID, tokenDriver: tokenDriver)
self.keychainContents?.fill(with: configuration.keychainItems)
do {
let tag = "com.mwielgoszewski.SecureEnclaveToken.Key".data(using: .utf8)!
let certificate = try self.keychainContents?.certificate(forObjectID: tag)
NSLog("Got certificate for \(String(describing: certificate?.label)) -> \(String(describing: certificate?.data.base64EncodedString()))")
} catch {
NSLog("Failed pulling certificate")
}
NSLog("Got keychain items: \(String(describing: self.keychainContents?.items.count))")
}

Expand Down
1 change: 0 additions & 1 deletion SecureEnclaveTokenExtension/TokenDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ class TokenDriver: TKSmartCardTokenDriver, TKSmartCardTokenDriverDelegate {
func tokenDriver(_ driver: TKSmartCardTokenDriver, createTokenFor smartCard: TKSmartCard, aid AID: Data?) throws -> TKSmartCardToken {
return try Token(smartCard: smartCard, aid: AID, tokenDriver: self)
}

}
Loading

0 comments on commit c1d15f1

Please sign in to comment.