Skip to content

Commit c638b40

Browse files
committed
working ecdsa keys
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
1 parent 525651b commit c638b40

File tree

9 files changed

+322
-43
lines changed

9 files changed

+322
-43
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
@@ -209,13 +209,14 @@ impl KeyKind {
209209
cryptoki_config: CryptokiConfig,
210210
private_key_label: String,
211211
public_key_pem: String,
212+
sigalg: SigAlg,
212213
) -> Result<Self, CertificateError> {
213214
let public_key = pem::parse(public_key_pem).unwrap();
214215
let public_key_raw = public_key.into_contents();
215216
trace!("pubkey raw: {public_key_raw:x?}");
216217

217218
// TODO: implement other algs
218-
let algorithm = &rcgen::PKCS_RSA_SHA256;
219+
let algorithm = sigalg.into();
219220

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

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

crates/core/tedge/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ 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 = ["arithmetic", "sec1", "std"] }
2829
flate2 = { workspace = true }
2930
humantime = { workspace = true }
3031
hyper = { workspace = true, default-features = false }
3132
mime_guess = { workspace = true }
3233
mqtt_channel = { workspace = true }
3334
nix = { workspace = true }
35+
p256 = "0.13.2"
36+
p384 = "0.13.1"
3437
pad = { workspace = true }
3538
pem.workspace = true
3639
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)