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, int flags) 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 _data.writeInt(flags); 91 mRemote.transact(Stub.TRANSACTION_insert, _data, _reply, 0); 92 _reply.readException(); 93 _result = _reply.readInt(); 94 } finally { 95 _reply.recycle(); 96 _data.recycle(); 97 } 98 return _result; 99 } 100 101 public int del(String name, int uid) throws RemoteException { 102 Parcel _data = Parcel.obtain(); 103 Parcel _reply = Parcel.obtain(); 104 int _result; 105 try { 106 _data.writeInterfaceToken(DESCRIPTOR); 107 _data.writeString(name); 108 _data.writeInt(uid); 109 mRemote.transact(Stub.TRANSACTION_del, _data, _reply, 0); 110 _reply.readException(); 111 _result = _reply.readInt(); 112 } finally { 113 _reply.recycle(); 114 _data.recycle(); 115 } 116 return _result; 117 } 118 119 public int exist(String name, int uid) throws RemoteException { 120 Parcel _data = Parcel.obtain(); 121 Parcel _reply = Parcel.obtain(); 122 int _result; 123 try { 124 _data.writeInterfaceToken(DESCRIPTOR); 125 _data.writeString(name); 126 _data.writeInt(uid); 127 mRemote.transact(Stub.TRANSACTION_exist, _data, _reply, 0); 128 _reply.readException(); 129 _result = _reply.readInt(); 130 } finally { 131 _reply.recycle(); 132 _data.recycle(); 133 } 134 return _result; 135 } 136 137 public String[] saw(String name, int uid) throws RemoteException { 138 Parcel _data = Parcel.obtain(); 139 Parcel _reply = Parcel.obtain(); 140 String[] _result; 141 try { 142 _data.writeInterfaceToken(DESCRIPTOR); 143 _data.writeString(name); 144 _data.writeInt(uid); 145 mRemote.transact(Stub.TRANSACTION_saw, _data, _reply, 0); 146 _reply.readException(); 147 int size = _reply.readInt(); 148 _result = new String[size]; 149 for (int i = 0; i < size; i++) { 150 _result[i] = _reply.readString(); 151 } 152 int _ret = _reply.readInt(); 153 if (_ret != 1) { 154 return null; 155 } 156 } finally { 157 _reply.recycle(); 158 _data.recycle(); 159 } 160 return _result; 161 } 162 163 @Override 164 public int reset() throws RemoteException { 165 Parcel _data = Parcel.obtain(); 166 Parcel _reply = Parcel.obtain(); 167 int _result; 168 try { 169 _data.writeInterfaceToken(DESCRIPTOR); 170 mRemote.transact(Stub.TRANSACTION_reset, _data, _reply, 0); 171 _reply.readException(); 172 _result = _reply.readInt(); 173 } finally { 174 _reply.recycle(); 175 _data.recycle(); 176 } 177 return _result; 178 } 179 180 public int password(String password) throws RemoteException { 181 Parcel _data = Parcel.obtain(); 182 Parcel _reply = Parcel.obtain(); 183 int _result; 184 try { 185 _data.writeInterfaceToken(DESCRIPTOR); 186 _data.writeString(password); 187 mRemote.transact(Stub.TRANSACTION_password, _data, _reply, 0); 188 _reply.readException(); 189 _result = _reply.readInt(); 190 } finally { 191 _reply.recycle(); 192 _data.recycle(); 193 } 194 return _result; 195 } 196 197 public int lock() throws RemoteException { 198 Parcel _data = Parcel.obtain(); 199 Parcel _reply = Parcel.obtain(); 200 int _result; 201 try { 202 _data.writeInterfaceToken(DESCRIPTOR); 203 mRemote.transact(Stub.TRANSACTION_lock, _data, _reply, 0); 204 _reply.readException(); 205 _result = _reply.readInt(); 206 } finally { 207 _reply.recycle(); 208 _data.recycle(); 209 } 210 return _result; 211 } 212 213 public int unlock(String password) throws RemoteException { 214 Parcel _data = Parcel.obtain(); 215 Parcel _reply = Parcel.obtain(); 216 int _result; 217 try { 218 _data.writeInterfaceToken(DESCRIPTOR); 219 _data.writeString(password); 220 mRemote.transact(Stub.TRANSACTION_unlock, _data, _reply, 0); 221 _reply.readException(); 222 _result = _reply.readInt(); 223 } finally { 224 _reply.recycle(); 225 _data.recycle(); 226 } 227 return _result; 228 } 229 230 @Override 231 public int zero() throws RemoteException { 232 Parcel _data = Parcel.obtain(); 233 Parcel _reply = Parcel.obtain(); 234 int _result; 235 try { 236 _data.writeInterfaceToken(DESCRIPTOR); 237 mRemote.transact(Stub.TRANSACTION_zero, _data, _reply, 0); 238 _reply.readException(); 239 _result = _reply.readInt(); 240 } finally { 241 _reply.recycle(); 242 _data.recycle(); 243 } 244 return _result; 245 } 246 247 public int generate(String name, int uid, int keyType, int keySize, int flags, 248 byte[][] args) throws RemoteException { 249 Parcel _data = Parcel.obtain(); 250 Parcel _reply = Parcel.obtain(); 251 int _result; 252 try { 253 _data.writeInterfaceToken(DESCRIPTOR); 254 _data.writeString(name); 255 _data.writeInt(uid); 256 _data.writeInt(keyType); 257 _data.writeInt(keySize); 258 _data.writeInt(flags); 259 if (args == null) { 260 _data.writeInt(0); 261 } else { 262 _data.writeInt(args.length); 263 for (int i = 0; i < args.length; i++) { 264 _data.writeByteArray(args[i]); 265 } 266 } 267 mRemote.transact(Stub.TRANSACTION_generate, _data, _reply, 0); 268 _reply.readException(); 269 _result = _reply.readInt(); 270 } finally { 271 _reply.recycle(); 272 _data.recycle(); 273 } 274 return _result; 275 } 276 277 public int import_key(String name, byte[] data, int uid, int flags) 278 throws RemoteException { 279 Parcel _data = Parcel.obtain(); 280 Parcel _reply = Parcel.obtain(); 281 int _result; 282 try { 283 _data.writeInterfaceToken(DESCRIPTOR); 284 _data.writeString(name); 285 _data.writeByteArray(data); 286 _data.writeInt(uid); 287 _data.writeInt(flags); 288 mRemote.transact(Stub.TRANSACTION_import, _data, _reply, 0); 289 _reply.readException(); 290 _result = _reply.readInt(); 291 } finally { 292 _reply.recycle(); 293 _data.recycle(); 294 } 295 return _result; 296 } 297 298 public byte[] sign(String name, byte[] data) throws RemoteException { 299 Parcel _data = Parcel.obtain(); 300 Parcel _reply = Parcel.obtain(); 301 byte[] _result; 302 try { 303 _data.writeInterfaceToken(DESCRIPTOR); 304 _data.writeString(name); 305 _data.writeByteArray(data); 306 mRemote.transact(Stub.TRANSACTION_sign, _data, _reply, 0); 307 _reply.readException(); 308 _result = _reply.createByteArray(); 309 } finally { 310 _reply.recycle(); 311 _data.recycle(); 312 } 313 return _result; 314 } 315 316 public int verify(String name, byte[] data, byte[] signature) throws RemoteException { 317 Parcel _data = Parcel.obtain(); 318 Parcel _reply = Parcel.obtain(); 319 int _result; 320 try { 321 _data.writeInterfaceToken(DESCRIPTOR); 322 _data.writeString(name); 323 _data.writeByteArray(data); 324 _data.writeByteArray(signature); 325 mRemote.transact(Stub.TRANSACTION_verify, _data, _reply, 0); 326 _reply.readException(); 327 _result = _reply.readInt(); 328 } finally { 329 _reply.recycle(); 330 _data.recycle(); 331 } 332 return _result; 333 } 334 335 public byte[] get_pubkey(String name) throws RemoteException { 336 Parcel _data = Parcel.obtain(); 337 Parcel _reply = Parcel.obtain(); 338 byte[] _result; 339 try { 340 _data.writeInterfaceToken(DESCRIPTOR); 341 _data.writeString(name); 342 mRemote.transact(Stub.TRANSACTION_get_pubkey, _data, _reply, 0); 343 _reply.readException(); 344 _result = _reply.createByteArray(); 345 } finally { 346 _reply.recycle(); 347 _data.recycle(); 348 } 349 return _result; 350 } 351 352 public int del_key(String name, int uid) throws RemoteException { 353 Parcel _data = Parcel.obtain(); 354 Parcel _reply = Parcel.obtain(); 355 int _result; 356 try { 357 _data.writeInterfaceToken(DESCRIPTOR); 358 _data.writeString(name); 359 _data.writeInt(uid); 360 mRemote.transact(Stub.TRANSACTION_del_key, _data, _reply, 0); 361 _reply.readException(); 362 _result = _reply.readInt(); 363 } finally { 364 _reply.recycle(); 365 _data.recycle(); 366 } 367 return _result; 368 } 369 370 public int grant(String name, int granteeUid) throws RemoteException { 371 Parcel _data = Parcel.obtain(); 372 Parcel _reply = Parcel.obtain(); 373 int _result; 374 try { 375 _data.writeInterfaceToken(DESCRIPTOR); 376 _data.writeString(name); 377 _data.writeInt(granteeUid); 378 mRemote.transact(Stub.TRANSACTION_grant, _data, _reply, 0); 379 _reply.readException(); 380 _result = _reply.readInt(); 381 } finally { 382 _reply.recycle(); 383 _data.recycle(); 384 } 385 return _result; 386 } 387 388 public int ungrant(String name, int granteeUid) throws RemoteException { 389 Parcel _data = Parcel.obtain(); 390 Parcel _reply = Parcel.obtain(); 391 int _result; 392 try { 393 _data.writeInterfaceToken(DESCRIPTOR); 394 _data.writeString(name); 395 _data.writeInt(granteeUid); 396 mRemote.transact(Stub.TRANSACTION_ungrant, _data, _reply, 0); 397 _reply.readException(); 398 _result = _reply.readInt(); 399 } finally { 400 _reply.recycle(); 401 _data.recycle(); 402 } 403 return _result; 404 } 405 406 @Override 407 public long getmtime(String name) throws RemoteException { 408 Parcel _data = Parcel.obtain(); 409 Parcel _reply = Parcel.obtain(); 410 long _result; 411 try { 412 _data.writeInterfaceToken(DESCRIPTOR); 413 _data.writeString(name); 414 mRemote.transact(Stub.TRANSACTION_getmtime, _data, _reply, 0); 415 _reply.readException(); 416 _result = _reply.readLong(); 417 } finally { 418 _reply.recycle(); 419 _data.recycle(); 420 } 421 return _result; 422 } 423 424 @Override 425 public int duplicate(String srcKey, int srcUid, String destKey, int destUid) 426 throws RemoteException { 427 Parcel _data = Parcel.obtain(); 428 Parcel _reply = Parcel.obtain(); 429 int _result; 430 try { 431 _data.writeInterfaceToken(DESCRIPTOR); 432 _data.writeString(srcKey); 433 _data.writeInt(srcUid); 434 _data.writeString(destKey); 435 _data.writeInt(destUid); 436 mRemote.transact(Stub.TRANSACTION_duplicate, _data, _reply, 0); 437 _reply.readException(); 438 _result = _reply.readInt(); 439 } finally { 440 _reply.recycle(); 441 _data.recycle(); 442 } 443 return _result; 444 } 445 446 @Override 447 public int is_hardware_backed(String keyType) throws RemoteException { 448 Parcel _data = Parcel.obtain(); 449 Parcel _reply = Parcel.obtain(); 450 int _result; 451 try { 452 _data.writeInterfaceToken(DESCRIPTOR); 453 _data.writeString(keyType); 454 mRemote.transact(Stub.TRANSACTION_is_hardware_backed, _data, _reply, 0); 455 _reply.readException(); 456 _result = _reply.readInt(); 457 } finally { 458 _reply.recycle(); 459 _data.recycle(); 460 } 461 return _result; 462 } 463 464 @Override 465 public int clear_uid(long uid) throws RemoteException { 466 Parcel _data = Parcel.obtain(); 467 Parcel _reply = Parcel.obtain(); 468 int _result; 469 try { 470 _data.writeInterfaceToken(DESCRIPTOR); 471 _data.writeLong(uid); 472 mRemote.transact(Stub.TRANSACTION_clear_uid, _data, _reply, 0); 473 _reply.readException(); 474 _result = _reply.readInt(); 475 } finally { 476 _reply.recycle(); 477 _data.recycle(); 478 } 479 return _result; 480 } 481 482 public int reset_uid(int uid) throws RemoteException { 483 Parcel _data = Parcel.obtain(); 484 Parcel _reply = Parcel.obtain(); 485 int _result; 486 try { 487 _data.writeInterfaceToken(DESCRIPTOR); 488 _data.writeInt(uid); 489 mRemote.transact(Stub.TRANSACTION_reset_uid, _data, _reply, 0); 490 _reply.readException(); 491 _result = _reply.readInt(); 492 } finally { 493 _reply.recycle(); 494 _data.recycle(); 495 } 496 return _result; 497 } 498 499 public int sync_uid(int srcUid, int dstUid) throws RemoteException { 500 Parcel _data = Parcel.obtain(); 501 Parcel _reply = Parcel.obtain(); 502 int _result; 503 try { 504 _data.writeInterfaceToken(DESCRIPTOR); 505 _data.writeInt(srcUid); 506 _data.writeInt(dstUid); 507 mRemote.transact(Stub.TRANSACTION_sync_uid, _data, _reply, 0); 508 _reply.readException(); 509 _result = _reply.readInt(); 510 } finally { 511 _reply.recycle(); 512 _data.recycle(); 513 } 514 return _result; 515 } 516 517 public int password_uid(String password, int uid) throws RemoteException { 518 Parcel _data = Parcel.obtain(); 519 Parcel _reply = Parcel.obtain(); 520 int _result; 521 try { 522 _data.writeInterfaceToken(DESCRIPTOR); 523 _data.writeString(password); 524 _data.writeInt(uid); 525 mRemote.transact(Stub.TRANSACTION_password_uid, _data, _reply, 0); 526 _reply.readException(); 527 _result = _reply.readInt(); 528 } finally { 529 _reply.recycle(); 530 _data.recycle(); 531 } 532 return _result; 533 } 534 } 535 536 private static final String DESCRIPTOR = "android.security.keystore"; 537 538 static final int TRANSACTION_test = IBinder.FIRST_CALL_TRANSACTION + 0; 539 static final int TRANSACTION_get = IBinder.FIRST_CALL_TRANSACTION + 1; 540 static final int TRANSACTION_insert = IBinder.FIRST_CALL_TRANSACTION + 2; 541 static final int TRANSACTION_del = IBinder.FIRST_CALL_TRANSACTION + 3; 542 static final int TRANSACTION_exist = IBinder.FIRST_CALL_TRANSACTION + 4; 543 static final int TRANSACTION_saw = IBinder.FIRST_CALL_TRANSACTION + 5; 544 static final int TRANSACTION_reset = IBinder.FIRST_CALL_TRANSACTION + 6; 545 static final int TRANSACTION_password = IBinder.FIRST_CALL_TRANSACTION + 7; 546 static final int TRANSACTION_lock = IBinder.FIRST_CALL_TRANSACTION + 8; 547 static final int TRANSACTION_unlock = IBinder.FIRST_CALL_TRANSACTION + 9; 548 static final int TRANSACTION_zero = IBinder.FIRST_CALL_TRANSACTION + 10; 549 static final int TRANSACTION_generate = IBinder.FIRST_CALL_TRANSACTION + 11; 550 static final int TRANSACTION_import = IBinder.FIRST_CALL_TRANSACTION + 12; 551 static final int TRANSACTION_sign = IBinder.FIRST_CALL_TRANSACTION + 13; 552 static final int TRANSACTION_verify = IBinder.FIRST_CALL_TRANSACTION + 14; 553 static final int TRANSACTION_get_pubkey = IBinder.FIRST_CALL_TRANSACTION + 15; 554 static final int TRANSACTION_del_key = IBinder.FIRST_CALL_TRANSACTION + 16; 555 static final int TRANSACTION_grant = IBinder.FIRST_CALL_TRANSACTION + 17; 556 static final int TRANSACTION_ungrant = IBinder.FIRST_CALL_TRANSACTION + 18; 557 static final int TRANSACTION_getmtime = IBinder.FIRST_CALL_TRANSACTION + 19; 558 static final int TRANSACTION_duplicate = IBinder.FIRST_CALL_TRANSACTION + 20; 559 static final int TRANSACTION_is_hardware_backed = IBinder.FIRST_CALL_TRANSACTION + 21; 560 static final int TRANSACTION_clear_uid = IBinder.FIRST_CALL_TRANSACTION + 22; 561 static final int TRANSACTION_reset_uid = IBinder.FIRST_CALL_TRANSACTION + 23; 562 static final int TRANSACTION_sync_uid = IBinder.FIRST_CALL_TRANSACTION + 24; 563 static final int TRANSACTION_password_uid = IBinder.FIRST_CALL_TRANSACTION + 25; 564 565 /** 566 * Cast an IBinder object into an IKeystoreService interface, generating 567 * a proxy if needed. 568 */ 569 public static IKeystoreService asInterface(IBinder obj) { 570 if (obj == null) { 571 return null; 572 } 573 IInterface iin = obj.queryLocalInterface(DESCRIPTOR); 574 if (iin != null && iin instanceof IKeystoreService) { 575 return (IKeystoreService) iin; 576 } 577 return new IKeystoreService.Stub.Proxy(obj); 578 } 579 580 /** Construct the stub at attach it to the interface. */ 581 public Stub() { 582 attachInterface(this, DESCRIPTOR); 583 } 584 585 public IBinder asBinder() { 586 return this; 587 } 588 589 @Override 590 public boolean onTransact(int code, Parcel data, Parcel reply, int flags) 591 throws RemoteException { 592 switch (code) { 593 case INTERFACE_TRANSACTION: { 594 reply.writeString(DESCRIPTOR); 595 return true; 596 } 597 case TRANSACTION_test: { 598 data.enforceInterface(DESCRIPTOR); 599 int resultCode = test(); 600 reply.writeNoException(); 601 reply.writeInt(resultCode); 602 return true; 603 } 604 } 605 return super.onTransact(code, data, reply, flags); 606 } 607 } 608 609 public int test() throws RemoteException; 610 611 public byte[] get(String name) throws RemoteException; 612 613 public int insert(String name, byte[] item, int uid, int flags) throws RemoteException; 614 615 public int del(String name, int uid) throws RemoteException; 616 617 public int exist(String name, int uid) throws RemoteException; 618 619 public String[] saw(String name, int uid) throws RemoteException; 620 621 public int reset() throws RemoteException; 622 623 public int password(String password) throws RemoteException; 624 625 public int lock() throws RemoteException; 626 627 public int unlock(String password) throws RemoteException; 628 629 public int zero() throws RemoteException; 630 631 public int generate(String name, int uid, int keyType, int keySize, int flags, byte[][] args) 632 throws RemoteException; 633 634 public int import_key(String name, byte[] data, int uid, int flags) throws RemoteException; 635 636 public byte[] sign(String name, byte[] data) throws RemoteException; 637 638 public int verify(String name, byte[] data, byte[] signature) throws RemoteException; 639 640 public byte[] get_pubkey(String name) throws RemoteException; 641 642 public int del_key(String name, int uid) throws RemoteException; 643 644 public int grant(String name, int granteeUid) throws RemoteException; 645 646 public int ungrant(String name, int granteeUid) throws RemoteException; 647 648 public long getmtime(String name) throws RemoteException; 649 650 public int duplicate(String srcKey, int srcUid, String destKey, int destUid) 651 throws RemoteException; 652 653 public int is_hardware_backed(String string) throws RemoteException; 654 655 public int clear_uid(long uid) throws RemoteException; 656 657 public int reset_uid(int uid) throws RemoteException; 658 659 public int sync_uid(int sourceUid, int targetUid) throws RemoteException; 660 661 public int password_uid(String password, int uid) throws RemoteException; 662} 663