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

Microsoft DKE + refactoring of utils/crypto #170

Merged
merged 73 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
4e80521
DKE skeleton
bgrieder Dec 29, 2023
f9d2d68
get key alpha 1
bgrieder Jan 3, 2024
b7a4a4c
minor fixes
bgrieder Jan 8, 2024
3087b19
adding support for DKE decrypt
bgrieder Jan 8, 2024
d662fdd
encryption
bgrieder Jan 9, 2024
cd95f96
ms_dke: test RSA keys
bgrieder Jan 10, 2024
f3261b1
started ms_dks documentation
bgrieder Jan 11, 2024
503238a
more dke doc
bgrieder Jan 12, 2024
32bf52f
more ms_dke doc
bgrieder Jan 15, 2024
531f3b3
more ms_dke doc
bgrieder Jan 15, 2024
1712a5f
more more doc
bgrieder Jan 15, 2024
1210280
encryption OK - working on RSA decryption
bgrieder Jan 16, 2024
08fa916
before rebasing on RSA feature
bgrieder Jan 17, 2024
d8061ac
after rebase
bgrieder Jan 17, 2024
5b6e970
CVKM RSA PKCS OAEP
bgrieder Jan 19, 2024
614bdbb
fixed features
bgrieder Jan 19, 2024
ab59302
fixed RSA OAEP
bgrieder Jan 19, 2024
bfe3774
fixed RSA algorithms
bgrieder Jan 19, 2024
da13d70
first refacto of wrap tests
bgrieder Jan 20, 2024
0688b89
moved algorithms to the correct places
bgrieder Jan 20, 2024
6ee99f1
first pass at aead
bgrieder Jan 20, 2024
c101fb9
fixed MacOS tests and wrap tests
bgrieder Jan 21, 2024
16a5cd6
clippy fixes and removed some test logging
bgrieder Jan 21, 2024
dc910db
refactoring ecies and wrap
bgrieder Jan 22, 2024
49c6b42
direct use of HashingAlgorithm
bgrieder Jan 23, 2024
ddead15
refactored ECIES
bgrieder Jan 24, 2024
28666b9
getting rid of Hybrid Encryption
bgrieder Jan 24, 2024
c56daaf
minor fixes on encrypt.rs
bgrieder Jan 24, 2024
7e7c203
re-implementing Encrypt
bgrieder Jan 25, 2024
50367c6
cleaning up encrypt
bgrieder Jan 26, 2024
6e0e40a
working on decrypt
bgrieder Jan 26, 2024
9b5df0b
decrypt alpha
bgrieder Jan 29, 2024
ccec4fe
reworking decrypt.rs
bgrieder Jan 29, 2024
75282f1
finished reworking encrypt decrypt
bgrieder Jan 30, 2024
bf40935
DKE works
bgrieder Jan 30, 2024
b690542
first rebase on develop
bgrieder Feb 1, 2024
8c27074
cargo fmt
bgrieder Feb 1, 2024
f0029bc
clippy fixes
bgrieder Feb 1, 2024
8f799dc
RSA encrypt CLI
bgrieder Feb 1, 2024
31f5acc
added support for all RSA encryption algos in CLI
bgrieder Feb 2, 2024
833f76e
revisited algorithms documentation
bgrieder Feb 2, 2024
c316c23
fix: enhancing code and adding consts
Feb 2, 2024
e3c88d7
fix: removed TODO comments
Feb 2, 2024
e9ec0ea
FIPS doc + fix tests
bgrieder Feb 2, 2024
51cbec0
fixed ChaCha documentation
bgrieder Feb 2, 2024
eca3336
fix: fixed zeroization of keys and plaintexts
Feb 2, 2024
58371a1
fips: added non fips header for chacha20 consts
Feb 2, 2024
5cc9cb8
fixed EC tests
bgrieder Feb 2, 2024
cd1f3bb
rebase fix
bgrieder Feb 2, 2024
f470f9b
another rebase fix
bgrieder Feb 2, 2024
15e0d14
selecting client FIPS tests
bgrieder Feb 2, 2024
d05bc66
cleanup FIPS imports
bgrieder Feb 2, 2024
574f599
improved documentation
bgrieder Feb 3, 2024
060bacb
Intra ID JWT tests
bgrieder Feb 5, 2024
d341956
ecies with aes and shake depending on ec
bgrieder Feb 5, 2024
6ced333
doc: Correcting imprecisions in fips documentation
Feb 5, 2024
9839680
doc: fixed more imprecisions on fips documentation
Feb 5, 2024
4c4e0a5
doc: format issues
Feb 5, 2024
79df63b
start of MS DKE doc
bgrieder Feb 5, 2024
d56705d
documentation
bgrieder Feb 6, 2024
4fb6424
added test to verify ms_dke date format. Moved to UTC time
bgrieder Feb 6, 2024
d853020
rebase on develop
bgrieder Feb 6, 2024
b9954d0
ms_dke documentation
bgrieder Feb 6, 2024
0835e2f
fix: added not fips header to test encrypt certificate with P521
Feb 6, 2024
d7713de
fix: better naming
Feb 6, 2024
3fdc4cf
removed hybrid encryption
bgrieder Feb 6, 2024
1e35e7a
fix: removed dead file
Feb 6, 2024
4a1e26c
fix: removed dead file
Feb 6, 2024
18ea4be
improved ms_dke documentation
bgrieder Feb 7, 2024
6b47bff
fix: clippy and markdown lint warnings
Manuthor Feb 7, 2024
d053d15
docs(changelog): link the changelog to PR
Manuthor Feb 7, 2024
5033b74
ci: re-enable public_doc build on docs.staging.cosmian.com
Manuthor Feb 7, 2024
322ea6b
feat: zeroizing key material byte arrays (#175)
JosePisco Feb 8, 2024
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
15 changes: 15 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ jobs:

python_and_docker:
uses: ./.github/workflows/build_and_test_docker_image.yml

public_documentation:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Deploy documentation in staging
if: ${{ github.ref_name == 'develop' }}
uses: benc-uk/workflow-dispatch@v1
with:
workflow: staging.yml
repo: Cosmian/public_documentation
ref: develop
token: ${{ secrets.PAT_TOKEN }}
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ repos:
hooks:
- id: markdown-link-check
args: [-q]
exclude: documentation/docs/ms_dke/ms_dke.md

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.2
Expand Down Expand Up @@ -108,6 +109,7 @@ repos:
- id: debug-statements
- id: destroyed-symlinks
- id: detect-private-key
exclude: crate/server/src/tests/ms_dke
- id: double-quote-string-fixer
- id: end-of-file-fixer
- id: file-contents-sorter
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All notable changes to this project will be documented in this file.

- Generalize the refresh of JWKS in the middleware [#150](https://github.com/Cosmian/kms/pull/150).
- CI speed up [#173](https://github.com/Cosmian/kms/pull/173).
- Add support for Microsoft Double Key Encryption (DKE) endpoints [#170](https://github.com/Cosmian/kms/pull/170).
- Re-organized crypto package by algorithm, removed duplicated code [#170](https://github.com/Cosmian/kms/pull/170).
- Add support for FIPS mode for the ckms client [#170](https://github.com/Cosmian/kms/pull/170).
- Documented TOML configuration file for the KMS server [#170](https://github.com/Cosmian/kms/pull/170).
- Overall improvements to the documentation on algorithms and FIPS mode [#170](https://github.com/Cosmian/kms/pull/170).

## [4.11.3] - 2024-01-26

Expand Down Expand Up @@ -45,7 +50,7 @@ All notable changes to this project will be documented in this file.
### Features

- X509 v3 extensions support [#120](https://github.com/Cosmian/kms/issues/120)
- Dynamic salt for password derivation, resolving issue #124 [#128](https://github.com/Cosmian/kms/issues/128)
- Dynamic salt for password derivation, resolving issue [#124](https://github.com/Cosmian/kms/issues/124) [#128](https://github.com/Cosmian/kms/issues/128)
- Support Cosmian VM [#129](https://github.com/Cosmian/kms/issues/129)
- Make rsa oaep aes a generalized encryption system for use in all kms and not only for key wrapping [#130](https://github.com/Cosmian/kms/issues/130)
- ECIES implementation for Hybrid Encryption [#134](https://github.com/Cosmian/kms/issues/134)
Expand Down
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ reqwest = { version = "0.11.22", features = [
"native-tls",
"rustls-tls",
"stream",
"blocking",
] }
rustls = { version = "0.21", features = ["dangerous_configuration"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Then run the entrypoint script.

The `etc/app/server.toml` file contains:

```
```toml
[http]
port = 3000
hostname = "0.0.0.0"
Expand Down
1 change: 0 additions & 1 deletion crate/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ cosmian_logger = { path = "../logger" }
der = { workspace = true }
oauth2 = "4.4"
pem = "3.0"
rand = "0.8"
reqwest = { workspace = true }
rustls = { workspace = true }
serde = { workspace = true }
Expand Down
18 changes: 6 additions & 12 deletions crate/cli/src/actions/certificates/decrypt_certificate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::{fs::File, io::prelude::*, path::PathBuf};

use clap::Parser;
use cosmian_kmip::kmip::{
kmip_operations::{Decrypt, DecryptedData},
kmip_types::UniqueIdentifier,
};
use cosmian_kmip::kmip::{kmip_operations::Decrypt, kmip_types::UniqueIdentifier};
use cosmian_kms_client::KmsRestClient;

use crate::{
Expand Down Expand Up @@ -71,23 +68,20 @@ impl DecryptCertificateAction {
.decrypt(decrypt_request)
.await
.with_context(|| "Can't execute the query on the kms server")?;

let metadata_and_cleartext: DecryptedData = decrypt_response
let plaintext = decrypt_response
.data
.context("The plain data are empty")?
.as_slice()
.try_into()?;
.context("Decrypt with certificate: the plaintext is empty")?;

// Write the decrypted file
let output_file = self
.output_file
.clone()
.unwrap_or_else(|| self.input_file.clone().with_extension(".plain"));
let mut buffer =
File::create(&output_file).with_context(|| "Fail to write the plain file")?;
File::create(&output_file).with_context(|| "Fail to write the plaintext file")?;
buffer
.write_all(&metadata_and_cleartext.plaintext)
.with_context(|| "Fail to write the plain file")?;
.write_all(&plaintext)
.with_context(|| "Fail to write the plaintext file")?;

println!("The decrypted file is available at {:?}", &output_file);

Expand Down
3 changes: 2 additions & 1 deletion crate/cli/src/actions/certificates/import_certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cosmian_kms_client::KmsRestClient;
use der::{Decode, DecodePem, Encode};
use tracing::{debug, trace};
use x509_cert::Certificate;
use zeroize::Zeroizing;

use crate::{
actions::shared::{
Expand Down Expand Up @@ -243,7 +244,7 @@ impl ImportCertificateAction {

/// Import the certificate, the chain and the associated private key
async fn import_pkcs12(&self, kms_rest_client: &KmsRestClient) -> Result<String, CliError> {
let pkcs12_bytes = read_bytes_from_file(&self.get_certificate_file()?)?;
let pkcs12_bytes = Zeroizing::from(read_bytes_from_file(&self.get_certificate_file()?)?);

// Create a KMIP private key from the PKCS12 private key
let private_key = build_private_key_from_der_bytes(KeyFormatType::PKCS12, pkcs12_bytes);
Expand Down
1 change: 1 addition & 0 deletions crate/cli/src/actions/cover_crypt/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl DecryptAction {
.as_deref()
.map(|s| s.as_bytes().to_vec()),
Some(cryptographic_algorithm),
None,
);

tracing::debug!("{decrypt_request:?}");
Expand Down
1 change: 1 addition & 0 deletions crate/cli/src/actions/cover_crypt/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl EncryptAction {
.as_deref()
.map(|s| s.as_bytes().to_vec()),
Some(cryptographic_algorithm),
None,
)?;

tracing::debug!("{encrypt_request:?}");
Expand Down
11 changes: 4 additions & 7 deletions crate/cli/src/actions/elliptic_curves/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{fs::File, io::Write, path::PathBuf};

use clap::Parser;
use cosmian_kmip::kmip::kmip_operations::DecryptedData;
use cosmian_kms_client::KmsRestClient;
use cosmian_kms_utils::crypto::generic::kmip_requests::build_decryption_request;

Expand Down Expand Up @@ -64,19 +63,17 @@ impl DecryptAction {
.as_deref()
.map(|s| s.as_bytes().to_vec()),
None,
None,
);

// Query the KMS with your kmip data and get the key pair ids
let decrypt_response = kms_rest_client
.decrypt(decrypt_request)
.await
.with_context(|| "Can't execute the query on the kms server")?;

let metadata_and_cleartext: DecryptedData = decrypt_response
let plaintext = decrypt_response
.data
.context("The plain data is empty")?
.as_slice()
.try_into()?;
.context("Decrypt with elliptic curve: the plaintext is empty")?;

// Write the decrypted file
let output_file = self
Expand All @@ -86,7 +83,7 @@ impl DecryptAction {
let mut buffer =
File::create(&output_file).with_context(|| "Fail to write the plain file")?;
buffer
.write_all(&metadata_and_cleartext.plaintext)
.write_all(&plaintext)
.with_context(|| "Fail to write the plain file")?;

println!("The decrypted file is available at {output_file:?}");
Expand Down
1 change: 1 addition & 0 deletions crate/cli/src/actions/elliptic_curves/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl EncryptAction {
.as_deref()
.map(|s| s.as_bytes().to_vec()),
None,
None,
)?;

// Query the KMS with your kmip data and get the key pair ids
Expand Down
53 changes: 49 additions & 4 deletions crate/cli/src/actions/elliptic_curves/keys/create_key_pair.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
use clap::Parser;
use cosmian_kmip::kmip::kmip_types::RecommendedCurve;
use cosmian_kms_client::KmsRestClient;
use cosmian_kms_utils::crypto::elliptic_curves::kmip_requests::create_curve_25519_key_pair_request;
use cosmian_kms_utils::crypto::elliptic_curves::kmip_requests::create_ec_key_pair_request;

use crate::error::{result::CliResultHelper, CliError};

/// Create a new X25519 key pair
#[derive(clap::ValueEnum, Debug, Clone, Copy)]
pub enum Curve {
#[cfg(not(feature = "fips"))]
NistP192,
NistP224,
NistP256,
NistP384,
NistP521,
#[cfg(not(feature = "fips"))]
X25519,
#[cfg(not(feature = "fips"))]
Ed25519,
#[cfg(not(feature = "fips"))]
X448,
#[cfg(not(feature = "fips"))]
Ed448,
}

impl From<Curve> for RecommendedCurve {
fn from(curve: Curve) -> RecommendedCurve {
match curve {
#[cfg(not(feature = "fips"))]
Curve::NistP192 => RecommendedCurve::P192,
Curve::NistP224 => RecommendedCurve::P224,
Curve::NistP256 => RecommendedCurve::P256,
Curve::NistP384 => RecommendedCurve::P384,
Curve::NistP521 => RecommendedCurve::P521,
#[cfg(not(feature = "fips"))]
Curve::X25519 => RecommendedCurve::CURVE25519,
#[cfg(not(feature = "fips"))]
Curve::Ed25519 => RecommendedCurve::CURVEED25519,
#[cfg(not(feature = "fips"))]
Curve::X448 => RecommendedCurve::CURVE448,
#[cfg(not(feature = "fips"))]
Curve::Ed448 => RecommendedCurve::CURVEED448,
}
}
}

/// Create an elliptic curve key pair
///
/// - The public is used to encrypt
/// and can be safely shared.
/// - The private key is used to decrypt
/// and must be kept secret.
///
/// Run this subcommand with --help to see the list of supported curves.
/// Defaults to NIST P256
///
/// Tags can later be used to retrieve the keys. Tags are optional.
#[derive(Parser)]
#[clap(verbatim_doc_comment)]
pub struct CreateKeyPairAction {
/// The elliptic curve
#[clap(long = "curve", short = 'c', default_value = "nist-p256")]
curve: Curve,

/// The tag to associate with the master key pair.
/// To specify multiple tags, use the option multiple times.
#[clap(long = "tag", short = 't', value_name = "TAG")]
Expand All @@ -24,8 +70,7 @@ pub struct CreateKeyPairAction {

impl CreateKeyPairAction {
pub async fn run(&self, kms_rest_client: &KmsRestClient) -> Result<(), CliError> {
let create_key_pair_request =
create_curve_25519_key_pair_request(&self.tags, RecommendedCurve::CURVE25519)?;
let create_key_pair_request = create_ec_key_pair_request(&self.tags, self.curve.into())?;

// Query the KMS with your kmip data and get the key pair ids
let create_key_pair_response = kms_rest_client
Expand Down
10 changes: 9 additions & 1 deletion crate/cli/src/actions/elliptic_curves/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
#[cfg(not(feature = "fips"))]
mod decrypt;
#[cfg(not(feature = "fips"))]
mod encrypt;
mod keys;

use clap::Parser;
use cosmian_kms_client::KmsRestClient;

use self::{decrypt::DecryptAction, encrypt::EncryptAction, keys::KeysCommands};
use self::keys::KeysCommands;
#[cfg(not(feature = "fips"))]
use self::{decrypt::DecryptAction, encrypt::EncryptAction};
use crate::error::CliError;

/// Manage elliptic curve keys. Encrypt and decrypt data using ECIES.
#[derive(Parser)]
pub enum EllipticCurveCommands {
#[command(subcommand)]
Keys(KeysCommands),
#[cfg(not(feature = "fips"))]
Encrypt(EncryptAction),
#[cfg(not(feature = "fips"))]
Decrypt(DecryptAction),
}

impl EllipticCurveCommands {
pub async fn process(&self, kms_rest_client: &KmsRestClient) -> Result<(), CliError> {
match self {
Self::Keys(command) => command.process(kms_rest_client).await?,
#[cfg(not(feature = "fips"))]
Self::Encrypt(action) => action.run(kms_rest_client).await?,
#[cfg(not(feature = "fips"))]
Self::Decrypt(action) => action.run(kms_rest_client).await?,
};
Ok(())
Expand Down
1 change: 1 addition & 0 deletions crate/cli/src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod access;
pub mod certificates;
#[cfg(not(feature = "fips"))]
pub mod cover_crypt;
pub mod elliptic_curves;
pub mod login;
Expand Down
Loading