5772ee6e30adfbef0340cafc04b9b935f179cfe2
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 // csdb - system-supported Code Signing related database interfaces
27 #include "csdatabase.h"
28 #include "detachedrep.h"
31 namespace CodeSigning
{
33 using namespace SQLite
;
37 // The one and only SignatureDatabase object.
38 // It auto-adapts to readonly vs. writable use.
40 ModuleNexus
<SignatureDatabase
> signatureDatabase
;
41 ModuleNexus
<SignatureDatabaseWriter
> signatureDatabaseWriter
;
45 // Default path to the signature database.
47 const char SignatureDatabase::defaultPath
[] = "/var/db/DetachedSignatures";
51 // Creation commands to initialize the system database.
53 const char schema
[] = "\
54 create table if not exists code ( \n\
55 id integer primary key on conflict replace autoincrement not null, \n\
56 global integer null references global (id), \n\
57 identifier text not null, \n\
58 architecture integer, \n\
59 identification blob not null unique on conflict replace, \n\
60 signature blob not null, \n\
61 created text default current_timestamp \n\
63 create index if not exists identifier_index on code (identifier); \n\
64 create index if not exists architecture_index on code (architecture); \n\
65 create index if not exists id_index on code (identification); \n\
67 create table if not exists global ( \n\
68 id integer primary key on conflict replace autoincrement not null, \n\
69 sign_location text not null, \n\
70 signature blob null \n\
72 create index if not exists location_index on global (sign_location); \n\
78 // Open the database (creating it if necessary and possible).
79 // Note that this isn't creating the schema; we do that on first write.
81 SignatureDatabase::SignatureDatabase(const char *path
, int flags
)
82 : SQLite::Database(path
, flags
)
86 SignatureDatabase::~SignatureDatabase()
91 // Consult the database to find code by identification blob.
92 // Return the signature and (optional) global data blobs.
94 FilterRep
*SignatureDatabase::findCode(DiskRep
*rep
)
96 if (CFRef
<CFDataRef
> identification
= rep
->identification())
98 SQLite::Statement
query(*this,
99 "select code.signature, global.signature from code, global \
100 where code.identification = ?1 and code.global = global.id;");
101 query
.bind(1) = identification
.get();
103 return new DetachedRep(query
[0].data(), query
[1].data(), rep
, "system");
112 // Given a unified detached signature blob, store its data in the database.
113 // This writes exactly one Global record, plus one Code record per architecture
114 // (where non-architectural code is treated as single-architecture).
116 void SignatureDatabaseWriter::storeCode(const BlobCore
*sig
, const char *location
)
118 Transaction
xa(*this, Transaction::exclusive
); // lock out everyone
120 this->execute(schema
); // initialize schema
121 if (const EmbeddedSignatureBlob
*esig
= EmbeddedSignatureBlob::specific(sig
)) { // architecture-less
122 int64 globid
= insertGlobal(location
, NULL
);
123 insertCode(globid
, 0, esig
);
126 } else if (const DetachedSignatureBlob
*dsblob
= DetachedSignatureBlob::specific(sig
)) {
127 int64 globid
= insertGlobal(location
, dsblob
->find(0));
128 unsigned count
= dsblob
->count();
129 for (unsigned n
= 0; n
< count
; n
++)
130 if (uint32_t arch
= dsblob
->type(n
))
131 insertCode(globid
, arch
, EmbeddedSignatureBlob::specific(dsblob
->blob(n
)));
136 MacOSError::throwMe(errSecCSSignatureInvalid
);
140 int64
SignatureDatabaseWriter::insertGlobal(const char *location
, const BlobCore
*blob
)
142 Statement
insert(*this, "insert into global (sign_location, signature) values (?1, ?2);");
143 insert
.bind(1) = location
;
145 insert
.bind(2).blob(blob
, blob
->length(), true);
150 void SignatureDatabaseWriter::insertCode(int64 globid
, int arch
, const EmbeddedSignatureBlob
*sig
)
152 // retrieve binary identifier (was added by signer)
153 const BlobWrapper
*ident
= BlobWrapper::specific(sig
->find(cdIdentificationSlot
));
156 // extract CodeDirectory to get some information from it
157 const CodeDirectory
*cd
= CodeDirectory::specific(sig
->find(cdCodeDirectorySlot
));
161 Statement
insert(*this,
162 "insert into code (global, identifier, architecture, identification, signature) values (?1, ?2, ?3, ?4, ?5);");
163 insert
.bind(1) = globid
;
164 insert
.bind(2) = cd
->identifier();
166 insert
.bind(3) = arch
;
167 insert
.bind(4).blob(ident
->data(), ident
->length(), true);
168 insert
.bind(5).blob(sig
, sig
->length(), true);
174 } // end namespace CodeSigning
175 } // end namespace Security