]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/der_set.c
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / utilities / der_set.c
1 /*
2 * Copyright (c) 2015 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 #include "der_set.h"
27
28 #include "utilities/SecCFRelease.h"
29 #include "utilities/der_plist.h"
30 #include "utilities/der_plist_internal.h"
31 #include "utilities/SecCFWrappers.h"
32
33 #include <corecrypto/ccder.h>
34 #include <CoreFoundation/CoreFoundation.h>
35
36 const uint8_t* der_decode_set(CFAllocatorRef allocator, CFOptionFlags mutability,
37 CFSetRef* set, CFErrorRef *error,
38 const uint8_t* der, const uint8_t *der_end)
39 {
40 if (NULL == der)
41 return NULL;
42
43 const uint8_t *payload_end = 0;
44 const uint8_t *payload = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_CFSET, &payload_end, der, der_end);
45
46 if (NULL == payload) {
47 SecCFDERCreateError(kSecDERErrorUnknownEncoding, CFSTR("Unknown data encoding, expected CCDER_CONSTRUCTED_CFSET"), NULL, error);
48 return NULL;
49 }
50
51 CFMutableSetRef theSet = (set && *set) ? CFSetCreateMutableCopy(allocator, 0, *set)
52 : CFSetCreateMutable(allocator, 0, &kCFTypeSetCallBacks);
53
54 if (NULL == theSet) {
55 SecCFDERCreateError(kSecDERErrorAllocationFailure, CFSTR("Failed to create set"), NULL, error);
56 payload = NULL;
57 goto exit;
58 }
59
60 while (payload != NULL && payload < payload_end) {
61 CFTypeRef value = NULL;
62
63 payload = der_decode_plist(allocator, mutability, &value, error, payload, payload_end);
64
65 if (payload) {
66 CFSetAddValue(theSet, value);
67 }
68 CFReleaseNull(value);
69 }
70
71
72 exit:
73 if (payload == payload_end && set) {
74 CFTransferRetained(*set, theSet);
75 }
76
77 CFReleaseNull(theSet);
78
79 return payload;
80 }
81
82 struct size_context {
83 bool success;
84 size_t size;
85 CFErrorRef *error;
86 };
87
88 static void add_value_size(const void *value_void, void *context_void)
89 {
90 CFTypeRef value = (CFTypeRef) value_void;
91 struct size_context *context = (struct size_context*) context_void;
92
93 if (!context->success)
94 return;
95
96 size_t kv_size = der_sizeof_plist(value, context->error);
97 if (kv_size == 0) {
98 context->success = false;
99 return;
100 }
101
102 context->size += kv_size;
103 }
104
105 size_t der_sizeof_set(CFSetRef dict, CFErrorRef *error)
106 {
107 struct size_context context = { .success = true, .size = 0, .error = error };
108
109 CFSetApplyFunction(dict, add_value_size, &context);
110
111 if (!context.success)
112 return 0;
113
114 return ccder_sizeof(CCDER_CONSTRUCTED_CFSET, context.size);
115 }
116
117 struct encode_context {
118 bool success;
119 CFErrorRef * error;
120 CFMutableArrayRef list;
121 CFAllocatorRef allocator;
122 };
123
124 static void add_sequence_to_array(const void *value_void, void *context_void)
125 {
126 struct encode_context *context = (struct encode_context *) context_void;
127 if (context->success) {
128 size_t der_size = der_sizeof_plist(value_void, context->error);
129 if (der_size == 0) {
130 context-> success = false;
131 } else {
132 CFMutableDataRef value = CFDataCreateMutable(context->allocator, der_size);
133 CFDataSetLength(value, der_size);
134
135 uint8_t* const encode_begin = CFDataGetMutableBytePtr(value);
136 uint8_t *encode_end = encode_begin + der_size;
137
138 encode_end = der_encode_plist(value_void, context->error, encode_begin, encode_end);
139
140 if (encode_end != NULL) {
141 CFDataDeleteBytes(value, CFRangeMake(0, (encode_end - encode_begin)));
142 CFArrayAppendValue(context->list, value);
143 } else {
144 context-> success = false;
145 }
146 CFReleaseNull(value);
147 }
148 }
149 }
150
151 static CFComparisonResult cfdata_compare_contents(const void *val1, const void *val2, void *context __unused)
152 {
153 return CFDataCompare((CFDataRef) val1, (CFDataRef) val2);
154 }
155
156
157 uint8_t* der_encode_set(CFSetRef set, CFErrorRef *error,
158 const uint8_t *der, uint8_t *der_end)
159 {
160 CFMutableArrayRef elements = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
161
162 struct encode_context context = { .success = true, .error = error, .list = elements };
163 CFSetApplyFunction(set, add_sequence_to_array, &context);
164
165 if (!context.success) {
166 CFReleaseNull(elements);
167 return NULL;
168 }
169
170 CFRange allOfThem = CFRangeMake(0, CFArrayGetCount(elements));
171
172 CFArraySortValues(elements, allOfThem, cfdata_compare_contents, NULL);
173
174 uint8_t* original_der_end = der_end;
175
176 for(CFIndex position = CFArrayGetCount(elements); position > 0;) {
177 --position;
178 CFDataRef data = CFArrayGetValueAtIndex(elements, position);
179 der_end = ccder_encode_body(CFDataGetLength(data), CFDataGetBytePtr(data), der, der_end);
180 }
181
182 CFReleaseNull(elements);
183
184 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_CFSET, original_der_end, der, der_end);
185
186 }