-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de7fbfc
commit 479cde7
Showing
4 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::jwk::{Base64urlUInt, Params as JWKParams, JWK}; | ||
use openpgp::{ | ||
crypto::mpi::PublicKey, | ||
packet::{ | ||
key::{KeyParts, KeyRole}, | ||
Key, | ||
}, | ||
types::{Curve, PublicKeyAlgorithm}, | ||
}; | ||
use sequoia_openpgp as openpgp; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum GpgKeyToJWKError { | ||
#[error("Unsupported GPG key type")] | ||
UnsupportedGpgKeyType, | ||
#[error("Unsupported GPG public key algorithm")] | ||
UnsupportedGpgPkAlgorithm, | ||
#[error("P-256 parse error: {0}")] | ||
P256Parse(String), | ||
#[error("Unsupported ECDSA key type: {0}")] | ||
UnsupportedEcdsaKey(String), | ||
#[error("Missing features: {0}")] | ||
MissingFeatures(&'static str), | ||
} | ||
|
||
/// Convert a GPG public key to a JWK. | ||
pub fn gpg_pkk_to_jwk<P: KeyParts, R: KeyRole>(pkk: &Key<P, R>) -> Result<JWK, GpgKeyToJWKError> { | ||
// https://datatracker.ietf.org/doc/html/rfc4253#section-6.6 | ||
if let Key::V4(key) = pkk { | ||
match key.pk_algo() { | ||
PublicKeyAlgorithm::RSAEncryptSign | ||
| PublicKeyAlgorithm::ECDSA | ||
| PublicKeyAlgorithm::EdDSA => match key.mpis() { | ||
PublicKey::RSA { e, n } => Ok(JWK::from(JWKParams::RSA( | ||
crate::jwk::RSAParams::new_public(e.value(), n.value()), | ||
))), | ||
PublicKey::ECDSA { curve, q } => { | ||
if curve == &Curve::NistP256 { | ||
#[cfg(not(feature = "p256"))] | ||
{ | ||
Err(GpgKeyToJWKError::MissingFeatures("p256")) | ||
} | ||
#[cfg(feature = "p256")] | ||
{ | ||
crate::jwk::p256_parse(q.value()) | ||
.map_err(|e| GpgKeyToJWKError::P256Parse(e.to_string())) | ||
} | ||
} else { | ||
Err(GpgKeyToJWKError::UnsupportedEcdsaKey(curve.to_string())) | ||
} | ||
} | ||
PublicKey::EdDSA { curve, q } => { | ||
Ok(JWK::from(JWKParams::OKP(crate::jwk::OctetParams { | ||
curve: curve.to_string(), | ||
public_key: Base64urlUInt(q.value().to_vec()), | ||
private_key: None, | ||
}))) | ||
} | ||
_ => panic!("Something went wrong"), | ||
}, | ||
_ => Err(GpgKeyToJWKError::UnsupportedGpgPkAlgorithm), | ||
} | ||
} else { | ||
Err(GpgKeyToJWKError::UnsupportedGpgKeyType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters