use of com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException in project bazel by bazelbuild.
the class KeystoreHelper method createNewStore.
/**
* Creates a new store
* @param osKeyStorePath the location of the store
* @param storeType an optional keystore type, or <code>null</code> if the default is to
* be used.
* @param output an optional {@link IKeyGenOutput} object to get the stdout and stderr
* of the keytool process call.
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws UnrecoverableEntryException
* @throws IOException
* @throws KeytoolException
*/
public static boolean createNewStore(String osKeyStorePath, String storeType, String storePassword, String alias, String keyPassword, String description, int validityYears, final IKeyGenOutput output) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableEntryException, IOException, KeytoolException {
// get the executable name of keytool depending on the platform.
String os = System.getProperty("os.name");
String keytoolCommand;
if (os.startsWith("Windows")) {
keytoolCommand = "keytool.exe";
} else {
keytoolCommand = "keytool";
}
String javaHome = System.getProperty("java.home");
if (javaHome != null && !javaHome.isEmpty()) {
keytoolCommand = javaHome + File.separator + "bin" + File.separator + keytoolCommand;
}
// create the command line to call key tool to build the key with no user input.
ArrayList<String> commandList = new ArrayList<String>();
commandList.add(keytoolCommand);
commandList.add("-genkey");
commandList.add("-alias");
commandList.add(alias);
commandList.add("-keyalg");
commandList.add("RSA");
commandList.add("-dname");
commandList.add(description);
commandList.add("-validity");
commandList.add(Integer.toString(validityYears * 365));
commandList.add("-keypass");
commandList.add(keyPassword);
commandList.add("-keystore");
commandList.add(osKeyStorePath);
commandList.add("-storepass");
commandList.add(storePassword);
if (storeType != null) {
commandList.add("-storetype");
commandList.add(storeType);
}
String[] commandArray = commandList.toArray(new String[commandList.size()]);
// launch the command line process
int result = 0;
try {
Process process = Runtime.getRuntime().exec(commandArray);
result = GrabProcessOutput.grabProcessOutput(process, Wait.WAIT_FOR_READERS, new IProcessOutput() {
@Override
public void out(@Nullable String line) {
if (line != null) {
if (output != null) {
output.out(line);
} else {
System.out.println(line);
}
}
}
@Override
public void err(@Nullable String line) {
if (line != null) {
if (output != null) {
output.err(line);
} else {
System.err.println(line);
}
}
}
});
} catch (Exception e) {
// create the command line as one string for debugging purposes
StringBuilder builder = new StringBuilder();
boolean firstArg = true;
for (String arg : commandArray) {
boolean hasSpace = arg.indexOf(' ') != -1;
if (firstArg == true) {
firstArg = false;
} else {
builder.append(' ');
}
if (hasSpace) {
builder.append('"');
}
builder.append(arg);
if (hasSpace) {
builder.append('"');
}
}
throw new KeytoolException("Failed to create key: " + e.getMessage(), javaHome, builder.toString());
}
if (result != 0) {
return false;
}
return true;
}
use of com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException in project bazel by bazelbuild.
the class ApkBuilder method getDebugKey.
/**
* Returns the key and certificate from a given debug store.
*
* It is expected that the store password is 'android' and the key alias and password are
* 'androiddebugkey' and 'android' respectively.
*
* @param storeOsPath the OS path to the debug store.
* @param verboseStream an option {@link PrintStream} to display verbose information
* @return they key and certificate in a {@link SigningInfo} object or null.
* @throws ApkCreationException
*/
public static SigningInfo getDebugKey(String storeOsPath, final PrintStream verboseStream) throws ApkCreationException {
try {
if (storeOsPath != null) {
File storeFile = new File(storeOsPath);
try {
checkInputFile(storeFile);
} catch (FileNotFoundException e) {
// ignore these since the debug store can be created on the fly anyway.
}
// get the debug key
if (verboseStream != null) {
verboseStream.println(String.format("Using keystore: %s", storeOsPath));
}
IKeyGenOutput keygenOutput = null;
if (verboseStream != null) {
keygenOutput = new IKeyGenOutput() {
@Override
public void out(String message) {
verboseStream.println(message);
}
@Override
public void err(String message) {
verboseStream.println(message);
}
};
}
DebugKeyProvider keyProvider = new DebugKeyProvider(storeOsPath, null, /*store type*/
keygenOutput);
PrivateKey key = keyProvider.getDebugKey();
X509Certificate certificate = (X509Certificate) keyProvider.getCertificate();
if (key == null) {
throw new ApkCreationException("Unable to get debug signature key");
}
// compare the certificate expiration date
if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
// TODO, regenerate a new one.
throw new ApkCreationException("Debug Certificate expired on " + DateFormat.getInstance().format(certificate.getNotAfter()));
}
return new SigningInfo(key, certificate);
} else {
return null;
}
} catch (KeytoolException e) {
if (e.getJavaHome() == null) {
throw new ApkCreationException(e.getMessage() + "\nJAVA_HOME seems undefined, setting it will help locating keytool automatically\n" + "You can also manually execute the following command\n:" + e.getCommandLine(), e);
} else {
throw new ApkCreationException(e.getMessage() + "\nJAVA_HOME is set to: " + e.getJavaHome() + "\nUpdate it if necessary, or manually execute the following command:\n" + e.getCommandLine(), e);
}
} catch (ApkCreationException e) {
throw e;
} catch (Exception e) {
throw new ApkCreationException(e);
}
}
Aggregations