fix compiler error and update error library
This commit is contained in:
parent
93116d216a
commit
9e161e6dd9
4 changed files with 30 additions and 31 deletions
|
|
@ -4,7 +4,7 @@ Convert an RSA public key between Json Web Key and DER/PEM format.
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
Json Web Key CLI 0.1.0
|
Json Web Key CLI 0.1.1
|
||||||
Okamura Yasunobu <okamura@informationsea.info>
|
Okamura Yasunobu <okamura@informationsea.info>
|
||||||
Convert an RSA public key between Json Web Key and DER/PEM format
|
Convert an RSA public key between Json Web Key and DER/PEM format
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "jsonwebkey-cli"
|
name = "jsonwebkey-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Okamura Yasunobu <okamura@informationsea.info>"]
|
authors = ["Okamura Yasunobu <okamura@informationsea.info>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
|
@ -14,5 +14,5 @@ categories = ["authentication"]
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
jsonwebkey-convert = {version = "0.1", path = "../jsonwebkey-convert"}
|
jsonwebkey-convert = {version = "0.2", path = "../jsonwebkey-convert"}
|
||||||
clap = "2"
|
clap = "2"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "jsonwebkey-convert"
|
name = "jsonwebkey-convert"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["Okamura Yasunobu <okamura@informationsea.info>"]
|
authors = ["Okamura Yasunobu <okamura@informationsea.info>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
|
@ -22,4 +22,5 @@ serde_json = "1"
|
||||||
base64 = "0.12"
|
base64 = "0.12"
|
||||||
base64-url = "1"
|
base64-url = "1"
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
failure = "0.1"
|
anyhow = "1"
|
||||||
|
thiserror = "1"
|
||||||
|
|
@ -31,28 +31,30 @@
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use failure::Fail;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use simple_asn1::{oid, ASN1Block, BigInt, BigUint, OID};
|
use simple_asn1::{oid, ASN1Block, BigInt, BigUint, OID};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Fail, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum JWKConvertError {
|
pub enum JWKConvertError {
|
||||||
#[fail(display = "JWK Parse Error: {}", _0)]
|
#[error("JWK Parse Error: {0}")]
|
||||||
JWKParseError(&'static str),
|
JWKParseError(&'static str),
|
||||||
#[fail(display = "Public Key Parse Error: {}", _0)]
|
#[error("Public Key Parse Error: {0}")]
|
||||||
PubKeyParse(&'static str),
|
PubKeyParse(&'static str),
|
||||||
#[fail(display = "{}", _0)]
|
#[error(transparent)]
|
||||||
ANS1DecodeError(#[fail(cause)] simple_asn1::ASN1DecodeErr),
|
ANS1DecodeError(#[from] simple_asn1::ASN1DecodeErr),
|
||||||
#[fail(display = "{}", _0)]
|
#[error(transparent)]
|
||||||
ANS1EncodeError(#[fail(cause)] simple_asn1::ASN1EncodeErr),
|
ANS1EncodeError(#[from] simple_asn1::ASN1EncodeErr),
|
||||||
#[fail(display = "{}", _0)]
|
#[error(transparent)]
|
||||||
PEMParseErrror(#[fail(cause)] pem::PemError),
|
PEMParseErrror(#[from] pem::PemError),
|
||||||
#[fail(display = "{}", _0)]
|
#[error(transparent)]
|
||||||
Base64Error(#[fail(cause)] base64::DecodeError),
|
Base64Error(#[from] base64::DecodeError),
|
||||||
#[fail(display = "{}", _0)]
|
#[error(transparent)]
|
||||||
JSONParseError(#[fail(cause)] serde_json::Error),
|
Base64UrlError(#[from] base64_url::base64::DecodeError),
|
||||||
|
#[error(transparent)]
|
||||||
|
JSONParseError(#[from] serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|
@ -109,8 +111,8 @@ impl TryInto<RSAJWK> for RSAPubKeyJWK {
|
||||||
if self.kty != "RSA" {
|
if self.kty != "RSA" {
|
||||||
return Err(JWKConvertError::JWKParseError("Unspported type"));
|
return Err(JWKConvertError::JWKParseError("Unspported type"));
|
||||||
}
|
}
|
||||||
let n = base64_url::decode(&self.n).map_err(JWKConvertError::Base64Error)?;
|
let n = base64_url::decode(&self.n)?;
|
||||||
let e = base64_url::decode(&self.e).map_err(JWKConvertError::Base64Error)?;
|
let e = base64_url::decode(&self.e)?;
|
||||||
Ok(RSAJWK {
|
Ok(RSAJWK {
|
||||||
kid: self.kid,
|
kid: self.kid,
|
||||||
jwk_use: self.use_,
|
jwk_use: self.use_,
|
||||||
|
|
@ -144,8 +146,7 @@ impl RSAPubKey {
|
||||||
ASN1Block::Integer(0, self.e.clone()),
|
ASN1Block::Integer(0, self.e.clone()),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
let pubkey_der =
|
let pubkey_der = simple_asn1::to_der(&pubkey_asn1)?;
|
||||||
simple_asn1::to_der(&pubkey_asn1).map_err(JWKConvertError::ANS1EncodeError)?;
|
|
||||||
let asn1 = ASN1Block::Sequence(
|
let asn1 = ASN1Block::Sequence(
|
||||||
0,
|
0,
|
||||||
vec![
|
vec![
|
||||||
|
|
@ -160,7 +161,7 @@ impl RSAPubKey {
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(simple_asn1::to_der(&asn1).map_err(JWKConvertError::ANS1EncodeError)?)
|
Ok(simple_asn1::to_der(&asn1)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pem(&self) -> Result<String, JWKConvertError> {
|
pub fn to_pem(&self) -> Result<String, JWKConvertError> {
|
||||||
|
|
@ -175,14 +176,13 @@ impl RSAPubKey {
|
||||||
|
|
||||||
/// Load a Json Web Key from bytes slice
|
/// Load a Json Web Key from bytes slice
|
||||||
pub fn load_jwk(data: &[u8]) -> Result<RSAJWK, JWKConvertError> {
|
pub fn load_jwk(data: &[u8]) -> Result<RSAJWK, JWKConvertError> {
|
||||||
let jwk: RSAPubKeyJWK =
|
let jwk: RSAPubKeyJWK = serde_json::from_slice(data)?;
|
||||||
serde_json::from_slice(data).map_err(JWKConvertError::JSONParseError)?;
|
|
||||||
Ok(jwk.try_into()?)
|
Ok(jwk.try_into()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load an RSA public key from DER format
|
/// Load an RSA public key from DER format
|
||||||
pub fn load_der(data: &[u8]) -> Result<RSAPubKey, JWKConvertError> {
|
pub fn load_der(data: &[u8]) -> Result<RSAPubKey, JWKConvertError> {
|
||||||
let ans1_block_vec = simple_asn1::from_der(data).map_err(JWKConvertError::ANS1DecodeError)?;
|
let ans1_block_vec = simple_asn1::from_der(data)?;
|
||||||
if ans1_block_vec.len() != 1 {
|
if ans1_block_vec.len() != 1 {
|
||||||
return Err(JWKConvertError::PubKeyParse(
|
return Err(JWKConvertError::PubKeyParse(
|
||||||
"Invalid number of sequence: 1",
|
"Invalid number of sequence: 1",
|
||||||
|
|
@ -337,8 +337,7 @@ mod tests {
|
||||||
let generated_pem = jwk_rsa.pubkey.to_pem()?;
|
let generated_pem = jwk_rsa.pubkey.to_pem()?;
|
||||||
assert_eq!(generated_pem, str::from_utf8(&pem_data[..]).unwrap());
|
assert_eq!(generated_pem, str::from_utf8(&pem_data[..]).unwrap());
|
||||||
|
|
||||||
let jwk_parsed: RSAPubKeyJWK =
|
let jwk_parsed: RSAPubKeyJWK = serde_json::from_slice(jwk_data)?;
|
||||||
serde_json::from_slice(jwk_data).map_err(JWKConvertError::JSONParseError)?;
|
|
||||||
let pem_jwk: RSAPubKeyJWK = serde_json::from_str(
|
let pem_jwk: RSAPubKeyJWK = serde_json::from_str(
|
||||||
&RSAJWK {
|
&RSAJWK {
|
||||||
pubkey: pem_rsa,
|
pubkey: pem_rsa,
|
||||||
|
|
@ -346,8 +345,7 @@ mod tests {
|
||||||
jwk_use: jwk_rsa.jwk_use,
|
jwk_use: jwk_rsa.jwk_use,
|
||||||
}
|
}
|
||||||
.to_jwk()?,
|
.to_jwk()?,
|
||||||
)
|
)?;
|
||||||
.map_err(JWKConvertError::JSONParseError)?;
|
|
||||||
assert_eq!(jwk_parsed, pem_jwk);
|
assert_eq!(jwk_parsed, pem_jwk);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue