Commit Graph

259 Commits

Author SHA1 Message Date
Peter Marschall 738c933609 OpenPGP: a few cleanups for Gnuk
* make sure to remain within buffer size in pgp_read_blob()
* make logic more readable in pgp_get_data()
2018-12-17 09:49:57 +01:00
Frank Morgner 24b50a4277 replace sc_debug with sc_log 2018-12-06 09:26:42 +01:00
Peter Marschall 550665b906 OpenPGP: refactor pgp_get_card_features()
Use pgp_parse_alog_attr_blob() to get the algorithm attribute DO's contents.
2018-10-10 14:52:29 +02:00
Peter Marschall 8a564107a8 OpenPGP: introduce gpg_parse_algo_attr_blob()
Introduce a central function to parse the algorithm atributes in DOs C1 - C3.
2018-10-10 14:52:29 +02:00
Peter Marschall 248ece23c6 OpenPGP: bail out on non-RSA key generation/import
Also add the necessary algorithm info where necessary.
2018-10-10 14:52:29 +02:00
Peter Marschall c2f02f72bd OpenPGP: adapt data structures to support RSA alternatives
* update callers to use the adapted structures.
2018-10-10 14:52:29 +02:00
Peter Marschall 772d20969a OpenPGP: first steps to support key types beyond RSA
- rename 'keytype' in some OpenPGP-specific types to 'key_id'
  because they key ID was what the field was used for
- introduce field 'algorithm' in the structures above
  to indicate the key's algorithm: RSA, ...
- define constant SC_OPENPGP_KEYALGO_RSA and use it
- rename constants SC_OPENPGP_KEYFORMAT_* to SC_OPENPGP_KEYFORMAT_RSA_*
  because they are RSA specific
2018-10-10 14:52:29 +02:00
Peter Marschall f1ae31aea4 OpenPGP: expose additional algorithms only with EXT_CAP_ALG_ATTR_CHANGEABLE
List additional algorithms & attributes as supported only when the card
supports changing the algorithms attributes DOs and exposes this by having
the EXT_CAP_ALG_ATTR_CHANGEABLE capability set.

Using different algorithms and attributes requires changing the algorithm
attributes DOs. If that is not supported - as indicated by a missing
EXT_CAP_ALG_ATTR_CHANGEABLE capability - then only those algorithms
described by the current algorithms attributes DOs' contents can be used.

In addition simplify setting the flags.
2018-10-10 14:52:29 +02:00
Peter Marschall 44d6116c59 OpenPGP: slight cleanups
* use variables if they are already there
* be a bit more explicit in logging
* more consistent tag format: %04X
* cleanup flag setting for _sc_card_add_rsa_alg()
2018-10-10 14:52:29 +02:00
Frank Morgner 3750d70106 pgp: detect gnuk with newer fw
closes https://github.com/OpenSC/OpenSC/issues/1475
2018-09-13 13:46:27 +02:00
alex-nitrokey 083c18045e Make deciphering with AUT-key possible for OpenPGP Card >v3.2 (fixes #1352) (#1446) 2018-09-06 10:57:23 +02:00
alex-nitrokey 748234b7cc Fix SM algorithm in extended capabilities 2018-09-05 23:42:14 +02:00
alex-nitrokey b572b383b2 Add supported algorithms for OpenPGP Card (Fixes #1432) (#1442) 2018-08-31 14:38:14 +02:00
Priit Laes 45f407c021 Mark driver-specific global sc_atr_table structures as const
As most of the drivers do not modify these, we can mark them as const.
Two drivers that we cannot convert are dnie and masktech.

section                 size
.data                  35232 ->   25472
.data.rel.ro           36928 ->   46688
2018-08-22 22:50:30 +02:00
Frank Morgner 30fe0ad453 pgp: fixed integer underflow 2018-08-14 16:13:22 +02:00
Frank Morgner 8fe377e93b fixed out of bounds reads
Thanks to Eric Sesterhenn from X41 D-SEC GmbH
for reporting and suggesting security fixes.
2018-08-14 15:50:13 +02:00
Peter Marschall 1eb8391b4a OpenPGP: slightly re-factor pgp_get_card_features()
* length checks where needed
* more & better comments
2018-07-11 10:47:39 +02:00
Peter Marschall 7332a37abb OpenPGP: add serial number to card name 2018-07-11 10:47:39 +02:00
Peter Marschall 6d6efa2ded OpenPGP: fix FIXME in pgp_new_blob()
Form a correct path instead ofmusising an array of 2 u8's.
Perform proper error checking.
2018-07-11 10:47:39 +02:00
Peter Marschall 215fcdad15 OpenPGP: include detailed version into card name
... for "standard" OpenPGP cards.

This gives more detailed information to the user on the detailed specs
the card adheres to.

In addition it fixes a long-standing annoyance that every standard 2.x
card matching the v2.0 ATR was announced as CryptoStick 1.2.
This ATR is not only used in the CryptoStick 1.2, but also also in
ZeitControl cards as well as NitroKeys, ...
2018-07-11 10:47:39 +02:00
Peter Marschall 2e1b47a79a OpenPGP: improve get_full_pgp_aid()'s parameter checking 2018-07-11 10:47:39 +02:00
Peter Marschall 2a7a6a62fa OpenPGP: limit scope of variable 2018-07-11 10:47:39 +02:00
Peter Marschall 15125b03ab OpenPGP: use LOG_FUNC_CALLED & LOG_FUNC_RETURN symmetrically
To help debugging,
- replace plain return's after LOG_FUNC_CALLED()
  has been called with LOG_FUNC_RETURN()
- use LOG_FUNC_CALLED() & LOG_FUNC_RETURN() pairs more often
2018-07-11 10:47:39 +02:00
Peter Marschall fcecd1bdd2 OpenPGP: update comments on function use: ABI or internal 2018-07-11 10:47:39 +02:00
Peter Marschall 0d6be5db26 OpenPGP: define & set LCS (lifecycle support) as extended capability
Use it in pgp_erase_card() to slightly simplify the code.
2018-07-11 10:47:39 +02:00
Peter Marschall 3af54b2fe0 OpenPGP: harmonize some comments 2018-07-11 10:47:39 +02:00
Peter Marschall 3a59b0a182 OpenPGP: parse "extended length info" DO 7f66 on init 2018-07-11 10:47:39 +02:00
Peter Marschall f73005791c OpenPGP: improve parsing of extended capabilities 2018-07-11 10:47:39 +02:00
Peter Marschall dea5fd9551 OpenPGP: add new DOs introduced with OpenPGP card spec v3.0 & v3.3
For some files spec states CONSTRUCTED, but we treat them as SIMPLE,
because we only need parts of their contents.
2018-07-11 10:47:39 +02:00
Peter Marschall 9dbdf42e9e OpenPGP: update references to specifications 2018-07-11 10:47:39 +02:00
Peter Marschall 14cd6ee39e OpenPGP: clarify meaning of padding byte in pgp_decipher() 2018-07-11 10:47:39 +02:00
Peter Marschall 4323a3d37c OpenPGP: add new DO D5 introduced with OpenPGP card spec v2.1
... and make it accessible for v2.1+ cards
2018-07-11 10:47:39 +02:00
Peter Marschall 266b811e55 compact TLV - add generic sc_compacttlv_find_tag() function (#1374)
* new function sc_compacttlv_find_tag()

Add function sc_compacttlv_find_tag() to search for a tag in a
compact-TLV structure.

* OpenPGP: use sc_compacttlv_find_tag()

While doing so, fix a typo affection OpenPGP v3.x cards
2018-06-08 08:17:13 +02:00
Peter Marschall 8d503df754 OpenPGP: add missing symbolic constants for card versions 2018-05-31 10:16:23 +02:00
Peter Marschall 3e3e478476 OpenPGP: correctly parse hist_bytes
Instead of simply searching for a trigger byte with the risk of
getting garbage, correctly parse historical bytes from ATR as well
as the "historical bytes" DO as compact TLV structures.

In addition
- prepare for additional data used in OpenPGP cards 3.x
- ignore [per the spec] chaining capability fo OpenPGP cards 1.x
2018-05-31 10:16:23 +02:00
Peter Marschall c725c9c2e7 OpenPGP: 1st go at better parsing of hist_bytes
Use hist_bytes from already parsed ATR in reader->atr_info.hist_bytes
instead of the raw ATR from card->atr
2018-05-31 10:16:23 +02:00
Frank Morgner 410cdf0dcc refactored sc_get_challenge
Let sc_get_challenge() do sc_lock() and loop through the card driver's
get_challenge() until enough bytes were collected. The card driver's
get_challenge() now returns the number of bytes collected (less or equal
than requested) or an error code.

- Allow more code re-use.
- PIV driver now uses ASN.1 parser for reading the random bytes
2018-05-23 14:20:28 +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
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
Mouse b7dc5a0890 Retrieve OpenPGP applet version from OpenPGP applet on YubiKey token (#1262) 2018-04-04 21:41:39 +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
Vadim Penzin e1d6f0869b Add missing initialisation of variables to pacify Clang.
1. src/libopensc/card-openpgp.c:pgp_store_key().
2. src/libopensc/cwa14890.c:cwa_verify_icc_certificates().
3. src/libopensc/cwa14890.c:cwa_prepare_external_auth().
4. src/libopensc/pkcs15-itacns.c:itacns_check_and_add_keyset().

(Discovered by Clang 4.0.0 on OpenBSD 6.2.)
2018-03-19 09:48:21 +01:00
Frank Morgner 8ee2c61932 Return SC_SUCCESS in openpgp_card_reader_lock_obtained
prevents locking the card forever in case of a problem
2018-02-12 09:45:36 +01:00
Frank Morgner 50a35c1bae OpenPGP: Workaround for change in Extended Capabilities
The maximum length for sending and receiving data can now be found in DO
7F66. For now, we just use the default values for short/extended length
capabiliites.
2018-02-12 09:45:36 +01:00
Frank Morgner 87cdfcb08c OpenPGP: Implemented "keep alive" command 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 490e825a65 OpenPGP: implemented GET CHALLENGE 2018-01-22 15:26:44 +01:00
Frank Morgner 449f6c2b94 OpenPGP: Added support for PIN logout and status 2018-01-22 15:26:44 +01:00
Frank Morgner 995845b002 openpgp: factory reset is possible if LCS is supported 2018-01-22 15:26:44 +01:00
Frank Morgner fccc42295d OpenPGP: Added basic support for OpenPGP card V3
partially implements https://github.com/OpenSC/OpenSC/issues/1215

Refactored OpenPGP code so that future versions of the card will be
accessed using the logic for OpenPGP V2. We hope that backward
compatibility of the standard will keep the new versions functional.
2018-01-22 15:26:44 +01:00
Frank Morgner 16354f11af OpenPGP: fixed selecting Applet
fixes https://github.com/OpenSC/OpenSC/issues/1230
2018-01-22 15:26:44 +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
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
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
Maciej S. Szmigiero dc476a9f33 Improve handling of OpenPGP card PIN change and unblock commands
"CHANGE REFERENCE DATA" (PIN change) and "RESET RETRY COUNTER"
(PIN unblock) commands in OpenPGP card have various limitations.
These also depend on whether the card is version 1.x or 2.x.

Provide helpful debug messages for user in case he is trying to do
a PIN command in a way that isn't supported by the card.

Also, take into account that version 2.x cards don't support references to
PW1-mode 2 (82) in these commands - change them to PW1 (81).

Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
2016-08-30 23:30:43 +02:00
Maciej S. Szmigiero 3e3528bb68 OpenPGP card doesn't support raw RSA so don't set such algo flag
According to descriptions of commands "PSO: COMPUTE DIGITAL SIGNATURE",
"PSO: DECIPHER" and "INTERNAL AUTHENTICATE" in OpenPGP card spec (versions
1.1 and 2.1.1) the card adds / strips and checks PKCS#1 padding
automatically.
There is no documented way to perform raw RSA operations on this card so
SC_ALGORITHM_RSA_RAW flag shouldn't be set.

Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
2016-08-30 23:30:43 +02:00
Frank Morgner 98b9b37e12 card-openpgp.c: fixed potential resource leak 2015-10-30 12:18:04 +01:00
Peter Marschall a0ca6b2ef0 OpenPGP: set card's version in pgp_init() 2015-10-15 16:43:10 +02:00
Peter Marschall 7078fbd505 OpenPGP: cleanup pgp_init()
- use LOG_FUNC_RETURN instead of return for symmetry with LOG_FUNC_CALLED
2015-10-15 16:43:10 +02:00
Peter Marschall e1db96b73b OpenPGP: extend pgp_match_card()
- explicitly check for supported versions
- log successful matches by AID with their type
- log detection of unsupported OpenPGP-type cards
2015-10-15 16:43:09 +02:00
Peter Marschall 7c2adb1fc8 OpenPGP: 1st preparations for newer versions
- add references to new specs
- add new enum _version constants
2015-10-15 16:43:09 +02:00
Peter Marschall 69b482dce6 OpenPGP: cleanup pgp_get_card_features()
- bug fix: re-initialize index to start searching at the right place
- get rid of unnecessary variable
- add some line breaks & comemnts for easier understanding
2015-10-15 16:42:58 +02:00
Peter Marschall 2a269c5267 OpenPGP: cleanup pgp_build_tlv()
- get rid of intermediate variable
- clarify check in while() loop
- line breaks for easier reading
2015-10-15 16:17:22 +02:00
Peter Marschall 3d492ae376 OpenPGP: cleanup pgp_erase_card()
- get rid of unnecessary variable
- use easily understandable hex representation of APDUs
- auto-calculate APDU length based on hex representation
- restrict scope of some variables
- use sc_log() instead of directly writing to console
- line breaks & comments for easier reading/understanding
- fix typo in log message
2015-10-15 16:17:22 +02:00
Peter Marschall 3d4fbd776d OpenPGP: harmonize coding style
- 2 line breaks between functions for easier reading
- function type on separate line
- mark each function as 'ABI' or 'Internal'
- slightly doxygen-ize comments introducing functions
- fix typos in comments
- break over-long comment lines
- break comment lines at sensible places
- consistent calling style for DRVDATA()

- no code change
2015-10-15 16:17:22 +02:00
Peter Marschall 7ba47fd5a9 OpenPGP: consistently use card->ctx in pgp_put_data_plain()
- get rid of unnecessary variable
- harmonize coding style: adapt to prevailing use of card->ctx
2015-10-15 16:17:22 +02:00
Peter Marschall 1a05f968bd OpenPGP: simplify gnuk_write_certificate()
- get rid of unnecessary variables
- use for-loop to initialize/check/increase run variable in one place
- restrict variables to the necessary scope
- use ternary operator inside a statement instead of if...else...
2015-10-15 16:17:22 +02:00
Peter Marschall d2b1c8228f OpenPGP: set apdu_case on declaration
- use ternary operator to set apdu_case immediately on declaration
  for the GNUK and non-GNUK case
2015-10-15 16:17:22 +02:00
Peter Marschall 3341c5bb8f OpenPGP: make logical structure clearer
- one line per sub-term in the condition of the conditional statement
2015-10-15 16:17:11 +02:00
Frank Morgner 9e500e0b9a fixed bad typecast 2015-10-04 12:52:49 +02: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
Frank Morgner ba3890f8e0 Checks result of calloc 2015-09-17 22:24:33 +02:00
Frank Morgner d20290d2b3 openpgp: match application, not ATR
fixes #391
closes #507
2015-09-16 09:48:23 +02:00
Robert Ou b28c48afe0 Fix OpenPGP driver to work correctly with YubiKey NEO 2015-09-16 09:48:23 +02:00
Nguyễn Hồng Quân a64bbc55aa [OpenPGP] Fix building without OpenSSL. 2015-08-31 22:24:16 +08:00
Nguyễn Hồng Quân 70890a8f61 Merge branch 'master' into gnuk
Conflicts:
	src/libopensc/card-openpgp.c
	src/tools/openpgp-tool.c
2015-08-31 21:55:14 +08:00
Nguyễn Hồng Quân 6409202c2f [OpenPGP] Fix warnings about type conversion. 2015-08-08 14:17:12 +08:00
Nguyễn Hồng Quân d0e3d1be3d Fix tab & spaces 2015-07-04 00:21:38 +08: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
Frank Morgner 5cafbe0f4b fixed undefined shift behaviour 2015-02-04 08:50:19 +01:00
Frank Morgner 2e04fa99c1 fixed pointless array comparisons 2015-01-28 07:39:35 +01:00
Nguyễn Hồng Quân 99b5cb53e1 OpenPGP: Remove unused variables and fix type cast. 2014-12-10 04:01:06 +08:00
george 8d21cea7fc hardcode->defines for DO's 2014-11-11 16:16:15 +01:00
Nguyễn Hồng Quân 901c7952c1 Replace hardcode. 2014-11-09 15:58:40 +07:00
Nguyễn Hồng Quân 7c9bc4d283 OpenPGP: Fix crash after accessing inexistent file. 2014-07-14 23:58:28 +08:00
Nguyễn Hồng Quân c71934af67 OpenPGP: Rename private "blob" type to avoid confusing with variable name.
This name has been used for both data type and variable name of that
type.
2014-07-14 23:58:28 +08:00
Nguyễn Hồng Quân aded490b64 OpenPGP: Use directly binary array of APDUs for ERASE command.
I used a string presentation before and it needed an extra conversion step.
2014-07-14 23:53:44 +08:00
Nguyễn Hồng Quân 7c27cea61f OpenPGP: Make indentation consistent (space -> tab). 2014-07-14 23:53:44 +08:00
Nguyễn Hồng Quân 6aa4896b35 Move declaration to top of block. 2014-07-14 02:02:08 +08:00
Nguyễn Hồng Quân 7ba89893da OpenPGP: Make code neater 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân a42eb5e585 OpenPGP: Correct parameter checking. 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân a1c8c99858 OpenPGP: Delete key as file, for Gnuk. 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân e71906ed23 OpenPGP: Overcome the restriction of even data length of Gnuk.
When write certificate with odd length to Gnuk, we add zero padding to make it even.
2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân cbc53b9a97 OpenPGP: Support write certificate for Gnuk. 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân ebbebb4fa6 OpenPGP: Provide enough buffer to read pubkey from Gnuk. 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân 1df3daeb62 OpenPGP: Read some empty DOs from Gnuk.
In Gnuk, some empty DOs are returned as not exist, instead of existing with empty value.
So, we will consider them exist in driver.
2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân db39041cc1 OpenPGP: Correct building Extended Header List when importing keys. 2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân 3b8f77882b OpenPGP: Support erasing (reset) card.
Command: openpgp-tool --erase
2014-07-14 02:02:07 +08:00
Nguyễn Hồng Quân c81eab5a70 OpenPGP: Include private DO to filesystem at driver initialization.
In old implementation, the DOs which their access is restricted by
PIN (like DOs 0101 -> 0104) were excluded from the fake filesystem,
leading to that we cannot read their data later, even if we verified PIN.
2014-07-14 02:02:06 +08:00