Unable to compile a c application that reads smartcard

好久不见. 提交于 2019-12-13 07:52:30

问题


I am trying to compile an example c application that is using pkcs#11 to finds all the private keys on the token, and print their label and id, but getting following errors

/tmp/ccAqQ7UI.o: In function initialize':
pkcs11_example1.c:(.text+0x8e5): undefined reference to C_Initialize'
/tmp/ccAqQ7UI.o: In function `get_slot':

The example is taken from here

compilling by using following command;

 `gcc pkcs11_example1.c -o slots -L /usr/lib/opensc-pkcs11.so`

I am not sure which library i should link after -L. Can anyone guide how to compile this and are there some libraries required to link.


回答1:


C_Initialize and other 60+ functions with "C_" prefix are cryptoki functions defined in PKCS#11 specification. They are usually implemented in standalone library provided by HSM vendor. Looking at your code samples I would say that you need to directly link also PKCS#11 library or you can modify the code to dynamically load PKCS#11 library in runtime with LoadLibrary or dlopen and then acquire pointers to all cryptoki functions via the C_GetFunctionList call. You can also take a look at pkcs11-logger the source code for an example on how to do that.




回答2:


The link command you give, gcc pkcs11_example1.c -o slots -L /usr/lib/opensc-pkcs11.so, is wrong.

  • -L takes just path, which is added to paths where libs are searched from, but /usr/lib is default so you don't need this switch at all.

  • You are missing -l, which takes the library name without lib prefix or .so suffix, so looks like you need -lopensc-pkcs11.

So, first make sure your library file really is /usr/lib/libopensc-pkcs11.so (note lib prefix!) possibly with verion numbers following. Then change build options so link command becomes

gcc pkcs11_example1.c -o slots -lopensc-pkcs11


来源:https://stackoverflow.com/questions/24133704/unable-to-compile-a-c-application-that-reads-smartcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!