]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/der_plist.c
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / utilities / der_plist.c
1 /*
2 * Copyright (c) 2012-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 #include <stdio.h>
26
27 #include "utilities/SecCFRelease.h"
28 #include "utilities/der_plist.h"
29 #include "utilities/der_plist_internal.h"
30
31 #include <corecrypto/ccder.h>
32 #include <CoreFoundation/CoreFoundation.h>
33
34 #include "utilities/simulatecrash_assert.h"
35
36 //
37 // der..CFPropertyList
38 //
39 // We support:
40 //
41 // CFBoolean
42 // CFData
43 // CFDate
44 // CFString
45 // CFNumber
46 // CFNull
47 //
48 // CFArray
49 // CFDictionary
50 //
51 //
52
53 const uint8_t* der_decode_plist(CFAllocatorRef allocator,
54 CFPropertyListRef* pl, CFErrorRef *error,
55 const uint8_t* der, const uint8_t *der_end)
56 {
57 if (NULL == der) {
58 SecCFDERCreateError(kSecDERErrorNullInput, CFSTR("null input"), NULL, error);
59 return NULL;
60 }
61
62 ccder_tag tag;
63 if (NULL == ccder_decode_tag(&tag, der, der_end)) {
64 SecCFDERCreateError(kSecDERErrorUnknownEncoding, CFSTR("invalid tag"), NULL, error);
65 return NULL;
66 }
67
68 switch (tag) {
69 case CCDER_NULL:
70 return der_decode_null(allocator, (CFNullRef*)pl, error, der, der_end);
71 case CCDER_BOOLEAN:
72 return der_decode_boolean(allocator, (CFBooleanRef*)pl, error, der, der_end);
73 case CCDER_OCTET_STRING:
74 return der_decode_data(allocator, (CFDataRef*)pl, error, der, der_end);
75 case CCDER_GENERALIZED_TIME:
76 return der_decode_date(allocator, (CFDateRef*)pl, error, der, der_end);
77 case CCDER_CONSTRUCTED_SEQUENCE:
78 return der_decode_array(allocator, (CFArrayRef*)pl, error, der, der_end);
79 case CCDER_UTF8_STRING:
80 return der_decode_string(allocator, (CFStringRef*)pl, error, der, der_end);
81 case CCDER_INTEGER:
82 return der_decode_number(allocator, (CFNumberRef*)pl, error, der, der_end);
83 case CCDER_CONSTRUCTED_SET:
84 return der_decode_dictionary(allocator, (CFDictionaryRef*)pl, error, der, der_end);
85 case CCDER_CONSTRUCTED_CFSET:
86 return der_decode_set(allocator, (CFSetRef*)pl, error, der, der_end);
87 default:
88 SecCFDERCreateError(kSecDERErrorUnsupportedDERType, CFSTR("Unsupported DER Type"), NULL, error);
89 return NULL;
90 }
91 }
92
93
94 size_t der_sizeof_plist(CFPropertyListRef pl, CFErrorRef *error)
95 {
96 if (!pl) {
97 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject, CFSTR("Null CFType"), NULL, error);
98 return 0;
99 }
100
101 CFTypeID dataType = CFGetTypeID(pl);
102
103 if (CFArrayGetTypeID() == dataType)
104 return der_sizeof_array((CFArrayRef) pl, error);
105 else if (CFBooleanGetTypeID() == dataType)
106 return der_sizeof_boolean((CFBooleanRef) pl, error);
107 else if (CFDataGetTypeID() == dataType)
108 return der_sizeof_data((CFDataRef) pl, error);
109 else if (CFDateGetTypeID() == dataType)
110 return der_sizeof_date((CFDateRef) pl, error);
111 else if (CFDictionaryGetTypeID() == dataType)
112 return der_sizeof_dictionary((CFDictionaryRef) pl, error);
113 else if (CFSetGetTypeID() == dataType)
114 return der_sizeof_set((CFSetRef) pl, error);
115 else if (CFStringGetTypeID() == dataType)
116 return der_sizeof_string((CFStringRef) pl, error);
117 else if (CFNumberGetTypeID() == dataType)
118 return der_sizeof_number((CFNumberRef) pl, error);
119 else if (CFNullGetTypeID() == dataType)
120 return der_sizeof_null((CFNullRef) pl, error);
121 else {
122 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject, CFSTR("Unsupported CFType"), NULL, error);
123 return 0;
124 }
125 }
126
127 uint8_t* der_encode_plist(CFPropertyListRef pl, CFErrorRef *error,
128 const uint8_t *der, uint8_t *der_end)
129 {
130 return der_encode_plist_repair(pl, error, false, der, der_end);
131 }
132
133 uint8_t* der_encode_plist_repair(CFPropertyListRef pl, CFErrorRef *error,
134 bool repair, const uint8_t *der, uint8_t *der_end)
135 {
136 if (!pl) {
137 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject, CFSTR("Null CFType"), NULL, error);
138 return NULL;
139 }
140
141 CFTypeID dataType = CFGetTypeID(pl);
142
143 if (CFArrayGetTypeID() == dataType)
144 return der_encode_array((CFArrayRef) pl, error, der, der_end);
145 else if (CFBooleanGetTypeID() == dataType)
146 return der_encode_boolean((CFBooleanRef) pl, error, der, der_end);
147 else if (CFDataGetTypeID() == dataType)
148 return der_encode_data((CFDataRef) pl, error, der, der_end);
149 else if (CFDateGetTypeID() == dataType)
150 return der_encode_date_repair((CFDateRef) pl, error, repair, der, der_end);
151 else if (CFDictionaryGetTypeID() == dataType)
152 return der_encode_dictionary((CFDictionaryRef) pl, error, der, der_end);
153 else if (CFSetGetTypeID() == dataType)
154 return der_encode_set((CFSetRef) pl, error, der, der_end);
155 else if (CFStringGetTypeID() == dataType)
156 return der_encode_string((CFStringRef) pl, error, der, der_end);
157 else if (CFNumberGetTypeID() == dataType)
158 return der_encode_number((CFNumberRef) pl, error, der, der_end);
159 else if (CFNullGetTypeID() == dataType)
160 return der_encode_null((CFNullRef) pl, error, der, der_end);
161 else {
162 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject, CFSTR("Unsupported CFType"), NULL, error);
163 return NULL;
164 }
165 }
166
167 // Similar to CFPropertyListCreateData
168
169 CFDataRef CFPropertyListCreateDERData(CFAllocatorRef allocator, CFPropertyListRef plist, CFErrorRef *error) {
170 size_t len = der_sizeof_plist(plist, error);
171 CFMutableDataRef encoded = CFDataCreateMutable(0, len);
172 CFDataSetLength(encoded, len);
173 uint8_t *der_end = CFDataGetMutableBytePtr(encoded);
174 const uint8_t *der = der_end;
175 der_end += len;
176 der_end = der_encode_plist(plist, error, der, der_end);
177 if (!der_end) {
178 CFReleaseNull(encoded);
179 } else {
180 assert(!der_end || der_end == der);
181 }
182 return encoded;
183 }
184
185 CFPropertyListRef CFPropertyListCreateWithDERData(CFAllocatorRef allocator, CFDataRef data, CFOptionFlags options, CFPropertyListFormat *format, CFErrorRef *error) {
186 CFPropertyListRef plist = NULL;
187 const uint8_t *der = CFDataGetBytePtr(data);
188 const uint8_t *der_end = der + CFDataGetLength(data);
189 der = der_decode_plist(0, &plist, error, der, der_end);
190 if (der && der != der_end) {
191 SecCFDERCreateError(kSecDERErrorUnknownEncoding, CFSTR("trailing garbage after plist item"), NULL, error);
192 CFReleaseNull(plist);
193 } else if (format) {
194 *format = kCFPropertyListDERFormat_v1_0;
195 }
196 return plist;
197 }