|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2025 Keylime Authors |
| 3 | +use crate::config; |
| 4 | +use crate::error::{Error, Result}; |
| 5 | +use base64::{engine::general_purpose, Engine as _}; |
| 6 | +use keylime::{ |
| 7 | + crypto::{self, x509::CertificateBuilder}, |
| 8 | + registrar_client::RegistrarClientBuilder, |
| 9 | + tpm::{self, IAKResult, IDevIDResult}, |
| 10 | +}; |
| 11 | +use log::{error, info}; |
| 12 | +use openssl::x509::X509; |
| 13 | +use tss_esapi::handles::KeyHandle; |
| 14 | +use tss_esapi::structures::PublicBuffer; |
| 15 | +use tss_esapi::traits::Marshall; |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct AgentActivation { |
| 19 | + pub ak: tpm::AKResult, |
| 20 | + pub ek_result: tpm::EKResult, |
| 21 | + pub api_versions: Vec<String>, |
| 22 | + pub agent: config::AgentConfig, |
| 23 | + pub agent_uuid: String, |
| 24 | + pub mtls_cert: Option<X509>, |
| 25 | + pub device_id: Option<keylime::device_id::DeviceID>, |
| 26 | + pub attest: Option<tss_esapi::structures::Attest>, |
| 27 | + pub signature: Option<tss_esapi::structures::Signature>, |
| 28 | + pub ak_handle: KeyHandle, |
| 29 | +} |
| 30 | + |
| 31 | +pub async fn activate_agent( |
| 32 | + mut aa: AgentActivation, |
| 33 | + mut ctx: &mut tpm::Context<'_>, |
| 34 | +) -> Result<()> { |
| 35 | + let iak_pub; |
| 36 | + let idevid_pub; |
| 37 | + let ak_pub = &PublicBuffer::try_from(aa.ak.public)?.marshall()?; |
| 38 | + let ek_pub = |
| 39 | + &PublicBuffer::try_from(aa.ek_result.public.clone())?.marshall()?; |
| 40 | + |
| 41 | + // Create a RegistrarClientBuilder and set the parameters |
| 42 | + let mut builder = RegistrarClientBuilder::new() |
| 43 | + .ak_pub(ak_pub) |
| 44 | + .ek_pub(ek_pub) |
| 45 | + .enabled_api_versions( |
| 46 | + aa.api_versions.iter().map(|ver| ver.as_ref()).collect(), |
| 47 | + ) |
| 48 | + .registrar_ip(aa.agent.registrar_ip.clone()) |
| 49 | + .registrar_port(aa.agent.registrar_port) |
| 50 | + .uuid(&aa.agent_uuid) |
| 51 | + .ip(aa.agent.contact_ip.clone()) |
| 52 | + .port(aa.agent.contact_port); |
| 53 | + |
| 54 | + if let Some(mtls_cert) = aa.mtls_cert { |
| 55 | + builder = builder.mtls_cert(mtls_cert); |
| 56 | + } |
| 57 | + |
| 58 | + // If the certificate is not None add it to the builder |
| 59 | + if let Some(ek_cert) = aa.ek_result.ek_cert { |
| 60 | + builder = builder.ek_cert(ek_cert); |
| 61 | + } |
| 62 | + |
| 63 | + // Set the IAK/IDevID related fields, if enabled |
| 64 | + if aa.agent.enable_iak_idevid { |
| 65 | + let (Some(dev_id), Some(attest), Some(signature)) = |
| 66 | + (&aa.device_id, aa.attest, aa.signature) |
| 67 | + else { |
| 68 | + error!("IDevID and IAK are enabled but could not be generated"); |
| 69 | + return Err(Error::Configuration( |
| 70 | + config::KeylimeConfigError::Generic( |
| 71 | + "IDevID and IAK are enabled but could not be generated" |
| 72 | + .to_string(), |
| 73 | + ), |
| 74 | + )); |
| 75 | + }; |
| 76 | + |
| 77 | + iak_pub = |
| 78 | + PublicBuffer::try_from(dev_id.iak_pubkey.clone())?.marshall()?; |
| 79 | + idevid_pub = PublicBuffer::try_from(dev_id.idevid_pubkey.clone())? |
| 80 | + .marshall()?; |
| 81 | + builder = builder |
| 82 | + .iak_attest(attest.marshall()?) |
| 83 | + .iak_sign(signature.marshall()?) |
| 84 | + .iak_pub(&iak_pub) |
| 85 | + .idevid_pub(&idevid_pub); |
| 86 | + |
| 87 | + // If the IAK certificate was provided, set it |
| 88 | + if let Some(iak_cert) = dev_id.iak_cert.clone() { |
| 89 | + builder = builder.iak_cert(iak_cert); |
| 90 | + } |
| 91 | + |
| 92 | + // If the IDevID certificate was provided, set it |
| 93 | + if let Some(idevid_cert) = dev_id.idevid_cert.clone() { |
| 94 | + builder = builder.idevid_cert(idevid_cert); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Build the registrar client |
| 99 | + let mut registrar_client = builder.build().await?; |
| 100 | + |
| 101 | + // Request keyblob material |
| 102 | + let keyblob = registrar_client.register_agent().await?; |
| 103 | + |
| 104 | + info!("SUCCESS: Agent {} registered", &aa.agent_uuid); |
| 105 | + |
| 106 | + let key = ctx.activate_credential( |
| 107 | + keyblob, |
| 108 | + aa.ak_handle, |
| 109 | + aa.ek_result.key_handle, |
| 110 | + )?; |
| 111 | + |
| 112 | + // Flush EK if we created it |
| 113 | + if aa.agent.ek_handle.is_empty() { |
| 114 | + ctx.flush_context(aa.ek_result.key_handle.into())?; |
| 115 | + } |
| 116 | + |
| 117 | + let mackey = general_purpose::STANDARD.encode(key.value()); |
| 118 | + let auth_tag = |
| 119 | + crypto::compute_hmac(mackey.as_bytes(), aa.agent_uuid.as_bytes())?; |
| 120 | + let auth_tag = hex::encode(&auth_tag); |
| 121 | + |
| 122 | + registrar_client.activate_agent(&auth_tag).await?; |
| 123 | + info!("SUCCESS: Agent {} activated", &aa.agent_uuid); |
| 124 | + Ok(()) |
| 125 | +} |
0 commit comments