Code sample: Implementing a class to represent a type of smart card
package com.rim.samples.device.smartcard;
import net.rim.device.api.smartcard.*;
import net.rim.device.api.util.*;
import net.rim.device.api.crypto.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;
/*****************************************************************************
* This class represents a kind (or model or family) of physical smart card.
* There should only be one instance of this class in the system at a time. The
* instance is managed by the SmartCardFactory.
******************************************************************************/
public class MyCryptoSmartCard extends CryptoSmartCard implements Persistable
{
private final static byte MY_ATR [] = {
(byte)0x3b, (byte)0x7d, (byte)0x11,
(byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x31, (byte)0x80, (byte)0x71,
(byte)0x8e, (byte)0x64, (byte)0x86,
(byte)0xd6, (byte)0x01, (byte)0x00,
(byte)0x81, (byte)0x90, (byte)0x00 };
private final static AnswerToReset _myATR = new AnswerToReset( MY_ATR );
private static final String LABEL = "RIM Sample";
private static final String DISPLAY_SETTINGS = "Show driver settings now";
private static final String RSA = "RSA";
/*************************************************************************
* The libMain() method is invoked when the BlackBerry device starts and
* registers this smart card driver with the smart card factory.
*
* Registering this smart card driver with the smart card factory
* automatically registers the driver with the user authenticator framework
* which allows the smart card to be used as a second factor of
* authentication for unlocking a BlackBerry device.
*
* In the BlackBerry development environment, in the settings for the smart
* card driver project, set the project type to Library and select the
* auto-run on startup' setting.
**************************************************************************/
public static void libMain( String args[] )
{
try
{
SmartCardFactory.addSmartCard( new MyCryptoSmartCard() );
}
catch(ControlledAccessException cae)
{
// Application control may not allow your driver to be used with the
// user authenticator framework, in which case it will throw a
// ControlledAccessException.
// Your driver is registered with the smart card API framework and
//can still
// be used for importing certificates and signing/decrypting messages.
}
}
/********************************************************
* Retrieve the session handler for this smart card.
* Implementations of this method should not include any UI.
*********************************************************/
protected SmartCardSession openSessionImpl( SmartCardReaderSession readerSession )
throws SmartCardException
{
return new MyCryptoSmartCardSession( this, readerSession );
}
/**************************************************************************
* Determine if the system should use this smart card object
* to communicate with a physical smart card that has the given AnswerToReset.
* The system invokes this method to determine which smart card driver
* implementation it should use to communicate with a physical smart card
* found in a BlackBerry Smart Card Reader.
**************************************************************************/
protected boolean checkAnswerToResetImpl( AnswerToReset atr )
{
/***********************************************************************
* If this method returns false, the cryptographic smart card driver will
* not be used to perform additional operations on a particular smart card.
* This method should only return true if you support the specified ATR.
*
* If this method returns true, but there is no support for the smart card
* that corresponds to the ATR,other smart card drivers may stop working
* properly.
*
* The AnswerToReset argument contains the full ATR from the smart card.
* Your implementation of this method may check the entire ATR or just
* parts of the ATR, as long as the driver supports the corresponding
* smart card.
************************************************************************/
return _myATR.equals( atr );
}
/**************************************************************************
* Retrieve the label associated with this smart card.
* The string you return should not include the words "smart card", as the
* system uses this method to generate strings such as
* "Please insert your 'label' smart card".
**************************************************************************/
protected String getLabelImpl()
{
return LABEL;
}
/*******************************************
* Retrieves this smart card’s capabilities
*******************************************/
protected SmartCardCapabilities getCapabilitiesImpl()
{
return new SmartCardCapabilities( SmartCardCapabilities.PROTOCOL_T0 );
}
/**********************************************************
* Indicate whether this smart card can display its settings.
**********************************************************/
protected boolean isDisplaySettingsAvailableImpl( Object context )
{
return true;
}
/**************************************************************************
* Display this smart card’s settings.
* This method will be invoked from the smart card options screen when
* the user selects the driver and chooses to view the settings of that
* driver.
*
* This method could be called from the event thread. The driver should not
* block the event thread for long periods of time.
*
* The context parameter is reserved for possible future use.
**************************************************************************/
protected void displaySettingsImpl( Object context )
{
Dialog.alert( DISPLAY_SETTINGS );
}
/**********************************************************
* Retrieve the algorithms supported by this smart card.
* You must return one or more of “RSA”, “DSA”, or “ECC”.
**********************************************************/
public String[] getAlgorithms()
{
return new String [] { "RSA" };
}
/*********************************************************************
* Retrieve a crypto token that supports the given algorithm.
* The argument algorithm is the name of the algorithm.
* Return the Crypto token supporting the named algorithm.
*
* Throw NoSuchAlgorithmException if the specified algorithm is invalid.
* Throw CryptoTokenException if there is a token-related problem.
**********************************************************************/
public CryptoToken getCryptoToken( String algorithm )
throws NoSuchAlgorithmException, CryptoTokenException
{
if ( algorithm.equals( RSA ) )
{
return new MyRSACryptoToken();
}
throw new NoSuchAlgorithmException();
}
}
Next topic: Display cryptographic smart card driver options
Previous topic: Code sample: Implementing a class to enable a communication session with a smart card