5 // Created by Mitch Adler on 7/2/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
11 #include "utilities/SecCFRelease.h"
12 #include "utilities/der_plist.h"
13 #include "utilities/der_plist_internal.h"
15 #include <corecrypto/ccder.h>
16 #include <CoreFoundation/CoreFoundation.h>
19 // der..CFPropertyList
35 const uint8_t* der_decode_plist(CFAllocatorRef allocator
, CFOptionFlags mutability
,
36 CFPropertyListRef
* pl
, CFErrorRef
*error
,
37 const uint8_t* der
, const uint8_t *der_end
)
42 if (NULL
== ccder_decode_tag(&tag
, der
, der_end
))
47 return der_decode_null(allocator
, mutability
, (CFNullRef
*)pl
, error
, der
, der_end
);
49 return der_decode_boolean(allocator
, mutability
, (CFBooleanRef
*)pl
, error
, der
, der_end
);
50 case CCDER_OCTET_STRING
:
51 return der_decode_data(allocator
, mutability
, (CFDataRef
*)pl
, error
, der
, der_end
);
52 case CCDER_GENERALIZED_TIME
:
53 return der_decode_date(allocator
, mutability
, (CFDateRef
*)pl
, error
, der
, der_end
);
54 case CCDER_CONSTRUCTED_SEQUENCE
:
55 return der_decode_array(allocator
, mutability
, (CFArrayRef
*)pl
, error
, der
, der_end
);
56 case CCDER_UTF8_STRING
:
57 return der_decode_string(allocator
, mutability
, (CFStringRef
*)pl
, error
, der
, der_end
);
59 return der_decode_number(allocator
, mutability
, (CFNumberRef
*)pl
, error
, der
, der_end
);
60 case CCDER_CONSTRUCTED_SET
:
61 return der_decode_dictionary(allocator
, mutability
, (CFDictionaryRef
*)pl
, error
, der
, der_end
);
63 SecCFDERCreateError(kSecDERErrorUnsupportedDERType
, CFSTR("Unsupported DER Type"), NULL
, error
);
69 size_t der_sizeof_plist(CFPropertyListRef pl
, CFErrorRef
*error
)
72 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
76 CFTypeID dataType
= CFGetTypeID(pl
);
78 if (CFArrayGetTypeID() == dataType
)
79 return der_sizeof_array((CFArrayRef
) pl
, error
);
80 else if (CFBooleanGetTypeID() == dataType
)
81 return der_sizeof_boolean((CFBooleanRef
) pl
, error
);
82 else if (CFDataGetTypeID() == dataType
)
83 return der_sizeof_data((CFDataRef
) pl
, error
);
84 else if (CFDateGetTypeID() == dataType
)
85 return der_sizeof_date((CFDateRef
) pl
, error
);
86 else if (CFDictionaryGetTypeID() == dataType
)
87 return der_sizeof_dictionary((CFDictionaryRef
) pl
, error
);
88 else if (CFStringGetTypeID() == dataType
)
89 return der_sizeof_string((CFStringRef
) pl
, error
);
90 else if (CFNumberGetTypeID() == dataType
)
91 return der_sizeof_number((CFNumberRef
) pl
, error
);
92 if (CFNullGetTypeID() == dataType
)
93 return der_sizeof_null((CFNullRef
) pl
, error
);
95 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);
101 uint8_t* der_encode_plist(CFPropertyListRef pl
, CFErrorRef
*error
,
102 const uint8_t *der
, uint8_t *der_end
)
105 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Null CFType"), NULL
, error
);
109 CFTypeID dataType
= CFGetTypeID(pl
);
111 if (CFArrayGetTypeID() == dataType
)
112 return der_encode_array((CFArrayRef
) pl
, error
, der
, der_end
);
113 else if (CFBooleanGetTypeID() == dataType
)
114 return der_encode_boolean((CFBooleanRef
) pl
, error
, der
, der_end
);
115 else if (CFDataGetTypeID() == dataType
)
116 return der_encode_data((CFDataRef
) pl
, error
, der
, der_end
);
117 else if (CFDateGetTypeID() == dataType
)
118 return der_encode_date((CFDateRef
) pl
, error
, der
, der_end
);
119 else if (CFDictionaryGetTypeID() == dataType
)
120 return der_encode_dictionary((CFDictionaryRef
) pl
, error
, der
, der_end
);
121 else if (CFStringGetTypeID() == dataType
)
122 return der_encode_string((CFStringRef
) pl
, error
, der
, der_end
);
123 else if (CFNumberGetTypeID() == dataType
)
124 return der_encode_number((CFNumberRef
) pl
, error
, der
, der_end
);
125 else if (CFNullGetTypeID() == dataType
)
126 return der_encode_null((CFNullRef
) pl
, error
, der
, der_end
);
128 SecCFDERCreateError(kSecDERErrorUnsupportedCFObject
, CFSTR("Unsupported CFType"), NULL
, error
);