xref: /xnu-8792.41.9/EXTERNAL_HEADERS/TrustCache/API.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #ifndef libTrustCache_API_h
2*5c2921b0SApple OSS Distributions #define libTrustCache_API_h
3*5c2921b0SApple OSS Distributions 
4*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
5*5c2921b0SApple OSS Distributions __BEGIN_DECLS
6*5c2921b0SApple OSS Distributions 
7*5c2921b0SApple OSS Distributions #include <stdint.h>
8*5c2921b0SApple OSS Distributions #include <stdbool.h>
9*5c2921b0SApple OSS Distributions #include <img4/firmware.h>
10*5c2921b0SApple OSS Distributions #include <TrustCache/RawTypes.h>
11*5c2921b0SApple OSS Distributions #include <TrustCache/Types.h>
12*5c2921b0SApple OSS Distributions #include <TrustCache/TypesConfig.h>
13*5c2921b0SApple OSS Distributions #include <TrustCache/Return.h>
14*5c2921b0SApple OSS Distributions 
15*5c2921b0SApple OSS Distributions /**
16*5c2921b0SApple OSS Distributions  * NOTE: This library does not enforce any concurrency by itself. To be safe in a multi-threaded
17*5c2921b0SApple OSS Distributions  * environment, the caller must manually enforce concurrency on the runtime data structure as
18*5c2921b0SApple OSS Distributions  * otherwise the library is susceptible to memory corruption from race conditions.
19*5c2921b0SApple OSS Distributions  */
20*5c2921b0SApple OSS Distributions 
21*5c2921b0SApple OSS Distributions /**
22*5c2921b0SApple OSS Distributions  * Initialize a runtime to the default values.
23*5c2921b0SApple OSS Distributions  *
24*5c2921b0SApple OSS Distributions  * If the system supports read-only segments, and the runtime is allocated within the read-only
25*5c2921b0SApple OSS Distributions  * segment, then this function needs to be called before the segment is enforced to be read-only.
26*5c2921b0SApple OSS Distributions  * For more information, please look at <TrustCache/Types.h>.
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions static inline void
trustCacheInitializeRuntime(TrustCacheRuntime_t * runtime,TrustCacheMutableRuntime_t * mutableRT,bool allowSecondStaticTC,bool allowEngineeringTC,bool allowLegacyTC,const img4_runtime_t * image4RT)29*5c2921b0SApple OSS Distributions trustCacheInitializeRuntime(TrustCacheRuntime_t *runtime,
30*5c2921b0SApple OSS Distributions                             TrustCacheMutableRuntime_t *mutableRT,
31*5c2921b0SApple OSS Distributions                             bool allowSecondStaticTC,
32*5c2921b0SApple OSS Distributions                             bool allowEngineeringTC,
33*5c2921b0SApple OSS Distributions                             bool allowLegacyTC,
34*5c2921b0SApple OSS Distributions                             const img4_runtime_t *image4RT)
35*5c2921b0SApple OSS Distributions {
36*5c2921b0SApple OSS Distributions     /* Zero out everything */
37*5c2921b0SApple OSS Distributions     memset(runtime, 0, sizeof(*runtime));
38*5c2921b0SApple OSS Distributions     memset(mutableRT, 0, sizeof(*mutableRT));
39*5c2921b0SApple OSS Distributions 
40*5c2921b0SApple OSS Distributions     /* Set the mutable runtime pointer */
41*5c2921b0SApple OSS Distributions     runtime->mutableRT = mutableRT;
42*5c2921b0SApple OSS Distributions 
43*5c2921b0SApple OSS Distributions     /* Setup trust cache type permissions */
44*5c2921b0SApple OSS Distributions     runtime->allowSecondStaticTC = allowSecondStaticTC;
45*5c2921b0SApple OSS Distributions     runtime->allowEngineeringTC = allowEngineeringTC;
46*5c2921b0SApple OSS Distributions     runtime->allowLegacyTC = allowLegacyTC;
47*5c2921b0SApple OSS Distributions 
48*5c2921b0SApple OSS Distributions     /* Set the image4 runtime */
49*5c2921b0SApple OSS Distributions     runtime->image4RT = image4RT;
50*5c2921b0SApple OSS Distributions }
51*5c2921b0SApple OSS Distributions 
52*5c2921b0SApple OSS Distributions /**
53*5c2921b0SApple OSS Distributions  * Add a trust cache module directly to the runtime. This function is used to add modules which
54*5c2921b0SApple OSS Distributions  * don't need to be separately authenticated. Currently, the only trust cache types which can be
55*5c2921b0SApple OSS Distributions  * used with this function are static and engineering trust caches.
56*5c2921b0SApple OSS Distributions  *
57*5c2921b0SApple OSS Distributions  * If the system supports read-only segments, and the runtime is allocated within the read-only
58*5c2921b0SApple OSS Distributions  * segment, then this function needs to be called before the segment is enforced to be read-only.
59*5c2921b0SApple OSS Distributions  * For more information, please look at <TrustCache/Types.h>.
60*5c2921b0SApple OSS Distributions  */
61*5c2921b0SApple OSS Distributions TCReturn_t
62*5c2921b0SApple OSS Distributions trustCacheLoadModule(TrustCacheRuntime_t *runtime,
63*5c2921b0SApple OSS Distributions                      const TCType_t type,
64*5c2921b0SApple OSS Distributions                      TrustCache_t *trustCache,
65*5c2921b0SApple OSS Distributions                      const uintptr_t dataAddr,
66*5c2921b0SApple OSS Distributions                      const size_t dataSize);
67*5c2921b0SApple OSS Distributions 
68*5c2921b0SApple OSS Distributions /**
69*5c2921b0SApple OSS Distributions  * Load a  trust cache onto the system. This function validates the trust cache for a proper
70*5c2921b0SApple OSS Distributions  * signature and adds it to the runtime.
71*5c2921b0SApple OSS Distributions  *
72*5c2921b0SApple OSS Distributions  * Both the payload and the manifest must be provided and they will be validated as image4
73*5c2921b0SApple OSS Distributions  * objects.
74*5c2921b0SApple OSS Distributions  */
75*5c2921b0SApple OSS Distributions TCReturn_t
76*5c2921b0SApple OSS Distributions trustCacheLoad(TrustCacheRuntime_t *runtime,
77*5c2921b0SApple OSS Distributions                TCType_t type,
78*5c2921b0SApple OSS Distributions                TrustCache_t *trustCache,
79*5c2921b0SApple OSS Distributions                const uintptr_t payloadAddr,
80*5c2921b0SApple OSS Distributions                const size_t payloadSize,
81*5c2921b0SApple OSS Distributions                const uintptr_t manifestAddr,
82*5c2921b0SApple OSS Distributions                const size_t manifestSize);
83*5c2921b0SApple OSS Distributions 
84*5c2921b0SApple OSS Distributions /**
85*5c2921b0SApple OSS Distributions  * Query a  trust cache for a particular CDHash. The returned token can then be used to
86*5c2921b0SApple OSS Distributions  * query further attributes from the matched entry.
87*5c2921b0SApple OSS Distributions  */
88*5c2921b0SApple OSS Distributions TCReturn_t
89*5c2921b0SApple OSS Distributions trustCacheQuery(const TrustCacheRuntime_t *runtime,
90*5c2921b0SApple OSS Distributions                 TCQueryType_t queryType,
91*5c2921b0SApple OSS Distributions                 const uint8_t CDHash[kTCEntryHashSize],
92*5c2921b0SApple OSS Distributions                 TrustCacheQueryToken_t *queryToken);
93*5c2921b0SApple OSS Distributions 
94*5c2921b0SApple OSS Distributions /**
95*5c2921b0SApple OSS Distributions  * Get the capabilities of a trust cache. This function can be used to query which fields a given
96*5c2921b0SApple OSS Distributions  * trust cache supports.
97*5c2921b0SApple OSS Distributions  *
98*5c2921b0SApple OSS Distributions  * The fields which are supported are based on the version of the trust cache module.
99*5c2921b0SApple OSS Distributions  */
100*5c2921b0SApple OSS Distributions TCReturn_t
101*5c2921b0SApple OSS Distributions trustCacheGetCapabilities(const TrustCache_t *trustCache,
102*5c2921b0SApple OSS Distributions                           TCCapabilities_t *capabilities);
103*5c2921b0SApple OSS Distributions 
104*5c2921b0SApple OSS Distributions /**
105*5c2921b0SApple OSS Distributions  * Acquire the trust cache type for a query token.
106*5c2921b0SApple OSS Distributions  */
107*5c2921b0SApple OSS Distributions TCReturn_t
108*5c2921b0SApple OSS Distributions trustCacheQueryGetTCType(const TrustCacheQueryToken_t *queryToken,
109*5c2921b0SApple OSS Distributions                          TCType_t *typeRet);
110*5c2921b0SApple OSS Distributions 
111*5c2921b0SApple OSS Distributions /**
112*5c2921b0SApple OSS Distributions  * Acquire the capabilities of the trust cache through a query token.
113*5c2921b0SApple OSS Distributions  */
114*5c2921b0SApple OSS Distributions TCReturn_t
115*5c2921b0SApple OSS Distributions trustCacheQueryGetCapabilities(const TrustCacheQueryToken_t *queryToken,
116*5c2921b0SApple OSS Distributions                                TCCapabilities_t *capabilities);
117*5c2921b0SApple OSS Distributions 
118*5c2921b0SApple OSS Distributions /**
119*5c2921b0SApple OSS Distributions  * Acquire the hash type for the CDHash through a query token.
120*5c2921b0SApple OSS Distributions  */
121*5c2921b0SApple OSS Distributions TCReturn_t
122*5c2921b0SApple OSS Distributions trustCacheQueryGetHashType(const TrustCacheQueryToken_t *queryToken,
123*5c2921b0SApple OSS Distributions                            uint8_t *hashTypeRet);
124*5c2921b0SApple OSS Distributions 
125*5c2921b0SApple OSS Distributions /**
126*5c2921b0SApple OSS Distributions  * Acquire the flags for a trust cache entry through a query token.
127*5c2921b0SApple OSS Distributions  */
128*5c2921b0SApple OSS Distributions TCReturn_t
129*5c2921b0SApple OSS Distributions trustCacheQueryGetFlags(const TrustCacheQueryToken_t *queryToken,
130*5c2921b0SApple OSS Distributions                         uint64_t *flagsRet);
131*5c2921b0SApple OSS Distributions 
132*5c2921b0SApple OSS Distributions /**
133*5c2921b0SApple OSS Distributions  * Acquire the constraint category for a trust cache entry through a query token.
134*5c2921b0SApple OSS Distributions  */
135*5c2921b0SApple OSS Distributions TCReturn_t
136*5c2921b0SApple OSS Distributions trustCacheQueryGetConstraintCategory(const TrustCacheQueryToken_t *queryToken,
137*5c2921b0SApple OSS Distributions                                      uint8_t *constraintCategoryRet);
138*5c2921b0SApple OSS Distributions 
139*5c2921b0SApple OSS Distributions __END_DECLS
140*5c2921b0SApple OSS Distributions #endif /* libTrustCache_API_h */
141