Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 *
 * Author: Harti Brandt <harti@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Begemot: bsnmp/lib/asn1.c,v 1.31 2005/10/06 07:14:58 brandt_h Exp $
 *
 * ASN.1 for SNMP.
 */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#endif
#include <assert.h>

#include "support.h"
#include "asn1.h"

static void asn_error_func(const struct asn_buf *, const char *, ...);

void (*asn_error)(const struct asn_buf *, const char *, ...) = asn_error_func;

/*
 * Read the next header. This reads the tag (note, that only single
 * byte tags are supported for now) and the length field. The length field
 * is restricted to a 32-bit value.
 * All errors of this function stop the decoding.
 */
enum asn_err
asn_get_header(struct asn_buf *b, u_char *type, asn_len_t *len)
{
	u_int length;

	if (b->asn_len == 0) {
		asn_error(b, "no identifier for header");
		return (ASN_ERR_EOBUF);
	}
	*type = *b->asn_cptr;
	if ((*type & ASN_TYPE_MASK) > 0x30) {
		asn_error(b, "types > 0x30 not supported (%u)",
		    *type & ASN_TYPE_MASK);
		return (ASN_ERR_FAILED);
	}
	b->asn_cptr++;
	b->asn_len--;
	if (b->asn_len == 0) {
		asn_error(b, "no length field");
		return (ASN_ERR_EOBUF);
	}
	if (*b->asn_cptr & 0x80) {
		length = *b->asn_cptr++ & 0x7f;
		b->asn_len--;
		if (length == 0) {
			asn_error(b, "indefinite length not supported");
			return (ASN_ERR_FAILED);
		}
		if (length > ASN_MAXLENLEN) {
			asn_error(b, "long length too long (%u)", length);
			return (ASN_ERR_FAILED);
		}
		if (length > b->asn_len) {
			asn_error(b, "long length truncated");
			return (ASN_ERR_EOBUF);
		}
		*len = 0;
		while (length--) {
			*len = (*len << 8) | *b->asn_cptr++;
			b->asn_len--;
		}
	} else {
		*len = *b->asn_cptr++;
		b->asn_len--;
	}
	return (ASN_ERR_OK);
}

/*
 * Write a length field (restricted to values < 2^32-1) and return the
 * number of bytes this field takes. If ptr is NULL, the length is computed
 * but nothing is written. If the length would be too large return 0.
 */
static u_int
asn_put_len(u_char *ptr, asn_len_t len)
{
	u_int lenlen, lenlen1;
	asn_len_t tmp;

	if (len > ASN_MAXLEN) {
		asn_error(NULL, "encoding length too long: (%u)", len);
		return (0);
	}

	if (len <= 127) {
		if (ptr)
			*ptr++ = (u_char)len;
		return (1);
	} else {
		lenlen = 0;
		/* compute number of bytes for value (is at least 1) */
		for (tmp = len; tmp != 0; tmp >>= 8)
			lenlen++;
		if (ptr != NULL) {
			*ptr++ = (u_char)lenlen | 0x80;
			lenlen1 = lenlen;
			while (lenlen1-- > 0) {
				ptr[lenlen1] = len & 0xff;
				len >>= 8;
			}
		}
		return (lenlen + 1);
	}
}

/*
 * Write a header (tag and length fields).
 * Tags are restricted to one byte tags (value <= 0x30) and the
 * lenght field to 16-bit. All errors stop the encoding.
 */
enum asn_err
asn_put_header(struct asn_buf *b, u_char type, asn_len_t len)
{
	u_int lenlen;

	/* tag field */
	if ((type & ASN_TYPE_MASK) > 0x30) {
		asn_error(NULL, "types > 0x30 not supported (%u)",
		    type & ASN_TYPE_MASK);
		return (ASN_ERR_FAILED);
	}
	if (b->asn_len == 0)
		return (ASN_ERR_EOBUF);

	*b->asn_ptr++ = type;
	b->asn_len--;

	/* length field */
	if ((lenlen = asn_put_len(NULL, len)) == 0)
		return (ASN_ERR_FAILED);
	if (b->asn_len < lenlen)
		return (ASN_ERR_EOBUF);

	(void)asn_put_len(b->asn_ptr, len);
	b->asn_ptr += lenlen;
	b->asn_len -= lenlen;
	return (ASN_ERR_OK);
}


/*
 * This constructs a temporary sequence header with space for the maximum
 * length field (three byte). Set the pointer that ptr points to to the
 * start of the encoded header. This is used for a later call to
 * asn_commit_header which will fix-up the length field and move the
 * value if needed. All errors should stop the encoding.
 */
#define	TEMP_LEN (1 + ASN_MAXLENLEN + 1)
enum asn_err
asn_put_temp_header(struct asn_buf *b, u_char type, u_char **ptr)
{
	int ret;

	if (b->asn_len < TEMP_LEN)
		return (ASN_ERR_EOBUF);
	*ptr = b->asn_ptr;
	if ((ret = asn_put_header(b, type, ASN_MAXLEN)) == ASN_ERR_OK)
		assert(b->asn_ptr == *ptr + TEMP_LEN);
	return (ret);
}
enum asn_err
asn_commit_header(struct asn_buf *b, u_char *ptr, size_t *moved)
{
	asn_len_t len;
	u_int lenlen, shift;

	/* compute length of encoded value without header */
	len = b->asn_ptr - (ptr + TEMP_LEN);

	/* insert length. may not fail. */
	lenlen = asn_put_len(ptr + 1, len);
	if (lenlen > TEMP_LEN - 1)
		return (ASN_ERR_FAILED);

	if (lenlen < TEMP_LEN - 1) {
		/* shift value down */
		shift = (TEMP_LEN - 1) - lenlen;
		memmove(ptr + 1 + lenlen, ptr + TEMP_LEN, len);
		b->asn_ptr -= shift;
		b->asn_len += shift;
		if (moved != NULL)
			*moved = shift;
	}
	return (ASN_ERR_OK);
}
#undef TEMP_LEN

/*
 * BER integer. This may be used to get a signed 64 bit integer at maximum.
 * The maximum length should be checked by the caller. This cannot overflow
 * if the caller ensures that len is at maximum 8.
 *
 * <bytes>
 */
static enum asn_err
asn_get_real_integer(struct asn_buf *b, asn_len_t len, int64_t *vp)
{
	uint64_t val;
	int neg = 0;
	enum asn_err err;

	if (b->asn_len < len) {
		asn_error(b, "truncated integer");
		return (ASN_ERR_EOBUF);
	}
	if (len == 0) {
		asn_error(b, "zero-length integer");
		*vp = 0;
		return (ASN_ERR_BADLEN);
	}
	err = ASN_ERR_OK;
	if (len > 8)
		err = ASN_ERR_RANGE;
	else if (len > 1 &&
	    ((*b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) ||
	    (*b->asn_cptr == 0xff && (b->asn_cptr[1] & 0x80) == 0x80))) {
		asn_error(b, "non-minimal integer");
		err = ASN_ERR_BADLEN;
	}

	if (*b->asn_cptr & 0x80)
		neg = 1;
	val = 0;
	while (len--) {
		val <<= 8;
		val |= neg ? (u_char)~*b->asn_cptr : *b->asn_cptr;
		b->asn_len--;
		b->asn_cptr++;
	}
	if (neg) {
		*vp = -(int64_t)val - 1;
	} else
		*vp = (int64_t)val;
	return (err);
}

/*
 * Write a signed integer with the given type. The caller has to ensure
 * that the actual value is ok for this type.
 */
static enum asn_err
asn_put_real_integer(struct asn_buf *b, u_char type, int64_t ival)
{
	int i, neg = 0;
# define OCTETS 8
	u_char buf[OCTETS];
	uint64_t val;
	enum asn_err ret;

	if (ival < 0) {
		/* this may fail if |INT64_MIN| > |INT64_MAX| and
		 * the value is between * INT64_MIN <= ival < -(INT64_MAX+1) */
		val = (uint64_t)-(ival + 1);
		neg = 1;
	} else
		val = (uint64_t)ival;

	/* split the value into octets */
	for (i = OCTETS - 1; i >= 0; i--) {
		buf[i] = val & 0xff;
		if (neg)
			buf[i] = ~buf[i];
		val >>= 8;
	}
	/* no leading 9 zeroes or ones */
	for (i = 0; i < OCTETS - 1; i++)
		if (!((buf[i] == 0xff && (buf[i + 1] & 0x80) != 0) ||
		    (buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0)))
			break;
	if ((ret = asn_put_header(b, type, OCTETS - i)))
		return (ret);
	if (OCTETS - (u_int)i > b->asn_len)
		return (ASN_ERR_EOBUF);

	while (i < OCTETS) {
		*b->asn_ptr++ = buf[i++];
		b->asn_len--;
	}
	return (ASN_ERR_OK);
# undef OCTETS
}


/*
 * The same for unsigned 64-bitters. Here we have the problem, that overflow
 * can happen, because the value maybe 9 bytes long. In this case the
 * first byte must be 0.
 */
static enum asn_err
asn_get_real_unsigned(struct asn_buf *b, asn_len_t len, uint64_t *vp)
{
	enum asn_err err;

	if (b->asn_len < len) {
		asn_error(b, "truncated integer");
		return (ASN_ERR_EOBUF);
	}
	if (len == 0) {
		asn_error(b, "zero-length integer");
		*vp = 0;
		return (ASN_ERR_BADLEN);
	}
	err = ASN_ERR_OK;
	*vp = 0;
	if ((*b->asn_cptr & 0x80) || (len == 9 && *b->asn_cptr != 0)) {
		/* negative integer or too larger */
		*vp = 0xffffffffffffffffULL;
		err = ASN_ERR_RANGE;
	} else if (len > 1 &&
	    *b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) {
		asn_error(b, "non-minimal unsigned");
		err = ASN_ERR_BADLEN;
	}

	while (len--) {
		*vp = (*vp << 8) | *b->asn_cptr++;
		b->asn_len--;
	}
	return (err);
}


/*
 * Values with the msb on need 9 octets.
 */
static int
asn_put_real_unsigned(struct asn_buf *b, u_char type, uint64_t val)
{
	int i;
# define OCTETS 9
	u_char buf[OCTETS];
	enum asn_err ret;

	/* split the value into octets */
	for (i = OCTETS - 1; i >= 0; i--) {
		buf[i] = val & 0xff;
		val >>= 8;
	}
	/* no leading 9 zeroes */
	for (i = 0; i < OCTETS - 1; i++)
		if (!(buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0))
			break;
	if ((ret = asn_put_header(b, type, OCTETS - i)))
		return (ret);
	if (OCTETS - (u_int)i > b->asn_len)
		return (ASN_ERR_EOBUF);

	while (i < OCTETS) {
		*b->asn_ptr++ = buf[i++];
		b->asn_len--;
	}
#undef OCTETS
	return (ASN_ERR_OK);
}

/*
 * The ASN.1 INTEGER type is restricted to 32-bit signed by the SMI.
 */
enum asn_err
asn_get_integer_raw(struct asn_buf *b, asn_len_t len, int32_t *vp)
{
	int64_t val;
	enum asn_err ret;

	if ((ret = asn_get_real_integer(b, len, &val)) == ASN_ERR_OK) {
		if (len > 4)
			ret = ASN_ERR_BADLEN;
		else if (val > INT32_MAX || val < INT32_MIN)
			/* may not happen */
			ret = ASN_ERR_RANGE;
		*vp = (int32_t)val;
	}
	return (ret);
}

enum asn_err
asn_get_integer(struct asn_buf *b, int32_t *vp)
{
	asn_len_t len;
	u_char type;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != ASN_TYPE_INTEGER) {
		asn_error(b, "bad type for integer (%u)", type);
		return (ASN_ERR_TAG);
	}

	return (asn_get_integer_raw(b, len, vp));
}

enum asn_err
asn_put_integer(struct asn_buf *b, int32_t val)
{
	return (asn_put_real_integer(b, ASN_TYPE_INTEGER, val));
}

/*
 * OCTETSTRING
 *
 * <0x04> <len> <data ...>
 *
 * Get an octetstring. noctets must point to the buffer size and on
 * return will contain the size of the octetstring, regardless of the
 * buffer size.
 */
enum asn_err
asn_get_octetstring_raw(struct asn_buf *b, asn_len_t len, u_char *octets,
    u_int *noctets)
{
	enum asn_err err = ASN_ERR_OK;

	if (*noctets < len) {
		asn_error(b, "octetstring truncated");
		err = ASN_ERR_RANGE;
	}
	if (b->asn_len < len) {
		asn_error(b, "truncatet octetstring");
		return (ASN_ERR_EOBUF);
	}
	if (*noctets < len)
		memcpy(octets, b->asn_cptr, *noctets);
	else
		memcpy(octets, b->asn_cptr, len);
	*noctets = len;
	b->asn_cptr += len;
	b->asn_len -= len;
	return (err);
}

enum asn_err
asn_get_octetstring(struct asn_buf *b, u_char *octets, u_int *noctets)
{
	enum asn_err err;
	u_char type;
	asn_len_t len;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != ASN_TYPE_OCTETSTRING) {
		asn_error(b, "bad type for octetstring (%u)", type);
		return (ASN_ERR_TAG);
	}
	return (asn_get_octetstring_raw(b, len, octets, noctets));
}

enum asn_err
asn_put_octetstring(struct asn_buf *b, const u_char *octets, u_int noctets)
{
	enum asn_err ret;

	if ((ret = asn_put_header(b, ASN_TYPE_OCTETSTRING, noctets)) != ASN_ERR_OK)
		return (ret);
	if (b->asn_len < noctets)
		return (ASN_ERR_EOBUF);

	memcpy(b->asn_ptr, octets, noctets);
	b->asn_ptr += noctets;
	b->asn_len -= noctets;
	return (ASN_ERR_OK);
}

/*
 * NULL
 *
 * <0x05> <0x00>
 */
enum asn_err
asn_get_null_raw(struct asn_buf *b, asn_len_t len)
{
	if (len != 0) {
		if (b->asn_len < len) {
			asn_error(b, "truncated NULL");
			return (ASN_ERR_EOBUF);
		}
		asn_error(b, "bad length for NULL (%u)", len);
		b->asn_len -= len;
		b->asn_ptr += len;
		return (ASN_ERR_BADLEN);
	}
	return (ASN_ERR_OK);
}

enum asn_err
asn_get_null(struct asn_buf *b)
{
	u_char type;
	asn_len_t len;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != ASN_TYPE_NULL) {
		asn_error(b, "bad type for NULL (%u)", type);
		return (ASN_ERR_TAG);
	}
	return (asn_get_null_raw(b, len));
}

enum asn_err
asn_put_null(struct asn_buf *b)
{
	return (asn_put_header(b, ASN_TYPE_NULL, 0));
}

enum asn_err
asn_put_exception(struct asn_buf *b, u_int except)
{
	return (asn_put_header(b, ASN_CLASS_CONTEXT | except, 0));
}

/*
 * OBJID
 *
 * <0x06> <len> <subid...>
 */
enum asn_err
asn_get_objid_raw(struct asn_buf *b, asn_len_t len, struct asn_oid *oid)
{
	asn_subid_t subid;
	enum asn_err err;

	if (b->asn_len < len) {
		asn_error(b, "truncated OBJID");
		return (ASN_ERR_EOBUF);
	}
	oid->len = 0;
	if (len == 0) {
		asn_error(b, "short OBJID");
		oid->subs[oid->len++] = 0;
		oid->subs[oid->len++] = 0;
		return (ASN_ERR_BADLEN);
	}
	err = ASN_ERR_OK;
	while (len != 0) {
		if (oid->len == ASN_MAXOIDLEN) {
			asn_error(b, "OID too long (%u)", oid->len);
			b->asn_cptr += len;
			b->asn_len -= len;
			return (ASN_ERR_BADLEN);
		}
		subid = 0;
		do {
			if (len == 0) {
				asn_error(b, "unterminated subid");
				return (ASN_ERR_EOBUF);
			}
			if (subid > (ASN_MAXID >> 7)) {
				asn_error(b, "OBID subid too larger");
				err = ASN_ERR_RANGE;
			}
			subid = (subid << 7) | (*b->asn_cptr & 0x7f);
			len--;
			b->asn_len--;
		} while (*b->asn_cptr++ & 0x80);
		if (oid->len == 0) {
			if (subid < 80) {
				oid->subs[oid->len++] = subid / 40;
				oid->subs[oid->len++] = subid % 40;
			} else {
				oid->subs[oid->len++] = 2;
				oid->subs[oid->len++] = subid - 80;
			}
		} else {
			oid->subs[oid->len++] = subid;
		}
	}
	return (err);

}

enum asn_err
asn_get_objid(struct asn_buf *b, struct asn_oid *oid)
{
	u_char type;
	asn_len_t len;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != ASN_TYPE_OBJID) {
		asn_error(b, "bad type for OBJID (%u)", type);
		return (ASN_ERR_TAG);
	}
	return (asn_get_objid_raw(b, len, oid));
}

enum asn_err
asn_put_objid(struct asn_buf *b, const struct asn_oid *oid)
{
	asn_subid_t first, sub;
	enum asn_err err, err1;
	u_int i, oidlen;
	asn_len_t len;

	err = ASN_ERR_OK;
	if (oid->len == 0) {
		/* illegal */
		asn_error(NULL, "short oid");
		err = ASN_ERR_RANGE;
		first = 0;
		oidlen = 2;
	} else if (oid->len == 1) {
		/* illegal */
		asn_error(b, "short oid");
		if (oid->subs[0] > 2)
			asn_error(NULL, "oid[0] too large (%u)", oid->subs[0]);
		err = ASN_ERR_RANGE;
		first = oid->subs[0] * 40;
		oidlen = 2;
	} else {
		if (oid->len > ASN_MAXOIDLEN) {
			asn_error(NULL, "oid too long %u", oid->len);
			err = ASN_ERR_RANGE;
		}
		if (oid->subs[0] > 2 ||
		    (oid->subs[0] < 2 && oid->subs[1] >= 40)) {
			asn_error(NULL, "oid out of range (%u,%u)",
			    oid->subs[0], oid->subs[1]);
			err = ASN_ERR_RANGE;
		}
		first = 40 * oid->subs[0] + oid->subs[1];
		oidlen = oid->len;
	}
	len = 0;
	for (i = 1; i < oidlen; i++) {
		sub = (i == 1) ? first : oid->subs[i];
		if (sub > ASN_MAXID) {
			asn_error(NULL, "oid subid too large");
			err = ASN_ERR_RANGE;
		}
		len += (sub <= 0x7f) ? 1
		    : (sub <= 0x3fff) ? 2
		    : (sub <= 0x1fffff) ? 3
		    : (sub <= 0xfffffff) ? 4
		    : 5;
	}
	if ((err1 = asn_put_header(b, ASN_TYPE_OBJID, len)) != ASN_ERR_OK)
		return (err1);
	if (b->asn_len < len)
		return (ASN_ERR_EOBUF);

	for (i = 1; i < oidlen; i++) {
		sub = (i == 1) ? first : oid->subs[i];
		if (sub <= 0x7f) {
			*b->asn_ptr++ = sub;
			b->asn_len--;
		} else if (sub <= 0x3fff) {
			*b->asn_ptr++ = (sub >> 7) | 0x80;
			*b->asn_ptr++ = sub & 0x7f;
			b->asn_len -= 2;
		} else if (sub <= 0x1fffff) {
			*b->asn_ptr++ = (sub >> 14) | 0x80;
			*b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
			*b->asn_ptr++ = sub & 0x7f;
			b->asn_len -= 3;
		} else if (sub <= 0xfffffff) {
			*b->asn_ptr++ = (sub >> 21) | 0x80;
			*b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
			*b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
			*b->asn_ptr++ = sub & 0x7f;
			b->asn_len -= 4;
		} else {
			*b->asn_ptr++ = (sub >> 28) | 0x80;
			*b->asn_ptr++ = ((sub >> 21) & 0x7f) | 0x80;
			*b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
			*b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
			*b->asn_ptr++ = sub & 0x7f;
			b->asn_len -= 5;
		}
	}
	return (err);
}
/*
 * SEQUENCE header
 *
 * <0x10|0x20> <len> <data...>
 */
enum asn_err
asn_get_sequence(struct asn_buf *b, asn_len_t *len)
{
	u_char type;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, len)) != ASN_ERR_OK)
		return (err);
	if (type != (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED)) {
		asn_error(b, "bad sequence type %u", type);
		return (ASN_ERR_TAG);
	}
	if (*len > b->asn_len) {
		asn_error(b, "truncated sequence");
		return (ASN_ERR_EOBUF);
	}
	return (ASN_ERR_OK);
}

/*
 * Application types
 *
 * 0x40 4 MSB 2MSB 2LSB LSB
 */
enum asn_err
asn_get_ipaddress_raw(struct asn_buf *b, asn_len_t len, u_char *addr)
{
	u_int i;

	if (b->asn_len < len) {
		asn_error(b, "truncated ip-address");
		return (ASN_ERR_EOBUF);
	}
	if (len < 4) {
		asn_error(b, "short length for ip-Address %u", len);
		for (i = 0; i < len; i++)
			*addr++ = *b->asn_cptr++;
		while (i++ < len)
			*addr++ = 0;
		b->asn_len -= len;
		return (ASN_ERR_BADLEN);
	}
	for (i = 0; i < 4; i++)
		*addr++ = *b->asn_cptr++;
	b->asn_cptr += len - 4;
	b->asn_len -= len;
	return (ASN_ERR_OK);
}

enum asn_err
asn_get_ipaddress(struct asn_buf *b, u_char *addr)
{
	u_char type;
	asn_len_t len;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != (ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS)) {
		asn_error(b, "bad type for ip-address %u", type);
		return (ASN_ERR_TAG);
	}
	return (asn_get_ipaddress_raw(b, len, addr));
}

enum asn_err
asn_put_ipaddress(struct asn_buf *b, const u_char *addr)
{
	enum asn_err err;

	if ((err = asn_put_header(b, ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS,
	    4)) != ASN_ERR_OK)
		return (err);
	if (b->asn_len < 4)
		return (ASN_ERR_EOBUF);

	memcpy(b->asn_ptr, addr, 4);
	b->asn_ptr += 4;
	b->asn_len -= 4;
	return (ASN_ERR_OK);
}


/*
 * UNSIGNED32
 *
 * 0x42|0x41 <len> ...
 */
enum asn_err
asn_get_uint32_raw(struct asn_buf *b, asn_len_t len, uint32_t *vp)
{
	uint64_t v;
	enum asn_err err;

	if ((err = asn_get_real_unsigned(b, len, &v)) == ASN_ERR_OK) {
		if (len > 5) {
			asn_error(b, "uint32 too long %u", len);
			err = ASN_ERR_BADLEN;
		} else if (v > UINT32_MAX) {
			asn_error(b, "uint32 too large %llu", v);
			err = ASN_ERR_RANGE;
		}
		*vp = (uint32_t)v;
	}
	return (err);
}

enum asn_err
asn_put_uint32(struct asn_buf *b, u_char type, uint32_t val)
{
	uint64_t v = val;

	return (asn_put_real_unsigned(b, ASN_CLASS_APPLICATION|type, v));
}

/*
 * COUNTER64
 * 0x46 <len> ...
 */
enum asn_err
asn_get_counter64_raw(struct asn_buf *b, asn_len_t len, uint64_t *vp)
{
	return (asn_get_real_unsigned(b, len, vp));
}

enum asn_err
asn_put_counter64(struct asn_buf *b, uint64_t val)
{
	return (asn_put_real_unsigned(b,
	    ASN_CLASS_APPLICATION | ASN_APP_COUNTER64, val));
}

/*
 * TimeTicks
 * 0x43 <len> ...
 */
enum asn_err
asn_get_timeticks(struct asn_buf *b, uint32_t *vp)
{
	asn_len_t len;
	u_char type;
	enum asn_err err;

	if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
		return (err);
	if (type != (ASN_CLASS_APPLICATION|ASN_APP_TIMETICKS)) {
		asn_error(b, "bad type for timeticks %u", type);
		return (ASN_ERR_TAG);
	}
	return (asn_get_uint32_raw(b, len, vp));
}

enum asn_err
asn_put_timeticks(struct asn_buf *b, uint32_t val)
{
	uint64_t v = val;

	return (asn_put_real_unsigned(b,
	    ASN_CLASS_APPLICATION | ASN_APP_TIMETICKS, v));
}

/*
 * Construct a new OID by taking a range of sub ids of the original oid.
 */
void
asn_slice_oid(struct asn_oid *dest, const struct asn_oid *src,
    u_int from, u_int to)
{
	if (from >= to) {
		dest->len = 0;
		return;
	}
	dest->len = to - from;
	memcpy(dest->subs, &src->subs[from], dest->len * sizeof(dest->subs[0]));
}

/*
 * Append from to to
 */
void
asn_append_oid(struct asn_oid *to, const struct asn_oid *from)
{
	memcpy(&to->subs[to->len], &from->subs[0],
	    from->len * sizeof(from->subs[0]));
	to->len += from->len;
}

/*
 * Skip a value
 */
enum asn_err
asn_skip(struct asn_buf *b, asn_len_t len)
{
	if (b->asn_len < len)
		return (ASN_ERR_EOBUF);
	b->asn_cptr += len;
	b->asn_len -= len;
	return (ASN_ERR_OK);
}

/*
 * Add a padding
 */
enum asn_err
asn_pad(struct asn_buf *b, asn_len_t len)
{
	if (b->asn_len < len)
		return (ASN_ERR_EOBUF);
	b->asn_ptr += len;
	b->asn_len -= len;

	return (ASN_ERR_OK);
}

/*
 * Compare two OIDs.
 *
 * o1 < o2 : -1
 * o1 > o2 : +1
 * o1 = o2 :  0
 */
int
asn_compare_oid(const struct asn_oid *o1, const struct asn_oid *o2)
{
	u_long i;

	for (i = 0; i < o1->len && i < o2->len; i++) {
		if (o1->subs[i] < o2->subs[i])
			return (-1);
		if (o1->subs[i] > o2->subs[i])
			return (+1);
	}
	if (o1->len < o2->len)
		return (-1);
	if (o1->len > o2->len)
		return (+1);
	return (0);
}

/*
 * Check whether an OID is a sub-string of another OID.
 */
int
asn_is_suboid(const struct asn_oid *o1, const struct asn_oid *o2)
{
	u_long i;

	for (i = 0; i < o1->len; i++)
		if (i >= o2->len || o1->subs[i] != o2->subs[i])
			return (0);
	return (1);
}

/*
 * Put a string representation of an oid into a user buffer. This buffer
 * is assumed to be at least ASN_OIDSTRLEN characters long.
 *
 * sprintf is assumed not to fail here.
 */
char *
asn_oid2str_r(const struct asn_oid *oid, char *buf)
{
	u_int len, i;
	char *ptr;

	if ((len = oid->len) > ASN_MAXOIDLEN)
		len = ASN_MAXOIDLEN;
	buf[0] = '\0';
	for (i = 0, ptr = buf; i < len; i++) {
		if (i > 0)
			*ptr++ = '.';
		ptr += sprintf(ptr, "%u", oid->subs[i]);
	}
	return (buf);
}

/*
 * Make a string from an OID in a private buffer.
 */
char *
asn_oid2str(const struct asn_oid *oid)
{
	static char str[ASN_OIDSTRLEN];

	return (asn_oid2str_r(oid, str));
}


static void
asn_error_func(const struct asn_buf *b, const char *err, ...)
{
	va_list ap;
	u_long i;

	fprintf(stderr, "ASN.1: ");
	va_start(ap, err);
	vfprintf(stderr, err, ap);
	va_end(ap);

	if (b != NULL) {
		fprintf(stderr, " at");
		for (i = 0; b->asn_len > i; i++)
			fprintf(stderr, " %02x", b->asn_cptr[i]);
	}
	fprintf(stderr, "\n");
}