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@
28 #include "utilities/SecCFRelease.h"
29 #include "utilities/der_plist.h"
30 #include "utilities/der_plist_internal.h"
32 #include <corecrypto/ccder.h>
33 #include <CoreFoundation/CoreFoundation.h>
36 const uint8_t* der_decode_number(CFAllocatorRef allocator
, CFOptionFlags mutability
,
37 CFNumberRef
* number
, CFErrorRef
*error
,
38 const uint8_t* der
, const uint8_t *der_end
)
43 size_t payload_size
= 0;
44 const uint8_t *payload
= ccder_decode_tl(CCDER_INTEGER
, &payload_size
, der
, der_end
);
46 if (NULL
== payload
|| (ssize_t
) (der_end
- payload
) < (ssize_t
) payload_size
) {
47 SecCFDERCreateError(kSecDERErrorUnknownEncoding
, CFSTR("Unknown number encoding"), NULL
, error
);
50 if (payload_size
> sizeof(long long)) {
51 SecCFDERCreateError(kSecDERErrorUnsupportedNumberType
, CFSTR("Number too large"), NULL
, error
);
58 if (payload_size
> 0) {
59 if ((*payload
& 0x80) == 0x80)
60 value
= -1; // Negative integers fill with 1s so we end up negative.
62 const uint8_t* const payload_end
= payload
+ payload_size
;
64 for (const uint8_t *payload_byte
= payload
;
65 payload_byte
< payload_end
;
68 value
|= *payload_byte
;
72 *number
= CFNumberCreate(allocator
, kCFNumberLongLongType
, &value
);
74 if (*number
== NULL
) {
75 SecCFDERCreateError(kSecDERErrorAllocationFailure
, CFSTR("Number allocation failed"), NULL
, error
);
79 return payload
+ payload_size
;
83 static inline uint8_t byte_of(size_t byteNumber
, long long value
)
85 return value
>> (8 * (byteNumber
- 1));
88 static inline size_t bytes_when_encoded(long long value
)
90 size_t bytes_encoded
= sizeof(long long);
92 uint8_t first_byte
= byte_of(bytes_encoded
, value
);
94 // Skip initial 0xFFs or 0x00
95 if (first_byte
== 0xFF || first_byte
== 0x00) {
98 } while (bytes_encoded
> 1 && (byte_of(bytes_encoded
, value
) == first_byte
));
100 if ((first_byte
& 0x80) != (byte_of(bytes_encoded
, value
) & 0x80))
104 return bytes_encoded
;
107 size_t der_sizeof_number(CFNumberRef data
, CFErrorRef
*error
)
110 if (!CFNumberGetValue(data
, kCFNumberLongLongType
, &value
))
113 return ccder_sizeof(CCDER_INTEGER
, bytes_when_encoded(value
));
116 uint8_t* der_encode_number(CFNumberRef number
, CFErrorRef
*error
,
117 const uint8_t *der
, uint8_t *der_end
)
120 if (!CFNumberGetValue(number
, kCFNumberLongLongType
, &value
))
123 size_t first_byte_to_include
= bytes_when_encoded(value
);
125 if (!der_end
|| (ssize_t
) (der_end
- der
) < (ssize_t
) first_byte_to_include
)
128 // Put the bytes we should include on the end.
129 for(size_t bytes_included
= 0; bytes_included
< first_byte_to_include
; ++bytes_included
)
132 *der_end
= value
& 0xFF;
136 return ccder_encode_tl(CCDER_INTEGER
, first_byte_to_include
, der
, der_end
);