blob: b2c97ae95212b3c5fc7789af439c498353a6b1b5 [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080018asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER = {
Lev Walkin8e8078a2004-09-26 13:10:40 +000019 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000020 OBJECT_IDENTIFIER_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070021 OCTET_STRING_compare, /* Implemented in terms of a string comparison */
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ber_decode_primitive,
23 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000024 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 OBJECT_IDENTIFIER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070026#ifdef ASN_DISABLE_OER_SUPPORT
27 0,
28 0,
29#else
30 0,
31 0,
32#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040033#ifdef ASN_DISABLE_PER_SUPPORT
34 0,
35 0,
36#else
37 OCTET_STRING_decode_uper,
38 OCTET_STRING_encode_uper,
39#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080040 0 /* Use generic outmost tag fetcher */
41};
42asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
43 "OBJECT IDENTIFIER",
44 "OBJECT_IDENTIFIER",
45 &asn_OP_OBJECT_IDENTIFIER,
46 OBJECT_IDENTIFIER_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000047 asn_DEF_OBJECT_IDENTIFIER_tags,
48 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
49 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
50 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
51 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
52 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070053 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000054 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000055 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000056 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000057};
58
59
Lev Walkinf15320b2004-06-03 03:38:44 +000060int
Lev Walkin5e033762004-09-29 13:26:15 +000061OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000062 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000063 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65 if(st && st->buf) {
66 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070067 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000068 "%s: at least one numerical value "
69 "expected (%s:%d)",
70 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 return -1;
72 }
73 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070074 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000075 "%s: value not given (%s:%d)",
76 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000077 return -1;
78 }
79
80 return 0;
81}
82
83int
Wim Lewis14e6b162014-07-23 16:06:01 -070084OBJECT_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 +000085 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070086 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000087 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000088 unsigned char *rvbuf = (unsigned char *)rvbufp;
89 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000090 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000091
Lev Walkin29a044b2004-06-14 07:24:36 +000092 rvsize *= CHAR_BIT; /* bytes to bits */
93 arclen *= 7; /* bytes to bits */
94
Lev Walkin0995f352017-09-17 23:16:38 -070095 assert(add <= 0);
96
Lev Walkin29a044b2004-06-14 07:24:36 +000097 /*
98 * The arc has the number of bits
99 * cannot be represented using supplied return value type.
100 */
101 if(arclen > rvsize) {
102 if(arclen > (rvsize + CHAR_BIT)) {
103 errno = ERANGE; /* Overflow */
104 return -1;
105 } else {
106 /*
107 * Even if the number of bits in the arc representation
108 * is higher than the width of supplied * return value
109 * type, there is still possible to fit it when there
110 * are few unused high bits in the arc value
111 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000112 *
113 * Moreover, there is a possibility that the
114 * number could actually fit the arc space, given
115 * that add is negative, but we don't handle
116 * such "temporary lack of precision" situation here.
117 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000118 */
119 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
120 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 errno = ERANGE; /* Overflow */
122 return -1;
123 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000124 /* Fool the routine computing unused bits */
125 arclen -= 7;
126 cache = *arcbuf & 0x7f;
127 arcbuf++;
128 }
129 }
130
Lev Walkinc4c61962004-06-14 08:17:27 +0000131 /* Faster path for common size */
132 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
133 unsigned long accum;
134 /* Gather all bits into the accumulator */
135 for(accum = cache; arcbuf < arcend; arcbuf++)
136 accum = (accum << 7) | (*arcbuf & ~0x80);
Lev Walkin0995f352017-09-17 23:16:38 -0700137 if(accum < (unsigned)-add
138 || accum > (ULONG_MAX-(unsigned long)(-add))) {
Lev Walkinc4c61962004-06-14 08:17:27 +0000139 errno = ERANGE; /* Overflow */
140 return -1;
141 }
Lev Walkin0995f352017-09-17 23:16:38 -0700142 *(unsigned long *)(void *)rvbuf =
143 accum - (unsigned long)(-add); /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000144 return 0;
145 }
146
Lev Walkin29a044b2004-06-14 07:24:36 +0000147#ifndef WORDS_BIGENDIAN
148 if(*(unsigned char *)&LE) { /* Little endian (x86) */
149 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000150 rvbuf += rvsize / CHAR_BIT - 1;
151 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000152 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000153 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000154#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000155 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000156
Lev Walkinc4c61962004-06-14 08:17:27 +0000157 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000158 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000159
Lev Walkin29a044b2004-06-14 07:24:36 +0000160 /* Clear the high unused bits */
161 for(bits = rvsize - arclen;
162 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 rvbuf += inc, bits -= CHAR_BIT)
164 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000165
Lev Walkin29a044b2004-06-14 07:24:36 +0000166 /* Fill the body of a value */
167 for(; arcbuf < arcend; arcbuf++) {
168 cache = (cache << 7) | (*arcbuf & 0x7f);
169 bits += 7;
170 if(bits >= CHAR_BIT) {
171 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000172 *rvbuf = (cache >> bits);
173 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000174 }
175 }
176 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000177 *rvbuf = cache;
178 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000179 }
180 }
181
182 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000183 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
184 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700185 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000186 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000187 add = -1;
188 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000189 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000190 break;
191 }
192 }
193 if(rvbuf == rvstart) {
194 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 errno = ERANGE; /* Overflow */
196 return -1;
197 }
198 }
199
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 return 0;
201}
202
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700204OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 asn_app_consume_bytes_f *cb, void *app_key) {
206 char scratch[64]; /* Conservative estimate */
207 unsigned long accum; /* Bits accumulator */
208 char *p; /* Position in the scratch buffer */
209
Lev Walkin29a044b2004-06-14 07:24:36 +0000210 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
211 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 return -1;
213
Lev Walkin3251b8e2004-08-23 09:23:02 +0000214 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215 ssize_t len;
216
Lev Walkin3251b8e2004-08-23 09:23:02 +0000217 /* Fill the scratch buffer in reverse. */
218 p = scratch + sizeof(scratch);
219 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000220 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000221
Lev Walkina9cc46e2004-09-22 16:06:28 +0000222 len = sizeof(scratch) - (p - scratch);
223 if(cb(p, len, app_key) < 0)
224 return -1;
225 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000226 } else {
227 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000228 if(cb(scratch, 1, app_key) < 0)
229 return -1;
230 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000231 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000232}
233
234int
Wim Lewis14e6b162014-07-23 16:06:01 -0700235OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000236 asn_app_consume_bytes_f *cb, void *app_key) {
237
238 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
239 return -1;
240
241 return 0;
242}
243
244static ssize_t
245OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
246 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700247 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700249 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000250
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 for(i = 0, startn = 0; i < st->size; i++) {
252 uint8_t b = st->buf[i];
253 if((b & 0x80)) /* Continuation expected */
254 continue;
255
256 if(startn == 0) {
257 /*
258 * First two arcs are encoded through the backdoor.
259 */
260 if(i) {
261 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 } else if(b <= 39) {
264 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 } else if(b < 79) {
267 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 } else {
270 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000271 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 }
275
Lev Walkina9cc46e2004-09-22 16:06:28 +0000276 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 return -1;
278
Lev Walkina9cc46e2004-09-22 16:06:28 +0000279 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
280 i - startn + 1, add, cb, app_key);
281 if(add < 0) return -1;
282 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 startn = i + 1;
284 add = 0;
285 }
286
Lev Walkina9cc46e2004-09-22 16:06:28 +0000287 return wrote_len;
288}
289
Lev Walkin0fab1a62005-03-09 22:19:25 +0000290static enum xer_pbd_rval
291OBJECT_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 +0000292 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000293 const char *chunk_end = (const char *)chunk_buf + chunk_size;
294 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000295 long s_arcs[10];
296 long *arcs = s_arcs;
297 int arcs_count;
298 int ret;
299
Lev Walkine0b56e02005-02-25 12:10:27 +0000300 (void)td;
301
Lev Walkin92302252004-10-23 10:16:51 +0000302 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000303 (const char *)chunk_buf, chunk_size, arcs,
304 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700305 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000306 /* Expecting more than zero arcs */
307 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700308 } else if(arcs_count == 0) {
309 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000310 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700311 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000312
313 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000314 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000315 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000316 ret = OBJECT_IDENTIFIER_parse_arcs(
317 (const char *)chunk_buf, chunk_size,
318 arcs, arcs_count, &endptr);
319 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000320 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000321 }
322
323 /*
324 * Convert arcs into BER representation.
325 */
326 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000327 if(arcs != s_arcs) FREEMEM(arcs);
328
Lev Walkin2c34aad2005-03-10 11:50:24 +0000329 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000330}
331
332asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700333OBJECT_IDENTIFIER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin92302252004-10-23 10:16:51 +0000334 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000335 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000336
337 return xer_decode_primitive(opt_codec_ctx, td,
338 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
339 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
340}
341
Lev Walkina9cc46e2004-09-22 16:06:28 +0000342asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000343OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000344 int ilevel, enum xer_encoder_flags_e flags,
345 asn_app_consume_bytes_f *cb, void *app_key) {
346 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
347 asn_enc_rval_t er;
348
349 (void)ilevel;
350 (void)flags;
351
352 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700353 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000354
355 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700356 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000357
Lev Walkin7c1dc052016-03-14 03:08:15 -0700358 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000359}
360
361int
Lev Walkin5e033762004-09-29 13:26:15 +0000362OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000363 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
364 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
365
366 (void)td; /* Unused argument */
367 (void)ilevel; /* Unused argument */
368
369 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000370 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000371
372 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000373 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000374 return -1;
375
376 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
377 return -1;
378
Lev Walkin8e8078a2004-09-26 13:10:40 +0000379 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000380}
381
382int
Wim Lewis14e6b162014-07-23 16:06:01 -0700383OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000384 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000385 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000386 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 int startn = 0;
388 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700389 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000390
Lev Walkin0787ff02004-06-17 23:43:39 +0000391 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 errno = EINVAL;
393 return -1;
394 }
395
396 for(i = 0; i < oid->size; i++) {
397 uint8_t b = oid->buf[i];
398 if((b & 0x80)) /* Continuation expected */
399 continue;
400
Lev Walkin29a044b2004-06-14 07:24:36 +0000401 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 /*
403 * First two arcs are encoded through the backdoor.
404 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000405 unsigned LE = 1; /* Little endian */
406 int first_arc;
407 num_arcs++;
408 if(!arc_slots) { num_arcs++; continue; }
409
410 if(i) first_arc = 2;
411 else if(b <= 39) first_arc = 0;
412 else if(b < 79) first_arc = 1;
413 else first_arc = 2;
414
415 add = -40 * first_arc;
416 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000417 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000418 + ((*(char *)&LE)?0:(arc_type_size - 1)))
419 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000420 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 }
422
Lev Walkin29a044b2004-06-14 07:24:36 +0000423 /* Decode, if has space */
424 if(arcs < arcs_end) {
425 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
426 i - startn + 1, add,
427 arcs, arc_type_size))
428 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000430 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000431 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000433 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 }
435
Lev Walkin29a044b2004-06-14 07:24:36 +0000436 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000437}
438
Lev Walkin0787ff02004-06-17 23:43:39 +0000439
440/*
441 * Save the single value as an object identifier arc.
442 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000443int
Lev Walkinb1a15552005-12-22 21:39:44 +0000444OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000445 /*
446 * The following conditions must hold:
447 * assert(arcval);
448 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000449 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000450 * assert(arcbuf);
451 */
452#ifdef WORDS_BIGENDIAN
453 const unsigned isLittleEndian = 0;
454#else
455 unsigned LE = 1;
456 unsigned isLittleEndian = *(char *)&LE;
457#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000458 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000459 unsigned int cache;
460 uint8_t *bp = arcbuf;
461 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000462 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000463
464 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000465 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
466 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000467 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000468 uint8_t *tb;
469 for(tb = buffer; a >= aend; tb++, a--)
470 if((*tb = *a) && (tb < msb))
471 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000472 tend = &buffer[arcval_size];
473 tp = msb; /* Most significant non-zero byte */
474 } else {
475 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000476 tend = (const unsigned char *)arcval + arcval_size;
477 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000478 if(*tp) break;
479 }
480
481 /*
482 * Split the value in 7-bits chunks.
483 */
484 bits = ((tend - tp) * CHAR_BIT) % 7;
485 if(bits) {
486 cache = *tp >> (CHAR_BIT - bits);
487 if(cache) {
488 *bp++ = cache | 0x80;
489 cache = *tp++;
490 bits = CHAR_BIT - bits;
491 } else {
492 bits = -bits;
493 }
494 } else {
495 cache = 0;
496 }
497 for(; tp < tend; tp++) {
498 cache = (cache << CHAR_BIT) + *tp;
499 bits += CHAR_BIT;
500 while(bits >= 7) {
501 bits -= 7;
502 *bp++ = 0x80 | (cache >> bits);
503 }
504 }
505 if(bits) *bp++ = cache;
506 bp[-1] &= 0x7f; /* Clear the last bit */
507
508 return bp - arcbuf;
509}
510
Lev Walkinf15320b2004-06-03 03:38:44 +0000511int
Lev Walkinb1a15552005-12-22 21:39:44 +0000512OBJECT_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 +0000513 uint8_t *buf;
514 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 unsigned LE = 1; /* Little endian (x86) */
516 unsigned isLittleEndian = *((char *)&LE);
517 unsigned int arc0;
518 unsigned int arc1;
519 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000520 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000521
Lev Walkin523de9e2006-08-18 01:34:18 +0000522 if(!oid || !arcs || arc_type_size < 1
523 || arc_type_size > 16
524 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 errno = EINVAL;
526 return -1;
527 }
528
Lev Walkin0787ff02004-06-17 23:43:39 +0000529 switch(arc_type_size) {
530 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000531 arc0 = ((const unsigned char *)arcs)[0];
532 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000533 break;
534 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000535 arc0 = ((const unsigned short *)arcs)[0];
536 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000537 break;
538 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000539 arc0 = ((const unsigned int *)arcs)[0];
540 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000541 break;
542 default:
543 arc1 = arc0 = 0;
544 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000545 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000546 /* If more significant bytes are present,
547 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000548 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000549 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000550 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
551 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000552 arc0 = *((const unsigned char *)arcs + 0);
553 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000554 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000555 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000556 /* If more significant bytes are present,
557 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000558 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000559 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000560 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
561 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000562 }
563 }
564
565 /*
566 * The previous chapter left us with the first and the second arcs.
567 * The values are not precise (that is, they are valid only if
568 * they're less than 255), but OK for the purposes of making
569 * the sanity test below.
570 */
571 if(arc0 <= 1) {
572 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 /* 8.19.4: At most 39 subsequent values (including 0) */
574 errno = ERANGE;
575 return -1;
576 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000577 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 /* 8.19.4: Only three values are allocated from the root node */
579 errno = ERANGE;
580 return -1;
581 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000582 /*
583 * After above tests it is known that the value of arc0 is completely
584 * trustworthy (0..2). However, the arc1's value is still meaningless.
585 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000586
587 /*
588 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000589 * This estimation implicitly takes in account the following facts,
590 * that cancel each other:
591 * * the first two arcs are encoded in a single value.
592 * * the first value may require more space (+1 byte)
593 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000594 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000595 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000596 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 if(!buf) {
598 /* ENOMEM */
599 return -1;
600 }
601
602 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000603 * Encode the first two arcs.
604 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000607 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000608 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000609 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000610
Lev Walkin0787ff02004-06-17 23:43:39 +0000611 /*
612 * Simulate first_value = arc0 * 40 + arc1;
613 */
614 /* Copy the second (1'st) arcs[1] into the first_value */
615 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000616 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000617 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000618 const uint8_t *aend = (const unsigned char *)arcs - 1;
619 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000620 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000621 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000622 const uint8_t *a1 = (const uint8_t *)arcs;
623 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000624 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000625 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000626 /* Increase the first_value by arc0 */
627 arc0 *= 40; /* (0..80) */
628 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
629 unsigned int v = *tp;
630 v += arc0;
631 *tp = v;
632 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
633 else break;
634 }
635
636 assert(tp >= first_value);
637
638 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
639 fv - first_value, 1);
640 }
641
642 /*
643 * Save the rest of arcs.
644 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000645 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000646 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000647 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000648 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
649 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000650 }
651
Lev Walkin34b2a932004-06-17 23:46:45 +0000652 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000653
654 /*
655 * Replace buffer.
656 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000657 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 bp = oid->buf;
659 oid->buf = buf;
660 if(bp) FREEMEM(bp);
661
662 return 0;
663}
Lev Walkinc4c61962004-06-14 08:17:27 +0000664
Lev Walkin92302252004-10-23 10:16:51 +0000665
666int
667OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000668 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000669 unsigned int arcs_count = 0;
670 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000671 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700672 ST_LEADSPACE,
673 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700674 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800675 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700676 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000677
678 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000679 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000680 errno = EINVAL;
681 return -1;
682 }
683
684 if(oid_txt_length == -1)
685 oid_txt_length = strlen(oid_text);
686
Lev Walkincad560a2013-03-16 07:00:58 -0700687#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
688 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700689 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700690 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700691 case ASN_STRTOX_EXTRA_DATA: \
692 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700693 if(arcs_count < arcs_slots) \
694 arcs[arcs_count] = value; \
695 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700696 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700697 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700698 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700699 if(opt_oid_text_end) \
700 *opt_oid_text_end = oid_text; \
701 errno = ERANGE; \
702 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700703 case ASN_STRTOX_ERROR_INVAL: \
704 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700705 if(opt_oid_text_end) \
706 *opt_oid_text_end = oid_text; \
707 errno = EINVAL; \
708 return -1; \
709 } \
710 } while(0)
711
Lev Walkin92302252004-10-23 10:16:51 +0000712 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
713 switch(*oid_text) {
714 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700715 switch(state) {
716 case ST_LEADSPACE:
717 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000718 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700719 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700720 state = ST_TAILSPACE;
721 continue;
722 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700723 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000724 }
Lev Walkincad560a2013-03-16 07:00:58 -0700725 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700726 case 0x2e: /* '.' */
727 switch(state) {
728 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700729 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700730 case ST_WAITDIGITS:
731 if(opt_oid_text_end)
732 *opt_oid_text_end = oid_text;
733 errno = EINVAL; /* Broken OID */
734 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700735 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700736 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700737 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000738 continue;
739 }
Lev Walkinf5927112012-09-03 00:48:45 -0700740 break;
741 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
742 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
743 switch(state) {
744 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700745 case ST_AFTERVALUE:
746 if(opt_oid_text_end)
747 *opt_oid_text_end = oid_text;
748 errno = EINVAL; /* "1. 1" => broken OID */
749 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700750 case ST_LEADSPACE:
751 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700752 _OID_CAPTURE_ARC(oid_text, oid_end);
753 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700754 continue;
755 }
756 break;
Lev Walkin92302252004-10-23 10:16:51 +0000757 default:
758 /* Unexpected symbols */
759 state = ST_WAITDIGITS;
760 break;
761 } /* switch() */
762 break;
763 } /* for() */
764
765
Lev Walkin093ba2e2005-04-04 21:10:06 +0000766 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000767
768 /* Finalize last arc */
769 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700770 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700771 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700772 case ST_WAITDIGITS:
773 errno = EINVAL; /* Broken OID */
774 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700775 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700776 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000777 return arcs_count;
778 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700779
780 errno = EINVAL; /* Broken OID */
781 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000782}
783
784