summaryrefslogtreecommitdiffstats
path: root/scan_asn1generic.c
blob: 09c84a4dac254a78a41426200d960b97baa3d8e7 (plain) (blame)
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
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include "asn1.h"
#include <string.h>

size_t scan_asn1generic(const char* src,const char* max,const char* fmt,...) {
  size_t curlen,seqlen;
  const char* maxstack[100];
  size_t curmax=0;
  va_list args;
  int optional=0;
  unsigned long* application=NULL;
  unsigned long tag;
  enum asn1_tagclass tc;
  enum asn1_tagtype tt;
  unsigned int wantedtag;
  unsigned long* desttag=NULL;
  const char* orig=src;
  va_start(args,fmt);
  maxstack[0]=max;
  while (*fmt) {
    switch (*fmt) {
    case '?':		// ? = rest is optional (until end of sequence)
      optional=1;
      break;
    case 'B':		// B = BOOLEAN
    case 'i':		// i = INTEGER
      {
	long* dest=va_arg(args,long*);
	int* bdest=(int*)dest;
	long l;
	if (*fmt=='B') *bdest=0; else *dest=0;
	curlen=scan_asn1int(src,maxstack[curmax],&tc,&tt,&tag,&l);
	if (application) {
	  if (tc!=APPLICATION) goto error;
	  *application=tag;
	} else {
	  if (tc!=UNIVERSAL || tt!=PRIMITIVE || tag!=(*fmt=='B'?BOOLEAN:INTEGER))
	    goto error;
	}
	if (!curlen) { if (optional) break; else goto error; }
	if (*fmt=='B')
	  *bdest=l;
	else
	  *dest=l;
	src+=curlen;
	application=NULL;
	break;
      }
    case 'I':		// I = INTEGER, but for bignum integers; writes to an array of size_t, first one contains number of digits after it
      {
	size_t* dest=va_arg(args,size_t*);
	size_t len,tmp,tlen,j,t;
	if (!(len=scan_asn1tag(src,maxstack[curmax],&tc,&tt,&tag))) goto error;
	if (!(tmp=scan_asn1length(src+len,maxstack[curmax],&tlen))) goto error;
	len+=tmp;
	j=0; t=1;
	src+=len;
	/* asn.1 sends n bytes, most significant first.
	 * we want m digits, most significant first.
	 * if n is not a multiple of sizeof(digit) then we need to
	 * insert a few 0 bytes in the first word
	 */
	while (tlen) {
	  j=(j<<8)+(unsigned char)(*src);
	  ++src;
	  --tlen;
	  if ((tlen%sizeof(j))==0 && (j || t>1)) {
	    dest[t]=j;
	    j=0;
	    ++t;
	  }
	}
	if (j) dest[t++]=j;
	dest[0]=t-1;
	break;
      }
    case 'b':
      wantedtag=BIT_STRING; goto stringmain;
    case 'u':
      wantedtag=UTCTIME; goto stringmain;
    case 'p':
      wantedtag=PrintableString; goto stringmain;
    case 'a':
      wantedtag=IA5String; goto stringmain;
    case 's':
      wantedtag=OCTET_STRING; goto stringmain;
stringmain:
      {
	struct string* dest;
	struct string temp;
	time_t* desttime=NULL;
	size_t i;
	if (wantedtag==UTCTIME) {
	  dest=&temp;
	  desttime=va_arg(args,time_t*);
	} else
	  dest=va_arg(args,struct string*);
	dest->l=0;
	dest->s=0;
	curlen=scan_asn1string(src,maxstack[curmax],&tc,&tt,&tag,&dest->s,&dest->l);
	if (!curlen) { if (optional) break; else goto error; }
	if (application) {
	  if (tc!=APPLICATION) goto error;
	  *application=tag;
	} else {
	  if (tc!=UNIVERSAL || tt!=PRIMITIVE || tag!=wantedtag)
	    goto error;
	}
	if (wantedtag==BIT_STRING) {	// additional checks for bit strings
	  if (dest->l==0 ||	// length can't be 0 because the format starts with 1 octet that contains the number of unused bits in the last octet
	      ((unsigned char)(dest->s[0])>7) ||	// it's the number of unused bits in an octet, must be [0..7]
	      (dest->l==1 && dest->s[0])) goto error;	// if there is no last octet, there can't be any unused bits in there
	  dest->l=(dest->l-1)*8-dest->s[0];
	  dest->s+=1;
	} else if (wantedtag==PrintableString) {
	  for (i=0; i<dest->l; ++i)	// RFC 2252 section 4.1 production p
	    if (!isalnum(dest->s[i])
		&& dest->s[i]!='"'
		&& dest->s[i]!='('
		&& dest->s[i]!=')'
		&& dest->s[i]!='+'
		&& dest->s[i]!=','
		&& dest->s[i]!='-'
		&& dest->s[i]!='.'
		&& dest->s[i]!='/'
		&& dest->s[i]!=':'
		&& dest->s[i]!='?'
		&& dest->s[i]!=' ') goto error;
	} else if (wantedtag==IA5String) {
	  for (i=0; i<dest->l; ++i)	// IA5String is an ASCII string, which means 0 <= s[i] <= 127
	    if ((unsigned char)(dest->s[i]) > 127) goto error;
	} else if (wantedtag==UTCTIME) {
	  size_t j;
	  struct tm t;
	  memset(&t,0,sizeof(t));
	  /*
		YYMMDDhhmmZ
		YYMMDDhhmm+hh'mm'
		YYMMDDhhmm-hh'mm'
		YYMMDDhhmmssZ
		YYMMDDhhmmss+hh'mm'
		YYMMDDhhmmss-hh'mm'
	   */
	  if (dest->l<11 || dest->l>17) goto error;
	  j=(dest->s[0]-'0')*10+dest->s[1]-'0';
	  t.tm_year=j+(j<70)*100;

	  for (i=0; i<10; ++i)
	    if (!isdigit(dest->s[i])) goto error;
	  j=(dest->s[2]-'0')*10+dest->s[3]-'0';		// is the month plausible?
	  if (j<1 || j>12) goto error;
	  t.tm_mon=j-1;
	  j=(dest->s[4]-'0')*10+dest->s[5]-'0';		// is the day plausible?
	  if (j<1 || j>31) goto error;
	  t.tm_mday=j;
	  j=(dest->s[6]-'0')*10+dest->s[7]-'0';		// is the hour plausible?
	  if (j>23) goto error;
	  t.tm_hour=j;
	  j=(dest->s[8]-'0')*10+dest->s[9]-'0';		// is the minutes plausible?
	  if (j>59) goto error;
	  t.tm_min=j;
	  i=10;
	  if (isdigit(dest->s[10])) {
	    i+=2;
	    j=(dest->s[10]-'0')*10+dest->s[11]-'0';		// is the seconds plausible?
	    if (j>59) goto error;
	    t.tm_sec=j;
	  }
	  *desttime=mktime(&t);
	  if (dest->s[i]=='+' || dest->s[i]=='-') {
	    size_t j;
	    if (dest->l!=15) goto error;
	    for (j=i; j<i+4; ++j)
	      if (!isdigit(dest->s[j])) goto error;
	    j=(dest->s[i]-'0')*10+dest->s[i+1]-'0';		// is the offset minutes plausible?
	    if (j>59) goto error;
	    if (dest->s[i]=='+')
	      *desttime+=j*60;
	    else
	      *desttime-=j*60;
	    j=(dest->s[i+2]-'0')*10+dest->s[i+3]-'0';		// is the offset seconds plausible?
	    if (j>59) goto error;
	    if (dest->s[i]=='+')
	      *desttime+=j;
	    else
	      *desttime-=j;
	  } else if (dest->s[i]!='Z') goto error;
	}
	src+=curlen;
	application=NULL;
	break;
      }
    case 'o':		// o == OID
      {
	struct string* dest=va_arg(args,struct string*);
	curlen=scan_asn1tag(src,maxstack[curmax],&tc,&tt,&tag);
	if (!curlen) { if (optional) break; else goto error; }
	if (application) {
	  if (tc!=APPLICATION) goto error;
	  *application=tag;
	} else {
	  if (tc!=UNIVERSAL || tt!=PRIMITIVE || tag!=OBJECT_IDENTIFIER)
	    goto error;
	}
	src+=curlen;
	curlen=scan_asn1length(src,maxstack[curmax],&seqlen);
	if (!curlen) goto error;
	src+=curlen;
	dest->s=src;
	dest->l=seqlen;
	src+=seqlen;
	application=NULL;
	break;
      }
    case '*':		// next tag class is APPLICATION instead of UNIVERSAL; write tag to unsigned long*
      {
	application=va_arg(args,unsigned long*);
	break;
      }
    case 'c':		// c = context specific; CONTEXT_SPECIFIC CONSTRUCTED 0, close with '}'
      desttag=va_arg(args,unsigned long*);
      // fall through
    case '[':		// [ = SET
    case '{':		// { = SEQUENCE
      {
	curlen=scan_asn1tag(src,maxstack[curmax],&tc,&tt,&tag);
	if (!curlen) { if (optional) break; else goto error; }
	if (application) {
	  if (tc!=APPLICATION || tt!=CONSTRUCTED) goto error;
	  *application=tag;
	} else {
	  if (*fmt=='c') {
	    if (tc!=CONTEXT_SPECIFIC || tt!=CONSTRUCTED)
	      goto error;
	    *desttag=tag;
	  } else {
	    if (tc!=UNIVERSAL || tt!=CONSTRUCTED || tag!=(*fmt=='{'?SEQUENCE_OF:SET_OF))
	      goto error;
	  }
	}
	src+=curlen;
	curlen=scan_asn1length(src,maxstack[curmax],&seqlen);
	if (!curlen || curmax>99) goto error;
	maxstack[++curmax]=src+curlen+seqlen;
	src+=curlen;
	application=NULL;
	break;
      }
    case '!':		// save current src and max-src into struct string*
      // useful for optional parts or CHOICEs
      {
	struct string* dest=va_arg(args,struct string*);
	dest->s=src;
	dest->l=maxstack[curmax]-src;
	break;
      }
    case ']':		// ] = end of SET
    case '}':		// } = end of SEQUENCE
      {
	optional=0;
	if (curmax==0) goto error;
	src=maxstack[curmax];
	--curmax;
	break;
      }
    default:
      goto error;
    }
    ++fmt;
  }
  va_end(args);
  return src-orig;
error:
  va_end(args);
  return 0;
}