2 * Copyright (c) 2004,2011,2014 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@
24 #ifndef _SECURITY_MANIFEST_H_
25 #define _SECURITY_MANIFEST_H_
28 #include <Security/Security.h>
29 #include <security_utilities/security_utilities.h>
30 #include <security_utilities/cfclass.h>
31 #include <security_cdsa_client/cspclient.h>
32 #include "SecManifest.h"
37 // note: The error range for the file signing library is -22040 through -22079
44 static ModuleNexus
<CSSMInitializer
> mInstance
;
46 CssmClient::Module mModule
;
53 static CssmClient::CSP
* GetCSP ();
58 const int kSHA1DigestSize
= 20;
59 typedef unsigned char SHA1Digest
[kSHA1DigestSize
];
61 typedef std::set
<std::string
> StringSet
;
63 class ManifestItemList
: private std::vector
<ManifestItem
*>
67 friend class FileSystemItemList
;
69 typedef std::vector
<ManifestItem
*> ParentClass
;
71 void ConvertToStringSet (const char* path
, CFArrayRef array
, StringSet
& stringSet
);
74 void DecodeURL (CFURLRef url
, char *pathBuffer
, CFIndex maxBufLen
);
75 void AddDataObject (CFDataRef data
);
81 void AddFileSystemObject (char* path
, StringSet
& exceptions
, bool isRoot
, bool hasAppleDoubleResourceFork
);
82 void AddObject (CFTypeRef object
, CFArrayRef exceptionList
);
84 using ParentClass::push_back
;
85 using ParentClass::size
;
86 // using ParentClass::operator[];
88 ManifestItem
* operator[] (int n
) {return ParentClass::operator[] (n
);}
93 class FileSystemItemList
: public ManifestItemList
96 void Compare (FileSystemItemList
&itemList
, bool compareOwnerAndGroup
);
101 class RootItemList
: public ManifestItemList
104 void Compare (RootItemList
& itemList
, bool compareOwnerAndGroup
);
109 class ManifestInternal
112 RootItemList mManifestItems
;
117 virtual ~ManifestInternal ();
119 ManifestItemList
& GetItemList () {return mManifestItems
;}
121 static void CompareManifests (ManifestInternal
& m1
, ManifestInternal
& m2
, SecManifestCompareOptions options
);
126 enum ManifestItemType
{kManifestDataBlobItemType
, kManifestFileItemType
, kManifestDirectoryItemType
, kManifestSymLinkItemType
,
129 // base class for our internal object representation
133 virtual ~ManifestItem ();
135 virtual ManifestItemType
GetItemType () = 0;
136 virtual void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
) = 0;
141 class ManifestDataBlobItem
: public ManifestItem
144 SHA1Digest mSHA1Digest
;
148 ManifestDataBlobItem ();
149 virtual ~ManifestDataBlobItem ();
151 ManifestItemType
GetItemType ();
153 const SHA1Digest
* GetDigest ();
154 void SetDigest (const SHA1Digest
*sha1Digest
);
156 void SetLength (size_t length
);
157 void Compare (ManifestItem
* item
, bool compareOwnerAndGroup
);
162 class FileSystemEntryItem
: public ManifestItem
165 std::string mPath
, mName
;
171 FileSystemEntryItem ();
172 virtual ~FileSystemEntryItem ();
174 void SetName (char* name
);
175 void SetPath (char* path
);
176 void SetUID (uid_t uid
);
177 void SetGID (gid_t gid
);
178 void SetMode (mode_t mode
);
180 const char* GetName () const;
181 const std::string
& GetNameAsString () const {return mName
;}
182 uid_t
GetUID () const;
183 gid_t
GetGID () const;
184 mode_t
GetMode () const;
186 void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
);
191 const int kMaxForks
= 2;
193 class ManifestFileItem
: public FileSystemEntryItem
196 SHA1Digest mDigest
[kMaxForks
];
197 size_t mFileLengths
[kMaxForks
];
199 bool FileSystemHasTrueForks (char* pathToFile
);
200 bool HasResourceFork (char* path
, std::string
&pathName
, struct stat
&st
);
201 std::string
ResourceFileName (char* path
);
202 bool FileIsMachOBinary (char* path
);
203 void ComputeDigestForFile (char* path
, SHA1Digest
&digest
, size_t &length
, struct stat
&st
);
204 void ComputeDigestForAppleDoubleResourceFork (char* path
, SHA1Digest
&digest
, size_t &length
);
210 virtual ~ManifestFileItem ();
212 u_int32_t
GetNumberOfForks ();
213 void SetNumberOfForks (u_int32_t numForks
);
214 void ComputeRepresentations (struct stat
&st
, bool hasAppleDoubleResourceFork
);
215 void GetItemRepresentation (int whichFork
, void* &itemRep
, size_t &size
);
216 void SetItemRepresentation (int whichFork
, const void* itemRep
, size_t size
);
217 void SetForkLength (int whichFork
, size_t length
);
218 size_t GetForkLength (int whichFork
);
220 ManifestItemType
GetItemType ();
222 void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
);
227 class ManifestDirectoryItem
: public FileSystemEntryItem
230 FileSystemItemList mDirectoryItems
;
233 ManifestDirectoryItem ();
234 virtual ~ManifestDirectoryItem ();
236 void SetPath (char* path
, StringSet
&exceptions
, bool isRoot
);
237 ManifestItemType
GetItemType ();
238 ManifestItemList
& GetItemList () {return mDirectoryItems
;}
240 void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
);
245 class ManifestSymLinkItem
: public FileSystemEntryItem
248 std::string mContent
;
252 ManifestSymLinkItem ();
253 virtual ~ManifestSymLinkItem ();
255 const SHA1Digest
* GetDigest ();
256 void SetDigest (const SHA1Digest
* sha1Digest
);
257 void ComputeRepresentation ();
258 ManifestItemType
GetItemType ();
260 void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
);
265 class ManifestOtherItem
: public FileSystemEntryItem
268 std::string mPath
, mName
;
271 ManifestOtherItem ();
272 virtual ~ManifestOtherItem ();
274 ManifestItemType
GetItemType ();
276 void Compare (ManifestItem
*manifestItem
, bool compareOwnerAndGroup
);