11//! A container for a public / private key pair, or a certificate / private key.
22
3- use core :: str:: FromStr ;
3+ use std :: str:: FromStr as _ ;
44
55use ssh_encoding:: { self , CheckedSum , Decode , Encode , Reader , Writer } ;
6+ use ssh_key:: public:: KeyData ;
67use ssh_key:: { certificate:: Certificate , private:: KeypairData , Algorithm } ;
78
89use crate :: proto:: { Error , PrivateKeyData , Result } ;
@@ -16,7 +17,7 @@ use crate::proto::{Error, PrivateKeyData, Result};
1617/// This structure covers both types of identities a user may
1718/// send to an agent as part of a [`Request::AddIdentity`](crate::proto::Request::AddIdentity) message.
1819#[ derive( Clone , PartialEq , Debug ) ]
19- pub enum Credential {
20+ pub enum PrivateCredential {
2021 /// A public/private key pair
2122 Key {
2223 /// Public/private key pair data
@@ -42,37 +43,39 @@ pub enum Credential {
4243 } ,
4344}
4445
45- impl Decode for Credential {
46+ impl Decode for PrivateCredential {
4647 type Error = Error ;
4748
4849 fn decode ( reader : & mut impl Reader ) -> Result < Self > {
4950 let alg = String :: decode ( reader) ?;
5051 let cert_alg = Algorithm :: new_certificate ( & alg) ;
5152
5253 if let Ok ( algorithm) = cert_alg {
53- let certificate = reader. read_prefixed ( |reader| {
54- let cert = Certificate :: decode ( reader) ?;
55- Ok :: < _ , Error > ( cert)
56- } ) ?;
54+ let certificate = reader
55+ . read_prefixed ( |reader| {
56+ let cert = Certificate :: decode ( reader) ?;
57+ Ok :: < _ , Error > ( cert)
58+ } ) ?
59+ . into ( ) ;
5760 let privkey = PrivateKeyData :: decode_as ( reader, algorithm. clone ( ) ) ?;
5861 let comment = String :: decode ( reader) ?;
5962
60- Ok ( Credential :: Cert {
63+ Ok ( PrivateCredential :: Cert {
6164 algorithm,
62- certificate : Box :: new ( certificate ) ,
65+ certificate,
6366 privkey,
6467 comment,
6568 } )
6669 } else {
6770 let algorithm = Algorithm :: from_str ( & alg) . map_err ( ssh_encoding:: Error :: from) ?;
6871 let privkey = KeypairData :: decode_as ( reader, algorithm) ?;
6972 let comment = String :: decode ( reader) ?;
70- Ok ( Credential :: Key { privkey, comment } )
73+ Ok ( PrivateCredential :: Key { privkey, comment } )
7174 }
7275 }
7376}
7477
75- impl Encode for Credential {
78+ impl Encode for PrivateCredential {
7679 fn encoded_len ( & self ) -> ssh_encoding:: Result < usize > {
7780 match self {
7881 Self :: Key { privkey, comment } => {
@@ -113,3 +116,62 @@ impl Encode for Credential {
113116 }
114117 }
115118}
119+
120+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
121+ /// Represents a public credential.
122+ pub enum PublicCredential {
123+ /// Plain public key.
124+ Key ( KeyData ) ,
125+ /// Signed public key.
126+ Cert ( Box < Certificate > ) ,
127+ }
128+
129+ impl PublicCredential {
130+ /// Returns a reference to the [KeyData].
131+ pub fn key_data ( & self ) -> & KeyData {
132+ match self {
133+ Self :: Key ( key) => key,
134+ Self :: Cert ( cert) => cert. public_key ( ) ,
135+ }
136+ }
137+ }
138+
139+ impl Decode for PublicCredential {
140+ type Error = Error ;
141+
142+ fn decode ( reader : & mut impl Reader ) -> core:: result:: Result < Self , Self :: Error > {
143+ // TODO: implement parsing certificates
144+ Ok ( Self :: Key ( KeyData :: decode ( reader) ?) )
145+ }
146+ }
147+
148+ impl Encode for PublicCredential {
149+ fn encoded_len ( & self ) -> std:: result:: Result < usize , ssh_encoding:: Error > {
150+ match self {
151+ Self :: Key ( pubkey) => pubkey. encoded_len ( ) ,
152+ Self :: Cert ( certificate) => certificate. encoded_len ( ) ,
153+ }
154+ }
155+
156+ fn encode (
157+ & self ,
158+ writer : & mut impl ssh_encoding:: Writer ,
159+ ) -> std:: result:: Result < ( ) , ssh_encoding:: Error > {
160+ match self {
161+ Self :: Key ( pubkey) => pubkey. encode ( writer) ,
162+ Self :: Cert ( certificate) => certificate. encode ( writer) ,
163+ }
164+ }
165+ }
166+
167+ impl From < KeyData > for PublicCredential {
168+ fn from ( value : KeyData ) -> Self {
169+ Self :: Key ( value)
170+ }
171+ }
172+
173+ impl From < Certificate > for PublicCredential {
174+ fn from ( value : Certificate ) -> Self {
175+ Self :: Cert ( value. into ( ) )
176+ }
177+ }
0 commit comments