1 /* 2 * Copyright (C) 2011 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.appauth.Certificate; 20 import com.android.sdklib.ISdkLog; 21 22 import java.io.FileInputStream; 23 import java.io.IOException; 24 import java.io.PrintStream; 25 import java.security.KeyStore; 26 import java.security.KeyStoreException; 27 import java.security.NoSuchAlgorithmException; 28 import java.security.UnrecoverableEntryException; 29 import java.security.KeyStore.PrivateKeyEntry; 30 import java.security.cert.CertificateException; 31 32 public class MakeIdentity { 33 34 private final String mAccount; 35 private final String mKeystorePath; 36 private final String mKeystorePass; 37 private final String mAliasName; 38 private final String mAliasPass; 39 40 /** 41 * Create a {@link MakeIdentity} object. 42 * @param account the google account 43 * @param keystorePath the path to the keystore 44 * @param keystorePass the password of the keystore 45 * @param aliasName the key alias name 46 * @param aliasPass the key alias password 47 */ 48 public MakeIdentity(String account, String keystorePath, String keystorePass, 49 String aliasName, String aliasPass) { 50 mAccount = account; 51 mKeystorePath = keystorePath; 52 mKeystorePass = keystorePass; 53 mAliasName = aliasName; 54 mAliasPass = aliasPass; 55 } 56 57 /** 58 * Write the identity file to the given {@link PrintStream} object. 59 * @param ps the printstream object to write the identity file to. 60 * @return true if success. 61 * @throws KeyStoreException 62 * @throws NoSuchAlgorithmException 63 * @throws CertificateException 64 * @throws IOException 65 * @throws UnrecoverableEntryException 66 */ 67 public boolean make(PrintStream ps, ISdkLog log) 68 throws KeyStoreException, NoSuchAlgorithmException, 69 CertificateException, IOException, UnrecoverableEntryException { 70 71 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 72 FileInputStream fis = new FileInputStream(mKeystorePath); 73 keyStore.load(fis, mKeystorePass.toCharArray()); 74 fis.close(); 75 PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry)keyStore.getEntry( 76 mAliasName, new KeyStore.PasswordProtection(mAliasPass.toCharArray())); 77 78 if (entry == null) { 79 return false; 80 } 81 82 Certificate c = new Certificate(); 83 c.setVersion(Certificate.VERSION); 84 c.setType(Certificate.TYPE_IDENTITY); 85 c.setHashAlgo(Certificate.DIGEST_TYPE); 86 c.setPublicKey(entry.getCertificate().getPublicKey()); 87 c.setEntityName(mAccount); 88 c.signWith(c, entry.getPrivateKey()); 89 90 /* sanity check */ 91 if (!c.isSignedBy(c)) { 92 System.err.println("signature failed?!"); 93 return false; 94 } 95 96 /* write to the printstream object */ 97 try { 98 c.writeTo(ps); 99 } catch (Exception e) { 100 log.error(e, null); 101 } 102 103 return true; 104 } 105 } 106