1
1
//! X.509 `SubjectPublicKeyInfo`
2
2
3
- use crate :: { AlgorithmIdentifierRef , Error , Result } ;
3
+ use crate :: { AlgorithmIdentifier , Error , Result } ;
4
4
use core:: cmp:: Ordering ;
5
5
use der:: {
6
- asn1:: BitStringRef , Decode , DecodeValue , DerOrd , Encode , Header , Reader , Sequence , ValueOrd ,
6
+ asn1:: { AnyRef , BitStringRef } ,
7
+ Choice , Decode , DecodeValue , DerOrd , Encode , Header , Reader , Sequence , ValueOrd ,
7
8
} ;
8
9
9
10
#[ cfg( feature = "alloc" ) ]
21
22
#[ cfg( feature = "pem" ) ]
22
23
use der:: pem:: PemLabel ;
23
24
24
- # [ cfg ( doc ) ]
25
- use crate :: AlgorithmIdentifier ;
25
+ /// [`SubjectPublicKeyInfo`] with [`AnyRef`] algorithm parameters.
26
+ pub type SubjectPublicKeyInfoRef < ' a > = SubjectPublicKeyInfo < ' a , AnyRef < ' a > > ;
26
27
27
28
/// X.509 `SubjectPublicKeyInfo` (SPKI) as defined in [RFC 5280 § 4.1.2.7].
28
29
///
@@ -37,15 +38,25 @@ use crate::AlgorithmIdentifier;
37
38
///
38
39
/// [RFC 5280 § 4.1.2.7]: https://tools.ietf.org/html/rfc5280#section-4.1.2.7
39
40
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
40
- pub struct SubjectPublicKeyInfo < ' a > {
41
+ pub struct SubjectPublicKeyInfo < ' a , Params > {
41
42
/// X.509 [`AlgorithmIdentifier`] for the public key type
42
- pub algorithm : AlgorithmIdentifierRef < ' a > ,
43
+ pub algorithm : AlgorithmIdentifier < Params > ,
43
44
44
45
/// Public key data
45
46
pub subject_public_key : & ' a [ u8 ] ,
46
47
}
47
48
48
- impl < ' a > SubjectPublicKeyInfo < ' a > {
49
+ impl < ' a , Params > SubjectPublicKeyInfo < ' a , Params > {
50
+ /// Get a [`BitString`] representing the `subject_public_key`
51
+ fn bitstring ( & self ) -> der:: Result < BitStringRef < ' a > > {
52
+ BitStringRef :: from_bytes ( self . subject_public_key )
53
+ }
54
+ }
55
+
56
+ impl < ' a , Params > SubjectPublicKeyInfo < ' a , Params >
57
+ where
58
+ Params : Choice < ' a > + Encode ,
59
+ {
49
60
/// Calculate the SHA-256 fingerprint of this [`SubjectPublicKeyInfo`] and
50
61
/// encode it as a Base64 string.
51
62
///
@@ -71,14 +82,12 @@ impl<'a> SubjectPublicKeyInfo<'a> {
71
82
self . encode ( & mut builder) ?;
72
83
Ok ( builder. finish ( ) )
73
84
}
74
-
75
- /// Get a [`BitString`] representing the `subject_public_key`
76
- fn bitstring ( & self ) -> der:: Result < BitStringRef < ' a > > {
77
- BitStringRef :: from_bytes ( self . subject_public_key )
78
- }
79
85
}
80
86
81
- impl < ' a > DecodeValue < ' a > for SubjectPublicKeyInfo < ' a > {
87
+ impl < ' a , Params > DecodeValue < ' a > for SubjectPublicKeyInfo < ' a , Params >
88
+ where
89
+ Params : Choice < ' a > + Encode ,
90
+ {
82
91
fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> der:: Result < Self > {
83
92
reader. read_nested ( header. length , |reader| {
84
93
Ok ( Self {
@@ -91,7 +100,10 @@ impl<'a> DecodeValue<'a> for SubjectPublicKeyInfo<'a> {
91
100
}
92
101
}
93
102
94
- impl < ' a > Sequence < ' a > for SubjectPublicKeyInfo < ' a > {
103
+ impl < ' a , Params > Sequence < ' a > for SubjectPublicKeyInfo < ' a , Params >
104
+ where
105
+ Params : Choice < ' a > + Encode ,
106
+ {
95
107
fn fields < F , T > ( & self , f : F ) -> der:: Result < T >
96
108
where
97
109
F : FnOnce ( & [ & dyn Encode ] ) -> der:: Result < T > ,
@@ -100,15 +112,21 @@ impl<'a> Sequence<'a> for SubjectPublicKeyInfo<'a> {
100
112
}
101
113
}
102
114
103
- impl < ' a > TryFrom < & ' a [ u8 ] > for SubjectPublicKeyInfo < ' a > {
115
+ impl < ' a , Params > TryFrom < & ' a [ u8 ] > for SubjectPublicKeyInfo < ' a , Params >
116
+ where
117
+ Params : Choice < ' a > + Encode ,
118
+ {
104
119
type Error = Error ;
105
120
106
121
fn try_from ( bytes : & ' a [ u8 ] ) -> Result < Self > {
107
122
Ok ( Self :: from_der ( bytes) ?)
108
123
}
109
124
}
110
125
111
- impl ValueOrd for SubjectPublicKeyInfo < ' _ > {
126
+ impl < ' a , Params > ValueOrd for SubjectPublicKeyInfo < ' a , Params >
127
+ where
128
+ Params : Choice < ' a > + DerOrd + Encode ,
129
+ {
112
130
fn value_cmp ( & self , other : & Self ) -> der:: Result < Ordering > {
113
131
match self . algorithm . der_cmp ( & other. algorithm ) ? {
114
132
Ordering :: Equal => self . bitstring ( ) ?. der_cmp ( & other. bitstring ( ) ?) ,
@@ -119,26 +137,32 @@ impl ValueOrd for SubjectPublicKeyInfo<'_> {
119
137
120
138
#[ cfg( feature = "alloc" ) ]
121
139
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
122
- impl TryFrom < SubjectPublicKeyInfo < ' _ > > for Document {
140
+ impl < ' a , Params > TryFrom < SubjectPublicKeyInfo < ' a , Params > > for Document
141
+ where
142
+ Params : Choice < ' a > + Encode ,
143
+ {
123
144
type Error = Error ;
124
145
125
- fn try_from ( spki : SubjectPublicKeyInfo < ' _ > ) -> Result < Document > {
146
+ fn try_from ( spki : SubjectPublicKeyInfo < ' a , Params > ) -> Result < Document > {
126
147
Self :: try_from ( & spki)
127
148
}
128
149
}
129
150
130
151
#[ cfg( feature = "alloc" ) ]
131
152
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
132
- impl TryFrom < & SubjectPublicKeyInfo < ' _ > > for Document {
153
+ impl < ' a , Params > TryFrom < & SubjectPublicKeyInfo < ' a , Params > > for Document
154
+ where
155
+ Params : Choice < ' a > + Encode ,
156
+ {
133
157
type Error = Error ;
134
158
135
- fn try_from ( spki : & SubjectPublicKeyInfo < ' _ > ) -> Result < Document > {
159
+ fn try_from ( spki : & SubjectPublicKeyInfo < ' a , Params > ) -> Result < Document > {
136
160
Ok ( Self :: encode_msg ( spki) ?)
137
161
}
138
162
}
139
163
140
164
#[ cfg( feature = "pem" ) ]
141
165
#[ cfg_attr( docsrs, doc( cfg( feature = "pem" ) ) ) ]
142
- impl PemLabel for SubjectPublicKeyInfo < ' _ > {
166
+ impl < Params > PemLabel for SubjectPublicKeyInfo < ' _ , Params > {
143
167
const PEM_LABEL : & ' static str = "PUBLIC KEY" ;
144
168
}
0 commit comments