Add support for variable-length onion payload reads using TLV
[rust-lightning] / lightning / src / util / ser.rs
1 //! A very simple serialization framework which is used to serialize/deserialize messages as well
2 //! as ChannelsManagers and ChannelMonitors.
3
4 use std::result::Result;
5 use std::io::{Read, Write};
6 use std::collections::HashMap;
7 use std::hash::Hash;
8 use std::sync::Mutex;
9 use std::cmp;
10
11 use secp256k1::Signature;
12 use secp256k1::key::{PublicKey, SecretKey};
13 use bitcoin::blockdata::script::Script;
14 use bitcoin::blockdata::transaction::OutPoint;
15 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
16 use std::marker::Sized;
17 use ln::msgs::DecodeError;
18 use ln::channelmanager::{PaymentPreimage, PaymentHash};
19 use util::byte_utils;
20
21 use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
22
23 const MAX_BUF_SIZE: usize = 64 * 1024;
24
25 /// A trait that is similar to std::io::Write but has one extra function which can be used to size
26 /// buffers being written into.
27 /// An impl is provided for any type that also impls std::io::Write which simply ignores size
28 /// hints.
29 pub trait Writer {
30         /// Writes the given buf out. See std::io::Write::write_all for more
31         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
32         /// Hints that data of the given size is about the be written. This may not always be called
33         /// prior to data being written and may be safely ignored.
34         fn size_hint(&mut self, size: usize);
35 }
36
37 impl<W: Write> Writer for W {
38         #[inline]
39         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
40                 <Self as ::std::io::Write>::write_all(self, buf)
41         }
42         #[inline]
43         fn size_hint(&mut self, _size: usize) { }
44 }
45
46 pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
47 impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> {
48         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
49                 self.0.write_all(buf)
50         }
51         fn write(&mut self, buf: &[u8]) -> Result<usize, ::std::io::Error> {
52                 self.0.write_all(buf)?;
53                 Ok(buf.len())
54         }
55         fn flush(&mut self) -> Result<(), ::std::io::Error> {
56                 Ok(())
57         }
58 }
59
60 struct VecWriter(Vec<u8>);
61 impl Writer for VecWriter {
62         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
63                 self.0.extend_from_slice(buf);
64                 Ok(())
65         }
66         fn size_hint(&mut self, size: usize) {
67                 self.0.reserve_exact(size);
68         }
69 }
70
71 pub(crate) struct LengthCalculatingWriter(pub usize);
72 impl Writer for LengthCalculatingWriter {
73         #[inline]
74         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
75                 self.0 += buf.len();
76                 Ok(())
77         }
78         #[inline]
79         fn size_hint(&mut self, _size: usize) {}
80 }
81
82 /// Essentially std::io::Take but a bit simpler and exposing the amount read at the end, cause we
83 /// may need to skip ahead that much at the end.
84 pub(crate) struct FixedLengthReader<R: Read> {
85         pub read: R,
86         pub read_len: u64,
87         pub max_len: u64,
88 }
89 impl<R: Read> FixedLengthReader<R> {
90         pub fn eat_remaining(&mut self) -> Result<(), ::std::io::Error> {
91                 while self.read_len != self.max_len {
92                         debug_assert!(self.read_len < self.max_len);
93                         let mut buf = [0; 1024];
94                         let readsz = cmp::min(1024, self.max_len - self.read_len) as usize;
95                         self.read_exact(&mut buf[0..readsz])?;
96                 }
97                 Ok(())
98         }
99 }
100 impl<R: Read> Read for FixedLengthReader<R> {
101         fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
102                 if dest.len() as u64 > self.max_len - self.read_len {
103                         Ok(0)
104                 } else {
105                         self.read_len += dest.len() as u64;
106                         self.read.read(dest)
107                 }
108         }
109 }
110
111 /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
112 pub trait Writeable {
113         /// Writes self out to the given Writer
114         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
115
116         /// Writes self out to a Vec<u8>
117         fn encode(&self) -> Vec<u8> {
118                 let mut msg = VecWriter(Vec::new());
119                 self.write(&mut msg).unwrap();
120                 msg.0
121         }
122
123         /// Writes self out to a Vec<u8>
124         fn encode_with_len(&self) -> Vec<u8> {
125                 let mut msg = VecWriter(Vec::new());
126                 0u16.write(&mut msg).unwrap();
127                 self.write(&mut msg).unwrap();
128                 let len = msg.0.len();
129                 msg.0[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
130                 msg.0
131         }
132 }
133
134 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
135 pub trait Readable<R>
136         where Self: Sized,
137               R: Read
138 {
139         /// Reads a Self in from the given Read
140         fn read(reader: &mut R) -> Result<Self, DecodeError>;
141 }
142
143 /// A trait that various higher-level rust-lightning types implement allowing them to be read in
144 /// from a Read given some additional set of arguments which is required to deserialize.
145 pub trait ReadableArgs<R, P>
146         where Self: Sized,
147               R: Read
148 {
149         /// Reads a Self in from the given Read
150         fn read(reader: &mut R, params: P) -> Result<Self, DecodeError>;
151 }
152
153 pub(crate) struct U48(pub u64);
154 impl Writeable for U48 {
155         #[inline]
156         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
157                 writer.write_all(&be48_to_array(self.0))
158         }
159 }
160 impl<R: Read> Readable<R> for U48 {
161         #[inline]
162         fn read(reader: &mut R) -> Result<U48, DecodeError> {
163                 let mut buf = [0; 6];
164                 reader.read_exact(&mut buf)?;
165                 Ok(U48(slice_to_be48(&buf)))
166         }
167 }
168
169 macro_rules! impl_writeable_primitive {
170         ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
171                 impl Writeable for $val_type {
172                         #[inline]
173                         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
174                                 writer.write_all(&$meth_write(*self))
175                         }
176                 }
177                 impl<R: Read> Readable<R> for $val_type {
178                         #[inline]
179                         fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
180                                 let mut buf = [0; $len];
181                                 reader.read_exact(&mut buf)?;
182                                 Ok($meth_read(&buf))
183                         }
184                 }
185         }
186 }
187
188 impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
189 impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
190 impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
191
192 impl Writeable for u8 {
193         #[inline]
194         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
195                 writer.write_all(&[*self])
196         }
197 }
198 impl<R: Read> Readable<R> for u8 {
199         #[inline]
200         fn read(reader: &mut R) -> Result<u8, DecodeError> {
201                 let mut buf = [0; 1];
202                 reader.read_exact(&mut buf)?;
203                 Ok(buf[0])
204         }
205 }
206
207 impl Writeable for bool {
208         #[inline]
209         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
210                 writer.write_all(&[if *self {1} else {0}])
211         }
212 }
213 impl<R: Read> Readable<R> for bool {
214         #[inline]
215         fn read(reader: &mut R) -> Result<bool, DecodeError> {
216                 let mut buf = [0; 1];
217                 reader.read_exact(&mut buf)?;
218                 if buf[0] != 0 && buf[0] != 1 {
219                         return Err(DecodeError::InvalidValue);
220                 }
221                 Ok(buf[0] == 1)
222         }
223 }
224
225 // u8 arrays
226 macro_rules! impl_array {
227         ( $size:expr ) => (
228                 impl Writeable for [u8; $size]
229                 {
230                         #[inline]
231                         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
232                                 w.write_all(self)
233                         }
234                 }
235
236                 impl<R: Read> Readable<R> for [u8; $size]
237                 {
238                         #[inline]
239                         fn read(r: &mut R) -> Result<Self, DecodeError> {
240                                 let mut buf = [0u8; $size];
241                                 r.read_exact(&mut buf)?;
242                                 Ok(buf)
243                         }
244                 }
245         );
246 }
247
248 //TODO: performance issue with [u8; size] with impl_array!()
249 impl_array!(3); // for rgb
250 impl_array!(4); // for IPv4
251 impl_array!(10); // for OnionV2
252 impl_array!(16); // for IPv6
253 impl_array!(32); // for channel id & hmac
254 impl_array!(33); // for PublicKey
255 impl_array!(64); // for Signature
256 impl_array!(1300); // for OnionPacket.hop_data
257
258 // HashMap
259 impl<K, V> Writeable for HashMap<K, V>
260         where K: Writeable + Eq + Hash,
261               V: Writeable
262 {
263         #[inline]
264         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
265         (self.len() as u16).write(w)?;
266                 for (key, value) in self.iter() {
267                         key.write(w)?;
268                         value.write(w)?;
269                 }
270                 Ok(())
271         }
272 }
273
274 impl<R, K, V> Readable<R> for HashMap<K, V>
275         where R: Read,
276               K: Readable<R> + Eq + Hash,
277               V: Readable<R>
278 {
279         #[inline]
280         fn read(r: &mut R) -> Result<Self, DecodeError> {
281                 let len: u16 = Readable::read(r)?;
282                 let mut ret = HashMap::with_capacity(len as usize);
283                 for _ in 0..len {
284                         ret.insert(K::read(r)?, V::read(r)?);
285                 }
286                 Ok(ret)
287         }
288 }
289
290 // Vectors
291 impl Writeable for Vec<u8> {
292         #[inline]
293         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
294                 (self.len() as u16).write(w)?;
295                 w.write_all(&self)
296         }
297 }
298
299 impl<R: Read> Readable<R> for Vec<u8> {
300         #[inline]
301         fn read(r: &mut R) -> Result<Self, DecodeError> {
302                 let len: u16 = Readable::read(r)?;
303                 let mut ret = Vec::with_capacity(len as usize);
304                 ret.resize(len as usize, 0);
305                 r.read_exact(&mut ret)?;
306                 Ok(ret)
307         }
308 }
309 impl Writeable for Vec<Signature> {
310         #[inline]
311         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
312                 (self.len() as u16).write(w)?;
313                 for e in self.iter() {
314                         e.write(w)?;
315                 }
316                 Ok(())
317         }
318 }
319
320 impl<R: Read> Readable<R> for Vec<Signature> {
321         #[inline]
322         fn read(r: &mut R) -> Result<Self, DecodeError> {
323                 let len: u16 = Readable::read(r)?;
324                 let byte_size = (len as usize)
325                                 .checked_mul(33)
326                                 .ok_or(DecodeError::BadLengthDescriptor)?;
327                 if byte_size > MAX_BUF_SIZE {
328                         return Err(DecodeError::BadLengthDescriptor);
329                 }
330                 let mut ret = Vec::with_capacity(len as usize);
331                 for _ in 0..len { ret.push(Signature::read(r)?); }
332                 Ok(ret)
333         }
334 }
335
336 impl Writeable for Script {
337         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
338                 (self.len() as u16).write(w)?;
339                 w.write_all(self.as_bytes())
340         }
341 }
342
343 impl<R: Read> Readable<R> for Script {
344         fn read(r: &mut R) -> Result<Self, DecodeError> {
345                 let len = <u16 as Readable<R>>::read(r)? as usize;
346                 let mut buf = vec![0; len];
347                 r.read_exact(&mut buf)?;
348                 Ok(Script::from(buf))
349         }
350 }
351
352 impl Writeable for PublicKey {
353         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
354                 self.serialize().write(w)
355         }
356 }
357
358 impl<R: Read> Readable<R> for PublicKey {
359         fn read(r: &mut R) -> Result<Self, DecodeError> {
360                 let buf: [u8; 33] = Readable::read(r)?;
361                 match PublicKey::from_slice(&buf) {
362                         Ok(key) => Ok(key),
363                         Err(_) => return Err(DecodeError::InvalidValue),
364                 }
365         }
366 }
367
368 impl Writeable for SecretKey {
369         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
370                 let mut ser = [0; 32];
371                 ser.copy_from_slice(&self[..]);
372                 ser.write(w)
373         }
374 }
375
376 impl<R: Read> Readable<R> for SecretKey {
377         fn read(r: &mut R) -> Result<Self, DecodeError> {
378                 let buf: [u8; 32] = Readable::read(r)?;
379                 match SecretKey::from_slice(&buf) {
380                         Ok(key) => Ok(key),
381                         Err(_) => return Err(DecodeError::InvalidValue),
382                 }
383         }
384 }
385
386 impl Writeable for Sha256dHash {
387         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
388                 w.write_all(&self[..])
389         }
390 }
391
392 impl<R: Read> Readable<R> for Sha256dHash {
393         fn read(r: &mut R) -> Result<Self, DecodeError> {
394                 use bitcoin_hashes::Hash;
395
396                 let buf: [u8; 32] = Readable::read(r)?;
397                 Ok(Sha256dHash::from_slice(&buf[..]).unwrap())
398         }
399 }
400
401 impl Writeable for Signature {
402         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
403                 self.serialize_compact().write(w)
404         }
405 }
406
407 impl<R: Read> Readable<R> for Signature {
408         fn read(r: &mut R) -> Result<Self, DecodeError> {
409                 let buf: [u8; 64] = Readable::read(r)?;
410                 match Signature::from_compact(&buf) {
411                         Ok(sig) => Ok(sig),
412                         Err(_) => return Err(DecodeError::InvalidValue),
413                 }
414         }
415 }
416
417 impl Writeable for PaymentPreimage {
418         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
419                 self.0.write(w)
420         }
421 }
422
423 impl<R: Read> Readable<R> for PaymentPreimage {
424         fn read(r: &mut R) -> Result<Self, DecodeError> {
425                 let buf: [u8; 32] = Readable::read(r)?;
426                 Ok(PaymentPreimage(buf))
427         }
428 }
429
430 impl Writeable for PaymentHash {
431         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
432                 self.0.write(w)
433         }
434 }
435
436 impl<R: Read> Readable<R> for PaymentHash {
437         fn read(r: &mut R) -> Result<Self, DecodeError> {
438                 let buf: [u8; 32] = Readable::read(r)?;
439                 Ok(PaymentHash(buf))
440         }
441 }
442
443 impl<T: Writeable> Writeable for Option<T> {
444         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
445                 match *self {
446                         None => 0u8.write(w)?,
447                         Some(ref data) => {
448                                 1u8.write(w)?;
449                                 data.write(w)?;
450                         }
451                 }
452                 Ok(())
453         }
454 }
455
456 impl<R, T> Readable<R> for Option<T>
457         where R: Read,
458               T: Readable<R>
459 {
460         fn read(r: &mut R) -> Result<Self, DecodeError> {
461                 match <u8 as Readable<R>>::read(r)? {
462                         0 => Ok(None),
463                         1 => Ok(Some(Readable::read(r)?)),
464                         _ => return Err(DecodeError::InvalidValue),
465                 }
466         }
467 }
468
469 impl Writeable for OutPoint {
470         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
471                 self.txid.write(w)?;
472                 self.vout.write(w)?;
473                 Ok(())
474         }
475 }
476
477 impl<R: Read> Readable<R> for OutPoint {
478         fn read(r: &mut R) -> Result<Self, DecodeError> {
479                 let txid = Readable::read(r)?;
480                 let vout = Readable::read(r)?;
481                 Ok(OutPoint {
482                         txid,
483                         vout,
484                 })
485         }
486 }
487
488 impl<R: Read, T: Readable<R>> Readable<R> for Mutex<T> {
489         fn read(r: &mut R) -> Result<Self, DecodeError> {
490                 let t: T = Readable::read(r)?;
491                 Ok(Mutex::new(t))
492         }
493 }
494 impl<T: Writeable> Writeable for Mutex<T> {
495         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
496                 self.lock().unwrap().write(w)
497         }
498 }
499
500 impl<R: Read, A: Readable<R>, B: Readable<R>> Readable<R> for (A, B) {
501         fn read(r: &mut R) -> Result<Self, DecodeError> {
502                 let a: A = Readable::read(r)?;
503                 let b: B = Readable::read(r)?;
504                 Ok((a, b))
505         }
506 }
507 impl<A: Writeable, B: Writeable> Writeable for (A, B) {
508         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
509                 self.0.write(w)?;
510                 self.1.write(w)
511         }
512 }