4.4.1.3 创建/使用伙伴服务

伙伴服务是只能由特定应用使用的服务。 系统由伙伴公司的应用和内部应用组成,用于保护在伙伴应用和内部应用之间处理的信息和功能。

以下是 AIDL 绑定类型服务的示例。

要点(创建服务):

  1. 不要定义意图过滤器,并将导出属性显式设置为true

  2. 验证请求应用的证书是否已在自己的白名单中注册。

  3. 请勿(无法)通过onBind(onStartCommand, onHandleIntent)识别请求应用是否为伙伴。

  4. 小心并安全地处理接收到的意图,即使意图是从伙伴应用发送的。

  5. 仅返回公开给伙伴应用的信息。

另外,请参阅“5.2.1.3 如何验证应用证书的哈希值”,来了解如何验证目标应用的哈希值,它在白名单中指定。

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="org.jssec.android.service.partnerservice.aidl" >
  4. <application
  5. android:icon="@drawable/ic_launcher"
  6. android:label="@string/app_name"
  7. android:allowBackup="false" >
  8. <!-- Service using AIDL -->
  9. <!-- *** POINT 1 *** Do not define the intent filter and explicitly set the exported attribute to
  10. true. -->
  11. <service
  12. android:name="org.jssec.android.service.partnerservice.aidl.PartnerAIDLService"
  13. android:exported="true" />
  14. </application>
  15. </manifest>

在这个例子中,将创建 2 个 AIDL 文件。 一个是回调接口,将数据从服务提供给活动。 另一个接口将数据从活动提供给服务,并获取信息。 另外,AIDL 文件中描述的包名称,应与 AIDL 文件的目录层次一致,与java文件中描述的包名称相同。

IExclusiveAIDLServiceCallback.aidl

  1. package org.jssec.android.service.exclusiveservice.aidl;
  2. interface IExclusiveAIDLServiceCallback {
  3. /**
  4. * It's called when the value is changed.
  5. */
  6. void valueChanged(String info);
  7. }

IExclusiveAIDLService.aidl

  1. package org.jssec.android.service.exclusiveservice.aidl;
  2. import org.jssec.android.service.exclusiveservice.aidl.IExclusiveAIDLServiceCallback;
  3. interface IExclusiveAIDLService {
  4. /**
  5. * Register Callback.
  6. */
  7. void registerCallback(IExclusiveAIDLServiceCallback cb);
  8. /**
  9. * Get Information
  10. */
  11. String getInfo(String param);
  12. /**
  13. * Unregister Callback
  14. */
  15. void unregisterCallback(IExclusiveAIDLServiceCallback cb);
  16. }

PartnerAIDLService.java

  1. package org.jssec.android.service.partnerservice.aidl;
  2. import org.jssec.android.shared.PkgCertWhitelists;
  3. import org.jssec.android.shared.Utils;
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Handler;
  8. import android.os.IBinder;
  9. import android.os.Message;
  10. import android.os.RemoteCallbackList;
  11. import android.os.RemoteException;
  12. import android.widget.Toast;
  13. public class PartnerAIDLService extends Service {
  14. private static final int REPORT_MSG = 1;
  15. private static final int GETINFO_MSG = 2;
  16. // The value which this service informs to client
  17. private int mValue = 0;
  18. // *** POINT 2 *** Verify that the certificate of the requesting application has been registered in the own white list.
  19. private static PkgCertWhitelists sWhitelists = null;
  20. private static void buildWhitelists(Context context) {
  21. boolean isdebug = Utils.isDebuggable(context);
  22. sWhitelists = new PkgCertWhitelists();
  23. // Register certificate hash value of partner application "org.jssec.android.service.partnerservice.aidluser"
  24. sWhitelists.add("org.jssec.android.service.partnerservice.aidluser", isdebug ?
  25. // Certificate hash value of debug.keystore "androiddebugkey"
  26. "0EFB7236 328348A9 89718BAD DF57F544 D5CCB4AE B9DB34BC 1E29DD26 F77C8255" :
  27. // Certificate hash value of keystore "partner key"
  28. "1F039BB5 7861C27A 3916C778 8E78CE00 690B3974 3EB8259F E2627B8D 4C0EC35A");
  29. // Register other partner applications in the same way
  30. }
  31. private static boolean checkPartner(Context context, String pkgname) {
  32. if (sWhitelists == null) buildWhitelists(context);
  33. return sWhitelists.test(context, pkgname);
  34. }
  35. // Object to register callback
  36. // Methods which RemoteCallbackList provides are thread-safe.
  37. private final RemoteCallbackList<IPartnerAIDLServiceCallback> mCallbacks =
  38. new RemoteCallbackList<IPartnerAIDLServiceCallback>();
  39. // Handler to send data when callback is called.
  40. private static class ServiceHandler extends Handler{
  41. private Context mContext;
  42. private RemoteCallbackList<IPartnerAIDLServiceCallback> mCallbacks;
  43. private int mValue = 0;
  44. public ServiceHandler(Context context, RemoteCallbackList<IPartnerAIDLServiceCallback> callback, int value){
  45. this.mContext = context;
  46. this.mCallbacks = callback;
  47. this.mValue = value;
  48. }
  49. @Override
  50. public void handleMessage(Message msg) {
  51. switch (msg.what) {
  52. case REPORT_MSG: {
  53. if(mCallbacks == null){
  54. return;
  55. }
  56. // Start broadcast
  57. // To call back on to the registered clients, use beginBroadcast().
  58. // beginBroadcast() makes a copy of the currently registered callback list.
  59. final int N = mCallbacks.beginBroadcast();
  60. for (int i = 0; i < N; i++) {
  61. IPartnerAIDLServiceCallback target = mCallbacks.getBroadcastItem(i);
  62. try {
  63. // *** POINT 5 *** Information that is granted to disclose to partner applications can be returned.
  64. target.valueChanged("Information disclosed to partner application (callback from Service) No." + (++mValue));
  65. } catch (RemoteException e) {
  66. // Callbacks are managed by RemoteCallbackList, do not unregister callbacks here.
  67. // RemoteCallbackList.kill() unregister all callbacks
  68. }
  69. }
  70. // finishBroadcast() cleans up the state of a broadcast previously initiated by calling beginBroadcast().
  71. mCallbacks.finishBroadcast();
  72. // Repeat after 10 seconds
  73. sendEmptyMessageDelayed(REPORT_MSG, 10000);
  74. break;
  75. }
  76. case GETINFO_MSG: {
  77. if(mContext != null) {
  78. Toast.makeText(mContext,
  79. (String) msg.obj, Toast.LENGTH_LONG).show();
  80. }
  81. break;
  82. }
  83. default:
  84. super.handleMessage(msg);
  85. break;
  86. } // switch
  87. }
  88. protected final ServiceHandler mHandler = new ServiceHandler(this, mCallbacks, mValue);
  89. // Interfaces defined in AIDL
  90. private final IPartnerAIDLService.Stub mBinder = new IPartnerAIDLService.Stub() {
  91. private boolean checkPartner() {
  92. Context ctx = PartnerAIDLService.this;
  93. if (!PartnerAIDLService.checkPartner(ctx, Utils.getPackageNameFromUid(ctx, getCallingUid()))) {
  94. mHandler.post(new Runnable(){
  95. @Override
  96. public void run(){
  97. Toast.makeText(PartnerAIDLService.this, "Requesting application is not partner application.", Toast.LENGTH_LONG).show();
  98. }
  99. });
  100. return false;
  101. }
  102. return true;
  103. }
  104. public String getInfo(String param) {
  105. // *** POINT 2 *** Verify that the certificate of the requesting application has been registered in the own white list.
  106. if (!checkPartner()) {
  107. return null;
  108. }
  109. // *** POINT 4 *** Handle the received intent carefully and securely,
  110. // even though the intent was sent from a partner application
  111. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  112. Message msg = new Message();
  113. msg.what = GETINFO_MSG;
  114. msg.obj = String.format("Method calling from partner application. Recieved ¥"%s¥"", param);
  115. PartnerAIDLService.this.mHandler.sendMessage(msg);
  116. // *** POINT 5 *** Return only information that is granted to be disclosed to a partner application.
  117. return "Information disclosed to partner application (method from Service)";
  118. }
  119. public void unregisterCallback(IPartnerAIDLServiceCallback cb) {
  120. // *** POINT 2 *** Verify that the certificate of the requesting application has been registered in the own white list.
  121. if (!checkPartner()) {
  122. return;
  123. }
  124. if (cb != null) mCallbacks.unregister(cb);
  125. }
  126. };
  127. @Override
  128. public IBinder onBind(Intent intent) {
  129. // *** POINT 3 *** Verify that the certificate of the requesting application has been registered in the own white list.
  130. // So requesting application must be validated in methods defined in AIDL every time.
  131. return mBinder;
  132. }
  133. @Override
  134. public void onCreate() {
  135. Toast.makeText(this, this.getClass().getSimpleName() + " - onCreate()", Toast.LENGTH_SHORT).show();
  136. // During service is running, inform the incremented number periodically.
  137. mHandler.sendEmptyMessage(REPORT_MSG);
  138. }
  139. @Override
  140. public void onDestroy() {
  141. Toast.makeText(this, this.getClass().getSimpleName() + " - onDestroy()", Toast.LENGTH_SHORT).show();
  142. // Unregister all callbacks
  143. mCallbacks.kill();
  144. mHandler.removeMessages(REPORT_MSG);
  145. }
  146. }

PkgCertWhitelists.java

  1. package org.jssec.android.shared;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import android.content.Context;
  5. public class PkgCertWhitelists {
  6. private Map<String, String> mWhitelists = new HashMap<String, String>();
  7. public boolean add(String pkgname, String sha256) {
  8. if (pkgname == null) return false;
  9. if (sha256 == null) return false;
  10. sha256 = sha256.replaceAll(" ", "");
  11. if (sha256.length() != 64) return false; // SHA-256 -> 32 bytes -> 64 chars
  12. sha256 = sha256.toUpperCase();
  13. if (sha256.replaceAll("[0-9A-F]+", "").length() != 0) return false; // found non hex char
  14. mWhitelists.put(pkgname, sha256);
  15. return true;
  16. }
  17. public boolean test(Context ctx, String pkgname) {
  18. // Get the correct hash value which corresponds to pkgname.
  19. String correctHash = mWhitelists.get(pkgname);
  20. // Compare the actual hash value of pkgname with the correct hash value.
  21. return PkgCert.test(ctx, pkgname, correctHash);
  22. }
  23. }

PkgCert.java

  1. package org.jssec.android.shared;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import android.content.Context;
  5. import android.content.pm.PackageInfo;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.PackageManager.NameNotFoundException;
  8. import android.content.pm.Signature;
  9. public class PkgCert {
  10. public static boolean test(Context ctx, String pkgname, String correctHash) {
  11. if (correctHash == null) return false;
  12. correctHash = correctHash.replaceAll(" ", "");
  13. return correctHash.equals(hash(ctx, pkgname));
  14. }
  15. public static String hash(Context ctx, String pkgname) {
  16. if (pkgname == null) return null;
  17. try {
  18. PackageManager pm = ctx.getPackageManager();
  19. PackageInfo pkginfo = pm.getPackageInfo(pkgname, PackageManager.GET_SIGNATURES);
  20. if (pkginfo.signatures.length != 1) return null; // Will not handle multiple signatures.
  21. Signature sig = pkginfo.signatures[0];
  22. byte[] cert = sig.toByteArray();
  23. byte[] sha256 = computeSha256(cert);
  24. return byte2hex(sha256);
  25. } catch (NameNotFoundException e) {
  26. return null;
  27. }
  28. }
  29. private static byte[] computeSha256(byte[] data) {
  30. try {
  31. return MessageDigest.getInstance("SHA-256").digest(data);
  32. } catch (NoSuchAlgorithmException e) {
  33. return null;
  34. }
  35. }
  36. private static String byte2hex(byte[] data) {
  37. if (data == null) return null;
  38. final StringBuilder hexadecimal = new StringBuilder();
  39. for (final byte b : data) {
  40. hexadecimal.append(String.format("%02X", b));
  41. }
  42. return hexadecimal.toString();
  43. }
  44. }

下面是使用伙伴服务的活动代码:

要点(使用服务):

  1. 验证目标应用的证书是否已在自己的白名单中注册。

  2. 仅返回公开给伙伴应用的信息。

  3. 使用显式意图调用伙伴服务。

  4. 即使数据来自伙伴应用,也要小心并安全地处理收到的结果数据。

ExclusiveAIDLUserActivity.java

  1. package org.jssec.android.service.partnerservice.aidluser;
  2. import org.jssec.android.service.partnerservice.aidl.IPartnerAIDLService;
  3. import org.jssec.android.service.partnerservice.aidl.IPartnerAIDLServiceCallback;
  4. import org.jssec.android.shared.PkgCertWhitelists;
  5. import org.jssec.android.shared.Utils;
  6. import android.app.Activity;
  7. import android.content.ComponentName;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.ServiceConnection;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.IBinder;
  14. import android.os.Message;
  15. import android.os.RemoteException;
  16. import android.view.View;
  17. import android.widget.Toast;
  18. public class PartnerAIDLUserActivity extends Activity {
  19. private boolean mIsBound;
  20. private Context mContext;
  21. private final static int MGS_VALUE_CHANGED = 1;
  22. // *** POINT 6 *** Verify if the certificate of the target application has been registered in the own white list.
  23. private static PkgCertWhitelists sWhitelists = null;
  24. private static void buildWhitelists(Context context) {
  25. boolean isdebug = Utils.isDebuggable(context);
  26. sWhitelists = new PkgCertWhitelists();
  27. // Register certificate hash value of partner service application "org.jssec.android.service.partnerservice.aidl"
  28. sWhitelists.add("org.jssec.android.service.partnerservice.aidl", isdebug ?
  29. // Certificate hash value of debug.keystore "androiddebugkey"
  30. "0EFB7236 328348A9 89718BAD DF57F544 D5CCB4AE B9DB34BC 1E29DD26 F77C8255" :
  31. // Certificate hash value of keystore "my company key"
  32. "D397D343 A5CBC10F 4EDDEB7C A10062DE 5690984F 1FB9E88B D7B3A7C2 42E142CA");
  33. // Register other partner service applications in the same way
  34. }
  35. private static boolean checkPartner(Context context, String pkgname) {
  36. if (sWhitelists == null) buildWhitelists(context);
  37. return sWhitelists.test(context, pkgname);
  38. }
  39. // Information about destination (requested) partner activity.
  40. private static final String TARGET_PACKAGE = "org.jssec.android.service.partnerservice.aidl";
  41. private static final String TARGET_CLASS = "org.jssec.android.service.partnerservice.aidl.PartnerAIDLService";
  42. private static class ReceiveHandler extends Handler{
  43. private Context mContext;
  44. public ReceiveHandler(Context context){
  45. this.mContext = context;
  46. }
  47. @Override
  48. public void handleMessage(Message msg) {
  49. switch (msg.what) {
  50. case MGS_VALUE_CHANGED: {
  51. String info = (String)msg.obj;
  52. Toast.makeText(mContext, String.format("Received ¥"%s¥" with callback.", info), Toast.LENGTH_SHORT).show();
  53. break;
  54. }
  55. default:
  56. super.handleMessage(msg);
  57. break;
  58. } // switch
  59. }
  60. private final ReceiveHandler mHandler = new ReceiveHandler(this);
  61. // Interfaces defined in AIDL. Receive notice from service
  62. private final IPartnerAIDLServiceCallback.Stub mCallback =
  63. new IPartnerAIDLServiceCallback.Stub() {
  64. @Override
  65. public void valueChanged(String info) throws RemoteException {
  66. Message msg = mHandler.obtainMessage(MGS_VALUE_CHANGED, info);
  67. mHandler.sendMessage(msg);
  68. }
  69. };
  70. // Interfaces defined in AIDL. Inform service.
  71. private IPartnerAIDLService mService = null;
  72. // Connection used to connect with service. This is necessary when service is implemented with bindService().
  73. private ServiceConnection mConnection = new ServiceConnection() {
  74. // This is called when the connection with the service has been established.
  75. @Override
  76. public void onServiceConnected(ComponentName className, IBinder service) {
  77. mService = IPartnerAIDLService.Stub.asInterface(service);
  78. try{
  79. // connect to service
  80. mService.registerCallback(mCallback);
  81. }catch(RemoteException e){
  82. // service stopped abnormally
  83. }
  84. Toast.makeText(mContext, "Connected to service", Toast.LENGTH_SHORT).show();
  85. }
  86. // This is called when the service stopped abnormally and connection is disconnected.
  87. @Override
  88. public void onServiceDisconnected(ComponentName className) {
  89. Toast.makeText(mContext, "Disconnected from service", Toast.LENGTH_SHORT).show();
  90. }
  91. };
  92. @Override
  93. public void onCreate(Bundle savedInstanceState) {
  94. super.onCreate(savedInstanceState);
  95. setContentView(R.layout.partnerservice_activity);
  96. mContext = this;
  97. }
  98. // --- StartService control ---
  99. public void onStartServiceClick(View v) {
  100. // Start bindService
  101. doBindService();
  102. }
  103. public void onGetInfoClick(View v) {
  104. getServiceinfo();
  105. }
  106. public void onStopServiceClick(View v) {
  107. doUnbindService();
  108. }
  109. @Override
  110. public void onDestroy() {
  111. super.onDestroy();
  112. doUnbindService();
  113. }
  114. /**
  115. * Connect to service
  116. */
  117. private void doBindService() {
  118. if (!mIsBound){
  119. // *** POINT 6 *** Verify if the certificate of the target application has been registered in the own white list.
  120. if (!checkPartner(this, TARGET_PACKAGE)) {
  121. Toast.makeText(this, "Destination(Requested) sevice application is not registered in white list.", Toast.LENGTH_LONG).show();
  122. return;
  123. }
  124. Intent intent = new Intent();
  125. // *** POINT 7 *** Return only information that is granted to be disclosed to a partner application.
  126. intent.putExtra("PARAM", "Information disclosed to partner application");
  127. // *** POINT 8 *** Use the explicit intent to call a partner service.
  128. intent.setClassName(TARGET_PACKAGE, TARGET_CLASS);
  129. bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  130. mIsBound = true;
  131. }
  132. }
  133. /**
  134. * Disconnect service
  135. */
  136. private void doUnbindService() {
  137. if (mIsBound) {
  138. // Unregister callbacks which have been registered.
  139. if(mService != null){
  140. try{
  141. mService.unregisterCallback(mCallback);
  142. }catch(RemoteException e){
  143. // Service stopped abnormally
  144. // Omitted, since it' s sample.
  145. }
  146. }
  147. unbindService(mConnection);
  148. Intent intent = new Intent();
  149. // *** POINT 8 *** Use the explicit intent to call a partner service.
  150. intent.setClassName(TARGET_PACKAGE, TARGET_CLASS);
  151. stopService(intent);
  152. mIsBound = false;
  153. }
  154. }
  155. /**
  156. * Get information from service
  157. */
  158. void getServiceinfo() {
  159. if (mIsBound && mService != null) {
  160. String info = null;
  161. try {
  162. // *** POINT 7 *** Return only information that is granted to be disclosed to a partner application.
  163. info = mService.getInfo("Information disclosed to partner application (method from activity)");
  164. } catch (RemoteException e) {
  165. e.printStackTrace();
  166. }
  167. // *** POINT 9 *** Handle the received result data carefully and securely,
  168. // even though the data came from a partner application.
  169. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  170. Toast.makeText(mContext, String.format("Received ¥"%s¥" from service.", info), Toast.LENGTH_SHORT).show();
  171. }
  172. }
  173. }

PkgCertWhitelists.java

  1. package org.jssec.android.shared;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import android.content.Context;
  5. public class PkgCertWhitelists {
  6. private Map<String, String> mWhitelists = new HashMap<String, String>();
  7. public boolean add(String pkgname, String sha256) {
  8. if (pkgname == null) return false;
  9. if (sha256 == null) return false;
  10. sha256 = sha256.replaceAll(" ", "");
  11. if (sha256.length() != 64) return false; // SHA-256 -> 32 bytes -> 64 chars
  12. sha256 = sha256.toUpperCase();
  13. if (sha256.replaceAll("[0-9A-F]+", "").length() != 0) return false; // found non hex char
  14. mWhitelists.put(pkgname, sha256);
  15. return true;
  16. }
  17. public boolean test(Context ctx, String pkgname) {
  18. // Get the correct hash value which corresponds to pkgname.
  19. String correctHash = mWhitelists.get(pkgname);
  20. // Compare the actual hash value of pkgname with the correct hash value.
  21. return PkgCert.test(ctx, pkgname, correctHash);
  22. }
  23. }

PkgCert.java

  1. package org.jssec.android.shared;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import android.content.Context;
  5. import android.content.pm.PackageInfo;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.PackageManager.NameNotFoundException;
  8. import android.content.pm.Signature;
  9. public class PkgCert {
  10. public static boolean test(Context ctx, String pkgname, String correctHash) {
  11. if (correctHash == null) return false;
  12. correctHash = correctHash.replaceAll(" ", "");
  13. return correctHash.equals(hash(ctx, pkgname));
  14. }
  15. public static String hash(Context ctx, String pkgname) {
  16. if (pkgname == null) return null;
  17. try {
  18. PackageManager pm = ctx.getPackageManager();
  19. PackageInfo pkginfo = pm.getPackageInfo(pkgname, PackageManager.GET_SIGNATURES);
  20. if (pkginfo.signatures.length != 1) return null; // Will not handle multiple signatures.
  21. Signature sig = pkginfo.signatures[0];
  22. byte[] cert = sig.toByteArray();
  23. byte[] sha256 = computeSha256(cert);
  24. return byte2hex(sha256);
  25. } catch (NameNotFoundException e) {
  26. return null;
  27. }
  28. }
  29. private static byte[] computeSha256(byte[] data) {
  30. try {
  31. return MessageDigest.getInstance("SHA-256").digest(data);
  32. } catch (NoSuchAlgorithmException e) {
  33. return null;
  34. }
  35. }
  36. private static String byte2hex(byte[] data) {
  37. if (data == null) return null;
  38. final StringBuilder hexadecimal = new StringBuilder();
  39. for (final byte b : data) {
  40. hexadecimal.append(String.format("%02X", b));
  41. }
  42. return hexadecimal.toString();
  43. }
  44. }