Skip to content
Kevin Leong edited this page Aug 14, 2017 · 1 revision

iOS Keychain

Keychain macros

$security error -50
Error: 0xFFFFFFCE -50 One or more parameters passed to a function were not valid.
  • SecItemAdd
    NSDictionary *keychainData = @{
        (id)kSecAttrService: [NSBundle mainBundle].bundleIdentifier,
              (id)kSecClass: (id)kSecClassGenericPassword,
        (id)kSecAttrAccount:@"bar",
          (id)kSecValueData:[@"foo" dataUsingEncoding:NSUTF8StringEncoding]};

    // Manually add the entry above to the keychain
    OSStatus addResult = SecItemAdd((__bridge CFDictionaryRef)keychainData, NULL);
    NSLog("@%", addResult);
  • SecItemCopyMatching
    NSDictionary *query = @{
                            (id)kSecAttrService:[NSBundle mainBundle].bundleIdentifier,
                            (id)kSecReturnData:(id)kCFBooleanTrue,
                            (id)kSecReturnAttributes:(id)kCFBooleanTrue,
                            (id)kSecMatchLimit:(id)kSecMatchLimitAll,
                            (id)kSecClass:(id)kSecClassGenericPassword
                            };

    CFTypeRef dataTypeRef = NULL;

    // Retrieving items from the keychain
    OSStatus queryResult = SecItemCopyMatching((__bridge CFDictionaryRef)query, &dataTypeRef);
    NSLog("@%", queryResult);
  • SecItemDelete
    NSDictionary *deleteQuery = @{
                            (id)kSecAttrService:[NSBundle mainBundle].bundleIdentifier,
                            (id)kSecReturnAttributes:(id)kCFBooleanTrue,
                            (id)kSecClass:(id)kSecClassGenericPassword
                            };

    // Delete items from the keychain
    unused OSStatus deleteResult = SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
    NSLog("@%", deleteResult);
  • SecItemUpdate

Keychain Error Codes: OSStatus

Protip: enter security error <error code> in Terminal to display an explanation of the OSStatus code:

Error codes are defined in an enum at the bottom of SecBase.h

/***********************************************
 *** OSStatus values unique to Security APIs ***
 ***********************************************/

/*
    Note: the comments that appear after these errors are used to create
    SecErrorMessages.strings. The comments must not be multi-line, and
    should be in a form meaningful to an end user. If a different or
    additional comment is needed, it can be put in the header doc format,
    or on a line that does not start with errZZZ.
*/

enum
{
    errSecSuccess                               = 0,       /* No error. */
    errSecUnimplemented                         = -4,      /* Function or operation not implemented. */
    errSecIO                                    = -36,     /*I/O error (bummers)*/
    errSecOpWr                                  = -49,     /*file already open with with write permission*/
    errSecParam                                 = -50,     /* One or more parameters passed to a function where not valid. */
    errSecAllocate                              = -108,    /* Failed to allocate memory. */
    errSecUserCanceled                          = -128,    /* User canceled the operation. */
    errSecBadReq                                = -909,    /* Bad parameter or invalid state for operation. */
    errSecInternalComponent                     = -2070,
    errSecNotAvailable                          = -25291,  /* No keychain is available. You may need to restart your computer. */
    errSecDuplicateItem                         = -25299,  /* The specified item already exists in the keychain. */
    errSecItemNotFound                          = -25300,  /* The specified item could not be found in the keychain. */
    errSecInteractionNotAllowed                 = -25308,  /* User interaction is not allowed. */
    errSecDecode                                = -26275,  /* Unable to decode the provided data. */
    errSecAuthFailed                            = -25293,  /* The user name or passphrase you entered is not correct. */
};
Clone this wiki locally