Commit Graph

88 Commits

Author SHA1 Message Date
Jakub Jelen caae75758c Add internal support for (X)EdDSA keys 2021-03-01 15:42:26 +01:00
Jakub Jelen 95122abe2e pkcs11: Implement PKCS#11 3.0 stubs for required functions 2021-01-11 14:49:22 +01:00
Alexander 7296210224 Hide private objects only when not logged in 2020-09-28 13:01:15 +02:00
Alexander d85610b29b Fixed issue with C_FindObjectsInit, when CKU_SO does not see private objects, but CKU_USER see them 2020-09-28 13:01:15 +02:00
Raul Metsma d14cf97d7a Allow to create temporary objects with readonly sessions and readonly cards
Fixes #1719

Signed-off-by: Raul Metsma <raul@metsma.ee>
2019-07-22 13:34:59 +02:00
Frank Morgner aed95b2f2b pkcs11: check inputs
prevents NULL pointer dereference
2019-01-30 21:57:59 +01:00
Frank Morgner f2145eca79 don't debug pointers 2018-12-06 09:26:42 +01:00
Hannu Honkanen 351e0d2bd6 Merge remote-tracking branch 'upstream/master' into wrapping-rebased and resolve conflicts 2018-11-02 13:42:41 +02:00
Frank Morgner 54cb1099a0 fixed warnings about precision loss 2018-11-01 00:17:22 +01:00
Hannu Honkanen 7bb53423a1 Code cleanup and minor corrections according to review. pkcs15-lib: Extractable keys are now marked as native. Check return value of check_key_compatibility in more explicit way to avoid misunderstandings. 2018-10-31 10:36:41 +02:00
Hannu Honkanen aa814fd8e8 Implemented C_Wrap into PKCS#11 interface. Added support for wrapping and unwrapping with secret keys into framework-pkcs15.c and all the way to the card driver level. 2018-10-31 10:24:19 +02:00
Hannu Honkanen 5f51d5d315 Added implementation of C_UnwrapKey all the way from PKCS#11 interface to the card driver level.
Not yet complete, but can be run with CKA_TOKEN=FALSE set in the target object. Currently unwrapping emulated
with a decrypt operation in card-myeid.c. To be improved.
2018-10-31 10:22:16 +02:00
Frank Morgner 00d1501ae6 pkcs11: fail in C_CreateObject/C_DestroyObject if write protected 2018-09-14 08:23:08 +02:00
Jakub Jelen a6e6cfb6de Log return value of C_GenerateRandom() 2018-04-04 21:43:01 +02:00
Maciej S. Szmigiero 62cbda6cd9 Fix log messages format and parameter issues flagged by GCC
Since "Add GCC format checking attributes to log functions" commit GCC
warns us about problems with format strings and their arguments provided
to OpenSC message logging functions.

This commit fixes all cases where GCC warned about incorrect format on
64-bit Linux, 32-bit and 64-bit mingw builds (with SM and OpenSSL enabled).
Well, almost all since on mingw GCC does not recognize "ll" size specifier
(present at least since Visual Studio 2005, also in mingw own CRT) so these
(few) warnings about it remain.

In most cases format size specifier for size_t type was missing (usually
size was left at default int level, with is different on 64-bit x86).
Some formats had too few / too many arguments.
In some cases pointers were printed as integers.
Some long variables were missing "l" prefix (especially with regard to %x
format).

Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
2017-03-27 11:05:16 +02:00
Frank Morgner 3384113616 reset `login_user` with `reset_login_state` 2015-12-06 10:42:45 +01:00
Frank Morgner b43ea19320 reset login_user on CKR_USER_NOT_LOGGED_IN 2015-12-06 10:42:45 +01:00
Frank Morgner 83ef753799 Implemented atomic PKCS#11 transactions 2015-12-06 10:42:45 +01:00
Doug Engert b48fa70308 sc_pkcs11_card improvements
This is name change only fix.

    The variable name "card" was being used to refer to a struct sc_card or a struct sc_pkcs11_card
    in some files including sc_pkcs11.h. In other files the variable name "p11card" is used for struct sc_pkcs11_card.
    This creates hard to read code, such as: slot->card->card.

    All definitations of sc_pkcs11_card *card now use p11card as the variable name.

Fix #471
2015-05-24 11:41:29 +02:00
Frank Morgner e84951a5bf fix resource leaks in while registering PKCS#11 mechanisms
introduces a free_mech_data for sc_pkcs11_mechanism_type_t to clear the
mechanisms private memory
2015-05-08 09:11:55 +02:00
Frank Morgner 16ea926d29 fixed indentation 2015-02-20 19:54:40 +01:00
Frank Morgner 11881a61b8 removed/fixed dead code 2015-02-05 01:38:41 +01:00
Frank Morgner 77752f442d fixed unused value 2015-01-28 05:47:20 +01:00
Doug Engert cd01a73caf C_Digest does not check if buffer too small before update. Issue #327
C_Digest will now query for the buffer size using sc_pkcs15_md_final
before calling sc_pkcs15_md_update. This avoids doing a double update
when the user passes in a buffer to small, then gets the buffer and calls
C_Digest again.
2014-12-02 11:15:24 +01:00
Frank Morgner 5d6c4b391d Merge pull request #253 from github-frankmorgner/remove-deadcode
card-asepcos: removed dead code
card-authentic: removed dead code
card-belpic: removed dead code
card-epass2003: removed dead code
card-flex: removed dead code
card-gpk: removed dead code
card-oberthur: removed dead code
card-piv: removed dead code
card-setcos: removed dead code
ctbcs: removed dead code
cwa14890: removed dead code
muscle: removed dead code
pkcs15-atrust-acos: removed dead code
pkcs15-gemsafeV1: removed dead code
pkcs15-skey: removed dead code
reader-ctapi: removed dead code
framework-pkcs15: removed dead code
pkcs11-object: removed dead code
pkcs15-asepcos: removed dead code
pkcs15-cardos: removed dead code
pkcs15-jcop: removed dead code
pkcs15-lib: removed dead code
pkcs15-oberthur: removed dead code
parse: removed dead code
sclex: removed dead code
sm-card-authentic: removed dead code
sm-card-iasecc: removed dead code
sm-cwa14890: removed dead code
sm-global-platform: removed dead code
sc-test: removed dead code
pkcs11-tool: removed dead code
pkcs15-tool: removed dead code
2014-09-07 21:22:43 +02:00
Martin Paljak 32ed309037 pkcs11: use calloc instead of malloc; remove obsolete malloc.h references. 2013-03-15 23:02:02 +02:00
Viktor Tarasov 9c5dbea883 pkcs11: ECHD and secret keys support from Douglas
This support were initially proposed by Douglas (https://github.com/dengert/OpenSC/commits/ecdh) and
then merged into SM branch (https://github.com/viktorTarasov/OpenSC-SM/tree/secure-messaging).
2012-05-28 20:06:23 +02:00
Viktor Tarasov 14049fb806 pkcs11: prepare internal API for multi-application mode
- simplify some of framework handles: remove from it's prototype the arguments that can be derived from the other arguments;
for exemple: foo(slot, slot->card) --> foo(slot)
- add the 'application' argument to the bind, unbind and similar handles;
- preview more then one framework data attached to the pkcs11card object.
- placehold for the future 'derive' and 'can_do' handles.
2012-05-22 17:18:00 +02:00
Viktor Tarasov 20bf3f5484 pkcs11: use short form of log messages
Change-Id: Ice298552238da9eeb0b646fc1ddfaf4d7a8ee4bb
2012-05-13 00:27:38 +02:00
Viktor Tarasov cb13633634 remove trailing whitespaces
inspired by
http://www.opensc-project.org/pipermail/opensc-devel/2012-March/017883.html

Change-Id: If170e830d8d9587a31742feffb6fff54cfdf75b4
2012-04-03 00:00:56 +02:00
Ludovic Rousseau b7c2836d94 C_Digest(): check context validity before logging
If the context is invalid (no C_Initialize) the we get a:
log.c:76: sc_do_log_va: Assertion `ctx != ((void *)0)' failed.
2011-06-23 16:10:46 +02:00
andre 30bf0f17a8 pkcs11-object.c: Add missing initializers.
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4966 c6295689-39f2-0310-b995-f0e70906c6a9
2010-12-17 19:09:24 +00:00
dengert c2fe4609a9 Modifications to support EC and ECDSA for
emulated cards. True PKCS#15 cards with EC 
will need additional changes. 

Main changes are in framework-pkcs15.c, mechanism.c,
padding.c, pkcs15-algo.c and pkcs15-sec.c 
where switch statements for key type, and testing
of flags was modified to make it easier to add 
additional key types in the future. 

The code was tested using RSA and ECDSA using a PIV card 
from pkcs11-tool, OpenSSL and Thunderbird with 
modifications to NSS-3.12.7 to get ECDSA to sign e-mail. 

Only named curves are supported for ECDSA, ECDH is still 
needed. pkcs11-tool has only minimal changes need to work 
with the -O option to list EC keys. 

One additional line was added to pkcs15-sec.c which 
should get GOSTR sign to work. 

libp11 and engine do not yet have EC support. 

 --This line, and those below, will be ignored--

M    src/tools/piv-tool.c
M    src/tools/pkcs11-tool.c
M    src/pkcs11/framework-pkcs15.c
M    src/pkcs11/mechanism.c
M    src/pkcs11/pkcs11-object.c
M    src/libopensc/pkcs15-prkey.c
M    src/libopensc/card-piv.c
M    src/libopensc/padding.c
M    src/libopensc/cardctl.h
M    src/libopensc/pkcs15-algo.c
M    src/libopensc/libopensc.exports
M    src/libopensc/pkcs15-piv.c
M    src/libopensc/pkcs15-sec.c


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4904 c6295689-39f2-0310-b995-f0e70906c6a9
2010-12-01 20:08:42 +00:00
ludovic.rousseau 9c433a9cce pkcs11-object.c: In function ‘C_DigestInit’:
pkcs11-object.c:455: warning: label ‘out’ defined but not used


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4864 c6295689-39f2-0310-b995-f0e70906c6a9
2010-11-06 17:00:09 +00:00
dengert 573a1de4d6 Allow c_FindObjectsInit to find unlimited objects - #258
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4760 c6295689-39f2-0310-b995-f0e70906c6a9
2010-09-22 14:41:50 +00:00
martin 8c9666d631 pkcs11: C_SeedRandom is not implemented by OpenSC nor most smart cards, nor does it fit well into PKCS#15 concept.
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4662 c6295689-39f2-0310-b995-f0e70906c6a9
2010-09-01 06:23:26 +00:00
martin 4f03502b60 pkcs11: check PKCS#11 function parameters for obvious errors and return CKR_ARGUMENTS_BAD early without locking the module.
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4647 c6295689-39f2-0310-b995-f0e70906c6a9
2010-08-23 14:47:14 +00:00
martin 9a63e03e9a pkcs11/pkcs15-init: remove automagic software key generation. Remove software based secret key handling from PKCS#11.
Support for importing cleartext keys is left untouched, but all transparent key generation by either opensc-pkcs11.so or pkcs15-init is removed, to make the operation with cleartext keys visible to the user and his explicit wish.
OpenSC is a PKCS#11 library for accessing keys protected by a smart card. Key material in software is not protected by smart cards and can leave a false sense of security to the user.

http://www.opensc-project.org/pipermail/opensc-devel/2010-April/013877.html

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4646 c6295689-39f2-0310-b995-f0e70906c6a9
2010-08-23 14:47:07 +00:00
ludovic.rousseau f47416d60e Do not cast the return value of malloc(3) and calloc(3)
From http://en.wikipedia.org/wiki/Malloc#Casting_and_type_safety
" Casting and type safety

malloc returns a void pointer (void *), which indicates that it is a
pointer to a region of unknown data type. One may "cast" (see type
conversion) this pointer to a specific type, as in

int *ptr = (int*)malloc(10 * sizeof (int));

When using C, this is considered bad practice; it is redundant under the
C standard. Moreover, putting in a cast may mask failure to include the
header stdlib.h, in which the prototype for malloc is found. In the
absence of a prototype for malloc, the C compiler will assume that
malloc returns an int, and will issue a warning in a context such as the
above, provided the error is not masked by a cast. On certain
architectures and data models (such as LP64 on 64 bit systems, where
long and pointers are 64 bit and int is 32 bit), this error can actually
result in undefined behavior, as the implicitly declared malloc returns
a 32 bit value whereas the actually defined function returns a 64 bit
value. Depending on calling conventions and memory layout, this may
result in stack smashing.

The returned pointer need not be explicitly cast to a more specific
pointer type, since ANSI C defines an implicit conversion between the
void pointer type and other pointers to objects. An explicit cast of
malloc's return value is sometimes performed because malloc originally
returned a char *, but this cast is unnecessary in standard C
code.[4][5] Omitting the cast, however, creates an incompatibility with
C++, which does require it.

The lack of a specific pointer type returned from malloc is type-unsafe
behaviour: malloc allocates based on byte count but not on type. This
distinguishes it from the C++ new operator that returns a pointer whose
type relies on the operand. (see C Type Safety). "

See also
http://www.opensc-project.org/pipermail/opensc-devel/2010-August/014586.html


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4636 c6295689-39f2-0310-b995-f0e70906c6a9
2010-08-18 15:08:51 +00:00
ludovic.rousseau e6803d9ca0 Use CK_RV (defined as unsigned long) instead of int for the type the
return code of C_* functions

Thanks to Martin Vogt for the patch
http://www.opensc-project.org/pipermail/opensc-devel/2010-June/014351.html


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4415 c6295689-39f2-0310-b995-f0e70906c6a9
2010-06-16 11:43:10 +00:00
viktor.tarasov 0400a2dc35 pkcs11: make possible context dependent 'sc' to 'cryptoki' error conversion
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4262 c6295689-39f2-0310-b995-f0e70906c6a9
2010-04-21 10:51:13 +00:00
aj 6545b42c02 cleanup of the debug code:
* reduce to a few, supported functions.
* change all functions to take the debug level as parameter.
* use symbolic names for the debug levels.
* fix tools to pass "verbose"/"opt_debug" as ctx->debug.


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4118 c6295689-39f2-0310-b995-f0e70906c6a9
2010-03-15 12:17:13 +00:00
aj d22a2483c0 Header file cleanup.
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4081 c6295689-39f2-0310-b995-f0e70906c6a9
2010-03-04 08:14:36 +00:00
martin cf4ec4ab3f #47: C_SignRecover is not implemented at the moment, don't give any promises with C_SignRecoverInit either.
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4071 c6295689-39f2-0310-b995-f0e70906c6a9
2010-02-24 14:06:04 +00:00
s 33228c7cbc cleanup and correct style
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3945 c6295689-39f2-0310-b995-f0e70906c6a9
2010-01-27 16:04:19 +00:00
s 66f9ccaa94 fix SIGSEGV:
$ pkcs11-tool --slot 5 --login --pin "12345678" --label 1 --type data --private --write-object tmp.txt
Generated Data Object:
Data object 0
...
$ pkcs11-tool --slot 5 -O --login --pin "12345678"
Data object 135436368
  label:          '1'
  application:    '1'
  app_id:         -1
  flags:           modifiable private
...
(gdb) run
Starting program: /usr/local/bin/pkcs11-tool --slot 5 --login --pin "12345678" --label 1 --type data --delete-object

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1210333504 (LWP 7193)]
0xb7cc8181 in slot_token_removed (id=5) at slot.c:319
319                     if (object->ops->release)
(gdb) bt
#0  0xb7cc8181 in slot_token_removed (id=5) at slot.c:319
#1  0xb7cc82dd in card_removed (reader=0x811bdf0) at slot.c:132
#2  0xb7cc43e7 in C_Finalize (pReserved=0x0) at pkcs11-global.c:298
#3  0x08050fc0 in main (argc=11, argv=0xbf98d6f4) at pkcs11-tool.c:677


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3943 c6295689-39f2-0310-b995-f0e70906c6a9
2010-01-27 12:22:50 +00:00
martin 0244baa494 pkcs11:
- slots, sessions and objects are kept as lists.
  - change the way slots, cards and readers are managed.
  - re-implement C_WaitForSlotEvent(/C_Finalize) as written in PCKS#11 v2.20, canceling pending blocking calls.
  - implement a "virtual hotplug slot" with a floating slot id to keep NSS working with C_WaitForSlotEvent with a new reader.
    NSS does not call C_GetSlotList(NULL) to re-fetch the list of available slots if C_WaitForSlotEvent returns an event in an already known slot ID.
    By changing the ID of a slot whenever a reader attached  NSS/Firefox can be tricked into recognizing new readers when waiting for events with C_WaitForSlotEvent.
  - change (possibly break something) sc_to_cryptoki_error() to not have side-effects
  - Implement CKU_CONTEXT_SPECIFIC in C_Login to implement CKA_ALWAYS_AUTHENTICATE (keys with user consent) 
 


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3935 c6295689-39f2-0310-b995-f0e70906c6a9
2010-01-24 20:45:02 +00:00
s 7cf8760f9f Fix: any of these calls
C_CreateObject(hSession, NULL_PTR, 1, NULL_PTR);
C_GetAttributeValue(hSession, hObject, NULL_PTR, 1);
C_SetAttributeValue(hSession, hObject, NULL_PTR, 1);
C_FindObjectsInit(hSession, NULL_PTR, 1);
C_FindObjects(hSession, NULL_PTR, 0, NULL_PTR);
C_FindObjects(hSession, NULL_PTR, 1, NULL_PTR);
C_FindObjects(hSession, NULL_PTR, 1, pulObjectCount);
C_DigestInit(hSession, NULL_PTR);
C_SignInit(hSession, NULL_PTR, hKey);
C_SignRecoverInit(hSession, NULL_PTR, hKey);
C_DecryptInit(hSession, NULL_PTR, hKey);
C_VerifyInit(hSession, NULL_PTR, hKey);
C_GenerateKeyPair(hSession, NULL_PTR, pubKeyTmpl, arraysize(pubKeyTmpl),
prvKeyTmpl, arraysize(prvKeyTmpl), &hPubKey, &hPrvKey);
C_GenerateKeyPair(hSession, pMechanism, pubKeyTmpl,
arraysize(pubKeyTmpl), NULL_PTR, 1, &hPubKey, &hPrvKey);
C_GenerateKeyPair(hSession, pMechanism, NULL_PTR, 1, prvKeyTmpl,
arraysize(prvKeyTmpl), &hPubKey, &hPrvKey);
=>
Segmentation fault

Remark: Allow calls:
C_FindObjectsInit(hSession, NULL_PTR, 0)
C_GenerateKeyPair(hSession, pMechanism, NULL_PTR, 0, NULL_PTR, 0, phPublicKey, phPrivateKey)
C_UnwrapKey(hSession, pMechanism, hUnwrappingKey, pWrappedKey, ulWrappedKeyLen, NULL_PTR, 0, phKey)



git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3891 c6295689-39f2-0310-b995-f0e70906c6a9
2009-12-10 14:50:32 +00:00
s 22a950cd0d Fix: return CKR_SESSION_READ_ONLY from C_InitPIN, C_SetPIN,
C_CreateObject, C_CopyObject, C_DestroyObject, C_SetAttributeValue,
C_GenerateKey, C_GenerateKeyPair, C_UnwrapKey, C_DeriveKey if session is
read-only.

PKCS#11:
"C_InitPIN can only be called in the 'R/W SO Functions' state."

"C_SetPIN can only be called in the 'R/W Public Session' state, 'R/W SO
Functions' state, or 'R/W User Functions' state. An attempt to call it
from a session in any other state fails with error CKR_SESSION_READ_ONLY."

"Only session objects can be created/destroyed/modified
(C_CreateObject/C_DestroyObject/C_SetAttributeValue) during a read-only
session."


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3886 c6295689-39f2-0310-b995-f0e70906c6a9
2009-12-08 14:29:49 +00:00
s 5881b46ac1 PKCS#11: Spec does not allow CKR_OBJECT_HANDLE_INVALID from C_EncryptInit, C_DecryptInit, C_DigestKey, C_SignInit, C_SignRecoverInit, C_VerifyInit, C_VerifyRecoverInit, C_WrapKey, C_DeriveKey, C_UnwrapKey
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3885 c6295689-39f2-0310-b995-f0e70906c6a9
2009-12-07 12:50:59 +00:00