Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions librustls/src/acceptor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ptr::NonNull;

use libc::{EINVAL, EIO, c_void, size_t};
use rustls::server::{Accepted, AcceptedAlert, Acceptor};

Expand Down Expand Up @@ -123,9 +125,9 @@ impl rustls_acceptor {
) -> rustls_io_result {
ffi_panic_boundary! {
let acceptor = try_mut_from_ptr!(acceptor);
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_io_result(EINVAL);
}
};
let callback = try_callback!(callback);

let mut reader = CallbackReader { callback, userdata };
Expand All @@ -135,7 +137,7 @@ impl rustls_acceptor {
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
unsafe {
*out_n = n_read;
out_n.write(n_read);
}

rustls_io_result(0)
Expand Down Expand Up @@ -489,9 +491,9 @@ impl rustls_accepted_alert {
out_n: *mut size_t,
) -> rustls_io_result {
ffi_panic_boundary! {
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_io_result(EINVAL);
}
};
let accepted_alert = try_mut_from_ptr!(accepted_alert);
let mut writer = CallbackWriter {
callback: try_callback!(callback),
Expand All @@ -502,7 +504,7 @@ impl rustls_accepted_alert {
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
unsafe {
*out_n = n_written;
out_n.write(n_written);
}
rustls_io_result(0)
}
Expand Down
33 changes: 17 additions & 16 deletions librustls/src/certificate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ffi::CStr;
use std::marker::PhantomData;
use std::ptr::null;
use std::ptr::{NonNull, null};
use std::slice;

use libc::{c_char, size_t};
Expand Down Expand Up @@ -42,13 +42,15 @@ pub extern "C" fn rustls_certificate_get_der(
) -> rustls_result {
ffi_panic_boundary! {
let cert = try_ref_from_ptr!(cert);
if out_der_data.is_null() || out_der_len.is_null() {
let (Some(out_der_data), Some(out_der_len)) =
(NonNull::new(out_der_data), NonNull::new(out_der_len))
else {
return rustls_result::NullParameter;
}
};
let der = cert.as_ref();
unsafe {
*out_der_data = der.as_ptr();
*out_der_len = der.len();
out_der_data.write(der.as_ptr());
out_der_len.write(der.len());
}
rustls_result::Ok
}
Expand Down Expand Up @@ -235,12 +237,13 @@ impl rustls_certified_key {
};
let certified_key = try_ref_from_ptr!(certified_key);
let mut new_key = certified_key.clone();
if !ocsp_response.is_null() {
let ocsp_slice = unsafe { &*ocsp_response };
new_key.ocsp = Some(Vec::from(try_slice!(ocsp_slice.data, ocsp_slice.len)));
} else {
new_key.ocsp = None;
}
new_key.ocsp = match NonNull::new(ocsp_response.cast_mut()) {
Some(ocsp_response) => {
let ocsp_slice = unsafe { ocsp_response.as_ref() };
Some(Vec::from(try_slice!(ocsp_slice.data, ocsp_slice.len)))
}
None => None,
};
Comment thread
djc marked this conversation as resolved.
*cloned_key_out = to_arc_const_ptr(new_key);
rustls_result::Ok
}
Expand Down Expand Up @@ -383,12 +386,10 @@ impl rustls_root_cert_store_builder {
Some(b) => b,
};

let filename = unsafe {
if filename.is_null() {
return rustls_result::NullParameter;
}
CStr::from_ptr(filename)
let Some(filename) = NonNull::new(filename.cast_mut()) else {
return rustls_result::NullParameter;
};
let filename = unsafe { CStr::from_ptr(filename.as_ptr()) };

let filename = filename.to_bytes();
let filename = match std::str::from_utf8(filename) {
Expand Down
9 changes: 4 additions & 5 deletions librustls/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::CStr;
use std::fmt::{Debug, Formatter};
use std::ptr::NonNull;
use std::slice;
use std::sync::Arc;

Expand Down Expand Up @@ -889,12 +890,10 @@ impl rustls_client_config {
alpn_protocols: Vec<Vec<u8>>,
conn_out: *mut *mut rustls_connection,
) -> rustls_result {
let server_name = unsafe {
if server_name.is_null() {
return rustls_result::NullParameter;
}
CStr::from_ptr(server_name)
let Some(server_name) = NonNull::new(server_name.cast_mut()) else {
return rustls_result::NullParameter;
};
let server_name = unsafe { CStr::from_ptr(server_name.as_ptr()) };
let Ok(server_name) = server_name.to_str() else {
return rustls_result::InvalidDnsNameError;
};
Expand Down
61 changes: 32 additions & 29 deletions librustls/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::{ErrorKind, Read, Write};
use std::{ffi::c_void, ptr::null};
use std::{ffi::c_void, ptr::NonNull, ptr::null};
use std::{ptr::null_mut, slice};

use libc::{EINVAL, EIO, size_t};
Expand Down Expand Up @@ -146,9 +146,9 @@ impl rustls_connection {
) -> rustls_io_result {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_io_result(EINVAL);
}
};
let callback = try_callback!(callback);

let mut reader = CallbackReader { callback, userdata };
Expand All @@ -157,7 +157,7 @@ impl rustls_connection {
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
unsafe {
*out_n = n_read;
out_n.write(n_read);
}

rustls_io_result(0)
Expand All @@ -183,9 +183,9 @@ impl rustls_connection {
) -> rustls_io_result {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_io_result(EINVAL);
}
};
let callback = try_callback!(callback);

let mut writer = CallbackWriter { callback, userdata };
Expand All @@ -194,7 +194,7 @@ impl rustls_connection {
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
unsafe {
*out_n = n_written;
out_n.write(n_written);
}

rustls_io_result(0)
Expand All @@ -220,9 +220,9 @@ impl rustls_connection {
) -> rustls_io_result {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_io_result(EINVAL);
}
};
let callback = try_callback!(callback);

let mut writer = VectoredCallbackWriter { callback, userdata };
Expand All @@ -231,7 +231,7 @@ impl rustls_connection {
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
unsafe {
*out_n = n_written;
out_n.write(n_written);
}

rustls_io_result(0)
Expand Down Expand Up @@ -391,17 +391,19 @@ impl rustls_connection {
) {
ffi_panic_boundary! {
let conn = try_ref_from_ptr!(conn);
if protocol_out.is_null() || protocol_out_len.is_null() {
let (Some(protocol_out), Some(protocol_out_len)) =
(NonNull::new(protocol_out), NonNull::new(protocol_out_len))
else {
return;
}
};
match conn.alpn_protocol() {
Some(p) => unsafe {
*protocol_out = p.as_ptr();
*protocol_out_len = p.len();
protocol_out.write(p.as_ptr());
protocol_out_len.write(p.len());
},
None => unsafe {
*protocol_out = null();
*protocol_out_len = 0;
protocol_out.write(null());
protocol_out_len.write(0);
},
}
}
Expand Down Expand Up @@ -527,15 +529,15 @@ impl rustls_connection {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
let write_buf = try_slice!(buf, count);
if out_n.is_null() {
let Some(out_n) = NonNull::new(out_n) else {
return rustls_result::NullParameter;
}
};
let n_written = match conn.writer().write(write_buf) {
Ok(n) => n,
Err(_) => return rustls_result::Io,
};
unsafe {
*out_n = n_written;
out_n.write(n_written);
}
rustls_result::Ok
}
Expand All @@ -562,16 +564,16 @@ impl rustls_connection {
) -> rustls_result {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
if buf.is_null() {
let Some(buf) = NonNull::new(buf) else {
return rustls_result::NullParameter;
}
if out_n.is_null() {
};
let Some(out_n) = NonNull::new(out_n) else {
return rustls_result::NullParameter;
}
};

// Safety: the memory pointed at by buf must be initialized
// (required by documentation of this function).
let read_buf = try_slice_mut!(buf, count);
let read_buf = try_slice_mut!(buf.as_ptr(), count);

let n_read = match conn.reader().read(read_buf) {
Ok(n) => n,
Expand All @@ -584,7 +586,7 @@ impl rustls_connection {
Err(_) => return rustls_result::Io,
};
unsafe {
*out_n = n_read;
out_n.write(n_read);
}
rustls_result::Ok
}
Expand Down Expand Up @@ -613,10 +615,11 @@ impl rustls_connection {
) -> rustls_result {
ffi_panic_boundary! {
let conn = try_mut_from_ptr!(conn);
if buf.is_null() || out_n.is_null() {
let (Some(buf), Some(out_n)) = (NonNull::new(buf), NonNull::new(out_n)) else {
return rustls_result::NullParameter;
}
let mut read_buf: std::io::BorrowedBuf<'_, u8> = try_slice_mut!(buf, count).into();
};
let mut read_buf: std::io::BorrowedBuf<'_, u8> =
try_slice_mut!(buf.as_ptr(), count).into();

let n_read = match conn.reader().read_buf(read_buf.unfilled()) {
Ok(()) => read_buf.filled().len(),
Expand All @@ -629,7 +632,7 @@ impl rustls_connection {
Err(_) => return rustls_result::Io,
};
unsafe {
*out_n = n_read;
out_n.write(n_read);
}
rustls_result::Ok
}
Expand Down
16 changes: 9 additions & 7 deletions librustls/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::min;
use std::fmt::Display;
use std::ptr::NonNull;
use std::sync::Arc;

use libc::{c_char, c_uint, size_t};
Expand Down Expand Up @@ -224,17 +225,18 @@ impl rustls_result {
out_n: *mut size_t,
) {
ffi_panic_boundary! {
if buf.is_null() {
let (Some(buf), Some(out_n)) = (NonNull::new(buf), NonNull::new(out_n)) else {
return;
}
if out_n.is_null() {
return;
}
};
let error_str = rustls_result::from(result).to_string();
let out_len = min(len, error_str.len());
unsafe {
std::ptr::copy_nonoverlapping(error_str.as_ptr() as *mut c_char, buf, out_len);
*out_n = out_len;
std::ptr::copy_nonoverlapping(
error_str.as_ptr() as *mut c_char,
buf.as_ptr(),
out_len,
);
out_n.write(out_len);
}
}
}
Expand Down
Loading
Loading