rustls/
check.rs

1use crate::enums::{ContentType, HandshakeType};
2use crate::error::Error;
3#[cfg(feature = "logging")]
4use crate::log::warn;
5use crate::msgs::message::MessagePayload;
6
7/// For a Message $m, and a HandshakePayload enum member $payload_type,
8/// return Ok(payload) if $m is both a handshake message and one that
9/// has the given $payload_type.  If not, return Err(rustls::Error) quoting
10/// $handshake_type as the expected handshake type.
11macro_rules! require_handshake_msg(
12  ( $m:expr, $handshake_type:path, $payload_type:path ) => (
13    match &$m.payload {
14        MessagePayload::Handshake { parsed: $crate::msgs::handshake::HandshakeMessagePayload {
15            payload: $payload_type(hm),
16            ..
17        }, .. } => Ok(hm),
18        payload => Err($crate::check::inappropriate_handshake_message(
19            payload,
20            &[$crate::ContentType::Handshake],
21            &[$handshake_type]))
22    }
23  )
24);
25
26/// Like require_handshake_msg, but moves the payload out of $m.
27#[cfg(feature = "tls12")]
28macro_rules! require_handshake_msg_move(
29  ( $m:expr, $handshake_type:path, $payload_type:path ) => (
30    match $m.payload {
31        MessagePayload::Handshake { parsed: $crate::msgs::handshake::HandshakeMessagePayload {
32            payload: $payload_type(hm),
33            ..
34        }, .. } => Ok(hm),
35        payload =>
36            Err($crate::check::inappropriate_handshake_message(
37                &payload,
38                &[$crate::ContentType::Handshake],
39                &[$handshake_type]))
40    }
41  )
42);
43
44pub(crate) fn inappropriate_message(
45    payload: &MessagePayload,
46    content_types: &[ContentType],
47) -> Error {
48    warn!(
49        "Received a {:?} message while expecting {:?}",
50        payload.content_type(),
51        content_types
52    );
53    Error::InappropriateMessage {
54        expect_types: content_types.to_vec(),
55        got_type: payload.content_type(),
56    }
57}
58
59pub(crate) fn inappropriate_handshake_message(
60    payload: &MessagePayload,
61    content_types: &[ContentType],
62    handshake_types: &[HandshakeType],
63) -> Error {
64    match payload {
65        MessagePayload::Handshake { parsed, .. } => {
66            warn!(
67                "Received a {:?} handshake message while expecting {:?}",
68                parsed.typ, handshake_types
69            );
70            Error::InappropriateHandshakeMessage {
71                expect_types: handshake_types.to_vec(),
72                got_type: parsed.typ,
73            }
74        }
75        payload => inappropriate_message(payload, content_types),
76    }
77}