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=%u use=0x%x attr=0x%x 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() 
 104         StLock
<Mutex
> _(*this); 
 114 // Return external key attributees 
 116 CSSM_KEYATTR_FLAGS 
LocalKey::attributes() 
 123 // Return a key's handle and header in external form 
 125 void LocalKey::returnKey(Handle 
&h
, CssmKey::Header 
&hdr
) 
 127         StLock
<Mutex
> _(*this); 
 132         // obtain the key header, from the valid key or the blob if no valid key 
 139     // adjust for external attributes 
 140         hdr
.clearAttribute(forcedAttributes
); 
 141     hdr
.setAttribute(mAttributes
); 
 146 // Generate the canonical key digest. 
 147 // This is defined by a CSP feature that we invoke here. 
 149 const CssmData 
&LocalKey::canonicalDigest() 
 151         StLock
<Mutex
> _(*this); 
 153                 CssmClient::PassThrough 
ctx(Server::csp()); 
 155                 CssmData 
*digest 
= NULL
; 
 156                 ctx(CSSM_APPLECSP_KEYDIGEST
, (const void *)NULL
, &digest
); 
 158                 mDigest
.set(*digest
);   // takes ownership of digest data 
 159                 Server::csp().allocator().free(digest
); // the CssmData itself 
 161         return mDigest
.get(); 
 166 // Default getKey/getHeader calls - should never be called 
 168 void LocalKey::getKey() 
 173 void LocalKey::getHeader(CssmKey::Header 
&) 
 180 // Form a KeySpec with checking and masking 
 182 LocalKey::KeySpec::KeySpec(CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
) 
 183         : CssmClient::KeySpec(usage
, (attrs 
& ~managedAttributes
) | forcedAttributes
) 
 185         if (attrs 
& generatedAttributes
) 
 186                 CssmError::throwMe(CSSMERR_CSP_INVALID_KEYATTR_MASK
); 
 189 LocalKey::KeySpec::KeySpec(CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
, const CssmData 
&label
) 
 190         : CssmClient::KeySpec(usage
, (attrs 
& ~managedAttributes
) | forcedAttributes
, label
) 
 192         if (attrs 
& generatedAttributes
) 
 193                 CssmError::throwMe(CSSMERR_CSP_INVALID_KEYATTR_MASK
);