/// Parse a public key and verify if it is of the expected type and size
///
/// The format is SubjectPublicKeyInfo as defined in RFC 5480
Parse a public key and verify if it is of the expected type and size
The format is SubjectPublicKeyInfo as defined in RFC 5480
Parse a public key and verify if it is of the expected type and size The format is SubjectPublicKeyInfo as defined in RFC 5480
pub fn parse_public_key(
    der: &[u8],
    algorithm: AlgorithmId,
    expected_algo_id: PkixAlgorithmIdentifier,
    expected_pk_len: Option<usize>,
) -> CryptoResult<Vec<u8>> {
    let (algo_id, pk_bytes) = algo_id_and_public_key_bytes_from_der(der).map_err(|e| {
        CryptoError::MalformedPublicKey {
            algorithm,
            key_bytes: Some(der.to_vec()),
            internal_error: e.internal_error,
        }
    })?;
    if algo_id != expected_algo_id {
        return Err(CryptoError::MalformedPublicKey {
            algorithm,
            key_bytes: Some(der.to_vec()),
            internal_error: format!(
                "Wrong algorithm identifier for {:?}: expected {:?} got {:?}",
                algorithm, expected_algo_id, algo_id
            ),
        });
    }
    if let Some(expected_pk_len) = expected_pk_len {
        if pk_bytes.len() != expected_pk_len {
            return Err(CryptoError::MalformedPublicKey {
                algorithm,
                key_bytes: Some(der.to_vec()),
                internal_error: format!(
                    "Wrong length for {:?} expected {} got {}",
                    algorithm,
                    expected_pk_len,
                    pk_bytes.len()
                ),
            });
        }
    }
    Ok(pk_bytes)
}
pub fn parse_public_key ( der : & [ u8 ] , algorithm : AlgorithmId , expected_algo_id : PkixAlgorithmIdentifier , expected_pk_len : Option < usize > , ) -> CryptoResult < Vec < u8 > > { let ( algo_id , pk_bytes ) = algo_id_and_public_key_bytes_from_der ( der ) . map_err ( | e | { CryptoError :: MalformedPublicKey { algorithm , key_bytes : Some ( der . to_vec ( ) ) , internal_error : e . internal_error , } } ) ? ; if algo_id != expected_algo_id { return Err ( CryptoError :: MalformedPublicKey { algorithm , key_bytes : Some ( der . to_vec ( ) ) , internal_error : format ! ( " " algorithm expected_algo_id algo_id ) , } ) ; } if let Some ( expected_pk_len ) = expected_pk_len { if pk_bytes . len ( ) != expected_pk_len { return Err ( CryptoError :: MalformedPublicKey { algorithm , key_bytes : Some ( der . to_vec ( ) ) , internal_error : format ! ( " " algorithm expected_pk_len pk_bytes len ( ) ) , } ) ; } } Ok ( pk_bytes ) }
Parse a public key and verify if it is of the expected type and size
The format is SubjectPublicKeyInfo as defined in RFC 5480
Parse a public key and verify if it is of the expected type and size The format is SubjectPublicKeyInfo as defined in RFC 5480
der
&[u8]
algorithm
AlgorithmId
expected_algo_id
PkixAlgorithmIdentifier
expected_pk_len
Option<usize>