1
1
#[ cfg( feature = "pem" ) ]
2
2
use pem:: Pem ;
3
+ #[ cfg( feature = "crypto" ) ]
3
4
use std:: convert:: TryFrom ;
4
5
use std:: fmt;
5
6
use yasna:: DERWriter ;
6
7
8
+ #[ cfg( any( feature = "crypto" , feature = "pem" ) ) ]
7
9
use crate :: error:: ExternalError ;
8
- use crate :: ring_like:: error as ring_error;
9
- use crate :: ring_like:: rand:: SystemRandom ;
10
- use crate :: ring_like:: signature:: {
11
- self , EcdsaKeyPair , Ed25519KeyPair , KeyPair as RingKeyPair , RsaEncoding , RsaKeyPair ,
10
+ #[ cfg( feature = "crypto" ) ]
11
+ use crate :: ring_like:: {
12
+ error as ring_error,
13
+ rand:: SystemRandom ,
14
+ signature:: {
15
+ self , EcdsaKeyPair , Ed25519KeyPair , KeyPair as RingKeyPair , RsaEncoding , RsaKeyPair ,
16
+ } ,
17
+ { ecdsa_from_pkcs8, rsa_key_pair_public_modulus_len} ,
12
18
} ;
13
- use crate :: ring_like:: { ecdsa_from_pkcs8, rsa_key_pair_public_modulus_len} ;
14
- use crate :: sign_algo:: algo:: * ;
15
- use crate :: sign_algo:: SignAlgo ;
19
+ #[ cfg( feature = "crypto" ) ]
20
+ use crate :: sign_algo:: { algo:: * , SignAlgo } ;
16
21
#[ cfg( feature = "pem" ) ]
17
22
use crate :: ENCODE_CONFIG ;
18
- use crate :: { Error , SignatureAlgorithm } ;
23
+ use crate :: { sign_algo :: SignatureAlgorithm , Error } ;
19
24
20
25
/// A key pair variant
21
26
#[ allow( clippy:: large_enum_variant) ]
22
27
pub ( crate ) enum KeyPairKind {
23
28
/// A Ecdsa key pair
29
+ #[ cfg( feature = "crypto" ) ]
24
30
Ec ( EcdsaKeyPair ) ,
25
31
/// A Ed25519 key pair
32
+ #[ cfg( feature = "crypto" ) ]
26
33
Ed ( Ed25519KeyPair ) ,
27
34
/// A RSA key pair
35
+ #[ cfg( feature = "crypto" ) ]
28
36
Rsa ( RsaKeyPair , & ' static dyn RsaEncoding ) ,
29
37
/// A remote key pair
30
38
Remote ( Box < dyn RemoteKeyPair + Send + Sync > ) ,
@@ -33,8 +41,11 @@ pub(crate) enum KeyPairKind {
33
41
impl fmt:: Debug for KeyPairKind {
34
42
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
35
43
match self {
44
+ #[ cfg( feature = "crypto" ) ]
36
45
Self :: Ec ( key_pair) => write ! ( f, "{:?}" , key_pair) ,
46
+ #[ cfg( feature = "crypto" ) ]
37
47
Self :: Ed ( key_pair) => write ! ( f, "{:?}" , key_pair) ,
48
+ #[ cfg( feature = "crypto" ) ]
38
49
Self :: Rsa ( key_pair, _) => write ! ( f, "{:?}" , key_pair) ,
39
50
Self :: Remote ( _) => write ! ( f, "Box<dyn RemotePrivateKey>" ) ,
40
51
}
@@ -59,6 +70,7 @@ impl KeyPair {
59
70
/// Parses the key pair from the DER format
60
71
///
61
72
/// Equivalent to using the [`TryFrom`] implementation.
73
+ #[ cfg( feature = "crypto" ) ]
62
74
pub fn from_der ( der : & [ u8 ] ) -> Result < Self , Error > {
63
75
Ok ( der. try_into ( ) ?)
64
76
}
@@ -69,7 +81,7 @@ impl KeyPair {
69
81
}
70
82
71
83
/// Parses the key pair from the ASCII PEM format
72
- #[ cfg( feature = "pem" ) ]
84
+ #[ cfg( all ( feature = "pem" , feature = "crypto" ) ) ]
73
85
pub fn from_pem ( pem_str : & str ) -> Result < Self , Error > {
74
86
let private_key = pem:: parse ( pem_str) . _err ( ) ?;
75
87
let private_key_der: & [ _ ] = private_key. contents ( ) ;
@@ -89,7 +101,7 @@ impl KeyPair {
89
101
/// using the specified [`SignatureAlgorithm`]
90
102
///
91
103
/// Same as [from_pem_and_sign_algo](Self::from_pem_and_sign_algo).
92
- #[ cfg( feature = "pem" ) ]
104
+ #[ cfg( all ( feature = "pem" , feature = "crypto" ) ) ]
93
105
pub fn from_pem_and_sign_algo (
94
106
pem_str : & str ,
95
107
alg : & ' static SignatureAlgorithm ,
@@ -108,6 +120,7 @@ impl KeyPair {
108
120
/// key pair. However, sometimes multiple signature algorithms fit for the
109
121
/// same der key. In that instance, you can use this function to precisely
110
122
/// specify the `SignatureAlgorithm`.
123
+ #[ cfg( feature = "crypto" ) ]
111
124
pub fn from_der_and_sign_algo (
112
125
pkcs8 : & [ u8 ] ,
113
126
alg : & ' static SignatureAlgorithm ,
@@ -152,6 +165,7 @@ impl KeyPair {
152
165
} )
153
166
}
154
167
168
+ #[ cfg( feature = "crypto" ) ]
155
169
pub ( crate ) fn from_raw (
156
170
pkcs8 : & [ u8 ] ,
157
171
) -> Result < ( KeyPairKind , & ' static SignatureAlgorithm ) , Error > {
@@ -178,6 +192,7 @@ impl KeyPair {
178
192
}
179
193
180
194
/// Generate a new random key pair for the specified signature algorithm
195
+ #[ cfg( feature = "crypto" ) ]
181
196
pub fn generate ( alg : & ' static SignatureAlgorithm ) -> Result < Self , Error > {
182
197
let rng = & SystemRandom :: new ( ) ;
183
198
@@ -227,10 +242,22 @@ impl KeyPair {
227
242
return Err ( Error :: CertificateKeyPairMismatch )
228
243
} ,
229
244
Some ( kp) => Ok ( kp) ,
230
- None => KeyPair :: generate ( sig_alg) ,
245
+ None => {
246
+ if cfg ! ( not( feature = "crypto" ) ) {
247
+ return Err ( Error :: KeyGenerationUnavailable ) ;
248
+ } else {
249
+ KeyPair :: generate ( sig_alg)
250
+ }
251
+ } ,
231
252
}
232
253
}
233
254
255
+ /// Generate a new random key pair for the specified signature algorithm
256
+ #[ cfg( not( feature = "crypto" ) ) ]
257
+ pub fn generate ( _alg : & ' static SignatureAlgorithm ) -> Result < Self , Error > {
258
+ Err ( Error :: KeyGenerationUnavailable )
259
+ }
260
+
234
261
/// Get the raw public key of this key pair
235
262
///
236
263
/// The key is in raw format, as how [`ring::signature::KeyPair::public_key`]
@@ -253,17 +280,20 @@ impl KeyPair {
253
280
254
281
pub ( crate ) fn sign ( & self , msg : & [ u8 ] , writer : DERWriter ) -> Result < ( ) , Error > {
255
282
match & self . kind {
283
+ #[ cfg( feature = "crypto" ) ]
256
284
KeyPairKind :: Ec ( kp) => {
257
285
let system_random = SystemRandom :: new ( ) ;
258
286
let signature = kp. sign ( & system_random, msg) . _err ( ) ?;
259
287
let sig = & signature. as_ref ( ) ;
260
288
writer. write_bitvec_bytes ( & sig, & sig. len ( ) * 8 ) ;
261
289
} ,
290
+ #[ cfg( feature = "crypto" ) ]
262
291
KeyPairKind :: Ed ( kp) => {
263
292
let signature = kp. sign ( msg) ;
264
293
let sig = & signature. as_ref ( ) ;
265
294
writer. write_bitvec_bytes ( & sig, & sig. len ( ) * 8 ) ;
266
295
} ,
296
+ #[ cfg( feature = "crypto" ) ]
267
297
KeyPairKind :: Rsa ( kp, padding_alg) => {
268
298
let system_random = SystemRandom :: new ( ) ;
269
299
let mut signature = vec ! [ 0 ; rsa_key_pair_public_modulus_len( kp) ] ;
@@ -303,6 +333,7 @@ impl KeyPair {
303
333
///
304
334
/// Panics if called on a remote key pair.
305
335
pub fn serialize_der ( & self ) -> Vec < u8 > {
336
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
306
337
if let KeyPairKind :: Remote ( _) = self . kind {
307
338
panic ! ( "Serializing a remote key pair is not supported" )
308
339
}
@@ -315,6 +346,7 @@ impl KeyPair {
315
346
///
316
347
/// Panics if called on a remote key pair.
317
348
pub fn serialized_der ( & self ) -> & [ u8 ] {
349
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
318
350
if let KeyPairKind :: Remote ( _) = self . kind {
319
351
panic ! ( "Serializing a remote key pair is not supported" )
320
352
}
@@ -324,6 +356,7 @@ impl KeyPair {
324
356
325
357
/// Access the remote key pair if it is a remote one
326
358
pub fn as_remote ( & self ) -> Option < & ( dyn RemoteKeyPair + Send + Sync ) > {
359
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
327
360
if let KeyPairKind :: Remote ( remote) = & self . kind {
328
361
Some ( remote. as_ref ( ) )
329
362
} else {
@@ -340,6 +373,7 @@ impl KeyPair {
340
373
}
341
374
}
342
375
376
+ #[ cfg( feature = "crypto" ) ]
343
377
impl TryFrom < & [ u8 ] > for KeyPair {
344
378
type Error = Error ;
345
379
@@ -353,6 +387,7 @@ impl TryFrom<&[u8]> for KeyPair {
353
387
}
354
388
}
355
389
390
+ #[ cfg( feature = "crypto" ) ]
356
391
impl TryFrom < Vec < u8 > > for KeyPair {
357
392
type Error = Error ;
358
393
@@ -372,8 +407,11 @@ impl PublicKeyData for KeyPair {
372
407
}
373
408
fn raw_bytes ( & self ) -> & [ u8 ] {
374
409
match & self . kind {
410
+ #[ cfg( feature = "crypto" ) ]
375
411
KeyPairKind :: Ec ( kp) => kp. public_key ( ) . as_ref ( ) ,
412
+ #[ cfg( feature = "crypto" ) ]
376
413
KeyPairKind :: Ed ( kp) => kp. public_key ( ) . as_ref ( ) ,
414
+ #[ cfg( feature = "crypto" ) ]
377
415
KeyPairKind :: Rsa ( kp, _) => kp. public_key ( ) . as_ref ( ) ,
378
416
KeyPairKind :: Remote ( kp) => kp. public_key ( ) ,
379
417
}
@@ -395,12 +433,14 @@ pub trait RemoteKeyPair {
395
433
fn algorithm ( & self ) -> & ' static SignatureAlgorithm ;
396
434
}
397
435
436
+ #[ cfg( feature = "crypto" ) ]
398
437
impl < T > ExternalError < T > for Result < T , ring_error:: KeyRejected > {
399
438
fn _err ( self ) -> Result < T , Error > {
400
439
self . map_err ( |e| Error :: RingKeyRejected ( e. to_string ( ) ) )
401
440
}
402
441
}
403
442
443
+ #[ cfg( feature = "crypto" ) ]
404
444
impl < T > ExternalError < T > for Result < T , ring_error:: Unspecified > {
405
445
fn _err ( self ) -> Result < T , Error > {
406
446
self . map_err ( |_| Error :: RingUnspecified )
0 commit comments