]> git.saurik.com Git - apple/security.git/blame - OSX/libsecurity_codesigning/lib/signer.h
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_codesigning / lib / signer.h
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2006-2012,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
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// signer - Signing operation supervisor and controller
26//
27#ifndef _H_SIGNER
28#define _H_SIGNER
29
30#include "CodeSigner.h"
31#include "cdbuilder.h"
32#include "signerutils.h"
33#include "StaticCode.h"
34#include <security_utilities/utilities.h>
35
36namespace Security {
37namespace CodeSigning {
38
39
40//
41// The signer driver class.
42// This is a workflow object, containing all the data needed for the various
43// signing stages to cooperate. It is not meant to be API visible; that is
44// SecCodeSigner's job.
45//
e3d460c9 46class SecCodeSigner::Signer : public DiskRep::SigningContext {
b1ab9ed8 47public:
80e23899 48 Signer(SecCodeSigner &s, SecStaticCode *c) : state(s), code(c), requirements(NULL)
e3d460c9 49 { strict = signingFlags() & kSecCSSignStrictPreflight; }
427c49bc
A
50 ~Signer() { ::free((Requirements *)requirements); }
51
b1ab9ed8
A
52 void sign(SecCSFlags flags);
53 void remove(SecCSFlags flags);
dbe77505 54 void edit(SecCSFlags flags);
b1ab9ed8
A
55
56 SecCodeSigner &state;
57 SecStaticCode * const code;
58
e3d460c9
A
59 const CodeDirectory::HashAlgorithms& digestAlgorithms() const { return hashAlgorithms; }
60 void setDigestAlgorithms(CodeDirectory::HashAlgorithms types) { hashAlgorithms = types; }
b1ab9ed8 61
d8f41ccd 62 std::string path() const { return cfStringRelease(rep->copyCanonicalPath()); }
b1ab9ed8
A
63 SecIdentityRef signingIdentity() const { return state.mSigner; }
64 std::string signingIdentifier() const { return identifier; }
65
66protected:
67 void prepare(SecCSFlags flags); // set up signing parameters
68 void signMachO(Universal *fat, const Requirement::Context &context); // sign a Mach-O binary
69 void signArchitectureAgnostic(const Requirement::Context &context); // sign anything else
dbe77505
A
70
71 void prepareForEdit(SecCSFlags flags); // set up signature editing
72 void editMachO(Universal *fat); // edit a Mach-O binary
73 void editArchitectureAgnostic(); // edit anything else
b1ab9ed8 74
90dc47c2
A
75 // HashAlgorithm -> PreEncrypt hashes
76 typedef std::map<CodeDirectory::HashAlgorithm, CFCopyRef<CFDataRef> >
77 PreEncryptHashMap;
78 // Architecture -> PreEncryptHashMap
79 typedef std::map<Architecture, PreEncryptHashMap >
80 PreEncryptHashMaps;
81 // HashAlgorithm -> Hardened Runtime Version
82 typedef std::map<CodeDirectory::HashAlgorithm, uint32_t>
83 RuntimeVersionMap;
84 // Architecture -> RuntimeVersionMap
85 typedef std::map<Architecture, RuntimeVersionMap>
86 RuntimeVersionMaps;
dbe77505
A
87 // Architecture -> RawComponentMap
88 typedef EditableDiskRep::RawComponentMap RawComponentMap;
89 typedef std::map<Architecture, std::unique_ptr<RawComponentMap>>
90 RawComponentMaps;
90dc47c2 91
b1ab9ed8
A
92 void populate(DiskRep::Writer &writer); // global
93 void populate(CodeDirectory::Builder &builder, DiskRep::Writer &writer,
866f8763
A
94 InternalRequirements &ireqs,
95 size_t offset, size_t length,
96 bool mainBinary, size_t execSegBase, size_t execSegLimit,
90dc47c2
A
97 unsigned alternateDigestCount,
98 const PreEncryptHashMap& preEncryptHashMap,
d64be36e 99 uint32_t runtimeVersion, bool generateEntitlementDER); // per-architecture
90dc47c2
A
100 CFDataRef signCodeDirectory(const CodeDirectory *cd,
101 CFDictionaryRef hashDict, CFArrayRef hashList);
b1ab9ed8
A
102
103 uint32_t cdTextFlags(std::string text); // convert text CodeDirectory flags
104 std::string uniqueName() const; // derive unique string from rep
e3d460c9
A
105
106protected:
90dc47c2 107
e3d460c9
A
108 std::string sdkPath(const std::string &path) const;
109 bool isAdhoc() const;
110 SecCSFlags signingFlags() const;
111
112private:
dbe77505
A
113 static EditableDiskRep *editMainExecutableRep(DiskRep *rep);
114
90dc47c2
A
115 void addPreEncryptHashes(PreEncryptHashMap &map, SecStaticCode const *code);
116 void addRuntimeVersions(RuntimeVersionMap &map, SecStaticCode const *code);
117
e3d460c9
A
118 void considerTeamID(const PreSigningContext& context);
119 std::vector<Endian<uint32_t> > topSlots(CodeDirectory::Builder &builder) const;
427c49bc 120
866f8763 121 static bool booleanEntitlement(CFDictionaryRef entDict, CFStringRef key);
90dc47c2
A
122 static void cookEntitlements(CFDataRef entitlements, bool generateDER,
123 uint64_t *execSegFlags, CFDataRef *entitlementsDER);
866f8763 124
427c49bc 125protected:
80e23899 126 void buildResources(std::string root, std::string relBase, CFDictionaryRef rules);
d87e1158 127 CFMutableDictionaryRef signNested(const std::string &path, const std::string &relpath);
e3d460c9
A
128 CFDataRef hashFile(const char *path, CodeDirectory::HashAlgorithm type);
129 CFDictionaryRef hashFile(const char *path, CodeDirectory::HashAlgorithms types);
130
b1ab9ed8
A
131private:
132 RefPointer<DiskRep> rep; // DiskRep of Code being signed
427c49bc
A
133 CFRef<CFDictionaryRef> resourceDirectory; // resource directory
134 CFRef<CFDataRef> resourceDictData; // XML form of resourceDirectory
e3d460c9 135 CodeDirectory::HashAlgorithms hashAlgorithms; // hash algorithm(s) to use
b1ab9ed8 136 std::string identifier; // signing identifier
420ff9d9 137 std::string teamID; // team identifier
427c49bc 138 CFRef<CFDataRef> entitlements; // entitlements
90dc47c2
A
139 Architecture preEncryptMainArch; // pre-encrypt main architecture
140 PreEncryptHashMaps preEncryptHashMaps; // pre-encrypt hashes to keep
141 Architecture runtimeVersionMainArch; // runtime version main architecture
142 RuntimeVersionMaps runtimeVersionMap; // runtime versions to keep
b1ab9ed8 143 uint32_t cdFlags; // CodeDirectory flags
427c49bc 144 const Requirements *requirements; // internal requirements ready-to-use
b1ab9ed8 145 size_t pagesize; // size of main executable pages
fa7225c8
A
146 CFAbsoluteTime signingTime; // signing time for CMS signature (0 => now)
147 bool emitSigningTime; // emit signing time as a signed CMS attribute
80e23899 148 bool strict; // strict validation
5c19dc3a 149
dbe77505
A
150 // Signature Editing
151 Architecture editMainArch; // main architecture for editing
152 RawComponentMaps editComponents; // components for signature
153
5c19dc3a 154private:
d87e1158 155 Mutex resourceLock;
b1ab9ed8
A
156};
157
158
159} // end namespace CodeSigning
160} // end namespace Security
161
162#endif // !_H_CODESIGNER