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