Harald Welte | d523a69 | 2015-08-30 23:11:19 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <asn_internal.h> |
| 6 | #include <INTEGER.h> |
| 7 | #include <OBJECT_IDENTIFIER.h> |
| 8 | #include <OCTET_STRING.h> |
| 9 | #include <limits.h> /* for CHAR_BIT */ |
| 10 | #include <errno.h> |
| 11 | |
| 12 | /* |
| 13 | * OBJECT IDENTIFIER basic type description. |
| 14 | */ |
| 15 | static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = { |
| 16 | (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 17 | }; |
| 18 | asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = { |
| 19 | "OBJECT IDENTIFIER", |
| 20 | "OBJECT_IDENTIFIER", |
| 21 | ASN__PRIMITIVE_TYPE_free, |
| 22 | OBJECT_IDENTIFIER_print, |
| 23 | OBJECT_IDENTIFIER_constraint, |
| 24 | ber_decode_primitive, |
| 25 | der_encode_primitive, |
| 26 | OBJECT_IDENTIFIER_decode_xer, |
| 27 | OBJECT_IDENTIFIER_encode_xer, |
| 28 | OCTET_STRING_decode_uper, |
| 29 | OCTET_STRING_encode_uper, |
| 30 | OCTET_STRING_decode_aper, |
| 31 | OCTET_STRING_encode_aper, |
| 32 | 0, /* Use generic outmost tag fetcher */ |
| 33 | asn_DEF_OBJECT_IDENTIFIER_tags, |
| 34 | sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 35 | / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 36 | asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */ |
| 37 | sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 38 | / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 39 | 0, /* No PER visible constraints */ |
| 40 | 0, 0, /* No members */ |
| 41 | 0 /* No specifics */ |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | int |
| 46 | OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
| 47 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 48 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 49 | |
| 50 | if(st && st->buf) { |
| 51 | if(st->size < 1) { |
| 52 | _ASN_CTFAIL(app_key, td, sptr, |
| 53 | "%s: at least one numerical value " |
| 54 | "expected (%s:%d)", |
| 55 | td->name, __FILE__, __LINE__); |
| 56 | return -1; |
| 57 | } |
| 58 | } else { |
| 59 | _ASN_CTFAIL(app_key, td, sptr, |
| 60 | "%s: value not given (%s:%d)", |
| 61 | td->name, __FILE__, __LINE__); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | int |
| 70 | OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) { |
| 71 | unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */ |
| 72 | const uint8_t *arcend = arcbuf + arclen; /* End of arc */ |
| 73 | unsigned int cache = 0; /* No more than 14 significant bits */ |
| 74 | unsigned char *rvbuf = (unsigned char *)rvbufp; |
| 75 | unsigned char *rvstart = rvbuf; /* Original start of the value buffer */ |
| 76 | int inc; /* Return value growth direction */ |
| 77 | |
| 78 | rvsize *= CHAR_BIT; /* bytes to bits */ |
| 79 | arclen *= 7; /* bytes to bits */ |
| 80 | |
| 81 | /* |
| 82 | * The arc has the number of bits |
| 83 | * cannot be represented using supplied return value type. |
| 84 | */ |
| 85 | if(arclen > rvsize) { |
| 86 | if(arclen > (rvsize + CHAR_BIT)) { |
| 87 | errno = ERANGE; /* Overflow */ |
| 88 | return -1; |
| 89 | } else { |
| 90 | /* |
| 91 | * Even if the number of bits in the arc representation |
| 92 | * is higher than the width of supplied * return value |
| 93 | * type, there is still possible to fit it when there |
| 94 | * are few unused high bits in the arc value |
| 95 | * representaion. |
| 96 | * |
| 97 | * Moreover, there is a possibility that the |
| 98 | * number could actually fit the arc space, given |
| 99 | * that add is negative, but we don't handle |
| 100 | * such "temporary lack of precision" situation here. |
| 101 | * May be considered as a bug. |
| 102 | */ |
| 103 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 104 | if((*arcbuf & mask)) { |
| 105 | errno = ERANGE; /* Overflow */ |
| 106 | return -1; |
| 107 | } |
| 108 | /* Fool the routine computing unused bits */ |
| 109 | arclen -= 7; |
| 110 | cache = *arcbuf & 0x7f; |
| 111 | arcbuf++; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* Faster path for common size */ |
| 116 | if(rvsize == (CHAR_BIT * sizeof(unsigned long))) { |
| 117 | unsigned long accum; |
| 118 | /* Gather all bits into the accumulator */ |
| 119 | for(accum = cache; arcbuf < arcend; arcbuf++) |
| 120 | accum = (accum << 7) | (*arcbuf & ~0x80); |
| 121 | if(accum < (unsigned)-add) { |
| 122 | errno = ERANGE; /* Overflow */ |
| 123 | return -1; |
| 124 | } |
| 125 | *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */ |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | #ifndef WORDS_BIGENDIAN |
| 130 | if(*(unsigned char *)&LE) { /* Little endian (x86) */ |
| 131 | /* "Convert" to big endian */ |
| 132 | rvbuf += rvsize / CHAR_BIT - 1; |
| 133 | rvstart--; |
| 134 | inc = -1; /* Descending */ |
| 135 | } else |
| 136 | #endif /* !WORDS_BIGENDIAN */ |
| 137 | inc = +1; /* Big endian is known [at compile time] */ |
| 138 | |
| 139 | { |
| 140 | int bits; /* typically no more than 3-4 bits */ |
| 141 | |
| 142 | /* Clear the high unused bits */ |
| 143 | for(bits = rvsize - arclen; |
| 144 | bits > CHAR_BIT; |
| 145 | rvbuf += inc, bits -= CHAR_BIT) |
| 146 | *rvbuf = 0; |
| 147 | |
| 148 | /* Fill the body of a value */ |
| 149 | for(; arcbuf < arcend; arcbuf++) { |
| 150 | cache = (cache << 7) | (*arcbuf & 0x7f); |
| 151 | bits += 7; |
| 152 | if(bits >= CHAR_BIT) { |
| 153 | bits -= CHAR_BIT; |
| 154 | *rvbuf = (cache >> bits); |
| 155 | rvbuf += inc; |
| 156 | } |
| 157 | } |
| 158 | if(bits) { |
| 159 | *rvbuf = cache; |
| 160 | rvbuf += inc; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if(add) { |
| 165 | for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) { |
| 166 | int v = add + *rvbuf; |
| 167 | if(v & (-1 << CHAR_BIT)) { |
| 168 | *rvbuf = (unsigned char)(v + (1 << CHAR_BIT)); |
| 169 | add = -1; |
| 170 | } else { |
| 171 | *rvbuf = v; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | if(rvbuf == rvstart) { |
| 176 | /* No space to carry over */ |
| 177 | errno = ERANGE; /* Overflow */ |
| 178 | return -1; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | ssize_t |
| 186 | OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add, |
| 187 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 188 | char scratch[64]; /* Conservative estimate */ |
| 189 | unsigned long accum; /* Bits accumulator */ |
| 190 | char *p; /* Position in the scratch buffer */ |
| 191 | |
| 192 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 193 | &accum, sizeof(accum))) |
| 194 | return -1; |
| 195 | |
| 196 | if(accum) { |
| 197 | ssize_t len; |
| 198 | |
| 199 | /* Fill the scratch buffer in reverse. */ |
| 200 | p = scratch + sizeof(scratch); |
| 201 | for(; accum; accum /= 10) |
| 202 | *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */ |
| 203 | |
| 204 | len = sizeof(scratch) - (p - scratch); |
| 205 | if(cb(p, len, app_key) < 0) |
| 206 | return -1; |
| 207 | return len; |
| 208 | } else { |
| 209 | *scratch = 0x30; |
| 210 | if(cb(scratch, 1, app_key) < 0) |
| 211 | return -1; |
| 212 | return 1; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int |
| 217 | OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add, |
| 218 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 219 | |
| 220 | if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0) |
| 221 | return -1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static ssize_t |
| 227 | OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
| 228 | ssize_t wrote_len = 0; |
| 229 | int startn; |
| 230 | int add = 0; |
| 231 | int i; |
| 232 | |
| 233 | for(i = 0, startn = 0; i < st->size; i++) { |
| 234 | uint8_t b = st->buf[i]; |
| 235 | if((b & 0x80)) /* Continuation expected */ |
| 236 | continue; |
| 237 | |
| 238 | if(startn == 0) { |
| 239 | /* |
| 240 | * First two arcs are encoded through the backdoor. |
| 241 | */ |
| 242 | if(i) { |
| 243 | add = -80; |
| 244 | if(cb("2", 1, app_key) < 0) return -1; |
| 245 | } else if(b <= 39) { |
| 246 | add = 0; |
| 247 | if(cb("0", 1, app_key) < 0) return -1; |
| 248 | } else if(b < 79) { |
| 249 | add = -40; |
| 250 | if(cb("1", 1, app_key) < 0) return -1; |
| 251 | } else { |
| 252 | add = -80; |
| 253 | if(cb("2", 1, app_key) < 0) return -1; |
| 254 | } |
| 255 | wrote_len += 1; |
| 256 | } |
| 257 | |
| 258 | if(cb(".", 1, app_key) < 0) /* Separate arcs */ |
| 259 | return -1; |
| 260 | |
| 261 | add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn], |
| 262 | i - startn + 1, add, cb, app_key); |
| 263 | if(add < 0) return -1; |
| 264 | wrote_len += 1 + add; |
| 265 | startn = i + 1; |
| 266 | add = 0; |
| 267 | } |
| 268 | |
| 269 | return wrote_len; |
| 270 | } |
| 271 | |
| 272 | static enum xer_pbd_rval |
| 273 | OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) { |
| 274 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr; |
| 275 | const char *chunk_end = (const char *)chunk_buf + chunk_size; |
| 276 | const char *endptr; |
| 277 | long s_arcs[10]; |
| 278 | long *arcs = s_arcs; |
| 279 | int arcs_count; |
| 280 | int ret; |
| 281 | |
| 282 | (void)td; |
| 283 | |
| 284 | arcs_count = OBJECT_IDENTIFIER_parse_arcs( |
| 285 | (const char *)chunk_buf, chunk_size, arcs, |
| 286 | sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr); |
| 287 | if(arcs_count < 0) { |
| 288 | /* Expecting more than zero arcs */ |
| 289 | return XPBD_BROKEN_ENCODING; |
| 290 | } else if(arcs_count == 0) { |
| 291 | return XPBD_NOT_BODY_IGNORE; |
| 292 | } |
| 293 | assert(endptr == chunk_end); |
| 294 | |
| 295 | if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) { |
| 296 | arcs = (long *)MALLOC(arcs_count * sizeof(long)); |
| 297 | if(!arcs) return XPBD_SYSTEM_FAILURE; |
| 298 | ret = OBJECT_IDENTIFIER_parse_arcs( |
| 299 | (const char *)chunk_buf, chunk_size, |
| 300 | arcs, arcs_count, &endptr); |
| 301 | if(ret != arcs_count) |
| 302 | return XPBD_SYSTEM_FAILURE; /* assert?.. */ |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Convert arcs into BER representation. |
| 307 | */ |
| 308 | ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count); |
| 309 | if(arcs != s_arcs) FREEMEM(arcs); |
| 310 | |
| 311 | return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED; |
| 312 | } |
| 313 | |
| 314 | asn_dec_rval_t |
| 315 | OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 316 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 317 | const void *buf_ptr, size_t size) { |
| 318 | |
| 319 | return xer_decode_primitive(opt_codec_ctx, td, |
| 320 | sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname, |
| 321 | buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode); |
| 322 | } |
| 323 | |
| 324 | asn_enc_rval_t |
| 325 | OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 326 | int ilevel, enum xer_encoder_flags_e flags, |
| 327 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 328 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 329 | asn_enc_rval_t er; |
| 330 | |
| 331 | (void)ilevel; |
| 332 | (void)flags; |
| 333 | |
| 334 | if(!st || !st->buf) |
| 335 | _ASN_ENCODE_FAILED; |
| 336 | |
| 337 | er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key); |
| 338 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 339 | |
| 340 | _ASN_ENCODED_OK(er); |
| 341 | } |
| 342 | |
| 343 | int |
| 344 | OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr, |
| 345 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
| 346 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 347 | |
| 348 | (void)td; /* Unused argument */ |
| 349 | (void)ilevel; /* Unused argument */ |
| 350 | |
| 351 | if(!st || !st->buf) |
| 352 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 353 | |
| 354 | /* Dump preamble */ |
| 355 | if(cb("{ ", 2, app_key) < 0) |
| 356 | return -1; |
| 357 | |
| 358 | if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) |
| 359 | return -1; |
| 360 | |
| 361 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
| 362 | } |
| 363 | |
| 364 | int |
| 365 | OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 366 | unsigned int arc_type_size, unsigned int arc_slots) { |
| 367 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
| 368 | int num_arcs = 0; |
| 369 | int startn = 0; |
| 370 | int add = 0; |
| 371 | int i; |
| 372 | |
| 373 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
| 374 | errno = EINVAL; |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | for(i = 0; i < oid->size; i++) { |
| 379 | uint8_t b = oid->buf[i]; |
| 380 | if((b & 0x80)) /* Continuation expected */ |
| 381 | continue; |
| 382 | |
| 383 | if(num_arcs == 0) { |
| 384 | /* |
| 385 | * First two arcs are encoded through the backdoor. |
| 386 | */ |
| 387 | unsigned LE = 1; /* Little endian */ |
| 388 | int first_arc; |
| 389 | num_arcs++; |
| 390 | if(!arc_slots) { num_arcs++; continue; } |
| 391 | |
| 392 | if(i) first_arc = 2; |
| 393 | else if(b <= 39) first_arc = 0; |
| 394 | else if(b < 79) first_arc = 1; |
| 395 | else first_arc = 2; |
| 396 | |
| 397 | add = -40 * first_arc; |
| 398 | memset(arcs, 0, arc_type_size); |
| 399 | *(unsigned char *)((char *)arcs |
| 400 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 401 | = first_arc; |
| 402 | arcs = ((char *)arcs) + arc_type_size; |
| 403 | } |
| 404 | |
| 405 | /* Decode, if has space */ |
| 406 | if(arcs < arcs_end) { |
| 407 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 408 | i - startn + 1, add, |
| 409 | arcs, arc_type_size)) |
| 410 | return -1; |
| 411 | startn = i + 1; |
| 412 | arcs = ((char *)arcs) + arc_type_size; |
| 413 | add = 0; |
| 414 | } |
| 415 | num_arcs++; |
| 416 | } |
| 417 | |
| 418 | return num_arcs; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /* |
| 423 | * Save the single value as an object identifier arc. |
| 424 | */ |
| 425 | int |
| 426 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) { |
| 427 | /* |
| 428 | * The following conditions must hold: |
| 429 | * assert(arcval); |
| 430 | * assert(arcval_size > 0); |
| 431 | * assert(arcval_size <= 16); |
| 432 | * assert(arcbuf); |
| 433 | */ |
| 434 | #ifdef WORDS_BIGENDIAN |
| 435 | const unsigned isLittleEndian = 0; |
| 436 | #else |
| 437 | unsigned LE = 1; |
| 438 | unsigned isLittleEndian = *(char *)&LE; |
| 439 | #endif |
| 440 | const uint8_t *tend, *tp; |
| 441 | unsigned int cache; |
| 442 | uint8_t *bp = arcbuf; |
| 443 | int bits; |
| 444 | uint8_t buffer[16]; |
| 445 | |
| 446 | if(isLittleEndian && !prepared_order) { |
| 447 | const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1; |
| 448 | const uint8_t *aend = (const uint8_t *)arcval; |
| 449 | uint8_t *msb = buffer + arcval_size - 1; |
| 450 | uint8_t *tb; |
| 451 | for(tb = buffer; a >= aend; tb++, a--) |
| 452 | if((*tb = *a) && (tb < msb)) |
| 453 | msb = tb; |
| 454 | tend = &buffer[arcval_size]; |
| 455 | tp = msb; /* Most significant non-zero byte */ |
| 456 | } else { |
| 457 | /* Look for most significant non-zero byte */ |
| 458 | tend = (const unsigned char *)arcval + arcval_size; |
| 459 | for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++) |
| 460 | if(*tp) break; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Split the value in 7-bits chunks. |
| 465 | */ |
| 466 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 467 | if(bits) { |
| 468 | cache = *tp >> (CHAR_BIT - bits); |
| 469 | if(cache) { |
| 470 | *bp++ = cache | 0x80; |
| 471 | cache = *tp++; |
| 472 | bits = CHAR_BIT - bits; |
| 473 | } else { |
| 474 | bits = -bits; |
| 475 | } |
| 476 | } else { |
| 477 | cache = 0; |
| 478 | } |
| 479 | for(; tp < tend; tp++) { |
| 480 | cache = (cache << CHAR_BIT) + *tp; |
| 481 | bits += CHAR_BIT; |
| 482 | while(bits >= 7) { |
| 483 | bits -= 7; |
| 484 | *bp++ = 0x80 | (cache >> bits); |
| 485 | } |
| 486 | } |
| 487 | if(bits) *bp++ = cache; |
| 488 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 489 | |
| 490 | return bp - arcbuf; |
| 491 | } |
| 492 | |
| 493 | int |
| 494 | OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
| 495 | uint8_t *buf; |
| 496 | uint8_t *bp; |
| 497 | unsigned LE = 1; /* Little endian (x86) */ |
| 498 | unsigned isLittleEndian = *((char *)&LE); |
| 499 | unsigned int arc0; |
| 500 | unsigned int arc1; |
| 501 | unsigned size; |
| 502 | unsigned i; |
| 503 | |
| 504 | if(!oid || !arcs || arc_type_size < 1 |
| 505 | || arc_type_size > 16 |
| 506 | || arc_slots < 2) { |
| 507 | errno = EINVAL; |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | switch(arc_type_size) { |
| 512 | case sizeof(char): |
| 513 | arc0 = ((const unsigned char *)arcs)[0]; |
| 514 | arc1 = ((const unsigned char *)arcs)[1]; |
| 515 | break; |
| 516 | case sizeof(short): |
| 517 | arc0 = ((const unsigned short *)arcs)[0]; |
| 518 | arc1 = ((const unsigned short *)arcs)[1]; |
| 519 | break; |
| 520 | case sizeof(int): |
| 521 | arc0 = ((const unsigned int *)arcs)[0]; |
| 522 | arc1 = ((const unsigned int *)arcs)[1]; |
| 523 | break; |
| 524 | default: |
| 525 | arc1 = arc0 = 0; |
| 526 | if(isLittleEndian) { /* Little endian (x86) */ |
| 527 | const unsigned char *ps, *pe; |
| 528 | /* If more significant bytes are present, |
| 529 | * make them > 255 quick */ |
| 530 | for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 531 | ps < pe; ps++) |
| 532 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 533 | arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
| 534 | arc0 = *((const unsigned char *)arcs + 0); |
| 535 | arc1 = *((const unsigned char *)arcs + arc_type_size); |
| 536 | } else { |
| 537 | const unsigned char *ps, *pe; |
| 538 | /* If more significant bytes are present, |
| 539 | * make them > 255 quick */ |
| 540 | for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++) |
| 541 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 542 | arc0 = *((const unsigned char *)arcs + arc_type_size - 1); |
| 543 | arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * The previous chapter left us with the first and the second arcs. |
| 549 | * The values are not precise (that is, they are valid only if |
| 550 | * they're less than 255), but OK for the purposes of making |
| 551 | * the sanity test below. |
| 552 | */ |
| 553 | if(arc0 <= 1) { |
| 554 | if(arc1 >= 39) { |
| 555 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 556 | errno = ERANGE; |
| 557 | return -1; |
| 558 | } |
| 559 | } else if(arc0 > 2) { |
| 560 | /* 8.19.4: Only three values are allocated from the root node */ |
| 561 | errno = ERANGE; |
| 562 | return -1; |
| 563 | } |
| 564 | /* |
| 565 | * After above tests it is known that the value of arc0 is completely |
| 566 | * trustworthy (0..2). However, the arc1's value is still meaningless. |
| 567 | */ |
| 568 | |
| 569 | /* |
| 570 | * Roughly estimate the maximum size necessary to encode these arcs. |
| 571 | * This estimation implicitly takes in account the following facts, |
| 572 | * that cancel each other: |
| 573 | * * the first two arcs are encoded in a single value. |
| 574 | * * the first value may require more space (+1 byte) |
| 575 | * * the value of the first arc which is in range (0..2) |
| 576 | */ |
| 577 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
| 578 | bp = buf = (uint8_t *)MALLOC(size + 1); |
| 579 | if(!buf) { |
| 580 | /* ENOMEM */ |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Encode the first two arcs. |
| 586 | * These require special treatment. |
| 587 | */ |
| 588 | { |
| 589 | uint8_t *tp; |
| 590 | uint8_t first_value[1 + 16]; /* of two arcs */ |
| 591 | uint8_t *fv = first_value; |
| 592 | |
| 593 | /* |
| 594 | * Simulate first_value = arc0 * 40 + arc1; |
| 595 | */ |
| 596 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 597 | *fv++ = 0; |
| 598 | arcs = ((const char *)arcs) + arc_type_size; |
| 599 | if(isLittleEndian) { |
| 600 | const uint8_t *aend = (const unsigned char *)arcs - 1; |
| 601 | const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1; |
| 602 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
| 603 | } else { |
| 604 | const uint8_t *a1 = (const uint8_t *)arcs; |
| 605 | const uint8_t *aend = a1 + arc_type_size; |
| 606 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
| 607 | } |
| 608 | /* Increase the first_value by arc0 */ |
| 609 | arc0 *= 40; /* (0..80) */ |
| 610 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 611 | unsigned int v = *tp; |
| 612 | v += arc0; |
| 613 | *tp = v; |
| 614 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 615 | else break; |
| 616 | } |
| 617 | |
| 618 | assert(tp >= first_value); |
| 619 | |
| 620 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 621 | fv - first_value, 1); |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Save the rest of arcs. |
| 626 | */ |
| 627 | for(arcs = ((const char *)arcs) + arc_type_size, i = 2; |
| 628 | i < arc_slots; |
| 629 | i++, arcs = ((const char *)arcs) + arc_type_size) { |
| 630 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 631 | arcs, arc_type_size, 0); |
| 632 | } |
| 633 | |
| 634 | assert((unsigned)(bp - buf) <= size); |
| 635 | |
| 636 | /* |
| 637 | * Replace buffer. |
| 638 | */ |
| 639 | oid->size = bp - buf; |
| 640 | bp = oid->buf; |
| 641 | oid->buf = buf; |
| 642 | if(bp) FREEMEM(bp); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | int |
| 649 | OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length, |
| 650 | long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) { |
| 651 | unsigned int arcs_count = 0; |
| 652 | const char *oid_end; |
| 653 | enum { |
| 654 | ST_LEADSPACE, |
| 655 | ST_TAILSPACE, |
| 656 | ST_AFTERVALUE, /* Next character ought to be '.' or a space */ |
| 657 | ST_WAITDIGITS, /* Next character is expected to be a digit */ |
| 658 | } state = ST_LEADSPACE; |
| 659 | |
| 660 | if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) { |
| 661 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
| 662 | errno = EINVAL; |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | if(oid_txt_length == -1) |
| 667 | oid_txt_length = strlen(oid_text); |
| 668 | |
| 669 | #define _OID_CAPTURE_ARC(oid_text, oid_end) do { \ |
| 670 | const char *endp = oid_end; \ |
| 671 | long value; \ |
| 672 | switch(asn_strtol_lim(oid_text, &endp, &value)) { \ |
| 673 | case ASN_STRTOL_EXTRA_DATA: \ |
| 674 | case ASN_STRTOL_OK: \ |
| 675 | if(arcs_count < arcs_slots) \ |
| 676 | arcs[arcs_count] = value; \ |
| 677 | arcs_count++; \ |
| 678 | oid_text = endp - 1; \ |
| 679 | break; \ |
| 680 | case ASN_STRTOL_ERROR_RANGE: \ |
| 681 | if(opt_oid_text_end) \ |
| 682 | *opt_oid_text_end = oid_text; \ |
| 683 | errno = ERANGE; \ |
| 684 | return -1; \ |
| 685 | case ASN_STRTOL_ERROR_INVAL: \ |
| 686 | case ASN_STRTOL_EXPECT_MORE: \ |
| 687 | if(opt_oid_text_end) \ |
| 688 | *opt_oid_text_end = oid_text; \ |
| 689 | errno = EINVAL; \ |
| 690 | return -1; \ |
| 691 | } \ |
| 692 | } while(0) |
| 693 | |
| 694 | for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) { |
| 695 | switch(*oid_text) { |
| 696 | case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */ |
| 697 | switch(state) { |
| 698 | case ST_LEADSPACE: |
| 699 | case ST_TAILSPACE: |
| 700 | continue; |
| 701 | case ST_AFTERVALUE: |
| 702 | state = ST_TAILSPACE; |
| 703 | continue; |
| 704 | case ST_WAITDIGITS: |
| 705 | break; /* Digits expected after ".", got whitespace */ |
| 706 | } |
| 707 | break; |
| 708 | case 0x2e: /* '.' */ |
| 709 | switch(state) { |
| 710 | case ST_LEADSPACE: |
| 711 | case ST_TAILSPACE: |
| 712 | case ST_WAITDIGITS: |
| 713 | if(opt_oid_text_end) |
| 714 | *opt_oid_text_end = oid_text; |
| 715 | errno = EINVAL; /* Broken OID */ |
| 716 | return -1; |
| 717 | break; |
| 718 | case ST_AFTERVALUE: |
| 719 | state = ST_WAITDIGITS; |
| 720 | continue; |
| 721 | } |
| 722 | break; |
| 723 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 724 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 725 | switch(state) { |
| 726 | case ST_TAILSPACE: |
| 727 | case ST_AFTERVALUE: |
| 728 | if(opt_oid_text_end) |
| 729 | *opt_oid_text_end = oid_text; |
| 730 | errno = EINVAL; /* "1. 1" => broken OID */ |
| 731 | return -1; |
| 732 | case ST_LEADSPACE: |
| 733 | case ST_WAITDIGITS: |
| 734 | _OID_CAPTURE_ARC(oid_text, oid_end); |
| 735 | state = ST_AFTERVALUE; |
| 736 | continue; |
| 737 | } |
| 738 | break; |
| 739 | default: |
| 740 | /* Unexpected symbols */ |
| 741 | state = ST_WAITDIGITS; |
| 742 | break; |
| 743 | } /* switch() */ |
| 744 | break; |
| 745 | } /* for() */ |
| 746 | |
| 747 | |
| 748 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
| 749 | |
| 750 | /* Finalize last arc */ |
| 751 | switch(state) { |
| 752 | case ST_LEADSPACE: |
| 753 | return 0; /* No OID found in input data */ |
| 754 | case ST_WAITDIGITS: |
| 755 | errno = EINVAL; /* Broken OID */ |
| 756 | return -1; |
| 757 | case ST_AFTERVALUE: |
| 758 | case ST_TAILSPACE: |
| 759 | return arcs_count; |
| 760 | } |
| 761 | |
| 762 | errno = EINVAL; /* Broken OID */ |
| 763 | return -1; |
| 764 | } |
| 765 | |
| 766 | |