2 * Copyright (c) 2000-2001 Apple Computer, 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 // localkey - Key objects that store a local CSSM key object
31 #include <security_cdsa_utilities/acl_any.h>
35 // Create a Key from an explicit CssmKey.
37 LocalKey::LocalKey(Database
&db
, const CssmKey
&newKey
, CSSM_KEYATTR_FLAGS moreAttributes
)
38 : Key(db
), mDigest(Server::csp().allocator())
41 setup(newKey
, moreAttributes
);
42 secdebug("SSkey", "%p (handle 0x%lx) created from key alg=%ld use=0x%lx attr=0x%lx db=%p",
43 this, handle(), mKey
.header().algorithm(), mKey
.header().usage(), mAttributes
, &db
);
47 LocalKey::LocalKey(Database
&db
, CSSM_KEYATTR_FLAGS attributes
)
48 : Key(db
), mValidKey(false), mAttributes(attributes
), mDigest(Server::csp().allocator())
54 // Set up the CssmKey part of this Key according to instructions.
56 void LocalKey::setup(const CssmKey
&newKey
, CSSM_KEYATTR_FLAGS moreAttributes
)
58 mKey
= CssmClient::Key(Server::csp(), newKey
, false);
59 CssmKey::Header
&header
= mKey
->header();
62 header
= newKey
.header();
63 mAttributes
= (header
.attributes() & ~forcedAttributes
) | moreAttributes
;
65 // apply initial values of derived attributes (these are all in managedAttributes)
66 if (!(mAttributes
& CSSM_KEYATTR_EXTRACTABLE
))
67 mAttributes
|= CSSM_KEYATTR_NEVER_EXTRACTABLE
;
68 if (mAttributes
& CSSM_KEYATTR_SENSITIVE
)
69 mAttributes
|= CSSM_KEYATTR_ALWAYS_SENSITIVE
;
71 // verify internal/external attribute separation
72 assert((header
.attributes() & managedAttributes
) == forcedAttributes
);
78 secdebug("SSkey", "%p destroyed", this);
82 void LocalKey::setOwner(const AclEntryPrototype
*owner
)
84 // establish initial ACL; reinterpret empty (null-list) owner as NULL for resilence's sake
85 if (owner
&& !owner
->subject().empty())
86 acl().cssmSetInitial(*owner
); // specified
88 acl().cssmSetInitial(new AnyAclSubject()); // defaulted
92 LocalDatabase
&LocalKey::database() const
94 return referent
<LocalDatabase
>();
99 // Retrieve the actual CssmKey value for the key object.
100 // This will decode its blob if needed (and appropriate).
102 CssmClient::Key
LocalKey::keyValue()
113 // Return external key attributees
115 CSSM_KEYATTR_FLAGS
LocalKey::attributes()
122 // Return a key's handle and header in external form
124 void LocalKey::returnKey(Handle
&h
, CssmKey::Header
&hdr
)
129 // obtain the key header, from the valid key or the blob if no valid key
136 // adjust for external attributes
137 hdr
.clearAttribute(forcedAttributes
);
138 hdr
.setAttribute(mAttributes
);
143 // Generate the canonical key digest.
144 // This is defined by a CSP feature that we invoke here.
146 const CssmData
&LocalKey::canonicalDigest()
149 CssmClient::PassThrough
ctx(Server::csp());
151 CssmData
*digest
= NULL
;
152 ctx(CSSM_APPLECSP_KEYDIGEST
, (const void *)NULL
, &digest
);
154 mDigest
.set(*digest
); // takes ownership of digest data
155 Server::csp().allocator().free(digest
); // the CssmData itself
157 return mDigest
.get();
162 // Default getKey/getHeader calls - should never be called
164 void LocalKey::getKey()
169 void LocalKey::getHeader(CssmKey::Header
&)
176 // Form a KeySpec with checking and masking
178 LocalKey::KeySpec::KeySpec(CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
)
179 : CssmClient::KeySpec(usage
, (attrs
& ~managedAttributes
) | forcedAttributes
)
181 if (attrs
& generatedAttributes
)
182 CssmError::throwMe(CSSMERR_CSP_INVALID_KEYATTR_MASK
);
185 LocalKey::KeySpec::KeySpec(CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
, const CssmData
&label
)
186 : CssmClient::KeySpec(usage
, (attrs
& ~managedAttributes
) | forcedAttributes
, label
)
188 if (attrs
& generatedAttributes
)
189 CssmError::throwMe(CSSMERR_CSP_INVALID_KEYATTR_MASK
);