Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Antonov <[email protected]>
  • Loading branch information
anta5010 committed Sep 23, 2021
1 parent 27050d1 commit 057808a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ Book](https://parallaxsecond.github.io/parsec-book/parsec_client/operations/inde
- Plaintext data is expected/shown as a UTF-8 string (input data of `sign`, output data of
`decrypt`).
- Ciphertext data is expected/shown as base 64 (output data of `sign`, input data of `decrypt`).
- Exported public keys are encoded in PEM. By default PKCS#8 format is used for RSA and ECC
public keys. With `--pkcs1` parameter RSA keys exported in PKCS#1 format.
- Exported public keys are encoded in PEM. By default PKCS#8 format
is used for RSA [RFC 3279](https://datatracker.ietf.org/doc/html/rfc3279#section-2.3.1)
and ECC [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480#section-2)
public keys. With `--pkcs1` parameter RSA keys exported in PKCS#1 format
[RFC 2313](https://datatracker.ietf.org/doc/html/rfc2313#section-7.1).

## SPIFFE based authenticator

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub enum ToolErrorKind {
/// Expected input data was not given
#[error("A command expected input data that was not given")]
NoInput,

/// Cannot serialise or deserialise data
#[error("Incorrect data format")]
IncorrectData,
}

/// A Result type with the Err variant set as a ParsecToolError
Expand Down
36 changes: 23 additions & 13 deletions src/subcommands/export_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,29 @@ impl ExportPublicKey {
/// Exports a public key.
pub fn run(&self, basic_client: BasicClient) -> Result<()> {
let mut tag = String::from("PUBLIC KEY");
let psa_public_key = basic_client.psa_export_public_key(&self.key_name)?;
let mut public_key = psa_public_key.clone();
let mut psa_public_key = basic_client.psa_export_public_key(&self.key_name)?;
let psa_key_attributes = basic_client.key_attributes(&self.key_name)?;

match basic_client.key_attributes(&self.key_name)?.key_type {
match psa_key_attributes.key_type {
Type::RsaKeyPair | Type::RsaPublicKey => {
if self.pkcs1 {
tag = String::from("RSA PUBLIC KEY");
} else {
let pkcs8_public_key = SubjectPublicKeyInfo {
psa_public_key = picky_asn1_der::to_vec(&SubjectPublicKeyInfo {
algorithm: AlgorithmIdentifier::new_rsa_encryption(),
subject_public_key: PublicKey::Rsa(
picky_asn1_der::from_bytes::<RsaPublicKey>(&psa_public_key)
.unwrap()
.map_err(|_| {
error!("Could not deserialise RSA key");
ToolErrorKind::IncorrectData
})?
.into(),
),
};
public_key = picky_asn1_der::to_vec(&pkcs8_public_key).unwrap();
})
.map_err(|_| {
error!("Could not serialise RSA key");
ToolErrorKind::IncorrectData
})?;
}
}
Type::EccKeyPair {
Expand All @@ -58,16 +64,20 @@ impl ExportPublicKey {
error!("PKCS1 format doesn't support ECC keys");
return Err(ToolErrorKind::WrongKeyAlgorithm.into());
} else {
let key_bits = basic_client.key_attributes(&self.key_name)?.bits;
let pkcs8_public_key = SubjectPublicKeyInfo {
psa_public_key = picky_asn1_der::to_vec(&SubjectPublicKeyInfo {
algorithm: AlgorithmIdentifier::new_elliptic_curve(
EcParameters::NamedCurve(curve_oid(curve, key_bits).unwrap().into()),
EcParameters::NamedCurve(
curve_oid(curve, psa_key_attributes.bits)?.into(),
),
),
subject_public_key: PublicKey::Ec(
BitString::with_bytes(psa_public_key).into(),
),
};
public_key = picky_asn1_der::to_vec(&pkcs8_public_key).unwrap();
})
.map_err(|_| {
error!("Could not serialise ECC key");
ToolErrorKind::IncorrectData
})?;
}
}
_ => {
Expand All @@ -79,7 +89,7 @@ impl ExportPublicKey {
let pem_encoded = pem::encode_config(
&pem::Pem {
tag,
contents: public_key,
contents: psa_public_key,
},
pem::EncodeConfig {
line_ending: pem::LineEnding::LF,
Expand Down

0 comments on commit 057808a

Please sign in to comment.