Skip to content

Commit

Permalink
lli - Allow decoding both padded and unpadded Base64 data
Browse files Browse the repository at this point in the history
Signed-off-by: lli <lli@anonyome.com>
  • Loading branch information
lukewli-anonyome committed Dec 6, 2023
1 parent 882529c commit d4e757e
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions aries/aries_vcx/src/common/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ use time;

use crate::errors::error::prelude::*;

// Utility function to handle both padded and unpadded Base64URL data
fn base64url_decode(encoded: &str) -> VcxResult<Vec<u8>> {
general_purpose::URL_SAFE_NO_PAD.decode(encoded).or_else(|_|
general_purpose::URL_SAFE.decode(encoded)
).map_err(|err| {
AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidJson,
format!("Cannot decode Base64URL data: {:?}", err),
)
})
}

async fn get_signature_data(
wallet: &impl BaseWallet,
data: String,
Expand Down Expand Up @@ -52,14 +64,7 @@ pub async fn decode_signed_connection_response(
)
})?;

let sig_data = general_purpose::URL_SAFE
.decode(response.connection_sig.sig_data.as_bytes())
.map_err(|err| {
AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidJson,
format!("Cannot decode ConnectionResponse: {:?}", err),
)
})?;
let sig_data = base64url_decode(&response.connection_sig.sig_data)?;

if !wallet.verify(their_vk, &sig_data, &signature).await? {
return Err(AriesVcxError::from_msg(
Expand Down Expand Up @@ -146,3 +151,22 @@ pub async fn decode_signed_connection_response(
// .await;
// }
// }

#[cfg(test)]
mod tests {
use super::*;
use base64::{self, engine::general_purpose, Engine};

#[tokio::test]
async fn test_decode_padded_and_unpadded_sig_data() {
let data = b"example data for testing";
let encoded_padded = general_purpose::URL_SAFE.encode(data);
let encoded_unpadded = general_purpose::URL_SAFE_NO_PAD.encode(data);

let decoded_padded = base64url_decode(&encoded_padded).unwrap();
let decoded_unpadded = base64url_decode(&encoded_unpadded).unwrap();

assert_eq!(decoded_padded, data.to_vec());
assert_eq!(decoded_unpadded, data.to_vec());
}
}

0 comments on commit d4e757e

Please sign in to comment.