2023-05-22 23:04:26 -07:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
2020-06-05 09:35:14 -07:00
|
|
|
//
|
2023-05-22 23:04:26 -07:00
|
|
|
// This source code is dual-licensed under either the MIT license found in the
|
|
|
|
|
// LICENSE-MIT file in the root directory of this source tree or the Apache
|
2021-12-03 14:38:11 -08:00
|
|
|
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
|
2023-05-22 23:04:26 -07:00
|
|
|
// of this source tree. You may select, at your option, one of the above-listed
|
|
|
|
|
// licenses.
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
//! A list of error types which are produced during an execution of the protocol
|
2021-08-12 06:25:07 +02:00
|
|
|
use core::convert::Infallible;
|
2025-04-15 22:31:37 +02:00
|
|
|
use core::error::Error;
|
2021-08-12 06:25:07 +02:00
|
|
|
use core::fmt::Debug;
|
2021-07-20 11:49:37 +02:00
|
|
|
|
2020-07-06 15:40:35 -04:00
|
|
|
use displaydoc::Display;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
/// Represents an error in the manipulation of internal cryptographic data
|
2022-01-04 00:50:40 +01:00
|
|
|
#[derive(Clone, Copy, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2025-04-22 09:08:17 +02:00
|
|
|
pub enum InternalError {
|
2022-02-25 07:13:22 +01:00
|
|
|
/// Size of input is empty or longer then [`u16::MAX`].
|
|
|
|
|
HashToScalar,
|
2020-07-06 15:40:35 -04:00
|
|
|
/// Computing HKDF failed while deriving subkeys
|
2020-06-05 09:35:14 -07:00
|
|
|
HkdfError,
|
2020-07-06 15:40:35 -04:00
|
|
|
/// Computing HMAC failed while supplying a secret key
|
2020-06-05 09:35:14 -07:00
|
|
|
HmacError,
|
2022-04-02 01:10:00 +02:00
|
|
|
/// Computing the key stretching function failed
|
|
|
|
|
KsfError,
|
2021-07-31 00:36:57 +02:00
|
|
|
/** This error occurs when the envelope seal open hmac check fails
|
|
|
|
|
HMAC check in seal open failed. */
|
2020-07-02 12:24:53 -07:00
|
|
|
SealOpenHmacError,
|
2021-07-31 00:36:57 +02:00
|
|
|
/** This error occurs when attempting to open an envelope of the wrong
|
|
|
|
|
type (base mode, custom identifier) */
|
2021-01-04 14:27:20 -08:00
|
|
|
IncompatibleEnvelopeModeError,
|
2021-10-25 02:54:32 -07:00
|
|
|
/// Error from the OPRF evaluation
|
2022-01-06 00:10:57 +01:00
|
|
|
OprfError(voprf::Error),
|
2022-02-25 07:13:22 +01:00
|
|
|
/// Error from the OPRF evaluation
|
|
|
|
|
OprfInternalError(voprf::InternalError),
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-22 09:08:17 +02:00
|
|
|
impl Debug for InternalError {
|
2021-08-12 06:25:07 +02:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2021-07-20 11:49:37 +02:00
|
|
|
match self {
|
2022-02-25 07:13:22 +01:00
|
|
|
Self::HashToScalar => f.debug_tuple("HashToScalar").finish(),
|
2021-07-20 11:49:37 +02:00
|
|
|
Self::HkdfError => f.debug_tuple("HkdfError").finish(),
|
|
|
|
|
Self::HmacError => f.debug_tuple("HmacError").finish(),
|
2022-04-02 01:10:00 +02:00
|
|
|
Self::KsfError => f.debug_tuple("KsfError").finish(),
|
2021-07-20 11:49:37 +02:00
|
|
|
Self::SealOpenHmacError => f.debug_tuple("SealOpenHmacError").finish(),
|
|
|
|
|
Self::IncompatibleEnvelopeModeError => {
|
|
|
|
|
f.debug_tuple("IncompatibleEnvelopeModeError").finish()
|
|
|
|
|
}
|
2021-10-25 02:54:32 -07:00
|
|
|
Self::OprfError(error) => f.debug_tuple("OprfError").field(error).finish(),
|
2022-02-25 07:13:22 +01:00
|
|
|
Self::OprfInternalError(error) => {
|
|
|
|
|
f.debug_tuple("OprfInternalError").field(error).finish()
|
|
|
|
|
}
|
2021-07-20 11:49:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 09:08:17 +02:00
|
|
|
impl Error for InternalError {}
|
2021-07-20 11:49:37 +02:00
|
|
|
|
2022-01-06 00:10:57 +01:00
|
|
|
impl From<voprf::Error> for InternalError {
|
|
|
|
|
fn from(voprf_error: voprf::Error) -> Self {
|
2021-10-25 02:54:32 -07:00
|
|
|
Self::OprfError(voprf_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 00:10:57 +01:00
|
|
|
impl From<voprf::Error> for ProtocolError {
|
|
|
|
|
fn from(voprf_error: voprf::Error) -> Self {
|
2021-10-25 02:54:32 -07:00
|
|
|
Self::LibraryError(InternalError::OprfError(voprf_error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
impl From<voprf::InternalError> for ProtocolError {
|
|
|
|
|
fn from(voprf_error: voprf::InternalError) -> Self {
|
|
|
|
|
Self::LibraryError(InternalError::OprfInternalError(voprf_error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 12:28:19 -07:00
|
|
|
/// Represents an error in protocol handling
|
2022-01-04 00:50:40 +01:00
|
|
|
#[derive(Clone, Copy, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-08-22 12:28:19 -07:00
|
|
|
pub enum ProtocolError<T = Infallible> {
|
|
|
|
|
/// Internal error encountered
|
2025-04-22 09:08:17 +02:00
|
|
|
LibraryError(InternalError),
|
2020-07-06 15:40:35 -04:00
|
|
|
/// Error in validating credentials
|
2020-06-05 09:35:14 -07:00
|
|
|
InvalidLoginError,
|
2020-09-10 17:01:06 -04:00
|
|
|
/// Error with serializing / deserializing protocol messages
|
|
|
|
|
SerializationError,
|
2025-04-22 09:08:17 +02:00
|
|
|
/// Invalid length for `name`: expected `len`, but is actually `actual_len`.
|
|
|
|
|
SizeError {
|
|
|
|
|
/// name
|
|
|
|
|
name: &'static str,
|
|
|
|
|
/// length
|
|
|
|
|
len: usize,
|
|
|
|
|
/// actual
|
|
|
|
|
actual_len: usize,
|
|
|
|
|
},
|
2021-08-22 12:28:19 -07:00
|
|
|
/** This error occurs when the client detects that the server has
|
|
|
|
|
reflected the OPRF value (beta == alpha) */
|
|
|
|
|
ReflectedValueError,
|
2022-01-06 06:19:02 +01:00
|
|
|
/** Identity group element was encountered during deserialization, which is
|
|
|
|
|
invalid */
|
2021-06-15 17:31:12 -07:00
|
|
|
IdentityGroupElementError,
|
2025-04-22 09:08:17 +02:00
|
|
|
/// Custom [`SecretKey`](crate::keypair::PrivateKeySerialization) error type
|
|
|
|
|
Custom(T),
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 12:28:19 -07:00
|
|
|
impl<T: Debug> Debug for ProtocolError<T> {
|
2021-08-12 06:25:07 +02:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2021-07-20 11:49:37 +02:00
|
|
|
match self {
|
2021-08-22 12:28:19 -07:00
|
|
|
Self::LibraryError(pake_error) => {
|
|
|
|
|
f.debug_tuple("LibraryError").field(pake_error).finish()
|
2021-07-20 11:49:37 +02:00
|
|
|
}
|
|
|
|
|
Self::InvalidLoginError => f.debug_tuple("InvalidLoginError").finish(),
|
|
|
|
|
Self::SerializationError => f.debug_tuple("SerializationError").finish(),
|
2025-04-22 09:08:17 +02:00
|
|
|
Self::SizeError {
|
|
|
|
|
name,
|
|
|
|
|
len,
|
|
|
|
|
actual_len,
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("SizeError")
|
|
|
|
|
.field("name", name)
|
|
|
|
|
.field("len", len)
|
|
|
|
|
.field("actual_len", actual_len)
|
|
|
|
|
.finish(),
|
2021-07-20 11:49:37 +02:00
|
|
|
Self::ReflectedValueError => f.debug_tuple("ReflectedValueError").finish(),
|
2021-08-22 12:28:19 -07:00
|
|
|
Self::IdentityGroupElementError => f.debug_tuple("IdentityGroupElementError").finish(),
|
2025-04-22 09:08:17 +02:00
|
|
|
Self::Custom(custom) => f.debug_tuple("Custom").field(custom).finish(),
|
2021-07-20 11:49:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Error> Error for ProtocolError<T> {}
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
// This is meant to express future(ly) non-trivial ways of converting the
|
|
|
|
|
// internal error into a ProtocolError
|
2025-04-22 09:08:17 +02:00
|
|
|
impl<T> From<InternalError> for ProtocolError<T> {
|
|
|
|
|
fn from(e: InternalError) -> ProtocolError<T> {
|
2021-08-22 12:28:19 -07:00
|
|
|
Self::LibraryError(e)
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:19:02 +01:00
|
|
|
// See https://github.com/rust-lang/rust/issues/64715 and remove this when merged,
|
|
|
|
|
// and https://github.com/dtolnay/thiserror/issues/62 for why this comes up in our
|
|
|
|
|
// doc tests.
|
2021-08-12 06:25:07 +02:00
|
|
|
impl<T> From<::core::convert::Infallible> for ProtocolError<T> {
|
|
|
|
|
fn from(_: ::core::convert::Infallible) -> Self {
|
2020-06-05 09:35:14 -07:00
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 11:49:37 +02:00
|
|
|
impl ProtocolError {
|
2022-12-16 23:14:53 +01:00
|
|
|
/// Convert `ProtocolError<Infallible>` into `ProtocolError<T>`
|
2021-07-20 11:49:37 +02:00
|
|
|
pub fn into_custom<T>(self) -> ProtocolError<T> {
|
|
|
|
|
match self {
|
2025-04-22 09:08:17 +02:00
|
|
|
Self::LibraryError(internal_error) => ProtocolError::LibraryError(internal_error),
|
2021-08-22 12:28:19 -07:00
|
|
|
Self::InvalidLoginError => ProtocolError::InvalidLoginError,
|
|
|
|
|
Self::SerializationError => ProtocolError::SerializationError,
|
2025-04-22 09:08:17 +02:00
|
|
|
Self::SizeError {
|
|
|
|
|
name,
|
|
|
|
|
len,
|
|
|
|
|
actual_len,
|
|
|
|
|
} => ProtocolError::SizeError {
|
|
|
|
|
name,
|
|
|
|
|
len,
|
|
|
|
|
actual_len,
|
|
|
|
|
},
|
2021-07-20 11:49:37 +02:00
|
|
|
Self::ReflectedValueError => ProtocolError::ReflectedValueError,
|
2021-08-22 12:28:19 -07:00
|
|
|
Self::IdentityGroupElementError => ProtocolError::IdentityGroupElementError,
|
2021-07-20 11:49:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
pub(crate) mod utils {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2025-04-22 09:08:17 +02:00
|
|
|
pub fn check_slice_size<'a>(
|
2020-06-05 09:35:14 -07:00
|
|
|
slice: &'a [u8],
|
|
|
|
|
expected_len: usize,
|
|
|
|
|
arg_name: &'static str,
|
2025-04-22 09:08:17 +02:00
|
|
|
) -> Result<&'a [u8], ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
if slice.len() != expected_len {
|
2025-04-22 09:08:17 +02:00
|
|
|
return Err(ProtocolError::SizeError {
|
2020-06-05 09:35:14 -07:00
|
|
|
name: arg_name,
|
|
|
|
|
len: expected_len,
|
|
|
|
|
actual_len: slice.len(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Ok(slice)
|
|
|
|
|
}
|
2020-10-09 10:51:58 -07:00
|
|
|
|
|
|
|
|
pub fn check_slice_size_atleast<'a>(
|
|
|
|
|
slice: &'a [u8],
|
|
|
|
|
expected_len: usize,
|
|
|
|
|
arg_name: &'static str,
|
2025-04-22 09:08:17 +02:00
|
|
|
) -> Result<&'a [u8], ProtocolError> {
|
2020-10-09 10:51:58 -07:00
|
|
|
if slice.len() < expected_len {
|
2025-04-22 09:08:17 +02:00
|
|
|
return Err(ProtocolError::SizeError {
|
2020-10-09 10:51:58 -07:00
|
|
|
name: arg_name,
|
|
|
|
|
len: expected_len,
|
|
|
|
|
actual_len: slice.len(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Ok(slice)
|
|
|
|
|
}
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|