Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ disabled_rules:
excluded:
- Carthage
- Pods
- PlayChainHarness/.build
- PlayChain/.build

identifier_name:
allowed_symbols: ['_']
Expand Down
2 changes: 2 additions & 0 deletions PlayChain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.build/
.swiftpm/
20 changes: 20 additions & 0 deletions PlayChain/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "PlayChain",
platforms: [.macOS(.v13), .iOS(.v15), .macCatalyst(.v15)],
products: [
.library(name: "PlayChain", targets: ["PlayChain"])
],
targets: [
.target(
name: "PlayChain",
linkerSettings: [
.linkedLibrary("sqlite3"),
.linkedFramework("Security"),
.linkedFramework("CoreFoundation")
]
)
]
)
32 changes: 32 additions & 0 deletions PlayChain/Sources/PlayChain/include/pc_keychain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef PC_KEYCHAIN_H
#define PC_KEYCHAIN_H

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>

CF_ASSUME_NONNULL_BEGIN
CF_EXTERN_C_BEGIN

typedef struct {
const char *db_path; // .../PlayChain/<bundle>.v2.db, copied
CFStringRef default_group; // agrp for items added without one, retained
CFArrayRef _Nullable groups; // every group the app may name; NULL = no restriction
void (*_Nullable log)(const char *msg); // NULL = silent
} pc_config_t;

/// Sets the configuration and closes any open database; the next call reopens it. Thread-safe.
void pc_configure(const pc_config_t *cfg);

OSStatus pc_SecItemAdd(CFDictionaryRef attrs, CFTypeRef _Nullable * _Nullable result);
OSStatus pc_SecItemCopyMatching(CFDictionaryRef query, CFTypeRef _Nullable * _Nullable result);
OSStatus pc_SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attrs);
OSStatus pc_SecItemDelete(CFDictionaryRef query);
SecKeyRef _Nullable pc_SecKeyCreateRandomKey(CFDictionaryRef params,
CFErrorRef _Nullable * _Nullable error) CF_RETURNS_RETAINED;
OSStatus pc_SecKeyGeneratePair(CFDictionaryRef params, SecKeyRef _Nullable * _Nullable pub,
SecKeyRef _Nullable * _Nullable priv);

CF_EXTERN_C_END
CF_ASSUME_NONNULL_END

#endif
101 changes: 101 additions & 0 deletions PlayChain/Sources/PlayChain/pc_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef PC_INTERNAL_H
#define PC_INTERNAL_H

#include <stdbool.h>
#include <stdint.h>
#include "pc_keychain.h"

CF_ASSUME_NONNULL_BEGIN

typedef enum { PC_BLOB, PC_STR, PC_DATA, PC_NUM, PC_DATE } pc_kind; // BLOB: string or data, kept as given
enum {
PC_PRIMARY = 1 << 0, // part of the unique key
PC_DEFAULT = 1 << 1, // filled on add when missing ("" / 0 / empty data / epoch by kind)
};
typedef struct {
CFStringRef name;
pc_kind kind;
uint32_t flags;
} pc_attr;
typedef struct {
char tag[5]; // "genp", also the class name and persistent-ref prefix
CFStringRef name;
const pc_attr *attrs;
size_t count;
} pc_class;

const pc_class *_Nullable pc_class_find(CFStringRef name);
const pc_class *_Nullable pc_class_by_tag(const char *tag);
const pc_attr *_Nullable pc_class_attr(const pc_class *cls, CFStringRef name);
bool pc_class_is_password(const pc_class *cls);
/// Converts a caller value to the schema's storage type (bools → numbers for numeric attrs). Returns +1.
CFTypeRef pc_coerce(const pc_class *_Nullable cls, CFStringRef name, CFTypeRef value);
bool pc_string_int(CFStringRef text, int64_t *out);

bool pc_truthy(CFTypeRef value);

enum { PC_RET_DATA = 1, PC_RET_ATTRS = 2, PC_RET_REF = 4, PC_RET_PREF = 8 };
typedef enum { PC_USE_ADD, PC_USE_SEARCH, PC_USE_CHANGES } pc_use;
typedef struct {
const pc_class *_Nullable cls;
CFMutableDictionaryRef match; // attribute name → coerced value (excludes sync)
unsigned ret;
CFIndex limit; // 1 = One (default), -1 = All, n
int sync; // 0, 1, or -1 = any (syna); default 0
bool sync_given;
int64_t rowid; // from v_PersistentRef, 0 = none
CFDataRef _Nullable value_data;
CFTypeRef _Nullable value_ref;
} pc_query;

OSStatus pc_query_parse(CFDictionaryRef dict, pc_use use, pc_query *q);
void pc_query_free(pc_query *q);

typedef struct {
int64_t rowid;
CFMutableDictionaryRef attrs; // every attribute except v_Data
CFDataRef _Nullable data;
} pc_row;
typedef struct {
pc_row *_Nullable rows;
size_t count;
} pc_rows;

OSStatus pc_store_open(const char *path);
void pc_store_close(void);
OSStatus pc_store_begin(void);
OSStatus pc_store_commit(void);
void pc_store_rollback(void);
/// Loads rows of `cls` in rowid order; `rowid` > 0 loads just that row.
OSStatus pc_store_load(const pc_class *cls, int64_t rowid, pc_rows *out);
OSStatus pc_store_insert(const pc_class *cls, CFDictionaryRef attrs, CFDataRef _Nullable data, int64_t *rowid);
OSStatus pc_store_update(const pc_class *cls, int64_t rowid, CFDictionaryRef attrs, CFDataRef _Nullable data);
OSStatus pc_store_delete(int64_t rowid);
const pc_class *_Nullable pc_store_class_of(int64_t rowid);
void pc_rows_free(pc_rows *rows);

OSStatus pc_item_add(pc_query *q, CFTypeRef _Nullable *_Nullable result);
OSStatus pc_item_copy(pc_query *q, CFTypeRef _Nullable *_Nullable result);
OSStatus pc_item_update(pc_query *q, pc_query *changes);
OSStatus pc_item_delete(pc_query *q);
/// Stamps sha1 from the row's attributes and data, as securityd does on every write.
void pc_item_stamp_sha1(CFMutableDictionaryRef attrs, CFDataRef _Nullable data);

CFDataRef pc_persistent_ref(const pc_class *cls, int64_t rowid);
bool pc_persistent_ref_parse(CFTypeRef value, char *tag, int64_t *rowid); // tag: 5 bytes
/// Builds the SecItem result for already-limited rows. `for_add` uses SecItemAdd's attribute set.
OSStatus pc_result_build(const pc_class *cls, pc_row *rows, size_t count, unsigned ret, CFIndex limit,
bool for_add, CFTypeRef _Nullable *_Nullable result);

/// Fills a key add's attributes and data from the key itself (v_Ref, or v_Data + type/kcls). Every way a key
/// enters the keychain passes here, so a stored key always has its class, type, sizes and klbl.
OSStatus pc_key_prepare_add(pc_query *q);
SecKeyRef _Nullable pc_key_from_row(const pc_row *row) CF_RETURNS_RETAINED;

CFStringRef pc_default_group(void);
CFArrayRef _Nullable pc_groups(void);
void pc_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));

CF_ASSUME_NONNULL_END

#endif
Loading
Loading