Skip to content

Commit 838b580

Browse files
committed
working ecdsa keys
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
1 parent 8810e5d commit 838b580

File tree

9 files changed

+329
-45
lines changed

9 files changed

+329
-45
lines changed

Cargo.lock

Lines changed: 155 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/certificate/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ anyhow = { workspace = true }
1717
asn1-rs = { workspace = true }
1818
base64 = { workspace = true }
1919
camino = { workspace = true }
20+
elliptic-curve = "0.13.8"
2021
pem.workspace = true
2122
rcgen = { workspace = true }
2223
reqwest = { workspace = true, optional = true, features = [

crates/common/certificate/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ impl KeyKind {
208208
cryptoki_config: CryptokiConfig,
209209
private_key_label: String,
210210
public_key_pem: String,
211+
sigalg: SigAlg,
211212
) -> Result<Self, CertificateError> {
212213
let public_key = pem::parse(public_key_pem).unwrap();
213214
let public_key_raw = public_key.into_contents();
214215
trace!("pubkey raw: {public_key_raw:x?}");
215216

216217
// TODO: implement other algs
217-
let algorithm = &rcgen::PKCS_RSA_SHA256;
218+
let algorithm = sigalg.into();
218219

219220
// construct a URI that uses private key we just created to sign
220221
let mut cryptoki_config = cryptoki_config;
@@ -224,7 +225,7 @@ impl KeyKind {
224225
};
225226
let private_key_uri = match uri {
226227
Some(uri) if uri.contains("object=") => {
227-
let mut uri: String = uri
228+
let uri: String = uri
228229
.strip_prefix("pkcs11:")
229230
.unwrap_or("")
230231
.split(';')
@@ -247,6 +248,23 @@ impl KeyKind {
247248
}
248249
}
249250

251+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252+
pub enum SigAlg {
253+
PkcsRsaSha256,
254+
PkcsEcdsaP256Sha256,
255+
PkcsEcdsaP384Sha384,
256+
}
257+
258+
impl From<SigAlg> for &'static rcgen::SignatureAlgorithm {
259+
fn from(value: SigAlg) -> Self {
260+
match value {
261+
SigAlg::PkcsRsaSha256 => &rcgen::PKCS_RSA_SHA256,
262+
SigAlg::PkcsEcdsaP256Sha256 => &rcgen::PKCS_ECDSA_P256_SHA256,
263+
SigAlg::PkcsEcdsaP384Sha384 => &rcgen::PKCS_ECDSA_P384_SHA384,
264+
}
265+
}
266+
}
267+
250268
/// A key pair using a remote private key.
251269
///
252270
/// To generate a CSR we need:

crates/core/tedge/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ certificate = { workspace = true }
2525
clap = { workspace = true }
2626
clap_complete = { version = "4.5.42", features = ["unstable-dynamic"] }
2727
doku = { workspace = true }
28+
elliptic-curve = { version = "0.13.8", features = [
29+
"arithmetic",
30+
"sec1",
31+
"std",
32+
] }
2833
flate2 = { workspace = true }
2934
humantime = { workspace = true }
3035
hyper = { workspace = true, default-features = false }
3136
mime_guess = { workspace = true }
3237
mqtt_channel = { workspace = true }
3338
nix = { workspace = true }
39+
p256 = "0.13.2"
40+
p384 = "0.13.1"
3441
pad = { workspace = true }
3542
pem.workspace = true
3643
rasn = { workspace = true }

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl BuildCommand for TEdgeCertCli {
220220
config,
221221
privkey_label: None,
222222
pubkey_pem: None,
223+
sigalg: None,
223224
})
224225
.unwrap_or(Key::Local(
225226
config.device_key_path(cloud.as_ref())?.to_owned(),
@@ -356,6 +357,7 @@ impl BuildCommand for TEdgeCertCli {
356357
config,
357358
privkey_label: None,
358359
pubkey_pem: None,
360+
sigalg: None,
359361
})
360362
.unwrap_or(Key::Local(
361363
config
@@ -448,6 +450,7 @@ impl BuildCommand for TEdgeCertCli {
448450
config,
449451
privkey_label: None,
450452
pubkey_pem: None,
453+
sigalg: None,
451454
})
452455
.unwrap_or(Key::Local(
453456
config.device_key_path(cloud.as_ref())?.to_owned(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub enum Key {
4444
// TODO: move it where it makes sense
4545
privkey_label: Option<String>,
4646
pubkey_pem: Option<String>,
47+
// TODO: hack to pass sigalg
48+
sigalg: Option<certificate::SigAlg>,
4749
},
4850
}
4951

@@ -76,6 +78,7 @@ impl CreateCsrCmd {
7678
config,
7779
privkey_label,
7880
pubkey_pem,
81+
sigalg,
7982
} => {
8083
let current_cert = self.current_cert.clone();
8184
match current_cert {
@@ -86,6 +89,7 @@ impl CreateCsrCmd {
8689
config.clone(),
8790
privkey_label.clone().unwrap(),
8891
pubkey_pem.as_ref().unwrap().clone(),
92+
sigalg.expect("sigalg should be set when generating a new key"),
8993
)?,
9094
}
9195
}

0 commit comments

Comments
 (0)