[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / 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 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /* Name mangling */
36 #define asn1_der_iterator_first nettle_asn1_der_iterator_first
37 #define asn1_der_iterator_next nettle_asn1_der_iterator_next
38 #define asn1_der_decode_constructed nettle_asn1_der_decode_constructed
39 #define asn1_der_decode_constructed_last nettle_asn1_der_decode_constructed_last
40 #define asn1_der_decode_bitstring nettle_asn1_der_decode_bitstring
41 #define asn1_der_decode_bitstring_last nettle_asn1_der_decode_bitstring_last
42 #define asn1_der_get_uint32 nettle_asn1_der_get_uint32
43 #define asn1_der_get_bignum nettle_asn1_der_get_bignum
44
45
46 /* enum asn1_type keeps the class number and the constructive in bits
47    13-14, and the constructive flag in bit 12. The remaining 14 bits
48    are the tag (although currently, only tags in the range 0-30 are
49    supported). */
50
51 enum
52   {
53     ASN1_TYPE_CONSTRUCTED = 1 << 12,
54
55     ASN1_CLASS_UNIVERSAL = 0,
56     ASN1_CLASS_APPLICATION = 1 << 13,
57     ASN1_CLASS_CONTEXT_SPECIFIC = 2 << 13,
58     ASN1_CLASS_PRIVATE = 3 << 13,
59
60     ASN1_CLASS_MASK = 3 << 13,
61     ASN1_CLASS_SHIFT = 13,
62   };
63
64 enum asn1_type
65   {
66     ASN1_BOOLEAN = 1,
67     ASN1_INTEGER = 2,
68     ASN1_BITSTRING = 3,
69     ASN1_OCTETSTRING = 4,
70     ASN1_NULL = 5,
71     ASN1_IDENTIFIER = 6,
72     ASN1_REAL = 9,
73     ASN1_ENUMERATED = 10,
74     ASN1_UTF8STRING = 12,
75     ASN1_SEQUENCE = 16 | ASN1_TYPE_CONSTRUCTED,
76     ASN1_SET = 17 | ASN1_TYPE_CONSTRUCTED,
77     ASN1_PRINTABLESTRING = 19,
78     ASN1_TELETEXSTRING = 20,
79     ASN1_IA5STRING = 22,
80     ASN1_UTC = 23,
81     ASN1_UNIVERSALSTRING = 28,
82     ASN1_BMPSTRING = 30,
83   };
84
85 enum asn1_iterator_result
86   {
87     ASN1_ITERATOR_ERROR,
88     ASN1_ITERATOR_PRIMITIVE,
89     ASN1_ITERATOR_CONSTRUCTED,
90     ASN1_ITERATOR_END,
91   };
92
93 /* Parsing DER objects. */
94 struct asn1_der_iterator
95 {
96   unsigned buffer_length;
97   const uint8_t *buffer;
98
99   /* Next object to parse. */
100   unsigned pos;
101
102   enum asn1_type type;
103
104   /* Pointer to the current object */
105   unsigned length;
106   const uint8_t *data;
107 };
108
109 /* Initializes the iterator. */
110 enum asn1_iterator_result
111 asn1_der_iterator_first(struct asn1_der_iterator *iterator,
112                         unsigned length, const uint8_t *input);
113
114 enum asn1_iterator_result
115 asn1_der_iterator_next(struct asn1_der_iterator *iterator);
116
117 /* Starts parsing of a constructed object. */
118 enum asn1_iterator_result
119 asn1_der_decode_constructed(struct asn1_der_iterator *i,
120                             struct asn1_der_iterator *contents);
121
122 /* For the common case that we have a sequence at the end of the
123    object. Checks that the current object is the final one, and then
124    reinitializes the iterator to parse its ontents. */
125 enum asn1_iterator_result
126 asn1_der_decode_constructed_last(struct asn1_der_iterator *i);
127
128 enum asn1_iterator_result
129 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
130                           struct asn1_der_iterator *contents);
131
132 enum asn1_iterator_result
133 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i);
134
135 /* All these functions return 1 on success, 0 on failure */
136 int
137 asn1_der_get_uint32(struct asn1_der_iterator *i,
138                     uint32_t *x);
139
140 #ifdef __cplusplus
141 }
142 #endif
143
144 #endif /* NETTLE_ASN1_H_INCLUDED */