Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:14

0001 /* asn1.h
0002 
0003    Limited support for ASN.1 DER decoding.
0004 
0005    Copyright (C) 2005 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033 
0034 #ifndef NETTLE_ASN1_H_INCLUDED
0035 #define NETTLE_ASN1_H_INCLUDED
0036 
0037 #include "nettle-types.h"
0038 
0039 #ifdef __cplusplus
0040 extern "C" {
0041 #endif
0042 
0043 /* Name mangling */
0044 #define asn1_der_iterator_first nettle_asn1_der_iterator_first
0045 #define asn1_der_iterator_next nettle_asn1_der_iterator_next
0046 #define asn1_der_decode_constructed nettle_asn1_der_decode_constructed
0047 #define asn1_der_decode_constructed_last nettle_asn1_der_decode_constructed_last
0048 #define asn1_der_decode_bitstring nettle_asn1_der_decode_bitstring
0049 #define asn1_der_decode_bitstring_last nettle_asn1_der_decode_bitstring_last
0050 #define asn1_der_get_uint32 nettle_asn1_der_get_uint32
0051 #define asn1_der_get_bignum nettle_asn1_der_get_bignum
0052 
0053 
0054 /* enum asn1_type keeps the class number and the constructive in bits
0055    13-14, and the constructive flag in bit 12. The remaining 14 bits
0056    are the tag (although currently, only tags in the range 0-30 are
0057    supported). */
0058 
0059 enum
0060   {
0061     ASN1_TYPE_CONSTRUCTED = 1 << 12,
0062 
0063     ASN1_CLASS_UNIVERSAL = 0,
0064     ASN1_CLASS_APPLICATION = 1 << 13,
0065     ASN1_CLASS_CONTEXT_SPECIFIC = 2 << 13,
0066     ASN1_CLASS_PRIVATE = 3 << 13,
0067 
0068     ASN1_CLASS_MASK = 3 << 13,
0069     ASN1_CLASS_SHIFT = 13,
0070   };
0071 
0072 enum asn1_type
0073   {
0074     ASN1_BOOLEAN = 1,
0075     ASN1_INTEGER = 2,
0076     ASN1_BITSTRING = 3,
0077     ASN1_OCTETSTRING = 4,
0078     ASN1_NULL = 5,
0079     ASN1_IDENTIFIER = 6,
0080     ASN1_REAL = 9,
0081     ASN1_ENUMERATED = 10,
0082     ASN1_UTF8STRING = 12,
0083     ASN1_SEQUENCE = 16 | ASN1_TYPE_CONSTRUCTED,
0084     ASN1_SET = 17 | ASN1_TYPE_CONSTRUCTED,
0085     ASN1_PRINTABLESTRING = 19,
0086     ASN1_TELETEXSTRING = 20,
0087     ASN1_IA5STRING = 22,
0088     ASN1_UTC = 23,
0089     ASN1_UNIVERSALSTRING = 28,
0090     ASN1_BMPSTRING = 30,
0091   };
0092 
0093 enum asn1_iterator_result
0094   {
0095     ASN1_ITERATOR_ERROR,
0096     ASN1_ITERATOR_PRIMITIVE,
0097     ASN1_ITERATOR_CONSTRUCTED,
0098     ASN1_ITERATOR_END,
0099   };
0100 
0101 /* Parsing DER objects. */
0102 struct asn1_der_iterator
0103 {
0104   size_t buffer_length;
0105   const uint8_t *buffer;
0106 
0107   /* Next object to parse. */
0108   size_t pos;
0109 
0110   enum asn1_type type;
0111 
0112   /* Pointer to the current object */
0113   size_t length;
0114   const uint8_t *data;
0115 };
0116 
0117 /* Initializes the iterator. */
0118 enum asn1_iterator_result
0119 asn1_der_iterator_first(struct asn1_der_iterator *iterator,
0120             size_t length, const uint8_t *input);
0121 
0122 enum asn1_iterator_result
0123 asn1_der_iterator_next(struct asn1_der_iterator *iterator);
0124 
0125 /* Starts parsing of a constructed object. */
0126 enum asn1_iterator_result
0127 asn1_der_decode_constructed(struct asn1_der_iterator *i,
0128                 struct asn1_der_iterator *contents);
0129 
0130 /* For the common case that we have a sequence at the end of the
0131    object. Checks that the current object is the final one, and then
0132    reinitializes the iterator to parse its ontents. */
0133 enum asn1_iterator_result
0134 asn1_der_decode_constructed_last(struct asn1_der_iterator *i);
0135 
0136 enum asn1_iterator_result
0137 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
0138               struct asn1_der_iterator *contents);
0139 
0140 enum asn1_iterator_result
0141 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i);
0142 
0143 /* All these functions return 1 on success, 0 on failure */
0144 int
0145 asn1_der_get_uint32(struct asn1_der_iterator *i,
0146             uint32_t *x);
0147 
0148 #ifdef __cplusplus
0149 }
0150 #endif
0151 
0152 #endif /* NETTLE_ASN1_H_INCLUDED */