@@ -3,7 +3,7 @@ use crate::{agent::EnvelopeContent, export::Principal, Identity, Signature};
33#[ cfg( feature = "pem" ) ]
44use crate :: identity:: error:: PemError ;
55
6- use ring :: signature :: { Ed25519KeyPair , KeyPair } ;
6+ use ed25519_consensus :: SigningKey ;
77use simple_asn1:: {
88 oid, to_der,
99 ASN1Block :: { BitString , ObjectIdentifier , Sequence } ,
@@ -13,9 +13,11 @@ use std::fmt;
1313
1414use super :: Delegation ;
1515
16- /// A Basic Identity which sign using an ED25519 key pair.
16+ /// A cryptographic identity which signs using an Ed25519 key pair.
17+ ///
18+ /// The caller will be represented via [`Principal::self_authenticating`], which contains the SHA-224 hash of the public key.
1719pub struct BasicIdentity {
18- key_pair : Ed25519KeyPair ,
20+ private_key : KeyCompat ,
1921 der_encoded_public_key : Vec < u8 > ,
2022}
2123
@@ -28,35 +30,65 @@ impl fmt::Debug for BasicIdentity {
2830}
2931
3032impl BasicIdentity {
31- /// Create a BasicIdentity from reading a PEM file at the path.
33+ /// Create a ` BasicIdentity` from reading a PEM file at the path.
3234 #[ cfg( feature = "pem" ) ]
3335 pub fn from_pem_file < P : AsRef < std:: path:: Path > > ( file_path : P ) -> Result < Self , PemError > {
3436 Self :: from_pem ( std:: fs:: File :: open ( file_path) ?)
3537 }
3638
37- /// Create a BasicIdentity from reading a PEM File from a Reader.
39+ /// Create a ` BasicIdentity` from reading a PEM File from a Reader.
3840 #[ cfg( feature = "pem" ) ]
3941 pub fn from_pem < R : std:: io:: Read > ( pem_reader : R ) -> Result < Self , PemError > {
42+ use der:: { Decode , PemReader } ;
43+ use pkcs8:: PrivateKeyInfo ;
44+
4045 let bytes: Vec < u8 > = pem_reader
4146 . bytes ( )
4247 . collect :: < Result < Vec < u8 > , std:: io:: Error > > ( ) ?;
48+ let pki = PrivateKeyInfo :: decode ( & mut PemReader :: new ( & bytes) ?) ?;
49+ let private_key = SigningKey :: try_from ( pki. private_key ) ?;
50+ Ok ( BasicIdentity :: from_signing_key ( private_key) )
51+ }
4352
44- Ok ( BasicIdentity :: from_key_pair ( Ed25519KeyPair :: from_pkcs8 (
45- pem:: parse ( bytes) ?. contents ( ) ,
46- ) ?) )
53+ /// Create a `BasicIdentity` from a `SigningKey` from `ed25519-consensus`.
54+ pub fn from_signing_key ( key : SigningKey ) -> Self {
55+ let public_key = key. verification_key ( ) ;
56+ let der_encoded_public_key = der_encode_public_key ( public_key. as_bytes ( ) . to_vec ( ) ) ;
57+
58+ Self {
59+ private_key : KeyCompat :: Standard ( key) ,
60+ der_encoded_public_key,
61+ }
4762 }
4863
49- /// Create a BasicIdentity from a KeyPair from the ring crate.
50- pub fn from_key_pair ( key_pair : Ed25519KeyPair ) -> Self {
64+ /// Create a `BasicIdentity` from an `Ed25519KeyPair` from `ring`.
65+ #[ cfg( feature = "ring" ) ]
66+ pub fn from_key_pair ( key_pair : ring:: signature:: Ed25519KeyPair ) -> Self {
67+ use ring:: signature:: KeyPair ;
5168 let der_encoded_public_key = der_encode_public_key ( key_pair. public_key ( ) . as_ref ( ) . to_vec ( ) ) ;
52-
5369 Self {
54- key_pair,
70+ private_key : KeyCompat :: Ring ( key_pair) ,
5571 der_encoded_public_key,
5672 }
5773 }
5874}
5975
76+ enum KeyCompat {
77+ Standard ( SigningKey ) ,
78+ #[ cfg( feature = "ring" ) ]
79+ Ring ( ring:: signature:: Ed25519KeyPair ) ,
80+ }
81+
82+ impl KeyCompat {
83+ fn sign ( & self , payload : & [ u8 ] ) -> Vec < u8 > {
84+ match self {
85+ Self :: Standard ( k) => k. sign ( payload) . to_bytes ( ) . to_vec ( ) ,
86+ #[ cfg( feature = "ring" ) ]
87+ Self :: Ring ( k) => k. sign ( payload) . as_ref ( ) . to_vec ( ) ,
88+ }
89+ }
90+ }
91+
6092impl Identity for BasicIdentity {
6193 fn sender ( & self ) -> Result < Principal , String > {
6294 Ok ( Principal :: self_authenticating ( & self . der_encoded_public_key ) )
@@ -75,9 +107,9 @@ impl Identity for BasicIdentity {
75107 }
76108
77109 fn sign_arbitrary ( & self , content : & [ u8 ] ) -> Result < Signature , String > {
78- let signature = self . key_pair . sign ( content) ;
110+ let signature = self . private_key . sign ( content) ;
79111 Ok ( Signature {
80- signature : Some ( signature. as_ref ( ) . to_vec ( ) ) ,
112+ signature : Some ( signature) ,
81113 public_key : self . public_key ( ) ,
82114 delegations : None ,
83115 } )
0 commit comments