blob: f414f681b3fc46f6ad32b9d4a946b7d6ba758e3a [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 Walkincc159472017-07-06 08:26:36 -070035#ifdef ASN_DISABLE_OER_SUPPORT
36 0,
37 0,
38#else
39 0,
40 0,
41#endif /* ASN_DISABLE_OER_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 Walkin59b176e2005-11-26 11:25:14 +000049 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000050 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000051 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000052};
53
54
Lev Walkinf15320b2004-06-03 03:38:44 +000055int
Lev Walkin5e033762004-09-29 13:26:15 +000056OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000057 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000058 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000059
60 if(st && st->buf) {
61 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070062 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000063 "%s: at least one numerical value "
64 "expected (%s:%d)",
65 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000066 return -1;
67 }
68 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070069 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000070 "%s: value not given (%s:%d)",
71 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000072 return -1;
73 }
74
75 return 0;
76}
77
Lev Walkinc4c61962004-06-14 08:17:27 +000078
Lev Walkinf15320b2004-06-03 03:38:44 +000079int
Wim Lewis14e6b162014-07-23 16:06:01 -070080OBJECT_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 +000081 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070082 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000083 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000084 unsigned char *rvbuf = (unsigned char *)rvbufp;
85 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000086 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000087
Lev Walkin29a044b2004-06-14 07:24:36 +000088 rvsize *= CHAR_BIT; /* bytes to bits */
89 arclen *= 7; /* bytes to bits */
90
91 /*
92 * The arc has the number of bits
93 * cannot be represented using supplied return value type.
94 */
95 if(arclen > rvsize) {
96 if(arclen > (rvsize + CHAR_BIT)) {
97 errno = ERANGE; /* Overflow */
98 return -1;
99 } else {
100 /*
101 * Even if the number of bits in the arc representation
102 * is higher than the width of supplied * return value
103 * type, there is still possible to fit it when there
104 * are few unused high bits in the arc value
105 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000106 *
107 * Moreover, there is a possibility that the
108 * number could actually fit the arc space, given
109 * that add is negative, but we don't handle
110 * such "temporary lack of precision" situation here.
111 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000112 */
113 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
114 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 errno = ERANGE; /* Overflow */
116 return -1;
117 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000118 /* Fool the routine computing unused bits */
119 arclen -= 7;
120 cache = *arcbuf & 0x7f;
121 arcbuf++;
122 }
123 }
124
Lev Walkinc4c61962004-06-14 08:17:27 +0000125 /* Faster path for common size */
126 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
127 unsigned long accum;
128 /* Gather all bits into the accumulator */
129 for(accum = cache; arcbuf < arcend; arcbuf++)
130 accum = (accum << 7) | (*arcbuf & ~0x80);
131 if(accum < (unsigned)-add) {
132 errno = ERANGE; /* Overflow */
133 return -1;
134 }
Lev Walkin70732a22013-12-07 10:55:14 -0800135 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000136 return 0;
137 }
138
Lev Walkin29a044b2004-06-14 07:24:36 +0000139#ifndef WORDS_BIGENDIAN
140 if(*(unsigned char *)&LE) { /* Little endian (x86) */
141 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000142 rvbuf += rvsize / CHAR_BIT - 1;
143 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000144 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000145 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000146#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000147 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000148
Lev Walkinc4c61962004-06-14 08:17:27 +0000149 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000150 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000151
Lev Walkin29a044b2004-06-14 07:24:36 +0000152 /* Clear the high unused bits */
153 for(bits = rvsize - arclen;
154 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000155 rvbuf += inc, bits -= CHAR_BIT)
156 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000157
Lev Walkin29a044b2004-06-14 07:24:36 +0000158 /* Fill the body of a value */
159 for(; arcbuf < arcend; arcbuf++) {
160 cache = (cache << 7) | (*arcbuf & 0x7f);
161 bits += 7;
162 if(bits >= CHAR_BIT) {
163 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000164 *rvbuf = (cache >> bits);
165 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000166 }
167 }
168 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000169 *rvbuf = cache;
170 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000171 }
172 }
173
174 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000175 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
176 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700177 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000178 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000179 add = -1;
180 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000181 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000182 break;
183 }
184 }
185 if(rvbuf == rvstart) {
186 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 errno = ERANGE; /* Overflow */
188 return -1;
189 }
190 }
191
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 return 0;
193}
194
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700196OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 asn_app_consume_bytes_f *cb, void *app_key) {
198 char scratch[64]; /* Conservative estimate */
199 unsigned long accum; /* Bits accumulator */
200 char *p; /* Position in the scratch buffer */
201
Lev Walkin29a044b2004-06-14 07:24:36 +0000202 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
203 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 return -1;
205
Lev Walkin3251b8e2004-08-23 09:23:02 +0000206 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207 ssize_t len;
208
Lev Walkin3251b8e2004-08-23 09:23:02 +0000209 /* Fill the scratch buffer in reverse. */
210 p = scratch + sizeof(scratch);
211 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000213
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214 len = sizeof(scratch) - (p - scratch);
215 if(cb(p, len, app_key) < 0)
216 return -1;
217 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000218 } else {
219 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000220 if(cb(scratch, 1, app_key) < 0)
221 return -1;
222 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000223 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000224}
225
226int
Wim Lewis14e6b162014-07-23 16:06:01 -0700227OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000228 asn_app_consume_bytes_f *cb, void *app_key) {
229
230 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
231 return -1;
232
233 return 0;
234}
235
236static ssize_t
237OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
238 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 int startn;
240 int add = 0;
241 int i;
242
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 for(i = 0, startn = 0; i < st->size; i++) {
244 uint8_t b = st->buf[i];
245 if((b & 0x80)) /* Continuation expected */
246 continue;
247
248 if(startn == 0) {
249 /*
250 * First two arcs are encoded through the backdoor.
251 */
252 if(i) {
253 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000254 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 } else if(b <= 39) {
256 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000257 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 } else if(b < 79) {
259 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 } else {
262 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000263 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 }
267
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 return -1;
270
Lev Walkina9cc46e2004-09-22 16:06:28 +0000271 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
272 i - startn + 1, add, cb, app_key);
273 if(add < 0) return -1;
274 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 startn = i + 1;
276 add = 0;
277 }
278
Lev Walkina9cc46e2004-09-22 16:06:28 +0000279 return wrote_len;
280}
281
Lev Walkin0fab1a62005-03-09 22:19:25 +0000282static enum xer_pbd_rval
283OBJECT_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 +0000284 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000285 const char *chunk_end = (const char *)chunk_buf + chunk_size;
286 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000287 long s_arcs[10];
288 long *arcs = s_arcs;
289 int arcs_count;
290 int ret;
291
Lev Walkine0b56e02005-02-25 12:10:27 +0000292 (void)td;
293
Lev Walkin92302252004-10-23 10:16:51 +0000294 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000295 (const char *)chunk_buf, chunk_size, arcs,
296 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700297 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000298 /* Expecting more than zero arcs */
299 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700300 } else if(arcs_count == 0) {
301 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000302 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700303 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000304
305 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000306 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000307 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000308 ret = OBJECT_IDENTIFIER_parse_arcs(
309 (const char *)chunk_buf, chunk_size,
310 arcs, arcs_count, &endptr);
311 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000312 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000313 }
314
315 /*
316 * Convert arcs into BER representation.
317 */
318 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000319 if(arcs != s_arcs) FREEMEM(arcs);
320
Lev Walkin2c34aad2005-03-10 11:50:24 +0000321 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000322}
323
324asn_dec_rval_t
325OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
326 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000327 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000328
329 return xer_decode_primitive(opt_codec_ctx, td,
330 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
331 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
332}
333
Lev Walkina9cc46e2004-09-22 16:06:28 +0000334asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000335OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000336 int ilevel, enum xer_encoder_flags_e flags,
337 asn_app_consume_bytes_f *cb, void *app_key) {
338 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
339 asn_enc_rval_t er;
340
341 (void)ilevel;
342 (void)flags;
343
344 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700345 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000346
347 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700348 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000349
Lev Walkin7c1dc052016-03-14 03:08:15 -0700350 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000351}
352
353int
Lev Walkin5e033762004-09-29 13:26:15 +0000354OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
356 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
357
358 (void)td; /* Unused argument */
359 (void)ilevel; /* Unused argument */
360
361 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000362 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000363
364 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000365 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000366 return -1;
367
368 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
369 return -1;
370
Lev Walkin8e8078a2004-09-26 13:10:40 +0000371 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000372}
373
374int
Wim Lewis14e6b162014-07-23 16:06:01 -0700375OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000376 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000377 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000378 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 int startn = 0;
380 int add = 0;
381 int i;
382
Lev Walkin0787ff02004-06-17 23:43:39 +0000383 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 errno = EINVAL;
385 return -1;
386 }
387
388 for(i = 0; i < oid->size; i++) {
389 uint8_t b = oid->buf[i];
390 if((b & 0x80)) /* Continuation expected */
391 continue;
392
Lev Walkin29a044b2004-06-14 07:24:36 +0000393 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 /*
395 * First two arcs are encoded through the backdoor.
396 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000397 unsigned LE = 1; /* Little endian */
398 int first_arc;
399 num_arcs++;
400 if(!arc_slots) { num_arcs++; continue; }
401
402 if(i) first_arc = 2;
403 else if(b <= 39) first_arc = 0;
404 else if(b < 79) first_arc = 1;
405 else first_arc = 2;
406
407 add = -40 * first_arc;
408 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000409 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000410 + ((*(char *)&LE)?0:(arc_type_size - 1)))
411 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000412 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 }
414
Lev Walkin29a044b2004-06-14 07:24:36 +0000415 /* Decode, if has space */
416 if(arcs < arcs_end) {
417 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
418 i - startn + 1, add,
419 arcs, arc_type_size))
420 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000422 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000423 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000425 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 }
427
Lev Walkin29a044b2004-06-14 07:24:36 +0000428 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429}
430
Lev Walkin0787ff02004-06-17 23:43:39 +0000431
432/*
433 * Save the single value as an object identifier arc.
434 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000435int
Lev Walkinb1a15552005-12-22 21:39:44 +0000436OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000437 /*
438 * The following conditions must hold:
439 * assert(arcval);
440 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000441 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000442 * assert(arcbuf);
443 */
444#ifdef WORDS_BIGENDIAN
445 const unsigned isLittleEndian = 0;
446#else
447 unsigned LE = 1;
448 unsigned isLittleEndian = *(char *)&LE;
449#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000450 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000451 unsigned int cache;
452 uint8_t *bp = arcbuf;
453 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000454 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000455
456 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000457 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
458 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000459 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000460 uint8_t *tb;
461 for(tb = buffer; a >= aend; tb++, a--)
462 if((*tb = *a) && (tb < msb))
463 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000464 tend = &buffer[arcval_size];
465 tp = msb; /* Most significant non-zero byte */
466 } else {
467 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000468 tend = (const unsigned char *)arcval + arcval_size;
469 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000470 if(*tp) break;
471 }
472
473 /*
474 * Split the value in 7-bits chunks.
475 */
476 bits = ((tend - tp) * CHAR_BIT) % 7;
477 if(bits) {
478 cache = *tp >> (CHAR_BIT - bits);
479 if(cache) {
480 *bp++ = cache | 0x80;
481 cache = *tp++;
482 bits = CHAR_BIT - bits;
483 } else {
484 bits = -bits;
485 }
486 } else {
487 cache = 0;
488 }
489 for(; tp < tend; tp++) {
490 cache = (cache << CHAR_BIT) + *tp;
491 bits += CHAR_BIT;
492 while(bits >= 7) {
493 bits -= 7;
494 *bp++ = 0x80 | (cache >> bits);
495 }
496 }
497 if(bits) *bp++ = cache;
498 bp[-1] &= 0x7f; /* Clear the last bit */
499
500 return bp - arcbuf;
501}
502
Lev Walkinf15320b2004-06-03 03:38:44 +0000503int
Lev Walkinb1a15552005-12-22 21:39:44 +0000504OBJECT_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 +0000505 uint8_t *buf;
506 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000507 unsigned LE = 1; /* Little endian (x86) */
508 unsigned isLittleEndian = *((char *)&LE);
509 unsigned int arc0;
510 unsigned int arc1;
511 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000512 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000513
Lev Walkin523de9e2006-08-18 01:34:18 +0000514 if(!oid || !arcs || arc_type_size < 1
515 || arc_type_size > 16
516 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 errno = EINVAL;
518 return -1;
519 }
520
Lev Walkin0787ff02004-06-17 23:43:39 +0000521 switch(arc_type_size) {
522 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000523 arc0 = ((const unsigned char *)arcs)[0];
524 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000525 break;
526 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000527 arc0 = ((const unsigned short *)arcs)[0];
528 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000529 break;
530 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000531 arc0 = ((const unsigned int *)arcs)[0];
532 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000533 break;
534 default:
535 arc1 = arc0 = 0;
536 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000537 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000538 /* If more significant bytes are present,
539 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000540 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000541 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000542 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
543 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000544 arc0 = *((const unsigned char *)arcs + 0);
545 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000546 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000547 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000548 /* If more significant bytes are present,
549 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000550 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000551 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000552 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
553 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000554 }
555 }
556
557 /*
558 * The previous chapter left us with the first and the second arcs.
559 * The values are not precise (that is, they are valid only if
560 * they're less than 255), but OK for the purposes of making
561 * the sanity test below.
562 */
563 if(arc0 <= 1) {
564 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000565 /* 8.19.4: At most 39 subsequent values (including 0) */
566 errno = ERANGE;
567 return -1;
568 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000569 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 /* 8.19.4: Only three values are allocated from the root node */
571 errno = ERANGE;
572 return -1;
573 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000574 /*
575 * After above tests it is known that the value of arc0 is completely
576 * trustworthy (0..2). However, the arc1's value is still meaningless.
577 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000578
579 /*
580 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000581 * This estimation implicitly takes in account the following facts,
582 * that cancel each other:
583 * * the first two arcs are encoded in a single value.
584 * * the first value may require more space (+1 byte)
585 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000587 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000588 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000589 if(!buf) {
590 /* ENOMEM */
591 return -1;
592 }
593
594 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000595 * Encode the first two arcs.
596 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000599 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000600 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000601 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000602
Lev Walkin0787ff02004-06-17 23:43:39 +0000603 /*
604 * Simulate first_value = arc0 * 40 + arc1;
605 */
606 /* Copy the second (1'st) arcs[1] into the first_value */
607 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000608 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000609 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000610 const uint8_t *aend = (const unsigned char *)arcs - 1;
611 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000612 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000613 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000614 const uint8_t *a1 = (const uint8_t *)arcs;
615 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000616 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000617 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000618 /* Increase the first_value by arc0 */
619 arc0 *= 40; /* (0..80) */
620 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
621 unsigned int v = *tp;
622 v += arc0;
623 *tp = v;
624 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
625 else break;
626 }
627
628 assert(tp >= first_value);
629
630 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
631 fv - first_value, 1);
632 }
633
634 /*
635 * Save the rest of arcs.
636 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000637 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000638 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000639 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000640 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
641 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000642 }
643
Lev Walkin34b2a932004-06-17 23:46:45 +0000644 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000645
646 /*
647 * Replace buffer.
648 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000649 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000650 bp = oid->buf;
651 oid->buf = buf;
652 if(bp) FREEMEM(bp);
653
654 return 0;
655}
Lev Walkinc4c61962004-06-14 08:17:27 +0000656
Lev Walkin92302252004-10-23 10:16:51 +0000657
658int
659OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000660 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000661 unsigned int arcs_count = 0;
662 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000663 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700664 ST_LEADSPACE,
665 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700666 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800667 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700668 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000669
670 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000671 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000672 errno = EINVAL;
673 return -1;
674 }
675
676 if(oid_txt_length == -1)
677 oid_txt_length = strlen(oid_text);
678
Lev Walkincad560a2013-03-16 07:00:58 -0700679#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
680 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700681 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700682 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700683 case ASN_STRTOX_EXTRA_DATA: \
684 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700685 if(arcs_count < arcs_slots) \
686 arcs[arcs_count] = value; \
687 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700688 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700689 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700690 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700691 if(opt_oid_text_end) \
692 *opt_oid_text_end = oid_text; \
693 errno = ERANGE; \
694 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700695 case ASN_STRTOX_ERROR_INVAL: \
696 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700697 if(opt_oid_text_end) \
698 *opt_oid_text_end = oid_text; \
699 errno = EINVAL; \
700 return -1; \
701 } \
702 } while(0)
703
Lev Walkin92302252004-10-23 10:16:51 +0000704 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
705 switch(*oid_text) {
706 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700707 switch(state) {
708 case ST_LEADSPACE:
709 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000710 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700711 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700712 state = ST_TAILSPACE;
713 continue;
714 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700715 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000716 }
Lev Walkincad560a2013-03-16 07:00:58 -0700717 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700718 case 0x2e: /* '.' */
719 switch(state) {
720 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700721 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700722 case ST_WAITDIGITS:
723 if(opt_oid_text_end)
724 *opt_oid_text_end = oid_text;
725 errno = EINVAL; /* Broken OID */
726 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700727 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700728 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700729 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000730 continue;
731 }
Lev Walkinf5927112012-09-03 00:48:45 -0700732 break;
733 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
734 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
735 switch(state) {
736 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700737 case ST_AFTERVALUE:
738 if(opt_oid_text_end)
739 *opt_oid_text_end = oid_text;
740 errno = EINVAL; /* "1. 1" => broken OID */
741 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700742 case ST_LEADSPACE:
743 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700744 _OID_CAPTURE_ARC(oid_text, oid_end);
745 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700746 continue;
747 }
748 break;
Lev Walkin92302252004-10-23 10:16:51 +0000749 default:
750 /* Unexpected symbols */
751 state = ST_WAITDIGITS;
752 break;
753 } /* switch() */
754 break;
755 } /* for() */
756
757
Lev Walkin093ba2e2005-04-04 21:10:06 +0000758 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000759
760 /* Finalize last arc */
761 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700762 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700763 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700764 case ST_WAITDIGITS:
765 errno = EINVAL; /* Broken OID */
766 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700767 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700768 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000769 return arcs_count;
770 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700771
772 errno = EINVAL; /* Broken OID */
773 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000774}
775
776