[refactoring] Group all the CBOR util functions in cbor_util
This cl only moves all the CBOR utility functions in one place.
Some return error types are adjusted to adapt the change. There
should be no behavior change.
Bug: 310931749
Test: atest rialto_test
Change-Id: Ie3db9c93b1355d25d7d796818e19e554560e1f23
diff --git a/service_vm/comm/Android.bp b/service_vm/comm/Android.bp
index bdfc099..bf923a4 100644
--- a/service_vm/comm/Android.bp
+++ b/service_vm/comm/Android.bp
@@ -23,6 +23,7 @@
rustlibs: [
"libbssl_avf_error_nostd",
"libciborium_nostd",
+ "libcbor_util_nostd",
"libcoset_nostd",
"libder_nostd",
"liblog_rust_nostd",
@@ -36,6 +37,7 @@
rustlibs: [
"libbssl_avf_error",
"libciborium",
+ "libcbor_util",
"libcoset",
"liblog_rust",
"libserde",
diff --git a/service_vm/comm/src/csr.rs b/service_vm/comm/src/csr.rs
index f14787f..a87d28f 100644
--- a/service_vm/comm/src/csr.rs
+++ b/service_vm/comm/src/csr.rs
@@ -17,9 +17,9 @@
use alloc::vec;
use alloc::vec::Vec;
+use cbor_util::{cbor_value_type, value_to_bytes};
use ciborium::Value;
use coset::{self, CborSerializable, CoseError};
-use log::error;
/// Represents a CSR sent from the client VM to the service VM for attestation.
///
@@ -99,37 +99,3 @@
})
}
}
-
-/// Converts the provided value `v` to bytes array.
-pub fn value_to_bytes(v: Value, context: &'static str) -> coset::Result<Vec<u8>> {
- v.into_bytes().map_err(|e| to_unexpected_item_error(&e, "bstr", context))
-}
-
-/// Builds a `CoseError::UnexpectedItem` error when the provided value `v` is not of the expected
-/// type `expected_type` and logs the error message with the provided `context`.
-pub fn to_unexpected_item_error(
- v: &Value,
- expected_type: &'static str,
- context: &'static str,
-) -> CoseError {
- let v_type = cbor_value_type(v);
- assert!(v_type != expected_type);
- error!("The provided value type '{v_type}' is not of type '{expected_type}': {context}");
- CoseError::UnexpectedItem(v_type, expected_type)
-}
-
-/// Reads the type of the provided value `v`.
-pub fn cbor_value_type(v: &Value) -> &'static str {
- match v {
- Value::Integer(_) => "int",
- Value::Bytes(_) => "bstr",
- Value::Float(_) => "float",
- Value::Text(_) => "tstr",
- Value::Bool(_) => "bool",
- Value::Null => "nul",
- Value::Tag(_, _) => "tag",
- Value::Array(_) => "array",
- Value::Map(_) => "map",
- _ => "other",
- }
-}
diff --git a/service_vm/comm/src/lib.rs b/service_vm/comm/src/lib.rs
index 343d48e..bb85a26 100644
--- a/service_vm/comm/src/lib.rs
+++ b/service_vm/comm/src/lib.rs
@@ -23,7 +23,7 @@
mod message;
mod vsock;
-pub use csr::{cbor_value_type, to_unexpected_item_error, value_to_bytes, Csr, CsrPayload};
+pub use csr::{Csr, CsrPayload};
pub use message::{
ClientVmAttestationParams, EcdsaP256KeyPair, GenerateCertificateRequestParams, Request,
RequestProcessingError, Response, ServiceVmRequest,
diff --git a/service_vm/requests/src/dice.rs b/service_vm/requests/src/dice.rs
index 15cfbc9..557b678 100644
--- a/service_vm/requests/src/dice.rs
+++ b/service_vm/requests/src/dice.rs
@@ -16,7 +16,11 @@
use alloc::string::String;
use alloc::vec::Vec;
-use ciborium::value::{Integer, Value};
+use cbor_util::{
+ cbor_value_type, value_to_array, value_to_byte_array, value_to_bytes, value_to_map,
+ value_to_num, value_to_text,
+};
+use ciborium::value::Value;
use core::cell::OnceCell;
use core::result;
use coset::{
@@ -24,9 +28,7 @@
};
use diced_open_dice::{DiceMode, HASH_SIZE};
use log::error;
-use service_vm_comm::{
- cbor_value_type, to_unexpected_item_error, value_to_bytes, RequestProcessingError,
-};
+use service_vm_comm::RequestProcessingError;
type Result<T> = result::Result<T, RequestProcessingError>;
@@ -399,33 +401,6 @@
}
}
-fn value_to_array(v: Value, context: &'static str) -> coset::Result<Vec<Value>> {
- v.into_array().map_err(|e| to_unexpected_item_error(&e, "array", context))
-}
-
-fn value_to_text(v: Value, context: &'static str) -> coset::Result<String> {
- v.into_text().map_err(|e| to_unexpected_item_error(&e, "tstr", context))
-}
-
-fn value_to_map(v: Value, context: &'static str) -> coset::Result<Vec<(Value, Value)>> {
- v.into_map().map_err(|e| to_unexpected_item_error(&e, "map", context))
-}
-
-fn value_to_num<T: TryFrom<Integer>>(v: Value, context: &'static str) -> Result<T> {
- let num = v.into_integer().map_err(|e| to_unexpected_item_error(&e, "int", context))?;
- num.try_into().map_err(|_| {
- error!("The provided value '{num:?}' is not a valid number: {context}");
- RequestProcessingError::InvalidDiceChain
- })
-}
-
-fn value_to_byte_array<const N: usize>(v: Value, context: &'static str) -> Result<[u8; N]> {
- value_to_bytes(v, context)?.try_into().map_err(|e| {
- error!("The provided value '{context}' is not an array of length {N}: {e:?}");
- RequestProcessingError::InternalError
- })
-}
-
fn to_mode(value: Value) -> Result<DiceMode> {
let mode = match value {
// Mode is supposed to be encoded as a 1-byte bstr, but some implementations instead