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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use std::any::TypeId;

use puffin::codec;
use puffin::codec::{Codec, Reader, VecCodecWoSize};
use puffin::error::Error::Term;
use puffin::protocol::{EvaluatedTerm, ProtocolMessage};

use crate::protocol::{MessageFlight, OpaqueMessageFlight, TLSProtocolTypes};
use crate::tls::rustls::error::Error;
use crate::tls::rustls::hash_hs::HandshakeHash;
use crate::tls::rustls::key::Certificate;
use crate::tls::rustls::msgs::alert::AlertMessagePayload;
use crate::tls::rustls::msgs::base::{Payload, PayloadU16, PayloadU24, PayloadU8};
use crate::tls::rustls::msgs::ccs::ChangeCipherSpecPayload;
use crate::tls::rustls::msgs::enums::ContentType::ApplicationData;
use crate::tls::rustls::msgs::enums::ProtocolVersion::TLSv1_3;
use crate::tls::rustls::msgs::enums::{
    AlertDescription, AlertLevel, CipherSuite, Compression, ContentType, HandshakeType, NamedGroup,
    ProtocolVersion, SignatureScheme,
};
use crate::tls::rustls::msgs::handshake::{
    CertReqExtension, CertificateEntries, CertificateEntry, CertificateExtension, CipherSuites,
    ClientExtension, ClientExtensions, Compressions, HandshakeMessagePayload, HelloRetryExtension,
    HelloRetryExtensions, NewSessionTicketExtension, NewSessionTicketExtensions,
    PresharedKeyIdentity, Random, ServerExtension, ServerExtensions, SessionID, VecU16OfPayloadU16,
    VecU16OfPayloadU8,
};
use crate::tls::rustls::msgs::heartbeat::HeartbeatPayload;

#[derive(Debug, Clone)]
pub enum MessagePayload {
    Alert(AlertMessagePayload),
    Handshake(HandshakeMessagePayload),
    // this type is for TLS 1.2 encrypted handshake messages
    TLS12EncryptedHandshake(Payload),
    ChangeCipherSpec(ChangeCipherSpecPayload),
    ApplicationData(Payload),
    Heartbeat(HeartbeatPayload),
}

impl codec::CodecP for MessagePayload {
    fn encode(&self, bytes: &mut Vec<u8>) {
        MessagePayload::encode(self, bytes);
    }

    fn read(&mut self, _: &mut Reader) -> Result<(), puffin::error::Error> {
        Err(puffin::error::Error::Term(format!(
            "Failed to read for type {:?}",
            std::any::type_name::<MessagePayload>()
        )))
    }
}
impl MessagePayload {
    pub fn encode(&self, bytes: &mut Vec<u8>) {
        match *self {
            Self::Alert(ref x) => x.encode(bytes),
            Self::Handshake(ref x) => x.encode(bytes),
            Self::TLS12EncryptedHandshake(ref x) => x.encode(bytes),
            Self::ChangeCipherSpec(ref x) => x.encode(bytes),
            Self::ApplicationData(ref x) => x.encode(bytes),
            Self::Heartbeat(ref x) => x.encode(bytes),
        }
    }

    pub fn new(typ: ContentType, vers: ProtocolVersion, payload: Payload) -> Result<Self, Error> {
        let fallback_payload = payload.clone();
        let mut r = Reader::init(&payload.0);
        let parsed = match typ {
            ContentType::ApplicationData => return Ok(Self::ApplicationData(payload)),
            ContentType::Alert => AlertMessagePayload::read(&mut r).map(MessagePayload::Alert),
            ContentType::Handshake => {
                HandshakeMessagePayload::read_version(&mut r, vers)
                    .map(MessagePayload::Handshake)
                    // this type is for TLS 1.2 encrypted handshake messages
                    .or(Some(MessagePayload::TLS12EncryptedHandshake(
                        fallback_payload,
                    )))
            }
            ContentType::ChangeCipherSpec => {
                ChangeCipherSpecPayload::read(&mut r).map(MessagePayload::ChangeCipherSpec)
            }
            ContentType::Heartbeat => HeartbeatPayload::read(&mut r).map(MessagePayload::Heartbeat),
            _ => None,
        };

        parsed.ok_or(Error::corrupt_message(typ))
        /*        parsed
        .filter(|_| !r.any_left())
        .ok_or_else(|| Error::corrupt_message(typ))*/
    }

    /// Extract multiple messages payloads from one ApplicationData message
    pub fn multiple_new(
        typ: ContentType,
        vers: ProtocolVersion,
        payload: Payload,
    ) -> Result<Vec<Self>, Error> {
        let fallback_payload = &payload;
        let mut r = Reader::init(&payload.0);
        let mut parsed: Vec<Self> = vec![];
        while r.any_left() {
            let parsed_msg = match typ {
                ContentType::ApplicationData => Some(Self::ApplicationData(payload.clone())),
                ContentType::Alert => AlertMessagePayload::read(&mut r).map(MessagePayload::Alert),
                ContentType::Handshake => {
                    HandshakeMessagePayload::read_version(&mut r, vers)
                        .map(MessagePayload::Handshake)
                        // this type is for TLS 1.2 encrypted handshake messages
                        .or(Some(MessagePayload::TLS12EncryptedHandshake(
                            fallback_payload.clone(),
                        )))
                }
                ContentType::ChangeCipherSpec => {
                    ChangeCipherSpecPayload::read(&mut r).map(MessagePayload::ChangeCipherSpec)
                }
                ContentType::Heartbeat => {
                    HeartbeatPayload::read(&mut r).map(MessagePayload::Heartbeat)
                }
                _ => None,
            };
            if let Some(msg) = parsed_msg {
                parsed.push(msg);
            }
        }

        Ok(parsed)
    }

    pub fn content_type(&self) -> ContentType {
        match self {
            Self::Alert(_) => ContentType::Alert,
            Self::Handshake(_) => ContentType::Handshake,
            Self::TLS12EncryptedHandshake(_) => ContentType::Handshake,
            Self::ChangeCipherSpec(_) => ContentType::ChangeCipherSpec,
            Self::ApplicationData(_) => ContentType::ApplicationData,
            Self::Heartbeat(_) => ContentType::Heartbeat,
        }
    }
}

/// A TLS frame, named TLSPlaintext in the standard.
///
/// This type owns all memory for its interior parts. It is used to read/write from/to I/O
/// buffers as well as for fragmenting, joining and encryption/decryption. It can be converted
/// into a `Message` by decoding the payload.
#[derive(Clone, Debug)]
pub struct OpaqueMessage {
    pub typ: ContentType,
    pub version: ProtocolVersion,
    pub payload: Payload,
}

impl Codec for OpaqueMessage {
    fn encode(&self, bytes: &mut Vec<u8>) {
        self.typ.encode(bytes);
        self.version.encode(bytes);
        (self.payload.0.len() as u16).encode(bytes);
        self.payload.encode(bytes);
    }

    fn read(reader: &mut Reader) -> Option<Self> {
        Self::read(reader).ok()
    }
}

impl OpaqueMessage {
    /// Content type, version and size.
    const HEADER_SIZE: u16 = 1 + 2 + 2;
    /// This is the maximum on-the-wire size of a TLSCiphertext.
    /// That's 2^14 payload bytes, a header, and a 2KB allowance
    /// for ciphertext overheads.
    const MAX_PAYLOAD: u16 = 16384 + 2048;
    /// Maximum on-wire message size.
    pub const MAX_WIRE_SIZE: usize = (Self::MAX_PAYLOAD + Self::HEADER_SIZE) as usize;

    /// `MessageError` allows callers to distinguish between valid prefixes (might
    /// become valid if we read more data) and invalid data.
    pub fn read(r: &mut Reader) -> Result<Self, MessageError> {
        #[cfg(not(feature = "enable-guards"))]
        let typ = ContentType::read(r).unwrap_or(ApplicationData);
        #[cfg(not(feature = "enable-guards"))]
        let version = ProtocolVersion::read(r).unwrap_or(TLSv1_3);

        #[cfg(feature = "enable-guards")]
        let typ = ContentType::read(r).ok_or(MessageError::TooShortForHeader)?;
        #[cfg(feature = "enable-guards")]
        let version = ProtocolVersion::read(r).ok_or(MessageError::TooShortForHeader)?;

        let len = u16::read(r).ok_or(MessageError::TooShortForHeader)?;

        #[cfg(feature = "enable-guards")]
        // Reject undersize messages
        //  implemented per section 5.1 of RFC8446 (TLSv1.3)
        //              per section 6.2.1 of RFC5246 (TLSv1.2)
        if typ != ContentType::ApplicationData && len == 0 {
            return Err(MessageError::IllegalLength);
        }

        #[cfg(feature = "enable-guards")]
        // Reject oversize messages
        if len >= Self::MAX_PAYLOAD {
            return Err(MessageError::IllegalLength);
        }

        #[cfg(feature = "enable-guards")]
        // Don't accept any new content-types.
        if let ContentType::Unknown(_) = typ {
            return Err(MessageError::IllegalContentType);
        }

        #[cfg(feature = "enable-guards")]
        // Accept only versions 0x03XX for any XX.
        match version {
            ProtocolVersion::Unknown(ref v) if (v & 0xff00) != 0x0300 => {
                return Err(MessageError::IllegalProtocolVersion);
            }
            _ => {}
        };

        let mut sub = r.sub(len as usize).ok_or(MessageError::TooShortForLength)?;
        let payload = Payload::read(&mut sub);

        Ok(Self {
            typ,
            version,
            payload,
        })
    }

    /// Force conversion into a plaintext message.
    ///
    /// This should only be used for messages that are known to be in plaintext. Otherwise, the
    /// `OpaqueMessage` should be decrypted into a `PlainMessage` using a `MessageDecrypter`.
    pub fn into_plain_message(self) -> PlainMessage {
        PlainMessage {
            version: self.version,
            typ: self.typ,
            payload: self.payload,
        }
    }
}

impl From<Message> for PlainMessage {
    fn from(msg: Message) -> Self {
        let typ = msg.payload.content_type();
        let payload = match msg.payload {
            MessagePayload::ApplicationData(payload) => payload,
            _ => {
                let mut buf = Vec::new();
                msg.payload.encode(&mut buf);
                Payload(buf)
            }
        };

        Self {
            typ,
            version: msg.version,
            payload,
        }
    }
}

/// A decrypted TLS frame
///
/// This type owns all memory for its interior parts. It can be decrypted from an OpaqueMessage
/// or encrypted into an OpaqueMessage, and it is also used for joining and fragmenting.
#[derive(Clone, Debug)]
pub struct PlainMessage {
    pub typ: ContentType,
    pub version: ProtocolVersion,
    pub payload: Payload,
}

impl PlainMessage {
    pub fn into_unencrypted_opaque(self) -> OpaqueMessage {
        OpaqueMessage {
            version: self.version,
            typ: self.typ,
            payload: self.payload,
        }
    }

    pub fn borrow(&self) -> BorrowedPlainMessage<'_> {
        BorrowedPlainMessage {
            version: self.version,
            typ: self.typ,
            payload: &self.payload.0,
        }
    }
}

/// A message with decoded payload
#[derive(Debug, Clone)]
pub struct Message {
    pub version: ProtocolVersion,
    pub payload: MessagePayload,
}

// Make it VecCodecWoSize so that we have `Vec<T>: Codec` for free
impl VecCodecWoSize for Message {}

impl Codec for Message {
    fn encode(&self, bytes: &mut Vec<u8>) {
        Codec::encode(&self.create_opaque(), bytes);
    }

    fn read(reader: &mut Reader) -> Option<Self> {
        <OpaqueMessage>::read(reader)
            .ok()
            .and_then(|op| Message::try_from(op).ok())
    }
}

impl Message {
    pub fn is_handshake_type(&self, hstyp: HandshakeType) -> bool {
        // Bit of a layering violation, but OK.
        if let MessagePayload::Handshake(ref hsp) = self.payload {
            hsp.typ == hstyp
        } else {
            false
        }
    }

    pub fn build_alert(level: AlertLevel, desc: AlertDescription) -> Self {
        Self {
            version: ProtocolVersion::TLSv1_2,
            payload: MessagePayload::Alert(AlertMessagePayload {
                level,
                description: desc,
            }),
        }
    }

    pub fn build_key_update_notify() -> Self {
        Self {
            version: ProtocolVersion::TLSv1_3,
            payload: MessagePayload::Handshake(HandshakeMessagePayload::build_key_update_notify()),
        }
    }
}

/// Parses a plaintext message into a well-typed [`Message`].
///
/// A [`PlainMessage`] must contain plaintext content. Encrypted content should be stored in an
/// [`OpaqueMessage`] and decrypted before being stored into a [`PlainMessage`].
impl TryFrom<PlainMessage> for Message {
    type Error = Error;

    fn try_from(plain: PlainMessage) -> Result<Self, Self::Error> {
        Ok(Self {
            version: plain.version,
            payload: MessagePayload::new(plain.typ, plain.version, plain.payload)?,
        })
    }
}

impl TryFrom<OpaqueMessage> for Message {
    type Error = Error;

    fn try_from(value: OpaqueMessage) -> Result<Self, Self::Error> {
        Message::try_from(value.into_plain_message())
    }
}

/// A TLS frame, named TLSPlaintext in the standard.
///
/// This type differs from `OpaqueMessage` because it borrows
/// its payload.  You can make a `OpaqueMessage` from an
/// `BorrowMessage`, but this involves a copy.
///
/// This type also cannot decode its internals and
/// cannot be read/encoded; only `OpaqueMessage` can do that.
pub struct BorrowedPlainMessage<'a> {
    pub typ: ContentType,
    pub version: ProtocolVersion,
    pub payload: &'a [u8],
}

#[derive(Debug)]
pub enum MessageError {
    TooShortForHeader,
    TooShortForLength,
    IllegalLength,
    IllegalContentType,
    IllegalProtocolVersion,
}

// Rationale for `any_get_encoding` and `try_read_bytes`:
// 1. Messages of types Vec<Item> will be read and encoded without considering the size of the
//    vector (reading until end of buffer). We consider such messages as "intermediate values",
//    which are not meant to be directly used in struct fields such as
//   `extensions` in `ClientHello`. We use `VecCodecWoSize` for that.
//    In particular, an empty vector yield an empty bitstring and not [0].
// 2. Field elements of struct messages such as `extensions` in `ClientHello` are wrapped into a
//    constructor, whose `Codec` implementation consider the size of the vector, encoded into the
//    appropriate number of bytes. This depends on the field under consideration. For the above
//    example, we shall use `read_vec_u16` and `encode_vec_u16`.

// For all Countable types, we encode list of items of such type by prefixing with the length
// encoded in 2 bytes For each type: whether it produces empty bitstring for empty list ([]), and u8
// or u16 length prefix (8/16)
impl VecCodecWoSize for ClientExtension {} // []/u16
impl VecCodecWoSize for ServerExtension {} // u16    (server has to return at least oen extension it seems)
impl VecCodecWoSize for HelloRetryExtension {} // ?/u16
impl VecCodecWoSize for CertReqExtension {} // u16 -s
impl VecCodecWoSize for CertificateExtension {} // u16 -s
impl VecCodecWoSize for NewSessionTicketExtension {} //u16 -s
impl VecCodecWoSize for Compression {} // u8
impl VecCodecWoSize for Certificate {} // u24, no need?
impl VecCodecWoSize for CertificateEntry {} // u24
impl VecCodecWoSize for CipherSuite {} // u16
impl VecCodecWoSize for PresharedKeyIdentity {} //u16

#[macro_export]
macro_rules! try_read {
  ($bitstring:expr, $ti:expr, $T:ty, $($Ts:ty),+) => {
      {
      if $ti == TypeId::of::<$T>() {
        log::trace!("Type match TypeID {:?}...!", core::any::type_name::<$T>());
        <$T>::read_bytes($bitstring).ok_or(Term(format!(
                "[try_read_bytes] Failed to read to type {:?} the bitstring {:?}",
                core::any::type_name::<$T>(),
                & $bitstring
            )).into()).map(|v| Box::new(v) as Box<dyn EvaluatedTerm<TLSProtocolTypes>>)
      } else {
        try_read!($bitstring, $ti, $($Ts),+)
      }
      }
  };

  ($bitstring:expr, $ti:expr, $T:ty ) => {
      {
        if $ti == TypeId::of::<$T>() {
            log::trace!("Type match TypeID {:?}...!", core::any::type_name::<$T>());
            <$T>::read_bytes($bitstring).ok_or(Term(format!(
                "[try_read_bytes] Failed to read to type {:?} the bitstring {:?}",
                core::any::type_name::<$T>(),
                & $bitstring
            )).into()).map(|v| Box::new(v) as Box<dyn EvaluatedTerm<TLSProtocolTypes>>)
        } else {
                log::error!("Failed to find a suitable type with typeID {:?} to read the bitstring {:?}", $ti, &$bitstring);
                Err(Term(format!(
                    "[try_read_bytes] Failed to find a suitable type with typeID {:?} to read the bitstring {:?}",
                    $ti,
                    &$bitstring
                )).into())
        }
      }
  };
}

/// To `read` an `EvaluatedTerm<PT>` out of a bitstring, we cannot simply use `Codec::read_bytes`
/// since the type of the value to be initialized is not known, we only have the argument `ty` from
/// which we can downcast and then call `read_bytes` on the appropriate type.
/// `try_read_bytes` calls a macro `try_read` that does this.
///  (There is no workaround for the uninitialized value type since we need to make Codec traits
/// into dyn objects, hence it cannot have `Sized` as a supertrait.)
pub fn try_read_bytes(
    bitstring: &[u8],
    ty: TypeId,
) -> Result<Box<dyn EvaluatedTerm<TLSProtocolTypes>>, puffin::error::Error> {
    log::trace!("Trying read...");
    try_read!(
        bitstring,
        ty,
        // We list all the types that have the Codec trait and that can be the type of a rustls
        // message
        // The uni-test `term_zoo::test_term_read_encode` tests this is exhaustive for the TLS
        // signature at least
        Message,
        OpaqueMessage,
        MessageFlight,
        OpaqueMessageFlight,
        Vec<Certificate>,
        Certificate,
        CertificateEntries,
        Vec<CertificateEntry>,
        CertificateEntry,
        ServerExtensions,
        Vec<ServerExtension>,
        ClientExtensions,
        Vec<ClientExtension>,
        ClientExtension,
        ServerExtension,
        HelloRetryExtensions,
        Vec<HelloRetryExtension>,
        HelloRetryExtension,
        Vec<CertReqExtension>,
        CertReqExtension,
        Vec<CertificateExtension>,
        CertificateExtension,
        Vec<NewSessionTicketExtension>,
        NewSessionTicketExtension,
        NewSessionTicketExtensions,
        Random,
        Compressions,
        Vec<Compression>,
        Compression,
        SessionID,
        // HandshakeHash,
        // PrivateKey,
        CipherSuites,
        Vec<CipherSuite>,
        CipherSuite,
        Vec<PresharedKeyIdentity>,
        PresharedKeyIdentity,
        AlertMessagePayload,
        SignatureScheme,
        ProtocolVersion,
        HandshakeHash,
        u64,
        u32,
        // u8,
        // Vec<u64>,
        PayloadU24,
        PayloadU16,
        PayloadU8,
        Vec<PayloadU24>,
        Vec<PayloadU16>,
        Vec<PayloadU8>,
        VecU16OfPayloadU16,
        VecU16OfPayloadU8,
        Vec<u8>,
        Option<Vec<u8>>,
        Vec<Vec<u8>>,
        bool,
        // Option<Vec<Vec<u8>>>,
        // Result<Option<Vec<u8>>, FnError>,
        // Result<Vec<u8>, FnError>,
        // Result<bool, FnError>,
        // Result<Vec<u8>, FnError>,
        // Result<Vec<Vec<u8>>, FnError>,
        //
        // Message,
        // Result<Message FnError>,
        // MessagePayload,
        // ExtensionType,
        NamedGroup
    )
}