use ic_auth_protocol_types::{
AudienceId, CanonicalId, DelegatedRoleGrant, PortableClaims, PortableMechanism, Principal,
};
use sha2::{Digest, Sha256};
use std::{error::Error, fmt};
const DOMAIN: &[u8] = b"IC-AUTH-PORTABLE-CREDENTIAL-V1\0";
const MAX_ROLES: usize = 16;
const MAX_SCOPES_PER_ROLE: usize = 32;
const MAX_SCOPE_ENTRIES: usize = 32;
const MAX_LABEL_BYTES: usize = 64;
const MAX_GRANT_BYTES: usize = 4096;
pub const MAX_CLAIMS_BYTES: usize = 65_536;
const _: () = assert!(
4 + MAX_ROLES * (8 + MAX_LABEL_BYTES) + MAX_SCOPE_ENTRIES * (4 + MAX_LABEL_BYTES)
<= MAX_GRANT_BYTES
);
const _: () = assert!(
284 + 4 + ic_auth_protocol_types::MAX_TOKEN_EXT_BYTES + MAX_GRANT_BYTES <= MAX_CLAIMS_BYTES
);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ClaimsError {
TooLarge,
Truncated,
WrongDomain,
TrailingBytes,
InvalidShape,
AnonymousSubject,
NonCanonicalRoles,
NonCanonicalScopes,
InvalidGrantCount {
found: usize,
},
InvalidScopeCount {
found: usize,
},
TooManyScopeEntries {
found: usize,
max: usize,
},
}
impl fmt::Display for ClaimsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = match self {
Self::TooLarge => "claims exceed their byte bound",
Self::Truncated => "truncated claims",
Self::WrongDomain => "unsupported claims domain or mechanism",
Self::TrailingBytes => "trailing claims bytes",
Self::InvalidShape => "invalid claims structure",
Self::AnonymousSubject => "anonymous account subject",
Self::NonCanonicalRoles => "roles must be strictly ordered and unique",
Self::NonCanonicalScopes => "scopes must be strictly ordered and unique",
Self::InvalidGrantCount { found } => {
return write!(f, "grant count {found} is outside 1..=16");
}
Self::InvalidScopeCount { found } => {
return write!(f, "scope count {found} is outside 1..=32");
}
Self::TooManyScopeEntries { found, max } => {
return write!(f, "credential has {found} scope entries; maximum is {max}");
}
};
f.write_str(message)
}
}
impl Error for ClaimsError {}
fn check_entries(found: usize) -> Result<(), ClaimsError> {
if found > MAX_SCOPE_ENTRIES {
return Err(ClaimsError::TooManyScopeEntries {
found,
max: MAX_SCOPE_ENTRIES,
});
}
Ok(())
}
fn validate(claims: &PortableClaims) -> Result<(), ClaimsError> {
if claims
.ext
.as_ref()
.is_some_and(|ext| ext.len() > ic_auth_protocol_types::MAX_TOKEN_EXT_BYTES)
{
return Err(ClaimsError::TooLarge);
}
if claims.device_slot > 7 || claims.device_generation == 0 {
return Err(ClaimsError::InvalidShape);
}
if claims.account_subject == Principal::anonymous() {
return Err(ClaimsError::AnonymousSubject);
}
validate_grants(&claims.grants)?;
check_entries(claims.grants.iter().map(|grant| grant.scopes.len()).sum())
}
pub(crate) fn validate_grants(grants: &[DelegatedRoleGrant]) -> Result<(), ClaimsError> {
if !(1..=MAX_ROLES).contains(&grants.len()) {
return Err(ClaimsError::InvalidGrantCount {
found: grants.len(),
});
}
for (index, grant) in grants.iter().enumerate() {
if index > 0 && grants[index - 1].target >= grant.target {
return Err(ClaimsError::NonCanonicalRoles);
}
if !(1..=MAX_SCOPES_PER_ROLE).contains(&grant.scopes.len()) {
return Err(ClaimsError::InvalidScopeCount {
found: grant.scopes.len(),
});
}
if grant.scopes.windows(2).any(|pair| pair[0] >= pair[1]) {
return Err(ClaimsError::NonCanonicalScopes);
}
}
Ok(())
}
pub fn encode_claims(claims: &PortableClaims) -> Result<Vec<u8>, ClaimsError> {
validate(claims)?;
let mut output = Vec::new();
write_fields(claims, |bytes| output.extend_from_slice(bytes));
Ok(output)
}
pub fn claims_hash(claims: &PortableClaims) -> Result<[u8; 32], ClaimsError> {
validate(claims)?;
let mut digest = Sha256::new();
write_fields(claims, |bytes| digest.update(bytes));
Ok(digest.finalize().into())
}
fn write_fields(claims: &PortableClaims, mut emit: impl FnMut(&[u8])) {
emit(DOMAIN);
emit(&[match claims.mechanism {
PortableMechanism::Certified => 0,
PortableMechanism::Ed25519 => 1,
}]);
emit(claims.audience.canonical_network_id.as_bytes());
emit(claims.audience.application_id.as_bytes());
emit(&(u32::from(claims.issuer.len())).to_be_bytes());
emit(claims.issuer.as_slice());
emit(&claims.key_binding_id);
emit(&claims.browser_public_key);
emit(&(u32::from(claims.account_subject.len())).to_be_bytes());
emit(claims.account_subject.as_slice());
emit(&[u8::from(claims.ext.is_some())]);
if let Some(ext) = &claims.ext {
emit(&(ext.len() as u32).to_be_bytes());
emit(ext);
}
emit(&[claims.device_slot]);
for value in [
claims.device_generation,
claims.login_started_at_ns,
claims.login_deadline_ns,
claims.issued_at_ns,
claims.admission_expires_at_ns,
] {
emit(&value.to_be_bytes());
}
emit(&claims.nonce);
write_grants(&claims.grants, &mut emit);
}
pub(crate) fn write_grants(grants: &[DelegatedRoleGrant], emit: &mut dyn FnMut(&[u8])) {
emit(&(grants.len() as u32).to_be_bytes());
for grant in grants {
emit(&(grant.target.as_str().len() as u32).to_be_bytes());
emit(grant.target.as_str().as_bytes());
emit(&(grant.scopes.len() as u32).to_be_bytes());
for scope in &grant.scopes {
emit(&(scope.as_str().len() as u32).to_be_bytes());
emit(scope.as_str().as_bytes());
}
}
}
pub fn decode_claims(bytes: &[u8]) -> Result<PortableClaims, ClaimsError> {
if bytes.len() > MAX_CLAIMS_BYTES {
return Err(ClaimsError::TooLarge);
}
let mut input = Input { remaining: bytes };
if input.take(DOMAIN.len())? != DOMAIN {
return Err(ClaimsError::WrongDomain);
}
let mechanism = match input.fixed()? {
[0] => PortableMechanism::Certified,
[1] => PortableMechanism::Ed25519,
_ => return Err(ClaimsError::WrongDomain),
};
let audience = AudienceId {
canonical_network_id: CanonicalId::from_bytes(input.fixed()?),
application_id: CanonicalId::from_bytes(input.fixed()?),
};
let issuer = input.principal()?;
let key_binding_id = input.fixed()?;
let browser_public_key = input.fixed()?;
let account_subject = input.principal()?;
let ext = match input.fixed()? {
[0] => None,
[1] => {
let len = input.size(0, ic_auth_protocol_types::MAX_TOKEN_EXT_BYTES)?;
Some(input.take(len)?.to_vec())
}
_ => return Err(ClaimsError::InvalidShape),
};
let [device_slot] = input.fixed()?;
let device_generation = u64::from_be_bytes(input.fixed()?);
let login_started_at_ns = u64::from_be_bytes(input.fixed()?);
let login_deadline_ns = u64::from_be_bytes(input.fixed()?);
let issued_at_ns = u64::from_be_bytes(input.fixed()?);
let admission_expires_at_ns = u64::from_be_bytes(input.fixed()?);
let nonce = input.fixed()?;
if input.remaining.len() > MAX_GRANT_BYTES {
return Err(ClaimsError::TooLarge);
}
let roles = input.size(1, MAX_ROLES)?;
let mut grants = Vec::with_capacity(roles);
let mut entries = 0;
for _ in 0..roles {
let target = input
.label()?
.parse()
.map_err(|_| ClaimsError::InvalidShape)?;
let count = input.size(1, MAX_SCOPES_PER_ROLE)?;
entries += count;
check_entries(entries)?;
let mut scopes = Vec::with_capacity(count);
for _ in 0..count {
scopes.push(
input
.label()?
.parse()
.map_err(|_| ClaimsError::InvalidShape)?,
);
}
grants.push(DelegatedRoleGrant { target, scopes });
}
if !input.remaining.is_empty() {
return Err(ClaimsError::TrailingBytes);
}
let claims = PortableClaims {
mechanism,
audience,
issuer,
key_binding_id,
browser_public_key,
account_subject,
ext,
device_slot,
device_generation,
login_started_at_ns,
login_deadline_ns,
issued_at_ns,
admission_expires_at_ns,
nonce,
grants,
};
validate(&claims)?;
Ok(claims)
}
struct Input<'a> {
remaining: &'a [u8],
}
impl<'a> Input<'a> {
fn take(&mut self, length: usize) -> Result<&'a [u8], ClaimsError> {
let head = self.remaining.get(..length).ok_or(ClaimsError::Truncated)?;
self.remaining = &self.remaining[length..];
Ok(head)
}
fn fixed<const N: usize>(&mut self) -> Result<[u8; N], ClaimsError> {
self.take(N)?.try_into().map_err(|_| ClaimsError::Truncated)
}
fn size(&mut self, minimum: usize, maximum: usize) -> Result<usize, ClaimsError> {
let size = usize::try_from(u32::from_be_bytes(self.fixed()?))
.map_err(|_| ClaimsError::InvalidShape)?;
if !(minimum..=maximum).contains(&size) {
return Err(ClaimsError::InvalidShape);
}
Ok(size)
}
fn principal(&mut self) -> Result<Principal, ClaimsError> {
let size = self.size(0, Principal::MAX_LENGTH_IN_BYTES)?;
Principal::try_from_slice(self.take(size)?).map_err(|_| ClaimsError::InvalidShape)
}
fn label(&mut self) -> Result<&'a str, ClaimsError> {
let size = self.size(1, MAX_LABEL_BYTES)?;
std::str::from_utf8(self.take(size)?).map_err(|_| ClaimsError::InvalidShape)
}
}