IKeystoreService.java revision 5cb5cec6a4a4d5432d4ce6468c12de9508db1633
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.security;
18
19import android.os.Binder;
20import android.os.IBinder;
21import android.os.IInterface;
22import android.os.Parcel;
23import android.os.RemoteException;
24
25/**
26 * This must be kept manually in sync with system/security/keystore until AIDL
27 * can generate both Java and C++ bindings.
28 *
29 * @hide
30 */
31public interface IKeystoreService extends IInterface {
32    public static abstract class Stub extends Binder implements IKeystoreService {
33        private static class Proxy implements IKeystoreService {
34            private final IBinder mRemote;
35
36            Proxy(IBinder remote) {
37                mRemote = remote;
38            }
39
40            public IBinder asBinder() {
41                return mRemote;
42            }
43
44            public String getInterfaceDescriptor() {
45                return DESCRIPTOR;
46            }
47
48            public int test() throws RemoteException {
49                Parcel _data = Parcel.obtain();
50                Parcel _reply = Parcel.obtain();
51                int _result;
52                try {
53                    _data.writeInterfaceToken(DESCRIPTOR);
54                    mRemote.transact(Stub.TRANSACTION_test, _data, _reply, 0);
55                    _reply.readException();
56                    _result = _reply.readInt();
57                } finally {
58                    _reply.recycle();
59                    _data.recycle();
60                }
61                return _result;
62            }
63
64            public byte[] get(String name) throws RemoteException {
65                Parcel _data = Parcel.obtain();
66                Parcel _reply = Parcel.obtain();
67                byte[] _result;
68                try {
69                    _data.writeInterfaceToken(DESCRIPTOR);
70                    _data.writeString(name);
71                    mRemote.transact(Stub.TRANSACTION_get, _data, _reply, 0);
72                    _reply.readException();
73                    _result = _reply.createByteArray();
74                } finally {
75                    _reply.recycle();
76                    _data.recycle();
77                }
78                return _result;
79            }
80
81            public int insert(String name, byte[] item, int uid) throws RemoteException {
82                Parcel _data = Parcel.obtain();
83                Parcel _reply = Parcel.obtain();
84                int _result;
85                try {
86                    _data.writeInterfaceToken(DESCRIPTOR);
87                    _data.writeString(name);
88                    _data.writeByteArray(item);
89                    _data.writeInt(uid);
90                    mRemote.transact(Stub.TRANSACTION_insert, _data, _reply, 0);
91                    _reply.readException();
92                    _result = _reply.readInt();
93                } finally {
94                    _reply.recycle();
95                    _data.recycle();
96                }
97                return _result;
98            }
99
100            public int del(String name, int uid) throws RemoteException {
101                Parcel _data = Parcel.obtain();
102                Parcel _reply = Parcel.obtain();
103                int _result;
104                try {
105                    _data.writeInterfaceToken(DESCRIPTOR);
106                    _data.writeString(name);
107                    _data.writeInt(uid);
108                    mRemote.transact(Stub.TRANSACTION_del, _data, _reply, 0);
109                    _reply.readException();
110                    _result = _reply.readInt();
111                } finally {
112                    _reply.recycle();
113                    _data.recycle();
114                }
115                return _result;
116            }
117
118            public int exist(String name, int uid) throws RemoteException {
119                Parcel _data = Parcel.obtain();
120                Parcel _reply = Parcel.obtain();
121                int _result;
122                try {
123                    _data.writeInterfaceToken(DESCRIPTOR);
124                    _data.writeString(name);
125                    _data.writeInt(uid);
126                    mRemote.transact(Stub.TRANSACTION_exist, _data, _reply, 0);
127                    _reply.readException();
128                    _result = _reply.readInt();
129                } finally {
130                    _reply.recycle();
131                    _data.recycle();
132                }
133                return _result;
134            }
135
136            public String[] saw(String name, int uid) throws RemoteException {
137                Parcel _data = Parcel.obtain();
138                Parcel _reply = Parcel.obtain();
139                String[] _result;
140                try {
141                    _data.writeInterfaceToken(DESCRIPTOR);
142                    _data.writeString(name);
143                    _data.writeInt(uid);
144                    mRemote.transact(Stub.TRANSACTION_saw, _data, _reply, 0);
145                    _reply.readException();
146                    int size = _reply.readInt();
147                    _result = new String[size];
148                    for (int i = 0; i < size; i++) {
149                        _result[i] = _reply.readString();
150                    }
151                    int _ret = _reply.readInt();
152                    if (_ret != 1) {
153                        return null;
154                    }
155                } finally {
156                    _reply.recycle();
157                    _data.recycle();
158                }
159                return _result;
160            }
161
162            @Override
163            public int reset() throws RemoteException {
164                Parcel _data = Parcel.obtain();
165                Parcel _reply = Parcel.obtain();
166                int _result;
167                try {
168                    _data.writeInterfaceToken(DESCRIPTOR);
169                    mRemote.transact(Stub.TRANSACTION_reset, _data, _reply, 0);
170                    _reply.readException();
171                    _result = _reply.readInt();
172                } finally {
173                    _reply.recycle();
174                    _data.recycle();
175                }
176                return _result;
177            }
178
179            public int password(String password) throws RemoteException {
180                Parcel _data = Parcel.obtain();
181                Parcel _reply = Parcel.obtain();
182                int _result;
183                try {
184                    _data.writeInterfaceToken(DESCRIPTOR);
185                    _data.writeString(password);
186                    mRemote.transact(Stub.TRANSACTION_password, _data, _reply, 0);
187                    _reply.readException();
188                    _result = _reply.readInt();
189                } finally {
190                    _reply.recycle();
191                    _data.recycle();
192                }
193                return _result;
194            }
195
196            public int lock() throws RemoteException {
197                Parcel _data = Parcel.obtain();
198                Parcel _reply = Parcel.obtain();
199                int _result;
200                try {
201                    _data.writeInterfaceToken(DESCRIPTOR);
202                    mRemote.transact(Stub.TRANSACTION_lock, _data, _reply, 0);
203                    _reply.readException();
204                    _result = _reply.readInt();
205                } finally {
206                    _reply.recycle();
207                    _data.recycle();
208                }
209                return _result;
210            }
211
212            public int unlock(String password) throws RemoteException {
213                Parcel _data = Parcel.obtain();
214                Parcel _reply = Parcel.obtain();
215                int _result;
216                try {
217                    _data.writeInterfaceToken(DESCRIPTOR);
218                    _data.writeString(password);
219                    mRemote.transact(Stub.TRANSACTION_unlock, _data, _reply, 0);
220                    _reply.readException();
221                    _result = _reply.readInt();
222                } finally {
223                    _reply.recycle();
224                    _data.recycle();
225                }
226                return _result;
227            }
228
229            @Override
230            public int zero() throws RemoteException {
231                Parcel _data = Parcel.obtain();
232                Parcel _reply = Parcel.obtain();
233                int _result;
234                try {
235                    _data.writeInterfaceToken(DESCRIPTOR);
236                    mRemote.transact(Stub.TRANSACTION_zero, _data, _reply, 0);
237                    _reply.readException();
238                    _result = _reply.readInt();
239                } finally {
240                    _reply.recycle();
241                    _data.recycle();
242                }
243                return _result;
244            }
245
246            public int generate(String name, int uid) throws RemoteException {
247                Parcel _data = Parcel.obtain();
248                Parcel _reply = Parcel.obtain();
249                int _result;
250                try {
251                    _data.writeInterfaceToken(DESCRIPTOR);
252                    _data.writeString(name);
253                    _data.writeInt(uid);
254                    mRemote.transact(Stub.TRANSACTION_generate, _data, _reply, 0);
255                    _reply.readException();
256                    _result = _reply.readInt();
257                } finally {
258                    _reply.recycle();
259                    _data.recycle();
260                }
261                return _result;
262            }
263
264            public int import_key(String name, byte[] data, int uid) throws RemoteException {
265                Parcel _data = Parcel.obtain();
266                Parcel _reply = Parcel.obtain();
267                int _result;
268                try {
269                    _data.writeInterfaceToken(DESCRIPTOR);
270                    _data.writeString(name);
271                    _data.writeByteArray(data);
272                    _data.writeInt(uid);
273                    mRemote.transact(Stub.TRANSACTION_import, _data, _reply, 0);
274                    _reply.readException();
275                    _result = _reply.readInt();
276                } finally {
277                    _reply.recycle();
278                    _data.recycle();
279                }
280                return _result;
281            }
282
283            public byte[] sign(String name, byte[] data) throws RemoteException {
284                Parcel _data = Parcel.obtain();
285                Parcel _reply = Parcel.obtain();
286                byte[] _result;
287                try {
288                    _data.writeInterfaceToken(DESCRIPTOR);
289                    _data.writeString(name);
290                    _data.writeByteArray(data);
291                    mRemote.transact(Stub.TRANSACTION_sign, _data, _reply, 0);
292                    _reply.readException();
293                    _result = _reply.createByteArray();
294                } finally {
295                    _reply.recycle();
296                    _data.recycle();
297                }
298                return _result;
299            }
300
301            public int verify(String name, byte[] data, byte[] signature) throws RemoteException {
302                Parcel _data = Parcel.obtain();
303                Parcel _reply = Parcel.obtain();
304                int _result;
305                try {
306                    _data.writeInterfaceToken(DESCRIPTOR);
307                    _data.writeString(name);
308                    _data.writeByteArray(data);
309                    _data.writeByteArray(signature);
310                    mRemote.transact(Stub.TRANSACTION_verify, _data, _reply, 0);
311                    _reply.readException();
312                    _result = _reply.readInt();
313                } finally {
314                    _reply.recycle();
315                    _data.recycle();
316                }
317                return _result;
318            }
319
320            public byte[] get_pubkey(String name) throws RemoteException {
321                Parcel _data = Parcel.obtain();
322                Parcel _reply = Parcel.obtain();
323                byte[] _result;
324                try {
325                    _data.writeInterfaceToken(DESCRIPTOR);
326                    _data.writeString(name);
327                    mRemote.transact(Stub.TRANSACTION_get_pubkey, _data, _reply, 0);
328                    _reply.readException();
329                    _result = _reply.createByteArray();
330                } finally {
331                    _reply.recycle();
332                    _data.recycle();
333                }
334                return _result;
335            }
336
337            public int del_key(String name, int uid) throws RemoteException {
338                Parcel _data = Parcel.obtain();
339                Parcel _reply = Parcel.obtain();
340                int _result;
341                try {
342                    _data.writeInterfaceToken(DESCRIPTOR);
343                    _data.writeString(name);
344                    _data.writeInt(uid);
345                    mRemote.transact(Stub.TRANSACTION_del_key, _data, _reply, 0);
346                    _reply.readException();
347                    _result = _reply.readInt();
348                } finally {
349                    _reply.recycle();
350                    _data.recycle();
351                }
352                return _result;
353            }
354
355            public int grant(String name, int granteeUid) throws RemoteException {
356                Parcel _data = Parcel.obtain();
357                Parcel _reply = Parcel.obtain();
358                int _result;
359                try {
360                    _data.writeInterfaceToken(DESCRIPTOR);
361                    _data.writeString(name);
362                    _data.writeInt(granteeUid);
363                    mRemote.transact(Stub.TRANSACTION_grant, _data, _reply, 0);
364                    _reply.readException();
365                    _result = _reply.readInt();
366                } finally {
367                    _reply.recycle();
368                    _data.recycle();
369                }
370                return _result;
371            }
372
373            public int ungrant(String name, int granteeUid) throws RemoteException {
374                Parcel _data = Parcel.obtain();
375                Parcel _reply = Parcel.obtain();
376                int _result;
377                try {
378                    _data.writeInterfaceToken(DESCRIPTOR);
379                    _data.writeString(name);
380                    _data.writeInt(granteeUid);
381                    mRemote.transact(Stub.TRANSACTION_ungrant, _data, _reply, 0);
382                    _reply.readException();
383                    _result = _reply.readInt();
384                } finally {
385                    _reply.recycle();
386                    _data.recycle();
387                }
388                return _result;
389            }
390
391            @Override
392            public long getmtime(String name) throws RemoteException {
393                Parcel _data = Parcel.obtain();
394                Parcel _reply = Parcel.obtain();
395                long _result;
396                try {
397                    _data.writeInterfaceToken(DESCRIPTOR);
398                    _data.writeString(name);
399                    mRemote.transact(Stub.TRANSACTION_getmtime, _data, _reply, 0);
400                    _reply.readException();
401                    _result = _reply.readLong();
402                } finally {
403                    _reply.recycle();
404                    _data.recycle();
405                }
406                return _result;
407            }
408
409            @Override
410            public int duplicate(String srcKey, int srcUid, String destKey, int destUid)
411                    throws RemoteException {
412                Parcel _data = Parcel.obtain();
413                Parcel _reply = Parcel.obtain();
414                int _result;
415                try {
416                    _data.writeInterfaceToken(DESCRIPTOR);
417                    _data.writeString(srcKey);
418                    _data.writeInt(srcUid);
419                    _data.writeString(destKey);
420                    _data.writeInt(destUid);
421                    mRemote.transact(Stub.TRANSACTION_duplicate, _data, _reply, 0);
422                    _reply.readException();
423                    _result = _reply.readInt();
424                } finally {
425                    _reply.recycle();
426                    _data.recycle();
427                }
428                return _result;
429            }
430
431            @Override
432            public int is_hardware_backed() throws RemoteException {
433                Parcel _data = Parcel.obtain();
434                Parcel _reply = Parcel.obtain();
435                int _result;
436                try {
437                    _data.writeInterfaceToken(DESCRIPTOR);
438                    mRemote.transact(Stub.TRANSACTION_is_hardware_backed, _data, _reply, 0);
439                    _reply.readException();
440                    _result = _reply.readInt();
441                } finally {
442                    _reply.recycle();
443                    _data.recycle();
444                }
445                return _result;
446            }
447        }
448
449        private static final String DESCRIPTOR = "android.security.keystore";
450
451        static final int TRANSACTION_test = IBinder.FIRST_CALL_TRANSACTION + 0;
452        static final int TRANSACTION_get = IBinder.FIRST_CALL_TRANSACTION + 1;
453        static final int TRANSACTION_insert = IBinder.FIRST_CALL_TRANSACTION + 2;
454        static final int TRANSACTION_del = IBinder.FIRST_CALL_TRANSACTION + 3;
455        static final int TRANSACTION_exist = IBinder.FIRST_CALL_TRANSACTION + 4;
456        static final int TRANSACTION_saw = IBinder.FIRST_CALL_TRANSACTION + 5;
457        static final int TRANSACTION_reset = IBinder.FIRST_CALL_TRANSACTION + 6;
458        static final int TRANSACTION_password = IBinder.FIRST_CALL_TRANSACTION + 7;
459        static final int TRANSACTION_lock = IBinder.FIRST_CALL_TRANSACTION + 8;
460        static final int TRANSACTION_unlock = IBinder.FIRST_CALL_TRANSACTION + 9;
461        static final int TRANSACTION_zero = IBinder.FIRST_CALL_TRANSACTION + 10;
462        static final int TRANSACTION_generate = IBinder.FIRST_CALL_TRANSACTION + 11;
463        static final int TRANSACTION_import = IBinder.FIRST_CALL_TRANSACTION + 12;
464        static final int TRANSACTION_sign = IBinder.FIRST_CALL_TRANSACTION + 13;
465        static final int TRANSACTION_verify = IBinder.FIRST_CALL_TRANSACTION + 14;
466        static final int TRANSACTION_get_pubkey = IBinder.FIRST_CALL_TRANSACTION + 15;
467        static final int TRANSACTION_del_key = IBinder.FIRST_CALL_TRANSACTION + 16;
468        static final int TRANSACTION_grant = IBinder.FIRST_CALL_TRANSACTION + 17;
469        static final int TRANSACTION_ungrant = IBinder.FIRST_CALL_TRANSACTION + 18;
470        static final int TRANSACTION_getmtime = IBinder.FIRST_CALL_TRANSACTION + 19;
471        static final int TRANSACTION_duplicate = IBinder.FIRST_CALL_TRANSACTION + 20;
472        static final int TRANSACTION_is_hardware_backed = IBinder.FIRST_CALL_TRANSACTION + 21;
473
474        /**
475         * Cast an IBinder object into an IKeystoreService interface, generating
476         * a proxy if needed.
477         */
478        public static IKeystoreService asInterface(IBinder obj) {
479            if (obj == null) {
480                return null;
481            }
482            IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
483            if (iin != null && iin instanceof IKeystoreService) {
484                return (IKeystoreService) iin;
485            }
486            return new IKeystoreService.Stub.Proxy(obj);
487        }
488
489        /** Construct the stub at attach it to the interface. */
490        public Stub() {
491            attachInterface(this, DESCRIPTOR);
492        }
493
494        public IBinder asBinder() {
495            return this;
496        }
497
498        @Override
499        public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
500                throws RemoteException {
501            switch (code) {
502                case INTERFACE_TRANSACTION: {
503                    reply.writeString(DESCRIPTOR);
504                    return true;
505                }
506                case TRANSACTION_test: {
507                    data.enforceInterface(DESCRIPTOR);
508                    int resultCode = test();
509                    reply.writeNoException();
510                    reply.writeInt(resultCode);
511                    return true;
512                }
513            }
514            return super.onTransact(code, data, reply, flags);
515        }
516    }
517
518    public int test() throws RemoteException;
519
520    public byte[] get(String name) throws RemoteException;
521
522    public int insert(String name, byte[] item, int uid) throws RemoteException;
523
524    public int del(String name, int uid) throws RemoteException;
525
526    public int exist(String name, int uid) throws RemoteException;
527
528    public String[] saw(String name, int uid) throws RemoteException;
529
530    public int reset() throws RemoteException;
531
532    public int password(String password) throws RemoteException;
533
534    public int lock() throws RemoteException;
535
536    public int unlock(String password) throws RemoteException;
537
538    public int zero() throws RemoteException;
539
540    public int generate(String name, int uid) throws RemoteException;
541
542    public int import_key(String name, byte[] data, int uid) throws RemoteException;
543
544    public byte[] sign(String name, byte[] data) throws RemoteException;
545
546    public int verify(String name, byte[] data, byte[] signature) throws RemoteException;
547
548    public byte[] get_pubkey(String name) throws RemoteException;
549
550    public int del_key(String name, int uid) throws RemoteException;
551
552    public int grant(String name, int granteeUid) throws RemoteException;
553
554    public int ungrant(String name, int granteeUid) throws RemoteException;
555
556    public long getmtime(String name) throws RemoteException;
557
558    public int duplicate(String srcKey, int srcUid, String destKey, int destUid)
559            throws RemoteException;
560
561    public int is_hardware_backed() throws RemoteException;
562}
563