2 * Copyright (c) 2012-2014 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@
27 #include "utilities/SecCFRelease.h"
28 #include "utilities/der_plist.h"
29 #include "utilities/der_plist_internal.h"
31 #include <corecrypto/ccder.h>
32 #include <CoreFoundation/CoreFoundation.h>
34 #include "utilities/simulatecrash_assert.h"
37 // der..CFPropertyList
53 const uint8_t* der_decode_plist(CFAllocatorRef allocator
,
54 CFPropertyListRef
* pl
, CFErrorRef
*error
,
55 const uint8_t* der
, const uint8_t *der_end
)
58 SecCFDERCreateError(kSecDERErrorNullInput
, CFSTR("null input"), NULL
, error
);
63 if (NULL
== ccder_decode_tag(&tag
, der
, der_end
)) {
64 SecCFDERCreateError(kSecDERErrorUnknownEncoding
, CFSTR("invalid tag"), NULL
, error
);
70 return der_decode_null(allocator
, (CFNullRef
*)pl
, error
, der
, der_end
);
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
);
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
);
88 SecCFDERCreateError(kSecDERErrorUnsupportedDERType
, CFSTR("Unsupported DER Type"), NULL
, error
);
94 size_t der_sizeof_plist(CFPropertyListRef pl
, CFErrorRef
*error
)
97 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
101 CFTypeID dataType
= CFGetTypeID(pl
);
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
);
122 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);
127 uint8_t* der_encode_plist(CFPropertyListRef pl
, CFErrorRef
*error
,
128 const uint8_t *der
, uint8_t *der_end
)
130 return der_encode_plist_repair(pl
, error
, false, der
, der_end
);
133 uint8_t* der_encode_plist_repair(CFPropertyListRef pl
, CFErrorRef
*error
,
134 bool repair
, const uint8_t *der
, uint8_t *der_end
)
137 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
141 CFTypeID dataType
= CFGetTypeID(pl
);
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
);
162 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);
167 // Similar to CFPropertyListCreateData
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
;
176 der_end
= der_encode_plist(plist
, error
, der
, der_end
);
178 CFReleaseNull(encoded
);
180 assert(!der_end
|| der_end
== der
);
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
);
194 *format
= kCFPropertyListDERFormat_v1_0
;