|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use optee_teec::{Context, Operation, ParamNone, ParamTmpRef, ParamType, ParamValue, Uuid}; |
| 19 | +use proto; |
| 20 | +use url; |
| 21 | + |
| 22 | +type Result<T> = optee_teec::Result<T>; |
| 23 | + |
| 24 | +pub struct EnclaveClient { |
| 25 | + uuid: String, |
| 26 | + context: optee_teec::Context, |
| 27 | + buffer: Vec<u8>, |
| 28 | +} |
| 29 | + |
| 30 | +impl EnclaveClient { |
| 31 | + pub fn open(url: &str) -> Result<Self> { |
| 32 | + let url = url::Url::parse(url).unwrap(); |
| 33 | + match url.scheme() { |
| 34 | + "trustzone-enclave" => Self::open_uuid(url.host_str().unwrap()), |
| 35 | + _ => unimplemented!(), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn open_uuid(uuid: &str) -> Result<Self> { |
| 40 | + let context = Context::new()?; |
| 41 | + Ok(Self { |
| 42 | + uuid: uuid.to_string(), |
| 43 | + context: context, |
| 44 | + buffer: vec![0; 128], |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn invoke(&mut self, input: &proto::EnclaveInput) -> Result<proto::EnclaveOutput> { |
| 49 | + let command_id = input.command as u32; |
| 50 | + let mut serialized_input = proto::serde_json::to_vec(input).unwrap(); |
| 51 | + |
| 52 | + let p0 = ParamTmpRef::new_input(serialized_input.as_mut_slice()); |
| 53 | + let p1 = ParamTmpRef::new_output(&mut self.buffer); |
| 54 | + let p2 = ParamValue::new(0, 0, ParamType::ValueInout); |
| 55 | + |
| 56 | + let mut operation = Operation::new(0, p0, p1, p2, ParamNone); |
| 57 | + |
| 58 | + let uuid = Uuid::parse_str(&self.uuid).unwrap(); |
| 59 | + let mut session = self.context.open_session(uuid)?; |
| 60 | + session.invoke_command(command_id, &mut operation)?; |
| 61 | + let len = operation.parameters().2.a() as usize; |
| 62 | + |
| 63 | + let output: proto::EnclaveOutput = |
| 64 | + proto::serde_json::from_slice(&self.buffer[0..len]).unwrap(); |
| 65 | + Ok(output) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn main() -> optee_teec::Result<()> { |
| 70 | + let url = format!("trustzone-enclave://{}", proto::UUID); |
| 71 | + let mut enclave = EnclaveClient::open(&url).unwrap(); |
| 72 | + let input = proto::EnclaveInput { |
| 73 | + command: proto::Command::Hello, |
| 74 | + message: String::from("World!"), |
| 75 | + }; |
| 76 | + let output = enclave.invoke(&input).unwrap(); |
| 77 | + println!("{:?}", output); |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
0 commit comments