Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow external private key in v2 #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ct_codecs::{Base64UrlSafeNoPadding, Decoder, Encoder};
use subtle::ConstantTimeEq;

/// Encode bytes with Base64 URL-safe and no padding.
pub(crate) fn encode_b64<T: AsRef<[u8]>>(bytes: T) -> Result<String, Error> {
pub fn encode_b64<T: AsRef<[u8]>>(bytes: T) -> Result<String, Error> {
let inlen = bytes.as_ref().len();
let mut buf = vec![0u8; Base64UrlSafeNoPadding::encoded_len(inlen)?];

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@
#[macro_use]
extern crate alloc;

mod pae;
pub mod pae;

Check failure on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / rustfmt and clippy

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / WebAssembly - Release build (wasm32-unknown-unknown)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (i686-unknown-linux-gnu)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (armv7-unknown-linux-gnueabihf)

missing documentation for a module

Check warning on line 192 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (mips64-unknown-linux-gnuabi64)

missing documentation for a module

/// Errors for token operations.
pub mod errors;

mod common;
pub mod common;

Check failure on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / rustfmt and clippy

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / WebAssembly - Release build (wasm32-unknown-unknown)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (i686-unknown-linux-gnu)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (armv7-unknown-linux-gnueabihf)

missing documentation for a module

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux/ARM/Big-Endian/32-Bit - Release tests (mips64-unknown-linux-gnuabi64)

missing documentation for a module

#[cfg(feature = "std")]
/// Claims for tokens and validation thereof.
Expand Down
37 changes: 24 additions & 13 deletions src/version2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,44 @@ impl PublicToken {
/// The header and purpose for the public token: `v2.public.`.
pub const HEADER: &'static str = "v2.public.";

/// Create a public token.
pub fn sign(
secret_key: &AsymmetricSecretKey<V2>,
message: &[u8],
footer: Option<&[u8]>,
) -> Result<String, Error> {
/// Pre-authenticated encoding
pub fn pae(message: &[u8], footer: &[u8]) -> Result<Vec<u8>, Error> {
if message.is_empty() {
return Err(Error::EmptyPayload);
}

let sk = SigningKey::from_slice(secret_key.as_bytes()).map_err(|_| Error::Key)?;
let f = footer.unwrap_or(&[]);
let m2 = pae::pae(&[Self::HEADER.as_bytes(), message, f])?;
let sig = sk.sign(m2, None);
pae::pae(&[Self::HEADER.as_bytes(), message, footer])
}

/// Concatenate message with signature
pub fn concatenate(message: &[u8], sig: &[u8], footer: &[u8]) -> Result<String, Error> {
let mut m_sig: Vec<u8> = Vec::from(message);
m_sig.extend_from_slice(sig.as_ref());
m_sig.extend_from_slice(sig);

let token_no_footer = format!("{}{}", Self::HEADER, encode_b64(m_sig)?);

if f.is_empty() {
if footer.is_empty() {
Ok(token_no_footer)
} else {
Ok(format!("{}.{}", token_no_footer, encode_b64(f)?))
Ok(format!("{}.{}", token_no_footer, encode_b64(footer)?))
}
}

/// Create a public token.
pub fn sign(
secret_key: &AsymmetricSecretKey<V2>,
message: &[u8],
footer: Option<&[u8]>,
) -> Result<String, Error> {
let f = footer.unwrap_or(&[]);
let m2 = Self::pae(message, f)?;

let sk = SigningKey::from_slice(secret_key.as_bytes()).map_err(|_| Error::Key)?;
let sig = sk.sign(m2, None);

Self::concatenate(message, sig.as_ref(), f)
}

/// Verify a public token.
///
/// If `footer.is_none()`, then it will be validated but not compared to a known value.
Expand Down
Loading