JoeyLaBarck
JoeyLaBarck

Reputation: 56

updating kSecValueData in iOS using secItemUpdate Error Code -50

I am at a loss here, I create a keychain query, add the item if it does not already exist, then I try and update kSecValueData with a test string and it returns error code -50, which means there was an error with one or more parameters I entered...

NSString *initial = @"";
NSData *initData = [initial dataUsingEncoding:NSUTF8StringEncoding];

//Create Search Dictionary For Phone Number...
NSDictionary *secPhoneItem = @{ (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
                                (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,
                                (__bridge id)kSecValueData : initData
                                };

//Check to see if keychain already exists by using secItemCopyMatching and associated status code
OSStatus PhoneCheckStatus = SecItemCopyMatching((__bridge CFDictionaryRef)secPhoneItem, NULL);

//Check Status Code Phone
if (PhoneCheckStatus == errSecItemNotFound) //If Phone Keychain Item Does Not already Exist
{
    //Add Phone Number To Keychain
    SecItemAdd((__bridge CFDictionaryRef)secPhoneItem, NULL);




}

//Update Phone Number to String
NSString *string = @"Test String";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *attributesForUpdate = @{
                                      (__bridge id)kSecValueData : data
                                      };


OSStatus news = SecItemUpdate((__bridge CFDictionaryRef)secPhoneItem, (__bridge CFDictionaryRef)attributesForUpdate);
NSLog(@"Update Status Code: %ld", news);

If anyone knows why or can shed some info, the only lead I have right now from Apples Documentation is that you can only pass real attributes into secItemUpdate(), not "meta" attributes.

Upvotes: 1

Views: 2848

Answers (2)

Priyanka Sindha
Priyanka Sindha

Reputation: 1

Key and certificate Attribute Update-----------

for the normal SecItemUpdate code gave two error:-25300(item not found),-50(trying to update more than one item in one update method) here is the code for update method: //Search Dictionary.....

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];

NSData *privateKeyTag = [NSData dataWithBytes:[keyIdentifier UTF8String]   length:keyIdentifier.length];

[searchDict setObject:privateKeyTag forKey:(__bridge id)kSecAttrApplicationTag];
[searchDict setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[searchDict setObject:(__bridge id)(kSecAttrKeyTypeRSA) forKey:(__bridge id<NSCopying>)(kSecAttrKeyType)];
[searchDict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
[searchDict setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id<NSCopying>)(kSecReturnData)];

status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDict, (CFTypeRef*)&item);

if (status != errSecSuccess)
{//your code for error handling }


 //dictionary for the attribute that are going to update in key of certificate
  //if youwant to update your passward the add the passward attribute

  NSDictionary *dict = [NSDictionary    dictionaryWithObjectsAndKeys:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,kSecAttrAccessible, nil];

    /*removing the some of the from the searching dictionary*/
    [searchDict removeObjectForKey:(__bridge id)kSecReturnData];
    [searchDict removeObjectForKey:(__bridge id)kSecMatchLimit];
//Creating the Array for every item in the keychain that is cert of key as Search Dictionary
    NSArray *secItemclasses=    @[(__bridge id)kSecClassKey,(__bridge id)kSecClassCertificate];

    for (id secitemclass in secItemclasses) {
        //updating the key as well as certificate attribute....//

    status = SecItemUpdate((__bridge CFDictionaryRef)searchDict,(__bridge CFDictionaryRef)dict);

    }


    if(status != errSecSuccess)

Upvotes: 0

JoeyLaBarck
JoeyLaBarck

Reputation: 56

So after rereading the documentation, I found out that the key-value pair (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue cannot be used in the secItemUpdate()' query parameter. To fix my problem and help better refine the search, I added the key-value pair(__bridge id)kSecAttrDescription : someUniqueData` to the search query along with the class item specification then making my attributes dictionary returned status 0: SUCCESS!!!

Upvotes: 1

Related Questions