Skip to content

Commit

Permalink
try implementing via rpgp
Browse files Browse the repository at this point in the history
BUG: L148, somehow the fingerprint given by the key is not proper utf8
  • Loading branch information
fairingrey committed Jan 14, 2022
1 parent 5fcf8d3 commit ec4c007
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions did-webkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ use openpgp::{
parse::{PacketParser, Parse},
serialize::SerializeInto,
};
#[cfg(any(target_arch = "wasm32", feature = "pgp"))]
use pgp::{
composed::{PublicOrSecret, SignedPublicKey},
errors::Error as PgpError,
types::KeyTrait,
};
#[cfg(all(not(target_arch = "wasm32"), feature = "sequoia-openpgp"))]
use sequoia_openpgp as openpgp;
use sshkeys::PublicKeyKind;
Expand Down Expand Up @@ -101,8 +107,65 @@ fn parse_pubkeys_gpg(
did: &str,
bytes: Vec<u8>,
) -> Result<(Vec<VerificationMethodMap>, Vec<DIDURL>), String> {
//
unimplemented!()
use std::io::Cursor;

let mut did_urls = Vec::new();
let mut vm_maps = Vec::new();

let c = Cursor::new(bytes);
let keys = pgp::composed::signed_key::parse::from_armor_many(c)
.map_err(|e| format!("Unable to parse GPG keyring: {}", e))?
.0
.collect::<Result<Vec<PublicOrSecret>, PgpError>>()
.map_err(|e| format!("Unable to parse GPG keyring: {}", e))?;

for key in keys {
// ignore if secret key (which shouldn't happen)
let key = if let PublicOrSecret::Public(inner) = key {
inner
} else {
continue;
};
let (vm_map, did_url) = gpg_pk_to_vm(did, key).map_err(|e| {
format!(
"Unable to convert GPG public key to verification method: {}",
e
)
})?;
vm_maps.push(vm_map);
did_urls.push(did_url);
}

Ok((vm_maps, did_urls))
}

#[cfg(any(target_arch = "wasm32", feature = "pgp"))]
fn gpg_pk_to_vm(
did: &str,
key: SignedPublicKey,
) -> Result<(VerificationMethodMap, DIDURL), String> {
// BUG: Can't convert to UTF-8 here
let fingerprint: String = String::from_utf8(key.fingerprint())
.map_err(|e| format!("Error converting fingerprint to utf8: {}", e))?;

let vm_url = DIDURL {
did: did.to_string(),
fragment: Some(fingerprint),
..Default::default()
};

let armored_pgp = key
.to_armored_string(None)
.map_err(|e| format!("Failed to re-serialize cert: {}", e))?;

let vm_map = VerificationMethodMap {
id: vm_url.to_string(),
type_: "PgpVerificationKey2021".to_string(),
public_key_pgp: Some(armored_pgp),
controller: did.to_string(),
..Default::default()
};
Ok((vm_map, vm_url))
}

fn pk_to_vm_ed25519(
Expand Down

0 comments on commit ec4c007

Please sign in to comment.