2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 // localkey - Key objects that store a local CSSM key object
33 #include <security_cdsa_utilities/acl_any.h>
37 // Create a Key from an explicit CssmKey.
39 LocalKey::LocalKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
40 const AclEntryPrototype
*owner
)
41 : mDigest(Server::csp().allocator())
45 setup(newKey
, moreAttributes
);
47 // establish initial ACL; reinterpret empty (null-list) owner as NULL for resilence's sake
48 if (owner
&& !owner
->subject().empty())
49 cssmSetInitial(*owner
); // specified
51 cssmSetInitial(new AnyAclSubject()); // defaulted
52 secdebug("SSkey", "%p (handle 0x%lx) created from key alg=%ld use=0x%lx attr=0x%lx db=%p",
53 this, handle(), mKey
.header().algorithm(), mKey
.header().usage(), mAttributes
, &db
);
57 LocalKey::LocalKey(Database
&db
)
58 : mValidKey(false), mAttributes(0), mDigest(Server::csp().allocator())
65 // Set up the CssmKey part of this Key according to instructions.
67 void LocalKey::setup(const CssmKey
&newKey
, uint32 moreAttributes
)
69 mKey
= CssmClient::Key(Server::csp(), newKey
, false);
70 CssmKey::Header
&header
= mKey
->header();
73 header
= newKey
.header();
74 mAttributes
= (header
.attributes() & ~forcedAttributes
) | moreAttributes
;
76 // apply initial values of derived attributes (these are all in managedAttributes)
77 if (!(mAttributes
& CSSM_KEYATTR_EXTRACTABLE
))
78 mAttributes
|= CSSM_KEYATTR_NEVER_EXTRACTABLE
;
79 if (mAttributes
& CSSM_KEYATTR_SENSITIVE
)
80 mAttributes
|= CSSM_KEYATTR_ALWAYS_SENSITIVE
;
82 // verify internal/external attribute separation
83 assert((header
.attributes() & managedAttributes
) == forcedAttributes
);
89 secdebug("SSkey", "%p destroyed", this);
93 LocalDatabase
&LocalKey::database() const
95 return referent
<LocalDatabase
>();
100 // Retrieve the actual CssmKey value for the key object.
101 // This will decode its blob if needed (and appropriate).
103 CssmClient::Key
LocalKey::keyValue()
114 // Return a key's handle and header in external form
116 void LocalKey::returnKey(Handle
&h
, CssmKey::Header
&hdr
)
121 // obtain the key header, from the valid key or the blob if no valid key
128 // adjust for external attributes
129 hdr
.clearAttribute(forcedAttributes
);
130 hdr
.setAttribute(mAttributes
);
135 // Generate the canonical key digest.
136 // This is defined by a CSP feature that we invoke here.
138 const CssmData
&LocalKey::canonicalDigest()
141 CssmClient::PassThrough
ctx(Server::csp());
143 CssmData
*digest
= NULL
;
144 ctx(CSSM_APPLECSP_KEYDIGEST
, (const void *)NULL
, &digest
);
146 mDigest
.set(*digest
); // takes ownership of digest data
147 Server::csp().allocator().free(digest
); // the CssmData itself
149 return mDigest
.get();
154 // Default getKey/getHeader calls - should never be called
156 void LocalKey::getKey()
161 void LocalKey::getHeader(CssmKey::Header
&)