1//
2// Runtime.h
3// CoreEntitlements
4//
5//
6
7#ifndef CORE_ENTITLEMENTS_RUNTIME_H
8#define CORE_ENTITLEMENTS_RUNTIME_H
9
10#ifndef _CE_INDIRECT
11#error "Please include <CoreEntitlements/CoreEntitlements.h> instead of this file"
12#endif
13
14#include <stdint.h>
15#include <stddef.h>
16#include <stdbool.h>
17
18__ptrcheck_abi_assume_single();
19
20#define CE_MAX_KEY_SIZE 240
21
22#ifndef __result_use_check
23#define __result_use_check
24#endif
25
26#define CE_RUNTIME_VERSION 1
27#define CE_RUNTIME_WITH_INDEX_VERSION 2
28
29/*!
30 * @struct CEBuffer
31 * Represents a sized chunk of DER data
32 * Strings and blobs used and returned by CoreEntitlements always use CEBuffer
33 *
34 * @note
35 * If a DER string is returned to you via a CEBuffer, you cannot assume it is null-terminated.
36 */
37typedef struct {
38 const uint8_t *__counted_by(length) data;
39 size_t length;
40} CEBuffer;
41
42/*!
43 * @struct CEStaticBuffer
44 * Represents a sized chunk of data that is stored inline
45 */
46typedef struct {
47 uint8_t data[CE_MAX_KEY_SIZE];
48 size_t length;
49} CEStaticBuffer;
50
51#define CEBuffStr(str) (CEBuffer){.data = (const uint8_t*)str, .length = sizeof(str) - 1}
52
53/*!
54 * @typedef CERuntimeMalloc
55 * Function prototype that the CERuntime may ues to allocate data (e.g.. malloc)
56 */
57typedef void* __unsafe_indexable (*CERuntimeMalloc)(const CERuntime_t rt, size_t size) __result_use_check;
58/*!
59 * @typedef CERuntimeFree
60 * Function prototype that the CERuntime may ues to free allocated data (e.g. free)
61 */
62typedef void (*CERuntimeFree)(const CERuntime_t rt, void* address);
63/*!
64 * @typedef CERuntimeLog
65 * Function prototype that the CERuntime may use to log helpful information (e.g. printf)
66 */
67typedef void (*CERuntimeLog)(const CERuntime_t rt, const char* __unsafe_indexable fmt, ...) __printflike(2, 3);
68/*!
69 * @typedef CERuntimeAbort
70 * Function prototype that the CERuntime will use if it encounters a condition which may compromise the integrity of the system (e.g. abort, panic)
71 */
72typedef void (*CERuntimeAbort)(const CERuntime_t rt, const char* __unsafe_indexable fmt, ...) __printflike(2, 3) __attribute__((noreturn));
73/*!
74 * @typedef CERuntimeInternalStatus
75 * Function prototype that the CERuntime may use to query AppleInternal status
76 */
77typedef bool (*CERuntimeInternalStatus)(const CERuntime_t rt);
78
79/*!
80 * @typedef CERuntimeAllocIndex
81 * Function prototype that the CERuntime may ues to allocate an index of the specified size
82 */
83typedef void* __unsafe_indexable (*CERuntimeAllocIndex)(const CERuntime_t rt, size_t size) __result_use_check;
84
85/*!
86 * @typedef CERuntimeFreeIndex
87 * Function prototype that the CERuntime may ues to free an index of the specified size
88 */
89typedef void (*CERuntimeFreeIndex)(const CERuntime_t rt, void* index, size_t size);
90
91/*!
92 * @struct CERuntime
93 * This structure represents the interface that CoreEntitlements uses to communicate with the outside world.
94 * The presense or absence of function pointers in this structure may degrade certain functionality.
95 *
96 * @note
97 * The only prototype that MUST be implemented is CERuntimeAbort abort.
98 */
99struct CERuntime {
100 const uint64_t version;
101 const CERuntimeMalloc alloc;
102 const CERuntimeFree free;
103 const CERuntimeLog log;
104 const CERuntimeAbort abort;
105 const CERuntimeInternalStatus internalStatus;
106 const CERuntimeAllocIndex allocIndex;
107 const CERuntimeFreeIndex freeIndex;
108} ;
109
110#endif
111