]> git.saurik.com Git - apple/security.git/blob - Keychain/Keychains.h
Security-30.1.tar.gz
[apple/security.git] / Keychain / Keychains.h
1 /*
2 * Copyright (c) 2000-2001 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 /*
20 File: Keychains.h
21
22 Contains: The keychain class
23
24 Copyright: 2000 by Apple Computer, Inc., all rights reserved.
25
26 To Do:
27 */
28
29 #ifndef _H_KEYCHAINS_
30 #define _H_KEYCHAINS_
31
32 #include <Security/dlclient.h>
33 #include <Security/refcount.h>
34 #include <Security/utilities.h>
35 #include <Security/DLDBListCFPref.h>
36 #include <Security/Refs.h>
37 #include <Security/SecKeychainAPI.h>
38 #include <Security/SecKeychainAPIPriv.h>
39 #include <memory>
40
41 namespace Security
42 {
43
44 namespace KeychainCore
45 {
46
47 class KCCursor;
48 class Item;
49 class ItemImpl;
50 class Keychain;
51 class PrimaryKey;
52 class StorageManager;
53
54
55 class KeychainSchemaImpl : public ReferencedObject
56 {
57 public:
58 KeychainSchemaImpl(const CssmClient::Db &db);
59 ~KeychainSchemaImpl();
60
61 CSSM_DB_ATTRIBUTE_FORMAT attributeFormatFor(CSSM_DB_RECORDTYPE recordType, uint32 attributeId) const;
62 const CssmAutoDbRecordAttributeInfo &primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType);
63
64 bool operator <(const KeychainSchemaImpl &other) const;
65 bool operator ==(const KeychainSchemaImpl &other) const;
66
67 void getAttributeInfoForRecordType(CSSM_DB_RECORDTYPE recordType, SecKeychainAttributeInfo **Info);
68 CssmDbAttributeInfo attributeInfoForTag(UInt32 tag);
69
70 private:
71 typedef map<CSSM_DB_RECORDTYPE, CssmAutoDbRecordAttributeInfo *> PrimaryKeyInfoMap;
72 PrimaryKeyInfoMap mPrimaryKeyInfoMap;
73
74 typedef map<uint32, CSSM_DB_ATTRIBUTE_FORMAT> RelationInfoMap;
75 typedef map<CSSM_DB_RECORDTYPE, RelationInfoMap> DatabaseInfoMap;
76 DatabaseInfoMap mDatabaseInfoMap;
77
78
79 };
80
81
82 class KeychainSchema : public RefPointer<KeychainSchemaImpl>
83 {
84 public:
85 KeychainSchema() {}
86 KeychainSchema(KeychainSchemaImpl *impl) : RefPointer<KeychainSchemaImpl>(impl) {}
87 KeychainSchema(const CssmClient::Db &db) : RefPointer<KeychainSchemaImpl>(new KeychainSchemaImpl(db)) {}
88
89 bool operator <(const KeychainSchema &other) const
90 { return ptr && other.ptr ? *ptr < *other.ptr : ptr < other.ptr; }
91 bool operator ==(const KeychainSchema &other) const
92 { return ptr && other.ptr ? *ptr == *other.ptr : ptr == other.ptr; }
93
94 private:
95 typedef KeychainSchemaImpl Impl;
96 };
97
98
99 class KeychainImpl : public ReferencedObject
100 {
101 NOCOPY(KeychainImpl)
102 friend class Keychain;
103 protected:
104 KeychainImpl(const CssmClient::Db &db);
105
106 protected:
107 // Methods called by ItemImpl;
108 friend class ItemImpl;
109
110 void didUpdate(ItemImpl *inItemImpl, PrimaryKey &oldPK,
111 PrimaryKey &newPK);
112
113 public:
114 virtual ~KeychainImpl();
115
116 // Item calls
117 void add(Item &item); // item must not be persistant. Item will change.
118 void deleteItem(Item &item); // item must be persistant.
119
120 // Keychain calls
121 void create(UInt32 passwordLength, const void *inPassword);
122 void create(ConstStringPtr inPassword);
123 void create();
124 void create(const ResourceControlContext *rcc);
125 void open(); // There is no close since the client lib deals with that itself. might throw
126
127 // Locking and unlocking a keychain.
128 void lock();
129 void unlock();
130 void unlock(const CssmData &password);
131 void unlock(ConstStringPtr password); // @@@ This has a length limit, we should remove it.
132
133 void getSettings(uint32 &outIdleTimeOut, bool &outLockOnSleep);
134 void setSettings(uint32 inIdleTimeOut, bool inLockOnSleep);
135
136 // Passing in NULL for either oldPassword or newPassword will cause them to be prompted for.
137 // To specify a zero length password in either case the oldPasswordLength or newPasswordLength
138 // value must be 0 and the oldPassword or newPassword must not be NULL.
139 void changePassphrase(UInt32 oldPasswordLength, const void *oldPassword,
140 UInt32 newPasswordLength, const void *newPassword);
141 void changePassphrase(ConstStringPtr oldPassword, ConstStringPtr newPassword);
142
143 void authenticate(const CSSM_ACCESS_CREDENTIALS *cred); // Does not do an unlock.
144
145 const char *name() const { return mDb->name(); }
146 UInt32 status() const;
147 bool exists();
148 bool isActive() const;
149
150 KCCursor createCursor(const SecKeychainAttributeList *attrList);
151 KCCursor createCursor(SecItemClass itemClass, const SecKeychainAttributeList *attrList);
152 CssmClient::Db database() { return mDb; }
153 DLDbIdentifier dLDbIdentifier() const { return mDb->dlDbIdentifier(); }
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 attributeInfoForTag(UInt32 tag);
164 void getAttributeInfoForItemID(CSSM_DB_RECORDTYPE itemID, SecKeychainAttributeInfo **Info);
165 static void freeAttributeInfo(SecKeychainAttributeInfo *Info);
166
167 private:
168 KeychainSchema keychainSchema();
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 typedef Ref<Keychain, KeychainImpl, SecKeychainRef, errSecInvalidKeychain> KeychainRef;
199
200 } // end namespace KeychainCore
201
202 } // end namespace Security
203
204 #endif /* _H_KEYCHAINS_ */
205