]> git.saurik.com Git - apple/security.git/blob - libsecurity_keychain/lib/Item.h
Security-55179.13.tar.gz
[apple/security.git] / libsecurity_keychain / lib / Item.h
1 /*
2 * Copyright (c) 2000-2004 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 // Item.h
27 //
28 #ifndef _SECURITY_ITEM_H_
29 #define _SECURITY_ITEM_H_
30
31 #include <security_keychain/Keychains.h>
32 #include <security_keychain/PrimaryKey.h>
33 #include <security_cdsa_client/securestorage.h>
34 #include <security_keychain/Access.h>
35
36 namespace Security
37 {
38
39 using namespace CssmClient;
40
41 namespace KeychainCore
42 {
43 class Keychain;
44
45 class ItemImpl : public SecCFObject
46 {
47 public:
48 SECCFFUNCTIONS(ItemImpl, SecKeychainItemRef, errSecInvalidItemRef, gTypes().ItemImpl)
49
50 friend class Item;
51 friend class KeychainImpl;
52 protected:
53
54 // new item constructors
55 ItemImpl(SecItemClass itemClass, OSType itemCreator, UInt32 length, const void* data, bool inhibitCheck = false);
56
57 ItemImpl(SecItemClass itemClass, SecKeychainAttributeList *attrList, UInt32 length, const void* data);
58
59 // db item contstructor
60 ItemImpl(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId);
61
62 // PrimaryKey item contstructor
63 ItemImpl(const Keychain &keychain, const PrimaryKey &primaryKey);
64
65 public:
66
67 static ItemImpl* make(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId);
68 static ItemImpl* make(const Keychain &keychain, const PrimaryKey &primaryKey);
69
70 ItemImpl(ItemImpl &item);
71
72 // Return true if we got the attribute, false if we only got the actualLength.
73 void getAttributeFrom(CssmDbAttributeData *data, SecKeychainAttribute &attr, UInt32 *actualLength);
74 void getClass(SecKeychainAttribute &attr, UInt32 *actualLength);
75
76 PrimaryKey addWithCopyInfo(Keychain &keychain, bool isCopy);
77 Mutex* getMutexForObject();
78
79 protected:
80 // Methods called by KeychainImpl;
81
82 // Add the receiver to keychain
83 virtual PrimaryKey add(Keychain &keychain);
84
85 // Get the default value for an attribute
86 static const CSSM_DATA &defaultAttributeValue(const CSSM_DB_ATTRIBUTE_INFO &info);
87
88 public:
89 virtual ~ItemImpl();
90 bool isPersistent();
91 bool isModified();
92
93 virtual void update();
94
95 void aboutToDestruct();
96
97 // put a copy of the item into a given keychain
98 virtual Item copyTo(const Keychain &keychain, Access *newAccess = NULL);
99
100 CSSM_DB_RECORDTYPE recordType();
101
102 // Used for writing the record to the database.
103 CssmClient::DbUniqueRecord dbUniqueRecord();
104 const CssmClient::DbAttributes *modifiedAttributes();
105 const CssmData *modifiedData();
106 virtual void didModify(); // Forget any attributes and data we just wrote to the db
107
108 Keychain keychain();
109 PrimaryKey primaryKey();
110 bool operator < (const ItemImpl &other);
111
112 void getAttribute(SecKeychainAttribute& attr, UInt32 *actualLength);
113 void getData(CssmDataContainer& outData);
114
115 void modifyContent(const SecKeychainAttributeList *attrList, UInt32 dataLength, const void *inData);
116 void getContent(SecItemClass *itemClass, SecKeychainAttributeList *attrList, UInt32 *length, void **outData);
117 static void freeContent(SecKeychainAttributeList *attrList, void *data);
118 static void freeAttributesAndData(SecKeychainAttributeList *attrList, void *data);
119
120 void getAttributesAndData(SecKeychainAttributeInfo *info, SecItemClass *itemClass,
121 SecKeychainAttributeList **attrList, UInt32 *length, void **outData);
122 void modifyAttributesAndData(const SecKeychainAttributeList *attrList, UInt32 dataLength, const void *inData);
123
124 void setAttribute(SecKeychainAttribute& attr);
125 void setAttribute(const CssmDbAttributeInfo &info, const CssmPolyData &data);
126 void setData(UInt32 length,const void *data);
127 void setAccess(Access *newAccess);
128 void copyRecordIdentifier(CSSM_DATA &data);
129 SSGroup group();
130
131 void getContent(DbAttributes *dbAttributes, CssmDataContainer *itemData);
132 void getLocalContent(SecKeychainAttributeList *attributeList, UInt32 *outLength, void **outData);
133
134 bool useSecureStorage(const CssmClient::Db &db);
135 virtual void willRead();
136
137 // create a persistent reference to this item
138 void copyPersistentReference(CFDataRef &outDataRef);
139
140 // for keychain syncing
141 void doNotEncrypt () {mDoNotEncrypt = true;}
142
143 // for posting events on this item
144 void postItemEvent (SecKeychainEvent theEvent);
145
146 // Only call these functions while holding globals().apiLock.
147 bool inCache() const throw() { return mInCache; }
148 void inCache(bool inCache) throw() { mInCache = inCache; }
149
150 /* For binding to extended attributes. */
151 virtual const CssmData &itemID();
152
153 protected:
154 // new item members
155 RefPointer<CssmDataContainer> mData;
156 auto_ptr<CssmClient::DbAttributes> mDbAttributes;
157 SecPointer<Access> mAccess;
158
159 // db item members
160 CssmClient::DbUniqueRecord mUniqueId;
161 Keychain mKeychain;
162 PrimaryKey mPrimaryKey;
163
164 private:
165 // keychain syncing flags
166 bool mDoNotEncrypt;
167
168 // mInCache is protected by globals().apiLock
169 // True iff we are in the cache of items in mKeychain
170 bool mInCache;
171
172 protected:
173 Mutex mMutex;
174 };
175
176
177 class Item : public SecPointer<ItemImpl>
178 {
179 public:
180 Item();
181 Item(ItemImpl *impl);
182 Item(SecItemClass itemClass, OSType itemCreator, UInt32 length, const void* data, bool inhibitCheck);
183 Item(SecItemClass itemClass, SecKeychainAttributeList *attrList, UInt32 length, const void* data);
184 Item(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId);
185 Item(const Keychain &keychain, const PrimaryKey &primaryKey);
186 Item(ItemImpl &item);
187 };
188
189 CFIndex GetItemRetainCount(Item& item);
190
191 } // end namespace KeychainCore
192
193 } // end namespace Security
194
195 #endif // !_SECURITY_ITEM_H_