File: ber.C

package info (click to toggle)
npadmin 0.8-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 524 kB
  • ctags: 792
  • sloc: cpp: 3,514; ansic: 1,176; sh: 327; makefile: 52
file content (306 lines) | stat: -rw-r--r-- 9,199 bytes parent folder | download
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <netinet/in.h>

#include "ber.h"
#include "compat.h"

BerEncodedData::BerEncodedData(unsigned char *dat, unsigned long len): 
  data(dat),length(len),next(NULL){
  assert(data);
}

unsigned char *start_data(Tags type,unsigned int len, unsigned char &headlen){
  unsigned char *retstr;
  if(len<128){
    assert(retstr=new unsigned char[len+2]);
    retstr[1]=len;
    headlen=2;
  } else {
    unsigned char buf[sizeof(unsigned long)];
    unsigned long nlen=htonl(len);
    memcpy(buf,&nlen,sizeof(unsigned long));

    //counts the number of leading 0's
    for(headlen=0;buf[headlen]==0 && headlen<3;headlen++);
    //magic function to to turn this into header length
    headlen=sizeof(unsigned long)-headlen+2;

    assert(retstr=new unsigned char[len+headlen]);
    retstr[1]=0x80-2+headlen; 

    char k=2;
    for(char j=sizeof(unsigned long)+2-headlen;j<=3;j++)
      retstr[k++]=buf[j];
  }
  retstr[0]=type;
  return retstr;
}

unsigned long unpack_len(unsigned char *start,unsigned char &headerlen){
  if(start[1]<128){
    headerlen=2;    
    return start[1];
  }
  long sizeofsize=start[1]&0x7f;
  assert(sizeofsize<=sizeof(long));
  long size=0;
  memcpy(((unsigned char*)(&size))+sizeof(long)-sizeofsize,start+2,sizeofsize);
  headerlen=2+sizeofsize;
  return htonl(size);
}

/* ber class is the basic encoding rules for ASN.1 class. All the classes that
 *     have encoding are derived from this class.
 * len - is the length of the encoded sequence.
 * mbufcache - is the the head of the mbuf chain that this class will hand out 
 *     when getmbuf is called. It is set to NULL initially and getmbuf fills 
 *     it in and subsequent calls just return it.
 * getmbuf - most classes just use the default getmbuf but sequences need to 
 *     do some special processing and so it must be used be virtual.
 * encode - does whatever is necessary to come up with a string to pass out
 *     the calling function. The string is probably not NULL terminated and 
 *     probably has NULLs in it.
 * Len - is used to find out the length of the encoded sequence. The value is
 *     undefined before an encode is called.
 */

BerEncodedData *BerBase::encode(){
  assert(0);
  return NULL;
}

BerEncodedData *BerBase::getdata(){
  assert(edatacache || (edatacache=encode()));
  return edatacache;
}

unsigned long BerBase::fulllen(){
  BerEncodedData *bed=getdata();
  unsigned long retval=bed->Length();
  while(bed->next){
    bed=bed->next;
    retval+=bed->Length();
  }
  return retval;
}

BerBase::BerBase(unsigned char* str):next(NULL){
  unsigned char *retstr;
  unsigned char headerlen;
  unsigned long size=unpack_len(str,headerlen);
  unsigned long fullen=size+headerlen;
  assert(retstr=new unsigned char[fullen]);
  memcpy(retstr,str,fullen);
  assert(edatacache=new BerEncodedData(retstr,fullen));
}

/* 
 *
 * berNull - is the string 0x05 0x00 there is really not much too it.
 *
 */

int BerNull::print(char *buf, unsigned int len){
  if(len<4) return -1;
  strncpy(buf,"NULL",4);
  return 4;
}

BerNull::BerNull(unsigned char *str): BerBase(str){
  assert(str[0]==NULL_TAG);
  assert(str[1]==0);
}

BerEncodedData *BerNull::encode(){
  unsigned char headlen;
  return new BerEncodedData(start_data(NULL_TAG,0,headlen),headlen);
}

/* berInt is the encoding of an int in ASN.1
 * The encoding is a 0x02 followed by the size of the data then the bytes of
 * of the data. The data is stored in the big endian 2's complement using the
 * fewest bytes possible. However a 0 still uses one byte. i.e. 0x20 0x01 0x00
 */

int BerInt::print(char *buf, unsigned int len){
  return snprintf(buf,len,"%d",val);
}

BerInt::BerInt(unsigned char *str): BerBase(str),val(0){
  /* this might cause some problems with the counters that exceede 31 bits in 
     length but for now it seems ok */
  assert(str[0]==INT_TAG||str[0]==COUNTER_TAG);
  assert(str[1]<=sizeof(long));
  memcpy(((unsigned char*)&val)+sizeof(long)-str[1],str+2,str[1]);
  //extend out the sign if necessary
  if(str[0]==INT_TAG && str[1]!=sizeof(long) && str[2]&0x80)
    memset(&val,0xff,sizeof(long)-str[1]);
  // being lazy again some high bit counters may cause a problem.
  assert(!(str[0]==COUNTER_TAG && str[1]==sizeof(long) && str[2]&0x80));
  val=ntohl(val);
}

BerEncodedData *BerInt::encode(){
  unsigned char buf[sizeof(long)];
  unsigned char *retval;
  unsigned char i,headerlen;
  long value=htonl(val);
  memcpy(buf,&value,sizeof(long));
  //  printf("0=%d 1=%d 2=%d 3=%d ",(int)buf[0],(int)buf[1],(int)buf[2],
  // (int)buf[3]);

  //count the number of bytes that are actually used
  for(i=sizeof(long);!buf[sizeof(long)-i] && i>1;i--);

  //  printf("val=%ld len=%u buf[0]=%u buf[1]=%u buf[2]=%u buf[3]=%u\n",val,
  //	 (unsigned) i,(unsigned) buf[0],(unsigned) buf[1],(unsigned) buf[2],
  //	 (unsigned) buf[3]);

  retval=start_data(INT_TAG,i,headerlen);
  memcpy(retval+headerlen,buf+sizeof(long)-i,i);
  //  printf("i=%d ",(int) i);
  
  //  printf("va// lue=%d ",val);
//   for(int j=0;j<i;j++){
//     printf("i=%d val=%d ",j,(int) *(retval+headerlen+j));
//   }
//   printf("\n");

  return new BerEncodedData(retval,headerlen+i);
}

/*
 * Number of timeticks since some epoch. In 1/100 seconds
 */

BerTimeTick::BerTimeTick(unsigned char *str): BerBase(str),val(0){
  assert(str[0]==TIME_TICK_TAG);
  assert(str[1]<=sizeof(unsigned long));
  memcpy(((unsigned char*)&val)+sizeof(unsigned long)-str[1],str+2,str[1]);
  val=ntohl(val);
}

BerEncodedData *BerTimeTick::encode(){
  unsigned char buf[sizeof(unsigned long)];
  unsigned char *retval;
  unsigned char i,headerlen;
  long value=htonl(val);
  memcpy(buf,&value,sizeof(unsigned long));

  //count the number of bytes that are actually used
  for(i=sizeof(long);!buf[sizeof(unsigned long)-i] && i>1;i--);

  retval=start_data(TIME_TICK_TAG,i,headerlen);
  memcpy(retval+headerlen,buf,i);
  return new BerEncodedData(retval,headerlen+i);
}

int BerTimeTick::print(char *buf, unsigned int len){
  // hopefully this works by just assming the bits are right rather than 
  // coercing the value into a different format.
  unsigned t1=val%8640000;
  unsigned t2=t1%360000;
  unsigned t3=t2%6000;
  return snprintf(buf,len,"Time: %ud %uh %um %u.%usec (%u)",val/8640000,
		  t1/360000,t2/6000,t3/100,t3%100,val);
}


/* 
 * berString is used to encode a string. It is simply the length and the data.
 * the only complication is the encoding of the length.
 */

int BerString::copy(char *buf, unsigned int len){
  assert(len>stringlen);
  memcpy(buf,str,stringlen);
  return stringlen;
}

int BerString::print(char *buf, unsigned int len){
  int totlenused=0;
  /* strings are used for binary data as well as text. We need to handle 8 bit
     data as well as normal printable strings */
  for(char *cur=str;cur<str+stringlen;cur++){
    int lenused=snprintf(buf,len,isprint(*cur)?"%c":"\\0x%02x",
			 *(unsigned char*)cur);
    assert(lenused!=-1);
    totlenused+=lenused;
    len-=lenused;
    buf+=lenused;
  }
  return totlenused;
}

BerString::BerString(CONST char *strng,unsigned int length): 
  stringlen(length){ 
  assert(strng);
  assert(str=new char[length]);
  memcpy(str,strng,length);
}

BerString::BerString(unsigned char *strn): BerBase(strn){ // Wire protocol only
  assert(strn[0]==STRING_TAG);
  unsigned char headerlen;
  unpack_len(strn,headerlen);
  // I can use edatacache->len here because BerBase(strn) fills in edatacache.
  assert(str=new char[edatacache->Length()-headerlen]);
  stringlen=edatacache->Length()-headerlen;
  memcpy(str,strn+headerlen,stringlen);
}

BerEncodedData *BerString::encode(){
  unsigned char headerlen;
  unsigned char *retval=start_data(STRING_TAG,stringlen,headerlen);
  memcpy(retval+headerlen,str,stringlen);
  return new BerEncodedData(retval,headerlen+stringlen);
}

/* 
 * BerIPAddr is used to encode an ipaddress. 
 */

int BerIPAddr::print(char *buf, unsigned int length){
  assert(len==4); // don't know how to deal with v6 yet
  // might be a problem with endian but I borrowed this code out of nslookup
  return snprintf(buf,length,"%u.%u.%u.%u;",
		  ((unsigned)ipaddr[0] & 0xff),
		  ((unsigned)ipaddr[1] & 0xff),
		  ((unsigned)ipaddr[2] & 0xff),
		  ((unsigned)ipaddr[3] & 0xff));
}

BerIPAddr::BerIPAddr(const char *addr,int length): 
  len(length){ 
  assert(addr);
  assert(len==4); //don't know how to deal with anything but v4
  assert(ipaddr=new unsigned char[4]);
  memcpy(ipaddr,addr,4);
}

// Wire protocol only
BerIPAddr::BerIPAddr(unsigned char *strn): BerBase(strn),len(4){ 
  assert(strn[0]==IPADDR_TAG);
  unsigned char headerlen;
  unpack_len(strn,headerlen);
  // I can use edatacache->len here because BerBase(strn) fills in edatacache.
  assert(edatacache->Length()==6);
  assert(ipaddr=new unsigned char[4]);
  memcpy(ipaddr,strn+headerlen,4);
}

BerEncodedData *BerIPAddr::encode(){
  unsigned char headerlen;
  unsigned char *retval=start_data(IPADDR_TAG,4,headerlen);
  memcpy(retval+headerlen,ipaddr,4);
  return new BerEncodedData(retval,headerlen+len);
}