Skip to content

Commit 62445d4

Browse files
committed
Add std-only examples
1 parent 3d1545e commit 62445d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+4763
-2
lines changed

.licenserc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ header:
3030
- 'KEYS'
3131
- 'DISCLAIMER-WIP'
3232
- '*.json'
33+
- 'examples/tls_server-rs/ta/test-ca/**'

ci/ci.sh

+10
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ pushd ../tests
3636
./test_supp_plugin.sh
3737
./test_error_handling.sh
3838

39+
# Run std only tests
40+
if [ "$STD" ]; then
41+
./test_serde.sh
42+
./test_message_passing_interface.sh
43+
./test_tcp_client.sh
44+
./test_udp_socket.sh
45+
./test_tls_client.sh
46+
./test_tls_server.sh
47+
fi
48+
3949
popd

environment

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ then
9292
else
9393
echo -e "Error: OPTEE_CLIENT_EXPORT=$OPTEE_CLIENT_EXPORT does not exist, please set the correct OPTEE_CLIENT_EXPORT or run \"$ ./build_optee_libraries.sh optee/\" then try again\n"
9494
unset OPTEE_DIR
95-
fi
95+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
# If _HOST or _TA specific compiler/target are not specified, then use common
19+
# compiler/target for both
20+
CROSS_COMPILE_HOST ?= aarch64-linux-gnu-
21+
CROSS_COMPILE_TA ?= aarch64-linux-gnu-
22+
TARGET_HOST ?= aarch64-unknown-linux-gnu
23+
TARGET_TA ?= aarch64-unknown-linux-gnu
24+
25+
all:
26+
$(q)make -C host TARGET_HOST=$(TARGET_HOST) \
27+
CROSS_COMPILE_HOST=$(CROSS_COMPILE_HOST)
28+
$(q)make -C ta TARGET_TA=$(TARGET_TA) \
29+
CROSS_COMPILE_TA=$(CROSS_COMPILE_TA)
30+
31+
clean:
32+
$(q)make -C host clean
33+
$(q)make -C ta clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
[package]
19+
name = "message_passing_interface-rs"
20+
version = "0.3.0"
21+
authors = ["Teaclave Contributors <[email protected]>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "An example of Rust OP-TEE TrustZone SDK."
25+
edition = "2018"
26+
27+
[dependencies]
28+
url = "2.5.0"
29+
proto = { path = "../proto" }
30+
optee-teec = { path = "../../../optee-teec" }
31+
32+
[profile.release]
33+
lto = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
NAME := message_passing_interface-rs
19+
20+
TARGET_HOST ?= aarch64-unknown-linux-gnu
21+
CROSS_COMPILE_HOST ?= aarch64-linux-gnu-
22+
OBJCOPY := $(CROSS_COMPILE_HOST)objcopy
23+
LINKER_CFG := target.$(TARGET_HOST).linker=\"$(CROSS_COMPILE_HOST)gcc\"
24+
25+
OUT_DIR := $(CURDIR)/target/$(TARGET_HOST)/release
26+
27+
28+
all: host strip
29+
30+
host:
31+
@cargo build --target $(TARGET_HOST) --release --config $(LINKER_CFG)
32+
33+
strip: host
34+
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME)
35+
36+
clean:
37+
@cargo clean
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
[package]
19+
name = "proto"
20+
version = "0.3.0"
21+
authors = ["Teaclave Contributors <[email protected]>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "Data structures and functions shared by host and TA."
25+
edition = "2018"
26+
27+
[dependencies]
28+
serde = { version = "1.0", features = ["derive"] }
29+
serde_json = "1.0"
30+
31+
[build-dependencies]
32+
uuid = { version = "1.8", default-features = false }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 std::fs;
19+
use std::path::PathBuf;
20+
use std::fs::File;
21+
use std::env;
22+
use std::io::Write;
23+
24+
fn main() {
25+
let uuid = match fs::read_to_string("../uuid.txt") {
26+
Ok(u) => {
27+
u.trim().to_string()
28+
},
29+
Err(_) => {
30+
panic!("Cannot find uuid.txt");
31+
}
32+
};
33+
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
34+
let mut buffer = File::create(out.join("uuid.txt")).unwrap();
35+
write!(buffer, "{}", uuid).unwrap();
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 serde::{Serialize, Deserialize};
19+
pub use serde_json;
20+
21+
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
22+
pub enum Command {
23+
Hello,
24+
Bye,
25+
Unknown,
26+
}
27+
28+
#[derive(Serialize, Deserialize, Debug)]
29+
pub struct EnclaveInput {
30+
pub command: Command,
31+
pub message: String
32+
}
33+
34+
#[derive(Serialize, Deserialize, Debug)]
35+
pub struct EnclaveOutput {
36+
pub message: String
37+
}
38+
39+
impl From<u32> for Command {
40+
#[inline]
41+
fn from(value: u32) -> Command {
42+
match value {
43+
0 => Command::Hello,
44+
1 => Command::Bye,
45+
_ => Command::Unknown,
46+
}
47+
}
48+
}
49+
50+
51+
pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
[package]
19+
name = "ta"
20+
version = "0.3.0"
21+
authors = ["Teaclave Contributors <[email protected]>"]
22+
license = "Apache-2.0"
23+
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
24+
description = "An example of Rust OP-TEE TrustZone SDK."
25+
edition = "2018"
26+
27+
[dependencies]
28+
proto = { path = "../proto" }
29+
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
30+
optee-utee = { path = "../../../optee-utee" }
31+
32+
[build-dependencies]
33+
uuid = { version = "1.8", default-features = false }
34+
proto = { path = "../proto" }
35+
36+
[profile.release]
37+
panic = "abort"
38+
lto = false
39+
opt-level = 1

0 commit comments

Comments
 (0)