version incremented
[samba] / source / libsmb / asn1.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple SPNEGO routines
4    Copyright (C) Andrew Tridgell 2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /* free an asn1 structure */
24 void asn1_free(ASN1_DATA *data)
25 {
26         SAFE_FREE(data->data);
27 }
28
29 /* write to the ASN1 buffer, advancing the buffer pointer */
30 BOOL asn1_write(ASN1_DATA *data, const void *p, int len)
31 {
32         if (data->has_error) return False;
33         if (data->length < data->ofs+len) {
34                 uint8 *newp;
35                 newp = SMB_REALLOC(data->data, data->ofs+len);
36                 if (!newp) {
37                         SAFE_FREE(data->data);
38                         data->has_error = True;
39                         return False;
40                 }
41                 data->data = newp;
42                 data->length = data->ofs+len;
43         }
44         memcpy(data->data + data->ofs, p, len);
45         data->ofs += len;
46         return True;
47 }
48
49 /* useful fn for writing a uint8 */
50 BOOL asn1_write_uint8(ASN1_DATA *data, uint8 v)
51 {
52         return asn1_write(data, &v, 1);
53 }
54
55 /* push a tag onto the asn1 data buffer. Used for nested structures */
56 BOOL asn1_push_tag(ASN1_DATA *data, uint8 tag)
57 {
58         struct nesting *nesting;
59
60         asn1_write_uint8(data, tag);
61         nesting = SMB_MALLOC_P(struct nesting);
62         if (!nesting) {
63                 data->has_error = True;
64                 return False;
65         }
66
67         nesting->start = data->ofs;
68         nesting->next = data->nesting;
69         data->nesting = nesting;
70         return asn1_write_uint8(data, 0xff);
71 }
72
73 /* pop a tag */
74 BOOL asn1_pop_tag(ASN1_DATA *data)
75 {
76         struct nesting *nesting;
77         size_t len;
78
79         nesting = data->nesting;
80
81         if (!nesting) {
82                 data->has_error = True;
83                 return False;
84         }
85         len = data->ofs - (nesting->start+1);
86         /* yes, this is ugly. We don't know in advance how many bytes the length
87            of a tag will take, so we assumed 1 byte. If we were wrong then we 
88            need to correct our mistake */
89         if (len > 0xFFFF) {
90                 data->data[nesting->start] = 0x83;
91                 if (!asn1_write_uint8(data, 0)) return False;
92                 if (!asn1_write_uint8(data, 0)) return False;
93                 if (!asn1_write_uint8(data, 0)) return False;
94                 memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
95                 data->data[nesting->start+1] = (len>>16) & 0xFF;
96                 data->data[nesting->start+2] = (len>>8) & 0xFF;
97                 data->data[nesting->start+3] = len&0xff;
98         } else if (len > 255) {
99                 data->data[nesting->start] = 0x82;
100                 if (!asn1_write_uint8(data, 0)) return False;
101                 if (!asn1_write_uint8(data, 0)) return False;
102                 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
103                 data->data[nesting->start+1] = len>>8;
104                 data->data[nesting->start+2] = len&0xff;
105         } else if (len > 127) {
106                 data->data[nesting->start] = 0x81;
107                 if (!asn1_write_uint8(data, 0)) return False;
108                 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
109                 data->data[nesting->start+1] = len;
110         } else {
111                 data->data[nesting->start] = len;
112         }
113
114         data->nesting = nesting->next;
115         free(nesting);
116         return True;
117 }
118
119
120 /* write an integer */
121 BOOL asn1_write_Integer(ASN1_DATA *data, int i)
122 {
123         if (!asn1_push_tag(data, ASN1_INTEGER)) return False;
124         do {
125                 asn1_write_uint8(data, i);
126                 i = i >> 8;
127         } while (i);
128         return asn1_pop_tag(data);
129 }
130
131 /* write an object ID to a ASN1 buffer */
132 BOOL asn1_write_OID(ASN1_DATA *data, const char *OID)
133 {
134         unsigned v, v2;
135         const char *p = (const char *)OID;
136         char *newp;
137
138         if (!asn1_push_tag(data, ASN1_OID))
139                 return False;
140         v = strtol(p, &newp, 10);
141         p = newp;
142         v2 = strtol(p, &newp, 10);
143         p = newp;
144         if (!asn1_write_uint8(data, 40*v + v2))
145                 return False;
146
147         while (*p) {
148                 v = strtol(p, &newp, 10);
149                 p = newp;
150                 if (v >= (1<<28)) asn1_write_uint8(data, 0x80 | ((v>>28)&0xff));
151                 if (v >= (1<<21)) asn1_write_uint8(data, 0x80 | ((v>>21)&0xff));
152                 if (v >= (1<<14)) asn1_write_uint8(data, 0x80 | ((v>>14)&0xff));
153                 if (v >= (1<<7)) asn1_write_uint8(data, 0x80 | ((v>>7)&0xff));
154                 if (!asn1_write_uint8(data, v&0x7f))
155                         return False;
156         }
157         return asn1_pop_tag(data);
158 }
159
160 /* write an octet string */
161 BOOL asn1_write_OctetString(ASN1_DATA *data, const void *p, size_t length)
162 {
163         asn1_push_tag(data, ASN1_OCTET_STRING);
164         asn1_write(data, p, length);
165         asn1_pop_tag(data);
166         return !data->has_error;
167 }
168
169 /* write a general string */
170 BOOL asn1_write_GeneralString(ASN1_DATA *data, const char *s)
171 {
172         asn1_push_tag(data, ASN1_GENERAL_STRING);
173         asn1_write(data, s, strlen(s));
174         asn1_pop_tag(data);
175         return !data->has_error;
176 }
177
178 /* write a BOOLEAN */
179 BOOL asn1_write_BOOLEAN(ASN1_DATA *data, BOOL v)
180 {
181         asn1_write_uint8(data, ASN1_BOOLEAN);
182         asn1_write_uint8(data, v);
183         return !data->has_error;
184 }
185
186 /* write a BOOLEAN - hmm, I suspect this one is the correct one, and the 
187    above boolean is bogus. Need to check */
188 BOOL asn1_write_BOOLEAN2(ASN1_DATA *data, BOOL v)
189 {
190         asn1_push_tag(data, ASN1_BOOLEAN);
191         asn1_write_uint8(data, v);
192         asn1_pop_tag(data);
193         return !data->has_error;
194 }
195
196 /* check a BOOLEAN */
197 BOOL asn1_check_BOOLEAN(ASN1_DATA *data, BOOL v)
198 {
199         uint8 b = 0;
200
201         asn1_read_uint8(data, &b);
202         if (b != ASN1_BOOLEAN) {
203                 data->has_error = True;
204                 return False;
205         }
206         asn1_read_uint8(data, &b);
207         if (b != v) {
208                 data->has_error = True;
209                 return False;
210         }
211         return !data->has_error;
212 }
213
214
215 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
216 BOOL asn1_load(ASN1_DATA *data, DATA_BLOB blob)
217 {
218         ZERO_STRUCTP(data);
219         data->data = memdup(blob.data, blob.length);
220         if (!data->data) {
221                 data->has_error = True;
222                 return False;
223         }
224         data->length = blob.length;
225         return True;
226 }
227
228 /* read from a ASN1 buffer, advancing the buffer pointer */
229 BOOL asn1_read(ASN1_DATA *data, void *p, int len)
230 {
231         if (data->has_error)
232                 return False;
233
234         if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len) {
235                 data->has_error = True;
236                 return False;
237         }
238
239         if (data->ofs + len > data->length) {
240                 data->has_error = True;
241                 return False;
242         }
243         memcpy(p, data->data + data->ofs, len);
244         data->ofs += len;
245         return True;
246 }
247
248 /* read a uint8 from a ASN1 buffer */
249 BOOL asn1_read_uint8(ASN1_DATA *data, uint8 *v)
250 {
251         return asn1_read(data, v, 1);
252 }
253
254 /* start reading a nested asn1 structure */
255 BOOL asn1_start_tag(ASN1_DATA *data, uint8 tag)
256 {
257         uint8 b;
258         struct nesting *nesting;
259         
260         if (!asn1_read_uint8(data, &b))
261                 return False;
262
263         if (b != tag) {
264                 data->has_error = True;
265                 return False;
266         }
267         nesting = SMB_MALLOC_P(struct nesting);
268         if (!nesting) {
269                 data->has_error = True;
270                 return False;
271         }
272
273         if (!asn1_read_uint8(data, &b)) {
274                 return False;
275         }
276
277         if (b & 0x80) {
278                 int n = b & 0x7f;
279                 if (!asn1_read_uint8(data, &b))
280                         return False;
281                 nesting->taglen = b;
282                 while (n > 1) {
283                         if (!asn1_read_uint8(data, &b)) 
284                                 return False;
285                         nesting->taglen = (nesting->taglen << 8) | b;
286                         n--;
287                 }
288         } else {
289                 nesting->taglen = b;
290         }
291         nesting->start = data->ofs;
292         nesting->next = data->nesting;
293         data->nesting = nesting;
294         return !data->has_error;
295 }
296
297
298 /* stop reading a tag */
299 BOOL asn1_end_tag(ASN1_DATA *data)
300 {
301         struct nesting *nesting;
302
303         /* make sure we read it all */
304         if (asn1_tag_remaining(data) != 0) {
305                 data->has_error = True;
306                 return False;
307         }
308
309         nesting = data->nesting;
310
311         if (!nesting) {
312                 data->has_error = True;
313                 return False;
314         }
315
316         data->nesting = nesting->next;
317         free(nesting);
318         return True;
319 }
320
321 /* work out how many bytes are left in this nested tag */
322 int asn1_tag_remaining(ASN1_DATA *data)
323 {
324         if (data->has_error)
325                 return 0;
326
327         if (!data->nesting) {
328                 data->has_error = True;
329                 return -1;
330         }
331         return data->nesting->taglen - (data->ofs - data->nesting->start);
332 }
333
334 /* read an object ID from a ASN1 buffer */
335 BOOL asn1_read_OID(ASN1_DATA *data, char **OID)
336 {
337         uint8 b;
338         pstring oid_str;
339         fstring el;
340
341         if (!asn1_start_tag(data, ASN1_OID)) return False;
342         asn1_read_uint8(data, &b);
343
344         oid_str[0] = 0;
345         fstr_sprintf(el, "%u",  b/40);
346         pstrcat(oid_str, el);
347         fstr_sprintf(el, " %u",  b%40);
348         pstrcat(oid_str, el);
349
350         while (asn1_tag_remaining(data) > 0) {
351                 unsigned v = 0;
352                 do {
353                         asn1_read_uint8(data, &b);
354                         v = (v<<7) | (b&0x7f);
355                 } while (!data->has_error && b & 0x80);
356                 fstr_sprintf(el, " %u",  v);
357                 pstrcat(oid_str, el);
358         }
359
360         asn1_end_tag(data);
361
362         *OID = SMB_STRDUP(oid_str);
363
364         return !data->has_error;
365 }
366
367 /* check that the next object ID is correct */
368 BOOL asn1_check_OID(ASN1_DATA *data, const char *OID)
369 {
370         char *id;
371
372         if (!asn1_read_OID(data, &id)) return False;
373
374         if (strcmp(id, OID) != 0) {
375                 data->has_error = True;
376                 return False;
377         }
378         free(id);
379         return True;
380 }
381
382 /* read a GeneralString from a ASN1 buffer */
383 BOOL asn1_read_GeneralString(ASN1_DATA *data, char **s)
384 {
385         int len;
386         if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return False;
387         len = asn1_tag_remaining(data);
388         if (len < 0) {
389                 data->has_error = True;
390                 return False;
391         }
392         *s = SMB_MALLOC(len+1);
393         if (! *s) {
394                 data->has_error = True;
395                 return False;
396         }
397         asn1_read(data, *s, len);
398         (*s)[len] = 0;
399         asn1_end_tag(data);
400         return !data->has_error;
401 }
402
403 /* read a octet string blob */
404 BOOL asn1_read_OctetString(ASN1_DATA *data, DATA_BLOB *blob)
405 {
406         int len;
407         ZERO_STRUCTP(blob);
408         if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return False;
409         len = asn1_tag_remaining(data);
410         if (len < 0) {
411                 data->has_error = True;
412                 return False;
413         }
414         *blob = data_blob(NULL, len);
415         asn1_read(data, blob->data, len);
416         asn1_end_tag(data);
417         return !data->has_error;
418 }
419
420 /* read an interger */
421 BOOL asn1_read_Integer(ASN1_DATA *data, int *i)
422 {
423         uint8 b;
424         *i = 0;
425         
426         if (!asn1_start_tag(data, ASN1_INTEGER)) return False;
427         while (asn1_tag_remaining(data)>0) {
428                 asn1_read_uint8(data, &b);
429                 *i = (*i << 8) + b;
430         }
431         return asn1_end_tag(data);      
432         
433 }
434
435 /* check a enumarted value is correct */
436 BOOL asn1_check_enumerated(ASN1_DATA *data, int v)
437 {
438         uint8 b;
439         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return False;
440         asn1_read_uint8(data, &b);
441         asn1_end_tag(data);
442
443         if (v != b)
444                 data->has_error = False;
445
446         return !data->has_error;
447 }
448
449 /* write an enumarted value to the stream */
450 BOOL asn1_write_enumerated(ASN1_DATA *data, uint8 v)
451 {
452         if (!asn1_push_tag(data, ASN1_ENUMERATED)) return False;
453         asn1_write_uint8(data, v);
454         asn1_pop_tag(data);
455         return !data->has_error;
456 }