1 /* * Copyright (c) 2000-2008,2011-2014 Apple Inc. All Rights Reserved.
3 * @APPLE_LICENSE_HEADER_START@
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. Please obtain a copy of the License at
9 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * The Original Code and all software distributed under the License are
13 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
17 * Please see the License for the specific language governing rights and
18 * limitations under the License.
20 * @APPLE_LICENSE_HEADER_END@
24 @header SecKeychainItem
25 SecKeychainItem implements an item which may be stored in a SecKeychain, with publicly
26 visible attributes and encrypted data. Access to the data of an item is protected
27 using strong cryptographic algorithms.
30 #ifndef _SECURITY_SECKEYCHAINITEM_H_
31 #define _SECURITY_SECKEYCHAINITEM_H_
33 #include <AvailabilityMacros.h>
34 #include <CoreFoundation/CFData.h>
35 #include <Security/SecBase.h>
36 #include <Security/cssmapple.h>
38 #if defined(__cplusplus)
44 @abstract Represents a keychain item's class.
46 typedef FourCharCode SecItemClass
;
49 @enum ItemClassConstants
50 @abstract Specifies a keychain item's class code.
51 @constant kSecInternetPasswordItemClass Indicates that the item is an Internet password.
52 @constant kSecGenericPasswordItemClass Indicates that the item is a generic password.
53 @constant kSecAppleSharePasswordItemClass Indicates that the item is an AppleShare password.
54 Note: AppleShare passwords are no longer used by OS X, starting in Leopard (10.5). Use of this item class is deprecated in OS X 10.9 and later; kSecInternetPasswordItemClass should be used instead when storing or looking up passwords for an Apple Filing Protocol (AFP) server.
55 @constant kSecCertificateItemClass Indicates that the item is a digital certificate.
56 @constant kSecPublicKeyItemClass Indicates that the item is a public key.
57 @constant kSecPrivateKeyItemClass Indicates that the item is a private key.
58 @constant kSecSymmetricKeyItemClass Indicates that the item is a symmetric key.
59 @discussion The SecItemClass enumeration defines constants your application can use to specify the type of the keychain item you wish to create, dispose, add, delete, update, copy, or locate. You can also use these constants with the tag constant SecItemAttr.
63 kSecInternetPasswordItemClass
= 'inet',
64 kSecGenericPasswordItemClass
= 'genp',
65 kSecAppleSharePasswordItemClass
CF_ENUM_DEPRECATED(10_0
, 10_9
, NA
, NA
) = 'ashp',
66 kSecCertificateItemClass
= 0x80001000,
67 kSecPublicKeyItemClass
= 0x0000000F,
68 kSecPrivateKeyItemClass
= 0x00000010,
69 kSecSymmetricKeyItemClass
= 0x00000011
74 @abstract Represents a keychain item's attributes.
77 typedef FourCharCode SecItemAttr
;
80 @enum ItemAttributeConstants
81 @abstract Specifies keychain item attributes.
82 @constant kSecCreationDateItemAttr (read-only) Identifies the creation date attribute. You use this tag to get a value of type string that represents the date the item was created, expressed in Zulu Time format ("YYYYMMDDhhmmSSZ"). This format is identical to CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE (cssmtype.h). When specifying the creation date as input to a function (e.g. SecKeychainSearchCreateFromAttributes), you may alternatively provide a numeric value of type UInt32 or SInt64, expressed as seconds since 1/1/1904 (DateTimeUtils.h).
83 @constant kSecModDateItemAttr (read-only) Identifies the modification date attribute. You use this tag to get a value of type string that represents the last time the item was updated, expressed in Zulu Time format ("YYYYMMDDhhmmSSZ"). This format is identical to CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE (cssmtype.h). When specifying the modification date as input to a function (e.g. SecKeychainSearchCreateFromAttributes), you may alternatively provide a numeric value of type UInt32 or SInt64, expressed as seconds since 1/1/1904 (DateTimeUtils.h).
84 @constant kSecDescriptionItemAttr Identifies the description attribute. You use this tag to set or get a value of type string that represents a user-visible string describing this particular kind of item (e.g. "disk image password").
85 @constant kSecCommentItemAttr Identifies the comment attribute. You use this tag to set or get a value of type string that represents a user-editable string containing comments for this item.
86 @constant kSecCreatorItemAttr Identifies the creator attribute. You use this tag to set or get a value of type FourCharCode that represents the item's creator.
87 @constant kSecTypeItemAttr Identifies the type attribute. You use this tag to set or get a value of type FourCharCode that represents the item's type.
88 @constant kSecScriptCodeItemAttr Identifies the script code attribute. You use this tag to set or get a value of type ScriptCode that represents the script code for all strings. (Note: use of this attribute is deprecated; string attributes should always be stored in UTF-8 encoding.)
89 @constant kSecLabelItemAttr Identifies the label attribute. You use this tag to set or get a value of type string that represents a user-editable string containing the label for this item.
90 @constant kSecInvisibleItemAttr Identifies the invisible attribute. You use this tag to set or get a value of type Boolean that indicates whether the item is invisible (i.e. should not be displayed).
91 @constant kSecNegativeItemAttr Identifies the negative attribute. You use this tag to set or get a value of type Boolean that indicates whether there is a valid password associated with this keychain item. This is useful if your application doesn't want a password for some particular service to be stored in the keychain, but prefers that it always be entered by the user. The item (typically invisible and with zero-length data) acts as a placeholder to say "don't use me."
92 @constant kSecCustomIconItemAttr Identifies the custom icon attribute. You use this tag to set or get a value of type Boolean that indicates whether the item has an application-specific icon. To do this, you must also set the attribute value identified by the tag kSecTypeItemAttr to a file type for which there is a corresponding icon in the desktop database, and set the attribute value identified by the tag kSecCreatorItemAttr to an appropriate application creator type. If a custom icon corresponding to the item's type and creator can be found in the desktop database, it will be displayed by Keychain Access. Otherwise, default icons are used. (Note: use of this attribute is deprecated; custom icons for keychain items are not supported in Mac OS X.)
93 @constant kSecAccountItemAttr Identifies the account attribute. You use this tag to set or get a string that represents the user account. This attribute applies to generic, Internet, and AppleShare password items.
94 @constant kSecServiceItemAttr Identifies the service attribute. You use this tag to set or get a string that represents the service associated with this item. This attribute is unique to generic password items.
95 @constant kSecGenericItemAttr Identifies the generic attribute. You use this tag to set or get a value of untyped bytes that represents a user-defined attribute. This attribute is unique to generic password items.
96 @constant kSecSecurityDomainItemAttr Identifies the security domain attribute. You use this tag to set or get a value that represents the Internet security domain. This attribute is unique to Internet password items.
97 @constant kSecServerItemAttr Identifies the server attribute. You use this tag to set or get a value of type string that represents the Internet server's domain name or IP address. This attribute is unique to Internet password items.
98 @constant kSecAuthenticationTypeItemAttr Identifies the authentication type attribute. You use this tag to set or get a value of type SecAuthenticationType that represents the Internet authentication scheme. This attribute is unique to Internet password items.
99 @constant kSecPortItemAttr Identifies the port attribute. You use this tag to set or get a value of type UInt32 that represents the Internet port number. This attribute is unique to Internet password items.
100 @constant kSecPathItemAttr Identifies the path attribute. You use this tag to set or get a string value that represents the path. This attribute is unique to Internet password items.
101 @constant kSecVolumeItemAttr Identifies the volume attribute. You use this tag to set or get a string value that represents the AppleShare volume. This attribute is unique to AppleShare password items. Note: AppleShare passwords are no longer used by OS X as of Leopard (10.5); Internet password items are used instead.
102 @constant kSecAddressItemAttr Identifies the address attribute. You use this tag to set or get a string value that represents the AppleTalk zone name, or the IP or domain name that represents the server address. This attribute is unique to AppleShare password items. Note: AppleShare passwords are no longer used by OS X as of Leopard (10.5); Internet password items are used instead.
103 @constant kSecSignatureItemAttr Identifies the server signature attribute. You use this tag to set or get a value of type SecAFPServerSignature that represents the server signature block. This attribute is unique to AppleShare password items. Note: AppleShare passwords are no longer used by OS X as of Leopard (10.5); Internet password items are used instead.
104 @constant kSecProtocolItemAttr Identifies the protocol attribute. You use this tag to set or get a value of type SecProtocolType that represents the Internet protocol. This attribute applies to AppleShare and Internet password items.
105 @constant kSecCertificateType Indicates a CSSM_CERT_TYPE type.
106 @constant kSecCertificateEncoding Indicates a CSSM_CERT_ENCODING type.
107 @constant kSecCrlType Indicates a CSSM_CRL_TYPE type.
108 @constant kSecCrlEncoding Indicates a CSSM_CRL_ENCODING type.
109 @constant kSecAlias Indicates an alias.
110 @discussion To obtain information about a certificate, use the CDSA Certificate Library (CL) API. To obtain information about a key, use the SecKeyGetCSSMKey function and the CDSA Cryptographic Service Provider (CSP) API.
114 kSecCreationDateItemAttr
= 'cdat',
115 kSecModDateItemAttr
= 'mdat',
116 kSecDescriptionItemAttr
= 'desc',
117 kSecCommentItemAttr
= 'icmt',
118 kSecCreatorItemAttr
= 'crtr',
119 kSecTypeItemAttr
= 'type',
120 kSecScriptCodeItemAttr
= 'scrp',
121 kSecLabelItemAttr
= 'labl',
122 kSecInvisibleItemAttr
= 'invi',
123 kSecNegativeItemAttr
= 'nega',
124 kSecCustomIconItemAttr
= 'cusi',
125 kSecAccountItemAttr
= 'acct',
126 kSecServiceItemAttr
= 'svce',
127 kSecGenericItemAttr
= 'gena',
128 kSecSecurityDomainItemAttr
= 'sdmn',
129 kSecServerItemAttr
= 'srvr',
130 kSecAuthenticationTypeItemAttr
= 'atyp',
131 kSecPortItemAttr
= 'port',
132 kSecPathItemAttr
= 'path',
133 kSecVolumeItemAttr
= 'vlme',
134 kSecAddressItemAttr
= 'addr',
135 kSecSignatureItemAttr
= 'ssig',
136 kSecProtocolItemAttr
= 'ptcl',
137 kSecCertificateType
= 'ctyp',
138 kSecCertificateEncoding
= 'cenc',
139 kSecCrlType
= 'crtp',
140 kSecCrlEncoding
= 'crnc',
145 @typedef SecAFPServerSignature
146 @abstract Represents a 16-byte Apple File Protocol server signature block.
148 typedef UInt8 SecAFPServerSignature
[16];
151 @typedef SecPublicKeyHash
152 @abstract Represents a 20-byte public key hash.
154 typedef UInt8 SecPublicKeyHash
[20];
156 #pragma mark ---- Keychain Item Management ----
158 @function SecKeychainItemGetTypeID
159 @abstract Returns the type identifier of SecKeychainItem instances.
160 @result The CFTypeID of SecKeychainItem instances.
162 CFTypeID
SecKeychainItemGetTypeID(void);
165 @function SecKeychainItemModifyAttributesAndData
166 @abstract Updates an existing keychain item after changing its attributes or data.
167 @param itemRef A reference to the keychain item to modify.
168 @param attrList The list of attributes to modify, along with their new values. Pass NULL if you don't need to modify any attributes.
169 @param length The length of the buffer pointed to by data.
170 @param data Pointer to a buffer containing the data to store. Pass NULL if you don't need to modify the data.
171 @result A result code. See "Security Error Codes" (SecBase.h).
172 @discussion The keychain item is written to the keychain's permanent data store. If the keychain item has not previously been added to a keychain, a call to the SecKeychainItemModifyContent function does nothing and returns errSecSuccess.
174 OSStatus
SecKeychainItemModifyAttributesAndData(SecKeychainItemRef itemRef
, const SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
);
177 @function SecKeychainItemCreateFromContent
178 @abstract Creates a new keychain item from the supplied parameters.
179 @param itemClass A constant identifying the class of item to create.
180 @param attrList The list of attributes of the item to create.
181 @param length The length of the buffer pointed to by data.
182 @param data A pointer to a buffer containing the data to store.
183 @param initialAccess A reference to the access for this keychain item.
184 @param keychainRef A reference to the keychain in which to add the item.
185 @param itemRef On return, a pointer to a reference to the newly created keychain item (optional). When the item reference is no longer required, call CFRelease to deallocate memory occupied by the item.
186 @result A result code. See "Security Error Codes" (SecBase.h). In addition, errSecParam (-50) may be returned if not enough valid parameters are supplied, or errSecAllocate (-108) if there is not enough memory in the current heap zone to create the object.
188 OSStatus
SecKeychainItemCreateFromContent(SecItemClass itemClass
, SecKeychainAttributeList
*attrList
,
189 UInt32 length
, const void *data
, SecKeychainRef keychainRef
,
190 SecAccessRef initialAccess
, SecKeychainItemRef
*itemRef
);
193 @function SecKeychainItemModifyContent
194 @abstract Updates an existing keychain item after changing its attributes or data. This call should only be used in conjunction with SecKeychainItemCopyContent().
195 @param itemRef A reference to the keychain item to modify.
196 @param attrList The list of attributes to modify, along with their new values. Pass NULL if you don't need to modify any attributes.
197 @param length The length of the buffer pointed to by data.
198 @param data A pointer to a buffer containing the data to store. Pass NULL if you don't need to modify the data.
199 @result A result code. See "Security Error Codes" (SecBase.h).
201 OSStatus
SecKeychainItemModifyContent(SecKeychainItemRef itemRef
, const SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
);
204 @function SecKeychainItemCopyContent
205 @abstract Copies the data and/or attributes stored in the given keychain item. It is recommended that you use SecKeychainItemCopyAttributesAndData(). You must call SecKeychainItemFreeContent when you no longer need the attributes and data. If you want to modify the attributes returned here, use SecKeychainModifyContent().
206 @param itemRef A reference to the keychain item to modify.
207 @param itemClass On return, the item's class. Pass NULL if you don't require this information.
208 @param attrList On input, the list of attributes to retrieve. On output, the attributes are filled in. Pass NULL if you don't need to retrieve any attributes. You must call SecKeychainItemFreeContent when you no longer need the attributes.
209 @param length On return, the length of the buffer pointed to by outData.
210 @param outData On return, a pointer to a buffer containing the data in this item. Pass NULL if you don't need to retrieve the data. You must call SecKeychainItemFreeContent when you no longer need the data.
211 @result A result code. See "Security Error Codes" (SecBase.h). In addition, errSecParam (-50) may be returned if not enough valid parameters are supplied.
213 OSStatus
SecKeychainItemCopyContent(SecKeychainItemRef itemRef
, SecItemClass
*itemClass
, SecKeychainAttributeList
*attrList
, UInt32
*length
, void **outData
);
216 @function SecKeychainItemFreeContent
217 @abstract Releases the memory used by the keychain attribute list and the keychain data retrieved in a previous call to SecKeychainItemCopyContent.
218 @param attrList A pointer to the attribute list to release. Pass NULL to ignore this parameter.
219 @param data A pointer to the data buffer to release. Pass NULL to ignore this parameter.
221 OSStatus
SecKeychainItemFreeContent(SecKeychainAttributeList
*attrList
, void *data
);
224 @function SecKeychainItemCopyAttributesAndData
225 @abstract Copies the data and/or attributes stored in the given keychain item. You must call SecKeychainItemFreeAttributesAndData when you no longer need the attributes and data. If you want to modify the attributes returned here, use SecKeychainModifyAttributesAndData.
226 @param itemRef A reference to the keychain item to copy.
227 @param info A list of tags and formats of the attributes you wish to retrieve. Pass NULL if you don't need to retrieve any attributes. You can call SecKeychainAttributeInfoForItemID to obtain a list with all possible attribute tags and formats for the item's class.
228 @param itemClass On return, the item's class. Pass NULL if you don't require this information.
229 @param attrList On return, a pointer to the list of retrieved attributes. Pass NULL if you don't need to retrieve any attributes. You must call SecKeychainItemFreeAttributesAndData when you no longer need this list.
230 @param length On return, the length of the buffer pointed to by outData.
231 @param outData On return, a pointer to a buffer containing the data in this item. Pass NULL if you don't need to retrieve the data. You must call SecKeychainItemFreeAttributesAndData when you no longer need the data.
232 @result A result code. See "Security Error Codes" (SecBase.h). In addition, errSecParam (-50) may be returned if not enough valid parameters are supplied.
234 OSStatus
SecKeychainItemCopyAttributesAndData(SecKeychainItemRef itemRef
, SecKeychainAttributeInfo
*info
, SecItemClass
*itemClass
, SecKeychainAttributeList
**attrList
, UInt32
*length
, void **outData
);
237 @function SecKeychainItemFreeAttributesAndData
238 @abstract Releases the memory used by the keychain attribute list and the keychain data retrieved in a previous call to SecKeychainItemCopyAttributesAndData.
239 @param attrList A pointer to the attribute list to release. Pass NULL to ignore this parameter.
240 @param data A pointer to the data buffer to release. Pass NULL to ignore this parameter.
241 @result A result code. See "Security Error Codes" (SecBase.h).
243 OSStatus
SecKeychainItemFreeAttributesAndData(SecKeychainAttributeList
*attrList
, void *data
);
246 @function SecKeychainItemDelete
247 @abstract Deletes a keychain item from the default keychain's permanent data store.
248 @param itemRef A keychain item reference of the item to delete.
249 @result A result code. See "Security Error Codes" (SecBase.h).
250 @discussion If itemRef has not previously been added to the keychain, SecKeychainItemDelete does nothing and returns errSecSuccess. IMPORTANT: SecKeychainItemDelete does not dispose the memory occupied by the item reference itself; use the CFRelease function when you are completely finished with an item.
252 OSStatus
SecKeychainItemDelete(SecKeychainItemRef itemRef
);
255 @function SecKeychainItemCopyKeychain
256 @abstract Copies an existing keychain reference from a keychain item.
257 @param itemRef A keychain item reference.
258 @param keychainRef On return, the keychain reference for the specified item. Release this reference by calling the CFRelease function.
259 @result A result code. See "Security Error Codes" (SecBase.h).
261 OSStatus
SecKeychainItemCopyKeychain(SecKeychainItemRef itemRef
, SecKeychainRef
*keychainRef
);
264 @function SecKeychainItemCreateCopy
265 @abstract Copies a keychain item.
266 @param itemRef A reference to the keychain item to copy.
267 @param destKeychainRef A reference to the keychain in which to insert the copied keychain item.
268 @param initialAccess The initial access for the copied keychain item.
269 @param itemCopy On return, a reference to the copied keychain item.
270 @result A result code. See "Security Error Codes" (SecBase.h).
272 OSStatus
SecKeychainItemCreateCopy(SecKeychainItemRef itemRef
, SecKeychainRef destKeychainRef
,
273 SecAccessRef initialAccess
, SecKeychainItemRef
*itemCopy
);
276 @function SecKeychainItemCreatePersistentReference
277 @abstract Returns a CFDataRef which can be used as a persistent reference to the given keychain item. The data obtained can be turned back into a SecKeychainItemRef later by calling SecKeychainItemCopyFromPersistentReference().
278 @param itemRef A reference to a keychain item.
279 @param persistentItemRef On return, a CFDataRef containing a persistent reference. You must release this data reference by calling the CFRelease function.
280 @result A result code. See "Security Error Codes" (SecBase.h).
282 OSStatus
SecKeychainItemCreatePersistentReference(SecKeychainItemRef itemRef
, CFDataRef
*persistentItemRef
);
286 @function SecKeychainItemCopyFromPersistentReference
287 @abstract Returns a SecKeychainItemRef, given a persistent reference previously obtained by calling SecKeychainItemCreatePersistentReference().
288 @param persistentItemRef A CFDataRef containing a persistent reference to a keychain item.
289 @param itemRef On return, a SecKeychainItemRef for the keychain item described by the persistent reference. You must release this item reference by calling the CFRelease function.
290 @result A result code. See "Security Error Codes" (SecBase.h).
292 OSStatus
SecKeychainItemCopyFromPersistentReference(CFDataRef persistentItemRef
, SecKeychainItemRef
*itemRef
);
295 #pragma mark ---- CSSM Bridge Functions ----
297 @function SecKeychainItemGetDLDBHandle
298 @abstract Returns the CSSM_DL_DB_HANDLE for a given keychain item reference.
299 @param keyItemRef A keychain item reference.
300 @param dldbHandle On return, a CSSM_DL_DB_HANDLE for the keychain database containing the given item. The handle is valid until the keychain reference is released.
301 @result A result code. See "Security Error Codes" (SecBase.h).
302 @discussion This API is deprecated for 10.7. It should no longer be needed.
304 OSStatus
SecKeychainItemGetDLDBHandle(SecKeychainItemRef keyItemRef
, CSSM_DL_DB_HANDLE
*dldbHandle
)
305 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
308 @function SecKeychainItemGetUniqueRecordID
309 @abstract Returns a CSSM_DB_UNIQUE_RECORD for the given keychain item reference.
310 @param itemRef A keychain item reference.
311 @param uniqueRecordID On return, a pointer to a CSSM_DB_UNIQUE_RECORD structure for the given item. The unique record is valid until the item reference is released.
312 @result A result code. See "Security Error Codes" (SecBase.h).
313 @discussion This API is deprecated for 10.7. It should no longer be needed.
315 OSStatus
SecKeychainItemGetUniqueRecordID(SecKeychainItemRef itemRef
, const CSSM_DB_UNIQUE_RECORD
**uniqueRecordID
)
316 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
318 #pragma mark ---- Keychain Item Access Management ----
320 @function SecKeychainItemCopyAccess
321 @abstract Copies the access of a given keychain item.
322 @param itemRef A reference to a keychain item.
323 @param access On return, a reference to the keychain item's access.
324 @result A result code. See "Security Error Codes" (SecBase.h).
326 OSStatus
SecKeychainItemCopyAccess(SecKeychainItemRef itemRef
, SecAccessRef
*access
);
329 @function SecKeychainItemSetAccess
330 @abstract Sets the access of a given keychain item.
331 @param itemRef A reference to a keychain item.
332 @param access A reference to an access to replace the keychain item's current access.
333 @result A result code. See "Security Error Codes" (SecBase.h).
335 OSStatus
SecKeychainItemSetAccess(SecKeychainItemRef itemRef
, SecAccessRef access
);
337 #if defined(__cplusplus)
341 #endif /* !_SECURITY_SECKEYCHAINITEM_H_ */