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
#[macro_use]
extern crate serde_derive;
use common_failures::prelude::*;
#[macro_use]
extern crate failure;
pub mod id;
pub mod local;
pub mod proof;
pub mod repo;
pub mod staging;
pub mod trustdb;
pub mod util;
pub use self::local::Local;
use crev_data::Digest;
use crev_data::Id;
use std::convert::AsRef;
use std::{
collections::HashSet,
fmt,
path::{Path, PathBuf},
};
pub trait ProofStore {
fn insert(&self, proof: &crev_data::proof::Proof) -> Result<()>;
fn proofs_iter(&self) -> Result<Box<dyn Iterator<Item = crev_data::proof::Proof>>>;
}
pub enum VerificationStatus {
Verified,
Unknown,
Flagged,
}
#[derive(Copy, Clone)]
pub enum TrustOrDistrust {
Trust,
Distrust,
}
impl TrustOrDistrust {
pub fn is_trust(self) -> bool {
if let TrustOrDistrust::Trust = self {
return true;
}
false
}
pub fn to_review(self) -> crev_data::Review {
use self::TrustOrDistrust::*;
match self {
Trust => crev_data::Review::new_positive(),
Distrust => crev_data::Review::new_negative(),
}
}
}
impl fmt::Display for VerificationStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VerificationStatus::Verified => f.pad("verified"),
VerificationStatus::Unknown => f.pad("unknown"),
VerificationStatus::Flagged => f.pad("flagged"),
}
}
}
pub fn dir_or_git_repo_verify<H1, H2>(
path: &Path,
ignore_list: &HashSet<PathBuf, H1>,
db: &trustdb::TrustDB,
trusted_set: &HashSet<Id, H2>,
) -> Result<crate::VerificationStatus>
where
H1: std::hash::BuildHasher + std::default::Default,
H2: std::hash::BuildHasher + std::default::Default,
{
let digest = if path.join(".git").exists() {
get_recursive_digest_for_git_dir(path, ignore_list)?
} else {
Digest::from_vec(crev_recursive_digest::get_recursive_digest_for_dir::<
crev_common::Blake2b256,
H1,
>(path, ignore_list)?)
};
Ok(db.verify_digest(&digest, trusted_set))
}
pub fn dir_verify<H1, H2>(
path: &Path,
ignore_list: &HashSet<PathBuf, H1>,
db: &trustdb::TrustDB,
trusted_set: &HashSet<Id, H2>,
) -> Result<crate::VerificationStatus>
where
H1: std::hash::BuildHasher + std::default::Default,
H2: std::hash::BuildHasher + std::default::Default,
{
let digest = Digest::from_vec(crev_recursive_digest::get_recursive_digest_for_dir::<
crev_common::Blake2b256,
H1,
>(path, ignore_list)?);
Ok(db.verify_digest(&digest, trusted_set))
}
pub fn get_dir_digest<H1>(path: &Path, ignore_list: &HashSet<PathBuf, H1>) -> Result<Digest>
where
H1: std::hash::BuildHasher + std::default::Default,
{
Ok(Digest::from_vec(
crev_recursive_digest::get_recursive_digest_for_dir::<crev_common::Blake2b256, H1>(
path,
ignore_list,
)?,
))
}
pub fn show_current_id() -> Result<()> {
let local = Local::auto_open()?;
let id = local.read_current_locked_id()?;
let id = id.to_pubid();
println!("{}", id.id);
Ok(())
}
pub fn get_recursive_digest_for_git_dir<H>(
root_path: &Path,
ignore_list: &HashSet<PathBuf, H>,
) -> Result<Digest>
where
H: std::hash::BuildHasher + std::default::Default,
{
let git_repo = git2::Repository::open(root_path)?;
let mut status_opts = git2::StatusOptions::new();
let mut paths = HashSet::default();
status_opts.include_unmodified(true);
status_opts.include_untracked(false);
for entry in git_repo.statuses(Some(&mut status_opts))?.iter() {
let entry_path = PathBuf::from(
entry
.path()
.ok_or_else(|| format_err!("Git entry without a path"))?,
);
if ignore_list.contains(&entry_path) {
continue;
};
paths.insert(entry_path);
}
Ok(Digest::from_vec(
crev_recursive_digest::get_recursive_digest_for_paths::<crev_common::Blake2b256, H>(
root_path, paths,
)?,
))
}
pub fn get_recursive_digest_for_paths<H>(
root_path: &Path,
paths: HashSet<PathBuf, H>,
) -> Result<Vec<u8>>
where
H: std::hash::BuildHasher,
{
Ok(crev_recursive_digest::get_recursive_digest_for_paths::<
crev_common::Blake2b256,
H,
>(root_path, paths)?)
}
pub fn get_recursive_digest_for_dir<H>(
root_path: &Path,
rel_path_ignore_list: &HashSet<PathBuf, H>,
) -> Result<Digest>
where
H: std::hash::BuildHasher,
{
Ok(Digest::from_vec(
crev_recursive_digest::get_recursive_digest_for_dir::<crev_common::Blake2b256, H>(
root_path,
rel_path_ignore_list,
)?,
))
}
pub fn generate_id(
url: Option<String>,
github_username: Option<String>,
use_https_push: bool,
) -> Result<()> {
let url = match (url, github_username) {
(Some(url), None) => url,
(None, Some(username)) => format!("https://github.com/{}/crev-proofs", username),
(Some(_), Some(_)) => bail!("Can't provide both username and url"),
(None, None) => bail!("Must provide github username or url"),
};
if !url.starts_with("https://") {
bail!("URL must start with 'https://");
}
let local = Local::auto_create_or_open()?;
local.clone_proof_dir_from_git(&url, use_https_push)?;
let id = crev_data::id::OwnId::generate(crev_data::Url::new_git(url.clone()));
eprintln!("CrevID will be protected by a passphrase.");
eprintln!("There's no way to recover your CrevID if you forget your passphrase.");
let passphrase = crev_common::read_new_passphrase()?;
let locked = id::LockedId::from_own_id(&id, &passphrase)?;
local.save_locked_id(&locked)?;
local.save_current_id(id.as_ref())?;
eprintln!("");
eprintln!("Your CrevID was created and will be printed below in an encrypted form.");
eprintln!("Make sure to back it up on another device, to prevent loosing it.");
eprintln!("");
println!("{}", locked);
local.init_readme_using_this_repo_file()?;
Ok(())
}
pub fn switch_id(id_str: &str) -> Result<()> {
let id: Id = Id::crevid_from_str(id_str)?;
let local = Local::auto_open()?;
local.save_current_id(&id)?;
Ok(())
}
pub fn list_own_ids() -> Result<()> {
let local = Local::auto_open()?;
for id in local.list_ids()? {
println!("{}", id);
}
Ok(())
}
#[cfg(test)]
mod tests;