]>
Commit | Line | Data |
---|---|---|
0a7de745 A |
1 | /*! |
2 | * @header | |
3 | * Provides an interface for managing nonces to govern the lifetime of a | |
cb323159 A |
4 | * personalization performed with TSS. A nonce managed by this interface may |
5 | * be used in a TSS signing request as the value for the BNCH tag. | |
0a7de745 A |
6 | * |
7 | * These interfaces require the caller to possess the | |
8 | * | |
9 | * com.apple.private.security.AppleImage4.user-client | |
10 | * | |
11 | * entitlement. | |
12 | * | |
13 | * @section Threat Model | |
14 | * The adversary possesses the following: | |
15 | * | |
16 | * 1. a manifest which was previously valid but has since been invalidated | |
17 | * by rolling the nonce associated with it | |
18 | * 2. user-level code execution | |
19 | * 3. knowledge of the raw nonce value for the previously-valid manifest | |
20 | * | |
21 | * The defense against this adversary is a system in which knowledge of the raw | |
22 | * nonce is insufficient to impact the evaluation of a personalization. This | |
23 | * system has the following characteristics: | |
24 | * | |
25 | * 1. A nonce seed is stored in an nvram variable which is only writable by | |
26 | * the kernel | |
27 | * 2. When making a new signing request, the nonce seed is encrypted by a | |
28 | * UID1-derived key in-kernel and then hashed -- the output of this | |
29 | * operation the nonce to be used in the signing request | |
30 | * 3. On boot, AppleImage4 obtains the nonce seed from nvram and stores it | |
31 | * in a data structure which will be covered by KTRR | |
32 | * 4. When evaluating a manifest, AppleImage4 reads the raw nonce from the | |
33 | * KTRR-covered data structure and validates it with the same | |
34 | * transformation as was done in (2) | |
35 | * 5. When the nonce is to be invalidated, AppleImage4 sets a flag in an | |
36 | * nvram variable which is only writable by the kernel | |
37 | * 6. On the subsequent boot, AppleImage4 notices the flag, generates a new | |
38 | * nonce and repeats the procedure in (3) | |
39 | * | |
40 | * In this system, the raw nonce seed never leaves the kernel, and the nonce | |
41 | * itself is a non-reversible representation of the seed. | |
42 | */ | |
43 | ||
44 | ||
45 | #ifndef __IMG4_NONCE_H | |
46 | #define __IMG4_NONCE_H | |
47 | ||
48 | #ifndef __IMG4_INDIRECT | |
49 | #error "Please #include <img4/img4.h> instead of this file directly" | |
50 | #endif // __IMG4_INDIRECT | |
51 | ||
cb323159 A |
52 | #if IMG4_TAPI |
53 | #include "tapi.h" | |
54 | #endif | |
55 | ||
0a7de745 A |
56 | /*! |
57 | * @typedef img4_nonce_domain_t | |
58 | * An opaque type describing a nonce domain. | |
59 | */ | |
60 | IMG4_API_AVAILABLE_20181106 | |
61 | typedef struct _img4_nonce_domain img4_nonce_domain_t; | |
62 | ||
63 | /*! | |
64 | * @const IMG4_NONCE_VERSION | |
65 | * The version of the {@link img4_nonce_t} structure supported by the | |
66 | * implementation. | |
67 | */ | |
68 | #define IMG4_NONCE_VERSION ((img4_struct_version_t)0) | |
69 | ||
70 | /*! | |
71 | * @const IMG4_NONCE_MAX_LENGTH | |
72 | * The maximum length of a nonce. Currently, this is the length of a SHA2-384 | |
73 | * hash. | |
74 | */ | |
75 | #define IMG4_NONCE_MAX_LENGTH (48) | |
76 | ||
77 | /*! | |
78 | * @typedef img4_nonce_t | |
79 | * A structure describing a nonce. | |
80 | * | |
81 | * @field i4n_version | |
82 | * The version of the structure. When declaring this structure, you must | |
83 | * initialize this field to {@link IMG4_NONCE_VERSION}. | |
84 | * | |
85 | * @field i4n_nonce | |
86 | * The bytes comprising the nonce. | |
87 | * | |
88 | * @field i4n_length | |
89 | * The length of the nonce. Will be at most {@link IMG4_NONCE_MAX_LENGTH}. | |
90 | */ | |
91 | IMG4_API_AVAILABLE_20181106 | |
92 | typedef struct _img4_nonce { | |
93 | img4_struct_version_t i4n_version; | |
94 | const uint8_t i4n_nonce[IMG4_NONCE_MAX_LENGTH]; | |
95 | uint32_t i4n_length; | |
96 | } img4_nonce_t; | |
97 | ||
98 | /*! | |
99 | * @const IMG4_NONCE_INIT | |
100 | * A convenience initializer for {@link img4_nonce_t} which ensures that the | |
101 | * {@link i4n_version} field is properly initialized. | |
102 | */ | |
103 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L | |
104 | #define IMG4_NONCE_INIT (img4_nonce_t){.i4n_version = IMG4_NONCE_VERSION} | |
105 | #elif defined(__cplusplus) && __cplusplus >= 201103L | |
106 | #define IMG4_NONCE_INIT (img4_nonce_t{IMG4_NONCE_VERSION}) | |
107 | #elif defined(__cplusplus) | |
108 | #define IMG4_NONCE_INIT \ | |
109 | (img4_nonce_t((img4_nonce_t){IMG4_NONCE_VERSION})) | |
110 | #else | |
111 | #define IMG4_NONCE_INIT {IMG4_NONCE_VERSION} | |
112 | #endif | |
113 | ||
114 | /*! | |
115 | * @const IMG4_NONCE_DOMAIN_TRUST_CACHE | |
116 | * The nonce domain governing trust cache personalizations. Use of this domain | |
117 | * requires the | |
118 | * | |
119 | * com.apple.private.img4.nonce.trust-cache | |
120 | * | |
121 | * entitlement. | |
122 | */ | |
cb323159 | 123 | #if !XNU_KERNEL_PRIVATE |
0a7de745 A |
124 | IMG4_API_AVAILABLE_20181106 |
125 | OS_EXPORT | |
126 | const struct _img4_nonce_domain _img4_nonce_domain_trust_cache; | |
127 | #define IMG4_NONCE_DOMAIN_TRUST_CACHE (&_img4_nonce_domain_trust_cache) | |
128 | #else | |
129 | #define IMG4_NONCE_DOMAIN_TRUST_CACHE (img4if->i4if_v1.nonce_domain_trust_cache) | |
130 | #endif | |
131 | ||
cb323159 A |
132 | /*! |
133 | * @const IMG4_NONCE_DOMAIN_PDI | |
134 | * The nonce domain governing disk image personalizations. Use of this domain | |
135 | * requires the | |
136 | * | |
137 | * com.apple.private.img4.nonce.pdi | |
138 | * | |
139 | * entitlement. The nonce for this domain is regenerated once every boot. | |
140 | */ | |
141 | #if !XNU_KERNEL_PRIVATE | |
142 | IMG4_API_AVAILABLE_20181106 | |
143 | OS_EXPORT | |
144 | const struct _img4_nonce_domain _img4_nonce_domain_pdi; | |
145 | #define IMG4_NONCE_DOMAIN_PDI (&_img4_nonce_domain_pdi) | |
146 | #else | |
147 | #define IMG4_NONCE_DOMAIN_PDI (img4if->i4if_v3.nonce_domain_pdi) | |
148 | #endif | |
149 | ||
150 | /*! | |
151 | * @const IMG4_NONCE_DOMAIN_CRYPTEX | |
152 | * The nonce domain governing cryptex personalizations. Use of this domain | |
153 | * requires the | |
154 | * | |
155 | * com.apple.private.img4.nonce.cryptex | |
156 | * | |
157 | * entitlement. | |
158 | */ | |
159 | #if !XNU_KERNEL_PRIVATE | |
160 | IMG4_API_AVAILABLE_20181106 | |
161 | OS_EXPORT | |
162 | const struct _img4_nonce_domain _img4_nonce_domain_cryptex; | |
163 | #define IMG4_NONCE_DOMAIN_CRYPTEX (&_img4_nonce_domain_cryptex) | |
164 | #else | |
165 | #define IMG4_NONCE_DOMAIN_CRYPTEX (img4if->i4if_v1.nonce_domain_cryptex) | |
166 | #endif | |
167 | ||
0a7de745 A |
168 | /*! |
169 | * @function img4_nonce_domain_copy_nonce | |
170 | * Copies the current value of the nonce in the given domain. | |
171 | * | |
172 | * @param nd | |
173 | * The nonce domain. | |
174 | * | |
175 | * @param n | |
176 | * Upon successful return, storage that will contain the current nonce. The | |
177 | * provided structure's {@link i4n_version} must be initialized to | |
178 | * {@link IMG4_NONCE_VERSION}. | |
179 | * | |
180 | * @result | |
181 | * Upon success, zero is returned. The implementation may also return one of the | |
182 | * following error codes directly: | |
183 | * | |
184 | * [ESTALE] The nonce for the given domain has been invalidated, and the | |
185 | * host must reboot in order to generate a new one | |
186 | * [EPERM] The caller lacked the entitlement necessary to read the | |
187 | * given nonce | |
188 | */ | |
cb323159 | 189 | #if !XNU_KERNEL_PRIVATE |
0a7de745 A |
190 | IMG4_API_AVAILABLE_20181106 |
191 | OS_EXPORT OS_WARN_RESULT OS_NONNULL1 OS_NONNULL2 | |
192 | errno_t | |
193 | img4_nonce_domain_copy_nonce(const img4_nonce_domain_t *nd, img4_nonce_t *n); | |
194 | #else | |
195 | #define img4_nonce_domain_copy_nonce(...) \ | |
196 | (i4if->i4if_v1.nonce_domain_copy_nonce(__VA_ARGS__)) | |
197 | #endif | |
198 | ||
199 | /*! | |
200 | * @function img4_nonce_domain_roll_nonce | |
201 | * Invalidates the current nonce for the given domain and forces a re-generation | |
202 | * of the domain's nonce seed at the next boot. | |
203 | * | |
204 | * @param nd | |
205 | * The nonce domain. | |
206 | * | |
207 | * @result | |
208 | * Upon success, zero is returned. The kernel implementation will never return | |
209 | * a non-zero code. The userspace implementation may return one of the following | |
210 | * error codes directly: | |
211 | * | |
212 | * [EPERM] The caller lacked the entitlement necessary to roll the | |
213 | * given nonce | |
214 | */ | |
cb323159 | 215 | #if !XNU_KERNEL_PRIVATE |
0a7de745 A |
216 | IMG4_API_AVAILABLE_20181106 |
217 | OS_EXPORT OS_NONNULL1 | |
218 | errno_t | |
219 | img4_nonce_domain_roll_nonce(const img4_nonce_domain_t *nd); | |
220 | #else | |
221 | #define img4_nonce_domain_roll_nonce(...) \ | |
222 | (i4if->i4if_v1.nonce_domain_roll_nonce(__VA_ARGS__)) | |
223 | #endif | |
224 | ||
225 | #endif // __IMG4_NONCE_H |