rustls/
check.rs

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