/// An invalid sized encryption key was encountered.
///
/// This error is most likely caused by an attempt to register an invalid
- /// secret or public key in [`ConnectionContext::set_authorized_keys`] or
- /// [`ConnectionContext::set_secret_key`]. Both should be 32 bytes.
+ /// secret or public key in [`OssuaryContext::set_authorized_keys`] or
+ /// [`OssuaryContext::set_secret_key`]. Both should be 32 bytes.
KeySize(usize, usize), // (expected, actual)
/// An error occurred when parsing or using an encryption key.
}
}
-/// Internal state of ConnectionContext state machine.
+/// Internal state of OssuaryContext state machine.
enum ConnectionState {
/// Idle server witing for a connection
ServerNew,
}
}
-/// Enum specifying the client or server role of a [`ConnectionContext`]
+/// Enum specifying the client or server role of a [`OssuaryContext`]
#[derive(Clone)]
pub enum ConnectionType {
/// This context is a client
/// This context is a server that requires authentication.
///
/// Authenticated servers only allow connections from clients with secret
- /// keys set using [`ConnectionContext::set_secret_key`], and with the
+ /// keys set using [`OssuaryContext::set_secret_key`], and with the
/// matching public key registered with the server using
- /// [`ConnectionContext::set_authorized_keys`].
+ /// [`OssuaryContext::set_authorized_keys`].
AuthenticatedServer,
/// This context is a server that does not support authentication.
/// Context for interacting with an encrypted communication channel
///
/// All interaction with ossuary's encrypted channels is performed via a
-/// ConnectionContext instance. It holds all of the state required to maintain
+/// OssuaryContext instance. It holds all of the state required to maintain
/// one side of an encrypted connection.
///
-/// A context is created with [`ConnectionContext::new`], passing it a
+/// A context is created with [`OssuaryContext::new`], passing it a
/// [`ConnectionType`] identifying whether it is to act as a client or server.
/// Server contexts can optionally require authentication, verified by providing
/// a list of public keys of permitted clients with
-/// [`ConnectionContext::set_authorized_keys`]. Clients, on the other hand,
+/// [`OssuaryContext::set_authorized_keys`]. Clients, on the other hand,
/// authenticate by setting their secret key with
-/// [`ConnectionContext::set_secret_key`].
+/// [`OssuaryContext::set_secret_key`].
///
-/// A server must create one ConnectionContext per connected client. Multiple
+/// A server must create one OssuaryContext per connected client. Multiple
/// connections cannot be multiplexed in one context.
///
-/// A ConnectionContext keeps temporary buffers for both received and soon-to-be
+/// A OssuaryContext keeps temporary buffers for both received and soon-to-be
/// transmitted data. This means they are not particularly small objects, but
/// in exchange they can read and write from/to streams set in non-blocking mode
/// without blocking single-threaded applications.
///
-pub struct ConnectionContext {
+pub struct OssuaryContext {
state: ConnectionState,
conn_type: ConnectionType,
local_key: KeyMaterial, // session key
write_buf: [u8; PACKET_BUF_SIZE],
write_buf_used: usize,
}
-impl Default for ConnectionContext {
+impl Default for OssuaryContext {
fn default() -> Self {
- ConnectionContext {
+ OssuaryContext {
state: ConnectionState::ClientNew,
conn_type: ConnectionType::Client,
local_key: Default::default(),
}
}
}
-impl ConnectionContext {
- /// Allocate a new ConnectionContext.
+impl OssuaryContext {
+ /// Allocate a new OssuaryContext.
///
/// `conn_type` is a [`ConnectionType`] indicating whether this instance
/// is for a client or server.
- pub fn new(conn_type: ConnectionType) -> ConnectionContext {
+ pub fn new(conn_type: ConnectionType) -> OssuaryContext {
//let mut rng = thread_rng();
let mut rng = OsRng::new().expect("RNG not available.");
let sec_key = EphemeralSecret::new(&mut rng);
nonce: nonce,
session: None,
};
- ConnectionContext {
+ OssuaryContext {
state: match conn_type {
ConnectionType::Client => ConnectionState::ClientNew,
_ => ConnectionState::ServerNew,
/// connection, such as when the client's key is not authorized.
///
fn reset_state(&mut self, permanent_err: Option<OssuaryError>) {
- let default = ConnectionContext::new(self.conn_type.clone());
+ let default = OssuaryContext::new(self.conn_type.clone());
*self = default;
self.state = match permanent_err {
None => {
}
/// Get the client's authentication public verification key
///
- /// When a secret key is set with [`ConnectionContext::set_secret_key`], the
+ /// When a secret key is set with [`OssuaryContext::set_secret_key`], the
/// matching public key is calculated. This function returns that public
/// key, which can be shared with a remote server for future authentication.
///
///
/// On success, returns a NetworkPacket struct containing the header and data,
/// and a `usize` indicating how many bytes were consumed from the input buffer.
-fn read_packet<T,U>(conn: &mut ConnectionContext,
+fn read_packet<T,U>(conn: &mut OssuaryContext,
mut stream: T) ->Result<(NetworkPacket, usize), OssuaryError>
where T: std::ops::DerefMut<Target = U>,
U: std::io::Read {
header_size + packet_len))
}
-/// Write a packet from ConnectionContext's internal storage to the out buffer.
+/// Write a packet from OssuaryContext's internal storage to the out buffer.
///
/// All packets are buffered to internal storage before writing, so this is
/// the function responsible for putting all packets "on the wire".
///
/// On success, returns the number of bytes written to the output buffer
-fn write_stored_packet<T,U>(conn: &mut ConnectionContext,
+fn write_stored_packet<T,U>(conn: &mut OssuaryContext,
stream: &mut T) -> Result<usize, OssuaryError>
where T: std::ops::DerefMut<Target = U>,
U: std::io::Write {
Ok(written)
}
-/// Write a packet to the ConnectionContext's internal packet buffer
+/// Write a packet to the OssuaryContext's internal packet buffer
///
/// All packets are buffered internally because there is no guarantee that a
/// complete packet can be written without blocking, and Ossuary is a non-
/// blocking library.
///
/// On success, returns the number of bytes written to the output buffer.
-fn write_packet<T,U>(conn: &mut ConnectionContext,
+fn write_packet<T,U>(conn: &mut OssuaryContext,
stream: &mut T, data: &[u8],
kind: PacketType) -> Result<usize, OssuaryError>
where T: std::ops::DerefMut<Target = U>,
#[test]
fn test_set_authorized_keys() {
- let mut conn = ConnectionContext::new(ConnectionType::AuthenticatedServer);
+ let mut conn = OssuaryContext::new(ConnectionType::AuthenticatedServer);
// Vec of slices
let keys: Vec<&[u8]> = vec![
-use ossuary::{ConnectionContext, ConnectionType};
+use ossuary::{OssuaryContext, ConnectionType};
use ossuary::OssuaryError;
use std::thread;
use std::net::{TcpListener, TcpStream};
-fn event_loop<T>(mut conn: ConnectionContext,
+fn event_loop<T>(mut conn: OssuaryContext,
mut stream: T,
is_server: bool) -> Result<(), std::io::Error>
where T: std::io::Read + std::io::Write {
fn server() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:9988").unwrap();
let stream: TcpStream = listener.incoming().next().unwrap().unwrap();
- let conn = ConnectionContext::new(ConnectionType::UnauthenticatedServer);
+ let conn = OssuaryContext::new(ConnectionType::UnauthenticatedServer);
let _ = event_loop(conn, stream, true);
Ok(())
}
fn client() -> Result<(), std::io::Error> {
let stream = TcpStream::connect("127.0.0.1:9988").unwrap();
- let conn = ConnectionContext::new(ConnectionType::Client);
+ let conn = OssuaryContext::new(ConnectionType::Client);
let _ = event_loop(conn, stream, false);
Ok(())
}
-use ossuary::{ConnectionContext, ConnectionType};
+use ossuary::{OssuaryContext, ConnectionType};
use ossuary::OssuaryError;
use std::thread;
use std::net::{TcpListener, TcpStream};
-fn event_loop<T>(mut conn: ConnectionContext,
+fn event_loop<T>(mut conn: OssuaryContext,
mut stream: T,
is_server: bool) -> Result<(), std::io::Error>
where T: std::io::Read + std::io::Write {
fn server() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:9988").unwrap();
let stream: TcpStream = listener.incoming().next().unwrap().unwrap();
- let mut conn = ConnectionContext::new(ConnectionType::AuthenticatedServer);
+ let mut conn = OssuaryContext::new(ConnectionType::AuthenticatedServer);
let keys: Vec<&[u8]> = vec![
&[0xbe, 0x1c, 0xa0, 0x74, 0xf4, 0xa5, 0x8b, 0xbb,
0xd2, 0x62, 0xa7, 0xf9, 0x52, 0x3b, 0x6f, 0xb0,
fn client() -> Result<(), std::io::Error> {
let stream = TcpStream::connect("127.0.0.1:9988").unwrap();
- let mut conn = ConnectionContext::new(ConnectionType::Client);
+ let mut conn = OssuaryContext::new(ConnectionType::Client);
let _ = conn.set_secret_key(
&[0x10, 0x86, 0x6e, 0xc4, 0x8a, 0x11, 0xf3, 0xc5,
0x6d, 0x77, 0xa6, 0x4b, 0x2f, 0x54, 0xaa, 0x06,