]>
git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/requirement.h
2 * Copyright (c) 2006-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 // requirement - Code Requirement Blob description
27 #ifndef _H_REQUIREMENT
28 #define _H_REQUIREMENT
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"
38 namespace CodeSigning
{
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).
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.
54 class Requirement
: public Blob
<Requirement
, 0xfade0c00> {
56 class Maker
; // makes Requirement blobs
57 class Context
; // evaluation context
58 class Reader
; // structured reader
59 class Interpreter
; // evaluation engine
61 // different forms of Requirements. Right now, we only support exprForm ("opExprs")
63 exprForm
= 1 // postfix expr form
66 void kind(Kind k
) { mKind
= k
; }
67 Kind
kind() const { return Kind(uint32_t(mKind
)); }
69 // validate this requirement against a code context
70 void validate(const Context
&ctx
, OSStatus failure
= errSecCSReqFailed
) const;
72 // certificate positions (within a standard certificate chain)
73 static const int leafCert
= 0; // index for leaf (first in chain)
74 static const int anchorCert
= -1; // index for anchor (last in chain)
76 // the SHA1 hash of the canonical "Apple Anchor", i.e. the X509 Anchor
77 // that is considered "Apple's anchor certificate", as defined by hashOfCertificate().
78 static const SHA1::Digest
&appleAnchorHash();
79 #if defined(TEST_APPLE_ANCHOR)
80 static const char testAppleAnchorEnv
[];
81 static const SHA1::Digest
&testAppleAnchorHash();
82 #endif //TEST_APPLE_ANCHOR
84 // common alignment rule for all requirement forms
85 static const size_t baseAlignment
= sizeof(uint32_t); // (we might as well say "four")
87 // canonical (source) names of Requirement types (matched to SecRequirementType in CSCommon.h)
88 static const char *const typeNames
[];
90 IFDUMP(void dump() const);
93 Endian
<uint32_t> mKind
; // expression kind
98 // An interpretation context
100 struct Requirement::Context
{
101 Context(CFArrayRef certChain
, CFDictionaryRef infoDict
, const CodeDirectory
*dir
)
102 : certs(certChain
), info(infoDict
), directory(dir
) { }
104 const CFArrayRef certs
;
105 const CFDictionaryRef info
;
106 const CodeDirectory
* const directory
;
108 SecCertificateRef
cert(int ix
) const; // get a cert from the cert chain
109 unsigned int certCount() const; // length of cert chain
116 // Opcodes are broken into flags in the (HBO) high byte, and an opcode value
117 // in the remaining 24 bits. Note that opcodes will remain fairly small
118 // (almost certainly <60000), so we have the third byte to play around with
119 // in the future, if needed. For now, small opcodes effective reserve this byte
121 // The flag byte allows for limited understanding of unknown opcodes. It allows
122 // the interpreter to use the known opcode parts of the program while semi-creatively
123 // disregarding the parts it doesn't know about. An unrecognized opcode with zero
124 // flag byte causes evaluation to categorically fail, since the semantics of such
125 // an opcode cannot safely be predicted.
128 // semantic bits or'ed into the opcode
129 opFlagMask
= 0xFF000000, // high bit flags
130 opGenericFalse
= 0x80000000, // has size field; okay to default to false
131 opGenericSkip
= 0x40000000, // has size field; skip and continue
135 opFalse
, // unconditionally false
136 opTrue
, // unconditionally true
137 opIdent
, // match canonical code [string]
138 opAppleAnchor
, // match apple anchor
139 opAnchorHash
, // match anchor [cert hash]
140 opInfoKeyValue
, // *legacy* match Info.plist field [key; value]
141 opAnd
, // binary prefix expr AND expr
142 opOr
, // binary prefix expr OR expr
143 opCDHash
, // match hash of CodeDirectory directly
144 opNot
, // logical inverse
145 opInfoKeyField
, // Info.plist key field [string; match suffix]
146 opCertField
, // Certificate field [cert index; field name; match suffix]
147 opTrustedCert
, // require trust settings to approve one particular cert [cert index]
148 opTrustedCerts
, // require trust settings to approve the cert chain
149 exprOpCount
// (total opcode count in use)
152 // match suffix opcodes
153 enum MatchOperation
{
154 matchExists
, // anything but explicit "false" - no value stored
155 matchEqual
, // equal (CFEqual)
156 matchContains
, // partial match (substring)
161 // We keep Requirement groups in SuperBlobs, indexed by SecRequirementType
163 typedef SuperBlob
<0xfade0c01> Requirements
;
170 // Flipper overloads must go directly into the Security namespace
172 inline CodeSigning::ExprOp
h2n(CodeSigning::ExprOp op
) { return CodeSigning::ExprOp(h2n(uint32_t(op
))); }
173 inline CodeSigning::ExprOp
n2h(CodeSigning::ExprOp op
) { return CodeSigning::ExprOp(n2h(uint32_t(op
))); }
174 inline CodeSigning::MatchOperation
h2n(CodeSigning::MatchOperation op
) { return CodeSigning::MatchOperation(h2n(uint32_t(op
))); }
175 inline CodeSigning::MatchOperation
n2h(CodeSigning::MatchOperation op
) { return CodeSigning::MatchOperation(n2h(uint32_t(op
))); }
180 #endif //_H_REQUIREMENT