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