2 * Copyright (c) 2004 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 // tempdatabase - temporary (scratch) storage for keys
30 #include <security_cdsa_utilities/cssmdata.h>
31 #include <security_cdsa_utilities/cssmkey.h>
32 #include <security_cdsa_client/wrapkey.h>
33 #include "tempdatabase.h"
37 #include "agentquery.h"
40 class TempKey
: public LocalKey
{
42 TempKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
43 const AclEntryPrototype
*owner
= NULL
);
48 TempKey::TempKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
,
49 const AclEntryPrototype
*owner
)
50 : LocalKey(db
, newKey
, moreAttributes
, owner
)
52 secdebug("SS adhoc", "Creating temporary (local) key"); // XXX/gh
53 db
.addReference(*this);
58 // Create a Database object from initial parameters (create operation)
60 TempDatabase::TempDatabase(Process
&proc
)
63 proc
.addReference(*this);
68 // A LocalDatabase itself doesn't really have a database name,
69 // but here's an innocent placeholder.
71 const char *TempDatabase::dbName() const
78 // Invoke the Security Agent to get a passphrase (other than for a Keychain)
80 void TempDatabase::getSecurePassphrase(const Context
&context
,
83 uint32 verify
= context
.getInt(CSSM_ATTRIBUTE_VERIFY_PASSPHRASE
, CSSMERR_CSSM_ATTRIBUTE_NOT_IN_CONTEXT
);
85 CssmData
*promptData
= context
.get
<CssmData
>(CSSM_ATTRIBUTE_PROMPT
);
86 const char *prompt
= NULL
;
91 QueryGenericPassphrase agentQuery
;
92 agentQuery
.inferHints(Server::process());
93 agentQuery(prompt
, verify
, passphrase
);
97 void TempDatabase::makeSecurePassphraseKey(const Context
&context
,
98 const AccessCredentials
*cred
,
99 const AclEntryPrototype
*owner
,
100 uint32 usage
, uint32 attrs
,
101 RefPointer
<Key
> &newKey
)
103 secdebug("SSdb", "requesting secure passphrase");
106 getSecurePassphrase(context
, passphrase
);
108 secdebug("SSdb", "wrapping securely-obtained passphrase as key");
110 // CssmKey rawKey(StringData(passphrase)) confuses gcc
111 StringData
passphraseData(passphrase
);
112 CssmKey
rawKey(passphraseData
);
113 rawKey
.algorithm(context
.algorithm());
114 rawKey
.blobType(CSSM_KEYBLOB_RAW
);
115 rawKey
.blobFormat(CSSM_KEYBLOB_WRAPPED_FORMAT_NONE
);
116 rawKey
.keyClass(CSSM_KEYCLASS_SESSION_KEY
);
118 CssmClient::UnwrapKey
unwrap(Server::csp(), CSSM_ALGID_NONE
);
120 unwrap(rawKey
, Key::KeySpec(usage
, attrs
), cspKey
);
122 newKey
= makeKey(cspKey
, attrs
& Key::managedAttributes
, owner
);
127 // Obtain "secure passphrases" for the CSP. Useful for PKCS 12.
129 void TempDatabase::generateKey(const Context
&context
,
130 const AccessCredentials
*cred
,
131 const AclEntryPrototype
*owner
,
132 uint32 usage
, uint32 attrs
,
133 RefPointer
<Key
> &newKey
)
135 switch (context
.algorithm())
137 case CSSM_ALGID_SECURE_PASSPHRASE
:
138 makeSecurePassphraseKey(context
, cred
, owner
, usage
, attrs
, newKey
);
141 LocalDatabase::generateKey(context
, cred
, owner
, usage
, attrs
, newKey
);
148 // Make a new TempKey
150 RefPointer
<Key
> TempDatabase::makeKey(const CssmKey
&newKey
,
151 uint32 moreAttributes
, const AclEntryPrototype
*owner
)
153 return new TempKey(*this, newKey
, moreAttributes
, owner
);