title: Android: secure IPC alternative to AIDL?
body: <p><strong>What I'm trying to do:</strong> implement lightweight, <strong>secure</strong> IPC protocol between two applications installed on the device. Client app should be able to send commands and queries to <code>Service</code> running in a service app, and receive back results of computations.</p>
  
  <p><strong>Apps relationship:</strong> source code of both apps is under my control, but the apps will have different signatures (non-negotiable).</p>
  
  <p><strong>Security requirements:</strong> service app should provide its services to a single client. Client's application ID (package name) is known and constant.</p>
  
  <p><strong>What I tried:</strong> I tried to implement IPC with two-way <code>Messenger</code> communication scheme (similar to <a href="http://codetheory.in/android-interprocess-communication-ipc-messenger-remote-bound-services/" rel="nofollow">this blog post</a>). This approach works fine, but I encountered one major issue - I can't find a way to obtain <code>UID</code> of the client, therefore I can't satisfy security requirements.</p>
  
  <p>Consider this code found in <code>Service</code> of the service application:</p>
  
  <pre><code>// This messenger will be used by the clients of this service in order to send commands
  private Messenger inboxMessenger = new Messenger(new Handler() {
  
      @Override
      public void handleMessage(Message msg)
  
          // TODO: verify the identity of the client
  
          switch (msg.what) {
              case MSG_GET_DATA:
                  returnDataToClient(msg.replyTo);
                  break;
          }
      }
  });
  </code></pre>
  
  <p>The idea here is that when the client app sends a message to this <code>Service</code>, it puts its local "callback" <code>Messenger</code> into <code>replyTo</code> member of the sent <code>Message</code>. Documentation for <code>Messenger</code> states that:</p>
  
  <blockquote>
    <p>Note: the implementation underneath is just a simple wrapper around a
    Binder that is used to perform the communication.</p>
  </blockquote>
  
  <p>so I thought that I could somehow map the <a href="http://developer.android.com/reference/android/os/Binder.html" rel="nofollow">Binder</a> returned by <code>Messenger#getBinder()</code> to client's <code>UID</code>, but I'm now having troubles:</p>
  
  <ol>
  <li><code>Messenger#getBinder()</code> returns <a href="http://developer.android.com/reference/android/os/IBinder.html" rel="nofollow">IBinder</a> which can't be cast to <code>Binder</code></li>
  <li>Even if I do manage to get a reference to client's <code>Binder</code>, the method <code>Binder#getCallingUid()</code> is <code>static</code> and does not accept arguments... </li>
  </ol>
  
  <p>So, in order to make this particular implementation work securely, I need to find a way to obtain caller's <code>UID</code> based on the contents of <code>Message</code> or based on a particular <code>Messenger</code> which was created by the client. Since Android's security architecture is built around <code>Binders</code>, it seems strange that there is no straightforward approach to mapping <code>Binders</code> to <code>UIDs</code> (or package names)... So, how can I do this?</p>
  
  <p>Bonus question: except for AIDL, is there any other IPC technique on Android that fulfills the requirements stated above?</p>