]> git.saurik.com Git - apple/security.git/blob - utilities/src/der_boolean.c
Security-55471.tar.gz
[apple/security.git] / utilities / src / der_boolean.c
1 //
2 // der_boolean.c
3 // utilities
4 //
5 // Created by Mitch Adler on 6/19/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_boolean(CFAllocatorRef allocator, CFOptionFlags mutability,
20 CFBooleanRef* boolean, CFErrorRef *error,
21 const uint8_t* der, const uint8_t *der_end)
22 {
23 if (NULL == der)
24 return NULL;
25
26 size_t payload_size = 0;
27 const uint8_t *payload = ccder_decode_tl(CCDER_BOOLEAN, &payload_size, der, der_end);
28
29 if (NULL == payload || (der_end - payload) < payload_size || payload_size != 1) {
30 SecCFDERCreateError(kSecDERErrorUnknownEncoding, CFSTR("Unknown boolean encoding"), NULL, error);
31 return NULL;
32 }
33
34 *boolean = *payload ? kCFBooleanTrue : kCFBooleanFalse;
35
36 return payload + payload_size;
37 }
38
39
40 size_t der_sizeof_boolean(CFBooleanRef data __unused, CFErrorRef *error)
41 {
42 return ccder_sizeof(CCDER_BOOLEAN, 1);
43 }
44
45
46 uint8_t* der_encode_boolean(CFBooleanRef boolean, CFErrorRef *error,
47 const uint8_t *der, uint8_t *der_end)
48 {
49 uint8_t value = CFBooleanGetValue(boolean);
50
51 return ccder_encode_tl(CCDER_BOOLEAN, 1, der,
52 ccder_encode_body(1, &value, der, der_end));
53
54 }