blob: a3d9e72bcb168464d89dd0ef275fb19a9337193a [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 Walkincc159472017-07-06 08:26:36 -070028#ifdef ASN_DISABLE_OER_SUPPORT
29 0,
30 0,
31#else
32 0,
33 0,
34#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040035#ifdef ASN_DISABLE_PER_SUPPORT
36 0,
37 0,
38#else
39 OCTET_STRING_decode_uper,
40 OCTET_STRING_encode_uper,
41#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000042 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000043 asn_DEF_OBJECT_IDENTIFIER_tags,
44 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
45 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
46 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
47 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
48 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070049 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000050 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000051 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000052 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000053};
54
55
Lev Walkinf15320b2004-06-03 03:38:44 +000056int
Lev Walkin5e033762004-09-29 13:26:15 +000057OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000058 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000059 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000060
61 if(st && st->buf) {
62 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070063 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000064 "%s: at least one numerical value "
65 "expected (%s:%d)",
66 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000067 return -1;
68 }
69 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070070 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000071 "%s: value not given (%s:%d)",
72 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000073 return -1;
74 }
75
76 return 0;
77}
78
Lev Walkinc4c61962004-06-14 08:17:27 +000079
Lev Walkinf15320b2004-06-03 03:38:44 +000080int
Wim Lewis14e6b162014-07-23 16:06:01 -070081OBJECT_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 +000082 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070083 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000084 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000085 unsigned char *rvbuf = (unsigned char *)rvbufp;
86 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000087 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000088
Lev Walkin29a044b2004-06-14 07:24:36 +000089 rvsize *= CHAR_BIT; /* bytes to bits */
90 arclen *= 7; /* bytes to bits */
91
92 /*
93 * The arc has the number of bits
94 * cannot be represented using supplied return value type.
95 */
96 if(arclen > rvsize) {
97 if(arclen > (rvsize + CHAR_BIT)) {
98 errno = ERANGE; /* Overflow */
99 return -1;
100 } else {
101 /*
102 * Even if the number of bits in the arc representation
103 * is higher than the width of supplied * return value
104 * type, there is still possible to fit it when there
105 * are few unused high bits in the arc value
106 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000107 *
108 * Moreover, there is a possibility that the
109 * number could actually fit the arc space, given
110 * that add is negative, but we don't handle
111 * such "temporary lack of precision" situation here.
112 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000113 */
114 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
115 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 errno = ERANGE; /* Overflow */
117 return -1;
118 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000119 /* Fool the routine computing unused bits */
120 arclen -= 7;
121 cache = *arcbuf & 0x7f;
122 arcbuf++;
123 }
124 }
125
Lev Walkinc4c61962004-06-14 08:17:27 +0000126 /* Faster path for common size */
127 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
128 unsigned long accum;
129 /* Gather all bits into the accumulator */
130 for(accum = cache; arcbuf < arcend; arcbuf++)
131 accum = (accum << 7) | (*arcbuf & ~0x80);
132 if(accum < (unsigned)-add) {
133 errno = ERANGE; /* Overflow */
134 return -1;
135 }
Lev Walkin70732a22013-12-07 10:55:14 -0800136 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000137 return 0;
138 }
139
Lev Walkin29a044b2004-06-14 07:24:36 +0000140#ifndef WORDS_BIGENDIAN
141 if(*(unsigned char *)&LE) { /* Little endian (x86) */
142 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000143 rvbuf += rvsize / CHAR_BIT - 1;
144 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000145 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000146 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000147#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000148 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000149
Lev Walkinc4c61962004-06-14 08:17:27 +0000150 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000151 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000152
Lev Walkin29a044b2004-06-14 07:24:36 +0000153 /* Clear the high unused bits */
154 for(bits = rvsize - arclen;
155 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000156 rvbuf += inc, bits -= CHAR_BIT)
157 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000158
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 /* Fill the body of a value */
160 for(; arcbuf < arcend; arcbuf++) {
161 cache = (cache << 7) | (*arcbuf & 0x7f);
162 bits += 7;
163 if(bits >= CHAR_BIT) {
164 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 *rvbuf = (cache >> bits);
166 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 }
168 }
169 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000170 *rvbuf = cache;
171 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000172 }
173 }
174
175 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000176 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
177 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700178 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000179 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000180 add = -1;
181 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000182 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000183 break;
184 }
185 }
186 if(rvbuf == rvstart) {
187 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 errno = ERANGE; /* Overflow */
189 return -1;
190 }
191 }
192
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 return 0;
194}
195
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700197OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 asn_app_consume_bytes_f *cb, void *app_key) {
199 char scratch[64]; /* Conservative estimate */
200 unsigned long accum; /* Bits accumulator */
201 char *p; /* Position in the scratch buffer */
202
Lev Walkin29a044b2004-06-14 07:24:36 +0000203 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
204 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 return -1;
206
Lev Walkin3251b8e2004-08-23 09:23:02 +0000207 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 ssize_t len;
209
Lev Walkin3251b8e2004-08-23 09:23:02 +0000210 /* Fill the scratch buffer in reverse. */
211 p = scratch + sizeof(scratch);
212 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000214
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215 len = sizeof(scratch) - (p - scratch);
216 if(cb(p, len, app_key) < 0)
217 return -1;
218 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000219 } else {
220 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 if(cb(scratch, 1, app_key) < 0)
222 return -1;
223 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000224 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000225}
226
227int
Wim Lewis14e6b162014-07-23 16:06:01 -0700228OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000229 asn_app_consume_bytes_f *cb, void *app_key) {
230
231 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
232 return -1;
233
234 return 0;
235}
236
237static ssize_t
238OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
239 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700240 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000241 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700242 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 for(i = 0, startn = 0; i < st->size; i++) {
245 uint8_t b = st->buf[i];
246 if((b & 0x80)) /* Continuation expected */
247 continue;
248
249 if(startn == 0) {
250 /*
251 * First two arcs are encoded through the backdoor.
252 */
253 if(i) {
254 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 } else if(b <= 39) {
257 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 } else if(b < 79) {
260 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 } else {
263 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000266 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 }
268
Lev Walkina9cc46e2004-09-22 16:06:28 +0000269 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 return -1;
271
Lev Walkina9cc46e2004-09-22 16:06:28 +0000272 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
273 i - startn + 1, add, cb, app_key);
274 if(add < 0) return -1;
275 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 startn = i + 1;
277 add = 0;
278 }
279
Lev Walkina9cc46e2004-09-22 16:06:28 +0000280 return wrote_len;
281}
282
Lev Walkin0fab1a62005-03-09 22:19:25 +0000283static enum xer_pbd_rval
284OBJECT_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 +0000285 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000286 const char *chunk_end = (const char *)chunk_buf + chunk_size;
287 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000288 long s_arcs[10];
289 long *arcs = s_arcs;
290 int arcs_count;
291 int ret;
292
Lev Walkine0b56e02005-02-25 12:10:27 +0000293 (void)td;
294
Lev Walkin92302252004-10-23 10:16:51 +0000295 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000296 (const char *)chunk_buf, chunk_size, arcs,
297 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700298 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000299 /* Expecting more than zero arcs */
300 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700301 } else if(arcs_count == 0) {
302 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000303 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700304 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000305
306 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000307 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000308 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000309 ret = OBJECT_IDENTIFIER_parse_arcs(
310 (const char *)chunk_buf, chunk_size,
311 arcs, arcs_count, &endptr);
312 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000313 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000314 }
315
316 /*
317 * Convert arcs into BER representation.
318 */
319 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000320 if(arcs != s_arcs) FREEMEM(arcs);
321
Lev Walkin2c34aad2005-03-10 11:50:24 +0000322 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000323}
324
325asn_dec_rval_t
326OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
327 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000328 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000329
330 return xer_decode_primitive(opt_codec_ctx, td,
331 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
332 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
333}
334
Lev Walkina9cc46e2004-09-22 16:06:28 +0000335asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000336OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000337 int ilevel, enum xer_encoder_flags_e flags,
338 asn_app_consume_bytes_f *cb, void *app_key) {
339 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
340 asn_enc_rval_t er;
341
342 (void)ilevel;
343 (void)flags;
344
345 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700346 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000347
348 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700349 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000350
Lev Walkin7c1dc052016-03-14 03:08:15 -0700351 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352}
353
354int
Lev Walkin5e033762004-09-29 13:26:15 +0000355OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
357 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
358
359 (void)td; /* Unused argument */
360 (void)ilevel; /* Unused argument */
361
362 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000363 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000364
365 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000366 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000367 return -1;
368
369 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
370 return -1;
371
Lev Walkin8e8078a2004-09-26 13:10:40 +0000372 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000373}
374
375int
Wim Lewis14e6b162014-07-23 16:06:01 -0700376OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000377 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000378 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000379 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 int startn = 0;
381 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700382 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000383
Lev Walkin0787ff02004-06-17 23:43:39 +0000384 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 errno = EINVAL;
386 return -1;
387 }
388
389 for(i = 0; i < oid->size; i++) {
390 uint8_t b = oid->buf[i];
391 if((b & 0x80)) /* Continuation expected */
392 continue;
393
Lev Walkin29a044b2004-06-14 07:24:36 +0000394 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000395 /*
396 * First two arcs are encoded through the backdoor.
397 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000398 unsigned LE = 1; /* Little endian */
399 int first_arc;
400 num_arcs++;
401 if(!arc_slots) { num_arcs++; continue; }
402
403 if(i) first_arc = 2;
404 else if(b <= 39) first_arc = 0;
405 else if(b < 79) first_arc = 1;
406 else first_arc = 2;
407
408 add = -40 * first_arc;
409 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000410 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000411 + ((*(char *)&LE)?0:(arc_type_size - 1)))
412 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000413 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 }
415
Lev Walkin29a044b2004-06-14 07:24:36 +0000416 /* Decode, if has space */
417 if(arcs < arcs_end) {
418 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
419 i - startn + 1, add,
420 arcs, arc_type_size))
421 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000422 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000423 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000424 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000426 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 }
428
Lev Walkin29a044b2004-06-14 07:24:36 +0000429 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430}
431
Lev Walkin0787ff02004-06-17 23:43:39 +0000432
433/*
434 * Save the single value as an object identifier arc.
435 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000436int
Lev Walkinb1a15552005-12-22 21:39:44 +0000437OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000438 /*
439 * The following conditions must hold:
440 * assert(arcval);
441 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000442 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000443 * assert(arcbuf);
444 */
445#ifdef WORDS_BIGENDIAN
446 const unsigned isLittleEndian = 0;
447#else
448 unsigned LE = 1;
449 unsigned isLittleEndian = *(char *)&LE;
450#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000451 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000452 unsigned int cache;
453 uint8_t *bp = arcbuf;
454 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000455 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000456
457 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000458 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
459 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000460 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000461 uint8_t *tb;
462 for(tb = buffer; a >= aend; tb++, a--)
463 if((*tb = *a) && (tb < msb))
464 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000465 tend = &buffer[arcval_size];
466 tp = msb; /* Most significant non-zero byte */
467 } else {
468 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000469 tend = (const unsigned char *)arcval + arcval_size;
470 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000471 if(*tp) break;
472 }
473
474 /*
475 * Split the value in 7-bits chunks.
476 */
477 bits = ((tend - tp) * CHAR_BIT) % 7;
478 if(bits) {
479 cache = *tp >> (CHAR_BIT - bits);
480 if(cache) {
481 *bp++ = cache | 0x80;
482 cache = *tp++;
483 bits = CHAR_BIT - bits;
484 } else {
485 bits = -bits;
486 }
487 } else {
488 cache = 0;
489 }
490 for(; tp < tend; tp++) {
491 cache = (cache << CHAR_BIT) + *tp;
492 bits += CHAR_BIT;
493 while(bits >= 7) {
494 bits -= 7;
495 *bp++ = 0x80 | (cache >> bits);
496 }
497 }
498 if(bits) *bp++ = cache;
499 bp[-1] &= 0x7f; /* Clear the last bit */
500
501 return bp - arcbuf;
502}
503
Lev Walkinf15320b2004-06-03 03:38:44 +0000504int
Lev Walkinb1a15552005-12-22 21:39:44 +0000505OBJECT_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 +0000506 uint8_t *buf;
507 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000508 unsigned LE = 1; /* Little endian (x86) */
509 unsigned isLittleEndian = *((char *)&LE);
510 unsigned int arc0;
511 unsigned int arc1;
512 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000513 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000514
Lev Walkin523de9e2006-08-18 01:34:18 +0000515 if(!oid || !arcs || arc_type_size < 1
516 || arc_type_size > 16
517 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 errno = EINVAL;
519 return -1;
520 }
521
Lev Walkin0787ff02004-06-17 23:43:39 +0000522 switch(arc_type_size) {
523 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000524 arc0 = ((const unsigned char *)arcs)[0];
525 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000526 break;
527 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000528 arc0 = ((const unsigned short *)arcs)[0];
529 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000530 break;
531 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000532 arc0 = ((const unsigned int *)arcs)[0];
533 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000534 break;
535 default:
536 arc1 = arc0 = 0;
537 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000538 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000539 /* If more significant bytes are present,
540 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000541 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000542 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000543 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
544 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000545 arc0 = *((const unsigned char *)arcs + 0);
546 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000547 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000548 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000549 /* If more significant bytes are present,
550 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000551 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000552 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000553 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
554 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000555 }
556 }
557
558 /*
559 * The previous chapter left us with the first and the second arcs.
560 * The values are not precise (that is, they are valid only if
561 * they're less than 255), but OK for the purposes of making
562 * the sanity test below.
563 */
564 if(arc0 <= 1) {
565 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000566 /* 8.19.4: At most 39 subsequent values (including 0) */
567 errno = ERANGE;
568 return -1;
569 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000570 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 /* 8.19.4: Only three values are allocated from the root node */
572 errno = ERANGE;
573 return -1;
574 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000575 /*
576 * After above tests it is known that the value of arc0 is completely
577 * trustworthy (0..2). However, the arc1's value is still meaningless.
578 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000579
580 /*
581 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000582 * This estimation implicitly takes in account the following facts,
583 * that cancel each other:
584 * * the first two arcs are encoded in a single value.
585 * * the first value may require more space (+1 byte)
586 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000588 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000589 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 if(!buf) {
591 /* ENOMEM */
592 return -1;
593 }
594
595 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000596 * Encode the first two arcs.
597 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000599 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000601 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000602 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000603
Lev Walkin0787ff02004-06-17 23:43:39 +0000604 /*
605 * Simulate first_value = arc0 * 40 + arc1;
606 */
607 /* Copy the second (1'st) arcs[1] into the first_value */
608 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000609 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000610 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000611 const uint8_t *aend = (const unsigned char *)arcs - 1;
612 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000613 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000614 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000615 const uint8_t *a1 = (const uint8_t *)arcs;
616 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000617 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000618 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000619 /* Increase the first_value by arc0 */
620 arc0 *= 40; /* (0..80) */
621 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
622 unsigned int v = *tp;
623 v += arc0;
624 *tp = v;
625 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
626 else break;
627 }
628
629 assert(tp >= first_value);
630
631 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
632 fv - first_value, 1);
633 }
634
635 /*
636 * Save the rest of arcs.
637 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000638 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000639 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000640 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000641 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
642 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 }
644
Lev Walkin34b2a932004-06-17 23:46:45 +0000645 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000646
647 /*
648 * Replace buffer.
649 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000650 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000651 bp = oid->buf;
652 oid->buf = buf;
653 if(bp) FREEMEM(bp);
654
655 return 0;
656}
Lev Walkinc4c61962004-06-14 08:17:27 +0000657
Lev Walkin92302252004-10-23 10:16:51 +0000658
659int
660OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000661 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000662 unsigned int arcs_count = 0;
663 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000664 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700665 ST_LEADSPACE,
666 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700667 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800668 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700669 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000670
671 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000672 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000673 errno = EINVAL;
674 return -1;
675 }
676
677 if(oid_txt_length == -1)
678 oid_txt_length = strlen(oid_text);
679
Lev Walkincad560a2013-03-16 07:00:58 -0700680#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
681 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700682 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700683 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700684 case ASN_STRTOX_EXTRA_DATA: \
685 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700686 if(arcs_count < arcs_slots) \
687 arcs[arcs_count] = value; \
688 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700689 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700690 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700691 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700692 if(opt_oid_text_end) \
693 *opt_oid_text_end = oid_text; \
694 errno = ERANGE; \
695 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700696 case ASN_STRTOX_ERROR_INVAL: \
697 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700698 if(opt_oid_text_end) \
699 *opt_oid_text_end = oid_text; \
700 errno = EINVAL; \
701 return -1; \
702 } \
703 } while(0)
704
Lev Walkin92302252004-10-23 10:16:51 +0000705 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
706 switch(*oid_text) {
707 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700708 switch(state) {
709 case ST_LEADSPACE:
710 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000711 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700712 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700713 state = ST_TAILSPACE;
714 continue;
715 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700716 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000717 }
Lev Walkincad560a2013-03-16 07:00:58 -0700718 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700719 case 0x2e: /* '.' */
720 switch(state) {
721 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700722 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700723 case ST_WAITDIGITS:
724 if(opt_oid_text_end)
725 *opt_oid_text_end = oid_text;
726 errno = EINVAL; /* Broken OID */
727 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700728 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700729 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700730 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000731 continue;
732 }
Lev Walkinf5927112012-09-03 00:48:45 -0700733 break;
734 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
735 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
736 switch(state) {
737 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700738 case ST_AFTERVALUE:
739 if(opt_oid_text_end)
740 *opt_oid_text_end = oid_text;
741 errno = EINVAL; /* "1. 1" => broken OID */
742 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700743 case ST_LEADSPACE:
744 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700745 _OID_CAPTURE_ARC(oid_text, oid_end);
746 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700747 continue;
748 }
749 break;
Lev Walkin92302252004-10-23 10:16:51 +0000750 default:
751 /* Unexpected symbols */
752 state = ST_WAITDIGITS;
753 break;
754 } /* switch() */
755 break;
756 } /* for() */
757
758
Lev Walkin093ba2e2005-04-04 21:10:06 +0000759 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000760
761 /* Finalize last arc */
762 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700763 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700764 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700765 case ST_WAITDIGITS:
766 errno = EINVAL; /* Broken OID */
767 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700768 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700769 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000770 return arcs_count;
771 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700772
773 errno = EINVAL; /* Broken OID */
774 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000775}
776
777