use std::io::{Read, Write};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use serde_json::Value;
use crate::error::{CoreError, Result};
pub const DEFAULT_BITSTRING_LENGTH: usize = 131_072;
pub const MULTIBASE_BASE64URL_PREFIX: char = 'u';
pub const MAX_DECOMPRESSED_BYTES: usize = 16 * 1024 * 1024;
pub const STATUS_PURPOSE_REVOCATION: &str = "revocation";
pub const BITSTRING_STATUS_LIST_CREDENTIAL_TYPE: &str = "BitstringStatusListCredential";
pub const BITSTRING_STATUS_LIST_SUBJECT_TYPE: &str = "BitstringStatusList";
pub const BITSTRING_STATUS_LIST_ENTRY_TYPE: &str = "BitstringStatusListEntry";
pub fn decode_bitstring(encoded: &str) -> Result<Vec<u8>> {
let body = encoded
.strip_prefix(MULTIBASE_BASE64URL_PREFIX)
.ok_or_else(|| CoreError::Json("encodedList must use multibase prefix 'u'".into()))?;
let compressed = URL_SAFE_NO_PAD
.decode(body)
.map_err(|e| CoreError::Json(format!("bad base64url: {e}")))?;
let gz = GzDecoder::new(&compressed[..]);
let mut out = Vec::new();
let mut limited = gz.take(MAX_DECOMPRESSED_BYTES as u64 + 1);
limited
.read_to_end(&mut out)
.map_err(|e| CoreError::Json(format!("gunzip failed: {e}")))?;
if out.len() > MAX_DECOMPRESSED_BYTES {
return Err(CoreError::Json(format!(
"decompressed status list exceeds the {MAX_DECOMPRESSED_BYTES}-byte limit"
)));
}
Ok(out)
}
pub fn encode_bitstring(bits: &[u8]) -> Result<String> {
let mut encoder = GzEncoder::new(Vec::new(), Compression::new(9));
encoder
.write_all(bits)
.map_err(|e| CoreError::Json(format!("gzip failed: {e}")))?;
let mut compressed = encoder
.finish()
.map_err(|e| CoreError::Json(format!("gzip finish failed: {e}")))?;
if compressed.len() >= 10 {
compressed[4] = 0;
compressed[5] = 0;
compressed[6] = 0;
compressed[7] = 0;
compressed[9] = 0xff;
}
Ok(format!(
"{}{}",
MULTIBASE_BASE64URL_PREFIX,
URL_SAFE_NO_PAD.encode(compressed)
))
}
pub fn get_status(bits: &[u8], index: usize) -> Result<bool> {
let byte = index >> 3;
if byte >= bits.len() {
return Err(CoreError::Json(format!(
"index {index} out of range for {}-bit list",
bits.len() * 8
)));
}
let pos = 7 - (index % 8);
Ok(bits[byte] & (1 << pos) != 0)
}
pub fn set_status(bits: &mut [u8], index: usize, value: bool) -> Result<()> {
let byte = index >> 3;
if byte >= bits.len() {
return Err(CoreError::Json(format!("index {index} out of range")));
}
let pos = 7 - (index % 8);
if value {
bits[byte] |= 1 << pos;
} else {
bits[byte] &= !(1 << pos);
}
Ok(())
}
pub fn verify_status(credential_status: &Value, status_list_credential: &Value) -> Result<bool> {
let cs = credential_status
.as_object()
.ok_or_else(|| CoreError::Json("credentialStatus must be an object".into()))?;
let sl = status_list_credential
.as_object()
.ok_or_else(|| CoreError::Json("statusListCredential must be an object".into()))?;
if cs.get("type").and_then(|v| v.as_str()) != Some(BITSTRING_STATUS_LIST_ENTRY_TYPE) {
return Err(CoreError::Json(format!(
"credentialStatus.type must be {BITSTRING_STATUS_LIST_ENTRY_TYPE}"
)));
}
let referenced = cs
.get("statusListCredential")
.and_then(|v| v.as_str())
.ok_or_else(|| {
CoreError::Json("credentialStatus.statusListCredential is required".into())
})?;
let actual_id = sl.get("id").and_then(|v| v.as_str()).unwrap_or_default();
if actual_id != referenced {
return Err(CoreError::Json(format!(
"status list id mismatch: references {referenced}, fetched {actual_id}"
)));
}
let has_type = sl
.get("type")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.any(|t| t.as_str() == Some(BITSTRING_STATUS_LIST_CREDENTIAL_TYPE))
})
.unwrap_or(false);
if !has_type {
return Err(CoreError::Json(format!(
"fetched credential is not a {BITSTRING_STATUS_LIST_CREDENTIAL_TYPE}"
)));
}
let subject = sl
.get("credentialSubject")
.and_then(|v| v.as_object())
.ok_or_else(|| CoreError::Json("status list has no credentialSubject".into()))?;
if subject.get("type").and_then(|v| v.as_str()) != Some(BITSTRING_STATUS_LIST_SUBJECT_TYPE) {
return Err(CoreError::Json(format!(
"credentialSubject.type must be {BITSTRING_STATUS_LIST_SUBJECT_TYPE}"
)));
}
if cs.get("statusPurpose").and_then(|v| v.as_str())
!= subject.get("statusPurpose").and_then(|v| v.as_str())
{
return Err(CoreError::Json("statusPurpose mismatch".into()));
}
let encoded = subject
.get("encodedList")
.and_then(|v| v.as_str())
.ok_or_else(|| CoreError::Json("credentialSubject.encodedList is required".into()))?;
let index = match cs.get("statusListIndex") {
Some(Value::String(s)) => s
.parse::<usize>()
.map_err(|_| CoreError::Json(format!("bad statusListIndex: {s:?}")))?,
Some(Value::Number(n)) => n
.as_u64()
.ok_or_else(|| CoreError::Json("statusListIndex must be non-negative".into()))?
as usize,
_ => {
return Err(CoreError::Json(
"credentialStatus.statusListIndex is required".into(),
))
}
};
let bits = decode_bitstring(encoded)?;
get_status(&bits, index)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn malformed_encoded_list_errors_gracefully() {
assert!(decode_bitstring("not-multibase").is_err()); assert!(decode_bitstring("uNotValidGzipData").is_err()); }
#[test]
fn encode_decode_roundtrip_preserves_bits() {
let mut bits = vec![0u8; DEFAULT_BITSTRING_LENGTH / 8];
for &i in &[0usize, 1, 7, 8, 15, 100, 1023, 65535, 131070, 131071] {
set_status(&mut bits, i, true).unwrap();
}
let encoded = encode_bitstring(&bits).unwrap();
let decoded = decode_bitstring(&encoded).unwrap();
assert_eq!(decoded, bits);
assert!(get_status(&decoded, 100).unwrap());
assert!(!get_status(&decoded, 99).unwrap());
}
}