为 SwiftUI 应用构建基于 LevelDB 和 AEAD 加密的客户端安全存储引擎


在开发需要处理敏感用户数据(如 API 密钥、个人笔记、认证令牌)的 SwiftUI 应用时,一个棘手的技术痛点浮出水面:如何实现一个既高性能又具备强加密保障的本地键值存储方案。苹果生态系统提供了 UserDefaults、文件系统和 CoreData 等选项。UserDefaults 绝不应用于存储敏感信息;直接操作文件系统意味着我们需要自行处理所有原子操作和性能问题;CoreData 虽然支持加密,但对于简单的键值存储场景来说,其对象图管理的复杂性显得过重。

我们的需求很简单:一个类似 Redis 的高性能键值存储,但完全运行在客户端,并且数据在磁盘上必须是加密的。LevelDB 因其轻量、高性能和 LSM-Tree 架构成为一个有吸引力的底层引擎。然而,LevelDB 本身不提供任何加密功能。单纯地对整个数据库文件进行加密是一种方案,但这存在一个致命缺陷:一旦解密密钥在应用运行时被加载到内存中,整个数据库文件就完全暴露了。如果应用崩溃或被恶意工具 dump 内存,密钥的短暂暴露就可能导致所有数据泄露。

一个更稳健的构想是实现“条目级加密”。每一对键值(key-value pair)在写入 LevelDB 之前都独立加密。这种方法的优势在于,即使攻击者获取了数据库文件,没有相应的密钥和上下文,他们也无法解密任何单个条目。这大大缩小了攻击面。我们将采用基于 AEAD (Authenticated Encryption with Associated Data) 的加密模式,特别是 AES-GCM,它不仅提供机密性(保密),还提供完整性和真实性校验,能有效防止数据被篡改。

技术选型决策

  1. 存储引擎: LevelDB

    • 原因: Google 出品,经过 Chrome 等大规模产品验证,性能可靠。其基于 LSM-Tree 的设计对写入操作极其友好,非常适合需要频繁更新少量数据的场景。它只是一个 C++ 库,没有复杂的依赖,易于集成到原生应用中。
    • 替代方案分析: SQLite 是另一个优秀的选择,但它是一个关系型数据库。对于纯粹的键值存储需求,LevelDB 的 API 更简单直接,开销也更小。
  2. 加密库: Apple CryptoKit

    • 原因: 从 iOS 13/macOS 10.15 开始,苹果提供了现代化的、Swift-native 的加密库 CryptoKit。它封装了底层 CommonCrypto 的复杂性,提供了安全的 API,尤其是对 AES-GCM 这种现代 AEAD 算法的直接支持,能有效避免开发者在使用旧有 C API 时犯下常见的密码学错误(如 Nonce 重用)。
    • 替代方案分析: 使用 CommonCrypto 或 OpenSSL。这些是强大的 C 语言库,但在 Swift 项目中使用它们需要复杂的桥接,且 API 更底层,更容易误用。例如,手动管理 CCCryptor 的上下文和内存就是一个常见的错误源。CryptoKit 极大降低了安全实现门槛。
  3. 密钥派生: PBKDF2

    • 原因: 我们不能直接将用户的密码或 PIN 作为加密密钥。必须使用密钥派生函数(KDF)从低熵的密码中生成高熵的加密密钥。PBKDF2 是一个经过时间考验的标准化算法。我们将结合一个随机生成的盐(Salt)来增加破解难度。
    • 真实项目中: 对于安全性要求更高的应用,应考虑使用 Argon2,它能更好地抵抗 GPU 加速破解。但出于演示目的和系统库的可用性,CommonCrypto 中提供的 CCKeyDerivationPBKDF 已足够说明问题。

步骤化实现:从 C++ 库到安全的 Swift 存储

1. 桥接 LevelDB 到 Swift

LevelDB 是一个 C++ 库,Swift 无法直接调用。标准做法是编写一个 C 风格的封装层,然后通过 Swift 的 C 语言互操作性来调用。

首先,你需要将 LevelDB 的源码或预编译库集成到你的 Xcode 项目中。然后,我们创建一个 C 语言的封装文件 leveldb_c_wrapper.hleveldb_c_wrapper.c

leveldb_c_wrapper.h

#ifndef leveldb_c_wrapper_h
#define leveldb_c_wrapper_h

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

// Opaque pointer to the LevelDB database instance
typedef struct leveldb_t leveldb_t;
// Opaque pointer to the LevelDB options
typedef struct leveldb_options_t leveldb_options_t;
// Opaque pointer to the LevelDB read options
typedef struct leveldb_readoptions_t leveldb_readoptions_t;
// Opaque pointer to the LevelDB write options
typedef struct leveldb_writeoptions_t leveldb_writeoptions_t;

// --- Database Operations ---
leveldb_t* leveldb_open_c(const char* path, char** errptr);
void leveldb_close_c(leveldb_t* db);

// --- Write Operations ---
// The value is copied, so the caller can free val after the call.
void leveldb_put_c(leveldb_t* db, const char* key, size_t keylen, const char* val, size_t vallen, char** errptr);

// --- Read Operations ---
// Returns a heap-allocated value that must be freed by the caller.
// Sets vallen_ptr to the length of the returned value.
char* leveldb_get_c(leveldb_t* db, const char* key, size_t keylen, size_t* vallen_ptr, char** errptr);

// --- Delete Operations ---
void leveldb_delete_c(leveldb_t* db, const char* key, size_t keylen, char** errptr);

// --- Utility ---
// Must be used to free memory allocated by leveldb_get_c or error pointers.
void leveldb_free_c(void* ptr);


#ifdef __cplusplus
}
#endif

#endif /* leveldb_c_wrapper_h */

leveldb_c_wrapper.cc (注意后缀是 .cc 以便使用 C++ Features)

#include "leveldb_c_wrapper.h"
#include "leveldb/db.h"
#include "leveldb/options.h"
#include <cstdlib>
#include <cstring>

// These C structs are just wrappers around the C++ classes
struct leveldb_t { leveldb::DB* rep; };

// Helper to copy string to char* for error reporting
static char* copy_string(const std::string& str) {
    char* result = reinterpret_cast<char*>(malloc(str.size() + 1));
    memcpy(result, str.data(), str.size());
    result[str.size()] = '\0';
    return result;
}

extern "C" {

leveldb_t* leveldb_open_c(const char* path, char** errptr) {
    leveldb::Options options;
    options.create_if_missing = true;
    leveldb::DB* db;
    leveldb::Status status = leveldb::DB::Open(options, path, &db);
    if (!status.ok()) {
        *errptr = copy_string(status.ToString());
        return nullptr;
    }
    leveldb_t* result = new leveldb_t;
    result->rep = db;
    return result;
}

void leveldb_close_c(leveldb_t* db) {
    if (db) {
        delete db->rep;
        delete db;
    }
}

void leveldb_put_c(leveldb_t* db, const char* key, size_t keylen, const char* val, size_t vallen, char** errptr) {
    leveldb::Slice key_slice(key, keylen);
    leveldb::Slice val_slice(val, vallen);
    leveldb::Status status = db->rep->Put(leveldb::WriteOptions(), key_slice, val_slice);
    if (!status.ok()) {
        *errptr = copy_string(status.ToString());
    }
}

char* leveldb_get_c(leveldb_t* db, const char* key, size_t keylen, size_t* vallen_ptr, char** errptr) {
    std::string value;
    leveldb::Slice key_slice(key, keylen);
    leveldb::Status status = db->rep->Get(leveldb::ReadOptions(), key_slice, &value);
    
    if (status.IsNotFound()) {
        *vallen_ptr = 0;
        *errptr = nullptr;
        return nullptr; // Not an error, just not found
    }
    
    if (!status.ok()) {
        *vallen_ptr = 0;
        *errptr = copy_string(status.ToString());
        return nullptr;
    }
    
    char* result = static_cast<char*>(malloc(value.size()));
    memcpy(result, value.data(), value.size());
    *vallen_ptr = value.size();
    return result;
}

void leveldb_delete_c(leveldb_t* db, const char* key, size_t keylen, char** errptr) {
    leveldb::Slice key_slice(key, keylen);
    leveldb::Status status = db->rep->Delete(leveldb::WriteOptions(), key_slice);
    if (!status.ok()) {
        *errptr = copy_string(status.ToString());
    }
}

void leveldb_free_c(void* ptr) {
    free(ptr);
}

} // extern "C"

最后,在 Xcode 的 Bridging Header 文件中导入 C 封装头文件:

// MyProject-Bridging-Header.h
#import "leveldb_c_wrapper.h"

2. 实现 Swift 端的 LevelDB 封装

现在我们可以创建一个 Swift 类来管理 LevelDB 实例,处理底层的指针和内存管理。

import Foundation

final class LevelDB {

    enum LevelDBError: Error {
        case openFailed(String)
        case writeFailed(String)
        case readFailed(String)
        case deleteFailed(String)
    }

    private var db: OpaquePointer?

    init(path: String) throws {
        var err: UnsafeMutablePointer<CChar>? = nil
        let dbPath = (path as NSString).utf8String
        self.db = leveldb_open_c(dbPath, &err)

        if let err = err {
            let errorString = String(cString: err)
            leveldb_free_c(err)
            throw LevelDBError.openFailed(errorString)
        }
    }

    deinit {
        if let db = db {
            leveldb_close_c(db)
        }
    }

    func put(key: Data, value: Data) throws {
        var err: UnsafeMutablePointer<CChar>? = nil
        key.withUnsafeBytes { keyPtr in
            value.withUnsafeBytes { valPtr in
                leveldb_put_c(
                    db,
                    keyPtr.baseAddress?.assumingMemoryBound(to: CChar.self),
                    key.count,
                    valPtr.baseAddress?.assumingMemoryBound(to: CChar.self),
                    value.count,
                    &err
                )
            }
        }

        if let err = err {
            let errorString = String(cString: err)
            leveldb_free_c(err)
            throw LevelDBError.writeFailed(errorString)
        }
    }

    func get(key: Data) throws -> Data? {
        var err: UnsafeMutablePointer<CChar>? = nil
        var valueLen: Int = 0
        
        let valuePtr: UnsafeMutablePointer<CChar>? = key.withUnsafeBytes { keyPtr in
            return leveldb_get_c(
                db,
                keyPtr.baseAddress?.assumingMemoryBound(to: CChar.self),
                key.count,
                &valueLen,
                &err
            )
        }

        if let err = err {
            let errorString = String(cString: err)
            leveldb_free_c(err)
            throw LevelDBError.readFailed(errorString)
        }
        
        guard let value = valuePtr else {
            // Not found is not an error
            return nil
        }
        
        // IMPORTANT: The C wrapper returns a malloc'd pointer, we must take ownership and free it.
        let data = Data(bytes: value, count: valueLen)
        leveldb_free_c(value)
        return data
    }

    func delete(key: Data) throws {
        var err: UnsafeMutablePointer<CChar>? = nil
        key.withUnsafeBytes { keyPtr in
            leveldb_delete_c(
                db,
                keyPtr.baseAddress?.assumingMemoryBound(to: CChar.self),
                key.count,
                &err
            )
        }

        if let err = err {
            let errorString = String(cString: err)
            leveldb_free_c(err)
            throw LevelDBError.deleteFailed(errorString)
        }
    }
}

3. 构建 AEAD 加密存储引擎

这是我们的核心逻辑。我们将创建一个 SecureStore 类,它内部持有一个 LevelDB 实例和一个加密密钥。所有的读写操作都将通过这个类,它负责加密和解密。

import Foundation
import CryptoKit
import CommonCrypto

final class SecureStore {
    
    enum StoreError: Error {
        case underlyingLevelDBError(Error)
        case encryptionFailed(String)
        case decryptionFailed(String)
        case dataSerializationError
        case dataDeserializationError
        case keyDerivationFailed
    }
    
    private let db: LevelDB
    private let encryptionKey: SymmetricKey
    
    /// A data structure to hold the payload for storage.
    /// We need to store nonce and tag along with the ciphertext.
    private struct EncryptedPayload: Codable {
        let nonce: Data
        let ciphertext: Data
        let tag: Data
    }

    /// Initializes the secure store with a user-provided password and a path.
    /// A key is derived from the password using PBKDF2.
    ///
    /// - Parameters:
    ///   - path: The file path for the LevelDB database.
    ///   - password: The user's password to derive the encryption key from.
    ///   - salt: A cryptographic salt. In a real app, this MUST be stored securely and be unique per user.
    init(path: String, password: Data, salt: Data) throws {
        do {
            self.db = try LevelDB(path: path)
        } catch {
            throw StoreError.underlyingLevelDBError(error)
        }
        
        // Derive a 256-bit (32-byte) key from the password using PBKDF2.
        // The iteration count should be high enough to be slow for attackers, but acceptable for users.
        var derivedKey = [UInt8](repeating: 0, count: 32)
        let status = CCKeyDerivationPBKDF(
            CCPBKDFAlgorithm(kCCPBKDF2),
            password.withUnsafeBytes { $0.baseAddress!.assumingMemoryBound(to: CChar.self) },
            password.count,
            salt.withUnsafeBytes { $0.baseAddress!.assumingMemoryBound(to: UInt8.self) },
            salt.count,
            CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256),
            100_000, // Iteration count
            &derivedKey,
            derivedKey.count
        )
        
        guard status == kCCSuccess else {
            throw StoreError.keyDerivationFailed
        }
        
        self.encryptionKey = SymmetricKey(data: derivedKey)
    }
    
    func put(key: String, value: String) throws {
        guard let keyData = key.data(using: .utf8), let valueData = value.data(using: .utf8) else {
            // In a real project, handle this encoding failure more gracefully.
            return
        }
        
        do {
            // Encrypt using AES-GCM from CryptoKit. This provides AEAD.
            let sealedBox = try AES.GCM.seal(valueData, using: encryptionKey)
            
            // We must store the nonce, ciphertext, and authentication tag.
            // A simple way is to serialize them into a single Data object.
            let payload = EncryptedPayload(
                nonce: Data(sealedBox.nonce),
                ciphertext: sealedBox.ciphertext,
                tag: sealedBox.tag
            )
            
            let encoder = JSONEncoder()
            let storableData = try encoder.encode(payload)
            
            try db.put(key: keyData, value: storableData)
            
        } catch let error as CryptoKitError {
            throw StoreError.encryptionFailed(error.localizedDescription)
        } catch {
            // This could be JSON encoding error or LevelDB put error.
            throw StoreError.underlyingLevelDBError(error)
        }
    }
    
    func get(key: String) throws -> String? {
        guard let keyData = key.data(using: .utf8) else { return nil }
        
        do {
            guard let storableData = try db.get(key: keyData) else {
                // Key not found in the database.
                return nil
            }
            
            let decoder = JSONDecoder()
            let payload = try decoder.decode(EncryptedPayload.self, from: storableData)
            
            // Reconstruct the sealed box for decryption.
            let nonce = try AES.GCM.Nonce(data: payload.nonce)
            let sealedBox = try AES.GCM.SealedBox(nonce: nonce, ciphertext: payload.ciphertext, tag: payload.tag)
            
            // Decrypt. This operation will fail if the key is wrong OR if the data has been tampered with.
            // This is the "Authenticated" part of AEAD.
            let decryptedData = try AES.GCM.open(sealedBox, using: encryptionKey)
            
            return String(data: decryptedData, encoding: .utf8)
            
        } catch let error where error is DecodingError {
            throw StoreError.dataDeserializationError
        } catch is CryptoKitError {
            // This is a critical security event. It means either the key is wrong or the data was tampered with.
            // The application should handle this securely, e.g., by logging an audit event and refusing to proceed.
            throw StoreError.decryptionFailed("Data authentication failed. The data may have been tampered with.")
        } catch {
            throw StoreError.underlyingLevelDBError(error)
        }
    }
    
    func delete(key: String) throws {
        guard let keyData = key.data(using: .utf8) else { return }
        try db.delete(key: keyData)
    }
}

4. 集成到 SwiftUI 视图

现在,我们可以轻松地在 SwiftUI 视图中使用这个 SecureStore

import SwiftUI

struct ContentView: View {

    @StateObject private var viewModel = ContentViewModel()

    var body: some View {
        VStack(spacing: 20) {
            Text("Secure Storage Demo")
                .font(.largeTitle)

            TextField("Storage Key", text: $viewModel.key)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            TextField("Secret Value", text: $viewModel.value)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            HStack(spacing: 15) {
                Button("Save Securely") {
                    viewModel.saveValue()
                }
                Button("Load Securely") {
                    viewModel.loadValue()
                }
            }

            if !viewModel.status.isEmpty {
                Text(viewModel.status)
                    .padding()
                    .background(viewModel.isError ? Color.red.opacity(0.2) : Color.green.opacity(0.2))
                    .cornerRadius(8)
            }
            
            Spacer()
        }
        .padding()
        .onAppear {
            viewModel.setup()
        }
    }
}

@MainActor
class ContentViewModel: ObservableObject {
    @Published var key: String = "api_token"
    @Published var value: String = ""
    @Published var status: String = ""
    @Published var isError: Bool = false
    
    private var secureStore: SecureStore?

    func setup() {
        do {
            let fileManager = FileManager.default
            let docsDir = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let dbPath = docsDir.appendingPathComponent("securedb").path
            
            // In a real app, the password would come from the user (e.g., via a login screen)
            // and the salt should be stored per-user, e.g., in the Keychain.
            // For this demo, we use hardcoded values.
            let password = "a_very_strong_password".data(using: .utf8)!
            let salt = "unique_salt_per_user".data(using: .utf8)!
            
            self.secureStore = try SecureStore(path: dbPath, password: password, salt: salt)
            status = "Secure store initialized."
            isError = false
        } catch {
            status = "Error: \(error.localizedDescription)"
            isError = true
        }
    }
    
    func saveValue() {
        guard let store = secureStore else {
            status = "Store not initialized."
            isError = true
            return
        }
        
        do {
            try store.put(key: key, value: value)
            status = "Successfully saved value for key '\(key)'."
            isError = false
        } catch {
            status = "Save error: \(error.localizedDescription)"
            isError = true
        }
    }
    
    func loadValue() {
        guard let store = secureStore else {
            status = "Store not initialized."
            isError = true
            return
        }
        
        do {
            if let loadedValue = try store.get(key: key) {
                self.value = loadedValue
                status = "Successfully loaded value for key '\(key)'."
                isError = false
            } else {
                self.value = ""
                status = "No value found for key '\(key)'."
                isError = false
            }
        } catch {
            status = "Load error: \(error.localizedDescription)"
            isError = true
        }
    }
}

架构与数据流

整个加密和解密流程可以被可视化,以更好地理解数据在系统中的转换。

sequenceDiagram
    participant SwiftUI_View as SwiftUI View
    participant SecureStore
    participant LevelDB
    participant CryptoKit

    SwiftUI_View->>SecureStore: put(key: "api", value: "secret")
    SecureStore->>CryptoKit: AES.GCM.seal("secret", using: key)
    CryptoKit-->>SecureStore: returns sealedBox (nonce, ciphertext, tag)
    SecureStore->>SecureStore: Serialize sealedBox to JSON Data
    SecureStore->>LevelDB: put(key: "api", value: jsonData)
    LevelDB-->>SecureStore: Write OK
    SecureStore-->>SwiftUI_View: Success

    %% Decryption Flow
    SwiftUI_View->>SecureStore: get(key: "api")
    SecureStore->>LevelDB: get(key: "api")
    LevelDB-->>SecureStore: returns jsonData
    SecureStore->>SecureStore: Deserialize JSON to sealedBox (nonce, ciphertext, tag)
    Note right of SecureStore: If tampered, this might fail or next step fails.
    SecureStore->>CryptoKit: AES.GCM.open(sealedBox, using: key)
    CryptoKit-->>SecureStore: returns decryptedData ("secret")
    SecureStore->>SecureStore: Convert Data to String
    SecureStore-->>SwiftUI_View: returns "secret"

遗留问题与未来迭代路径

这个实现提供了一个健壮的、条目级加密的本地存储引擎,但它并非完美。在生产环境中,还需要考虑以下几点:

  1. 密钥管理: 当前实现中,从密码派生出的 SymmetricKey 存在于 SecureStore 实例的内存中。这对于防止离线攻击(即直接分析数据库文件)是有效的,但无法抵御运行时内存分析。一个更安全的方案是利用平台的硬件安全模块,如苹果的 Secure Enclave,来生成和存储密钥,并执行加密/解密操作,这样密钥本身永远不会暴露给主处理器。

  2. Salt 的管理: 示例中 Salt 是硬编码的。在一个真实的多用户或可重装的应用中,Salt 必须是随机生成的,并与用户账户关联。一个常见的做法是,在用户首次创建密码时生成 Salt,并将其存储在相对安全的位置,如 iOS Keychain 中。

  3. 性能考量: 每次读写都涉及到一次密钥派生(在 init 中)、一次 JSON 序列化/反序列化和一次 AEAD 加密/解密。对于大量小数据的频繁读写,这个开销可能会变得显著。性能分析是必要的,可以考虑的优化包括:使用更高效的序列化格式(如 Protocol Buffers)代替 JSON,或者在应用会话期间缓存派生密钥(同时注意内存安全)。

  4. 密钥轮换: 任何密码学系统都应支持密钥轮换。当前架构没有内置此功能。实现密钥轮换需要一个复杂的迁移过程:使用旧密钥解密每个条目,然后用新密钥重新加密并写回。这需要仔细设计,以确保过程的原子性和数据在迁移过程中的完整性。


  目录