Skip to content

Commit af94fc8

Browse files
committed
introduce RsaParseError
1 parent c54c14f commit af94fc8

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

anchor/common/ssv_types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use cluster::{Cluster, ClusterId, ClusterMember, ValidatorIndex, ValidatorMe
22
pub use committee::{CommitteeId, CommitteeInfo};
33
pub use operator::{Operator, OperatorId};
44
pub use share::Share;
5-
pub use util::parse_rsa;
5+
pub use util::{RsaParseError, parse_rsa};
66
mod cluster;
77
mod committee;
88
pub mod consensus;

anchor/common/ssv_types/src/operator.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use openssl::{pkey::Public, rsa::Rsa};
55
use ssz_derive::{Decode, Encode};
66
use types::Address;
77

8-
use crate::util::parse_rsa;
8+
use crate::{RsaParseError, util::parse_rsa};
99

1010
/// Unique identifier for an Operator.
1111
#[derive(
@@ -41,7 +41,11 @@ pub struct Operator {
4141

4242
impl Operator {
4343
/// Creates a new operator from its OperatorId and PEM-encoded public key string
44-
pub fn new(pem_data: &str, operator_id: OperatorId, owner: Address) -> Result<Self, String> {
44+
pub fn new(
45+
pem_data: &str,
46+
operator_id: OperatorId,
47+
owner: Address,
48+
) -> Result<Self, RsaParseError> {
4549
let rsa_pubkey = parse_rsa(pem_data)?;
4650
Ok(Self::new_with_pubkey(rsa_pubkey, operator_id, owner))
4751
}
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
use base64::prelude::*;
22
use openssl::{pkey::Public, rsa::Rsa};
3+
use thiserror::Error;
4+
5+
/// Errors that can occur during RSA key parsing
6+
#[derive(Error, Debug, Clone)]
7+
pub enum RsaParseError {
8+
#[error("Unable to decode base64 PEM data: {0}")]
9+
Base64Decode(#[from] base64::DecodeError),
10+
11+
#[error("Unable to convert decoded PEM data into a string: {0}")]
12+
Utf8Conversion(#[from] std::string::FromUtf8Error),
13+
14+
#[error("Failed to parse RSA public key: {source}")]
15+
RsaParsing {
16+
#[source]
17+
source: openssl::error::ErrorStack,
18+
},
19+
}
20+
21+
impl From<RsaParseError> for String {
22+
fn from(error: RsaParseError) -> Self {
23+
error.to_string()
24+
}
25+
}
326

427
// Parse from a RSA public key string into the associated RSA representation
5-
pub fn parse_rsa(pem_data: &str) -> Result<Rsa<Public>, String> {
28+
pub fn parse_rsa(pem_data: &str) -> Result<Rsa<Public>, RsaParseError> {
629
// First decode the base64 data
7-
let pem_decoded = BASE64_STANDARD
8-
.decode(pem_data)
9-
.map_err(|e| format!("Unable to decode base64 pem data: {e}"))?;
30+
let pem_decoded = BASE64_STANDARD.decode(pem_data)?;
1031

1132
// Convert the decoded data to a string
12-
let mut pem_string = String::from_utf8(pem_decoded)
13-
.map_err(|e| format!("Unable to convert decoded pem data into a string: {e}"))?;
33+
let mut pem_string = String::from_utf8(pem_decoded)?;
1434

1535
// Fix the header - replace PKCS1 header with PKCS8 header
1636
pem_string = pem_string
@@ -22,7 +42,7 @@ pub fn parse_rsa(pem_data: &str) -> Result<Rsa<Public>, String> {
2242

2343
// Parse the PEM string into an RSA public key using PKCS8 format
2444
let rsa_pubkey = Rsa::public_key_from_pem(pem_string.as_bytes())
25-
.map_err(|e| format!("Failed to parse RSA public key: {e}"))?;
45+
.map_err(|source| RsaParseError::RsaParsing { source })?;
2646

2747
Ok(rsa_pubkey)
2848
}

0 commit comments

Comments
 (0)