2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2001
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.
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.
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.
23 /* free an asn1 structure */
24 void asn1_free(ASN1_DATA
*data
)
26 struct nesting
*nesting
= data
->nesting
;
29 struct nesting
*nnext
= nesting
->next
;
34 SAFE_FREE(data
->data
);
37 /* write to the ASN1 buffer, advancing the buffer pointer */
38 BOOL
asn1_write(ASN1_DATA
*data
, const void *p
, int len
)
40 if (data
->has_error
) return False
;
41 if (data
->length
< data
->ofs
+len
) {
42 data
->data
= SMB_REALLOC_ARRAY(data
->data
, unsigned char,
45 data
->has_error
= True
;
48 data
->length
= data
->ofs
+len
;
50 memcpy(data
->data
+ data
->ofs
, p
, len
);
55 /* useful fn for writing a uint8 */
56 BOOL
asn1_write_uint8(ASN1_DATA
*data
, uint8 v
)
58 return asn1_write(data
, &v
, 1);
61 /* push a tag onto the asn1 data buffer. Used for nested structures */
62 BOOL
asn1_push_tag(ASN1_DATA
*data
, uint8 tag
)
64 struct nesting
*nesting
;
66 asn1_write_uint8(data
, tag
);
67 nesting
= SMB_MALLOC_P(struct nesting
);
69 data
->has_error
= True
;
73 nesting
->start
= data
->ofs
;
74 nesting
->next
= data
->nesting
;
75 data
->nesting
= nesting
;
76 return asn1_write_uint8(data
, 0xff);
80 BOOL
asn1_pop_tag(ASN1_DATA
*data
)
82 struct nesting
*nesting
;
85 if (data
->has_error
) {
89 nesting
= data
->nesting
;
92 data
->has_error
= True
;
95 len
= data
->ofs
- (nesting
->start
+1);
96 /* yes, this is ugly. We don't know in advance how many bytes the length
97 of a tag will take, so we assumed 1 byte. If we were wrong then we
98 need to correct our mistake */
100 data
->data
[nesting
->start
] = 0x83;
101 if (!asn1_write_uint8(data
, 0)) return False
;
102 if (!asn1_write_uint8(data
, 0)) return False
;
103 if (!asn1_write_uint8(data
, 0)) return False
;
104 memmove(data
->data
+nesting
->start
+4, data
->data
+nesting
->start
+1, len
);
105 data
->data
[nesting
->start
+1] = (len
>>16) & 0xFF;
106 data
->data
[nesting
->start
+2] = (len
>>8) & 0xFF;
107 data
->data
[nesting
->start
+3] = len
&0xff;
108 } else if (len
> 255) {
109 data
->data
[nesting
->start
] = 0x82;
110 if (!asn1_write_uint8(data
, 0)) return False
;
111 if (!asn1_write_uint8(data
, 0)) return False
;
112 memmove(data
->data
+nesting
->start
+3, data
->data
+nesting
->start
+1, len
);
113 data
->data
[nesting
->start
+1] = len
>>8;
114 data
->data
[nesting
->start
+2] = len
&0xff;
115 } else if (len
> 127) {
116 data
->data
[nesting
->start
] = 0x81;
117 if (!asn1_write_uint8(data
, 0)) return False
;
118 memmove(data
->data
+nesting
->start
+2, data
->data
+nesting
->start
+1, len
);
119 data
->data
[nesting
->start
+1] = len
;
121 data
->data
[nesting
->start
] = len
;
124 data
->nesting
= nesting
->next
;
130 /* write an integer */
131 BOOL
asn1_write_Integer(ASN1_DATA
*data
, int i
)
133 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return False
;
135 asn1_write_uint8(data
, i
);
138 return asn1_pop_tag(data
);
141 /* write an object ID to a ASN1 buffer */
142 BOOL
asn1_write_OID(ASN1_DATA
*data
, const char *OID
)
145 const char *p
= (const char *)OID
;
148 if (!asn1_push_tag(data
, ASN1_OID
))
150 v
= strtol(p
, &newp
, 10);
152 v2
= strtol(p
, &newp
, 10);
154 if (!asn1_write_uint8(data
, 40*v
+ v2
))
158 v
= strtol(p
, &newp
, 10);
160 if (v
>= (1<<28)) asn1_write_uint8(data
, 0x80 | ((v
>>28)&0xff));
161 if (v
>= (1<<21)) asn1_write_uint8(data
, 0x80 | ((v
>>21)&0xff));
162 if (v
>= (1<<14)) asn1_write_uint8(data
, 0x80 | ((v
>>14)&0xff));
163 if (v
>= (1<<7)) asn1_write_uint8(data
, 0x80 | ((v
>>7)&0xff));
164 if (!asn1_write_uint8(data
, v
&0x7f))
167 return asn1_pop_tag(data
);
170 /* write an octet string */
171 BOOL
asn1_write_OctetString(ASN1_DATA
*data
, const void *p
, size_t length
)
173 asn1_push_tag(data
, ASN1_OCTET_STRING
);
174 asn1_write(data
, p
, length
);
176 return !data
->has_error
;
179 /* write a general string */
180 BOOL
asn1_write_GeneralString(ASN1_DATA
*data
, const char *s
)
182 asn1_push_tag(data
, ASN1_GENERAL_STRING
);
183 asn1_write(data
, s
, strlen(s
));
185 return !data
->has_error
;
188 /* write a BOOLEAN */
189 BOOL
asn1_write_BOOLEAN(ASN1_DATA
*data
, BOOL v
)
191 asn1_write_uint8(data
, ASN1_BOOLEAN
);
192 asn1_write_uint8(data
, v
);
193 return !data
->has_error
;
196 /* write a BOOLEAN - hmm, I suspect this one is the correct one, and the
197 above boolean is bogus. Need to check */
198 BOOL
asn1_write_BOOLEAN2(ASN1_DATA
*data
, BOOL v
)
200 asn1_push_tag(data
, ASN1_BOOLEAN
);
201 asn1_write_uint8(data
, v
);
203 return !data
->has_error
;
206 /* check a BOOLEAN */
207 BOOL
asn1_check_BOOLEAN(ASN1_DATA
*data
, BOOL v
)
211 asn1_read_uint8(data
, &b
);
212 if (b
!= ASN1_BOOLEAN
) {
213 data
->has_error
= True
;
216 asn1_read_uint8(data
, &b
);
218 data
->has_error
= True
;
221 return !data
->has_error
;
225 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
226 BOOL
asn1_load(ASN1_DATA
*data
, DATA_BLOB blob
)
229 data
->data
= (unsigned char *)memdup(blob
.data
, blob
.length
);
231 data
->has_error
= True
;
234 data
->length
= blob
.length
;
238 /* read from a ASN1 buffer, advancing the buffer pointer */
239 BOOL
asn1_read(ASN1_DATA
*data
, void *p
, int len
)
244 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
) {
245 data
->has_error
= True
;
249 if (data
->ofs
+ len
> data
->length
) {
250 data
->has_error
= True
;
253 memcpy(p
, data
->data
+ data
->ofs
, len
);
258 /* read a uint8 from a ASN1 buffer */
259 BOOL
asn1_read_uint8(ASN1_DATA
*data
, uint8
*v
)
261 return asn1_read(data
, v
, 1);
264 /* start reading a nested asn1 structure */
265 BOOL
asn1_start_tag(ASN1_DATA
*data
, uint8 tag
)
268 struct nesting
*nesting
;
270 if (!asn1_read_uint8(data
, &b
))
274 data
->has_error
= True
;
277 nesting
= SMB_MALLOC_P(struct nesting
);
279 data
->has_error
= True
;
283 if (!asn1_read_uint8(data
, &b
)) {
290 if (!asn1_read_uint8(data
, &b
)) {
296 if (!asn1_read_uint8(data
, &b
)) {
300 nesting
->taglen
= (nesting
->taglen
<< 8) | b
;
306 nesting
->start
= data
->ofs
;
307 nesting
->next
= data
->nesting
;
308 data
->nesting
= nesting
;
309 return !data
->has_error
;
313 /* stop reading a tag */
314 BOOL
asn1_end_tag(ASN1_DATA
*data
)
316 struct nesting
*nesting
;
318 /* make sure we read it all */
319 if (asn1_tag_remaining(data
) != 0) {
320 data
->has_error
= True
;
324 nesting
= data
->nesting
;
327 data
->has_error
= True
;
331 data
->nesting
= nesting
->next
;
336 /* work out how many bytes are left in this nested tag */
337 int asn1_tag_remaining(ASN1_DATA
*data
)
342 if (!data
->nesting
) {
343 data
->has_error
= True
;
346 return data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
349 /* read an object ID from a ASN1 buffer */
350 BOOL
asn1_read_OID(ASN1_DATA
*data
, char **OID
)
358 if (!asn1_start_tag(data
, ASN1_OID
)) {
361 asn1_read_uint8(data
, &b
);
364 fstr_sprintf(el
, "%u", b
/40);
365 pstrcat(oid_str
, el
);
366 fstr_sprintf(el
, " %u", b
%40);
367 pstrcat(oid_str
, el
);
369 while (asn1_tag_remaining(data
) > 0) {
372 asn1_read_uint8(data
, &b
);
373 v
= (v
<<7) | (b
&0x7f);
374 } while (!data
->has_error
&& b
& 0x80);
375 fstr_sprintf(el
, " %u", v
);
376 pstrcat(oid_str
, el
);
381 if (!data
->has_error
) {
382 *OID
= SMB_STRDUP(oid_str
);
385 return !data
->has_error
;
388 /* check that the next object ID is correct */
389 BOOL
asn1_check_OID(ASN1_DATA
*data
, const char *OID
)
393 if (!asn1_read_OID(data
, &id
)) {
397 if (strcmp(id
, OID
) != 0) {
398 data
->has_error
= True
;
405 /* read a GeneralString from a ASN1 buffer */
406 BOOL
asn1_read_GeneralString(ASN1_DATA
*data
, char **s
)
413 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) {
416 len
= asn1_tag_remaining(data
);
418 data
->has_error
= True
;
421 str
= SMB_MALLOC_ARRAY(char, len
+1);
423 data
->has_error
= True
;
426 asn1_read(data
, str
, len
);
430 if (!data
->has_error
) {
433 return !data
->has_error
;
436 /* read a octet string blob */
437 BOOL
asn1_read_OctetString(ASN1_DATA
*data
, DATA_BLOB
*blob
)
441 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return False
;
442 len
= asn1_tag_remaining(data
);
444 data
->has_error
= True
;
447 *blob
= data_blob(NULL
, len
);
448 asn1_read(data
, blob
->data
, len
);
450 return !data
->has_error
;
453 /* read an interger */
454 BOOL
asn1_read_Integer(ASN1_DATA
*data
, int *i
)
459 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return False
;
460 while (asn1_tag_remaining(data
)>0) {
461 asn1_read_uint8(data
, &b
);
464 return asn1_end_tag(data
);
468 /* check a enumarted value is correct */
469 BOOL
asn1_check_enumerated(ASN1_DATA
*data
, int v
)
472 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return False
;
473 asn1_read_uint8(data
, &b
);
477 data
->has_error
= False
;
479 return !data
->has_error
;
482 /* write an enumarted value to the stream */
483 BOOL
asn1_write_enumerated(ASN1_DATA
*data
, uint8 v
)
485 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return False
;
486 asn1_write_uint8(data
, v
);
488 return !data
->has_error
;