aboutsummaryrefslogtreecommitdiff
path: root/src/trunnel/circpad_negotiation.c
blob: 547818f2ece961eece75b2aa53782dc0b7183b1e (plain)
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
541
542
543
544
545
546
547
548
549
/* circpad_negotiation.c -- generated by Trunnel v1.5.3.
 * https://gitweb.torproject.org/trunnel.git
 * You probably shouldn't edit this file.
 */
#include <stdlib.h>
#include "trunnel-impl.h"

#include "circpad_negotiation.h"

#define TRUNNEL_SET_ERROR_CODE(obj) \
  do {                              \
    (obj)->trunnel_error_code_ = 1; \
  } while (0)

#if defined(__COVERITY__) || defined(__clang_analyzer__)
/* If we're running a static analysis tool, we don't want it to complain
 * that some of our remaining-bytes checks are dead-code. */
int circpadnegotiation_deadcode_dummy__ = 0;
#define OR_DEADCODE_DUMMY || circpadnegotiation_deadcode_dummy__
#else
#define OR_DEADCODE_DUMMY
#endif

#define CHECK_REMAINING(nbytes, label)                           \
  do {                                                           \
    if (remaining < (nbytes) OR_DEADCODE_DUMMY) {                \
      goto label;                                                \
    }                                                            \
  } while (0)

circpad_negotiate_t *
circpad_negotiate_new(void)
{
  circpad_negotiate_t *val = trunnel_calloc(1, sizeof(circpad_negotiate_t));
  if (NULL == val)
    return NULL;
  val->command = CIRCPAD_COMMAND_START;
  return val;
}

/** Release all storage held inside 'obj', but do not free 'obj'.
 */
static void
circpad_negotiate_clear(circpad_negotiate_t *obj)
{
  (void) obj;
}

void
circpad_negotiate_free(circpad_negotiate_t *obj)
{
  if (obj == NULL)
    return;
  circpad_negotiate_clear(obj);
  trunnel_memwipe(obj, sizeof(circpad_negotiate_t));
  trunnel_free_(obj);
}

uint8_t
circpad_negotiate_get_version(const circpad_negotiate_t *inp)
{
  return inp->version;
}
int
circpad_negotiate_set_version(circpad_negotiate_t *inp, uint8_t val)
{
  if (! ((val == 0))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->version = val;
  return 0;
}
uint8_t
circpad_negotiate_get_command(const circpad_negotiate_t *inp)
{
  return inp->command;
}
int
circpad_negotiate_set_command(circpad_negotiate_t *inp, uint8_t val)
{
  if (! ((val == CIRCPAD_COMMAND_START || val == CIRCPAD_COMMAND_STOP))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->command = val;
  return 0;
}
uint8_t
circpad_negotiate_get_machine_type(const circpad_negotiate_t *inp)
{
  return inp->machine_type;
}
int
circpad_negotiate_set_machine_type(circpad_negotiate_t *inp, uint8_t val)
{
  inp->machine_type = val;
  return 0;
}
uint8_t
circpad_negotiate_get_echo_request(const circpad_negotiate_t *inp)
{
  return inp->echo_request;
}
int
circpad_negotiate_set_echo_request(circpad_negotiate_t *inp, uint8_t val)
{
  if (! ((val == 0 || val == 1))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->echo_request = val;
  return 0;
}
const char *
circpad_negotiate_check(const circpad_negotiate_t *obj)
{
  if (obj == NULL)
    return "Object was NULL";
  if (obj->trunnel_error_code_)
    return "A set function failed on this object";
  if (! (obj->version == 0))
    return "Integer out of bounds";
  if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
    return "Integer out of bounds";
  if (! (obj->echo_request == 0 || obj->echo_request == 1))
    return "Integer out of bounds";
  return NULL;
}

ssize_t
circpad_negotiate_encoded_len(const circpad_negotiate_t *obj)
{
  ssize_t result = 0;

  if (NULL != circpad_negotiate_check(obj))
     return -1;


  /* Length of u8 version IN [0] */
  result += 1;

  /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  result += 1;

  /* Length of u8 machine_type */
  result += 1;

  /* Length of u8 echo_request IN [0, 1] */
  result += 1;
  return result;
}
int
circpad_negotiate_clear_errors(circpad_negotiate_t *obj)
{
  int r = obj->trunnel_error_code_;
  obj->trunnel_error_code_ = 0;
  return r;
}
ssize_t
circpad_negotiate_encode(uint8_t *output, const size_t avail, const circpad_negotiate_t *obj)
{
  ssize_t result = 0;
  size_t written = 0;
  uint8_t *ptr = output;
  const char *msg;
#ifdef TRUNNEL_CHECK_ENCODED_LEN
  const ssize_t encoded_len = circpad_negotiate_encoded_len(obj);
#endif

  if (NULL != (msg = circpad_negotiate_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u8 version IN [0] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->version));
  written += 1; ptr += 1;

  /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->command));
  written += 1; ptr += 1;

  /* Encode u8 machine_type */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->machine_type));
  written += 1; ptr += 1;

  /* Encode u8 echo_request IN [0, 1] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->echo_request));
  written += 1; ptr += 1;


  trunnel_assert(ptr == output + written);
#ifdef TRUNNEL_CHECK_ENCODED_LEN
  {
    trunnel_assert(encoded_len >= 0);
    trunnel_assert((size_t)encoded_len == written);
  }

#endif

  return written;

 truncated:
  result = -2;
  goto fail;
 check_failed:
  (void)msg;
  result = -1;
  goto fail;
 fail:
  trunnel_assert(result < 0);
  return result;
}

/** As circpad_negotiate_parse(), but do not allocate the output
 * object.
 */
static ssize_t
circpad_negotiate_parse_into(circpad_negotiate_t *obj, const uint8_t *input, const size_t len_in)
{
  const uint8_t *ptr = input;
  size_t remaining = len_in;
  ssize_t result = 0;
  (void)result;

  /* Parse u8 version IN [0] */
  CHECK_REMAINING(1, truncated);
  obj->version = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->version == 0))
    goto fail;

  /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  CHECK_REMAINING(1, truncated);
  obj->command = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
    goto fail;

  /* Parse u8 machine_type */
  CHECK_REMAINING(1, truncated);
  obj->machine_type = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;

  /* Parse u8 echo_request IN [0, 1] */
  CHECK_REMAINING(1, truncated);
  obj->echo_request = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->echo_request == 0 || obj->echo_request == 1))
    goto fail;
  trunnel_assert(ptr + remaining == input + len_in);
  return len_in - remaining;

 truncated:
  return -2;
 fail:
  result = -1;
  return result;
}

ssize_t
circpad_negotiate_parse(circpad_negotiate_t **output, const uint8_t *input, const size_t len_in)
{
  ssize_t result;
  *output = circpad_negotiate_new();
  if (NULL == *output)
    return -1;
  result = circpad_negotiate_parse_into(*output, input, len_in);
  if (result < 0) {
    circpad_negotiate_free(*output);
    *output = NULL;
  }
  return result;
}
circpad_negotiated_t *
circpad_negotiated_new(void)
{
  circpad_negotiated_t *val = trunnel_calloc(1, sizeof(circpad_negotiated_t));
  if (NULL == val)
    return NULL;
  val->command = CIRCPAD_COMMAND_START;
  val->response = CIRCPAD_RESPONSE_ERR;
  return val;
}

/** Release all storage held inside 'obj', but do not free 'obj'.
 */
static void
circpad_negotiated_clear(circpad_negotiated_t *obj)
{
  (void) obj;
}

void
circpad_negotiated_free(circpad_negotiated_t *obj)
{
  if (obj == NULL)
    return;
  circpad_negotiated_clear(obj);
  trunnel_memwipe(obj, sizeof(circpad_negotiated_t));
  trunnel_free_(obj);
}

uint8_t
circpad_negotiated_get_version(const circpad_negotiated_t *inp)
{
  return inp->version;
}
int
circpad_negotiated_set_version(circpad_negotiated_t *inp, uint8_t val)
{
  if (! ((val == 0))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->version = val;
  return 0;
}
uint8_t
circpad_negotiated_get_command(const circpad_negotiated_t *inp)
{
  return inp->command;
}
int
circpad_negotiated_set_command(circpad_negotiated_t *inp, uint8_t val)
{
  if (! ((val == CIRCPAD_COMMAND_START || val == CIRCPAD_COMMAND_STOP))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->command = val;
  return 0;
}
uint8_t
circpad_negotiated_get_response(const circpad_negotiated_t *inp)
{
  return inp->response;
}
int
circpad_negotiated_set_response(circpad_negotiated_t *inp, uint8_t val)
{
  if (! ((val == CIRCPAD_RESPONSE_ERR || val == CIRCPAD_RESPONSE_OK))) {
     TRUNNEL_SET_ERROR_CODE(inp);
     return -1;
  }
  inp->response = val;
  return 0;
}
uint8_t
circpad_negotiated_get_machine_type(const circpad_negotiated_t *inp)
{
  return inp->machine_type;
}
int
circpad_negotiated_set_machine_type(circpad_negotiated_t *inp, uint8_t val)
{
  inp->machine_type = val;
  return 0;
}
const char *
circpad_negotiated_check(const circpad_negotiated_t *obj)
{
  if (obj == NULL)
    return "Object was NULL";
  if (obj->trunnel_error_code_)
    return "A set function failed on this object";
  if (! (obj->version == 0))
    return "Integer out of bounds";
  if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
    return "Integer out of bounds";
  if (! (obj->response == CIRCPAD_RESPONSE_ERR || obj->response == CIRCPAD_RESPONSE_OK))
    return "Integer out of bounds";
  return NULL;
}

ssize_t
circpad_negotiated_encoded_len(const circpad_negotiated_t *obj)
{
  ssize_t result = 0;

  if (NULL != circpad_negotiated_check(obj))
     return -1;


  /* Length of u8 version IN [0] */
  result += 1;

  /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  result += 1;

  /* Length of u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
  result += 1;

  /* Length of u8 machine_type */
  result += 1;
  return result;
}
int
circpad_negotiated_clear_errors(circpad_negotiated_t *obj)
{
  int r = obj->trunnel_error_code_;
  obj->trunnel_error_code_ = 0;
  return r;
}
ssize_t
circpad_negotiated_encode(uint8_t *output, const size_t avail, const circpad_negotiated_t *obj)
{
  ssize_t result = 0;
  size_t written = 0;
  uint8_t *ptr = output;
  const char *msg;
#ifdef TRUNNEL_CHECK_ENCODED_LEN
  const ssize_t encoded_len = circpad_negotiated_encoded_len(obj);
#endif

  if (NULL != (msg = circpad_negotiated_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u8 version IN [0] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->version));
  written += 1; ptr += 1;

  /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->command));
  written += 1; ptr += 1;

  /* Encode u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->response));
  written += 1; ptr += 1;

  /* Encode u8 machine_type */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->machine_type));
  written += 1; ptr += 1;


  trunnel_assert(ptr == output + written);
#ifdef TRUNNEL_CHECK_ENCODED_LEN
  {
    trunnel_assert(encoded_len >= 0);
    trunnel_assert((size_t)encoded_len == written);
  }

#endif

  return written;

 truncated:
  result = -2;
  goto fail;
 check_failed:
  (void)msg;
  result = -1;
  goto fail;
 fail:
  trunnel_assert(result < 0);
  return result;
}

/** As circpad_negotiated_parse(), but do not allocate the output
 * object.
 */
static ssize_t
circpad_negotiated_parse_into(circpad_negotiated_t *obj, const uint8_t *input, const size_t len_in)
{
  const uint8_t *ptr = input;
  size_t remaining = len_in;
  ssize_t result = 0;
  (void)result;

  /* Parse u8 version IN [0] */
  CHECK_REMAINING(1, truncated);
  obj->version = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->version == 0))
    goto fail;

  /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
  CHECK_REMAINING(1, truncated);
  obj->command = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
    goto fail;

  /* Parse u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
  CHECK_REMAINING(1, truncated);
  obj->response = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  if (! (obj->response == CIRCPAD_RESPONSE_ERR || obj->response == CIRCPAD_RESPONSE_OK))
    goto fail;

  /* Parse u8 machine_type */
  CHECK_REMAINING(1, truncated);
  obj->machine_type = (trunnel_get_uint8(ptr));
  remaining -= 1; ptr += 1;
  trunnel_assert(ptr + remaining == input + len_in);
  return len_in - remaining;

 truncated:
  return -2;
 fail:
  result = -1;
  return result;
}

ssize_t
circpad_negotiated_parse(circpad_negotiated_t **output, const uint8_t *input, const size_t len_in)
{
  ssize_t result;
  *output = circpad_negotiated_new();
  if (NULL == *output)
    return -1;
  result = circpad_negotiated_parse_into(*output, input, len_in);
  if (result < 0) {
    circpad_negotiated_free(*output);
    *output = NULL;
  }
  return result;
}