|
| 1 | +// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | + |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use libparsec_platform_pki::{load_answer_payload, SignedMessage}; |
| 6 | +use libparsec_types::prelude::*; |
| 7 | + |
| 8 | +use libparsec_client_connection::{protocol::anonymous_cmds, AnonymousCmds, ConnectionError}; |
| 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 use anonymous_cmds::latest::pki_enrollment_info::PkiEnrollmentInfoStatus; |
| 23 | + |
| 24 | +use crate::ClientConfig; |
| 25 | + |
| 26 | +pub async fn info( |
| 27 | + config: Arc<ClientConfig>, |
| 28 | + addr: ParsecPkiEnrollmentAddr, |
| 29 | + enrollment_id: PKIEnrollmentID, |
| 30 | +) -> Result<(PkiEnrollmentInfoStatus, Option<PkiEnrollmentAnswerPayload>), PkiEnrollmentInfoError> { |
| 31 | + use anonymous_cmds::latest::pki_enrollment_info::{Rep, Req}; |
| 32 | + let cmds = AnonymousCmds::new( |
| 33 | + &config.config_dir, |
| 34 | + ParsecAnonymousAddr::ParsecPkiEnrollmentAddr(addr.clone()), |
| 35 | + config.proxy.clone(), |
| 36 | + )?; |
| 37 | + let rep = cmds.send(Req { enrollment_id }).await?; |
| 38 | + |
| 39 | + let status = match rep { |
| 40 | + Rep::Ok(status) => status, |
| 41 | + Rep::EnrollmentNotFound => return Err(PkiEnrollmentInfoError::EnrollmentNotFound), |
| 42 | + rep @ Rep::UnknownStatus { .. } => { |
| 43 | + return Err(anyhow::anyhow!("Unexpected server response: {:?}", rep).into()) |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // Check that the payload is valid |
| 48 | + let answer = match &status { |
| 49 | + PkiEnrollmentInfoStatus::Submitted { .. } |
| 50 | + | PkiEnrollmentInfoStatus::Rejected { .. } |
| 51 | + | PkiEnrollmentInfoStatus::Cancelled { .. } => None, // nothing to check |
| 52 | + PkiEnrollmentInfoStatus::Accepted { |
| 53 | + accept_payload, |
| 54 | + accept_payload_signature, |
| 55 | + accepted_on, |
| 56 | + accepter_der_x509_certificate, |
| 57 | + .. |
| 58 | + } => { |
| 59 | + let message = SignedMessage { |
| 60 | + algo: PkiSignatureAlgorithm::RsassaPssSha256, // TODO update after #https://github.com/Scille/parsec-cloud/pull/11604 |
| 61 | + signature: accept_payload_signature.to_vec(), |
| 62 | + message: accept_payload.to_vec(), |
| 63 | + }; |
| 64 | + Some( |
| 65 | + load_answer_payload(accepter_der_x509_certificate, &message, *accepted_on) |
| 66 | + .map_err(|_| PkiEnrollmentInfoError::InvalidAcceptPayload)?, |
| 67 | + ) |
| 68 | + } |
| 69 | + }; |
| 70 | + Ok((status, answer)) |
| 71 | +} |
0 commit comments