|
| 1 | +// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | + |
| 3 | +use crate::ClientConfig; |
| 4 | +pub use anonymous_cmds::latest::pki_enrollment_info::PkiEnrollmentInfoStatus; |
| 5 | +use libparsec_client_connection::{protocol::anonymous_cmds, AnonymousCmds, ConnectionError}; |
| 6 | +use libparsec_platform_pki::{load_answer_payload, SignedMessage}; |
| 7 | +use libparsec_types::prelude::*; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +#[derive(Debug, thiserror::Error)] |
| 11 | +pub enum PkiEnrollmentInfoError { |
| 12 | + #[error("Cannot communicate with the server: {0}")] |
| 13 | + Offline(#[from] ConnectionError), |
| 14 | + #[error("No enrollment found with that id")] |
| 15 | + EnrollmentNotFound, |
| 16 | + #[error("Invalid accept payload")] |
| 17 | + InvalidAcceptPayload, |
| 18 | + #[error(transparent)] |
| 19 | + Internal(#[from] anyhow::Error), |
| 20 | +} |
| 21 | + |
| 22 | +pub enum PKIInfoItem { |
| 23 | + Accepted { |
| 24 | + // Serialized version of the provided payload |
| 25 | + // signature should have been checked before loading it |
| 26 | + answer: PkiEnrollmentAnswerPayload, |
| 27 | + accepted_on: DateTime, |
| 28 | + submitted_on: DateTime, |
| 29 | + }, |
| 30 | + Submitted { |
| 31 | + submitted_on: DateTime, |
| 32 | + }, |
| 33 | + Rejected { |
| 34 | + rejected_on: DateTime, |
| 35 | + submitted_on: DateTime, |
| 36 | + }, |
| 37 | + Cancelled { |
| 38 | + submitted_on: DateTime, |
| 39 | + cancelled_on: DateTime, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +pub async fn info( |
| 44 | + config: Arc<ClientConfig>, |
| 45 | + addr: ParsecPkiEnrollmentAddr, |
| 46 | + enrollment_id: PKIEnrollmentID, |
| 47 | +) -> Result<PKIInfoItem, PkiEnrollmentInfoError> { |
| 48 | + use anonymous_cmds::latest::pki_enrollment_info::{Rep, Req}; |
| 49 | + let cmds = AnonymousCmds::new( |
| 50 | + &config.config_dir, |
| 51 | + ParsecAnonymousAddr::ParsecPkiEnrollmentAddr(addr.clone()), |
| 52 | + config.proxy.clone(), |
| 53 | + )?; |
| 54 | + let rep = cmds.send(Req { enrollment_id }).await?; |
| 55 | + |
| 56 | + let status = match rep { |
| 57 | + Rep::Ok(status) => status, |
| 58 | + Rep::EnrollmentNotFound => return Err(PkiEnrollmentInfoError::EnrollmentNotFound), |
| 59 | + rep @ Rep::UnknownStatus { .. } => { |
| 60 | + return Err(anyhow::anyhow!("Unexpected server response: {:?}", rep).into()) |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Check that the payload is valid |
| 65 | + let answer = match status { |
| 66 | + PkiEnrollmentInfoStatus::Submitted { submitted_on } => { |
| 67 | + PKIInfoItem::Submitted { submitted_on } |
| 68 | + } |
| 69 | + PkiEnrollmentInfoStatus::Rejected { |
| 70 | + rejected_on, |
| 71 | + submitted_on, |
| 72 | + } => PKIInfoItem::Rejected { |
| 73 | + rejected_on, |
| 74 | + submitted_on, |
| 75 | + }, |
| 76 | + PkiEnrollmentInfoStatus::Cancelled { |
| 77 | + submitted_on, |
| 78 | + cancelled_on, |
| 79 | + } => PKIInfoItem::Cancelled { |
| 80 | + submitted_on, |
| 81 | + cancelled_on, |
| 82 | + }, |
| 83 | + PkiEnrollmentInfoStatus::Accepted { |
| 84 | + accept_payload, |
| 85 | + accept_payload_signature, |
| 86 | + accepted_on, |
| 87 | + accepter_der_x509_certificate, |
| 88 | + submitted_on, |
| 89 | + } => { |
| 90 | + let message = SignedMessage { |
| 91 | + algo: PkiSignatureAlgorithm::RsassaPssSha256, // TODO update after #https://github.com/Scille/parsec-cloud/pull/11604 |
| 92 | + signature: accept_payload_signature.to_vec(), |
| 93 | + message: accept_payload.to_vec(), |
| 94 | + }; |
| 95 | + let answer = load_answer_payload(&accepter_der_x509_certificate, &message, accepted_on) |
| 96 | + .map_err(|_| PkiEnrollmentInfoError::InvalidAcceptPayload)?; |
| 97 | + PKIInfoItem::Accepted { |
| 98 | + answer, |
| 99 | + accepted_on, |
| 100 | + submitted_on, |
| 101 | + } |
| 102 | + } |
| 103 | + }; |
| 104 | + Ok(answer) |
| 105 | +} |
0 commit comments