Skip to content

Commit 637f884

Browse files
committed
show possible rsa bits and ec curve flag values in cli
Signed-off-by: Marcel Guzik <[email protected]>
1 parent 718e1e4 commit 637f884

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

crates/core/tedge/src/cli/certificate/cli.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::certificate_is_self_signed;
77
use crate::cli::certificate::c8y;
88
use crate::cli::certificate::create_csr::Key;
99
use crate::cli::certificate::create_key::CreateKeyCmd;
10+
use crate::cli::certificate::create_key::EcCurve;
1011
use crate::cli::certificate::create_key::KeyType;
12+
use crate::cli::certificate::create_key::RsaBits;
1113
use crate::cli::common::Cloud;
1214
use crate::cli::common::CloudArg;
1315
use crate::command::BuildCommand;
@@ -65,11 +67,11 @@ pub enum TEdgeCertCli {
6567
#[arg(long)]
6668
r#type: KeyType,
6769

68-
#[arg(long, default_value = "2048")]
69-
bits: u16,
70+
#[arg(long, default_value = "2048", group = "key_params")]
71+
bits: RsaBits,
7072

71-
#[arg(long, default_value = "256")]
72-
curve: u16,
73+
#[arg(long, default_value = "p256", group = "key_params")]
74+
curve: EcCurve,
7375

7476
/// The device identifier to be used as the common name for the certificate
7577
#[clap(long = "device-id", global = true)]

crates/core/tedge/src/cli/certificate/create_key.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::log::MaybeFancy;
1111

1212
pub struct CreateKeyCmd {
1313
pub cryptoki_config: CryptokiConfig,
14-
pub bits: u16,
15-
pub curve: u16,
14+
pub bits: RsaBits,
15+
pub curve: EcCurve,
1616
pub label: String,
1717
pub r#type: KeyType,
1818
/// The device identifier to be used as the common name for the certificate
@@ -27,6 +27,41 @@ pub enum KeyType {
2727
Ec,
2828
}
2929

30+
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
31+
pub enum RsaBits {
32+
#[value(name = "2048")]
33+
Bits2048,
34+
#[value(name = "3072")]
35+
Bits3072,
36+
#[value(name = "4096")]
37+
Bits4096,
38+
}
39+
40+
impl From<RsaBits> for u16 {
41+
fn from(value: RsaBits) -> Self {
42+
match value {
43+
RsaBits::Bits2048 => 2048,
44+
RsaBits::Bits3072 => 3072,
45+
RsaBits::Bits4096 => 4096,
46+
}
47+
}
48+
}
49+
50+
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
51+
pub enum EcCurve {
52+
P256,
53+
P384,
54+
}
55+
56+
impl From<EcCurve> for u16 {
57+
fn from(value: EcCurve) -> Self {
58+
match value {
59+
EcCurve::P256 => 256,
60+
EcCurve::P384 => 384,
61+
}
62+
}
63+
}
64+
3065
#[async_trait::async_trait]
3166
impl Command for CreateKeyCmd {
3267
fn description(&self) -> String {
@@ -35,8 +70,12 @@ impl Command for CreateKeyCmd {
3570

3671
async fn execute(&self, _config: TEdgeConfig) -> Result<(), MaybeFancy<anyhow::Error>> {
3772
let key = match self.r#type {
38-
KeyType::Rsa => KeyTypeParams::Rsa { bits: self.bits },
39-
KeyType::Ec => KeyTypeParams::Ec { curve: self.curve },
73+
KeyType::Rsa => KeyTypeParams::Rsa {
74+
bits: self.bits.into(),
75+
},
76+
KeyType::Ec => KeyTypeParams::Ec {
77+
curve: self.curve.into(),
78+
},
4079
};
4180
let params = CreateKeyParams {
4281
key,
@@ -54,13 +93,8 @@ impl Command for CreateKeyCmd {
5493
// use returned public key to create a CSR
5594
let sigalg = match (self.r#type, self.curve) {
5695
(KeyType::Rsa, _) => certificate::SignatureAlgorithm::RsaPkcs1Sha256,
57-
(KeyType::Ec, 256) => certificate::SignatureAlgorithm::EcdsaP256Sha256,
58-
(KeyType::Ec, 384) => certificate::SignatureAlgorithm::EcdsaP384Sha384,
59-
_ => {
60-
return Err(
61-
anyhow::anyhow!("invalid arguments: bad keytype/arg combination").into(),
62-
)
63-
}
96+
(KeyType::Ec, EcCurve::P256) => certificate::SignatureAlgorithm::EcdsaP256Sha256,
97+
(KeyType::Ec, EcCurve::P384) => certificate::SignatureAlgorithm::EcdsaP384Sha384,
6498
};
6599

66100
let key = super::create_csr::Key::Cryptoki {

tests/RobotFramework/tests/pkcs11/private_key_storage.robot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ Can create a private key on the PKCS11 token and download new cert from c8y
146146
... p11tool_keytype=RSA-4096
147147

148148
Create private key and download cert from c8y
149-
... label=ec-256
149+
... label=ec-p256
150150
... type=ec
151-
... curve=256
151+
... curve=p256
152152
... p11tool_keytype=EC/ECDSA-SECP256R1
153153
Create private key and download cert from c8y
154-
... label=ec-384
154+
... label=ec-p384
155155
... type=ec
156-
... curve=384
156+
... curve=p384
157157
... p11tool_keytype=EC/ECDSA-SECP384R1
158158
# ECDSA P521 not supported by rcgen
159159

0 commit comments

Comments
 (0)