diff options
author | Xavier Ducrohet <xav@android.com> | 2011-09-15 16:14:02 -0700 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2011-09-23 10:18:41 -0700 |
commit | 0a6192fd645d454bb7004600c6e1704abc05a4b7 (patch) | |
tree | a1bdec36047ad565fecdf9c7bd90dc635a5c5384 /sdkmanager | |
parent | 9ab5464b15ce50ec1fdd09c1d688afd1a5b25138 (diff) | |
download | sdk-0a6192fd645d454bb7004600c6e1704abc05a4b7.zip sdk-0a6192fd645d454bb7004600c6e1704abc05a4b7.tar.gz sdk-0a6192fd645d454bb7004600c6e1704abc05a4b7.tar.bz2 |
Add support to make identity files from the command line.
Change-Id: Ifaa1b4653ea6c1b311e711bd285d08afb0d3e12c
Diffstat (limited to 'sdkmanager')
-rw-r--r-- | sdkmanager/app/src/com/android/sdkmanager/Main.java | 42 | ||||
-rw-r--r-- | sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java | 52 | ||||
-rw-r--r-- | sdkmanager/libs/sdklib/.classpath | 3 | ||||
-rw-r--r-- | sdkmanager/libs/sdklib/Android.mk | 1 | ||||
-rw-r--r-- | sdkmanager/libs/sdklib/manifest.txt | 2 | ||||
-rw-r--r-- | sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/MakeIdentity.java | 105 |
6 files changed, 177 insertions, 28 deletions
diff --git a/sdkmanager/app/src/com/android/sdkmanager/Main.java b/sdkmanager/app/src/com/android/sdkmanager/Main.java index 0e4b1f8..780a821 100644 --- a/sdkmanager/app/src/com/android/sdkmanager/Main.java +++ b/sdkmanager/app/src/com/android/sdkmanager/Main.java @@ -31,6 +31,7 @@ import com.android.sdklib.internal.avd.AvdInfo; import com.android.sdklib.internal.avd.AvdManager; import com.android.sdklib.internal.avd.HardwareProperties; import com.android.sdklib.internal.avd.HardwareProperties.HardwareProperty; +import com.android.sdklib.internal.build.MakeIdentity; import com.android.sdklib.internal.project.ProjectCreator; import com.android.sdklib.internal.project.ProjectProperties; import com.android.sdklib.internal.project.ProjectCreator.OutputLevel; @@ -254,6 +255,9 @@ public class Main { } else if (SdkCommandLine.OBJECT_LIB_PROJECT.equals(directObject)) { createProject(true /*library*/); + } else if (SdkCommandLine.OBJECT_IDENTITY.equals(directObject)) { + createIdentity(); + } } else if (SdkCommandLine.VERB_UPDATE.equals(verb)) { if (SdkCommandLine.OBJECT_AVD.equals(directObject)) { @@ -1260,6 +1264,44 @@ public class Main { } } + + private void createIdentity() { + String account = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_ACCOUNT); + + String keystorePath = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_KEYSTORE); + + String aliasName = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_ALIAS); + + String keystorePass = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_STOREPASS); + + String aliasPass = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_KEYPASS); + + MakeIdentity mi = new MakeIdentity(account, keystorePath, keystorePass, + aliasName, aliasPass); + + try { + mi.make(System.out, mSdkLog); + } catch (Exception e) { + errorAndExit("Unexpected error: %s", e.getMessage()); + } + } + + /** * Prompts the user to setup a hardware config for a Platform-based AVD. * @throws IOException diff --git a/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java b/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java index 387cd6f..5be7d38 100644 --- a/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java +++ b/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java @@ -55,6 +55,7 @@ class SdkCommandLine extends CommandLineProcessor { public static final String OBJECT_TEST_PROJECT = "test-project"; //$NON-NLS-1$ public static final String OBJECT_LIB_PROJECT = "lib-project"; //$NON-NLS-1$ public static final String OBJECT_ADB = "adb"; //$NON-NLS-1$ + public static final String OBJECT_IDENTITY = "identity"; //$NON-NLS-1$ public static final String ARG_ALIAS = "alias"; //$NON-NLS-1$ public static final String ARG_ACTIVITY = "activity"; //$NON-NLS-1$ @@ -84,6 +85,11 @@ class SdkCommandLine extends CommandLineProcessor { public static final String KEY_COMPACT = "compact"; //$NON-NLS-1$ public static final String KEY_EOL_NULL = "null"; //$NON-NLS-1$ public static final String KEY_ABI = "abi"; //$NON-NLS-1$ + public static final String KEY_ACCOUNT = "account"; //$NON-NLS-1$ + public static final String KEY_KEYSTORE = "keystore"; //$NON-NLS-1$ + public static final String KEY_ALIAS = "alias"; //$NON-NLS-1$ + public static final String KEY_STOREPASS = "storepass"; //$NON-NLS-1$ + public static final String KEY_KEYPASS = "keypass"; //$NON-NLS-1$ /** * Action definitions for SdkManager command line. @@ -148,6 +154,9 @@ class SdkCommandLine extends CommandLineProcessor { { VERB_UPDATE, OBJECT_SDK, "Updates the SDK by suggesting new platforms to install if available." }, + + { VERB_CREATE, OBJECT_IDENTITY, + "Creates an identity file." }, }; public SdkCommandLine(ISdkLog logger) { @@ -355,20 +364,6 @@ class SdkCommandLine extends CommandLineProcessor { VERB_CREATE, OBJECT_LIB_PROJECT, "k", KEY_PACKAGE, //$NON-NLS-1$ "Android package name for the library.", null); - // --- create export-project --- -/* - * disabled until the feature is officially supported. - - define(Mode.STRING, true, - VERB_CREATE, OBJECT_EXPORT_PROJECT, "p", KEY_PATH, //$NON-NLS-1$ - "Location path of new project.", null); - define(Mode.STRING, false, - VERB_CREATE, OBJECT_EXPORT_PROJECT, "n", KEY_NAME, //$NON-NLS-1$ - "Project name.", null); - define(Mode.STRING, true, - VERB_CREATE, OBJECT_EXPORT_PROJECT, "k", KEY_PACKAGE, //$NON-NLS-1$ - "Package name.", null); -*/ // --- update project --- define(Mode.STRING, true, @@ -406,19 +401,24 @@ class SdkCommandLine extends CommandLineProcessor { VERB_UPDATE, OBJECT_LIB_PROJECT, "t", KEY_TARGET_ID, //$NON-NLS-1$ "Target ID to set for the project.", null); - // --- update export project --- -/* - * disabled until the feature is officially supported. + // --- create identity file --- + define(Mode.STRING, true, - VERB_UPDATE, OBJECT_EXPORT_PROJECT, "p", KEY_PATH, //$NON-NLS-1$ - "Location path of the project.", null); - define(Mode.STRING, false, - VERB_UPDATE, OBJECT_EXPORT_PROJECT, "n", KEY_NAME, //$NON-NLS-1$ - "Project name.", null); - define(Mode.BOOLEAN, false, - VERB_UPDATE, OBJECT_EXPORT_PROJECT, "f", KEY_FORCE, //$NON-NLS-1$ - "Force replacing the build.xml file.", false); -*/ + VERB_CREATE, OBJECT_IDENTITY, "a", KEY_ACCOUNT, //$NON-NLS-1$ + "The publisher account.", null); + define(Mode.STRING, true, + VERB_CREATE, OBJECT_IDENTITY, "s", KEY_KEYSTORE, //$NON-NLS-1$ + "The keystore path.", null); + define(Mode.STRING, true, + VERB_CREATE, OBJECT_IDENTITY, "k", KEY_ALIAS, //$NON-NLS-1$ + "The key alias.", null); + define(Mode.STRING, true, + VERB_CREATE, OBJECT_IDENTITY, "p", KEY_STOREPASS, //$NON-NLS-1$ + "The keystore password.", null); + define(Mode.STRING, true, + VERB_CREATE, OBJECT_IDENTITY, "w", KEY_KEYPASS, //$NON-NLS-1$ + "The alias password.", null); + } @Override diff --git a/sdkmanager/libs/sdklib/.classpath b/sdkmanager/libs/sdklib/.classpath index c48e0bf..d2415fc 100644 --- a/sdkmanager/libs/sdklib/.classpath +++ b/sdkmanager/libs/sdklib/.classpath @@ -12,5 +12,6 @@ <classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/http-client/httpclient-4.1.1.jar"/> <classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/http-client/httpcore-4.1.jar"/> <classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/http-client/httpmime-4.1.1.jar"/> + <classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/mkidentity/mkidentity-prebuilt.jar"/> <classpathentry kind="output" path="bin"/> -</classpath>
\ No newline at end of file +</classpath> diff --git a/sdkmanager/libs/sdklib/Android.mk b/sdkmanager/libs/sdklib/Android.mk index ab800da..77c7ee6 100644 --- a/sdkmanager/libs/sdklib/Android.mk +++ b/sdkmanager/libs/sdklib/Android.mk @@ -28,6 +28,7 @@ LOCAL_JAR_MANIFEST := manifest.txt LOCAL_JAVA_LIBRARIES := \ androidprefs \ common \ + mkidentity-prebuilt \ commons-compress-1.0 \ httpclient-4.1.1 \ httpcore-4.1 \ diff --git a/sdkmanager/libs/sdklib/manifest.txt b/sdkmanager/libs/sdklib/manifest.txt index 0efb701..4c4bd69 100644 --- a/sdkmanager/libs/sdklib/manifest.txt +++ b/sdkmanager/libs/sdklib/manifest.txt @@ -1 +1 @@ -Class-Path: androidprefs.jar common.jar commons-compress-1.0.jar httpclient-4.1.1.jar httpcore-4.1.jar httpmime-4.1.1.jar commons-logging-1.1.1.jar commons-codec-1.4.jar +Class-Path: androidprefs.jar common.jar mkidentity.jar commons-compress-1.0.jar httpclient-4.1.1.jar httpcore-4.1.jar httpmime-4.1.1.jar commons-logging-1.1.1.jar commons-codec-1.4.jar diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/MakeIdentity.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/MakeIdentity.java new file mode 100644 index 0000000..ca1605a --- /dev/null +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/MakeIdentity.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sdklib.internal.build; + +import com.android.appauth.Certificate; +import com.android.sdklib.ISdkLog; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableEntryException; +import java.security.KeyStore.PrivateKeyEntry; +import java.security.cert.CertificateException; + +public class MakeIdentity { + + private final String mAccount; + private final String mKeystorePath; + private final String mKeystorePass; + private final String mAliasName; + private final String mAliasPass; + + /** + * Create a {@link MakeIdentity} object. + * @param account the google account + * @param keystorePath the path to the keystore + * @param keystorePass the password of the keystore + * @param aliasName the key alias name + * @param aliasPass the key alias password + */ + public MakeIdentity(String account, String keystorePath, String keystorePass, + String aliasName, String aliasPass) { + mAccount = account; + mKeystorePath = keystorePath; + mKeystorePass = keystorePass; + mAliasName = aliasName; + mAliasPass = aliasPass; + } + + /** + * Write the identity file to the given {@link PrintStream} object. + * @param ps the printstream object to write the identity file to. + * @return true if success. + * @throws KeyStoreException + * @throws NoSuchAlgorithmException + * @throws CertificateException + * @throws IOException + * @throws UnrecoverableEntryException + */ + public boolean make(PrintStream ps, ISdkLog log) + throws KeyStoreException, NoSuchAlgorithmException, + CertificateException, IOException, UnrecoverableEntryException { + + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + FileInputStream fis = new FileInputStream(mKeystorePath); + keyStore.load(fis, mKeystorePass.toCharArray()); + fis.close(); + PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry)keyStore.getEntry( + mAliasName, new KeyStore.PasswordProtection(mAliasPass.toCharArray())); + + if (entry == null) { + return false; + } + + Certificate c = new Certificate(); + c.setVersion(Certificate.VERSION); + c.setType(Certificate.TYPE_IDENTITY); + c.setHashAlgo(Certificate.DIGEST_TYPE); + c.setPublicKey(entry.getCertificate().getPublicKey()); + c.setEntityName(mAccount); + c.signWith(c, entry.getPrivateKey()); + + /* sanity check */ + if (!c.isSignedBy(c)) { + System.err.println("signature failed?!"); + return false; + } + + /* write to the printstream object */ + try { + c.writeTo(ps); + } catch (Exception e) { + log.error(e, null); + } + + return true; + } +} |