]> git.saurik.com Git - apple/security.git/blob - libsecurity_asn1/lib/SecAsn1Coder.c
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_asn1 / lib / SecAsn1Coder.c
1 /*
2 * Copyright (c) 2003-2006,2008-2010 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, ANSI C version.
24 */
25
26 #include "SecAsn1Coder.h"
27 #include "plarenas.h"
28 #include "prerror.h"
29 #include "seccomon.h"
30 #include "secasn1.h"
31 #include <MacErrors.h>
32
33 /*
34 * Default chunk size for new arena pool.
35 * FIXME: analyze & measure different defaults here. I'm pretty sure
36 * that only performance - not correct behavior - is affected by
37 * an arena pool's chunk size.
38 */
39 #define CHUNKSIZE_DEF 1024
40
41 /*
42 * Caller's SecAsn1CoderRef points to one of these.
43 */
44 typedef struct SecAsn1Coder {
45 PLArenaPool *mPool;
46 } SecAsn1Coder_t;
47
48 /*
49 * Create/destroy SecAsn1Coder object.
50 */
51 OSStatus SecAsn1CoderCreate(
52 SecAsn1CoderRef *coder)
53 {
54 if(coder == NULL) {
55 return paramErr;
56 }
57 SecAsn1CoderRef _coder = (SecAsn1CoderRef)malloc(sizeof(SecAsn1Coder_t));
58 _coder->mPool = PORT_NewArena(CHUNKSIZE_DEF);
59 if(_coder->mPool == NULL) {
60 return memFullErr;
61 }
62 *coder = _coder;
63 return noErr;
64 }
65
66 OSStatus SecAsn1CoderRelease(
67 SecAsn1CoderRef coder)
68 {
69 if(coder == NULL) {
70 return paramErr;
71 }
72 if(coder->mPool != NULL) {
73 /*
74 * Note: we're asking for a memory zero here, but
75 * PORT_FreeArena doesn't do that (yet).
76 */
77 PORT_FreeArena(coder->mPool, PR_TRUE);
78 coder->mPool = NULL;
79 }
80 free(coder);
81 return noErr;
82 }
83
84 /*
85 * DER decode an untyped item per the specified template array.
86 * The result is allocated in this SecAsn1Coder's memory pool and
87 * is freed when this object is released.
88 *
89 * The dest pointer is a template-specific struct allocated by the caller
90 * and must be zeroed by the caller.
91 */
92 OSStatus SecAsn1Decode(
93 SecAsn1CoderRef coder,
94 const void *src, // DER-encoded source
95 size_t len,
96 const SecAsn1Template *templ,
97 void *dest)
98 {
99 if((coder == NULL) || (src == NULL) || (templ == NULL) || (dest == NULL)) {
100 return paramErr;
101 }
102 SECStatus prtn = SEC_ASN1Decode(coder->mPool, dest, templ, (const char *)src, len);
103 if(prtn) {
104 return errSecDecode;
105 }
106 else {
107 return noErr;
108 }
109 }
110
111 /*
112 * Convenience routine, decode from a SecAsn1Item.
113 */
114 OSStatus SecAsn1DecodeData(
115 SecAsn1CoderRef coder,
116 const SecAsn1Item *src,
117 const SecAsn1Template *templ,
118 void *dest)
119 {
120 return SecAsn1Decode(coder, src->Data, src->Length, templ, dest);
121 }
122
123 /*
124 * DER encode. The encoded data (in dest.Data) is allocated in this
125 * SecAsn1Coder's memory pool and is freed when this object is released.
126 *
127 * The src pointer is a template-specific struct.
128 */
129 OSStatus SecAsn1EncodeItem(
130 SecAsn1CoderRef coder,
131 const void *src,
132 const SecAsn1Template *templ,
133 SecAsn1Item *dest)
134 {
135 if((coder == NULL) || (src == NULL) || (templ == NULL) || (dest == NULL)) {
136 return paramErr;
137 }
138 dest->Data = NULL;
139 dest->Length = 0;
140
141 SecAsn1Item *rtnItem = SEC_ASN1EncodeItem(coder->mPool, dest, src, templ);
142 if(rtnItem == NULL) {
143 /* FIXME what to return here? */
144 return paramErr;
145 }
146 else {
147 return noErr;
148 }
149 }
150
151 /*
152 * Some alloc-related methods which come in handy when using
153 * this object. All memory is allocated using this object's
154 * memory pool. Caller never has to free it. Used for
155 * temp allocs of memory which only needs a scope which is the
156 * same as this object.
157 *
158 * These return a memFullErr in the highly unlikely event of
159 * a malloc failure.
160 */
161 void *SecAsn1Malloc(
162 SecAsn1CoderRef coder,
163 size_t len)
164 {
165 if(coder == NULL) {
166 return NULL;
167 }
168 return PORT_ArenaAlloc(coder->mPool, len);
169 }
170
171 /* malloc item.Data, set item.Length */
172 OSStatus SecAsn1AllocItem(
173 SecAsn1CoderRef coder,
174 SecAsn1Item *item,
175 size_t len)
176 {
177 if((coder == NULL) || (item == NULL)) {
178 return paramErr;
179 }
180 item->Data = (uint8_t *)PORT_ArenaAlloc(coder->mPool, len);
181 if(item->Data == NULL) {
182 return memFullErr;
183 }
184 item->Length = len;
185 return noErr;
186 }
187
188 /* malloc and copy, various forms */
189 OSStatus SecAsn1AllocCopy(
190 SecAsn1CoderRef coder,
191 const void *src,
192 size_t len,
193 SecAsn1Item *dest)
194 {
195 if(src == NULL) {
196 return paramErr;
197 }
198 OSStatus ortn = SecAsn1AllocItem(coder, dest, len);
199 if(ortn) {
200 return ortn;
201 }
202 memmove(dest->Data, src, len);
203 return noErr;
204 }
205
206 OSStatus SecAsn1AllocCopyItem(
207 SecAsn1CoderRef coder,
208 const SecAsn1Item *src,
209 SecAsn1Item *dest)
210 {
211 return SecAsn1AllocCopy(coder, src->Data, src->Length, dest);
212 }
213
214 bool SecAsn1OidCompare(const SecAsn1Oid *oid1, const SecAsn1Oid *oid2) {
215 if (!oid1 || !oid2)
216 return oid1 == oid2;
217 if (oid1->Length != oid2->Length)
218 return false;
219 return !memcmp(oid1->Data, oid2->Data, oid1->Length);
220 }