marshal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef PUTTY_MARSHAL_H
  2. #define PUTTY_MARSHAL_H
  3. #include "defs.h"
  4. /*
  5. * A sort of 'abstract base class' or 'interface' or 'trait' which is
  6. * the common feature of all types that want to accept data formatted
  7. * using the SSH binary conventions of uint32, string, mpint etc.
  8. */
  9. struct BinarySink {
  10. void (*write)(BinarySink *sink, const void *data, size_t len);
  11. BinarySink *binarysink_;
  12. };
  13. /*
  14. * To define a structure type as a valid target for binary formatted
  15. * data, put 'BinarySink_IMPLEMENTATION' in its declaration, and when
  16. * an instance is set up, use 'BinarySink_INIT' to initialise the
  17. * 'base class' state, providing a function pointer to be the
  18. * implementation of the write() call above.
  19. */
  20. #define BinarySink_IMPLEMENTATION BinarySink binarysink_[1]
  21. #define BinarySink_INIT(obj, writefn) \
  22. ((obj)->binarysink_->write = (writefn), \
  23. (obj)->binarysink_->binarysink_ = (obj)->binarysink_)
  24. /*
  25. * To define a larger structure type as a valid BinarySink in such a
  26. * way that it will delegate the write method to some other object,
  27. * put 'BinarySink_DELEGATE_IMPLEMENTATION' in its declaration, and
  28. * when an instance is set up, use 'BinarySink_DELEGATE_INIT' to point
  29. * at the object it wants to delegate to.
  30. *
  31. * In such a delegated structure, you might sometimes want to have the
  32. * delegation stop being valid (e.g. it might be delegating to an
  33. * object that only sometimes exists). You can null out the delegate
  34. * pointer using BinarySink_DELEGATE_CLEAR.
  35. */
  36. #define BinarySink_DELEGATE_IMPLEMENTATION BinarySink *binarysink_
  37. #define BinarySink_DELEGATE_INIT(obj, othersink) \
  38. ((obj)->binarysink_ = BinarySink_UPCAST(othersink))
  39. #define BinarySink_DELEGATE_CLEAR(obj) ((obj)->binarysink_ = NULL)
  40. /*
  41. * The implementing type's write function will want to downcast its
  42. * 'BinarySink *' parameter back to the more specific type. Also,
  43. * sometimes you'll want to upcast a pointer to a particular
  44. * implementing type into an abstract 'BinarySink *' to pass to
  45. * generic subroutines not defined in this file. These macros do that
  46. * job.
  47. *
  48. * Importantly, BinarySink_UPCAST can also be applied to a BinarySink
  49. * * itself (and leaves it unchanged). That's achieved by a small
  50. * piece of C trickery: implementing structures and the BinarySink
  51. * structure itself both contain a field called binarysink_, but in
  52. * implementing objects it's a BinarySink[1] whereas in the abstract
  53. * type it's a 'BinarySink *' pointing back to the same structure,
  54. * meaning that you can say 'foo->binarysink_' in either case and get
  55. * a pointer type by different methods.
  56. */
  57. #define BinarySink_DOWNCAST(object, type) \
  58. TYPECHECK((object) == ((type *)0)->binarysink_, \
  59. ((type *)(((char *)(object)) - offsetof(type, binarysink_))))
  60. #define BinarySink_UPCAST(object) \
  61. TYPECHECK((object)->binarysink_ == (BinarySink *)0, \
  62. (object)->binarysink_)
  63. /*
  64. * If you structure-copy an object that's implementing BinarySink,
  65. * then that tricky self-pointer in its trait subobject will point to
  66. * the wrong place. You could call BinarySink_INIT again, but this
  67. * macro is terser and does all that's needed to fix up the copied
  68. * object.
  69. */
  70. #define BinarySink_COPIED(obj) \
  71. ((obj)->binarysink_->binarysink_ = (obj)->binarysink_)
  72. /*
  73. * The put_* macros are the main client to this system. Any structure
  74. * which implements the BinarySink 'trait' is valid for use as the
  75. * first parameter of any of these put_* macros.
  76. */
  77. /* Basic big-endian integer types. */
  78. #define put_byte(bs, val) \
  79. BinarySink_put_byte(BinarySink_UPCAST(bs), val)
  80. #define put_uint16(bs, val) \
  81. BinarySink_put_uint16(BinarySink_UPCAST(bs), val)
  82. #define put_uint32(bs, val) \
  83. BinarySink_put_uint32(BinarySink_UPCAST(bs), val)
  84. #define put_uint64(bs, val) \
  85. BinarySink_put_uint64(BinarySink_UPCAST(bs), val)
  86. /* SSH booleans, encoded as a single byte storing either 0 or 1. */
  87. #define put_bool(bs, val) \
  88. BinarySink_put_bool(BinarySink_UPCAST(bs), val)
  89. /* SSH strings, with a leading uint32 length field. 'stringz' is a
  90. * convenience function that takes an ordinary C zero-terminated
  91. * string as input. 'stringsb' takes a strbuf * as input, and
  92. * finalises it as a side effect (handy for multi-level marshalling in
  93. * which you use these same functions to format an inner blob of data
  94. * that then gets wrapped into a string container in an outer one). */
  95. #define put_string(bs, val, len) \
  96. BinarySink_put_string(BinarySink_UPCAST(bs),val,len)
  97. #define put_stringpl(bs, ptrlen) \
  98. BinarySink_put_stringpl(BinarySink_UPCAST(bs),ptrlen)
  99. #define put_stringz(bs, val) \
  100. BinarySink_put_stringz(BinarySink_UPCAST(bs), val)
  101. #define put_stringsb(bs, val) \
  102. BinarySink_put_stringsb(BinarySink_UPCAST(bs), val)
  103. /* Other string outputs: 'asciz' emits the string data directly into
  104. * the output including the terminating \0, and 'pstring' emits the
  105. * string in Pascal style with a leading _one_-byte length field.
  106. * pstring can fail if the string is too long. */
  107. #define put_asciz(bs, val) \
  108. BinarySink_put_asciz(BinarySink_UPCAST(bs), val)
  109. #define put_pstring(bs, val) \
  110. BinarySink_put_pstring(BinarySink_UPCAST(bs), val)
  111. /* Multiprecision integers, in both the SSH-1 and SSH-2 formats. */
  112. #define put_mp_ssh1(bs, val) \
  113. BinarySink_put_mp_ssh1(BinarySink_UPCAST(bs), val)
  114. #define put_mp_ssh2(bs, val) \
  115. BinarySink_put_mp_ssh2(BinarySink_UPCAST(bs), val)
  116. /* Padding with a specified byte. */
  117. #define put_padding(bs, len, padbyte) \
  118. BinarySink_put_padding(BinarySink_UPCAST(bs), len, padbyte)
  119. /* Fallback: just emit raw data bytes, using a syntax that matches the
  120. * rest of these macros. */
  121. #define put_data(bs, val, len) \
  122. BinarySink_put_data(BinarySink_UPCAST(bs), val, len)
  123. #define put_datapl(bs, pl) \
  124. BinarySink_put_datapl(BinarySink_UPCAST(bs), pl)
  125. /*
  126. * The underlying real C functions that implement most of those
  127. * macros. Generally you won't want to call these directly, because
  128. * they have such cumbersome names; you call the wrapper macros above
  129. * instead.
  130. *
  131. * A few functions whose wrapper macros are defined above are actually
  132. * declared in other headers, so as to guarantee that the
  133. * declaration(s) of their other parameter type(s) are in scope.
  134. */
  135. void BinarySink_put_data(BinarySink *, const void *data, size_t len);
  136. void BinarySink_put_datapl(BinarySink *, ptrlen);
  137. void BinarySink_put_padding(BinarySink *, size_t len, unsigned char padbyte);
  138. void BinarySink_put_byte(BinarySink *, unsigned char);
  139. void BinarySink_put_bool(BinarySink *, bool);
  140. void BinarySink_put_uint16(BinarySink *, unsigned long);
  141. void BinarySink_put_uint32(BinarySink *, unsigned long);
  142. void BinarySink_put_uint64(BinarySink *, uint64_t);
  143. void BinarySink_put_string(BinarySink *, const void *data, size_t len);
  144. void BinarySink_put_stringpl(BinarySink *, ptrlen);
  145. void BinarySink_put_stringz(BinarySink *, const char *str);
  146. struct strbuf;
  147. void BinarySink_put_stringsb(BinarySink *, struct strbuf *);
  148. void BinarySink_put_asciz(BinarySink *, const char *str);
  149. bool BinarySink_put_pstring(BinarySink *, const char *str);
  150. void BinarySink_put_mp_ssh1(BinarySink *bs, mp_int *x);
  151. void BinarySink_put_mp_ssh2(BinarySink *bs, mp_int *x);
  152. /* ---------------------------------------------------------------------- */
  153. /*
  154. * A complementary trait structure for _un_-marshalling.
  155. *
  156. * This structure contains client-visible data fields rather than
  157. * methods, because that seemed more useful than leaving it totally
  158. * opaque. But it's still got the self-pointer system that will allow
  159. * the set of get_* macros to target one of these itself or any other
  160. * type that 'derives' from it. So, for example, an SSH packet
  161. * structure can act as a BinarySource while also having additional
  162. * fields like the packet type.
  163. */
  164. typedef enum BinarySourceError {
  165. BSE_NO_ERROR,
  166. BSE_OUT_OF_DATA,
  167. BSE_INVALID
  168. } BinarySourceError;
  169. struct BinarySource {
  170. /*
  171. * (data, len) is the data block being decoded. pos is the current
  172. * position within the block.
  173. */
  174. const void *data;
  175. size_t pos, len;
  176. /*
  177. * 'err' indicates whether a decoding error has happened at any
  178. * point. Once this has been set to something other than
  179. * BSE_NO_ERROR, it shouldn't be changed by any unmarshalling
  180. * function. So you can safely do a long sequence of get_foo()
  181. * operations and then test err just once at the end, rather than
  182. * having to conditionalise every single get.
  183. *
  184. * The unmarshalling functions should always return some value,
  185. * even if a decoding error occurs. Generally on error they'll
  186. * return zero (if numeric) or the empty string (if string-based),
  187. * or some other appropriate default value for more complicated
  188. * types.
  189. *
  190. * If the usual return value is dynamically allocated (e.g. a
  191. * bignum, or a normal C 'char *' string), then the error value is
  192. * also dynamic in the same way. So you have to free exactly the
  193. * same set of things whether or not there was a decoding error,
  194. * which simplifies exit paths - for example, you could call a big
  195. * pile of get_foo functions, then put the actual handling of the
  196. * results under 'if (!get_err(src))', and then free everything
  197. * outside that if.
  198. */
  199. BinarySourceError err;
  200. /*
  201. * Self-pointer for the implicit derivation trick, same as
  202. * BinarySink above.
  203. */
  204. BinarySource *binarysource_;
  205. };
  206. /*
  207. * Implementation macros, similar to BinarySink.
  208. */
  209. #define BinarySource_IMPLEMENTATION BinarySource binarysource_[1]
  210. static inline void BinarySource_INIT__(BinarySource *src, ptrlen data)
  211. {
  212. src->data = data.ptr;
  213. src->len = data.len;
  214. src->pos = 0;
  215. src->err = BSE_NO_ERROR;
  216. src->binarysource_ = src;
  217. }
  218. #define BinarySource_BARE_INIT_PL(obj, pl) \
  219. TYPECHECK(&(obj)->binarysource_ == (BinarySource **)0, \
  220. BinarySource_INIT__(obj, pl))
  221. #define BinarySource_BARE_INIT(obj, data_, len_) \
  222. BinarySource_BARE_INIT_PL(obj, make_ptrlen(data_, len_))
  223. #define BinarySource_INIT_PL(obj, pl) \
  224. TYPECHECK(&(obj)->binarysource_ == (BinarySource (*)[1])0, \
  225. BinarySource_INIT__(BinarySource_UPCAST(obj), pl))
  226. #define BinarySource_INIT(obj, data_, len_) \
  227. BinarySource_INIT_PL(obj, make_ptrlen(data_, len_))
  228. #define BinarySource_DOWNCAST(object, type) \
  229. TYPECHECK((object) == ((type *)0)->binarysource_, \
  230. ((type *)(((char *)(object)) - offsetof(type, binarysource_))))
  231. #define BinarySource_UPCAST(object) \
  232. TYPECHECK((object)->binarysource_ == (BinarySource *)0, \
  233. (object)->binarysource_)
  234. #define BinarySource_COPIED(obj) \
  235. ((obj)->binarysource_->binarysource_ = (obj)->binarysource_)
  236. #define get_data(src, len) \
  237. BinarySource_get_data(BinarySource_UPCAST(src), len)
  238. #define get_byte(src) \
  239. BinarySource_get_byte(BinarySource_UPCAST(src))
  240. #define get_bool(src) \
  241. BinarySource_get_bool(BinarySource_UPCAST(src))
  242. #define get_uint16(src) \
  243. BinarySource_get_uint16(BinarySource_UPCAST(src))
  244. #define get_uint32(src) \
  245. BinarySource_get_uint32(BinarySource_UPCAST(src))
  246. #define get_uint64(src) \
  247. BinarySource_get_uint64(BinarySource_UPCAST(src))
  248. #define get_string(src) \
  249. BinarySource_get_string(BinarySource_UPCAST(src))
  250. #define get_asciz(src) \
  251. BinarySource_get_asciz(BinarySource_UPCAST(src))
  252. #define get_pstring(src) \
  253. BinarySource_get_pstring(BinarySource_UPCAST(src))
  254. #define get_mp_ssh1(src) \
  255. BinarySource_get_mp_ssh1(BinarySource_UPCAST(src))
  256. #define get_mp_ssh2(src) \
  257. BinarySource_get_mp_ssh2(BinarySource_UPCAST(src))
  258. #define get_rsa_ssh1_pub(src, rsa, order) \
  259. BinarySource_get_rsa_ssh1_pub(BinarySource_UPCAST(src), rsa, order)
  260. #define get_rsa_ssh1_priv(src, rsa) \
  261. BinarySource_get_rsa_ssh1_priv(BinarySource_UPCAST(src), rsa)
  262. #define get_err(src) (BinarySource_UPCAST(src)->err)
  263. #define get_avail(src) (BinarySource_UPCAST(src)->len - \
  264. BinarySource_UPCAST(src)->pos)
  265. #define get_ptr(src) \
  266. ((const void *)( \
  267. (const unsigned char *)(BinarySource_UPCAST(src)->data) + \
  268. BinarySource_UPCAST(src)->pos))
  269. ptrlen BinarySource_get_data(BinarySource *, size_t);
  270. unsigned char BinarySource_get_byte(BinarySource *);
  271. bool BinarySource_get_bool(BinarySource *);
  272. unsigned BinarySource_get_uint16(BinarySource *);
  273. unsigned long BinarySource_get_uint32(BinarySource *);
  274. uint64_t BinarySource_get_uint64(BinarySource *);
  275. ptrlen BinarySource_get_string(BinarySource *);
  276. const char *BinarySource_get_asciz(BinarySource *);
  277. ptrlen BinarySource_get_pstring(BinarySource *);
  278. mp_int *BinarySource_get_mp_ssh1(BinarySource *src);
  279. mp_int *BinarySource_get_mp_ssh2(BinarySource *src);
  280. #endif /* PUTTY_MARSHAL_H */