]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/libDER/libDER/DER_Encode.h
bcde9757119e1714a679af50480183c02758ade9
[apple/security.git] / OSX / libsecurity_keychain / libDER / libDER / DER_Encode.h
1 /*
2 * Copyright (c) 2005-2016 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 #include <libDER/libDER.h>
34
35 __BEGIN_DECLS
36
37 /*
38 * Max size of an encoded item given its length.
39 * This includes a possible leading zero prepended to a signed integer
40 * (see DER_ENC_SIGNED_INT below).
41 */
42 #define DER_MAX_ENCODED_SIZE(len) \
43 ( 1 + /* tag */ \
44 5 + /* max length */ \
45 1 + /* possible prepended zero */ \
46 len)
47
48 /* calculate size of encoded length */
49 DERSize DERLengthOfLength(
50 DERSize length);
51
52 /* encode length */
53 DERReturn DEREncodeLength(
54 DERSize length,
55 DERByte *buf, /* encoded length goes here */
56 DERSize *inOutLen); /* IN/OUT */
57
58 /* calculate size of encoded length */
59 DERSize DERLengthOfItem(
60 DERTag tag,
61 DERSize length);
62
63 /* encode item */
64 DERReturn DEREncodeItem(
65 DERTag tag,
66 DERSize length,
67 const DERByte *src,
68 DERByte *derOut, /* encoded item goes here */
69 DERSize *inOutLen); /* IN/OUT */
70
71 /*
72 * Per-item encode options.
73 */
74
75 /* explicit default, no options */
76 #define DER_ENC_NO_OPTS 0x0000
77
78 /* signed integer check: if incoming m.s. bit is 1, prepend a zero */
79 #define DER_ENC_SIGNED_INT 0x0100
80
81 /* DERItem contains fully encoded item - copy, don't encode */
82 #define DER_ENC_WRITE_DER 0x0200
83
84
85 /*
86 * High-level sequence or set encode support.
87 *
88 * The outgoing sequence is expressed as an array of DERItemSpecs, each
89 * of which corresponds to one item in the encoded sequence.
90 *
91 * Normally the tag of the encoded item comes from the associated
92 * DERItemSpec, and the content comes from the DERItem whose address is
93 * the src arg plus the offset value in the associated DERItemSpec.
94 *
95 * If the DER_ENC_WRITE_DER option is true for a given DERItemSpec then
96 * no per-item encoding is done; the DER - with tag, length, and content -
97 * is taken en masse from the associated DERItem.
98 */
99 DERReturn DEREncodeSequence(
100 DERTag topTag, /* ASN1_CONSTR_SEQUENCE, ASN1_CONSTR_SET */
101 const void *src, /* generally a ptr to a struct full of
102 * DERItems */
103 DERShort numItems, /* size of itemSpecs[] */
104 const DERItemSpec *itemSpecs,
105 DERByte *derOut, /* encoded data written here */
106 DERSize *inOutLen); /* IN/OUT */
107
108 /* precalculate the length of an encoded sequence. */
109 DERSize DERLengthOfEncodedSequence(
110 DERTag topTag, /* ASN1_CONSTR_SEQUENCE, ASN1_CONSTR_SET */
111 const void *src, /* generally a ptr to a struct full of
112 * DERItems */
113 DERShort numItems, /* size of itemSpecs[] */
114 const DERItemSpec *itemSpecs);
115
116
117 __END_DECLS
118
119 #endif /* _DER_ENCODE_H_ */