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
}
@@ -57,13 +68,15 @@ pub struct KeyPair {
57
68
58
69
impl KeyPair {
59
70
/// Generate a new random [`PKCS_ECDSA_P256_SHA256`] key pair
71
+ #[ cfg( feature = "crypto" ) ]
60
72
pub fn generate ( ) -> Result < Self , Error > {
61
73
Self :: generate_for ( & PKCS_ECDSA_P256_SHA256 )
62
74
}
63
75
64
76
/// Generate a new random key pair for the specified signature algorithm
65
77
///
66
78
/// If you're not sure which algorithm to use, [`PKCS_ECDSA_P256_SHA256`] is a good choice.
79
+ #[ cfg( feature = "crypto" ) ]
67
80
pub fn generate_for ( alg : & ' static SignatureAlgorithm ) -> Result < Self , Error > {
68
81
let rng = & SystemRandom :: new ( ) ;
69
82
@@ -102,6 +115,7 @@ impl KeyPair {
102
115
/// Parses the key pair from the DER format
103
116
///
104
117
/// Equivalent to using the [`TryFrom`] implementation.
118
+ #[ cfg( feature = "crypto" ) ]
105
119
pub fn from_der ( der : & [ u8 ] ) -> Result < Self , Error > {
106
120
Ok ( der. try_into ( ) ?)
107
121
}
@@ -112,7 +126,7 @@ impl KeyPair {
112
126
}
113
127
114
128
/// Parses the key pair from the ASCII PEM format
115
- #[ cfg( feature = "pem" ) ]
129
+ #[ cfg( all ( feature = "pem" , feature = "crypto" ) ) ]
116
130
pub fn from_pem ( pem_str : & str ) -> Result < Self , Error > {
117
131
let private_key = pem:: parse ( pem_str) . _err ( ) ?;
118
132
let private_key_der: & [ _ ] = private_key. contents ( ) ;
@@ -132,7 +146,7 @@ impl KeyPair {
132
146
/// using the specified [`SignatureAlgorithm`]
133
147
///
134
148
/// Same as [from_pem_and_sign_algo](Self::from_pem_and_sign_algo).
135
- #[ cfg( feature = "pem" ) ]
149
+ #[ cfg( all ( feature = "pem" , feature = "crypto" ) ) ]
136
150
pub fn from_pem_and_sign_algo (
137
151
pem_str : & str ,
138
152
alg : & ' static SignatureAlgorithm ,
@@ -151,6 +165,7 @@ impl KeyPair {
151
165
/// key pair. However, sometimes multiple signature algorithms fit for the
152
166
/// same der key. In that instance, you can use this function to precisely
153
167
/// specify the `SignatureAlgorithm`.
168
+ #[ cfg( feature = "crypto" ) ]
154
169
pub fn from_der_and_sign_algo (
155
170
pkcs8 : & [ u8 ] ,
156
171
alg : & ' static SignatureAlgorithm ,
@@ -195,6 +210,7 @@ impl KeyPair {
195
210
} )
196
211
}
197
212
213
+ #[ cfg( feature = "crypto" ) ]
198
214
pub ( crate ) fn from_raw (
199
215
pkcs8 : & [ u8 ] ,
200
216
) -> Result < ( KeyPairKind , & ' static SignatureAlgorithm ) , Error > {
@@ -242,17 +258,20 @@ impl KeyPair {
242
258
243
259
pub ( crate ) fn sign ( & self , msg : & [ u8 ] , writer : DERWriter ) -> Result < ( ) , Error > {
244
260
match & self . kind {
261
+ #[ cfg( feature = "crypto" ) ]
245
262
KeyPairKind :: Ec ( kp) => {
246
263
let system_random = SystemRandom :: new ( ) ;
247
264
let signature = kp. sign ( & system_random, msg) . _err ( ) ?;
248
265
let sig = & signature. as_ref ( ) ;
249
266
writer. write_bitvec_bytes ( & sig, & sig. len ( ) * 8 ) ;
250
267
} ,
268
+ #[ cfg( feature = "crypto" ) ]
251
269
KeyPairKind :: Ed ( kp) => {
252
270
let signature = kp. sign ( msg) ;
253
271
let sig = & signature. as_ref ( ) ;
254
272
writer. write_bitvec_bytes ( & sig, & sig. len ( ) * 8 ) ;
255
273
} ,
274
+ #[ cfg( feature = "crypto" ) ]
256
275
KeyPairKind :: Rsa ( kp, padding_alg) => {
257
276
let system_random = SystemRandom :: new ( ) ;
258
277
let mut signature = vec ! [ 0 ; rsa_key_pair_public_modulus_len( kp) ] ;
@@ -292,6 +311,7 @@ impl KeyPair {
292
311
///
293
312
/// Panics if called on a remote key pair.
294
313
pub fn serialize_der ( & self ) -> Vec < u8 > {
314
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
295
315
if let KeyPairKind :: Remote ( _) = self . kind {
296
316
panic ! ( "Serializing a remote key pair is not supported" )
297
317
}
@@ -304,6 +324,7 @@ impl KeyPair {
304
324
///
305
325
/// Panics if called on a remote key pair.
306
326
pub fn serialized_der ( & self ) -> & [ u8 ] {
327
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
307
328
if let KeyPairKind :: Remote ( _) = self . kind {
308
329
panic ! ( "Serializing a remote key pair is not supported" )
309
330
}
@@ -313,6 +334,7 @@ impl KeyPair {
313
334
314
335
/// Access the remote key pair if it is a remote one
315
336
pub fn as_remote ( & self ) -> Option < & ( dyn RemoteKeyPair + Send + Sync ) > {
337
+ #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
316
338
if let KeyPairKind :: Remote ( remote) = & self . kind {
317
339
Some ( remote. as_ref ( ) )
318
340
} else {
@@ -329,6 +351,7 @@ impl KeyPair {
329
351
}
330
352
}
331
353
354
+ #[ cfg( feature = "crypto" ) ]
332
355
impl TryFrom < & [ u8 ] > for KeyPair {
333
356
type Error = Error ;
334
357
@@ -342,6 +365,7 @@ impl TryFrom<&[u8]> for KeyPair {
342
365
}
343
366
}
344
367
368
+ #[ cfg( feature = "crypto" ) ]
345
369
impl TryFrom < Vec < u8 > > for KeyPair {
346
370
type Error = Error ;
347
371
@@ -361,8 +385,11 @@ impl PublicKeyData for KeyPair {
361
385
}
362
386
fn raw_bytes ( & self ) -> & [ u8 ] {
363
387
match & self . kind {
388
+ #[ cfg( feature = "crypto" ) ]
364
389
KeyPairKind :: Ec ( kp) => kp. public_key ( ) . as_ref ( ) ,
390
+ #[ cfg( feature = "crypto" ) ]
365
391
KeyPairKind :: Ed ( kp) => kp. public_key ( ) . as_ref ( ) ,
392
+ #[ cfg( feature = "crypto" ) ]
366
393
KeyPairKind :: Rsa ( kp, _) => kp. public_key ( ) . as_ref ( ) ,
367
394
KeyPairKind :: Remote ( kp) => kp. public_key ( ) ,
368
395
}
@@ -384,12 +411,14 @@ pub trait RemoteKeyPair {
384
411
fn algorithm ( & self ) -> & ' static SignatureAlgorithm ;
385
412
}
386
413
414
+ #[ cfg( feature = "crypto" ) ]
387
415
impl < T > ExternalError < T > for Result < T , ring_error:: KeyRejected > {
388
416
fn _err ( self ) -> Result < T , Error > {
389
417
self . map_err ( |e| Error :: RingKeyRejected ( e. to_string ( ) ) )
390
418
}
391
419
}
392
420
421
+ #[ cfg( feature = "crypto" ) ]
393
422
impl < T > ExternalError < T > for Result < T , ring_error:: Unspecified > {
394
423
fn _err ( self ) -> Result < T , Error > {
395
424
self . map_err ( |_| Error :: RingUnspecified )
@@ -419,11 +448,16 @@ pub(crate) trait PublicKeyData {
419
448
420
449
#[ cfg( test) ]
421
450
mod test {
451
+ #[ cfg( crypto) ]
422
452
use super :: * ;
423
453
424
- use crate :: ring_like:: rand:: SystemRandom ;
425
- use crate :: ring_like:: signature:: { EcdsaKeyPair , ECDSA_P256_SHA256_FIXED_SIGNING } ;
454
+ #[ cfg( crypto) ]
455
+ use crate :: ring_like:: {
456
+ rand:: SystemRandom ,
457
+ signature:: { EcdsaKeyPair , ECDSA_P256_SHA256_FIXED_SIGNING } ,
458
+ } ;
426
459
460
+ #[ cfg( crypto) ]
427
461
#[ test]
428
462
fn test_algorithm ( ) {
429
463
let rng = SystemRandom :: new ( ) ;
0 commit comments