blob: b2470722c86081b757c722d1142a9597e1deee6b [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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
17package com.android.signapk;
18
Doug Zongker147626e2012-09-04 13:32:13 -070019import org.bouncycastle.asn1.ASN1InputStream;
20import org.bouncycastle.asn1.ASN1ObjectIdentifier;
21import org.bouncycastle.asn1.DEROutputStream;
22import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
Kenny Root62ea4a52013-09-25 09:59:10 -070023import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
Doug Zongker147626e2012-09-04 13:32:13 -070024import org.bouncycastle.cert.jcajce.JcaCertStore;
25import org.bouncycastle.cms.CMSException;
26import org.bouncycastle.cms.CMSProcessableByteArray;
27import org.bouncycastle.cms.CMSSignedData;
28import org.bouncycastle.cms.CMSSignedDataGenerator;
29import org.bouncycastle.cms.CMSTypedData;
30import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
31import org.bouncycastle.jce.provider.BouncyCastleProvider;
32import org.bouncycastle.operator.ContentSigner;
33import org.bouncycastle.operator.OperatorCreationException;
34import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
35import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
36import org.bouncycastle.util.encoders.Base64;
The Android Open Source Project88b60792009-03-03 19:28:42 -080037
38import java.io.BufferedReader;
Kenny Root62ea4a52013-09-25 09:59:10 -070039import java.io.ByteArrayInputStream;
The Android Open Source Project88b60792009-03-03 19:28:42 -080040import java.io.ByteArrayOutputStream;
41import java.io.DataInputStream;
42import java.io.File;
43import java.io.FileInputStream;
44import java.io.FileOutputStream;
45import java.io.FilterOutputStream;
46import java.io.IOException;
47import java.io.InputStream;
48import java.io.InputStreamReader;
49import java.io.OutputStream;
50import java.io.PrintStream;
Kenny Root89c961a2013-09-25 11:14:33 -070051import java.lang.reflect.Constructor;
The Android Open Source Project88b60792009-03-03 19:28:42 -080052import java.security.DigestOutputStream;
53import java.security.GeneralSecurityException;
54import java.security.Key;
55import java.security.KeyFactory;
56import java.security.MessageDigest;
57import java.security.PrivateKey;
Doug Zongker147626e2012-09-04 13:32:13 -070058import java.security.Provider;
59import java.security.Security;
60import java.security.cert.CertificateEncodingException;
The Android Open Source Project88b60792009-03-03 19:28:42 -080061import java.security.cert.CertificateFactory;
62import java.security.cert.X509Certificate;
63import java.security.spec.InvalidKeySpecException;
The Android Open Source Project88b60792009-03-03 19:28:42 -080064import java.security.spec.PKCS8EncodedKeySpec;
65import java.util.ArrayList;
66import java.util.Collections;
The Android Open Source Project88b60792009-03-03 19:28:42 -080067import java.util.Enumeration;
Kenny Root3d2365c2013-09-19 12:49:36 -070068import java.util.Locale;
The Android Open Source Project88b60792009-03-03 19:28:42 -080069import java.util.Map;
70import java.util.TreeMap;
71import java.util.jar.Attributes;
72import java.util.jar.JarEntry;
73import java.util.jar.JarFile;
74import java.util.jar.JarOutputStream;
75import java.util.jar.Manifest;
Doug Zongkeraf482b62009-06-08 10:46:55 -070076import java.util.regex.Pattern;
The Android Open Source Project88b60792009-03-03 19:28:42 -080077import javax.crypto.Cipher;
78import javax.crypto.EncryptedPrivateKeyInfo;
79import javax.crypto.SecretKeyFactory;
80import javax.crypto.spec.PBEKeySpec;
81
82/**
Doug Zongker8562fd42013-04-10 09:19:32 -070083 * HISTORICAL NOTE:
84 *
85 * Prior to the keylimepie release, SignApk ignored the signature
86 * algorithm specified in the certificate and always used SHA1withRSA.
87 *
Kenny Root3d2365c2013-09-19 12:49:36 -070088 * Starting with JB-MR2, the platform supports SHA256withRSA, so we use
89 * the signature algorithm in the certificate to select which to use
90 * (SHA256withRSA or SHA1withRSA). Also in JB-MR2, EC keys are supported.
Doug Zongker8562fd42013-04-10 09:19:32 -070091 *
92 * Because there are old keys still in use whose certificate actually
93 * says "MD5withRSA", we treat these as though they say "SHA1withRSA"
94 * for compatibility with older releases. This can be changed by
95 * altering the getAlgorithm() function below.
96 */
97
98
99/**
Kenny Root3d2365c2013-09-19 12:49:36 -0700100 * Command line tool to sign JAR files (including APKs and OTA updates) in a way
101 * compatible with the mincrypt verifier, using EC or RSA keys and SHA1 or
102 * SHA-256 (see historical note).
The Android Open Source Project88b60792009-03-03 19:28:42 -0800103 */
104class SignApk {
105 private static final String CERT_SF_NAME = "META-INF/CERT.SF";
Kenny Root3d2365c2013-09-19 12:49:36 -0700106 private static final String CERT_SIG_NAME = "META-INF/CERT.%s";
Doug Zongkerb14c9762012-10-15 17:10:13 -0700107 private static final String CERT_SF_MULTI_NAME = "META-INF/CERT%d.SF";
Kenny Root3d2365c2013-09-19 12:49:36 -0700108 private static final String CERT_SIG_MULTI_NAME = "META-INF/CERT%d.%s";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800109
Doug Zongker7bb04232012-05-11 09:20:50 -0700110 private static final String OTACERT_NAME = "META-INF/com/android/otacert";
111
Doug Zongker147626e2012-09-04 13:32:13 -0700112 private static Provider sBouncyCastleProvider;
113
Doug Zongker8562fd42013-04-10 09:19:32 -0700114 // bitmasks for which hash algorithms we need the manifest to include.
115 private static final int USE_SHA1 = 1;
116 private static final int USE_SHA256 = 2;
117
118 /**
119 * Return one of USE_SHA1 or USE_SHA256 according to the signature
120 * algorithm specified in the cert.
121 */
Kenny Root3d2365c2013-09-19 12:49:36 -0700122 private static int getDigestAlgorithm(X509Certificate cert) {
123 String sigAlg = cert.getSigAlgName().toUpperCase(Locale.US);
124 if ("SHA1WITHRSA".equals(sigAlg) ||
125 "MD5WITHRSA".equals(sigAlg)) { // see "HISTORICAL NOTE" above.
Doug Zongker8562fd42013-04-10 09:19:32 -0700126 return USE_SHA1;
Kenny Root3d2365c2013-09-19 12:49:36 -0700127 } else if (sigAlg.startsWith("SHA256WITH")) {
Doug Zongker8562fd42013-04-10 09:19:32 -0700128 return USE_SHA256;
129 } else {
130 throw new IllegalArgumentException("unsupported signature algorithm \"" + sigAlg +
131 "\" in cert [" + cert.getSubjectDN());
132 }
133 }
134
Kenny Root3d2365c2013-09-19 12:49:36 -0700135 /** Returns the expected signature algorithm for this key type. */
136 private static String getSignatureAlgorithm(X509Certificate cert) {
137 String sigAlg = cert.getSigAlgName().toUpperCase(Locale.US);
138 String keyType = cert.getPublicKey().getAlgorithm().toUpperCase(Locale.US);
139 if ("RSA".equalsIgnoreCase(keyType)) {
140 if (getDigestAlgorithm(cert) == USE_SHA256) {
141 return "SHA256withRSA";
142 } else {
143 return "SHA1withRSA";
144 }
145 } else if ("EC".equalsIgnoreCase(keyType)) {
146 return "SHA256withECDSA";
147 } else {
148 throw new IllegalArgumentException("unsupported key type: " + keyType);
149 }
150 }
151
Doug Zongkeraf482b62009-06-08 10:46:55 -0700152 // Files matching this pattern are not copied to the output.
153 private static Pattern stripPattern =
Kenny Root3d2365c2013-09-19 12:49:36 -0700154 Pattern.compile("^(META-INF/((.*)[.](SF|RSA|DSA|EC)|com/android/otacert))|(" +
Doug Zongkerb14c9762012-10-15 17:10:13 -0700155 Pattern.quote(JarFile.MANIFEST_NAME) + ")$");
Doug Zongkeraf482b62009-06-08 10:46:55 -0700156
The Android Open Source Project88b60792009-03-03 19:28:42 -0800157 private static X509Certificate readPublicKey(File file)
Koushik Dutta29706d12012-12-17 22:25:22 -0800158 throws IOException, GeneralSecurityException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800159 FileInputStream input = new FileInputStream(file);
160 try {
161 CertificateFactory cf = CertificateFactory.getInstance("X.509");
162 return (X509Certificate) cf.generateCertificate(input);
163 } finally {
164 input.close();
165 }
166 }
167
168 /**
169 * Reads the password from stdin and returns it as a string.
170 *
171 * @param keyFile The file containing the private key. Used to prompt the user.
172 */
173 private static String readPassword(File keyFile) {
174 // TODO: use Console.readPassword() when it's available.
175 System.out.print("Enter password for " + keyFile + " (password will not be hidden): ");
176 System.out.flush();
177 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
178 try {
179 return stdin.readLine();
180 } catch (IOException ex) {
181 return null;
182 }
183 }
184
185 /**
Kenny Root62ea4a52013-09-25 09:59:10 -0700186 * Decrypt an encrypted PKCS#8 format private key.
The Android Open Source Project88b60792009-03-03 19:28:42 -0800187 *
188 * Based on ghstark's post on Aug 6, 2006 at
189 * http://forums.sun.com/thread.jspa?threadID=758133&messageID=4330949
190 *
191 * @param encryptedPrivateKey The raw data of the private key
192 * @param keyFile The file containing the private key
193 */
Kenny Root62ea4a52013-09-25 09:59:10 -0700194 private static PKCS8EncodedKeySpec decryptPrivateKey(byte[] encryptedPrivateKey, File keyFile)
Koushik Dutta29706d12012-12-17 22:25:22 -0800195 throws GeneralSecurityException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800196 EncryptedPrivateKeyInfo epkInfo;
197 try {
198 epkInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey);
199 } catch (IOException ex) {
200 // Probably not an encrypted key.
201 return null;
202 }
203
204 char[] password = readPassword(keyFile).toCharArray();
205
206 SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
207 Key key = skFactory.generateSecret(new PBEKeySpec(password));
208
209 Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
210 cipher.init(Cipher.DECRYPT_MODE, key, epkInfo.getAlgParameters());
211
212 try {
213 return epkInfo.getKeySpec(cipher);
214 } catch (InvalidKeySpecException ex) {
215 System.err.println("signapk: Password for " + keyFile + " may be bad.");
216 throw ex;
217 }
218 }
219
Kenny Root62ea4a52013-09-25 09:59:10 -0700220 /** Read a PKCS#8 format private key. */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800221 private static PrivateKey readPrivateKey(File file)
Koushik Dutta29706d12012-12-17 22:25:22 -0800222 throws IOException, GeneralSecurityException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800223 DataInputStream input = new DataInputStream(new FileInputStream(file));
224 try {
225 byte[] bytes = new byte[(int) file.length()];
226 input.read(bytes);
227
Kenny Root62ea4a52013-09-25 09:59:10 -0700228 /* Check to see if this is in an EncryptedPrivateKeyInfo structure. */
229 PKCS8EncodedKeySpec spec = decryptPrivateKey(bytes, file);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800230 if (spec == null) {
231 spec = new PKCS8EncodedKeySpec(bytes);
232 }
233
Kenny Root62ea4a52013-09-25 09:59:10 -0700234 /*
235 * Now it's in a PKCS#8 PrivateKeyInfo structure. Read its Algorithm
236 * OID and use that to construct a KeyFactory.
237 */
238 ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(spec.getEncoded()));
239 PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
240 String algOid = pki.getPrivateKeyAlgorithm().getAlgorithm().getId();
Kenny Root3d2365c2013-09-19 12:49:36 -0700241
Kenny Root62ea4a52013-09-25 09:59:10 -0700242 return KeyFactory.getInstance(algOid).generatePrivate(spec);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800243 } finally {
244 input.close();
245 }
246 }
247
Doug Zongker8562fd42013-04-10 09:19:32 -0700248 /**
249 * Add the hash(es) of every file to the manifest, creating it if
250 * necessary.
251 */
252 private static Manifest addDigestsToManifest(JarFile jar, int hashes)
Koushik Dutta29706d12012-12-17 22:25:22 -0800253 throws IOException, GeneralSecurityException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800254 Manifest input = jar.getManifest();
255 Manifest output = new Manifest();
256 Attributes main = output.getMainAttributes();
257 if (input != null) {
258 main.putAll(input.getMainAttributes());
259 } else {
260 main.putValue("Manifest-Version", "1.0");
261 main.putValue("Created-By", "1.0 (Android SignApk)");
262 }
263
Doug Zongker8562fd42013-04-10 09:19:32 -0700264 MessageDigest md_sha1 = null;
265 MessageDigest md_sha256 = null;
266 if ((hashes & USE_SHA1) != 0) {
267 md_sha1 = MessageDigest.getInstance("SHA1");
268 }
269 if ((hashes & USE_SHA256) != 0) {
270 md_sha256 = MessageDigest.getInstance("SHA256");
271 }
272
The Android Open Source Project88b60792009-03-03 19:28:42 -0800273 byte[] buffer = new byte[4096];
274 int num;
275
276 // We sort the input entries by name, and add them to the
277 // output manifest in sorted order. We expect that the output
278 // map will be deterministic.
279
280 TreeMap<String, JarEntry> byName = new TreeMap<String, JarEntry>();
281
282 for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
283 JarEntry entry = e.nextElement();
284 byName.put(entry.getName(), entry);
285 }
286
287 for (JarEntry entry: byName.values()) {
288 String name = entry.getName();
Doug Zongkerb14c9762012-10-15 17:10:13 -0700289 if (!entry.isDirectory() &&
290 (stripPattern == null || !stripPattern.matcher(name).matches())) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800291 InputStream data = jar.getInputStream(entry);
292 while ((num = data.read(buffer)) > 0) {
Doug Zongker8562fd42013-04-10 09:19:32 -0700293 if (md_sha1 != null) md_sha1.update(buffer, 0, num);
294 if (md_sha256 != null) md_sha256.update(buffer, 0, num);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800295 }
296
297 Attributes attr = null;
298 if (input != null) attr = input.getAttributes(name);
299 attr = attr != null ? new Attributes(attr) : new Attributes();
Doug Zongker8562fd42013-04-10 09:19:32 -0700300 if (md_sha1 != null) {
301 attr.putValue("SHA1-Digest",
302 new String(Base64.encode(md_sha1.digest()), "ASCII"));
303 }
304 if (md_sha256 != null) {
305 attr.putValue("SHA-256-Digest",
306 new String(Base64.encode(md_sha256.digest()), "ASCII"));
307 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800308 output.getEntries().put(name, attr);
309 }
310 }
311
312 return output;
313 }
314
Doug Zongker7bb04232012-05-11 09:20:50 -0700315 /**
316 * Add a copy of the public key to the archive; this should
317 * exactly match one of the files in
318 * /system/etc/security/otacerts.zip on the device. (The same
319 * cert can be extracted from the CERT.RSA file but this is much
320 * easier to get at.)
321 */
322 private static void addOtacert(JarOutputStream outputJar,
323 File publicKeyFile,
324 long timestamp,
Doug Zongker8562fd42013-04-10 09:19:32 -0700325 Manifest manifest,
326 int hash)
Doug Zongker7bb04232012-05-11 09:20:50 -0700327 throws IOException, GeneralSecurityException {
Doug Zongker8562fd42013-04-10 09:19:32 -0700328 MessageDigest md = MessageDigest.getInstance(hash == USE_SHA1 ? "SHA1" : "SHA256");
Doug Zongker7bb04232012-05-11 09:20:50 -0700329
330 JarEntry je = new JarEntry(OTACERT_NAME);
331 je.setTime(timestamp);
332 outputJar.putNextEntry(je);
333 FileInputStream input = new FileInputStream(publicKeyFile);
334 byte[] b = new byte[4096];
335 int read;
336 while ((read = input.read(b)) != -1) {
337 outputJar.write(b, 0, read);
338 md.update(b, 0, read);
339 }
340 input.close();
341
342 Attributes attr = new Attributes();
Doug Zongker8562fd42013-04-10 09:19:32 -0700343 attr.putValue(hash == USE_SHA1 ? "SHA1-Digest" : "SHA-256-Digest",
Doug Zongker147626e2012-09-04 13:32:13 -0700344 new String(Base64.encode(md.digest()), "ASCII"));
Doug Zongker7bb04232012-05-11 09:20:50 -0700345 manifest.getEntries().put(OTACERT_NAME, attr);
346 }
347
348
Doug Zongker147626e2012-09-04 13:32:13 -0700349 /** Write to another stream and track how many bytes have been
350 * written.
351 */
352 private static class CountOutputStream extends FilterOutputStream {
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700353 private int mCount;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800354
Doug Zongker147626e2012-09-04 13:32:13 -0700355 public CountOutputStream(OutputStream out) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800356 super(out);
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700357 mCount = 0;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800358 }
359
360 @Override
361 public void write(int b) throws IOException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800362 super.write(b);
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700363 mCount++;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800364 }
365
366 @Override
367 public void write(byte[] b, int off, int len) throws IOException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800368 super.write(b, off, len);
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700369 mCount += len;
370 }
371
372 public int size() {
373 return mCount;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800374 }
375 }
376
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700377 /** Write a .SF file with a digest of the specified manifest. */
Doug Zongker8562fd42013-04-10 09:19:32 -0700378 private static void writeSignatureFile(Manifest manifest, OutputStream out,
379 int hash)
Doug Zongker147626e2012-09-04 13:32:13 -0700380 throws IOException, GeneralSecurityException {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800381 Manifest sf = new Manifest();
382 Attributes main = sf.getMainAttributes();
383 main.putValue("Signature-Version", "1.0");
384 main.putValue("Created-By", "1.0 (Android SignApk)");
385
Doug Zongker8562fd42013-04-10 09:19:32 -0700386 MessageDigest md = MessageDigest.getInstance(
387 hash == USE_SHA256 ? "SHA256" : "SHA1");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800388 PrintStream print = new PrintStream(
Koushik Dutta29706d12012-12-17 22:25:22 -0800389 new DigestOutputStream(new ByteArrayOutputStream(), md),
390 true, "UTF-8");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800391
392 // Digest of the entire manifest
393 manifest.write(print);
394 print.flush();
Doug Zongker8562fd42013-04-10 09:19:32 -0700395 main.putValue(hash == USE_SHA256 ? "SHA-256-Digest-Manifest" : "SHA1-Digest-Manifest",
Doug Zongker147626e2012-09-04 13:32:13 -0700396 new String(Base64.encode(md.digest()), "ASCII"));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800397
398 Map<String, Attributes> entries = manifest.getEntries();
399 for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
400 // Digest of the manifest stanza for this entry.
401 print.print("Name: " + entry.getKey() + "\r\n");
402 for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
403 print.print(att.getKey() + ": " + att.getValue() + "\r\n");
404 }
405 print.print("\r\n");
406 print.flush();
407
408 Attributes sfAttr = new Attributes();
Doug Zongker8562fd42013-04-10 09:19:32 -0700409 sfAttr.putValue(hash == USE_SHA256 ? "SHA-256-Digest" : "SHA1-Digest-Manifest",
Doug Zongker147626e2012-09-04 13:32:13 -0700410 new String(Base64.encode(md.digest()), "ASCII"));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800411 sf.getEntries().put(entry.getKey(), sfAttr);
412 }
413
Doug Zongker147626e2012-09-04 13:32:13 -0700414 CountOutputStream cout = new CountOutputStream(out);
415 sf.write(cout);
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700416
417 // A bug in the java.util.jar implementation of Android platforms
418 // up to version 1.6 will cause a spurious IOException to be thrown
419 // if the length of the signature file is a multiple of 1024 bytes.
420 // As a workaround, add an extra CRLF in this case.
Doug Zongker147626e2012-09-04 13:32:13 -0700421 if ((cout.size() % 1024) == 0) {
422 cout.write('\r');
423 cout.write('\n');
Ficus Kirkpatrick7978d502010-09-23 22:57:05 -0700424 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800425 }
426
Doug Zongker147626e2012-09-04 13:32:13 -0700427 /** Sign data and write the digital signature to 'out'. */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800428 private static void writeSignatureBlock(
Doug Zongker147626e2012-09-04 13:32:13 -0700429 CMSTypedData data, X509Certificate publicKey, PrivateKey privateKey,
430 OutputStream out)
431 throws IOException,
432 CertificateEncodingException,
433 OperatorCreationException,
434 CMSException {
435 ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>(1);
436 certList.add(publicKey);
437 JcaCertStore certs = new JcaCertStore(certList);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800438
Doug Zongker147626e2012-09-04 13:32:13 -0700439 CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
Kenny Root3d2365c2013-09-19 12:49:36 -0700440 ContentSigner signer = new JcaContentSignerBuilder(getSignatureAlgorithm(publicKey))
Doug Zongker147626e2012-09-04 13:32:13 -0700441 .setProvider(sBouncyCastleProvider)
442 .build(privateKey);
443 gen.addSignerInfoGenerator(
444 new JcaSignerInfoGeneratorBuilder(
445 new JcaDigestCalculatorProviderBuilder()
446 .setProvider(sBouncyCastleProvider)
447 .build())
448 .setDirectSignature(true)
Doug Zongker8562fd42013-04-10 09:19:32 -0700449 .build(signer, publicKey));
Doug Zongker147626e2012-09-04 13:32:13 -0700450 gen.addCertificates(certs);
451 CMSSignedData sigData = gen.generate(data, false);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800452
Doug Zongker147626e2012-09-04 13:32:13 -0700453 ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded());
454 DEROutputStream dos = new DEROutputStream(out);
455 dos.writeObject(asn1.readObject());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800456 }
457
Koushik Dutta29706d12012-12-17 22:25:22 -0800458 /**
459 * Copy all the files in a manifest from input to output. We set
460 * the modification times in the output to a fixed time, so as to
461 * reduce variation in the output file and make incremental OTAs
462 * more efficient.
463 */
464 private static void copyFiles(Manifest manifest,
465 JarFile in, JarOutputStream out, long timestamp) throws IOException {
466 byte[] buffer = new byte[4096];
467 int num;
468
469 Map<String, Attributes> entries = manifest.getEntries();
470 ArrayList<String> names = new ArrayList<String>(entries.keySet());
471 Collections.sort(names);
472 for (String name : names) {
473 JarEntry inEntry = in.getJarEntry(name);
474 JarEntry outEntry = null;
475 if (inEntry.getMethod() == JarEntry.STORED) {
476 // Preserve the STORED method of the input entry.
477 outEntry = new JarEntry(inEntry);
478 } else {
479 // Create a new entry so that the compressed len is recomputed.
480 outEntry = new JarEntry(name);
481 }
482 outEntry.setTime(timestamp);
483 out.putNextEntry(outEntry);
484
485 InputStream data = in.getInputStream(inEntry);
486 while ((num = data.read(buffer)) > 0) {
487 out.write(buffer, 0, num);
488 }
489 out.flush();
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700490 }
Koushik Dutta29706d12012-12-17 22:25:22 -0800491 }
492
493 private static class WholeFileSignerOutputStream extends FilterOutputStream {
494 private boolean closing = false;
495 private ByteArrayOutputStream footer = new ByteArrayOutputStream();
496 private OutputStream tee;
497
498 public WholeFileSignerOutputStream(OutputStream out, OutputStream tee) {
499 super(out);
500 this.tee = tee;
501 }
502
503 public void notifyClosing() {
504 closing = true;
505 }
506
507 public void finish() throws IOException {
508 closing = false;
509
510 byte[] data = footer.toByteArray();
511 if (data.length < 2)
512 throw new IOException("Less than two bytes written to footer");
513 write(data, 0, data.length - 2);
514 }
515
516 public byte[] getTail() {
517 return footer.toByteArray();
518 }
519
520 @Override
521 public void write(byte[] b) throws IOException {
522 write(b, 0, b.length);
523 }
524
525 @Override
526 public void write(byte[] b, int off, int len) throws IOException {
527 if (closing) {
528 // if the jar is about to close, save the footer that will be written
529 footer.write(b, off, len);
530 }
531 else {
532 // write to both output streams. out is the CMSTypedData signer and tee is the file.
533 out.write(b, off, len);
534 tee.write(b, off, len);
535 }
536 }
537
538 @Override
539 public void write(int b) throws IOException {
540 if (closing) {
541 // if the jar is about to close, save the footer that will be written
542 footer.write(b);
543 }
544 else {
545 // write to both output streams. out is the CMSTypedData signer and tee is the file.
546 out.write(b);
547 tee.write(b);
548 }
549 }
550 }
551
552 private static class CMSSigner implements CMSTypedData {
553 private JarFile inputJar;
554 private File publicKeyFile;
555 private X509Certificate publicKey;
556 private PrivateKey privateKey;
557 private String outputFile;
558 private OutputStream outputStream;
559 private final ASN1ObjectIdentifier type;
560 private WholeFileSignerOutputStream signer;
561
562 public CMSSigner(JarFile inputJar, File publicKeyFile,
563 X509Certificate publicKey, PrivateKey privateKey,
564 OutputStream outputStream) {
565 this.inputJar = inputJar;
566 this.publicKeyFile = publicKeyFile;
567 this.publicKey = publicKey;
568 this.privateKey = privateKey;
569 this.outputStream = outputStream;
570 this.type = new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId());
571 }
572
573 public Object getContent() {
574 throw new UnsupportedOperationException();
575 }
576
577 public ASN1ObjectIdentifier getContentType() {
578 return type;
579 }
580
581 public void write(OutputStream out) throws IOException {
582 try {
583 signer = new WholeFileSignerOutputStream(out, outputStream);
584 JarOutputStream outputJar = new JarOutputStream(signer);
585
Kenny Root3d2365c2013-09-19 12:49:36 -0700586 int hash = getDigestAlgorithm(publicKey);
Doug Zongker8562fd42013-04-10 09:19:32 -0700587
588 // Assume the certificate is valid for at least an hour.
589 long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;
590
591 Manifest manifest = addDigestsToManifest(inputJar, hash);
592 copyFiles(manifest, inputJar, outputJar, timestamp);
593 addOtacert(outputJar, publicKeyFile, timestamp, manifest, hash);
594
Koushik Dutta29706d12012-12-17 22:25:22 -0800595 signFile(manifest, inputJar,
596 new X509Certificate[]{ publicKey },
597 new PrivateKey[]{ privateKey },
598 outputJar);
Koushik Dutta29706d12012-12-17 22:25:22 -0800599
600 signer.notifyClosing();
601 outputJar.close();
602 signer.finish();
603 }
604 catch (Exception e) {
605 throw new IOException(e);
606 }
607 }
608
609 public void writeSignatureBlock(ByteArrayOutputStream temp)
610 throws IOException,
611 CertificateEncodingException,
612 OperatorCreationException,
613 CMSException {
614 SignApk.writeSignatureBlock(this, publicKey, privateKey, temp);
615 }
616
617 public WholeFileSignerOutputStream getSigner() {
618 return signer;
619 }
620 }
621
622 private static void signWholeFile(JarFile inputJar, File publicKeyFile,
623 X509Certificate publicKey, PrivateKey privateKey,
624 OutputStream outputStream) throws Exception {
625 CMSSigner cmsOut = new CMSSigner(inputJar, publicKeyFile,
626 publicKey, privateKey, outputStream);
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700627
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700628 ByteArrayOutputStream temp = new ByteArrayOutputStream();
629
630 // put a readable message and a null char at the start of the
631 // archive comment, so that tools that display the comment
632 // (hopefully) show something sensible.
633 // TODO: anything more useful we can put in this message?
634 byte[] message = "signed by SignApk".getBytes("UTF-8");
635 temp.write(message);
636 temp.write(0);
Doug Zongker147626e2012-09-04 13:32:13 -0700637
Koushik Dutta29706d12012-12-17 22:25:22 -0800638 cmsOut.writeSignatureBlock(temp);
639
640 byte[] zipData = cmsOut.getSigner().getTail();
641
642 // For a zip with no archive comment, the
643 // end-of-central-directory record will be 22 bytes long, so
644 // we expect to find the EOCD marker 22 bytes from the end.
645 if (zipData[zipData.length-22] != 0x50 ||
646 zipData[zipData.length-21] != 0x4b ||
647 zipData[zipData.length-20] != 0x05 ||
648 zipData[zipData.length-19] != 0x06) {
649 throw new IllegalArgumentException("zip data already has an archive comment");
650 }
651
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700652 int total_size = temp.size() + 6;
653 if (total_size > 0xffff) {
654 throw new IllegalArgumentException("signature is too big for ZIP file comment");
655 }
656 // signature starts this many bytes from the end of the file
657 int signature_start = total_size - message.length - 1;
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700658 temp.write(signature_start & 0xff);
659 temp.write((signature_start >> 8) & 0xff);
Doug Zongkerbadd2ca2009-08-14 16:42:35 -0700660 // Why the 0xff bytes? In a zip file with no archive comment,
661 // bytes [-6:-2] of the file are the little-endian offset from
662 // the start of the file to the central directory. So for the
663 // two high bytes to be 0xff 0xff, the archive would have to
Doug Zongker147626e2012-09-04 13:32:13 -0700664 // be nearly 4GB in size. So it's unlikely that a real
Doug Zongkerbadd2ca2009-08-14 16:42:35 -0700665 // commentless archive would have 0xffs here, and lets us tell
666 // an old signed archive from a new one.
667 temp.write(0xff);
668 temp.write(0xff);
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700669 temp.write(total_size & 0xff);
670 temp.write((total_size >> 8) & 0xff);
671 temp.flush();
672
Doug Zongkerbadd2ca2009-08-14 16:42:35 -0700673 // Signature verification checks that the EOCD header is the
674 // last such sequence in the file (to avoid minzip finding a
675 // fake EOCD appended after the signature in its scan). The
676 // odds of producing this sequence by chance are very low, but
677 // let's catch it here if it does.
678 byte[] b = temp.toByteArray();
679 for (int i = 0; i < b.length-3; ++i) {
680 if (b[i] == 0x50 && b[i+1] == 0x4b && b[i+2] == 0x05 && b[i+3] == 0x06) {
681 throw new IllegalArgumentException("found spurious EOCD header at " + i);
682 }
683 }
684
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700685 outputStream.write(total_size & 0xff);
686 outputStream.write((total_size >> 8) & 0xff);
687 temp.writeTo(outputStream);
688 }
689
Koushik Dutta29706d12012-12-17 22:25:22 -0800690 private static void signFile(Manifest manifest, JarFile inputJar,
691 X509Certificate[] publicKey, PrivateKey[] privateKey,
692 JarOutputStream outputJar)
693 throws Exception {
694 // Assume the certificate is valid for at least an hour.
695 long timestamp = publicKey[0].getNotBefore().getTime() + 3600L * 1000;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800696
Koushik Dutta29706d12012-12-17 22:25:22 -0800697 // MANIFEST.MF
Doug Zongker8562fd42013-04-10 09:19:32 -0700698 JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
Koushik Dutta29706d12012-12-17 22:25:22 -0800699 je.setTime(timestamp);
700 outputJar.putNextEntry(je);
701 manifest.write(outputJar);
702
703 int numKeys = publicKey.length;
704 for (int k = 0; k < numKeys; ++k) {
705 // CERT.SF / CERT#.SF
706 je = new JarEntry(numKeys == 1 ? CERT_SF_NAME :
707 (String.format(CERT_SF_MULTI_NAME, k)));
708 je.setTime(timestamp);
709 outputJar.putNextEntry(je);
710 ByteArrayOutputStream baos = new ByteArrayOutputStream();
Kenny Root3d2365c2013-09-19 12:49:36 -0700711 writeSignatureFile(manifest, baos, getDigestAlgorithm(publicKey[k]));
Koushik Dutta29706d12012-12-17 22:25:22 -0800712 byte[] signedData = baos.toByteArray();
713 outputJar.write(signedData);
714
Kenny Root3d2365c2013-09-19 12:49:36 -0700715 // CERT.{EC,RSA} / CERT#.{EC,RSA}
Kenny Root62ea4a52013-09-25 09:59:10 -0700716 final String keyType = publicKey[k].getPublicKey().getAlgorithm();
Kenny Root3d2365c2013-09-19 12:49:36 -0700717 je = new JarEntry(numKeys == 1 ?
Kenny Root62ea4a52013-09-25 09:59:10 -0700718 (String.format(CERT_SIG_NAME, keyType)) :
719 (String.format(CERT_SIG_MULTI_NAME, k, keyType)));
Koushik Dutta29706d12012-12-17 22:25:22 -0800720 je.setTime(timestamp);
721 outputJar.putNextEntry(je);
722 writeSignatureBlock(new CMSProcessableByteArray(signedData),
723 publicKey[k], privateKey[k], outputJar);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800724 }
725 }
726
Kenny Root89c961a2013-09-25 11:14:33 -0700727 /**
728 * Tries to load a JSE Provider by class name. This is for custom PrivateKey
729 * types that might be stored in PKCS#11-like storage.
730 */
731 private static void loadProviderIfNecessary(String providerClassName) {
732 if (providerClassName == null) {
733 return;
734 }
735
736 final Class<?> klass;
737 try {
738 final ClassLoader sysLoader = ClassLoader.getSystemClassLoader();
739 if (sysLoader != null) {
740 klass = sysLoader.loadClass(providerClassName);
741 } else {
742 klass = Class.forName(providerClassName);
743 }
744 } catch (ClassNotFoundException e) {
745 e.printStackTrace();
746 System.exit(1);
747 return;
748 }
749
750 Constructor<?> constructor = null;
751 for (Constructor<?> c : klass.getConstructors()) {
752 if (c.getParameterTypes().length == 0) {
753 constructor = c;
754 break;
755 }
756 }
757 if (constructor == null) {
758 System.err.println("No zero-arg constructor found for " + providerClassName);
759 System.exit(1);
760 return;
761 }
762
763 final Object o;
764 try {
765 o = constructor.newInstance();
766 } catch (Exception e) {
767 e.printStackTrace();
768 System.exit(1);
769 return;
770 }
771 if (!(o instanceof Provider)) {
772 System.err.println("Not a Provider class: " + providerClassName);
773 System.exit(1);
774 }
775
776 Security.insertProviderAt((Provider) o, 1);
777 }
778
Doug Zongkerb14c9762012-10-15 17:10:13 -0700779 private static void usage() {
780 System.err.println("Usage: signapk [-w] " +
Kenny Root89c961a2013-09-25 11:14:33 -0700781 "[-providerClass <className>] " +
Doug Zongkerb14c9762012-10-15 17:10:13 -0700782 "publickey.x509[.pem] privatekey.pk8 " +
783 "[publickey2.x509[.pem] privatekey2.pk8 ...] " +
784 "input.jar output.jar");
785 System.exit(2);
786 }
787
The Android Open Source Project88b60792009-03-03 19:28:42 -0800788 public static void main(String[] args) {
Doug Zongkerb14c9762012-10-15 17:10:13 -0700789 if (args.length < 4) usage();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800790
Doug Zongker147626e2012-09-04 13:32:13 -0700791 sBouncyCastleProvider = new BouncyCastleProvider();
792 Security.addProvider(sBouncyCastleProvider);
793
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700794 boolean signWholeFile = false;
Kenny Root89c961a2013-09-25 11:14:33 -0700795 String providerClass = null;
796 String providerArg = null;
797
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700798 int argstart = 0;
Kenny Root89c961a2013-09-25 11:14:33 -0700799 while (argstart < args.length && args[argstart].startsWith("-")) {
800 if ("-w".equals(args[argstart])) {
801 signWholeFile = true;
802 ++argstart;
803 } else if ("-providerClass".equals(args[argstart])) {
804 if (argstart + 1 >= args.length) {
805 usage();
806 }
807 providerClass = args[++argstart];
808 ++argstart;
809 } else {
810 usage();
811 }
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700812 }
813
Doug Zongkerb14c9762012-10-15 17:10:13 -0700814 if ((args.length - argstart) % 2 == 1) usage();
815 int numKeys = ((args.length - argstart) / 2) - 1;
816 if (signWholeFile && numKeys > 1) {
817 System.err.println("Only one key may be used with -w.");
818 System.exit(2);
819 }
820
Kenny Root89c961a2013-09-25 11:14:33 -0700821 loadProviderIfNecessary(providerClass);
822
Doug Zongkerb14c9762012-10-15 17:10:13 -0700823 String inputFilename = args[args.length-2];
824 String outputFilename = args[args.length-1];
825
The Android Open Source Project88b60792009-03-03 19:28:42 -0800826 JarFile inputJar = null;
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700827 FileOutputStream outputFile = null;
Doug Zongker8562fd42013-04-10 09:19:32 -0700828 int hashes = 0;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800829
830 try {
Doug Zongkerb14c9762012-10-15 17:10:13 -0700831 File firstPublicKeyFile = new File(args[argstart+0]);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800832
Doug Zongkerb14c9762012-10-15 17:10:13 -0700833 X509Certificate[] publicKey = new X509Certificate[numKeys];
Doug Zongker8562fd42013-04-10 09:19:32 -0700834 try {
835 for (int i = 0; i < numKeys; ++i) {
836 int argNum = argstart + i*2;
837 publicKey[i] = readPublicKey(new File(args[argNum]));
Kenny Root3d2365c2013-09-19 12:49:36 -0700838 hashes |= getDigestAlgorithm(publicKey[i]);
Doug Zongker8562fd42013-04-10 09:19:32 -0700839 }
840 } catch (IllegalArgumentException e) {
841 System.err.println(e);
842 System.exit(1);
Doug Zongkerb14c9762012-10-15 17:10:13 -0700843 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800844
Doug Zongkerb14c9762012-10-15 17:10:13 -0700845 // Set the ZIP file timestamp to the starting valid time
846 // of the 0th certificate plus one hour (to match what
847 // we've historically done).
848 long timestamp = publicKey[0].getNotBefore().getTime() + 3600L * 1000;
849
850 PrivateKey[] privateKey = new PrivateKey[numKeys];
851 for (int i = 0; i < numKeys; ++i) {
852 int argNum = argstart + i*2 + 1;
853 privateKey[i] = readPrivateKey(new File(args[argNum]));
854 }
855 inputJar = new JarFile(new File(inputFilename), false); // Don't verify.
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700856
Koushik Dutta29706d12012-12-17 22:25:22 -0800857 outputFile = new FileOutputStream(outputFilename);
858
859
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700860 if (signWholeFile) {
Koushik Dutta29706d12012-12-17 22:25:22 -0800861 SignApk.signWholeFile(inputJar, firstPublicKeyFile,
862 publicKey[0], privateKey[0], outputFile);
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700863 } else {
Koushik Dutta29706d12012-12-17 22:25:22 -0800864 JarOutputStream outputJar = new JarOutputStream(outputFile);
Doug Zongkere6913732012-07-03 15:03:04 -0700865
Koushik Dutta29706d12012-12-17 22:25:22 -0800866 // For signing .apks, use the maximum compression to make
867 // them as small as possible (since they live forever on
868 // the system partition). For OTA packages, use the
869 // default compression level, which is much much faster
870 // and produces output that is only a tiny bit larger
871 // (~0.1% on full OTA packages I tested).
Doug Zongkere6913732012-07-03 15:03:04 -0700872 outputJar.setLevel(9);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800873
Doug Zongker8562fd42013-04-10 09:19:32 -0700874 Manifest manifest = addDigestsToManifest(inputJar, hashes);
875 copyFiles(manifest, inputJar, outputJar, timestamp);
876 signFile(manifest, inputJar, publicKey, privateKey, outputJar);
Koushik Dutta29706d12012-12-17 22:25:22 -0800877 outputJar.close();
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700878 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800879 } catch (Exception e) {
880 e.printStackTrace();
881 System.exit(1);
882 } finally {
883 try {
884 if (inputJar != null) inputJar.close();
Doug Zongkerc6cf01a2009-08-12 18:20:24 -0700885 if (outputFile != null) outputFile.close();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800886 } catch (IOException e) {
887 e.printStackTrace();
888 System.exit(1);
889 }
890 }
891 }
892}