java 在android中向USIM/SIM卡发送APDU命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30325663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send APDU commands to USIM/SIM card in android
提问by Jean
I was already worked with smart cards and I am familiar with APDU commands (that are defined in ISO/IEC 7816and Global Platformspecifications).
我已经使用过智能卡,并且熟悉 APDU 命令(在ISO/IEC 7816和全球平台规范中定义)。
Now I want to know if there is any way to send an APDU command to my USIM/SIM card that is inserted to my mobile phone? (Samsung A3 with Android v4.4.4 kitkat installed.)
现在我想知道有没有什么办法可以给我插在手机上的USIM/SIM卡发送APDU命令?(安装了 Android v4.4.4 kitkat 的三星 A3。)
I already searched in the Google and I found some related topics and tools named SIM Toolkit Applicationand Seek for Android. But I don't really understand what are these? Are these items two applications that I must install on my mobile phone? or are those two tools that was installed on the USIM/SIM card already and receive commands from the mobile phone?
我已经在 Google 中搜索过,发现了一些名为SIM Toolkit Application和Seek for Android 的相关主题和工具。但我真的不明白这些是什么?这些项目是我必须在手机上安装的两个应用程序吗?或者这两个工具是否已经安装在USIM/SIM卡上并从手机接收命令?
What is the difference between Proactive commands, APDU commandsand AT commands?
Proactive 命令、APDU 命令和AT 命令有什么区别?
Should I learn android to develop SIM card applications or I just need Java Card specifications and ETSI standards?
我应该学习android来开发SIM卡应用程序还是我只需要Java Card规范和ETSI标准?
Thanks in advance.
提前致谢。
回答by vojta
There can be two different types of applets present on your SIM card.
SIM 卡上可以有两种不同类型的小程序。
Common applets
常见小程序
Common applets written in plain JavaCard. This is the type of applet you are used to from the world of common smart cards. It has the process
method and smart card is the passive subject in the communication: your app sends APDU commands and the card responses.
用普通 JavaCard 编写的常见小程序。这是您在普通智能卡世界中习惯的小程序类型。它有process
方法,智能卡是通信中的被动主体:您的应用程序发送 APDU 命令和卡响应。
You can communicate with these applets using a special set of Android libraries called SEEK for Android. Have a look at this tutorialto learn how to create such a phone application.
您可以使用一组称为 SEEK for Android 的特殊 Android 库与这些小程序进行通信。查看本教程以了解如何创建这样的电话应用程序。
Starting on API level 21 there is also a way to communicate to SIM using Telephony Manager. However, there is one huge obstacle: your app needs MODIFY_PHONE_STATE
permission, which can be granted only to system apps. A reqular, non-system app isn't allowed to use it.
从 API 级别 21 开始,还有一种使用Telephony Manager与 SIM 通信的方法。但是,有一个巨大的障碍:您的应用程序需要MODIFY_PHONE_STATE
权限,该权限只能授予系统应用程序。不允许常规的非系统应用程序使用它。
SIM Toolkit Applets
SIM 工具包小程序
A SIM card is much more than just a common smart card and writing an applet for a SIM card can be much more complicated than for a common smart card if you want to use all the possibilities the SIM card offers. I recommend you to read this paper- it is someone's bachelor thesis, but it is the best overview for a beginner I have found all over the Internet. I also recommend this video from the DefConn conference.
SIM 卡不仅仅是一张普通的智能卡,如果您想使用 SIM 卡提供的所有可能性,那么为 SIM 卡编写小程序可能比普通智能卡复杂得多。我建议你阅读这篇论文——它是某人的学士论文,但它是我在互联网上找到的对初学者最好的概述。我也推荐这个来自 DefConn 会议的视频。
The role of the applet loaded on the SIM card is different: the applet is no longer a passive entity. The phone asks your applet regularly: "Is there anything new I can do for you?" and your applet can reply: "Yes, send this SMS, please" or "Tell me what time it is" etc. Moreover, your applet can become a listener of some events: incoming call, received SMS, time interval elapsed etc. Yes, the SIM card seems to be passive from the technical point of view, but its role is in fact an active one: it is the SIM card who sends commands to the phone.
SIM卡上加载的小程序的作用不同:小程序不再是一个被动的实体。电话会定期询问您的小程序:“我可以为您做些什么吗?” 您的小程序可以回复:“是的,请发送此短信”或“告诉我现在几点了”等。此外,您的小程序可以成为某些事件的侦听器:来电、收到的短信、已过的时间间隔等。是的,SIM卡从技术上看似乎是被动的,但其实它的作用是主动的:向手机发送指令的是SIM卡。
These commands are called "proactive commands" or SIM Application Toolkit commands. Structure is the same - CLA INS P1 P2 LC data LE; the meaning is different.
这些命令称为“主动命令”或 SIM 应用程序工具包命令。结构相同——CLA INS P1 P2 LC 数据LE;意思是不同的。
You can send them from your applet using classes in a special JavaCard package called sim.toolkit
.
您可以使用名为 的特殊 JavaCard 包中的类从您的小程序发送它们sim.toolkit
。
(SIM Application Toolkit is a standard that specifies the proactive commands in the same way Global Platform specifies the applet's lifecycle.)
(SIM 应用程序工具包是一种标准,它以与 Global Platform 指定小程序生命周期相同的方式指定主动命令。)
Example of SIM Toolkit applet:
SIM 工具包小程序示例:
import sim.toolkit.ToolkitInterface;
import sim.toolkit.ToolkitRegistry;
...
import javacard.framework.ISOException;
public class STKTest extends Applet implements ToolkitInterface {
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new STKTest().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
//this method handles standard APDU commands
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
apdu.setIncomingAndReceive();
final byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x00:
//do something
break;
}
}
//this method handles the SIM Toolkit commands
public void processToolkit(byte event) throws ToolkitException {
switch (event) {
case ToolkitConstants.EVENT_TIMER_EXPIRATION:
//do something
break;
}
}
}
Yes, you should learn Android - you will need it to use the SEEK library. Your question is very broad, please ask me for any details, if you want.
是的,您应该学习 Android - 您将需要它来使用 SEEK 库。您的问题非常广泛,如果您愿意,请向我询问任何详细信息。
回答by Sirie A.
Starting on API level 22 (Android 5.1) there is another Option called "Carrier Privileges". It allows non-system apps to send APDUs to the SIM card using Android TelephonyManager. See: https://developer.android.com/reference/android/telephony/TelephonyManager.html#hasCarrierPrivileges()
从 API 级别 22(Android 5.1)开始,还有另一个称为“运营商特权”的选项。它允许非系统应用程序使用 Android TelephonyManager 将 APDU 发送到 SIM 卡。请参阅:https: //developer.android.com/reference/android/telephony/TelephonyManager.html#hasCarrierPrivileges()
For example mobile network operator (MNO) Apps that are distributed on Google Play can use this. But again it's not open for everybody. In this case you need to be granted access by the SIM. The Access Rules on the SIM are managed by the MNO who issued it. See also: http://source.android.com/devices/tech/config/uicc.html
例如,在 Google Play 上分发的移动网络运营商 (MNO) 应用程序可以使用它。但它也不是对所有人开放。在这种情况下,您需要获得 SIM 卡的访问权限。SIM 上的访问规则由发布它的 MNO 管理。另见:http: //source.android.com/devices/tech/config/uicc.html