]> git.saurik.com Git - apple/security.git/blob - Keychain/Keychains.h
57fe785b0f48c043be93ecd794d588e22c2d4bdc
[apple/security.git] / Keychain / Keychains.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18 //
19 // Keychains.h - The Keychain class
20 //
21 #ifndef _SECURITY_KEYCHAINS_H_
22 #define _SECURITY_KEYCHAINS_H_
23
24 #include <Security/cspclient.h>
25 #include <Security/dlclient.h>
26 #include <Security/refcount.h>
27 #include <Security/utilities.h>
28 #include <Security/DLDBListCFPref.h>
29 #include <Security/SecRuntime.h>
30 #include <Security/SecKeychain.h>
31 #include <Security/SecKeychainItem.h>
32 #include <memory>
33
34 namespace Security
35 {
36
37 namespace KeychainCore
38 {
39
40 class KCCursor;
41 class Item;
42 class PrimaryKey;
43 class StorageManager;
44
45
46 class KeychainSchemaImpl : public RefCount
47 {
48 NOCOPY(KeychainSchemaImpl)
49 public:
50 friend class KeychainSchema;
51 protected:
52 KeychainSchemaImpl(const CssmClient::Db &db);
53 public:
54 ~KeychainSchemaImpl();
55
56 CSSM_DB_ATTRIBUTE_FORMAT attributeFormatFor(CSSM_DB_RECORDTYPE recordType, uint32 attributeId) const;
57 const CssmAutoDbRecordAttributeInfo &primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType) const;
58
59 bool operator <(const KeychainSchemaImpl &other) const;
60 bool operator ==(const KeychainSchemaImpl &other) const;
61
62 void getAttributeInfoForRecordType(CSSM_DB_RECORDTYPE recordType, SecKeychainAttributeInfo **Info) const;
63 CssmDbAttributeInfo attributeInfoFor(CSSM_DB_RECORDTYPE recordType, uint32 attributeId) const;
64 bool hasAttribute(CSSM_DB_RECORDTYPE recordType, uint32 attributeId) const;
65
66 private:
67 typedef map<CSSM_DB_RECORDTYPE, CssmAutoDbRecordAttributeInfo *> PrimaryKeyInfoMap;
68 PrimaryKeyInfoMap mPrimaryKeyInfoMap;
69
70 typedef map<uint32, CSSM_DB_ATTRIBUTE_FORMAT> RelationInfoMap;
71 typedef map<CSSM_DB_RECORDTYPE, RelationInfoMap> DatabaseInfoMap;
72 DatabaseInfoMap mDatabaseInfoMap;
73 private:
74 const RelationInfoMap &relationInfoMapFor(CSSM_DB_RECORDTYPE recordType) const;
75 };
76
77
78 class KeychainSchema : public RefPointer<KeychainSchemaImpl>
79 {
80 public:
81 KeychainSchema() {}
82 KeychainSchema(KeychainSchemaImpl *impl) : RefPointer<KeychainSchemaImpl>(impl) {}
83 KeychainSchema(const CssmClient::Db &db) : RefPointer<KeychainSchemaImpl>(new KeychainSchemaImpl(db)) {}
84
85 bool operator <(const KeychainSchema &other) const
86 { return ptr && other.ptr ? *ptr < *other.ptr : ptr < other.ptr; }
87 bool operator ==(const KeychainSchema &other) const
88 { return ptr && other.ptr ? *ptr == *other.ptr : ptr == other.ptr; }
89
90 private:
91 typedef KeychainSchemaImpl Impl;
92 };
93
94
95 class KeychainImpl : public SecCFObject
96 {
97 NOCOPY(KeychainImpl)
98 public:
99 friend class Keychain;
100 friend class ItemImpl;
101 protected:
102 KeychainImpl(const CssmClient::Db &db);
103
104 protected:
105 // Methods called by ItemImpl;
106 void didUpdate(ItemImpl *inItemImpl, PrimaryKey &oldPK,
107 PrimaryKey &newPK);
108
109 public:
110 virtual ~KeychainImpl();
111
112 bool operator ==(const KeychainImpl &) const;
113
114 // Item calls
115 void add(Item &item); // item must not be persistant. Item will change.
116 void deleteItem(Item &item); // item must be persistant.
117
118 // Keychain calls
119 void create(UInt32 passwordLength, const void *inPassword);
120 void create(ConstStringPtr inPassword);
121 void create();
122 void create(const ResourceControlContext *rcc);
123 void open(); // There is no close since the client lib deals with that itself. might throw
124
125 // Locking and unlocking a keychain.
126 void lock();
127 void unlock();
128 void unlock(const CssmData &password);
129 void unlock(ConstStringPtr password); // @@@ This has a length limit, we should remove it.
130
131 void getSettings(uint32 &outIdleTimeOut, bool &outLockOnSleep);
132 void setSettings(uint32 inIdleTimeOut, bool inLockOnSleep);
133
134 // Passing in NULL for either oldPassword or newPassword will cause them to be prompted for.
135 // To specify a zero length password in either case the oldPasswordLength or newPasswordLength
136 // value must be 0 and the oldPassword or newPassword must not be NULL.
137 void changePassphrase(UInt32 oldPasswordLength, const void *oldPassword,
138 UInt32 newPasswordLength, const void *newPassword);
139 void changePassphrase(ConstStringPtr oldPassword, ConstStringPtr newPassword);
140
141 void authenticate(const CSSM_ACCESS_CREDENTIALS *cred); // Does not do an unlock.
142
143 const char *name() const { return mDb->name(); }
144 UInt32 status() const;
145 bool exists();
146 bool isActive() const;
147
148 KCCursor createCursor(const SecKeychainAttributeList *attrList);
149 KCCursor createCursor(SecItemClass itemClass, const SecKeychainAttributeList *attrList);
150 CssmClient::Db database() { return mDb; }
151 DLDbIdentifier dLDbIdentifier() const { return mDb->dlDbIdentifier(); }
152
153 CssmClient::CSP csp();
154
155 PrimaryKey makePrimaryKey(CSSM_DB_RECORDTYPE recordType, CssmClient::DbUniqueRecord &uniqueId);
156 void gatherPrimaryKeyAttributes(CssmClient::DbAttributes& primaryKeyAttrs);
157
158 const CssmAutoDbRecordAttributeInfo &primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType);
159
160 Item item(const PrimaryKey& primaryKey);
161 Item item(CSSM_DB_RECORDTYPE recordType, CssmClient::DbUniqueRecord &uniqueId);
162
163 CssmDbAttributeInfo attributeInfoFor(CSSM_DB_RECORDTYPE recordType, UInt32 tag);
164 void getAttributeInfoForItemID(CSSM_DB_RECORDTYPE itemID, SecKeychainAttributeInfo **Info);
165 static void freeAttributeInfo(SecKeychainAttributeInfo *Info);
166 KeychainSchema keychainSchema();
167
168 private:
169 void addItem(const PrimaryKey &primaryKey, ItemImpl *dbItemImpl);
170 void removeItem(const PrimaryKey &primaryKey, const ItemImpl *inItemImpl);
171
172 CssmClient::Db mDb;
173 Mutex mDbItemMapLock;
174 typedef map<PrimaryKey, ItemImpl *> DbItemMap;
175 DbItemMap mDbItemMap;
176
177 KeychainSchema mKeychainSchema;
178 };
179
180
181 class Keychain : public RefPointer<KeychainImpl>
182 {
183 public:
184 Keychain() {}
185 Keychain(KeychainImpl *impl) : RefPointer<KeychainImpl>(impl) {}
186
187 static Keychain optional(SecKeychainRef handle);
188
189 private:
190 friend class StorageManager;
191 Keychain(const CssmClient::Db &db)
192 : RefPointer<KeychainImpl>(new KeychainImpl(db)) {}
193
194 typedef KeychainImpl Impl;
195 };
196
197
198 } // end namespace KeychainCore
199
200 } // end namespace Security
201
202 #endif // !_SECURITY_KEYCHAINS_H_