1 | use crate::enums::{ContentType, HandshakeType}; |
2 | use crate::error::Error; |
3 | #[cfg (feature = "logging" )] |
4 | use crate::log::warn; |
5 | use 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. |
11 | macro_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 | macro_rules! require_handshake_msg_move( |
28 | ( $m:expr, $handshake_type:path, $payload_type:path ) => ( |
29 | match $m.payload { |
30 | MessagePayload::Handshake { parsed: $crate::msgs::handshake::HandshakeMessagePayload { |
31 | payload: $payload_type(hm), |
32 | .. |
33 | }, .. } => Ok(hm), |
34 | payload => |
35 | Err($crate::check::inappropriate_handshake_message( |
36 | &payload, |
37 | &[$crate::ContentType::Handshake], |
38 | &[$handshake_type])) |
39 | } |
40 | ) |
41 | ); |
42 | |
43 | pub(crate) fn inappropriate_message( |
44 | payload: &MessagePayload, |
45 | content_types: &[ContentType], |
46 | ) -> Error { |
47 | warn!( |
48 | "Received a {:?} message while expecting {:?}" , |
49 | payload.content_type(), |
50 | content_types |
51 | ); |
52 | Error::InappropriateMessage { |
53 | expect_types: content_types.to_vec(), |
54 | got_type: payload.content_type(), |
55 | } |
56 | } |
57 | |
58 | pub(crate) fn inappropriate_handshake_message( |
59 | payload: &MessagePayload, |
60 | content_types: &[ContentType], |
61 | handshake_types: &[HandshakeType], |
62 | ) -> Error { |
63 | match payload { |
64 | MessagePayload::Handshake { parsed: &HandshakeMessagePayload, .. } => { |
65 | warn!( |
66 | "Received a {:?} handshake message while expecting {:?}" , |
67 | parsed.typ, handshake_types |
68 | ); |
69 | Error::InappropriateHandshakeMessage { |
70 | expect_types: handshake_types.to_vec(), |
71 | got_type: parsed.typ, |
72 | } |
73 | } |
74 | payload: &MessagePayload => inappropriate_message(payload, content_types), |
75 | } |
76 | } |
77 | |