Skip to content

Commit a0698fe

Browse files
authored
fix: Reject empty certificate at DTLS 1.2 construction
1 parent e384c93 commit a0698fe

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/dtls12/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl Client {
128128
certificate: DtlsCertificate,
129129
now: Instant,
130130
) -> Result<Client, Error> {
131+
assert!(
132+
!certificate.certificate.is_empty(),
133+
"Client certificate cannot be empty"
134+
);
131135
// unwrap: malformed private_key bytes are a programmer error from the
132136
// caller who constructed DtlsCertificate; panic matches the prior
133137
// CryptoContext::new behavior which also panicked on empty/invalid

src/dtls12/server.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ enum State {
126126
impl Server {
127127
/// Create a new DTLS server
128128
pub fn new(config: Arc<Config>, certificate: crate::DtlsCertificate, now: Instant) -> Server {
129+
assert!(
130+
!certificate.certificate.is_empty(),
131+
"Server certificate cannot be empty"
132+
);
129133
// unwrap: malformed private_key bytes are a programmer error from the
130134
// caller who constructed DtlsCertificate; panic matches the prior
131135
// CryptoContext::new behavior which also panicked on empty/invalid

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,18 @@ mod test {
876876
let _ = Dtls::new_12_psk(config, Instant::now());
877877
}
878878

879+
#[test]
880+
#[should_panic(expected = "Server certificate cannot be empty")]
881+
fn new_12_panics_on_empty_certificate() {
882+
let cert = generate_self_signed_certificate().expect("Failed to generate cert");
883+
let config = Arc::new(Config::default());
884+
let empty = DtlsCertificate {
885+
certificate: vec![],
886+
private_key: cert.private_key,
887+
};
888+
let _ = Dtls::new_12(config, empty, Instant::now());
889+
}
890+
879891
#[test]
880892
fn test_auto_server_send_application_data_pending() {
881893
let mut dtls = new_instance_auto();

0 commit comments

Comments
 (0)