xref: /xnu-12377.81.4/iokit/Kernel/i386/IOKeyStoreHelper.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2010 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions #include <sys/cdefs.h>
30*043036a2SApple OSS Distributions #include <stdbool.h>
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions #include <IOKit/assert.h>
33*043036a2SApple OSS Distributions #include <IOKit/system.h>
34*043036a2SApple OSS Distributions #include <IOKit/IOLib.h>
35*043036a2SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
36*043036a2SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*043036a2SApple OSS Distributions 
38*043036a2SApple OSS Distributions __BEGIN_DECLS
39*043036a2SApple OSS Distributions 
40*043036a2SApple OSS Distributions #include <pexpert/pexpert.h>
41*043036a2SApple OSS Distributions 
42*043036a2SApple OSS Distributions static volatile UInt32 alreadyFetched = 0;
43*043036a2SApple OSS Distributions static IOMemoryDescriptor * newData;
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions IOMemoryDescriptor *
46*043036a2SApple OSS Distributions IOGetBootKeyStoreData(void);
47*043036a2SApple OSS Distributions void
48*043036a2SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data);
49*043036a2SApple OSS Distributions 
50*043036a2SApple OSS Distributions // APFS
51*043036a2SApple OSS Distributions static volatile UInt32 apfsKeyFetched = 0;
52*043036a2SApple OSS Distributions static IOMemoryDescriptor* apfsKeyData = NULL;
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions IOMemoryDescriptor* IOGetAPFSKeyStoreData();
55*043036a2SApple OSS Distributions void IOSetAPFSKeyStoreData(IOMemoryDescriptor* data);
56*043036a2SApple OSS Distributions 
57*043036a2SApple OSS Distributions static volatile UInt32 ARVRootHashFetched = 0;
58*043036a2SApple OSS Distributions static volatile UInt32 bsARVRootHashFetched = 0;
59*043036a2SApple OSS Distributions 
60*043036a2SApple OSS Distributions IOMemoryDescriptor* IOGetARVRootHashData(void);
61*043036a2SApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVRootHashData(void);
62*043036a2SApple OSS Distributions 
63*043036a2SApple OSS Distributions bool IOBaseSystemARVRootHashAvailable(void);
64*043036a2SApple OSS Distributions 
65*043036a2SApple OSS Distributions static volatile UInt32 ARVManifestFetched = 0;
66*043036a2SApple OSS Distributions static volatile UInt32 bsARVManifestFetched = 0;
67*043036a2SApple OSS Distributions 
68*043036a2SApple OSS Distributions IOMemoryDescriptor* IOGetARVManifestData(void);
69*043036a2SApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVManifestData(void);
70*043036a2SApple OSS Distributions 
71*043036a2SApple OSS Distributions __END_DECLS
72*043036a2SApple OSS Distributions 
73*043036a2SApple OSS Distributions #if 1
74*043036a2SApple OSS Distributions #define DEBG(fmt, args...)      { kprintf(fmt, ## args); }
75*043036a2SApple OSS Distributions #else
76*043036a2SApple OSS Distributions #define DEBG(fmt, args...)      {}
77*043036a2SApple OSS Distributions #endif
78*043036a2SApple OSS Distributions 
79*043036a2SApple OSS Distributions void
IOSetKeyStoreData(IOMemoryDescriptor * data)80*043036a2SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data)
81*043036a2SApple OSS Distributions {
82*043036a2SApple OSS Distributions 	newData = data;
83*043036a2SApple OSS Distributions 	alreadyFetched = 0;
84*043036a2SApple OSS Distributions }
85*043036a2SApple OSS Distributions 
86*043036a2SApple OSS Distributions IOMemoryDescriptor *
IOGetBootKeyStoreData(void)87*043036a2SApple OSS Distributions IOGetBootKeyStoreData(void)
88*043036a2SApple OSS Distributions {
89*043036a2SApple OSS Distributions 	IOMemoryDescriptor *memoryDescriptor;
90*043036a2SApple OSS Distributions 	boot_args *args = (boot_args *)PE_state.bootArgs;
91*043036a2SApple OSS Distributions 	IOOptionBits options;
92*043036a2SApple OSS Distributions 	IOAddressRange ranges;
93*043036a2SApple OSS Distributions 
94*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &alreadyFetched)) {
95*043036a2SApple OSS Distributions 		return NULL;
96*043036a2SApple OSS Distributions 	}
97*043036a2SApple OSS Distributions 
98*043036a2SApple OSS Distributions 	if (newData) {
99*043036a2SApple OSS Distributions 		IOMemoryDescriptor * data = newData;
100*043036a2SApple OSS Distributions 		newData = NULL;
101*043036a2SApple OSS Distributions 		return data;
102*043036a2SApple OSS Distributions 	}
103*043036a2SApple OSS Distributions 
104*043036a2SApple OSS Distributions 	DEBG("%s: data at address %u size %u\n", __func__,
105*043036a2SApple OSS Distributions 	    args->keyStoreDataStart,
106*043036a2SApple OSS Distributions 	    args->keyStoreDataSize);
107*043036a2SApple OSS Distributions 
108*043036a2SApple OSS Distributions 	if (args->keyStoreDataStart == 0) {
109*043036a2SApple OSS Distributions 		return NULL;
110*043036a2SApple OSS Distributions 	}
111*043036a2SApple OSS Distributions 
112*043036a2SApple OSS Distributions 	ranges.address = args->keyStoreDataStart;
113*043036a2SApple OSS Distributions 	ranges.length = args->keyStoreDataSize;
114*043036a2SApple OSS Distributions 
115*043036a2SApple OSS Distributions 	options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
116*043036a2SApple OSS Distributions 
117*043036a2SApple OSS Distributions 	memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges,
118*043036a2SApple OSS Distributions 	    1,
119*043036a2SApple OSS Distributions 	    0,
120*043036a2SApple OSS Distributions 	    NULL,
121*043036a2SApple OSS Distributions 	    options);
122*043036a2SApple OSS Distributions 
123*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
124*043036a2SApple OSS Distributions 
125*043036a2SApple OSS Distributions 	return memoryDescriptor;
126*043036a2SApple OSS Distributions }
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions // APFS volume key fetcher
129*043036a2SApple OSS Distributions 
130*043036a2SApple OSS Distributions // Store in-memory key (could be used by IOHibernateDone)
131*043036a2SApple OSS Distributions void
IOSetAPFSKeyStoreData(IOMemoryDescriptor * data)132*043036a2SApple OSS Distributions IOSetAPFSKeyStoreData(IOMemoryDescriptor* data)
133*043036a2SApple OSS Distributions {
134*043036a2SApple OSS Distributions 	// Do not allow re-fetching of the boot_args key by passing NULL here.
135*043036a2SApple OSS Distributions 	if (data != NULL) {
136*043036a2SApple OSS Distributions 		apfsKeyData = data;
137*043036a2SApple OSS Distributions 		apfsKeyFetched = 0;
138*043036a2SApple OSS Distributions 	}
139*043036a2SApple OSS Distributions }
140*043036a2SApple OSS Distributions 
141*043036a2SApple OSS Distributions // Retrieve any key we may have (stored in boot_args or by Hibernate)
142*043036a2SApple OSS Distributions IOMemoryDescriptor*
IOGetAPFSKeyStoreData()143*043036a2SApple OSS Distributions IOGetAPFSKeyStoreData()
144*043036a2SApple OSS Distributions {
145*043036a2SApple OSS Distributions 	// Check if someone got the key before us
146*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &apfsKeyFetched)) {
147*043036a2SApple OSS Distributions 		return NULL;
148*043036a2SApple OSS Distributions 	}
149*043036a2SApple OSS Distributions 
150*043036a2SApple OSS Distributions 	// Do we have in-memory key?
151*043036a2SApple OSS Distributions 	if (apfsKeyData) {
152*043036a2SApple OSS Distributions 		IOMemoryDescriptor* data = apfsKeyData;
153*043036a2SApple OSS Distributions 		apfsKeyData = NULL;
154*043036a2SApple OSS Distributions 		return data;
155*043036a2SApple OSS Distributions 	}
156*043036a2SApple OSS Distributions 
157*043036a2SApple OSS Distributions 	// Looks like there was no in-memory key and it's the first call - try boot_args
158*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
159*043036a2SApple OSS Distributions 
160*043036a2SApple OSS Distributions 	DEBG("%s: data at address %u size %u\n", __func__, args->apfsDataStart, args->apfsDataSize);
161*043036a2SApple OSS Distributions 	if (args->apfsDataStart == 0) {
162*043036a2SApple OSS Distributions 		return NULL;
163*043036a2SApple OSS Distributions 	}
164*043036a2SApple OSS Distributions 
165*043036a2SApple OSS Distributions 	// We have the key in the boot_args, create IOMemoryDescriptor for the blob
166*043036a2SApple OSS Distributions 	IOAddressRange ranges;
167*043036a2SApple OSS Distributions 	ranges.address = args->apfsDataStart;
168*043036a2SApple OSS Distributions 	ranges.length = args->apfsDataSize;
169*043036a2SApple OSS Distributions 
170*043036a2SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
171*043036a2SApple OSS Distributions 
172*043036a2SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
173*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
174*043036a2SApple OSS Distributions 	return memoryDescriptor;
175*043036a2SApple OSS Distributions }
176*043036a2SApple OSS Distributions 
177*043036a2SApple OSS Distributions // ARV Root Hash fetcher
178*043036a2SApple OSS Distributions 
179*043036a2SApple OSS Distributions // Retrieve any root hash we may have (stored in boot_args)
180*043036a2SApple OSS Distributions IOMemoryDescriptor*
IOGetARVRootHashData(void)181*043036a2SApple OSS Distributions IOGetARVRootHashData(void)
182*043036a2SApple OSS Distributions {
183*043036a2SApple OSS Distributions 	// Check if someone got the root hash before us
184*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &ARVRootHashFetched)) {
185*043036a2SApple OSS Distributions 		return NULL;
186*043036a2SApple OSS Distributions 	}
187*043036a2SApple OSS Distributions 
188*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
189*043036a2SApple OSS Distributions 
190*043036a2SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->arvRootHashStart, args->arvRootHashSize);
191*043036a2SApple OSS Distributions 	if (args->arvRootHashStart == 0) {
192*043036a2SApple OSS Distributions 		return NULL;
193*043036a2SApple OSS Distributions 	}
194*043036a2SApple OSS Distributions 
195*043036a2SApple OSS Distributions 	// We have the root hash in the boot_args, create IOMemoryDescriptor for the blob
196*043036a2SApple OSS Distributions 	IOAddressRange ranges;
197*043036a2SApple OSS Distributions 	ranges.address = args->arvRootHashStart;
198*043036a2SApple OSS Distributions 	ranges.length = args->arvRootHashSize;
199*043036a2SApple OSS Distributions 
200*043036a2SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
201*043036a2SApple OSS Distributions 
202*043036a2SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
203*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
204*043036a2SApple OSS Distributions 	return memoryDescriptor;
205*043036a2SApple OSS Distributions }
206*043036a2SApple OSS Distributions 
207*043036a2SApple OSS Distributions // Base System Analogue
208*043036a2SApple OSS Distributions 
209*043036a2SApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVRootHashData(void)210*043036a2SApple OSS Distributions IOGetBaseSystemARVRootHashData(void)
211*043036a2SApple OSS Distributions {
212*043036a2SApple OSS Distributions 	// Check if someone got the base system root hash before us
213*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &bsARVRootHashFetched)) {
214*043036a2SApple OSS Distributions 		return NULL;
215*043036a2SApple OSS Distributions 	}
216*043036a2SApple OSS Distributions 
217*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
218*043036a2SApple OSS Distributions 
219*043036a2SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVRootHashStart, args->bsARVRootHashSize);
220*043036a2SApple OSS Distributions 	if (args->bsARVRootHashStart == 0) {
221*043036a2SApple OSS Distributions 		return NULL;
222*043036a2SApple OSS Distributions 	}
223*043036a2SApple OSS Distributions 
224*043036a2SApple OSS Distributions 	// We have the base system root hash in the boot_args, create IOMemoryDescriptor for the blob
225*043036a2SApple OSS Distributions 	IOAddressRange ranges;
226*043036a2SApple OSS Distributions 	ranges.address = args->bsARVRootHashStart;
227*043036a2SApple OSS Distributions 	ranges.length = args->bsARVRootHashSize;
228*043036a2SApple OSS Distributions 
229*043036a2SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
230*043036a2SApple OSS Distributions 
231*043036a2SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
232*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
233*043036a2SApple OSS Distributions 	return memoryDescriptor;
234*043036a2SApple OSS Distributions }
235*043036a2SApple OSS Distributions 
236*043036a2SApple OSS Distributions bool
IOBaseSystemARVRootHashAvailable(void)237*043036a2SApple OSS Distributions IOBaseSystemARVRootHashAvailable(void)
238*043036a2SApple OSS Distributions {
239*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
240*043036a2SApple OSS Distributions 
241*043036a2SApple OSS Distributions 	if (args->bsARVRootHashStart == 0 || args->bsARVRootHashSize == 0) {
242*043036a2SApple OSS Distributions 		return false;
243*043036a2SApple OSS Distributions 	}
244*043036a2SApple OSS Distributions 
245*043036a2SApple OSS Distributions 	if (args->bsARVManifestStart == 0 || args->bsARVManifestSize == 0) {
246*043036a2SApple OSS Distributions 		return false;
247*043036a2SApple OSS Distributions 	}
248*043036a2SApple OSS Distributions 
249*043036a2SApple OSS Distributions 	return true;
250*043036a2SApple OSS Distributions }
251*043036a2SApple OSS Distributions 
252*043036a2SApple OSS Distributions // ARV Manifest fetcher
253*043036a2SApple OSS Distributions 
254*043036a2SApple OSS Distributions // Retrieve any manifest we may have (stored in boot_args)
255*043036a2SApple OSS Distributions IOMemoryDescriptor*
IOGetARVManifestData(void)256*043036a2SApple OSS Distributions IOGetARVManifestData(void)
257*043036a2SApple OSS Distributions {
258*043036a2SApple OSS Distributions 	// Check if someone got the manifest before us
259*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &ARVManifestFetched)) {
260*043036a2SApple OSS Distributions 		return NULL;
261*043036a2SApple OSS Distributions 	}
262*043036a2SApple OSS Distributions 
263*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
264*043036a2SApple OSS Distributions 
265*043036a2SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->arvManifestStart, args->arvManifestSize);
266*043036a2SApple OSS Distributions 	if (args->arvManifestStart == 0) {
267*043036a2SApple OSS Distributions 		return NULL;
268*043036a2SApple OSS Distributions 	}
269*043036a2SApple OSS Distributions 
270*043036a2SApple OSS Distributions 	// We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
271*043036a2SApple OSS Distributions 	IOAddressRange ranges;
272*043036a2SApple OSS Distributions 	ranges.address = args->arvManifestStart;
273*043036a2SApple OSS Distributions 	ranges.length = args->arvManifestSize;
274*043036a2SApple OSS Distributions 
275*043036a2SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
276*043036a2SApple OSS Distributions 
277*043036a2SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
278*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
279*043036a2SApple OSS Distributions 	return memoryDescriptor;
280*043036a2SApple OSS Distributions }
281*043036a2SApple OSS Distributions 
282*043036a2SApple OSS Distributions // Base System Analogue
283*043036a2SApple OSS Distributions 
284*043036a2SApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVManifestData(void)285*043036a2SApple OSS Distributions IOGetBaseSystemARVManifestData(void)
286*043036a2SApple OSS Distributions {
287*043036a2SApple OSS Distributions 	// Check if someone got the base system manifest before us
288*043036a2SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &bsARVManifestFetched)) {
289*043036a2SApple OSS Distributions 		return NULL;
290*043036a2SApple OSS Distributions 	}
291*043036a2SApple OSS Distributions 
292*043036a2SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
293*043036a2SApple OSS Distributions 
294*043036a2SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVManifestStart, args->bsARVManifestSize);
295*043036a2SApple OSS Distributions 	if (args->bsARVManifestStart == 0) {
296*043036a2SApple OSS Distributions 		return NULL;
297*043036a2SApple OSS Distributions 	}
298*043036a2SApple OSS Distributions 
299*043036a2SApple OSS Distributions 	// We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
300*043036a2SApple OSS Distributions 	IOAddressRange ranges;
301*043036a2SApple OSS Distributions 	ranges.address = args->bsARVManifestStart;
302*043036a2SApple OSS Distributions 	ranges.length = args->bsARVManifestSize;
303*043036a2SApple OSS Distributions 
304*043036a2SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
305*043036a2SApple OSS Distributions 
306*043036a2SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
307*043036a2SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
308*043036a2SApple OSS Distributions 	return memoryDescriptor;
309*043036a2SApple OSS Distributions }
310