]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/requirement.h
da9175d40ea9728dbe954f18351b573ee9dfdd5a
[apple/security.git] / OSX / libsecurity_codesigning / lib / requirement.h
1 /*
2 * Copyright (c) 2006-2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // requirement - Code Requirement Blob description
26 //
27 #ifndef _H_REQUIREMENT
28 #define _H_REQUIREMENT
29
30 #include <security_utilities/blob.h>
31 #include <security_utilities/superblob.h>
32 #include <security_utilities/hashing.h>
33 #include <Security/CodeSigning.h>
34 #include "codedirectory.h"
35 #include <map>
36
37 namespace Security {
38 namespace CodeSigning {
39
40
41 //
42 // Single requirement.
43 // This is a contiguous binary blob, starting with this header
44 // and followed by binary expr-code. All links within the blob
45 // are offset-relative to the start of the header.
46 // This is designed to be a binary stable format. Note that we restrict
47 // outselves to 4GB maximum size (4 byte size/offset), and we expect real
48 // Requirement blobs to be fairly small (a few kilobytes at most).
49 //
50 // The "kind" field allows for adding different kinds of Requirements altogether
51 // in the future. We expect to stay within the framework of "opExpr" requirements,
52 // but it never hurts to have a way out.
53 //
54 class Requirement: public Blob<Requirement, 0xfade0c00> {
55 public:
56 class Maker; // makes Requirement blobs
57 class Context; // evaluation context
58 class Reader; // structured reader
59 class Interpreter; // evaluation engine
60
61 // different forms of Requirements. Right now, we only support exprForm ("opExprs")
62 enum Kind {
63 exprForm = 1 // prefix expr form
64 };
65
66 void kind(Kind k) { mKind = k; }
67 Kind kind() const { return Kind(uint32_t(mKind)); }
68
69 // validate this requirement against a code context
70 void validate(const Context &ctx, OSStatus failure = errSecCSReqFailed) const; // throws on all failures
71 bool validates(const Context &ctx, OSStatus failure = errSecCSReqFailed) const; // returns on clean miss
72
73 // certificate positions (within a standard certificate chain)
74 static const int leafCert = 0; // index for leaf (first in chain)
75 static const int anchorCert = -1; // index for anchor (last in chain)
76
77 #if defined(TEST_APPLE_ANCHOR)
78 static const char testAppleAnchorEnv[];
79 static const SHA1::Digest &testAppleAnchorHash();
80 #endif //TEST_APPLE_ANCHOR
81
82 // common alignment rule for all requirement forms
83 static const size_t baseAlignment = sizeof(uint32_t); // (we might as well say "four")
84
85 // canonical (source) names of Requirement types (matched to SecRequirementType in CSCommon.h)
86 static const char *const typeNames[];
87 #if TARGET_OS_OSX
88 IFDUMP(void dump() const);
89 #endif
90
91 private:
92 Endian<uint32_t> mKind; // expression kind
93 };
94
95
96 //
97 // An interpretation context
98 //
99 class Requirement::Context {
100 protected:
101 Context()
102 : certs(NULL), info(NULL), entitlements(NULL), identifier(""), directory(NULL) { }
103
104 public:
105 Context(CFArrayRef certChain, CFDictionaryRef infoDict, CFDictionaryRef entitlementDict,
106 const std::string &ident, const CodeDirectory *dir)
107 : certs(certChain), info(infoDict), entitlements(entitlementDict), identifier(ident), directory(dir) { }
108
109 CFArrayRef certs; // certificate chain
110 CFDictionaryRef info; // Info.plist
111 CFDictionaryRef entitlements; // entitlement plist
112 std::string identifier; // signing identifier
113 const CodeDirectory *directory; // CodeDirectory
114
115 SecCertificateRef cert(int ix) const; // get a cert from the cert chain (NULL if not found)
116 unsigned int certCount() const; // length of cert chain (including root)
117 };
118
119
120 //
121 // exprForm opcodes.
122 //
123 // Opcodes are broken into flags in the (HBO) high byte, and an opcode value
124 // in the remaining 24 bits. Note that opcodes will remain fairly small
125 // (almost certainly <60000), so we have the third byte to play around with
126 // in the future, if needed. For now, small opcodes effective reserve this byte
127 // as zero.
128 // The flag byte allows for limited understanding of unknown opcodes. It allows
129 // the interpreter to use the known opcode parts of the program while semi-creatively
130 // disregarding the parts it doesn't know about. An unrecognized opcode with zero
131 // flag byte causes evaluation to categorically fail, since the semantics of such
132 // an opcode cannot safely be predicted.
133 //
134 enum {
135 // semantic bits or'ed into the opcode
136 opFlagMask = 0xFF000000, // high bit flags
137 opGenericFalse = 0x80000000, // has size field; okay to default to false
138 opGenericSkip = 0x40000000, // has size field; skip and continue
139 };
140
141 enum ExprOp {
142 opFalse, // unconditionally false
143 opTrue, // unconditionally true
144 opIdent, // match canonical code [string]
145 opAppleAnchor, // signed by Apple as Apple's product
146 opAnchorHash, // match anchor [cert hash]
147 opInfoKeyValue, // *legacy* - use opInfoKeyField [key; value]
148 opAnd, // binary prefix expr AND expr [expr; expr]
149 opOr, // binary prefix expr OR expr [expr; expr]
150 opCDHash, // match hash of CodeDirectory directly [cd hash]
151 opNot, // logical inverse [expr]
152 opInfoKeyField, // Info.plist key field [string; match suffix]
153 opCertField, // Certificate field [cert index; field name; match suffix]
154 opTrustedCert, // require trust settings to approve one particular cert [cert index]
155 opTrustedCerts, // require trust settings to approve the cert chain
156 opCertGeneric, // Certificate component by OID [cert index; oid; match suffix]
157 opAppleGenericAnchor, // signed by Apple in any capacity
158 opEntitlementField, // entitlement dictionary field [string; match suffix]
159 opCertPolicy, // Certificate policy by OID [cert index; oid; match suffix]
160 opNamedAnchor, // named anchor type
161 opNamedCode, // named subroutine
162 opPlatform, // platform constraint [integer]
163 exprOpCount // (total opcode count in use)
164 };
165
166 // match suffix opcodes
167 enum MatchOperation {
168 matchExists, // anything but explicit "false" - no value stored
169 matchEqual, // equal (CFEqual)
170 matchContains, // partial match (substring)
171 matchBeginsWith, // partial match (initial substring)
172 matchEndsWith, // partial match (terminal substring)
173 matchLessThan, // less than (string with numeric comparison)
174 matchGreaterThan, // greater than (string with numeric comparison)
175 matchLessEqual, // less or equal (string with numeric comparison)
176 matchGreaterEqual, // greater or equal (string with numeric comparison)
177 };
178
179
180 //
181 // We keep Requirement groups in SuperBlobs, indexed by SecRequirementType
182 //
183 typedef SuperBlob<0xfade0c01> Requirements;
184
185
186 //
187 // Byte order flippers
188 //
189 inline CodeSigning::ExprOp h2n(CodeSigning::ExprOp op)
190 {
191 uint32_t intOp = (uint32_t) op;
192 return (CodeSigning::ExprOp) ::h2n(intOp);
193 }
194
195 inline CodeSigning::ExprOp n2h(CodeSigning::ExprOp op)
196 {
197 uint32_t intOp = (uint32_t) op;
198 return (CodeSigning::ExprOp) ::n2h(intOp);
199 }
200
201
202 inline CodeSigning::MatchOperation h2n(CodeSigning::MatchOperation op)
203 {
204 return CodeSigning::MatchOperation(::h2n((uint32_t) op));
205 }
206
207 inline CodeSigning::MatchOperation n2h(CodeSigning::MatchOperation op)
208 {
209 return CodeSigning::MatchOperation(::n2h((uint32_t) op));
210 }
211
212
213 } // CodeSigning
214 } // Security
215
216 #endif //_H_REQUIREMENT