@secrez/core 1.1.1 → 2.0.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.
- package/README.md +9 -8
- package/coverage.report +94 -78
- package/package.json +4 -4
- package/src/Secrez.js +126 -28
- package/src/_Secrez.js +86 -2
- package/src/accountKdf.js +35 -0
- package/src/config/ConfigUtils.js +1 -0
- package/src/config/VaultPermissions.js +4 -1
- package/src/config/index.js +1 -1
package/README.md
CHANGED
|
@@ -41,19 +41,20 @@ API documentation
|
|
|
41
41
|
## Test coverage
|
|
42
42
|
|
|
43
43
|
```
|
|
44
|
-
|
|
44
|
+
72 passing (436ms)
|
|
45
45
|
|
|
46
46
|
----------------------|---------|----------|---------|---------|----------------------
|
|
47
47
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
48
48
|
----------------------|---------|----------|---------|---------|----------------------
|
|
49
|
-
All files | 98.
|
|
50
|
-
src |
|
|
49
|
+
All files | 98.6 | 92.3 | 98.92 | 98.6 |
|
|
50
|
+
src | 98.39 | 94.44 | 98.61 | 98.39 |
|
|
51
51
|
Entry.js | 100 | 95.65 | 100 | 100 | 39
|
|
52
|
-
Secrez.js | 100 | 97.
|
|
53
|
-
_Secrez.js |
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
Secrez.js | 100 | 97.32 | 100 | 100 | 195,324
|
|
53
|
+
_Secrez.js | 95.86 | 86.04 | 96.55 | 95.86 | 12,15-16,152,333-334
|
|
54
|
+
accountKdf.js | 100 | 100 | 100 | 100 |
|
|
55
|
+
src/config | 99.21 | 85.18 | 100 | 99.21 |
|
|
56
|
+
ConfigUtils.js | 98.59 | 84.84 | 100 | 98.59 | 144
|
|
57
|
+
VaultPermissions.js | 100 | 85.71 | 100 | 100 | 56,82,105
|
|
57
58
|
booleans.js | 100 | 100 | 100 | 100 |
|
|
58
59
|
index.js | 100 | 100 | 100 | 100 |
|
|
59
60
|
----------------------|---------|----------|---------|---------|----------------------
|
package/coverage.report
CHANGED
|
@@ -1,112 +1,128 @@
|
|
|
1
1
|
|
|
2
|
-
> @secrez/core@
|
|
2
|
+
> @secrez/core@2.0.0 test /Users/francescosullo/Projects/Secrez/secrez/packages/core
|
|
3
3
|
> cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
#Entry
|
|
8
8
|
#constructor
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
✔ should instantiate the Entry
|
|
10
|
+
✔ should ignore wrong parameters
|
|
11
11
|
#set
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
✔ should set some options for the Entry
|
|
13
|
+
✔ should skip invalid options
|
|
14
14
|
#unset
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
✔ should unset some options
|
|
16
|
+
✔ should skip invalid or undefined options
|
|
17
17
|
#sanitizeName
|
|
18
|
-
|
|
18
|
+
✔ should sanitize names
|
|
19
19
|
#sanitizePath
|
|
20
|
-
|
|
20
|
+
✔ should sanitize path
|
|
21
21
|
|
|
22
22
|
#Secrez
|
|
23
23
|
default secrez dir
|
|
24
|
-
|
|
24
|
+
✔ should throw trying to test in the default ~/.secrez folder
|
|
25
25
|
custom (testing) secrez dir
|
|
26
|
+
accountScryptParams
|
|
27
|
+
✔ should use the production cost outside tests
|
|
26
28
|
derivePassword
|
|
27
|
-
|
|
29
|
+
✔ should derive a password and obtain a predeterminded hash
|
|
28
30
|
signup and signin
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
✔ should signup the user and signin without saving the iterations
|
|
32
|
+
✔ should signup in two instances without conflicts
|
|
33
|
+
✔ should signup the user and signin saved the iterations
|
|
34
|
+
✔ should signup with version one, and signin and upgrade to version two
|
|
35
|
+
✔ should signup the user, change password and signin
|
|
36
|
+
✔ should refuse to change the iteration count of a version 4 key
|
|
37
|
+
✔ should refuse to change password and iterations together on version 4
|
|
38
|
+
✔ should clear secrets from memory on signout
|
|
39
|
+
✔ should accept a password in either unicode form
|
|
40
|
+
✔ should migrate a version 3 key and keep the master key
|
|
41
|
+
✔ should change a version 3 password wrapping before migration
|
|
42
|
+
✔ should read a saved iteration count when migrating
|
|
43
|
+
✔ should reject an empty password
|
|
44
|
+
✔ should not write version 4 when the version 3 password is wrong
|
|
36
45
|
should throw an error
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
✔ trying to signup if Secrez has not been initiated
|
|
47
|
+
✔ trying to signin if Secrez has not been initiated
|
|
48
|
+
✔ trying to signup if account already exists
|
|
49
|
+
✔ trying to migrate without the iteration count
|
|
50
|
+
✔ trying to signin if account is not set yet
|
|
51
|
+
✔ should sign in to a version 4 account without an iteration count
|
|
52
|
+
✔ trying to signin with the wrong password
|
|
53
|
+
✔ should ignore a wrong iteration count on a version 4 account
|
|
54
|
+
✔ trying to signin with the right password but using a wrong hash on file
|
|
55
|
+
✔ trying to sign out if never signed-in
|
|
46
56
|
encryptEntry and decryptEntry
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
✔ should encrypt a name and decrypt it
|
|
58
|
+
✔ should encrypt a very long name and decrypt it
|
|
59
|
+
✔ should encrypt name and content and decrypt it
|
|
60
|
+
✔ should encrypt only the content and decrypt it
|
|
61
|
+
✔ should decrypt content-only entry with the bound timestamp
|
|
62
|
+
✔ should re-encrypt the content and decrypt it
|
|
63
|
+
✔ should throw if the user is not logged in
|
|
64
|
+
✔ should throw if wrong type
|
|
65
|
+
✔ should throw if no entry is passed
|
|
66
|
+
✔ should throw if encrypted data are corrupted
|
|
67
|
+
✔ should throw if content has not the same id of the name
|
|
68
|
+
✔ should throw if parameters are missed
|
|
58
69
|
encryptData and decryptData
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
✔ should encrypt some data and decrypt it
|
|
71
|
+
✔ should throw trying to get box and sign secret keys
|
|
61
72
|
preEncryptData and preDecryptData
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
generateSharedSecrets && recoverSharedSecrets
|
|
65
|
-
✓ should set up a second factor (72ms)
|
|
66
|
-
✓ should set up a second factor and then upgrade from derivationVersion 1 to 2 (46ms)
|
|
67
|
-
✓ should set up a second factor and a recovery code and remove them (70ms)
|
|
68
|
-
✓ should set up a second factor and a recovery code and remove the recovery code (69ms)
|
|
69
|
-
✓ should throw if wrong secret (42ms)
|
|
70
|
-
✓ should throw if wrong authenticator (43ms)
|
|
73
|
+
✔ should encrypt some data with the derivedPassword and decrypt it
|
|
74
|
+
✔ should throw trying to get the master key
|
|
71
75
|
#readConf
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
✔ should encrypt some data with the derivedPassword and decrypt it
|
|
77
|
+
✔ should throw trying to get the master key
|
|
78
|
+
✔ should throw trying to get the master key
|
|
75
79
|
#signMessage
|
|
76
|
-
|
|
80
|
+
✔ should sign a message and verify it
|
|
77
81
|
#getPublicKey
|
|
78
|
-
|
|
82
|
+
✔ should sign a message and verify it
|
|
79
83
|
#encrypt and decrypt shared data
|
|
80
|
-
|
|
84
|
+
✔ should get a shared ket
|
|
81
85
|
|
|
82
86
|
#config
|
|
83
|
-
|
|
87
|
+
✔ all field should be correctly configured
|
|
84
88
|
|
|
85
89
|
#ConfigUtils
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
✔ should check if a type is valid
|
|
91
|
+
✔ #setSecrez should configure a secrez environment
|
|
92
|
+
✔ #setAndGetDataset should set and get a secondary db
|
|
93
|
+
✔ #deleteDataset should delete a dataset path
|
|
94
|
+
✔ #getDatasetPath should get a dataset path
|
|
95
|
+
✔ #getEnv should return the environment
|
|
92
96
|
|
|
97
|
+
#VaultPermissions
|
|
98
|
+
✔ should create directories with mode 0o700
|
|
99
|
+
✔ should chmod directories synchronously
|
|
100
|
+
✔ should probe the keys directory before an account exists
|
|
101
|
+
✔ should create files with mode 0o600
|
|
102
|
+
✔ should probe a version 3 key file when version 4 is absent
|
|
103
|
+
✔ should detect secure vault permissions via probe
|
|
104
|
+
✔ should secure legacy permissions on login
|
|
105
|
+
✔ should skip securing when probe is already secure
|
|
93
106
|
|
|
94
|
-
60 passing (2s)
|
|
95
107
|
|
|
96
|
-
|
|
97
|
-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
98
|
-
-----------------|---------|----------|---------|---------|-------------------
|
|
99
|
-
All files | 99.75 | 94.31 | 100 | 99.75 |
|
|
100
|
-
src | 100 | 96.06 | 100 | 100 |
|
|
101
|
-
Entry.js | 100 | 95.65 | 100 | 100 | 39
|
|
102
|
-
Secrez.js | 100 | 96.94 | 100 | 100 | 154,319,442
|
|
103
|
-
_Secrez.js | 100 | 91.66 | 100 | 100 | 14,226
|
|
104
|
-
src/config | 98.57 | 84.84 | 100 | 98.57 |
|
|
105
|
-
ConfigUtils.js | 98.48 | 84.84 | 100 | 98.48 | 137
|
|
106
|
-
booleans.js | 100 | 100 | 100 | 100 |
|
|
107
|
-
index.js | 100 | 100 | 100 | 100 |
|
|
108
|
-
-----------------|---------|----------|---------|---------|-------------------
|
|
108
|
+
72 passing (436ms)
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
----------------------|---------|----------|---------|---------|----------------------
|
|
111
|
+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
112
|
+
----------------------|---------|----------|---------|---------|----------------------
|
|
113
|
+
All files | 98.6 | 92.3 | 98.92 | 98.6 |
|
|
114
|
+
src | 98.39 | 94.44 | 98.61 | 98.39 |
|
|
115
|
+
Entry.js | 100 | 95.65 | 100 | 100 | 39
|
|
116
|
+
Secrez.js | 100 | 97.32 | 100 | 100 | 195,324
|
|
117
|
+
_Secrez.js | 95.86 | 86.04 | 96.55 | 95.86 | 12,15-16,152,333-334
|
|
118
|
+
accountKdf.js | 100 | 100 | 100 | 100 |
|
|
119
|
+
src/config | 99.21 | 85.18 | 100 | 99.21 |
|
|
120
|
+
ConfigUtils.js | 98.59 | 84.84 | 100 | 98.59 | 144
|
|
121
|
+
VaultPermissions.js | 100 | 85.71 | 100 | 100 | 56,82,105
|
|
122
|
+
booleans.js | 100 | 100 | 100 | 100 |
|
|
123
|
+
index.js | 100 | 100 | 100 | 100 |
|
|
124
|
+
----------------------|---------|----------|---------|---------|----------------------
|
|
125
|
+
|
|
126
|
+
> @secrez/core@2.0.0 posttest /Users/francescosullo/Projects/Secrez/secrez/packages/core
|
|
127
|
+
> nyc check-coverage --statements 98 --branches 91 --functions 98 --lines 98
|
|
112
128
|
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secrez/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@secrez/crypto": "~1.
|
|
10
|
-
"@secrez/utils": "~1.1.
|
|
9
|
+
"@secrez/crypto": "~1.2.0",
|
|
10
|
+
"@secrez/utils": "~1.1.2",
|
|
11
11
|
"fs-extra": "^9.1.0",
|
|
12
12
|
"lodash": "^4.17.21"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@secrez/test-helpers": "~2.0.
|
|
15
|
+
"@secrez/test-helpers": "~2.0.5",
|
|
16
16
|
"chai": "^4.5.0",
|
|
17
17
|
"cross-env": "^7.0.3",
|
|
18
18
|
"mocha": "^10.8.2",
|
package/src/Secrez.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
const { homedir } = require("os");
|
|
2
|
+
const path = require("path");
|
|
2
3
|
const fs = require("fs-extra");
|
|
3
4
|
const _ = require("lodash");
|
|
4
5
|
const Crypto = require("@secrez/crypto");
|
|
5
6
|
const ConfigUtils = require("./config/ConfigUtils");
|
|
6
7
|
const VaultPermissions = require("./config/VaultPermissions");
|
|
7
8
|
const Entry = require("./Entry");
|
|
9
|
+
const { newAccountKdf } = require("./accountKdf");
|
|
8
10
|
|
|
9
11
|
const { DO_NOT_VERIFY, URL_SAFE } = require("./config/booleans");
|
|
10
12
|
|
|
@@ -37,40 +39,104 @@ module.exports = function () {
|
|
|
37
39
|
);
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
async
|
|
42
|
+
async hasAccount() {
|
|
41
43
|
if (!this.config || !this.config.keysPath) {
|
|
42
|
-
|
|
44
|
+
return false;
|
|
43
45
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
return (
|
|
47
|
+
(await fs.pathExists(this.config.keysPathV4)) ||
|
|
48
|
+
(await fs.pathExists(this.config.keysPath))
|
|
49
|
+
);
|
|
50
|
+
}
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
async signup(password) {
|
|
53
|
+
if (!this.config || !this.config.keysPath) {
|
|
54
|
+
throw new Error("Secrez not initiated");
|
|
55
|
+
}
|
|
56
|
+
if (await this.hasAccount()) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"An account already exists. Please, sign in or chose a different container directory"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const kdf = newAccountKdf();
|
|
62
|
+
_secrez = new _Secrez(this);
|
|
63
|
+
await _secrez.initV4(password, kdf);
|
|
64
|
+
await this.persistNewAccount(kdf);
|
|
65
|
+
}
|
|
49
66
|
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
// Version 3 signup, kept so a vault can be opened and migrated.
|
|
68
|
+
// New accounts use signup(), which writes version 4.
|
|
69
|
+
async signupV3(password, iterations) {
|
|
70
|
+
if (!this.config || !this.config.keysPath) {
|
|
71
|
+
throw new Error("Secrez not initiated");
|
|
72
|
+
}
|
|
73
|
+
if (await this.hasAccount()) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"An account already exists. Please, sign in or chose a different container directory"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
_secrez = new _Secrez(this);
|
|
79
|
+
await _secrez.init(password, iterations);
|
|
80
|
+
const id = Crypto.b64Hash(Crypto.generateKey());
|
|
81
|
+
const { sign, box, key, hash } = await _secrez.signup();
|
|
82
|
+
this.masterKeyHash = hash;
|
|
83
|
+
this.keyMigrated = false;
|
|
84
|
+
const data = {
|
|
85
|
+
id,
|
|
86
|
+
sign,
|
|
87
|
+
box,
|
|
88
|
+
key,
|
|
89
|
+
hash,
|
|
90
|
+
when: _secrez.encrypt(Date.now().toString()),
|
|
91
|
+
version: 3,
|
|
92
|
+
};
|
|
93
|
+
_secrez.setConf(
|
|
94
|
+
await this.signAndSave(data, this.config.keysPath),
|
|
95
|
+
DO_NOT_VERIFY
|
|
96
|
+
);
|
|
97
|
+
}
|
|
52
98
|
|
|
53
|
-
|
|
99
|
+
async persistNewAccount(kdf, extras = {}) {
|
|
100
|
+
const id = Crypto.b64Hash(Crypto.generateKey());
|
|
101
|
+
const { sign, box, key, hash } = await _secrez.signup();
|
|
102
|
+
sign.alg = "ed25519";
|
|
103
|
+
box.alg = "x25519";
|
|
104
|
+
this.masterKeyHash = hash;
|
|
105
|
+
this.keyMigrated = false;
|
|
106
|
+
const data = Object.assign(
|
|
107
|
+
{
|
|
54
108
|
id,
|
|
55
109
|
sign,
|
|
56
110
|
box,
|
|
57
111
|
key,
|
|
58
112
|
hash,
|
|
59
113
|
when: _secrez.encrypt(Date.now().toString()),
|
|
60
|
-
version:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
114
|
+
version: 4,
|
|
115
|
+
kdf,
|
|
116
|
+
cipher: "xsalsa20-poly1305",
|
|
117
|
+
},
|
|
118
|
+
extras
|
|
119
|
+
);
|
|
120
|
+
_secrez.setConf(
|
|
121
|
+
await this.signAndSave(data, this.config.keysPathV4),
|
|
122
|
+
DO_NOT_VERIFY
|
|
123
|
+
);
|
|
68
124
|
}
|
|
69
125
|
|
|
70
126
|
async signin(password, iterations) {
|
|
71
127
|
if (!this.config || !this.config.keysPath) {
|
|
72
128
|
throw new Error("Secrez not initiated");
|
|
73
129
|
}
|
|
130
|
+
this.keyMigrated = false;
|
|
131
|
+
if (await fs.pathExists(this.config.keysPathV4)) {
|
|
132
|
+
const conf = JSON.parse(
|
|
133
|
+
await fs.readFile(this.config.keysPathV4, "utf8")
|
|
134
|
+
);
|
|
135
|
+
return this.signinWithConf(conf, password, { version: 4 });
|
|
136
|
+
}
|
|
137
|
+
if (!(await fs.pathExists(this.config.keysPath))) {
|
|
138
|
+
throw new Error("Account not set yet");
|
|
139
|
+
}
|
|
74
140
|
if (!iterations) {
|
|
75
141
|
const env = await ConfigUtils.getEnv(this.config);
|
|
76
142
|
iterations = env.iterations;
|
|
@@ -79,29 +145,56 @@ module.exports = function () {
|
|
|
79
145
|
throw new Error("Iterations is missed");
|
|
80
146
|
}
|
|
81
147
|
iterations = parseInt(iterations);
|
|
82
|
-
const conf = await this.
|
|
148
|
+
const conf = JSON.parse(await fs.readFile(this.config.keysPath, "utf8"));
|
|
149
|
+
await this.signinWithConf(conf, password, {
|
|
150
|
+
version: 3,
|
|
151
|
+
iterations,
|
|
152
|
+
});
|
|
153
|
+
await this.migrateToV4();
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async signinWithConf(conf, password, options) {
|
|
83
158
|
const data = conf.data;
|
|
84
159
|
_secrez = new _Secrez(this);
|
|
85
|
-
|
|
160
|
+
if (options.version === 4) {
|
|
161
|
+
await _secrez.initV4(password, data.kdf);
|
|
162
|
+
} else {
|
|
163
|
+
await _secrez.init(password, options.iterations);
|
|
164
|
+
}
|
|
86
165
|
/* istanbul ignore if */
|
|
87
166
|
if (!data.key) {
|
|
88
167
|
throw new Error("No valid data found");
|
|
89
168
|
}
|
|
90
|
-
|
|
169
|
+
const masterKeyHash = await _secrez.signin(data);
|
|
91
170
|
this.setMasterKeyHash(conf, masterKeyHash);
|
|
92
171
|
return 0;
|
|
93
172
|
}
|
|
94
173
|
|
|
174
|
+
async migrateToV4() {
|
|
175
|
+
const conf = _secrez.migrateToV4();
|
|
176
|
+
await this.writeKeyFile(this.config.keysPathV4, conf);
|
|
177
|
+
_secrez.setConf(conf, DO_NOT_VERIFY);
|
|
178
|
+
this.keyMigrated = true;
|
|
179
|
+
}
|
|
180
|
+
|
|
95
181
|
async derivePassword(password, iterations) {
|
|
96
182
|
return await _Secrez.derivePassword(password, iterations);
|
|
97
183
|
}
|
|
98
184
|
|
|
99
|
-
async
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
JSON.stringify(conf)
|
|
185
|
+
async writeKeyFile(filePath, conf) {
|
|
186
|
+
const tmp = path.join(
|
|
187
|
+
this.config.tmpPath,
|
|
188
|
+
`${path.basename(filePath)}.tmp`
|
|
104
189
|
);
|
|
190
|
+
await VaultPermissions.writeFile(tmp, JSON.stringify(conf));
|
|
191
|
+
await fs.move(tmp, filePath, { overwrite: true });
|
|
192
|
+
await fs.chmod(filePath, VaultPermissions.fileMode);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async signAndSave(data, filePath = this.config.keysPath) {
|
|
196
|
+
const conf = _secrez.signData(data);
|
|
197
|
+
await this.writeKeyFile(filePath, conf);
|
|
105
198
|
return conf;
|
|
106
199
|
}
|
|
107
200
|
|
|
@@ -127,8 +220,11 @@ module.exports = function () {
|
|
|
127
220
|
if (!this.config || !this.config.keysPath) {
|
|
128
221
|
throw new Error("Secrez not initiated");
|
|
129
222
|
}
|
|
130
|
-
|
|
131
|
-
|
|
223
|
+
const filePath = (await fs.pathExists(this.config.keysPathV4))
|
|
224
|
+
? this.config.keysPathV4
|
|
225
|
+
: this.config.keysPath;
|
|
226
|
+
if (await fs.pathExists(filePath)) {
|
|
227
|
+
return JSON.parse(await fs.readFile(filePath, "utf8"));
|
|
132
228
|
} else {
|
|
133
229
|
throw new Error("Account not set yet");
|
|
134
230
|
}
|
|
@@ -136,7 +232,9 @@ module.exports = function () {
|
|
|
136
232
|
|
|
137
233
|
async upgradeAccount(password, iterations) {
|
|
138
234
|
let data = await _secrez.changePassword(password, iterations);
|
|
139
|
-
|
|
235
|
+
const filePath =
|
|
236
|
+
data.version === 4 ? this.config.keysPathV4 : this.config.keysPath;
|
|
237
|
+
_secrez.setConf(await this.signAndSave(data, filePath), DO_NOT_VERIFY);
|
|
140
238
|
}
|
|
141
239
|
|
|
142
240
|
async verifyPassword(password) {
|
package/src/_Secrez.js
CHANGED
|
@@ -5,6 +5,7 @@ const { RETURN_UINT8_ARRAY } = require("./config/booleans");
|
|
|
5
5
|
|
|
6
6
|
const Crypto = require("@secrez/crypto");
|
|
7
7
|
const bs64 = Crypto.bs64;
|
|
8
|
+
const { newAccountKdf } = require("./accountKdf");
|
|
8
9
|
|
|
9
10
|
function zeroSensitive(value) {
|
|
10
11
|
if (!value) {
|
|
@@ -58,6 +59,38 @@ module.exports = function () {
|
|
|
58
59
|
__.derivedPassword = await _Secrez.derivePassword(password, iterations);
|
|
59
60
|
}
|
|
60
61
|
|
|
62
|
+
async initV4(password, kdf) {
|
|
63
|
+
__.password = _Secrez.normalizePassword(password);
|
|
64
|
+
__.kdf = kdf;
|
|
65
|
+
__.derivedPassword = _Secrez.derivePasswordV4(__.password, kdf);
|
|
66
|
+
delete __.iterations;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Rewrap the master key already in this session. The password and the
|
|
70
|
+
// master key stay in the closure; the caller only receives the signed file.
|
|
71
|
+
migrateToV4() {
|
|
72
|
+
const previous = this.conf.data;
|
|
73
|
+
const kdf = newAccountKdf();
|
|
74
|
+
__.password = _Secrez.normalizePassword(__.password);
|
|
75
|
+
__.kdf = kdf;
|
|
76
|
+
__.derivedPassword = _Secrez.derivePasswordV4(__.password, kdf);
|
|
77
|
+
delete __.iterations;
|
|
78
|
+
const sign = Object.assign({}, previous.sign, { alg: "ed25519" });
|
|
79
|
+
const box = Object.assign({}, previous.box, { alg: "x25519" });
|
|
80
|
+
return this.signData({
|
|
81
|
+
id: previous.id,
|
|
82
|
+
sign,
|
|
83
|
+
box,
|
|
84
|
+
key: this.preEncrypt(__.masterKey),
|
|
85
|
+
hash: previous.hash,
|
|
86
|
+
when: previous.when,
|
|
87
|
+
version: 4,
|
|
88
|
+
migratedFrom: 3,
|
|
89
|
+
kdf,
|
|
90
|
+
cipher: "xsalsa20-poly1305",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
61
94
|
preEncrypt(data) {
|
|
62
95
|
return Crypto.encrypt(data, __.derivedPassword);
|
|
63
96
|
}
|
|
@@ -70,10 +103,15 @@ module.exports = function () {
|
|
|
70
103
|
if (!this.secrez) {
|
|
71
104
|
throw new Error("Secrez not initiated");
|
|
72
105
|
}
|
|
73
|
-
|
|
106
|
+
const keyFile =
|
|
107
|
+
this.secrez.config.keysPathV4 &&
|
|
108
|
+
fs.existsSync(this.secrez.config.keysPathV4)
|
|
109
|
+
? this.secrez.config.keysPathV4
|
|
110
|
+
: this.secrez.config.keysPath;
|
|
111
|
+
if (!fs.existsSync(keyFile)) {
|
|
74
112
|
throw new Error("Account not set yet");
|
|
75
113
|
}
|
|
76
|
-
conf = JSON.parse(fs.readFileSync(
|
|
114
|
+
conf = JSON.parse(fs.readFileSync(keyFile, "utf8"));
|
|
77
115
|
}
|
|
78
116
|
if (encryptedData === __.encryptedMasterKey) {
|
|
79
117
|
throw new Error("Forbidden");
|
|
@@ -119,6 +157,12 @@ module.exports = function () {
|
|
|
119
157
|
}
|
|
120
158
|
|
|
121
159
|
async verifyPassword(password) {
|
|
160
|
+
if (this.conf.data.version === 4) {
|
|
161
|
+
return (
|
|
162
|
+
__.derivedPassword ===
|
|
163
|
+
_Secrez.derivePasswordV4(password, this.conf.data.kdf)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
122
166
|
return (
|
|
123
167
|
__.derivedPassword ===
|
|
124
168
|
(await _Secrez.derivePassword(password, __.iterations))
|
|
@@ -127,6 +171,23 @@ module.exports = function () {
|
|
|
127
171
|
|
|
128
172
|
async changePassword(password = __.password, iterations = __.iterations) {
|
|
129
173
|
let data = this.conf.data;
|
|
174
|
+
if (data.version === 4) {
|
|
175
|
+
if (iterations != null) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
"A version 4 key does not use an iteration count. Change the password instead."
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const kdf = Object.assign({}, data.kdf, {
|
|
181
|
+
salt: Crypto.bs64.encode(Crypto.randomBytes(16)),
|
|
182
|
+
});
|
|
183
|
+
__.password = _Secrez.normalizePassword(password);
|
|
184
|
+
__.kdf = kdf;
|
|
185
|
+
__.derivedPassword = _Secrez.derivePasswordV4(__.password, kdf);
|
|
186
|
+
data.kdf = kdf;
|
|
187
|
+
delete data.keys;
|
|
188
|
+
data.key = this.preEncrypt(__.masterKey);
|
|
189
|
+
return data;
|
|
190
|
+
}
|
|
130
191
|
__.password = password;
|
|
131
192
|
__.iterations = iterations;
|
|
132
193
|
__.derivedPassword = await _Secrez.derivePassword(password, iterations);
|
|
@@ -199,6 +260,9 @@ module.exports = function () {
|
|
|
199
260
|
__.encryptedBoxPrivateKey = data.box.secretKey;
|
|
200
261
|
__.encryptedSignPrivateKey = data.sign.secretKey;
|
|
201
262
|
} catch (e) {
|
|
263
|
+
if (data.version === 4) {
|
|
264
|
+
throw new Error("Wrong password");
|
|
265
|
+
}
|
|
202
266
|
throw new Error("Wrong password or wrong number of iterations");
|
|
203
267
|
}
|
|
204
268
|
if (utils.secureCompare(Crypto.b64Hash(__.masterKey), data.hash)) {
|
|
@@ -208,12 +272,32 @@ module.exports = function () {
|
|
|
208
272
|
}
|
|
209
273
|
}
|
|
210
274
|
|
|
275
|
+
static normalizePassword(password) {
|
|
276
|
+
if (typeof password !== "string" || password.length === 0) {
|
|
277
|
+
throw new Error("Not a valid string");
|
|
278
|
+
}
|
|
279
|
+
return password.normalize("NFKC");
|
|
280
|
+
}
|
|
281
|
+
|
|
211
282
|
static async derivePassword(password = __.password, iterations) {
|
|
212
283
|
password = Crypto.SHA3(password);
|
|
213
284
|
let salt = Crypto.SHA3(password + iterations.toString());
|
|
214
285
|
return bs64.encode(Crypto.deriveKey(password, salt, iterations, 32));
|
|
215
286
|
}
|
|
216
287
|
|
|
288
|
+
static derivePasswordV4(password, kdf) {
|
|
289
|
+
const normalized = _Secrez.normalizePassword(password);
|
|
290
|
+
const salt = Crypto.bs64.decode(kdf.salt);
|
|
291
|
+
return bs64.encode(
|
|
292
|
+
Crypto.scryptKey(normalized, salt, {
|
|
293
|
+
N: kdf.N,
|
|
294
|
+
r: kdf.r,
|
|
295
|
+
p: kdf.p,
|
|
296
|
+
keyLen: kdf.keyLen || 32,
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
217
301
|
signData(data) {
|
|
218
302
|
const signature = this.signMessage(JSON.stringify(this.sortObj(data)));
|
|
219
303
|
const conf = {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Public scrypt cost for version 4 account keys.
|
|
2
|
+
// N = 2^17, r = 8 uses about 128 MiB and is the same on every computer.
|
|
3
|
+
// Tests use a smaller N so the suite stays fast. The value used is stored
|
|
4
|
+
// in the key file, and signin always follows the file.
|
|
5
|
+
const PROD_N = 1 << 17;
|
|
6
|
+
const TEST_N = 1024;
|
|
7
|
+
|
|
8
|
+
function accountScryptParams() {
|
|
9
|
+
return {
|
|
10
|
+
name: "scrypt",
|
|
11
|
+
N: process.env.NODE_ENV === "test" ? TEST_N : PROD_N,
|
|
12
|
+
r: 8,
|
|
13
|
+
p: 1,
|
|
14
|
+
keyLen: 32,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function newAccountKdf() {
|
|
19
|
+
const Crypto = require("@secrez/crypto");
|
|
20
|
+
const params = accountScryptParams();
|
|
21
|
+
return {
|
|
22
|
+
name: params.name,
|
|
23
|
+
salt: Crypto.bs64.encode(Crypto.randomBytes(16)),
|
|
24
|
+
N: params.N,
|
|
25
|
+
r: params.r,
|
|
26
|
+
p: params.p,
|
|
27
|
+
keyLen: params.keyLen,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
PROD_N,
|
|
33
|
+
accountScryptParams,
|
|
34
|
+
newAccountKdf,
|
|
35
|
+
};
|
|
@@ -21,6 +21,7 @@ class ConfigUtils {
|
|
|
21
21
|
config.localWorkingDir = localWorkingDir;
|
|
22
22
|
config.keysDataPath = path.join(container, "keys");
|
|
23
23
|
config.keysPath = path.join(config.keysDataPath, "default.json");
|
|
24
|
+
config.keysPathV4 = path.join(config.keysDataPath, "version4.json");
|
|
24
25
|
config.oldKeysPath = path.join(container, "keys.json");
|
|
25
26
|
config.localDataPath = path.join(container, "local");
|
|
26
27
|
config.tmpPath = path.join(container, "tmp");
|
|
@@ -47,7 +47,10 @@ class VaultPermissions {
|
|
|
47
47
|
let probePath = config.container;
|
|
48
48
|
let expectDir = true;
|
|
49
49
|
|
|
50
|
-
if (await fs.pathExists(config.
|
|
50
|
+
if (config.keysPathV4 && (await fs.pathExists(config.keysPathV4))) {
|
|
51
|
+
probePath = config.keysPathV4;
|
|
52
|
+
expectDir = false;
|
|
53
|
+
} else if (await fs.pathExists(config.keysPath)) {
|
|
51
54
|
probePath = config.keysPath;
|
|
52
55
|
expectDir = false;
|
|
53
56
|
} else if (await fs.pathExists(config.keysDataPath)) {
|