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 -1 on 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_object ( struct asn1_cursor *cursor,
53 if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
55 DBGC ( cursor, "ASN1 %p too short\n", cursor );
59 /* Check the tag byte */
60 if ( cursor->data[0] != type ) {
61 DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
62 cursor, type, cursor->data[0] );
68 /* Extract length of the length field and sanity check */
69 len_len = cursor->data[0];
70 if ( len_len & 0x80 ) {
71 len_len = ( len_len & 0x7f );
77 if ( cursor->len < len_len ) {
78 DBGC ( cursor, "ASN1 %p bad length field length %d (max "
79 "%zd)\n", cursor, len_len, cursor->len );
83 /* Extract the length and sanity check */
84 for ( len = 0 ; len_len ; len_len-- ) {
86 len |= cursor->data[0];
90 if ( cursor->len < len ) {
91 DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
92 cursor, len, cursor->len );
107 * @v cursor ASN.1 object cursor
108 * @v type Expected type
109 * @ret rc Return status code
111 * The object cursor will be updated to point to the body of the
112 * current ASN.1 object. If any error occurs, the object cursor will
115 int asn1_enter_object ( struct asn1_cursor *cursor, unsigned int type ) {
118 len = asn1_start_object ( cursor, type );
123 DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
132 * @v cursor ASN.1 object cursor
133 * @v type Expected type
134 * @ret rc Return status code
136 * The object cursor will be updated to point to the next ASN.1
137 * object. If any error occurs, the object cursor will be
140 int asn1_skip_object ( struct asn1_cursor *cursor, unsigned int type ) {
143 len = asn1_start_object ( cursor, type );
149 DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
152 if ( ! cursor->len ) {
153 DBGC ( cursor, "ASN1 %p reached end of object\n", cursor );