vta-service 0.10.0

Service for Verifiable Trust Agents operating in Verifiable Trust Communities
Documentation
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, info};

use crate::error::{AppError, tee_attestation_error};

use super::provider::{StructuralCheckOutcome, TeeProvider};
use super::types::{AttestationReport, TeeStatus, TeeType};

/// AWS Nitro Enclaves attestation provider.
///
/// Uses the Nitro Secure Module (NSM) device at `/dev/nsm` to generate
/// attestation documents signed by the AWS Nitro Attestation PKI.
///
/// # Attestation Document
///
/// The evidence field contains a COSE_Sign1 structure (RFC 8152) with:
/// - Protected header: algorithm ES384 (ECDSA P-384 with SHA-384)
/// - Payload: CBOR map with PCRs, module_id, timestamp, nonce, user_data, public_key
/// - Signature: signed by the enclave's attestation certificate
///
/// # Verification
///
/// Remote parties verify by:
/// 1. Parsing the COSE_Sign1 document
/// 2. Extracting the certificate chain from the payload
/// 3. Validating the chain against the AWS Nitro root certificate
///    (available at https://aws-nitro-enclaves.amazonaws.com/AWS_NitroEnclaves_Root-G1.zip)
/// 4. Verifying the COSE_Sign1 signature using the leaf certificate
/// 5. Checking PCR values match expected enclave image measurements
/// 6. Validating nonce and user_data fields
pub struct NitroProvider;

impl TeeProvider for NitroProvider {
    fn tee_type(&self) -> TeeType {
        TeeType::Nitro
    }

    fn detect(&self) -> Result<TeeStatus, AppError> {
        let detected = std::path::Path::new("/dev/nsm").exists();

        if detected {
            info!("Nitro Secure Module device detected");
        }

        Ok(TeeStatus {
            tee_type: TeeType::Nitro,
            detected,
            platform_version: if detected {
                Some("nitro-enclave".into())
            } else {
                None
            },
        })
    }

    fn attest(&self, user_data: &[u8], nonce: &[u8]) -> Result<AttestationReport, AppError> {
        debug!(
            user_data_len = user_data.len(),
            nonce_len = nonce.len(),
            "requesting Nitro attestation document"
        );

        let evidence = request_nsm_attestation(user_data, nonce)?;

        debug!(
            evidence_len = evidence.len(),
            "Nitro attestation document generated"
        );

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Ok(AttestationReport {
            tee_type: TeeType::Nitro,
            evidence: BASE64.encode(&evidence),
            nonce: hex::encode(nonce),
            generated_at: now,
            vta_did: None,
        })
    }

    fn smoke_check_structure(
        &self,
        report: &AttestationReport,
    ) -> Result<StructuralCheckOutcome, AppError> {
        if report.tee_type != TeeType::Nitro {
            return Ok(StructuralCheckOutcome::Malformed);
        }

        let evidence = BASE64
            .decode(&report.evidence)
            .map_err(|e| tee_attestation_error(format!("invalid evidence encoding: {e}")))?;

        if evidence.is_empty() {
            return Ok(StructuralCheckOutcome::Malformed);
        }

        // Nitro attestation documents are CBOR-encoded COSE_Sign1 structures.
        // COSE_Sign1 is CBOR tag 18, which encodes as:
        //   - 0xD8 0x12 (2-byte tag for tag number 18)
        // Some implementations may also use the untagged form starting with
        // CBOR array(4): 0x84
        let first_byte = evidence[0];
        let valid_start = first_byte == 0xD8  // CBOR tag (2-byte form)
            || first_byte == 0x84; // CBOR array(4) — untagged COSE_Sign1

        if !valid_start {
            debug!(
                first_byte = format!("{first_byte:#04x}"),
                "unexpected first byte in attestation document"
            );
            return Ok(StructuralCheckOutcome::Malformed);
        }

        // Full cryptographic verification requires:
        // 1. Parse COSE_Sign1 structure
        // 2. Extract the certificate chain from the payload
        // 3. Validate chain against AWS Nitro root certificate
        // 4. Verify signature using the leaf certificate's public key
        // 5. Check PCR values match expected enclave measurements
        //
        // This smoke-check only validates the structural shape.
        debug!("Nitro attestation document structural smoke-check passed");
        Ok(StructuralCheckOutcome::StructurallyValid)
    }
}

// ---------------------------------------------------------------------------
// NSM device interface
// ---------------------------------------------------------------------------

/// Request an attestation document from the Nitro Secure Module.
///
/// The NSM device (`/dev/nsm`) uses an ioctl-based interface. The request
/// and response are CBOR-encoded messages passed via a file descriptor
/// obtained from the device.
///
/// Request: `{ "Attestation": { "user_data": <bytes>, "nonce": <bytes>, "public_key": null } }`
/// Response: `{ "Attestation": { "document": <bytes> } }` (the COSE_Sign1 document)
fn request_nsm_attestation(user_data: &[u8], nonce: &[u8]) -> Result<Vec<u8>, AppError> {
    // Open the NSM device
    let fd = open_nsm_device()?;

    // Build the CBOR-encoded attestation request
    let request = build_nsm_request(user_data, nonce, None);

    // Send request and receive response via ioctl
    let response = nsm_ioctl(fd.as_raw_fd(), &request)?;

    // Parse the CBOR response to extract the attestation document
    extract_attestation_document(&response)
}

/// Request an NSM attestation document with an embedded public key.
///
/// Used by KMS bootstrap to bind an ephemeral RSA public key to the
/// attestation document. KMS uses this to re-encrypt the response so
/// only the enclave (holding the private key) can decrypt it.
///
/// `public_key_der` must be the RSA public key in DER-encoded SPKI format.
pub(crate) fn request_nsm_attestation_for_kms(public_key_der: &[u8]) -> Result<Vec<u8>, AppError> {
    let fd = open_nsm_device()?;
    let request = build_nsm_request(&[], &[], Some(public_key_der));
    let response = nsm_ioctl(fd.as_raw_fd(), &request)?;
    extract_attestation_document(&response)
}

/// RAII wrapper for the NSM file descriptor.
struct NsmFd(std::os::unix::io::RawFd);

impl NsmFd {
    fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
        self.0
    }
}

impl Drop for NsmFd {
    fn drop(&mut self) {
        unsafe {
            libc::close(self.0);
        }
    }
}

fn open_nsm_device() -> Result<NsmFd, AppError> {
    use std::ffi::CString;

    let path = CString::new("/dev/nsm").unwrap();
    let fd = unsafe { libc::open(path.as_ptr(), libc::O_RDWR) };
    if fd < 0 {
        let err = std::io::Error::last_os_error();
        return Err(tee_attestation_error(format!(
            "failed to open /dev/nsm: {err}"
        )));
    }
    Ok(NsmFd(fd))
}

/// Send a request to the NSM device and receive the response.
///
/// The NSM ioctl uses a simple request/response buffer protocol:
/// - Input:  pointer to request CBOR buffer + length
/// - Output: response written into a provided buffer
fn nsm_ioctl(fd: std::os::unix::io::RawFd, request: &[u8]) -> Result<Vec<u8>, AppError> {
    // NSM ioctl request structure (from aws-nitro-enclaves-nsm-api)
    #[repr(C)]
    struct NsmMessage {
        request: NsmIoBuffer,
        response: NsmIoBuffer,
    }

    #[repr(C)]
    struct NsmIoBuffer {
        addr: u64,
        len: u32,
    }

    // NSM_IOCTL_REQUEST: _IOWR(0x0A, 0, struct nsm_message)
    const NSM_IOCTL_REQUEST: libc::c_ulong = 0xC020_0A00;

    // Allocate response buffer (NSM documents are typically 2-5 KB)
    let mut response_buf = vec![0u8; 16384];

    let mut msg = NsmMessage {
        request: NsmIoBuffer {
            addr: request.as_ptr() as u64,
            len: request.len() as u32,
        },
        response: NsmIoBuffer {
            addr: response_buf.as_mut_ptr() as u64,
            len: response_buf.len() as u32,
        },
    };

    let ret = unsafe { libc::ioctl(fd, NSM_IOCTL_REQUEST, &mut msg as *mut NsmMessage) };

    if ret != 0 {
        let err = std::io::Error::last_os_error();
        return Err(tee_attestation_error(format!("NSM ioctl failed: {err}")));
    }

    let actual_len = msg.response.len as usize;
    if actual_len == 0 {
        return Err(tee_attestation_error("empty response from NSM device"));
    }

    response_buf.truncate(actual_len);
    Ok(response_buf)
}

// ---------------------------------------------------------------------------
// CBOR encoding/decoding helpers
// ---------------------------------------------------------------------------

/// Build a CBOR-encoded NSM attestation request.
///
/// When `public_key` is `None`, the request encodes `public_key: null`.
/// When `Some(der_bytes)`, the public key DER is embedded in the attestation
/// document — used by KMS to re-encrypt the response to that key.
///
/// ```text
/// { "Attestation": { "user_data": <bytes>, "nonce": <bytes>, "public_key": <bytes|null> } }
/// ```
fn build_nsm_request(user_data: &[u8], nonce: &[u8], public_key: Option<&[u8]>) -> Vec<u8> {
    let mut buf = Vec::with_capacity(64 + user_data.len() + nonce.len());

    // Map(1)
    buf.push(0xA1);
    // Text "Attestation" (11 bytes)
    encode_cbor_text(&mut buf, b"Attestation");
    // Map(3)
    buf.push(0xA3);
    // "user_data" => bytes
    encode_cbor_text(&mut buf, b"user_data");
    encode_cbor_bytes(&mut buf, user_data);
    // "nonce" => bytes
    encode_cbor_text(&mut buf, b"nonce");
    encode_cbor_bytes(&mut buf, nonce);
    // "public_key" => bytes or null
    encode_cbor_text(&mut buf, b"public_key");
    match public_key {
        Some(pk) => encode_cbor_bytes(&mut buf, pk),
        None => buf.push(0xF6), // CBOR null
    }

    buf
}

/// Extract the attestation document from an NSM CBOR response.
///
/// The response has the shape:
/// ```text
/// { "Attestation": { "document": <bytes> } }
/// ```
///
/// We do a simple scan for the "document" key and extract the following
/// byte string. This avoids a full CBOR parser dependency.
fn extract_attestation_document(response: &[u8]) -> Result<Vec<u8>, AppError> {
    // Look for the "document" text key in the CBOR response
    let marker = b"document";

    // Scan for the marker in the response bytes
    let pos = response
        .windows(marker.len())
        .position(|w| w == marker)
        .ok_or_else(|| tee_attestation_error("NSM response does not contain 'document' field"))?;

    // The byte string follows immediately after the "document" text
    let after_key = pos + marker.len();
    if after_key >= response.len() {
        return Err(tee_attestation_error(
            "NSM response truncated after 'document' key",
        ));
    }

    // Decode the CBOR byte string that follows
    decode_cbor_bytes(&response[after_key..])
}

fn encode_cbor_text(buf: &mut Vec<u8>, text: &[u8]) {
    let len = text.len();
    if len < 24 {
        buf.push(0x60 | len as u8);
    } else if len < 256 {
        buf.push(0x78);
        buf.push(len as u8);
    } else {
        buf.push(0x79);
        buf.push((len >> 8) as u8);
        buf.push(len as u8);
    }
    buf.extend_from_slice(text);
}

fn encode_cbor_bytes(buf: &mut Vec<u8>, data: &[u8]) {
    let len = data.len();
    if len < 24 {
        buf.push(0x40 | len as u8);
    } else if len < 256 {
        buf.push(0x58);
        buf.push(len as u8);
    } else {
        buf.push(0x59);
        buf.push((len >> 8) as u8);
        buf.push(len as u8);
    }
    buf.extend_from_slice(data);
}

/// Decode a CBOR byte string at the start of the given slice.
fn decode_cbor_bytes(data: &[u8]) -> Result<Vec<u8>, AppError> {
    if data.is_empty() {
        return Err(tee_attestation_error("unexpected end of CBOR data"));
    }

    let major = data[0] >> 5;
    if major != 2 {
        return Err(tee_attestation_error(format!(
            "expected CBOR byte string (major type 2), got major type {major}"
        )));
    }

    let additional = data[0] & 0x1F;
    let (len, offset) = if additional < 24 {
        (additional as usize, 1)
    } else if additional == 24 {
        if data.len() < 2 {
            return Err(tee_attestation_error("truncated CBOR length"));
        }
        (data[1] as usize, 2)
    } else if additional == 25 {
        if data.len() < 3 {
            return Err(tee_attestation_error("truncated CBOR length"));
        }
        (((data[1] as usize) << 8) | data[2] as usize, 3)
    } else if additional == 26 {
        if data.len() < 5 {
            return Err(tee_attestation_error("truncated CBOR length"));
        }
        (
            ((data[1] as usize) << 24)
                | ((data[2] as usize) << 16)
                | ((data[3] as usize) << 8)
                | data[4] as usize,
            5,
        )
    } else {
        return Err(tee_attestation_error(format!(
            "unsupported CBOR additional info: {additional}"
        )));
    };

    if data.len() < offset + len {
        return Err(tee_attestation_error(format!(
            "CBOR byte string length {len} exceeds available data"
        )));
    }

    Ok(data[offset..offset + len].to_vec())
}