1*27b03b36SApple OSS Distributions /* 2*27b03b36SApple OSS Distributions * Copyright (c) 2009-2014 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 #ifndef _SYS_CPROTECT_H_ 30*27b03b36SApple OSS Distributions #define _SYS_CPROTECT_H_ 31*27b03b36SApple OSS Distributions 32*27b03b36SApple OSS Distributions #ifdef KERNEL_PRIVATE 33*27b03b36SApple OSS Distributions 34*27b03b36SApple OSS Distributions #include <sys/cdefs.h> 35*27b03b36SApple OSS Distributions #include <sys/param.h> 36*27b03b36SApple OSS Distributions #include <sys/buf.h> 37*27b03b36SApple OSS Distributions #include <sys/kdebug.h> 38*27b03b36SApple OSS Distributions #include <crypto/aes.h> 39*27b03b36SApple OSS Distributions #include <stdbool.h> 40*27b03b36SApple OSS Distributions #include <uuid/uuid.h> 41*27b03b36SApple OSS Distributions #include <libkern/crypto/sha1.h> 42*27b03b36SApple OSS Distributions 43*27b03b36SApple OSS Distributions __BEGIN_DECLS 44*27b03b36SApple OSS Distributions 45*27b03b36SApple OSS Distributions #define CP_CODE(code) FSDBG_CODE(DBG_CONTENT_PROT, code) 46*27b03b36SApple OSS Distributions /* 47*27b03b36SApple OSS Distributions * Class DBG_FSYSTEM == 0x03 48*27b03b36SApple OSS Distributions * Subclass DBG_CONTENT_PROT == 0xCF 49*27b03b36SApple OSS Distributions * These debug codes are of the form 0x03CFzzzz 50*27b03b36SApple OSS Distributions */ 51*27b03b36SApple OSS Distributions 52*27b03b36SApple OSS Distributions enum { 53*27b03b36SApple OSS Distributions CPDBG_OFFSET_IO = CP_CODE(0), /* 0x03CF0000 */ 54*27b03b36SApple OSS Distributions }; 55*27b03b36SApple OSS Distributions 56*27b03b36SApple OSS Distributions /* normally the debug events are no-ops */ 57*27b03b36SApple OSS Distributions #define CP_DEBUG(x, a, b, c, d, e) do {} while (0); 58*27b03b36SApple OSS Distributions 59*27b03b36SApple OSS Distributions /* dev kernels only! */ 60*27b03b36SApple OSS Distributions #if !SECURE_KERNEL 61*27b03b36SApple OSS Distributions 62*27b03b36SApple OSS Distributions /* KDEBUG events used by content protection subsystem */ 63*27b03b36SApple OSS Distributions #if 0 64*27b03b36SApple OSS Distributions #undef CP_DEBUG 65*27b03b36SApple OSS Distributions #define CP_DEBUG KERNEL_DEBUG_CONSTANT 66*27b03b36SApple OSS Distributions #endif 67*27b03b36SApple OSS Distributions 68*27b03b36SApple OSS Distributions #endif 69*27b03b36SApple OSS Distributions 70*27b03b36SApple OSS Distributions #define CP_MAX_WRAPPEDKEYSIZE 128 /* The size of the largest allowed key */ 71*27b03b36SApple OSS Distributions #define VFS_CP_MAX_CACHEBUFLEN 64 /* Maximum size of the cached key */ 72*27b03b36SApple OSS Distributions 73*27b03b36SApple OSS Distributions /* lock events from AppleKeyStore */ 74*27b03b36SApple OSS Distributions enum { 75*27b03b36SApple OSS Distributions CP_ACTION_LOCKED = 0, 76*27b03b36SApple OSS Distributions CP_ACTION_UNLOCKED = 1, 77*27b03b36SApple OSS Distributions }; 78*27b03b36SApple OSS Distributions /* 79*27b03b36SApple OSS Distributions * Ideally, cp_key_store_action_t would be an enum, but we cannot fix 80*27b03b36SApple OSS Distributions * that until AppleKeyStore is updated. 81*27b03b36SApple OSS Distributions */ 82*27b03b36SApple OSS Distributions typedef int cp_key_store_action_t; 83*27b03b36SApple OSS Distributions 84*27b03b36SApple OSS Distributions /* 85*27b03b36SApple OSS Distributions * It was once the case (and it may still be the case) where the lock 86*27b03b36SApple OSS Distributions * state got conflated with the possible actions/events that 87*27b03b36SApple OSS Distributions * AppleKeyStore can send. For that reason, the locked states below 88*27b03b36SApple OSS Distributions * should numerically match their corresponding actions above. 89*27b03b36SApple OSS Distributions */ 90*27b03b36SApple OSS Distributions typedef unsigned char cp_lock_state_t; 91*27b03b36SApple OSS Distributions enum { 92*27b03b36SApple OSS Distributions CP_LOCKED_STATE = 0, 93*27b03b36SApple OSS Distributions CP_UNLOCKED_STATE = 1, 94*27b03b36SApple OSS Distributions }; 95*27b03b36SApple OSS Distributions 96*27b03b36SApple OSS Distributions typedef uint32_t cp_key_class_t; 97*27b03b36SApple OSS Distributions typedef uint32_t cp_key_os_version_t; 98*27b03b36SApple OSS Distributions typedef uint16_t cp_key_revision_t; 99*27b03b36SApple OSS Distributions typedef uint64_t cp_crypto_id_t; 100*27b03b36SApple OSS Distributions 101*27b03b36SApple OSS Distributions typedef struct cprotect *cprotect_t; 102*27b03b36SApple OSS Distributions typedef struct cpx *cpx_t; 103*27b03b36SApple OSS Distributions 104*27b03b36SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE 105*27b03b36SApple OSS Distributions /* Not for consumption outside of XNU */ 106*27b03b36SApple OSS Distributions typedef uint32_t cpx_flags_t; 107*27b03b36SApple OSS Distributions /* 108*27b03b36SApple OSS Distributions * This is a CPX structure with a fixed-length key buffer. We need this defined in a header 109*27b03b36SApple OSS Distributions * so that we can use this structure to allocate the memory for the zone(s) properly. 110*27b03b36SApple OSS Distributions */ 111*27b03b36SApple OSS Distributions typedef struct fcpx { 112*27b03b36SApple OSS Distributions #ifdef DEBUG 113*27b03b36SApple OSS Distributions uint32_t cpx_magic1; 114*27b03b36SApple OSS Distributions #endif // DEBUG 115*27b03b36SApple OSS Distributions aes_encrypt_ctx *cpx_iv_aes_ctx_ptr;// Context used for generating the IV 116*27b03b36SApple OSS Distributions cpx_flags_t cpx_flags; 117*27b03b36SApple OSS Distributions uint16_t cpx_max_key_len; 118*27b03b36SApple OSS Distributions uint16_t cpx_key_len; 119*27b03b36SApple OSS Distributions uint8_t cpx_cached_key[VFS_CP_MAX_CACHEBUFLEN]; 120*27b03b36SApple OSS Distributions //Fixed length all the way through 121*27b03b36SApple OSS Distributions } fcpx_t; 122*27b03b36SApple OSS Distributions 123*27b03b36SApple OSS Distributions #endif // BSD_KERNEL_PRIVATE 124*27b03b36SApple OSS Distributions 125*27b03b36SApple OSS Distributions typedef struct cp_key { 126*27b03b36SApple OSS Distributions uint8_t len; 127*27b03b36SApple OSS Distributions void *key; 128*27b03b36SApple OSS Distributions } cp_key_t; 129*27b03b36SApple OSS Distributions 130*27b03b36SApple OSS Distributions /* Interface to AKS kext */ 131*27b03b36SApple OSS Distributions typedef struct { 132*27b03b36SApple OSS Distributions void *key; 133*27b03b36SApple OSS Distributions unsigned key_len; 134*27b03b36SApple OSS Distributions void *iv_key; 135*27b03b36SApple OSS Distributions unsigned iv_key_len; 136*27b03b36SApple OSS Distributions uint32_t flags; 137*27b03b36SApple OSS Distributions } cp_raw_key_s; 138*27b03b36SApple OSS Distributions 139*27b03b36SApple OSS Distributions typedef cp_raw_key_s* cp_raw_key_t; 140*27b03b36SApple OSS Distributions 141*27b03b36SApple OSS Distributions typedef struct { 142*27b03b36SApple OSS Distributions void *key; 143*27b03b36SApple OSS Distributions unsigned key_len; 144*27b03b36SApple OSS Distributions cp_key_class_t dp_class; 145*27b03b36SApple OSS Distributions } cp_wrapped_key_s; 146*27b03b36SApple OSS Distributions 147*27b03b36SApple OSS Distributions typedef cp_wrapped_key_s* cp_wrapped_key_t; 148*27b03b36SApple OSS Distributions 149*27b03b36SApple OSS Distributions typedef struct { 150*27b03b36SApple OSS Distributions union { 151*27b03b36SApple OSS Distributions ino64_t inode; 152*27b03b36SApple OSS Distributions cp_crypto_id_t crypto_id; 153*27b03b36SApple OSS Distributions }; 154*27b03b36SApple OSS Distributions uint32_t volume; 155*27b03b36SApple OSS Distributions pid_t pid; 156*27b03b36SApple OSS Distributions uid_t uid; 157*27b03b36SApple OSS Distributions cp_key_revision_t key_revision; 158*27b03b36SApple OSS Distributions } cp_cred_s; 159*27b03b36SApple OSS Distributions 160*27b03b36SApple OSS Distributions typedef cp_cred_s* cp_cred_t; 161*27b03b36SApple OSS Distributions 162*27b03b36SApple OSS Distributions /* The wrappers are invoked on the AKS kext */ 163*27b03b36SApple OSS Distributions typedef int unwrapper_t(cp_cred_t access, const cp_wrapped_key_t wrapped_key_in, cp_raw_key_t key_out); 164*27b03b36SApple 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); 165*27b03b36SApple 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); 166*27b03b36SApple OSS Distributions typedef int invalidater_t(cp_cred_t access); /* invalidates keys */ 167*27b03b36SApple 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); 168*27b03b36SApple OSS Distributions 169*27b03b36SApple OSS Distributions /* 170*27b03b36SApple OSS Distributions * Flags for Interaction between AKS / Kernel 171*27b03b36SApple OSS Distributions * These are twiddled via the input/output structs in the above 172*27b03b36SApple OSS Distributions * wrapper/unwrapper functions. 173*27b03b36SApple OSS Distributions */ 174*27b03b36SApple OSS Distributions #define CP_RAW_KEY_WRAPPEDKEY 0x00000001 175*27b03b36SApple OSS Distributions 176*27b03b36SApple OSS Distributions /* 177*27b03b36SApple OSS Distributions * Function prototypes for kexts to interface with our internal cprotect 178*27b03b36SApple OSS Distributions * fields; cpx provides opacity and allows us to modify behavior internally 179*27b03b36SApple OSS Distributions * without requiring kext changes. 180*27b03b36SApple OSS Distributions */ 181*27b03b36SApple OSS Distributions cpx_t cpx_alloc(size_t key_size, bool needs_ctx); 182*27b03b36SApple OSS Distributions int cpx_alloc_ctx(cpx_t cpx); 183*27b03b36SApple OSS Distributions void cpx_free_ctx(cpx_t cpx); 184*27b03b36SApple OSS Distributions void cpx_init(cpx_t, size_t key_len); 185*27b03b36SApple OSS Distributions void cpx_init_ctx_ptr(cpx_t cpx); 186*27b03b36SApple OSS Distributions void cpx_free(cpx_t); 187*27b03b36SApple OSS Distributions void cpx_writeprotect(cpx_t cpx); 188*27b03b36SApple OSS Distributions __attribute__((const)) size_t cpx_size(size_t key_len); 189*27b03b36SApple OSS Distributions __attribute__((pure)) bool cpx_is_sep_wrapped_key(const struct cpx *); 190*27b03b36SApple OSS Distributions void cpx_set_is_sep_wrapped_key(struct cpx *, bool); 191*27b03b36SApple OSS Distributions __attribute__((pure)) bool cpx_is_composite_key(const struct cpx *); 192*27b03b36SApple OSS Distributions void cpx_set_is_composite_key(struct cpx *, bool); 193*27b03b36SApple OSS Distributions __attribute__((pure)) bool cpx_use_offset_for_iv(const struct cpx *); 194*27b03b36SApple OSS Distributions void cpx_set_use_offset_for_iv(struct cpx *, bool); 195*27b03b36SApple OSS Distributions __attribute__((pure)) bool cpx_synthetic_offset_for_iv(const struct cpx *); 196*27b03b36SApple OSS Distributions void cpx_set_synthetic_offset_for_iv(struct cpx *, bool); 197*27b03b36SApple OSS Distributions __attribute__((pure)) uint16_t cpx_key_len(const struct cpx *); 198*27b03b36SApple OSS Distributions void cpx_set_key_len(struct cpx *, uint16_t key_len); 199*27b03b36SApple OSS Distributions __attribute__((pure)) void *cpx_key(const struct cpx *); 200*27b03b36SApple OSS Distributions aes_encrypt_ctx *cpx_iv_aes_ctx(struct cpx *); 201*27b03b36SApple OSS Distributions void cpx_flush(cpx_t cpx); 202*27b03b36SApple OSS Distributions bool cpx_can_copy(const struct cpx *src, const struct cpx *dst); 203*27b03b36SApple OSS Distributions void cpx_copy(const struct cpx *src, cpx_t dst); 204*27b03b36SApple OSS Distributions uint16_t cpx_max_key_len(const struct cpx *cpx); 205*27b03b36SApple OSS Distributions bool cpx_has_key(const struct cpx *cpx); 206*27b03b36SApple OSS Distributions size_t cpx_sizex(const struct cpx *cpx); 207*27b03b36SApple OSS Distributions void cpx_set_aes_iv_key(struct cpx *cpx, void *iv_key); 208*27b03b36SApple OSS Distributions 209*27b03b36SApple OSS Distributions int cp_key_store_action(cp_key_store_action_t); 210*27b03b36SApple OSS Distributions int cp_key_store_action_for_volume(uuid_t volume_uuid, cp_key_store_action_t action); 211*27b03b36SApple OSS Distributions cp_key_os_version_t cp_os_version(void); 212*27b03b36SApple OSS Distributions // Should be cp_key_class_t but HFS has a conflicting definition 213*27b03b36SApple OSS Distributions int cp_is_valid_class(int isdir, int32_t protectionclass); 214*27b03b36SApple OSS Distributions 215*27b03b36SApple OSS Distributions __END_DECLS 216*27b03b36SApple OSS Distributions 217*27b03b36SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 218*27b03b36SApple OSS Distributions #endif /* !_SYS_CPROTECT_H_ */ 219