blob: bdc6b5c7f3b99fff1f604b47481e3280a8a745a6 [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 Walkin80334ed2017-07-06 07:28:21 -070028#ifdef ASN_DISABLE_PER_SUPPORT
29 0,
30 0,
31#else
Lev Walkin725883b2006-10-09 12:07:58 +000032 OCTET_STRING_decode_uper,
33 OCTET_STRING_encode_uper,
Lev Walkin80334ed2017-07-06 07:28:21 -070034#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000035 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000036 asn_DEF_OBJECT_IDENTIFIER_tags,
37 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
38 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
39 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
40 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
41 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000042 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000043 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000044 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000045};
46
47
Lev Walkinf15320b2004-06-03 03:38:44 +000048int
Lev Walkin5e033762004-09-29 13:26:15 +000049OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000050 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000051 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000052
53 if(st && st->buf) {
54 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070055 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000056 "%s: at least one numerical value "
57 "expected (%s:%d)",
58 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000059 return -1;
60 }
61 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070062 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000063 "%s: value not given (%s:%d)",
64 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000065 return -1;
66 }
67
68 return 0;
69}
70
Lev Walkinc4c61962004-06-14 08:17:27 +000071
Lev Walkinf15320b2004-06-03 03:38:44 +000072int
Wim Lewis14e6b162014-07-23 16:06:01 -070073OBJECT_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 +000074 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070075 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000076 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000077 unsigned char *rvbuf = (unsigned char *)rvbufp;
78 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000079 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000080
Lev Walkin29a044b2004-06-14 07:24:36 +000081 rvsize *= CHAR_BIT; /* bytes to bits */
82 arclen *= 7; /* bytes to bits */
83
84 /*
85 * The arc has the number of bits
86 * cannot be represented using supplied return value type.
87 */
88 if(arclen > rvsize) {
89 if(arclen > (rvsize + CHAR_BIT)) {
90 errno = ERANGE; /* Overflow */
91 return -1;
92 } else {
93 /*
94 * Even if the number of bits in the arc representation
95 * is higher than the width of supplied * return value
96 * type, there is still possible to fit it when there
97 * are few unused high bits in the arc value
98 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000099 *
100 * Moreover, there is a possibility that the
101 * number could actually fit the arc space, given
102 * that add is negative, but we don't handle
103 * such "temporary lack of precision" situation here.
104 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000105 */
106 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
107 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 errno = ERANGE; /* Overflow */
109 return -1;
110 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000111 /* Fool the routine computing unused bits */
112 arclen -= 7;
113 cache = *arcbuf & 0x7f;
114 arcbuf++;
115 }
116 }
117
Lev Walkinc4c61962004-06-14 08:17:27 +0000118 /* Faster path for common size */
119 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
120 unsigned long accum;
121 /* Gather all bits into the accumulator */
122 for(accum = cache; arcbuf < arcend; arcbuf++)
123 accum = (accum << 7) | (*arcbuf & ~0x80);
124 if(accum < (unsigned)-add) {
125 errno = ERANGE; /* Overflow */
126 return -1;
127 }
Lev Walkin70732a22013-12-07 10:55:14 -0800128 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000129 return 0;
130 }
131
Lev Walkin29a044b2004-06-14 07:24:36 +0000132#ifndef WORDS_BIGENDIAN
133 if(*(unsigned char *)&LE) { /* Little endian (x86) */
134 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000135 rvbuf += rvsize / CHAR_BIT - 1;
136 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000137 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000138 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000139#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000140 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000141
Lev Walkinc4c61962004-06-14 08:17:27 +0000142 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000143 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000144
Lev Walkin29a044b2004-06-14 07:24:36 +0000145 /* Clear the high unused bits */
146 for(bits = rvsize - arclen;
147 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000148 rvbuf += inc, bits -= CHAR_BIT)
149 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000150
Lev Walkin29a044b2004-06-14 07:24:36 +0000151 /* Fill the body of a value */
152 for(; arcbuf < arcend; arcbuf++) {
153 cache = (cache << 7) | (*arcbuf & 0x7f);
154 bits += 7;
155 if(bits >= CHAR_BIT) {
156 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 *rvbuf = (cache >> bits);
158 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 }
160 }
161 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000162 *rvbuf = cache;
163 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000164 }
165 }
166
167 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000168 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
169 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700170 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000171 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000172 add = -1;
173 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000174 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000175 break;
176 }
177 }
178 if(rvbuf == rvstart) {
179 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 errno = ERANGE; /* Overflow */
181 return -1;
182 }
183 }
184
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 return 0;
186}
187
Lev Walkina9cc46e2004-09-22 16:06:28 +0000188ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700189OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 asn_app_consume_bytes_f *cb, void *app_key) {
191 char scratch[64]; /* Conservative estimate */
192 unsigned long accum; /* Bits accumulator */
193 char *p; /* Position in the scratch buffer */
194
Lev Walkin29a044b2004-06-14 07:24:36 +0000195 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
196 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 return -1;
198
Lev Walkin3251b8e2004-08-23 09:23:02 +0000199 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 ssize_t len;
201
Lev Walkin3251b8e2004-08-23 09:23:02 +0000202 /* Fill the scratch buffer in reverse. */
203 p = scratch + sizeof(scratch);
204 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000206
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207 len = sizeof(scratch) - (p - scratch);
208 if(cb(p, len, app_key) < 0)
209 return -1;
210 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000211 } else {
212 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213 if(cb(scratch, 1, app_key) < 0)
214 return -1;
215 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000216 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000217}
218
219int
Wim Lewis14e6b162014-07-23 16:06:01 -0700220OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 asn_app_consume_bytes_f *cb, void *app_key) {
222
223 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
224 return -1;
225
226 return 0;
227}
228
229static ssize_t
230OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
231 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 int startn;
233 int add = 0;
234 int i;
235
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 for(i = 0, startn = 0; i < st->size; i++) {
237 uint8_t b = st->buf[i];
238 if((b & 0x80)) /* Continuation expected */
239 continue;
240
241 if(startn == 0) {
242 /*
243 * First two arcs are encoded through the backdoor.
244 */
245 if(i) {
246 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 } else if(b <= 39) {
249 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 } else if(b < 79) {
252 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 } else {
255 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 }
260
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 return -1;
263
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
265 i - startn + 1, add, cb, app_key);
266 if(add < 0) return -1;
267 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 startn = i + 1;
269 add = 0;
270 }
271
Lev Walkina9cc46e2004-09-22 16:06:28 +0000272 return wrote_len;
273}
274
Lev Walkin0fab1a62005-03-09 22:19:25 +0000275static enum xer_pbd_rval
276OBJECT_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 +0000277 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000278 const char *chunk_end = (const char *)chunk_buf + chunk_size;
279 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000280 long s_arcs[10];
281 long *arcs = s_arcs;
282 int arcs_count;
283 int ret;
284
Lev Walkine0b56e02005-02-25 12:10:27 +0000285 (void)td;
286
Lev Walkin92302252004-10-23 10:16:51 +0000287 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000288 (const char *)chunk_buf, chunk_size, arcs,
289 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700290 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000291 /* Expecting more than zero arcs */
292 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700293 } else if(arcs_count == 0) {
294 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000295 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700296 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000297
298 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000299 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000300 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000301 ret = OBJECT_IDENTIFIER_parse_arcs(
302 (const char *)chunk_buf, chunk_size,
303 arcs, arcs_count, &endptr);
304 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000305 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000306 }
307
308 /*
309 * Convert arcs into BER representation.
310 */
311 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000312 if(arcs != s_arcs) FREEMEM(arcs);
313
Lev Walkin2c34aad2005-03-10 11:50:24 +0000314 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000315}
316
317asn_dec_rval_t
318OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
319 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000320 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000321
322 return xer_decode_primitive(opt_codec_ctx, td,
323 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
324 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
325}
326
Lev Walkina9cc46e2004-09-22 16:06:28 +0000327asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000328OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000329 int ilevel, enum xer_encoder_flags_e flags,
330 asn_app_consume_bytes_f *cb, void *app_key) {
331 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
332 asn_enc_rval_t er;
333
334 (void)ilevel;
335 (void)flags;
336
337 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700338 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000339
340 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700341 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000342
Lev Walkin7c1dc052016-03-14 03:08:15 -0700343 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000344}
345
346int
Lev Walkin5e033762004-09-29 13:26:15 +0000347OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000348 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
349 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
350
351 (void)td; /* Unused argument */
352 (void)ilevel; /* Unused argument */
353
354 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000355 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356
357 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000358 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000359 return -1;
360
361 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
362 return -1;
363
Lev Walkin8e8078a2004-09-26 13:10:40 +0000364 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000365}
366
367int
Wim Lewis14e6b162014-07-23 16:06:01 -0700368OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000369 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000370 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000371 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 int startn = 0;
373 int add = 0;
374 int i;
375
Lev Walkin0787ff02004-06-17 23:43:39 +0000376 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 errno = EINVAL;
378 return -1;
379 }
380
381 for(i = 0; i < oid->size; i++) {
382 uint8_t b = oid->buf[i];
383 if((b & 0x80)) /* Continuation expected */
384 continue;
385
Lev Walkin29a044b2004-06-14 07:24:36 +0000386 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 /*
388 * First two arcs are encoded through the backdoor.
389 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000390 unsigned LE = 1; /* Little endian */
391 int first_arc;
392 num_arcs++;
393 if(!arc_slots) { num_arcs++; continue; }
394
395 if(i) first_arc = 2;
396 else if(b <= 39) first_arc = 0;
397 else if(b < 79) first_arc = 1;
398 else first_arc = 2;
399
400 add = -40 * first_arc;
401 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000402 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000403 + ((*(char *)&LE)?0:(arc_type_size - 1)))
404 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000405 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000406 }
407
Lev Walkin29a044b2004-06-14 07:24:36 +0000408 /* Decode, if has space */
409 if(arcs < arcs_end) {
410 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
411 i - startn + 1, add,
412 arcs, arc_type_size))
413 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000415 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000416 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000418 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 }
420
Lev Walkin29a044b2004-06-14 07:24:36 +0000421 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000422}
423
Lev Walkin0787ff02004-06-17 23:43:39 +0000424
425/*
426 * Save the single value as an object identifier arc.
427 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000428int
Lev Walkinb1a15552005-12-22 21:39:44 +0000429OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000430 /*
431 * The following conditions must hold:
432 * assert(arcval);
433 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000434 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000435 * assert(arcbuf);
436 */
437#ifdef WORDS_BIGENDIAN
438 const unsigned isLittleEndian = 0;
439#else
440 unsigned LE = 1;
441 unsigned isLittleEndian = *(char *)&LE;
442#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000443 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000444 unsigned int cache;
445 uint8_t *bp = arcbuf;
446 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000447 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000448
449 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000450 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
451 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000452 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000453 uint8_t *tb;
454 for(tb = buffer; a >= aend; tb++, a--)
455 if((*tb = *a) && (tb < msb))
456 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000457 tend = &buffer[arcval_size];
458 tp = msb; /* Most significant non-zero byte */
459 } else {
460 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000461 tend = (const unsigned char *)arcval + arcval_size;
462 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000463 if(*tp) break;
464 }
465
466 /*
467 * Split the value in 7-bits chunks.
468 */
469 bits = ((tend - tp) * CHAR_BIT) % 7;
470 if(bits) {
471 cache = *tp >> (CHAR_BIT - bits);
472 if(cache) {
473 *bp++ = cache | 0x80;
474 cache = *tp++;
475 bits = CHAR_BIT - bits;
476 } else {
477 bits = -bits;
478 }
479 } else {
480 cache = 0;
481 }
482 for(; tp < tend; tp++) {
483 cache = (cache << CHAR_BIT) + *tp;
484 bits += CHAR_BIT;
485 while(bits >= 7) {
486 bits -= 7;
487 *bp++ = 0x80 | (cache >> bits);
488 }
489 }
490 if(bits) *bp++ = cache;
491 bp[-1] &= 0x7f; /* Clear the last bit */
492
493 return bp - arcbuf;
494}
495
Lev Walkinf15320b2004-06-03 03:38:44 +0000496int
Lev Walkinb1a15552005-12-22 21:39:44 +0000497OBJECT_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 +0000498 uint8_t *buf;
499 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000500 unsigned LE = 1; /* Little endian (x86) */
501 unsigned isLittleEndian = *((char *)&LE);
502 unsigned int arc0;
503 unsigned int arc1;
504 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000505 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000506
Lev Walkin523de9e2006-08-18 01:34:18 +0000507 if(!oid || !arcs || arc_type_size < 1
508 || arc_type_size > 16
509 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 errno = EINVAL;
511 return -1;
512 }
513
Lev Walkin0787ff02004-06-17 23:43:39 +0000514 switch(arc_type_size) {
515 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000516 arc0 = ((const unsigned char *)arcs)[0];
517 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000518 break;
519 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000520 arc0 = ((const unsigned short *)arcs)[0];
521 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000522 break;
523 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000524 arc0 = ((const unsigned int *)arcs)[0];
525 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000526 break;
527 default:
528 arc1 = arc0 = 0;
529 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000530 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000531 /* If more significant bytes are present,
532 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000533 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000534 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000535 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
536 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000537 arc0 = *((const unsigned char *)arcs + 0);
538 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000539 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000540 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000541 /* If more significant bytes are present,
542 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000543 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000545 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
546 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000547 }
548 }
549
550 /*
551 * The previous chapter left us with the first and the second arcs.
552 * The values are not precise (that is, they are valid only if
553 * they're less than 255), but OK for the purposes of making
554 * the sanity test below.
555 */
556 if(arc0 <= 1) {
557 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 /* 8.19.4: At most 39 subsequent values (including 0) */
559 errno = ERANGE;
560 return -1;
561 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000562 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000563 /* 8.19.4: Only three values are allocated from the root node */
564 errno = ERANGE;
565 return -1;
566 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000567 /*
568 * After above tests it is known that the value of arc0 is completely
569 * trustworthy (0..2). However, the arc1's value is still meaningless.
570 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
572 /*
573 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000574 * This estimation implicitly takes in account the following facts,
575 * that cancel each other:
576 * * the first two arcs are encoded in a single value.
577 * * the first value may require more space (+1 byte)
578 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000580 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000581 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 if(!buf) {
583 /* ENOMEM */
584 return -1;
585 }
586
587 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000588 * Encode the first two arcs.
589 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000592 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000593 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000594 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595
Lev Walkin0787ff02004-06-17 23:43:39 +0000596 /*
597 * Simulate first_value = arc0 * 40 + arc1;
598 */
599 /* Copy the second (1'st) arcs[1] into the first_value */
600 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000601 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000602 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000603 const uint8_t *aend = (const unsigned char *)arcs - 1;
604 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000605 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000607 const uint8_t *a1 = (const uint8_t *)arcs;
608 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000609 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000610 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000611 /* Increase the first_value by arc0 */
612 arc0 *= 40; /* (0..80) */
613 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
614 unsigned int v = *tp;
615 v += arc0;
616 *tp = v;
617 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
618 else break;
619 }
620
621 assert(tp >= first_value);
622
623 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
624 fv - first_value, 1);
625 }
626
627 /*
628 * Save the rest of arcs.
629 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000630 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000631 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000632 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000633 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
634 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000635 }
636
Lev Walkin34b2a932004-06-17 23:46:45 +0000637 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000638
639 /*
640 * Replace buffer.
641 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000642 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 bp = oid->buf;
644 oid->buf = buf;
645 if(bp) FREEMEM(bp);
646
647 return 0;
648}
Lev Walkinc4c61962004-06-14 08:17:27 +0000649
Lev Walkin92302252004-10-23 10:16:51 +0000650
651int
652OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000653 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000654 unsigned int arcs_count = 0;
655 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000656 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700657 ST_LEADSPACE,
658 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700659 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800660 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700661 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000662
663 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000664 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000665 errno = EINVAL;
666 return -1;
667 }
668
669 if(oid_txt_length == -1)
670 oid_txt_length = strlen(oid_text);
671
Lev Walkincad560a2013-03-16 07:00:58 -0700672#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
673 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700674 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700675 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700676 case ASN_STRTOX_EXTRA_DATA: \
677 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700678 if(arcs_count < arcs_slots) \
679 arcs[arcs_count] = value; \
680 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700681 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700682 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700683 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700684 if(opt_oid_text_end) \
685 *opt_oid_text_end = oid_text; \
686 errno = ERANGE; \
687 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700688 case ASN_STRTOX_ERROR_INVAL: \
689 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700690 if(opt_oid_text_end) \
691 *opt_oid_text_end = oid_text; \
692 errno = EINVAL; \
693 return -1; \
694 } \
695 } while(0)
696
Lev Walkin92302252004-10-23 10:16:51 +0000697 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
698 switch(*oid_text) {
699 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700700 switch(state) {
701 case ST_LEADSPACE:
702 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000703 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700704 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700705 state = ST_TAILSPACE;
706 continue;
707 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700708 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000709 }
Lev Walkincad560a2013-03-16 07:00:58 -0700710 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700711 case 0x2e: /* '.' */
712 switch(state) {
713 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700714 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700715 case ST_WAITDIGITS:
716 if(opt_oid_text_end)
717 *opt_oid_text_end = oid_text;
718 errno = EINVAL; /* Broken OID */
719 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700720 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700721 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700722 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000723 continue;
724 }
Lev Walkinf5927112012-09-03 00:48:45 -0700725 break;
726 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
727 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
728 switch(state) {
729 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700730 case ST_AFTERVALUE:
731 if(opt_oid_text_end)
732 *opt_oid_text_end = oid_text;
733 errno = EINVAL; /* "1. 1" => broken OID */
734 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700735 case ST_LEADSPACE:
736 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700737 _OID_CAPTURE_ARC(oid_text, oid_end);
738 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700739 continue;
740 }
741 break;
Lev Walkin92302252004-10-23 10:16:51 +0000742 default:
743 /* Unexpected symbols */
744 state = ST_WAITDIGITS;
745 break;
746 } /* switch() */
747 break;
748 } /* for() */
749
750
Lev Walkin093ba2e2005-04-04 21:10:06 +0000751 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000752
753 /* Finalize last arc */
754 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700755 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700756 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700757 case ST_WAITDIGITS:
758 errno = EINVAL; /* Broken OID */
759 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700760 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700761 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000762 return arcs_count;
763 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700764
765 errno = EINVAL; /* Broken OID */
766 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000767}
768
769