| 1 |
use std::{ |
| 2 |
time::Duration, |
| 3 |
}; |
| 4 |
|
| 5 |
use anyhow::Result; |
| 6 |
|
| 7 |
use crate::Sq; |
| 8 |
use crate::common::NULL_POLICY; |
| 9 |
|
| 10 |
use crate::cli::pki::link; |
| 11 |
use crate::cli::types::Expiration; |
| 12 |
use crate::cli::types::TrustAmount; |
| 13 |
use crate::sq::TrustThreshold; |
| 14 |
|
| 15 |
mod list; |
| 16 |
|
| 17 |
pub fn link(sq: Sq, c: link::Command) -> Result<()> { |
| 18 |
use link::Subcommands::*; |
| 19 |
match c.subcommand { |
| 20 |
Add(c) => add(sq, c)?, |
| 21 |
Authorize(c) => authorize(sq, c)?, |
| 22 |
Retract(c) => retract(sq, c)?, |
| 23 |
List(c) => list::list(sq, c)?, |
| 24 |
} |
| 25 |
Ok(()) |
| 26 |
} |
| 27 |
|
| 28 |
pub fn add(sq: Sq, c: link::AddCommand) |
| 29 |
-> Result<()> |
| 30 |
{ |
| 31 |
let trust_root = sq.local_trust_root()?; |
| 32 |
let trust_root = trust_root.to_cert()?; |
| 33 |
|
| 34 |
let (cert, _source) |
| 35 |
= sq.resolve_cert(&c.cert, TrustThreshold::Full)?; |
| 36 |
|
| 37 |
let vc = cert.with_policy(sq.policy, Some(sq.time))?; |
| 38 |
let userids = c.userids.resolve(&vc)?; |
| 39 |
|
| 40 |
let notations = c.signature_notations.parse()?; |
| 41 |
|
| 42 |
let templates: Vec<(TrustAmount<_>, Expiration)> = if c.temporary { |
| 43 |
// Make the partially trusted link one second younger. When |
| 44 |
// the fully trusted link expired, then this link will come |
| 45 |
// into effect. If the user has fully linked the binding in |
| 46 |
// the meantime, then this won't override that, which is |
| 47 |
// exactly what we want. |
| 48 |
let week = Duration::new(7 * 24 * 60 * 60, 0); |
| 49 |
|
| 50 |
vec![ |
| 51 |
(TrustAmount::Other(40), c.expiration.value()), |
| 52 |
(c.amount, Expiration::from_duration(week)), |
| 53 |
] |
| 54 |
} else { |
| 55 |
vec![ |
| 56 |
(c.amount, c.expiration.value()), |
| 57 |
] |
| 58 |
}; |
| 59 |
|
| 60 |
crate::common::pki::certify::certify( |
| 61 |
&mut std::io::stdout(), |
| 62 |
&sq, |
| 63 |
c.recreate, // Recreate. |
| 64 |
&trust_root, |
| 65 |
&cert, |
| 66 |
&userids[..], |
| 67 |
! c.userids.all().unwrap_or(false), // User-supplied user IDs. |
| 68 |
&templates, |
| 69 |
0, // Trust depth. |
| 70 |
&[][..], // Domain. |
| 71 |
&[][..], // Regex. |
| 72 |
true, // Local. |
| 73 |
false, // Non-revocable. |
| 74 |
¬ations[..], |
| 75 |
None, // Output. |
| 76 |
false) // Binary. |
| 77 |
} |
| 78 |
|
| 79 |
pub fn authorize(sq: Sq, c: link::AuthorizeCommand) |
| 80 |
-> Result<()> |
| 81 |
{ |
| 82 |
let trust_root = sq.local_trust_root()?; |
| 83 |
let trust_root = trust_root.to_cert()?; |
| 84 |
|
| 85 |
let (cert, _source) |
| 86 |
= sq.resolve_cert(&c.cert, TrustThreshold::Full)?; |
| 87 |
|
| 88 |
let vc = cert.with_policy(sq.policy, Some(sq.time))?; |
| 89 |
let userids = c.userids.resolve(&vc)?; |
| 90 |
|
| 91 |
let notations = c.signature_notations.parse()?; |
| 92 |
|
| 93 |
crate::common::pki::certify::certify( |
| 94 |
&mut std::io::stdout(), |
| 95 |
&sq, |
| 96 |
c.recreate, // Recreate. |
| 97 |
&trust_root, |
| 98 |
&cert, |
| 99 |
&userids[..], |
| 100 |
! c.userids.all().unwrap_or(false), // User-supplied user IDs. |
| 101 |
&[(c.amount, c.expiration.value())][..], |
| 102 |
c.depth, |
| 103 |
&c.domain[..], |
| 104 |
&c.regex[..], |
| 105 |
true, // Local. |
| 106 |
false, // Non-revocable. |
| 107 |
¬ations[..], |
| 108 |
None, // Output. |
| 109 |
false) // Binary. |
| 110 |
} |
| 111 |
|
| 112 |
pub fn retract(sq: Sq, c: link::RetractCommand) |
| 113 |
-> Result<()> |
| 114 |
{ |
| 115 |
let trust_root = sq.local_trust_root()?; |
| 116 |
let trust_root = trust_root.to_cert()?; |
| 117 |
|
| 118 |
let (cert, _source) |
| 119 |
= sq.resolve_cert(&c.cert, TrustThreshold::Full)?; |
| 120 |
|
| 121 |
let vc = cert.with_policy(NULL_POLICY, Some(sq.time))?; |
| 122 |
let userids = c.userids.resolve(&vc)?; |
| 123 |
|
| 124 |
let notations = c.signature_notations.parse()?; |
| 125 |
|
| 126 |
crate::common::pki::certify::certify( |
| 127 |
&mut std::io::stdout(), |
| 128 |
&sq, |
| 129 |
c.recreate, // Recreate. |
| 130 |
&trust_root, |
| 131 |
&cert, |
| 132 |
&userids[..], |
| 133 |
! c.userids.all().unwrap_or(false), // User-supplied user IDs. |
| 134 |
&[(TrustAmount::None, Expiration::Never)], |
| 135 |
0, |
| 136 |
&[][..], &[][..], // Domain, regex. |
| 137 |
true, // Local. |
| 138 |
false, // Non-revocable. |
| 139 |
¬ations[..], |
| 140 |
None, // Output. |
| 141 |
false) // Binary. |
| 142 |
} |