forked from Dashlane/dashlane-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychainManager.ts
226 lines (195 loc) Β· 7.45 KB
/
keychainManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { Database } from 'better-sqlite3';
import keytar from 'keytar';
import winston from 'winston';
import crypto from 'crypto';
import { decrypt, getDerivateUsingParametersFromEncryptedData } from './decrypt';
import { encryptAES } from './encrypt';
import { sha512 } from './hash';
import { EncryptedData } from './types';
import { CLI_VERSION, cliVersionToString } from '../cliVersion';
import { registerDevice } from '../middleware/registerDevice';
import { DeviceConfiguration, DeviceKeys, Secrets } from '../types';
import { askEmailAddress, askMasterPassword } from '../utils/dialogs';
const SERVICE = 'dashlane-cli';
export const setLocalKey = async (
login: string,
shouldNotSaveMasterPassword: boolean,
localKey?: Buffer
): Promise<Buffer> => {
if (!localKey) {
localKey = crypto.randomBytes(32);
if (!localKey) {
throw new Error('Unable to generate AES local key');
}
}
if (!shouldNotSaveMasterPassword) {
await keytar.setPassword(SERVICE, login, localKey.toString('base64'));
}
return localKey;
};
const getLocalKey = async (login: string): Promise<Buffer | undefined> => {
const localKeyEncoded = await keytar.getPassword(SERVICE, login);
if (localKeyEncoded) {
return Buffer.from(localKeyEncoded, 'base64');
} else {
return undefined;
}
};
export const deleteLocalKey = (login: string): Promise<boolean> => {
return keytar.deletePassword(SERVICE, login);
};
/**
* Fake transaction used to set derivation parameters to encrypt the local key in the DB using the master password
*/
const getDerivationParametersForLocalKey = (login: string): EncryptedData => {
return {
keyDerivation: {
algo: 'argon2d',
saltLength: 16,
tCost: 3,
mCost: 32768,
parallelism: 2,
},
cipherConfig: {
encryption: 'aes256', // Unused parameter
cipherMode: 'cbchmac', // Unused parameter
ivLength: 0, // Unused parameter
},
cipherData: {
salt: sha512(login).slice(0, 16),
iv: Buffer.from(''), // Unused parameter
hash: Buffer.from(''), // Unused parameter
encryptedPayload: Buffer.from(''), // Unused parameter
},
};
};
const getSecretsWithoutDB = async (
db: Database,
login: string,
shouldNotSaveMasterPassword: boolean
): Promise<Secrets> => {
let localKey: Buffer;
try {
localKey = await setLocalKey(login, shouldNotSaveMasterPassword);
} catch (error) {
let errorMessage = 'unknown error';
if (error instanceof Error) {
errorMessage = error.message;
}
winston.debug(`Unable to reach OS keychain: ${errorMessage}`);
throw new Error(
'Your OS keychain is probably unreachable. Install it or disable its usage via `dcli configure save-master-password false`'
);
}
const { deviceAccessKey, deviceSecretKey } = await registerDevice({ login });
const masterPassword = await askMasterPassword();
const derivate = await getDerivateUsingParametersFromEncryptedData(
masterPassword,
getDerivationParametersForLocalKey(login)
);
const deviceSecretKeyEncrypted = encryptAES(localKey, Buffer.from(deviceSecretKey, 'hex'));
const masterPasswordEncrypted = encryptAES(localKey, Buffer.from(masterPassword));
const localKeyEncrypted = encryptAES(derivate, localKey);
db.prepare('REPLACE INTO device VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
.bind(
login,
cliVersionToString(CLI_VERSION),
deviceAccessKey,
deviceSecretKeyEncrypted,
shouldNotSaveMasterPassword ? null : masterPasswordEncrypted,
shouldNotSaveMasterPassword ? 1 : 0,
localKeyEncrypted,
1
)
.run();
return {
login,
masterPassword,
shouldNotSaveMasterPassword,
localKey,
accessKey: deviceAccessKey,
secretKey: deviceSecretKey,
};
};
const getSecretsWithoutKeychain = async (login: string, deviceKeys: DeviceKeys): Promise<Secrets> => {
const masterPassword = await askMasterPassword();
const derivate = await getDerivateUsingParametersFromEncryptedData(
masterPassword,
getDerivationParametersForLocalKey(login)
);
const localKey = await decrypt(deviceKeys.localKeyEncrypted, { type: 'alreadyComputed', symmetricKey: derivate });
const secretKey = (
await decrypt(deviceKeys.secretKeyEncrypted, { type: 'alreadyComputed', symmetricKey: localKey })
).toString('hex');
await setLocalKey(login, deviceKeys.shouldNotSaveMasterPassword, localKey);
return {
login,
masterPassword,
shouldNotSaveMasterPassword: deviceKeys.shouldNotSaveMasterPassword,
localKey,
accessKey: deviceKeys.accessKey,
secretKey,
};
};
export const replaceMasterPassword = async (db: Database, secrets: Secrets): Promise<Secrets> => {
const { localKey, login, accessKey, secretKey, shouldNotSaveMasterPassword } = secrets;
const newMasterPassword = await askMasterPassword();
const derivate = await getDerivateUsingParametersFromEncryptedData(
newMasterPassword,
getDerivationParametersForLocalKey(login)
);
const masterPasswordEncrypted = encryptAES(secrets.localKey, Buffer.from(newMasterPassword));
const localKeyEncrypted = encryptAES(derivate, localKey);
db.prepare('UPDATE device SET localKeyEncrypted = ?, masterPasswordEncrypted = ? WHERE login = ?')
.bind(localKeyEncrypted, shouldNotSaveMasterPassword ? null : masterPasswordEncrypted, login)
.run();
return {
login,
masterPassword: newMasterPassword,
shouldNotSaveMasterPassword,
localKey,
accessKey,
secretKey,
};
};
export const getSecrets = async (
db: Database,
deviceConfiguration: DeviceConfiguration | null,
shouldNotSaveMasterPasswordIfNoDeviceKeys = false
): Promise<Secrets> => {
let login: string;
if (deviceConfiguration) {
login = deviceConfiguration.login;
} else {
login = await askEmailAddress();
}
// If there are no secrets in the DB
if (!deviceConfiguration) {
return getSecretsWithoutDB(db, login, shouldNotSaveMasterPasswordIfNoDeviceKeys);
}
let localKey: Buffer | undefined = undefined;
// If the master password is not saved or if the keychain is unreachable, or empty, the local key is retrieved from
// the master password from the DB
if (
deviceConfiguration.shouldNotSaveMasterPassword ||
!deviceConfiguration.masterPasswordEncrypted ||
!(localKey = await getLocalKey(login))
) {
return getSecretsWithoutKeychain(login, deviceConfiguration);
}
// Otherwise, the local key can be used to decrypt the device secret key and the master password in the DB
const masterPassword = (
await decrypt(deviceConfiguration.masterPasswordEncrypted, { type: 'alreadyComputed', symmetricKey: localKey })
).toString();
const secretKey = (
await decrypt(deviceConfiguration.secretKeyEncrypted, { type: 'alreadyComputed', symmetricKey: localKey })
).toString('hex');
return {
login,
masterPassword,
shouldNotSaveMasterPassword: deviceConfiguration.shouldNotSaveMasterPassword,
localKey,
accessKey: deviceConfiguration.accessKey,
secretKey,
};
};