| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{ |
| 8 |
fmt::{self, Display, Formatter}, |
| 9 |
os::raw::c_char, |
| 10 |
str::Utf8Error, |
| 11 |
}; |
| 12 |
|
| 13 |
use crate::ssl::{SECStatus, SECSuccess}; |
| 14 |
|
| 15 |
include!(concat!(env!("OUT_DIR"), "/nspr_error.rs")); |
| 16 |
#[expect(non_snake_case, dead_code, reason = "Code is bindgen-generated.")] |
| 17 |
mod codes { |
| 18 |
include!(concat!(env!("OUT_DIR"), "/nss_secerr.rs")); |
| 19 |
include!(concat!(env!("OUT_DIR"), "/nss_sslerr.rs")); |
| 20 |
} |
| 21 |
pub use codes::{SECErrorCodes as sec, SSLErrorCodes as ssl}; |
| 22 |
#[expect(dead_code, reason = "Code is bindgen-generated.")] |
| 23 |
pub mod nspr { |
| 24 |
include!(concat!(env!("OUT_DIR"), "/nspr_err.rs")); |
| 25 |
} |
| 26 |
|
| 27 |
#[expect(dead_code, reason = "Some constants are not used.")] |
| 28 |
pub mod mozpkix { |
| 29 |
// These are manually extracted from the many bindings generated |
| 30 |
// by bindgen when provided with the simple header: |
| 31 |
// #include "mozpkix/pkixnss.h" |
| 32 |
|
| 33 |
#[expect(non_camel_case_types, reason = "Code is bindgen-generated.")] |
| 34 |
pub type mozilla_pkix_ErrorCode = ::std::os::raw::c_int; |
| 35 |
pub const MOZILLA_PKIX_ERROR_KEY_PINNING_FAILURE: mozilla_pkix_ErrorCode = -16384; |
| 36 |
pub const MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY: mozilla_pkix_ErrorCode = -16383; |
| 37 |
pub const MOZILLA_PKIX_ERROR_INADEQUATE_KEY_SIZE: mozilla_pkix_ErrorCode = -16382; |
| 38 |
pub const MOZILLA_PKIX_ERROR_V1_CERT_USED_AS_CA: mozilla_pkix_ErrorCode = -16381; |
| 39 |
pub const MOZILLA_PKIX_ERROR_NO_RFC822NAME_MATCH: mozilla_pkix_ErrorCode = -16380; |
| 40 |
pub const MOZILLA_PKIX_ERROR_NOT_YET_VALID_CERTIFICATE: mozilla_pkix_ErrorCode = -16379; |
| 41 |
pub const MOZILLA_PKIX_ERROR_NOT_YET_VALID_ISSUER_CERTIFICATE: mozilla_pkix_ErrorCode = -16378; |
| 42 |
pub const MOZILLA_PKIX_ERROR_SIGNATURE_ALGORITHM_MISMATCH: mozilla_pkix_ErrorCode = -16377; |
| 43 |
pub const MOZILLA_PKIX_ERROR_OCSP_RESPONSE_FOR_CERT_MISSING: mozilla_pkix_ErrorCode = -16376; |
| 44 |
pub const MOZILLA_PKIX_ERROR_VALIDITY_TOO_LONG: mozilla_pkix_ErrorCode = -16375; |
| 45 |
pub const MOZILLA_PKIX_ERROR_REQUIRED_TLS_FEATURE_MISSING: mozilla_pkix_ErrorCode = -16374; |
| 46 |
pub const MOZILLA_PKIX_ERROR_INVALID_INTEGER_ENCODING: mozilla_pkix_ErrorCode = -16373; |
| 47 |
pub const MOZILLA_PKIX_ERROR_EMPTY_ISSUER_NAME: mozilla_pkix_ErrorCode = -16372; |
| 48 |
pub const MOZILLA_PKIX_ERROR_ADDITIONAL_POLICY_CONSTRAINT_FAILED: mozilla_pkix_ErrorCode = |
| 49 |
-16371; |
| 50 |
pub const MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT: mozilla_pkix_ErrorCode = -16370; |
| 51 |
pub const MOZILLA_PKIX_ERROR_MITM_DETECTED: mozilla_pkix_ErrorCode = -16369; |
| 52 |
pub const END_OF_LIST: mozilla_pkix_ErrorCode = -16368; |
| 53 |
} |
| 54 |
|
| 55 |
pub type Res<T> = Result<T, Error>; |
| 56 |
|
| 57 |
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)] |
| 58 |
pub enum Error { |
| 59 |
AeadError, |
| 60 |
CertificateLoading, |
| 61 |
CipherInitFailure, |
| 62 |
CreateSslSocket, |
| 63 |
EchRetry(Vec<u8>), |
| 64 |
HkdfError, |
| 65 |
InternalError, |
| 66 |
IntegerOverflow, |
| 67 |
InvalidEpoch, |
| 68 |
MixedHandshakeMethod, |
| 69 |
NoDataAvailable, |
| 70 |
NssError { |
| 71 |
name: String, |
| 72 |
code: PRErrorCode, |
| 73 |
desc: String, |
| 74 |
}, |
| 75 |
OverrunError, |
| 76 |
SelfEncryptFailure, |
| 77 |
StringError, |
| 78 |
TimeTravelError, |
| 79 |
UnsupportedCipher, |
| 80 |
UnsupportedVersion, |
| 81 |
} |
| 82 |
|
| 83 |
impl Error { |
| 84 |
pub(crate) fn last_nss_error() -> Self { |
| 85 |
Self::from(unsafe { PR_GetError() }) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl std::error::Error for Error { |
| 90 |
fn cause(&self) -> Option<&dyn std::error::Error> { |
| 91 |
None |
| 92 |
} |
| 93 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 94 |
None |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl Display for Error { |
| 99 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 100 |
write!(f, "Error: {self:?}") |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl From<std::num::TryFromIntError> for Error { |
| 105 |
fn from(_: std::num::TryFromIntError) -> Self { |
| 106 |
Self::IntegerOverflow |
| 107 |
} |
| 108 |
} |
| 109 |
impl From<std::ffi::NulError> for Error { |
| 110 |
fn from(_: std::ffi::NulError) -> Self { |
| 111 |
Self::InternalError |
| 112 |
} |
| 113 |
} |
| 114 |
impl From<Utf8Error> for Error { |
| 115 |
fn from(_: Utf8Error) -> Self { |
| 116 |
Self::StringError |
| 117 |
} |
| 118 |
} |
| 119 |
impl From<PRErrorCode> for Error { |
| 120 |
fn from(code: PRErrorCode) -> Self { |
| 121 |
let name = wrap_str_fn(|| unsafe { PR_ErrorToName(code) }, "UNKNOWN_ERROR"); |
| 122 |
let desc = wrap_str_fn( |
| 123 |
|| unsafe { PR_ErrorToString(code, PR_LANGUAGE_I_DEFAULT) }, |
| 124 |
"...", |
| 125 |
); |
| 126 |
Self::NssError { name, code, desc } |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
use std::ffi::CStr; |
| 131 |
|
| 132 |
fn wrap_str_fn<F>(f: F, dflt: &str) -> String |
| 133 |
where |
| 134 |
F: FnOnce() -> *const c_char, |
| 135 |
{ |
| 136 |
unsafe { |
| 137 |
let p = f(); |
| 138 |
if p.is_null() { |
| 139 |
return dflt.to_string(); |
| 140 |
} |
| 141 |
CStr::from_ptr(p).to_string_lossy().into_owned() |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub fn secstatus_to_res(rv: SECStatus) -> Res<()> { |
| 146 |
if rv == SECSuccess { |
| 147 |
Ok(()) |
| 148 |
} else { |
| 149 |
Err(Error::last_nss_error()) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
pub const fn is_blocked(result: &Res<()>) -> bool { |
| 154 |
match result { |
| 155 |
Err(Error::NssError { code, .. }) => *code == nspr::PR_WOULD_BLOCK_ERROR, |
| 156 |
_ => false, |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[cfg(test)] |
| 161 |
mod tests { |
| 162 |
use test_fixture::fixture_init; |
| 163 |
|
| 164 |
use crate::{ |
| 165 |
err::{self, is_blocked, secstatus_to_res, Error, PRErrorCode, PR_SetError}, |
| 166 |
ssl::{SECFailure, SECSuccess}, |
| 167 |
}; |
| 168 |
|
| 169 |
fn set_error_code(code: PRErrorCode) { |
| 170 |
// This code doesn't work without initializing NSS first. |
| 171 |
fixture_init(); |
| 172 |
unsafe { |
| 173 |
PR_SetError(code, 0); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn error_code() { |
| 179 |
fixture_init(); |
| 180 |
assert_eq!(15 - 0x3000, err::ssl::SSL_ERROR_BAD_MAC_READ); |
| 181 |
assert_eq!(166 - 0x2000, err::sec::SEC_ERROR_LIBPKIX_INTERNAL); |
| 182 |
assert_eq!(-5998, err::nspr::PR_WOULD_BLOCK_ERROR); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn is_ok() { |
| 187 |
assert!(secstatus_to_res(SECSuccess).is_ok()); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn is_err() { |
| 192 |
set_error_code(err::ssl::SSL_ERROR_BAD_MAC_READ); |
| 193 |
let r = secstatus_to_res(SECFailure); |
| 194 |
assert!(r.is_err()); |
| 195 |
match r.unwrap_err() { |
| 196 |
Error::NssError { name, code, desc } => { |
| 197 |
assert_eq!(name, "SSL_ERROR_BAD_MAC_READ"); |
| 198 |
assert_eq!(code, -12273); |
| 199 |
assert_eq!( |
| 200 |
desc, |
| 201 |
"SSL received a record with an incorrect Message Authentication Code." |
| 202 |
); |
| 203 |
} |
| 204 |
_ => unreachable!(), |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn is_err_zero_code() { |
| 210 |
set_error_code(0); |
| 211 |
let r = secstatus_to_res(SECFailure); |
| 212 |
assert!(r.is_err()); |
| 213 |
match r.unwrap_err() { |
| 214 |
Error::NssError { name, code, .. } => { |
| 215 |
assert_eq!(name, "UNKNOWN_ERROR"); |
| 216 |
assert_eq!(code, 0); |
| 217 |
// Note that we don't test |desc| here because that comes from |
| 218 |
// strerror(0), which is platform-dependent. |
| 219 |
} |
| 220 |
_ => unreachable!(), |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn blocked() { |
| 226 |
set_error_code(err::nspr::PR_WOULD_BLOCK_ERROR); |
| 227 |
let r = secstatus_to_res(SECFailure); |
| 228 |
assert!(r.is_err()); |
| 229 |
assert!(is_blocked(&r)); |
| 230 |
match r.unwrap_err() { |
| 231 |
Error::NssError { name, code, desc } => { |
| 232 |
assert_eq!(name, "PR_WOULD_BLOCK_ERROR"); |
| 233 |
assert_eq!(code, -5998); |
| 234 |
assert_eq!(desc, "The operation would have blocked"); |
| 235 |
} |
| 236 |
_ => panic!("bad error type"), |
| 237 |
} |
| 238 |
} |
| 239 |
} |