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