2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * SecDbItem.c - CoreFoundation-based constants and functions representing
26 * database items (certificates, keys, identities, and passwords.)
31 #undef SECUREOBJECTSYNC
32 #undef SHAREDWEBCREDENTIALS
35 #include "keychain/securityd/SecDbItem.h"
36 #include "keychain/securityd/SecDbKeychainItem.h"
37 #include "keychain/securityd/SecItemDb.h"
38 #include <utilities/SecCFWrappers.h>
39 #include <utilities/SecCFCCWrappers.h>
40 #include <utilities/der_date.h>
41 #include <utilities/der_plist.h>
42 #include <utilities/debugging.h>
44 #include <Security/SecBasePriv.h>
45 #include <Security/SecInternal.h>
46 #include <corecrypto/ccsha1.h>
47 #include <Security/SecItem.h>
48 #include <Security/SecItemPriv.h>
49 #include <Security/SecAccessControl.h>
50 #include <Security/SecAccessControlPriv.h>
51 #include "keychain/securityd/SecItemSchema.h"
53 #include <keychain/ckks/CKKS.h>
55 // MARK: type converters
57 CFStringRef
copyString(CFTypeRef obj
) {
58 CFTypeID tid
= CFGetTypeID(obj
);
59 if (tid
== CFStringGetTypeID()) {
60 return CFStringCreateCopy(0, obj
);
61 }else if (tid
== CFDataGetTypeID()) {
62 return CFStringCreateFromExternalRepresentation(0, obj
, kCFStringEncodingUTF8
);
63 } else if (tid
== CFUUIDGetTypeID()) {
64 return CFUUIDCreateString(NULL
, obj
);
70 CFDataRef
copyData(CFTypeRef obj
) {
71 CFTypeID tid
= CFGetTypeID(obj
);
72 if (tid
== CFDataGetTypeID()) {
73 return CFDataCreateCopy(0, obj
);
74 } else if (tid
== CFStringGetTypeID()) {
75 return CFStringCreateExternalRepresentation(0, obj
, kCFStringEncodingUTF8
, 0);
76 } else if (tid
== CFNumberGetTypeID()) {
78 CFNumberGetValue(obj
, kCFNumberSInt32Type
, &value
);
79 return CFDataCreate(0, (const UInt8
*)&value
, sizeof(value
));
85 CFTypeRef
copyUUID(CFTypeRef obj
) {
86 CFTypeID tid
= CFGetTypeID(obj
);
87 if (tid
== CFDataGetTypeID()) {
88 CFIndex length
= CFDataGetLength(obj
);
89 if (length
!= 0 && length
!= 16)
91 return CFDataCreateCopy(NULL
, obj
);
92 } else if (tid
== CFNullGetTypeID()) {
93 return CFDataCreate(NULL
, NULL
, 0);
94 } else if (tid
== CFUUIDGetTypeID()) {
95 CFUUIDBytes uuidbytes
= CFUUIDGetUUIDBytes(obj
);
96 CFDataRef uuiddata
= CFDataCreate(NULL
, (void*) &uuidbytes
, sizeof(uuidbytes
));
104 CFTypeRef
copyBlob(CFTypeRef obj
) {
105 CFTypeID tid
= CFGetTypeID(obj
);
106 if (tid
== CFDataGetTypeID()) {
107 return CFDataCreateCopy(0, obj
);
108 } else if (tid
== CFStringGetTypeID()) {
109 return CFStringCreateCopy(0, obj
);
110 } else if (tid
== CFNumberGetTypeID()) {
118 CFDataRef
copySHA1(CFTypeRef obj
) {
119 CFTypeID tid
= CFGetTypeID(obj
);
120 if (tid
== CFDataGetTypeID() && CFDataGetLength(obj
) == CCSHA1_OUTPUT_SIZE
) {
121 return CFDataCreateCopy(CFGetAllocator(obj
), obj
);
127 CFTypeRef
copyNumber(CFTypeRef obj
) {
128 CFTypeID tid
= CFGetTypeID(obj
);
129 if (tid
== CFNumberGetTypeID()) {
132 } else if (tid
== CFBooleanGetTypeID()) {
133 SInt32 value
= CFBooleanGetValue(obj
);
134 return CFNumberCreate(0, kCFNumberSInt32Type
, &value
);
135 } else if (tid
== CFStringGetTypeID()) {
136 SInt32 value
= CFStringGetIntValue(obj
);
137 CFStringRef t
= CFStringCreateWithFormat(0, 0, CFSTR("%ld"), (long) value
);
138 /* If a string converted to an int isn't equal to the int printed as
139 a string, return a CFStringRef instead. */
140 if (!CFEqual(t
, obj
)) {
142 return CFStringCreateCopy(0, obj
);
145 return CFNumberCreate(0, kCFNumberSInt32Type
, &value
);
150 CFDateRef
copyDate(CFTypeRef obj
) {
151 CFTypeID tid
= CFGetTypeID(obj
);
152 if (tid
== CFDateGetTypeID()) {
159 // MARK: SecDbColumn accessors, to retrieve values as CF types in SecDbStep.
161 static CFDataRef
SecDbColumnCopyData(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
162 return CFDataCreate(allocator
, sqlite3_column_blob(stmt
, col
),
163 sqlite3_column_bytes(stmt
, col
));
164 //return CFDataCreateWithBytesNoCopy(0, sqlite3_column_blob(stmt, col),
165 // sqlite3_column_bytes(stmt, col),
166 // kCFAllocatorNull);
169 static CFDateRef
SecDbColumnCopyDate(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
170 return CFDateCreate(allocator
, sqlite3_column_double(stmt
, col
));
173 static CFNumberRef
SecDbColumnCopyDouble(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
174 double number
= sqlite3_column_double(stmt
, col
);
175 return CFNumberCreate(allocator
, kCFNumberDoubleType
, &number
);
178 static CFNumberRef
SecDbColumnCopyNumber64(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
179 sqlite_int64 number
= sqlite3_column_int64(stmt
, col
);
180 return CFNumberCreate(allocator
, kCFNumberSInt64Type
, &number
);
183 static CFNumberRef
SecDbColumnCopyNumber(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
184 sqlite_int64 number
= sqlite3_column_int64(stmt
, col
);
185 if (INT32_MIN
<= number
&& number
<= INT32_MAX
) {
186 int32_t num32
= (int32_t)number
;
187 return CFNumberCreate(allocator
, kCFNumberSInt32Type
, &num32
);
189 return CFNumberCreate(allocator
, kCFNumberSInt64Type
, &number
);
193 static CFTypeRef
SecDbColumnCopyString(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
,
194 CFOptionFlags flags
) {
195 const unsigned char *text
= sqlite3_column_text(stmt
, col
);
196 if (!text
|| 0 == strlen((const char *)text
)) {
197 if (flags
& kSecDbDefaultEmptyFlag
) {
199 } else if (flags
& kSecDbDefault0Flag
) {
205 return CFStringCreateWithBytes(allocator
, text
, strlen((const char *)text
), kCFStringEncodingUTF8
, false);
208 // MARK: SecDbClass helpers
210 const SecDbAttr
*SecDbClassAttrWithKind(const SecDbClass
*class, SecDbAttrKind kind
, CFErrorRef
*error
) {
211 const SecDbAttr
*result
= NULL
;
212 SecDbForEachAttr(class, desc
) {
213 if (desc
->kind
== kind
)
218 SecError(errSecInternal
, error
, CFSTR("Can't find attribute of kind %d in class %@"), kind
, class->name
);
223 // MARK: SecDbAttr helpers
225 static bool SecDbIsTombstoneDbSelectAttr(const SecDbAttr
*attr
) {
226 return attr
->flags
& kSecDbPrimaryKeyFlag
|| attr
->kind
== kSecDbTombAttr
;
230 static bool SecDbIsTombstoneDbInsertAttr(const SecDbAttr
*attr
) {
231 return SecDbIsTombstoneDbSelectAttr(attr
) || attr
->kind
== kSecDbAccessAttr
|| attr
->kind
== kSecDbCreationDateAttr
|| attr
->kind
== kSecDbModificationDateAttr
;
235 static bool SecDbIsTombstoneDbUpdateAttr(const SecDbAttr
*attr
) {
236 // We add AuthenticatedData to include UUIDs, which can't be primary keys
237 return SecDbIsTombstoneDbSelectAttr(attr
) || attr
->kind
== kSecDbAccessAttr
|| attr
->kind
== kSecDbCreationDateAttr
|| attr
->kind
== kSecDbRowIdAttr
|| (attr
->flags
& kSecDbInAuthenticatedDataFlag
);
240 CFTypeRef
SecDbAttrCopyDefaultValue(const SecDbAttr
*attr
, CFErrorRef
*error
) {
241 CFTypeRef value
= NULL
;
242 switch (attr
->kind
) {
243 case kSecDbAccessAttr
:
244 case kSecDbStringAttr
:
245 case kSecDbAccessControlAttr
:
250 value
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
253 value
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
255 case kSecDbNumberAttr
:
260 value
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &zero
);
264 value
= CFDateCreate(kCFAllocatorDefault
, 0.0);
266 case kSecDbCreationDateAttr
:
267 case kSecDbModificationDateAttr
:
268 value
= CFDateCreate(kCFAllocatorDefault
, CFAbsoluteTimeGetCurrent());
271 SecError(errSecInternal
, error
, CFSTR("attr %@ has no default value"), attr
->name
);
278 static CFTypeRef
SecDbAttrCopyValueForDb(const SecDbAttr
*attr
, CFTypeRef value
, CFErrorRef
*error
) {
279 CFDataRef data
= NULL
;
280 CFTypeRef result
= NULL
;
285 if (CFEqual(value
, kCFNull
) && attr
->flags
& kSecDbPrimaryKeyFlag
) {
286 // SQLITE3 doesn't like NULL for primary key attributes, pretend kSecDbDefaultEmptyFlag was specified
287 require_quiet(result
= SecDbAttrCopyDefaultValue(attr
, error
), out
);
289 result
= CFRetain(value
);
292 if (attr
->flags
& kSecDbSHA1ValueInFlag
&& !CFEqual(result
, kCFNull
)) {
293 require_action_quiet(data
= copyData(result
), out
,
294 SecError(errSecInternal
, error
, CFSTR("failed to get attribute %@ data"), attr
->name
);
295 CFReleaseNull(result
));
296 CFAssignRetained(result
, CFDataCopySHA1Digest(data
, error
));
304 static CFStringRef
SecDbAttrGetHashName(const SecDbAttr
*attr
) {
305 if ((attr
->flags
& kSecDbSHA1ValueInFlag
) == 0) {
309 static dispatch_once_t once
;
310 static CFMutableDictionaryRef hash_store
;
311 static dispatch_queue_t queue
;
312 dispatch_once(&once
, ^{
313 queue
= dispatch_queue_create("secd-hash-name", NULL
);
314 hash_store
= CFDictionaryCreateMutableForCFTypes(NULL
);
317 __block CFStringRef name
;
318 dispatch_sync(queue
, ^{
319 name
= CFDictionaryGetValue(hash_store
, attr
->name
);
321 name
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("#%@"), attr
->name
);
322 CFDictionarySetValue(hash_store
, attr
->name
, name
);
331 CFTypeRef
SecDbItemGetCachedValueWithName(SecDbItemRef item
, CFStringRef name
) {
332 return CFDictionaryGetValue(item
->attributes
, name
);
335 static CFTypeRef
SecDbItemGetCachedValue(SecDbItemRef item
, const SecDbAttr
*desc
) {
336 return CFDictionaryGetValue(item
->attributes
, desc
->name
);
339 CFMutableDictionaryRef
SecDbItemCopyPListWithMask(SecDbItemRef item
, CFOptionFlags mask
, CFErrorRef
*error
) {
340 CFMutableDictionaryRef dict
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
341 SecDbForEachAttrWithMask(item
->class, desc
, mask
) {
342 CFTypeRef value
= SecDbItemGetValue(item
, desc
, error
);
344 if (!CFEqual(kCFNull
, value
)) {
345 CFDictionarySetValue(dict
, desc
->name
, value
);
346 } else if (desc
->flags
& kSecDbNotNullFlag
) {
347 SecError(errSecDecode
, error
, CFSTR("attribute %@ has NULL value"), desc
->name
);
348 secerror("%@", error
? *error
: (CFErrorRef
)CFSTR("error == NULL"));
360 void SecDbItemSetCredHandle(SecDbItemRef item
, CFTypeRef cred_handle
) {
361 CFRetainAssign(item
->credHandle
, cred_handle
);
364 void SecDbItemSetCallerAccessGroups(SecDbItemRef item
, CFArrayRef caller_access_groups
) {
365 CFRetainAssign(item
->callerAccessGroups
, caller_access_groups
);
368 CFDataRef
SecDbItemCopyEncryptedDataToBackup(SecDbItemRef item
, uint64_t handle
, CFErrorRef
*error
) {
369 CFDataRef edata
= NULL
;
370 keybag_handle_t keybag
= (keybag_handle_t
)handle
;
371 CFMutableDictionaryRef attributes
= SecDbItemCopyPListWithMask(item
, kSecDbInCryptoDataFlag
, error
);
372 CFMutableDictionaryRef auth_attributes
= SecDbItemCopyPListWithMask(item
, kSecDbInAuthenticatedDataFlag
, error
);
373 if (attributes
|| auth_attributes
) {
374 SecAccessControlRef access_control
= SecDbItemCopyAccessControl(item
, error
);
375 if (access_control
) {
376 if (ks_encrypt_data_legacy(keybag
, access_control
, item
->credHandle
, attributes
, auth_attributes
, &edata
, false, error
)) {
377 item
->_edataState
= kSecDbItemEncrypting
;
379 seccritical("ks_encrypt_data (db): failed: %@", error
? *error
: (CFErrorRef
)CFSTR(""));
381 CFRelease(access_control
);
383 CFReleaseNull(attributes
);
384 CFReleaseNull(auth_attributes
);
390 bool SecDbItemEnsureDecrypted(SecDbItemRef item
, bool decryptSecretData
, CFErrorRef
*error
) {
392 // If we haven't yet decrypted the item, make sure we do so now
394 if (item
->_edataState
== kSecDbItemEncrypted
|| (decryptSecretData
&& item
->_edataState
== kSecDbItemSecretEncrypted
)) {
395 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, error
);
397 CFDataRef edata
= SecDbItemGetCachedValue(item
, attr
);
399 return SecError(errSecInternal
, error
, CFSTR("state= encrypted but edata is NULL"));
400 // Decrypt calls set value a bunch of times which clears our edata and changes our state.
401 item
->_edataState
= kSecDbItemDecrypting
;
402 result
= SecDbItemDecrypt(item
, decryptSecretData
, edata
, error
);
404 item
->_edataState
= decryptSecretData
? kSecDbItemClean
: kSecDbItemSecretEncrypted
;
406 item
->_edataState
= kSecDbItemEncrypted
;
412 // Only called if cached value is not found.
413 static CFTypeRef
SecDbItemCopyValue(SecDbItemRef item
, const SecDbAttr
*attr
, CFErrorRef
*error
) {
414 if (attr
->copyValue
) {
415 return attr
->copyValue(item
, attr
, error
);
418 CFTypeRef value
= NULL
;
419 switch (attr
->kind
) {
420 // These have an explicit copyValue; here to shut up compiler
422 case kSecDbEncryptedDataAttr
:
423 case kSecDbPrimaryKeyAttr
:
426 case kSecDbAccessAttr
:
427 case kSecDbStringAttr
:
429 case kSecDbAccessControlAttr
:
430 if (attr
->flags
& kSecDbNotNullFlag
) {
431 if (attr
->flags
& kSecDbDefault0Flag
) {
434 } else if (attr
->kind
!= kSecDbBlobAttr
&& attr
->flags
& kSecDbDefaultEmptyFlag
) {
435 // blob drops through to data everything else is empty string
442 if (attr
->flags
& kSecDbNotNullFlag
&& attr
->flags
& kSecDbDefaultEmptyFlag
) {
443 value
= CFDataCreate(CFGetAllocator(item
), NULL
, 0);
449 value
= CFDataCreate(CFGetAllocator(item
), NULL
, 0);
451 case kSecDbNumberAttr
:
454 if (attr
->flags
& kSecDbNotNullFlag
) {
456 value
= CFNumberCreate(CFGetAllocator(item
), kCFNumberSInt32Type
, &zero
);
462 if (attr
->flags
& kSecDbNotNullFlag
&& attr
->flags
& kSecDbDefault0Flag
) {
463 value
= CFDateCreate(kCFAllocatorDefault
, 0.0);
468 case kSecDbRowIdAttr
:
469 if (attr
->flags
& kSecDbNotNullFlag
) {
474 case kSecDbCreationDateAttr
:
475 case kSecDbModificationDateAttr
:
476 value
= CFDateCreate(CFGetAllocator(item
), CFAbsoluteTimeGetCurrent());
478 case kSecDbUTombAttr
:
486 // SecDbItemGetValue will return kCFNull if there is no value for an attribute and this was not
487 // an error. It will return NULL and optionally set *error if there was an error computing an
488 // attribute, or if a required attribute was missing a value and had no known way to compute
490 CFTypeRef
SecDbItemGetValue(SecDbItemRef item
, const SecDbAttr
*desc
, CFErrorRef
*error
) {
491 // Propagate chained errors
495 if (desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
|| desc
->flags
& kSecDbReturnDataFlag
) {
496 if (!SecDbItemEnsureDecrypted(item
, desc
->flags
& kSecDbReturnDataFlag
, error
))
500 CFTypeRef value
= SecDbItemGetCachedValue(item
, desc
);
502 value
= SecDbItemCopyValue(item
, desc
, error
);
504 if (CFEqual(kCFNull
, value
)) {
505 CFRelease(value
); // This is redundant but it shuts clang's static analyzer up.
508 SecDbItemSetValue(item
, desc
, value
, error
);
510 value
= SecDbItemGetCachedValue(item
, desc
);
517 CFTypeRef
SecDbItemGetValueKind(SecDbItemRef item
, SecDbAttrKind descKind
, CFErrorRef
*error
) {
518 CFTypeRef result
= NULL
;
520 const SecDbClass
* itemClass
= SecDbItemGetClass(item
);
521 const SecDbAttr
* desc
= SecDbClassAttrWithKind(itemClass
, descKind
, error
);
524 result
= SecDbItemGetValue(item
, desc
, error
);
531 // Similar as SecDbItemGetValue, but if attr represents attribute stored into DB field as hash, returns
532 // hashed value for the attribute.
533 static CFTypeRef
SecDbItemCopyValueForDb(SecDbItemRef item
, const SecDbAttr
*desc
, CFErrorRef
*error
) {
534 CFTypeRef value
= NULL
;
535 CFStringRef hash_name
= NULL
;
536 hash_name
= SecDbAttrGetHashName(desc
);
537 if ((desc
->flags
& kSecDbSHA1ValueInFlag
) && (desc
->flags
& kSecDbInFlag
)) {
538 value
= CFRetainSafe(CFDictionaryGetValue(item
->attributes
, hash_name
));
542 require_quiet(value
= SecDbItemGetValue(item
, desc
, error
), out
);
543 require_action_quiet(value
= SecDbAttrCopyValueForDb(desc
, value
, error
), out
, CFReleaseNull(value
));
544 if ((desc
->flags
& kSecDbSHA1ValueInFlag
) != 0) {
545 CFDictionarySetValue(item
->attributes
, hash_name
, value
);
553 static bool SecDbItemGetBoolValue(SecDbItemRef item
, const SecDbAttr
*desc
, bool *bvalue
, CFErrorRef
*error
) {
554 CFTypeRef value
= SecDbItemGetValue(item
, desc
, error
);
558 *bvalue
= (isNumber(value
) && CFNumberGetValue(value
, kCFNumberCharType
, &cvalue
) && cvalue
== 1);
562 static CFStringRef
SecDbItemCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
564 if (isDictionary(formatOptions
) && CFDictionaryContainsKey(formatOptions
, kSecDebugFormatOption
)) {
565 SecDbItemRef item
= (SecDbItemRef
)cf
;
566 CFMutableStringRef mdesc
= CFStringCreateMutable(CFGetAllocator(cf
), 0);
567 CFStringAppendFormat(mdesc
, NULL
, CFSTR("<%@"), item
->class->name
);
568 SecDbForEachAttr(item
->class, attr
) {
569 CFTypeRef value
= SecDbItemGetValue(item
, attr
, NULL
);
571 CFStringAppend(mdesc
, CFSTR(","));
572 CFStringAppend(mdesc
, attr
->name
);
573 CFStringAppend(mdesc
, CFSTR("="));
574 if (CFEqual(CFSTR("data"), attr
->name
)) {
575 CFStringAppendEncryptedData(mdesc
, value
);
576 } else if (CFEqual(CFSTR("v_Data"), attr
->name
)) {
577 CFStringAppend(mdesc
, CFSTR("<?>"));
578 } else if (isData(value
)) {
579 CFStringAppendHexData(mdesc
, value
);
581 CFStringAppendFormat(mdesc
, 0, CFSTR("%@"), value
);
585 CFStringAppend(mdesc
, CFSTR(">"));
588 SecDbItemRef item
= (SecDbItemRef
)cf
;
589 const UInt8 zero4
[4] = {};
590 const UInt8
*pk
= &zero4
[0], *sha1
= &zero4
[0];
594 CFStringRef access
= NULL
;
595 uint8_t mdatbuf
[32] = {};
596 uint8_t *mdat
= &mdatbuf
[0];
597 CFMutableStringRef attrs
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
598 CFStringRef agrp
= NULL
;
599 CFBooleanRef utomb
= NULL
;
601 SecDbForEachAttr(item
->class, attr
) {
603 switch (attr
->kind
) {
606 case kSecDbStringAttr
:
607 case kSecDbNumberAttr
:
609 case kSecDbEncryptedDataAttr
:
610 if (attr
->flags
& (kSecDbReturnAttrFlag
| kSecDbReturnDataFlag
) && (value
= SecDbItemGetValue(item
, attr
, NULL
)) && !CFEqual(value
, kCFNull
)) {
611 if (isString(value
) && CFEqual(attr
->name
, kSecAttrAccessGroup
)) {
614 // We don't log these, just record that we saw the attribute.
615 CFStringAppend(attrs
, CFSTR(","));
616 CFStringAppend(attrs
, attr
->name
);
621 if ((value
= SecDbItemGetValue(item
, attr
, NULL
))) {
622 if (CFEqual(attr
->name
, kSecAttrMultiUser
)) {
624 CFStringAppend(attrs
, CFSTR(","));
625 if (CFDataGetLength(value
)) {
626 CFStringAppendHexData(attrs
, value
);
628 CFStringAppend(attrs
, attr
->name
);
634 case kSecDbCreationDateAttr
:
635 // We don't care about this and every object has one.
637 case kSecDbModificationDateAttr
:
638 value
= SecDbItemGetValue(item
, attr
, NULL
);
640 mdat
= der_encode_generalizedtime_body(CFDateGetAbsoluteTime(value
), NULL
, mdat
, &mdatbuf
[31]);
643 value
= SecDbItemGetValue(item
, attr
, NULL
);
644 if (isData(value
) && CFDataGetLength(value
) >= (CFIndex
)sizeof(zero4
))
645 sha1
= CFDataGetBytePtr(value
);
647 case kSecDbRowIdAttr
:
648 value
= SecDbItemGetValue(item
, attr
, NULL
);
650 CFNumberGetValue(value
, kCFNumberSInt64Type
, &rowid
);
652 case kSecDbPrimaryKeyAttr
:
653 value
= SecDbItemGetValue(item
, attr
, NULL
);
655 pk
= CFDataGetBytePtr(value
);
658 value
= SecDbItemGetValue(item
, attr
, NULL
);
660 CFNumberGetValue(value
, kCFNumberCharType
, &sync
);
663 value
= SecDbItemGetValue(item
, attr
, NULL
);
665 CFNumberGetValue(value
, kCFNumberCharType
, &tomb
);
667 case kSecDbAccessAttr
:
668 value
= SecDbItemGetValue(item
, attr
, NULL
);
672 case kSecDbUTombAttr
:
673 value
= SecDbItemGetValue(item
, attr
, NULL
);
674 if (isBoolean(value
))
676 case kSecDbAccessControlAttr
:
677 /* TODO: Add formatting of ACLs. */
682 desc
= CFStringCreateWithFormat(CFGetAllocator(cf
), NULL
,
697 pk
[0], pk
[1], pk
[2], pk
[3],
704 utomb
? (CFEqual(utomb
, kCFBooleanFalse
) ? "F," : "T,") : "",
705 sha1
[0], sha1
[1], sha1
[2], sha1
[3]);
706 CFReleaseSafe(attrs
);
712 static void SecDbItemDestroy(CFTypeRef cf
) {
713 SecDbItemRef item
= (SecDbItemRef
)cf
;
714 CFReleaseSafe(item
->attributes
);
715 CFReleaseSafe(item
->credHandle
);
716 CFReleaseSafe(item
->callerAccessGroups
);
717 CFReleaseSafe(item
->cryptoOp
);
720 static CFHashCode
SecDbItemHash(CFTypeRef cf
) {
721 SecDbItemRef item
= (SecDbItemRef
)cf
;
722 CFDataRef digest
= SecDbItemGetSHA1(item
, NULL
);
724 const UInt8
*p
= CFDataGetBytePtr(digest
);
725 // Read first 8 bytes of digest in order
726 code
= p
[0] + ((p
[1] + ((p
[2] + ((p
[3] + ((p
[4] + ((p
[5] + ((p
[6] + (p
[7] << 8)) << 8)) << 8)) << 8)) << 8)) << 8)) << 8);
730 static Boolean
SecDbItemCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
731 SecDbItemRef item1
= (SecDbItemRef
)cf1
;
732 SecDbItemRef item2
= (SecDbItemRef
)cf2
;
733 CFDataRef digest1
= NULL
;
734 CFDataRef digest2
= NULL
;
736 digest1
= SecDbItemGetSHA1(item1
, NULL
);
738 digest2
= SecDbItemGetSHA1(item2
, NULL
);
739 Boolean equal
= CFEqual(digest1
, digest2
);
743 CFGiblisWithHashFor(SecDbItem
)
745 static SecDbItemRef
SecDbItemCreate(CFAllocatorRef allocator
, const SecDbClass
*class, keybag_handle_t keybag
) {
746 SecDbItemRef item
= CFTypeAllocate(SecDbItem
, struct SecDbItem
, allocator
);
748 item
->attributes
= CFDictionaryCreateMutableForCFTypes(allocator
);
749 item
->keybag
= keybag
;
750 item
->_edataState
= kSecDbItemDirty
;
751 item
->cryptoOp
= kAKSKeyOpDecrypt
;
756 const SecDbClass
*SecDbItemGetClass(SecDbItemRef item
) {
760 keybag_handle_t
SecDbItemGetKeybag(SecDbItemRef item
) {
764 bool SecDbItemSetKeybag(SecDbItemRef item
, keybag_handle_t keybag
, CFErrorRef
*error
) {
765 if (!SecDbItemEnsureDecrypted(item
, true, error
))
767 if (item
->keybag
!= keybag
) {
768 item
->keybag
= keybag
;
769 if (item
->_edataState
== kSecDbItemClean
) {
770 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, NULL
), kCFNull
, NULL
);
777 bool SecDbItemSetValue(SecDbItemRef item
, const SecDbAttr
*desc
, CFTypeRef value
, CFErrorRef
*error
) {
778 // Propagate chained errors.
786 return desc
->setValue(item
, desc
, value
, error
);
788 if (desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
) {
789 if (!SecDbItemEnsureDecrypted(item
, true, error
)) {
794 bool changed
= false;
795 CFTypeRef attr
= NULL
;
796 switch (desc
->kind
) {
797 case kSecDbPrimaryKeyAttr
:
799 attr
= copyData(value
);
801 case kSecDbEncryptedDataAttr
:
802 attr
= copyData(value
);
804 if (item
->_edataState
== kSecDbItemEncrypting
)
805 item
->_edataState
= kSecDbItemClean
;
807 item
->_edataState
= kSecDbItemEncrypted
;
808 } else if (!value
|| CFEqual(kCFNull
, value
)) {
809 item
->_edataState
= kSecDbItemDirty
;
813 case kSecDbAccessControlAttr
:
814 attr
= copyBlob(value
);
817 case kSecDbCreationDateAttr
:
818 case kSecDbModificationDateAttr
:
819 attr
= copyDate(value
);
821 case kSecDbNumberAttr
:
824 case kSecDbRowIdAttr
:
825 attr
= copyNumber(value
);
827 case kSecDbAccessAttr
:
828 case kSecDbStringAttr
:
829 attr
= copyString(value
);
832 attr
= copySHA1(value
);
834 case kSecDbUTombAttr
:
835 attr
= CFRetainSafe(asBoolean(value
, NULL
));
838 attr
= copyUUID(value
);
843 CFTypeRef ovalue
= CFDictionaryGetValue(item
->attributes
, desc
->name
);
844 changed
= (!ovalue
|| !CFEqual(ovalue
, attr
));
845 CFDictionarySetValue(item
->attributes
, desc
->name
, attr
);
848 if (value
&& !CFEqual(kCFNull
, value
)) {
849 SecError(errSecItemInvalidValue
, error
, CFSTR("attribute %@: value: %@ failed to convert"), desc
->name
, value
);
852 CFTypeRef ovalue
= CFDictionaryGetValue(item
->attributes
, desc
->name
);
853 changed
= (ovalue
&& !CFEqual(ovalue
, kCFNull
));
854 CFDictionaryRemoveValue(item
->attributes
, desc
->name
);
858 if (desc
->flags
& kSecDbInHashFlag
)
859 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, NULL
), kCFNull
, NULL
);
860 if (desc
->flags
& kSecDbPrimaryKeyFlag
)
861 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbPrimaryKeyAttr
, NULL
), kCFNull
, NULL
);
862 if ((desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
) && (item
->_edataState
== kSecDbItemClean
|| (item
->_edataState
== kSecDbItemSecretEncrypted
&& (desc
->flags
& kSecDbReturnDataFlag
) == 0)))
863 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, NULL
), kCFNull
, NULL
);
864 if (desc
->flags
& kSecDbSHA1ValueInFlag
)
865 CFDictionaryRemoveValue(item
->attributes
, SecDbAttrGetHashName(desc
));
871 bool SecDbItemSetValues(SecDbItemRef item
, CFDictionaryRef values
, CFErrorRef
*error
) {
872 SecDbForEachAttr(item
->class, attr
) {
873 CFTypeRef value
= CFDictionaryGetValue(values
, attr
->name
);
874 if (value
&& !SecDbItemSetValue(item
, attr
, value
, error
))
880 bool SecDbItemSetValueWithName(SecDbItemRef item
, CFStringRef name
, CFTypeRef value
, CFErrorRef
*error
) {
881 SecDbForEachAttr(item
->class, attr
) {
882 if (CFEqual(attr
->name
, name
)) {
883 return SecDbItemSetValue(item
, attr
, value
, error
);
889 bool SecDbItemSetAccessControl(SecDbItemRef item
, SecAccessControlRef access_control
, CFErrorRef
*error
) {
891 if (item
->_edataState
== kSecDbItemClean
)
892 ok
= SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, error
), kCFNull
, error
);
893 if (ok
&& access_control
) { //added check for access_control because ks_decrypt_data can leave NULL in access_control in case of error
894 item
->_edataState
= kSecDbItemDirty
;
895 CFDataRef data
= SecAccessControlCopyData(access_control
);
896 ok
= SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbAccessControlAttr
, error
), data
, error
);
902 SecDbItemRef
SecDbItemCreateWithAttributes(CFAllocatorRef allocator
, const SecDbClass
*class, CFDictionaryRef attributes
, keybag_handle_t keybag
, CFErrorRef
*error
) {
903 SecDbItemRef item
= SecDbItemCreate(kCFAllocatorDefault
, class, keybag
);
904 if (item
&& !SecDbItemSetValues(item
, attributes
, error
))
910 SecDbColumnCopyValueWithAttr(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, const SecDbAttr
*attr
, int col
, CFErrorRef
*error
) {
911 CFTypeRef value
= NULL
;
912 switch (attr
->kind
) {
914 case kSecDbCreationDateAttr
:
915 case kSecDbModificationDateAttr
:
916 value
= SecDbColumnCopyDate(allocator
, stmt
, col
, error
);
919 case kSecDbNumberAttr
:
920 switch (sqlite3_column_type(stmt
, col
)) {
922 value
= SecDbColumnCopyNumber(allocator
, stmt
, col
, error
);
925 value
= SecDbColumnCopyDouble(allocator
, stmt
, col
, error
);
928 value
= SecDbColumnCopyString(allocator
, stmt
, col
, error
,
932 value
= SecDbColumnCopyData(allocator
, stmt
, col
, error
);
939 case kSecDbAccessAttr
:
940 case kSecDbStringAttr
:
941 value
= SecDbColumnCopyString(allocator
, stmt
, col
, error
,
947 case kSecDbPrimaryKeyAttr
:
948 case kSecDbEncryptedDataAttr
:
949 value
= SecDbColumnCopyData(allocator
, stmt
, col
, error
);
953 value
= SecDbColumnCopyNumber(allocator
, stmt
, col
, error
);
955 case kSecDbRowIdAttr
:
956 value
= SecDbColumnCopyNumber64(allocator
, stmt
, col
, error
);
958 case kSecDbAccessControlAttr
:
959 case kSecDbUTombAttr
:
960 /* This attributes does not have any database column associated, exists only inside encrypted blob as metadata. */
966 SecDbItemRef
SecDbItemCreateWithStatement(CFAllocatorRef allocator
, const SecDbClass
*class, sqlite3_stmt
*stmt
, keybag_handle_t keybag
, CFErrorRef
*error
, bool (^return_attr
)(const SecDbAttr
*attr
)) {
967 SecDbItemRef item
= SecDbItemCreate(allocator
, class, keybag
);
969 SecDbForEachAttr(class, attr
) {
970 if (return_attr(attr
)) {
971 CFTypeRef value
= SecDbColumnCopyValueWithAttr(allocator
, stmt
, attr
, col
++, error
);
972 require_action_quiet(value
, errOut
, CFReleaseNull(item
));
974 CFDictionarySetValue(item
->attributes
, SecDbAttrGetHashName(attr
), value
);
978 const SecDbAttr
*data_attr
= SecDbClassAttrWithKind(class, kSecDbEncryptedDataAttr
, NULL
);
979 if (data_attr
!= NULL
&& CFDictionaryGetValue(item
->attributes
, data_attr
->name
) != NULL
) {
980 item
->_edataState
= kSecDbItemEncrypted
;
988 SecDbItemRef
SecDbItemCreateWithEncryptedData(CFAllocatorRef allocator
, const SecDbClass
*class,
989 CFDataRef edata
, keybag_handle_t keybag
, CFErrorRef
*error
) {
990 SecDbItemRef item
= SecDbItemCreate(allocator
, class, keybag
);
991 const SecDbAttr
*edata_attr
= SecDbClassAttrWithKind(class, kSecDbEncryptedDataAttr
, error
);
993 if (!SecDbItemSetValue(item
, edata_attr
, edata
, error
))
999 // TODO: Hack -- Replace with real filtering
1001 // Return true iff an item for which SecDbItemIsSyncable() already returns true should be part of the v2 view.
1002 bool SecDbItemInV2(SecDbItemRef item
) {
1003 const SecDbClass
*iclass
= SecDbItemGetClass(item
);
1004 return (SecDbItemGetCachedValueWithName(item
, kSecAttrSyncViewHint
) == NULL
&&
1005 (iclass
== genp_class() || iclass
== inet_class() || iclass
== keys_class() || iclass
== cert_class()));
1008 // Return true iff an item for which SecDbItemIsSyncable() and SecDbItemInV2() already return true should be part of the v0 view.
1009 bool SecDbItemInV2AlsoInV0(SecDbItemRef item
) {
1010 return (SecDbItemGetCachedValueWithName(item
, kSecAttrTokenID
) == NULL
&& SecDbItemGetClass(item
) != cert_class());
1013 SecDbItemRef
SecDbItemCopyWithUpdates(SecDbItemRef item
, CFDictionaryRef updates
, CFErrorRef
*error
) {
1014 SecDbItemRef new_item
= SecDbItemCreate(CFGetAllocator(item
), item
->class, item
->keybag
);
1015 SecDbItemSetCredHandle(new_item
, item
->credHandle
);
1016 SecDbForEachAttr(item
->class, attr
) {
1017 // Copy each attribute, except the mod date attribute (it will be reset to now when needed),
1018 // from the updates dict unless it's not there in which case we copy the attribute from the passed in item.
1019 if (attr
->kind
!= kSecDbModificationDateAttr
&& attr
->kind
!= kSecDbEncryptedDataAttr
&& attr
->kind
!= kSecDbSHA1Attr
&& attr
->kind
!= kSecDbPrimaryKeyAttr
) {
1020 CFTypeRef value
= NULL
;
1021 if (CFDictionaryGetValueIfPresent(updates
, attr
->name
, &value
)) {
1023 SecError(errSecParam
, error
, CFSTR("NULL value in dictionary"));
1025 value
= SecDbItemGetValue(item
, attr
, error
);
1027 if (!value
|| !SecDbItemSetValue(new_item
, attr
, value
, error
)) {
1028 CFReleaseNull(new_item
);
1036 // Ensure that the date value of attr of new_item is greater than that of old_item.
1037 static bool SecDbItemMakeAttrYounger(SecDbItemRef new_item
, SecDbItemRef old_item
, const SecDbAttr
*attr
, CFErrorRef
*error
) {
1038 CFDateRef old_date
= SecDbItemGetValue(old_item
, attr
, error
);
1041 CFDateRef new_date
= SecDbItemGetValue(new_item
, attr
, error
);
1045 if (CFDateCompare(new_date
, old_date
, NULL
) != kCFCompareGreaterThan
) {
1046 CFDateRef adjusted_date
= CFDateCreate(kCFAllocatorDefault
, CFDateGetAbsoluteTime(old_date
) + 0.001);
1047 if (adjusted_date
) {
1048 ok
= SecDbItemSetValue(new_item
, attr
, adjusted_date
, error
);
1049 CFRelease(adjusted_date
);
1055 // Ensure that the mod date of new_item is greater than that of old_item.
1056 static bool SecDbItemMakeYounger(SecDbItemRef new_item
, SecDbItemRef old_item
, CFErrorRef
*error
) {
1057 const SecDbAttr
*attr
= SecDbClassAttrWithKind(new_item
->class, kSecDbModificationDateAttr
, error
);
1058 return attr
&& SecDbItemMakeAttrYounger(new_item
, old_item
, attr
, error
);
1061 static SecDbItemRef
SecDbItemCopyTombstone(SecDbItemRef item
, CFBooleanRef makeTombStone
, CFErrorRef
*error
) {
1062 SecDbItemRef new_item
= SecDbItemCreate(CFGetAllocator(item
), item
->class, item
->keybag
);
1063 SecDbForEachAttr(item
->class, attr
) {
1064 if (attr
->kind
== kSecDbTombAttr
) {
1065 // Set the tomb attr to true to indicate a tombstone.
1066 if (!SecDbItemSetValue(new_item
, attr
, kCFBooleanTrue
, error
)) {
1067 CFReleaseNull(new_item
);
1070 } else if (SecDbIsTombstoneDbUpdateAttr(attr
)) {
1071 // Copy all primary key attributes and creation timestamps from the original item.
1072 CFTypeRef value
= SecDbItemGetValue(item
, attr
, error
);
1073 if (!value
|| (!CFEqual(kCFNull
, value
) && !SecDbItemSetValue(new_item
, attr
, value
, error
))) {
1074 CFReleaseNull(new_item
);
1077 } else if (attr
->kind
== kSecDbModificationDateAttr
) {
1078 if (!SecDbItemMakeAttrYounger(new_item
, item
, attr
, error
)) {
1079 CFReleaseNull(new_item
);
1082 } else if (makeTombStone
&& attr
->kind
== kSecDbUTombAttr
) {
1084 SecDbItemSetValue(new_item
, attr
, makeTombStone
, error
);
1091 bool SecDbItemIsEngineInternalState(SecDbItemRef itemObject
) {
1092 // Only used for controlling logging
1093 // Use agrp=com.apple.security.sos, since it is not encrypted
1097 const SecDbAttr
*agrp
= SecDbAttrWithKey(SecDbItemGetClass(itemObject
), kSecAttrAccessGroup
, NULL
);
1098 CFTypeRef cfval
= SecDbItemGetValue(itemObject
, agrp
, NULL
);
1099 return cfval
&& CFStringCompareSafe(cfval
, kSOSInternalAccessGroup
, NULL
) == kCFCompareEqualTo
;
1104 // MARK: SQL Construction helpers -- These should become private in the future
1106 void SecDbAppendElement(CFMutableStringRef sql
, CFStringRef value
, bool *needComma
) {
1109 CFStringAppend(sql
, CFSTR(","));
1113 CFStringAppend(sql
, value
);
1116 static void SecDbAppendElementEquals(CFMutableStringRef sql
, CFStringRef value
, bool *needComma
) {
1117 SecDbAppendElement(sql
, value
, needComma
);
1118 CFStringAppend(sql
, CFSTR("=?"));
1121 /* Append AND is needWhere is NULL or *needWhere is false. Append WHERE
1122 otherwise. Upon return *needWhere will be false. */
1124 SecDbAppendWhereOrAnd(CFMutableStringRef sql
, bool *needWhere
) {
1125 if (!needWhere
|| !*needWhere
) {
1126 CFStringAppend(sql
, CFSTR(" AND "));
1128 CFStringAppend(sql
, CFSTR(" WHERE "));
1134 SecDbAppendWhereOrAndEquals(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
) {
1135 SecDbAppendWhereOrAnd(sql
, needWhere
);
1136 CFStringAppend(sql
, col
);
1137 CFStringAppend(sql
, CFSTR("=?"));
1141 SecDbAppendWhereOrAndNotEquals(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
) {
1142 SecDbAppendWhereOrAnd(sql
, needWhere
);
1143 CFStringAppend(sql
, col
);
1144 CFStringAppend(sql
, CFSTR("!=?"));
1147 static void SecDbAppendCountArgsAndCloseParen(CFMutableStringRef sql
, CFIndex count
) {
1148 bool needComma
= false;
1150 SecDbAppendElement(sql
, CFSTR("?"), &needComma
);
1151 CFStringAppend(sql
, CFSTR(")"));
1155 SecDbAppendWhereOrAndIn(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
, CFIndex count
) {
1157 return SecDbAppendWhereOrAndEquals(sql
, col
, needWhere
);
1158 SecDbAppendWhereOrAnd(sql
, needWhere
);
1159 CFStringAppend(sql
, col
);
1160 CFStringAppend(sql
, CFSTR(" IN ("));
1161 SecDbAppendCountArgsAndCloseParen(sql
, count
);
1165 SecDbAppendWhereOrAndNotIn(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
, CFIndex count
) {
1167 return SecDbAppendWhereOrAndNotEquals(sql
, col
, needWhere
);
1168 SecDbAppendWhereOrAnd(sql
, needWhere
);
1169 CFStringAppend(sql
, col
);
1170 CFStringAppend(sql
, CFSTR(" NOT IN ("));
1171 SecDbAppendCountArgsAndCloseParen(sql
, count
);
1174 static CFStringRef
SecDbItemCopyInsertSQL(SecDbItemRef item
, bool(^use_attr
)(const SecDbAttr
*attr
)) {
1175 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(item
), 0);
1176 CFStringAppend(sql
, CFSTR("INSERT INTO "));
1177 CFStringAppend(sql
, item
->class->name
);
1178 CFStringAppend(sql
, CFSTR("("));
1179 bool needComma
= false;
1180 CFIndex used_attr
= 0;
1181 SecDbForEachAttr(item
->class, attr
) {
1182 if (use_attr(attr
)) {
1184 SecDbAppendElement(sql
, attr
->name
, &needComma
);
1187 CFStringAppend(sql
, CFSTR(")VALUES(?"));
1188 while (used_attr
-- > 1) {
1189 CFStringAppend(sql
, CFSTR(",?"));
1191 CFStringAppend(sql
, CFSTR(")"));
1196 static bool SecDbItemInsertBind(SecDbItemRef item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr
)(const SecDbAttr
*attr
)) {
1199 SecDbForEachAttr(item
->class, attr
) {
1200 if (use_attr(attr
)) {
1201 CFTypeRef value
= SecDbItemCopyValueForDb(item
, attr
, error
);
1202 ok
= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1203 CFReleaseSafe(value
);
1211 sqlite3_int64
SecDbItemGetRowId(SecDbItemRef item
, CFErrorRef
*error
) {
1212 sqlite3_int64 row_id
= 0;
1213 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1215 CFNumberRef number
= SecDbItemGetValue(item
, attr
, error
);
1216 if (!isNumber(number
)|| !CFNumberGetValue(number
, kCFNumberSInt64Type
, &row_id
))
1217 SecDbError(SQLITE_ERROR
, error
, CFSTR("rowid %@ is not a 64 bit number"), number
);
1223 static CFNumberRef
SecDbItemCreateRowId(SecDbItemRef item
, sqlite3_int64 rowid
, CFErrorRef
*error
) {
1224 return CFNumberCreate(CFGetAllocator(item
), kCFNumberSInt64Type
, &rowid
);
1227 bool SecDbItemSetRowId(SecDbItemRef item
, sqlite3_int64 rowid
, CFErrorRef
*error
) {
1229 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1231 CFNumberRef value
= SecDbItemCreateRowId(item
, rowid
, error
);
1235 ok
= SecDbItemSetValue(item
, attr
, value
, error
);
1241 bool SecDbItemClearRowId(SecDbItemRef item
, CFErrorRef
*error
) {
1243 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1245 CFDictionaryRemoveValue(item
->attributes
, attr
->name
);
1246 //ok = SecDbItemSetValue(item, attr, kCFNull, error);
1251 static bool SecDbItemSetLastInsertRowId(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1252 sqlite3_int64 rowid
= sqlite3_last_insert_rowid(SecDbHandle(dbconn
));
1253 return SecDbItemSetRowId(item
, rowid
, error
);
1256 bool SecDbItemIsSyncableOrCorrupted(SecDbItemRef item
) {
1257 bool is_syncable_or_corrupted
= false;
1258 CFErrorRef localError
= NULL
;
1259 if (!SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, &localError
),
1260 &is_syncable_or_corrupted
, &localError
)) {
1261 is_syncable_or_corrupted
= SecErrorGetOSStatus(localError
) == errSecDecode
;
1263 CFReleaseSafe(localError
);
1264 return is_syncable_or_corrupted
;
1267 bool SecDbItemIsSyncable(SecDbItemRef item
) {
1269 if (SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, NULL
), &is_syncable
, NULL
))
1274 bool SecDbItemSetSyncable(SecDbItemRef item
, bool sync
, CFErrorRef
*error
)
1276 return SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, error
), sync
? kCFBooleanTrue
: kCFBooleanFalse
, error
);
1279 bool SecDbItemIsTombstone(SecDbItemRef item
) {
1281 if (SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbTombAttr
, NULL
), &is_tomb
, NULL
))
1286 CFDataRef
SecDbItemGetPrimaryKey(SecDbItemRef item
, CFErrorRef
*error
) {
1287 return SecDbItemGetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbPrimaryKeyAttr
, error
), error
);
1290 CFDataRef
SecDbItemGetSHA1(SecDbItemRef item
, CFErrorRef
*error
) {
1291 return SecDbItemGetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, error
), error
);
1294 static SecDbQueryRef
SecDbQueryCreateWithItemPrimaryKey(SecDbItemRef item
, CFErrorRef
*error
) {
1295 CFMutableDictionaryRef dict
= SecDbItemCopyPListWithMask(item
, kSecDbPrimaryKeyFlag
, error
);
1299 SecDbQueryRef query
= query_create(item
->class, NULL
, NULL
, error
);
1301 CFReleaseSafe(query
->q_item
);
1302 query
->q_item
= dict
;
1310 static bool SecDbItemIsCorrupt(SecDbItemRef item
, bool *is_corrupt
, CFErrorRef
*error
) {
1311 CFErrorRef localError
= NULL
;
1312 // Cache the storedSHA1 digest so we use the one from the db not the recomputed one for notifications.
1313 const struct SecDbAttr
*sha1attr
= SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, &localError
);
1314 CFDataRef storedSHA1
= CFRetainSafe(SecDbItemGetValue(item
, sha1attr
, &localError
));
1317 if (localError
|| !SecDbItemEnsureDecrypted(item
, true, &localError
)) {
1318 if (SecErrorGetOSStatus(localError
) == errSecDecode
) {
1319 // We failed to decrypt the item
1320 const SecDbAttr
*desc
= SecDbClassAttrWithKind(item
->class, kSecDbAccessControlAttr
, &localError
);
1321 SecAccessControlRef accc
= NULL
;
1322 CFDataRef acccData
= NULL
;
1324 acccData
= (CFDataRef
)SecDbItemGetValue(item
, desc
, &localError
);
1325 if (isData(acccData
)) {
1326 accc
= SecAccessControlCreateFromData(CFGetAllocator(item
), acccData
, &localError
);
1329 if (accc
&& CFEqualSafe(SecAccessControlGetProtection(accc
), kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
)) {
1331 secwarning("cannot decrypt item %@, item is irrecoverably lost with older passcode (error %@)", item
, localError
);
1333 secerror("error %@ reading item %@ (corrupted)", localError
, item
);
1334 __security_simulatecrash(CFSTR("Corrupted item found in keychain"), __sec_exception_code_CorruptItem
);
1336 CFReleaseNull(localError
);
1341 // Recompute sha1 hash attribute and compare with the cached one.
1342 CFDataRef computedSHA1
= SecDbItemCopyValue(item
, sha1attr
, &localError
);
1343 if (storedSHA1
&& computedSHA1
&& !CFEqual(storedSHA1
, computedSHA1
)) {
1344 CFStringRef storedHex
= CFDataCopyHexString(storedSHA1
), computedHex
= CFDataCopyHexString(computedSHA1
);
1345 secerror("error %@ %@ != %@ item %@ (corrupted)", sha1attr
->name
, storedHex
, computedHex
, item
);
1346 __security_simulatecrash(CFSTR("Corrupted item (sha1 mismatch) found in keychain"), __sec_exception_code_CorruptItem
);
1347 CFReleaseSafe(storedHex
);
1348 CFReleaseSafe(computedHex
);
1352 // Sanity check that all attributes that must not be NULL actually aren't
1353 if (!localError
) SecDbForEachAttr(item
->class, attr
) {
1354 if (attr
->flags
& (kSecDbInCryptoDataFlag
| kSecDbInAuthenticatedDataFlag
)) {
1355 CFTypeRef value
= SecDbItemGetValue(item
, attr
, &localError
);
1357 if (CFEqual(kCFNull
, value
) && attr
->flags
& kSecDbNotNullFlag
) {
1358 secerror("error attribute %@ has NULL value in item %@ (corrupted)", attr
->name
, item
);
1359 __security_simulatecrash(CFSTR("Corrupted item (attr NULL) found in keychain"), __sec_exception_code_CorruptItem
);
1364 if (SecErrorGetOSStatus(localError
) == errSecDecode
) {
1365 // We failed to decrypt the item
1367 secwarning("attribute %@: %@ item %@ (item lost with older passcode)", attr
->name
, localError
, item
);
1369 secerror("error attribute %@: %@ item %@ (corrupted)", attr
->name
, localError
, item
);
1370 __security_simulatecrash(CFSTR("Corrupted item found in keychain"), __sec_exception_code_CorruptItem
);
1373 CFReleaseNull(localError
);
1380 CFReleaseSafe(computedSHA1
);
1381 CFReleaseSafe(storedSHA1
);
1382 return SecErrorPropagate(localError
, error
);
1385 static void SecDbItemRecordUpdate(SecDbConnectionRef dbconn
, SecDbItemRef deleted
, SecDbItemRef inserted
) {
1386 SecDbRecordChange(dbconn
, deleted
, inserted
);
1389 static bool SecDbItemDoInsert(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1390 bool (^use_attr
)(const SecDbAttr
*attr
) = ^bool(const SecDbAttr
*attr
) {
1391 return (attr
->flags
& kSecDbInFlag
);
1394 if (!SecDbItemEnsureDecrypted(item
, true, error
)) {
1398 CFStringRef sql
= SecDbItemCopyInsertSQL(item
, use_attr
);
1399 __block
bool ok
= sql
;
1401 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1402 ok
= (SecDbItemInsertBind(item
, stmt
, error
, use_attr
) &&
1403 SecDbStep(dbconn
, stmt
, error
, NULL
) &&
1404 SecDbItemSetLastInsertRowId(item
, dbconn
, error
));
1409 secnotice("item", "inserted %@", item
);
1410 SecDbItemRecordUpdate(dbconn
, NULL
, item
);
1412 if (SecDbItemIsEngineInternalState(item
)) {
1413 secdebug ("item", "insert failed for item %@ with %@", item
, error
? *error
: NULL
);
1415 secnotice("item", "insert failed for item %@ with %@", item
, error
? *error
: NULL
);
1422 bool SecErrorIsSqliteDuplicateItemError(CFErrorRef error
) {
1423 return error
&& CFErrorGetCode(error
) == SQLITE_CONSTRAINT
&& CFEqual(kSecDbErrorDomain
, CFErrorGetDomain(error
));
1426 bool SecDbItemInsertOrReplace(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, void(^duplicate
)(SecDbItemRef item
, SecDbItemRef
*replace
)) {
1427 __block CFErrorRef localError
= NULL
;
1428 __block
bool ok
= SecDbItemDoInsert(item
, dbconn
, &localError
);
1429 if (!ok
&& localError
&& CFErrorGetCode(localError
) == SQLITE_CONSTRAINT
&& CFEqual(kSecDbErrorDomain
, CFErrorGetDomain(localError
))) {
1430 SecDbQueryRef query
= SecDbQueryCreateWithItemPrimaryKey(item
, error
);
1432 CFRetainAssign(query
->q_use_cred_handle
, item
->credHandle
);
1433 SecDbItemSelect(query
, dbconn
, error
, NULL
, ^bool(const SecDbAttr
*attr
) {
1434 return attr
->flags
& kSecDbPrimaryKeyFlag
;
1435 }, NULL
, NULL
, ^(SecDbItemRef old_item
, bool *stop
) {
1436 bool is_corrupt
= false;
1437 ok
= SecDbItemIsCorrupt(old_item
, &is_corrupt
, error
);
1438 SecDbItemRef replace
= NULL
;
1440 // If old_item is corrupted pretend it's not there and just replace it.
1444 CFReleaseNull(*error
); //item is corrupted and will be replaced, so drop the error
1445 } else if (ok
&& duplicate
) {
1446 duplicate(old_item
, &replace
);
1449 const SecDbAttr
*rowid_attr
= SecDbClassAttrWithKind(old_item
->class, kSecDbRowIdAttr
, error
);
1450 CFNumberRef oldrowid
= SecDbItemGetCachedValue(old_item
, rowid_attr
);
1452 ok
= SecDbItemSetValue(replace
, rowid_attr
, oldrowid
, &localError
);
1453 if (ok
&& !is_corrupt
) {
1454 ok
= SecDbItemMakeYounger(replace
, old_item
, error
);
1456 ok
= ok
&& SecDbItemDoUpdate(old_item
, replace
, dbconn
, &localError
, ^bool (const SecDbAttr
*attr
) {
1457 return attr
->kind
== kSecDbRowIdAttr
;
1460 ok
= SecError(errSecInternal
, &localError
, CFSTR("no rowid for %@"), old_item
);
1464 CFReleaseNull(localError
); // Clear the error, since we replaced the item.
1467 SecDbItemSetCredHandle(item
, query
->q_use_cred_handle
);
1468 ok
&= query_destroy(query
, error
);
1472 return ok
& SecErrorPropagate(localError
, error
); // Don't use && here!
1475 bool SecDbItemInsert(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1476 return SecDbItemInsertOrReplace(item
, dbconn
, error
, ^(SecDbItemRef old_item
, SecDbItemRef
*replace
) {
1477 if (SecDbItemIsTombstone(old_item
)) {
1484 static CFStringRef
SecDbItemCopyUpdateSQL(SecDbItemRef old_item
, SecDbItemRef new_item
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1485 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(new_item
), 0);
1486 CFStringAppend(sql
, CFSTR("UPDATE "));
1487 CFStringAppend(sql
, new_item
->class->name
);
1488 CFStringAppend(sql
, CFSTR(" SET "));
1489 bool needComma
= false;
1490 CFIndex used_attr
= 0;
1491 SecDbForEachAttrWithMask(new_item
->class, attr
, kSecDbInFlag
) {
1493 SecDbAppendElementEquals(sql
, attr
->name
, &needComma
);
1496 bool needWhere
= true;
1497 SecDbForEachAttr(old_item
->class, attr
) {
1498 if (use_attr_in_where(attr
)) {
1499 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1506 static bool SecDbItemUpdateBind(SecDbItemRef old_item
, SecDbItemRef new_item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1509 SecDbForEachAttrWithMask(new_item
->class, attr
, kSecDbInFlag
) {
1510 CFTypeRef value
= SecDbItemCopyValueForDb(new_item
, attr
, error
);
1511 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1512 CFReleaseSafe(value
);
1516 SecDbForEachAttr(old_item
->class, attr
) {
1517 if (use_attr_in_where(attr
)) {
1518 CFTypeRef value
= SecDbItemCopyValueForDb(old_item
, attr
, error
);
1519 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1520 CFReleaseSafe(value
);
1528 // Primary keys are the same -- do an update
1529 bool SecDbItemDoUpdate(SecDbItemRef old_item
, SecDbItemRef new_item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1530 CFStringRef sql
= SecDbItemCopyUpdateSQL(old_item
, new_item
, use_attr_in_where
);
1531 __block
bool ok
= sql
;
1533 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1534 ok
= SecDbItemUpdateBind(old_item
, new_item
, stmt
, error
, use_attr_in_where
) && SecDbStep(dbconn
, stmt
, error
, NULL
);
1539 if (SecDbItemIsEngineInternalState(old_item
)) {
1540 secdebug ("item", "replaced %@ in %@", old_item
, dbconn
);
1541 secdebug ("item", " with %@ in %@", new_item
, dbconn
);
1543 secnotice("item", "replaced %@ in %@", old_item
, dbconn
);
1544 secnotice("item", " with %@ in %@", new_item
, dbconn
);
1546 SecDbItemRecordUpdate(dbconn
, old_item
, new_item
);
1551 static CFStringRef
SecDbItemCopyDeleteSQL(SecDbItemRef item
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1552 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(item
), 0);
1553 CFStringAppend(sql
, CFSTR("DELETE FROM "));
1554 CFStringAppend(sql
, item
->class->name
);
1555 bool needWhere
= true;
1556 SecDbForEachAttr(item
->class, attr
) {
1557 if (use_attr_in_where(attr
)) {
1558 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1565 static bool SecDbItemDeleteBind(SecDbItemRef item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1568 SecDbForEachAttr(item
->class, attr
) {
1569 if (use_attr_in_where(attr
)) {
1570 CFTypeRef value
= SecDbItemCopyValueForDb(item
, attr
, error
);
1571 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1572 CFReleaseSafe(value
);
1580 static bool SecDbItemDoDeleteOnly(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1581 CFStringRef sql
= SecDbItemCopyDeleteSQL(item
, use_attr_in_where
);
1582 __block
bool ok
= sql
;
1584 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1585 ok
= SecDbItemDeleteBind(item
, stmt
, error
, use_attr_in_where
) && SecDbStep(dbconn
, stmt
, error
, NULL
);
1592 bool SecDbItemDoDeleteSilently(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1593 return SecDbItemDoDeleteOnly(item
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1594 return attr
->kind
== kSecDbRowIdAttr
;
1598 static bool SecDbItemDoDelete(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1599 bool ok
= SecDbItemDoDeleteOnly(item
, dbconn
, error
, use_attr_in_where
);
1601 secnotice("item", "deleted %@ from %@", item
, dbconn
);
1602 SecDbItemRecordUpdate(dbconn
, item
, NULL
);
1608 static bool SecDbItemDeleteTombstone(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1610 // TODO: Treat non decryptable items like tombstones here too and delete them
1611 SecDbItemRef tombstone
= SecDbItemCopyTombstone(item
, error
);
1614 ok
= SecDbItemClearRowId(tombstone
, error
);
1616 ok
= SecDbItemDoDelete(tombstone
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1617 return SecDbIsTombstoneDbSelectAttr(attr
);
1620 CFRelease(tombstone
);
1630 return SecCKKSIsEnabled();
1636 // Replace old_item with new_item. If primary keys are the same this does an update otherwise it does a delete + add
1637 bool SecDbItemUpdate(SecDbItemRef old_item
, SecDbItemRef new_item
, SecDbConnectionRef dbconn
, CFBooleanRef makeTombstone
, bool uuid_from_primary_key
, CFErrorRef
*error
) {
1638 __block
bool ok
= true;
1639 __block CFErrorRef localError
= NULL
;
1641 CFDataRef old_pk
= SecDbItemGetPrimaryKey(old_item
, error
);
1642 CFDataRef new_pk
= SecDbItemGetPrimaryKey(new_item
, error
);
1644 ok
= old_pk
&& new_pk
;
1646 bool pk_equal
= ok
&& CFEqual(old_pk
, new_pk
);
1648 ok
= SecDbItemMakeYounger(new_item
, old_item
, error
);
1649 } else if(!CFEqualSafe(makeTombstone
, kCFBooleanFalse
) && isCKKSEnabled()) {
1650 // The primary keys aren't equal, and we're going to make a tombstone.
1651 // Help CKKS out: the tombstone should have the existing item's UUID, and the newly updated item should have a new UUID.
1653 s3dl_item_make_new_uuid(new_item
, uuid_from_primary_key
, error
);
1655 ok
= ok
&& SecDbItemDoUpdate(old_item
, new_item
, dbconn
, &localError
, ^bool(const SecDbAttr
*attr
) {
1656 return attr
->kind
== kSecDbRowIdAttr
;
1660 if(CFErrorGetCode(localError
) == SQLITE_CONSTRAINT
&& CFEqual(kSecDbErrorDomain
, CFErrorGetDomain(localError
))) {
1661 /* Update failed because we changed the PrimaryKey and there was a dup.
1662 Find the dup and see if it is a tombstone or corrupted item. */
1663 SecDbQueryRef query
= SecDbQueryCreateWithItemPrimaryKey(new_item
, error
);
1666 ok
&= SecDbItemSelect(query
, dbconn
, error
, NULL
, ^bool(const SecDbAttr
*attr
) {
1667 return attr
->flags
& kSecDbPrimaryKeyFlag
;
1668 }, NULL
, NULL
, ^(SecDbItemRef duplicate_item
, bool *stop
) {
1669 bool is_corrupt
= false;
1670 bool is_tomb
= false;
1671 ok
= SecDbItemIsCorrupt(duplicate_item
, &is_corrupt
, error
);
1672 if (ok
&& !is_corrupt
) {
1673 if ((is_tomb
= SecDbItemIsTombstone(duplicate_item
)))
1674 ok
= SecDbItemMakeYounger(new_item
, duplicate_item
, error
);
1676 if (ok
&& (is_corrupt
|| is_tomb
)) {
1677 ok
= SecDbItemDoDelete(old_item
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1678 return attr
->kind
== kSecDbRowIdAttr
;
1680 ok
= ok
&& SecDbItemDoUpdate(duplicate_item
, new_item
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1681 return attr
->kind
== kSecDbRowIdAttr
;
1683 CFReleaseNull(localError
);
1686 ok
&= query_destroy(query
, error
);
1692 if (error
&& *error
== NULL
) {
1693 *error
= localError
;
1696 CFReleaseSafe(localError
);
1700 if (ok
&& !pk_equal
&& !CFEqualSafe(makeTombstone
, kCFBooleanFalse
)) {
1701 /* The primary key of new_item is different than that of old_item, we
1702 have been asked to make a tombstone so leave one for the old_item. */
1703 SecDbItemRef tombstone
= SecDbItemCopyTombstone(old_item
, makeTombstone
, error
);
1706 ok
= (SecDbItemClearRowId(tombstone
, error
) &&
1707 SecDbItemDoInsert(tombstone
, dbconn
, error
));
1708 CFRelease(tombstone
);
1715 // Replace the object with a tombstone
1716 bool SecDbItemDelete(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFBooleanRef makeTombstone
, CFErrorRef
*error
) {
1718 if (!CFEqualSafe(makeTombstone
, kCFBooleanFalse
)) {
1719 SecDbItemRef tombstone
= SecDbItemCopyTombstone(item
, makeTombstone
, error
);
1721 ok
= SecDbItemDoUpdate(item
, tombstone
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1722 return attr
->kind
== kSecDbRowIdAttr
;
1724 CFRelease(tombstone
);
1727 ok
= SecDbItemDoDelete(item
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1728 return attr
->kind
== kSecDbRowIdAttr
;
1734 CFStringRef
SecDbItemCopySelectSQL(SecDbQueryRef query
,
1735 bool (^return_attr
)(const SecDbAttr
*attr
),
1736 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1737 bool (^add_where_sql
)(CFMutableStringRef sql
, bool *needWhere
)) {
1738 CFMutableStringRef sql
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
1739 CFStringAppend(sql
, CFSTR("SELECT "));
1740 // What are we selecting?
1741 bool needComma
= false;
1742 SecDbForEachAttr(query
->q_class
, attr
) {
1743 if (return_attr(attr
))
1744 SecDbAppendElement(sql
, attr
->name
, &needComma
);
1747 // From which table?
1748 CFStringAppend(sql
, CFSTR(" FROM "));
1749 CFStringAppend(sql
, query
->q_class
->name
);
1751 // And which elements do we want to select
1752 bool needWhere
= true;
1753 SecDbForEachAttr(query
->q_class
, attr
) {
1754 if (use_attr_in_where(attr
)) {
1755 CFTypeRef value
= CFDictionaryGetValue(query
->q_item
, attr
->name
);
1756 if (isArray(value
)) {
1757 CFArrayRef array
= (CFArrayRef
)value
;
1758 CFIndex length
= CFArrayGetCount(array
);
1760 CFTypeRef head
= CFArrayGetValueAtIndex(array
, 0);
1761 if (CFEqualSafe(head
, kCFNull
)) {
1762 SecDbAppendWhereOrAndNotIn(sql
, attr
->name
, &needWhere
, length
- 1);
1764 SecDbAppendWhereOrAndIn(sql
, attr
->name
, &needWhere
, length
);
1768 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1772 // Append SQL for access groups and limits.
1774 add_where_sql(sql
, &needWhere
);
1779 static bool SecDbItemSelectBindValue(SecDbQueryRef query
, sqlite3_stmt
*stmt
, int param
, const SecDbAttr
*attr
, CFTypeRef inValue
, CFErrorRef
*error
) {
1781 CFTypeRef value
= NULL
;
1782 if (attr
->kind
== kSecDbRowIdAttr
) {
1783 // TODO: Ignores inValue and uses rowid directly instead HACK should go
1784 value
= CFNumberCreate(NULL
, kCFNumberSInt64Type
, &query
->q_row_id
);
1786 value
= SecDbAttrCopyValueForDb(attr
, inValue
, error
);
1788 ok
= ok
&& value
!= NULL
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1789 CFReleaseSafe(value
);
1793 bool SecDbItemSelectBind(SecDbQueryRef query
, sqlite3_stmt
*stmt
, CFErrorRef
*error
,
1794 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1795 bool (^bind_added_where
)(sqlite3_stmt
*stmt
, int col
)) {
1796 __block
bool ok
= true;
1797 __block
int param
= 0;
1798 SecDbForEachAttr(query
->q_class
, attr
) {
1799 if (use_attr_in_where(attr
)) {
1800 CFTypeRef value
= CFDictionaryGetValue(query
->q_item
, attr
->name
);
1801 if (isArray(value
)) {
1802 CFArrayRef array
= (CFArrayRef
)value
;
1803 CFRange range
= {.location
= 0, .length
= CFArrayGetCount(array
) };
1804 if (range
.length
> 0) {
1805 CFTypeRef head
= CFArrayGetValueAtIndex(array
, 0);
1806 if (CFEqualSafe(head
, kCFNull
)) {
1811 CFArrayApplyFunction(array
, range
, apply_block_1
, (void (^)(const void *value
)) ^(const void *arrayValue
) {
1812 ok
= SecDbItemSelectBindValue(query
, stmt
, param
++, attr
, arrayValue
, error
);
1815 ok
= SecDbItemSelectBindValue(query
, stmt
, param
++, attr
, value
, error
);
1822 // TODO: Bind arguments for access groups and limits.
1823 if (bind_added_where
)
1824 bind_added_where(stmt
, ++param
);
1829 bool SecDbItemSelect(SecDbQueryRef query
, SecDbConnectionRef dbconn
, CFErrorRef
*error
,
1830 bool (^return_attr
)(const SecDbAttr
*attr
),
1831 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1832 bool (^add_where_sql
)(CFMutableStringRef sql
, bool *needWhere
),
1833 bool (^bind_added_where
)(sqlite3_stmt
*stmt
, int col
),
1834 void (^handle_row
)(SecDbItemRef item
, bool *stop
)) {
1835 __block
bool ok
= true;
1836 if (return_attr
== NULL
) {
1837 return_attr
= ^bool (const SecDbAttr
* attr
) {
1838 return attr
->kind
== kSecDbRowIdAttr
|| attr
->kind
== kSecDbEncryptedDataAttr
|| attr
->kind
== kSecDbSHA1Attr
;
1841 if (use_attr_in_where
== NULL
) {
1842 use_attr_in_where
= ^bool (const SecDbAttr
* attr
) { return false; };
1845 CFStringRef sql
= SecDbItemCopySelectSQL(query
, return_attr
, use_attr_in_where
, add_where_sql
);
1847 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1848 ok
= (SecDbItemSelectBind(query
, stmt
, error
, use_attr_in_where
, bind_added_where
) &&
1849 SecDbStep(dbconn
, stmt
, error
, ^(bool *stop
) {
1850 SecDbItemRef item
= SecDbItemCreateWithStatement(kCFAllocatorDefault
, query
->q_class
, stmt
, query
->q_keybag
, error
, return_attr
);
1852 CFRetainAssign(item
->credHandle
, query
->q_use_cred_handle
);
1853 handle_row(item
, stop
);