ds-01 1.0.13 → 1.0.14
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.
|
@@ -8,17 +8,20 @@ func fail(_ message: String) -> Never {
|
|
|
8
8
|
exit(1)
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
// Find
|
|
11
|
+
// Find our existing private key in the current user's Keychain.
|
|
12
12
|
func findPrivateKey() -> SecKey? {
|
|
13
13
|
let query: [String: Any] = [
|
|
14
|
-
kSecClass as String:
|
|
14
|
+
kSecClass as String:
|
|
15
|
+
kSecClassKey,
|
|
16
|
+
|
|
15
17
|
kSecAttrApplicationTag as String:
|
|
16
18
|
keyTag.data(using: .utf8)!,
|
|
19
|
+
|
|
17
20
|
kSecAttrKeyType as String:
|
|
18
21
|
kSecAttrKeyTypeECSECPrimeRandom,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
|
|
23
|
+
kSecReturnRef as String:
|
|
24
|
+
true
|
|
22
25
|
]
|
|
23
26
|
|
|
24
27
|
var result: CFTypeRef?
|
|
@@ -35,54 +38,16 @@ func findPrivateKey() -> SecKey? {
|
|
|
35
38
|
return (result as! SecKey)
|
|
36
39
|
}
|
|
37
40
|
|
|
41
|
+
|
|
42
|
+
// Create a persistent EC P-256 private key.
|
|
38
43
|
func createKey() {
|
|
39
44
|
|
|
40
|
-
//
|
|
45
|
+
// Don't create a second key if one already exists.
|
|
41
46
|
if findPrivateKey() != nil {
|
|
42
47
|
print("EXISTS")
|
|
43
48
|
return
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
// Keychain access control.
|
|
47
|
-
//
|
|
48
|
-
// WhenUnlockedThisDeviceOnly means:
|
|
49
|
-
// - available only while the device is unlocked
|
|
50
|
-
// - cannot be restored onto another device
|
|
51
|
-
//
|
|
52
|
-
// privateKeyUsage means the key can be used for signing.
|
|
53
|
-
var accessError: Unmanaged<CFError>?
|
|
54
|
-
|
|
55
|
-
guard let access = SecAccessControlCreateWithFlags(
|
|
56
|
-
nil,
|
|
57
|
-
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
58
|
-
.privateKeyUsage,
|
|
59
|
-
&accessError
|
|
60
|
-
) else {
|
|
61
|
-
|
|
62
|
-
if let error = accessError {
|
|
63
|
-
fail(
|
|
64
|
-
"Unable to create Keychain access control: " +
|
|
65
|
-
error.takeRetainedValue().localizedDescription
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fail("Unable to create Keychain access control.")
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let privateAttributes: [String: Any] = [
|
|
73
|
-
|
|
74
|
-
// Permanently store the private key.
|
|
75
|
-
kSecAttrIsPermanent as String: true,
|
|
76
|
-
|
|
77
|
-
// Identifier used to find the key later.
|
|
78
|
-
kSecAttrApplicationTag as String:
|
|
79
|
-
keyTag.data(using: .utf8)!,
|
|
80
|
-
|
|
81
|
-
// Access rules for the private key.
|
|
82
|
-
kSecAttrAccessControl as String:
|
|
83
|
-
access
|
|
84
|
-
]
|
|
85
|
-
|
|
86
51
|
let attributes: [String: Any] = [
|
|
87
52
|
|
|
88
53
|
// EC P-256 key.
|
|
@@ -92,14 +57,15 @@ func createKey() {
|
|
|
92
57
|
kSecAttrKeySizeInBits as String:
|
|
93
58
|
256,
|
|
94
59
|
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
60
|
+
// Store the private key permanently in Keychain.
|
|
61
|
+
kSecPrivateKeyAttrs as String: [
|
|
62
|
+
|
|
63
|
+
kSecAttrIsPermanent as String:
|
|
64
|
+
true,
|
|
99
65
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
66
|
+
kSecAttrApplicationTag as String:
|
|
67
|
+
keyTag.data(using: .utf8)!
|
|
68
|
+
]
|
|
103
69
|
]
|
|
104
70
|
|
|
105
71
|
var error: Unmanaged<CFError>?
|
|
@@ -111,28 +77,30 @@ func createKey() {
|
|
|
111
77
|
|
|
112
78
|
if let error {
|
|
113
79
|
fail(
|
|
114
|
-
"
|
|
80
|
+
"Keychain key creation failed: " +
|
|
115
81
|
error.takeRetainedValue().localizedDescription
|
|
116
82
|
)
|
|
117
83
|
}
|
|
118
84
|
|
|
119
|
-
fail("
|
|
85
|
+
fail("Keychain key creation failed.")
|
|
120
86
|
}
|
|
121
87
|
|
|
122
88
|
print("CREATED")
|
|
123
89
|
}
|
|
124
90
|
|
|
125
91
|
|
|
126
|
-
// Return
|
|
92
|
+
// Return only the public key.
|
|
127
93
|
//
|
|
128
94
|
// The private key is never exported.
|
|
129
95
|
func publicKey() {
|
|
130
96
|
|
|
131
97
|
guard let privateKey = findPrivateKey() else {
|
|
132
|
-
fail("DS01
|
|
98
|
+
fail("DS01 device key not found.")
|
|
133
99
|
}
|
|
134
100
|
|
|
135
|
-
guard let publicKey = SecKeyCopyPublicKey(
|
|
101
|
+
guard let publicKey = SecKeyCopyPublicKey(
|
|
102
|
+
privateKey
|
|
103
|
+
) else {
|
|
136
104
|
fail("Unable to obtain public key.")
|
|
137
105
|
}
|
|
138
106
|
|
|
@@ -159,11 +127,11 @@ func publicKey() {
|
|
|
159
127
|
}
|
|
160
128
|
|
|
161
129
|
|
|
162
|
-
// Sign data using the
|
|
130
|
+
// Sign data using the Keychain private key.
|
|
163
131
|
func sign(_ base64Data: String) {
|
|
164
132
|
|
|
165
133
|
guard let privateKey = findPrivateKey() else {
|
|
166
|
-
fail("DS01
|
|
134
|
+
fail("DS01 device key not found.")
|
|
167
135
|
}
|
|
168
136
|
|
|
169
137
|
guard let data = Data(
|
|
@@ -180,9 +148,7 @@ func sign(_ base64Data: String) {
|
|
|
180
148
|
.sign,
|
|
181
149
|
algorithm
|
|
182
150
|
) else {
|
|
183
|
-
fail(
|
|
184
|
-
"Secure Enclave key does not support signing."
|
|
185
|
-
)
|
|
151
|
+
fail("Device key does not support signing.")
|
|
186
152
|
}
|
|
187
153
|
|
|
188
154
|
var error: Unmanaged<CFError>?
|
|
@@ -196,12 +162,12 @@ func sign(_ base64Data: String) {
|
|
|
196
162
|
|
|
197
163
|
if let error {
|
|
198
164
|
fail(
|
|
199
|
-
"
|
|
165
|
+
"Signing failed: " +
|
|
200
166
|
error.takeRetainedValue().localizedDescription
|
|
201
167
|
)
|
|
202
168
|
}
|
|
203
169
|
|
|
204
|
-
fail("
|
|
170
|
+
fail("Signing failed.")
|
|
205
171
|
}
|
|
206
172
|
|
|
207
173
|
print(
|
|
@@ -210,7 +176,7 @@ func sign(_ base64Data: String) {
|
|
|
210
176
|
}
|
|
211
177
|
|
|
212
178
|
|
|
213
|
-
//
|
|
179
|
+
// Delete the DS01 private key from Keychain.
|
|
214
180
|
func deleteKey() {
|
|
215
181
|
|
|
216
182
|
let query: [String: Any] = [
|
|
@@ -221,10 +187,7 @@ func deleteKey() {
|
|
|
221
187
|
keyTag.data(using: .utf8)!,
|
|
222
188
|
|
|
223
189
|
kSecAttrKeyType as String:
|
|
224
|
-
kSecAttrKeyTypeECSECPrimeRandom
|
|
225
|
-
|
|
226
|
-
kSecAttrTokenID as String:
|
|
227
|
-
kSecAttrTokenIDSecureEnclave
|
|
190
|
+
kSecAttrKeyTypeECSECPrimeRandom
|
|
228
191
|
]
|
|
229
192
|
|
|
230
193
|
let status = SecItemDelete(
|
|
@@ -235,7 +198,7 @@ func deleteKey() {
|
|
|
235
198
|
status != errSecItemNotFound {
|
|
236
199
|
|
|
237
200
|
fail(
|
|
238
|
-
"Unable to delete DS01
|
|
201
|
+
"Unable to delete DS01 device key."
|
|
239
202
|
)
|
|
240
203
|
}
|
|
241
204
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ds-01",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "Make your site best with DS01",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"ds01": "dist/index.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "tsc && node scripts/copy-native.js"
|
|
10
|
+
"build": "tsc && node scripts/copy-native.js && node scripts/build-macos-helper.js"
|
|
11
11
|
},
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
if (process.platform !== "darwin") {
|
|
6
|
+
console.log("Skipping macOS native helper build.");
|
|
7
|
+
process.exit(0);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const root = process.cwd();
|
|
11
|
+
|
|
12
|
+
const source = path.join(
|
|
13
|
+
root,
|
|
14
|
+
"src",
|
|
15
|
+
"native",
|
|
16
|
+
"macos-key-helper.swift",
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const nativeDir = path.join(
|
|
20
|
+
root,
|
|
21
|
+
"dist",
|
|
22
|
+
"native",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const output = path.join(
|
|
26
|
+
nativeDir,
|
|
27
|
+
"macos-key-helper",
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
if (!fs.existsSync(source)) {
|
|
31
|
+
throw new Error(`Swift helper not found: ${source}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fs.mkdirSync(nativeDir, {
|
|
35
|
+
recursive: true,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
console.log("Building macOS native helper...");
|
|
39
|
+
|
|
40
|
+
execFileSync(
|
|
41
|
+
"swiftc",
|
|
42
|
+
[
|
|
43
|
+
source,
|
|
44
|
+
"-o",
|
|
45
|
+
output,
|
|
46
|
+
],
|
|
47
|
+
{
|
|
48
|
+
stdio: "inherit",
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
console.log("macOS native helper built successfully.");
|
|
@@ -8,17 +8,20 @@ func fail(_ message: String) -> Never {
|
|
|
8
8
|
exit(1)
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
// Find
|
|
11
|
+
// Find our existing private key in the current user's Keychain.
|
|
12
12
|
func findPrivateKey() -> SecKey? {
|
|
13
13
|
let query: [String: Any] = [
|
|
14
|
-
kSecClass as String:
|
|
14
|
+
kSecClass as String:
|
|
15
|
+
kSecClassKey,
|
|
16
|
+
|
|
15
17
|
kSecAttrApplicationTag as String:
|
|
16
18
|
keyTag.data(using: .utf8)!,
|
|
19
|
+
|
|
17
20
|
kSecAttrKeyType as String:
|
|
18
21
|
kSecAttrKeyTypeECSECPrimeRandom,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
|
|
23
|
+
kSecReturnRef as String:
|
|
24
|
+
true
|
|
22
25
|
]
|
|
23
26
|
|
|
24
27
|
var result: CFTypeRef?
|
|
@@ -35,54 +38,16 @@ func findPrivateKey() -> SecKey? {
|
|
|
35
38
|
return (result as! SecKey)
|
|
36
39
|
}
|
|
37
40
|
|
|
41
|
+
|
|
42
|
+
// Create a persistent EC P-256 private key.
|
|
38
43
|
func createKey() {
|
|
39
44
|
|
|
40
|
-
//
|
|
45
|
+
// Don't create a second key if one already exists.
|
|
41
46
|
if findPrivateKey() != nil {
|
|
42
47
|
print("EXISTS")
|
|
43
48
|
return
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
// Keychain access control.
|
|
47
|
-
//
|
|
48
|
-
// WhenUnlockedThisDeviceOnly means:
|
|
49
|
-
// - available only while the device is unlocked
|
|
50
|
-
// - cannot be restored onto another device
|
|
51
|
-
//
|
|
52
|
-
// privateKeyUsage means the key can be used for signing.
|
|
53
|
-
var accessError: Unmanaged<CFError>?
|
|
54
|
-
|
|
55
|
-
guard let access = SecAccessControlCreateWithFlags(
|
|
56
|
-
nil,
|
|
57
|
-
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
58
|
-
.privateKeyUsage,
|
|
59
|
-
&accessError
|
|
60
|
-
) else {
|
|
61
|
-
|
|
62
|
-
if let error = accessError {
|
|
63
|
-
fail(
|
|
64
|
-
"Unable to create Keychain access control: " +
|
|
65
|
-
error.takeRetainedValue().localizedDescription
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fail("Unable to create Keychain access control.")
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let privateAttributes: [String: Any] = [
|
|
73
|
-
|
|
74
|
-
// Permanently store the private key.
|
|
75
|
-
kSecAttrIsPermanent as String: true,
|
|
76
|
-
|
|
77
|
-
// Identifier used to find the key later.
|
|
78
|
-
kSecAttrApplicationTag as String:
|
|
79
|
-
keyTag.data(using: .utf8)!,
|
|
80
|
-
|
|
81
|
-
// Access rules for the private key.
|
|
82
|
-
kSecAttrAccessControl as String:
|
|
83
|
-
access
|
|
84
|
-
]
|
|
85
|
-
|
|
86
51
|
let attributes: [String: Any] = [
|
|
87
52
|
|
|
88
53
|
// EC P-256 key.
|
|
@@ -92,14 +57,15 @@ func createKey() {
|
|
|
92
57
|
kSecAttrKeySizeInBits as String:
|
|
93
58
|
256,
|
|
94
59
|
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
60
|
+
// Store the private key permanently in Keychain.
|
|
61
|
+
kSecPrivateKeyAttrs as String: [
|
|
62
|
+
|
|
63
|
+
kSecAttrIsPermanent as String:
|
|
64
|
+
true,
|
|
99
65
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
66
|
+
kSecAttrApplicationTag as String:
|
|
67
|
+
keyTag.data(using: .utf8)!
|
|
68
|
+
]
|
|
103
69
|
]
|
|
104
70
|
|
|
105
71
|
var error: Unmanaged<CFError>?
|
|
@@ -111,28 +77,30 @@ func createKey() {
|
|
|
111
77
|
|
|
112
78
|
if let error {
|
|
113
79
|
fail(
|
|
114
|
-
"
|
|
80
|
+
"Keychain key creation failed: " +
|
|
115
81
|
error.takeRetainedValue().localizedDescription
|
|
116
82
|
)
|
|
117
83
|
}
|
|
118
84
|
|
|
119
|
-
fail("
|
|
85
|
+
fail("Keychain key creation failed.")
|
|
120
86
|
}
|
|
121
87
|
|
|
122
88
|
print("CREATED")
|
|
123
89
|
}
|
|
124
90
|
|
|
125
91
|
|
|
126
|
-
// Return
|
|
92
|
+
// Return only the public key.
|
|
127
93
|
//
|
|
128
94
|
// The private key is never exported.
|
|
129
95
|
func publicKey() {
|
|
130
96
|
|
|
131
97
|
guard let privateKey = findPrivateKey() else {
|
|
132
|
-
fail("DS01
|
|
98
|
+
fail("DS01 device key not found.")
|
|
133
99
|
}
|
|
134
100
|
|
|
135
|
-
guard let publicKey = SecKeyCopyPublicKey(
|
|
101
|
+
guard let publicKey = SecKeyCopyPublicKey(
|
|
102
|
+
privateKey
|
|
103
|
+
) else {
|
|
136
104
|
fail("Unable to obtain public key.")
|
|
137
105
|
}
|
|
138
106
|
|
|
@@ -159,11 +127,11 @@ func publicKey() {
|
|
|
159
127
|
}
|
|
160
128
|
|
|
161
129
|
|
|
162
|
-
// Sign data using the
|
|
130
|
+
// Sign data using the Keychain private key.
|
|
163
131
|
func sign(_ base64Data: String) {
|
|
164
132
|
|
|
165
133
|
guard let privateKey = findPrivateKey() else {
|
|
166
|
-
fail("DS01
|
|
134
|
+
fail("DS01 device key not found.")
|
|
167
135
|
}
|
|
168
136
|
|
|
169
137
|
guard let data = Data(
|
|
@@ -180,9 +148,7 @@ func sign(_ base64Data: String) {
|
|
|
180
148
|
.sign,
|
|
181
149
|
algorithm
|
|
182
150
|
) else {
|
|
183
|
-
fail(
|
|
184
|
-
"Secure Enclave key does not support signing."
|
|
185
|
-
)
|
|
151
|
+
fail("Device key does not support signing.")
|
|
186
152
|
}
|
|
187
153
|
|
|
188
154
|
var error: Unmanaged<CFError>?
|
|
@@ -196,12 +162,12 @@ func sign(_ base64Data: String) {
|
|
|
196
162
|
|
|
197
163
|
if let error {
|
|
198
164
|
fail(
|
|
199
|
-
"
|
|
165
|
+
"Signing failed: " +
|
|
200
166
|
error.takeRetainedValue().localizedDescription
|
|
201
167
|
)
|
|
202
168
|
}
|
|
203
169
|
|
|
204
|
-
fail("
|
|
170
|
+
fail("Signing failed.")
|
|
205
171
|
}
|
|
206
172
|
|
|
207
173
|
print(
|
|
@@ -210,7 +176,7 @@ func sign(_ base64Data: String) {
|
|
|
210
176
|
}
|
|
211
177
|
|
|
212
178
|
|
|
213
|
-
//
|
|
179
|
+
// Delete the DS01 private key from Keychain.
|
|
214
180
|
func deleteKey() {
|
|
215
181
|
|
|
216
182
|
let query: [String: Any] = [
|
|
@@ -221,10 +187,7 @@ func deleteKey() {
|
|
|
221
187
|
keyTag.data(using: .utf8)!,
|
|
222
188
|
|
|
223
189
|
kSecAttrKeyType as String:
|
|
224
|
-
kSecAttrKeyTypeECSECPrimeRandom
|
|
225
|
-
|
|
226
|
-
kSecAttrTokenID as String:
|
|
227
|
-
kSecAttrTokenIDSecureEnclave
|
|
190
|
+
kSecAttrKeyTypeECSECPrimeRandom
|
|
228
191
|
]
|
|
229
192
|
|
|
230
193
|
let status = SecItemDelete(
|
|
@@ -235,7 +198,7 @@ func deleteKey() {
|
|
|
235
198
|
status != errSecItemNotFound {
|
|
236
199
|
|
|
237
200
|
fail(
|
|
238
|
-
"Unable to delete DS01
|
|
201
|
+
"Unable to delete DS01 device key."
|
|
239
202
|
)
|
|
240
203
|
}
|
|
241
204
|
|