/
ber_decode.c
168 lines (138 loc) · 3.17 KB
/
ber_decode.c
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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <gber.h>
static void hex_dumpf_r(FILE *f, const uint8_t *tmp, size_t len,
size_t llen, unsigned int depth)
{
size_t i, j;
size_t line;
for(j = 0; j < len; j += line, tmp += line) {
if ( j + llen > len ) {
line = len - j;
}else{
line = llen;
}
fprintf(f, "%*c%05x : ", depth, ' ', j);
for(i = 0; i < line; i++) {
if ( isprint(tmp[i]) ) {
fprintf(f, "%c", tmp[i]);
}else{
fprintf(f, ".");
}
}
for(; i < llen; i++)
fprintf(f, " ");
for(i=0; i < line; i++)
fprintf(f, " %02x", tmp[i]);
fprintf(f, "\n");
}
fprintf(f, "\n");
}
static int do_ber_dump(FILE *f, const uint8_t *ptr, size_t len,
unsigned int depth)
{
const uint8_t *end = ptr + len;
while(ptr < end) {
struct gber_tag tag;
ptr = ber_decode_tag(&tag, ptr, end - ptr);
if ( NULL == ptr )
return 0;
fprintf(f, "%*c.tag = %x\n", depth, ' ', tag.ber_tag);
fprintf(f, "%*c.class = %s\n", depth, ' ',
ber_id_octet_clsname(tag.ber_id));
fprintf(f, "%*c.constructed = %s\n", depth, ' ',
ber_id_octet_constructed(tag.ber_id) ? "yes" : "no");
fprintf(f, "%*c.len = %u (0x%.2x)\n",
depth, ' ', tag.ber_len, tag.ber_len);
if ( ber_id_octet_constructed(tag.ber_id) ) {
if ( !do_ber_dump(f, ptr, tag.ber_len, depth + 1) )
return 0;
}else{
hex_dumpf_r(f, ptr, tag.ber_len, 16, depth + 1);
}
ptr += tag.ber_len;
}
return 1;
}
int ber_dump(const uint8_t *ptr, size_t len)
{
return do_ber_dump(stdout, ptr, len, 1);
}
int ber_dumpf(FILE *f, const uint8_t *ptr, size_t len)
{
return do_ber_dump(f, ptr, len, 1);
}
const char * const ber_id_octet_clsname(uint8_t id)
{
static const char * const clsname[]={
"universal",
"application",
"context-specific",
"private",
};
return clsname[(id & 0xc0) >> 6];
}
unsigned int ber_id_octet_class(uint8_t id)
{
return (id & 0xc0) >> 6;
}
unsigned int ber_id_octet_constructed(uint8_t id)
{
return (id & 0x20) >> 5;
}
static uint8_t ber_len_form_short(uint8_t lb)
{
return !(lb & 0x80);
}
static uint8_t ber_len_short(uint8_t lb)
{
return lb & ~0x80;
}
static const uint8_t *do_decode_tag(struct gber_tag *tag,
const uint8_t *ptr, size_t len)
{
const uint8_t *end = ptr + len;
if ( len < 2 )
return NULL;
tag->ber_id = *(ptr++);
tag->ber_tag = tag->ber_id;
if ( (tag->ber_id & 0x1f) == 0x1f ) {
if ( (*ptr & 0x80) )
return NULL;
tag->ber_tag <<= 8;
tag->ber_tag |= *(ptr++);
if ( ptr >= end )
return NULL;
}
if ( ber_len_form_short(*ptr) ) {
tag->ber_len = ber_len_short(*ptr);
ptr++;
}else{
unsigned int i;
uint8_t ll;
ll = ber_len_short(*(ptr++));
if ( ptr + ll > end || ll > 4 )
return NULL;
for(tag->ber_len = 0, i = 0; i < ll; i++, ptr++) {
tag->ber_len <<= 8;
tag->ber_len |= *ptr;
}
}
return ptr;
}
const uint8_t *ber_tag_info(struct gber_tag *tag,
const uint8_t *ptr, size_t len)
{
return do_decode_tag(tag, ptr, len);
}
const uint8_t *ber_decode_tag(struct gber_tag *tag,
const uint8_t *ptr, size_t len)
{
const uint8_t *end = ptr + len;
ptr = do_decode_tag(tag, ptr, len);
if ( NULL == ptr || ptr + tag->ber_len > end )
return NULL;
return ptr;
}