0

I'm pulling my hair out trying to implement what I assumed would be extremely basic: storing key-value pairs in the keychain in a Delphi iOS app. (In the Maui world it's trivial using SecureStorage, butI've found no equivalent wrapper in Delphi. If anyone kknows any, please let me know!)

I've gone through the Apple docs, as well as whatever examples I could find online, but I'm stuck with an kSecParam error when storing.

Here's some of the code I've tried so far:

function SaveToKeychain(const Key, Value: string) : Boolean;
var
  query, attributes: NSMutableDictionary;
  status: OSStatus;
  keyData, valueData: NSData;
  Data: CFDataRef;

  valueBytes : TBytes;
  testStr : String;
begin
  Result := False;

  try


    query := TNSMutableDictionary.Create;
    try
      valueBytes := TEncoding.UTF8.GetBytes(Value);
      //valueData := TNSData.Wrap(TNSData.alloc.initWithBytesNoCopy(@Value[1], Length(Value)));
      //Data := CFDataCreate(nil, @Value[1], Length(Value) * SizeOf(Char));
      try
        // Define the keychain item attributes
        query.setValue((kSecClassGenericPassword as ILocalObject).GetObjectID, kSecClass);
        query.setValue((StrToNSStr(key) as ILocalObject).GetObjectID, kSecAttrAccount);
        //query.setValue(Data, StrToNSStr('kSecValueData'));
        query.setValue((StrToNSStr(Value) as ILocalObject).GetObjectID, StrToNSStr('kSecValueData'));

        // Add the item to the keychain
        status := SecItemAdd((query as ILocalObject).GetObjectID, nil);
      finally
        CFRelease(Data);
      end;

      AddToLog(Format('SaveToKeychain status: %d', [status]));

      Result := (status = errSecSuccess);
    finally
      query.Release;
    end;


  except
    on E:Exception do
      AddToLog('Error in SaveToKeychain: ' + E.Message);

  end;
end;

The SecItemAdd call returns a kSecParam error, which means one or more of the parameters/attributes is incorrect. I've assumed that it was the data, so I've tried several ways of adding it with no luck.

What am I missing??

4
  • You're calling setValue when it should be setObject Commented Aug 2 at 21:12
  • @DaveNottage Thank you, but that doesn't make a difference, I still get the same error value. I'm really confused by this... Commented Aug 5 at 15:13
  • I also noticed you're passing a reference to an NSString for the kSecValueData parameter - it should be a reference to an NSData, like this: query.setObject(NSObjectToID(StrToNSStr(Value).dataUsingEncoding(NSUTF8StringEncoding)), StrToNSStr('kSecValueData')); Commented Aug 5 at 23:21
  • Thanks @DaveNottage, but still the same result. I have tried several ways of encapsulating the Value in an NSData, including my last attempt before your most recent comment valueBytes := TEncoding.UTF8.GetBytes(Value); valueData := TNSData.Wrap(TNSData.alloc.initWithBytesNoCopy(@valueBytes[0], Length(valueBytes))); I wasn't sure either if you meant that each setValue should be replaced with setObject, but I've tried every combination I can think of, and all return the -50 kSecParam error Commented Aug 21 at 3:20

1 Answer 1

1

My comment on the question about kSecValueData was not quite correct, as I'd missed that the second parameter is expecting an object ID. Also, this is not sufficient:

StrToNSStr('kSecValueData')

as the constant may not actually equal that value. This code should work:

uses
  Macapi.CoreFoundation, Macapi.Helpers, Macapi.ObjectiveC,
  iOSapi.Foundation, iOSapi.Security;

function kSecValueData: NSString;
begin
  Result := CocoaNSStringConst(libSecurity, 'kSecValueData');
end;

function SaveToKeychain(const Key, Value: string) : Boolean;
var
  query: NSMutableDictionary;
  status: OSStatus;
begin
  Result := False;
  try
    query := TNSMutableDictionary.Create;
    query.setObject(NSObjectToID(kSecClassGenericPassword), NSObjectToID(kSecClass));
    query.setObject(StringToID(key), NSObjectToID(kSecAttrAccount));
    query.setObject(NSObjectToID(StrToNSStr(Value).dataUsingEncoding(NSUTF8StringEncoding)), NSObjectToID(kSecValueData));
    status := SecItemAdd(NSObjectToID(query), nil);
    Log.d('SaveToKeychain status: %d', [status]);
    Result := (status = errSecSuccess);
  except
    on E:Exception do
      Log.d('Error in SaveToKeychain: ' + E.Message);
  end;
end;
1
  • Thank you! Indeed, the issue was with kSecValueData. Passing ... , NSObjectToID(StrToNSStr('kSecValueData')) ... fails with the -50 error Commented Aug 26 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.