BlackBerry 应用程序开发者指南 第二卷:高级--第14章 增加对智能卡(smart card)的支持 |
|||||||||||||||||||||||
| 作者:佚名 来源:本站整理 发布时间:2008-3-15 2:26:19 | |||||||||||||||||||||||
|
作者:Confach 发表于 2006-04-28 22:28 pm 第14章 增加对智能卡(smart card)的支持
使用智能卡智能卡和信用卡一般大,可以安全的存储和传输敏感数据.BlackBerry设备支持下面的智能卡:
如果你使用的智能卡不是CAC或SageNet智能卡,你也可以利用智能卡的API编写一个BlackBerry设备驱动来支持你的智能卡.智能卡API提供了一个组件库,它们在net.rim.device.api.smartcard 包中,用来和智能卡以及智能卡读卡器进行交互. 对智能卡API恰当实现了的驱动,可以和BlackBerry设备上已经启动的S/MIME一起工作.它包括从智能卡导入认证,以及对卡进行私有键操作(签名和解密消息) 创建一个智能卡驱动为创建一个智能卡驱动,完成下面的步骤: 1. 扩展CryptoSmartCard类,实现一个新的智能卡类. 2. 在新的智能卡里,实现libmain()方法,在启动的时候调用它. 3. 扩展CryptoSmartCardSession类,未智能卡类实现一个新的对话. 4. 扩展Crypto环(token)类,为RSA, DSA, ECC操作实现一个环。 5. 存储私有键文件位置. 实现一个智能卡扩展抽象类CryptoSmartCard.此类的扩展允许子类作为一个智能卡驱动在SmartCardFactory里注册.子类必须实现下面SmartCard和CryptoSmartCard抽象类的方法:
代码实例.下面的实例描述了通过扩展CryptoSmartCard类,如何创建一个智能卡对象. 例: MyCryptoSmartCard.java /** * MyCryptoSmartCard.java * Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved. */ 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.*; /** * This class represents a kind (or model or family) of a physical smart card. * There should only be one instance of this class in the system at one 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 properties/settings now"; private static final String RSA = "RSA"; /** *A smart card factory’s list of known smart cards. *Called on startup of the device. Register this driver with the */ public static void libMain( String args[] ) { SmartCardFactory.addSmartCard( new MyCryptoSmartCard() ); } /** *Retrieve the session handler for this smart card. *Implementations of this method should not bring up UI. */ protected SmartCardSession openSessionImpl( SmartCardReaderSession readerSession ) throws SmartCardException { return new MyCryptoSmartCardSession( this, readerSession ); } /** * Determine if the file 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 ascertain which smart card implementation it should * use to communicate with a physical smart card found in a reader. */ protected boolean checkAnswerToResetImpl( AnswerToReset atr ) { return _myATR.equals( atr ); } /** *Retrieve a label associated with this smart card. *The string should not include the words "smart card", as the file system uses this *this method to generate strings such as "Please insert your smart card". */ protected String getLabelImpl() { return LABEL; } /** * Retrieves this smart card’s capabilities */ protected SmartCardCapabilities getCapabilitiesImpl() { return new SmartCardCapabilities( SmartCardCapabilities.PROTOCOL_T0 ); } /** *Determine if 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. * *@param context Reserved for future use. **/ protected void displaySettingsImpl( Object context ) { Dialog.alert( DISPLAY_SETTINGS ); } /** * Retrieve the algorithms supported by this smart card. * * @return one of "RSA", "DSA", or "ECC" */ public String[] getAlgorithms() { return new String [] { RSA }; } /** Retrieve a crypto token that supports the given algorithm. * @param algorithm Name of the algorithm. * * @return Crypto Token supporting the named algorithm. * @throws NoSuchAlgorithmException If the specified algorithm is invalid. * @throws CryptoTokenException If there is a token-related problem. */ public CryptoToken getCryptoToken( String algorithm ) throws NoSuchAlgorithmException, CryptoTokenException { if ( algorithm.equals( RSA |
