Skip to content

Commit 2f94121

Browse files
Rust wrapper: require caller supplied buffer for Lms.get_kid()
Fixes F-1073.
1 parent 10fbc95 commit 2f94121

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

wrapper/rust/wolfssl-wolfcrypt/src/lms.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,17 @@ impl Lms {
757757

758758
/// Get the Key ID (I value) for this LMS/HSS key.
759759
///
760-
/// Returns a slice pointing into the key's internal storage.
760+
/// Copies the key ID into the provided buffer.
761+
///
762+
/// # Parameters
763+
///
764+
/// * `kid`: Buffer in which to store the key ID.
761765
///
762766
/// # Returns
763767
///
764-
/// Returns either Ok(&[u8]) containing the key ID on success, or Err(e)
765-
/// containing the wolfSSL library error code value.
766-
pub fn get_kid(&mut self) -> Result<&[u8], i32> {
768+
/// Returns either Ok(usize) containing the key ID length on success,
769+
/// or Err(e) containing the wolfSSL library error code value.
770+
pub fn get_kid(&mut self, kid: &mut [u8]) -> Result<usize, i32> {
767771
let mut kid_ptr: *const u8 = core::ptr::null();
768772
let mut kid_sz: u32 = 0;
769773
let rc = unsafe {
@@ -772,8 +776,12 @@ impl Lms {
772776
if rc != 0 {
773777
return Err(rc);
774778
}
775-
let slice = unsafe { core::slice::from_raw_parts(kid_ptr, kid_sz as usize) };
776-
Ok(slice)
779+
let src = unsafe { core::slice::from_raw_parts(kid_ptr, kid_sz as usize) };
780+
if kid.len() < src.len() {
781+
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
782+
}
783+
kid[..src.len()].copy_from_slice(src);
784+
Ok(src.len())
777785
}
778786
}
779787

wrapper/rust/wolfssl-wolfcrypt/tests/test_lms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ fn test_get_kid() {
386386
setup_callbacks(&mut key, ctx);
387387
key.make_key(&mut rng).expect("Error with make_key()");
388388

389-
let kid = key.get_kid().expect("Error with get_kid()");
390-
assert_eq!(kid.len(), Lms::KEY_ID_LEN, "kid must be KEY_ID_LEN bytes");
389+
let mut kid = [0u8; Lms::KEY_ID_LEN];
390+
key.get_kid(&mut kid).expect("Error with get_kid()");
391391

392392
let _ = store;
393393
}

0 commit comments

Comments
 (0)