1/*
2 * Copyright (c) 2004-2005 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// localdatabase - locally implemented database using internal CSP cryptography
27//
28// A LocalDatabase manages keys with a locally resident AppleCSP.
29// This is an abstract class useful for subclassing.
30//
31#ifndef _H_LOCALDATABASE
32#define _H_LOCALDATABASE
33
34#include "database.h"
35
36class LocalKey;
37
38class LocalDbCommon : public DbCommon {
39public:
40	LocalDbCommon(Session &ssn) : DbCommon(ssn) { }
41
42	Mutex &uiLock()  { return mUILock; }
43
44private:
45	// Contract: callers shall not simultaneously hold mUILock and the
46	// DbCommon lock.  StSyncLock coordinates them to uphold the contract.
47	Mutex mUILock;				// serializes user interaction
48};
49
50//
51// A Database object represents an Apple CSP/DL open database (DL/DB) object.
52// It maintains its protected semantic state (including keys) and provides controlled
53// access.
54//
55class LocalDatabase : public Database {
56public:
57	LocalDatabase(Process &proc);
58
59public:
60	//void releaseKey(Key &key);
61	void queryKeySizeInBits(Key &key, CssmKeySize &result);
62
63	// service calls
64	void generateSignature(const Context &context, Key &key, CSSM_ALGORITHMS signOnlyAlgorithm,
65		const CssmData &data, CssmData &signature);
66	void verifySignature(const Context &context, Key &key, CSSM_ALGORITHMS verifyOnlyAlgorithm,
67		const CssmData &data, const CssmData &signature);
68	void generateMac(const Context &context, Key &key,
69		const CssmData &data, CssmData &mac);
70	void verifyMac(const Context &context, Key &key,
71		const CssmData &data, const CssmData &mac);
72
73	void encrypt(const Context &context, Key &key, const CssmData &clear, CssmData &cipher);
74	void decrypt(const Context &context, Key &key, const CssmData &cipher, CssmData &clear);
75
76	void generateKey(const Context &context,
77		const AccessCredentials *cred, const AclEntryPrototype *owner,
78		CSSM_KEYUSE usage, CSSM_KEYATTR_FLAGS attrs, RefPointer<Key> &newKey);
79	void generateKey(const Context &context,
80		const AccessCredentials *cred, const AclEntryPrototype *owner,
81		CSSM_KEYUSE pubUsage, CSSM_KEYATTR_FLAGS pubAttrs,
82		CSSM_KEYUSE privUsage, CSSM_KEYATTR_FLAGS privAttrs,
83		RefPointer<Key> &publicKey, RefPointer<Key> &privateKey);
84	void deriveKey(const Context &context, Key *key,
85		const AccessCredentials *cred, const AclEntryPrototype *owner,
86		CssmData *param, uint32 usage, uint32 attrs, RefPointer<Key> &derivedKey);
87
88    void wrapKey(const Context &context, const AccessCredentials *cred,
89		Key *wrappingKey, Key &keyToBeWrapped,
90        const CssmData &descriptiveData, CssmKey &wrappedKey);
91	void unwrapKey(const Context &context,
92		const AccessCredentials *cred, const AclEntryPrototype *owner,
93		Key *wrappingKey, Key *publicKey, CSSM_KEYUSE usage, CSSM_KEYATTR_FLAGS attrs,
94		const CssmKey wrappedKey, RefPointer<Key> &unwrappedKey, CssmData &descriptiveData);
95
96    void getOutputSize(const Context &context, Key &key, uint32 inputSize, bool encrypt, uint32 &result);
97
98protected:
99	virtual RefPointer<Key> makeKey(const CssmKey &newKey, uint32 moreAttributes,
100		const AclEntryPrototype *owner) = 0;
101};
102
103#endif //_H_LOCALDATABASE
104