-
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
13 changed files
with
610 additions
and
33 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
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 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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
use rand::thread_rng; | ||
use rand::RngCore; | ||
|
||
pub mod agent; | ||
pub mod http; | ||
pub mod identity; | ||
pub mod setting; | ||
|
||
pub use identity::*; | ||
|
||
pub fn rand_bytes<const N: usize>() -> [u8; N] { | ||
let mut rng = thread_rng(); | ||
let mut bytes = [0u8; N]; | ||
rng.fill_bytes(&mut bytes); | ||
bytes | ||
} |
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,41 @@ | ||
use ciborium::from_reader; | ||
use ic_cose_types::{ | ||
cose::{encrypt0::cose_decrypt0, get_cose_key_secret, CborSerializable, CoseKey}, | ||
types::setting::SettingInfo, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_bytes::ByteBuf; | ||
|
||
pub fn decrypt_payload(info: SettingInfo, mut secret: [u8; 32]) -> Result<Vec<u8>, String> { | ||
let aad: &[u8] = &[]; | ||
if let Some(dek) = info.dek { | ||
let key = cose_decrypt0(dek.as_slice(), &secret, aad)?; | ||
let key = | ||
CoseKey::from_slice(&key).map_err(|err| format!("invalid COSE key: {:?}", err))?; | ||
let secret2 = get_cose_key_secret(key)?; | ||
if info.payload.is_none() { | ||
// payload in dek | ||
return Ok(secret2); | ||
} | ||
// get dek | ||
secret = secret2.try_into().map_err(|val: Vec<u8>| { | ||
format!("invalid COSE secret, expected 32 bytes, got {}", val.len()) | ||
})?; | ||
} | ||
|
||
match info.payload { | ||
None => Err("no payload".to_string()), | ||
Some(payload) => cose_decrypt0(payload.as_slice(), &secret, aad), | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct TLSPayload { | ||
pub crt: ByteBuf, // PEM-encoded certificate | ||
pub key: ByteBuf, // PEM-encoded private key | ||
} | ||
|
||
pub fn decrypt_tls(info: SettingInfo, secret: [u8; 32]) -> Result<TLSPayload, String> { | ||
let data = decrypt_payload(info, secret)?; | ||
from_reader(&data[..]).map_err(|err| format!("failed to decode TLS payload: {:?}", err)) | ||
} |
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 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
Oops, something went wrong.