4
4
//! Module for exposing a [`signature::Signer`] interface for keys
5
5
//!
6
6
//! This modules presents objects held in a TPM over a [`signature::DigestSigner`] interface.
7
- use super :: TransientKeyContext ;
8
7
use crate :: {
9
8
abstraction:: {
10
9
public:: AssociatedTpmCurve ,
11
- transient:: { KeyMaterial , KeyParams } ,
10
+ transient:: { KeyMaterial , KeyParams , TransientKeyContext } ,
12
11
AssociatedHashingAlgorithm ,
13
12
} ,
13
+ handles:: KeyHandle ,
14
14
interface_types:: algorithm:: EccSchemeAlgorithm ,
15
- structures:: { Auth , Digest as TpmDigest , EccScheme , Signature as TpmSignature } ,
16
- Error , WrapperErrorKind ,
15
+ structures:: {
16
+ Auth , Digest as TpmDigest , EccScheme , Public , Signature as TpmSignature , SignatureScheme ,
17
+ } ,
18
+ utils:: PublicKey as TpmPublicKey ,
19
+ Context , Error , WrapperErrorKind ,
17
20
} ;
18
21
19
22
use std:: { convert:: TryFrom , ops:: Add , sync:: Mutex } ;
@@ -31,21 +34,86 @@ use elliptic_curve::{
31
34
subtle:: CtOption ,
32
35
AffinePoint , CurveArithmetic , FieldBytesSize , PrimeCurve , PublicKey , Scalar ,
33
36
} ;
37
+ use log:: error;
34
38
use signature:: { DigestSigner , Error as SigError , KeypairRef , Signer } ;
35
39
use x509_cert:: {
36
40
der:: asn1:: AnyRef ,
37
41
spki:: { AlgorithmIdentifier , AssociatedAlgorithmIdentifier , SignatureAlgorithmIdentifier } ,
38
42
} ;
39
43
44
+ pub trait TpmSigner {
45
+ fn public ( & self ) -> crate :: Result < TpmPublicKey > ;
46
+ fn key_params ( & self ) -> crate :: Result < KeyParams > ;
47
+ fn sign ( & self , digest : TpmDigest ) -> crate :: Result < TpmSignature > ;
48
+ }
49
+
50
+ impl TpmSigner for ( Mutex < & ' _ mut Context > , KeyHandle ) {
51
+ fn public ( & self ) -> crate :: Result < TpmPublicKey > {
52
+ let mut context = self . 0 . lock ( ) . expect ( "Mutex got poisoned" ) ;
53
+ let ( public, _, _) = context. read_public ( self . 1 ) ?;
54
+
55
+ TpmPublicKey :: try_from ( public)
56
+ }
57
+
58
+ fn key_params ( & self ) -> crate :: Result < KeyParams > {
59
+ let mut context = self . 0 . lock ( ) . expect ( "Mutex got poisoned" ) ;
60
+ let ( public, _, _) = context. read_public ( self . 1 ) ?;
61
+
62
+ match public {
63
+ Public :: Rsa { parameters, .. } => Ok ( KeyParams :: Rsa {
64
+ size : parameters. key_bits ( ) ,
65
+ scheme : parameters. rsa_scheme ( ) ,
66
+ pub_exponent : parameters. exponent ( ) ,
67
+ } ) ,
68
+ Public :: Ecc { parameters, .. } => Ok ( KeyParams :: Ecc {
69
+ curve : parameters. ecc_curve ( ) ,
70
+ scheme : parameters. ecc_scheme ( ) ,
71
+ } ) ,
72
+ other => {
73
+ error ! ( "Unsupported key parameter used: {other:?}" ) ;
74
+ Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) )
75
+ }
76
+ }
77
+ }
78
+
79
+ fn sign ( & self , digest : TpmDigest ) -> crate :: Result < TpmSignature > {
80
+ let mut context = self . 0 . lock ( ) . expect ( "Mutex got poisoned" ) ;
81
+ context. sign ( self . 1 , digest, SignatureScheme :: Null , None )
82
+ }
83
+ }
84
+
85
+ impl TpmSigner
86
+ for (
87
+ Mutex < & ' _ mut TransientKeyContext > ,
88
+ KeyMaterial ,
89
+ KeyParams ,
90
+ Option < Auth > ,
91
+ )
92
+ {
93
+ fn public ( & self ) -> crate :: Result < TpmPublicKey > {
94
+ Ok ( self . 1 . public ( ) . clone ( ) )
95
+ }
96
+
97
+ fn key_params ( & self ) -> crate :: Result < KeyParams > {
98
+ Ok ( self . 2 )
99
+ }
100
+
101
+ fn sign ( & self , digest : TpmDigest ) -> crate :: Result < TpmSignature > {
102
+ let mut context = self . 0 . lock ( ) . expect ( "Mutex got poisoned" ) ;
103
+ context. sign ( self . 1 . clone ( ) , self . 2 , self . 3 . clone ( ) , digest)
104
+ }
105
+ }
106
+
40
107
/// [`EcSigner`] will sign a payload with an elliptic curve secret key stored on the TPM.
41
108
///
42
109
/// # Parameters
43
110
///
44
111
/// Parameter `C` describes the curve that is of use (Nist P-256, Nist P-384, ...)
45
112
///
46
113
/// ```no_run
114
+ /// # use std::sync::Mutex;
47
115
/// # use tss_esapi::{
48
- /// # abstraction::transient:: {EcSigner, TransientKeyContextBuilder},
116
+ /// # abstraction::{EcSigner, transient:: TransientKeyContextBuilder},
49
117
/// # TctiNameConf
50
118
/// # };
51
119
/// use p256::NistP256;
@@ -59,52 +127,57 @@ use x509_cert::{
59
127
/// # .build()
60
128
/// # .expect("Failed to create Context");
61
129
///
130
+ /// let key_params = EcSigner::<NistP256, ()>::key_params_default();
62
131
/// let (tpm_km, _tpm_auth) = context
63
- /// .create_key(EcSigner::<NistP256>::key_params_default() , 0)
132
+ /// .create_key(key_params , 0)
64
133
/// .expect("Failed to create a private keypair");
65
134
///
66
- /// let signer = EcSigner::<NistP256>::new(&mut context, tpm_km, None)
135
+ /// let signer = EcSigner::<NistP256,_ >::new((Mutex::new( &mut context) , tpm_km, key_params, None) )
67
136
/// .expect("Failed to create a signer");
68
137
/// let signature: p256::ecdsa::Signature = signer.sign(b"Hello Bob, Alice here.");
69
138
/// ```
70
139
#[ derive( Debug ) ]
71
- pub struct EcSigner < ' ctx , C >
140
+ pub struct EcSigner < C , Ctx >
72
141
where
73
142
C : PrimeCurve + CurveArithmetic ,
74
143
{
75
- context : Mutex < & ' ctx mut TransientKeyContext > ,
76
- key_material : KeyMaterial ,
77
- key_auth : Option < Auth > ,
144
+ context : Ctx ,
78
145
verifying_key : VerifyingKey < C > ,
79
146
}
80
147
81
- impl < ' ctx , C > EcSigner < ' ctx , C >
148
+ impl < C , Ctx > EcSigner < C , Ctx >
82
149
where
83
150
C : PrimeCurve + CurveArithmetic ,
84
151
C : AssociatedTpmCurve ,
85
152
FieldBytesSize < C > : ModulusSize ,
86
153
AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
154
+
155
+ Ctx : TpmSigner ,
87
156
{
88
- pub fn new (
89
- context : & ' ctx mut TransientKeyContext ,
90
- key_material : KeyMaterial ,
91
- key_auth : Option < Auth > ,
92
- ) -> Result < Self , Error > {
93
- let context = Mutex :: new ( context) ;
94
-
95
- let public_key = PublicKey :: try_from ( key_material. public ( ) ) ?;
157
+ pub fn new ( context : Ctx ) -> Result < Self , Error > {
158
+ match context. key_params ( ) ? {
159
+ KeyParams :: Ecc { curve, .. } if curve == C :: TPM_CURVE => { }
160
+ other => {
161
+ error ! (
162
+ "Unsupported key parameters: {other:?}, expected Ecc(curve: {:?})" ,
163
+ C :: default ( )
164
+ ) ;
165
+ return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
166
+ }
167
+ }
168
+
169
+ let public_key = context. public ( ) ?;
170
+ let public_key = PublicKey :: try_from ( & public_key) ?;
96
171
let verifying_key = VerifyingKey :: from ( public_key) ;
97
172
98
173
Ok ( Self {
99
174
context,
100
- key_material,
101
- key_auth,
102
175
verifying_key,
103
176
} )
104
177
}
105
178
}
106
179
107
- impl < C > EcSigner < ' _ , C >
180
+ impl < C , Ctx > EcSigner < C , Ctx >
108
181
where
109
182
C : PrimeCurve + CurveArithmetic ,
110
183
C : AssociatedTpmCurve ,
@@ -137,7 +210,7 @@ where
137
210
}
138
211
}
139
212
140
- impl < C > AsRef < VerifyingKey < C > > for EcSigner < ' _ , C >
213
+ impl < C , Ctx > AsRef < VerifyingKey < C > > for EcSigner < C , Ctx >
141
214
where
142
215
C : PrimeCurve + CurveArithmetic ,
143
216
Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
@@ -148,7 +221,7 @@ where
148
221
}
149
222
}
150
223
151
- impl < C > KeypairRef for EcSigner < ' _ , C >
224
+ impl < C , Ctx > KeypairRef for EcSigner < C , Ctx >
152
225
where
153
226
C : PrimeCurve + CurveArithmetic ,
154
227
Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
@@ -157,7 +230,7 @@ where
157
230
type VerifyingKey = VerifyingKey < C > ;
158
231
}
159
232
160
- impl < C , D > DigestSigner < D , Signature < C > > for EcSigner < ' _ , C >
233
+ impl < C , Ctx , D > DigestSigner < D , Signature < C > > for EcSigner < C , Ctx >
161
234
where
162
235
C : PrimeCurve + CurveArithmetic ,
163
236
C : AssociatedTpmCurve ,
@@ -166,20 +239,13 @@ where
166
239
Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
167
240
SignatureSize < C > : ArrayLength < u8 > ,
168
241
TpmDigest : From < Output < D > > ,
242
+ Ctx : TpmSigner ,
169
243
{
170
244
fn try_sign_digest ( & self , digest : D ) -> Result < Signature < C > , SigError > {
171
245
let digest = TpmDigest :: from ( digest. finalize_fixed ( ) ) ;
172
246
173
- let key_params = Self :: key_params :: < D > ( ) ;
174
- let mut context = self . context . lock ( ) . expect ( "Mutex got poisoned" ) ;
175
- let signature = context
176
- . sign (
177
- self . key_material . clone ( ) ,
178
- key_params,
179
- self . key_auth . clone ( ) ,
180
- digest,
181
- )
182
- . map_err ( SigError :: from_source) ?;
247
+ //let key_params = Self::key_params::<D>();
248
+ let signature = self . context . sign ( digest) . map_err ( SigError :: from_source) ?;
183
249
184
250
let TpmSignature :: EcDsa ( signature) = signature else {
185
251
return Err ( SigError :: from_source ( Error :: local_error (
@@ -193,7 +259,7 @@ where
193
259
}
194
260
}
195
261
196
- impl < C , D > DigestSigner < D , DerSignature < C > > for EcSigner < ' _ , C >
262
+ impl < C , Ctx , D > DigestSigner < D , DerSignature < C > > for EcSigner < C , Ctx >
197
263
where
198
264
C : PrimeCurve + CurveArithmetic ,
199
265
C : AssociatedTpmCurve ,
@@ -205,28 +271,32 @@ where
205
271
206
272
MaxSize < C > : ArrayLength < u8 > ,
207
273
<FieldBytesSize < C > as Add >:: Output : Add < MaxOverhead > + ArrayLength < u8 > ,
274
+
275
+ Ctx : TpmSigner ,
208
276
{
209
277
fn try_sign_digest ( & self , digest : D ) -> Result < DerSignature < C > , SigError > {
210
278
let signature: Signature < _ > = self . try_sign_digest ( digest) ?;
211
279
Ok ( signature. to_der ( ) )
212
280
}
213
281
}
214
282
215
- impl < C > Signer < Signature < C > > for EcSigner < ' _ , C >
283
+ impl < C , Ctx > Signer < Signature < C > > for EcSigner < C , Ctx >
216
284
where
217
285
C : PrimeCurve + CurveArithmetic + DigestPrimitive ,
218
286
C : AssociatedTpmCurve ,
219
287
<C as DigestPrimitive >:: Digest : AssociatedHashingAlgorithm ,
220
288
Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
221
289
SignatureSize < C > : ArrayLength < u8 > ,
222
290
TpmDigest : From < Output < <C as DigestPrimitive >:: Digest > > ,
291
+
292
+ Ctx : TpmSigner ,
223
293
{
224
294
fn try_sign ( & self , msg : & [ u8 ] ) -> Result < Signature < C > , SigError > {
225
295
self . try_sign_digest ( C :: Digest :: new_with_prefix ( msg) )
226
296
}
227
297
}
228
298
229
- impl < C > Signer < DerSignature < C > > for EcSigner < ' _ , C >
299
+ impl < C , Ctx > Signer < DerSignature < C > > for EcSigner < C , Ctx >
230
300
where
231
301
C : PrimeCurve + CurveArithmetic + DigestPrimitive ,
232
302
C : AssociatedTpmCurve ,
@@ -237,13 +307,15 @@ where
237
307
238
308
MaxSize < C > : ArrayLength < u8 > ,
239
309
<FieldBytesSize < C > as Add >:: Output : Add < MaxOverhead > + ArrayLength < u8 > ,
310
+
311
+ Ctx : TpmSigner ,
240
312
{
241
313
fn try_sign ( & self , msg : & [ u8 ] ) -> Result < DerSignature < C > , SigError > {
242
314
self . try_sign_digest ( C :: Digest :: new_with_prefix ( msg) )
243
315
}
244
316
}
245
317
246
- impl < C > SignatureAlgorithmIdentifier for EcSigner < ' _ , C >
318
+ impl < C , Ctx > SignatureAlgorithmIdentifier for EcSigner < C , Ctx >
247
319
where
248
320
C : PrimeCurve + CurveArithmetic ,
249
321
Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
0 commit comments