-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,575 additions
and
84 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "ic_tee_agent" | ||
description = "An agent to interact with the Internet Computer for Trusted Execution Environments (TEEs)" | ||
repository = "https://github.com/ldclabs/ic-tee/tree/main/src/ic_tee_agent" | ||
publish = true | ||
version.workspace = true | ||
edition.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
candid = { workspace = true } | ||
ed25519-consensus = { workspace = true } | ||
ic-agent = { workspace = true } | ||
rand = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `ic_tee_agent` | ||
![License](https://img.shields.io/crates/l/ic_tee_agent.svg) | ||
[![Crates.io](https://img.shields.io/crates/d/ic_tee_agent.svg)](https://crates.io/crates/ic_tee_agent) | ||
[![Test](https://github.com/ldclabs/ic-tee/actions/workflows/test.yml/badge.svg)](https://github.com/ldclabs/ic-tee/actions/workflows/test.yml) | ||
[![Docs.rs](https://img.shields.io/docsrs/ic_tee_agent?label=docs.rs)](https://docs.rs/ic_tee_agent) | ||
[![Latest Version](https://img.shields.io/crates/v/ic_tee_agent.svg)](https://crates.io/crates/ic_tee_agent) | ||
|
||
`ic_tee_agent` is an agent to interact with the Internet Computer for Trusted Execution Environments (TEEs). | ||
|
||
## License | ||
Copyright © 2024 [LDC Labs](https://github.com/ldclabs). | ||
|
||
`ldclabs/ic-tee` is licensed under the MIT License. See [LICENSE](../../LICENSE-MIT) for the full license text. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use candid::Principal; | ||
use ed25519_consensus::SigningKey; | ||
use ic_agent::{ | ||
identity::{ | ||
AnonymousIdentity, BasicIdentity, DelegatedIdentity, Delegation, Identity, SignedDelegation, | ||
}, | ||
{agent::EnvelopeContent, Signature}, | ||
}; | ||
use rand::thread_rng; | ||
use std::time::{Duration, SystemTime, UNIX_EPOCH}; | ||
|
||
enum InnerIdentity { | ||
Anonymous(AnonymousIdentity), | ||
Delegated(DelegatedIdentity), | ||
} | ||
|
||
pub struct TEEIdentity { | ||
identity: InnerIdentity, | ||
signing_key: SigningKey, | ||
user_key: Vec<u8>, | ||
session_key: Vec<u8>, | ||
principal: Principal, | ||
expiration: u64, // ns since UNIX epoch | ||
} | ||
|
||
impl Default for TEEIdentity { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl TEEIdentity { | ||
pub fn new() -> Self { | ||
let signing_key = SigningKey::new(thread_rng()); | ||
let basic = BasicIdentity::from_signing_key(SigningKey::new(thread_rng())); | ||
Self { | ||
identity: InnerIdentity::Anonymous(AnonymousIdentity), | ||
signing_key, | ||
user_key: vec![], | ||
session_key: basic.public_key().unwrap(), | ||
principal: AnonymousIdentity.sender().unwrap(), | ||
expiration: 0, | ||
} | ||
} | ||
|
||
pub fn is_authenticated(&self) -> bool { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(_) => false, | ||
InnerIdentity::Delegated(_) => { | ||
let now = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap_or_default() | ||
.saturating_sub(Duration::from_secs(300)); | ||
!now.is_zero() && now.as_nanos() < self.expiration as u128 | ||
} | ||
} | ||
} | ||
|
||
pub fn session_key(&self) -> Vec<u8> { | ||
self.session_key.clone() | ||
} | ||
|
||
pub fn principal(&self) -> Principal { | ||
self.principal | ||
} | ||
|
||
pub fn with_user_key(&mut self, user_key: Vec<u8>) { | ||
self.principal = Principal::self_authenticating(&user_key); | ||
self.user_key = user_key; | ||
} | ||
|
||
pub fn with_delegation(&mut self, delegation: SignedDelegation) -> Result<(), String> { | ||
if delegation.delegation.pubkey != self.session_key { | ||
return Err("delegation pubkey does not match".to_string()); | ||
} | ||
|
||
self.expiration = delegation.delegation.expiration; | ||
let id = DelegatedIdentity::new_unchecked( | ||
self.user_key.clone(), | ||
Box::new(BasicIdentity::from_signing_key(self.signing_key.clone())), | ||
vec![delegation], | ||
); | ||
self.identity = InnerIdentity::Delegated(id); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Identity for TEEIdentity { | ||
fn sender(&self) -> Result<Principal, String> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.sender(), | ||
InnerIdentity::Delegated(id) => id.sender(), | ||
} | ||
} | ||
fn public_key(&self) -> Option<Vec<u8>> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.public_key(), | ||
InnerIdentity::Delegated(id) => id.public_key(), | ||
} | ||
} | ||
fn sign(&self, content: &EnvelopeContent) -> Result<Signature, String> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.sign(content), | ||
InnerIdentity::Delegated(id) => id.sign(content), | ||
} | ||
} | ||
fn sign_delegation(&self, content: &Delegation) -> Result<Signature, String> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.sign_delegation(content), | ||
InnerIdentity::Delegated(id) => id.sign_delegation(content), | ||
} | ||
} | ||
fn sign_arbitrary(&self, content: &[u8]) -> Result<Signature, String> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.sign_arbitrary(content), | ||
InnerIdentity::Delegated(id) => id.sign_arbitrary(content), | ||
} | ||
} | ||
fn delegation_chain(&self) -> Vec<SignedDelegation> { | ||
match &self.identity { | ||
InnerIdentity::Anonymous(id) => id.delegation_chain(), | ||
InnerIdentity::Delegated(id) => id.delegation_chain(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod identity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "ic_tee_sdk" | ||
description = "A Rust library to make Trusted Execution Environments (TEEs) work with the Internet Computer" | ||
repository = "https://github.com/ldclabs/ic-tee/tree/main/src/ic_tee_sdk" | ||
publish = true | ||
version.workspace = true | ||
edition.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
candid = { workspace = true } | ||
serde = { workspace = true } | ||
serde_bytes = { workspace = true } | ||
sha3 = { workspace = true } | ||
ic-canister-sig-creation = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `ic_tee_sdk` | ||
![License](https://img.shields.io/crates/l/ic_tee_sdk.svg) | ||
[![Crates.io](https://img.shields.io/crates/d/ic_tee_sdk.svg)](https://crates.io/crates/ic_tee_sdk) | ||
[![Test](https://github.com/ldclabs/ic-tee/actions/workflows/test.yml/badge.svg)](https://github.com/ldclabs/ic-tee/actions/workflows/test.yml) | ||
[![Docs.rs](https://img.shields.io/docsrs/ic_tee_sdk?label=docs.rs)](https://docs.rs/ic_tee_sdk) | ||
[![Latest Version](https://img.shields.io/crates/v/ic_tee_sdk.svg)](https://crates.io/crates/ic_tee_sdk) | ||
|
||
`ic_tee_sdk` is a Rust library to make Trusted Execution Environments (TEEs) work with the Internet Computer. | ||
|
||
## License | ||
Copyright © 2024 [LDC Labs](https://github.com/ldclabs). | ||
|
||
`ldclabs/ic-tee` is licensed under the MIT License. See [LICENSE](../../LICENSE-MIT) for the full license text. |
Oops, something went wrong.