2 * Copyright (c) 2003-2006,2008-2013 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 * SecAsn1Coder.h: ANS1 encode/decode object.
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.
32 #ifndef _SEC_ASN1_CODER_H_
33 #define _SEC_ASN1_CODER_H_
35 #include <sys/types.h>
36 #include <Security/SecAsn1Types.h>
37 #include <TargetConditionals.h>
38 #include <Security/SecBase.h> /* error codes */
44 CF_ASSUME_NONNULL_BEGIN
47 * Opaque reference to a SecAsn1Coder object.
49 typedef struct SecAsn1Coder
*SecAsn1CoderRef
;
52 * Create/destroy SecAsn1Coder object.
54 OSStatus
SecAsn1CoderCreate(
55 SecAsn1CoderRef __nullable
* __nonnull coder
);
57 OSStatus
SecAsn1CoderRelease(
58 SecAsn1CoderRef coder
);
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.
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.
69 * The dest pointer is a template-specific struct allocated by the caller
70 * and must be zeroed by the caller.
72 * Returns errSecUnknownFormat on decode-specific error.
74 OSStatus
SecAsn1Decode(
75 SecAsn1CoderRef coder
,
76 const void *src
, // DER-encoded source
78 const SecAsn1Template
*templates
,
82 * Convenience routine, decode from a SecAsn1Item.
84 OSStatus
SecAsn1DecodeData(
85 SecAsn1CoderRef coder
,
86 const SecAsn1Item
*src
,
87 const SecAsn1Template
*templ
,
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.
94 * The src pointer is a template-specific struct.
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.
100 OSStatus
SecAsn1EncodeItem(
101 SecAsn1CoderRef coder
,
103 const SecAsn1Template
*templates
,
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.
113 * All except SecAsn1Malloc return a errSecAllocate in the highly
114 * unlikely event of a malloc failure.
116 * SecAsn1Malloc() returns a pointer to allocated memory, like
120 SecAsn1CoderRef coder
,
123 /* Allocate item.Data, set item.Length */
124 OSStatus
SecAsn1AllocItem(
125 SecAsn1CoderRef coder
,
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 */
137 OSStatus
SecAsn1AllocCopyItem(
138 SecAsn1CoderRef coder
,
139 const SecAsn1Item
*src
, /* src->Length bytes allocated and copied from
141 SecAsn1Item
*dest
); /* dest->Data allocated and copied to;
142 * dest->Length := src->Length */
144 /* Compare two decoded OIDs. Returns true iff they are equivalent. */
145 bool SecAsn1OidCompare(const SecAsn1Oid
*oid1
, const SecAsn1Oid
*oid2
);
147 CF_ASSUME_NONNULL_END
153 #endif /* _SEC_ASN1_CODER_H_ */