Coder Social home page Coder Social logo

tpm2-software / tpm2-pkcs11 Goto Github PK

View Code? Open in Web Editor NEW
257.0 27.0 105.0 3.18 MB

A PKCS#11 interface for TPM2 hardware

Home Page: https://tpm2-software.github.io

License: Other

Makefile 0.31% Shell 5.93% M4 1.40% C 82.17% Python 9.87% Java 0.32%
tpm tpm2 signing crypto cryptography pkcs11 security

tpm2-pkcs11's People

Contributors

aaronuniontech avatar alvinchchen avatar arvedarved avatar bb-froggy avatar diabonas avatar dilyanpalauzov avatar dkobras avatar dnoliver avatar idesai avatar irtimmer avatar jakuje avatar joholl avatar joshuagl avatar ldts avatar maage avatar malikabhi05 avatar mdempsky avatar nickbedbury avatar niooss-ledger avatar peterhuewe avatar petervagany avatar robbycornelissen avatar shoracek avatar szlin avatar tstellar avatar tstruk avatar tynsh avatar vjardin avatar vtcao297 avatar wxleong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tpm2-pkcs11's Issues

update types in interfaces

Some functions are taking things like unsigned char *, unsigned long when they should be using CK_BYTE_PTR and CK_ULONG respectively. Update to the proper CK_ types.

stored login session_ctx unwieldy

Because ESAPI requires the context to be the same to use TR_ handles, having per-session ESAPI contexts requires us to stash the login session_ctx * in token to use when unloading things. Just share ESAPI (ie tpm) context as a token level idiom.

twist.c is not automatically recompiled when enabling unit tests

Currently, if unit tests are not enabled during the initial configure run, but activated later, src/lib/twist.c is not recompiled automatically by make or make check. This causes the unit tests to fail because the necessary #ifdef UNIT_TESTING block is not included in the compiled file. For example, a sequence like

git clone https://github.com/tpm2-software/tpm2-pkcs11.git
cd tpm2-pkcs11
./bootstrap
./configure
make
./configure --enable-unit
make
make check

fails with the error

make   test/unit/test_twist
make[1]: Entering directory '/home/jonas/tpm2-pkcs11'
  CC       test/unit/test_twist-test_twist.o
  CCLD     test/unit/test_twist
/usr/bin/ld: test/unit/test_twist-test_twist.o: in function `test_twist_new_bad_alloc':
test_twist.c:(.text.test_twist_new_bad_alloc+0x15): undefined reference to `twist_next_alloc_fails'
/usr/bin/ld: test/unit/test_twist-test_twist.o: in function `test_twist_append_bad_alloc':
test_twist.c:(.text.test_twist_append_bad_alloc+0x40): undefined reference to `twist_next_alloc_fails'
/usr/bin/ld: test/unit/test_twist-test_twist.o: in function `test_twist_truncate_bigger_bad_alloc':
test_twist.c:(.text.test_twist_truncate_bigger_bad_alloc+0x4c): undefined reference to `twist_next_alloc_fails'
/usr/bin/ld: test/unit/test_twist-test_twist.o: in function `test_twist_unhexlify_failed_alloc':
test_twist.c:(.text.test_twist_unhexlify_failed_alloc+0x15): undefined reference to `twist_next_alloc_fails'
/usr/bin/ld: test/unit/test_twist-test_twist.o: in function `test_twist_hexlify_alloc_fail':
test_twist.c:(.text.test_twist_hexlify_alloc_fail+0x34): undefined reference to `twist_next_alloc_fails'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:1196: test/unit/test_twist] Error 1
make[1]: Leaving directory '/home/jonas/tpm2-pkcs11'
make: *** [Makefile:1929: check-am] Error 2

You need to e.g. touch src/lib/twist.c after the second configure to force recompilation, afterwards make check passes.

I do not know whether there is a good way to achieve automatic recompilation when this configure option changes using Autotools, but I at least wanted to document this behaviour.

Contradicting SPDX License Information

E.g.
https://github.com/tpm2-software/tpm2-pkcs11/blob/master/test/integration/pkcs-find-objects.int.c
shows:

/* SPDX-License-Identifier: BSD-2 */
/*
 * Copyright (c) 2018, Intel Corporation
 * All rights reserved.
 */
/* SPDX-License-Identifier: BSD-2 */
/***********************************************************************
 * Copyright (c) 2017-2018, Intel Corporation
 *
 * All rights reserved.
 ***********************************************************************/

--> Are we BSD 2 or 3?

Using tpm2-pkcs11 with GnuTLS p11tool and certtool

I have been trying to get tpm2-pkcs11 to work with GnuTLS's p11tool, but haven't been successful. Do you happen to have some steps for how to create a TPM 2 key with your tools or p11tool (better) and use the key with the p11tool then? Maybe something is wrong with my setup.

I also posted a message with the other PKCS11 TPM 2 module that I couldn't get to work, either: irtimmer/tpm2-pk11#84

test_pkcs11.c fails with custom TCTI options

When using the TPM2_PKCS11_TCTI environment variable with custom options, e.g.

export TPM2_PKCS11_TCTI="tabrmd:bus_type=session"

the second session test in test_pkcs11.c invariably fails, irrespective of the order of the tests.

The problem is caused by this line in tcti_ldr.c, which ends up modifying the underlying TPM2_PKCS11_TCTI environment variable (see here for an explanation), dropping the configuration options.

The offending code looks like this:

char *optstr = getenv (TPM2_PKCS11_TCTI);
// ...
char *split = strchr(optstr, ':');
// ...
split[0] = '\0';

Upon execution of the second test, the truncated environment variable will be retrieved, causing the test to fail.

Configure is failing

Thanks to williamcroberts for bootstrap LD_VERSION issue and now i'm able to do successful bootstrap step with ./bootstrap -I /usr/share/gnulib/m4 .

configure throws below error
pi@raspberrypi:~/tpm2-pkcs11 $ ./configure
configure: error: Invalid policy. Valid policies: git-directory, minor-version.

For more information:
Platform : Raspberry Pi model 3 revB board
OS : Linux version 4.14.72-v7+ (root@raspberrypi) (gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1+deb9u1)) #8 SMP

path based pkcs11 store option

All the current PKCS11 store locations depend on ENV variables, allow an option to use a path and allow configure to over ride that path. The search pattern should be:

  1. TPM2_PKCS11_STORE env variable
  2. $HOME location
  3. CWD
  4. Absolute path that is changeable at configure time

Character-Strings must be padded correctly and should not/must not be NULL terminated

All character-strings in pkcs#11 must be padded with the blank character ' '.
In most cases the character string SHOULD NOT be NULL terminated.
or in some cases it MUST not be NULL terminated.

-> Fix by applying the correct padding without NULL termination (for all cases).
Luckily pcks11t.h already includes comments which members should be padded.
-> Check all occurrences and fix them as necessary.

Add support for certificates in db

Some application which use NSS (like Firefox or Chrome) or gnupg-pkcs11-scd only use the keys in conjunction with the certificate provided by the pkcs#11 interface. Therefore support is needed to store certificates in the tpm2-pkcs11 database so they can be queried by the application.

operation state is session based

Currently the implementation allows for multiple concurrent operations on a session when the spec doesn't support this. Remove the per-session per-operation state and make it session global. Thus things like C_SignInit followed by C_DigestInit will fail. Add tests.

Session State

Session State is currently not global and it should be. Once a C_Login occurs, all sessions are logged in. Correct this in the implementation and test.

Default params for tpm2_createprimary

I have built tpm2_tools, tpm2_tss and tpm2_pkcs11 from the tip of their respective master branches.

When I run ptool init:
$ tpm2_ptool.py init --pobj-pin=mypobjpin --path=/tmp

I get the following error:
Could not execute tpm2_createprimary: ERROR: Tss2_Sys_CreatePrimary(0x2C4) - tpm:parameter(2):value is out of range or is not correct for the context
ERROR: Unable to run tpm2_createprimary

Recreating the cmd line ptool used I get:
$ tpm2_createprimary -p hex:55d15e8bff73005fd860d47191a4192087cfbfa09443c45320e47e4b8e912b64 -o /tmp/tmp4c6Z3M/context.out

This fails with the same error message. However if I specify the algorithm (rsa) the command succeeds
$ tpm2_createprimary -G rsa -p hex:55d15e8bff73005fd860d47191a4192087cfbfa09443c45320e47e4b8e912b64 -o /tmp/tmp4c6Z3M/context.out

Similarly, if I modify the ptool code to add '-G rsa' to the tpm2_createprimary cmd line it works.

The tpm2_createprimary man page suggests the default algorithm will be rsa if it is not specified but the behavior seems different.

Any ideas?

ECC Support

  • tpm2_ptool.py to support ecc option
  • support at least 1 ecc signing scheme (todo determine best scheme)

Deadlock on logout

I'm not sure why it doesn't trigger in the test,
but I get a deadlock on logout.

I already looked into it and
the problem is session_ctx_logout tries to lock a ctx, which is already held via the session_table_lookup and then fails at
session_ctx_lock(tok->login_session_ctx);

I'll look up the steps to reproduce when I find some time :/

token locks

The token data structure will need locking, identify and lock the structure as appropriate.

Could not execute tpm2_createprimary: tpm2_createprimary: invalid option -- 'p'

Any advise? Looks like tpm2-tools for tpm2_createprimary does not have option -p but -P instead?..

$ tpm2_ptool.py init --pobj-pin=mypobjpin --path=~/tmp
Traceback (most recent call last):
File "/home/user/Documents/tpm2-software/tpm2-pkcs11/tools/tpm2_ptool.py", line 686, in call
ctx = tpm2.createprimary(ownerauth, pobjauth)
File "/home/user/Documents/tpm2-software/tpm2-pkcs11/tools/tpm2_ptool.py", line 146, in createprimary
raise RuntimeError("Could not execute tpm2_createprimary: %s" % stderr)
RuntimeError: Could not execute tpm2_createprimary: tpm2_createprimary: invalid option -- 'p'

Could not execute tpm2_createprimary: tpm2_createprimary: invalid option -- 'p'

$ tpm2_createprimary
Usage: tpm2_createprimary []
Where are:
[ -H | --hierarchy=] [ -P | --pwdp=] [ -K | --pwdk=] [ -g | --halg=]
[ -G | --kalg=] [ -C | --context=] [ -L | --policy-file=] [ -A | --object-attributes=]
[ -S | --input-session-handle=] [ - | --(null)]

Login to two sessions raises assertion `t->login_session_ctx == NULL' failed.

Full pkcs11 log:

0: C_GetFunctionList
2018-11-15 20:19:38.963
Returned:  0 CKR_OK

1: C_Initialize
2018-11-15 20:19:38.963
[in] pInitArgs = 0x7e8c2d08
     flags: 2
       CKF_OS_LOCKING_OK
Returned:  0 CKR_OK

2: C_GetSlotList
2018-11-15 20:19:38.972
[in] tokenPresent = 0x1
[out] pSlotList: 
Count is 1
[out] *pulCount = 0x1
Returned:  0 CKR_OK

3: C_GetSlotList
2018-11-15 20:19:38.973
[in] tokenPresent = 0x1
[out] pSlotList: 
Slot 1
[out] *pulCount = 0x1
Returned:  0 CKR_OK

4: C_GetTokenInfo
2018-11-15 20:19:38.974
[in] slotID = 0x1
[out] pInfo: 
      label:                  'testtokens                      '
      manufacturerID:         'Intel                           '
      model:                  'TPM2 PKCS#11    '
      serialNumber:           '0000000000000000'
      ulMaxSessionCount:       1024
      ulSessionCount:          0
      ulMaxRwSessionCount:     1024
      ulRwSessionCount:        0
      ulMaxPinLen:             128
      ulMinPinLen:             5
      ulTotalPublicMemory:     -1
      ulFreePublicMemory:      -1
      ulTotalPrivateMemory:    -1
      ulFreePrivateMemory:     -1
      hardwareVersion:         0.0
      firmwareVersion:         0.0
      time:                   '  20181115191938'
      flags:                   405
        CKF_RNG                          
        CKF_LOGIN_REQUIRED               
        CKF_TOKEN_INITIALIZED            
Returned:  0 CKR_OK

5: C_OpenSession
2018-11-15 20:19:38.976
[in] slotID = 0x1
[in] flags = 0x6
pApplication=(nil)
Notify=(nil)
[out] *phSession = 0x1000000
Returned:  0 CKR_OK

6: C_Login
2018-11-15 20:19:39.046
[in] hSession = 0x1000000
[in] userType = CKU_USER
[in] pPin[ulPinLen] 01a8ae20 / 6
    00000000  31 32 33 34 35 36                                123456          
Returned:  0 CKR_OK

7: C_GetSlotList
2018-11-15 20:19:40.538
[in] tokenPresent = 0x1
[out] pSlotList: 
Count is 1
[out] *pulCount = 0x1
Returned:  0 CKR_OK

8: C_GetSlotList
2018-11-15 20:19:40.538
[in] tokenPresent = 0x1
[out] pSlotList: 
Slot 1
[out] *pulCount = 0x1
Returned:  0 CKR_OK

9: C_GetSlotInfo
2018-11-15 20:19:40.539
[in] slotID = 0x1
[out] pInfo: 
      slotDescription:        'bar                             '
                              '                                '
      manufacturerID:         'foo                             '
      hardwareVersion:         42.42
      firmwareVersion:         13.13
      flags:                   5
        CKF_TOKEN_PRESENT                
        CKF_HW_SLOT                      
Returned:  0 CKR_OK

10: C_GetTokenInfo
2018-11-15 20:19:40.539
[in] slotID = 0x1
[out] pInfo: 
      label:                  'testtokens                      '
      manufacturerID:         'Intel                           '
      model:                  'TPM2 PKCS#11    '
      serialNumber:           '0000000000000000'
      ulMaxSessionCount:       1024
      ulSessionCount:          1
      ulMaxRwSessionCount:     1024
      ulRwSessionCount:        1
      ulMaxPinLen:             128
      ulMinPinLen:             5
      ulTotalPublicMemory:     -1
      ulFreePublicMemory:      -1
      ulTotalPrivateMemory:    -1
      ulFreePrivateMemory:     -1
      hardwareVersion:         0.0
      firmwareVersion:         0.0
      time:                   '  20181115191940'
      flags:                   405
        CKF_RNG                          
        CKF_LOGIN_REQUIRED               
        CKF_TOKEN_INITIALIZED            
Returned:  0 CKR_OK

11: C_OpenSession
2018-11-15 20:19:40.539
[in] slotID = 0x1
[in] flags = 0x6
pApplication=(nil)
Notify=(nil)
[out] *phSession = 0x1000001
Returned:  0 CKR_OK

12: C_Login
2018-11-15 20:19:40.540
[in] hSession = 0x1000001
[in] userType = CKU_USER
[in] pPin[ulPinLen] 01a8ae20 / 6
    00000000  31 32 33 34 35 36                                123456          
pkcs11app: src/lib/session_ctx.c:310: session_ctx_login: Assertion `t->login_session_ctx == NULL' failed.

use python bindings

Once tpm2-tss has python bindings, re-write tpm2-ptool in python without the tpm2-tools dependencies.

Calling C_Sign for size doesn't work

Calling C_Sign/C_SignFinal or C_Verify/C_VerifyFinal with a buffer to small or NULL should result in a CKR_BUFFER_TOO_SMALL|CKR_OK return respectively and the value of the length in the length pointer.

Currently this fails, correct the behavior so it aligns to the specification, notably covered in section 5.2

C_OpenSession deadlocks if C_INITIALIZE(.flags=CKF_OS_LOCKING_OK) is called

C_OpenSession ends with a deadlock if C_INITIALIZE is called with .flags=CKF_OS_LOCKING_OK

Steps to reproduce:

  1. Pass CKF_OS_LOCKING_OK flag to C_INITIALIZE
  2. Call OpenSession
    If compiled with --debug:
    pkcs-open-close-session.int: src/lib/mutex.h:70: mutex_lock_fatal: Assertion `rv == CKR_OK' failed.

See example below (modified from test/integration/pkcs-open-close-session.int.c)


 CK_C_INITIALIZE_ARGS init_args ={0};
 init_args.flags=2;

 CK_RV rv = C_Initialize(&init_args);
    if(rv != CKR_OK){
        LOGE("C_Initialize failed! Response Code %x", rv);
        exit(1);
    }


    CK_SESSION_HANDLE handle;

    rv = C_OpenSession(0x01, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL , NULL, &handle);
// We never get here!
// pkcs-open-close-session.int: src/lib/mutex.h:70: mutex_lock_fatal: Assertion `rv == CKR_OK' failed.
// Abgebrochen

    if(rv != CKR_OK){
        LOGE("C_OpenSession failed! Response Code %x", rv);
        exit(1);
    }

C_CloseSession not loggin out on no more session handles

C_CloseSession or C_CloseAllSessions() should implicitly logout. Also, there's a state in the login/logout tests where test_user_login_logout_good is not logging out and (perhaps) the next passing login (i think test_user_global_login_logout_good) causes the full login code to-rerun without a logout. That code should skip a lot of the login steps if not all of them.

Keygen not implemented, returns dummy handles

The current key_gen returns just two dummy handles, which would confuse an application using this functionality.
Either we should return CKR_FUNCTION_NOT_SUPPORTED for now, or better implement / re-enable the code.

dependency on tpm2_encryptdecrypt

Currently tpm2-pkcs11 depends on tpm2_encryptdecrypt to be able to support a normal user and a security officer as defined by PKCS#11. However doesn't seem to work on TPMs (see also tpm2-software/tpm2-tools#407 ) based on an older revision like mine. So it would be nice if this dependency could be removed. However I'm not sure what would be an acceptable solutions, as I'm not completely sure how tpm2-pkcs11 works internally:

  • make pins optional (less secure, but could also have it own use cases)
  • optional only use a single pin (if pins are equal or only user pin is set) directly on the object itself

or are there other ideas on having these two users without the usage of tpm2_encryptdecrypt?

Make fails with "missing separator"

./bootstrap  -I /usr/share/gnulib/m4
./configure
make
Makefile:2059: *** missing separator.  Schluss.

The corresponding line in the makefile is
@CODE_COVERAGE_RULES@

Tested on raspberry pi with raspbian.
It used to work somehow (with the old m4 macros?)

OpenVPN setup example

Hello!

Is it possible to setup this pkcs11 module to be used with OpenVPN?
I found one comment from January 2018 in SourceForge by somebody that apparently was able to do it with the https://github.com/irtimmer/tpm2-pk11 module (deprecated by this module now?).

A lot have changed since then apparently. An this projects are actively being develop :)

Do you have any hints on how we could use this with OpenVPN?

Thank you in advance!

Please provide p11-kit module file

Modern *nix systems use p11-kit for managing PKCS#11 modules.

Please consider installing the module itself to $(pkg-config --variable=p11_module_path p11-kit-1) and also installing a p11-kit module file to $(pkg-config --variable=p11_module_configs p11-kit-1).

Then, any well-behaved application on the system will be able to use keys from the PKCS#11 token simply by passing it a RFC7512 PKCS#11 URI instead of a filename.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.