swift-lynx
swift-lynx

Reputation: 3765

How do I correctly store encryption keys on macOS so only my executable can access them?

Introduction

Basically, how/where do I persist encryption keys my executable needs?

Let me explain how my executable looks like. It's basically a Swift script that is compiled using swift build --configuration=release --product=App.

Package.swift:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "App",
    defaultLocalization: "en",
    platforms: [
        .macOS(.v10_15),
    ],
    products: [
        .executable(name: "App", targets: ["App"]),
    ],
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.1"),
        .package(url: "https://github.com/apple/swift-crypto", from: "2.0.0")
    ],
    targets: [
        .target(name: "App", dependencies: [
            .product(name: "ArgumentParser", package: "swift-argument-parser"),
            .product(name: "Crypto", package: "swift-crypto"),
        ]),
    ]
)

Some Ideas

Git seems to somehow store user credentials in Keychain. How do they do it? Is their way even secure?

Question

How can I securely store encryption keys so only my executable (edit: and the user) can access them?

Upvotes: 3

Views: 734

Answers (1)

Rob Napier
Rob Napier

Reputation: 299565

To protect your app from modification, codesign it. You can use a private key or use Apple's notarization service. This will ensure no one modifies your app or distributes an installer that tries to replace your app.

Keychain items your app creates can have access control lists, but even by default, the OS won't allow other apps to read your app's keychain items without being approved by the user. The user will receive a pop-up indicating the item the app is requesting.

So I believe your best bet is to sign your app, and store the data in Keychain. It should generally work as you want out of the box. But of course do a lot of testing. Generally these things fail-secure, so in most cases it won't leak any data to other apps. But you may get more pop-ups than you want the user to deal with if you make mistakes.

Upvotes: 1

Related Questions