1/*
2 * Copyright (c) 2006 Apple Computer, 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_CODESIGN_H_
30#define _SYS_CODESIGN_H_
31
32/* code signing attributes of a process */
33#define	CS_VALID		0x0000001	/* dynamically valid */
34#define CS_ADHOC		0x0000002	/* ad hoc signed */
35#define CS_GET_TASK_ALLOW	0x0000004	/* has get-task-allow entitlement */
36#define CS_INSTALLER		0x0000008	/* has installer entitlement */
37
38#define	CS_HARD			0x0000100	/* don't load invalid pages */
39#define	CS_KILL			0x0000200	/* kill process if it becomes invalid */
40#define CS_CHECK_EXPIRATION	0x0000400	/* force expiration checking */
41#define CS_RESTRICT		0x0000800	/* tell dyld to treat restricted */
42#define CS_ENFORCEMENT		0x0001000	/* require enforcement */
43#define CS_REQUIRE_LV		0x0002000	/* require library validation */
44
45#define	CS_ALLOWED_MACHO	0x00ffffe
46
47#define CS_EXEC_SET_HARD	0x0100000	/* set CS_HARD on any exec'ed process */
48#define CS_EXEC_SET_KILL	0x0200000	/* set CS_KILL on any exec'ed process */
49#define CS_EXEC_SET_ENFORCEMENT	0x0400000	/* set CS_ENFORCEMENT on any exec'ed process */
50#define CS_EXEC_SET_INSTALLER	0x0800000	/* set CS_INSTALLER on any exec'ed process */
51
52#define CS_KILLED		0x1000000	/* was killed by kernel for invalidity */
53#define CS_DYLD_PLATFORM	0x2000000	/* dyld used to load this is a platform binary */
54
55#define CS_ENTITLEMENT_FLAGS	(CS_GET_TASK_ALLOW | CS_INSTALLER)
56
57/* csops  operations */
58#define	CS_OPS_STATUS		0	/* return status */
59#define	CS_OPS_MARKINVALID	1	/* invalidate process */
60#define	CS_OPS_MARKHARD		2	/* set HARD flag */
61#define	CS_OPS_MARKKILL		3	/* set KILL flag (sticky) */
62#ifdef KERNEL_PRIVATE
63/* CS_OPS_PIDPATH		4	*/
64#endif
65#define	CS_OPS_CDHASH		5	/* get code directory hash */
66#define CS_OPS_PIDOFFSET	6	/* get offset of active Mach-o slice */
67#define CS_OPS_ENTITLEMENTS_BLOB 7	/* get entitlements blob */
68#define CS_OPS_MARKRESTRICT	8	/* set RESTRICT flag (sticky) */
69#define CS_OPS_SET_STATUS	9	/* set codesign flags */
70#define CS_OPS_BLOB		10	/* get codesign blob */
71#define CS_OPS_IDENTITY		11	/* get codesign identity */
72
73/* SigPUP */
74#define CS_OPS_SIGPUP_INSTALL	20
75#define CS_OPS_SIGPUP_DROP	21
76#define CS_OPS_SIGPUP_VALIDATE	22
77
78struct sigpup_install_table {
79	uint64_t data;
80	uint64_t length;
81	uint64_t path;
82};
83
84
85/*
86 * Magic numbers used by Code Signing
87 */
88enum {
89	CSMAGIC_REQUIREMENT = 0xfade0c00,		/* single Requirement blob */
90	CSMAGIC_REQUIREMENTS = 0xfade0c01,		/* Requirements vector (internal requirements) */
91	CSMAGIC_CODEDIRECTORY = 0xfade0c02,		/* CodeDirectory blob */
92	CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
93	CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02,	/* XXX */
94	CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171,	/* embedded entitlements */
95	CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1, /* multi-arch collection of embedded signatures */
96	CSMAGIC_BLOBWRAPPER = 0xfade0b01,	/* CMS Signature, among other things */
97
98	CS_SUPPORTSSCATTER = 0x20100,
99	CS_SUPPORTSTEAMID = 0x20200,
100
101	CSSLOT_CODEDIRECTORY = 0,				/* slot index for CodeDirectory */
102	CSSLOT_INFOSLOT = 1,
103	CSSLOT_REQUIREMENTS = 2,
104	CSSLOT_RESOURCEDIR = 3,
105	CSSLOT_APPLICATION = 4,
106	CSSLOT_ENTITLEMENTS = 5,
107
108	CSSLOT_SIGNATURESLOT = 0x10000,			/* CMS Signature */
109
110	CSTYPE_INDEX_REQUIREMENTS = 0x00000002,		/* compat with amfi */
111	CSTYPE_INDEX_ENTITLEMENTS = 0x00000005,		/* compat with amfi */
112
113	CS_HASHTYPE_SHA1 = 1
114};
115
116
117#define KERNEL_HAVE_CS_CODEDIRECTORY 1
118
119/*
120 * C form of a CodeDirectory.
121 */
122typedef struct __CodeDirectory {
123	uint32_t magic;					/* magic number (CSMAGIC_CODEDIRECTORY) */
124	uint32_t length;				/* total length of CodeDirectory blob */
125	uint32_t version;				/* compatibility version */
126	uint32_t flags;					/* setup and mode flags */
127	uint32_t hashOffset;			/* offset of hash slot element at index zero */
128	uint32_t identOffset;			/* offset of identifier string */
129	uint32_t nSpecialSlots;			/* number of special hash slots */
130	uint32_t nCodeSlots;			/* number of ordinary (code) hash slots */
131	uint32_t codeLimit;				/* limit to main image signature range */
132	uint8_t hashSize;				/* size of each hash in bytes */
133	uint8_t hashType;				/* type of hash (cdHashType* constants) */
134	uint8_t spare1;					/* unused (must be zero) */
135	uint8_t	pageSize;				/* log2(page size in bytes); 0 => infinite */
136	uint32_t spare2;				/* unused (must be zero) */
137	/* Version 0x20100 */
138	uint32_t scatterOffset;				/* offset of optional scatter vector */
139	/* Version 0x20200 */
140	uint32_t teamOffset;				/* offset of optional team identifier */
141	/* followed by dynamic content as located by offset fields above */
142} CS_CodeDirectory;
143
144/*
145 * Structure of an embedded-signature SuperBlob
146 */
147
148typedef struct __BlobIndex {
149	uint32_t type;					/* type of entry */
150	uint32_t offset;				/* offset of entry */
151} CS_BlobIndex;
152
153typedef struct __SC_SuperBlob {
154	uint32_t magic;					/* magic number */
155	uint32_t length;				/* total length of SuperBlob */
156	uint32_t count;					/* number of index entries following */
157	CS_BlobIndex index[];			/* (count) entries */
158	/* followed by Blobs in no particular order as indicated by offsets in index */
159} CS_SuperBlob;
160
161typedef struct __SC_GenericBlob {
162	uint32_t magic;				/* magic number */
163	uint32_t length;			/* total length of blob */
164	char data[];
165} CS_GenericBlob;
166
167typedef struct __SC_Scatter {
168	uint32_t count;			// number of pages; zero for sentinel (only)
169	uint32_t base;			// first page number
170	uint64_t targetOffset;		// offset in target
171	uint64_t spare;			// reserved
172} SC_Scatter;
173
174
175#ifndef KERNEL
176
177#include <sys/types.h>
178#include <mach/message.h>
179
180__BEGIN_DECLS
181/* code sign operations */
182int csops(pid_t pid, unsigned int  ops, void * useraddr, size_t usersize);
183int csops_audittoken(pid_t pid, unsigned int  ops, void * useraddr, size_t usersize, audit_token_t * token);
184__END_DECLS
185
186#else /* !KERNEL */
187
188#include <sys/cdefs.h>
189#include <sys/_types/_off_t.h>
190
191struct vnode;
192struct cs_blob;
193struct fileglob;
194
195struct cscsr_functions  {
196	int		csr_version;
197#define CSCSR_VERSION 1
198	int		(*csr_validate_header)(const uint8_t *, size_t);
199	const void*	(*csr_find_file_codedirectory)(struct vnode *, const uint8_t *, size_t, size_t *);
200};
201
202__BEGIN_DECLS
203int	cs_enforcement(struct proc *);
204int	cs_require_lv(struct proc *);
205uint32_t cs_entitlement_flags(struct proc *p);
206int	cs_entitlements_blob_get(struct proc *, void **, size_t *);
207uint8_t * cs_get_cdhash(struct proc *);
208void	cs_register_cscsr(struct cscsr_functions *);
209
210const 	CS_GenericBlob *
211	cs_find_blob(struct cs_blob *, uint32_t, uint32_t);
212
213const 	char * csblob_get_teamid(struct cs_blob *);
214const 	char * csproc_get_teamid(struct proc *);
215const 	char * csvnode_get_teamid(struct vnode *, off_t);
216int 	csproc_get_platform_binary(struct proc *);
217const 	char * csfg_get_teamid(struct fileglob *);
218int	csfg_get_path(struct fileglob *, char *, int *);
219int 	csfg_get_platform_binary(struct fileglob *);
220
221__END_DECLS
222
223#ifdef XNU_KERNEL_PRIVATE
224
225void	cs_init(void);
226int	cs_allow_invalid(struct proc *);
227int	cs_invalid_page(addr64_t);
228int	sigpup_install(user_addr_t);
229int	sigpup_drop(void);
230
231extern int cs_debug;
232extern int cs_validation;
233#if !SECURE_KERNEL
234extern int cs_enforcement_panic;
235#endif
236
237#endif /* XNU_KERNEL_PRIVATE */
238
239#endif /* KERNEL */
240
241#endif /* _SYS_CODESIGN_H_ */
242