Skip to content

Commit 775775c

Browse files
authored
Merge pull request #201 from sbailey-arm/migrate-RSAPublicKey
Migrated uses of a locally declared RsaPublic key to new create picky-asn1-x509
2 parents a4d0a2c + 611e494 commit 775775c

File tree

12 files changed

+46
-82
lines changed

12 files changed

+46
-82
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ hex = "0.4.2"
4242
picky = "5.0.0"
4343
psa-crypto = { version = "0.2.1" , default-features = false, features = ["with-mbed-crypto"], optional = true }
4444
zeroize = { version = "1.1.0", features = ["zeroize_derive"] }
45+
picky-asn1-x509 = { version = "0.1.0", optional = true }
4546

4647
[dev-dependencies]
4748
ring = "0.16.12"
@@ -62,8 +63,8 @@ features = ["docs"]
6263
[features]
6364
default = []
6465
mbed-crypto-provider = ["psa-crypto"]
65-
pkcs11-provider = ["pkcs11", "picky-asn1-der", "picky-asn1"]
66-
tpm-provider = ["tss-esapi", "picky-asn1-der", "picky-asn1"]
66+
pkcs11-provider = ["pkcs11", "picky-asn1-der", "picky-asn1", "picky-asn1-x509"]
67+
tpm-provider = ["tss-esapi", "picky-asn1-der", "picky-asn1", "picky-asn1-x509"]
6768
all-providers = ["tpm-provider", "pkcs11-provider", "mbed-crypto-provider"]
6869
# The Mbed provider is not included in the docs because of 2 reasons:
6970
# 1) it is currently impossible for it to be built inside the docs.rs build system (as it has dependencies

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ This project uses the following third party crates:
100100
* pkcs11 (Apache-2.0)
101101
* picky-asn1-der (MIT and Apache-2.0)
102102
* picky-asn1 (MIT and Apache-2.0)
103+
* picky-asn1-x509 (MIT and Apache-2.0)
103104
* bincode (MIT)
104105
* structopt (MIT and Apache-2.0)
105106
* derivative (MIT and Apache-2.0)

e2e_tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ rand = "0.7.3"
2525
[dev-dependencies]
2626
env_logger = "0.7.1"
2727
uuid = "0.7.4"
28+
picky-asn1-x509 = "0.1.0"

e2e_tests/tests/per_provider/normal_tests/create_destroy_key.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
use e2e_tests::TestClient;
44
use parsec_client::core::interface::requests::ResponseStatus;
55
use parsec_client::core::interface::requests::Result;
6-
use picky_asn1::wrapper::IntegerAsn1;
7-
use serde::{Deserialize, Serialize};
8-
9-
// The RSA Public Key data are DER encoded with the following representation:
10-
// RSAPublicKey ::= SEQUENCE {
11-
// modulus INTEGER, -- n
12-
// publicExponent INTEGER -- e
13-
// }
14-
#[derive(Serialize, Deserialize, Debug)]
15-
struct RsaPublicKey {
16-
modulus: IntegerAsn1,
17-
public_exponent: IntegerAsn1,
18-
}
6+
use picky_asn1_x509::RSAPublicKey;
197

208
#[test]
219
fn create_and_destroy() -> Result<()> {
@@ -92,7 +80,7 @@ fn generate_public_rsa_check_modulus() -> Result<()> {
9280
client.generate_rsa_sign_key(key_name.clone())?;
9381
let public_key = client.export_public_key(key_name)?;
9482

95-
let public_key: RsaPublicKey = picky_asn1_der::from_bytes(&public_key).unwrap();
83+
let public_key: RSAPublicKey = picky_asn1_der::from_bytes(&public_key).unwrap();
9684
assert_eq!(
9785
public_key.public_exponent.as_unsigned_bytes_be(),
9886
[0x01, 0x00, 0x01]

e2e_tests/tests/per_provider/normal_tests/export_public_key.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@ use parsec_client::core::interface::operations::psa_algorithm::*;
55
use parsec_client::core::interface::operations::psa_key_attributes::*;
66
use parsec_client::core::interface::requests::ResponseStatus;
77
use parsec_client::core::interface::requests::Result;
8-
use picky_asn1::wrapper::IntegerAsn1;
9-
use serde::{Deserialize, Serialize};
10-
11-
// The RSA Public Key data are DER encoded with the following representation:
12-
// RSAPublicKey ::= SEQUENCE {
13-
// modulus INTEGER, -- n
14-
// publicExponent INTEGER -- e
15-
// }
16-
#[derive(Serialize, Deserialize, Debug)]
17-
struct RsaPublicKey {
18-
modulus: IntegerAsn1,
19-
public_exponent: IntegerAsn1,
20-
}
8+
use picky_asn1_x509::RSAPublicKey;
219

2210
#[test]
2311
fn export_public_key() -> Result<()> {
@@ -70,7 +58,7 @@ fn check_public_rsa_export_format() -> Result<()> {
7058
let public_key = client.export_public_key(key_name)?;
7159

7260
// That should not fail if the bytes are in the expected format.
73-
let _public_key: RsaPublicKey = picky_asn1_der::from_bytes(&public_key).unwrap();
61+
let _public_key: RSAPublicKey = picky_asn1_der::from_bytes(&public_key).unwrap();
7462
Ok(())
7563
}
7664

e2e_tests/tests/per_provider/normal_tests/import_key.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ use parsec_client::core::interface::operations::psa_key_attributes::*;
66
use parsec_client::core::interface::requests::ResponseStatus;
77
use parsec_client::core::interface::requests::Result;
88
use picky_asn1::wrapper::IntegerAsn1;
9-
use serde::{Deserialize, Serialize};
10-
11-
// The RSA Public Key data are DER encoded with the following representation:
12-
// RSAPublicKey ::= SEQUENCE {
13-
// modulus INTEGER, -- n
14-
// publicExponent INTEGER -- e
15-
// }
16-
#[derive(Serialize, Deserialize, Debug)]
17-
struct RsaPublicKey {
18-
modulus: IntegerAsn1,
19-
public_exponent: IntegerAsn1,
20-
}
9+
use picky_asn1_x509::RSAPublicKey;
2110

2211
const KEY_DATA: [u8; 140] = [
2312
48, 129, 137, 2, 129, 129, 0, 153, 165, 220, 135, 89, 101, 254, 229, 28, 33, 138, 247, 20, 102,
@@ -83,7 +72,7 @@ fn check_format_import1() -> Result<()> {
8372
let mut client = TestClient::new();
8473
let key_name = String::from("check_format_import");
8574

86-
let public_key = RsaPublicKey {
75+
let public_key = RSAPublicKey {
8776
modulus: IntegerAsn1::from_unsigned_bytes_be(example_modulus_1024()),
8877
public_exponent: IntegerAsn1::from_unsigned_bytes_be(vec![0x01, 0x00, 0x01]),
8978
};
@@ -100,7 +89,7 @@ fn check_format_import2() -> Result<()> {
10089
let mut client = TestClient::new();
10190
let key_name = String::from("check_format_import2");
10291

103-
let public_key = RsaPublicKey {
92+
let public_key = RSAPublicKey {
10493
modulus: IntegerAsn1::from_unsigned_bytes_be(example_modulus_1024()),
10594
public_exponent: IntegerAsn1::from_unsigned_bytes_be(vec![0x01, 0x00, 0x01]),
10695
};
@@ -146,7 +135,7 @@ fn check_format_import3() -> Result<()> {
146135
let mut client = TestClient::new();
147136
let key_name = String::from("check_format_import3");
148137

149-
let public_key = RsaPublicKey {
138+
let public_key = RSAPublicKey {
150139
modulus: IntegerAsn1::from_unsigned_bytes_be(vec![0xDE; 1024]),
151140
public_exponent: IntegerAsn1::from_unsigned_bytes_be(vec![0x01, 0x00, 0x01]),
152141
};
@@ -194,7 +183,7 @@ fn failed_imported_key_should_be_removed() -> Result<()> {
194183
let mut client = TestClient::new();
195184
let key_name = String::from("failed_imported_key_should_be_removed");
196185

197-
let public_key = RsaPublicKey {
186+
let public_key = RSAPublicKey {
198187
modulus: IntegerAsn1::from_unsigned_bytes_be(example_modulus_1024()),
199188
public_exponent: IntegerAsn1::from_unsigned_bytes_be(vec![0x01, 0x00, 0x01]),
200189
};

src/providers/pkcs11_provider/key_management.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
use super::{
4-
utils, KeyInfo, KeyPairType, LocalIdStore, Pkcs11Provider, ReadWriteSession, RsaPublicKey,
5-
Session,
6-
};
3+
use super::{utils, KeyInfo, KeyPairType, LocalIdStore, Pkcs11Provider, ReadWriteSession, Session};
74
use crate::authenticators::ApplicationName;
85
use crate::key_info_managers::KeyTriple;
96
use crate::key_info_managers::{self, ManageKeyInfo};
@@ -15,6 +12,7 @@ use parsec_interface::operations::{
1512
use parsec_interface::requests::{ProviderID, ResponseStatus, Result};
1613
use parsec_interface::secrecy::ExposeSecret;
1714
use picky_asn1::wrapper::IntegerAsn1;
15+
use picky_asn1_x509::RSAPublicKey;
1816
use pkcs11::types::{CKR_OK, CK_ATTRIBUTE, CK_MECHANISM, CK_OBJECT_HANDLE, CK_SESSION_HANDLE};
1917
use std::mem;
2018

@@ -277,7 +275,7 @@ impl Pkcs11Provider {
277275

278276
let mut template: Vec<CK_ATTRIBUTE> = Vec::new();
279277

280-
let public_key: RsaPublicKey = picky_asn1_der::from_bytes(op.data.expose_secret())
278+
let public_key: RSAPublicKey = picky_asn1_der::from_bytes(op.data.expose_secret())
281279
.or_else(|e| {
282280
format_error!("Failed to parse RsaPublicKey data", e);
283281
remove_key_id(
@@ -467,7 +465,7 @@ impl Pkcs11Provider {
467465
let modulus = IntegerAsn1::from_unsigned_bytes_be(modulus);
468466
let public_exponent = IntegerAsn1::from_unsigned_bytes_be(public_exponent);
469467

470-
let key = RsaPublicKey {
468+
let key = RSAPublicKey {
471469
modulus,
472470
public_exponent,
473471
};

src/providers/pkcs11_provider/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use pkcs11::Ctx;
2020
use std::collections::HashSet;
2121
use std::io::{Error, ErrorKind};
2222
use std::sync::{Arc, Mutex, RwLock};
23-
use utils::{KeyPairType, ReadWriteSession, RsaPublicKey, Session};
23+
use utils::{KeyPairType, ReadWriteSession, Session};
2424
use uuid::Uuid;
2525

2626
type LocalIdStore = HashSet<[u8; 4]>;

src/providers/pkcs11_provider/utils.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ use log::error;
66
use log::{info, trace, warn};
77
use parsec_interface::requests::ResponseStatus;
88
use parsec_interface::requests::Result;
9-
use picky_asn1::wrapper::IntegerAsn1;
109
use pkcs11::errors::Error;
1110
use pkcs11::types::*;
1211
use pkcs11::types::{CKF_RW_SESSION, CKF_SERIAL_SESSION, CKU_USER};
13-
use serde::{Deserialize, Serialize};
1412

1513
/// Convert the PKCS 11 library specific error values to ResponseStatus values that are returned on
1614
/// the wire protocol
@@ -60,17 +58,6 @@ pub fn rv_to_response_status(rv: CK_RV) -> ResponseStatus {
6058
}
6159
}
6260

63-
// The RSA Public Key data are DER encoded with the following representation:
64-
// RSAPublicKey ::= SEQUENCE {
65-
// modulus INTEGER, -- n
66-
// publicExponent INTEGER -- e
67-
// }
68-
#[derive(Serialize, Deserialize, Debug)]
69-
pub struct RsaPublicKey {
70-
pub modulus: IntegerAsn1,
71-
pub public_exponent: IntegerAsn1,
72-
}
73-
7461
// For PKCS 11, a key pair consists of two independant public and private keys. Both will share the
7562
// same key ID.
7663
pub enum KeyPairType {

src/providers/tpm_provider/key_management.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use super::utils;
4-
use super::utils::{PasswordContext, RsaPublicKey};
4+
use super::utils::PasswordContext;
55
use super::TpmProvider;
66
use crate::authenticators::ApplicationName;
77
use crate::key_info_managers;
@@ -14,6 +14,7 @@ use parsec_interface::operations::{
1414
};
1515
use parsec_interface::requests::{ProviderID, ResponseStatus, Result};
1616
use parsec_interface::secrecy::ExposeSecret;
17+
use picky_asn1_x509::RSAPublicKey;
1718

1819
// Public exponent value for all RSA keys.
1920
const PUBLIC_EXPONENT: [u8; 3] = [0x01, 0x00, 0x01];
@@ -130,7 +131,7 @@ impl TpmProvider {
130131
.lock()
131132
.expect("ESAPI Context lock poisoned");
132133

133-
let public_key: RsaPublicKey = picky_asn1_der::from_bytes(key_data.expose_secret())
134+
let public_key: RSAPublicKey = picky_asn1_der::from_bytes(key_data.expose_secret())
134135
.or_else(|err| {
135136
format_error!("Could not deserialise key elements", err);
136137
Err(ResponseStatus::PsaErrorInvalidArgument)

src/providers/tpm_provider/utils.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use parsec_interface::operations::psa_algorithm::*;
66
use parsec_interface::operations::psa_key_attributes::*;
77
use parsec_interface::requests::{ResponseStatus, Result};
88
use picky_asn1::wrapper::IntegerAsn1;
9+
use picky_asn1_x509::RSAPublicKey;
910
use serde::{Deserialize, Serialize};
1011
use std::convert::TryFrom;
1112
use std::convert::TryInto;
@@ -85,17 +86,6 @@ pub fn to_response_status(error: Error) -> ResponseStatus {
8586
}
8687
}
8788

88-
// The RSA Public Key data are DER encoded with the following representation:
89-
// RSAPublicKey ::= SEQUENCE {
90-
// modulus INTEGER, -- n
91-
// publicExponent INTEGER -- e
92-
// }
93-
#[derive(Serialize, Deserialize, Debug)]
94-
pub struct RsaPublicKey {
95-
pub modulus: IntegerAsn1,
96-
pub public_exponent: IntegerAsn1,
97-
}
98-
9989
// The PasswordContext is what is stored by the Key Info Manager.
10090
#[derive(Serialize, Deserialize)]
10191
pub struct PasswordContext {
@@ -171,7 +161,7 @@ fn convert_curve_to_tpm(key_attributes: Attributes) -> Result<EllipticCurve> {
171161

172162
pub fn pub_key_to_bytes(pub_key: PublicKey, key_attributes: Attributes) -> Result<Vec<u8>> {
173163
match pub_key {
174-
PublicKey::Rsa(key) => picky_asn1_der::to_vec(&RsaPublicKey {
164+
PublicKey::Rsa(key) => picky_asn1_der::to_vec(&RSAPublicKey {
175165
modulus: IntegerAsn1::from_unsigned_bytes_be(key),
176166
public_exponent: IntegerAsn1::from_signed_bytes_be(PUBLIC_EXPONENT.to_vec()),
177167
})

0 commit comments

Comments
 (0)