@@ -43,7 +43,7 @@ impl<T> EVP_PKEY_CTX_consumer for T where T: Fn(*mut EVP_PKEY_CTX) -> Result<(),
43
43
#[ allow( non_upper_case_globals, clippy:: type_complexity) ]
44
44
pub ( crate ) const No_EVP_PKEY_CTX_consumer : Option < fn ( * mut EVP_PKEY_CTX ) -> Result < ( ) , ( ) > > = None ;
45
45
46
- impl LcPtr < EVP_PKEY > {
46
+ impl ConstPointer < ' _ , EVP_PKEY > {
47
47
pub ( crate ) fn validate_as_ed25519 ( & self ) -> Result < ( ) , KeyRejected > {
48
48
const ED25519_KEY_TYPE : c_int = EVP_PKEY_ED25519 ;
49
49
const ED25519_MIN_BITS : c_int = 253 ;
@@ -79,35 +79,29 @@ impl LcPtr<EVP_PKEY> {
79
79
// EVP_PKEY_X448 = 961;
80
80
// EVP_PKEY_ED448 = 960;
81
81
pub ( crate ) fn id ( & self ) -> i32 {
82
- unsafe { EVP_PKEY_id ( * self . as_const ( ) ) }
82
+ unsafe { EVP_PKEY_id ( * * self ) }
83
83
}
84
84
85
85
pub ( crate ) fn key_size_bytes ( & self ) -> usize {
86
86
self . key_size_bits ( ) / 8
87
87
}
88
88
89
89
pub ( crate ) fn key_size_bits ( & self ) -> usize {
90
- unsafe { EVP_PKEY_bits ( * self . as_const ( ) ) }
91
- . try_into ( )
92
- . unwrap ( )
90
+ unsafe { EVP_PKEY_bits ( * * self ) } . try_into ( ) . unwrap ( )
93
91
}
94
92
95
93
pub ( crate ) fn signature_size_bytes ( & self ) -> usize {
96
- unsafe { EVP_PKEY_size ( * self . as_const ( ) ) }
97
- . try_into ( )
98
- . unwrap ( )
94
+ unsafe { EVP_PKEY_size ( * * self ) } . try_into ( ) . unwrap ( )
99
95
}
100
96
101
97
#[ allow( dead_code) ]
102
98
pub ( crate ) fn get_ec_key ( & self ) -> Result < ConstPointer < EC_KEY > , KeyRejected > {
103
- self . project_const_lifetime ( unsafe {
104
- |evp_pkey| EVP_PKEY_get0_EC_KEY ( * evp_pkey. as_const ( ) )
105
- } )
106
- . map_err ( |( ) | KeyRejected :: wrong_algorithm ( ) )
99
+ self . project_const_lifetime ( unsafe { |evp_pkey| EVP_PKEY_get0_EC_KEY ( * * evp_pkey) } )
100
+ . map_err ( |( ) | KeyRejected :: wrong_algorithm ( ) )
107
101
}
108
102
109
103
pub ( crate ) fn get_rsa ( & self ) -> Result < ConstPointer < RSA > , KeyRejected > {
110
- self . project_const_lifetime ( unsafe { |evp_pkey| EVP_PKEY_get0_RSA ( * evp_pkey. as_const ( ) ) } )
104
+ self . project_const_lifetime ( unsafe { |evp_pkey| EVP_PKEY_get0_RSA ( * * evp_pkey) } )
111
105
. map_err ( |( ) | KeyRejected :: wrong_algorithm ( ) )
112
106
}
113
107
@@ -116,76 +110,37 @@ impl LcPtr<EVP_PKEY> {
116
110
// size in bytes for keys ranging from 2048-bit to 4096-bit. So size the initial capacity to be roughly
117
111
// 500% as a conservative estimate to avoid needing to reallocate for any key in that range.
118
112
let mut cbb = LcCBB :: new ( self . key_size_bytes ( ) * 5 ) ;
119
- if 1 != unsafe { EVP_marshal_public_key ( cbb. as_mut_ptr ( ) , * self . as_const ( ) ) } {
113
+ if 1 != unsafe { EVP_marshal_public_key ( cbb. as_mut_ptr ( ) , * * self ) } {
120
114
return Err ( Unspecified ) ;
121
115
}
122
116
cbb. into_vec ( )
123
117
}
124
118
125
- pub ( crate ) fn parse_rfc5280_public_key (
126
- bytes : & [ u8 ] ,
127
- evp_pkey_type : c_int ,
128
- ) -> Result < Self , KeyRejected > {
129
- let mut cbs = cbs:: build_CBS ( bytes) ;
130
- // Also checks the validity of the key
131
- let evp_pkey = LcPtr :: new ( unsafe { EVP_parse_public_key ( & mut cbs) } )
132
- . map_err ( |( ) | KeyRejected :: invalid_encoding ( ) ) ?;
133
- evp_pkey
134
- . id ( )
135
- . eq ( & evp_pkey_type)
136
- . then_some ( evp_pkey)
137
- . ok_or ( KeyRejected :: wrong_algorithm ( ) )
138
- }
139
-
140
119
pub ( crate ) fn marshal_rfc5208_private_key (
141
120
& self ,
142
121
version : Version ,
143
122
) -> Result < Vec < u8 > , Unspecified > {
144
- let key_size_bytes = TryInto :: < usize > :: try_into ( unsafe { EVP_PKEY_bits ( * self . as_const ( ) ) } )
145
- . expect ( "fit in usize" )
146
- / 8 ;
123
+ let key_size_bytes =
124
+ TryInto :: < usize > :: try_into ( unsafe { EVP_PKEY_bits ( * * self ) } ) . expect ( "fit in usize" ) / 8 ;
147
125
let mut cbb = LcCBB :: new ( key_size_bytes * 5 ) ;
148
126
match version {
149
127
Version :: V1 => {
150
- if 1 != unsafe { EVP_marshal_private_key ( cbb. as_mut_ptr ( ) , * self . as_const ( ) ) } {
128
+ if 1 != unsafe { EVP_marshal_private_key ( cbb. as_mut_ptr ( ) , * * self ) } {
151
129
return Err ( Unspecified ) ;
152
130
}
153
131
}
154
132
Version :: V2 => {
155
- if 1 != unsafe { EVP_marshal_private_key_v2 ( cbb. as_mut_ptr ( ) , * self . as_const ( ) ) } {
133
+ if 1 != unsafe { EVP_marshal_private_key_v2 ( cbb. as_mut_ptr ( ) , * * self ) } {
156
134
return Err ( Unspecified ) ;
157
135
}
158
136
}
159
137
}
160
138
cbb. into_vec ( )
161
139
}
162
140
163
- pub ( crate ) fn parse_rfc5208_private_key (
164
- bytes : & [ u8 ] ,
165
- evp_pkey_type : c_int ,
166
- ) -> Result < Self , KeyRejected > {
167
- let mut cbs = cbs:: build_CBS ( bytes) ;
168
- // Also checks the validity of the key
169
- let evp_pkey = LcPtr :: new ( unsafe { EVP_parse_private_key ( & mut cbs) } )
170
- . map_err ( |( ) | KeyRejected :: invalid_encoding ( ) ) ?;
171
- evp_pkey
172
- . id ( )
173
- . eq ( & evp_pkey_type)
174
- . then_some ( evp_pkey)
175
- . ok_or ( KeyRejected :: wrong_algorithm ( ) )
176
- }
177
-
178
- #[ allow( non_snake_case) ]
179
- pub ( crate ) fn create_EVP_PKEY_CTX ( & self ) -> Result < LcPtr < EVP_PKEY_CTX > , ( ) > {
180
- // The only modification made by EVP_PKEY_CTX_new to `priv_key` is to increment its
181
- // refcount. The modification is made while holding a global lock:
182
- // https://github.com/aws/aws-lc/blob/61503f7fe72457e12d3446853a5452d175560c49/crypto/refcount_lock.c#L29
183
- LcPtr :: new ( unsafe { EVP_PKEY_CTX_new ( * self . as_mut_unsafe ( ) , null_mut ( ) ) } )
184
- }
185
-
186
141
pub ( crate ) fn marshal_raw_private_key ( & self ) -> Result < Vec < u8 > , Unspecified > {
187
142
let mut size = 0 ;
188
- if 1 != unsafe { EVP_PKEY_get_raw_private_key ( * self . as_const ( ) , null_mut ( ) , & mut size) } {
143
+ if 1 != unsafe { EVP_PKEY_get_raw_private_key ( * * self , null_mut ( ) , & mut size) } {
189
144
return Err ( Unspecified ) ;
190
145
}
191
146
let mut buffer = vec ! [ 0u8 ; size] ;
@@ -199,9 +154,7 @@ impl LcPtr<EVP_PKEY> {
199
154
buffer : & mut [ u8 ] ,
200
155
) -> Result < usize , Unspecified > {
201
156
let mut key_len = buffer. len ( ) ;
202
- if 1 == unsafe {
203
- EVP_PKEY_get_raw_private_key ( * self . as_const ( ) , buffer. as_mut_ptr ( ) , & mut key_len)
204
- } {
157
+ if 1 == unsafe { EVP_PKEY_get_raw_private_key ( * * self , buffer. as_mut_ptr ( ) , & mut key_len) } {
205
158
Ok ( key_len)
206
159
} else {
207
160
Err ( Unspecified )
@@ -211,7 +164,7 @@ impl LcPtr<EVP_PKEY> {
211
164
#[ allow( dead_code) ]
212
165
pub ( crate ) fn marshal_raw_public_key ( & self ) -> Result < Vec < u8 > , Unspecified > {
213
166
let mut size = 0 ;
214
- if 1 != unsafe { EVP_PKEY_get_raw_public_key ( * self . as_const ( ) , null_mut ( ) , & mut size) } {
167
+ if 1 != unsafe { EVP_PKEY_get_raw_public_key ( * * self , null_mut ( ) , & mut size) } {
215
168
return Err ( Unspecified ) ;
216
169
}
217
170
let mut buffer = vec ! [ 0u8 ; size] ;
@@ -229,14 +182,57 @@ impl LcPtr<EVP_PKEY> {
229
182
// `EVP_PKEY_get_raw_public_key` writes the total length
230
183
// to `encapsulate_key_size` in the event that the buffer we provide is larger then
231
184
// required.
232
- EVP_PKEY_get_raw_public_key ( * self . as_const ( ) , buffer. as_mut_ptr ( ) , & mut key_len)
185
+ EVP_PKEY_get_raw_public_key ( * * self , buffer. as_mut_ptr ( ) , & mut key_len)
233
186
} {
234
187
Ok ( key_len)
235
188
} else {
236
189
Err ( Unspecified )
237
190
}
238
191
}
239
192
193
+ }
194
+
195
+ impl LcPtr < EVP_PKEY > {
196
+ pub ( crate ) fn parse_rfc5280_public_key (
197
+ bytes : & [ u8 ] ,
198
+ evp_pkey_type : c_int ,
199
+ ) -> Result < Self , KeyRejected > {
200
+ let mut cbs = cbs:: build_CBS ( bytes) ;
201
+ // Also checks the validity of the key
202
+ let evp_pkey = LcPtr :: new ( unsafe { EVP_parse_public_key ( & mut cbs) } )
203
+ . map_err ( |( ) | KeyRejected :: invalid_encoding ( ) ) ?;
204
+ evp_pkey
205
+ . as_const ( )
206
+ . id ( )
207
+ . eq ( & evp_pkey_type)
208
+ . then_some ( evp_pkey)
209
+ . ok_or ( KeyRejected :: wrong_algorithm ( ) )
210
+ }
211
+
212
+ pub ( crate ) fn parse_rfc5208_private_key (
213
+ bytes : & [ u8 ] ,
214
+ evp_pkey_type : c_int ,
215
+ ) -> Result < Self , KeyRejected > {
216
+ let mut cbs = cbs:: build_CBS ( bytes) ;
217
+ // Also checks the validity of the key
218
+ let evp_pkey = LcPtr :: new ( unsafe { EVP_parse_private_key ( & mut cbs) } )
219
+ . map_err ( |( ) | KeyRejected :: invalid_encoding ( ) ) ?;
220
+ evp_pkey
221
+ . as_const ( )
222
+ . id ( )
223
+ . eq ( & evp_pkey_type)
224
+ . then_some ( evp_pkey)
225
+ . ok_or ( KeyRejected :: wrong_algorithm ( ) )
226
+ }
227
+
228
+ #[ allow( non_snake_case) ]
229
+ pub ( crate ) fn create_EVP_PKEY_CTX ( & self ) -> Result < LcPtr < EVP_PKEY_CTX > , ( ) > {
230
+ // The only modification made by EVP_PKEY_CTX_new to `priv_key` is to increment its
231
+ // refcount. The modification is made while holding a global lock:
232
+ // https://github.com/aws/aws-lc/blob/61503f7fe72457e12d3446853a5452d175560c49/crypto/refcount_lock.c#L29
233
+ LcPtr :: new ( unsafe { EVP_PKEY_CTX_new ( * self . as_mut_unsafe ( ) , null_mut ( ) ) } )
234
+ }
235
+
240
236
pub ( crate ) fn parse_raw_private_key (
241
237
bytes : & [ u8 ] ,
242
238
evp_pkey_type : c_int ,
0 commit comments