Skip to content

Commit

Permalink
chore: Move base64 into c2pa-crypto (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten-adobe authored Nov 15, 2024
1 parent d2b3082 commit 081655a
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 21 deletions.
1 change: 1 addition & 0 deletions internal/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
base64 = "0.22.1"
sha1 = "0.10.6"

[dependencies.chrono]
Expand Down
10 changes: 7 additions & 3 deletions sdk/src/utils/base64.rs → internal/crypto/src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
// specific language governing permissions and limitations under
// each license.

use base64::{engine::general_purpose, Engine as _};
//! Base64 convenience functions.

pub(crate) fn encode(data: &[u8]) -> String {
use ::base64::{engine::general_purpose, DecodeError, Engine as _};

/// Encode a byte slice to Base64 string using general encoding without padding.
pub fn encode(data: &[u8]) -> String {
general_purpose::STANDARD.encode(data)
}

pub(crate) fn decode(data: &str) -> Result<Vec<u8>, base64::DecodeError> {
/// Decode a Base 64 string into a byte slice.
pub fn decode(data: &str) -> Result<Vec<u8>, DecodeError> {
general_purpose::STANDARD.decode(data)
}
1 change: 1 addition & 0 deletions internal/crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg, doc_cfg_hide))]

pub mod base64;
pub mod hash;
pub(crate) mod internal;

Expand Down
32 changes: 32 additions & 0 deletions internal/crypto/src/tests/base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;

use crate::base64;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn encode() {
assert_eq!(base64::encode(b"Hello, world"), "SGVsbG8sIHdvcmxk");
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn decode() {
assert_eq!(
base64::decode("SGVsbG8sIHdvcmxk"),
Ok(b"Hello, world".to_vec())
);
}
1 change: 1 addition & 0 deletions internal/crypto/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

mod base64;
mod hash;
mod internal;
1 change: 0 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ async-generic = "1.1"
async-recursion = "1.1.1"
async-trait = { version = "0.1.77" }
atree = "0.5.2"
base64 = "0.22.1"
bcder = "0.7.3"
bytes = "1.7.2"
byteorder = { version = "1.4.3", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/asset_handlers/svg_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::{
path::Path,
};

use c2pa_crypto::base64;
use conv::ValueFrom;
use fast_xml::{
events::{BytesText, Event},
Expand All @@ -39,7 +40,7 @@ use crate::{
RemoteRefEmbed,
},
error::{Error, Result},
utils::{base64, io_utils::stream_len},
utils::io_utils::stream_len,
};

static SUPPORTED_TYPES: [&str; 8] = [
Expand Down
6 changes: 2 additions & 4 deletions sdk/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::path::Path;
use std::{collections::HashMap, fmt};

use async_generic::async_generic;
use c2pa_crypto::base64;
use c2pa_status_tracker::{log_item, OneShotStatusTracker, StatusTracker};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -52,10 +53,7 @@ use crate::{
jumbf_io::get_assetio_handler,
salt::{DefaultSalt, SaltGenerator, NO_SALT},
trust_handler::TrustHandlerConfig,
utils::{
base64,
hash_utils::{hash_by_alg, vec_compare, verify_by_alg},
},
utils::hash_utils::{hash_by_alg, vec_compare, verify_by_alg},
validation_status,
validator::ValidationInfo,
ClaimGeneratorInfo,
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::path::{Path, PathBuf};
use std::{borrow::Cow, io::Cursor};

use async_generic::async_generic;
use c2pa_crypto::base64;
use c2pa_status_tracker::{log_item, DetailedStatusTracker, StatusTracker};
use log::{debug, error};
#[cfg(feature = "json_schema")]
Expand All @@ -42,7 +43,7 @@ use crate::{
jumbf_io::load_jumbf_from_stream,
resource_store::{skip_serializing_resources, ResourceRef, ResourceStore},
store::Store,
utils::{base64, xmp_inmemory_utils::XmpInfo},
utils::xmp_inmemory_utils::XmpInfo,
validation_status::{self, status_for_store, ValidationStatus},
};

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/manifest_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{
};

use async_generic::async_generic;
use c2pa_crypto::base64;
use c2pa_status_tracker::{DetailedStatusTracker, StatusTracker};
#[cfg(feature = "json_schema")]
use schemars::JsonSchema;
Expand All @@ -28,7 +29,6 @@ use crate::{
claim::ClaimAssetData,
jumbf::labels::{manifest_label_from_uri, to_absolute_uri, to_relative_uri},
store::Store,
utils::base64,
validation_status::{status_for_store, ValidationStatus},
Error, Manifest, Result,
};
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/manifest_store_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ use std::collections::HashMap;
use std::path::Path;

use atree::{Arena, Token};
use c2pa_crypto::base64;
#[cfg(feature = "v1_api")]
use c2pa_status_tracker::{DetailedStatusTracker, StatusTracker};
use extfmt::Hexlify;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
assertion::AssertionData, claim::Claim, store::Store, utils::base64,
validation_status::ValidationStatus, Result,
assertion::AssertionData, claim::Claim, store::Store, validation_status::ValidationStatus,
Result,
};

/// Low level JSON based representation of Manifest Store - used for debugging
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/ocsp_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub(crate) fn fetch_ocsp_response(certs: &[Vec<u8>]) -> Option<Vec<u8>> {

// build query param
let request_der = rasn::der::encode(&ocsp_request).ok()?;
let request_str = crate::utils::base64::encode(&request_der);
let request_str = c2pa_crypto::base64::encode(&request_der);

let req_url = url.join(&request_str).ok()?;

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/openssl/openssl_trust_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use std::{
};

use asn1_rs::Oid;
use c2pa_crypto::base64;
use openssl::x509::verify::X509VerifyFlags;

use crate::{
hash_utils::hash_sha256,
trust_handler::{load_eku_configuration, TrustHandlerConfig},
utils::base64,
Error, Result,
};

Expand Down
3 changes: 2 additions & 1 deletion sdk/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use std::{
sync::RwLock,
};

use c2pa_crypto::base64;
use config::{Config, FileFormat};
use lazy_static::lazy_static;
#[cfg(feature = "json_schema")]
use schemars::JsonSchema;
use serde_derive::{Deserialize, Serialize};

use crate::{utils::base64, Error, Result};
use crate::{Error, Result};

lazy_static! {
static ref SETTINGS: RwLock<Config> =
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/trust_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use std::{
};

use asn1_rs::{oid, Oid};
use c2pa_crypto::base64;

use crate::{hash_utils::hash_sha256, utils::base64, Error, Result};
use crate::{hash_utils::hash_sha256, Error, Result};

pub(crate) static EMAIL_PROTECTION_OID: Oid<'static> = oid!(1.3.6 .1 .5 .5 .7 .3 .4);
pub(crate) static TIMESTAMPING_OID: Oid<'static> = oid!(1.3.6 .1 .5 .5 .7 .3 .8);
Expand Down
1 change: 0 additions & 1 deletion sdk/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// specific language governing permissions and limitations under
// each license.

pub(crate) mod base64;
pub(crate) mod cbor_types;
#[allow(dead_code)]
pub(crate) mod hash_utils;
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/utils/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@ impl WebCryptoSigner {
.replace("\n", "")
.replace(START_KEY, "")
.replace(END_KEY, "");
let key = crate::utils::base64::decode(&key).unwrap();
let key = c2pa_crypto::base64::decode(&key).unwrap();

let certs = cert
.replace("\n", "")
.replace(START_CERTIFICATE, "")
.split(END_CERTIFICATE)
.map(|x| crate::utils::base64::decode(x).unwrap())
.map(|x| c2pa_crypto::base64::decode(x).unwrap())
.collect();

Self {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wasm/webpki_trust_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{
};

use asn1_rs::{nom::AsBytes, Any, Class, Header, Tag};
use c2pa_crypto::base64;
use x509_parser::{
der_parser::der::{parse_der_integer, parse_der_sequence_of},
prelude::*,
Expand All @@ -30,7 +31,6 @@ use crate::{
trust_handler::{
has_allowed_oid, load_eku_configuration, load_trust_from_data, TrustHandlerConfig,
},
utils::base64,
wasm::webcrypto_validator::async_validate,
SigningAlg,
};
Expand Down

0 comments on commit 081655a

Please sign in to comment.