Skip to content

Commit 1015b8d

Browse files
authored
Merge pull request #27 from aumetra/x509-cert
Migrate to `x509-cert`
2 parents 21f4d0e + 6148818 commit 1015b8d

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

Cargo.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ license = "MIT"
99
readme = "README.md"
1010

1111
[dependencies]
12+
const-oid = { version = "0.9.6", default-features = false, features = ["db"] }
1213
ring = { version = "0.17", default-features = false }
1314
rustls = { version = "0.23", default-features = false }
1415
tokio = { version = "1", default-features = false }
1516
tokio-postgres = { version = "0.7", default-features = false }
1617
tokio-rustls = { version = "0.26", default-features = false }
17-
x509-certificate = {version = "0.23", default-features = false }
18+
x509-cert = { version = "0.2.5", default-features = false, features = ["std"] }
1819

1920
[dev-dependencies]
2021
env_logger = { version = "0.11", default-features = false }
2122
tokio = { version = "1", default-features = false, features = ["macros", "rt"] }
22-
tokio-postgres = { version = "0.7", default-features = false, features = ["runtime"] }
23-
rustls = { version = "0.23", default-features = false, features = ["std", "logging", "tls12", "ring"] }
23+
tokio-postgres = { version = "0.7", default-features = false, features = [
24+
"runtime",
25+
] }
26+
rustls = { version = "0.23", default-features = false, features = [
27+
"std",
28+
"logging",
29+
"tls12",
30+
"ring",
31+
] }

src/lib.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ use std::{
66
sync::Arc,
77
task::{Context, Poll},
88
};
9-
use DigestAlgorithm::{Sha1, Sha256, Sha384, Sha512};
109

10+
use const_oid::db::{
11+
rfc5912::{
12+
ECDSA_WITH_SHA_256, ECDSA_WITH_SHA_384, ID_SHA_1, ID_SHA_256, ID_SHA_384, ID_SHA_512,
13+
SHA_1_WITH_RSA_ENCRYPTION, SHA_256_WITH_RSA_ENCRYPTION, SHA_384_WITH_RSA_ENCRYPTION,
14+
SHA_512_WITH_RSA_ENCRYPTION,
15+
},
16+
rfc8410::ID_ED_25519,
17+
};
1118
use ring::digest;
1219
use rustls::pki_types::ServerName;
1320
use rustls::ClientConfig;
1421
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
1522
use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect};
1623
use tokio_rustls::{client::TlsStream, TlsConnector};
17-
use x509_certificate::{DigestAlgorithm, SignatureAlgorithm, X509Certificate};
18-
use SignatureAlgorithm::{
19-
EcdsaSha256, EcdsaSha384, Ed25519, NoSignature, RsaSha1, RsaSha256, RsaSha384, RsaSha512,
20-
};
24+
use x509_cert::{der::Decode, TbsCertificate};
2125

2226
#[derive(Clone)]
2327
pub struct MakeRustlsConnect {
@@ -85,20 +89,24 @@ where
8589
fn channel_binding(&self) -> ChannelBinding {
8690
let (_, session) = self.0.get_ref();
8791
match session.peer_certificates() {
88-
Some(certs) if !certs.is_empty() => X509Certificate::from_der(&certs[0])
92+
Some(certs) if !certs.is_empty() => TbsCertificate::from_der(&certs[0])
8993
.ok()
90-
.and_then(|cert| cert.signature_algorithm())
91-
.map(|algorithm| match algorithm {
92-
// Note: SHA1 is upgraded to SHA256 as per https://datatracker.ietf.org/doc/html/rfc5929#section-4.1
93-
RsaSha1 | RsaSha256 | EcdsaSha256 => &digest::SHA256,
94-
RsaSha384 | EcdsaSha384 => &digest::SHA384,
95-
RsaSha512 => &digest::SHA512,
96-
Ed25519 => &digest::SHA512,
97-
NoSignature(algo) => match algo {
98-
Sha1 | Sha256 => &digest::SHA256,
99-
Sha384 => &digest::SHA384,
100-
Sha512 => &digest::SHA512,
101-
},
94+
.and_then(|cert| {
95+
let digest = match cert.signature.oid {
96+
// Note: SHA1 is upgraded to SHA256 as per https://datatracker.ietf.org/doc/html/rfc5929#section-4.1
97+
ID_SHA_1
98+
| ID_SHA_256
99+
| SHA_1_WITH_RSA_ENCRYPTION
100+
| SHA_256_WITH_RSA_ENCRYPTION
101+
| ECDSA_WITH_SHA_256 => &digest::SHA256,
102+
ID_SHA_384 | SHA_384_WITH_RSA_ENCRYPTION | ECDSA_WITH_SHA_384 => {
103+
&digest::SHA384
104+
}
105+
ID_SHA_512 | SHA_512_WITH_RSA_ENCRYPTION | ID_ED_25519 => &digest::SHA512,
106+
_ => return None,
107+
};
108+
109+
Some(digest)
102110
})
103111
.map(|algorithm| {
104112
let hash = digest::digest(algorithm, certs[0].as_ref());

0 commit comments

Comments
 (0)