Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while linking sample program with opensc-pkcs11 #227

Closed
anjann opened this issue Mar 26, 2014 · 7 comments
Closed

Error while linking sample program with opensc-pkcs11 #227

anjann opened this issue Mar 26, 2014 · 7 comments

Comments

@anjann
Copy link

anjann commented Mar 26, 2014

I have written a sample program in C++ by using libopensc-pkcs11.so library in Red Hat linux. My program takes a string as input and encrypts the input string and decrypts the encrypted string. My program is compiled successfully but error is occuring while linking and the symbols 'C_DecryptInit','C_Decrypt', 'C_EncryptInit','C_Encrypt' are not found.

Following is the error log during linking:

CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::decrypt(unsigned char const*)': CAESHandlerImplSolaris.cpp:(.text+0x1c6): undefined reference toC_DecryptInit' CAESHandlerImplSolaris.cpp:(.text+0x25c): undefined reference to C_Decrypt' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::encrypt(unsigned char const*)': CAESHandlerImplSolaris.cpp:(.text+0x41b): undefined reference to C_EncryptInit' CAESHandlerImplSolaris.cpp:(.text+0x4ba): undefined reference toC_Encrypt' CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::initializeSession()': CAESHandlerImplSolaris.cpp:(.text+0x8dd): undefined reference toC_Initialize' CAESHandlerImplSolaris.cpp:(.text+0x95c): undefined reference to C_GetSlotList' CAESHandlerImplSolaris.cpp:(.text+0x998): undefined reference toC_GetSlotList' CAESHandlerImplSolaris.cpp:(.text+0xa7c): undefined reference to C_OpenSession' CAESHandlerImplSolaris.cpp:(.text+0xaef): undefined reference toC_GetMechanismInfo' CAESHandlerImplSolaris.cpp:(.text+0xb81): undefined reference to C_CreateObject' CAESHandlerImplSolaris.cpp:(.text+0xbfa): undefined reference toC_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0xc0a): undefined reference to C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()': CAESHandlerImplSolaris.cpp:(.text+0xd69): undefined reference to C_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0xdb0): undefined reference toC_CloseSession' CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::~CAESHandlerImplSolaris()': CAESHandlerImplSolaris.cpp:(.text+0xeb1): undefined reference toC_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0xef8): undefined reference to C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()': CAESHandlerImplSolaris.cpp:(.text+0xff9): undefined reference to C_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0x1040): undefined reference toC_CloseSession' main.o: In function main': main.cpp:(.text+0x69d): undefined reference toC_Finalize' main.cpp:(.text+0x758): undefined reference to `C_Finalize' collect2: ld returned 1 exit status

I have provided the following libraries during linking : opensc-pkcs11.so, pkcs11-spy.so, libopensc.so.

Please suggest me if I am missing anything.

Thanks in advance AnjanN

@martinpaljak
Copy link
Member

You don't link against PKCS#11, you use it through dlopen()

@dengert
Copy link
Member

dengert commented Mar 26, 2014

In addition to what Martin said, If you want to use other PKCS#11
implementation, you need to follow the PKCS#11 conventions.
A module is loaded using dlopen,  and the C_GetFunctionList entry
point is found, and use to return the C_FunctionList.
Depending on how the module was linked, it may not have any entry
points. 
It looks like you are using Solaris.  I used to build OpenSC for
Solaris 10 and before running the configure  I would set these:
PTHREAD_CFLAGS="-pthreads"
CPPFLAGS="$CPPFLAGS  -D_TS_ERRNO"
Then after config, I would do this:
                ltversion=`./libtool --version | grep  ltmain.sh |
cut -d" " -f 4`
                echo patching libtool  $ltversion for Solaris
                patch -b < ../other/libtool.diff.$ltversion
libtool.diff.2.2.6b (also for 2.2.6)
--- ,libtool    Mon Jun  8 16:04:31 2009
+++ libtool    Mon Jun  8 16:13:01 2009
@@ -314,7 +314,7 @@
 
 # Commands used to build a loadable module if different from
building
 # a shared archive.
-module_cmds=""
+module_cmds="$archive_cmds \${wl}-B \${wl}direct \${wl}-z
\${wl}defs"
 module_expsym_cmds=""
 
 # Whether we are building with GNU ld or not.
The intent is a program can load one or more PKCS#11 modules and
keep track of them via the C_FunctionList.
Mozilla's NSS does this.  Pkcs11-spy  is another example , it loads
a PKCS#11 module so it can trace it. 
 The linker needs to assure that the module is self contained, so
references to functions in the module by the module
are resolved in the module.  With Solairs the -B direct and -z do
this. 
 
 On 3/26/2014 12:29 AM, anjann wrote:

  I have written a sample program in C++ by using
    libopensc-pkcs11.so library in Red Hat linux. My program takes a
    string as input and encrypts the input string and decrypts the
    encrypted string. My program is compiled successfully but error
    is occuring while linking and the symbols
    'C_DecryptInit','C_Decrypt', 'C_EncryptInit','C_Encrypt' are not
    found.
  Following is the error log during linking:
  CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::decrypt(unsigned
      char const*)': CAESHandlerImplSolaris.cpp:(.text+0x1c6):
      undefined reference toC_DecryptInit'
    CAESHandlerImplSolaris.cpp:(.text+0x25c): undefined reference to
    C_Decrypt' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::encrypt(unsigned
    char const*)': CAESHandlerImplSolaris.cpp:(.text+0x41b):
    undefined reference to C_EncryptInit'
      CAESHandlerImplSolaris.cpp:(.text+0x4ba): undefined reference
      toC_Encrypt' CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::initializeSession()':
      CAESHandlerImplSolaris.cpp:(.text+0x8dd): undefined reference
      toC_Initialize'
    CAESHandlerImplSolaris.cpp:(.text+0x95c): undefined reference to
    C_GetSlotList' CAESHandlerImplSolaris.cpp:(.text+0x998):
      undefined reference toC_GetSlotList'
    CAESHandlerImplSolaris.cpp:(.text+0xa7c): undefined reference to
    C_OpenSession' CAESHandlerImplSolaris.cpp :(.text+0xaef):
      undefined reference toC_GetMechanismInfo'
    CAESHandlerImplSolaris.cpp:(.text+0xb81): undefined reference to
    C_CreateObject' CAESHandlerImplSolaris.cpp:(.text+0xbfa):
      undefined reference toC_DestroyObject'
    CAESHandlerImplSolaris.cpp:(.text+0xc0a): undefined reference to
    C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
    CAESHandlerImplSolaris.cpp:(.text+0xd69): undefined reference to
    C_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0xdb0):
      undefined reference toC_CloseSession'
    CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
      CAESHandlerImplSolaris.cpp:(.text+0xeb1): undefined reference
      toC_DestroyObject'
    CAESHandlerImplSolaris.cpp:(.text+0xef8): undefined reference to
    C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
    CAESHandlerImplSolaris.cpp:(.tex t+0xff9): undefined reference
    to C_DestroyObject'
      CAESHandlerImplSolaris.cpp:(.text+0x1040): undefined reference
      toC_CloseSession' main.o: In function main':
      main.cpp:(.text+0x69d): undefined reference toC_Finalize'
    main.cpp:(.text+0x758): undefined reference to `C_Finalize'
    collect2: ld returned 1 exit status
  I have provided the following libraries during linking :
    opensc-pkcs11.so, pkcs11-spy.so, libopensc.so.
  Please suggest me if I am missing anything.
  Thanks in advance AnjanN
  —
    Reply to this email directly or view it on
      GitHub.

-- 

Douglas E. Engert [email protected]

@dengert
Copy link
Member

dengert commented Mar 26, 2014

P.S.
You mighty find the p11_load.c  in libp11  helpful.  It sound like a
lot of what you are trying to do in in libp11 repository at Github.On 3/26/2014 12:29 AM, anjann wrote:

  I have written a sample program in C++ by using
    libopensc-pkcs11.so library in Red Hat linux. My program takes a
    string as input and encrypts the input string and decrypts the
    encrypted string. My program is compiled successfully but error
    is occuring while linking and the symbols
    'C_DecryptInit','C_Decrypt', 'C_EncryptInit','C_Encrypt' are not
    found.
  Following is the error log during linking:
  CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::decrypt(unsigned
      char const*)': CAESHandlerImplSolaris.cpp:(.text+0x1c6):
      undefined reference toC_DecryptInit'
    CAESHandlerImplSolaris.cpp:(.text+0x25c): undefined reference to
    C_Decrypt' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::encrypt(unsigned
    char const*)': CAESHandlerImplSolaris.cpp:(.text+0x41b):
    undefined reference to C_EncryptInit'
      CAESHandlerImplSolaris.cpp:(.text+0x4ba): undefined reference
      toC_Encrypt' CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::initializeSession()':
      CAESHandlerImplSolaris.cpp:(.text+0x8dd): undefined reference
      toC_Initialize'
    CAESHandlerImplSolaris.cpp:(.text+0x95c): undefined reference to
    C_GetSlotList' CAESHandlerImplSolaris.cpp:(.text+0x998):
      undefined reference toC_GetSlotList'
    CAESHandlerImplSolaris.cpp:(.text+0xa7c): undefined reference to
    C_OpenSession' CAESHandlerImplSolaris.cpp :(.text+0xaef):
      undefined reference toC_GetMechanismInfo'
    CAESHandlerImplSolaris.cpp:(.text+0xb81): undefined reference to
    C_CreateObject' CAESHandlerImplSolaris.cpp:(.text+0xbfa):
      undefined reference toC_DestroyObject'
    CAESHandlerImplSolaris.cpp:(.text+0xc0a): undefined reference to
    C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
    CAESHandlerImplSolaris.cpp:(.text+0xd69): undefined reference to
    C_DestroyObject' CAESHandlerImplSolaris.cpp:(.text+0xdb0):
      undefined reference toC_CloseSession'
    CAESHandlerImplSolaris.o: In function CAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
      CAESHandlerImplSolaris.cpp:(.text+0xeb1): undefined reference
      toC_DestroyObject'
    CAESHandlerImplSolaris.cpp:(.text+0xef8): undefined reference to
    C_CloseSession' CAESHandlerImplSolaris.o: In functionCAESHandlerImplSolaris::~CAESHandlerImplSolaris()':
    CAESHandlerImplSolaris.cpp:(.tex t+0xff9): undefined reference
    to C_DestroyObject'
      CAESHandlerImplSolaris.cpp:(.text+0x1040): undefined reference
      toC_CloseSession' main.o: In function main':
      main.cpp:(.text+0x69d): undefined reference toC_Finalize'
    main.cpp:(.text+0x758): undefined reference to `C_Finalize'
    collect2: ld returned 1 exit status
  I have provided the following libraries during linking :
    opensc-pkcs11.so, pkcs11-spy.so, libopensc.so.
  Please suggest me if I am missing anything.
  Thanks in advance AnjanN
  —
    Reply to this email directly or view it on
      GitHub.

-- 

Douglas E. Engert [email protected]

@LudovicRousseau
Copy link
Member

OpenSC may/should also support direct linking of the PKCS#11 lib instead of using it with dlopen(3).

But this is no more possible since the C_* symbols (except C_GetFunctionList) are no more exported (since 5a23069)

@martinpaljak
Copy link
Member

I think the answer here IMHO is "may". While I don't recall precisely the whole motivation for the change, 99% of applications making use of PKCS#11 work like this these days (by using dynamic loading and C_GetFunctionList) and for the sake of interoperability (being able to swap providers or underlying hardware, or use several at the same time) this is preferred anyway. While PKCS#11 v2.20 (dated, 10 years old) does talk about some possible security drawbacks of shared libraries, the overall weight and complexity of OpenSC does IMHO not warrant for the possible security benefits of static linking as outlined in the doc.

If someone paranoid enough wants to use OpenSC in a static way and a possibly constrained environment, that would anyway mean cutting out drivers not needed etc. Or embedding OpenSC source code directly.

While for correctness the commit could be reverted, I think it should be carefully considered if encouraging such development practices that don't allow to replace the underlying vendor easily would be beneficial.

@LudovicRousseau
Copy link
Member

I do not speak about static linking but dynamic linking, like for any .so library.

You do not need to dlopen("libc.so.6") before you use printf().

In general the PKCS#11 library name is configurable so the application have to use dlopen(3).

@martinpaljak
Copy link
Member

My ambiguous wording. But the point is the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants