Commit Graph

118 Commits

Author SHA1 Message Date
Alexander Paetzelt 09a594d0f0 OpenPGP Card v3 ECC support (#1506)
* pgp: initialize ecc keys for OPC3

* Add supported ECC algorithms by card version

* Add tasks identified so far

* pgp: Recognize ECC set on card

* pgp: get_pubkey_pem read ECC pubkey from card

* pgp: minor code changes for ECC compatibility

* pgp: expand sc_cardctl_openpgp_keygen_info to hold ec info

* Fix segfault problem in pkcs15-pubkey.c

* pgp: enable key generation with pkcs15-init and ECC

* pgp: adapt calculate_and_store_fingerprint to accept ECC

* pgp: adapt rest of pgp_gen_key and subfunctions to accept ECC

* pgp: add kdf parameters for ECDH fingerprint calculation

* pgp: enable key import with pkcs15-init and ECC

* pkcs15-pubkey: fix_ec_parameters onlz accpets explicit data or named_curve

* Fix some mistakes during merge

* More clean up for PR

* Fix some ugly alignments

* Improve code readability

* Prevent unitialized variable by using FUNC_RETURN

* OpenPGP: add length check

* pgp: save exponent length in bits for sc_cardctl_openpgp_keystore_info_t

* pgp: length checks and reallocations

* pgp: oid init added

* OpenPGP: slightly re-factor pgp_update_new_algo_attr()

* replace loop copy with memcpy()
* use ushort2bebytes() to set RSA modulus & exponent
* use symbolic name SC_OPENPGP_KEYFORMAT_RSA_STD for the key import format

* OpenPGP: slighly re-factor pgp_parse_and_set_pubkey_output()

* check for RSA modulus & exponent lengths not being a multiple of 8
* make sure RSA modulus & exponent lengths are always set
* remove a left-over RSA setting from the EC code

* pgp: adding BYTES4BITS

* pgp: initialization of values in pgp_build_extended_header_list based on key type

* pgp: add BYTES4BITS and remove unnecessary tests

* Fix broken pgp_update_new_algo_attr

* pgp: fix the ecpoint_len variable
2019-01-30 22:00:36 +01:00
Frank Morgner f2145eca79 don't debug pointers 2018-12-06 09:26:42 +01:00
asc 2b60a0db0f Add support for SmartCard-HSM 4K (V3.0) 2018-08-31 13:42:44 +02:00
Peter Marschall 5abe99d228 fix typos
Mass-typo fixing, almost exclusively in comments and text strings.

While at it also fixed a few (very few) grammar errors.
2018-04-15 09:34:45 +02:00
Frank Morgner 45ad44e311 fixed handling SC_ASN1_TAG_EOC from sc_asn1_read_tag
We can't check for `tag == SC_ASN1_TAG_EOC` directly, because this
would also be true for a tag of 0x80 (with `class ==
SC_ASN1_CLASS_CONSTRUCTED`). So what we do is we check for the output
buffer to be NULL!

fixes https://github.com/OpenSC/OpenSC/issues/1273
2018-03-30 08:28:51 +02:00
Frank Morgner a84bf918af fixed memory leak 2018-03-22 14:54:50 +01:00
Jakub Jelen e2fe24d56a clang static analyzer reports
tools/pkcs15-tool.c:
	Dead assignment: Value stored to 'c' is never read
tools/pkcs11-tool.c:
	Dead assignment: Value stored to 'n' is never read
	Dead assignment: Value stored to 'rv' is never read
libopensc/card-cac.c:
	Dead assignemnt: Value stored to 'tl_head_len' is never read
	Dead increment: Value stored to 'outp' is never read
common/libpkcs11.c:
	Memory leak in case of C_UnloadModule() fails
libopensc/pkcs15-pubkey.c:
	Potential memory leaks
pkcs11/mechanism.c:
	Potential memory leak
pkcs11/framework-pkcs15.c:
	Potential memory leaks
	Dereference of null pointer
	Dead assignments
tools/sc-hsm-tool.c:
	Function call argument is an uninitialized value
	Dead assignment: Value stored to 'r' is never read
libopensc/card-openpgp.c:
	Dead assignment: ignoring the errors in case of sc_pkcs15_encode_pubkey() failed
libopensc/pkcs15-cac.c:
	Dead assignments: ignoring return values
libopensc/pkcs15-coolkey.c:
	Dead assignments: ignoring return values
libopensc/card-sc-hsm.c:
	Dereference of undefined pointer value: Properly check the file allocation
pkcs11/slot.c:
	Dead assignment
pkcs15init/pkcs15-cflex.c:
	Dereference of null pointer
	Uninitialized argument values
2017-07-18 22:40:20 +02:00
Frank Morgner 93fa520bc9 Cope with incomplete ASN.1 data in sc_asn1_read_tag 2017-07-18 22:35:23 +02:00
Frank Morgner 805c6b5a30 PIV: workaround for reading public key
closes https://github.com/OpenSC/OpenSC/pull/1069
2017-07-11 10:08:50 +02:00
Frank Morgner 4c654606ea fixed Coverity issues
Fixes https://github.com/OpenSC/OpenSC/issues/1057
2017-06-13 13:07:52 +02:00
Frank Morgner 888215ca7c prevent integer overflow 2017-06-09 10:33:46 +02:00
Frank Morgner 6bfb39454b Fix missing error handling of memory allocation (#1020)
* libopensc: handle allocation errors
* handle more faults during memory allocation

fixes several situations that cause segmentation fault
2017-04-20 21:08:49 +02:00
Frank Morgner e7915ec198 replace assert with error handling 2017-04-04 09:11:35 +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
Jakub Jelen b3f047619d Prevent leaking memory from PIV readers 2017-01-05 16:34:40 +01:00
Jakuje 12f4026160 Fix Coverity remarks (#876) 2016-10-10 22:21:46 +02:00
Doug Engert 5fb4db6373 Use OpenSSL versions OpenSSL-0.9.7 to 1.1.0a for OpenSC
OpenSSL-1.1.0 was released 8/25/2016
OpenSSL-1.1.0a was released 9/22/2016

  https://www.openssl.org/news/openssl-1.1.0-notes.html

Changes to allow the OpenSC code base to work with OpenSSL versions from
0.9.7 to 1.1.0 with few changes.

This is an update and rebased version of my prep-openssl-1.1.0-pre6 branch.

No attempt was made to back port any OpenSSL features. These changes
just allow an updated OpenSC code base to use what is in the various OpenSSL
releases.

A new header libopensc/sc-ossl-compat.h contains extra defines
to reduce the need for so many #if OPENSSL_VERSION_NUMBER statements
in the source code.

The OpenSC source can now use the OpenSSL 1.1 API. The libopensc/sc-ossl-compat.h
has defines for the new API for use with older versions of OpenSSL.

sc-ossl-compat.h is included by libopensc/internal.h so all OpenSC
library routines can take advantage of it. For the tools, which do not use
libopensc/internal.h, libopensc/sc-ossl-compat.h is included by the tools.

The OpenSC source has been modified to use OpenSSL functions to access
hidden structures, such X509, BIGNUM, EVP_CIPHER_CTX, and use XXX_new
functions to allocate structures which must use pointer such as
BIGNUM and EVP_CIPHER_CTX.

For backward compatability sc-ossl-compat.h now defines inline routines
to emulate the RSA and DSA  access routines in OpenSSL-1.1.0. Thus
the same OpenSC source code can be used with openSSL versions from
0.9.7 to 1.1.0.

Inline routines were chosen, because using macros does not work on all platforms.
Having OpenSC versions of these routines in libopensc would be a posibility,
but they are only used for older version of OpenSSL, and could be removed in
the future.
 Changes to be committed:
	modified:   src/libopensc/card-entersafe.c
	modified:   src/libopensc/card-epass2003.c
	modified:   src/libopensc/card-gids.c
	modified:   src/libopensc/card-gpk.c
	modified:   src/libopensc/card-oberthur.c
	modified:   src/libopensc/card-piv.c
	modified:   src/libopensc/card-westcos.c
	modified:   src/libopensc/cwa-dnie.c
	modified:   src/libopensc/cwa14890.c
	modified:   src/libopensc/internal.h
	modified:   src/libopensc/p15card-helper.c
	modified:   src/libopensc/pkcs15-itacns.c
	modified:   src/libopensc/pkcs15-prkey.c
	modified:   src/libopensc/pkcs15-pubkey.c
	new file:   src/libopensc/sc-ossl-compat.h
	modified:   src/pkcs11/openssl.c
	modified:   src/pkcs15init/pkcs15-lib.c
	modified:   src/pkcs15init/pkcs15-oberthur-awp.c
	modified:   src/pkcs15init/pkcs15-oberthur.c
	modified:   src/pkcs15init/pkcs15-oberthur.h
	modified:   src/pkcs15init/pkcs15-westcos.c
	modified:   src/tools/cryptoflex-tool.c
	modified:   src/tools/gids-tool.c
	modified:   src/tools/netkey-tool.c
	modified:   src/tools/piv-tool.c
	modified:   src/tools/pkcs11-tool.c
	modified:   src/tools/pkcs15-init.c
	modified:   src/tools/sc-hsm-tool.c
	modified:   src/tools/westcos-tool.c
2016-10-08 06:15:06 -05:00
Viktor Tarasov 6f667e0510 pkcs15: fix warning Wsign-compare 2016-03-21 07:55:02 +01:00
Viktor Tarasov 8b9ce2cf4a coverity: INTEGER_OVERFLOW 2016-03-17 11:30:40 +01:00
Viktor Tarasov 9a22f146f8 pkcs15: fix argument for 'read-tag' call 2016-03-10 11:39:59 +01:00
Frank Morgner c2ff4f090a pkcs15-pubkey.c: fixed potential resource leak 2015-11-01 10:44:23 +01:00
Frank Morgner 9f0087d968 fixed missing includes 2015-10-30 18:21:40 +01:00
Frank Morgner 0853ecd887 pkcs15-pubkey.c: fixed potential resource leak 2015-10-30 12:18:04 +01:00
Frank Morgner 841d89dda6 pkcs15-pubkey: fixed resource leak 2015-05-08 09:10:47 +02:00
Doug Engert ee23d28654 EC field_length changes for non-multiple of 8 bits curves
In OpenSC the EC field_length is the number of bits in the field.
Most curves have a field_length which is a multiple of 8 bits
but there are many that are not.

The X and Y points and privateD are stored in octetstrings
so there may need to be an extra byte in the octetstring.

An OpenSSL BIGNUM will drop leading zero bytes, so its size can not be used
to determine the field_length.

fix #440
fix #433
2015-04-25 12:21:39 +02:00
Frank Morgner bcb5fc15e5 honour HAVE_CONFIG_H 2015-04-22 23:55:33 +02:00
vletoux 8ea328ff7f Minor code quality improvements.
Basically checks that the memory allocation succeed.

The ctbcs.c change improve the readability
because count = 0 and len > 254 does not add any value.

VTA: added few coding style changes
2015-04-05 13:15:57 +02:00
Philip Wendland 4b51b99748 pkcs11-tool: harmonize supported ECC curves
pkcs15-pubkey.c holds a struct containing supported ECC curves. The contents of
this struct are being harmonized with pkcs11-tool supported curves.
2015-03-29 12:55:54 +02:00
Viktor Tarasov 1739300657 pkcs15: fix name of EC curve, add new ones 2015-03-05 10:32:54 +01:00
Viktor Tarasov fa923831f8 introduce EC curve OID into algorithm info data
needed to store information about EC curve supported by card.
Primary usage is when importing/generating key to get know if particular curve is supported by card.
2015-02-20 13:49:18 +01:00
Viktor Tarasov e374f88b82 pkcs15-pubkey: add EC params in SPKI encoding 2015-02-07 19:55:08 +01:00
Viktor Tarasov 9148f21cd1 pkcs15-pubkey: possible NULL dereference, logs
... remove obsolete comments, minor coding style corrections
2015-02-07 19:55:08 +01:00
Frank Morgner 3a92bf7af5 fixed resource leaks 2015-02-05 01:38:41 +01:00
Frank Morgner 11881a61b8 removed/fixed dead code 2015-02-05 01:38:41 +01:00
Andreas Schwier d014056f1d sc-hsm: Add support for Koblitz curves secp192k1 and secp256k1 (Bitcoin) 2014-11-04 17:11:34 +01:00
Philip Wendland 955a339148 Merge PR#288 from philipWendland:upstream-ecc-fix
add the possibility to store public ECC keys encoded according to SPKI
EC pubkey storing: Check if params are available before copying.
pkcs15-lib.c / sc_pkcs15init_store_public_key may be called with keyargs->key.u.ec.params.value == NULL. In this case, allocating and copying the parameters will fail. Add a check to prevent this.
2014-10-01 15:27:26 +02:00
Andreas Schwier be200ab3c8 Merge pull request #282 from CardContact/fix-deleted-related-public-key
framework-pkcs15: Duplicate public key related to private key rather than referencing the framework object

Referencing the related public key is required to return PKCS#11 attributes for a private key only available
in the public key object (i.e. CKA_MODULUS). This patch adds a copy of the public key to the private key object rather than
referencing the public key object in the framework. This prevents SEGV when the public key framework object
is deleted with C_DestroyObject, but the reference from the public key remains intact.

The bug leads to all kind of stability problems when keys are created and deleted in the same session.

The patch is in particular important if OpenSC is used with EJBCA or any other application using the
SUN PKCS#11 provider: When generating key pairs, then the public key object is eventually garbage collected
which removes the related object in the PKCS#11 module. Because there is no fixed time for this operation,
corruption occurs at random.

In a next step, the remaining related_xxx fields in sc_pkcs11_object should be revised and possibly removed.

framework: Added more error checking
2014-09-07 23:47:24 +02:00
Viktor Tarasov 8871e61596 pkcs15: change names of internal SPKI procedures
sc_pkcs15_pubkey_from_spki_sequence() takes the ASN1 'subjectPublicKeyInfo' data
sc_pkcs15_pubkey_from_spki_fields() takes the ASN1 'subjectPublicKeyInfo' data without outter SEQUENCE tag
2014-02-16 22:35:52 +01:00
Viktor Tarasov 53eae613d4 pkcs15: remove not used internal procedures
sc_pkcs15_copy_pubkey_from_spki_object() not used
2014-02-16 22:11:39 +01:00
Viktor Tarasov 8000564239 pkcs15: fix encoding of 'SubjectPublicKeyInfo' 2014-02-09 18:49:58 +01:00
Viktor Tarasov dd5115bc02 pkcs15: use 'direct' public key value
pkcs15: in pubkey-info data
 * introduced new 'direct' 'raw' and 'spki' members
 * removed 'encoded der data' member
 * in 'read-public-key' try firstly SPKI direct value

pkcs11:
'direct' data used when getting CKA_VALUE attribute of public key

pkcs15init:
 * initialize 'raw' and 'spki' direct public key value
2014-02-09 18:49:58 +01:00
Doug Engert c5c49e3fd4 sc_pkcs15_encode_pubkey_as_spki depends on DER encoded RAW key
Make sure the sc_pkcs15_pubkey structure contains the DER endoding
of the RAW public key before trying to produce the SPKI version.
2014-01-19 19:30:04 +01:00
Viktor Tarasov 5437f87d4b pkcs15: encode public key algorithm (issue 202)
coding style: use linux style line break
2014-01-13 13:02:08 +01:00
Andreas Schwier d4be8ec747 sc_pkcs15_encode_pubkey_as_spki replaces sc_pkcs15_encode_pubkey_with_param.
The name implies what the format of the returned value, a SPKI.

The support for spki as a pkcs15 format of a pubkey, is extended to
work for any algorithm not just EC pubkeys. PKCS#15 appears to allow this.

sc_pkcs15_decode_pubkey_with_param will look for a SPKI
and attempt to use it for any algorithm, including RSA.
(RSA is the null case, as there are no algorithm parameters.)

sc_pkcs15_encode_pubkey_as_spki is exported from libopensc.

pkcs15-piv.c will use sc_pkcs15_encode_pubkey_as_spki to load public keys
as SPKI for RSA and EC.

The pubkey->data is never a SPKI, it is the DER encoding of the
pubkey without the parameters.  If an spki is needed, use the
sc_pkcs15_encode_pubkey_as_spki to get the DER encoding of the spki.

As in the previous set of patches, pkcs15-tool.c will output both
sc_pkcs15_decode_pubkey_with_param and its internal.
This was left for testing, and the pubkey_pem_encode should be deleted
2013-12-06 09:23:57 +01:00
Andreas Schwier 09e5a9fa7f pkcs11: Fixed typo 2013-12-05 14:26:49 +01:00
Andreas Schwier 3a6e7ba959 pkcs15: Changed encoding for EC public keys in PuKDF to SPKI rather than ECPoint, preserving domain parameter 2013-11-15 11:41:10 +01:00
Doug Engert 9ff8bd1c61 EC parameters not copied correctly
As reported in Github, the memcpy was not valid.

	pkcs15-pubkey.c-20131106-09007
2013-11-14 09:38:49 -06:00
Doug Engert 03c196eeaf ECC ecpointQ Fixes
The original ECC code in OpenSC stored the ecpointQ as a DER encoded OCTET STRING.
Shortly before 0.13.0, code changes where made to store the ecpointQ as raw data
without the DER encoding.

Only some of the code was changed to support this but not all, and the comments
that said the ecpointQ was in DER where not changed either.

Some card drivers continued to work, using the original code in all place,
while some cards failed, as they where using a mixture of original code and
0.13.0 code.

This commit fixes these problems.

The ecpointQ is stored in raw format

A new structure type sc_pkcs15_u8 is defined.

The ecpointQ are changed to use the struct sc_pkcs15_u8. This was done to avoid
 the confusion of using struct sc_pkcs15_der to hold non-DER encoded data.
(There may be other uses for this too...)

Comments are change is many places.

sc_pkcs15_decode_pubkey_ec was fixed to store the raw ecpointQ correctly.

sc_pkcs15_pubkey_from_spki was change to get the sc_ec_params from the alg_id
and fix up u.ec.params. Unfortunately the OpenSC code has two places EC parameters
are stored. They can get out of sync, or there may still be code
that looks in the wrng oplace. o(TODO get it to only only place.)

The u.ec.params.field_length is now set in a number of places, as this is need
in many of the PKCS#11 routines.

framework-pkcs15.c will now correctly return the DER encode ecpointQ,
for the CKA_EC_POINT attribute using pubkey->data which has the DER encoding
for the ecpointQ.

framework-pkcs15.c will look for the EC parameters in either the u.ec.params.der,
or in the alg_id->params. (TODO get it to only only place.)

pkcs15-myeid.c has some comments, as it looks like the code is storing a TLV
rather then a DER encoding of the ecpointQ. With the wrong encoding PKCS#11 will
return the wrong attribute for CKA_ECDSA_PARAMS.

pkcs15-piv.c is changed so emulation of a pubkey taken from a certificate will
work correctly.
2013-11-06 16:31:34 -06:00
Viktor Tarasov 8b07b9c5a7 compile on Windows, minor codding style issues 2012-12-03 15:21:22 +01:00
sjoblomt 457426543d MyEID ECDSA support 2012-12-03 14:37:13 +01:00