From b48e80aa271c1670b930ecce51238d5e980da2d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tama=CC=81s=20Lustyik?= Date: Sun, 13 May 2018 15:36:56 +0200 Subject: [PATCH 1/5] Minor naming improvement --- generate_appcast/DSASignature.swift | 6 +++--- generate_appcast/main.swift | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/generate_appcast/DSASignature.swift b/generate_appcast/DSASignature.swift index eccd1e18..2a9e37bf 100644 --- a/generate_appcast/DSASignature.swift +++ b/generate_appcast/DSASignature.swift @@ -5,8 +5,8 @@ import Foundation -func loadPrivateKey(privateKeyPath: URL) throws -> SecKey { - let data = try Data(contentsOf: privateKeyPath); +func loadPrivateKey(at privateKeyURL: URL) throws -> SecKey { + let data = try Data(contentsOf: privateKeyURL); var cfitems: CFArray? = nil; var format = SecExternalFormat.formatOpenSSL; @@ -14,7 +14,7 @@ func loadPrivateKey(privateKeyPath: URL) throws -> SecKey { let status = SecItemImport(data as CFData, nil, &format, &type, SecItemImportExportFlags(rawValue: UInt32(0)), nil, nil, &cfitems); if (status != errSecSuccess || cfitems == nil) { - print("Private DSA key file", privateKeyPath, "exists, but it could not be read. SecItemImport error", status); + print("Private DSA key file", privateKeyURL.path, "exists, but it could not be read. SecItemImport error", status); throw NSError(domain: SUSparkleErrorDomain, code: Int(OSStatus(SUError.signatureError.rawValue)), userInfo: nil); } diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index 58b12a96..4ab62d1b 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -22,11 +22,11 @@ func main() { exit(1); } - let privateKeyPath = URL(fileURLWithPath: args[1]); + let privateKeyURL = URL(fileURLWithPath: args[1]); let archivesSourceDir = URL(fileURLWithPath: args[2], isDirectory:true); do { - let privateKey = try loadPrivateKey(privateKeyPath: privateKeyPath); + let privateKey = try loadPrivateKey(at: privateKeyURL); do { let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, privateKey: privateKey, verbose:verbose); @@ -40,7 +40,7 @@ func main() { exit(1); } } catch { - print("Unable to load DSA private key from", privateKeyPath.path, "\n", error); + print("Unable to load DSA private key from", privateKeyURL.path, "\n", error); exit(1); } } From a418d8fe646823b254284a108fc4d1165ffb521a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tama=CC=81s=20Lustyik?= Date: Sun, 13 May 2018 16:04:48 +0200 Subject: [PATCH 2/5] Function to load the private key directly from the keychain --- generate_appcast/DSASignature.swift | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/generate_appcast/DSASignature.swift b/generate_appcast/DSASignature.swift index 2a9e37bf..7040d54c 100644 --- a/generate_appcast/DSASignature.swift +++ b/generate_appcast/DSASignature.swift @@ -25,6 +25,30 @@ func loadPrivateKey(at privateKeyURL: URL) throws -> SecKey { return (cfitems! as NSArray)[0] as! SecKey; } +func loadPrivateKey(named keyName: String, fromKeychainAt keychainURL: URL) throws -> SecKey { + var keychain: SecKeychain? = nil + + guard SecKeychainOpen(keychainURL.path, &keychain) == errSecSuccess, keychain != nil else { + throw NSError(domain: SUSparkleErrorDomain, code: Int(OSStatus(SUError.signatureError.rawValue)), userInfo: nil) + } + + let query: [CFString: CFTypeRef] = [ + kSecClass: kSecClassKey, + kSecAttrKeyClass: kSecAttrKeyClassPrivate, + kSecAttrLabel: keyName as CFString, + kSecMatchLimit: kSecMatchLimitOne, + kSecUseKeychain: keychain!, + kSecReturnRef: kCFBooleanTrue + ] + + var item: CFTypeRef? = nil + guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess, item != nil else { + throw NSError(domain: SUSparkleErrorDomain, code: Int(OSStatus(SUError.signatureError.rawValue)), userInfo: nil) + } + + return item! as! SecKey +} + func dsaSignature(path: URL, privateKey: SecKey) throws -> String { var error: Unmanaged?; From 886e4b2c8145b8f02549748e265fb7850c74c9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tama=CC=81s=20Lustyik?= Date: Sun, 13 May 2018 16:28:05 +0200 Subject: [PATCH 3/5] More complex argument parsing to support both inputs --- generate_appcast/main.swift | 92 +++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index 4ab62d1b..56178956 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -10,37 +10,81 @@ import Foundation var verbose = false; +func printUsage() { + let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent; + print("Generate appcast from a directory of Sparkle updates\n", + "Usage: \(command) < -f private key path | -k keychain_path -n key_name > \n", + " e.g. \(command) -f dsa_priv.pem archives/\n", + " Appcast files and deltas will be written to the archives directory.\n", + " Note that pkg-based updates are not supported.\n" + ) +} + func main() { let args = CommandLine.arguments; - if args.count < 3 { - let command = URL(fileURLWithPath: args[0]).lastPathComponent; - print("Generate appcast from a directory of Sparkle updates\nUsage: \(command) \n", - " e.g. \(command) dsa_priv.pem archives/\n", - " Appcast files and deltas will be written to the archives directory.\n", - " Note that pkg-based updates are not supported.\n" - ) - exit(1); - } - let privateKeyURL = URL(fileURLWithPath: args[1]); - let archivesSourceDir = URL(fileURLWithPath: args[2], isDirectory:true); + let firstOption = args[1] + let privateKey: SecKey + + if firstOption == "-f" && args.count == 4 { + // private key specified by filename + let privateKeyURL = URL(fileURLWithPath: args[2]) + + do { + privateKey = try loadPrivateKey(at: privateKeyURL) + } catch { + print("Unable to load DSA private key from", privateKeyURL.path, "\n", error) + exit(1) + } + } + else if (firstOption == "-n" || firstOption == "-k") && args.count == 6 { + // private key specified by keychain + key name + let keyName: String + let keychainURL: URL + + if firstOption == "-n" { + if args[3] != "-k" { + printUsage() + exit(1) + } + + keyName = args[2] + keychainURL = URL(fileURLWithPath: args[4]) + } + else { + if args[3] != "-n" { + printUsage() + exit(1) + } + + keyName = args[4] + keychainURL = URL(fileURLWithPath: args[2]) + } + + do { + privateKey = try loadPrivateKey(named: keyName, fromKeychainAt: keychainURL) + } catch { + print("Unable to load DSA private key '\(keyName)' from keychain at", keychainURL.path, "\n", error) + exit(1) + } + } + else { + printUsage() + exit(1) + } + + let archivesSourceDir = URL(fileURLWithPath: args.last!, isDirectory: true) do { - let privateKey = try loadPrivateKey(at: privateKeyURL); - do { - let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, privateKey: privateKey, verbose:verbose); - - for (appcastFile, updates) in allUpdates { - let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir); - try writeAppcast(appcastDestPath:appcastDestPath, updates:updates); - print("Written", appcastDestPath.path, "based on", updates.count, "updates"); - } - } catch { - print("Error generating appcast from directory", archivesSourceDir.path, "\n", error); - exit(1); + let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, privateKey: privateKey, verbose:verbose); + + for (appcastFile, updates) in allUpdates { + let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir); + try writeAppcast(appcastDestPath:appcastDestPath, updates:updates); + print("Written", appcastDestPath.path, "based on", updates.count, "updates"); } } catch { - print("Unable to load DSA private key from", privateKeyURL.path, "\n", error); + print("Error generating appcast from directory", archivesSourceDir.path, "\n", error); exit(1); } } From 3f3ace055d6bf8aaa4b0f20d5f4203204808caf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tama=CC=81s=20Lustyik?= Date: Sun, 13 May 2018 17:28:29 +0200 Subject: [PATCH 4/5] Fixed index out of bounds --- generate_appcast/main.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index 56178956..ff0df099 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -22,7 +22,11 @@ func printUsage() { func main() { let args = CommandLine.arguments; - + if args.count < 4 { + printUsage() + exit(1) + } + let firstOption = args[1] let privateKey: SecKey From a582d46096816ec8a5bbc87ca705355bbcba9876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tama=CC=81s=20Lustyik?= Date: Sun, 13 May 2018 21:33:03 +0200 Subject: [PATCH 5/5] Restored legacy CLI for backward compatibility --- generate_appcast/main.swift | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index ff0df099..57ae99f2 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -13,8 +13,13 @@ var verbose = false; func printUsage() { let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent; print("Generate appcast from a directory of Sparkle updates\n", - "Usage: \(command) < -f private key path | -k keychain_path -n key_name > \n", - " e.g. \(command) -f dsa_priv.pem archives/\n", + "Usage:\n", + " \(command) < -f private key path | -k keychain_path -n key_name > \n", + " e.g. \(command) -k ~/Library/Keychains/login.keychain -n 'My Private Key' archives/\n", + "\nOR (legacy)\n\n", + " \(command) \n", + " e.g. \(command) dsa_priv.pem archives/\n", + "\n", " Appcast files and deltas will be written to the archives directory.\n", " Note that pkg-based updates are not supported.\n" ) @@ -22,17 +27,16 @@ func printUsage() { func main() { let args = CommandLine.arguments; - if args.count < 4 { + if args.count < 3 { printUsage() exit(1) } - let firstOption = args[1] let privateKey: SecKey - if firstOption == "-f" && args.count == 4 { + if args.count == 3 || (args.count == 4 && args[1] == "-f") { // private key specified by filename - let privateKeyURL = URL(fileURLWithPath: args[2]) + let privateKeyURL = URL(fileURLWithPath: args.count == 3 ? args[1] : args[2]) do { privateKey = try loadPrivateKey(at: privateKeyURL) @@ -41,12 +45,12 @@ func main() { exit(1) } } - else if (firstOption == "-n" || firstOption == "-k") && args.count == 6 { + else if args.count == 6 && (args[1] == "-n" || args[1] == "-k") { // private key specified by keychain + key name let keyName: String let keychainURL: URL - if firstOption == "-n" { + if args[1] == "-n" { if args[3] != "-k" { printUsage() exit(1)