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>
35 // der..CFPropertyList
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
)
58 if (NULL
== ccder_decode_tag(&tag
, der
, der_end
))
63 return der_decode_null(allocator
, mutability
, (CFNullRef
*)pl
, error
, der
, der_end
);
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
);
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
);
81 SecCFDERCreateError(kSecDERErrorUnsupportedDERType
, CFSTR("Unsupported DER Type"), NULL
, error
);
87 size_t der_sizeof_plist(CFPropertyListRef pl
, CFErrorRef
*error
)
90 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
94 CFTypeID dataType
= CFGetTypeID(pl
);
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
);
115 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);
121 uint8_t* der_encode_plist(CFPropertyListRef pl
, CFErrorRef
*error
,
122 const uint8_t *der
, uint8_t *der_end
)
125 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
129 CFTypeID dataType
= CFGetTypeID(pl
);
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
);
150 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);
155 // Similar to CFPropertyListCreateData
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
;
164 der_end
= der_encode_plist(plist
, error
, der
, der_end
);
166 CFReleaseNull(encoded
);
168 assert(!der_end
|| der_end
== der
);
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
);
182 *format
= kCFPropertyListDERFormat_v1_0
;