@@ -3,7 +3,7 @@ use crate::{agent::EnvelopeContent, export::Principal, Identity, Signature};
3
3
#[ cfg( feature = "pem" ) ]
4
4
use crate :: identity:: error:: PemError ;
5
5
6
- use ring :: signature :: { Ed25519KeyPair , KeyPair } ;
6
+ use ed25519_consensus :: SigningKey ;
7
7
use simple_asn1:: {
8
8
oid, to_der,
9
9
ASN1Block :: { BitString , ObjectIdentifier , Sequence } ,
@@ -13,9 +13,11 @@ use std::fmt;
13
13
14
14
use super :: Delegation ;
15
15
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.
17
19
pub struct BasicIdentity {
18
- key_pair : Ed25519KeyPair ,
20
+ private_key : KeyCompat ,
19
21
der_encoded_public_key : Vec < u8 > ,
20
22
}
21
23
@@ -28,35 +30,65 @@ impl fmt::Debug for BasicIdentity {
28
30
}
29
31
30
32
impl 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.
32
34
#[ cfg( feature = "pem" ) ]
33
35
pub fn from_pem_file < P : AsRef < std:: path:: Path > > ( file_path : P ) -> Result < Self , PemError > {
34
36
Self :: from_pem ( std:: fs:: File :: open ( file_path) ?)
35
37
}
36
38
37
- /// Create a BasicIdentity from reading a PEM File from a Reader.
39
+ /// Create a ` BasicIdentity` from reading a PEM File from a Reader.
38
40
#[ cfg( feature = "pem" ) ]
39
41
pub fn from_pem < R : std:: io:: Read > ( pem_reader : R ) -> Result < Self , PemError > {
42
+ use der:: { Decode , PemReader } ;
43
+ use pkcs8:: PrivateKeyInfo ;
44
+
40
45
let bytes: Vec < u8 > = pem_reader
41
46
. bytes ( )
42
47
. 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
+ }
43
52
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
+ }
47
62
}
48
63
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 ;
51
68
let der_encoded_public_key = der_encode_public_key ( key_pair. public_key ( ) . as_ref ( ) . to_vec ( ) ) ;
52
-
53
69
Self {
54
- key_pair,
70
+ private_key : KeyCompat :: Ring ( key_pair) ,
55
71
der_encoded_public_key,
56
72
}
57
73
}
58
74
}
59
75
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
+
60
92
impl Identity for BasicIdentity {
61
93
fn sender ( & self ) -> Result < Principal , String > {
62
94
Ok ( Principal :: self_authenticating ( & self . der_encoded_public_key ) )
@@ -75,9 +107,9 @@ impl Identity for BasicIdentity {
75
107
}
76
108
77
109
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) ;
79
111
Ok ( Signature {
80
- signature : Some ( signature. as_ref ( ) . to_vec ( ) ) ,
112
+ signature : Some ( signature) ,
81
113
public_key : self . public_key ( ) ,
82
114
delegations : None ,
83
115
} )
0 commit comments