igvmfilegen/
identity_mapping.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Implements creation of JSON formatted launch measurement identity documents.
//! This comes from Intel's TD Identity Mapping, and has been expanded
//! for SNP and VBS.
//! This format allows verifiers to validate a COSE Sign1 payload,
//! and correlate that to a launch measurement and SVN.
use serde::Serialize;

/// Defined by specification.
const CLASS_ID_GUID: &str = "7fb00ee4-a7ff-11ed-9e2f-00155d09de56";

/// This field is required by TD mapping specification.
#[derive(Serialize, Debug)]
pub struct Environment {
    pub class_id: String,
}

/// SNP launch measurement.
#[derive(Serialize, Debug, Clone)]
pub struct SnpLaunchMeasurement {
    #[serde(
        serialize_with = "hex::serde::serialize_upper",
        deserialize_with = "hex::serde::deserialize"
    )]
    pub snp_ld: [u8; 48],
}

/// TDX MRTD.
#[derive(Serialize, Debug, Clone)]
pub struct TdxLaunchMeasurement {
    #[serde(
        serialize_with = "hex::serde::serialize_upper",
        deserialize_with = "hex::serde::deserialize"
    )]
    pub tdx_mrtd: [u8; 48],
}

/// VBS Boot Digest.
#[derive(Serialize, Debug, Clone)]
pub struct VbsLaunchMeasurement {
    #[serde(
        serialize_with = "hex::serde::serialize_upper",
        deserialize_with = "hex::serde::deserialize"
    )]
    pub vbs_boot_digest: [u8; 32],
}

/// Build information.
#[derive(Serialize, Debug, Clone)]
pub struct BuildInfo {
    pub debug_build: bool,
}

/// SVN of this image.
#[derive(Serialize, Debug, Clone)]
pub struct SnpEndorsement {
    pub snp_isvsvn: u32,
    pub build_info: BuildInfo,
}

/// SVN of this image.
#[derive(Serialize, Debug, Clone)]
pub struct TdxEndorsement {
    pub tdx_isvsvn: u32,
    pub build_info: BuildInfo,
}

/// SVN of this image.
#[derive(Serialize, Debug, Clone)]
pub struct VbsEndorsement {
    pub vbs_isvsvn: u32,
    pub build_info: BuildInfo,
}

#[derive(Serialize, Debug, Clone)]
pub struct MeasurementInstance<R, E> {
    pub reference: R,
    pub endorsement: E,
}

#[derive(Serialize, Debug)]
pub struct BaseMeasurement<R, E> {
    pub environment: Environment,
    pub series: Vec<MeasurementInstance<R, E>>,
}

/// Combined measurement structure.
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum Measurement {
    Snp(BaseMeasurement<SnpLaunchMeasurement, SnpEndorsement>),
    Tdx(BaseMeasurement<TdxLaunchMeasurement, TdxEndorsement>),
    Vbs(BaseMeasurement<VbsLaunchMeasurement, VbsEndorsement>),
}

pub type SnpMeasurement = BaseMeasurement<SnpLaunchMeasurement, SnpEndorsement>;
pub type TdxMeasurement = BaseMeasurement<TdxLaunchMeasurement, TdxEndorsement>;
pub type VbsMeasurement = BaseMeasurement<VbsLaunchMeasurement, VbsEndorsement>;

impl SnpMeasurement {
    /// SNP measurement and endorsements.
    pub fn new(ld: [u8; 48], svn: u32, debug_enabled: bool) -> Self {
        let info = BuildInfo {
            debug_build: debug_enabled,
        };
        let measurements: MeasurementInstance<_, _> = MeasurementInstance {
            reference: SnpLaunchMeasurement { snp_ld: ld },
            endorsement: SnpEndorsement {
                snp_isvsvn: svn,
                build_info: info,
            },
        };
        BaseMeasurement {
            environment: Environment {
                class_id: CLASS_ID_GUID.to_string(),
            },
            series: [measurements].to_vec(),
        }
    }
}
impl TdxMeasurement {
    /// TDX measurement and endorsements.
    pub fn new(mrtd: [u8; 48], svn: u32, debug_enabled: bool) -> Self {
        let info = BuildInfo {
            debug_build: debug_enabled,
        };
        let measurements: MeasurementInstance<_, _> = MeasurementInstance {
            reference: TdxLaunchMeasurement { tdx_mrtd: mrtd },
            endorsement: TdxEndorsement {
                tdx_isvsvn: svn,
                build_info: info,
            },
        };
        BaseMeasurement {
            environment: Environment {
                class_id: CLASS_ID_GUID.to_string(),
            },
            series: [measurements].to_vec(),
        }
    }
}

impl VbsMeasurement {
    /// VBS measurement and endorsements.
    pub fn new(digest: [u8; 32], svn: u32, debug_enabled: bool) -> Self {
        let info = BuildInfo {
            debug_build: debug_enabled,
        };
        let measurements: MeasurementInstance<_, _> = MeasurementInstance {
            reference: VbsLaunchMeasurement {
                vbs_boot_digest: digest,
            },
            endorsement: VbsEndorsement {
                vbs_isvsvn: svn,
                build_info: info,
            },
        };
        BaseMeasurement {
            environment: Environment {
                class_id: CLASS_ID_GUID.to_string(),
            },
            series: [measurements].to_vec(),
        }
    }
}