@secure-signing/react-native 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.js","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":";;AA0CA,kCAIC;AA4DD,gDAsIC;AAhPD,yCAAyF;AACzF,qCAAkE;AAClE,mCAiBiB;AAEjB,MAAM,aAAa,GAA0B,CAAC,sBAAsB,EAAE,cAAc,CAAC,CAAC;AACtF,MAAM,QAAQ,GAAkC,CAAC,oBAAoB,EAAE,qBAAqB,EAAE,iBAAiB,CAAC,CAAC;AACjH,MAAM,eAAe,GAA6B,CAAC,gBAAgB,EAAE,WAAW,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,CAAC,CAAC;AAChH,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAchC,SAAS,OAAO,CAAC,OAAe;IAC9B,MAAM,IAAI,0BAAiB,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;AAC3D,CAAC;AAED,SAAgB,WAAW,CAAC,KAAc;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,sBAAc,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7D,OAAO,CAAC,2CAA2C,CAAC,CAAC;IACvD,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,IAAY,EAAE,KAAc,EAAE,QAAiB;IACnE,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,QAAQ;QAAE,OAAO,SAAS,CAAC;IACvD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,yBAAiB,EAAE,CAAC;QAC/F,OAAO,CAAC,GAAG,IAAI,0CAA0C,yBAAiB,aAAa,CAAC,CAAC;IAC3F,CAAC;IACD,OAAO,KAAK,CAAC;A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BACT,YAAY,GAAG,KAAK,CAAC;YACvB,CAAC;QACH,CAAC;QAED,KAAK,CAAC,SAAS,CAAC,KAAK;YACnB,WAAW,CAAC,KAAK,CAAC,CAAC;YACnB,MAAM,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QACxC,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,114 @@
1
+ export declare const SIGNING_ALGORITHM: "ECDSA-P256-SHA256";
2
+ export declare const PUBLIC_KEY_ENCODING: "spki-der-base64";
3
+ export declare const SIGNATURE_ENCODING: "ecdsa-der-base64";
4
+ /** Maximum payload size in bytes. Approval payloads are small; anything larger is a bug. */
5
+ export declare const MAX_PAYLOAD_BYTES: number;
6
+ export declare const MAX_PROMPT_LENGTH = 256;
7
+ export declare const KEY_ID_PATTERN: RegExp;
8
+ export type SigningAlgorithm = typeof SIGNING_ALGORITHM;
9
+ export type PublicKeyEncoding = typeof PUBLIC_KEY_ENCODING;
10
+ export type SignatureEncoding = typeof SIGNATURE_ENCODING;
11
+ /**
12
+ * Which biometric enrollment state the key is bound to.
13
+ * - `biometry-current-set`: key is permanently invalidated if biometrics are added or removed (recommended).
14
+ * - `biometry-any`: key survives enrollment changes; any enrolled biometric can authorize.
15
+ * There is deliberately no policy without authentication.
16
+ */
17
+ export type AuthPolicy = 'biometry-current-set' | 'biometry-any';
18
+ /**
19
+ * Hardware requirement. iOS always uses the Secure Enclave (StrongBox options map to it).
20
+ * - `strongbox-required`: Android fails with STRONGBOX_UNAVAILABLE if no StrongBox.
21
+ * - `strongbox-preferred`: Android tries StrongBox, then TEE. Reported in `securityLevel`.
22
+ * - `secure-hardware`: Android TEE or better, StrongBox not requested.
23
+ */
24
+ export type HardwarePreference = 'strongbox-required' | 'strongbox-preferred' | 'secure-hardware';
25
+ export type SecurityLevel = 'secure-enclave' | 'strongbox' | 'tee' | 'software' | 'unknown';
26
+ export type CreateKeyOptions = {
27
+ keyId: string;
28
+ authPolicy?: AuthPolicy;
29
+ hardware?: HardwarePreference;
30
+ /** Server-issued, single-use challenge embedded in the Android key attestation certificate. */
31
+ attestationChallenge?: string | Uint8Array;
32
+ /**
33
+ * Allow a key that is NOT protected by secure hardware (emulators/simulators).
34
+ * The key still requires biometric authentication, and is reported as
35
+ * `hardwareBacked: false`, `securityLevel: "software"`. Never enable in production builds.
36
+ */
37
+ allowNonHardwareKey?: boolean;
38
+ };
39
+ export type KeyAttestation = {
40
+ format: 'android-key';
41
+ /** DER X.509 certificates, base64, leaf first. */
42
+ certificateChain: string[];
43
+ };
44
+ export type KeyInfo = {
45
+ keyId: string;
46
+ publicKey: string;
47
+ algorithm: SigningAlgorithm;
48
+ publicKeyEncoding: PublicKeyEncoding;
49
+ signatureEncoding: SignatureEncoding;
50
+ authPolicy: AuthPolicy;
51
+ hardwareBacked: boolean | 'unknown';
52
+ securityLevel: SecurityLevel;
53
+ /** Present only in the result of `createKey` on Android when a challenge was supplied. */
54
+ attestation: KeyAttestation | null;
55
+ };
56
+ export type SignOptions = {
57
+ keyId: string;
58
+ /** Exact bytes to sign. Strings are encoded as UTF-8 without normalization. */
59
+ payload: string | Uint8Array;
60
+ /** Shown in the biometric prompt (iOS reason / Android subtitle). Required. */
61
+ promptMessage: string;
62
+ /** Android prompt title. Defaults to promptMessage. */
63
+ promptTitle?: string;
64
+ /** Android negative button label. Defaults to "Cancel". */
65
+ cancelLabel?: string;
66
+ };
67
+ export type SignatureResult = {
68
+ keyId: string;
69
+ signature: string;
70
+ algorithm: SigningAlgorithm;
71
+ signatureEncoding: SignatureEncoding;
72
+ };
73
+ export type Capabilities = {
74
+ platform: 'ios' | 'android' | 'web' | 'unknown';
75
+ secureEnclaveAvailable: boolean;
76
+ strongBoxAvailable: boolean;
77
+ biometricAuthenticationAvailable: boolean;
78
+ biometricAuthenticationEnrolled: boolean;
79
+ deviceSecure: boolean;
80
+ };
81
+ /** Shape exchanged with the native module. Binary values cross the bridge as base64. */
82
+ export type NativeKeyInfo = {
83
+ keyId: string;
84
+ publicKey: string;
85
+ authPolicy: string;
86
+ hardwareBacked: boolean | 'unknown';
87
+ securityLevel: string;
88
+ attestationChain?: string[] | null;
89
+ };
90
+ export interface NativeSecureSigner {
91
+ isAvailable(): Promise<boolean>;
92
+ getCapabilities(): Promise<Omit<Capabilities, 'platform'> & {
93
+ platform: string;
94
+ }>;
95
+ createKey(options: {
96
+ keyId: string;
97
+ authPolicy: AuthPolicy;
98
+ hardware: HardwarePreference;
99
+ attestationChallenge: string | null;
100
+ allowNonHardwareKey: boolean;
101
+ }): Promise<NativeKeyInfo>;
102
+ getKeyInfo(keyId: string): Promise<NativeKeyInfo | null>;
103
+ hasKey(keyId: string): Promise<boolean>;
104
+ sign(options: {
105
+ keyId: string;
106
+ payload: string;
107
+ promptMessage: string;
108
+ promptTitle: string;
109
+ cancelLabel: string;
110
+ }): Promise<{
111
+ signature: string;
112
+ }>;
113
+ deleteKey(keyId: string): Promise<void>;
114
+ }
package/build/types.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KEY_ID_PATTERN = exports.MAX_PROMPT_LENGTH = exports.MAX_PAYLOAD_BYTES = exports.SIGNATURE_ENCODING = exports.PUBLIC_KEY_ENCODING = exports.SIGNING_ALGORITHM = void 0;
4
+ exports.SIGNING_ALGORITHM = 'ECDSA-P256-SHA256';
5
+ exports.PUBLIC_KEY_ENCODING = 'spki-der-base64';
6
+ exports.SIGNATURE_ENCODING = 'ecdsa-der-base64';
7
+ /** Maximum payload size in bytes. Approval payloads are small; anything larger is a bug. */
8
+ exports.MAX_PAYLOAD_BYTES = 16 * 1024;
9
+ exports.MAX_PROMPT_LENGTH = 256;
10
+ exports.KEY_ID_PATTERN = /^[A-Za-z0-9._-]{1,64}$/;
11
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAa,QAAA,iBAAiB,GAAG,mBAA4B,CAAC;AACjD,QAAA,mBAAmB,GAAG,iBAA0B,CAAC;AACjD,QAAA,kBAAkB,GAAG,kBAA2B,CAAC;AAE9D,4FAA4F;AAC/E,QAAA,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AAC9B,QAAA,iBAAiB,GAAG,GAAG,CAAC;AACxB,QAAA,cAAc,GAAG,wBAAwB,CAAC"}
@@ -0,0 +1,9 @@
1
+ {
2
+ "platforms": ["apple", "android"],
3
+ "apple": {
4
+ "modules": ["SecureSignerModule"]
5
+ },
6
+ "android": {
7
+ "modules": ["expo.modules.securesigner.SecureSignerModule"]
8
+ }
9
+ }
@@ -0,0 +1,192 @@
1
+ import CryptoKit
2
+ import Foundation
3
+ import LocalAuthentication
4
+ import Security
5
+
6
+ /// Stable error codes shared with JS (src/errors.ts). Never rename a case's raw value.
7
+ enum SignerErrorCode: String {
8
+ case notAvailable = "NOT_AVAILABLE"
9
+ case biometricsUnavailable = "BIOMETRICS_UNAVAILABLE"
10
+ case biometricsNotEnrolled = "BIOMETRICS_NOT_ENROLLED"
11
+ case passcodeNotSet = "PASSCODE_NOT_SET"
12
+ case authenticationCancelled = "AUTHENTICATION_CANCELLED"
13
+ case authenticationFailed = "AUTHENTICATION_FAILED"
14
+ case authenticationLockedOut = "AUTHENTICATION_LOCKED_OUT"
15
+ case authenticationInProgress = "AUTHENTICATION_IN_PROGRESS"
16
+ case keyNotFound = "KEY_NOT_FOUND"
17
+ case keyAlreadyExists = "KEY_ALREADY_EXISTS"
18
+ case keyInvalidated = "KEY_INVALIDATED"
19
+ case secureHardwareUnavailable = "SECURE_HARDWARE_UNAVAILABLE"
20
+ case strongBoxUnavailable = "STRONGBOX_UNAVAILABLE"
21
+ case deviceLocked = "DEVICE_LOCKED"
22
+ case invalidArgument = "INVALID_ARGUMENT"
23
+ case payloadTooLarge = "PAYLOAD_TOO_LARGE"
24
+ case signingFailed = "SIGNING_FAILED"
25
+ case unknown = "UNKNOWN"
26
+ }
27
+
28
+ struct SignerError: Error, Equatable {
29
+ let code: SignerErrorCode
30
+ let message: String
31
+ let platform: String?
32
+
33
+ init(_ code: SignerErrorCode, _ message: String, platform: String? = nil) {
34
+ self.code = code
35
+ self.message = message
36
+ self.platform = platform
37
+ }
38
+
39
+ /// Message format parsed by toSecureSignerError() in JS. Never contains payloads, keys or key IDs.
40
+ var jsMessage: String {
41
+ guard let platform = platform else { return message }
42
+ return "\(message) [platform:\(platform)]"
43
+ }
44
+ }
45
+
46
+ enum AuthPolicy: String {
47
+ case biometryCurrentSet = "biometry-current-set"
48
+ case biometryAny = "biometry-any"
49
+
50
+ var accessControlFlag: SecAccessControlCreateFlags {
51
+ switch self {
52
+ case .biometryCurrentSet: return .biometryCurrentSet
53
+ case .biometryAny: return .biometryAny
54
+ }
55
+ }
56
+ }
57
+
58
+ enum SecureSignerCore {
59
+ static let maxPayloadBytes = 16 * 1024
60
+
61
+ /// DER prefix of a P-256 SubjectPublicKeyInfo:
62
+ /// SEQUENCE { SEQUENCE { OID ecPublicKey, OID prime256v1 }, BIT STRING (0 unused bits) ... }
63
+ static let p256SpkiHeader: [UInt8] = [
64
+ 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
65
+ 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
66
+ ]
67
+
68
+ /// Converts the X9.63 uncompressed point returned by SecKeyCopyExternalRepresentation into DER SPKI.
69
+ static func spki(fromX963 raw: Data) throws -> Data {
70
+ guard raw.count == 65, raw.first == 0x04 else {
71
+ throw SignerError(.unknown, "unexpected public key representation")
72
+ }
73
+ return Data(p256SpkiHeader) + raw
74
+ }
75
+
76
+ static func isValidKeyId(_ keyId: String) -> Bool {
77
+ guard (1...64).contains(keyId.utf8.count) else { return false }
78
+ return keyId.utf8.allSatisfy { b in
79
+ (b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5A) || (b >= 0x61 && b <= 0x7A) || b == 0x2E || b == 0x5F || b == 0x2D
80
+ }
81
+ }
82
+
83
+ /// Attributes for SecKeyCreateRandomKey. Kept here (not in the module) so the
84
+ /// exact dictionary can be exercised against a real Secure Enclave in tests.
85
+ ///
86
+ /// Secure Enclave keys reject `kSecAttrCanDerive: false` with errSecParam
87
+ /// ("sepkey: drve requires '1' value when present"), so it is only set for
88
+ /// software keys. This does not widen what the key can do: the access control
89
+ /// (`.privateKeyUsage` + biometry) gates every private-key operation, and the
90
+ /// module exposes signing only.
91
+ static func keyGenerationAttributes(
92
+ tag: Data, access: SecAccessControl, secureEnclave: Bool, permanent: Bool = true
93
+ ) -> [String: Any] {
94
+ var privateAttrs: [String: Any] = [
95
+ kSecAttrIsPermanent as String: permanent,
96
+ kSecAttrApplicationTag as String: tag,
97
+ kSecAttrAccessControl as String: access,
98
+ kSecAttrCanSign as String: true,
99
+ kSecAttrCanDecrypt as String: false,
100
+ ]
101
+ var attributes: [String: Any] = [
102
+ kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
103
+ kSecAttrKeySizeInBits as String: 256,
104
+ ]
105
+ if secureEnclave {
106
+ attributes[kSecAttrTokenID as String] = kSecAttrTokenIDSecureEnclave
107
+ } else {
108
+ privateAttrs[kSecAttrCanDerive as String] = false
109
+ }
110
+ attributes[kSecPrivateKeyAttrs as String] = privateAttrs
111
+ return attributes
112
+ }
113
+
114
+ static func tagString(bundleId: String, keyId: String) -> String {
115
+ "\(bundleId).secure-signing.\(keyId)"
116
+ }
117
+
118
+ /// Hash of the biometric enrollment state. Not secret; used to detect enrollment changes deterministically.
119
+ static func domainStateHash(_ state: Data?) -> String? {
120
+ guard let state = state, !state.isEmpty else { return nil }
121
+ return SHA256.hash(data: state).map { String(format: "%02x", $0) }.joined()
122
+ }
123
+
124
+ static func mapLAError(_ error: Error, policy: AuthPolicy? = nil) -> SignerError {
125
+ let ns = error as NSError
126
+ guard ns.domain == LAError.errorDomain, let code = LAError.Code(rawValue: ns.code) else {
127
+ return SignerError(.unknown, "authentication error", platform: "\(ns.domain).\(ns.code)")
128
+ }
129
+ let p = "LAError.\(ns.code)"
130
+ switch code {
131
+ case .userCancel, .appCancel, .systemCancel, .userFallback:
132
+ return SignerError(.authenticationCancelled, "authentication was cancelled", platform: p)
133
+ case .authenticationFailed:
134
+ return SignerError(.authenticationFailed, "biometric authentication failed", platform: p)
135
+ case .biometryLockout:
136
+ return SignerError(.authenticationLockedOut, "biometrics are locked out; unlock the device with the passcode", platform: p)
137
+ case .biometryNotEnrolled:
138
+ if policy == .biometryCurrentSet {
139
+ return SignerError(.keyInvalidated, "biometric enrollment changed; the key is no longer usable", platform: p)
140
+ }
141
+ return SignerError(.biometricsNotEnrolled, "no biometrics are enrolled", platform: p)
142
+ case .biometryNotAvailable:
143
+ return SignerError(.biometricsUnavailable, "biometric authentication is not available", platform: p)
144
+ case .passcodeNotSet:
145
+ return SignerError(.passcodeNotSet, "device passcode is not set", platform: p)
146
+ case .notInteractive:
147
+ return SignerError(.authenticationCancelled, "authentication UI could not be shown", platform: p)
148
+ default:
149
+ return SignerError(.unknown, "authentication error", platform: p)
150
+ }
151
+ }
152
+
153
+ enum Operation { case create, lookup, sign }
154
+
155
+ static func mapOSStatus(_ status: OSStatus, during op: Operation) -> SignerError {
156
+ let p = "OSStatus.\(status)"
157
+ switch status {
158
+ case errSecUserCanceled:
159
+ return SignerError(.authenticationCancelled, "authentication was cancelled", platform: p)
160
+ case errSecAuthFailed:
161
+ return SignerError(.authenticationFailed, "authentication failed", platform: p)
162
+ case errSecInteractionNotAllowed:
163
+ return SignerError(.deviceLocked, "the key is not accessible while the device is locked", platform: p)
164
+ case errSecItemNotFound:
165
+ return SignerError(.keyNotFound, "no key exists for this keyId", platform: p)
166
+ case errSecDuplicateItem:
167
+ return SignerError(.keyAlreadyExists, "a key already exists for this keyId", platform: p)
168
+ case errSecNotAvailable, errSecUnimplemented:
169
+ return op == .create
170
+ ? SignerError(.secureHardwareUnavailable, "secure hardware is not available", platform: p)
171
+ : SignerError(.unknown, "keychain unavailable", platform: p)
172
+ default:
173
+ switch op {
174
+ case .sign: return SignerError(.signingFailed, "signing failed", platform: p)
175
+ case .create: return SignerError(.unknown, "key generation failed", platform: p)
176
+ case .lookup: return SignerError(.unknown, "keychain lookup failed", platform: p)
177
+ }
178
+ }
179
+ }
180
+
181
+ /// Maps a CFError from SecKey* APIs, which may carry either an OSStatus or an LAError.
182
+ static func mapCFError(_ error: Error?, during op: Operation, policy: AuthPolicy? = nil) -> SignerError {
183
+ guard let ns = error as NSError? else {
184
+ return op == .sign ? SignerError(.signingFailed, "signing failed") : SignerError(.unknown, "operation failed")
185
+ }
186
+ if ns.domain == LAError.errorDomain { return mapLAError(ns, policy: policy) }
187
+ if ns.domain == NSOSStatusErrorDomain { return mapOSStatus(OSStatus(ns.code), during: op) }
188
+ return op == .sign
189
+ ? SignerError(.signingFailed, "signing failed", platform: "\(ns.domain).\(ns.code)")
190
+ : SignerError(.unknown, "operation failed", platform: "\(ns.domain).\(ns.code)")
191
+ }
192
+ }