RemoteAndroidKeyStore.java revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.net;
6
7import android.os.RemoteException;
8import android.util.Log;
9
10/**
11 * Provides a remoted implementation of AndroidKeyStore where all calls are forwarded via
12 * binder to an external process.
13 */
14public class RemoteAndroidKeyStore implements AndroidKeyStore {
15
16    private static final String TAG = "AndroidKeyStoreRemoteImpl";
17
18    private static class RemotePrivateKey implements AndroidPrivateKey {
19        // Reference to the key on a remote store.
20        final int mHandle;
21        // Key store handling this key.
22        final RemoteAndroidKeyStore mStore;
23
24        RemotePrivateKey(int handle, RemoteAndroidKeyStore store) {
25            mHandle = handle;
26            mStore = store;
27        }
28
29        public int getHandle() {
30            return mHandle;
31        }
32
33        @Override
34        public AndroidKeyStore getKeyStore() {
35            return mStore;
36        }
37    }
38
39    private final IRemoteAndroidKeyStore mRemoteManager;
40
41    public RemoteAndroidKeyStore(IRemoteAndroidKeyStore manager) {
42        mRemoteManager = manager;
43    }
44
45    @Override
46    public byte[] getRSAKeyModulus(AndroidPrivateKey key) {
47        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
48        try {
49            Log.d(TAG, "getRSAKeyModulus");
50            return mRemoteManager.getRSAKeyModulus(remoteKey.getHandle());
51        } catch (RemoteException e) {
52            e.printStackTrace();
53            return null;
54        }
55     }
56
57    @Override
58    public byte[] getDSAKeyParamQ(AndroidPrivateKey key) {
59        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
60        try {
61            Log.d(TAG, "getDSAKeyParamQ");
62            return mRemoteManager.getDSAKeyParamQ(remoteKey.getHandle());
63        } catch (RemoteException e) {
64            e.printStackTrace();
65            return null;
66        }
67    }
68
69    @Override
70    public byte[] getECKeyOrder(AndroidPrivateKey key) {
71        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
72        try {
73            Log.d(TAG, "getECKeyOrder");
74            return mRemoteManager.getECKeyOrder(remoteKey.getHandle());
75        } catch (RemoteException e) {
76            e.printStackTrace();
77            return null;
78        }
79    }
80
81    @Override
82    public byte[] rawSignDigestWithPrivateKey(AndroidPrivateKey key, byte[] message) {
83        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
84        try {
85            Log.d(TAG, "rawSignDigestWithPrivateKey");
86            return mRemoteManager.rawSignDigestWithPrivateKey(remoteKey.getHandle(), message);
87        } catch (RemoteException e) {
88            e.printStackTrace();
89            return null;
90        }
91    }
92
93    @Override
94    public int getPrivateKeyType(AndroidPrivateKey key) {
95        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
96        try {
97            Log.d(TAG, "getPrivateKeyType");
98            return mRemoteManager.getPrivateKeyType(remoteKey.getHandle());
99        } catch (RemoteException e) {
100            e.printStackTrace();
101            return 0;
102        }
103    }
104
105    @Override
106    public byte[] getPrivateKeyEncodedBytes(AndroidPrivateKey key) {
107        // This should not be called as it's only for older versions of Android.
108        assert false;
109        return null;
110    }
111
112    @Override
113    public long getOpenSSLHandleForPrivateKey(AndroidPrivateKey privateKey) {
114        // This should not be called as it's only for older versions of Android.
115        assert false;
116        return 0;
117    }
118
119    @Override
120    public Object getOpenSSLEngineForPrivateKey(AndroidPrivateKey privateKey) {
121        // This should not be called as it's only for older versions of Android.
122        assert false;
123        return null;
124    }
125
126    public AndroidPrivateKey createKey(String alias) {
127        try {
128            int handle = mRemoteManager.getPrivateKeyHandle(alias);
129            return new RemotePrivateKey(handle, this);
130        } catch (RemoteException e) {
131            e.printStackTrace();
132            return null;
133        }
134    }
135
136    @Override
137    public void releaseKey(AndroidPrivateKey key) {
138        RemotePrivateKey remoteKey = (RemotePrivateKey) key;
139        try {
140            Log.d(TAG, "releaseKey");
141            mRemoteManager.releaseKey(remoteKey.getHandle());
142        } catch (RemoteException e) {
143            e.printStackTrace();
144        }
145    }
146}
147