tightbeam-rs 0.8.0

A secure, high-performance messaging protocol library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;

#[cfg(feature = "derive")]
use crate::Errorizable;

pub type Result<T> = core::result::Result<T, HandshakeError>;

/// Errors specific to handshake operations
#[cfg_attr(feature = "derive", derive(Errorizable))]
#[derive(Debug)]
pub enum HandshakeError {
	// ---------------- Protocol & structure specific ----------------
	// Invariant violations (non-panicking)
	#[cfg_attr(
		feature = "derive",
		error("Handshake invariant violation: transcript already locked")
	)]
	TranscriptAlreadyLocked,
	#[cfg_attr(
		feature = "derive",
		error("Handshake invariant violation: transcript not locked")
	)]
	TranscriptNotLocked,
	#[cfg_attr(
		feature = "derive",
		error("Handshake invariant violation: AEAD key already derived")
	)]
	AeadAlreadyDerived,
	#[cfg_attr(
		feature = "derive",
		error("Handshake invariant violation: Finished already sent")
	)]
	FinishedAlreadySent,
	#[cfg_attr(
		feature = "derive",
		error("Handshake invariant violation: Finished before transcript lock")
	)]
	FinishedBeforeTranscriptLock,
	/// Invalid client key exchange message
	#[cfg_attr(feature = "derive", error("Invalid client key exchange message"))]
	InvalidClientKeyExchange,

	/// Invalid server key exchange message
	#[cfg_attr(feature = "derive", error("Invalid server key exchange message"))]
	InvalidServerKeyExchange,

	/// Invalid public key in handshake
	#[cfg_attr(feature = "derive", error("Invalid public key in handshake: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	InvalidPublicKey(crate::crypto::sign::ecdsa::k256::elliptic_curve::Error),

	/// Invalid certificate
	#[cfg_attr(feature = "derive", error("Invalid certificate: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	CertificateValidationError(crate::crypto::x509::error::CertificateValidationError),

	/// Signature verification failed
	#[cfg_attr(feature = "derive", error("Handshake signature verification failed"))]
	SignatureVerificationFailed,

	/// Signature error (parsing or verification)
	#[cfg_attr(feature = "derive", error("Signature error: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	SignatureError(crate::crypto::sign::Error),

	/// Key derivation failed
	#[cfg_attr(feature = "derive", error("Handshake key derivation failed: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	KeyDerivationFailed(crate::crypto::aead::Error),

	/// Underlying DER encode/decode error
	#[cfg_attr(feature = "derive", error("DER error: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	DerError(crate::der::Error),

	/// SPKI (SubjectPublicKeyInfo) error
	#[cfg_attr(feature = "derive", error("SPKI error: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	SpkiError(crate::spki::Error),

	/// Key provider error
	#[cfg_attr(feature = "derive", error("Key provider error: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	KeyError(crate::crypto::key::KeyError),

	/// CMS builder error
	#[cfg_attr(feature = "derive", error("CMS builder error: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	CmsBuilderError(crate::cms::builder::Error),

	/// Invalid handshake state
	#[cfg_attr(feature = "derive", error("Invalid handshake state"))]
	InvalidState,

	/// Missing server key
	#[cfg_attr(feature = "derive", error("Missing server key"))]
	MissingServerKey,

	/// Missing server certificate
	#[cfg_attr(feature = "derive", error("Missing server certificate"))]
	MissingServerCertificate,

	/// Missing client certificate
	#[cfg_attr(feature = "derive", error("Missing client certificate"))]
	MissingClientCertificate,

	/// Invalid transcript hash length or format
	#[cfg_attr(feature = "derive", error("Invalid transcript hash"))]
	InvalidTranscriptHash,

	/// Server requires mutual authentication but client has no identity configured
	#[cfg_attr(
		feature = "derive",
		error("Server requires mutual authentication but client has no identity")
	)]
	MutualAuthRequired,

	/// Peer identity mismatch during re-handshake (immutable identity violation)
	#[cfg_attr(
		feature = "derive",
		error("Peer identity changed during re-handshake - connection identity is immutable")
	)]
	PeerIdentityMismatch,

	/// Missing client random
	#[cfg_attr(feature = "derive", error("Missing client random from ClientHello"))]
	MissingClientRandom,

	/// Missing base session key
	#[cfg_attr(feature = "derive", error("Missing base session key"))]
	MissingBaseSessionKey,

	/// Missing client random
	#[cfg_attr(feature = "derive", error("Missing client random"))]
	MissingClientRandomState,

	/// Missing server random
	#[cfg_attr(feature = "derive", error("Missing server random"))]
	MissingServerRandom,

	/// CMS salt (transcript hash) below minimum entropy requirement
	#[cfg_attr(
		feature = "derive",
		error("CMS salt too short: {actual} bytes (minimum {minimum} required)")
	)]
	InsufficientSaltEntropy { actual: usize, minimum: usize },

	/// Peer sent abort alert during handshake
	#[cfg_attr(feature = "derive", error("Handshake aborted by peer: {0:?}"))]
	AbortReceived(crate::transport::handshake::HandshakeAlert),

	/// Handshake timeout
	#[cfg_attr(feature = "derive", error("Handshake timeout"))]
	Timeout,

	/// Invalid profile selection - server selected profile not in client's offer
	#[cfg_attr(feature = "derive", error("Server selected profile not in client's offer"))]
	InvalidProfileSelection,

	/// Negotiation error
	#[cfg_attr(feature = "derive", error("Profile negotiation failed: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	NegotiationError(crate::transport::handshake::negotiation::NegotiationError),

	/// No mutually supported profiles found during negotiation
	#[cfg_attr(feature = "derive", error("No mutually supported cryptographic profiles found"))]
	NoMutualProfiles,

	/// Dealer's choice failed - no supported profiles configured
	#[cfg_attr(
		feature = "derive",
		error("Dealer's choice failed: no supported profiles configured")
	)]
	NoSupportedProfiles,

	/// Profile negotiation required but no profiles configured
	#[cfg_attr(
		feature = "derive",
		error("Profile negotiation required but no profiles configured on server")
	)]
	NegotiationRequired,

	/// Certificate policy rejection
	#[cfg_attr(feature = "derive", error("Certificate rejected by policy: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	CertificatePolicyError(crate::crypto::policy::CryptoPolicyError),

	// ---------------- Attribute / ASN.1 profile errors ----------------
	#[cfg_attr(feature = "derive", error("Attribute must contain exactly one value"))]
	InvalidAttributeArity,
	#[cfg_attr(feature = "derive", error("Duplicate attribute present"))]
	DuplicateAttribute,
	#[cfg_attr(feature = "derive", error("Required attribute missing"))]
	MissingAttribute,
	#[cfg_attr(feature = "derive", error("Nonce value not valid OCTET STRING"))]
	InvalidNonceEncoding,
	#[cfg_attr(feature = "derive", error("Nonce length mismatch: {0}"))]
	NonceLengthError(crate::error::ReceivedExpectedError<usize, usize>),
	#[cfg_attr(feature = "derive", error("OCTET STRING length mismatch: {0}"))]
	OctetStringLengthError(crate::error::ReceivedExpectedError<usize, usize>),
	#[cfg_attr(feature = "derive", error("Version/alert value not valid INTEGER"))]
	InvalidIntegerEncoding,
	#[cfg_attr(feature = "derive", error("INTEGER out of range"))]
	IntegerOutOfRange,
	#[cfg_attr(feature = "derive", error("Unknown alert code: {0:?}"))]
	UnknownAlertCode(u8),

	// ---------------- Certificate time validation ----------------
	#[cfg_attr(feature = "derive", error("Certificate not yet valid"))]
	CertificateNotYetValid,
	#[cfg_attr(feature = "derive", error("Certificate expired"))]
	CertificateExpired,
	#[cfg_attr(feature = "derive", error("Invalid timestamp"))]
	InvalidTimestamp,

	// ---------------- ECIES / encryption path ----------------
	#[cfg(feature = "ecies")]
	#[cfg_attr(feature = "derive", error("ECIES operation failed: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	EciesError(crate::crypto::ecies::EciesError),
	#[cfg_attr(feature = "derive", error("Missing encrypted content in ECIES message"))]
	MissingEncryptedContent,
	#[cfg_attr(feature = "derive", error("Invalid decrypted payload size"))]
	InvalidDecryptedPayloadSize,
	#[cfg_attr(feature = "derive", error("client_random mismatch - possible replay attack"))]
	ClientRandomMismatchReplay,

	// ---------------- Key agreement / CMS KARI ----------------
	#[cfg_attr(feature = "derive", error("ECDH operation failed"))]
	EcdhFailed,
	#[cfg_attr(feature = "derive", error("KDF operation failed"))]
	KdfError,
	#[cfg_attr(
		feature = "derive",
		error("Invalid key size: expected {expected}, got {received}")
	)]
	InvalidKeySize { expected: usize, received: usize },
	#[cfg_attr(feature = "derive", error("ASN.1 encoding error: {0}"))]
	Asn1Error(der::Error),
	#[cfg_attr(feature = "derive", error("Invalid recipient index"))]
	InvalidRecipientIndex,
	#[cfg_attr(feature = "derive", error("Missing UKM in KeyAgreeRecipientInfo"))]
	MissingUkm,
	#[cfg_attr(feature = "derive", error("Failed to parse originator public key"))]
	InvalidOriginatorPublicKey,
	#[cfg_attr(feature = "derive", error("Unsupported originator identifier type"))]
	UnsupportedOriginatorIdentifier,
	#[cfg_attr(feature = "derive", error("KARI builder already consumed"))]
	KariBuilderConsumed,
	#[cfg_attr(feature = "derive", error("Content encryption algorithm not set"))]
	MissingContentEncryptionAlgorithm,
	#[cfg_attr(
		feature = "derive",
		error("Key wrap algorithm not configured in security profile")
	)]
	MissingKeyWrapAlgorithm,
	#[cfg_attr(
		feature = "derive",
		error("Negotiated key wrap algorithm unsupported (expected AES-128/192/256 key wrap)")
	)]
	UnsupportedKeyWrapAlgorithm,
	#[cfg(all(feature = "builder", feature = "aead"))]
	#[cfg_attr(feature = "derive", error("AES key wrap operation failed: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	AesKeyWrap(crate::crypto::aead::aes_kw::Error),

	#[cfg(feature = "kem")]
	#[cfg_attr(
		feature = "derive",
		error("Hybrid key agreement integrity check failed: combined ECDH+KEM key validation error")
	)]
	HybridKariIntegrityCheckFailed,

	// ---------------- Random generation ----------------
	#[cfg_attr(feature = "derive", error("Random generation failed"))]
	RandomGenerationFailed,

	/// Secret material was unavailable
	#[cfg_attr(feature = "derive", error("Secret unavailable: {0}"))]
	#[cfg_attr(feature = "derive", from)]
	SecretUnavailable(crate::crypto::secret::SecretError),

	// ---------------- Generic octet string length (server_random/client_random etc.) ----------------
	#[cfg_attr(feature = "derive", error("Invalid OCTET STRING length: {0}"))]
	InvalidOctetStringLength(&'static str),
}

#[cfg(not(feature = "derive"))]
impl core::fmt::Display for HandshakeError {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			HandshakeError::InvalidClientKeyExchange => write!(f, "Invalid client key exchange message"),
			HandshakeError::InvalidServerKeyExchange => write!(f, "Invalid server key exchange message"),
			HandshakeError::InvalidPublicKey(e) => write!(f, "Invalid public key in handshake: {}", e),
			HandshakeError::CertificateValidationError(e) => write!(f, "Invalid certificate: {}", e),
			HandshakeError::SpkiError(e) => write!(f, "SPKI error: {}", e),
			HandshakeError::KeyError(e) => write!(f, "Key provider error: {}", e),
			HandshakeError::CmsBuilderError(e) => write!(f, "CMS builder error: {}", e),
			HandshakeError::SignatureVerificationFailed => write!(f, "Handshake signature verification failed"),
			HandshakeError::KeyDerivationFailed(e) => write!(f, "Handshake key derivation failed: {}", e),
			HandshakeError::InvalidState => write!(f, "Invalid handshake state"),
			HandshakeError::MissingServerKey => write!(f, "Missing server key"),
			HandshakeError::MissingServerCertificate => write!(f, "Missing server certificate"),
			HandshakeError::MissingClientCertificate => write!(f, "Missing client certificate"),
			HandshakeError::InvalidTranscriptHash => write!(f, "Invalid transcript hash"),
			HandshakeError::MissingClientRandom => write!(f, "Missing client random from ClientHello"),
			HandshakeError::MissingBaseSessionKey => write!(f, "Missing base session key"),
			HandshakeError::MissingClientRandomState => write!(f, "Missing client random"),
			HandshakeError::MissingServerRandom => write!(f, "Missing server random"),
			HandshakeError::InsufficientSaltEntropy { actual, minimum } => {
				write!(f, "CMS salt too short: {} bytes (minimum {} required)", actual, minimum)
			}
			HandshakeError::AbortReceived(alert) => write!(f, "Handshake aborted by peer: {:?}", alert),
			HandshakeError::Timeout => write!(f, "Handshake timeout"),
			HandshakeError::InvalidProfileSelection => write!(f, "Server selected profile not in client's offer"),
			HandshakeError::NegotiationError(e) => write!(f, "Profile negotiation failed: {}", e),
			HandshakeError::NoMutualProfiles => write!(f, "No mutually supported cryptographic profiles found"),
			HandshakeError::NoSupportedProfiles => {
				write!(f, "Dealer's choice failed: no supported profiles configured")
			}
			HandshakeError::NegotiationRequired => {
				write!(f, "Profile negotiation required but no profiles configured on server")
			}
			HandshakeError::CertificatePolicyError(e) => write!(f, "Certificate rejected by policy: {}", e),
			HandshakeError::DerError(e) => write!(f, "DER error: {}", e),
			HandshakeError::InvalidAttributeArity => write!(f, "Attribute must contain exactly one value"),
			HandshakeError::DuplicateAttribute => write!(f, "Duplicate attribute present"),
			HandshakeError::MissingAttribute => write!(f, "Required attribute missing"),
			HandshakeError::InvalidNonceEncoding => write!(f, "Nonce value not valid OCTET STRING"),
			HandshakeError::NonceLengthError(e) => write!(f, "Nonce length mismatch: {}", e),
			HandshakeError::OctetStringLengthError(e) => write!(f, "OCTET STRING length mismatch: {}", e),
			HandshakeError::InvalidIntegerEncoding => write!(f, "Version/alert value not valid INTEGER"),
			HandshakeError::IntegerOutOfRange => write!(f, "INTEGER out of range"),
			HandshakeError::UnknownAlertCode(code) => write!(f, "Unknown alert code: {code}"),
			HandshakeError::CertificateNotYetValid => write!(f, "Certificate not yet valid"),
			HandshakeError::CertificateExpired => write!(f, "Certificate expired"),
			HandshakeError::InvalidTimestamp => write!(f, "Invalid timestamp"),
			HandshakeError::EciesError(e) => write!(f, "ECIES operation failed: {}", e),
			HandshakeError::MissingEncryptedContent => write!(f, "Missing encrypted content in ECIES message"),
			HandshakeError::InvalidDecryptedPayloadSize => write!(f, "Invalid decrypted payload size"),
			HandshakeError::ClientRandomMismatchReplay => write!(f, "client_random mismatch - possible replay attack"),
			HandshakeError::EcdhFailed => write!(f, "ECDH operation failed"),
			HandshakeError::KdfError => write!(f, "KDF operation failed"),
			HandshakeError::InvalidKeySize { expected, received } => {
				write!(f, "Invalid key size: expected {}, got {}", expected, received)
			}
			HandshakeError::Asn1Error(e) => write!(f, "ASN.1 encoding error: {}", e),
			HandshakeError::InvalidRecipientIndex => write!(f, "Invalid recipient index"),
			HandshakeError::MissingUkm => write!(f, "Missing UKM in KeyAgreeRecipientInfo"),
			HandshakeError::InvalidOriginatorPublicKey => write!(f, "Failed to parse originator public key"),
			HandshakeError::UnsupportedOriginatorIdentifier => write!(f, "Unsupported originator identifier type"),
			HandshakeError::KariBuilderConsumed => write!(f, "KARI builder already consumed"),
			HandshakeError::MissingContentEncryptionAlgorithm => write!(f, "Content encryption algorithm not set"),
			HandshakeError::MissingKeyWrapAlgorithm => {
				write!(f, "Key wrap algorithm not configured in security profile")
			}
			HandshakeError::UnsupportedKeyWrapAlgorithm => {
				write!(
					f,
					"Negotiated key wrap algorithm unsupported (expected AES-128/192/256 key wrap)"
				)
			}
			#[cfg(all(feature = "builder", feature = "aead"))]
			HandshakeError::AesKeyWrap(e) => write!(f, "AES key wrap operation failed: {}", e),
			HandshakeError::RandomGenerationFailed => write!(f, "Random generation failed"),
			HandshakeError::SecretUnavailable(e) => write!(f, "Secret unavailable: {}", e),
			HandshakeError::InvalidOctetStringLength(m) => write!(f, "Invalid OCTET STRING length: {}", m),
			HandshakeError::NonceLengthError(e) => write!(f, "Nonce length mismatch: {}", e),
			HandshakeError::OctetStringLengthError(e) => write!(f, "OCTET STRING length mismatch: {}", e),
			HandshakeError::UnknownAlertCode(code) => write!(f, "Unknown alert code: {code}"),
		}
	}
}

#[cfg(not(feature = "derive"))]
impl core::error::Error for HandshakeError {}

impl From<crate::crypto::kdf::KdfError> for HandshakeError {
	fn from(_: crate::crypto::kdf::KdfError) -> Self {
		HandshakeError::KeyDerivationFailed(crate::crypto::aead::Error)
	}
}

/// Narrows [`TightBeamError`](crate::error::TightBeamError) into [`HandshakeError`];
/// variants without a handshake counterpart collapse to [`HandshakeError::InvalidState`].
impl From<crate::error::TightBeamError> for HandshakeError {
	fn from(err: crate::error::TightBeamError) -> Self {
		use crate::error::TightBeamError;
		match err {
			TightBeamError::HandshakeError(h) => h,
			TightBeamError::SerializationError(e) => HandshakeError::DerError(e),
			#[cfg(feature = "x509")]
			TightBeamError::SpkiError(e) => HandshakeError::SpkiError(e),
			#[cfg(feature = "x509")]
			TightBeamError::CertificateValidationError(e) => HandshakeError::CertificateValidationError(e),
			#[cfg(feature = "crypto")]
			TightBeamError::CryptoPolicyError(e) => HandshakeError::CertificatePolicyError(e),
			#[cfg(feature = "crypto")]
			TightBeamError::KeyError(e) => HandshakeError::KeyError(e),
			#[cfg(feature = "signature")]
			TightBeamError::SignatureError(e) => HandshakeError::SignatureError(e),
			#[cfg(feature = "ecies")]
			TightBeamError::EciesError(e) => HandshakeError::EciesError(e),
			#[cfg(feature = "crypto")]
			TightBeamError::SecretUnavailable(e) => HandshakeError::SecretUnavailable(e),
			#[cfg(feature = "random")]
			TightBeamError::OsRngError(_) => HandshakeError::RandomGenerationFailed,
			_ => HandshakeError::InvalidState,
		}
	}
}

#[cfg(all(feature = "crypto", not(feature = "derive")))]
impl From<crate::crypto::secret::SecretError> for HandshakeError {
	fn from(err: crate::crypto::secret::SecretError) -> Self {
		HandshakeError::SecretUnavailable(err)
	}
}

/// Narrows [`HandshakeError`] into the foreign [`crate::cms::builder::Error`];
/// variants without a counterpart collapse into
/// [`Builder`](crate::cms::builder::Error::Builder) via their `Display`.
#[cfg(all(feature = "builder", feature = "aead"))]
impl From<HandshakeError> for crate::cms::builder::Error {
	fn from(err: HandshakeError) -> Self {
		match err {
			HandshakeError::CmsBuilderError(e) => e,
			HandshakeError::DerError(e) => crate::cms::builder::Error::Asn1(e),
			HandshakeError::Asn1Error(e) => crate::cms::builder::Error::Asn1(e),
			HandshakeError::SpkiError(e) => crate::cms::builder::Error::PublicKey(e),
			other => crate::cms::builder::Error::Builder(other.to_string()),
		}
	}
}

impl From<crypto_common::InvalidLength> for HandshakeError {
	fn from(_: crypto_common::InvalidLength) -> Self {
		HandshakeError::KeyDerivationFailed(crate::crypto::aead::Error)
	}
}

#[cfg(not(feature = "derive"))]
impl From<crate::crypto::key::KeyError> for HandshakeError {
	fn from(e: crate::crypto::key::KeyError) -> Self {
		HandshakeError::KeyError(e)
	}
}