Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import CryptoKitimport Foundationimport Security
struct KeychainFailure: Error, CustomStringConvertible { let description: String}
func query(service: String, account: String) -> [String: CFTypeRef] { var fields: [String: CFTypeRef] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service as CFString, kSecAttrAccount as String: account as CFString, ] if service == "https://sparkle-project.org" { fields[kSecAttrProtocol as String] = kSecAttrProtocolSSH } return fields}
func read(_ query: [String: CFTypeRef]) throws -> Data? { var fields = query fields[kSecReturnData as String] = kCFBooleanTrue fields[kSecMatchLimit as String] = kSecMatchLimitOne var result: CFTypeRef? let status = SecItemCopyMatching(fields as CFDictionary, &result) if status == errSecItemNotFound { return nil } guard status == errSecSuccess, let data = result as? Data else { throw KeychainFailure(description: "Could not read the keychain item (OSStatus \(status))") } return data}
func put(_ data: Data, query: [String: CFTypeRef]) throws { guard !data.isEmpty else { throw KeychainFailure(description: "Refusing to store an empty secret") } var fields = query fields[kSecValueData as String] = data as CFData let status = SecItemAdd(fields as CFDictionary, nil) guard status == errSecSuccess else { throw KeychainFailure( description: "Could not add the keychain item (OSStatus \(status)); existing secrets are never replaced") }}
func replace( _ data: Data, query: [String: CFTypeRef], update: (CFDictionary, CFDictionary) -> OSStatus = SecItemUpdate) throws { guard !data.isEmpty else { throw KeychainFailure(description: "Refusing to store an empty secret") } let changes = [kSecValueData as String: data as CFData] let status = update(query as CFDictionary, changes as CFDictionary) guard status == errSecSuccess else { throw KeychainFailure(description: "Could not replace the existing keychain item (OSStatus \(status))") }}
func updateKey(_ data: Data) throws -> Curve25519.Signing.PrivateKey { guard let decoded = Data(base64Encoded: data), decoded.count == 32 else { throw KeychainFailure(description: "Expected a base64 Ed25519 seed") } return try Curve25519.Signing.PrivateKey(rawRepresentation: decoded)}
func run() throws { let args = Array(CommandLine.arguments.dropFirst()) guard let command = args.first else { throw KeychainFailure(description: "A keychain command is required") } switch command { case "get" where args.count == 3, "put" where args.count == 3, "replace" where args.count == 3: let fields = query(service: args[1], account: args[2]) if command == "get" { guard let data = try read(fields) else { exit(44) } FileHandle.standardOutput.write(data) } else { let data = FileHandle.standardInput.readDataToEndOfFile() if args[1] == "https://sparkle-project.org" { _ = try updateKey(data) } if command == "replace" { try replace(data, query: fields) } else { try put(data, query: fields) } } case "ensure-update" where args.count == 2, "public-update" where args.count == 2: let fields = query(service: "https://sparkle-project.org", account: args[1]) let key: Curve25519.Signing.PrivateKey if let data = try read(fields) { key = try updateKey(data) } else if command == "ensure-update" { key = Curve25519.Signing.PrivateKey() try put(key.rawRepresentation.base64EncodedData(), query: fields) } else { throw KeychainFailure(description: "No update signing key exists for this account") } print(key.publicKey.rawRepresentation.base64EncodedString()) default: throw KeychainFailure( description: "Usage: launch-keychain get|put|replace SERVICE ACCOUNT | ensure-update|public-update ACCOUNT") }}
do { try run()} catch { FileHandle.standardError.write(Data("Keychain: \(error)\n".utf8)) exit(1)}