feat: v1.10.18, add parse-ecdsa-signature
This commit is contained in:
64
src/cmd_parseecdsasignature.rs
Normal file
64
src/cmd_parseecdsasignature.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
use rust_util::util_msg;
|
||||
|
||||
use crate::ecdsautil::parse_ecdsa_r_and_s;
|
||||
use crate::util::try_decode;
|
||||
|
||||
const SEPARATOR: &str = ".";
|
||||
|
||||
pub struct CommandImpl;
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> &str {
|
||||
"parse-ecdsa-signature"
|
||||
}
|
||||
|
||||
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||
SubCommand::with_name(self.name())
|
||||
.about("Parse ECDSA signature")
|
||||
.arg(
|
||||
Arg::with_name("signature")
|
||||
.long("signature")
|
||||
.required(true)
|
||||
.takes_value(true)
|
||||
.help("ECDSA signature"),
|
||||
)
|
||||
.arg(Arg::with_name("json").long("json").help("JSON output"))
|
||||
}
|
||||
|
||||
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
|
||||
let json_output = sub_arg_matches.is_present("json");
|
||||
if json_output {
|
||||
util_msg::set_logger_std_out(false);
|
||||
}
|
||||
|
||||
let mut json = BTreeMap::<&'_ str, String>::new();
|
||||
|
||||
let signature = sub_arg_matches.value_of("signature").unwrap();
|
||||
|
||||
let signature_der = try_decode(signature)?;
|
||||
|
||||
let (r, s) = parse_ecdsa_r_and_s(&signature_der)?;
|
||||
let mut r_and_s = r.clone();
|
||||
r_and_s.extend_from_slice(&s);
|
||||
|
||||
if json_output {
|
||||
json.insert("r", hex::encode(&r));
|
||||
json.insert("s", hex::encode(&s));
|
||||
json.insert("rs", hex::encode(&r_and_s));
|
||||
} else {
|
||||
information!("R: {}", hex::encode(&r));
|
||||
information!("S: {}", hex::encode(&s));
|
||||
information!("RS: {}", hex::encode(&r_and_s));
|
||||
}
|
||||
|
||||
if json_output {
|
||||
println!("{}", serde_json::to_string_pretty(&json).unwrap());
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@ mod cmd_sshpubkey;
|
||||
mod cmd_u2fregister;
|
||||
mod cmd_u2fsign;
|
||||
mod cmd_verifyfile;
|
||||
mod cmd_parseecdsasignature;
|
||||
mod digest;
|
||||
mod ecdhutil;
|
||||
mod ecdsautil;
|
||||
@@ -142,6 +143,7 @@ fn inner_main() -> CommandError {
|
||||
#[cfg(feature = "with-secure-enclave")]
|
||||
Box::new(cmd_se_ecdh::CommandImpl),
|
||||
Box::new(cmd_ecverify::CommandImpl),
|
||||
Box::new(cmd_parseecdsasignature::CommandImpl),
|
||||
];
|
||||
|
||||
#[allow(clippy::vec_init_then_push)]
|
||||
|
||||
@@ -17,12 +17,19 @@ pub fn base64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
|
||||
STANDARD.decode(input)
|
||||
}
|
||||
|
||||
pub fn base64_uri_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
|
||||
URL_SAFE_NO_PAD.decode(input)
|
||||
}
|
||||
|
||||
pub fn try_decode(input: &str) -> XResult<Vec<u8>> {
|
||||
match hex::decode(input) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => match base64_decode(input) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => simple_error!("decode hex or base64 error: {}", e),
|
||||
Err(_) => match base64_uri_decode(input) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => simple_error!("decode hex or base64 error: {}", e),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user