1 /*
2  * asn1.h: ASN.1 header file
3  *
4  * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef _OPENSC_ASN1_H
22 #define _OPENSC_ASN1_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "libopensc/opensc.h"
29 #include "libopensc/pkcs15.h"
30 
31 struct sc_asn1_entry {
32 	const char *name;
33 	unsigned int type;
34 	unsigned int tag;
35 	unsigned int flags;
36 	void *parm;
37 	void *arg;
38 };
39 
40 struct sc_asn1_pkcs15_object {
41 	struct sc_pkcs15_object *p15_obj;
42 	struct sc_asn1_entry *asn1_class_attr;
43 	struct sc_asn1_entry *asn1_subclass_attr;
44 	struct sc_asn1_entry *asn1_type_attr;
45 };
46 
47 struct sc_asn1_pkcs15_algorithm_info {
48 	int id;
49 	struct sc_object_id oid;
50 	int (*decode)(struct sc_context *, void **, const u8 *, size_t, int);
51 	int (*encode)(struct sc_context *, void *, u8 **, size_t *, int);
52 	void (*free)(void *);
53 };
54 
55 
56 /* Utility functions */
57 void sc_format_asn1_entry(struct sc_asn1_entry *entry, void *parm, void *arg,
58 			  int set_present);
59 void sc_copy_asn1_entry(const struct sc_asn1_entry *src,
60 			struct sc_asn1_entry *dest);
61 
62 /* DER tag and length parsing */
63 int sc_asn1_decode(struct sc_context *ctx, struct sc_asn1_entry *asn1,
64 		   const u8 *in, size_t len, const u8 **newp, size_t *left);
65 int sc_asn1_decode_choice(struct sc_context *ctx, struct sc_asn1_entry *asn1,
66 		   const u8 *in, size_t len, const u8 **newp, size_t *left);
67 int sc_asn1_encode(struct sc_context *ctx, const struct sc_asn1_entry *asn1,
68 		   u8 **buf, size_t *bufsize);
69 int _sc_asn1_decode(struct sc_context *, struct sc_asn1_entry *,
70 		   const u8 *, size_t, const u8 **, size_t *,
71 		   int, int);
72 int _sc_asn1_encode(struct sc_context *, const struct sc_asn1_entry *,
73 		   u8 **, size_t *, int);
74 
75 int sc_asn1_read_tag(const u8 ** buf, size_t buflen, unsigned int *cla_out,
76 		     unsigned int *tag_out, size_t *taglen);
77 const u8 *sc_asn1_find_tag(struct sc_context *ctx, const u8 * buf,
78 			   size_t buflen, unsigned int tag, size_t *taglen);
79 const u8 *sc_asn1_verify_tag(struct sc_context *ctx, const u8 * buf,
80 			     size_t buflen, unsigned int tag, size_t *taglen);
81 const u8 *sc_asn1_skip_tag(struct sc_context *ctx, const u8 ** buf,
82 			   size_t *buflen, unsigned int tag, size_t *taglen);
83 
84 /* DER encoding */
85 
86 /* Argument 'ptr' is set to the location of the next possible ASN.1 object.
87  * If NULL, no action on 'ptr' is performed.
88  * If out is NULL or outlen is zero, the length that would be written is returned.
89  * If data is NULL, the data field will not be written. This is helpful for constructed structures. */
90 int sc_asn1_put_tag(unsigned int tag, const u8 * data, size_t datalen, u8 * out, size_t outlen, u8 ** ptr);
91 
92 /* ASN.1 printing functions */
93 void sc_asn1_print_tags(const u8 * buf, size_t buflen);
94 
95 /* ASN.1 object decoding functions */
96 int sc_asn1_utf8string_to_ascii(const u8 * buf, size_t buflen,
97 				u8 * outbuf, size_t outlen);
98 int sc_asn1_decode_bit_string(const u8 * inbuf, size_t inlen,
99 			      void *outbuf, size_t outlen, const int strict);
100 /* non-inverting version */
101 int sc_asn1_decode_bit_string_ni(const u8 * inbuf, size_t inlen,
102 				 void *outbuf, size_t outlen, const int strict);
103 int sc_asn1_decode_integer(const u8 * inbuf, size_t inlen, int *out, int strict);
104 int sc_asn1_decode_object_id(const u8 * inbuf, size_t inlen,
105 			     struct sc_object_id *id);
106 int sc_asn1_encode_object_id(u8 **buf, size_t *buflen,
107 				const struct sc_object_id *id);
108 
109 /* algorithm encoding/decoding */
110 int sc_asn1_decode_algorithm_id(struct sc_context *,
111 				const u8 *, size_t,
112 				struct sc_algorithm_id *, int);
113 int sc_asn1_encode_algorithm_id(struct sc_context *,
114 				u8 **, size_t *,
115 				const struct sc_algorithm_id *, int);
116 void sc_asn1_clear_algorithm_id(struct sc_algorithm_id *);
117 
118 
119 /* ASN.1 object encoding functions */
120 int sc_asn1_write_element(sc_context_t *ctx, unsigned int tag,
121 		const u8 * data, size_t datalen, u8 ** out, size_t * outlen);
122 
123 int sc_asn1_sig_value_rs_to_sequence(struct sc_context *ctx,
124 		unsigned char *in, size_t inlen,
125                 unsigned char **buf, size_t *buflen);
126 int sc_asn1_sig_value_sequence_to_rs(struct sc_context *ctx,
127 		const unsigned char *in, size_t inlen,
128                 unsigned char *buf, size_t buflen);
129 
130 /* long form tags use these */
131 /* Same as  SC_ASN1_TAG_* shifted left by 24 bits  */
132 #define SC_ASN1_CLASS_MASK		0xC0000000
133 #define SC_ASN1_UNI			0x00000000 /* Universal */
134 #define SC_ASN1_APP			0x40000000 /* Application */
135 #define SC_ASN1_CTX			0x80000000 /* Context */
136 #define SC_ASN1_PRV			0xC0000000 /* Private */
137 #define SC_ASN1_CONS			0x20000000
138 
139 #define SC_ASN1_CLASS_CONS		0xE0000000 /* CLASS and CONS */
140 #define SC_ASN1_TAG_MASK		0x00FFFFFF
141 #define SC_ASN1_TAGNUM_SIZE		3
142 
143 #define SC_ASN1_PRESENT			0x00000001
144 #define SC_ASN1_OPTIONAL		0x00000002
145 #define SC_ASN1_ALLOC			0x00000004
146 #define SC_ASN1_UNSIGNED		0x00000008
147 #define SC_ASN1_EMPTY_ALLOWED           0x00000010
148 
149 #define SC_ASN1_BOOLEAN                 1
150 #define SC_ASN1_INTEGER                 2
151 #define SC_ASN1_BIT_STRING              3
152 #define SC_ASN1_BIT_STRING_NI           128
153 #define SC_ASN1_OCTET_STRING            4
154 #define SC_ASN1_NULL                    5
155 #define SC_ASN1_OBJECT                  6
156 #define SC_ASN1_ENUMERATED              10
157 #define SC_ASN1_UTF8STRING              12
158 #define SC_ASN1_SEQUENCE                16
159 #define SC_ASN1_SET                     17
160 #define SC_ASN1_PRINTABLESTRING         19
161 #define SC_ASN1_UTCTIME                 23
162 #define SC_ASN1_GENERALIZEDTIME         24
163 
164 /* internal structures */
165 #define SC_ASN1_STRUCT			129
166 #define SC_ASN1_CHOICE			130
167 #define SC_ASN1_BIT_FIELD		131	/* bit string as integer */
168 
169 /* 'complex' structures */
170 #define SC_ASN1_PATH			256
171 #define SC_ASN1_PKCS15_ID		257
172 #define SC_ASN1_PKCS15_OBJECT		258
173 #define SC_ASN1_ALGORITHM_ID		259
174 #define SC_ASN1_SE_INFO			260
175 
176 /* use callback function */
177 #define SC_ASN1_CALLBACK		384
178 
179 /* use with short one byte tags */
180 #define SC_ASN1_TAG_CLASS		0xC0
181 #define SC_ASN1_TAG_UNIVERSAL		0x00
182 #define SC_ASN1_TAG_APPLICATION		0x40
183 #define SC_ASN1_TAG_CONTEXT		0x80
184 #define SC_ASN1_TAG_PRIVATE		0xC0
185 
186 #define SC_ASN1_TAG_CONSTRUCTED		0x20
187 #define SC_ASN1_TAG_PRIMITIVE		0x1F
188 #define SC_ASN1_TAG_CLASS_CONS		0xE0
189 
190 #define SC_ASN1_TAG_EOC			0
191 #define SC_ASN1_TAG_BOOLEAN		1
192 #define SC_ASN1_TAG_INTEGER		2
193 #define SC_ASN1_TAG_BIT_STRING		3
194 #define SC_ASN1_TAG_OCTET_STRING	4
195 #define SC_ASN1_TAG_NULL		5
196 #define SC_ASN1_TAG_OBJECT		6
197 #define SC_ASN1_TAG_OBJECT_DESCRIPTOR	7
198 #define SC_ASN1_TAG_EXTERNAL		8
199 #define SC_ASN1_TAG_REAL		9
200 #define SC_ASN1_TAG_ENUMERATED		10
201 #define SC_ASN1_TAG_UTF8STRING		12
202 #define SC_ASN1_TAG_SEQUENCE		16
203 #define SC_ASN1_TAG_SET			17
204 #define SC_ASN1_TAG_NUMERICSTRING	18
205 #define SC_ASN1_TAG_PRINTABLESTRING	19
206 #define SC_ASN1_TAG_T61STRING		20
207 #define SC_ASN1_TAG_TELETEXSTRING	20
208 #define SC_ASN1_TAG_VIDEOTEXSTRING	21
209 #define SC_ASN1_TAG_IA5STRING		22
210 #define SC_ASN1_TAG_UTCTIME		23
211 #define SC_ASN1_TAG_GENERALIZEDTIME	24
212 #define SC_ASN1_TAG_GRAPHICSTRING	25
213 #define SC_ASN1_TAG_ISO64STRING		26
214 #define SC_ASN1_TAG_VISIBLESTRING	26
215 #define SC_ASN1_TAG_GENERALSTRING	27
216 #define SC_ASN1_TAG_UNIVERSALSTRING	28
217 #define SC_ASN1_TAG_BMPSTRING		30
218 #define SC_ASN1_TAG_ESCAPE_MARKER	31
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif
225