1*5e3eaea3SApple OSS Distributions /* 2*5e3eaea3SApple OSS Distributions * Copyright (c) 2009-2023 Apple Inc. All rights reserved. 3*5e3eaea3SApple OSS Distributions * 4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*5e3eaea3SApple OSS Distributions * 6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*5e3eaea3SApple OSS Distributions * 15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at 16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*5e3eaea3SApple OSS Distributions * 18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are 19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and 24*5e3eaea3SApple OSS Distributions * limitations under the License. 25*5e3eaea3SApple OSS Distributions * 26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*5e3eaea3SApple OSS Distributions */ 28*5e3eaea3SApple OSS Distributions 29*5e3eaea3SApple OSS Distributions #ifndef _SYS_CPROTECT_H_ 30*5e3eaea3SApple OSS Distributions #define _SYS_CPROTECT_H_ 31*5e3eaea3SApple OSS Distributions 32*5e3eaea3SApple OSS Distributions #ifdef KERNEL_PRIVATE 33*5e3eaea3SApple OSS Distributions 34*5e3eaea3SApple OSS Distributions #include <sys/cdefs.h> 35*5e3eaea3SApple OSS Distributions #include <sys/param.h> 36*5e3eaea3SApple OSS Distributions #include <sys/buf.h> 37*5e3eaea3SApple OSS Distributions #include <sys/kdebug.h> 38*5e3eaea3SApple OSS Distributions #include <crypto/aes.h> 39*5e3eaea3SApple OSS Distributions #include <stdbool.h> 40*5e3eaea3SApple OSS Distributions #include <uuid/uuid.h> 41*5e3eaea3SApple OSS Distributions #include <libkern/crypto/sha1.h> 42*5e3eaea3SApple OSS Distributions 43*5e3eaea3SApple OSS Distributions __BEGIN_DECLS 44*5e3eaea3SApple OSS Distributions 45*5e3eaea3SApple OSS Distributions #define CP_CODE(code) FSDBG_CODE(DBG_CONTENT_PROT, code) 46*5e3eaea3SApple OSS Distributions /* 47*5e3eaea3SApple OSS Distributions * Class DBG_FSYSTEM == 0x03 48*5e3eaea3SApple OSS Distributions * Subclass DBG_CONTENT_PROT == 0xCF 49*5e3eaea3SApple OSS Distributions * These debug codes are of the form 0x03CFzzzz 50*5e3eaea3SApple OSS Distributions */ 51*5e3eaea3SApple OSS Distributions 52*5e3eaea3SApple OSS Distributions enum { 53*5e3eaea3SApple OSS Distributions CPDBG_OFFSET_IO = CP_CODE(0), /* 0x03CF0000 */ 54*5e3eaea3SApple OSS Distributions }; 55*5e3eaea3SApple OSS Distributions 56*5e3eaea3SApple OSS Distributions /* normally the debug events are no-ops */ 57*5e3eaea3SApple OSS Distributions #define CP_DEBUG(x, a, b, c, d, e) do {} while (0); 58*5e3eaea3SApple OSS Distributions 59*5e3eaea3SApple OSS Distributions /* dev kernels only! */ 60*5e3eaea3SApple OSS Distributions #if !SECURE_KERNEL 61*5e3eaea3SApple OSS Distributions 62*5e3eaea3SApple OSS Distributions /* KDEBUG events used by content protection subsystem */ 63*5e3eaea3SApple OSS Distributions #if 0 64*5e3eaea3SApple OSS Distributions #undef CP_DEBUG 65*5e3eaea3SApple OSS Distributions #define CP_DEBUG KERNEL_DEBUG_CONSTANT 66*5e3eaea3SApple OSS Distributions #endif 67*5e3eaea3SApple OSS Distributions 68*5e3eaea3SApple OSS Distributions #endif 69*5e3eaea3SApple OSS Distributions 70*5e3eaea3SApple OSS Distributions #define CP_MAX_WRAPPEDKEYSIZE 128 /* The size of the largest allowed key */ 71*5e3eaea3SApple OSS Distributions #define VFS_CP_MAX_CACHEBUFLEN 64 /* Maximum size of the cached key */ 72*5e3eaea3SApple OSS Distributions 73*5e3eaea3SApple OSS Distributions /* lock events from AppleKeyStore */ 74*5e3eaea3SApple OSS Distributions enum { 75*5e3eaea3SApple OSS Distributions CP_ACTION_LOCKED = 0, 76*5e3eaea3SApple OSS Distributions CP_ACTION_UNLOCKED = 1, 77*5e3eaea3SApple OSS Distributions CP_ACTION_EP_INVALIDATED = 2, 78*5e3eaea3SApple OSS Distributions CP_ACTION_CX_EXPIRED = 3, 79*5e3eaea3SApple OSS Distributions }; 80*5e3eaea3SApple OSS Distributions /* 81*5e3eaea3SApple OSS Distributions * Ideally, cp_key_store_action_t would be an enum, but we cannot fix 82*5e3eaea3SApple OSS Distributions * that until AppleKeyStore is updated. 83*5e3eaea3SApple OSS Distributions */ 84*5e3eaea3SApple OSS Distributions typedef int cp_key_store_action_t; 85*5e3eaea3SApple OSS Distributions 86*5e3eaea3SApple OSS Distributions /* 87*5e3eaea3SApple OSS Distributions * It was once the case (and it may still be the case) where the lock 88*5e3eaea3SApple OSS Distributions * state got conflated with the possible actions/events that 89*5e3eaea3SApple OSS Distributions * AppleKeyStore can send. For that reason, the locked states below 90*5e3eaea3SApple OSS Distributions * should numerically match their corresponding actions above. 91*5e3eaea3SApple OSS Distributions */ 92*5e3eaea3SApple OSS Distributions typedef unsigned char cp_lock_state_t; 93*5e3eaea3SApple OSS Distributions enum { 94*5e3eaea3SApple OSS Distributions CP_LOCKED_STATE = 0, 95*5e3eaea3SApple OSS Distributions CP_UNLOCKED_STATE = 1, 96*5e3eaea3SApple OSS Distributions }; 97*5e3eaea3SApple OSS Distributions 98*5e3eaea3SApple OSS Distributions typedef unsigned char cp_ep_state_t; 99*5e3eaea3SApple OSS Distributions enum { 100*5e3eaea3SApple OSS Distributions CP_EP_INVALIDATED = 0, 101*5e3eaea3SApple OSS Distributions }; 102*5e3eaea3SApple OSS Distributions 103*5e3eaea3SApple OSS Distributions typedef unsigned char cp_cx_state_t; 104*5e3eaea3SApple OSS Distributions enum { 105*5e3eaea3SApple OSS Distributions CP_CX_EXPIRED = 0, 106*5e3eaea3SApple OSS Distributions }; 107*5e3eaea3SApple OSS Distributions 108*5e3eaea3SApple OSS Distributions typedef uint32_t cp_key_class_t; 109*5e3eaea3SApple OSS Distributions typedef uint32_t cp_key_os_version_t; 110*5e3eaea3SApple OSS Distributions typedef uint16_t cp_key_revision_t; 111*5e3eaea3SApple OSS Distributions typedef uint64_t cp_crypto_id_t; 112*5e3eaea3SApple OSS Distributions 113*5e3eaea3SApple OSS Distributions typedef struct cprotect *cprotect_t; 114*5e3eaea3SApple OSS Distributions typedef struct cpx *cpx_t; 115*5e3eaea3SApple OSS Distributions 116*5e3eaea3SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE 117*5e3eaea3SApple OSS Distributions /* Not for consumption outside of XNU */ 118*5e3eaea3SApple OSS Distributions typedef uint32_t cpx_flags_t; 119*5e3eaea3SApple OSS Distributions /* 120*5e3eaea3SApple OSS Distributions * This is a CPX structure with a fixed-length key buffer. We need this defined in a header 121*5e3eaea3SApple OSS Distributions * so that we can use this structure to allocate the memory for the zone(s) properly. 122*5e3eaea3SApple OSS Distributions */ 123*5e3eaea3SApple OSS Distributions typedef struct fcpx { 124*5e3eaea3SApple OSS Distributions #ifdef DEBUG 125*5e3eaea3SApple OSS Distributions uint32_t cpx_magic1; 126*5e3eaea3SApple OSS Distributions #endif // DEBUG 127*5e3eaea3SApple OSS Distributions aes_encrypt_ctx *cpx_iv_aes_ctx_ptr;// Context used for generating the IV 128*5e3eaea3SApple OSS Distributions cpx_flags_t cpx_flags; 129*5e3eaea3SApple OSS Distributions uint16_t cpx_max_key_len; 130*5e3eaea3SApple OSS Distributions uint16_t cpx_key_len; 131*5e3eaea3SApple OSS Distributions uint8_t cpx_cached_key[VFS_CP_MAX_CACHEBUFLEN]; 132*5e3eaea3SApple OSS Distributions //Fixed length all the way through 133*5e3eaea3SApple OSS Distributions } fcpx_t; 134*5e3eaea3SApple OSS Distributions 135*5e3eaea3SApple OSS Distributions #endif // BSD_KERNEL_PRIVATE 136*5e3eaea3SApple OSS Distributions 137*5e3eaea3SApple OSS Distributions typedef struct cp_key { 138*5e3eaea3SApple OSS Distributions uint8_t len; 139*5e3eaea3SApple OSS Distributions void *key; 140*5e3eaea3SApple OSS Distributions } cp_key_t; 141*5e3eaea3SApple OSS Distributions 142*5e3eaea3SApple OSS Distributions /* Interface to AKS kext */ 143*5e3eaea3SApple OSS Distributions typedef struct { 144*5e3eaea3SApple OSS Distributions void *key; 145*5e3eaea3SApple OSS Distributions unsigned key_len; 146*5e3eaea3SApple OSS Distributions void *iv_key; 147*5e3eaea3SApple OSS Distributions unsigned iv_key_len; 148*5e3eaea3SApple OSS Distributions uint32_t flags; 149*5e3eaea3SApple OSS Distributions } cp_raw_key_s; 150*5e3eaea3SApple OSS Distributions 151*5e3eaea3SApple OSS Distributions typedef cp_raw_key_s* cp_raw_key_t; 152*5e3eaea3SApple OSS Distributions 153*5e3eaea3SApple OSS Distributions typedef struct { 154*5e3eaea3SApple OSS Distributions void *key; 155*5e3eaea3SApple OSS Distributions unsigned key_len; 156*5e3eaea3SApple OSS Distributions cp_key_class_t dp_class; 157*5e3eaea3SApple OSS Distributions } cp_wrapped_key_s; 158*5e3eaea3SApple OSS Distributions 159*5e3eaea3SApple OSS Distributions typedef cp_wrapped_key_s* cp_wrapped_key_t; 160*5e3eaea3SApple OSS Distributions 161*5e3eaea3SApple OSS Distributions typedef struct { 162*5e3eaea3SApple OSS Distributions union { 163*5e3eaea3SApple OSS Distributions ino64_t inode; 164*5e3eaea3SApple OSS Distributions cp_crypto_id_t crypto_id; 165*5e3eaea3SApple OSS Distributions }; 166*5e3eaea3SApple OSS Distributions uint32_t volume; 167*5e3eaea3SApple OSS Distributions pid_t pid; 168*5e3eaea3SApple OSS Distributions uid_t uid; 169*5e3eaea3SApple OSS Distributions cp_key_revision_t key_revision; 170*5e3eaea3SApple OSS Distributions } cp_cred_s; 171*5e3eaea3SApple OSS Distributions 172*5e3eaea3SApple OSS Distributions typedef cp_cred_s* cp_cred_t; 173*5e3eaea3SApple OSS Distributions 174*5e3eaea3SApple OSS Distributions /* The wrappers are invoked on the AKS kext */ 175*5e3eaea3SApple OSS Distributions typedef int unwrapper_t(cp_cred_t access, const cp_wrapped_key_t wrapped_key_in, cp_raw_key_t key_out); 176*5e3eaea3SApple OSS Distributions typedef int rewrapper_t(cp_cred_t access, cp_key_class_t dp_class, const cp_wrapped_key_t wrapped_key_in, cp_wrapped_key_t wrapped_key_out); 177*5e3eaea3SApple OSS Distributions typedef int new_key_t(cp_cred_t access, cp_key_class_t dp_class, cp_raw_key_t key_out, cp_wrapped_key_t wrapped_key_out); 178*5e3eaea3SApple OSS Distributions typedef int invalidater_t(cp_cred_t access); /* invalidates keys */ 179*5e3eaea3SApple OSS Distributions typedef int backup_key_t(cp_cred_t access, const cp_wrapped_key_t wrapped_key_in, cp_wrapped_key_t wrapped_key_out); 180*5e3eaea3SApple OSS Distributions 181*5e3eaea3SApple OSS Distributions /* 182*5e3eaea3SApple OSS Distributions * Flags for Interaction between AKS / Kernel 183*5e3eaea3SApple OSS Distributions * These are twiddled via the input/output structs in the above 184*5e3eaea3SApple OSS Distributions * wrapper/unwrapper functions. 185*5e3eaea3SApple OSS Distributions */ 186*5e3eaea3SApple OSS Distributions #define CP_RAW_KEY_WRAPPEDKEY 0x00000001 187*5e3eaea3SApple OSS Distributions 188*5e3eaea3SApple OSS Distributions /* 189*5e3eaea3SApple OSS Distributions * Function prototypes for kexts to interface with our internal cprotect 190*5e3eaea3SApple OSS Distributions * fields; cpx provides opacity and allows us to modify behavior internally 191*5e3eaea3SApple OSS Distributions * without requiring kext changes. 192*5e3eaea3SApple OSS Distributions */ 193*5e3eaea3SApple OSS Distributions cpx_t cpx_alloc(size_t key_size, bool needs_ctx); 194*5e3eaea3SApple OSS Distributions int cpx_alloc_ctx(cpx_t cpx); 195*5e3eaea3SApple OSS Distributions void cpx_free_ctx(cpx_t cpx); 196*5e3eaea3SApple OSS Distributions void cpx_init(cpx_t, size_t key_len); 197*5e3eaea3SApple OSS Distributions void cpx_init_ctx_ptr(cpx_t cpx); 198*5e3eaea3SApple OSS Distributions void cpx_free(cpx_t); 199*5e3eaea3SApple OSS Distributions void cpx_writeprotect(cpx_t cpx); 200*5e3eaea3SApple OSS Distributions __attribute__((const)) size_t cpx_size(size_t key_len); 201*5e3eaea3SApple OSS Distributions __attribute__((pure)) bool cpx_is_sep_wrapped_key(const struct cpx *); 202*5e3eaea3SApple OSS Distributions void cpx_set_is_sep_wrapped_key(struct cpx *, bool); 203*5e3eaea3SApple OSS Distributions __attribute__((pure)) bool cpx_is_composite_key(const struct cpx *); 204*5e3eaea3SApple OSS Distributions void cpx_set_is_composite_key(struct cpx *, bool); 205*5e3eaea3SApple OSS Distributions __attribute__((pure)) bool cpx_use_offset_for_iv(const struct cpx *); 206*5e3eaea3SApple OSS Distributions void cpx_set_use_offset_for_iv(struct cpx *, bool); 207*5e3eaea3SApple OSS Distributions __attribute__((pure)) bool cpx_synthetic_offset_for_iv(const struct cpx *); 208*5e3eaea3SApple OSS Distributions void cpx_set_synthetic_offset_for_iv(struct cpx *, bool); 209*5e3eaea3SApple OSS Distributions __attribute__((pure)) uint16_t cpx_key_len(const struct cpx *); 210*5e3eaea3SApple OSS Distributions void cpx_set_key_len(struct cpx *, uint16_t key_len); 211*5e3eaea3SApple OSS Distributions __attribute__((pure)) void *cpx_key(const struct cpx *); 212*5e3eaea3SApple OSS Distributions aes_encrypt_ctx *cpx_iv_aes_ctx(struct cpx *); 213*5e3eaea3SApple OSS Distributions void cpx_flush(cpx_t cpx); 214*5e3eaea3SApple OSS Distributions bool cpx_can_copy(const struct cpx *src, const struct cpx *dst); 215*5e3eaea3SApple OSS Distributions void cpx_copy(const struct cpx *src, cpx_t dst); 216*5e3eaea3SApple OSS Distributions uint16_t cpx_max_key_len(const struct cpx *cpx); 217*5e3eaea3SApple OSS Distributions bool cpx_has_key(const struct cpx *cpx); 218*5e3eaea3SApple OSS Distributions size_t cpx_sizex(const struct cpx *cpx); 219*5e3eaea3SApple OSS Distributions void cpx_set_aes_iv_key(struct cpx *cpx, void *iv_key); 220*5e3eaea3SApple OSS Distributions 221*5e3eaea3SApple OSS Distributions int cp_key_store_action(cp_key_store_action_t); 222*5e3eaea3SApple OSS Distributions int cp_key_store_action_for_volume(uuid_t volume_uuid, cp_key_store_action_t action); 223*5e3eaea3SApple OSS Distributions cp_key_os_version_t cp_os_version(void); 224*5e3eaea3SApple OSS Distributions // Should be cp_key_class_t but HFS has a conflicting definition 225*5e3eaea3SApple OSS Distributions int cp_is_valid_class(int isdir, int32_t protectionclass); 226*5e3eaea3SApple OSS Distributions 227*5e3eaea3SApple OSS Distributions __END_DECLS 228*5e3eaea3SApple OSS Distributions 229*5e3eaea3SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 230*5e3eaea3SApple OSS Distributions #endif /* !_SYS_CPROTECT_H_ */ 231