2 * Copyright (c) 2000-2001,2004-2006,2008-2009 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@
26 // key - representation of securityd key objects
31 #include <security_cdsa_utilities/acl_any.h>
32 #include <security_cdsa_utilities/cssmendian.h>
36 // Create a Key object from a database-encoded blob.
37 // Note that this doesn't decode the blob (yet).
39 KeychainKey::KeychainKey(Database
&db
, const KeyBlob
*blob
)
40 : LocalKey(db
, n2h(blob
->header
.attributes()))
42 // perform basic validation on the incoming blob
44 blob
->validate(CSSMERR_APPLEDL_INVALID_KEY_BLOB
);
45 switch (blob
->version()) {
46 #if defined(COMPAT_OSX_10_0)
47 case KeyBlob::version_MacOS_10_0
:
50 case KeyBlob::version_MacOS_10_1
:
53 CssmError::throwMe(CSSMERR_APPLEDL_INCOMPATIBLE_KEY_BLOB
);
57 mBlob
= blob
->copy(Allocator::standard());
59 db
.addReference(*this);
60 secdebug("SSkey", "%p (handle %#x) created from blob version %x",
61 this, handle(), blob
->version());
66 // Create a Key from an explicit CssmKey.
68 KeychainKey::KeychainKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
69 const AclEntryPrototype
*owner
)
70 : LocalKey(db
, newKey
, moreAttributes
)
72 assert(moreAttributes
& CSSM_KEYATTR_PERMANENT
);
76 db
.addReference(*this);
80 KeychainKey::~KeychainKey()
82 Allocator::standard().free(mBlob
);
83 secdebug("SSkey", "%p destroyed", this);
87 KeychainDatabase
&KeychainKey::database() const
89 return referent
<KeychainDatabase
>();
94 // Retrieve the actual CssmKey value for the key object.
95 // This will decode its blob if needed (and appropriate).
97 void KeychainKey::getKey()
102 void KeychainKey::getHeader(CssmKey::Header
&hdr
)
106 n2hi(hdr
); // correct for endian-ness
111 // Ensure that a key is fully decoded.
112 // This makes the mKey key value available for use, as well as its ACL.
113 // Caller must hold the key object lock.
115 void KeychainKey::decode()
118 assert(mValidBlob
); // must have a blob to decode
121 void *publicAcl
, *privateAcl
;
123 database().decodeKey(mBlob
, key
, publicAcl
, privateAcl
);
124 mKey
= CssmClient::Key(Server::csp(), key
);
125 acl().importBlob(publicAcl
, privateAcl
);
126 // publicAcl points into the blob; privateAcl was allocated for us
127 Allocator::standard().free(privateAcl
);
129 // extract managed attribute bits
130 mAttributes
= mKey
.header().attributes() & managedAttributes
;
131 mKey
.header().clearAttribute(managedAttributes
);
132 mKey
.header().setAttribute(forcedAttributes
);
141 // Encode a key into a blob.
142 // We'll have to ask our Database to do this - we don't have its keys.
143 // Note that this returns memory we own and keep.
145 KeyBlob
*KeychainKey::blob()
148 assert(mValidKey
); // must have valid key to encode
150 // export Key ACL to blob form
151 CssmData pubAcl
, privAcl
;
152 acl().exportBlob(pubAcl
, privAcl
);
154 // assemble external key form
155 CssmKey externalKey
= mKey
;
156 externalKey
.clearAttribute(forcedAttributes
);
157 externalKey
.setAttribute(mAttributes
);
159 // encode the key and replace blob
160 KeyBlob
*newBlob
= database().encodeKey(externalKey
, pubAcl
, privAcl
);
161 Allocator::standard().free(mBlob
);
166 acl().allocator
.free(pubAcl
);
167 acl().allocator
.free(privAcl
);
172 void KeychainKey::invalidateBlob()
179 // Override ACL-related methods and events.
180 // Decode the key before ACL activity; invalidate the stored blob on ACL edits;
181 // and return the key's database as "related".
183 void KeychainKey::instantiateAcl()
185 StLock
<Mutex
> _(*this);
189 void KeychainKey::changedAcl()
196 // Intercept Key validation and double-check that the keychain is (still) unlocked
198 void KeychainKey::validate(AclAuthorization auth
, const AccessCredentials
*cred
,
199 Database
*relatedDatabase
)
201 if(!mBlob
->isClearText()) {
202 /* unlock not needed for cleartext keys */
203 if (KeychainDatabase
*db
= dynamic_cast<KeychainDatabase
*>(relatedDatabase
))
206 SecurityServerAcl::validate(auth
, cred
, relatedDatabase
);
207 database().activity(); // upon successful validation
214 AclKind
KeychainKey::aclKind() const
220 Database
*KeychainKey::relatedDatabase()
225 SecurityServerAcl
&KeychainKey::acl()