]> git.saurik.com Git - apple/libsecurity_codesigning.git/blame - lib/singlediskrep.cpp
libsecurity_codesigning-55037.15.tar.gz
[apple/libsecurity_codesigning.git] / lib / singlediskrep.cpp
CommitLineData
7d31e928 1/*
f60086fc 2 * Copyright (c) 2006-2010 Apple Inc. All Rights Reserved.
7d31e928
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// singlediskrep - semi-abstract diskrep for a single file of some kind
26//
27#include "singlediskrep.h"
d1c1ab47
A
28#include "csutilities.h"
29#include <security_utilities/cfutilities.h>
7d31e928
A
30
31namespace Security {
32namespace CodeSigning {
33
34using namespace UnixPlusPlus;
35
36
37//
38// Construct a SingleDiskRep
39//
d1c1ab47 40SingleDiskRep::SingleDiskRep(const std::string &path)
7d31e928
A
41 : mPath(path)
42{
43}
44
45
d1c1ab47
A
46//
47// The default binary identification of a SingleDiskRep is the (SHA-1) hash
48// of the entire file itself.
49//
50CFDataRef SingleDiskRep::identification()
51{
52 SHA1 hash;
53 this->fd().seek(0);
f60086fc 54 hashFileData(this->fd(), &hash);
d1c1ab47
A
55 SHA1::Digest digest;
56 hash.finish(digest);
57 return makeCFData(digest, sizeof(digest));
58}
59
60
7d31e928
A
61//
62// Both the canonical and main executable path of a SingleDiskRep is, well, its path.
63//
64CFURLRef SingleDiskRep::canonicalPath()
65{
66 return makeCFURL(mPath);
67}
68
69string SingleDiskRep::mainExecutablePath()
70{
71 return mPath;
72}
73
74
7d31e928
A
75//
76// The default signing limit is the size of the file.
77// This will do unless the signing data gets creatively stuck in there somewhere.
78//
79size_t SingleDiskRep::signingLimit()
80{
81 return fd().fileSize();
82}
83
84
85//
86// A lazily opened read-only file descriptor for the path.
87//
88FileDesc &SingleDiskRep::fd()
89{
90 if (!mFd)
91 mFd.open(mPath, O_RDONLY);
92 return mFd;
93}
94
95
96//
97// Flush cached state
98//
99void SingleDiskRep::flush()
100{
101 mFd.close();
102}
103
104
f60086fc
A
105//
106// The recommended identifier of a SingleDiskRep is, absent any better clue,
107// the basename of its path.
108//
109string SingleDiskRep::recommendedIdentifier(const SigningContext &)
110{
111 return canonicalIdentifier(mPath);
112}
113
114
7d31e928
A
115//
116// Prototype Writers
117//
118FileDesc &SingleDiskRep::Writer::fd()
119{
120 if (!mFd)
121 mFd.open(rep->path(), O_RDWR);
122 return mFd;
123}
124
125
126} // end namespace CodeSigning
127} // end namespace Security