|
| 1 | +// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | + |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +use libparsec_platform_device_loader::{AvailableDevice, DeviceSaveStrategy}; |
| 6 | +use libparsec_types::{ |
| 7 | + anyhow::{self, Context}, |
| 8 | + LocalDevice, PKILocalPendingEnrollment, PkiEnrollmentAnswerPayload, PrivateParts, SecretKey, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Debug, thiserror::Error)] |
| 12 | +pub enum PkiEnrollmentFinalizeError { |
| 13 | + #[error("Failed to save device: {0}")] |
| 14 | + SaveError(#[from] libparsec_platform_device_loader::SaveDeviceError), |
| 15 | + #[error(transparent)] |
| 16 | + Internal(#[from] anyhow::Error), |
| 17 | +} |
| 18 | + |
| 19 | +/// Generates and saves the new device |
| 20 | +pub async fn finalize( |
| 21 | + config_dir: &Path, |
| 22 | + key_file: PathBuf, |
| 23 | + strategy: &DeviceSaveStrategy, |
| 24 | + accepted: PkiEnrollmentAnswerPayload, |
| 25 | + local_pending: PKILocalPendingEnrollment, |
| 26 | +) -> Result<AvailableDevice, PkiEnrollmentFinalizeError> { |
| 27 | + let PKILocalPendingEnrollment { |
| 28 | + cert_ref, |
| 29 | + addr, |
| 30 | + encrypted_key, |
| 31 | + encrypted_key_algo, |
| 32 | + ciphertext, |
| 33 | + .. |
| 34 | + } = local_pending; |
| 35 | + |
| 36 | + let key = |
| 37 | + libparsec_platform_pki::decrypt_message(encrypted_key_algo, &encrypted_key, &cert_ref) |
| 38 | + .map(|v| v.data) |
| 39 | + .context("Cannot decrypt key") |
| 40 | + .and_then(|raw| SecretKey::try_from(raw.as_ref()).context("Invalid key"))?; |
| 41 | + let raw_private_parts = key |
| 42 | + .decrypt(&ciphertext) |
| 43 | + .context("Cannot decrypt local pending")?; |
| 44 | + let PrivateParts { |
| 45 | + private_key, |
| 46 | + signing_key, |
| 47 | + } = PrivateParts::load(&raw_private_parts).context("Cannot load local pending")?; |
| 48 | + |
| 49 | + let PkiEnrollmentAnswerPayload { |
| 50 | + user_id, |
| 51 | + device_id, |
| 52 | + device_label, |
| 53 | + human_handle, |
| 54 | + profile, |
| 55 | + root_verify_key, |
| 56 | + } = accepted; |
| 57 | + let organization_addr = addr.generate_organization_addr(root_verify_key); |
| 58 | + let time_provider = None; |
| 59 | + let user_realm_id = None; |
| 60 | + let user_realm_key = None; |
| 61 | + |
| 62 | + let device = LocalDevice::generate_new_device( |
| 63 | + organization_addr, |
| 64 | + profile, |
| 65 | + human_handle, |
| 66 | + device_label, |
| 67 | + Some(user_id), |
| 68 | + Some(device_id), |
| 69 | + Some(signing_key), |
| 70 | + Some(private_key), |
| 71 | + time_provider, |
| 72 | + user_realm_id, |
| 73 | + user_realm_key, |
| 74 | + ); |
| 75 | + |
| 76 | + let available_device = |
| 77 | + libparsec_platform_device_loader::save_device(config_dir, strategy, &device, key_file) |
| 78 | + .await?; |
| 79 | + |
| 80 | + Ok(available_device) |
| 81 | +} |
0 commit comments