marshal.h 14 KB

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