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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
use std::ops::Deref; use std::time; use crate::{ HashAlgorithm, Result, SignatureType, }; use crate::types::{ ReasonForRevocation, }; use crate::crypto::hash::Hash; use crate::crypto::Signer; use crate::packet::{ Key, key, signature, Signature, UserAttribute, UserID, }; use crate::cert::prelude::*; /// A `Cert` revocation builder. /// /// Note: a Cert revocation has two degrees of freedom: the Cert, and /// the key used to generate the revocation. /// /// Normally, the key used to generate the revocation is the Cert's /// primary key. However, this is not required. /// /// If Alice has marked Robert's key (R) as a designated revoker /// for her key (A), then R can revoke A or parts of A. In this /// case, the Cert is A, and the key used to generate the /// revocation comes from R. /// /// # Example /// /// ```rust /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; /// use openpgp::types::{ReasonForRevocation, RevocationStatus, SignatureType}; /// use openpgp::cert::prelude::*; /// use openpgp::crypto::KeyPair; /// use openpgp::parse::Parse; /// use sequoia_openpgp::policy::StandardPolicy; /// /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> /// # { /// let p = &StandardPolicy::new(); /// /// let (cert, _) = CertBuilder::new() /// .set_cipher_suite(CipherSuite::Cv25519) /// .generate()?; /// assert_eq!(RevocationStatus::NotAsFarAsWeKnow, /// cert.revoked(p, None)); /// /// let mut signer = cert.primary_key().key().clone() /// .parts_into_secret()?.into_keypair()?; /// let sig = CertRevocationBuilder::new() /// .set_reason_for_revocation(ReasonForRevocation::KeyCompromised, /// b"It was the maid :/")? /// .build(&mut signer, &cert, None)?; /// assert_eq!(sig.typ(), SignatureType::KeyRevocation); /// /// let cert = cert.merge_packets(vec![sig.clone().into()])?; /// assert_eq!(RevocationStatus::Revoked(vec![&sig]), /// cert.revoked(p, None)); /// # Ok(()) /// # } pub struct CertRevocationBuilder { builder: signature::Builder, } impl CertRevocationBuilder { /// Returns a new `CertRevocationBuilder`. pub fn new() -> Self { Self { builder: signature::Builder::new(SignatureType::KeyRevocation) } } /// Sets the reason for revocation. pub fn set_reason_for_revocation(self, code: ReasonForRevocation, reason: &[u8]) -> Result<Self> { Ok(Self { builder: self.builder.set_reason_for_revocation(code, reason)? }) } /// Sets the revocation signature's creation time. pub fn set_signature_creation_time(self, creation_time: time::SystemTime) -> Result<Self> { Ok(Self { builder: self.builder.set_signature_creation_time(creation_time)? }) } /// Returns a revocation certificate for the cert `Cert` signed by /// `signer`. pub fn build<H>(self, signer: &mut dyn Signer, cert: &Cert, hash_algo: H) -> Result<Signature> where H: Into<Option<HashAlgorithm>> { let hash_algo = hash_algo.into().unwrap_or(HashAlgorithm::SHA512); let mut hash = hash_algo.context()?; cert.primary_key().hash(&mut hash); let creation_time = self.signature_creation_time() .unwrap_or_else(|| time::SystemTime::now()); self.builder // If not set, set it to now. .set_signature_creation_time(creation_time)? .set_issuer_fingerprint(signer.public().fingerprint())? .set_issuer(signer.public().keyid())? .sign_hash(signer, hash) } } impl Deref for CertRevocationBuilder { type Target = signature::Builder; fn deref(&self) -> &Self::Target { &self.builder } } /// A `Subkey` revocation builder. /// /// Note: this function has three degrees of freedom: the Cert, the /// key used to generate the revocation, and the subkey. /// /// Normally, the key used to generate the revocation is the Cert's /// primary key, and the subkey is a subkey that is bound to the /// Cert. However, this is not required. /// /// If Alice has marked Robert's key (R) as a designated revoker /// for her key (A), then R can revoke A or parts of A. In this /// case, the Cert is A, the key used to generate the revocation /// comes from R, and the User ID is bound to A. /// /// But, the component doesn't technically need to be bound to the /// Cert. For instance, it is possible for R to revoke the User ID /// "bob@example.org" in the context of A, even if /// "bob@example.org" is not bound to A. /// /// # Example /// /// ``` /// # use sequoia_openpgp::{*, packet::*, types::*, cert::*}; /// use sequoia_openpgp::policy::StandardPolicy; /// /// # f().unwrap(); /// # fn f() -> Result<()> { /// let p = &StandardPolicy::new(); /// /// // Generate a Cert, and create a keypair from the primary key. /// let (cert, _) = CertBuilder::new() /// .add_transport_encryption_subkey() /// .generate()?; /// let mut keypair = cert.primary_key().key().clone() /// .parts_into_secret()?.into_keypair()?; /// let subkey = cert.keys().subkeys().nth(0).unwrap(); /// /// // Generate the revocation for the first and only Subkey. /// let revocation = /// SubkeyRevocationBuilder::new() /// .set_reason_for_revocation( /// ReasonForRevocation::KeyRetired, /// b"Smells funny.").unwrap() /// .build(&mut keypair, &cert, subkey.key(), None)?; /// assert_eq!(revocation.typ(), SignatureType::SubkeyRevocation); /// /// // Now merge the revocation signature into the Cert. /// let cert = cert.merge_packets(vec![revocation.clone().into()])?; /// /// // Check that it is revoked. /// let subkey = cert.keys().subkeys().nth(0).unwrap(); /// if let RevocationStatus::Revoked(revocations) = subkey.revoked(p, None) { /// assert_eq!(revocations.len(), 1); /// assert_eq!(*revocations[0], revocation); /// } else { /// panic!("Subkey is not revoked."); /// } /// # Ok(()) } /// ``` pub struct SubkeyRevocationBuilder { builder: signature::Builder, } impl SubkeyRevocationBuilder { /// Returns a new `SubkeyRevocationBuilder`. pub fn new() -> Self { Self { builder: signature::Builder::new(SignatureType::SubkeyRevocation) } } /// Sets the reason for revocation. pub fn set_reason_for_revocation(self, code: ReasonForRevocation, reason: &[u8]) -> Result<Self> { Ok(Self { builder: self.builder.set_reason_for_revocation(code, reason)? }) } /// Sets the revocation signature's creation time. pub fn set_signature_creation_time(self, creation_time: time::SystemTime) -> Result<Self> { Ok(Self { builder: self.builder.set_signature_creation_time(creation_time)? }) } /// Returns a revocation certificate for the cert `Cert` signed by /// `signer`. pub fn build<H, P>(mut self, signer: &mut dyn Signer, cert: &Cert, key: &Key<P, key::SubordinateRole>, hash_algo: H) -> Result<Signature> where H: Into<Option<HashAlgorithm>>, P: key::KeyParts, { let hash_algo = hash_algo.into().unwrap_or(HashAlgorithm::SHA512); if let Some(algo) = hash_algo.into() { self.builder = self.builder.set_hash_algo(algo); } key.bind(signer, cert, self.builder) } } impl Deref for SubkeyRevocationBuilder { type Target = signature::Builder; fn deref(&self) -> &Self::Target { &self.builder } } /// A `UserID` revocation builder. /// /// Note: this function has three degrees of freedom: the Cert, the /// key used to generate the revocation, and the user id. /// /// Normally, the key used to generate the revocation is the Cert's /// primary key, and the user id is a user id that is bound to the /// Cert. However, this is not required. /// /// If Alice has marked Robert's key (R) as a designated revoker /// for her key (A), then R can revoke A or parts of A. In this /// case, the Cert is A, the key used to generate the revocation /// comes from R, and the User ID is bound to A. /// /// But, the component doesn't technically need to be bound to the /// Cert. For instance, it is possible for R to revoke the User ID /// "bob@example.org" in the context of A, even if /// "bob@example.org" is not bound to A. /// /// # Example /// /// ``` /// # use sequoia_openpgp::{*, packet::*, types::*}; /// use sequoia_openpgp::cert::prelude::*; /// use sequoia_openpgp::policy::StandardPolicy; /// /// # f().unwrap(); /// # fn f() -> Result<()> { /// let p = &StandardPolicy::new(); /// /// // Generate a Cert, and create a keypair from the primary key. /// let (cert, _) = CertBuilder::new() /// .add_userid("some@example.org") /// .generate()?; /// let mut keypair = cert.primary_key().key().clone() /// .parts_into_secret()?.into_keypair()?; /// let ca = cert.userids().nth(0).unwrap(); /// /// // Generate the revocation for the first and only UserID. /// let revocation = /// UserIDRevocationBuilder::new() /// .set_reason_for_revocation( /// ReasonForRevocation::KeyRetired, /// b"Left example.org.").unwrap() /// .build(&mut keypair, &cert, ca.userid(), None)?; /// assert_eq!(revocation.typ(), SignatureType::CertificationRevocation); /// /// // Now merge the revocation signature into the Cert. /// let cert = cert.merge_packets(vec![revocation.clone().into()])?; /// /// // Check that it is revoked. /// let userid = cert.userids().with_policy(p, None).nth(0).unwrap(); /// if let RevocationStatus::Revoked(revocations) = userid.revoked() { /// assert_eq!(revocations.len(), 1); /// assert_eq!(*revocations[0], revocation); /// } else { /// panic!("UserID is not revoked."); /// } /// # Ok(()) } /// ``` pub struct UserIDRevocationBuilder { builder: signature::Builder, } impl UserIDRevocationBuilder { /// Returns a new `UserIDRevocationBuilder`. pub fn new() -> Self { Self { builder: signature::Builder::new(SignatureType::CertificationRevocation) } } /// Sets the reason for revocation. pub fn set_reason_for_revocation(self, code: ReasonForRevocation, reason: &[u8]) -> Result<Self> { Ok(Self { builder: self.builder.set_reason_for_revocation(code, reason)? }) } /// Sets the revocation signature's creation time. pub fn set_signature_creation_time(self, creation_time: time::SystemTime) -> Result<Self> { Ok(Self { builder: self.builder.set_signature_creation_time(creation_time)? }) } /// Returns a revocation certificate for the cert `Cert` signed by /// `signer`. pub fn build<H>(mut self, signer: &mut dyn Signer, cert: &Cert, userid: &UserID, hash_algo: H) -> Result<Signature> where H: Into<Option<HashAlgorithm>> { let hash_algo = hash_algo.into().unwrap_or(HashAlgorithm::SHA512); if let Some(algo) = hash_algo.into() { self.builder = self.builder.set_hash_algo(algo); } userid.bind(signer, cert, self.builder) } } impl Deref for UserIDRevocationBuilder { type Target = signature::Builder; fn deref(&self) -> &Self::Target { &self.builder } } /// A `UserAttribute` revocation builder. /// /// Note: this function has three degrees of freedom: the Cert, the /// key used to generate the revocation, and the user attribute. /// /// Normally, the key used to generate the revocation is the Cert's /// primary key, and the user attribute is a user attribute that is /// bound to the Cert. However, this is not required. /// /// If Alice has marked Robert's key (R) as a designated revoker /// for her key (A), then R can revoke A or parts of A. In this /// case, the Cert is A, the key used to generate the revocation /// comes from R, and the User Attribute is bound to A. /// /// But, the component doesn't technically need to be bound to the /// Cert. For instance, it is possible for R to revoke the User ID /// "bob@example.org" in the context of A, even if /// "bob@example.org" is not bound to A. /// /// # Example /// /// ``` /// # use sequoia_openpgp::{*, packet::*, types::*}; /// use sequoia_openpgp::cert::prelude::*; /// use sequoia_openpgp::policy::StandardPolicy; /// /// # f().unwrap(); /// # fn f() -> Result<()> { /// let p = &StandardPolicy::new(); /// /// # let subpacket /// # = user_attribute::Subpacket::Unknown(1, [ 1 ].to_vec().into_boxed_slice()); /// # let some_user_attribute = UserAttribute::new(&[ subpacket ])?; /// // Generate a Cert, and create a keypair from the primary key. /// let (cert, _) = CertBuilder::new() /// .add_user_attribute(some_user_attribute) /// .generate()?; /// let mut keypair = cert.primary_key().key().clone() /// .parts_into_secret()?.into_keypair()?; /// let ca = cert.user_attributes().nth(0).unwrap(); /// /// // Generate the revocation for the first and only UserAttribute. /// let revocation = /// UserAttributeRevocationBuilder::new() /// .set_reason_for_revocation( /// ReasonForRevocation::KeyRetired, /// b"Left example.org.").unwrap() /// .build(&mut keypair, &cert, ca.user_attribute(), None)?; /// assert_eq!(revocation.typ(), SignatureType::CertificationRevocation); /// /// // Now merge the revocation signature into the Cert. /// let cert = cert.merge_packets(vec![revocation.clone().into()])?; /// /// // Check that it is revoked. /// let ua = cert.user_attributes().with_policy(p, None).nth(0).unwrap(); /// if let RevocationStatus::Revoked(revocations) = ua.revoked() { /// assert_eq!(revocations.len(), 1); /// assert_eq!(*revocations[0], revocation); /// } else { /// panic!("UserAttribute is not revoked."); /// } /// # Ok(()) } /// ``` pub struct UserAttributeRevocationBuilder { builder: signature::Builder, } impl UserAttributeRevocationBuilder { /// Returns a new `UserAttributeRevocationBuilder`. pub fn new() -> Self { Self { builder: signature::Builder::new(SignatureType::CertificationRevocation) } } /// Sets the reason for revocation. pub fn set_reason_for_revocation(self, code: ReasonForRevocation, reason: &[u8]) -> Result<Self> { Ok(Self { builder: self.builder.set_reason_for_revocation(code, reason)? }) } /// Sets the revocation signature's creation time. pub fn set_signature_creation_time(self, creation_time: time::SystemTime) -> Result<Self> { Ok(Self { builder: self.builder.set_signature_creation_time(creation_time)? }) } /// Returns a revocation certificate for the cert `Cert` signed by /// `signer`. pub fn build<H>(mut self, signer: &mut dyn Signer, cert: &Cert, ua: &UserAttribute, hash_algo: H) -> Result<Signature> where H: Into<Option<HashAlgorithm>> { let hash_algo = hash_algo.into().unwrap_or(HashAlgorithm::SHA512); if let Some(algo) = hash_algo.into() { self.builder = self.builder.set_hash_algo(algo); } ua.bind(signer, cert, self.builder) } } impl Deref for UserAttributeRevocationBuilder { type Target = signature::Builder; fn deref(&self) -> &Self::Target { &self.builder } }