forked from satori-com/tcpkali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasn_application.c
362 lines (318 loc) · 11 KB
/
asn_application.c
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
/*
* Copyright (c) 2017 Lev Walkin <[email protected]>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <asn_application.h>
#include <errno.h>
static asn_enc_rval_t asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
enum asn_transfer_syntax syntax,
const asn_TYPE_descriptor_t *td,
const void *sptr,
asn_app_consume_bytes_f *callback,
void *callback_key);
struct callback_count_bytes_key {
asn_app_consume_bytes_f *callback;
void *callback_key;
size_t computed_size;
};
/*
* Encoder which just counts bytes that come through it.
*/
static int
callback_count_bytes_cb(const void *data, size_t size, void *keyp) {
struct callback_count_bytes_key *key = keyp;
int ret;
ret = key->callback(data, size, key->callback_key);
if(ret >= 0) {
key->computed_size += size;
}
return ret;
}
struct overrun_encoder_key {
void *buffer;
size_t buffer_size;
size_t computed_size;
};
struct callback_failure_catch_key {
asn_app_consume_bytes_f *callback;
void *callback_key;
int callback_failed;
};
/*
* Encoder which doesn't stop counting bytes
* even if it reaches the end of the buffer.
*/
static int
overrun_encoder_cb(const void *data, size_t size, void *keyp) {
struct overrun_encoder_key *key = keyp;
if(key->computed_size + size > key->buffer_size) {
/*
* Avoid accident on the next call:
* stop adding bytes to the buffer.
*/
key->buffer_size = 0;
} else {
memcpy((char *)key->buffer + key->computed_size, data, size);
}
key->computed_size += size;
return 0;
}
/*
* Encoder which help convert the application level encoder failure into EIO.
*/
static int
callback_failure_catch_cb(const void *data, size_t size, void *keyp) {
struct callback_failure_catch_key *key = keyp;
int ret;
ret = key->callback(data, size, key->callback_key);
if(ret < 0) {
key->callback_failed = 1;
}
return ret;
}
asn_enc_rval_t
asn_encode(const asn_codec_ctx_t *opt_codec_ctx,
enum asn_transfer_syntax syntax, asn_TYPE_descriptor_t *td,
void *sptr, asn_app_consume_bytes_f *callback, void *callback_key) {
struct callback_failure_catch_key cb_key;
asn_enc_rval_t er;
if(!callback) {
errno = EINVAL;
ASN__ENCODE_FAILED;
}
cb_key.callback = callback;
cb_key.callback_key = callback_key;
cb_key.callback_failed = 0;
er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
callback_failure_catch_cb, &cb_key);
if(cb_key.callback_failed) {
assert(er.encoded == -1);
assert(errno == EBADF);
errno = EIO;
}
return er;
}
asn_enc_rval_t
asn_encode_to_buffer(const asn_codec_ctx_t *opt_codec_ctx,
enum asn_transfer_syntax syntax,
const asn_TYPE_descriptor_t *td, const void *sptr,
void *buffer, size_t buffer_size) {
struct overrun_encoder_key buf_key;
asn_enc_rval_t er;
if(buffer_size > 0 && !buffer) {
errno = EINVAL;
ASN__ENCODE_FAILED;
}
buf_key.buffer = buffer;
buf_key.buffer_size = buffer_size;
buf_key.computed_size = 0;
er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
overrun_encoder_cb, &buf_key);
if(er.encoded >= 0 && (size_t)er.encoded != buf_key.computed_size) {
ASN_DEBUG("asn_encode() returned %" ASN_PRI_SSIZE " yet produced %" ASN_PRI_SIZE " bytes",
er.encoded, buf_key.computed_size);
assert(er.encoded < 0 || (size_t)er.encoded == buf_key.computed_size);
}
return er;
}
static asn_enc_rval_t
asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
enum asn_transfer_syntax syntax,
const asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *callback, void *callback_key) {
asn_enc_rval_t er;
enum xer_encoder_flags_e xer_flags = XER_F_CANONICAL;
(void)opt_codec_ctx; /* Parameters are not checked on encode yet. */
if(!td || !sptr) {
errno = EINVAL;
ASN__ENCODE_FAILED;
}
switch(syntax) {
case ATS_NONSTANDARD_PLAINTEXT:
if(td->op->print_struct) {
struct callback_count_bytes_key cb_key;
cb_key.callback = callback;
cb_key.callback_key = callback_key;
cb_key.computed_size = 0;
if(td->op->print_struct(td, sptr, 1, callback_count_bytes_cb,
&cb_key)
< 0
|| callback_count_bytes_cb("\n", 1, &cb_key) < 0) {
errno = EBADF; /* Structure has incorrect form. */
er.encoded = -1;
er.failed_type = td;
er.structure_ptr = sptr;
} else {
er.encoded = cb_key.computed_size;
er.failed_type = 0;
er.structure_ptr = 0;
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
case ATS_RANDOM:
errno = ENOENT; /* Randomization doesn't make sense on output. */
ASN__ENCODE_FAILED;
case ATS_BER:
/* BER is a superset of DER. */
/* Fall through. */
case ATS_DER:
if(td->op->der_encoder) {
er = der_encode(td, sptr, callback, callback_key);
if(er.encoded == -1) {
if(er.failed_type && er.failed_type->op->der_encoder) {
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* DER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
case ATS_CER:
errno = ENOENT; /* Transfer syntax is not defined for any type. */
ASN__ENCODE_FAILED;
#ifdef ASN_DISABLE_OER_SUPPORT
case ATS_BASIC_OER:
case ATS_CANONICAL_OER:
errno = ENOENT; /* PER is not defined. */
ASN__ENCODE_FAILED;
break;
#else /* ASN_DISABLE_OER_SUPPORT */
case ATS_BASIC_OER:
/* CANONICAL-OER is a superset of BASIC-OER. */
/* Fall through. */
case ATS_CANONICAL_OER:
if(td->op->oer_encoder) {
er = oer_encode(td, sptr, callback, callback_key);
if(er.encoded == -1) {
if(er.failed_type && er.failed_type->op->oer_encoder) {
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* OER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
#endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
case ATS_UNALIGNED_BASIC_PER:
case ATS_UNALIGNED_CANONICAL_PER:
errno = ENOENT; /* PER is not defined. */
ASN__ENCODE_FAILED;
break;
#else /* ASN_DISABLE_PER_SUPPORT */
case ATS_UNALIGNED_BASIC_PER:
/* CANONICAL-UPER is a superset of BASIC-UPER. */
/* Fall through. */
case ATS_UNALIGNED_CANONICAL_PER:
if(td->op->uper_encoder) {
er = uper_encode(td, 0, sptr, callback, callback_key);
if(er.encoded == -1) {
if(er.failed_type && er.failed_type->op->uper_encoder) {
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* UPER is not defined for this type. */
}
} else {
ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
if(er.encoded == 0) {
/* Enforce "Complete Encoding" of X.691 #11.1 */
if(callback("\0", 1, callback_key) < 0) {
errno = EBADF;
ASN__ENCODE_FAILED;
}
er.encoded = 8; /* Exactly 8 zero bits is added. */
}
/* Convert bits into bytes */
er.encoded = (er.encoded + 7) >> 3;
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
#endif /* ASN_DISABLE_PER_SUPPORT */
case ATS_BASIC_XER:
/* CANONICAL-XER is a superset of BASIC-XER. */
xer_flags &= ~XER_F_CANONICAL;
xer_flags |= XER_F_BASIC;
/* Fall through. */
case ATS_CANONICAL_XER:
if(td->op->xer_encoder) {
er = xer_encode(td, sptr, xer_flags, callback, callback_key);
if(er.encoded == -1) {
if(er.failed_type && er.failed_type->op->xer_encoder) {
errno = EBADF; /* Structure has incorrect form. */
} else {
errno = ENOENT; /* XER is not defined for this type. */
}
}
} else {
errno = ENOENT; /* Transfer syntax is not defined for this type. */
ASN__ENCODE_FAILED;
}
break;
default:
errno = ENOENT;
ASN__ENCODE_FAILED;
}
return er;
}
asn_dec_rval_t
asn_decode(const asn_codec_ctx_t *opt_codec_ctx,
enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td,
void **sptr, const void *buffer, size_t size) {
if(!td || !td->op || !sptr || (size && !buffer)) {
ASN__DECODE_FAILED;
}
switch(syntax) {
case ATS_CER:
case ATS_NONSTANDARD_PLAINTEXT:
default:
errno = ENOENT;
ASN__DECODE_FAILED;
case ATS_RANDOM:
if(!td->op->random_fill) {
ASN__DECODE_FAILED;
} else {
if(asn_random_fill(td, sptr, 16000) == 0) {
asn_dec_rval_t ret = {RC_OK, 0};
return ret;
} else {
ASN__DECODE_FAILED;
}
}
break;
case ATS_DER:
case ATS_BER:
return ber_decode(opt_codec_ctx, td, sptr, buffer, size);
case ATS_BASIC_OER:
case ATS_CANONICAL_OER:
#ifdef ASN_DISABLE_OER_SUPPORT
errno = ENOENT;
ASN__DECODE_FAILED;
#else
return oer_decode(opt_codec_ctx, td, sptr, buffer, size);
#endif
case ATS_UNALIGNED_BASIC_PER:
case ATS_UNALIGNED_CANONICAL_PER:
#ifdef ASN_DISABLE_PER_SUPPORT
errno = ENOENT;
ASN__DECODE_FAILED;
#else
return uper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
#endif
case ATS_BASIC_XER:
case ATS_CANONICAL_XER:
return xer_decode(opt_codec_ctx, td, sptr, buffer, size);
}
}