Calypso Application User Guide


Overview

Keyple provides a card extension add-on dedicated to the Calypso® card technology.

This component allows operating commands with a Calypso card and to manage a secure Calypso transaction in a simple way. It completely hides the details of APDU orders that are sent to Calypso cards and SAMs, which are usually tedious operations.

The main features are:

  • support for different card revisions;
  • object mapping of card data structures;
  • complete management of the secure session with SAMs;
  • PIN code management;
  • Stored Value operations management;
  • card invalidation / rehabilitation.

The diagram below illustrates the organization of a Calypso application based on Keyple:


Operating mode

Pre-requisites:

  • Have a global view of Calypso product concepts (cards, SAM, security principles)
  • Have read the Standalone Application User Guide to understand the main concepts of Keyple in a standalone application
  1. Access to the Calypso card extension service
  2. Select a card
  3. Set up the security settings (optional)
  4. Operate a card transaction

The Calypso extension service

As part of the Calypso card extension add-on, the Calypso extension service is the provider of the API implementations.

The service is accessible by calling the CalypsoExtensionService.getInstance() static method.

CalypsoExtensionService calypsoExtensionService = CalypsoExtensionService.getInstance();

During initialization, it is recommended to check the extension with the smart card service to ensure the compatibility of the different libraries involved.

In case of incompatibility a warning will be produced in the log file.

smartCardService.checkCardExtension(calypsoExtensionService);

Select a card

In order to perform a transaction it is necessary to have selected the card first.

To do this, you must create a selection case for a selection scenario by calling the createCardSelection() method.

In addition to the filtering capabilities offered by Keyple Service, the Calypso Selection API allows you to add commands that will be sent to the card after a successful selection (the details of these features are described in the API documentation).

The resulting IsoSmartCard can be cast to a CalypsoCard object which concentrate all known information about the card being processed. Its content is dynamically updated during the transaction. The application will use it to get the data necessary for its business logic.

// Create a card selection manager.
CardSelectionManager cardSelectionManager =
    smartCardService.getReaderApiFactory().createCardSelectionManager();

// Create a card selection using the Calypso card extension.
cardSelectionManager.prepareSelection(
    smartCardService.getReaderApiFactory()
        .createIsoCardSelector()
        .filterByDfName(AID),
    calypsoExtensionService.getCalypsoCardApiFactory()
        .createCalypsoCardSelectionExtension());

// Actual card communication: process the card selection.
CardSelectionResult cardSelectionResult =
    cardSelectionManager.processCardSelectionScenario(cardReader);

// Get the SmartCard resulting of the selection.
CalypsoCard calypsoCard = (CalypsoCard) cardSelectionResult.getActiveSmartCard();

// Check the selection result.
if (calypsoCard == null) {
    throw new IllegalStateException("The card selection failed.");
}

Set up security settings

The security settings must be initialized only for secure transactions.

The API offers several types of settings such as choosing the SAM to use, enabling various modes, specifying keys for legacy cards, etc… (see the API documentation for more information).

If the card transaction is to be secured using a symmetrical key cryptographic module (such as a SAM), it will be necessary to initialize a SymmetricSecuritySetting, associated with an implementation of the cryptographic module to be used (e.g. Calypso Crypto Legacy SAM Lib).

The SAM must first be selected via the Calypso Crypto Legacy SAM Lib.

In the case of the Card Resource Service, you have to create a profile extension, specifying the previously built selection case, and then associate it to a dedicated profile in the service (see the Card Resource Service User Guide).

The following snippet shows the selection of the SAM and the initialization of the security settings:

// Create a SAM selection manager.
CardSelectionManager samSelectionManager = readerApiFactory.createCardSelectionManager();

// Create a card selector without filer
CardSelector<IsoCardSelector> cardSelector = readerApiFactory.createIsoCardSelector();

// Retrieve the Legacy SAM API factory
LegacySamApiFactory legacySamApiFactory =
    LegacySamExtensionService.getInstance().getLegacySamApiFactory();

// Create a SAM selection using the Calypso Legacy SAM card extension.
samSelectionManager.prepareSelection(
    cardSelector, legacySamApiFactory.createLegacySamSelectionExtension());

// SAM communication: run the selection scenario.
CardSelectionResult samSelectionResult =
    samSelectionManager.processCardSelectionScenario(reader);

// Check the selection result.
if (samSelectionResult.getActiveSmartCard() == null) {
  throw new IllegalStateException("The selection of the SAM failed.");
}

// Get the Calypso SAM SmartCard resulting of the selection.
LegacySam sam = (LegacySam) samSelectionResult.getActiveSmartCard();

// Build the security settings
symmetricCryptoSecuritySetting =
    calypsoCardApiFactory.createSymmetricCryptoSecuritySetting(
        LegacySamExtensionService.getInstance()
            .getLegacySamApiFactory()
            .createSymmetricCryptoCardTransactionManagerFactory(samReader, sam));


Operate a card transaction

It is possible to perform secure or non-secure transactions depending on the need. A transaction is managed by a dedicated CardTransactionManager which is provided by the Calypso card extension service.

The transaction manager provides high-level API to manage transactions with a Calypso card. The provided CalypsoCard object is kept and updated dynamically all along the transaction process.

The transaction takes place in several repeatable steps:

  • Preparation of the commands to be sent to the card. Several command preparations can be stacked (no communication neither with the card nor with the SAM).
  • Processing of the prepared commands. Performs all necessary communications with the card and/or the SAM to carry out the previously prepared operations.
// Execute the transaction: the environment file is read within a secure session to ensure data
// authenticity.
calypsoCardApiFactory
    .createSecureRegularModeTransactionManager(cardReader, calypsoCard, symmetricCryptoSecuritySetting)
    .prepareOpenSecureSession(WriteAccessLevel.DEBIT)
    .prepareReadRecords(SFI_ENVIRONMENT_AND_HOLDER, 1, 1, 29)
    .prepareCloseSecureSession()
    .processCommands(ChannelControl.CLOSE_AFTER);

Specific security measures

Legacy card authentication

In order to securely manage legacy card solutions configured with DES or DES-X keys, it is imperative to exchange with these cards in secure sessions only APDU commands for which the size of the response data can be predetermined.

For a Keyple-based processing, this means that the following methods of the TransactionManager must not be used within a secure session: prepareReadRecord(...), prepareReadRecordsPartially(...), prepareSearchRecords(...) and prepareGetData(...). The only exception is prepareGetData(GetDataTag.FCI_FOR_CURRENT_DF), which is allowed within a secure session since the size of its response data is known.

As these methods could be useful for high-performance identification of some card data, here’s how to use them safely on legacy cards:

  • first of all, exchange these commands before opening a secure session: this enables effective identification of the card data,
  • then, if any of the data read out of session is sensitive and requires authentication, repeat a secure read of the sensitive data within the session, using commands that predetermine the size of the response data.

HCE service handling

In the context of access control, it is necessary to check the validity of the token of a Calypso HCE application in order to accept and authenticate card solutions based on an HCE-type NFC service.

That validity is carried by the SN Header, the two most significant bytes of the serial number of such an application. It encodes the end of validity of the token, as a number of half-days elapsed since 1 January 2010 at 0:00 local time, and is not significant when equal to 0. What that validity materialises depends on the cryptographic nature of the transaction: in symmetric-key mode it is the end of validity of the debit key; in PKI mode it is the expiry date of the key carried by the card certificate. Nothing in the card dialogue reveals an outdated header: in symmetric-key mode the header takes part in the diversification of the keys, so a mobile whose validity window has passed still authenticates normally, and in PKI mode the expiry is attested by the card certificate and is not signalled by the card either. The check therefore belongs to the terminal.

Version 3 of the Terminal APIs will handle that validity directly, the application only providing the time reference it trusts.

  • The application implements the HceTimeProvider SPI, whose single operation getCurrentLocalEpochSeconds() returns the current time in the same reference as the SN Header, that is local time, as a number of seconds elapsed since 1 January 1970 at 0:00 in that same reference. In Java, this is LocalDateTime.now().toEpochSecond(ZoneOffset.UTC), and not Instant.now().getEpochSecond(), which would be counted in UTC and would make an expired token appear valid for the duration of the time zone offset.
  • The provider is registered once on the factory, with setHceTimeProvider(provider). It is consulted when the selection response is parsed, by the environment holding the Calypso card extension: in a distributed architecture, that is the client when it embeds the extension, and the server when the client is a thin one.
  • The card extension then decodes the header, converts it and compares it. The result is exposed by the card image: isHceTokenValid() returns the verdict, or null when the application is not HCE, when its header is not significant, or when no provider was registered; getHceTokenExpiryLocalEpochSeconds() returns the expiry itself, ready to be displayed to the holder.
  • A card whose token has expired is still selected: the ticketing layer decides what to do, refuse the service, invite the holder to renew the token, or simply log the event.
  • Opening a secure session with such a card is however forbidden, whatever the session mode and the write access level, unless the security settings explicitly allow it with authorizeExpiredHceToken().
// Provide the time reference in which the HCE tokens were provisioned
calypsoCardApiFactory.setHceTimeProvider(
    () -> LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));

[...]

// After the selection, the verdict is carried by the card image
Boolean hceTokenValid = calypsoCard.isHceTokenValid();
if (Boolean.FALSE.equals(hceTokenValid)) {
  // Expired token: the card is selected, the decision belongs to the ticketing layer
  long expiry = calypsoCard.getHceTokenExpiryLocalEpochSeconds();
  [...]
}

This mechanism comes with version 3 of the Terminal APIs. Until Keyple is aligned with them, use the manual extraction described below.

Manual extraction (alternative)

Until the above is available, and for applications that stay on the current versions, the two bytes of the SN Header can be obtained from the response to the Select Application command. This approach leaves the decoding, the comparison and the protection of the secure session entirely to the application, and will no longer be the recommended one.

import org.eclipse.keyple.core.util.BerTlvUtil;

[...]

    if (calypsoCard.isHce()) {
      byte[] snHeader = extractSnHeader(calypsoCard.getSelectApplicationResponse());
      // Check the validity for the current
      [...]
    }

[...]

private static byte[] extractSnHeader(byte[] selectApplicationResponse) {
    byte[] fullSerialNumber = BerTlvUtil.parseSimple(selectApplicationResponse, true).get(0xC7);
    return Arrays.copyOf(fullSerialNumber, 2);
}

[...]

Card revocation

An issuer may withdraw a card from service — one reported lost or stolen, one that is the subject of a dispute, one whose holder’s entitlement has ended. Nothing in the card records that decision: it lives in a list the issuer maintains, outside the card, so the check belongs to the terminal.

Version 3 of the Terminal APIs performs it, the application only providing the list it trusts:

  • the application implements the CardRevocationStatusProvider SPI and registers it once on the factory, with setCardRevocationStatusProvider(...). It is consulted while the card is in front of the reader, so it must answer from an in-memory structure or a local cache, never through a blocking remote call,
  • a revoked card is still selected: isRevoked() returns the verdict and the ticketing layer decides what to do, refuse the service, raise an alert, or simply log the event,
  • opening a secure session with such a card is forbidden, whatever the session mode and the write access level, unless the security settings explicitly allow it with authorizeRevokedCard().
The check is fail-closed, including when no provider is registered. Absent a CardRevocationStatusProvider, the status counts as unverified and no secure session can be opened until authorizeRevokedCard() is called. Every application will therefore have to state one or the other when Keyple is aligned with version 3: provide a revocation list, or explicitly accept cards whose revocation status is unknown.

Ratification mechanism

In contactless mode, the card may be removed before the terminal has received the acknowledgement of the Close Secure Session command: the terminal then cannot know whether the card has actually committed the modifications made during the session.

The ratification mechanism handles this early card removal. It is enabled on the security settings by calling enableRatificationMechanism() (disabled by default):

  • a ratification command is added after the closing command in contactless mode,
  • the card considers the secure session ratified only once it has processed that command: if the card is removed beforehand, the session stays unratified in the card, which thus keeps the proof that the terminal may not have been informed of the outcome,
  • on cards where ratification happens on deselect, no ratification command is required (see CalypsoCard.isRatificationOnDeselectSupported()).

When a new secure session is opened, CalypsoCard.isDfRatified() tells whether the previous session has been ratified, allowing the terminal to detect an interrupted transaction and to handle its resumption.

// Build the security settings with the ratification mechanism enabled
symmetricCryptoSecuritySetting =
    calypsoCardApiFactory
        .createSymmetricCryptoSecuritySetting(
            LegacySamExtensionService.getInstance()
                .getLegacySamApiFactory()
                .createSymmetricCryptoCardTransactionManagerFactory(samReader, sam))
        .enableRatificationMechanism();

Whether to enable the mechanism depends on which terminal resumes an interrupted transaction:

  • enable it when the resumption may be handled by another terminal, since the proof of the interruption is then carried by the card itself. Typical case: several gates on the same access line of a network,
  • leave it disabled when the resumption is necessarily handled by the same terminal, which keeps the context of the interrupted transaction. Typical case: a remote ticket reloading server.
The ratification mechanism can only be used within the anti-passback delay defined by the transport operator: beyond this delay, an unratified session can no longer be handled as the resumption of an interrupted transaction.

API


Examples


Download


Previous
Next