Skip to content

Commit 0deaaab

Browse files
committed
Migrate functionality to ConstPointer<'_, EVP_PKEY>
1 parent bfe6b7f commit 0deaaab

14 files changed

+122
-112
lines changed

aws-lc-rs/src/agreement.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,9 @@ impl PrivateKey {
415415
}
416416
KeyInner::X25519(priv_key) => {
417417
let mut buffer = [0u8; MAX_PUBLIC_KEY_LEN];
418-
let out_len = priv_key.marshal_raw_public_to_buffer(&mut buffer)?;
418+
let out_len = priv_key
419+
.as_const()
420+
.marshal_raw_public_to_buffer(&mut buffer)?;
419421
Ok(PublicKey {
420422
inner_key: self.inner_key.clone(),
421423
key_bytes: buffer,
@@ -477,6 +479,7 @@ impl AsDer<Pkcs8V1Der<'static>> for PrivateKey {
477479
Ok(Pkcs8V1Der::new(
478480
self.inner_key
479481
.get_evp_pkey()
482+
.as_const()
480483
.marshal_rfc5208_private_key(Version::V1)?,
481484
))
482485
}
@@ -510,7 +513,9 @@ impl AsBigEndian<Curve25519SeedBin<'static>> for PrivateKey {
510513
return Err(Unspecified);
511514
}
512515
let evp_pkey = self.inner_key.get_evp_pkey();
513-
Ok(Curve25519SeedBin::new(evp_pkey.marshal_raw_private_key()?))
516+
Ok(Curve25519SeedBin::new(
517+
evp_pkey.as_const().marshal_raw_private_key()?,
518+
))
514519
}
515520
}
516521

@@ -577,7 +582,7 @@ impl AsDer<PublicKeyX509Der<'static>> for PublicKey {
577582
| KeyInner::ECDH_P384(evp_pkey)
578583
| KeyInner::ECDH_P521(evp_pkey)
579584
| KeyInner::X25519(evp_pkey) => {
580-
let der = evp_pkey.marshal_rfc5280_public_key()?;
585+
let der = evp_pkey.as_const().marshal_rfc5280_public_key()?;
581586
Ok(PublicKeyX509Der::from(Buffer::new(der)))
582587
}
583588
}

aws-lc-rs/src/ec/encoding.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ pub(crate) mod sec1 {
135135
compressed: bool,
136136
) -> Result<Vec<u8>, Unspecified> {
137137
let pub_key_size = if compressed {
138-
compressed_public_key_size_bytes(evp_pkey.key_size_bits())
138+
compressed_public_key_size_bytes(evp_pkey.as_const().key_size_bits())
139139
} else {
140-
uncompressed_public_key_size_bytes(evp_pkey.key_size_bits())
140+
uncompressed_public_key_size_bytes(evp_pkey.as_const().key_size_bits())
141141
};
142142
let mut cbb = LcCBB::new(pub_key_size);
143143
marshal_sec1_public_point_into_cbb(&mut cbb, evp_pkey, compressed)?;
@@ -245,7 +245,7 @@ pub(crate) mod rfc5915 {
245245
let ec_key = evp_pkey.project_const_lifetime(unsafe {
246246
|evp_pkey| EVP_PKEY_get0_EC_KEY(*evp_pkey.as_const())
247247
})?;
248-
let mut cbb = LcCBB::new(evp_pkey.key_size_bytes());
248+
let mut cbb = LcCBB::new(evp_pkey.as_const().key_size_bytes());
249249
let enc_flags = unsafe { EC_KEY_get_enc_flags(*ec_key) };
250250
if 1 != unsafe { EC_KEY_marshal_private_key(cbb.as_mut_ptr(), *ec_key, enc_flags) } {
251251
return Err(Unspecified);

aws-lc-rs/src/ec/key_pair.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ impl EcdsaKeyPair {
128128
///
129129
pub fn to_pkcs8v1(&self) -> Result<Document, Unspecified> {
130130
Ok(Document::new(
131-
self.evp_pkey.marshal_rfc5208_private_key(Version::V1)?,
131+
self.evp_pkey
132+
.as_const()
133+
.marshal_rfc5208_private_key(Version::V1)?,
132134
))
133135
}
134136

aws-lc-rs/src/ec/signature.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl AsDer<PublicKeyX509Der<'static>> for PublicKey {
121121
/// # Errors
122122
/// Returns an error if the public key fails to marshal to X.509.
123123
fn as_der(&self) -> Result<PublicKeyX509Der<'static>, Unspecified> {
124-
let der = self.evp_pkey.marshal_rfc5280_public_key()?;
124+
let der = self.evp_pkey.as_const().marshal_rfc5280_public_key()?;
125125
Ok(PublicKeyX509Der::new(der))
126126
}
127127
}

aws-lc-rs/src/ed25519.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl AsDer<PublicKeyX509Der<'static>> for PublicKey {
153153
// 2:d=1 hl=2 l= 5 cons: SEQUENCE
154154
// 4:d=2 hl=2 l= 3 prim: OBJECT :ED25519
155155
// 9:d=1 hl=2 l= 33 prim: BIT STRING
156-
let der = self.evp_pkey.marshal_rfc5280_public_key()?;
156+
let der = self.evp_pkey.as_const().marshal_rfc5280_public_key()?;
157157
Ok(PublicKeyX509Der::from(Buffer::new(der)))
158158
}
159159
}
@@ -182,7 +182,7 @@ impl Ed25519KeyPair {
182182
let evp_pkey = generate_key()?;
183183

184184
let mut public_key = [0u8; ED25519_PUBLIC_KEY_LEN];
185-
let out_len: usize = evp_pkey.marshal_raw_public_to_buffer(&mut public_key)?;
185+
let out_len: usize = evp_pkey.as_const().marshal_raw_public_to_buffer(&mut public_key)?;
186186
debug_assert_eq!(public_key.len(), out_len);
187187

188188
Ok(Self {
@@ -219,7 +219,7 @@ impl Ed25519KeyPair {
219219
pub fn generate_pkcs8(_rng: &dyn SecureRandom) -> Result<Document, Unspecified> {
220220
let evp_pkey = generate_key()?;
221221
Ok(Document::new(
222-
evp_pkey.marshal_rfc5208_private_key(Version::V2)?,
222+
evp_pkey.as_const().marshal_rfc5208_private_key(Version::V2)?,
223223
))
224224
}
225225

@@ -230,7 +230,7 @@ impl Ed25519KeyPair {
230230
///
231231
pub fn to_pkcs8(&self) -> Result<Document, Unspecified> {
232232
Ok(Document::new(
233-
self.evp_pkey.marshal_rfc5208_private_key(Version::V2)?,
233+
self.evp_pkey.as_const().marshal_rfc5208_private_key(Version::V2)?,
234234
))
235235
}
236236

@@ -251,7 +251,7 @@ impl Ed25519KeyPair {
251251
pub fn generate_pkcs8v1(_rng: &dyn SecureRandom) -> Result<Document, Unspecified> {
252252
let evp_pkey = generate_key()?;
253253
Ok(Document::new(
254-
evp_pkey.marshal_rfc5208_private_key(Version::V1)?,
254+
evp_pkey.as_const().marshal_rfc5208_private_key(Version::V1)?,
255255
))
256256
}
257257

@@ -262,7 +262,7 @@ impl Ed25519KeyPair {
262262
///
263263
pub fn to_pkcs8v1(&self) -> Result<Document, Unspecified> {
264264
Ok(Document::new(
265-
self.evp_pkey.marshal_rfc5208_private_key(Version::V1)?,
265+
self.evp_pkey.as_const().marshal_rfc5208_private_key(Version::V1)?,
266266
))
267267
}
268268

@@ -307,7 +307,7 @@ impl Ed25519KeyPair {
307307
let evp_pkey = LcPtr::<EVP_PKEY>::parse_raw_private_key(seed, EVP_PKEY_ED25519)?;
308308

309309
let mut derived_public_key = [0u8; ED25519_PUBLIC_KEY_LEN];
310-
let out_len: usize = evp_pkey.marshal_raw_public_to_buffer(&mut derived_public_key)?;
310+
let out_len: usize = evp_pkey.as_const().marshal_raw_public_to_buffer(&mut derived_public_key)?;
311311
debug_assert_eq!(derived_public_key.len(), out_len);
312312

313313
Ok(Self {
@@ -360,10 +360,10 @@ impl Ed25519KeyPair {
360360
fn parse_pkcs8(pkcs8: &[u8]) -> Result<Self, KeyRejected> {
361361
let evp_pkey = LcPtr::<EVP_PKEY>::parse_rfc5208_private_key(pkcs8, EVP_PKEY_ED25519)?;
362362

363-
evp_pkey.validate_as_ed25519()?;
363+
evp_pkey.as_const().validate_as_ed25519()?;
364364

365365
let mut public_key = [0u8; ED25519_PUBLIC_KEY_LEN];
366-
let out_len: usize = evp_pkey.marshal_raw_public_to_buffer(&mut public_key)?;
366+
let out_len: usize = evp_pkey.as_const().marshal_raw_public_to_buffer(&mut public_key)?;
367367
debug_assert_eq!(public_key.len(), out_len);
368368

369369
Ok(Self {
@@ -406,7 +406,7 @@ impl Ed25519KeyPair {
406406
/// Currently the function cannot fail, but it might in future implementations.
407407
pub fn seed(&self) -> Result<Seed<'static>, Unspecified> {
408408
Ok(Seed {
409-
bytes: self.evp_pkey.marshal_raw_private_key()?.into_boxed_slice(),
409+
bytes: self.evp_pkey.as_const().marshal_raw_private_key()?.into_boxed_slice(),
410410
phantom: PhantomData,
411411
})
412412
}
@@ -419,7 +419,7 @@ impl AsDer<Pkcs8V1Der<'static>> for Ed25519KeyPair {
419419
/// `error::Unspecified` on internal error.
420420
fn as_der(&self) -> Result<Pkcs8V1Der<'static>, crate::error::Unspecified> {
421421
Ok(Pkcs8V1Der::new(
422-
self.evp_pkey.marshal_rfc5208_private_key(Version::V1)?,
422+
self.evp_pkey.as_const().marshal_rfc5208_private_key(Version::V1)?,
423423
))
424424
}
425425
}
@@ -431,7 +431,7 @@ impl AsDer<Pkcs8V2Der<'static>> for Ed25519KeyPair {
431431
/// `error::Unspecified` on internal error.
432432
fn as_der(&self) -> Result<Pkcs8V2Der<'static>, crate::error::Unspecified> {
433433
Ok(Pkcs8V2Der::new(
434-
self.evp_pkey.marshal_rfc5208_private_key(Version::V2)?,
434+
self.evp_pkey.as_const().marshal_rfc5208_private_key(Version::V2)?,
435435
))
436436
}
437437
}

aws-lc-rs/src/evp_pkey.rs

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<T> EVP_PKEY_CTX_consumer for T where T: Fn(*mut EVP_PKEY_CTX) -> Result<(),
4343
#[allow(non_upper_case_globals, clippy::type_complexity)]
4444
pub(crate) const No_EVP_PKEY_CTX_consumer: Option<fn(*mut EVP_PKEY_CTX) -> Result<(), ()>> = None;
4545

46-
impl LcPtr<EVP_PKEY> {
46+
impl ConstPointer<'_, EVP_PKEY> {
4747
pub(crate) fn validate_as_ed25519(&self) -> Result<(), KeyRejected> {
4848
const ED25519_KEY_TYPE: c_int = EVP_PKEY_ED25519;
4949
const ED25519_MIN_BITS: c_int = 253;
@@ -79,35 +79,29 @@ impl LcPtr<EVP_PKEY> {
7979
// EVP_PKEY_X448 = 961;
8080
// EVP_PKEY_ED448 = 960;
8181
pub(crate) fn id(&self) -> i32 {
82-
unsafe { EVP_PKEY_id(*self.as_const()) }
82+
unsafe { EVP_PKEY_id(**self) }
8383
}
8484

8585
pub(crate) fn key_size_bytes(&self) -> usize {
8686
self.key_size_bits() / 8
8787
}
8888

8989
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()
9391
}
9492

9593
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()
9995
}
10096

10197
#[allow(dead_code)]
10298
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())
107101
}
108102

109103
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) })
111105
.map_err(|()| KeyRejected::wrong_algorithm())
112106
}
113107

@@ -116,76 +110,37 @@ impl LcPtr<EVP_PKEY> {
116110
// size in bytes for keys ranging from 2048-bit to 4096-bit. So size the initial capacity to be roughly
117111
// 500% as a conservative estimate to avoid needing to reallocate for any key in that range.
118112
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) } {
120114
return Err(Unspecified);
121115
}
122116
cbb.into_vec()
123117
}
124118

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-
140119
pub(crate) fn marshal_rfc5208_private_key(
141120
&self,
142121
version: Version,
143122
) -> 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;
147125
let mut cbb = LcCBB::new(key_size_bytes * 5);
148126
match version {
149127
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) } {
151129
return Err(Unspecified);
152130
}
153131
}
154132
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) } {
156134
return Err(Unspecified);
157135
}
158136
}
159137
}
160138
cbb.into_vec()
161139
}
162140

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-
186141
pub(crate) fn marshal_raw_private_key(&self) -> Result<Vec<u8>, Unspecified> {
187142
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) } {
189144
return Err(Unspecified);
190145
}
191146
let mut buffer = vec![0u8; size];
@@ -199,9 +154,7 @@ impl LcPtr<EVP_PKEY> {
199154
buffer: &mut [u8],
200155
) -> Result<usize, Unspecified> {
201156
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) } {
205158
Ok(key_len)
206159
} else {
207160
Err(Unspecified)
@@ -211,7 +164,7 @@ impl LcPtr<EVP_PKEY> {
211164
#[allow(dead_code)]
212165
pub(crate) fn marshal_raw_public_key(&self) -> Result<Vec<u8>, Unspecified> {
213166
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) } {
215168
return Err(Unspecified);
216169
}
217170
let mut buffer = vec![0u8; size];
@@ -229,14 +182,57 @@ impl LcPtr<EVP_PKEY> {
229182
// `EVP_PKEY_get_raw_public_key` writes the total length
230183
// to `encapsulate_key_size` in the event that the buffer we provide is larger then
231184
// 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)
233186
} {
234187
Ok(key_len)
235188
} else {
236189
Err(Unspecified)
237190
}
238191
}
239192

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+
240236
pub(crate) fn parse_raw_private_key(
241237
bytes: &[u8],
242238
evp_pkey_type: c_int,

0 commit comments

Comments
 (0)