3

I'm in the proces of making an app for interacting with smartcards. For that I'd like to use the CryptoTokenKit Framework which is standard on a Mac (located at /System/Library/Frameworks/CryptoTokenKit.framework).

This link says that it's possible to bind frameworks in a Mac project: https://developer.xamarin.com/guides/cross-platform/macios/native-references/

I've created an ApiDefinition.cs file and a StrucsAndEnums.cs file using the following sharpie command: sharpie bind -framework ./CryptoTokenKit.framework -sdk macosx10.13 -o ~/CryptoTokenKitBinding

I can't find any info on the internet how to implement the above mentioned files and start using the framework.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165

1 Answers1

4

Create a Xamarin.Mac binding project within a solution.

Add a NativeReference to:

/System/Library/Frameworks/CryptoTokenKit.framework

Bind it using sharpie:

sharpie bind \
    -o CryptoTokenKitFramework \
    -namespace CryptoTokenKit \
    -sdk macosx10.13 \
    -f /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CryptoTokenKit.framework

There is a mismatch between the binding project template and the output of sharpie, so you can either delete ApiDefinition.cs and add ApiDefinitions.cs or just overwrite the template created one:

mv CryptoTokenKitFramework/ApiDefinitions.cs CryptoTokenKitFramework/ApiDefinition.cs

There will be a number of attributes like (versions will change across them):

[Watch (4,0), TV (11,0), Mac (10,12), iOS (10,0)]

As these are private frameworks on iOS, Watch, TV, so strip those platforms out. Leave the Mac attribute and the original version:

[Mac (10,11)]

Note: PlatformAttribute is obsolete but sharpie is still using it (assuming backwards version compatibility(?)), so you can use Introduced if you really want to clean up the build output:

[Introduced (PlatformName.MacOSX, 10, 11, PlatformArchitecture.Arch64)]

There will be a few [Verify] attributes that have to be reviewed/corrected. i.e. TKSmartCardUserInteraction.Cancel and TKSmartCardSlot.MakeSmartCard should both be methods not properties.

Example / Generated:

// -(TKSmartCard * _Nullable)makeSmartCard;
[NullAllowed, Export ("makeSmartCard")]
[Verify (MethodToProperty)]
TKSmartCard MakeSmartCard { get; }

Corrected:

// -(TKSmartCard * _Nullable)makeSmartCard;
[NullAllowed, Export("makeSmartCard")]
TKSmartCard MakeSmartCard();

Fix the rest of the [Verify] attributes and compiler errors, there are a bunch of bad method signatures, pointers, return types, etc.. that are generated and need corrected.

Note: TO make your life easier, make sure that you are using the latest Sharpie version:

Version:  3.4.0
SHA1:     c12859dac8d43121b5a9ed866a0db8409f9df817
URL:      https://dl.xamarin.com/objective-sharpie/ObjectiveSharpie-3.4.0.pkg
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks for answering my question @SushiHangover. Is there a reason why you use the framework in Xcode with Sharpie but add the system framework as a reference? – Koen Hendriks Oct 26 '17 at 07:03
  • I get the error, when I want to compile: /Users/Koen/Developer/poc_digid/CryptoTokenKitFramework/CryptoTokenKitFramework/BTOUCH: Error BI1017: bgen: Do not know how to make a signature for Security.SecCertificate in parameter `certificateRef' from CryptoTokenKitFramework.TKTokenKeychainCertificate.Constructor (BI1017) (CryptoTokenKitFramework) – Koen Hendriks Oct 26 '17 at 11:15
  • In the following piece of code: https://gist.github.com/anonymous/0d1aad180c111b726be1ec14dddd564e – Koen Hendriks Oct 26 '17 at 11:17
  • @KoenHendriks The framework provided by the OS is what everyone has installed, but the Xcode version has the headers needed by sharpie. – SushiHangover Oct 26 '17 at 14:23