blob: 24f4e0e85816815a8cd384481dd418b6318ad0bb [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf5927112012-09-03 00:48:45 -07006#include <INTEGER.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OBJECT_IDENTIFIER.h>
Lev Walkin725883b2006-10-09 12:07:58 +00008#include <OCTET_STRING.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00009#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +000010#include <errno.h>
11
12/*
13 * OBJECT IDENTIFIER basic type description.
14 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070015static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
17};
Lev Walkin5e033762004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 "OBJECT IDENTIFIER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 "OBJECT_IDENTIFIER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
25 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000026 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 OBJECT_IDENTIFIER_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000028 OCTET_STRING_decode_uper,
29 OCTET_STRING_encode_uper,
Harald Welte498c9712015-08-30 16:33:07 +020030 OCTET_STRING_decode_aper,
31 OCTET_STRING_encode_aper,
Lev Walkinf15320b2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000033 asn_DEF_OBJECT_IDENTIFIER_tags,
34 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
35 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
36 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
37 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
38 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000040 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000041 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000042};
43
44
Lev Walkinf15320b2004-06-03 03:38:44 +000045int
Lev Walkin5e033762004-09-29 13:26:15 +000046OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000047 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000048 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000049
50 if(st && st->buf) {
51 if(st->size < 1) {
Lev Walkined9019a2006-10-16 12:18:41 +000052 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000053 "%s: at least one numerical value "
54 "expected (%s:%d)",
55 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000056 return -1;
57 }
58 } else {
Lev Walkined9019a2006-10-16 12:18:41 +000059 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000060 "%s: value not given (%s:%d)",
61 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000062 return -1;
63 }
64
65 return 0;
66}
67
Lev Walkinc4c61962004-06-14 08:17:27 +000068
Lev Walkinf15320b2004-06-03 03:38:44 +000069int
Wim Lewis14e6b162014-07-23 16:06:01 -070070OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
Lev Walkin50b0f6c2006-10-16 12:01:36 +000071 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070072 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000073 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000074 unsigned char *rvbuf = (unsigned char *)rvbufp;
75 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000076 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000077
Lev Walkin29a044b2004-06-14 07:24:36 +000078 rvsize *= CHAR_BIT; /* bytes to bits */
79 arclen *= 7; /* bytes to bits */
80
81 /*
82 * The arc has the number of bits
83 * cannot be represented using supplied return value type.
84 */
85 if(arclen > rvsize) {
86 if(arclen > (rvsize + CHAR_BIT)) {
87 errno = ERANGE; /* Overflow */
88 return -1;
89 } else {
90 /*
91 * Even if the number of bits in the arc representation
92 * is higher than the width of supplied * return value
93 * type, there is still possible to fit it when there
94 * are few unused high bits in the arc value
95 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000096 *
97 * Moreover, there is a possibility that the
98 * number could actually fit the arc space, given
99 * that add is negative, but we don't handle
100 * such "temporary lack of precision" situation here.
101 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000102 */
103 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
104 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 errno = ERANGE; /* Overflow */
106 return -1;
107 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000108 /* Fool the routine computing unused bits */
109 arclen -= 7;
110 cache = *arcbuf & 0x7f;
111 arcbuf++;
112 }
113 }
114
Lev Walkinc4c61962004-06-14 08:17:27 +0000115 /* Faster path for common size */
116 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
117 unsigned long accum;
118 /* Gather all bits into the accumulator */
119 for(accum = cache; arcbuf < arcend; arcbuf++)
120 accum = (accum << 7) | (*arcbuf & ~0x80);
121 if(accum < (unsigned)-add) {
122 errno = ERANGE; /* Overflow */
123 return -1;
124 }
Lev Walkin70732a22013-12-07 10:55:14 -0800125 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000126 return 0;
127 }
128
Lev Walkin29a044b2004-06-14 07:24:36 +0000129#ifndef WORDS_BIGENDIAN
130 if(*(unsigned char *)&LE) { /* Little endian (x86) */
131 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000132 rvbuf += rvsize / CHAR_BIT - 1;
133 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000134 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000135 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000136#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000137 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000138
Lev Walkinc4c61962004-06-14 08:17:27 +0000139 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000140 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000141
Lev Walkin29a044b2004-06-14 07:24:36 +0000142 /* Clear the high unused bits */
143 for(bits = rvsize - arclen;
144 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000145 rvbuf += inc, bits -= CHAR_BIT)
146 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000147
Lev Walkin29a044b2004-06-14 07:24:36 +0000148 /* Fill the body of a value */
149 for(; arcbuf < arcend; arcbuf++) {
150 cache = (cache << 7) | (*arcbuf & 0x7f);
151 bits += 7;
152 if(bits >= CHAR_BIT) {
153 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000154 *rvbuf = (cache >> bits);
155 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000156 }
157 }
158 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000159 *rvbuf = cache;
160 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000161 }
162 }
163
164 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
166 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000168 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000169 add = -1;
170 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000171 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000172 break;
173 }
174 }
175 if(rvbuf == rvstart) {
176 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 errno = ERANGE; /* Overflow */
178 return -1;
179 }
180 }
181
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 return 0;
183}
184
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700186OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 asn_app_consume_bytes_f *cb, void *app_key) {
188 char scratch[64]; /* Conservative estimate */
189 unsigned long accum; /* Bits accumulator */
190 char *p; /* Position in the scratch buffer */
191
Lev Walkin29a044b2004-06-14 07:24:36 +0000192 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
193 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 return -1;
195
Lev Walkin3251b8e2004-08-23 09:23:02 +0000196 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 ssize_t len;
198
Lev Walkin3251b8e2004-08-23 09:23:02 +0000199 /* Fill the scratch buffer in reverse. */
200 p = scratch + sizeof(scratch);
201 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000203
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204 len = sizeof(scratch) - (p - scratch);
205 if(cb(p, len, app_key) < 0)
206 return -1;
207 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000208 } else {
209 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 if(cb(scratch, 1, app_key) < 0)
211 return -1;
212 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000213 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000214}
215
216int
Wim Lewis14e6b162014-07-23 16:06:01 -0700217OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218 asn_app_consume_bytes_f *cb, void *app_key) {
219
220 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
221 return -1;
222
223 return 0;
224}
225
226static ssize_t
227OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
228 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 int startn;
230 int add = 0;
231 int i;
232
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 for(i = 0, startn = 0; i < st->size; i++) {
234 uint8_t b = st->buf[i];
235 if((b & 0x80)) /* Continuation expected */
236 continue;
237
238 if(startn == 0) {
239 /*
240 * First two arcs are encoded through the backdoor.
241 */
242 if(i) {
243 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 } else if(b <= 39) {
246 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 } else if(b < 79) {
249 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 } else {
252 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 }
257
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 return -1;
260
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
262 i - startn + 1, add, cb, app_key);
263 if(add < 0) return -1;
264 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 startn = i + 1;
266 add = 0;
267 }
268
Lev Walkina9cc46e2004-09-22 16:06:28 +0000269 return wrote_len;
270}
271
Lev Walkin0fab1a62005-03-09 22:19:25 +0000272static enum xer_pbd_rval
273OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin92302252004-10-23 10:16:51 +0000274 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000275 const char *chunk_end = (const char *)chunk_buf + chunk_size;
276 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000277 long s_arcs[10];
278 long *arcs = s_arcs;
279 int arcs_count;
280 int ret;
281
Lev Walkine0b56e02005-02-25 12:10:27 +0000282 (void)td;
283
Lev Walkin92302252004-10-23 10:16:51 +0000284 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000285 (const char *)chunk_buf, chunk_size, arcs,
286 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700287 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000288 /* Expecting more than zero arcs */
289 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700290 } else if(arcs_count == 0) {
291 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000292 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700293 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000294
295 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000296 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000297 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000298 ret = OBJECT_IDENTIFIER_parse_arcs(
299 (const char *)chunk_buf, chunk_size,
300 arcs, arcs_count, &endptr);
301 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000302 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000303 }
304
305 /*
306 * Convert arcs into BER representation.
307 */
308 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000309 if(arcs != s_arcs) FREEMEM(arcs);
310
Lev Walkin2c34aad2005-03-10 11:50:24 +0000311 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000312}
313
314asn_dec_rval_t
315OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
316 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000317 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000318
319 return xer_decode_primitive(opt_codec_ctx, td,
320 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
321 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
322}
323
Lev Walkina9cc46e2004-09-22 16:06:28 +0000324asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000325OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000326 int ilevel, enum xer_encoder_flags_e flags,
327 asn_app_consume_bytes_f *cb, void *app_key) {
328 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
329 asn_enc_rval_t er;
330
331 (void)ilevel;
332 (void)flags;
333
334 if(!st || !st->buf)
335 _ASN_ENCODE_FAILED;
336
337 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
338 if(er.encoded < 0) _ASN_ENCODE_FAILED;
339
Lev Walkin59b176e2005-11-26 11:25:14 +0000340 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000341}
342
343int
Lev Walkin5e033762004-09-29 13:26:15 +0000344OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000345 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
346 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
347
348 (void)td; /* Unused argument */
349 (void)ilevel; /* Unused argument */
350
351 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000352 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353
354 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000355 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356 return -1;
357
358 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
359 return -1;
360
Lev Walkin8e8078a2004-09-26 13:10:40 +0000361 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000362}
363
364int
Wim Lewis14e6b162014-07-23 16:06:01 -0700365OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000366 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000367 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000368 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 int startn = 0;
370 int add = 0;
371 int i;
372
Lev Walkin0787ff02004-06-17 23:43:39 +0000373 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 errno = EINVAL;
375 return -1;
376 }
377
378 for(i = 0; i < oid->size; i++) {
379 uint8_t b = oid->buf[i];
380 if((b & 0x80)) /* Continuation expected */
381 continue;
382
Lev Walkin29a044b2004-06-14 07:24:36 +0000383 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 /*
385 * First two arcs are encoded through the backdoor.
386 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000387 unsigned LE = 1; /* Little endian */
388 int first_arc;
389 num_arcs++;
390 if(!arc_slots) { num_arcs++; continue; }
391
392 if(i) first_arc = 2;
393 else if(b <= 39) first_arc = 0;
394 else if(b < 79) first_arc = 1;
395 else first_arc = 2;
396
397 add = -40 * first_arc;
398 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000399 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000400 + ((*(char *)&LE)?0:(arc_type_size - 1)))
401 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000402 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 }
404
Lev Walkin29a044b2004-06-14 07:24:36 +0000405 /* Decode, if has space */
406 if(arcs < arcs_end) {
407 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
408 i - startn + 1, add,
409 arcs, arc_type_size))
410 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000412 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000413 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000415 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 }
417
Lev Walkin29a044b2004-06-14 07:24:36 +0000418 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000419}
420
Lev Walkin0787ff02004-06-17 23:43:39 +0000421
422/*
423 * Save the single value as an object identifier arc.
424 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000425int
Lev Walkinb1a15552005-12-22 21:39:44 +0000426OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000427 /*
428 * The following conditions must hold:
429 * assert(arcval);
430 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000431 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000432 * assert(arcbuf);
433 */
434#ifdef WORDS_BIGENDIAN
435 const unsigned isLittleEndian = 0;
436#else
437 unsigned LE = 1;
438 unsigned isLittleEndian = *(char *)&LE;
439#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000440 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000441 unsigned int cache;
442 uint8_t *bp = arcbuf;
443 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000444 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000445
446 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000447 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
448 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000449 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000450 uint8_t *tb;
451 for(tb = buffer; a >= aend; tb++, a--)
452 if((*tb = *a) && (tb < msb))
453 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000454 tend = &buffer[arcval_size];
455 tp = msb; /* Most significant non-zero byte */
456 } else {
457 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000458 tend = (const unsigned char *)arcval + arcval_size;
459 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000460 if(*tp) break;
461 }
462
463 /*
464 * Split the value in 7-bits chunks.
465 */
466 bits = ((tend - tp) * CHAR_BIT) % 7;
467 if(bits) {
468 cache = *tp >> (CHAR_BIT - bits);
469 if(cache) {
470 *bp++ = cache | 0x80;
471 cache = *tp++;
472 bits = CHAR_BIT - bits;
473 } else {
474 bits = -bits;
475 }
476 } else {
477 cache = 0;
478 }
479 for(; tp < tend; tp++) {
480 cache = (cache << CHAR_BIT) + *tp;
481 bits += CHAR_BIT;
482 while(bits >= 7) {
483 bits -= 7;
484 *bp++ = 0x80 | (cache >> bits);
485 }
486 }
487 if(bits) *bp++ = cache;
488 bp[-1] &= 0x7f; /* Clear the last bit */
489
490 return bp - arcbuf;
491}
492
Lev Walkinf15320b2004-06-03 03:38:44 +0000493int
Lev Walkinb1a15552005-12-22 21:39:44 +0000494OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000495 uint8_t *buf;
496 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000497 unsigned LE = 1; /* Little endian (x86) */
498 unsigned isLittleEndian = *((char *)&LE);
499 unsigned int arc0;
500 unsigned int arc1;
501 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000502 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000503
Lev Walkin523de9e2006-08-18 01:34:18 +0000504 if(!oid || !arcs || arc_type_size < 1
505 || arc_type_size > 16
506 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 errno = EINVAL;
508 return -1;
509 }
510
Lev Walkin0787ff02004-06-17 23:43:39 +0000511 switch(arc_type_size) {
512 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000513 arc0 = ((const unsigned char *)arcs)[0];
514 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 break;
516 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000517 arc0 = ((const unsigned short *)arcs)[0];
518 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000519 break;
520 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000521 arc0 = ((const unsigned int *)arcs)[0];
522 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000523 break;
524 default:
525 arc1 = arc0 = 0;
526 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000527 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000528 /* If more significant bytes are present,
529 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000530 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000531 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000532 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
533 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000534 arc0 = *((const unsigned char *)arcs + 0);
535 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000536 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000537 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000538 /* If more significant bytes are present,
539 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000540 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000541 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000542 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
543 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 }
545 }
546
547 /*
548 * The previous chapter left us with the first and the second arcs.
549 * The values are not precise (that is, they are valid only if
550 * they're less than 255), but OK for the purposes of making
551 * the sanity test below.
552 */
553 if(arc0 <= 1) {
554 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 /* 8.19.4: At most 39 subsequent values (including 0) */
556 errno = ERANGE;
557 return -1;
558 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000559 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 /* 8.19.4: Only three values are allocated from the root node */
561 errno = ERANGE;
562 return -1;
563 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000564 /*
565 * After above tests it is known that the value of arc0 is completely
566 * trustworthy (0..2). However, the arc1's value is still meaningless.
567 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000568
569 /*
570 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000571 * This estimation implicitly takes in account the following facts,
572 * that cancel each other:
573 * * the first two arcs are encoded in a single value.
574 * * the first value may require more space (+1 byte)
575 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000577 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000578 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 if(!buf) {
580 /* ENOMEM */
581 return -1;
582 }
583
584 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000585 * Encode the first two arcs.
586 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000589 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000590 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000591 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000592
Lev Walkin0787ff02004-06-17 23:43:39 +0000593 /*
594 * Simulate first_value = arc0 * 40 + arc1;
595 */
596 /* Copy the second (1'st) arcs[1] into the first_value */
597 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000598 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000599 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000600 const uint8_t *aend = (const unsigned char *)arcs - 1;
601 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000602 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000604 const uint8_t *a1 = (const uint8_t *)arcs;
605 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000607 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000608 /* Increase the first_value by arc0 */
609 arc0 *= 40; /* (0..80) */
610 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
611 unsigned int v = *tp;
612 v += arc0;
613 *tp = v;
614 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
615 else break;
616 }
617
618 assert(tp >= first_value);
619
620 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
621 fv - first_value, 1);
622 }
623
624 /*
625 * Save the rest of arcs.
626 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000627 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000628 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000629 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000630 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
631 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 }
633
Lev Walkin34b2a932004-06-17 23:46:45 +0000634 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000635
636 /*
637 * Replace buffer.
638 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000639 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 bp = oid->buf;
641 oid->buf = buf;
642 if(bp) FREEMEM(bp);
643
644 return 0;
645}
Lev Walkinc4c61962004-06-14 08:17:27 +0000646
Lev Walkin92302252004-10-23 10:16:51 +0000647
648int
649OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000650 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000651 unsigned int arcs_count = 0;
652 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000653 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700654 ST_LEADSPACE,
655 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700656 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin92302252004-10-23 10:16:51 +0000657 ST_WAITDIGITS, /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700658 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000659
660 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000661 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000662 errno = EINVAL;
663 return -1;
664 }
665
666 if(oid_txt_length == -1)
667 oid_txt_length = strlen(oid_text);
668
Lev Walkincad560a2013-03-16 07:00:58 -0700669#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
670 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700671 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700672 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
673 case ASN_STRTOL_EXTRA_DATA: \
Lev Walkinf5927112012-09-03 00:48:45 -0700674 case ASN_STRTOL_OK: \
675 if(arcs_count < arcs_slots) \
676 arcs[arcs_count] = value; \
677 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700678 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700679 break; \
680 case ASN_STRTOL_ERROR_RANGE: \
681 if(opt_oid_text_end) \
682 *opt_oid_text_end = oid_text; \
683 errno = ERANGE; \
684 return -1; \
685 case ASN_STRTOL_ERROR_INVAL: \
Lev Walkincad560a2013-03-16 07:00:58 -0700686 case ASN_STRTOL_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700687 if(opt_oid_text_end) \
688 *opt_oid_text_end = oid_text; \
689 errno = EINVAL; \
690 return -1; \
691 } \
692 } while(0)
693
Lev Walkin92302252004-10-23 10:16:51 +0000694 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
695 switch(*oid_text) {
696 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700697 switch(state) {
698 case ST_LEADSPACE:
699 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000700 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700701 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700702 state = ST_TAILSPACE;
703 continue;
704 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700705 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000706 }
Lev Walkincad560a2013-03-16 07:00:58 -0700707 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700708 case 0x2e: /* '.' */
709 switch(state) {
710 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700711 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700712 case ST_WAITDIGITS:
713 if(opt_oid_text_end)
714 *opt_oid_text_end = oid_text;
715 errno = EINVAL; /* Broken OID */
716 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700717 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700718 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700719 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000720 continue;
721 }
Lev Walkinf5927112012-09-03 00:48:45 -0700722 break;
723 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
724 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
725 switch(state) {
726 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700727 case ST_AFTERVALUE:
728 if(opt_oid_text_end)
729 *opt_oid_text_end = oid_text;
730 errno = EINVAL; /* "1. 1" => broken OID */
731 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700732 case ST_LEADSPACE:
733 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700734 _OID_CAPTURE_ARC(oid_text, oid_end);
735 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700736 continue;
737 }
738 break;
Lev Walkin92302252004-10-23 10:16:51 +0000739 default:
740 /* Unexpected symbols */
741 state = ST_WAITDIGITS;
742 break;
743 } /* switch() */
744 break;
745 } /* for() */
746
747
Lev Walkin093ba2e2005-04-04 21:10:06 +0000748 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000749
750 /* Finalize last arc */
751 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700752 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700753 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700754 case ST_WAITDIGITS:
755 errno = EINVAL; /* Broken OID */
756 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700757 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700758 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000759 return arcs_count;
760 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700761
762 errno = EINVAL; /* Broken OID */
763 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000764}
765
766