简体   繁体   English

在android中向USIM/SIM卡发送APDU命令

[英]Send APDU commands to USIM/SIM card in android

I was already worked with smart cards and I am familiar with APDU commands (that are defined in ISO/IEC 7816 and Global Platform specifications).我已经使用过智能卡,并且熟悉 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?现在我想知道有没有什么办法可以给我插在手机上的USIM/SIM卡发送APDU命令? (Samsung A3 with Android v4.4.4 kitkat installed.) (安装了 Android v4.4.4 kitkat 的三星 A3。)

I already searched in the Google and I found some related topics and tools named SIM Toolkit Application and Seek for Android .我已经在 Google 中搜索过,发现了一些名为SIM Toolkit ApplicationSeek 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?或者这两个工具是否已经安装在USIM/SIM卡上并从手机接收命令?

What is the difference between Proactive commands , APDU commands and 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.提前致谢。

There can be two different types of applets present on your SIM card. SIM 卡上可以有两种不同类型的小程序。

Common applets常见小程序

Common applets written in plain JavaCard.用普通 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.它具有process方法,智能卡是通信中的被动主体:您的应用程序发送 APDU 命令和卡响应。

You can communicate with these applets using a special set of Android libraries called SEEK for Android.您可以使用一组称为 SEEK for Android 的特殊 Android 库与这些小程序进行通信。 Have a look at thistutorial to learn how to create such a phone application.查看本教程以了解如何创建这样的电话应用程序。

Starting on API level 21 there is also a way to communicate to SIM using Telephony Manager .从 API 级别 21 开始,还有一种使用Telephony Manager与 SIM 通信的方法。 However, there is one huge obstacle: your app needs MODIFY_PHONE_STATE permission, which can be granted only to system apps.但是,有一个巨大的障碍:您的应用程序需要MODIFY_PHONE_STATE权限,该权限只能授予系统应用程序。 A reqular, non-system app isn't allowed to use it.不允许常规的非系统应用程序使用它。

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. SIM 卡不仅仅是一张普通的智能卡,如果您想使用 SIM 卡提供的所有可能性,那么为 SIM 卡编写小程序可能比普通智能卡复杂得多。 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 .我也推荐这个来自 DefConn 会议的视频

The role of the applet loaded on the SIM card is different: the applet is no longer a passive entity. SIM卡上加载的小程序的作用不同:小程序不再是一个被动的实体。 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卡。

These commands are called "proactive commands" or SIM Application Toolkit commands.这些命令称为“主动命令”或 SIM 应用程序工具包命令。 Structure is the same - CLA INS P1 P2 LC data LE;结构相同——CLA INS P1 P2 LC 数据LE; the meaning is different.意思是不同的。

You can send them from your applet using classes in a special JavaCard package called sim.toolkit .您可以使用名为sim.toolkit的特殊 JavaCard 包中的类从您的小程序发送它们。

(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.是的,您应该学习 Android - 您将需要它来使用 SEEK 库。 Your question is very broad, please ask me for any details, if you want.您的问题非常广泛,如果您愿意,请向我询问任何详细信息。

Starting on API level 22 (Android 5.1) there is another Option called "Carrier Privileges".从 API 级别 22(Android 5.1)开始,还有另一个称为“运营商特权”的选项。 It allows non-system apps to send APDUs to the SIM card using Android TelephonyManager.它允许非系统应用程序使用 Android TelephonyManager 向 SIM 卡发送 APDU。 See: https://developer.android.com/reference/android/telephony/TelephonyManager.html#hasCarrierPrivileges()请参阅: 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.例如,在 Google Play 上分发的移动网络运营商 (MNO) 应用程序可以使用它。 But again it's not open for everybody.但它也不是对所有人开放。 In this case you need to be granted access by the SIM.在这种情况下,您需要获得 SIM 卡的访问权限。 The Access Rules on the SIM are managed by the MNO who issued it. SIM 上的访问规则由发布它的 MNO 管理。 See also: http://source.android.com/devices/tech/config/uicc.html另见: http : //source.android.com/devices/tech/config/uicc.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM