blob: 2664b9061eaedec6bb75a09bac6027c277db0650 [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 */
5#include <constr_TYPE.h>
6#include <ber_tlv_tag.h>
7#include <errno.h>
8
9ssize_t
10ber_fetch_tag(void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
11 ber_tlv_tag_t val;
12 ber_tlv_tag_t tclass;
Lev Walkind9bd7752004-06-05 08:17:50 +000013 size_t skipped;
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15 if(size == 0)
16 return 0;
17
18 val = *(uint8_t *)ptr;
19 tclass = (val >> 6);
20 if((val &= 31) != 31) {
21 /*
22 * Simple form: everything encoded in a single octet.
23 * Tag Class is encoded using two least significant bits.
24 */
25 *tag_r = (val << 2) | tclass;
26 return 1;
27 }
28
29 /*
30 * Each octet contains 7 bits of useful information.
31 * The MSB is 0 if it is the last octet of the tag.
32 */
Lev Walkin4d9528c2004-08-11 08:10:13 +000033 for(val = 0, ((char *)ptr)++, skipped = 2;
34 skipped < size; ((char *)ptr)++, skipped++) {
Lev Walkinf15320b2004-06-03 03:38:44 +000035 unsigned oct = *(uint8_t *)ptr;
36 if(oct & 0x80) {
37 val = (val << 7) | (oct & 0x7F);
38 /*
39 * Make sure there are at least 9 bits spare
40 * at the MS side of a value.
41 */
42 if(val >> ((8 * sizeof(val)) - 9)) {
43 /*
44 * We would not be able to accomodate
45 * any more tag bits.
46 */
47 return -1;
48 }
49 } else {
50 *tag_r = (val << 9) | (oct << 2) | tclass;
51 return skipped;
52 }
53 }
54
55 return 0; /* Want more */
56}
57
58
59ssize_t
60ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) {
61 char buf[sizeof("[APPLICATION ]") + 32];
62 ssize_t ret;
63
64 ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf));
Lev Walkind9bd7752004-06-05 08:17:50 +000065 if(ret >= (ssize_t)sizeof(buf) || ret < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +000066 errno = EPERM;
67 return -1;
68 }
69
70 return fwrite(buf, 1, ret, f);
71}
72
73ssize_t
74ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) {
75 char *type = 0;
76 int ret;
77
78 switch(tag & 0x3) {
79 case ASN_TAG_CLASS_UNIVERSAL: type = "UNIVERSAL "; break;
80 case ASN_TAG_CLASS_APPLICATION: type = "APPLICATION "; break;
81 case ASN_TAG_CLASS_CONTEXT: type = ""; break;
82 case ASN_TAG_CLASS_PRIVATE: type = "PRIVATE "; break;
83 }
84
85 ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2);
86 if(ret <= 0 && size) buf[0] = '\0'; /* against broken libc's */
87
88 return ret;
89}
90
91char *
92ber_tlv_tag_string(ber_tlv_tag_t tag) {
93 static char buf[sizeof("[APPLICATION ]") + 32];
94
95 (void)ber_tlv_tag_snprint(tag, buf, sizeof(buf));
96
97 return buf;
98}
99
100
101ssize_t
102der_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
103 int tclass = BER_TAG_CLASS(tag);
104 ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
Lev Walkinc2346572004-08-11 09:07:36 +0000105 uint8_t *buf = (uint8_t *)bufp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 uint8_t *end;
Lev Walkind9bd7752004-06-05 08:17:50 +0000107 size_t computed_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 int i;
109
110 if(tval <= 30) {
111 /* Encoded in 1 octet */
112 if(size) buf[0] = (tclass << 6) | tval;
113 return 1;
114 } else if(size) {
115 *buf++ = (tclass << 6) | 0x1F;
116 size--;
117 }
118
119 /*
120 * Compute the size of the subsequent bytes.
121 * The routine is written so every floating-point
122 * operation is done at compile time.
123 * Note, there is a subtle problem lurking here,
124 * could you guess where it is? :)
125 * Hint: what happens when ((8*sizeof(tag))%7) == 0?
126 */
127 computed_size = 1 + 8 * sizeof(tag) / 7;
128 for(i = (8*sizeof(tag)) - ((8*sizeof(tag))%7); i >= 7; i -= 7) {
129 if((tval >> i) & 0x7F) break;
130 computed_size--;
131 }
132
133 /*
134 * Fill in the buffer, space permitting.
135 */
136 if(size > computed_size)
137 end = buf + computed_size;
138 else
139 end = buf + size;
140 for((void)i; buf < end; i -= 7, buf++) {
141 *buf = 0x80 | ((tval>>i) & 0x7F);
142 }
143
144 return computed_size + 1;
145}
146