Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | import android.os.Environment; |
Oscar Montemayor | 1ff8fee | 2010-02-22 11:16:51 -0800 | [diff] [blame] | 20 | import android.os.FileUtils; |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 21 | import android.os.Process; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.FileInputStream; |
| 25 | import java.io.FileOutputStream; |
| 26 | import java.io.IOException; |
| 27 | import java.security.NoSuchAlgorithmException; |
| 28 | import java.security.SecureRandom; |
| 29 | |
| 30 | import javax.crypto.KeyGenerator; |
| 31 | import javax.crypto.SecretKey; |
| 32 | |
Elliott Hughes | 460c26e | 2010-11-11 16:42:09 -0800 | [diff] [blame] | 33 | import libcore.io.IoUtils; |
| 34 | |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 35 | /** |
| 36 | *@hide |
| 37 | */ |
| 38 | public class SystemKeyStore { |
| 39 | |
| 40 | private static final String SYSTEM_KEYSTORE_DIRECTORY = "misc/systemkeys"; |
Oscar Montemayor | b62e813 | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 41 | private static final String KEY_FILE_EXTENSION = ".sks"; |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 42 | private static SystemKeyStore mInstance = new SystemKeyStore(); |
| 43 | |
| 44 | private SystemKeyStore() { } |
| 45 | |
| 46 | public static SystemKeyStore getInstance() { |
| 47 | return mInstance; |
| 48 | } |
| 49 | |
Oscar Montemayor | b62e813 | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 50 | public static String toHexString(byte[] keyData) { |
| 51 | if (keyData == null) { |
| 52 | return null; |
| 53 | } |
| 54 | int keyLen = keyData.length; |
| 55 | int expectedStringLen = keyData.length * 2; |
| 56 | StringBuilder sb = new StringBuilder(expectedStringLen); |
| 57 | for (int i = 0; i < keyData.length; i++) { |
| 58 | String hexStr = Integer.toString(keyData[i] & 0x00FF, 16); |
| 59 | if (hexStr.length() == 1) { |
| 60 | hexStr = "0" + hexStr; |
| 61 | } |
| 62 | sb.append(hexStr); |
| 63 | } |
| 64 | return sb.toString(); |
| 65 | } |
| 66 | |
| 67 | public String generateNewKeyHexString(int numBits, String algName, String keyName) |
| 68 | throws NoSuchAlgorithmException { |
| 69 | return toHexString(generateNewKey(numBits, algName, keyName)); |
| 70 | } |
| 71 | |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 72 | public byte[] generateNewKey(int numBits, String algName, String keyName) |
| 73 | throws NoSuchAlgorithmException { |
| 74 | |
| 75 | // Check if key with similar name exists. If so, return null. |
| 76 | File keyFile = getKeyFile(keyName); |
| 77 | if (keyFile.exists()) { |
| 78 | throw new IllegalArgumentException(); |
| 79 | } |
| 80 | |
| 81 | KeyGenerator skg = KeyGenerator.getInstance(algName); |
| 82 | SecureRandom srng = SecureRandom.getInstance("SHA1PRNG"); |
| 83 | skg.init(numBits, srng); |
| 84 | |
| 85 | SecretKey sk = skg.generateKey(); |
| 86 | byte[] retKey = sk.getEncoded(); |
| 87 | |
| 88 | try { |
| 89 | // Store the key |
| 90 | if (!keyFile.createNewFile()) { |
| 91 | throw new IllegalArgumentException(); |
| 92 | } |
| 93 | |
| 94 | FileOutputStream fos = new FileOutputStream(keyFile); |
| 95 | fos.write(retKey); |
| 96 | fos.flush(); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 97 | FileUtils.sync(fos); |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 98 | fos.close(); |
Oscar Montemayor | 1ff8fee | 2010-02-22 11:16:51 -0800 | [diff] [blame] | 99 | FileUtils.setPermissions(keyFile.getName(), (FileUtils.S_IRUSR | FileUtils.S_IWUSR), |
| 100 | -1, -1); |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 101 | } catch (IOException ioe) { |
| 102 | return null; |
| 103 | } |
| 104 | return retKey; |
| 105 | } |
| 106 | |
| 107 | private File getKeyFile(String keyName) { |
| 108 | File sysKeystoreDir = new File(Environment.getDataDirectory(), |
| 109 | SYSTEM_KEYSTORE_DIRECTORY); |
Oscar Montemayor | b62e813 | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 110 | File keyFile = new File(sysKeystoreDir, keyName + KEY_FILE_EXTENSION); |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 111 | return keyFile; |
| 112 | } |
| 113 | |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 114 | public String retrieveKeyHexString(String keyName) throws IOException { |
Oscar Montemayor | b62e813 | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 115 | return toHexString(retrieveKey(keyName)); |
| 116 | } |
| 117 | |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 118 | public byte[] retrieveKey(String keyName) throws IOException { |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 119 | File keyFile = getKeyFile(keyName); |
| 120 | if (!keyFile.exists()) { |
| 121 | return null; |
| 122 | } |
Elliott Hughes | 460c26e | 2010-11-11 16:42:09 -0800 | [diff] [blame] | 123 | return IoUtils.readFileAsByteArray(keyFile.toString()); |
Oscar Montemayor | 8da98e3 | 2010-01-06 11:35:59 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public void deleteKey(String keyName) { |
| 127 | |
| 128 | // Get the file first. |
| 129 | File keyFile = getKeyFile(keyName); |
| 130 | if (!keyFile.exists()) { |
| 131 | throw new IllegalArgumentException(); |
| 132 | } |
| 133 | |
| 134 | keyFile.delete(); |
| 135 | } |
| 136 | } |