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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

use std::{fs, io};
use std::path::{PathBuf, Path};
use parking_lot::Mutex;
use {json, SafeAccount, Error};
use crypto::Keccak256;
use super::super::account::Crypto;
use super::{KeyDirectory, VaultKeyDirectory, VaultKey, SetKeyError};
use super::disk::{self, DiskDirectory, KeyFileManager};

/// Name of vault metadata file
pub const VAULT_FILE_NAME: &'static str = "vault.json";
/// Name of temporary vault metadata file
pub const VAULT_TEMP_FILE_NAME: &'static str = "vault_temp.json";

/// Vault directory implementation
pub type VaultDiskDirectory = DiskDirectory<VaultKeyFileManager>;

/// Vault key file manager
pub struct VaultKeyFileManager {
	name: String,
	key: VaultKey,
	meta: Mutex<String>,
}

impl VaultDiskDirectory {
	/// Create new vault directory with given key
	pub fn create<P>(root: P, name: &str, key: VaultKey) -> Result<Self, Error> where P: AsRef<Path> {
		// check that vault directory does not exists
		let vault_dir_path = make_vault_dir_path(root, name, true)?;
		if vault_dir_path.exists() {
			return Err(Error::CreationFailed);
		}

		// create vault && vault file
		let vault_meta = "{}";
		fs::create_dir_all(&vault_dir_path)?;
		if let Err(err) = create_vault_file(&vault_dir_path, &key, vault_meta) {
			let _ = fs::remove_dir_all(&vault_dir_path); // can't do anything with this
			return Err(err);
		}

		Ok(DiskDirectory::new(vault_dir_path, VaultKeyFileManager::new(name, key, vault_meta)))
	}

	/// Open existing vault directory with given key
	pub fn at<P>(root: P, name: &str, key: VaultKey) -> Result<Self, Error> where P: AsRef<Path> {
		// check that vault directory exists
		let vault_dir_path = make_vault_dir_path(root, name, true)?;
		if !vault_dir_path.is_dir() {
			return Err(Error::CreationFailed);
		}

		// check that passed key matches vault file
		let meta = read_vault_file(&vault_dir_path, Some(&key))?;

		Ok(DiskDirectory::new(vault_dir_path, VaultKeyFileManager::new(name, key, &meta)))
	}

	/// Read vault meta without actually opening the vault
	pub fn meta_at<P>(root: P, name: &str) -> Result<String, Error> where P: AsRef<Path> {
		// check that vault directory exists
		let vault_dir_path = make_vault_dir_path(root, name, true)?;
		if !vault_dir_path.is_dir() {
			return Err(Error::VaultNotFound);
		}

		// check that passed key matches vault file
		read_vault_file(&vault_dir_path, None)
	}

	fn create_temp_vault(&self, key: VaultKey) -> Result<VaultDiskDirectory, Error> {
		let original_path = self.path().expect("self is instance of DiskDirectory; DiskDirectory always returns path; qed");
		let mut path: PathBuf = original_path.clone();
		let name = self.name();

		path.push(name); // to jump to the next level

		let mut index = 0;
		loop {
			let name = format!("{}_temp_{}", name, index);
			path.set_file_name(&name);
			if !path.exists() {
				return VaultDiskDirectory::create(original_path, &name, key);
			}

			index += 1;
		}
	}

	fn copy_to_vault(&self, vault: &VaultDiskDirectory) -> Result<(), Error> {
		for account in self.load()? {
			let filename = account.filename.clone().expect("self is instance of DiskDirectory; DiskDirectory fills filename in load; qed");
			vault.insert_with_filename(account, filename, true)?;
		}

		Ok(())
	}

	fn delete(&self) -> Result<(), Error> {
		let path = self.path().expect("self is instance of DiskDirectory; DiskDirectory always returns path; qed");
		fs::remove_dir_all(path).map_err(Into::into)
	}
}

impl VaultKeyDirectory for VaultDiskDirectory {
	fn as_key_directory(&self) -> &KeyDirectory {
		self
	}

	fn name(&self) -> &str {
		&self.key_manager().name
	}

	fn key(&self) -> VaultKey {
		self.key_manager().key.clone()
	}

	fn set_key(&self, new_key: VaultKey) -> Result<(), SetKeyError> {
		let temp_vault = VaultDiskDirectory::create_temp_vault(self, new_key.clone()).map_err(|err| SetKeyError::NonFatalOld(err))?;
		let mut source_path = temp_vault.path().expect("temp_vault is instance of DiskDirectory; DiskDirectory always returns path; qed").clone();
		let mut target_path = self.path().expect("self is instance of DiskDirectory; DiskDirectory always returns path; qed").clone();

		// preserve meta
		temp_vault.set_meta(&self.meta()).map_err(SetKeyError::NonFatalOld)?;

		// jump to next fs level
		source_path.push("next");
		target_path.push("next");

		let temp_accounts = self.copy_to_vault(&temp_vault)
			.and_then(|_| temp_vault.load())
			.map_err(|err| {
				// ignore error, as we already processing error
				let _ = temp_vault.delete();
				SetKeyError::NonFatalOld(err)
			})?;

		// we can't just delete temp vault until all files moved, because
		// original vault content has already been partially replaced
		// => when error or crash happens here, we can't do anything
		for temp_account in temp_accounts {
			let filename = temp_account.filename.expect("self is instance of DiskDirectory; DiskDirectory fills filename in load; qed");
			source_path.set_file_name(&filename);
			target_path.set_file_name(&filename);
			fs::rename(&source_path, &target_path).map_err(|err| SetKeyError::Fatal(err.into()))?;
		}
		source_path.set_file_name(VAULT_FILE_NAME);
		target_path.set_file_name(VAULT_FILE_NAME);
		fs::rename(source_path, target_path).map_err(|err| SetKeyError::Fatal(err.into()))?;

		temp_vault.delete().map_err(|err| SetKeyError::NonFatalNew(err))
	}

	fn meta(&self) -> String {
		self.key_manager().meta.lock().clone()
	}

	fn set_meta(&self, meta: &str) -> Result<(), Error> {
		let key_manager = self.key_manager();
		let vault_path = self.path().expect("self is instance of DiskDirectory; DiskDirectory always returns path; qed");
		create_vault_file(vault_path, &key_manager.key, meta)?;
		*key_manager.meta.lock() = meta.to_owned();
		Ok(())
	}
}

impl VaultKeyFileManager {
	pub fn new(name: &str, key: VaultKey, meta: &str) -> Self {
		VaultKeyFileManager {
			name: name.into(),
			key: key,
			meta: Mutex::new(meta.to_owned()),
		}
	}
}

impl KeyFileManager for VaultKeyFileManager {
	fn read<T>(&self, filename: Option<String>, reader: T) -> Result<SafeAccount, Error> where T: io::Read {
		let vault_file = json::VaultKeyFile::load(reader).map_err(|e| Error::Custom(format!("{:?}", e)))?;
		let mut safe_account = SafeAccount::from_vault_file(&self.key.password, vault_file, filename.clone())?;

		safe_account.meta = json::insert_vault_name_to_json_meta(&safe_account.meta, &self.name)
			.map_err(|err| Error::Custom(format!("{:?}", err)))?;
		Ok(safe_account)
	}

	fn write<T>(&self, mut account: SafeAccount, writer: &mut T) -> Result<(), Error> where T: io::Write {
		account.meta = json::remove_vault_name_from_json_meta(&account.meta)
			.map_err(|err| Error::Custom(format!("{:?}", err)))?;

		let vault_file: json::VaultKeyFile = account.into_vault_file(self.key.iterations, &self.key.password)?;
		vault_file.write(writer).map_err(|e| Error::Custom(format!("{:?}", e)))
	}
}

/// Makes path to vault directory, checking that vault name is appropriate
fn make_vault_dir_path<P>(root: P, name: &str, check_name: bool) -> Result<PathBuf, Error> where P: AsRef<Path> {
	// check vault name
	if check_name && !check_vault_name(name) {
		return Err(Error::InvalidVaultName);
	}

	let mut vault_dir_path: PathBuf = root.as_ref().into();
	vault_dir_path.push(name);
	Ok(vault_dir_path)
}

/// Every vault must have unique name => we rely on filesystem to check this
/// => vault name must not contain any fs-special characters to avoid directory traversal
/// => we only allow alphanumeric + separator characters in vault name.
fn check_vault_name(name: &str) -> bool {
	!name.is_empty()
	&& name.chars()
		.all(|c| c.is_alphanumeric()
			|| c.is_whitespace()
			|| c == '-' || c == '_')
}

/// Vault can be empty, but still must be pluggable => we store vault password in separate file
fn create_vault_file<P>(vault_dir_path: P, key: &VaultKey, meta: &str) -> Result<(), Error> where P: AsRef<Path> {
	let password_hash = key.password.as_bytes().keccak256();
	let crypto = Crypto::with_plain(&password_hash, &key.password, key.iterations)?;

	let vault_file_path = vault_dir_path.as_ref().join(VAULT_FILE_NAME);
	let temp_vault_file_name = disk::find_unique_filename_using_random_suffix(vault_dir_path.as_ref(), &VAULT_TEMP_FILE_NAME)?;
	let temp_vault_file_path = vault_dir_path.as_ref().join(&temp_vault_file_name);

	// this method is used to rewrite existing vault file
	// => write to temporary file first, then rename temporary file to vault file
	let mut vault_file = disk::create_new_file_with_permissions_to_owner(&temp_vault_file_path)?;
	let vault_file_contents = json::VaultFile {
		crypto: crypto.into(),
		meta: Some(meta.to_owned()),
	};
	vault_file_contents.write(&mut vault_file).map_err(|e| Error::Custom(format!("{:?}", e)))?;
	drop(vault_file);
	fs::rename(&temp_vault_file_path, &vault_file_path)?;

	Ok(())
}

/// When vault is opened => we must check that password matches && read metadata
fn read_vault_file<P>(vault_dir_path: P, key: Option<&VaultKey>) -> Result<String, Error> where P: AsRef<Path> {
	let mut vault_file_path: PathBuf = vault_dir_path.as_ref().into();
	vault_file_path.push(VAULT_FILE_NAME);

	let vault_file = fs::File::open(vault_file_path)?;
	let vault_file_contents = json::VaultFile::load(vault_file).map_err(|e| Error::Custom(format!("{:?}", e)))?;
	let vault_file_meta = vault_file_contents.meta.unwrap_or("{}".to_owned());
	let vault_file_crypto: Crypto = vault_file_contents.crypto.into();

	if let Some(key) = key {
		let password_bytes = vault_file_crypto.decrypt(&key.password)?;
		let password_hash = key.password.as_bytes().keccak256();
		if password_hash != password_bytes.as_slice() {
			return Err(Error::InvalidPassword);
		}
	}

	Ok(vault_file_meta)
}

#[cfg(test)]
mod test {
	extern crate tempdir;

	use std::fs;
	use std::io::Write;
	use std::path::PathBuf;
	use super::VaultKey;
	use super::{VAULT_FILE_NAME, check_vault_name, make_vault_dir_path, create_vault_file, read_vault_file, VaultDiskDirectory};
	use self::tempdir::TempDir;

	#[test]
	fn check_vault_name_succeeds() {
		assert!(check_vault_name("vault"));
		assert!(check_vault_name("vault with spaces"));
		assert!(check_vault_name("vault	with	tabs"));
		assert!(check_vault_name("vault_with_underscores"));
		assert!(check_vault_name("vault-with-dashes"));
		assert!(check_vault_name("vault-with-digits-123"));
		assert!(check_vault_name("vault中文名字"));
	}

	#[test]
	fn check_vault_name_fails() {
		assert!(!check_vault_name(""));
		assert!(!check_vault_name("."));
		assert!(!check_vault_name("*"));
		assert!(!check_vault_name("../.bash_history"));
		assert!(!check_vault_name("/etc/passwd"));
		assert!(!check_vault_name("c:\\windows"));
	}

	#[test]
	fn make_vault_dir_path_succeeds() {
		assert_eq!(make_vault_dir_path("/home/user/parity", "vault", true).unwrap().to_str().unwrap(), "/home/user/parity/vault");
		assert_eq!(make_vault_dir_path("/home/user/parity", "*bad-name*", false).unwrap().to_str().unwrap(), "/home/user/parity/*bad-name*");
	}

	#[test]
	fn make_vault_dir_path_fails() {
		assert!(make_vault_dir_path("/home/user/parity", "*bad-name*", true).is_err());
	}

	#[test]
	fn create_vault_file_succeeds() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password".into(), 1024);
		let mut vault_dir: PathBuf = temp_path.path().into();
		vault_dir.push("vault");
		fs::create_dir_all(&vault_dir).unwrap();

		// when
		let result = create_vault_file(&vault_dir, &key, "{}");

		// then
		assert!(result.is_ok());
		let mut vault_file_path = vault_dir.clone();
		vault_file_path.push(VAULT_FILE_NAME);
		assert!(vault_file_path.exists() && vault_file_path.is_file());
	}

	#[test]
	fn read_vault_file_succeeds() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password".into(), 1024);
		let vault_file_contents = r#"{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"758696c8dc6378ab9b25bb42790da2f5"},"ciphertext":"54eb50683717d41caaeb12ea969f2c159daada5907383f26f327606a37dc7168","kdf":"pbkdf2","kdfparams":{"c":1024,"dklen":32,"prf":"hmac-sha256","salt":"3c320fa566a1a7963ac8df68a19548d27c8f40bf92ef87c84594dcd5bbc402b6"},"mac":"9e5c2314c2a0781962db85611417c614bd6756666b6b1e93840f5b6ed895f003"}}"#;
		let dir: PathBuf = temp_path.path().into();
		let mut vault_file_path: PathBuf = dir.clone();
		vault_file_path.push(VAULT_FILE_NAME);
		{
			let mut vault_file = fs::File::create(vault_file_path).unwrap();
			vault_file.write_all(vault_file_contents.as_bytes()).unwrap();
		}

		// when
		let result = read_vault_file(&dir, Some(&key));

		// then
		assert!(result.is_ok());
	}

	#[test]
	fn read_vault_file_fails() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password1".into(), 1024);
		let dir: PathBuf = temp_path.path().into();
		let mut vault_file_path: PathBuf = dir.clone();
		vault_file_path.push(VAULT_FILE_NAME);

		// when
		let result = read_vault_file(&dir, Some(&key));

		// then
		assert!(result.is_err());

		// and when given
		let vault_file_contents = r#"{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"0155e3690be19fbfbecabcd440aa284b"},"ciphertext":"4d6938a1f49b7782","kdf":"pbkdf2","kdfparams":{"c":1024,"dklen":32,"prf":"hmac-sha256","salt":"b6a9338a7ccd39288a86dba73bfecd9101b4f3db9c9830e7c76afdbd4f6872e5"},"mac":"16381463ea11c6eb2239a9f339c2e780516d29d234ce30ac5f166f9080b5a262"}}"#;
		{
			let mut vault_file = fs::File::create(vault_file_path).unwrap();
			vault_file.write_all(vault_file_contents.as_bytes()).unwrap();
		}

		// when
		let result = read_vault_file(&dir, Some(&key));

		// then
		assert!(result.is_err());
	}

	#[test]
	fn vault_directory_can_be_created() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password".into(), 1024);
		let dir: PathBuf = temp_path.path().into();

		// when
		let vault = VaultDiskDirectory::create(&dir, "vault", key.clone());

		// then
		assert!(vault.is_ok());

		// and when
		let vault = VaultDiskDirectory::at(&dir, "vault", key);

		// then
		assert!(vault.is_ok());
	}

	#[test]
	fn vault_directory_cannot_be_created_if_already_exists() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password".into(), 1024);
		let dir: PathBuf = temp_path.path().into();
		let mut vault_dir = dir.clone();
		vault_dir.push("vault");
		fs::create_dir_all(&vault_dir).unwrap();

		// when
		let vault = VaultDiskDirectory::create(&dir, "vault", key);

		// then
		assert!(vault.is_err());
	}

	#[test]
	fn vault_directory_cannot_be_opened_if_not_exists() {
		// given
		let temp_path = TempDir::new("").unwrap();
		let key = VaultKey::new(&"password".into(), 1024);
		let dir: PathBuf = temp_path.path().into();

		// when
		let vault = VaultDiskDirectory::at(&dir, "vault", key);

		// then
		assert!(vault.is_err());
	}
}