2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/asn1.h>
31 * Start parsing ASN.1 object
33 * @v cursor ASN.1 object cursor
34 * @v type Expected type
35 * @ret len Length of object body, or negative error
37 * The object cursor will be updated to point to the start of the
38 * object body (i.e. the first byte following the length byte(s)), and
39 * the length of the object body (i.e. the number of bytes until the
40 * following object tag, if any) is returned.
42 * If any error occurs (i.e. if the object is not of the expected
43 * type, or if we overflow beyond the end of the ASN.1 object), then
44 * the cursor will be invalidated and a negative value will be
47 static int asn1_start ( struct asn1_cursor
*cursor
,
54 if ( cursor
->len
< 2 /* Tag byte and first length byte */ ) {
56 DBGC ( cursor
, "ASN1 %p too short\n", cursor
);
61 /* Check the tag byte */
62 if ( *( ( uint8_t * ) cursor
->data
) != type
) {
63 DBGC ( cursor
, "ASN1 %p type mismatch (expected %d, got %d)\n",
64 cursor
, type
, *( ( uint8_t * ) cursor
->data
) );
71 /* Extract length of the length field and sanity check */
72 len_len
= *( ( uint8_t * ) cursor
->data
);
73 if ( len_len
& 0x80 ) {
74 len_len
= ( len_len
& 0x7f );
80 if ( cursor
->len
< len_len
) {
81 DBGC ( cursor
, "ASN1 %p bad length field length %d (max "
82 "%zd)\n", cursor
, len_len
, cursor
->len
);
87 /* Extract the length and sanity check */
88 for ( len
= 0 ; len_len
; len_len
-- ) {
90 len
|= *( ( uint8_t * ) cursor
->data
);
94 if ( cursor
->len
< len
) {
95 DBGC ( cursor
, "ASN1 %p bad length %d (max %zd)\n",
96 cursor
, len
, cursor
->len
);
112 * @v cursor ASN.1 object cursor
113 * @v type Expected type
114 * @ret rc Return status code
116 * The object cursor will be updated to point to the body of the
117 * current ASN.1 object. If any error occurs, the object cursor will
120 int asn1_enter ( struct asn1_cursor
*cursor
, unsigned int type
) {
123 len
= asn1_start ( cursor
, type
);
128 DBGC ( cursor
, "ASN1 %p entered object type %02x (len %x)\n",
137 * @v cursor ASN.1 object cursor
138 * @v type Expected type
139 * @ret rc Return status code
141 * The object cursor will be updated to point to the next ASN.1
142 * object. If any error occurs, the object cursor will be
145 int asn1_skip ( struct asn1_cursor
*cursor
, unsigned int type
) {
148 len
= asn1_start ( cursor
, type
);
154 DBGC ( cursor
, "ASN1 %p skipped object type %02x (len %x)\n",
157 if ( ! cursor
->len
) {
158 DBGC ( cursor
, "ASN1 %p reached end of object\n", cursor
);