]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/SecAsn1Coder.h
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / libsecurity_asn1 / lib / SecAsn1Coder.h
1 /*
2 * Copyright (c) 2003-2006,2008-2013 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 * SecAsn1Coder.h: ANS1 encode/decode object.
24 *
25 * A SecAsn1Coder is capable of encoding and decoding both DER and BER data
26 * streams, based on caller-supplied templates which in turn are based
27 * upon ASN.1 specifications. A SecAsn1Coder allocates memory during encode
28 * and decode using a memory pool which is owned and managed by the SecAsn1Coder
29 * object, and which is freed when the SecAsn1Coder object os released.
30 */
31
32 #ifndef _SEC_ASN1_CODER_H_
33 #define _SEC_ASN1_CODER_H_
34
35 #include <sys/types.h>
36 #include <Security/SecAsn1Types.h>
37 #include <TargetConditionals.h>
38 #include <Security/SecBase.h> /* error codes */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 CF_ASSUME_NONNULL_BEGIN
45
46 /*
47 * Opaque reference to a SecAsn1Coder object.
48 */
49 typedef struct SecAsn1Coder *SecAsn1CoderRef;
50
51 /*
52 * Create/destroy SecAsn1Coder object.
53 */
54 OSStatus SecAsn1CoderCreate(
55 SecAsn1CoderRef __nullable * __nonnull coder);
56
57 OSStatus SecAsn1CoderRelease(
58 SecAsn1CoderRef coder);
59
60 /*
61 * DER decode an untyped item per the specified template array.
62 * The result is allocated in this SecAsn1Coder's memory pool and
63 * is freed when this object is released.
64 *
65 * The templates argument points to a an array of SecAsn1Templates
66 * defining the object to be decoded; the end of the array is
67 * indicated by a SecAsn1Template with file kind equalling 0.
68 *
69 * The dest pointer is a template-specific struct allocated by the caller
70 * and must be zeroed by the caller.
71 *
72 * Returns errSecUnknownFormat on decode-specific error.
73 */
74 OSStatus SecAsn1Decode(
75 SecAsn1CoderRef coder,
76 const void *src, // DER-encoded source
77 size_t len,
78 const SecAsn1Template *templates,
79 void *dest);
80
81 /*
82 * Convenience routine, decode from a SecAsn1Item.
83 */
84 OSStatus SecAsn1DecodeData(
85 SecAsn1CoderRef coder,
86 const SecAsn1Item *src,
87 const SecAsn1Template *templ,
88 void *dest);
89
90 /*
91 * DER encode. The encoded data (in dest.Data) is allocated in this
92 * SecAsn1Coder's memory pool and is freed when this object is released.
93 *
94 * The src pointer is a template-specific struct.
95 *
96 * The templates argument points to a an array of SecAsn1Templates
97 * defining the object to be decoded; the end of the array is
98 * indicated by a SecAsn1Template with file kind equalling 0.
99 */
100 OSStatus SecAsn1EncodeItem(
101 SecAsn1CoderRef coder,
102 const void *src,
103 const SecAsn1Template *templates,
104 SecAsn1Item *dest);
105
106 /*
107 * Some alloc-related methods which come in handy when using
108 * this object. All memory is allocated using this object's
109 * memory pool. Caller never has to free it. Used for
110 * temp allocs of memory which only needs a scope which is the
111 * same as this object.
112 *
113 * All except SecAsn1Malloc return a errSecAllocate in the highly
114 * unlikely event of a malloc failure.
115 *
116 * SecAsn1Malloc() returns a pointer to allocated memory, like
117 * malloc().
118 */
119 void *SecAsn1Malloc(
120 SecAsn1CoderRef coder,
121 size_t len);
122
123 /* Allocate item.Data, set item.Length */
124 OSStatus SecAsn1AllocItem(
125 SecAsn1CoderRef coder,
126 SecAsn1Item *item,
127 size_t len);
128
129 /* Allocate and copy, various forms */
130 OSStatus SecAsn1AllocCopy(
131 SecAsn1CoderRef coder,
132 const void *src, /* memory copied from here */
133 size_t len, /* length to allocate & copy */
134 SecAsn1Item *dest); /* dest->Data allocated and copied to;
135 * dest->Length := len */
136
137 OSStatus SecAsn1AllocCopyItem(
138 SecAsn1CoderRef coder,
139 const SecAsn1Item *src, /* src->Length bytes allocated and copied from
140 * src->Data */
141 SecAsn1Item *dest); /* dest->Data allocated and copied to;
142 * dest->Length := src->Length */
143
144 /* Compare two decoded OIDs. Returns true iff they are equivalent. */
145 bool SecAsn1OidCompare(const SecAsn1Oid *oid1, const SecAsn1Oid *oid2);
146
147 CF_ASSUME_NONNULL_END
148
149 #ifdef __cplusplus
150 }
151 #endif
152
153 #endif /* _SEC_ASN1_CODER_H_ */