pub fn public_key_from_der(pk_der: &[u8]) -> CryptoResult<types::PublicKeyBytes> {
    let pkey = PKey::public_key_from_der(pk_der).map_err(|e| CryptoError::MalformedPublicKey {
        algorithm: AlgorithmId::EcdsaP256,
        key_bytes: Some(Vec::from(pk_der)),
        internal_error: e.to_string(),
    })?;
    let ec_key = pkey.ec_key().map_err(|e| CryptoError::MalformedPublicKey {
        algorithm: AlgorithmId::EcdsaP256,
        key_bytes: Some(Vec::from(pk_der)),
        internal_error: e.to_string(),
    })?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let pk_bytes = ec_key
        .public_key()
        .to_bytes(
            &group,
            openssl::ec::PointConversionForm::UNCOMPRESSED,
            &mut ctx,
        )
        .map_err(|e| CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaP256,
            key_bytes: Some(Vec::from(pk_der)),
            internal_error: e.to_string(),
        })?;
    // Check pk_der is in canonical form (uncompressed).
    let canon =
        public_key_to_der(&types::PublicKeyBytes::from(pk_bytes.clone())).map_err(|_e| {
            CryptoError::MalformedPublicKey {
                algorithm: AlgorithmId::EcdsaP256,
                key_bytes: Some(Vec::from(pk_der)),
                internal_error: "cannot encode decoded key".to_string(),
            }
        })?;
    if canon != pk_der {
        return Err(CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaP256,
            key_bytes: Some(Vec::from(pk_der)),
            internal_error: "non-canonical encoding".to_string(),
        });
    }
    Ok(types::PublicKeyBytes::from(pk_bytes))
}
/// Parse a secp256r1 public key from the DER enncoding
///
/// # Arguments
/// * `pk_der` is the binary DER encoding of the public key
/// # Errors
/// * `AlgorithmNotSupported` if an error occured while invoking OpenSSL
/// * `MalformedPublicKey` if the public key could not be parsed
/// # Returns
/// The decoded public key
Parse a secp256r1 public key from the DER enncoding
Parse a secp256r1 public key from the DER enncoding
pub fn public_key_from_der(pk_der: &[u8]) -> CryptoResult<types::PublicKeyBytes> {
    let pkey = PKey::public_key_from_der(pk_der).map_err(|e| CryptoError::MalformedPublicKey {
        algorithm: AlgorithmId::EcdsaP256,
        key_bytes: Some(Vec::from(pk_der)),
        internal_error: e.to_string(),
    })?;
    let ec_key = pkey.ec_key().map_err(|e| CryptoError::MalformedPublicKey {
        algorithm: AlgorithmId::EcdsaP256,
        key_bytes: Some(Vec::from(pk_der)),
        internal_error: e.to_string(),
    })?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let pk_bytes = ec_key
        .public_key()
        .to_bytes(
            &group,
            openssl::ec::PointConversionForm::UNCOMPRESSED,
            &mut ctx,
        )
        .map_err(|e| CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaP256,
            key_bytes: Some(Vec::from(pk_der)),
            internal_error: e.to_string(),
        })?;
    // Check pk_der is in canonical form (uncompressed).
    let canon =
        public_key_to_der(&types::PublicKeyBytes::from(pk_bytes.clone())).map_err(|_e| {
            CryptoError::MalformedPublicKey {
                algorithm: AlgorithmId::EcdsaP256,
                key_bytes: Some(Vec::from(pk_der)),
                internal_error: "cannot encode decoded key".to_string(),
            }
        })?;
    if canon != pk_der {
        return Err(CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaP256,
            key_bytes: Some(Vec::from(pk_der)),
            internal_error: "non-canonical encoding".to_string(),
        });
    }
    Ok(types::PublicKeyBytes::from(pk_bytes))
}
pub fn public_key_from_der ( pk_der : & [ u8 ] ) -> CryptoResult < types :: PublicKeyBytes > { let pkey = PKey :: public_key_from_der ( pk_der ) . map_err ( | e | CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaP256 , key_bytes : Some ( Vec :: from ( pk_der ) ) , internal_error : e . to_string ( ) , } ) ? ; let ec_key = pkey . ec_key ( ) . map_err ( | e | CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaP256 , key_bytes : Some ( Vec :: from ( pk_der ) ) , internal_error : e . to_string ( ) , } ) ? ; let mut ctx = BigNumContext :: new ( ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let group = EcGroup :: from_curve_name ( CURVE_NAME ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let pk_bytes = ec_key . public_key ( ) . to_bytes ( & group , openssl :: ec :: PointConversionForm :: UNCOMPRESSED , & mut ctx , ) . map_err ( | e | CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaP256 , key_bytes : Some ( Vec :: from ( pk_der ) ) , internal_error : e . to_string ( ) , } ) ? ; // Check pk_der is in canonical form (uncompressed). let canon = public_key_to_der ( & types :: PublicKeyBytes :: from ( pk_bytes . clone ( ) ) ) . map_err ( | _e | { CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaP256 , key_bytes : Some ( Vec :: from ( pk_der ) ) , internal_error : " " . to_string ( ) , } } ) ? ; if canon != pk_der { return Err ( CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaP256 , key_bytes : Some ( Vec :: from ( pk_der ) ) , internal_error : " " . to_string ( ) , } ) ; } Ok ( types :: PublicKeyBytes :: from ( pk_bytes ) ) }
Parse a secp256r1 public key from the DER enncoding
Parse a secp256r1 public key from the DER enncoding
pk_der
&[u8]
The decoded public key
The decoded public key
pk_der
&[u8]
errors
`AlgorithmNotSupported` if an error occured while invoking OpenSSL
`MalformedPublicKey` if the public key could not be parsed
` AlgorithmNotSupported ` if an error occured while invoking OpenSSL ` MalformedPublicKey ` if the public key could not be parsed