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,40 @@ 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 > ,
90
93
{
91
94
fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> der:: Result < Self > {
92
95
reader. read_nested ( header. length , |reader| {
93
96
Ok ( Self {
94
97
algorithm : reader. decode ( ) ?,
95
- subject_public_key : BitString :: decode ( reader) ?,
98
+ subject_public_key : Key :: decode ( reader) ?,
96
99
} )
97
100
} )
98
101
}
99
102
}
100
103
101
- impl < ' a , Params > Sequence < ' a > for SubjectPublicKeyInfo < Params >
104
+ impl < ' a : ' k , ' k , Params , Key : ' k > Sequence < ' a > for SubjectPublicKeyInfo < Params , Key >
102
105
where
103
106
Params : Choice < ' a > + Encode ,
107
+ Key : BitStringLike < ' k , ' a > ,
108
+ BitStringRef < ' a > : From < & ' k Key > ,
104
109
{
105
110
fn fields < F , T > ( & self , f : F ) -> der:: Result < T >
106
111
where
107
112
F : FnOnce ( & [ & dyn Encode ] ) -> der:: Result < T > ,
108
113
{
109
- f ( & [ & self . algorithm , & self . bitstring ( ) ] )
114
+ f ( & [ & self . algorithm , & self . subject_public_key ] )
110
115
}
111
116
}
112
117
113
- impl < ' a , Params > TryFrom < & ' a [ u8 ] > for SubjectPublicKeyInfo < Params >
118
+ impl < ' a : ' k , ' k , Params , Key : ' k > TryFrom < & ' a [ u8 ] > for SubjectPublicKeyInfo < Params , Key >
114
119
where
115
120
Params : Choice < ' a > + Encode ,
121
+ Key : BitStringLike < ' k , ' a > ,
122
+ BitStringRef < ' a > : From < & ' k Key > ,
116
123
{
117
124
type Error = Error ;
118
125
@@ -121,46 +128,51 @@ where
121
128
}
122
129
}
123
130
124
- impl < ' a , Params > ValueOrd for SubjectPublicKeyInfo < Params >
131
+ impl < ' a , Params , Key > ValueOrd for SubjectPublicKeyInfo < Params , Key >
125
132
where
126
133
Params : Choice < ' a > + DerOrd + Encode ,
134
+ Key : ValueOrd ,
127
135
{
128
136
fn value_cmp ( & self , other : & Self ) -> der:: Result < Ordering > {
129
137
match self . algorithm . der_cmp ( & other. algorithm ) ? {
130
- Ordering :: Equal => self . bitstring ( ) . der_cmp ( & other. bitstring ( ) ) ,
138
+ Ordering :: Equal => self . subject_public_key . value_cmp ( & other. subject_public_key ) ,
131
139
other => Ok ( other) ,
132
140
}
133
141
}
134
142
}
135
143
136
144
#[ cfg( feature = "alloc" ) ]
137
145
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
138
- impl < ' a , Params > TryFrom < SubjectPublicKeyInfo < Params > > for Document
146
+ impl < ' a : ' k , ' k , Params , Key : ' k > TryFrom < SubjectPublicKeyInfo < Params , Key > > for Document
139
147
where
140
148
Params : Choice < ' a > + Encode ,
149
+ Key : BitStringLike < ' k , ' a > ,
150
+ BitStringRef < ' a > : From < & ' k Key > ,
141
151
{
142
152
type Error = Error ;
143
153
144
- fn try_from ( spki : SubjectPublicKeyInfo < Params > ) -> Result < Document > {
154
+ fn try_from ( spki : SubjectPublicKeyInfo < Params , Key > ) -> Result < Document > {
145
155
Self :: try_from ( & spki)
146
156
}
147
157
}
148
158
149
159
#[ cfg( feature = "alloc" ) ]
150
160
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
151
- impl < ' a , Params > TryFrom < & SubjectPublicKeyInfo < Params > > for Document
161
+ impl < ' a : ' k , ' k , Params , Key : ' k > TryFrom < & SubjectPublicKeyInfo < Params , Key > > for Document
152
162
where
153
163
Params : Choice < ' a > + Encode ,
164
+ Key : BitStringLike < ' k , ' a > ,
165
+ BitStringRef < ' a > : From < & ' k Key > ,
154
166
{
155
167
type Error = Error ;
156
168
157
- fn try_from ( spki : & SubjectPublicKeyInfo < Params > ) -> Result < Document > {
169
+ fn try_from ( spki : & SubjectPublicKeyInfo < Params , Key > ) -> Result < Document > {
158
170
Ok ( Self :: encode_msg ( spki) ?)
159
171
}
160
172
}
161
173
162
174
#[ cfg( feature = "pem" ) ]
163
175
#[ cfg_attr( docsrs, doc( cfg( feature = "pem" ) ) ) ]
164
- impl < Params > PemLabel for SubjectPublicKeyInfo < Params > {
176
+ impl < Params , Key > PemLabel for SubjectPublicKeyInfo < Params , Key > {
165
177
const PEM_LABEL : & ' static str = "PUBLIC KEY" ;
166
178
}
0 commit comments