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

Add helper methods to make it more convenient to reuse a URL #8

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
14 changes: 12 additions & 2 deletions src/jose.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use std::time::Duration;

use base64ct::{Base64Url, Base64UrlUnpadded, Encoding};
use elliptic_curve::sec1::{EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint};
Expand All @@ -16,7 +17,7 @@ use zeroize::Zeroize;

use crate::key_exchange::{create_enc_key, recover_enc_key};
use crate::util::{b64_to_bytes, b64_to_str};
use crate::{EncryptionKey, Error, Result};
use crate::{EncryptionKey, Error, Result, TangClient};

/// Representation of a tang advertisment response which is a JWS of available keys.
///
Expand Down Expand Up @@ -486,7 +487,6 @@ struct TangParams {

impl KeyMeta {
/// Serialize this data to a JSON string
#[must_use]
pub fn to_json(&self) -> String {
serde_json::to_string(self).expect("serialization failure")
}
Expand All @@ -496,6 +496,16 @@ impl KeyMeta {
serde_json::from_str(val).map_err(Into::into)
}

/// Create a [`TangClient`] from the URL used to generate this key
pub fn client(&self, timeout: Option<Duration>) -> TangClient {
TangClient::new(&self.clevis.tang.url, timeout)
}

/// The URL that was used to generate this key
pub fn url(&self) -> &str {
&self.clevis.tang.url
}

pub(crate) fn recover_key<const N: usize>(
&self,
server_key_exchange: impl FnOnce(&str, &Jwk) -> Result<Jwk>,
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
//! This is still under development, but works reasonibly well.
//!
//! ```
//! # #[cfg(not(feature = "_backend"))] fn main() {}
//! # #[cfg(feature = "_backend")]
//! # fn main() {
//! # #[cfg(feature = "_backend")] test();
//! # }
//! #
//! # fn test() {
//! use clevis::{KeyMeta, TangClient};
//!
//! /// 32-byte (256 bit) key
//! /// 32-byte (256 bit) key, such as for AES256-GCM
//! const KEY_BYTES: usize = 32;
//!
//! /* key provisioning */
//!
//! let client = TangClient::new("localhost:11697", None);
//!
//! // create a key suitible for encryption (i.e. has gone through a KDF)
//! let out = client
//! let out = TangClient::new("localhost:11697", None)
//! .create_secure_key::<KEY_BYTES>()
//! .expect("failed to generate key");
//!
Expand All @@ -33,7 +33,8 @@
//! /* key recovery */
//!
//! let new_meta = KeyMeta::from_json(&meta_str).expect("invalid metadata");
//! let new_key = client
//! let new_key = new_meta
//! .client(None)
//! .recover_secure_key::<KEY_BYTES>(&new_meta)
//! .expect("failed to retrieve key");
//!
Expand All @@ -44,6 +45,7 @@
#![warn(clippy::pedantic)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::must_use_candidate)]

mod error;
mod jose;
Expand Down
11 changes: 9 additions & 2 deletions src/tang_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::{EncryptionKey, Result};
// const DEFAULT_URL: &str = "http://tang.local";
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(120);

/// A tang server connection specification
/// A tang server connection specification.
///
/// This does not hold an active connection, only connection parameters.
#[must_use]
#[derive(Clone, Debug)]
pub struct TangClient {
url: String,
Expand All @@ -15,7 +18,6 @@ pub struct TangClient {

impl TangClient {
/// Create a new client. If timeout is not specified, it will default to 120s.
#[must_use]
pub fn new(url: &str, timeout: Option<Duration>) -> Self {
let url = if url.starts_with("http") {
url.to_owned()
Expand All @@ -28,6 +30,11 @@ impl TangClient {
}
}

/// This client's connection URL
pub fn url(&self) -> &str {
&self.url
}

/// Locate derive keys from the server and provision an encryption key with specified lengh.
pub fn create_secure_key<const KEYBYTES: usize>(&self) -> Result<ProvisionedData<KEYBYTES>> {
let (keys, signing_thp) = self.fetch_keys(None)?;
Expand Down
Loading