Skip to content

Commit fd41ab9

Browse files
chore: remove x509-parser (#11247)
Both crates seem well maintained. x509-cert is part of the high quality RustCrypto project that we already make heavy use of, and I think it makes sense to reduce the dependencies where possible.
1 parent 2dfff6a commit fd41ab9

File tree

8 files changed

+31
-119
lines changed

8 files changed

+31
-119
lines changed

Cargo.lock

+1-90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ urlencoding = "2.1"
215215
uuid = { version = "1.6.1", features = ["v4", "v7", "serde"] }
216216
walkdir = "2.3.2"
217217
rustls-native-certs = "0.8"
218-
x509-parser = "0.16"
219218
whoami = "1.5.1"
220219
zerocopy = { version = "0.7", features = ["derive"] }
221220
json-structural-diff = { version = "0.2.0" }
221+
x509-cert = { version = "0.2.5" }
222222

223223
## TODO replace this with tracing
224224
env_logger = "0.11"

compute_tools/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ thiserror.workspace = true
6161
url.workspace = true
6262
uuid.workspace = true
6363
walkdir.workspace = true
64-
x509-cert = { version = "0.2.5" }
64+
x509-cert.workspace = true
6565

6666
postgres_initdb.workspace = true
6767
compute_api.workspace = true

compute_tools/src/tls.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{io::Write, os::unix::fs::OpenOptionsExt, path::Path, time::Duration};
33
use anyhow::{Context, Result, bail};
44
use compute_api::responses::TlsConfig;
55
use ring::digest;
6-
use spki::ObjectIdentifier;
76
use spki::der::{Decode, PemReader};
87
use x509_cert::Certificate;
98

@@ -91,13 +90,13 @@ fn try_update_key_path_blocking(pg_data: &Path, tls_config: &TlsConfig) -> Resul
9190
}
9291

9392
fn verify_key_cert(key: &str, cert: &str) -> Result<()> {
94-
const ECDSA_WITH_SHA256: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2");
93+
use x509_cert::der::oid::db::rfc5912::ECDSA_WITH_SHA_256;
9594

9695
let cert = Certificate::decode(&mut PemReader::new(cert.as_bytes()).context("pem reader")?)
9796
.context("decode cert")?;
9897

9998
match cert.signature_algorithm.oid {
100-
ECDSA_WITH_SHA256 => {
99+
ECDSA_WITH_SHA_256 => {
101100
let key = p256::SecretKey::from_sec1_pem(key).context("parse key")?;
102101

103102
let a = key.public_key().to_sec1_bytes();

proxy/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ reqwest-middleware = { workspace = true, features = ["json"] }
7070
reqwest-retry.workspace = true
7171
reqwest-tracing.workspace = true
7272
rustc-hash.workspace = true
73-
rustls-pemfile.workspace = true
7473
rustls.workspace = true
74+
rustls-native-certs.workspace = true
75+
rustls-pemfile.workspace = true
7576
scopeguard.workspace = true
7677
serde.workspace = true
7778
serde_json.workspace = true
@@ -99,8 +100,7 @@ url.workspace = true
99100
urlencoding.workspace = true
100101
utils.workspace = true
101102
uuid.workspace = true
102-
rustls-native-certs.workspace = true
103-
x509-parser.workspace = true
103+
x509-cert.workspace = true
104104
redis.workspace = true
105105
zerocopy.workspace = true
106106

proxy/src/tls/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anyhow::Context;
66
use rustls::pki_types::CertificateDer;
77
use sha2::{Digest, Sha256};
88
use tracing::{error, info};
9-
use x509_parser::oid_registry;
9+
use x509_cert::der::{Reader, SliceReader, oid};
1010

1111
/// <https://github.com/postgres/postgres/blob/ca481d3c9ab7bf69ff0c8d71ad3951d407f6a33c/src/include/libpq/pqcomm.h#L159>
1212
pub const PG_ALPN_PROTOCOL: &[u8] = b"postgresql";
@@ -41,27 +41,27 @@ pub enum TlsServerEndPoint {
4141

4242
impl TlsServerEndPoint {
4343
pub fn new(cert: &CertificateDer<'_>) -> anyhow::Result<Self> {
44-
let sha256_oids = [
44+
const SHA256_OIDS: &[oid::ObjectIdentifier] = &[
4545
// I'm explicitly not adding MD5 or SHA1 here... They're bad.
46-
oid_registry::OID_SIG_ECDSA_WITH_SHA256,
47-
oid_registry::OID_PKCS1_SHA256WITHRSA,
46+
oid::db::rfc5912::ECDSA_WITH_SHA_256,
47+
oid::db::rfc5912::SHA_256_WITH_RSA_ENCRYPTION,
4848
];
4949

50-
let pem = x509_parser::parse_x509_certificate(cert)
51-
.context("Failed to parse PEM object from cerficiate")?
52-
.1;
50+
let certificate = SliceReader::new(cert)
51+
.context("Failed to parse cerficiate")?
52+
.decode::<x509_cert::Certificate>()
53+
.context("Failed to parse cerficiate")?;
5354

54-
info!(subject = %pem.subject, "parsing TLS certificate");
55+
let subject = certificate.tbs_certificate.subject;
56+
info!(%subject, "parsing TLS certificate");
5557

56-
let reg = oid_registry::OidRegistry::default().with_all_crypto();
57-
let oid = pem.signature_algorithm.oid();
58-
let alg = reg.get(oid);
59-
if sha256_oids.contains(oid) {
58+
let oid = certificate.signature_algorithm.oid;
59+
if SHA256_OIDS.contains(&oid) {
6060
let tls_server_end_point: [u8; 32] = Sha256::new().chain_update(cert).finalize().into();
61-
info!(subject = %pem.subject, signature_algorithm = alg.map(|a| a.description()), tls_server_end_point = %base64::encode(tls_server_end_point), "determined channel binding");
61+
info!(%subject, tls_server_end_point = %base64::encode(tls_server_end_point), "determined channel binding");
6262
Ok(Self::Sha256(tls_server_end_point))
6363
} else {
64-
error!(subject = %pem.subject, signature_algorithm = alg.map(|a| a.description()), "unknown channel binding");
64+
error!(%subject, "unknown channel binding");
6565
Ok(Self::Undefined)
6666
}
6767
}

proxy/src/tls/server_config.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::{Context, bail};
55
use itertools::Itertools;
66
use rustls::crypto::ring::{self, sign};
77
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
8+
use x509_cert::der::{Reader, SliceReader};
89

910
use super::{PG_ALPN_PROTOCOL, TlsServerEndPoint};
1011

@@ -131,11 +132,13 @@ impl CertResolver {
131132

132133
let first_cert = &cert_chain[0];
133134
let tls_server_end_point = TlsServerEndPoint::new(first_cert)?;
134-
let pem = x509_parser::parse_x509_certificate(first_cert)
135-
.context("Failed to parse PEM object from cerficiate")?
136-
.1;
137135

138-
let common_name = pem.subject().to_string();
136+
let certificate = SliceReader::new(first_cert)
137+
.context("Failed to parse cerficiate")?
138+
.decode::<x509_cert::Certificate>()
139+
.context("Failed to parse cerficiate")?;
140+
141+
let common_name = certificate.tbs_certificate.subject.to_string();
139142

140143
// We need to get the canonical name for this certificate so we can match them against any domain names
141144
// seen within the proxy codebase.

workspace_hack/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ memchr = { version = "2" }
6161
nix = { version = "0.26" }
6262
nom = { version = "7" }
6363
num = { version = "0.4" }
64-
num-bigint = { version = "0.4" }
64+
num-bigint = { version = "0.4", default-features = false, features = ["std"] }
6565
num-complex = { version = "0.4", default-features = false, features = ["std"] }
6666
num-integer = { version = "0.1", features = ["i128"] }
6767
num-iter = { version = "0.1", default-features = false, features = ["i128", "std"] }
@@ -115,7 +115,6 @@ anyhow = { version = "1", features = ["backtrace"] }
115115
bytes = { version = "1", features = ["serde"] }
116116
cc = { version = "1", default-features = false, features = ["parallel"] }
117117
chrono = { version = "0.4", default-features = false, features = ["clock", "serde", "wasmbind"] }
118-
displaydoc = { version = "0.2" }
119118
either = { version = "1" }
120119
getrandom = { version = "0.2", default-features = false, features = ["std"] }
121120
half = { version = "2", default-features = false, features = ["num-traits"] }
@@ -128,7 +127,7 @@ log = { version = "0.4", default-features = false, features = ["std"] }
128127
memchr = { version = "2" }
129128
nom = { version = "7" }
130129
num = { version = "0.4" }
131-
num-bigint = { version = "0.4" }
130+
num-bigint = { version = "0.4", default-features = false, features = ["std"] }
132131
num-complex = { version = "0.4", default-features = false, features = ["std"] }
133132
num-integer = { version = "0.1", features = ["i128"] }
134133
num-iter = { version = "0.1", default-features = false, features = ["i128", "std"] }

0 commit comments

Comments
 (0)