-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encrypt bearer tokens at rest (#302)
- Loading branch information
Showing
25 changed files
with
444 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
migration/src/m20230731_181722_rename_aggregator_bearer_token.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.exec_stmt(Table::truncate().table(Task::Table).to_owned()) | ||
.await?; | ||
|
||
manager | ||
.exec_stmt(Table::truncate().table(Aggregator::Table).to_owned()) | ||
.await?; | ||
|
||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Aggregator::Table) | ||
.add_column( | ||
ColumnDef::new(Aggregator::EncryptedBearerToken) | ||
.binary() | ||
.not_null(), | ||
) | ||
.drop_column(Aggregator::BearerToken) | ||
.to_owned(), | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.exec_stmt(Table::truncate().table(Task::Table).to_owned()) | ||
.await?; | ||
manager | ||
.exec_stmt(Table::truncate().table(Aggregator::Table).to_owned()) | ||
.await?; | ||
|
||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Aggregator::Table) | ||
.add_column(ColumnDef::new(Aggregator::BearerToken).string().not_null()) | ||
.drop_column(Aggregator::EncryptedBearerToken) | ||
.to_owned(), | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Iden)] | ||
enum Aggregator { | ||
Table, | ||
BearerToken, | ||
EncryptedBearerToken, | ||
} | ||
|
||
#[derive(Iden)] | ||
enum Task { | ||
Table, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use aes_gcm::{ | ||
aead::{AeadCore, AeadInPlace, KeyInit, OsRng}, | ||
Aes128Gcm as AesGcm, Error, KeySizeUser, Nonce, | ||
}; | ||
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, DecodeError, Engine}; | ||
use std::{ | ||
collections::VecDeque, | ||
fmt::{self, Debug, Formatter}, | ||
iter, | ||
str::FromStr, | ||
sync::Arc, | ||
}; | ||
use typenum::marker_traits::Unsigned; | ||
|
||
pub type Key = aes_gcm::Key<AesGcm>; | ||
|
||
#[derive(Clone)] | ||
pub struct Crypter(Arc<CrypterInner>); | ||
|
||
#[derive(thiserror::Error, Debug, Clone)] | ||
pub enum CrypterParseError { | ||
#[error(transparent)] | ||
Base64(#[from] DecodeError), | ||
|
||
#[error("incorrect key length, must be {} bytes after base64 decode", <AesGcm as KeySizeUser>::KeySize::USIZE)] | ||
IncorrectLength, | ||
|
||
#[error("at least one key needed")] | ||
Missing, | ||
} | ||
|
||
impl FromStr for Crypter { | ||
type Err = CrypterParseError; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut keys = s | ||
.split(',') | ||
.map(|s| { | ||
URL_SAFE_NO_PAD | ||
.decode(s) | ||
.map_err(CrypterParseError::Base64) | ||
.and_then(|v| Key::from_exact_iter(v).ok_or(CrypterParseError::IncorrectLength)) | ||
}) | ||
.collect::<Result<VecDeque<Key>, _>>()?; | ||
let current_key = keys.pop_front().ok_or(CrypterParseError::Missing)?; | ||
Ok(Self::new(current_key, keys)) | ||
} | ||
} | ||
|
||
impl Debug for Crypter { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Crypter") | ||
.field("current_ciphers", &"..") | ||
.field("past_ciphers", &self.0.past_ciphers.len()) | ||
.finish() | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
struct CrypterInner { | ||
current_cipher: AesGcm, | ||
past_ciphers: Vec<AesGcm>, | ||
} | ||
|
||
impl From<Key> for Crypter { | ||
fn from(key: Key) -> Self { | ||
Self::new(key, []) | ||
} | ||
} | ||
|
||
impl Crypter { | ||
pub fn new(current_key: Key, past_keys: impl IntoIterator<Item = Key>) -> Self { | ||
Self(Arc::new(CrypterInner { | ||
current_cipher: AesGcm::new(¤t_key), | ||
past_ciphers: past_keys.into_iter().map(|key| AesGcm::new(&key)).collect(), | ||
})) | ||
} | ||
|
||
pub fn generate_key() -> Key { | ||
AesGcm::generate_key(OsRng) | ||
} | ||
|
||
pub fn encrypt(&self, associated_data: &[u8], plaintext: &[u8]) -> Result<Vec<u8>, Error> { | ||
self.0.encrypt(associated_data, plaintext) | ||
} | ||
|
||
pub fn decrypt( | ||
&self, | ||
associated_data: &[u8], | ||
nonce_and_ciphertext: &[u8], | ||
) -> Result<Vec<u8>, Error> { | ||
self.0.decrypt(associated_data, nonce_and_ciphertext) | ||
} | ||
} | ||
|
||
impl CrypterInner { | ||
fn encrypt(&self, associated_data: &[u8], plaintext: &[u8]) -> Result<Vec<u8>, Error> { | ||
let nonce = AesGcm::generate_nonce(&mut OsRng); | ||
let mut buffer = plaintext.to_vec(); | ||
self.current_cipher | ||
.encrypt_in_place(&nonce, associated_data, &mut buffer)?; | ||
let mut nonce_and_ciphertext = nonce.to_vec(); | ||
nonce_and_ciphertext.append(&mut buffer); | ||
Ok(nonce_and_ciphertext) | ||
} | ||
|
||
fn decrypt( | ||
&self, | ||
associated_data: &[u8], | ||
nonce_and_ciphertext: &[u8], | ||
) -> Result<Vec<u8>, Error> { | ||
let nonce_size = <AesGcm as AeadCore>::NonceSize::USIZE; | ||
if nonce_and_ciphertext.len() < nonce_size { | ||
return Err(Error); | ||
} | ||
|
||
let (nonce, ciphertext) = nonce_and_ciphertext.split_at(nonce_size); | ||
|
||
self.cipher_iter() | ||
.find_map(|cipher| { | ||
self.decrypt_with_key(cipher, associated_data, nonce, ciphertext) | ||
.ok() | ||
}) | ||
.ok_or(Error) | ||
} | ||
|
||
fn cipher_iter(&self) -> impl Iterator<Item = &AesGcm> { | ||
iter::once(&self.current_cipher).chain(self.past_ciphers.iter()) | ||
} | ||
|
||
fn decrypt_with_key( | ||
&self, | ||
cipher: &AesGcm, | ||
associated_data: &[u8], | ||
nonce: &[u8], | ||
ciphertext: &[u8], | ||
) -> Result<Vec<u8>, Error> { | ||
let nonce = Nonce::from_slice(nonce); | ||
let mut bytes = ciphertext.to_vec(); | ||
cipher.decrypt_in_place(nonce, associated_data, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.