0

I have a Safenet 5110 dongle. I am using MacOS ventura 13.2.1 (with Swift 5.7 and Xcode 14.2). I have downloaded the driver to authenticate with the dongle (Safenet Authentication Token - SAC). There, I have found libeTPkcs11.dylib which can be used to communicate with their dongle.

Here is my code:

import Foundation

let pkcs11LibPath = "/usr/local/lib/libeTPkcs11.dylib"
let pkcs11LibHandle = dlopen(pkcs11LibPath, RTLD_NOW)

if pkcs11LibHandle == nil {
    let errorMsg = String(cString: dlerror())
    print("Failed to load PKCS#11 library: \(errorMsg)")
    exit(1)
}

// Get a pointer to the C_GetInfo function
let cGetInfoPtr = dlsym(pkcs11LibHandle, "C_GetInfo")
typealias C_GetInfo_Type = @convention(c) (UnsafeMutableRawPointer?, UnsafeMutablePointer<CK_INFO>?) -> CK_RV
let cGetInfo = unsafeBitCast(cGetInfoPtr, to: C_GetInfo_Type.self)

// Call the C_GetInfo function to get information about the token
var info = CK_INFO()
let rv = cGetInfo(nil, &info)

if rv != CKR_OK {
    print("Failed to get token info: \(rv)")
    exit(1)
}

print("Manufacturer ID: \(String(cString: &info.manufacturerID))")
print("Model: \(String(cString: &info.model))")
print("Serial number: \(String(cString: &info.serialNumber))")

I am getting the following errors when I try to compile the code:

  • Cannot find type 'CK_RV' in scope
  • Cannot find type 'CK_INFO' in scope

I think that, these come from the pkcs11.h header files from OASIS specifications.

I have created a bridging header caclient-Bridging-Header.h. There I have included the file pkcs11.h. But I think xcode is recognizing the header file as a objective-c header (not c99 header).

XCode errors

desertSniper87
  • 795
  • 2
  • 13
  • 25

1 Answers1

1

Objective-C is a superset of C. It has no trouble importing C headers (because C headers are ObjC headers). The problem is the CK_PTR macro is defined in a header not included in your bridging header. You're probably supposed to import some other "umbrella" header instead of pkcs11.h, or some platform-configuration header before pkcs11.h. Without knowing exactly what library you're using, and the documentation for it, it's difficult to know how this particular code expects to be used.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Since it's impossible to give a precise answer to the question, why not ask for extra details or even close the question with "needs debugging details" resolution? – The Dreams Wind May 15 '23 at 03:45
  • Because this answer is very likely sufficient. Without seeing the code, I don't know the name of the header to import, but the answer is "import the header that defines `CK_PTR`." I don't believe this should be closed or the OP needs to edit the question if that's enough. – Rob Napier May 15 '23 at 13:20