Commit Graph

105 Commits

Author SHA1 Message Date
Doug Engert d7d674129e PIV History Object Related Changes - Fixes #1330
&& is replaced by || in the test of valid key references
for retired keys found in the Historic object.

For retired keys, the user_consent flag was being set by default.
Thus a C_Login(CKU_CONTEXT_SPECIFIC) would be required.
NIST 800-73 only requires PIN_Always on the Sign Key.

To extend the usefullnes of "retired keys" on non government
issued PIV-like cards, code had already been added
to use the certificate keyUsage flags to override the NIST
defined key usage flags. The NONREPUDATION  flag is now used
to set the user_consent flag.

So rather then always requiring C_Login(CKU_CONTEXT_SPECIFIC)
for any retured key, the code only requires it for non government
cards where teh certificate has NONREPUDATION.

 Changes to be committed:
	modified:   card-piv.c
	modified:   pkcs15-piv.c
2018-05-23 14:19:05 +02:00
Frank Morgner 439a95f2d2
If card initialization fails, return SC_ERROR_INVALID_CARD (#1251)
fixes https://github.com/OpenSC/OpenSC/issues/946
2018-05-18 23:49:29 +02:00
Frank Morgner 1798bbe692 manuals: added missing "Authors" section 2018-05-16 13:43:25 +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
Doug Engert 0911982bef Various PIV changes
Some ActivIdentity CAC/PIV cards lose the login state when selecting
the PIV AID SC_CARD_TYPE_PIV_II_CAC and CI_PIV_AID_LOSE_STATE were added
so piv_card_reader_lock_obtained will  try and do a SELECT PIV AID.

card->type is reset to its original value if piv_match_card_continued
fails to match a card as PIV.

pkcs15-piv.c now uses sc_card_ctl which checks card->ops->card_ctl for NULL.

closes https://github.com/OpenSC/OpenSC/pull/1307
fixes https://github.com/OpenSC/OpenSC/issues/1297
2018-04-05 15:23:16 +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
Doug Engert 4222036a60 PIV completely separate piv_match_card from piv_init
In order to satisfy some concerns over the use of <card>_match_card
and <card>_init, this modification will do that at the cost of additional
overhead of repeating some card commands.

Hopefully this commit will not be needed.

 On branch piv-aid-discovery
 Changes to be committed:
	modified:   card-piv.c
2018-02-28 13:49:14 -06:00
Doug Engert aee62c7c67 Requested changed to not rely on success *_match_card being followed by *_init
As requested and as the alternative solution see:
https://github.com/OpenSC/OpenSC/pull/1256#issuecomment-365319444

In order to not pass a card lock and the card->drv_data from piv_match_card
piv_match_card is split in 2 parts.

the piv_match_card_continued is called from piv_init. piv_init may
now return with SC_ERROR_INVALID_CARD to single to sc_connect_card to look
for additional drivers.

Cosmetic change to indicate neo_version is really a Yubico version.
Change wording on the comments when setting card_issues.

 On branch piv-aid-discovery

 Changes to be committed:
	modified:   src/libopensc/card-piv.c
2018-02-22 09:48:43 -06:00
Doug Engert efe7eb598f Some CAC / PIV cards do not support Discovery Object
Some CAC card return '6A80` Incorrect parameters in APDU when trying to
read the Discovery object. If it fails other then not found, then we can
not use the Discovery object to test for the active AID.

The test is done in piv_match_card just after doing a SELECT AID for the PIV.
and set CI_DISCOVERY_USELESS if needed. piv_card_reader_lock_obtained will
then not use the Discovery object.

Some older PIV cards, prior to the introduction of the PIV
Discovery and History objects, may get errors trying to read them.
Ignore these errors too.

Remove comment and remove code to check verify Lc=0 as requested in:

https://github.com/OpenSC/OpenSC/pull/1256#pullrequestreview-96124443

They can easily be added back in.

 On branch piv-aid-discovery

 Changes to be committed:
	modified:   src/libopensc/card-piv.c
2018-02-22 09:40:42 -06:00
Doug Engert 27add2ee3c Inform pkcs15 and card drivers of PKCS#11 C_Login(CKU_CONTEXT_SPECIFIC)"
Framework-pkcs15.c will now set pin_info->auth_method to SC_AC_CONTEXT_SPECIFIC

iso7816.c iso7816_build_pin_apdu treats this the same as SC_AC_CHV

card-piv.c piv_pin_cmd sets priv->xcontext_specific=1 and calls sc_lock before
the verify command. If the verify fails sc_unlock is called.
Later after the next card command returns, if priv->context_specific==1 piv_check_sw
will call sc_unlock as the application may not have requested the crypto but
some other command.

Some additional calls to sc_lock and sc_unlock have been added to make sure
PIV internal command sequences including the crypto command ('87') and any get
responses are always protected by a lock.

This guarantees the card is locked for verify and the next command
which should be the crypto operation. The PIV card also inforces this restriction
on the card.

This is based on suggestions in:
://github.com/OpenSC/OpenSC/pull/1256#issuecomment-361975751

 On branch piv-aid-discovery

 Changes to be committed:
	modified:   src/libopensc/card-piv.c
	modified:   src/libopensc/iso7816.c
	modified:   src/libopensc/types.h
	modified:   src/pkcs11/framework-pkcs15.c
2018-02-22 09:37:46 -06:00
Doug Engert 3fea6b7927 PIV detection of AID using Discovery Object before doing select AID
Many OpenSC drivers try and detect during match if the card supports
their AID by doing a SELECT FILE for the AID.

But this can cause problems with cards such as Yubico that do not ignore
SELECT AID commands for applications they do not support. Other cards may
have the same problems. Selecting the wrong AID can also lose the security
state.

The card-piv.c will now uses the GET DATA to read the PIV Discovery Object '7E'
which is a ISO standard template that will contain the AID of the currently
active application. The driver will then double check that the template is
for the PIV application.

If the template contains the PIV AID, then no SELECT AID is done.
PIV standards say there can only be one PIV application on a card.
PIV standards also say PIV must be the the default application,
but Yubico does not follow this.

The command fails only then will a SELECT AID be done.

Thus this can avoid the Yubico problem.

This logic is used in both "match" and in the piv_card_reader_lock_obtained
routine.

Additional logic was in piv_card_reader_lock_obtained was added to handle
when the card reset was received by some other program. Multiple programs
may be trying to use the PIV application on the card, and thus multiple
programs will all receive that the card was reset. The first program to receive
the card was reset will do all of the above logic, and may leave the card in
a state will cause other programs to not  have to do much at all.

 The intent of all of this is to avoid sending extra commands to the card
 including SELECT AID that could change the card state when not needed.

 On branch piv-aid-discovery
 Changes to be committed:
	modified:   card-piv.c
2018-02-22 09:37:46 -06:00
Doug Engert 8cc0c3911a Yubico PIV application fixed CI_VERIFY_LC0_FAIL in version 4.3.2
Also add PIV card types to  sc_pkcs15_is_emulation_only

 On branch piv-aid-discovery

 Changes to be committed:
	modified:   src/libopensc/card-piv.c
	modified:   src/libopensc/pkcs15-syn.c
2018-02-22 09:33:30 -06:00
Frank Morgner 88175e35d3 PIV: use better long name 2018-02-07 11:57:48 +01:00
Frank Morgner d719977d14 Reselect PKI-Applets after card reset
PKI-Applets may not be active if the card has been reset or unpowered.
The SELECT command used to activate the applet, is identical to the one
used during card matching or initialization.
2018-02-07 11:57:48 +01:00
Frank Morgner 44447b7801 fixed warnings about uninitialized data 2018-01-17 00:28:42 +01:00
Frank Morgner 1d03000222 fixed dead assignment 2017-11-09 12:42:29 +01:00
Frank Morgner 4ea2828246 card-piv: Fix Thunderbird and SC_PIN_CMD_GET_INFO
Fixes https://github.com/OpenSC/OpenSC/issues/1071
Closes https://github.com/OpenSC/OpenSC/pull/1072
2017-07-04 10:16:41 +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
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
Maciej S. Szmigiero b646a306dc Print size_t variables on properly on Windows
OpenSC used SUSv3 "z" printf length modifier for printing size_t variables,
however this modifier is not available on Windows ("I" must be used
instead), at least for now.

Introduce SC_FORMAT_LEN_SIZE_T define for that purpose and convert existing
code to use it when printing size_t variables.

This define can't go into libopensc/internal.h since tools use it, too.

Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
2017-03-27 10:57:21 +02:00
Michał Trojnara 645780e6d4 NULL parameter check moved to sc_file_free()
This fixes numerous issues where the check is *not* performed,
and also simplifies the code.
2017-01-10 12:46:44 +01: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
Doug Engert bb2d863e4f piv: add a piv_card_reader_lock_obtained function
When sc_lock obtains a reader lock  this function is called
If the card was reset the PIV AID is seletcted and logged_in is reset.
This is need for some PIV cards where the default AID is not the PIV AID
and some other process has reset the card.

closes #842
2016-08-15 13:34:50 +02:00
Viktor Tarasov eb21c41e53 piv: coding style, use short log calls 2016-07-24 20:40:58 +02:00
Doug Engert 9c7f67309f piv: logout place holder
Until a full logout function can be implemented, return SC_ERROR_NOT_SUPPORTED

closes #832
2016-07-24 20:40:44 +02:00
Viktor Tarasov cee092a930 piv: change driver's short name to 'PIV-II'
closes #828
2016-07-19 15:27:26 +02:00
Doug Engert edf24d0e2e PIV and PIV-Want-To-Be Issues
Not all PIV cards follow the  NIST 800-73-3 standard. This commit is designed to address some
of the issues.  OpenSC developers don't have access to all the different versions of devices
or access to release notes for the devices to see when a bug was introduced and when it is fixed.

To make OpenSC code changes easier,  the code is divided into four sections:

(1) Identify the card/token as best possible by looking at the "Historical bytes" in the ATR.
For the Yubico devices read their version number and log it via sc_debug.

(2) Define the card_issues  CI_* defines in card-piv.c. There are 8 of them at the moment.
See below.

(3) based on the card->type and possibly Yubico version set the priv->card_issues flags that
apply to  current card or device.

(4) Implement in the code changes needed for each issue.

Other issues can be added. As more info is obtained (3) can be updated using the version
number as needed.

The card issues are:

CI_VERIFY_630X - VERIFY "tries left" returns 630X rather then 63CX

CI_VERIFY_LC0_FAIL - VERIFY Lc=0 never returns 90 00 if PIN not needed. Will also test after
first PIN verify if protected object can be used instead

CI_CANT_USE_GETDATA_FOR_STATE - No object to test verification in place of VERIFY Lc=0

CI_LEAKS_FILE_NOT_FOUND - GET DATA of empty object returns 6A 82 even if PIN not verified

CI_OTHER_AID_LOSE_STATE - Other drivers match routines may reset our security state and lose AID

CI_NFC_EXPOSE_TOO_MUCH - PIN, crypto and objects exposed over NFS in violation of 800-73-3

CI_NO_RSA2048 - does not have RSA 2048

CI_NO_EC384 - does not have EC 384

The piv_card_match and piv_init interactions were cleaned up.

 Changes to be committed:
	modified:   card-piv.c
	modified:   cards.h
2016-07-19 15:17:28 +02:00
Frank Morgner 9f0087d968 fixed missing includes 2015-10-30 18:21:40 +01:00
Doug Engert 65bc754b8b Do not expose access to PIV emulated files from utilities
PIV cards uses get/put data not select file and read_binary.
To allow access via pkcs15 emulation card-piv.c emulates
select_file and read_binary but only when used with the path as
created by the piv emulation.

There are no MF.DIR or ED.DIR files.
opensc-tool and opensc-explorer will not work with this emulation.

Patch removes code that caused problems with opensc-tooland opensc-explorer.
2015-10-14 15:27:58 -05:00
Doug Engert 0b268f789a Allow PIV driver to use cards where default application in not PIV
card-piv.c was not selecting the PIV AID correctly from piv_find_aid.
This cause a CAC card that also has the PIV application to fail a VERIFY command
of the pin would use a VERIFY  APDU P2 where P2 for PIV is 80, but for CAC was 00.

A CAC card could work if the caller requested the serial number of the card
which did call piv_select_aid. All the OpenSC tools, minidriver and
PKCS#11 do this, but Tokend does not.

This is a partial fix for https://github.com/OpenSC/OpenSC/issues/570.
Tokend in later MacOS versions still has other issues.

A  more complete solution is needed for cards with multiple applications.

I do not have a CAC card or MAC to do any testing.

Thanks to https://github.com/mouse07410 who has a CAC card, and a Mac,
and has tested this fix.
2015-10-11 19:14:02 -05:00
Frank Morgner 7120a9b549 Merge pull request #554 from frankmorgner/fixes
Some more fixes for problems reported by Coverity scan
2015-09-25 11:13:17 +02:00
Doug Engert 24a3999386 Fix indentation for readability 2015-09-17 19:03:44 -05:00
Frank Morgner be073396be Fixes warnings about unused variables/functions 2015-09-17 22:24:33 +02:00
Frank Morgner 59254d9d88 Checks on errors for ftell and fseek 2015-09-17 22:24:33 +02:00
Nicholas Wilson 2897e6fb5c Leniently interpret the ISO7816 return codes in card-piv.c
This adds support for the Yubikey NEO. I'm not sure whether it breaks
the specification, or follows some other version of the spec, but in my
testing it returns SW1=0x63, SW2=0x0N for N PIN tries remaining.
Ignoring the top nibble seems a harmless change to the behaviour to
support this device.
2015-08-25 15:53:32 +01:00
Doug Engert c7af08c68a PIV - read just length of object to get size
card-piv.c tries to read the first 8 bytes of an object to get object size
so it can allocate a buffer. It then reads the whole object. apdu.c has changed
over the years, and apdu.c will keep reading as long as the card returns
status of 61 XX  thus apdu.c will read the whole object while discarding
the extra data and returning to the caller only the first part of the data.
This in effect causes a double read of objects.

This patch sets SC_APDU_FLAGS_NO_GET_RESP to tell apdu to stop doing the
extra get-response commands thus avoiding most of the extra overhead.

This in not an optimal patch as it only works with T=1 cards/readers
but the patch is confined to just card-piv.c.
A better patch is in the works.

Fixes #462
2015-05-13 09:24:46 +02:00
Viktor Tarasov 3e0356b170 register CKM_ECDSA and CKM_ECDSA_SHA1 depending on card capabilities
fix #429
2015-05-08 20:45:56 +02:00
Philip Wendland 4142456c74 PIV, sc-hsm, myeid: register ECDH card capabilites
Prior to 066132327c71300188aa66180fde2fb3d90c5140, CKM_ECDH1_DERIVE and
CKM_ECDH1_COFACTOR_DERIVE were always registered for cards that support
SC_ALGORITHM_ECDSA_RAW.
The mentioned commit changed this behavior, so that the ECDH mechanisms
are only registered for cards that set the SC_ALGORITHM_ECDH_CDH_RAW
capability flag.
To keep the existing behavior for the cards, they need to set this flag
in the card driver.
2015-05-08 20:45:37 +02:00
Frank Morgner bcb5fc15e5 honour HAVE_CONFIG_H 2015-04-22 23:55:33 +02: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
Doug Engert d7475c8180 Add brackets in card-piv.c 2015-02-16 10:43:19 -06:00
Doug Engert 981a0fbbaf Fix-up changes caused by Coverity scan
piv-tool.c add  /* fall through */ to avoif false warning
card-piv.c - clean up if sc_lock fails.
2015-02-11 13:39:59 -06:00
Frank Morgner 6759c04b26 don't ignore errors 2015-01-28 04:45:08 +01:00
Frank Morgner 2e04fa99c1 fixed pointless array comparisons 2015-01-28 07:39:35 +01:00
Doug Engert 4dbfba3102 Use correct bit when writing PIV certificate object with gzipped certificate
NIST 800-73 clarified the CertInfo flag for gzipped certificate is 0x01
piv-tool was writting 0x80.
OpenSC card-piv.c continues to accept both.
2014-11-07 19:49:36 -06:00
Doug Engert cb89a870ae Merge pull request #280 from shootingatshadow/aes-support
Remove hardcodes from Mutual Authenticate
2014-09-18 16:32:21 -05: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
William Roberts 3a0ca5aa7d Remove hardcodes from Mutual Authenticate
Support nonces that are not only 8 bytes in
Mutual Authenticate. Use the witness length
to determine the nonce size, thus existing
systems using 8 bytes will continue to use 8
bytes. However, with AES 256, the nonces could
be a single block size of 16 bytes or greater.
2014-09-03 10:53:30 -07:00
William Roberts 295c523e4e Add AES support for PIV General Authenticate
This adds algorithm IDs 0xA, 0xA, 0xC which as documented
by the NIST PIV specification is algorithms AES-128, AES-192
and AES-256 respectively.

This patch also addresses some of the hardcodes that prevented
nonces greater than the single byte TLV length tags would allow.
It was explicitly tested with AES-256 and 256 byte nonces.

Signed-off-by: William Roberts <w2.roberts@samsung.com>
2014-08-25 18:27:13 -07:00
Frank Morgner a64326e768 fixed compiler warnings (partially submitted)
Signed-off-by: Frank Morgner <morgner@informatik.hu-berlin.de>

PR-222: commit 0b567dbaa8
partially submitted by Viktor Tarasov
2014-05-03 21:47:15 +02:00