Bug 58720 - SecKeychain.QueryAsData returns invalid data type
Summary: SecKeychain.QueryAsData returns invalid data type
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: master
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-08-11 14:48 UTC by github
Modified: 2017-10-12 07:22 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description github 2017-08-11 14:48:08 UTC
When specifying wantPersistentReference=true in the following method, the underlying native return type is actually NSDictionary, not NSData.

public static NSData QueryAsData (SecRecord query, bool wantPersistentReference, out SecStatusCode status)

The reason for this is that when wantPersistentReference=true, multiple return types are specified (i.e. SecItem.ReturnData & SecItem.ReturnPersistentRef as seen on https://github.com/xamarin/xamarin-macios/blob/master/src/Security/Items.cs). According to Apple's docs, when multiple return types are requested, a dictionary containing each type is returned.

I suppose it should read more like:

if (wantPersistentReference) {
    copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnPersistentRef);
} else {
    copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnData);

Comment 1 Vincent Dondain [MSFT] 2017-08-11 19:10:49 UTC

I understand what you're suggesting however in practice (quick test) that doesn't seem to be a problem. I do get an NSData object even when wantPersistentReference=true.

Do you have a test case of your own that shows some kind of issue when using `QueryAsData` with `wantPersistentReference=true`?

Some stack trace maybe?

Also could you please link to the Apple doc you're referring to?

Thanks (:
Comment 3 github 2017-08-11 19:58:11 UTC
Thanks for the response. Yes that's the doc I was referring to.

As an example if I try to access the NSData.Bytes property I get the following stack trace:

Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSCFDictionary bytes]: unrecognized selector sent to instance 0x61000047ef00
Native stack trace:
	0   CoreFoundation                      0x0000000119b37b0b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000011a266141 objc_exception_throw + 48
	2   CoreFoundation                      0x0000000119ba7134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x0000000119abe840 ___forwarding___ + 1024
	4   CoreFoundation                      0x0000000119abe3b8 _CF_forwarding_prep_0 + 120
	5   SecureSuite.Native.IOS              0x000000010e3e7da9 xamarin_dyn_objc_msgSend + 217
	6   ???                                 0x00000001324464f6 0x0 + 5138310390

If I print the NSObject.Description property of the returned object I get the following:

    acct = "kchain-password";
    agrp = "<removed>";
    cdat = "2017-08-11 19:44:54 +0000";
    mdat = "2017-08-11 19:44:54 +0000";
    musr = <>;
    pdmn = ak;
    svce = "<removed>";
    sync = 0;
    tomb = 0;
    "v_Data" = <70686432 79676637 657a>;
    "v_PersistentRef" = <67656e70 00000000 0000001c>;

Where the "v_PersistentRef" value matches the expected NSData object.
Comment 4 Vincent Dondain [MSFT] 2017-08-14 05:24:02 UTC
Attempt at fixing this issue in the following PR: https://github.com/xamarin/xamarin-macios/pull/2485
Comment 5 Rolf Bjarne Kvinge [MSFT] 2017-10-12 07:22:30 UTC
The PR was merged: https://github.com/xamarin/xamarin-macios/commit/a628b5c799a6759bb3a2dd794e7cf94e0f09bd5e