]> git.saurik.com Git - apple/security.git/blob - utilities/src/der_array.c
Security-55471.14.tar.gz
[apple/security.git] / utilities / src / der_array.c
1 //
2 // der_array.c
3 // utilities
4 //
5 // Created by Mitch Adler on 7/2/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
7 //
8
9 #include <stdio.h>
10
11 #include "utilities/SecCFRelease.h"
12 #include "utilities/der_plist.h"
13 #include "utilities/der_plist_internal.h"
14
15 #include <corecrypto/ccder.h>
16 #include <CoreFoundation/CoreFoundation.h>
17
18
19 const uint8_t* der_decode_array(CFAllocatorRef allocator, CFOptionFlags mutability,
20 CFArrayRef* array, CFErrorRef *error,
21 const uint8_t* der, const uint8_t *der_end)
22 {
23 if (NULL == der)
24 return NULL;
25
26 CFMutableArrayRef result = CFArrayCreateMutable(allocator, 0, &kCFTypeArrayCallBacks);
27
28 const uint8_t *elements_end;
29 const uint8_t *current_element = ccder_decode_sequence_tl(&elements_end, der, der_end);
30
31 while (current_element != NULL && current_element < elements_end) {
32 CFPropertyListRef element = NULL;
33 current_element = der_decode_plist(allocator, mutability, &element, error, current_element, elements_end);
34 if (current_element) {
35 CFArrayAppendValue(result, element);
36 CFReleaseNull(element);
37 }
38 }
39
40 if (current_element) {
41 *array = result;
42 result = NULL;
43 }
44
45 CFReleaseNull(result);
46 return current_element;
47 }
48
49
50 size_t der_sizeof_array(CFArrayRef data, CFErrorRef *error)
51 {
52 size_t body_size = 0;
53 for(CFIndex position = CFArrayGetCount(data) - 1;
54 position >= 0;
55 --position)
56 {
57 body_size += der_sizeof_plist(CFArrayGetValueAtIndex(data, position), error);
58 }
59 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, body_size);
60 }
61
62
63 uint8_t* der_encode_array(CFArrayRef array, CFErrorRef *error,
64 const uint8_t *der, uint8_t *der_end)
65 {
66 uint8_t* original_der_end = der_end;
67 for(CFIndex position = CFArrayGetCount(array) - 1;
68 position >= 0;
69 --position)
70 {
71 der_end = der_encode_plist(CFArrayGetValueAtIndex(array, position), error, der, der_end);
72 }
73
74 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, original_der_end, der, der_end);
75 }