/// Parse a COSE public key (RFC 8152) in CBOR encoding
///
/// # Arguments
/// * `pk_cose` the CBOR-encoded COSE public key
/// # Errors
/// * `AlgorithmNotSupported` if some unsupported algorithm is used
/// * `MalformedPublicKey` if the public key could not be parsed
/// # Returns
/// The decoded public key
Parse a COSE public key (RFC 8152) in CBOR encoding
Parse a COSE public key ( RFC 8152 ) in CBOR encoding
pub fn from_cbor(pk_cose: &[u8]) -> Result<Self, CosePublicKeyParseError> {
        let parsed_value: serde_cbor::value::Value = serde_cbor::from_slice(pk_cose)
            .map_err(|_| CosePublicKeyParseError::MalformedPublicKey(AlgorithmId::Placeholder))?;
        if let serde_cbor::Value::Map(fields) = parsed_value {
            let kty =
                fields
                    .get(&COSE_PARAM_KTY)
                    .ok_or(CosePublicKeyParseError::MalformedPublicKey(
                        AlgorithmId::Placeholder,
                    ))?;
            let alg =
                fields
                    .get(&COSE_PARAM_ALG)
                    .ok_or(CosePublicKeyParseError::MalformedPublicKey(
                        AlgorithmId::Placeholder,
                    ))?;
            if *kty == COSE_KTY_EC2 && *alg == COSE_ALG_ES256 {
                Self::parse_ecdsa_p256(&fields)
            } else if *kty == COSE_KTY_RSA && *alg == COSE_ALG_RS256 {
                Self::parse_rsa_pkcs1_sha256(&fields)
            } else {
                // Some other algorithm
                Err(CosePublicKeyParseError::AlgorithmNotSupported)
            }
        } else {
            Err(CosePublicKeyParseError::MalformedPublicKey(
                AlgorithmId::Placeholder,
            )) // not a map!
        }
    }
pub fn from_cbor ( pk_cose : & [ u8 ] ) -> Result < Self , CosePublicKeyParseError > { let parsed_value : serde_cbor :: value :: Value = serde_cbor :: from_slice ( pk_cose ) . map_err ( | _ | CosePublicKeyParseError :: MalformedPublicKey ( AlgorithmId :: Placeholder ) ) ? ; if let serde_cbor :: Value :: Map ( fields ) = parsed_value { let kty = fields . get ( & COSE_PARAM_KTY ) . ok_or ( CosePublicKeyParseError :: MalformedPublicKey ( AlgorithmId :: Placeholder , ) ) ? ; let alg = fields . get ( & COSE_PARAM_ALG ) . ok_or ( CosePublicKeyParseError :: MalformedPublicKey ( AlgorithmId :: Placeholder , ) ) ? ; if * kty == COSE_KTY_EC2 && * alg == COSE_ALG_ES256 { Self :: parse_ecdsa_p256 ( & fields ) } else if * kty == COSE_KTY_RSA && * alg == COSE_ALG_RS256 { Self :: parse_rsa_pkcs1_sha256 ( & fields ) } else { // Some other algorithm Err ( CosePublicKeyParseError :: AlgorithmNotSupported ) } } else { Err ( CosePublicKeyParseError :: MalformedPublicKey ( AlgorithmId :: Placeholder , ) ) // not a map! } }
Parse a COSE public key (RFC 8152) in CBOR encoding
Parse a COSE public key ( RFC 8152 ) in CBOR encoding
The decoded public key
The decoded public key
pk_cose
&[u8]
errors
`AlgorithmNotSupported` if some unsupported algorithm is used
`MalformedPublicKey` if the public key could not be parsed
` AlgorithmNotSupported ` if some unsupported algorithm is used ` MalformedPublicKey ` if the public key could not be parsed