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.)
29 #include <securityd/SecDbItem.h>
30 #include <securityd/SecDbKeychainItem.h>
31 #include <securityd/SecItemDb.h>
32 #include <utilities/SecCFWrappers.h>
33 #include <utilities/SecCFCCWrappers.h>
34 #include <utilities/der_date.h>
35 #include <utilities/der_plist.h>
36 #include <utilities/debugging.h>
38 #include <Security/SecBasePriv.h>
39 #include <Security/SecInternal.h>
40 #include <corecrypto/ccsha1.h>
41 #include <Security/SecItem.h>
42 #include <Security/SecItemPriv.h>
43 #include <Security/SecAccessControl.h>
44 #include <Security/SecAccessControlPriv.h>
45 #include <securityd/SecItemSchema.h>
47 // MARK: type converters
49 CFStringRef
copyString(CFTypeRef obj
) {
50 CFTypeID tid
= CFGetTypeID(obj
);
51 if (tid
== CFStringGetTypeID())
52 return CFStringCreateCopy(0, obj
);
53 else if (tid
== CFDataGetTypeID())
54 return CFStringCreateFromExternalRepresentation(0, obj
, kCFStringEncodingUTF8
);
59 CFDataRef
copyData(CFTypeRef obj
) {
60 CFTypeID tid
= CFGetTypeID(obj
);
61 if (tid
== CFDataGetTypeID()) {
62 return CFDataCreateCopy(0, obj
);
63 } else if (tid
== CFStringGetTypeID()) {
64 return CFStringCreateExternalRepresentation(0, obj
, kCFStringEncodingUTF8
, 0);
65 } else if (tid
== CFNumberGetTypeID()) {
67 CFNumberGetValue(obj
, kCFNumberSInt32Type
, &value
);
68 return CFDataCreate(0, (const UInt8
*)&value
, sizeof(value
));
74 CFTypeRef
copyUUID(CFTypeRef obj
) {
75 CFTypeID tid
= CFGetTypeID(obj
);
76 if (tid
== CFDataGetTypeID()) {
77 CFIndex length
= CFDataGetLength(obj
);
78 if (length
!= 0 && length
!= 16)
80 return CFDataCreateCopy(NULL
, obj
);
81 } if (tid
== CFNullGetTypeID()) {
82 return CFDataCreate(NULL
, NULL
, 0);
89 CFTypeRef
copyBlob(CFTypeRef obj
) {
90 CFTypeID tid
= CFGetTypeID(obj
);
91 if (tid
== CFDataGetTypeID()) {
92 return CFDataCreateCopy(0, obj
);
93 } else if (tid
== CFStringGetTypeID()) {
94 return CFStringCreateCopy(0, obj
);
95 } else if (tid
== CFNumberGetTypeID()) {
103 CFDataRef
copySHA1(CFTypeRef obj
) {
104 CFTypeID tid
= CFGetTypeID(obj
);
105 if (tid
== CFDataGetTypeID() && CFDataGetLength(obj
) == CCSHA1_OUTPUT_SIZE
) {
106 return CFDataCreateCopy(CFGetAllocator(obj
), obj
);
112 CFTypeRef
copyNumber(CFTypeRef obj
) {
113 CFTypeID tid
= CFGetTypeID(obj
);
114 if (tid
== CFNumberGetTypeID()) {
117 } else if (tid
== CFBooleanGetTypeID()) {
118 SInt32 value
= CFBooleanGetValue(obj
);
119 return CFNumberCreate(0, kCFNumberSInt32Type
, &value
);
120 } else if (tid
== CFStringGetTypeID()) {
121 SInt32 value
= CFStringGetIntValue(obj
);
122 CFStringRef t
= CFStringCreateWithFormat(0, 0, CFSTR("%ld"), (long) value
);
123 /* If a string converted to an int isn't equal to the int printed as
124 a string, return a CFStringRef instead. */
125 if (!CFEqual(t
, obj
)) {
127 return CFStringCreateCopy(0, obj
);
130 return CFNumberCreate(0, kCFNumberSInt32Type
, &value
);
135 CFDateRef
copyDate(CFTypeRef obj
) {
136 CFTypeID tid
= CFGetTypeID(obj
);
137 if (tid
== CFDateGetTypeID()) {
144 // MARK: SecDbColumn accessors, to retrieve values as CF types in SecDbStep.
146 static CFDataRef
SecDbColumnCopyData(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
147 return CFDataCreate(allocator
, sqlite3_column_blob(stmt
, col
),
148 sqlite3_column_bytes(stmt
, col
));
149 //return CFDataCreateWithBytesNoCopy(0, sqlite3_column_blob(stmt, col),
150 // sqlite3_column_bytes(stmt, col),
151 // kCFAllocatorNull);
154 static CFDateRef
SecDbColumnCopyDate(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
155 return CFDateCreate(allocator
, sqlite3_column_double(stmt
, col
));
158 static CFNumberRef
SecDbColumnCopyDouble(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
159 double number
= sqlite3_column_double(stmt
, col
);
160 return CFNumberCreate(allocator
, kCFNumberDoubleType
, &number
);
163 static CFNumberRef
SecDbColumnCopyNumber64(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
164 sqlite_int64 number
= sqlite3_column_int64(stmt
, col
);
165 return CFNumberCreate(allocator
, kCFNumberSInt64Type
, &number
);
168 static CFNumberRef
SecDbColumnCopyNumber(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
) {
169 sqlite_int64 number
= sqlite3_column_int64(stmt
, col
);
170 if (INT32_MIN
<= number
&& number
<= INT32_MAX
) {
171 int32_t num32
= (int32_t)number
;
172 return CFNumberCreate(allocator
, kCFNumberSInt32Type
, &num32
);
174 return CFNumberCreate(allocator
, kCFNumberSInt64Type
, &number
);
178 static CFTypeRef
SecDbColumnCopyString(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, int col
, CFErrorRef
*error
,
179 CFOptionFlags flags
) {
180 const unsigned char *text
= sqlite3_column_text(stmt
, col
);
181 if (!text
|| 0 == strlen((const char *)text
)) {
182 if (flags
& kSecDbDefaultEmptyFlag
) {
184 } else if (flags
& kSecDbDefault0Flag
) {
190 return CFStringCreateWithBytes(allocator
, text
, strlen((const char *)text
), kCFStringEncodingUTF8
, false);
193 // MARK: SecDbClass helpers
195 const SecDbAttr
*SecDbClassAttrWithKind(const SecDbClass
*class, SecDbAttrKind kind
, CFErrorRef
*error
) {
196 const SecDbAttr
*result
= NULL
;
197 SecDbForEachAttr(class, desc
) {
198 if (desc
->kind
== kind
)
203 SecError(errSecInternal
, error
, CFSTR("Can't find attribute of kind %d in class %@"), kind
, class->name
);
208 // MARK: SecDbAttr helpers
210 static bool SecDbIsTombstoneDbSelectAttr(const SecDbAttr
*attr
) {
211 return attr
->flags
& kSecDbPrimaryKeyFlag
|| attr
->kind
== kSecDbTombAttr
;
215 static bool SecDbIsTombstoneDbInsertAttr(const SecDbAttr
*attr
) {
216 return SecDbIsTombstoneDbSelectAttr(attr
) || attr
->kind
== kSecDbAccessAttr
|| attr
->kind
== kSecDbCreationDateAttr
|| attr
->kind
== kSecDbModificationDateAttr
;
220 static bool SecDbIsTombstoneDbUpdateAttr(const SecDbAttr
*attr
) {
221 return SecDbIsTombstoneDbSelectAttr(attr
) || attr
->kind
== kSecDbAccessAttr
|| attr
->kind
== kSecDbCreationDateAttr
|| attr
->kind
== kSecDbRowIdAttr
;
224 CFTypeRef
SecDbAttrCopyDefaultValue(const SecDbAttr
*attr
, CFErrorRef
*error
) {
225 CFTypeRef value
= NULL
;
226 switch (attr
->kind
) {
227 case kSecDbAccessAttr
:
228 case kSecDbStringAttr
:
229 case kSecDbAccessControlAttr
:
234 value
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
237 value
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
239 case kSecDbNumberAttr
:
244 value
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &zero
);
248 value
= CFDateCreate(kCFAllocatorDefault
, 0.0);
250 case kSecDbCreationDateAttr
:
251 case kSecDbModificationDateAttr
:
252 value
= CFDateCreate(kCFAllocatorDefault
, CFAbsoluteTimeGetCurrent());
255 SecError(errSecInternal
, error
, CFSTR("attr %@ has no default value"), attr
->name
);
262 static CFTypeRef
SecDbAttrCopyValueForDb(const SecDbAttr
*attr
, CFTypeRef value
, CFErrorRef
*error
) {
263 CFDataRef data
= NULL
;
264 CFTypeRef result
= NULL
;
269 if (CFEqual(value
, kCFNull
) && attr
->flags
& kSecDbPrimaryKeyFlag
) {
270 // SQLITE3 doesn't like NULL for primary key attributes, pretend kSecDbDefaultEmptyFlag was specified
271 require_quiet(result
= SecDbAttrCopyDefaultValue(attr
, error
), out
);
273 result
= CFRetain(value
);
276 if (attr
->flags
& kSecDbSHA1ValueInFlag
&& !CFEqual(result
, kCFNull
)) {
277 require_action_quiet(data
= copyData(result
), out
,
278 SecError(errSecInternal
, error
, CFSTR("failed to get attribute %@ data"), attr
->name
);
279 CFReleaseNull(result
));
280 CFAssignRetained(result
, CFDataCopySHA1Digest(data
, error
));
288 static CFStringRef
SecDbAttrGetHashName(const SecDbAttr
*attr
) {
289 if ((attr
->flags
& kSecDbSHA1ValueInFlag
) == 0) {
293 static dispatch_once_t once
;
294 static CFMutableDictionaryRef hash_store
;
295 static dispatch_queue_t queue
;
296 dispatch_once(&once
, ^{
297 queue
= dispatch_queue_create("secd-hash-name", NULL
);
298 hash_store
= CFDictionaryCreateMutableForCFTypes(NULL
);
301 __block CFStringRef name
;
302 dispatch_sync(queue
, ^{
303 name
= CFDictionaryGetValue(hash_store
, attr
->name
);
305 name
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("#%@"), attr
->name
);
306 CFDictionarySetValue(hash_store
, attr
->name
, name
);
315 CFTypeRef
SecDbItemGetCachedValueWithName(SecDbItemRef item
, CFStringRef name
) {
316 return CFDictionaryGetValue(item
->attributes
, name
);
319 static CFTypeRef
SecDbItemGetCachedValue(SecDbItemRef item
, const SecDbAttr
*desc
) {
320 return CFDictionaryGetValue(item
->attributes
, desc
->name
);
323 CFMutableDictionaryRef
SecDbItemCopyPListWithMask(SecDbItemRef item
, CFOptionFlags mask
, CFErrorRef
*error
) {
324 CFMutableDictionaryRef dict
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
325 SecDbForEachAttrWithMask(item
->class, desc
, mask
) {
326 CFTypeRef value
= SecDbItemGetValue(item
, desc
, error
);
328 if (!CFEqual(kCFNull
, value
)) {
329 CFDictionarySetValue(dict
, desc
->name
, value
);
330 } else if (desc
->flags
& kSecDbNotNullFlag
) {
331 SecError(errSecDecode
, error
, CFSTR("attribute %@ has NULL value"), desc
->name
);
332 secerror("%@", error
? *error
: (CFErrorRef
)CFSTR("error == NULL"));
344 void SecDbItemSetCredHandle(SecDbItemRef item
, CFTypeRef cred_handle
) {
345 CFRetainAssign(item
->credHandle
, cred_handle
);
348 void SecDbItemSetCallerAccessGroups(SecDbItemRef item
, CFArrayRef caller_access_groups
) {
349 CFRetainAssign(item
->callerAccessGroups
, caller_access_groups
);
352 CFDataRef
SecDbItemCopyEncryptedDataToBackup(SecDbItemRef item
, uint64_t handle
, CFErrorRef
*error
) {
353 CFDataRef edata
= NULL
;
354 keybag_handle_t keybag
= (keybag_handle_t
)handle
;
355 CFMutableDictionaryRef attributes
= SecDbItemCopyPListWithMask(item
, kSecDbInCryptoDataFlag
, error
);
356 CFMutableDictionaryRef auth_attributes
= SecDbItemCopyPListWithMask(item
, kSecDbInAuthenticatedDataFlag
, error
);
357 if (attributes
|| auth_attributes
) {
358 SecAccessControlRef access_control
= SecDbItemCopyAccessControl(item
, error
);
359 if (access_control
) {
360 if (ks_encrypt_data(keybag
, access_control
, item
->credHandle
, attributes
, auth_attributes
, &edata
, false, error
)) {
361 item
->_edataState
= kSecDbItemEncrypting
;
363 seccritical("ks_encrypt_data (db): failed: %@", error
? *error
: (CFErrorRef
)CFSTR(""));
365 CFRelease(access_control
);
367 CFReleaseSafe(attributes
);
368 CFReleaseSafe(auth_attributes
);
373 bool SecDbItemEnsureDecrypted(SecDbItemRef item
, CFErrorRef
*error
) {
375 // If we haven't yet decrypted the item, make sure we do so now
377 if (item
->_edataState
== kSecDbItemEncrypted
) {
378 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, error
);
380 CFDataRef edata
= SecDbItemGetCachedValue(item
, attr
);
382 return SecError(errSecInternal
, error
, CFSTR("state= encrypted but edata is NULL"));
383 // Decrypt calls set value a bunch of times which clears our edata and changes our state.
384 item
->_edataState
= kSecDbItemDecrypting
;
385 result
= SecDbItemDecrypt(item
, edata
, error
);
387 item
->_edataState
= kSecDbItemClean
;
389 item
->_edataState
= kSecDbItemEncrypted
;
395 // Only called if cached value is not found.
396 static CFTypeRef
SecDbItemCopyValue(SecDbItemRef item
, const SecDbAttr
*attr
, CFErrorRef
*error
) {
398 return attr
->copyValue(item
, attr
, error
);
400 CFTypeRef value
= NULL
;
401 switch (attr
->kind
) {
402 // These have an explicit copyValue; here to shut up compiler
404 case kSecDbEncryptedDataAttr
:
405 case kSecDbPrimaryKeyAttr
:
408 case kSecDbAccessAttr
:
409 case kSecDbStringAttr
:
411 case kSecDbAccessControlAttr
:
412 if (attr
->flags
& kSecDbNotNullFlag
) {
413 if (attr
->flags
& kSecDbDefault0Flag
) {
416 } else if (attr
->kind
!= kSecDbBlobAttr
&& attr
->flags
& kSecDbDefaultEmptyFlag
) {
417 // blob drops through to data everything else is empty string
424 if (attr
->flags
& kSecDbNotNullFlag
&& attr
->flags
& kSecDbDefaultEmptyFlag
) {
425 value
= CFDataCreate(CFGetAllocator(item
), NULL
, 0);
431 value
= CFDataCreate(CFGetAllocator(item
), NULL
, 0);
433 case kSecDbNumberAttr
:
436 if (attr
->flags
& kSecDbNotNullFlag
) {
438 value
= CFNumberCreate(CFGetAllocator(item
), kCFNumberSInt32Type
, &zero
);
444 if (attr
->flags
& kSecDbNotNullFlag
&& attr
->flags
& kSecDbDefault0Flag
) {
445 value
= CFDateCreate(kCFAllocatorDefault
, 0.0);
450 case kSecDbRowIdAttr
:
451 if (attr
->flags
& kSecDbNotNullFlag
) {
456 case kSecDbCreationDateAttr
:
457 case kSecDbModificationDateAttr
:
458 value
= CFDateCreate(CFGetAllocator(item
), CFAbsoluteTimeGetCurrent());
460 case kSecDbUTombAttr
:
468 // SecDbItemGetValue will return kCFNull if there is no value for an attribute and this was not
469 // an error. It will return NULL and optionally set *error if there was an error computing an
470 // attribute, or if a required attribute was missing a value and had no known way to compute
472 CFTypeRef
SecDbItemGetValue(SecDbItemRef item
, const SecDbAttr
*desc
, CFErrorRef
*error
) {
473 // Propagate chained errors
477 if (desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
) {
478 if (!SecDbItemEnsureDecrypted(item
, error
))
482 CFTypeRef value
= SecDbItemGetCachedValue(item
, desc
);
484 value
= SecDbItemCopyValue(item
, desc
, error
);
486 if (CFEqual(kCFNull
, value
)) {
487 CFRelease(value
); // This is redundant but it shuts clang's static analyzer up.
490 SecDbItemSetValue(item
, desc
, value
, error
);
492 value
= SecDbItemGetCachedValue(item
, desc
);
499 // Similar as SecDbItemGetValue, but if attr represents attribute stored into DB field as hash, returns
500 // hashed value for the attribute.
501 static CFTypeRef
SecDbItemCopyValueForDb(SecDbItemRef item
, const SecDbAttr
*desc
, CFErrorRef
*error
) {
502 CFTypeRef value
= NULL
;
503 CFStringRef hash_name
= NULL
;
504 hash_name
= SecDbAttrGetHashName(desc
);
505 if ((desc
->flags
& kSecDbSHA1ValueInFlag
) && (desc
->flags
& kSecDbInFlag
)) {
506 value
= CFRetainSafe(CFDictionaryGetValue(item
->attributes
, hash_name
));
510 require_quiet(value
= SecDbItemGetValue(item
, desc
, error
), out
);
511 require_action_quiet(value
= SecDbAttrCopyValueForDb(desc
, value
, error
), out
, CFReleaseNull(value
));
512 if ((desc
->flags
& kSecDbSHA1ValueInFlag
) != 0) {
513 CFDictionarySetValue(item
->attributes
, hash_name
, value
);
521 static bool SecDbItemGetBoolValue(SecDbItemRef item
, const SecDbAttr
*desc
, bool *bvalue
, CFErrorRef
*error
) {
522 CFTypeRef value
= SecDbItemGetValue(item
, desc
, error
);
526 *bvalue
= (isNumber(value
) && CFNumberGetValue(value
, kCFNumberCharType
, &cvalue
) && cvalue
== 1);
530 static CFStringRef
SecDbItemCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
532 if (isDictionary(formatOptions
) && CFDictionaryContainsKey(formatOptions
, kSecDebugFormatOption
)) {
533 SecDbItemRef item
= (SecDbItemRef
)cf
;
534 CFMutableStringRef mdesc
= CFStringCreateMutable(CFGetAllocator(cf
), 0);
535 CFStringAppendFormat(mdesc
, NULL
, CFSTR("<%@"), item
->class->name
);
536 SecDbForEachAttr(item
->class, attr
) {
537 CFTypeRef value
= SecDbItemGetValue(item
, attr
, NULL
);
539 CFStringAppend(mdesc
, CFSTR(","));
540 CFStringAppend(mdesc
, attr
->name
);
541 CFStringAppend(mdesc
, CFSTR("="));
542 if (CFEqual(CFSTR("data"), attr
->name
)) {
543 CFStringAppendEncryptedData(mdesc
, value
);
544 } else if (CFEqual(CFSTR("v_Data"), attr
->name
)) {
545 CFStringAppend(mdesc
, CFSTR("<?>"));
546 } else if (isData(value
)) {
547 CFStringAppendHexData(mdesc
, value
);
549 CFStringAppendFormat(mdesc
, 0, CFSTR("%@"), value
);
553 CFStringAppend(mdesc
, CFSTR(">"));
556 SecDbItemRef item
= (SecDbItemRef
)cf
;
557 const UInt8 zero4
[4] = {};
558 const UInt8
*pk
= &zero4
[0], *sha1
= &zero4
[0];
562 CFStringRef access
= NULL
;
563 uint8_t mdatbuf
[32] = {};
564 uint8_t *mdat
= &mdatbuf
[0];
565 CFMutableStringRef attrs
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
566 CFStringRef agrp
= NULL
;
567 CFBooleanRef utomb
= NULL
;
569 SecDbForEachAttr(item
->class, attr
) {
571 switch (attr
->kind
) {
574 case kSecDbStringAttr
:
575 case kSecDbNumberAttr
:
577 case kSecDbEncryptedDataAttr
:
578 if (attr
->flags
& (kSecDbReturnAttrFlag
| kSecDbReturnDataFlag
) && (value
= SecDbItemGetValue(item
, attr
, NULL
)) && !CFEqual(value
, kCFNull
)) {
579 if (isString(value
) && CFEqual(attr
->name
, kSecAttrAccessGroup
)) {
582 // We don't log these, just record that we saw the attribute.
583 CFStringAppend(attrs
, CFSTR(","));
584 CFStringAppend(attrs
, attr
->name
);
589 if ((value
= SecDbItemGetValue(item
, attr
, NULL
))) {
590 if (CFEqual(attr
->name
, kSecAttrMultiUser
)) {
592 CFStringAppend(attrs
, CFSTR(","));
593 if (CFDataGetLength(value
)) {
594 CFStringAppendHexData(attrs
, value
);
596 CFStringAppend(attrs
, attr
->name
);
602 case kSecDbCreationDateAttr
:
603 // We don't care about this and every object has one.
605 case kSecDbModificationDateAttr
:
606 value
= SecDbItemGetValue(item
, attr
, NULL
);
608 mdat
= der_encode_generalizedtime_body(CFDateGetAbsoluteTime(value
), NULL
, mdat
, &mdatbuf
[31]);
611 value
= SecDbItemGetValue(item
, attr
, NULL
);
612 if (isData(value
) && CFDataGetLength(value
) >= (CFIndex
)sizeof(zero4
))
613 sha1
= CFDataGetBytePtr(value
);
615 case kSecDbRowIdAttr
:
616 value
= SecDbItemGetValue(item
, attr
, NULL
);
618 CFNumberGetValue(value
, kCFNumberSInt64Type
, &rowid
);
620 case kSecDbPrimaryKeyAttr
:
621 value
= SecDbItemGetValue(item
, attr
, NULL
);
623 pk
= CFDataGetBytePtr(value
);
626 value
= SecDbItemGetValue(item
, attr
, NULL
);
628 CFNumberGetValue(value
, kCFNumberCharType
, &sync
);
631 value
= SecDbItemGetValue(item
, attr
, NULL
);
633 CFNumberGetValue(value
, kCFNumberCharType
, &tomb
);
635 case kSecDbAccessAttr
:
636 value
= SecDbItemGetValue(item
, attr
, NULL
);
640 case kSecDbUTombAttr
:
641 value
= SecDbItemGetValue(item
, attr
, NULL
);
642 if (isBoolean(value
))
644 case kSecDbAccessControlAttr
:
645 /* TODO: Add formatting of ACLs. */
650 desc
= CFStringCreateWithFormat(CFGetAllocator(cf
), NULL
,
665 pk
[0], pk
[1], pk
[2], pk
[3],
672 utomb
? (CFEqual(utomb
, kCFBooleanFalse
) ? "F," : "T,") : "",
673 sha1
[0], sha1
[1], sha1
[2], sha1
[3]);
674 CFReleaseSafe(attrs
);
680 static void SecDbItemDestroy(CFTypeRef cf
) {
681 SecDbItemRef item
= (SecDbItemRef
)cf
;
682 CFReleaseSafe(item
->attributes
);
683 CFReleaseSafe(item
->credHandle
);
684 CFReleaseSafe(item
->callerAccessGroups
);
685 CFReleaseSafe(item
->cryptoOp
);
688 static CFHashCode
SecDbItemHash(CFTypeRef cf
) {
689 SecDbItemRef item
= (SecDbItemRef
)cf
;
690 CFDataRef digest
= SecDbItemGetSHA1(item
, NULL
);
692 const UInt8
*p
= CFDataGetBytePtr(digest
);
693 // Read first 8 bytes of digest in order
694 code
= p
[0] + ((p
[1] + ((p
[2] + ((p
[3] + ((p
[4] + ((p
[5] + ((p
[6] + (p
[7] << 8)) << 8)) << 8)) << 8)) << 8)) << 8)) << 8);
698 static Boolean
SecDbItemCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
699 SecDbItemRef item1
= (SecDbItemRef
)cf1
;
700 SecDbItemRef item2
= (SecDbItemRef
)cf2
;
701 CFDataRef digest1
= NULL
;
702 CFDataRef digest2
= NULL
;
704 digest1
= SecDbItemGetSHA1(item1
, NULL
);
706 digest2
= SecDbItemGetSHA1(item2
, NULL
);
707 Boolean equal
= CFEqual(digest1
, digest2
);
711 CFGiblisWithHashFor(SecDbItem
)
713 static SecDbItemRef
SecDbItemCreate(CFAllocatorRef allocator
, const SecDbClass
*class, keybag_handle_t keybag
) {
714 SecDbItemRef item
= CFTypeAllocate(SecDbItem
, struct SecDbItem
, allocator
);
716 item
->attributes
= CFDictionaryCreateMutableForCFTypes(allocator
);
717 item
->keybag
= keybag
;
718 item
->_edataState
= kSecDbItemDirty
;
719 item
->cryptoOp
= kAKSKeyOpDecrypt
;
723 const SecDbClass
*SecDbItemGetClass(SecDbItemRef item
) {
727 keybag_handle_t
SecDbItemGetKeybag(SecDbItemRef item
) {
731 bool SecDbItemSetKeybag(SecDbItemRef item
, keybag_handle_t keybag
, CFErrorRef
*error
) {
732 if (!SecDbItemEnsureDecrypted(item
, error
))
734 if (item
->keybag
!= keybag
) {
735 item
->keybag
= keybag
;
736 if (item
->_edataState
== kSecDbItemClean
) {
737 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, NULL
), kCFNull
, NULL
);
744 bool SecDbItemSetValue(SecDbItemRef item
, const SecDbAttr
*desc
, CFTypeRef value
, CFErrorRef
*error
) {
745 // Propagate chained errors.
753 return desc
->setValue(item
, desc
, value
, error
);
755 if (desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
)
756 if (!SecDbItemEnsureDecrypted(item
, error
))
759 bool changed
= false;
760 CFTypeRef attr
= NULL
;
761 switch (desc
->kind
) {
762 case kSecDbPrimaryKeyAttr
:
764 attr
= copyData(value
);
766 case kSecDbEncryptedDataAttr
:
767 attr
= copyData(value
);
769 if (item
->_edataState
== kSecDbItemEncrypting
)
770 item
->_edataState
= kSecDbItemClean
;
772 item
->_edataState
= kSecDbItemEncrypted
;
773 } else if (!value
|| CFEqual(kCFNull
, value
)) {
774 item
->_edataState
= kSecDbItemDirty
;
778 case kSecDbAccessControlAttr
:
779 attr
= copyBlob(value
);
782 case kSecDbCreationDateAttr
:
783 case kSecDbModificationDateAttr
:
784 attr
= copyDate(value
);
786 case kSecDbNumberAttr
:
789 case kSecDbRowIdAttr
:
790 attr
= copyNumber(value
);
792 case kSecDbAccessAttr
:
793 case kSecDbStringAttr
:
794 attr
= copyString(value
);
797 attr
= copySHA1(value
);
799 case kSecDbUTombAttr
:
800 attr
= CFRetainSafe(asBoolean(value
, NULL
));
803 attr
= copyUUID(value
);
808 CFTypeRef ovalue
= CFDictionaryGetValue(item
->attributes
, desc
->name
);
809 changed
= (!ovalue
|| !CFEqual(ovalue
, attr
));
810 CFDictionarySetValue(item
->attributes
, desc
->name
, attr
);
813 if (value
&& !CFEqual(kCFNull
, value
)) {
814 SecError(errSecItemInvalidValue
, error
, CFSTR("attribute %@: value: %@ failed to convert"), desc
->name
, value
);
817 CFTypeRef ovalue
= CFDictionaryGetValue(item
->attributes
, desc
->name
);
818 changed
= (ovalue
&& !CFEqual(ovalue
, kCFNull
));
819 CFDictionaryRemoveValue(item
->attributes
, desc
->name
);
823 if (desc
->flags
& kSecDbInHashFlag
)
824 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, NULL
), kCFNull
, NULL
);
825 if (desc
->flags
& kSecDbPrimaryKeyFlag
)
826 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbPrimaryKeyAttr
, NULL
), kCFNull
, NULL
);
827 if ((desc
->flags
& kSecDbInCryptoDataFlag
|| desc
->flags
& kSecDbInAuthenticatedDataFlag
) && item
->_edataState
== kSecDbItemClean
)
828 SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, NULL
), kCFNull
, NULL
);
829 if (desc
->flags
& kSecDbSHA1ValueInFlag
)
830 CFDictionaryRemoveValue(item
->attributes
, SecDbAttrGetHashName(desc
));
836 bool SecDbItemSetValues(SecDbItemRef item
, CFDictionaryRef values
, CFErrorRef
*error
) {
837 SecDbForEachAttr(item
->class, attr
) {
838 CFTypeRef value
= CFDictionaryGetValue(values
, attr
->name
);
839 if (value
&& !SecDbItemSetValue(item
, attr
, value
, error
))
845 bool SecDbItemSetValueWithName(SecDbItemRef item
, CFStringRef name
, CFTypeRef value
, CFErrorRef
*error
) {
846 SecDbForEachAttr(item
->class, attr
) {
847 if (CFEqual(attr
->name
, name
)) {
848 return SecDbItemSetValue(item
, attr
, value
, error
);
854 bool SecDbItemSetAccessControl(SecDbItemRef item
, SecAccessControlRef access_control
, CFErrorRef
*error
) {
856 if (item
->_edataState
== kSecDbItemClean
)
857 ok
= SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbEncryptedDataAttr
, error
), kCFNull
, error
);
858 if (ok
&& access_control
) { //added check for access_control because ks_decrypt_data can leave NULL in access_control in case of error
859 item
->_edataState
= kSecDbItemDirty
;
860 CFDataRef data
= SecAccessControlCopyData(access_control
);
861 ok
= SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbAccessControlAttr
, error
), data
, error
);
867 SecDbItemRef
SecDbItemCreateWithAttributes(CFAllocatorRef allocator
, const SecDbClass
*class, CFDictionaryRef attributes
, keybag_handle_t keybag
, CFErrorRef
*error
) {
868 SecDbItemRef item
= SecDbItemCreate(kCFAllocatorDefault
, class, keybag
);
869 if (item
&& !SecDbItemSetValues(item
, attributes
, error
))
875 SecDbColumnCopyValueWithAttr(CFAllocatorRef allocator
, sqlite3_stmt
*stmt
, const SecDbAttr
*attr
, int col
, CFErrorRef
*error
) {
876 CFTypeRef value
= NULL
;
877 switch (attr
->kind
) {
879 case kSecDbCreationDateAttr
:
880 case kSecDbModificationDateAttr
:
881 value
= SecDbColumnCopyDate(allocator
, stmt
, col
, error
);
884 case kSecDbNumberAttr
:
885 switch (sqlite3_column_type(stmt
, col
)) {
887 value
= SecDbColumnCopyNumber(allocator
, stmt
, col
, error
);
890 value
= SecDbColumnCopyDouble(allocator
, stmt
, col
, error
);
893 value
= SecDbColumnCopyString(allocator
, stmt
, col
, error
,
897 value
= SecDbColumnCopyData(allocator
, stmt
, col
, error
);
904 case kSecDbAccessAttr
:
905 case kSecDbStringAttr
:
906 value
= SecDbColumnCopyString(allocator
, stmt
, col
, error
,
912 case kSecDbPrimaryKeyAttr
:
913 case kSecDbEncryptedDataAttr
:
914 value
= SecDbColumnCopyData(allocator
, stmt
, col
, error
);
918 value
= SecDbColumnCopyNumber(allocator
, stmt
, col
, error
);
920 case kSecDbRowIdAttr
:
921 value
= SecDbColumnCopyNumber64(allocator
, stmt
, col
, error
);
923 case kSecDbAccessControlAttr
:
924 case kSecDbUTombAttr
:
925 /* This attributes does not have any database column associated, exists only inside encrypted blob as metadata. */
931 SecDbItemRef
SecDbItemCreateWithStatement(CFAllocatorRef allocator
, const SecDbClass
*class, sqlite3_stmt
*stmt
, keybag_handle_t keybag
, CFErrorRef
*error
, bool (^return_attr
)(const SecDbAttr
*attr
)) {
932 SecDbItemRef item
= SecDbItemCreate(allocator
, class, keybag
);
934 SecDbForEachAttr(class, attr
) {
935 if (return_attr(attr
)) {
936 CFTypeRef value
= SecDbColumnCopyValueWithAttr(allocator
, stmt
, attr
, col
++, error
);
937 require_action_quiet(value
, errOut
, CFReleaseNull(item
));
939 CFDictionarySetValue(item
->attributes
, SecDbAttrGetHashName(attr
), value
);
943 const SecDbAttr
*data_attr
= SecDbClassAttrWithKind(class, kSecDbEncryptedDataAttr
, NULL
);
944 if (data_attr
!= NULL
&& CFDictionaryGetValue(item
->attributes
, data_attr
->name
) != NULL
) {
945 item
->_edataState
= kSecDbItemEncrypted
;
953 SecDbItemRef
SecDbItemCreateWithEncryptedData(CFAllocatorRef allocator
, const SecDbClass
*class,
954 CFDataRef edata
, keybag_handle_t keybag
, CFErrorRef
*error
) {
955 SecDbItemRef item
= SecDbItemCreate(allocator
, class, keybag
);
956 const SecDbAttr
*edata_attr
= SecDbClassAttrWithKind(class, kSecDbEncryptedDataAttr
, error
);
958 if (!SecDbItemSetValue(item
, edata_attr
, edata
, error
))
964 // TODO: Hack -- Replace with real filtering
966 // Return true iff an item for which SecDbItemIsSyncable() already returns true should be part of the v2 view.
967 bool SecDbItemInV2(SecDbItemRef item
) {
968 const SecDbClass
*iclass
= SecDbItemGetClass(item
);
969 return (SecDbItemGetCachedValueWithName(item
, kSecAttrSyncViewHint
) == NULL
&&
970 (iclass
== &genp_class
|| iclass
== &inet_class
|| iclass
== &keys_class
|| iclass
== &cert_class
));
973 // Return true iff an item for which SecDbItemIsSyncable() and SecDbItemInV2() already return true should be part of the v0 view.
974 bool SecDbItemInV2AlsoInV0(SecDbItemRef item
) {
975 return (SecDbItemGetCachedValueWithName(item
, kSecAttrTokenID
) == NULL
&& SecDbItemGetClass(item
) != &cert_class
);
978 SecDbItemRef
SecDbItemCopyWithUpdates(SecDbItemRef item
, CFDictionaryRef updates
, CFErrorRef
*error
) {
979 SecDbItemRef new_item
= SecDbItemCreate(CFGetAllocator(item
), item
->class, item
->keybag
);
980 SecDbItemSetCredHandle(new_item
, item
->credHandle
);
981 SecDbForEachAttr(item
->class, attr
) {
982 // Copy each attribute, except the mod date attribute (it will be reset to now when needed),
983 // from the updates dict unless it's not there in which case we copy the attribute from the passed in item.
984 if (attr
->kind
!= kSecDbModificationDateAttr
&& attr
->kind
!= kSecDbEncryptedDataAttr
&& attr
->kind
!= kSecDbSHA1Attr
&& attr
->kind
!= kSecDbPrimaryKeyAttr
) {
985 CFTypeRef value
= NULL
;
986 if (CFDictionaryGetValueIfPresent(updates
, attr
->name
, &value
)) {
988 SecError(errSecParam
, error
, CFSTR("NULL value in dictionary"));
990 value
= SecDbItemGetValue(item
, attr
, error
);
992 if (!value
|| !SecDbItemSetValue(new_item
, attr
, value
, error
)) {
993 CFReleaseNull(new_item
);
1001 // Ensure that the date value of attr of new_item is greater than that of old_item.
1002 static bool SecDbItemMakeAttrYounger(SecDbItemRef new_item
, SecDbItemRef old_item
, const SecDbAttr
*attr
, CFErrorRef
*error
) {
1003 CFDateRef old_date
= SecDbItemGetValue(old_item
, attr
, error
);
1006 CFDateRef new_date
= SecDbItemGetValue(new_item
, attr
, error
);
1010 if (CFDateCompare(new_date
, old_date
, NULL
) != kCFCompareGreaterThan
) {
1011 CFDateRef adjusted_date
= CFDateCreate(kCFAllocatorDefault
, CFDateGetAbsoluteTime(old_date
) + 0.001);
1012 if (adjusted_date
) {
1013 ok
= SecDbItemSetValue(new_item
, attr
, adjusted_date
, error
);
1014 CFRelease(adjusted_date
);
1020 // Ensure that the mod date of new_item is greater than that of old_item.
1021 static bool SecDbItemMakeYounger(SecDbItemRef new_item
, SecDbItemRef old_item
, CFErrorRef
*error
) {
1022 const SecDbAttr
*attr
= SecDbClassAttrWithKind(new_item
->class, kSecDbModificationDateAttr
, error
);
1023 return attr
&& SecDbItemMakeAttrYounger(new_item
, old_item
, attr
, error
);
1026 static SecDbItemRef
SecDbItemCopyTombstone(SecDbItemRef item
, CFBooleanRef makeTombStone
, CFErrorRef
*error
) {
1027 SecDbItemRef new_item
= SecDbItemCreate(CFGetAllocator(item
), item
->class, item
->keybag
);
1028 SecDbForEachAttr(item
->class, attr
) {
1029 if (attr
->kind
== kSecDbTombAttr
) {
1030 // Set the tomb attr to true to indicate a tombstone.
1031 if (!SecDbItemSetValue(new_item
, attr
, kCFBooleanTrue
, error
)) {
1032 CFReleaseNull(new_item
);
1035 } else if (SecDbIsTombstoneDbUpdateAttr(attr
)) {
1036 // Copy all primary key attributes and creation timestamps from the original item.
1037 CFTypeRef value
= SecDbItemGetValue(item
, attr
, error
);
1038 if (!value
|| (!CFEqual(kCFNull
, value
) && !SecDbItemSetValue(new_item
, attr
, value
, error
))) {
1039 CFReleaseNull(new_item
);
1042 } else if (attr
->kind
== kSecDbModificationDateAttr
) {
1043 if (!SecDbItemMakeAttrYounger(new_item
, item
, attr
, error
)) {
1044 CFReleaseNull(new_item
);
1047 } else if (makeTombStone
&& attr
->kind
== kSecDbUTombAttr
) {
1049 SecDbItemSetValue(new_item
, attr
, makeTombStone
, error
);
1057 // MARK: SQL Construction helpers -- These should become private in the future
1059 void SecDbAppendElement(CFMutableStringRef sql
, CFStringRef value
, bool *needComma
) {
1062 CFStringAppend(sql
, CFSTR(","));
1066 CFStringAppend(sql
, value
);
1069 static void SecDbAppendElementEquals(CFMutableStringRef sql
, CFStringRef value
, bool *needComma
) {
1070 SecDbAppendElement(sql
, value
, needComma
);
1071 CFStringAppend(sql
, CFSTR("=?"));
1074 /* Append AND is needWhere is NULL or *needWhere is false. Append WHERE
1075 otherwise. Upon return *needWhere will be false. */
1077 SecDbAppendWhereOrAnd(CFMutableStringRef sql
, bool *needWhere
) {
1078 if (!needWhere
|| !*needWhere
) {
1079 CFStringAppend(sql
, CFSTR(" AND "));
1081 CFStringAppend(sql
, CFSTR(" WHERE "));
1087 SecDbAppendWhereOrAndEquals(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
) {
1088 SecDbAppendWhereOrAnd(sql
, needWhere
);
1089 CFStringAppend(sql
, col
);
1090 CFStringAppend(sql
, CFSTR("=?"));
1094 SecDbAppendWhereOrAndNotEquals(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
) {
1095 SecDbAppendWhereOrAnd(sql
, needWhere
);
1096 CFStringAppend(sql
, col
);
1097 CFStringAppend(sql
, CFSTR("!=?"));
1100 static void SecDbAppendCountArgsAndCloseParen(CFMutableStringRef sql
, CFIndex count
) {
1101 bool needComma
= false;
1103 SecDbAppendElement(sql
, CFSTR("?"), &needComma
);
1104 CFStringAppend(sql
, CFSTR(")"));
1108 SecDbAppendWhereOrAndIn(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
, CFIndex count
) {
1110 return SecDbAppendWhereOrAndEquals(sql
, col
, needWhere
);
1111 SecDbAppendWhereOrAnd(sql
, needWhere
);
1112 CFStringAppend(sql
, col
);
1113 CFStringAppend(sql
, CFSTR(" IN ("));
1114 SecDbAppendCountArgsAndCloseParen(sql
, count
);
1118 SecDbAppendWhereOrAndNotIn(CFMutableStringRef sql
, CFStringRef col
, bool *needWhere
, CFIndex count
) {
1120 return SecDbAppendWhereOrAndNotEquals(sql
, col
, needWhere
);
1121 SecDbAppendWhereOrAnd(sql
, needWhere
);
1122 CFStringAppend(sql
, col
);
1123 CFStringAppend(sql
, CFSTR(" NOT IN ("));
1124 SecDbAppendCountArgsAndCloseParen(sql
, count
);
1127 static CFStringRef
SecDbItemCopyInsertSQL(SecDbItemRef item
, bool(^use_attr
)(const SecDbAttr
*attr
)) {
1128 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(item
), 0);
1129 CFStringAppend(sql
, CFSTR("INSERT INTO "));
1130 CFStringAppend(sql
, item
->class->name
);
1131 CFStringAppend(sql
, CFSTR("("));
1132 bool needComma
= false;
1133 CFIndex used_attr
= 0;
1134 SecDbForEachAttr(item
->class, attr
) {
1135 if (use_attr(attr
)) {
1137 SecDbAppendElement(sql
, attr
->name
, &needComma
);
1140 CFStringAppend(sql
, CFSTR(")VALUES(?"));
1141 while (used_attr
-- > 1) {
1142 CFStringAppend(sql
, CFSTR(",?"));
1144 CFStringAppend(sql
, CFSTR(")"));
1149 static bool SecDbItemInsertBind(SecDbItemRef item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr
)(const SecDbAttr
*attr
)) {
1152 SecDbForEachAttr(item
->class, attr
) {
1153 if (use_attr(attr
)) {
1154 CFTypeRef value
= SecDbItemCopyValueForDb(item
, attr
, error
);
1155 ok
= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1156 CFReleaseSafe(value
);
1164 sqlite3_int64
SecDbItemGetRowId(SecDbItemRef item
, CFErrorRef
*error
) {
1165 sqlite3_int64 row_id
= 0;
1166 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1168 CFNumberRef number
= SecDbItemGetValue(item
, attr
, error
);
1169 if (!isNumber(number
)|| !CFNumberGetValue(number
, kCFNumberSInt64Type
, &row_id
))
1170 SecDbError(SQLITE_ERROR
, error
, CFSTR("rowid %@ is not a 64 bit number"), number
);
1176 static CFNumberRef
SecDbItemCreateRowId(SecDbItemRef item
, sqlite3_int64 rowid
, CFErrorRef
*error
) {
1177 return CFNumberCreate(CFGetAllocator(item
), kCFNumberSInt64Type
, &rowid
);
1180 bool SecDbItemSetRowId(SecDbItemRef item
, sqlite3_int64 rowid
, CFErrorRef
*error
) {
1182 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1184 CFNumberRef value
= SecDbItemCreateRowId(item
, rowid
, error
);
1188 ok
= SecDbItemSetValue(item
, attr
, value
, error
);
1194 bool SecDbItemClearRowId(SecDbItemRef item
, CFErrorRef
*error
) {
1196 const SecDbAttr
*attr
= SecDbClassAttrWithKind(item
->class, kSecDbRowIdAttr
, error
);
1198 CFDictionaryRemoveValue(item
->attributes
, attr
->name
);
1199 //ok = SecDbItemSetValue(item, attr, kCFNull, error);
1204 static bool SecDbItemSetLastInsertRowId(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1205 sqlite3_int64 rowid
= sqlite3_last_insert_rowid(SecDbHandle(dbconn
));
1206 return SecDbItemSetRowId(item
, rowid
, error
);
1209 bool SecDbItemIsSyncableOrCorrupted(SecDbItemRef item
) {
1210 bool is_syncable_or_corrupted
= false;
1211 CFErrorRef localError
= NULL
;
1212 if (!SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, &localError
),
1213 &is_syncable_or_corrupted
, &localError
)) {
1214 is_syncable_or_corrupted
= SecErrorGetOSStatus(localError
) == errSecDecode
;
1216 CFReleaseSafe(localError
);
1217 return is_syncable_or_corrupted
;
1220 bool SecDbItemIsSyncable(SecDbItemRef item
) {
1222 if (SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, NULL
), &is_syncable
, NULL
))
1227 bool SecDbItemSetSyncable(SecDbItemRef item
, bool sync
, CFErrorRef
*error
)
1229 return SecDbItemSetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSyncAttr
, error
), sync
? kCFBooleanTrue
: kCFBooleanFalse
, error
);
1232 bool SecDbItemIsTombstone(SecDbItemRef item
) {
1234 if (SecDbItemGetBoolValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbTombAttr
, NULL
), &is_tomb
, NULL
))
1239 CFDataRef
SecDbItemGetPrimaryKey(SecDbItemRef item
, CFErrorRef
*error
) {
1240 return SecDbItemGetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbPrimaryKeyAttr
, error
), error
);
1243 CFDataRef
SecDbItemGetSHA1(SecDbItemRef item
, CFErrorRef
*error
) {
1244 return SecDbItemGetValue(item
, SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, error
), error
);
1247 static SecDbQueryRef
SecDbQueryCreateWithItemPrimaryKey(SecDbItemRef item
, CFErrorRef
*error
) {
1248 CFMutableDictionaryRef dict
= SecDbItemCopyPListWithMask(item
, kSecDbPrimaryKeyFlag
, error
);
1252 SecDbQueryRef query
= query_create(item
->class, NULL
, NULL
, error
);
1254 CFReleaseSafe(query
->q_item
);
1255 query
->q_item
= dict
;
1263 static bool SecDbItemIsCorrupt(SecDbItemRef item
, bool *is_corrupt
, CFErrorRef
*error
) {
1264 CFErrorRef localError
= NULL
;
1265 // Cache the storedSHA1 digest so we use the one from the db not the recomputed one for notifications.
1266 const struct SecDbAttr
*sha1attr
= SecDbClassAttrWithKind(item
->class, kSecDbSHA1Attr
, &localError
);
1267 CFDataRef storedSHA1
= CFRetainSafe(SecDbItemGetValue(item
, sha1attr
, &localError
));
1270 if (localError
|| !SecDbItemEnsureDecrypted(item
, &localError
)) {
1271 if (SecErrorGetOSStatus(localError
) == errSecDecode
) {
1272 // We failed to decrypt the item
1273 const SecDbAttr
*desc
= SecDbClassAttrWithKind(item
->class, kSecDbAccessControlAttr
, &localError
);
1274 SecAccessControlRef accc
= NULL
;
1275 CFDataRef acccData
= NULL
;
1277 acccData
= (CFDataRef
)SecDbItemGetValue(item
, desc
, &localError
);
1278 if (isData(acccData
)) {
1279 accc
= SecAccessControlCreateFromData(CFGetAllocator(item
), acccData
, &localError
);
1282 if (accc
&& CFEqualSafe(SecAccessControlGetProtection(accc
), kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
)) {
1284 secwarning("cannot decrypt item %@, item is irrecoverably lost with older passcode (error %@)", item
, localError
);
1286 secerror("error %@ reading item %@ (corrupted)", localError
, item
);
1287 __security_simulatecrash(CFSTR("Corrupted item found in keychain"), __sec_exception_code_CorruptItem
);
1289 CFReleaseNull(localError
);
1294 // Recompute sha1 hash attribute and compare with the cached one.
1295 CFDataRef computedSHA1
= SecDbItemCopyValue(item
, sha1attr
, &localError
);
1296 if (storedSHA1
&& computedSHA1
&& !CFEqual(storedSHA1
, computedSHA1
)) {
1297 CFStringRef storedHex
= CFDataCopyHexString(storedSHA1
), computedHex
= CFDataCopyHexString(computedSHA1
);
1298 secerror("error %@ %@ != %@ item %@ (corrupted)", sha1attr
->name
, storedHex
, computedHex
, item
);
1299 __security_simulatecrash(CFSTR("Corrupted item (sha1 mismatch) found in keychain"), __sec_exception_code_CorruptItem
);
1300 CFReleaseSafe(storedHex
);
1301 CFReleaseSafe(computedHex
);
1305 // Sanity check that all attributes that must not be NULL actually aren't
1306 if (!localError
) SecDbForEachAttr(item
->class, attr
) {
1307 if (attr
->flags
& (kSecDbInCryptoDataFlag
| kSecDbInAuthenticatedDataFlag
)) {
1308 CFTypeRef value
= SecDbItemGetValue(item
, attr
, &localError
);
1310 if (CFEqual(kCFNull
, value
) && attr
->flags
& kSecDbNotNullFlag
) {
1311 secerror("error attribute %@ has NULL value in item %@ (corrupted)", attr
->name
, item
);
1312 __security_simulatecrash(CFSTR("Corrupted item (attr NULL) found in keychain"), __sec_exception_code_CorruptItem
);
1317 if (SecErrorGetOSStatus(localError
) == errSecDecode
) {
1318 // We failed to decrypt the item
1320 secwarning("attribute %@: %@ item %@ (item lost with older passcode)", attr
->name
, localError
, item
);
1322 secerror("error attribute %@: %@ item %@ (corrupted)", attr
->name
, localError
, item
);
1323 __security_simulatecrash(CFSTR("Corrupted item found in keychain"), __sec_exception_code_CorruptItem
);
1326 CFReleaseNull(localError
);
1333 CFReleaseSafe(computedSHA1
);
1334 CFReleaseSafe(storedSHA1
);
1335 return SecErrorPropagate(localError
, error
);
1338 static void SecDbItemRecordUpdate(SecDbConnectionRef dbconn
, SecDbItemRef deleted
, SecDbItemRef inserted
) {
1339 SecDbRecordChange(dbconn
, deleted
, inserted
);
1342 static bool SecDbItemDoInsert(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1343 bool (^use_attr
)(const SecDbAttr
*attr
) = ^bool(const SecDbAttr
*attr
) {
1344 return (attr
->flags
& kSecDbInFlag
);
1346 CFStringRef sql
= SecDbItemCopyInsertSQL(item
, use_attr
);
1347 __block
bool ok
= sql
;
1349 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1350 ok
= (SecDbItemInsertBind(item
, stmt
, error
, use_attr
) &&
1351 SecDbStep(dbconn
, stmt
, error
, NULL
) &&
1352 SecDbItemSetLastInsertRowId(item
, dbconn
, error
));
1357 secnotice("item", "inserted %@", item
);
1358 SecDbItemRecordUpdate(dbconn
, NULL
, item
);
1360 secnotice("item", "insert failed for item %@ with %@", item
, error
? *error
: NULL
);
1366 bool SecDbItemInsertOrReplace(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, void(^duplicate
)(SecDbItemRef item
, SecDbItemRef
*replace
)) {
1367 __block CFErrorRef localError
= NULL
;
1368 __block
bool ok
= SecDbItemDoInsert(item
, dbconn
, &localError
);
1369 if (!ok
&& localError
&& CFErrorGetCode(localError
) == SQLITE_CONSTRAINT
&& CFEqual(kSecDbErrorDomain
, CFErrorGetDomain(localError
))) {
1370 SecDbQueryRef query
= SecDbQueryCreateWithItemPrimaryKey(item
, error
);
1372 CFRetainAssign(query
->q_use_cred_handle
, item
->credHandle
);
1373 SecDbItemSelect(query
, dbconn
, error
, NULL
, ^bool(const SecDbAttr
*attr
) {
1374 return attr
->flags
& kSecDbPrimaryKeyFlag
;
1375 }, NULL
, NULL
, ^(SecDbItemRef old_item
, bool *stop
) {
1376 bool is_corrupt
= false;
1377 ok
= SecDbItemIsCorrupt(old_item
, &is_corrupt
, error
);
1378 SecDbItemRef replace
= NULL
;
1380 // If old_item is corrupted pretend it's not there and just replace it.
1384 CFReleaseNull(*error
); //item is corrupted and will be replaced, so drop the error
1385 } else if (ok
&& duplicate
) {
1386 duplicate(old_item
, &replace
);
1389 const SecDbAttr
*rowid_attr
= SecDbClassAttrWithKind(old_item
->class, kSecDbRowIdAttr
, error
);
1390 CFNumberRef oldrowid
= SecDbItemGetCachedValue(old_item
, rowid_attr
);
1392 ok
= SecDbItemSetValue(replace
, rowid_attr
, oldrowid
, &localError
);
1393 if (ok
&& !is_corrupt
) {
1394 ok
= SecDbItemMakeYounger(replace
, old_item
, error
);
1396 ok
= ok
&& SecDbItemDoUpdate(old_item
, replace
, dbconn
, &localError
, ^bool (const SecDbAttr
*attr
) {
1397 return attr
->kind
== kSecDbRowIdAttr
;
1400 ok
= SecError(errSecInternal
, &localError
, CFSTR("no rowid for %@"), old_item
);
1404 CFReleaseNull(localError
); // Clear the error, since we replaced the item.
1407 SecDbItemSetCredHandle(item
, query
->q_use_cred_handle
);
1408 ok
&= query_destroy(query
, error
);
1412 return ok
& SecErrorPropagate(localError
, error
); // Don't use && here!
1415 bool SecDbItemInsert(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1416 return SecDbItemInsertOrReplace(item
, dbconn
, error
, ^(SecDbItemRef old_item
, SecDbItemRef
*replace
) {
1417 if (SecDbItemIsTombstone(old_item
)) {
1424 static CFStringRef
SecDbItemCopyUpdateSQL(SecDbItemRef old_item
, SecDbItemRef new_item
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1425 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(new_item
), 0);
1426 CFStringAppend(sql
, CFSTR("UPDATE "));
1427 CFStringAppend(sql
, new_item
->class->name
);
1428 CFStringAppend(sql
, CFSTR(" SET "));
1429 bool needComma
= false;
1430 CFIndex used_attr
= 0;
1431 SecDbForEachAttrWithMask(new_item
->class, attr
, kSecDbInFlag
) {
1433 SecDbAppendElementEquals(sql
, attr
->name
, &needComma
);
1436 bool needWhere
= true;
1437 SecDbForEachAttr(old_item
->class, attr
) {
1438 if (use_attr_in_where(attr
)) {
1439 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1446 static bool SecDbItemUpdateBind(SecDbItemRef old_item
, SecDbItemRef new_item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1449 SecDbForEachAttrWithMask(new_item
->class, attr
, kSecDbInFlag
) {
1450 CFTypeRef value
= SecDbItemCopyValueForDb(new_item
, attr
, error
);
1451 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1452 CFReleaseSafe(value
);
1456 SecDbForEachAttr(old_item
->class, attr
) {
1457 if (use_attr_in_where(attr
)) {
1458 CFTypeRef value
= SecDbItemCopyValueForDb(old_item
, attr
, error
);
1459 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1460 CFReleaseSafe(value
);
1468 // Primary keys are the same -- do an update
1469 bool SecDbItemDoUpdate(SecDbItemRef old_item
, SecDbItemRef new_item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1470 CFStringRef sql
= SecDbItemCopyUpdateSQL(old_item
, new_item
, use_attr_in_where
);
1471 __block
bool ok
= sql
;
1473 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1474 ok
= SecDbItemUpdateBind(old_item
, new_item
, stmt
, error
, use_attr_in_where
) && SecDbStep(dbconn
, stmt
, error
, NULL
);
1479 secnotice("item", "replaced %@ with %@ in %@", old_item
, new_item
, dbconn
);
1480 SecDbItemRecordUpdate(dbconn
, old_item
, new_item
);
1485 static CFStringRef
SecDbItemCopyDeleteSQL(SecDbItemRef item
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1486 CFMutableStringRef sql
= CFStringCreateMutable(CFGetAllocator(item
), 0);
1487 CFStringAppend(sql
, CFSTR("DELETE FROM "));
1488 CFStringAppend(sql
, item
->class->name
);
1489 bool needWhere
= true;
1490 SecDbForEachAttr(item
->class, attr
) {
1491 if (use_attr_in_where(attr
)) {
1492 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1499 static bool SecDbItemDeleteBind(SecDbItemRef item
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1502 SecDbForEachAttr(item
->class, attr
) {
1503 if (use_attr_in_where(attr
)) {
1504 CFTypeRef value
= SecDbItemCopyValueForDb(item
, attr
, error
);
1505 ok
&= value
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1506 CFReleaseSafe(value
);
1514 static bool SecDbItemDoDeleteOnly(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1515 CFStringRef sql
= SecDbItemCopyDeleteSQL(item
, use_attr_in_where
);
1516 __block
bool ok
= sql
;
1518 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1519 ok
= SecDbItemDeleteBind(item
, stmt
, error
, use_attr_in_where
) && SecDbStep(dbconn
, stmt
, error
, NULL
);
1526 bool SecDbItemDoDeleteSilently(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1527 return SecDbItemDoDeleteOnly(item
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1528 return attr
->kind
== kSecDbRowIdAttr
;
1532 static bool SecDbItemDoDelete(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
, bool (^use_attr_in_where
)(const SecDbAttr
*attr
)) {
1533 bool ok
= SecDbItemDoDeleteOnly(item
, dbconn
, error
, use_attr_in_where
);
1535 secnotice("item", "deleted %@ from %@", item
, dbconn
);
1536 SecDbItemRecordUpdate(dbconn
, item
, NULL
);
1542 static bool SecDbItemDeleteTombstone(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
1544 // TODO: Treat non decryptable items like tombstones here too and delete them
1545 SecDbItemRef tombstone
= SecDbItemCopyTombstone(item
, error
);
1548 ok
= SecDbItemClearRowId(tombstone
, error
);
1550 ok
= SecDbItemDoDelete(tombstone
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1551 return SecDbIsTombstoneDbSelectAttr(attr
);
1554 CFRelease(tombstone
);
1560 // Replace old_item with new_item. If primary keys are the same this does an update otherwise it does a delete + add
1561 bool SecDbItemUpdate(SecDbItemRef old_item
, SecDbItemRef new_item
, SecDbConnectionRef dbconn
, CFBooleanRef makeTombstone
, CFErrorRef
*error
) {
1562 __block
bool ok
= true;
1563 __block CFErrorRef localError
= NULL
;
1565 CFDataRef old_pk
= SecDbItemGetPrimaryKey(old_item
, error
);
1566 CFDataRef new_pk
= SecDbItemGetPrimaryKey(new_item
, error
);
1568 ok
= old_pk
&& new_pk
;
1570 bool pk_equal
= ok
&& CFEqual(old_pk
, new_pk
);
1572 ok
= SecDbItemMakeYounger(new_item
, old_item
, error
);
1574 ok
= ok
&& SecDbItemDoUpdate(old_item
, new_item
, dbconn
, &localError
, ^bool(const SecDbAttr
*attr
) {
1575 return attr
->kind
== kSecDbRowIdAttr
;
1579 if(CFErrorGetCode(localError
) == SQLITE_CONSTRAINT
&& CFEqual(kSecDbErrorDomain
, CFErrorGetDomain(localError
))) {
1580 /* Update failed because we changed the PrimaryKey and there was a dup.
1581 Find the dup and see if it is a tombstone or corrupted item. */
1582 SecDbQueryRef query
= SecDbQueryCreateWithItemPrimaryKey(new_item
, error
);
1585 ok
&= SecDbItemSelect(query
, dbconn
, error
, NULL
, ^bool(const SecDbAttr
*attr
) {
1586 return attr
->flags
& kSecDbPrimaryKeyFlag
;
1587 }, NULL
, NULL
, ^(SecDbItemRef duplicate_item
, bool *stop
) {
1588 bool is_corrupt
= false;
1589 bool is_tomb
= false;
1590 ok
= SecDbItemIsCorrupt(duplicate_item
, &is_corrupt
, error
);
1591 if (ok
&& !is_corrupt
) {
1592 if ((is_tomb
= SecDbItemIsTombstone(duplicate_item
)))
1593 ok
= SecDbItemMakeYounger(new_item
, duplicate_item
, error
);
1595 if (ok
&& (is_corrupt
|| is_tomb
)) {
1596 ok
= SecDbItemDoDelete(old_item
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1597 return attr
->kind
== kSecDbRowIdAttr
;
1599 ok
= ok
&& SecDbItemDoUpdate(duplicate_item
, new_item
, dbconn
, error
, ^bool (const SecDbAttr
*attr
) {
1600 return attr
->kind
== kSecDbRowIdAttr
;
1602 CFReleaseNull(localError
);
1605 ok
&= query_destroy(query
, error
);
1611 if (error
&& *error
== NULL
) {
1612 *error
= localError
;
1615 CFReleaseSafe(localError
);
1619 if (ok
&& !pk_equal
&& !CFEqualSafe(makeTombstone
, kCFBooleanFalse
)) {
1620 /* The primary key of new_item is different than that of old_item, we
1621 have been asked to make a tombstone so leave one for the old_item. */
1622 SecDbItemRef tombstone
= SecDbItemCopyTombstone(old_item
, makeTombstone
, error
);
1625 ok
= (SecDbItemClearRowId(tombstone
, error
) &&
1626 SecDbItemDoInsert(tombstone
, dbconn
, error
));
1627 CFRelease(tombstone
);
1634 // Replace the object with a tombstone
1635 bool SecDbItemDelete(SecDbItemRef item
, SecDbConnectionRef dbconn
, CFBooleanRef makeTombstone
, CFErrorRef
*error
) {
1637 if (!CFEqualSafe(makeTombstone
, kCFBooleanFalse
)) {
1638 SecDbItemRef tombstone
= SecDbItemCopyTombstone(item
, makeTombstone
, error
);
1640 ok
= SecDbItemDoUpdate(item
, tombstone
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1641 return attr
->kind
== kSecDbRowIdAttr
;
1643 CFRelease(tombstone
);
1646 ok
= SecDbItemDoDelete(item
, dbconn
, error
, ^bool(const SecDbAttr
*attr
) {
1647 return attr
->kind
== kSecDbRowIdAttr
;
1653 CFStringRef
SecDbItemCopySelectSQL(SecDbQueryRef query
,
1654 bool (^return_attr
)(const SecDbAttr
*attr
),
1655 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1656 bool (^add_where_sql
)(CFMutableStringRef sql
, bool *needWhere
)) {
1657 CFMutableStringRef sql
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
1658 CFStringAppend(sql
, CFSTR("SELECT "));
1659 // What are we selecting?
1660 bool needComma
= false;
1661 SecDbForEachAttr(query
->q_class
, attr
) {
1662 if (return_attr(attr
))
1663 SecDbAppendElement(sql
, attr
->name
, &needComma
);
1666 // From which table?
1667 CFStringAppend(sql
, CFSTR(" FROM "));
1668 CFStringAppend(sql
, query
->q_class
->name
);
1670 // And which elements do we want to select
1671 bool needWhere
= true;
1672 SecDbForEachAttr(query
->q_class
, attr
) {
1673 if (use_attr_in_where(attr
)) {
1674 CFTypeRef value
= CFDictionaryGetValue(query
->q_item
, attr
->name
);
1675 if (isArray(value
)) {
1676 CFArrayRef array
= (CFArrayRef
)value
;
1677 CFIndex length
= CFArrayGetCount(array
);
1679 CFTypeRef head
= CFArrayGetValueAtIndex(array
, 0);
1680 if (CFEqualSafe(head
, kCFNull
)) {
1681 SecDbAppendWhereOrAndNotIn(sql
, attr
->name
, &needWhere
, length
- 1);
1683 SecDbAppendWhereOrAndIn(sql
, attr
->name
, &needWhere
, length
);
1687 SecDbAppendWhereOrAndEquals(sql
, attr
->name
, &needWhere
);
1691 // Append SQL for access groups and limits.
1693 add_where_sql(sql
, &needWhere
);
1698 static bool SecDbItemSelectBindValue(SecDbQueryRef query
, sqlite3_stmt
*stmt
, int param
, const SecDbAttr
*attr
, CFTypeRef inValue
, CFErrorRef
*error
) {
1700 CFTypeRef value
= NULL
;
1701 if (attr
->kind
== kSecDbRowIdAttr
) {
1702 // TODO: Ignores inValue and uses rowid directly instead HACK should go
1703 value
= CFNumberCreate(NULL
, kCFNumberSInt64Type
, &query
->q_row_id
);
1705 value
= SecDbAttrCopyValueForDb(attr
, inValue
, error
);
1707 ok
= ok
&& value
!= NULL
&& SecDbBindObject(stmt
, ++param
, value
, error
);
1708 CFReleaseSafe(value
);
1712 bool SecDbItemSelectBind(SecDbQueryRef query
, sqlite3_stmt
*stmt
, CFErrorRef
*error
,
1713 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1714 bool (^bind_added_where
)(sqlite3_stmt
*stmt
, int col
)) {
1715 __block
bool ok
= true;
1716 __block
int param
= 0;
1717 SecDbForEachAttr(query
->q_class
, attr
) {
1718 if (use_attr_in_where(attr
)) {
1719 CFTypeRef value
= CFDictionaryGetValue(query
->q_item
, attr
->name
);
1720 if (isArray(value
)) {
1721 CFArrayRef array
= (CFArrayRef
)value
;
1722 CFRange range
= {.location
= 0, .length
= CFArrayGetCount(array
) };
1723 if (range
.length
> 0) {
1724 CFTypeRef head
= CFArrayGetValueAtIndex(array
, 0);
1725 if (CFEqualSafe(head
, kCFNull
)) {
1730 CFArrayApplyFunction(array
, range
, apply_block_1
, (void (^)(const void *value
)) ^(const void *value
) {
1731 ok
= SecDbItemSelectBindValue(query
, stmt
, param
++, attr
, value
, error
);
1734 ok
= SecDbItemSelectBindValue(query
, stmt
, param
++, attr
, value
, error
);
1741 // TODO: Bind arguments for access groups and limits.
1742 if (bind_added_where
)
1743 bind_added_where(stmt
, ++param
);
1748 bool SecDbItemSelect(SecDbQueryRef query
, SecDbConnectionRef dbconn
, CFErrorRef
*error
,
1749 bool (^return_attr
)(const SecDbAttr
*attr
),
1750 bool (^use_attr_in_where
)(const SecDbAttr
*attr
),
1751 bool (^add_where_sql
)(CFMutableStringRef sql
, bool *needWhere
),
1752 bool (^bind_added_where
)(sqlite3_stmt
*stmt
, int col
),
1753 void (^handle_row
)(SecDbItemRef item
, bool *stop
)) {
1754 __block
bool ok
= true;
1755 if (return_attr
== NULL
) {
1756 return_attr
= ^bool (const SecDbAttr
* attr
) {
1757 return attr
->kind
== kSecDbRowIdAttr
|| attr
->kind
== kSecDbEncryptedDataAttr
|| attr
->kind
== kSecDbSHA1Attr
;
1761 CFStringRef sql
= SecDbItemCopySelectSQL(query
, return_attr
, use_attr_in_where
, add_where_sql
);
1763 ok
&= SecDbPrepare(dbconn
, sql
, error
, ^(sqlite3_stmt
*stmt
) {
1764 ok
= (SecDbItemSelectBind(query
, stmt
, error
, use_attr_in_where
, bind_added_where
) &&
1765 SecDbStep(dbconn
, stmt
, error
, ^(bool *stop
) {
1766 SecDbItemRef item
= SecDbItemCreateWithStatement(kCFAllocatorDefault
, query
->q_class
, stmt
, query
->q_keybag
, error
, return_attr
);
1768 CFRetainAssign(item
->credHandle
, query
->q_use_cred_handle
);
1769 handle_row(item
, stop
);