1 /* 2 * Copyright (C) 2008 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.sdklib.internal.build; 18 19 import com.android.sdklib.internal.build.DebugKeyProvider.IKeyGenOutput; 20 import com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException; 21 import com.android.sdklib.util.GrabProcessOutput; 22 import com.android.sdklib.util.GrabProcessOutput.IProcessOutput; 23 import com.android.sdklib.util.GrabProcessOutput.Wait; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.security.KeyStoreException; 28 import java.security.NoSuchAlgorithmException; 29 import java.security.UnrecoverableEntryException; 30 import java.security.cert.CertificateException; 31 import java.util.ArrayList; 32 33 /** 34 * A Helper to create new keystore/key. 35 */ 36 public final class KeystoreHelper { 37 38 /** 39 * Creates a new store 40 * @param osKeyStorePath the location of the store 41 * @param storeType an optional keystore type, or <code>null</code> if the default is to 42 * be used. 43 * @param output an optional {@link IKeyGenOutput} object to get the stdout and stderr 44 * of the keytool process call. 45 * @throws KeyStoreException 46 * @throws NoSuchAlgorithmException 47 * @throws CertificateException 48 * @throws UnrecoverableEntryException 49 * @throws IOException 50 * @throws KeytoolException 51 */ createNewStore( String osKeyStorePath, String storeType, String storePassword, String alias, String keyPassword, String description, int validityYears, final IKeyGenOutput output)52 public static boolean createNewStore( 53 String osKeyStorePath, 54 String storeType, 55 String storePassword, 56 String alias, 57 String keyPassword, 58 String description, 59 int validityYears, 60 final IKeyGenOutput output) 61 throws KeyStoreException, NoSuchAlgorithmException, CertificateException, 62 UnrecoverableEntryException, IOException, KeytoolException { 63 64 // get the executable name of keytool depending on the platform. 65 String os = System.getProperty("os.name"); 66 67 String keytoolCommand; 68 if (os.startsWith("Windows")) { 69 keytoolCommand = "keytool.exe"; 70 } else { 71 keytoolCommand = "keytool"; 72 } 73 74 String javaHome = System.getProperty("java.home"); 75 76 if (javaHome != null && javaHome.length() > 0) { 77 keytoolCommand = javaHome + File.separator + "bin" + File.separator + keytoolCommand; 78 } 79 80 // create the command line to call key tool to build the key with no user input. 81 ArrayList<String> commandList = new ArrayList<String>(); 82 commandList.add(keytoolCommand); 83 commandList.add("-genkey"); 84 commandList.add("-alias"); 85 commandList.add(alias); 86 commandList.add("-keyalg"); 87 commandList.add("RSA"); 88 commandList.add("-dname"); 89 commandList.add(description); 90 commandList.add("-validity"); 91 commandList.add(Integer.toString(validityYears * 365)); 92 commandList.add("-keypass"); 93 commandList.add(keyPassword); 94 commandList.add("-keystore"); 95 commandList.add(osKeyStorePath); 96 commandList.add("-storepass"); 97 commandList.add(storePassword); 98 if (storeType != null) { 99 commandList.add("-storetype"); 100 commandList.add(storeType); 101 } 102 103 String[] commandArray = commandList.toArray(new String[commandList.size()]); 104 105 // launch the command line process 106 int result = 0; 107 try { 108 Process process = Runtime.getRuntime().exec(commandArray); 109 result = GrabProcessOutput.grabProcessOutput( 110 process, 111 Wait.WAIT_FOR_READERS, 112 new IProcessOutput() { 113 @Override 114 public void out(String line) { 115 if (line != null) { 116 if (output != null) { 117 output.out(line); 118 } else { 119 System.out.println(line); 120 } 121 } 122 } 123 124 @Override 125 public void err(String line) { 126 if (line != null) { 127 if (output != null) { 128 output.err(line); 129 } else { 130 System.err.println(line); 131 } 132 } 133 } 134 }); 135 } catch (Exception e) { 136 // create the command line as one string for debugging purposes 137 StringBuilder builder = new StringBuilder(); 138 boolean firstArg = true; 139 for (String arg : commandArray) { 140 boolean hasSpace = arg.indexOf(' ') != -1; 141 142 if (firstArg == true) { 143 firstArg = false; 144 } else { 145 builder.append(' '); 146 } 147 148 if (hasSpace) { 149 builder.append('"'); 150 } 151 152 builder.append(arg); 153 154 if (hasSpace) { 155 builder.append('"'); 156 } 157 } 158 159 throw new KeytoolException("Failed to create key: " + e.getMessage(), 160 javaHome, builder.toString()); 161 } 162 163 if (result != 0) { 164 return false; 165 } 166 167 return true; 168 } 169 } 170