Chung-yih Wang | 10e371f | 2009-06-10 18:45:14 +0800 | [diff] [blame^] | 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 android.security; |
| 18 | |
| 19 | /** |
| 20 | * The Keystore class provides the functions to list the certs/keys in keystore. |
| 21 | * {@hide} |
| 22 | */ |
| 23 | public abstract class Keystore { |
| 24 | private static final String TAG = "Keystore"; |
| 25 | private static final String[] NOTFOUND = new String[0]; |
| 26 | |
| 27 | /** |
| 28 | */ |
| 29 | public static Keystore getInstance() { |
| 30 | return new FileKeystore(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | */ |
| 35 | public abstract String getUserkey(String key); |
| 36 | |
| 37 | /** |
| 38 | */ |
| 39 | public abstract String getCertificate(String key); |
| 40 | |
| 41 | /** |
| 42 | */ |
| 43 | public abstract String[] getAllCertificateKeys(); |
| 44 | |
| 45 | /** |
| 46 | */ |
| 47 | public abstract String[] getAllUserkeyKeys(); |
| 48 | |
| 49 | private static class FileKeystore extends Keystore { |
| 50 | private static final String SERVICE_NAME = "keystore"; |
| 51 | private static final String LIST_CERTIFICATES = "listcerts"; |
| 52 | private static final String LIST_USERKEYS = "listuserkeys"; |
| 53 | private static final String PATH = "/data/misc/keystore/"; |
| 54 | private static final String USERKEY_PATH = PATH + "userkeys/"; |
| 55 | private static final String CERT_PATH = PATH + "certs/"; |
| 56 | private static final ServiceCommand mServiceCommand = |
| 57 | new ServiceCommand(SERVICE_NAME); |
| 58 | |
| 59 | @Override |
| 60 | public String getUserkey(String key) { |
| 61 | return USERKEY_PATH + key; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String getCertificate(String key) { |
| 66 | return CERT_PATH + key; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the array of the certificate names in keystore if successful. |
| 71 | * Or return an empty array if error. |
| 72 | * |
| 73 | * @return array of the certificates |
| 74 | */ |
| 75 | @Override |
| 76 | public String[] getAllCertificateKeys() { |
| 77 | try { |
| 78 | String result = mServiceCommand.execute(LIST_CERTIFICATES); |
| 79 | if (result != null) return result.split("\\s+"); |
| 80 | return NOTFOUND; |
| 81 | } catch (NumberFormatException ex) { |
| 82 | return NOTFOUND; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the array of the names of private keys in keystore if successful. |
| 88 | * Or return an empty array if errors. |
| 89 | * |
| 90 | * @return array of the user keys |
| 91 | */ |
| 92 | @Override |
| 93 | public String[] getAllUserkeyKeys() { |
| 94 | try { |
| 95 | String result = mServiceCommand.execute(LIST_USERKEYS); |
| 96 | if (result != null) return result.split("\\s+"); |
| 97 | return NOTFOUND; |
| 98 | } catch (NumberFormatException ex) { |
| 99 | return NOTFOUND; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |