b7cfca41fbf19facfeadde3688cf38a493d09e21
[tprouty/samba.git] / 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 = 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 = (struct nesting *)malloc(sizeof(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 > 255) {
90                 data->data[nesting->start] = 0x82;
91                 if (!asn1_write_uint8(data, 0)) return False;
92                 if (!asn1_write_uint8(data, 0)) return False;
93                 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
94                 data->data[nesting->start+1] = len>>8;
95                 data->data[nesting->start+2] = len&0xff;
96         } else if (len > 127) {
97                 data->data[nesting->start] = 0x81;
98                 if (!asn1_write_uint8(data, 0)) return False;
99                 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
100                 data->data[nesting->start+1] = len;
101         } else {
102                 data->data[nesting->start] = len;
103         }
104
105         data->nesting = nesting->next;
106         free(nesting);
107         return True;
108 }
109
110
111 /* write an integer */
112 BOOL asn1_write_Integer(ASN1_DATA *data, int i)
113 {
114         if (!asn1_push_tag(data, ASN1_INTEGER)) return False;
115         do {
116                 asn1_write_uint8(data, i);
117                 i = i >> 8;
118         } while (i);
119         return asn1_pop_tag(data);
120 }
121
122 /* write an object ID to a ASN1 buffer */
123 BOOL asn1_write_OID(ASN1_DATA *data, const char *OID)
124 {
125         unsigned v, v2;
126         const char *p = (const char *)OID;
127         char *newp;
128
129         if (!asn1_push_tag(data, ASN1_OID))
130                 return False;
131         v = strtol(p, &newp, 10);
132         p = newp;
133         v2 = strtol(p, &newp, 10);
134         p = newp;
135         if (!asn1_write_uint8(data, 40*v + v2))
136                 return False;
137
138         while (*p) {
139                 v = strtol(p, &newp, 10);
140                 p = newp;
141                 if (v >= (1<<28)) asn1_write_uint8(data, 0x80 | ((v>>28)&0xff));
142                 if (v >= (1<<21)) asn1_write_uint8(data, 0x80 | ((v>>21)&0xff));
143                 if (v >= (1<<14)) asn1_write_uint8(data, 0x80 | ((v>>14)&0xff));
144                 if (v >= (1<<7)) asn1_write_uint8(data, 0x80 | ((v>>7)&0xff));
145                 if (!asn1_write_uint8(data, v&0x7f))
146                         return False;
147         }
148         return asn1_pop_tag(data);
149 }
150
151 /* write an octet string */
152 BOOL asn1_write_OctetString(ASN1_DATA *data, const void *p, size_t length)
153 {
154         asn1_push_tag(data, ASN1_OCTET_STRING);
155         asn1_write(data, p, length);
156         asn1_pop_tag(data);
157         return !data->has_error;
158 }
159
160 /* write a general string */
161 BOOL asn1_write_GeneralString(ASN1_DATA *data, const char *s)
162 {
163         asn1_push_tag(data, ASN1_GENERAL_STRING);
164         asn1_write(data, s, strlen(s));
165         asn1_pop_tag(data);
166         return !data->has_error;
167 }
168
169 /* write a BOOLEAN */
170 BOOL asn1_write_BOOLEAN(ASN1_DATA *data, BOOL v)
171 {
172         asn1_write_uint8(data, ASN1_BOOLEAN);
173         asn1_write_uint8(data, v);
174         return !data->has_error;
175 }
176
177 /* write a BOOLEAN - hmm, I suspect this one is the correct one, and the 
178    above boolean is bogus. Need to check */
179 BOOL asn1_write_BOOLEAN2(ASN1_DATA *data, BOOL v)
180 {
181         asn1_push_tag(data, ASN1_BOOLEAN);
182         asn1_write_uint8(data, v);
183         asn1_pop_tag(data);
184         return !data->has_error;
185 }
186
187 /* check a BOOLEAN */
188 BOOL asn1_check_BOOLEAN(ASN1_DATA *data, BOOL v)
189 {
190         uint8 b = 0;
191
192         asn1_read_uint8(data, &b);
193         if (b != ASN1_BOOLEAN) {
194                 data->has_error = True;
195                 return False;
196         }
197         asn1_read_uint8(data, &b);
198         if (b != v) {
199                 data->has_error = True;
200                 return False;
201         }
202         return !data->has_error;
203 }
204
205
206 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
207 BOOL asn1_load(ASN1_DATA *data, DATA_BLOB blob)
208 {
209         ZERO_STRUCTP(data);
210         data->data = memdup(blob.data, blob.length);
211         if (!data->data) {
212                 data->has_error = True;
213                 return False;
214         }
215         data->length = blob.length;
216         return True;
217 }
218
219 /* read from a ASN1 buffer, advancing the buffer pointer */
220 BOOL asn1_read(ASN1_DATA *data, void *p, int len)
221 {
222         if (data->ofs + len > data->length) {
223                 data->has_error = True;
224                 return False;
225         }
226         memcpy(p, data->data + data->ofs, len);
227         data->ofs += len;
228         return True;
229 }
230
231 /* read a uint8 from a ASN1 buffer */
232 BOOL asn1_read_uint8(ASN1_DATA *data, uint8 *v)
233 {
234         return asn1_read(data, v, 1);
235 }
236
237 /* start reading a nested asn1 structure */
238 BOOL asn1_start_tag(ASN1_DATA *data, uint8 tag)
239 {
240         uint8 b;
241         struct nesting *nesting;
242         
243         asn1_read_uint8(data, &b);
244         if (b != tag) {
245                 data->has_error = True;
246                 return False;
247         }
248         nesting = (struct nesting *)malloc(sizeof(struct nesting));
249         if (!nesting) {
250                 data->has_error = True;
251                 return False;
252         }
253
254         asn1_read_uint8(data, &b);
255         if (b & 0x80) {
256                 int n = b & 0x7f;
257                 asn1_read_uint8(data, &b);
258                 nesting->taglen = b;
259                 while (n > 1) {
260                         asn1_read_uint8(data, &b);
261                         nesting->taglen = (nesting->taglen << 8) | b;
262                         n--;
263                 }
264         } else {
265                 nesting->taglen = b;
266         }
267         nesting->start = data->ofs;
268         nesting->next = data->nesting;
269         data->nesting = nesting;
270         return !data->has_error;
271 }
272
273
274 /* stop reading a tag */
275 BOOL asn1_end_tag(ASN1_DATA *data)
276 {
277         struct nesting *nesting;
278
279         /* make sure we read it all */
280         if (asn1_tag_remaining(data) != 0) {
281                 data->has_error = True;
282                 return False;
283         }
284
285         nesting = data->nesting;
286
287         if (!nesting) {
288                 data->has_error = True;
289                 return False;
290         }
291
292         data->nesting = nesting->next;
293         free(nesting);
294         return True;
295 }
296
297 /* work out how many bytes are left in this nested tag */
298 int asn1_tag_remaining(ASN1_DATA *data)
299 {
300         if (!data->nesting) {
301                 data->has_error = True;
302                 return -1;
303         }
304         return data->nesting->taglen - (data->ofs - data->nesting->start);
305 }
306
307 /* read an object ID from a ASN1 buffer */
308 BOOL asn1_read_OID(ASN1_DATA *data, char **OID)
309 {
310         uint8 b;
311         pstring oid;
312         fstring el;
313
314         if (!asn1_start_tag(data, ASN1_OID)) return False;
315         asn1_read_uint8(data, &b);
316
317         oid[0] = 0;
318         snprintf(el, sizeof(el), "%u",  b/40);
319         pstrcat(oid, el);
320         snprintf(el, sizeof(el), " %u",  b%40);
321         pstrcat(oid, el);
322
323         while (asn1_tag_remaining(data) > 0) {
324                 unsigned v = 0;
325                 do {
326                         asn1_read_uint8(data, &b);
327                         v = (v<<7) | (b&0x7f);
328                 } while (!data->has_error && b & 0x80);
329                 snprintf(el, sizeof(el), " %u",  v);
330                 pstrcat(oid, el);
331         }
332
333         asn1_end_tag(data);
334
335         *OID = strdup(oid);
336
337         return !data->has_error;
338 }
339
340 /* check that the next object ID is correct */
341 BOOL asn1_check_OID(ASN1_DATA *data, char *OID)
342 {
343         char *id;
344
345         if (!asn1_read_OID(data, &id)) return False;
346
347         if (strcmp(id, OID) != 0) {
348                 data->has_error = True;
349                 return False;
350         }
351         free(id);
352         return True;
353 }
354
355 /* read a GeneralString from a ASN1 buffer */
356 BOOL asn1_read_GeneralString(ASN1_DATA *data, char **s)
357 {
358         int len;
359         if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return False;
360         len = asn1_tag_remaining(data);
361         *s = malloc(len+1);
362         if (! *s) {
363                 data->has_error = True;
364                 return False;
365         }
366         asn1_read(data, *s, len);
367         (*s)[len] = 0;
368         asn1_end_tag(data);
369         return !data->has_error;
370 }
371
372 /* read a octet string blob */
373 BOOL asn1_read_OctetString(ASN1_DATA *data, DATA_BLOB *blob)
374 {
375         int len;
376         ZERO_STRUCTP(blob);
377         if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return False;
378         len = asn1_tag_remaining(data);
379         *blob = data_blob(NULL, len);
380         asn1_read(data, blob->data, len);
381         asn1_end_tag(data);
382         return !data->has_error;
383 }
384
385 /* read an interger */
386 BOOL asn1_read_Integer(ASN1_DATA *data, int *i)
387 {
388         uint8 b;
389         *i = 0;
390         
391         if (!asn1_start_tag(data, ASN1_INTEGER)) return False;
392         while (asn1_tag_remaining(data)>0) {
393                 asn1_read_uint8(data, &b);
394                 *i = (*i << 8) + b;
395         }
396         return asn1_end_tag(data);      
397         
398 }
399
400 /* check a enumarted value is correct */
401 BOOL asn1_check_enumerated(ASN1_DATA *data, int v)
402 {
403         uint8 b;
404         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return False;
405         asn1_read_uint8(data, &b);
406         asn1_end_tag(data);
407         return !data->has_error && (v == b);
408 }
409
410 /* check a enumarted value is correct */
411 BOOL asn1_write_enumerated(ASN1_DATA *data, uint8 v)
412 {
413         if (!asn1_push_tag(data, ASN1_ENUMERATED)) return False;
414         asn1_write_uint8(data, v);
415         asn1_pop_tag(data);
416         return !data->has_error;
417 }