-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): add crypto module Add crypto module that allows to configure SDK to encrypt and decrypt messages. fix(crypto): fix legacy cryptor Improved security of crypto implementation by adding enhanced AES-CBC cryptor. --------- Co-authored-by: Mateusz Dahlke <39696234+Xavrax@users.noreply.github.com> Co-authored-by: Xavrax <matedahlke@gmail.com> Co-authored-by: josh-lubliner <103449628+josh-lubliner@users.noreply.github.com>
- Loading branch information
1 parent
d917aec
commit f15bb75
Showing
27 changed files
with
1,741 additions
and
247 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,22 +1,29 @@ | ||
The MIT License (MIT) | ||
PubNub Software Development Kit License Agreement | ||
Copyright © 2023 PubNub Inc. All rights reserved. | ||
|
||
Copyright (c) 2023 PubNub Inc. | ||
Subject to the terms and conditions of the license, you are hereby granted | ||
a non-exclusive, worldwide, royalty-free license to (a) copy and modify | ||
the software in source code or binary form for use with the software services | ||
and interfaces provided by PubNub, and (b) redistribute unmodified copies | ||
of the software to third parties. The software may not be incorporated in | ||
or used to provide any product or service competitive with the products | ||
and services of PubNub. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this license shall be included | ||
in or with all copies or substantial portions of the software. | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
This license does not grant you permission to use the trade names, trademarks, | ||
service marks, or product names of PubNub, except as required for reasonable | ||
and customary use in describing the origin of the software and reproducing | ||
the content of this license. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF | ||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | ||
EVENT SHALL PUBNUB OR THE AUTHORS OR COPYRIGHT HOLDERS OF THE SOFTWARE BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
https://www.pubnub.com/ | ||
https://www.pubnub.com/terms |
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,75 @@ | ||
use pubnub::core::CryptoProvider; | ||
use pubnub::providers::crypto::CryptoModule; | ||
use pubnub::{Keyset, PubNubClientBuilder}; | ||
use std::env; | ||
|
||
/// This example demonstrates how data can be manually encrypted or | ||
/// automatically as part of PubNubClient instance when publish / subscribe is | ||
/// used. | ||
/// | ||
/// The following example consists of two parts: a manual _encryption_ and | ||
/// _decryption_ demonstration, and an automated _encryption_ and _decryption_ | ||
/// demonstration, as part of a configured `PubNubClientInstance`. | ||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn snafu::Error>> { | ||
let source_data: Vec<u8> = "Hello world!".into(); | ||
let use_random_iv = true; | ||
let cipher = "enigma"; | ||
|
||
// ----------------------------------------- | ||
// Manual encryption and decryption example. | ||
// | ||
|
||
// Crypto module with legacy AES-CBC cryptor (with enhanced AES-CBC decrypt | ||
// support). | ||
let legacy_crypto_module = CryptoModule::new_legacy_module(cipher, use_random_iv)?; | ||
let legacy_encrypt_result = legacy_crypto_module.encrypt(source_data.clone()); | ||
|
||
println!("encrypt with legacy AES-CBC result: {legacy_encrypt_result:?}"); | ||
|
||
// Crypto module with enhanced AES-CBC cryptor (with legacy AES-CBC decrypt | ||
// support). | ||
let crypto_module = CryptoModule::new_aes_cbc_module(cipher, use_random_iv)?; | ||
let encrypt_result = crypto_module.encrypt(source_data.clone()); | ||
|
||
println!("encrypt with enhanced AES-CBC result: {encrypt_result:?}"); | ||
|
||
// Decrypt data created with legacy AES-CBC crypto module. | ||
let legacy_decrypt_result = crypto_module.decrypt(legacy_encrypt_result.ok().unwrap())?; | ||
assert_eq!(legacy_decrypt_result, source_data); | ||
|
||
// Decrypt data created with enhanced AES-CBC crypto module. | ||
let decrypt_result = legacy_crypto_module.decrypt(encrypt_result.ok().unwrap())?; | ||
assert_eq!(decrypt_result, source_data); | ||
|
||
// -------------------------------------------- | ||
// Automated encryption and decryption example. | ||
// | ||
|
||
// Setup client with crypto module | ||
let publish_key = env::var("SDK_PUB_KEY")?; | ||
let subscribe_key = env::var("SDK_SUB_KEY")?; | ||
|
||
let client = PubNubClientBuilder::with_reqwest_transport() | ||
.with_keyset(Keyset { | ||
subscribe_key, | ||
publish_key: Some(publish_key), | ||
secret_key: None, | ||
}) | ||
.with_user_id("user_id") | ||
.with_cryptor(crypto_module) | ||
.build()?; | ||
|
||
// publish encrypted string | ||
let result = client | ||
.publish_message("hello world!") | ||
.channel("my_channel") | ||
.r#type("text-message") | ||
.execute() | ||
.await?; | ||
|
||
println!("publish result: {:?}", result); | ||
|
||
Ok(()) | ||
} |
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,27 @@ | ||
//! # Crypto provider module | ||
//! | ||
//! This module contains the [`CryptoProvider`] trait, which is used to | ||
//! implement a module that can be used to configure [`PubNubClientInstance`] or | ||
//! for manual data encryption and decryption. | ||
use crate::{ | ||
core::PubNubError, | ||
lib::{alloc::vec::Vec, core::fmt::Debug}, | ||
}; | ||
|
||
/// Crypto provider trait. | ||
pub trait CryptoProvider: Debug + Send + Sync { | ||
/// Encrypt provided data. | ||
/// | ||
/// # Errors | ||
/// Should return an [`PubNubError::Encryption`] if provided data can't be | ||
/// _encrypted_ or underlying cryptor misconfigured. | ||
fn encrypt(&self, data: Vec<u8>) -> Result<Vec<u8>, PubNubError>; | ||
|
||
/// Decrypt provided data. | ||
/// | ||
/// # Errors | ||
/// Should return an [`PubNubError::Decryption`] if provided data can't be | ||
/// _decrypted_ or underlying cryptor misconfigured. | ||
fn decrypt(&self, data: Vec<u8>) -> Result<Vec<u8>, PubNubError>; | ||
} |
Oops, something went wrong.