* der-iterator.c: New file.
[gd/nettle] / asn1.h
1 /* asn1.h
2  *
3  * Some very limited asn.1 support.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2005 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25
26 #ifndef NETTLE_ASN1_H_INCLUDED
27 #define NETTLE_ASN1_H_INCLUDED
28
29 #include "nettle-types.h"
30
31 /* enum asn1_type keeps the class number and the constructive in bits
32    13-14, and the constructive flag in bit 12. The remaining 14 bits
33    are the tag (although currently, only tags in the range 0-30 are
34    supported). */
35
36 enum
37   {
38     ASN1_TYPE_CONSTRUCTED = 1 << 12,
39
40     ASN1_CLASS_UNIVERSAL = 0,
41     ASN1_CLASS_APPLICATION = 1 << 13,
42     ASN1_CLASS_CONTEXT_SPECIFIC = 2 << 13,
43     ASN1_CLASS_PRIVATE = 3 << 13,
44
45     ASN1_CLASS_MASK = 3 << 13,
46     ASN1_CLASS_SHIFT = 13,
47   };
48
49 enum asn1_type
50   {
51     ASN1_BOOLEAN = 1,
52     ASN1_INTEGER = 2,
53     ASN1_BITSTRING = 3,
54     ASN1_OCTETSTRING = 4,
55     ASN1_NULL = 5,
56     ASN1_IDENTIFIER = 6,
57     ASN1_REAL = 9,
58     ASN1_ENUMERATED = 10,
59     ASN1_UTF8STRING = 12,
60     ASN1_SEQUENCE = 16 | ASN1_TYPE_CONSTRUCTED,
61     ASN1_SET = 17 | ASN1_TYPE_CONSTRUCTED,
62     ASN1_PRINTABLESTRING = 19,
63     ASN1_TELETEXSTRING = 20,
64     ASN1_IA5STRING = 22,
65     ASN1_UTC = 23,
66     ASN1_UNIVERSALSTRING = 28,
67     ASN1_BMPSTRING = 30,
68   };
69
70 enum asn1_iterator_result
71   {
72     ASN1_ITERATOR_ERROR,
73     ASN1_ITERATOR_PRIMITIVE,
74     ASN1_ITERATOR_CONSTRUCTED,
75     ASN1_ITERATOR_END,
76   };
77
78 /* Parsing DER objects. */
79 struct asn1_der_iterator
80 {
81   unsigned buffer_length;
82   const uint8_t *buffer;
83
84   /* Next object to parse. */
85   unsigned pos;
86
87   enum asn1_type type;
88
89   /* Pointer to the current object */
90   unsigned length;
91   const uint8_t *data;
92 };
93
94 /* Initializes the iterator. */
95 enum asn1_iterator_result
96 asn1_der_iterator_first(struct asn1_der_iterator *iterator,
97                         unsigned length, const uint8_t *input);
98
99 enum asn1_iterator_result
100 asn1_der_iterator_next(struct asn1_der_iterator *iterator);
101
102 /* Starts parsing of a constructed object. */
103 enum asn1_iterator_result
104 asn1_der_decode_constructed(struct asn1_der_iterator *i,
105                             struct asn1_der_iterator *contents);
106
107 /* All these functions return 1 on success, 0 on failure */
108 int
109 asn1_der_get_uint32(struct asn1_der_iterator *i,
110                     uint32_t *x);
111
112 #endif /* NETTLE_ASN1_H_INCLUDED */