2 * Copyright (c) 2004,2008 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 // tempdatabase - temporary (scratch) storage for keys
28 #include <security_cdsa_utilities/cssmdata.h>
29 #include <security_cdsa_utilities/cssmkey.h>
30 #include <security_cdsa_client/wrapkey.h>
31 #include "tempdatabase.h"
35 #include "agentquery.h"
39 // Temporary-space Key objects are almost normal LocalKeys, with the key
40 // matter always preloaded (and thus no deferral of instantiation).
41 // A TempKey bears its own ACL.
43 class TempKey
: public LocalKey
, public SecurityServerAcl
{
45 TempKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
46 const AclEntryPrototype
*owner
= NULL
);
48 Database
*relatedDatabase();
50 SecurityServerAcl
&acl() { return *this; }
53 // SecurityServerAcl personality
54 AclKind
aclKind() const;
58 TempKey::TempKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
59 const AclEntryPrototype
*owner
)
60 : LocalKey(db
, newKey
, moreAttributes
)
63 db
.addReference(*this);
67 AclKind
TempKey::aclKind() const
73 Database
*TempKey::relatedDatabase()
80 // Create a Database object from initial parameters (create operation)
82 TempDatabase::TempDatabase(Process
&proc
)
85 proc
.addReference(*this);
90 // A LocalDatabase itself doesn't really have a database name,
91 // but here's an innocent placeholder.
93 const char *TempDatabase::dbName() const
98 bool TempDatabase::transient() const
105 // Invoke the Security Agent to get a passphrase (other than for a Keychain)
107 void TempDatabase::getSecurePassphrase(const Context
&context
,
110 uint32 verify
= context
.getInt(CSSM_ATTRIBUTE_VERIFY_PASSPHRASE
, CSSMERR_CSSM_ATTRIBUTE_NOT_IN_CONTEXT
);
112 CssmData
*promptData
= context
.get
<CssmData
>(CSSM_ATTRIBUTE_PROMPT
);
114 QueryGenericPassphrase agentQuery
;
115 agentQuery
.inferHints(Server::process());
116 agentQuery(promptData
, verify
, passphrase
);
120 void TempDatabase::makeSecurePassphraseKey(const Context
&context
,
121 const AccessCredentials
*cred
,
122 const AclEntryPrototype
*owner
,
123 uint32 usage
, uint32 attrs
,
124 RefPointer
<Key
> &newKey
)
126 secdebug("SSdb", "requesting secure passphrase");
129 getSecurePassphrase(context
, passphrase
);
131 secdebug("SSdb", "wrapping securely-obtained passphrase as key");
133 // CssmKey rawKey(StringData(passphrase)) confuses gcc
134 StringData
passphraseData(passphrase
);
135 CssmKey
rawKey(passphraseData
);
136 rawKey
.algorithm(context
.algorithm());
137 rawKey
.blobType(CSSM_KEYBLOB_RAW
);
138 rawKey
.blobFormat(CSSM_KEYBLOB_WRAPPED_FORMAT_NONE
);
139 rawKey
.keyClass(CSSM_KEYCLASS_SESSION_KEY
);
141 CssmClient::UnwrapKey
unwrap(Server::csp(), CSSM_ALGID_NONE
);
143 unwrap(rawKey
, TempKey::KeySpec(usage
, attrs
), cspKey
);
145 newKey
= makeKey(cspKey
, attrs
& TempKey::managedAttributes
, owner
);
150 // Obtain "secure passphrases" for the CSP. Useful for PKCS 12.
152 void TempDatabase::generateKey(const Context
&context
,
153 const AccessCredentials
*cred
,
154 const AclEntryPrototype
*owner
,
155 uint32 usage
, uint32 attrs
,
156 RefPointer
<Key
> &newKey
)
158 switch (context
.algorithm())
160 case CSSM_ALGID_SECURE_PASSPHRASE
:
161 makeSecurePassphraseKey(context
, cred
, owner
, usage
, attrs
, newKey
);
164 LocalDatabase::generateKey(context
, cred
, owner
, usage
, attrs
, newKey
);
171 // Make a new TempKey
173 RefPointer
<Key
> TempDatabase::makeKey(const CssmKey
&newKey
,
174 uint32 moreAttributes
, const AclEntryPrototype
*owner
)
176 assert(!newKey
.attribute(CSSM_KEYATTR_PERMANENT
));
177 return new TempKey(*this, newKey
, moreAttributes
, owner
);