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
use std::any::{Any, TypeId};
use std::fmt::{Debug, Display};
use std::hash::Hash;

use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::algebra::signature::Signature;
use crate::algebra::Matcher;
use crate::claims::{Claim, SecurityViolationPolicy};
use crate::codec;
use crate::error::Error;
use crate::trace::{Knowledge, Source, Trace};

pub trait AsAny {
    fn as_any(&self) -> &dyn Any;
}

impl<T: 'static> AsAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Fill `knowledges` with new knowledge gathered form the type implementing EvaluatedTerm
/// by recursively calling extract_knowledge on all contained element
/// Knowledges can be extracted from using `extract_knowledge`
pub trait Extractable<PT: ProtocolTypes>: std::fmt::Debug + AsAny
where
    Self: 'static,
{
    /// Fill `knowledges` with new knowledge gathered form the type implementing `EvaluatedTerm`
    /// by recursively calling `extract_knowledge` on all contained element
    /// This will put source as the source of all the produced knowledge, matcher is also passed
    /// recursively but might be overwritten by a type with a more specific matcher
    fn extract_knowledge<'a>(
        &'a self,
        knowledges: &mut Vec<Knowledge<'a, PT>>,
        matcher: Option<PT::Matcher>,
        source: &'a Source,
    ) -> Result<(), Error>;
}

/// `EvaluatedTerm`: have both Codec and a way to extract knowledge out of a Message/OpaqueMessage
/// or any type that might be used in a precomputation
pub trait EvaluatedTerm<PT: ProtocolTypes>:
    codec::CodecP + Extractable<PT> + Debug + AsAny + 'static
where
    Self: 'static,
{
    fn type_id(&self) -> TypeId {
        Any::type_id(self)
    }

    fn type_name(&self) -> &'static str {
        std::any::type_name::<Self>()
    }

    fn boxed(&self) -> Box<dyn EvaluatedTerm<PT>>;
}
impl<T, PT: ProtocolTypes> EvaluatedTerm<PT> for T
where
    T: codec::CodecP + Extractable<PT> + 'static + Clone,
{
    fn boxed(&self) -> Box<dyn EvaluatedTerm<PT>> {
        Box::new(self.clone())
    }
}

/// Store a message flight, a vec of all the messages sent by the PUT between two steps
pub trait ProtocolMessageFlight<
    PT: ProtocolTypes,
    M: ProtocolMessage<PT, O>,
    O: OpaqueProtocolMessage<PT>,
    OF: OpaqueProtocolMessageFlight<PT, O>,
>: Clone + Debug + From<M> + TryFrom<OF> + Into<OF> + EvaluatedTerm<PT>
{
    fn new() -> Self;
    fn push(&mut self, msg: M);
    fn debug(&self, info: &str);
}

/// Store a flight of opaque messages, a vec of all the messages sent by the PUT between two steps
// We require codec::Codec to be able to read a bitstring and produce an owned
// `OpaqueProtocolMessageFlight<PT>` so we use the `Sized` version of `Codec`
pub trait OpaqueProtocolMessageFlight<PT: ProtocolTypes, O: OpaqueProtocolMessage<PT>>:
    Clone + Debug + codec::Codec + From<O> + EvaluatedTerm<PT>
{
    fn new() -> Self;
    fn debug(&self, info: &str);
    fn push(&mut self, msg: O);
}

/// A structured message. This type defines how all possible messages of a protocol.
/// Usually this is implemented using an `enum`.
pub trait ProtocolMessage<PT: ProtocolTypes, O: OpaqueProtocolMessage<PT>>:
    Clone + Debug + EvaluatedTerm<PT>
{
    fn create_opaque(&self) -> O;
    fn debug(&self, info: &str);
}

/// A non-structured version of [`ProtocolMessage`]. This can be used for example for encrypted
/// messages which do not have a structure.
pub trait OpaqueProtocolMessage<PT: ProtocolTypes>: Clone + Debug + EvaluatedTerm<PT> {
    fn debug(&self, info: &str);
}

/// Deframes a stream of bytes into distinct [`OpaqueProtocolMessages`](OpaqueProtocolMessage).
///
/// A deframer is usually state-ful. This means it produces as many messages from the input bytes
/// and stores them.
pub trait ProtocolMessageDeframer<PT: ProtocolTypes> {
    type OpaqueProtocolMessage: OpaqueProtocolMessage<PT>;

    fn pop_frame(&mut self) -> Option<Self::OpaqueProtocolMessage>;
    fn read(&mut self, rd: &mut dyn std::io::Read) -> std::io::Result<usize>;
}

/// Defines the types used to manipulate and concretize Terms
pub trait ProtocolTypes:
    'static + Clone + Hash + Display + Debug + Serialize + DeserializeOwned
{
    type Matcher: Matcher;

    /// Get the signature that is used in the protocol
    fn signature() -> &'static Signature<Self>;
}

/// Defines the protocol which is being tested.
///
/// The fuzzer is generally abstract over the used protocol. We assume that protocols have
/// [opaque messages](ProtocolBehavior::OpaqueProtocolMessage), [structured
/// messages](ProtocolBehavior::ProtocolMessage), and a way to [deframe](ProtocolMessageDeframer) an
/// arbitrary stream of bytes into messages.
///
/// Also the library allows the definition of a type for [claims](Claim) and a
/// (security policy)[`SecurityViolationPolicy`] over
/// sequences of them. Finally, there is a [matcher](Matcher) which allows traces to include
/// queries for [knowledge](crate::trace::Knowledge).
pub trait ProtocolBehavior: 'static {
    type ProtocolTypes: ProtocolTypes;
    type Claim: Claim<Self::ProtocolTypes>;
    type SecurityViolationPolicy: SecurityViolationPolicy<Self::ProtocolTypes, Self::Claim>;
    type ProtocolMessage: ProtocolMessage<Self::ProtocolTypes, Self::OpaqueProtocolMessage>;
    type OpaqueProtocolMessage: OpaqueProtocolMessage<Self::ProtocolTypes>;
    type ProtocolMessageFlight: ProtocolMessageFlight<
        Self::ProtocolTypes,
        Self::ProtocolMessage,
        Self::OpaqueProtocolMessage,
        Self::OpaqueProtocolMessageFlight,
    >;
    type OpaqueProtocolMessageFlight: OpaqueProtocolMessageFlight<Self::ProtocolTypes, Self::OpaqueProtocolMessage>
        + From<Self::ProtocolMessageFlight>;

    /// Creates a sane initial seed corpus.
    fn create_corpus() -> Vec<(Trace<Self::ProtocolTypes>, &'static str)>;

    /// Downcast from `Box<dyn Any>` and encode as bitstring any message as per the PB's internal
    /// structure
    fn any_get_encoding(message: &dyn EvaluatedTerm<Self::ProtocolTypes>) -> Vec<u8> {
        /// Use the `codecP::get_encoding` method to encode any `EvaluatedTerm<PT>`
        codec::CodecP::get_encoding(message)
    }

    /// Try to read a bitstring and interpret it as the `TypeShape`, which is the type of a message
    /// as per the PB's internal structure This is expected to fail for many types of messages!
    fn try_read_bytes(
        bitstring: &[u8],
        ty: TypeId,
    ) -> Result<Box<dyn EvaluatedTerm<Self::ProtocolTypes>>, Error>;
}

// -- Macros --
#[macro_export]
macro_rules! dummy_extract_knowledge {
    ($protocol_type:ty, $extract_type:ty) => {
        impl Extractable<$protocol_type> for $extract_type {
            fn extract_knowledge<'a>(
                &'a self,
                _knowledges: &mut Vec<Knowledge<'a, $protocol_type>>,
                _matcher: Option<<$protocol_type as ProtocolTypes>::Matcher>,
                _source: &'a Source,
            ) -> Result<(), Error> {
                log::warn!(
                    "Trying to extract a dummy type: {}",
                    stringify!($extract_type)
                );
                Ok(())
            }
        }
    };
}

#[macro_export]
macro_rules! dummy_codec {
    ($protocol_type:ty, $extract_type:ty) => {
        impl codec::CodecP for $extract_type {
            fn encode(&self, _bytes: &mut Vec<u8>) {
                log::warn!(
                    "Trying to encode a dummy type: {}",
                    stringify!($extract_type)
                );
            }

            fn read(&mut self, _r: &mut codec::Reader) -> Result<(), Error> {
                log::warn!("Trying to read a dummy type: {}", stringify!($extract_type));
                Ok(())
            }
        }
    };
}

#[macro_export]
macro_rules! dummy_extract_knowledge_codec {
    ($protocol_type:ty, $extract_type:ty) => {
        dummy_extract_knowledge!($protocol_type, $extract_type);
        dummy_codec!($protocol_type, $extract_type);
    };
}

#[macro_export]
macro_rules! atom_extract_knowledge {
    ($protocol_type:ty, $extract_type:ty) => {
        impl Extractable<$protocol_type> for $extract_type {
            fn extract_knowledge<'a>(
                &'a self,
                knowledges: &mut Vec<Knowledge<'a, $protocol_type>>,
                matcher: Option<<$protocol_type as ProtocolTypes>::Matcher>,
                source: &'a Source,
            ) -> Result<(), Error> {
                log::debug!("Extract atom: {}", stringify!($extract_type));
                knowledges.push(Knowledge {
                    source,
                    matcher,
                    data: self,
                });
                Ok(())
            }
        }
    };
}