Skip to content

Commit

Permalink
tls: expose peer certificate
Browse files Browse the repository at this point in the history
Fixes #326.
  • Loading branch information
ghedo committed Jan 30, 2020
1 parent 1250cd9 commit 34c0b77
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,11 @@ impl Connection {
self.handshake.alpn_protocol()
}

/// Returns the peer's leaf certificate (if any) as a DER-encoded buffer.
pub fn peer_cert(&self) -> Option<Vec<u8>> {
self.handshake.peer_cert()
}

/// Returns true if the connection handshake is complete.
pub fn is_established(&self) -> bool {
self.handshake_completed
Expand Down Expand Up @@ -4942,6 +4947,21 @@ mod tests {
Err(Error::CongestionControl)
);
}

#[test]
fn peer_cert() {
let mut buf = [0; 65535];

let mut pipe = testing::Pipe::default().unwrap();

assert_eq!(pipe.handshake(&mut buf), Ok(()));

match pipe.client.peer_cert() {
Some(c) => assert_eq!(c.len(), 919),

None => panic!("missing server certificate"),
}
}
}

pub use crate::packet::Header;
Expand Down
33 changes: 32 additions & 1 deletion src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ struct X509_STORE(c_void);

#[allow(non_camel_case_types)]
#[repr(transparent)]
#[cfg(windows)]
struct X509(c_void);

#[repr(C)]
Expand Down Expand Up @@ -446,6 +445,31 @@ impl Handshake {
Some(sigalg.to_string())
}

pub fn peer_cert(&self) -> Option<Vec<u8>> {
let peer_cert = unsafe {
let mut out: *mut libc::c_uchar = ptr::null_mut();

let x509 = SSL_get_peer_certificate(self.as_ptr());
if x509.is_null() {
return None;
}

let out_len = i2d_X509(x509, &mut out);
if out_len <= 0 {
return None;
}

let der = slice::from_raw_parts(out, out_len as usize);
let der = der.to_vec();

OPENSSL_free(out as *mut c_void);

der
};

Some(peer_cert)
}

pub fn is_resumed(&self) -> bool {
unsafe { SSL_session_reused(self.as_ptr()) == 1 }
}
Expand Down Expand Up @@ -866,6 +890,8 @@ extern {
sigalg: u16, include_curve: i32,
) -> *const c_char;

fn SSL_get_peer_certificate(ssl: *mut SSL) -> *const X509;

fn SSL_set_min_proto_version(ssl: *mut SSL, version: u16);
fn SSL_set_max_proto_version(ssl: *mut SSL, version: u16);

Expand Down Expand Up @@ -923,8 +949,13 @@ extern {
#[cfg(windows)]
fn d2i_X509(px: *mut X509, input: *const *const u8, len: c_int) -> *mut X509;

fn i2d_X509(px: *const X509, out: *mut *mut u8) -> c_int;

// ERR
fn ERR_peek_error() -> c_uint;

fn ERR_error_string_n(err: c_uint, buf: *const u8, len: usize);

// OPENSSL
fn OPENSSL_free(ptr: *mut c_void);
}

0 comments on commit 34c0b77

Please sign in to comment.