Skip to content

Commit fe21d76

Browse files
committed
client: unify Provision and Run TLS configurations
1 parent 5cd5af1 commit fe21d76

File tree

3 files changed

+164
-117
lines changed

3 files changed

+164
-117
lines changed

common/src/client/certs.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
//! that clients and nodes can authenticate each other and build a secure
1414
//! channel via mTLS (mutual-auth TLS).
1515
16+
use asn1_rs::FromDer;
1617
use rcgen::{
1718
date_time_ymd, BasicConstraints, CertificateParams, DnType, IsCa,
1819
RcgenError, SanType,
1920
};
21+
use x509_parser::x509::X509Name;
2022

2123
use crate::{constants, ed25519};
2224

@@ -41,10 +43,30 @@ pub struct NodeCert(rcgen::Certificate);
4143

4244
// -- impl CaCert -- //
4345

46+
/// Parse CommonName from x509 DistinguishedName DER bytes
47+
fn is_matching_issuer_der(der: &[u8], expected: &str) -> Option<()> {
48+
let (_rest, name) = X509Name::from_der(der).ok()?;
49+
let common_name = name.iter_common_name().next()?;
50+
let common_name = common_name.as_str().ok()?;
51+
if common_name == expected {
52+
Some(())
53+
} else {
54+
None
55+
}
56+
}
57+
4458
impl CaCert {
59+
const COMMON_NAME: &'static str = "lexe node-client CA";
60+
61+
/// Returns `true` if the given DER bytes are an x509 DistinguishedName with
62+
/// a CommonName matching the standard node-client CA CommonName.
63+
pub fn is_matching_issuer_der(issuer_der: &[u8]) -> bool {
64+
is_matching_issuer_der(issuer_der, Self::COMMON_NAME).is_some()
65+
}
66+
4567
pub fn from_key_pair(key_pair: rcgen::KeyPair) -> Result<Self, RcgenError> {
4668
let mut name = constants::lexe_distinguished_name_prefix();
47-
name.push(DnType::CommonName, "client CA cert");
69+
name.push(DnType::CommonName, Self::COMMON_NAME);
4870

4971
let mut params = CertificateParams::default();
5072
params.alg = &rcgen::PKCS_ED25519;
@@ -74,7 +96,7 @@ impl CaCert {
7496
impl ClientCert {
7597
pub fn from_key_pair(key_pair: rcgen::KeyPair) -> Result<Self, RcgenError> {
7698
let mut name = constants::lexe_distinguished_name_prefix();
77-
name.push(DnType::CommonName, "client cert");
99+
name.push(DnType::CommonName, "lexe client cert");
78100

79101
let mut params = CertificateParams::default();
80102
params.alg = &rcgen::PKCS_ED25519;
@@ -116,7 +138,7 @@ impl NodeCert {
116138
dns_names: Vec<String>,
117139
) -> Result<Self, RcgenError> {
118140
let mut name = constants::lexe_distinguished_name_prefix();
119-
name.push(DnType::CommonName, "node cert");
141+
name.push(DnType::CommonName, "lexe node cert");
120142

121143
let subject_alt_names = dns_names
122144
.into_iter()

common/src/client/provision.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
// TODO
22

3-
use anyhow::{Context, Result};
3+
use anyhow::Context;
44
use reqwest::{Client, Proxy};
55

66
use crate::api::provision::ProvisionRequest;
77
use crate::api::UserPk;
8-
use crate::attest::EnclavePolicy;
8+
use crate::attest;
99
use crate::client::tls;
10+
use crate::rng::Crng;
11+
use crate::root_seed::RootSeed;
1012

1113
pub struct ProvisionClient {
1214
client: Client,
1315
provision_url: String,
1416
}
1517

1618
impl ProvisionClient {
17-
pub fn new(
19+
pub fn new<R: Crng>(
20+
rng: &mut R,
21+
seed: &RootSeed,
1822
user_pk: &UserPk,
1923
proxy_url: &str,
2024
proxy_ca: &rustls::Certificate,
2125
provision_url: String,
22-
expect_dummy_quote: bool,
23-
enclave_policy: EnclavePolicy,
24-
) -> Result<Self> {
26+
attest_verifier: attest::ServerCertVerifier,
27+
) -> anyhow::Result<Self> {
2528
// TODO(phlip9): actual auth in proxy header
2629
// TODO(phlip9): https only mode
2730

@@ -30,15 +33,11 @@ impl ProvisionClient {
3033
// TODO(phlip9): should be bearer auth
3134
.basic_auth(&user_pk.to_string(), "");
3235

33-
let tls = tls::client_provision_tls_config(
34-
proxy_ca,
35-
expect_dummy_quote,
36-
enclave_policy,
37-
)?;
36+
let tls = tls::client_tls_config(rng, proxy_ca, seed, attest_verifier)?;
3837

3938
let client = Client::builder()
4039
.proxy(proxy)
41-
.user_agent("lexe-provision-client")
40+
.user_agent("lexe-client")
4241
.use_preconfigured_tls(tls)
4342
.build()
4443
.context("Failed to build client")?;
@@ -49,7 +48,7 @@ impl ProvisionClient {
4948
})
5049
}
5150

52-
pub async fn provision(&self, req: ProvisionRequest) -> Result<()> {
51+
pub async fn provision(&self, req: ProvisionRequest) -> anyhow::Result<()> {
5352
let provision_url = &self.provision_url;
5453
let url = format!("{provision_url}/provision");
5554

0 commit comments

Comments
 (0)