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 SAFE_FREE(data
->data
);
29 /* write to the ASN1 buffer, advancing the buffer pointer */
30 BOOL
asn1_write(ASN1_DATA
*data
, const void *p
, int len
)
32 if (data
->has_error
) return False
;
33 if (data
->length
< data
->ofs
+len
) {
35 newp
= Realloc(data
->data
, data
->ofs
+len
);
37 SAFE_FREE(data
->data
);
38 data
->has_error
= True
;
42 data
->length
= data
->ofs
+len
;
44 memcpy(data
->data
+ data
->ofs
, p
, len
);
49 /* useful fn for writing a uint8 */
50 BOOL
asn1_write_uint8(ASN1_DATA
*data
, uint8 v
)
52 return asn1_write(data
, &v
, 1);
55 /* push a tag onto the asn1 data buffer. Used for nested structures */
56 BOOL
asn1_push_tag(ASN1_DATA
*data
, uint8 tag
)
58 struct nesting
*nesting
;
60 asn1_write_uint8(data
, tag
);
61 nesting
= (struct nesting
*)malloc(sizeof(struct nesting
));
63 data
->has_error
= True
;
67 nesting
->start
= data
->ofs
;
68 nesting
->next
= data
->nesting
;
69 data
->nesting
= nesting
;
70 return asn1_write_uint8(data
, 0xff);
74 BOOL
asn1_pop_tag(ASN1_DATA
*data
)
76 struct nesting
*nesting
;
79 nesting
= data
->nesting
;
82 data
->has_error
= True
;
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 */
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
;
102 data
->data
[nesting
->start
] = len
;
105 data
->nesting
= nesting
->next
;
111 /* write an integer */
112 BOOL
asn1_write_Integer(ASN1_DATA
*data
, int i
)
114 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return False
;
116 asn1_write_uint8(data
, i
);
119 return asn1_pop_tag(data
);
122 /* write an object ID to a ASN1 buffer */
123 BOOL
asn1_write_OID(ASN1_DATA
*data
, const char *OID
)
126 const char *p
= (const char *)OID
;
129 if (!asn1_push_tag(data
, ASN1_OID
))
131 v
= strtol(p
, &newp
, 10);
133 v2
= strtol(p
, &newp
, 10);
135 if (!asn1_write_uint8(data
, 40*v
+ v2
))
139 v
= strtol(p
, &newp
, 10);
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))
148 return asn1_pop_tag(data
);
151 /* write an octet string */
152 BOOL
asn1_write_OctetString(ASN1_DATA
*data
, const void *p
, size_t length
)
154 asn1_push_tag(data
, ASN1_OCTET_STRING
);
155 asn1_write(data
, p
, length
);
157 return !data
->has_error
;
160 /* write a general string */
161 BOOL
asn1_write_GeneralString(ASN1_DATA
*data
, const char *s
)
163 asn1_push_tag(data
, ASN1_GENERAL_STRING
);
164 asn1_write(data
, s
, strlen(s
));
166 return !data
->has_error
;
169 /* write a BOOLEAN */
170 BOOL
asn1_write_BOOLEAN(ASN1_DATA
*data
, BOOL v
)
172 asn1_write_uint8(data
, ASN1_BOOLEAN
);
173 asn1_write_uint8(data
, v
);
174 return !data
->has_error
;
177 /* check a BOOLEAN */
178 BOOL
asn1_check_BOOLEAN(ASN1_DATA
*data
, BOOL v
)
182 asn1_read_uint8(data
, &b
);
183 if (b
!= ASN1_BOOLEAN
) {
184 data
->has_error
= True
;
187 asn1_read_uint8(data
, &b
);
189 data
->has_error
= True
;
192 return !data
->has_error
;
196 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
197 BOOL
asn1_load(ASN1_DATA
*data
, DATA_BLOB blob
)
200 data
->data
= memdup(blob
.data
, blob
.length
);
202 data
->has_error
= True
;
205 data
->length
= blob
.length
;
209 /* read from a ASN1 buffer, advancing the buffer pointer */
210 BOOL
asn1_read(ASN1_DATA
*data
, void *p
, int len
)
212 if (data
->ofs
+ len
> data
->length
) {
213 data
->has_error
= True
;
216 memcpy(p
, data
->data
+ data
->ofs
, len
);
221 /* read a uint8 from a ASN1 buffer */
222 BOOL
asn1_read_uint8(ASN1_DATA
*data
, uint8
*v
)
224 return asn1_read(data
, v
, 1);
227 /* start reading a nested asn1 structure */
228 BOOL
asn1_start_tag(ASN1_DATA
*data
, uint8 tag
)
231 struct nesting
*nesting
;
233 asn1_read_uint8(data
, &b
);
235 data
->has_error
= True
;
238 nesting
= (struct nesting
*)malloc(sizeof(struct nesting
));
240 data
->has_error
= True
;
244 asn1_read_uint8(data
, &b
);
248 data
->has_error
= True
;
251 asn1_read_uint8(data
, &b
);
254 asn1_read_uint8(data
, &b
);
255 nesting
->taglen
= (nesting
->taglen
<< 8) | b
;
260 nesting
->start
= data
->ofs
;
261 nesting
->next
= data
->nesting
;
262 data
->nesting
= nesting
;
263 return !data
->has_error
;
267 /* stop reading a tag */
268 BOOL
asn1_end_tag(ASN1_DATA
*data
)
270 struct nesting
*nesting
;
272 /* make sure we read it all */
273 if (asn1_tag_remaining(data
) != 0) {
274 data
->has_error
= True
;
278 nesting
= data
->nesting
;
281 data
->has_error
= True
;
285 data
->nesting
= nesting
->next
;
290 /* work out how many bytes are left in this nested tag */
291 int asn1_tag_remaining(ASN1_DATA
*data
)
293 if (!data
->nesting
) {
294 data
->has_error
= True
;
297 return data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
300 /* read an object ID from a ASN1 buffer */
301 BOOL
asn1_read_OID(ASN1_DATA
*data
, char **OID
)
307 if (!asn1_start_tag(data
, ASN1_OID
)) return False
;
308 asn1_read_uint8(data
, &b
);
311 snprintf(el
, sizeof(el
), "%u", b
/40);
313 snprintf(el
, sizeof(el
), " %u", b
%40);
316 while (asn1_tag_remaining(data
) > 0) {
319 asn1_read_uint8(data
, &b
);
320 v
= (v
<<7) | (b
&0x7f);
321 } while (!data
->has_error
&& b
& 0x80);
322 snprintf(el
, sizeof(el
), " %u", v
);
330 return !data
->has_error
;
333 /* check that the next object ID is correct */
334 BOOL
asn1_check_OID(ASN1_DATA
*data
, char *OID
)
338 if (!asn1_read_OID(data
, &id
)) return False
;
340 if (strcmp(id
, OID
) != 0) {
341 data
->has_error
= True
;
348 /* read a GeneralString from a ASN1 buffer */
349 BOOL
asn1_read_GeneralString(ASN1_DATA
*data
, char **s
)
352 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return False
;
353 len
= asn1_tag_remaining(data
);
356 data
->has_error
= True
;
359 asn1_read(data
, *s
, len
);
362 return !data
->has_error
;
365 /* read a octet string blob */
366 BOOL
asn1_read_OctetString(ASN1_DATA
*data
, DATA_BLOB
*blob
)
369 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return False
;
370 len
= asn1_tag_remaining(data
);
371 *blob
= data_blob(NULL
, len
);
372 asn1_read(data
, blob
->data
, len
);
374 return !data
->has_error
;
377 /* read an interger */
378 BOOL
asn1_read_Integer(ASN1_DATA
*data
, int *i
)
383 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return False
;
384 while (asn1_tag_remaining(data
)>0) {
385 *i
= (*i
<< 8) + asn1_read_uint8(data
, &b
);
387 return asn1_end_tag(data
);
391 /* check a enumarted value is correct */
392 BOOL
asn1_check_enumerated(ASN1_DATA
*data
, int v
)
395 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return False
;
396 asn1_read_uint8(data
, &b
);
398 return !data
->has_error
&& (v
== b
);
401 /* check a enumarted value is correct */
402 BOOL
asn1_write_enumerated(ASN1_DATA
*data
, uint8 v
)
404 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return False
;
405 asn1_write_uint8(data
, v
);
407 return !data
->has_error
;