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