use qos_nsm::types::NsmResponse;
use crate::protocol::{
services::{
boot::{Approval, ManifestEnvelope},
genesis::{GenesisOutput, GenesisSet},
},
ProtocolError,
};
#[derive(Debug, PartialEq, borsh::BorshSerialize, borsh::BorshDeserialize)]
pub enum ProtocolMsg {
ProtocolErrorResponse(ProtocolError),
StatusRequest,
StatusResponse(super::ProtocolPhase),
BootStandardRequest {
manifest_envelope: Box<ManifestEnvelope>,
pivot: Vec<u8>,
},
BootStandardResponse {
nsm_response: NsmResponse,
},
BootGenesisRequest {
set: GenesisSet,
dr_key: Option<Vec<u8>>,
},
BootGenesisResponse {
nsm_response: NsmResponse,
genesis_output: Box<GenesisOutput>,
},
ProvisionRequest {
share: Vec<u8>,
approval: Approval,
},
ProvisionResponse {
reconstructed: bool,
},
ProxyRequest {
data: Vec<u8>,
},
ProxyResponse {
data: Vec<u8>,
},
LiveAttestationDocRequest,
LiveAttestationDocResponse {
nsm_response: NsmResponse,
manifest_envelope: Option<Box<ManifestEnvelope>>,
},
BootKeyForwardRequest {
manifest_envelope: Box<ManifestEnvelope>,
pivot: Vec<u8>,
},
BootKeyForwardResponse {
nsm_response: NsmResponse,
},
ExportKeyRequest {
manifest_envelope: Box<ManifestEnvelope>,
cose_sign1_attestation_doc: Vec<u8>,
},
ExportKeyResponse {
encrypted_quorum_key: Vec<u8>,
signature: Vec<u8>,
},
InjectKeyRequest {
encrypted_quorum_key: Vec<u8>,
signature: Vec<u8>,
},
InjectKeyResponse,
ManifestEnvelopeRequest,
ManifestEnvelopeResponse {
manifest_envelope: Box<Option<ManifestEnvelope>>,
},
}
impl std::fmt::Display for ProtocolMsg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ProtocolErrorResponse(_) => {
write!(f, "ProtocolErrorResponse")
}
Self::StatusRequest => write!(f, "StatusRequest"),
Self::StatusResponse(_) => {
write!(f, "StatusResponse")
}
Self::BootStandardRequest { .. } => {
write!(f, "BootStandardRequest")
}
Self::BootStandardResponse { .. } => {
write!(f, "BootStandardResponse")
}
Self::BootGenesisRequest { .. } => {
write!(f, "BootGenesisRequest")
}
Self::BootGenesisResponse { .. } => {
write!(f, "BootGenesisResponse")
}
Self::ProvisionRequest { .. } => {
write!(f, "ProvisionRequest")
}
Self::ProvisionResponse { reconstructed } => {
write!(
f,
"ProvisionResponse{{ reconstructed: {reconstructed} }}"
)
}
Self::ProxyRequest { .. } => {
write!(f, "ProxyRequest")
}
Self::ProxyResponse { .. } => {
write!(f, "ProxyResponse")
}
Self::LiveAttestationDocRequest { .. } => {
write!(f, "LiveAttestationDocRequest")
}
Self::LiveAttestationDocResponse { .. } => {
write!(f, "LiveAttestationDocResponse")
}
Self::BootKeyForwardRequest { .. } => {
write!(f, "BootKeyForwardRequest")
}
Self::BootKeyForwardResponse { nsm_response } => match nsm_response
{
NsmResponse::Attestation { .. } => write!(
f,
"BootKeyForwardResponse {{ nsm_response: Attestation }}"
),
NsmResponse::Error(ecode) => write!(
f,
"BootKeyForwardResponse {{ nsm_response: Error({ecode:?}) }}"
),
_ => write!(
f,
"BootKeyForwardResponse {{ nsm_response: Other }}" ),
},
Self::ExportKeyRequest { .. } => {
write!(f, "ExportKeyRequest")
}
Self::ExportKeyResponse { .. } => {
write!(f, "ExportKeyResponse")
}
Self::InjectKeyRequest { .. } => {
write!(f, "InjectKeyRequest")
}
Self::InjectKeyResponse { .. } => {
write!(f, "InjectKeyResponse")
}
Self::ManifestEnvelopeRequest { .. } => {
write!(f, "ManifestEnvelopeRequest")
}
Self::ManifestEnvelopeResponse { .. } => {
write!(f, "ManifestEnvelopeResponse")
}
}
}
}
#[cfg(test)]
mod test {
use borsh::BorshDeserialize;
use super::*;
#[test]
fn boot_genesis_response_deserialize() {
let nsm_response = NsmResponse::LockPCR;
let vec = borsh::to_vec(&nsm_response).unwrap();
let test = NsmResponse::try_from_slice(&vec).unwrap();
assert_eq!(nsm_response, test);
let genesis_response = ProtocolMsg::BootGenesisResponse {
nsm_response,
genesis_output: Box::new(GenesisOutput {
quorum_key: vec![3, 2, 1],
member_outputs: vec![],
recovery_permutations: vec![],
threshold: 2,
dr_key_wrapped_quorum_key: None,
quorum_key_hash: [22; 64],
test_message_ciphertext: vec![],
test_message_signature: vec![],
test_message: vec![],
}),
};
let vec = borsh::to_vec(&genesis_response).unwrap();
let test = ProtocolMsg::try_from_slice(&vec).unwrap();
assert_eq!(test, genesis_response);
}
}