1 /*
2  * ASN.1 DER parsing
3  * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdint.h>
18 
19 #ifndef ASN1_H
20 #define ASN1_H
21 
22 #define ASN1_TAG_EOC		0x00 /* not used with DER */
23 #define ASN1_TAG_BOOLEAN	0x01
24 #define ASN1_TAG_INTEGER	0x02
25 #define ASN1_TAG_BITSTRING	0x03
26 #define ASN1_TAG_OCTETSTRING	0x04
27 #define ASN1_TAG_NULL		0x05
28 #define ASN1_TAG_OID		0x06
29 #define ASN1_TAG_OBJECT_DESCRIPTOR	0x07 /* not yet parsed */
30 #define ASN1_TAG_EXTERNAL	0x08 /* not yet parsed */
31 #define ASN1_TAG_REAL		0x09 /* not yet parsed */
32 #define ASN1_TAG_ENUMERATED	0x0A /* not yet parsed */
33 #define ASN1_TAG_UTF8STRING	0x0C /* not yet parsed */
34 #define ANS1_TAG_RELATIVE_OID	0x0D
35 #define ASN1_TAG_SEQUENCE	0x10 /* shall be constructed */
36 #define ASN1_TAG_SET		0x11
37 #define ASN1_TAG_NUMERICSTRING	0x12 /* not yet parsed */
38 #define ASN1_TAG_PRINTABLESTRING	0x13
39 #define ASN1_TAG_TG1STRING	0x14 /* not yet parsed */
40 #define ASN1_TAG_VIDEOTEXSTRING	0x15 /* not yet parsed */
41 #define ASN1_TAG_IA5STRING	0x16
42 #define ASN1_TAG_UTCTIME	0x17
43 #define ASN1_TAG_GENERALIZEDTIME	0x18 /* not yet parsed */
44 #define ASN1_TAG_GRAPHICSTRING	0x19 /* not yet parsed */
45 #define ASN1_TAG_VISIBLESTRING	0x1A
46 #define ASN1_TAG_GENERALSTRING	0x1B /* not yet parsed */
47 #define ASN1_TAG_UNIVERSALSTRING	0x1C /* not yet parsed */
48 #define ASN1_TAG_BMPSTRING	0x1D /* not yet parsed */
49 
50 #define ASN1_CLASS_UNIVERSAL		0
51 #define ASN1_CLASS_APPLICATION		1
52 #define ASN1_CLASS_CONTEXT_SPECIFIC	2
53 #define ASN1_CLASS_PRIVATE		3
54 
55 
56 struct asn1_hdr {
57     const uint8_t *payload;
58     uint8_t identifier, class, constructed;
59     unsigned int tag, length;
60 };
61 
62 #define ASN1_MAX_OID_LEN 20
63 struct asn1_oid {
64     unsigned long oid[ASN1_MAX_OID_LEN];
65     size_t len;
66 };
67 
68 
69 int asn1_get_next(const uint8_t *buf, size_t len, struct asn1_hdr *hdr);
70 int asn1_parse_oid(const uint8_t *buf, size_t len, struct asn1_oid *oid);
71 int asn1_get_oid(const uint8_t *buf, size_t len, struct asn1_oid *oid,
72                  const uint8_t **next);
73 void asn1_oid_to_str(struct asn1_oid *oid, char *buf, size_t len);
74 unsigned long asn1_bit_string_to_long(const uint8_t *buf, size_t len);
75 
76 #endif /* ASN1_H */
77