将C头文件添加到Swift项目以与Safenet 5110进行PKCS11接口交互

huangapple go评论75阅读模式
英文:

Add C header files to Swift project for PKCS11 interfacing with Safenet 5110

问题

Here is the translated code portion you requested:

我有一个Safenet 5110加密狗。我正在使用MacOS Ventura 13.2.1(带有Swift 5.7Xcode 14.2)。我已下载了与加密狗进行身份验证的驱动程序(Safenet Authentication Token - SAC)。在那里,我找到了可以用于与他们的加密狗通信的`libeTPkcs11.dylib`

这是我的代码:

```swift
import Foundation

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

if pkcs11LibHandle == nil {
    let errorMsg = String(cString: dlerror())
    print("无法加载PKCS#11库: \(errorMsg)")
    exit(1)
}

// 获取C_GetInfo函数的指针
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)

// 调用C_GetInfo函数获取关于令牌的信息
var info = CK_INFO()
let rv = cGetInfo(nil, &info)

if rv != CKR_OK {
    print("无法获取令牌信息: \(rv)")
    exit(1)
}

print("制造商ID: \(String(cString: &info.manufacturerID))")
print("型号: \(String(cString: &info.model))")
print("序列号: \(String(cString: &info.serialNumber))")

当我尝试编译代码时,我收到以下错误消息:

  • 无法在作用域中找到类型'CK_RV'
  • 无法在作用域中找到类型'CK_INFO'

我认为这些错误来自于OASIS规范的pkcs11.h头文件。

我已经创建了一个桥接头文件caclient-Bridging-Header.h。在那里,我包含了pkcs11.h文件。但我认为Xcode将该头文件识别为Objective-C头文件(而不是C99头文件)。


Please note that the translated code is provided as per your request, and if you have any specific questions or issues related to it, feel free to ask.

<details>
<summary>英文:</summary>

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 &#39;CK_RV&#39; in scope
- Cannot find type &#39;CK_INFO&#39; in scope

I think that, these come from the [pkcs11.h](http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html) 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][1]][1]


  [1]: https://i.stack.imgur.com/9CczC.png

</details>


# 答案1
**得分**: 1

[Objective-C is a superset of C.] 它没有问题导入 C 头文件(因为 C 头文件**就是** ObjC 头文件)。 问题是 [`CK_PTR` 宏](http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html#_Toc416959684) 是在一个未包含在你的桥接头文件中的头文件中定义的。 你可能应该导入一些其他的“总头”文件,而不是 `pkcs11.h`,或者在 `pkcs11.h` 之前导入一些平台配置头文件。 没有确切了解你使用的库以及其文档,很难知道这段代码期望如何使用。

<details>
<summary>英文:</summary>

[Objective-C is a superset of C.](https://stackoverflow.com/questions/19366134/what-does-objective-c-is-a-superset-of-c-more-strictly-than-c-mean-exactly) It has no trouble importing C headers (because C headers **are** ObjC headers). The problem is the [`CK_PTR` macro](http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html#_Toc416959684) is defined in a header not included in your bridging header. You&#39;re probably supposed to import some other &quot;umbrella&quot; header instead of `pkcs11.h`, or some platform-configuration header before `pkcs11.h`. Without knowing exactly what library you&#39;re using, and the documentation for it, it&#39;s difficult to know how this particular code expects to be used.

</details>



huangapple
  • 本文由 发表于 2023年5月14日 16:53:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定