1/*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation.  Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above.  If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL.  If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34/*
35 * Support for encoding/decoding of ASN.1 using BER/DER (Basic/Distinguished
36 * Encoding Rules).  The routines are found in and used extensively by the
37 * security library, but exported for other use.
38 *
39 * $Id: secasn1.h,v 1.8 2004/05/13 15:29:13 dmitch Exp $
40 */
41
42#ifndef _SECASN1_H_
43#define _SECASN1_H_
44
45#include <security_asn1/plarenas.h>
46
47#include <security_asn1/seccomon.h>
48#include <Security/secasn1t.h>
49#include <Security/SecAsn1Templates.h>
50
51
52/************************************************************************/
53SEC_BEGIN_PROTOS
54
55/*
56 * XXX These function prototypes need full, explanatory comments.
57 */
58
59/*
60** Decoding.
61*/
62
63extern SEC_ASN1DecoderContext *SEC_ASN1DecoderStart(PRArenaPool *pool,
64						    void *dest,
65						    const SecAsn1Template *t,
66							/*
67							 * __APPLE__ addenda:
68							 *
69						     * Only needed if first element will
70							 * be SEC_ASN1_DYNAMIC
71							 */
72							const char *buf);
73
74/* XXX char or unsigned char? */
75extern SECStatus SEC_ASN1DecoderUpdate(SEC_ASN1DecoderContext *cx,
76				       const char *buf,
77				       size_t len);
78
79extern SECStatus SEC_ASN1DecoderFinish(SEC_ASN1DecoderContext *cx);
80
81/* Higher level code detected an error, abort the rest of the processing */
82extern void SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error);
83
84extern void SEC_ASN1DecoderSetFilterProc(SEC_ASN1DecoderContext *cx,
85					 SEC_ASN1WriteProc fn,
86					 void *arg, PRBool no_store);
87
88extern void SEC_ASN1DecoderClearFilterProc(SEC_ASN1DecoderContext *cx);
89
90extern void SEC_ASN1DecoderSetNotifyProc(SEC_ASN1DecoderContext *cx,
91					 SEC_ASN1NotifyProc fn,
92					 void *arg);
93
94extern void SEC_ASN1DecoderClearNotifyProc(SEC_ASN1DecoderContext *cx);
95
96extern SECStatus SEC_ASN1Decode(PRArenaPool *pool, void *dest,
97				const SecAsn1Template *t,
98				const char *buf, size_t len);
99
100extern SECStatus SEC_ASN1DecodeItem(PRArenaPool *pool, void *dest,
101				    const SecAsn1Template *t,
102				    const SecAsn1Item *item);
103
104extern SECStatus SEC_QuickDERDecodeItem(PRArenaPool* arena, void* dest,
105                     const SecAsn1Template* templateEntry,
106                     SecAsn1Item* src);
107
108/*
109** Encoding.
110*/
111
112extern SEC_ASN1EncoderContext *SEC_ASN1EncoderStart(const void *src,
113						    const SecAsn1Template *t,
114						    SEC_ASN1WriteProc fn,
115						    void *output_arg);
116
117/* XXX char or unsigned char? */
118extern SECStatus SEC_ASN1EncoderUpdate(SEC_ASN1EncoderContext *cx,
119				       const char *buf,
120				       unsigned long len);
121
122extern void SEC_ASN1EncoderFinish(SEC_ASN1EncoderContext *cx);
123
124/* Higher level code detected an error, abort the rest of the processing */
125extern void SEC_ASN1EncoderAbort(SEC_ASN1EncoderContext *cx, int error);
126
127extern void SEC_ASN1EncoderSetNotifyProc(SEC_ASN1EncoderContext *cx,
128					 SEC_ASN1NotifyProc fn,
129					 void *arg);
130
131extern void SEC_ASN1EncoderClearNotifyProc(SEC_ASN1EncoderContext *cx);
132
133extern void SEC_ASN1EncoderSetStreaming(SEC_ASN1EncoderContext *cx);
134
135extern void SEC_ASN1EncoderClearStreaming(SEC_ASN1EncoderContext *cx);
136
137extern void sec_ASN1EncoderSetDER(SEC_ASN1EncoderContext *cx);
138
139extern void sec_ASN1EncoderClearDER(SEC_ASN1EncoderContext *cx);
140
141extern void SEC_ASN1EncoderSetTakeFromBuf(SEC_ASN1EncoderContext *cx);
142
143extern void SEC_ASN1EncoderClearTakeFromBuf(SEC_ASN1EncoderContext *cx);
144
145extern SECStatus SEC_ASN1Encode(const void *src,
146				const SecAsn1Template *t,
147				SEC_ASN1WriteProc output_proc,
148				void *output_arg);
149
150extern SecAsn1Item * SEC_ASN1EncodeItem(PRArenaPool *pool, SecAsn1Item *dest,
151				    const void *src, const SecAsn1Template *t);
152
153extern SecAsn1Item * SEC_ASN1EncodeInteger(PRArenaPool *pool,
154				       SecAsn1Item *dest, long value);
155
156extern SecAsn1Item * SEC_ASN1EncodeUnsignedInteger(PRArenaPool *pool,
157					       SecAsn1Item *dest,
158					       unsigned long value);
159
160extern SECStatus SEC_ASN1DecodeInteger(SecAsn1Item *src,
161				       unsigned long *value);
162
163/*
164** Utilities.
165*/
166
167/*
168 * We have a length that needs to be encoded; how many bytes will the
169 * encoding take?
170 */
171extern int SEC_ASN1LengthLength (unsigned long len);
172
173/* encode the length and return the number of bytes we encoded. Buffer
174 * must be pre allocated  */
175extern int SEC_ASN1EncodeLength(unsigned char *buf,unsigned long value);
176
177/*
178 * Find the appropriate subtemplate for the given template.
179 * This may involve calling a "chooser" function, or it may just
180 * be right there.  In either case, it is expected to *have* a
181 * subtemplate; this is asserted in debug builds (in non-debug
182 * builds, NULL will be returned).
183 *
184 * "thing" is a pointer to the structure being encoded/decoded
185 * "encoding", when true, means that we are in the process of encoding
186 *	(as opposed to in the process of decoding)
187 */
188extern const SecAsn1Template *
189SEC_ASN1GetSubtemplate (
190	const SecAsn1Template *inTemplate,
191	void *thing,
192	PRBool encoding,
193	const char *buf);	/* __APPLE__ addenda: for decode only */
194
195extern SecAsn1Item *sec_asn1e_allocate_item (
196	PRArenaPool *poolp,
197	SecAsn1Item *dest,
198	unsigned long len);
199
200/*
201 * These two are exported for use by SecNssEncodeItem()
202 */
203extern void sec_asn1e_encode_item_count (
204	void *arg,
205	const char *buf,
206	size_t len,
207	int depth,
208	SEC_ASN1EncodingPart data_kind);
209
210extern void sec_asn1e_encode_item_store (
211	void *arg,
212	const char *buf,
213	size_t len,
214	int depth,
215	SEC_ASN1EncodingPart data_kind);
216
217
218SEC_END_PROTOS
219#endif /* _SECASN1_H_ */
220