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