1 /* 2 * Copyright (C) 2009 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 17 package com.android.settings; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.security.Credentials; 23 import android.security.KeyStore; 24 import android.util.Log; 25 26 /** 27 * Installs credentials to the system keystore. It reacts to the 28 * {@link Credentials#SYSTEM_INSTALL_ACTION} intent. All the key-value pairs in 29 * the intent are installed to the system keystore. For security reason, the 30 * current implementation limits that only com.android.certinstaller can use 31 * this service. 32 */ 33 public class CredentialInstaller extends Activity { 34 private static final String TAG = "CredentialInstaller"; 35 36 private KeyStore mKeyStore = KeyStore.getInstance(); 37 private boolean mUnlocking = false; 38 39 @Override onResume()40 protected void onResume() { 41 super.onResume(); 42 43 if (!"com.android.certinstaller".equals(getCallingPackage())) finish(); 44 45 if (!isKeyStoreLocked()) { 46 install(); 47 finish(); 48 } else if (!mUnlocking) { 49 mUnlocking = true; 50 Credentials.getInstance().unlock(this); 51 } else { 52 finish(); 53 } 54 } 55 install()56 private void install() { 57 Intent intent = getIntent(); 58 Bundle bundle = (intent == null) ? null : intent.getExtras(); 59 if (bundle == null) return; 60 for (String key : bundle.keySet()) { 61 byte[] data = bundle.getByteArray(key); 62 if (data == null) continue; 63 boolean success = mKeyStore.put(key.getBytes(), data); 64 Log.v(TAG, "install " + key + ": " + data.length + " success? " + success); 65 if (!success) return; 66 } 67 setResult(RESULT_OK); 68 } 69 isKeyStoreLocked()70 private boolean isKeyStoreLocked() { 71 return (mKeyStore.test() != KeyStore.NO_ERROR); 72 } 73 } 74