]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/cprotect.h
xnu-3248.40.184.tar.gz
[apple/xnu.git] / bsd / sys / cprotect.h
1 /*
2 * Copyright (c) 2009-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _SYS_CPROTECT_H_
30 #define _SYS_CPROTECT_H_
31
32 #if KERNEL_PRIVATE
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/buf.h>
37 #include <sys/kdebug.h>
38 #include <crypto/aes.h>
39 #include <stdbool.h>
40
41 __BEGIN_DECLS
42
43 #define CP_CODE(code) FSDBG_CODE(DBG_CONTENT_PROT, code)
44 /*
45 * Class DBG_FSYSTEM == 0x03
46 * Subclass DBG_CONTENT_PROT == 0xCF
47 * These debug codes are of the form 0x03CFzzzz
48 */
49
50 enum {
51 CPDBG_OFFSET_IO = CP_CODE(0), /* 0x03CF0000 */
52 };
53
54 /* normally the debug events are no-ops */
55 #define CP_DEBUG(x,a,b,c,d,e) do {} while (0);
56
57 /* dev kernels only! */
58 #if !SECURE_KERNEL
59
60 /* KDEBUG events used by content protection subsystem */
61 #if 0
62 #undef CP_DEBUG
63 #define CP_DEBUG KERNEL_DEBUG_CONSTANT
64 #endif
65
66 #endif
67
68 #define CP_MAX_WRAPPEDKEYSIZE 128 /* The size of the largest allowed key */
69
70 /* lock events from AppleKeyStore */
71 #define CP_LOCKED_STATE 0 /* Device is locked */
72 #define CP_UNLOCKED_STATE 1 /* Device is unlocked */
73
74 #define CP_MAX_STATE 1 /* uint8_t ; maximum # of states is 255 */
75
76 typedef struct cprotect *cprotect_t;
77 typedef struct cp_wrap_func *cp_wrap_func_t;
78 typedef struct cpx *cpx_t;
79
80 /* Structures passed between HFS and AKS kext */
81 typedef struct {
82 void *key;
83 unsigned key_len;
84 void *iv_key;
85 unsigned iv_key_len;
86 uint32_t flags;
87 } cp_raw_key_s;
88
89 typedef cp_raw_key_s* cp_raw_key_t;
90
91 typedef struct {
92 void *key;
93 unsigned key_len;
94 uint32_t dp_class;
95 } cp_wrapped_key_s;
96
97 typedef cp_wrapped_key_s* cp_wrapped_key_t;
98
99 typedef uint16_t cp_key_revision_t;
100
101 typedef struct {
102 ino64_t inode;
103 uint32_t volume;
104 pid_t pid;
105 uid_t uid;
106 cp_key_revision_t key_revision;
107 } cp_cred_s;
108
109 typedef cp_cred_s* cp_cred_t;
110
111 /* The wrappers are invoked on the AKS kext */
112 typedef int unwrapper_t(cp_cred_t access, const cp_wrapped_key_t wrapped_key_in, cp_raw_key_t key_out);
113 typedef int rewrapper_t(cp_cred_t access, uint32_t dp_class, const cp_wrapped_key_t wrapped_key_in, cp_wrapped_key_t wrapped_key_out);
114 typedef int new_key_t(cp_cred_t access, uint32_t dp_class, cp_raw_key_t key_out, cp_wrapped_key_t wrapped_key_out);
115 typedef int invalidater_t(cp_cred_t access); /* invalidates keys */
116 typedef int backup_key_t(cp_cred_t access, const cp_wrapped_key_t wrapped_key_in, cp_wrapped_key_t wrapped_key_out);
117
118
119 /*
120 * Flags for Interaction between AKS / Kernel
121 * These are twiddled via the input/output structs in the above
122 * wrapper/unwrapper functions.
123 */
124 #define CP_RAW_KEY_WRAPPEDKEY 0x00000001
125
126 /*
127 * Function prototypes for kexts to interface with our internal cprotect
128 * fields; cpx provides opacity and allows us to modify behavior internally
129 * without requiring kext changes.
130 */
131 cpx_t cpx_alloc(size_t key_size);
132 void cpx_free(cpx_t);
133 __attribute__((const)) size_t cpx_size(size_t key_size);
134 __attribute__((pure)) bool cpx_is_sep_wrapped_key(const struct cpx *);
135 void cpx_set_is_sep_wrapped_key(struct cpx *, bool);
136 __attribute__((pure)) bool cpx_use_offset_for_iv(const struct cpx *);
137 void cpx_set_use_offset_for_iv(struct cpx *, bool);
138 __attribute__((pure)) uint16_t cpx_key_len(const struct cpx *);
139 void cpx_set_key_len(struct cpx *, uint16_t key_len);
140 __attribute__((pure)) void *cpx_key(const struct cpx *);
141 aes_encrypt_ctx *cpx_iv_aes_ctx(struct cpx *);
142
143 /* Structure to store pointers for AKS functions */
144 struct cp_wrap_func {
145 new_key_t *new_key;
146 unwrapper_t *unwrapper;
147 rewrapper_t *rewrapper;
148 invalidater_t *invalidater;
149 backup_key_t *backup_key;
150 };
151
152 int cp_key_store_action(int);
153 int cp_register_wraps(cp_wrap_func_t);
154
155 #ifdef BSD_KERNEL_PRIVATE
156
157 /*
158 * Declarations that are not exported from the kernel but are used by
159 * VFS to call into the implementation (i.e. HFS) should be here.
160 */
161
162 /* Content Protection VNOP Operation flags */
163 #define CP_READ_ACCESS 0x1
164 #define CP_WRITE_ACCESS 0x2
165
166 /*
167 * Functions to check the status of a CP and to query
168 * the containing filesystem to see if it is supported.
169 */
170 struct vnode;
171 struct hfsmount;
172
173 int cp_vnode_getclass(struct vnode *, int *);
174 int cp_vnode_setclass(struct vnode *, uint32_t);
175 int cp_vnode_transcode(struct vnode * vp, void *key, unsigned *len);
176
177 int cp_handle_vnop(struct vnode *, int, int);
178 int cp_handle_open(struct vnode *vp, int mode);
179 int cp_get_root_major_vers (struct vnode *vp, uint32_t *level);
180 int cp_get_default_level (struct vnode *vp, uint32_t *level);
181 int cp_is_valid_class (int isdir, int32_t protectionclass);
182 int cp_set_trimmed(struct hfsmount *hfsmp);
183 int cp_set_rewrapped(struct hfsmount *hfsmp);
184 int cp_flop_generation (struct hfsmount *hfsmp);
185
186 #endif /* BSD_KERNEL_PRIVATE */
187
188 __END_DECLS
189
190 #endif /* KERNEL_PRIVATE */
191 #endif /* !_SYS_CPROTECT_H_ */