|
| 1 | +// |
| 2 | +// Copyright 2022 The Project Oak Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +//! Integration Test of Remote Attestation in UEFI. |
| 18 | +//! |
| 19 | +//! This tests that remote attestion works inside the UEFI app. While the test |
| 20 | +//! code is identical to (a subset of) the tests in the remote attestation crate |
| 21 | +//! they here utilize the qemu runner configured in the UEFI app. This means |
| 22 | +//! that test code actually compiled to a UEFI target, which changes the |
| 23 | +//! underlying implementation of the remote attestation crate. |
| 24 | +//! TODO(#2654): It would be preferable to remove the test here, and instead |
| 25 | +//! run the tests in the oak_remote_attestation crate itself for both standard |
| 26 | +//! and UEFI targets. Due to concerns related to the workspace this is presently |
| 27 | +//! not possible. Ref: https://github.com/project-oak/oak/issues/2654 |
| 28 | +
|
| 29 | +extern crate alloc; |
| 30 | + |
| 31 | +use alloc::{boxed::Box, sync::Arc}; |
| 32 | +use oak_remote_attestation::handshaker::{AttestationBehavior, ClientHandshaker, ServerHandshaker}; |
| 33 | + |
| 34 | +const TEE_MEASUREMENT: &str = "Test TEE measurement"; |
| 35 | +const DATA: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 36 | + |
| 37 | +fn create_handshakers() -> (ClientHandshaker, ServerHandshaker) { |
| 38 | + let bidirectional_attestation = |
| 39 | + AttestationBehavior::create_bidirectional_attestation(&[], TEE_MEASUREMENT.as_bytes()) |
| 40 | + .unwrap(); |
| 41 | + let client_handshaker = ClientHandshaker::new( |
| 42 | + bidirectional_attestation, |
| 43 | + Box::new(|server_identity| { |
| 44 | + if !server_identity.additional_info.is_empty() { |
| 45 | + Ok(()) |
| 46 | + } else { |
| 47 | + anyhow::bail!("No additional info provided.") |
| 48 | + } |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + let bidirectional_attestation = |
| 53 | + AttestationBehavior::create_bidirectional_attestation(&[], TEE_MEASUREMENT.as_bytes()) |
| 54 | + .unwrap(); |
| 55 | + |
| 56 | + let additional_info = br"Additional Info".to_vec(); |
| 57 | + let server_handshaker = |
| 58 | + ServerHandshaker::new(bidirectional_attestation, Arc::new(additional_info)); |
| 59 | + |
| 60 | + (client_handshaker, server_handshaker) |
| 61 | +} |
| 62 | + |
| 63 | +#[test_case] |
| 64 | +fn test_handshake() { |
| 65 | + let (mut client_handshaker, mut server_handshaker) = create_handshakers(); |
| 66 | + |
| 67 | + let client_hello = client_handshaker |
| 68 | + .create_client_hello() |
| 69 | + .expect("Couldn't create client hello message"); |
| 70 | + |
| 71 | + let server_identity = server_handshaker |
| 72 | + .next_step(&client_hello) |
| 73 | + .expect("Couldn't process client hello message") |
| 74 | + .expect("Empty server identity message"); |
| 75 | + |
| 76 | + let client_identity = client_handshaker |
| 77 | + .next_step(&server_identity) |
| 78 | + .expect("Couldn't process server identity message") |
| 79 | + .expect("Empty client identity message"); |
| 80 | + assert!(client_handshaker.is_completed()); |
| 81 | + |
| 82 | + let result = server_handshaker |
| 83 | + .next_step(&client_identity) |
| 84 | + .expect("Couldn't process client identity message"); |
| 85 | + assert_eq!(result, None); |
| 86 | + assert!(server_handshaker.is_completed()); |
| 87 | + |
| 88 | + let mut client_encryptor = client_handshaker |
| 89 | + .get_encryptor() |
| 90 | + .expect("Couldn't get client encryptor"); |
| 91 | + let mut server_encryptor = server_handshaker |
| 92 | + .get_encryptor() |
| 93 | + .expect("Couldn't get server encryptor"); |
| 94 | + |
| 95 | + let encrypted_client_data = client_encryptor |
| 96 | + .encrypt(&DATA) |
| 97 | + .expect("Couldn't encrypt client data"); |
| 98 | + let decrypted_client_data = server_encryptor |
| 99 | + .decrypt(&encrypted_client_data) |
| 100 | + .expect("Couldn't decrypt client data"); |
| 101 | + assert_eq!(decrypted_client_data, DATA); |
| 102 | + |
| 103 | + let encrypted_server_data = server_encryptor |
| 104 | + .encrypt(&DATA) |
| 105 | + .expect("Couldn't encrypt server data"); |
| 106 | + let decrypted_server_data = client_encryptor |
| 107 | + .decrypt(&encrypted_server_data) |
| 108 | + .expect("Couldn't decrypt server data"); |
| 109 | + assert_eq!(decrypted_server_data, DATA); |
| 110 | +} |
0 commit comments