1/*
2 * Copyright (c) 2005-2007,2011,2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25/*
26 * DER_Encode.h - DER encoding routines
27 *
28 */
29
30#ifndef	_DER_ENCODE_H_
31#define _DER_ENCODE_H_
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#include <libDER/libDER.h>
38
39/*
40 * Max size of an encoded item given its length.
41 * This includes a possible leading zero prepended to a signed integer
42 * (see DER_ENC_SIGNED_INT below).
43 */
44#define DER_MAX_ENCODED_SIZE(len)					\
45	( 1 +			/* tag */						\
46	  5 +			/* max length */				\
47	  1 +			/* possible prepended zero */	\
48	  len)
49
50/* calculate size of encoded length */
51DERSize DERLengthOfLength(
52	DERSize length);
53
54/* encode length */
55DERReturn DEREncodeLength(
56	DERSize length,
57	DERByte *buf,		/* encoded length goes here */
58	DERSize *inOutLen);	/* IN/OUT */
59
60/* calculate size of encoded length */
61DERSize DERLengthOfItem(
62	DERTag tag,
63	DERSize length);
64
65/* encode item */
66DERReturn DEREncodeItem(
67	DERTag tag,
68	DERSize length,
69    const DERByte *src,
70	DERByte *derOut,	/* encoded item goes here */
71	DERSize *inOutLen);	/* IN/OUT */
72
73/*
74 * Per-item encode options.
75 */
76
77/* explicit default, no options */
78#define DER_ENC_NO_OPTS			0x0000
79
80/* signed integer check: if incoming m.s. bit is 1, prepend a zero */
81#define DER_ENC_SIGNED_INT		0x0100
82
83/* DERItem contains fully encoded item - copy, don't encode */
84#define DER_ENC_WRITE_DER		0x0200
85
86
87/*
88 * High-level sequence or set encode support.
89 *
90 * The outgoing sequence is expressed as an array of DERItemSpecs, each
91 * of which corresponds to one item in the encoded sequence.
92 *
93 * Normally the tag of the encoded item comes from the associated
94 * DERItemSpec, and the content comes from the DERItem whose address is
95 * the src arg plus the offset value in the associated DERItemSpec.
96 *
97 * If the DER_ENC_WRITE_DER option is true for a given DERItemSpec then
98 * no per-item encoding is done; the DER - with tag, length, and content -
99 * is taken en masse from the associated DERItem.
100 */
101DERReturn DEREncodeSequence(
102	DERTag				topTag,		/* ASN1_CONSTR_SEQUENCE, ASN1_CONSTR_SET */
103	const void			*src,		/* generally a ptr to a struct full of
104									 *    DERItems */
105	DERShort			numItems,	/* size of itemSpecs[] */
106	const DERItemSpec	*itemSpecs,
107	DERByte				*derOut,	/* encoded data written here */
108	DERSize				*inOutLen);	/* IN/OUT */
109
110/* precalculate the length of an encoded sequence. */
111DERSize DERLengthOfEncodedSequence(
112	DERTag				topTag,		/* ASN1_CONSTR_SEQUENCE, ASN1_CONSTR_SET */
113	const void			*src,		/* generally a ptr to a struct full of
114									 *    DERItems */
115	DERShort			numItems,	/* size of itemSpecs[] */
116	const DERItemSpec	*itemSpecs);
117
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif	/* _DER_ENCODE_H_ */
124
125