Convert PEM RSA public key to JWK format (imcomplete)
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
use clap::{crate_authors, crate_version, App, Arg}; | |
use simple_asn1::ASN1Block; | |
use std::fs; | |
use std::io::prelude::*; | |
use std::str; | |
fn main() { | |
let matches = App::new("view-ans1") | |
.version(crate_version!()) | |
.author(crate_authors!()) | |
.arg( | |
Arg::with_name("ans1") | |
.index(1) | |
.takes_value(true) | |
.required(true) | |
.help("Ans.1 encoded file"), | |
) | |
.arg( | |
Arg::with_name("pem") | |
.long("pem") | |
.short("p") | |
.help("PEM file"), | |
) | |
.get_matches(); | |
let mut buffer = Vec::new(); | |
fs::File::open(matches.value_of("ans1").unwrap()) | |
.unwrap() | |
.read_to_end(&mut buffer) | |
.unwrap(); | |
let parsed = pem::parse(&buffer).unwrap(); | |
let decoded = simple_asn1::from_der(&parsed.contents).unwrap(); | |
print_ans1(&decoded, 0); | |
if let ASN1Block::Sequence(_, s) = &decoded[0] { | |
println!("data: {:?}", s[1]); | |
if let ASN1Block::BitString(a, b, d) = &s[1] { | |
let new_decoded = simple_asn1::from_der(&d).unwrap(); | |
//println!("base64: {}", base64_url::encode(&d)); | |
if let ASN1Block::Sequence(_, s) = &new_decoded[0] { | |
println!("new data: {:?}", s[0]); | |
if let ASN1Block::Integer(_, i) = &s[0] { | |
let b = i.to_bytes_be(); | |
println!("base64: {}", base64_url::encode(&b.1)); // JWK: n entry | |
} | |
} else { | |
unreachable!() | |
} | |
} else { | |
unreachable!() | |
} | |
} else { | |
unreachable!() | |
} | |
} | |
fn print_ans1(data: &[ASN1Block], indent: u32) { | |
let mut indent_str = String::new(); | |
for _ in 0..indent { | |
indent_str.push_str(" "); | |
} | |
for one in data { | |
match one { | |
ASN1Block::Sequence(s, d) => { | |
println!("{}sequence [{}]:", indent_str, s); | |
print_ans1(d, indent + 1); | |
} | |
_ => { | |
println!("{}{:?}", indent_str, one); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment