blob: e58ca609f1a82cf7e0831c287d47bc4f4f29b216 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -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 android.content.pm;
18
Mathew Inwood1c77a112018-08-14 14:06:26 +010019import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.os.Parcel;
21import android.os.Parcelable;
22
Jeff Sharkey94c91dc2013-03-06 16:25:50 -080023import com.android.internal.util.ArrayUtils;
24
Kenny Root05ca4c92011-09-15 10:36:25 -070025import java.io.ByteArrayInputStream;
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -080026import java.io.InputStream;
Kenny Rootde0ff632010-09-03 16:02:27 -070027import java.lang.ref.SoftReference;
Kenny Root05ca4c92011-09-15 10:36:25 -070028import java.security.PublicKey;
29import java.security.cert.Certificate;
Kenny Roota8e65fd2014-04-24 11:44:47 -070030import java.security.cert.CertificateEncodingException;
Kenny Root05ca4c92011-09-15 10:36:25 -070031import java.security.cert.CertificateException;
32import java.security.cert.CertificateFactory;
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -080033import java.security.cert.X509Certificate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import java.util.Arrays;
35
36/**
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070037 * Opaque, immutable representation of a signing certificate associated with an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * application package.
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070039 * <p>
40 * This class name is slightly misleading, since it's not actually a signature.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 */
42public class Signature implements Parcelable {
43 private final byte[] mSignature;
44 private int mHashCode;
45 private boolean mHaveHashCode;
Kenny Rootde0ff632010-09-03 16:02:27 -070046 private SoftReference<String> mStringRef;
Kenny Roota8e65fd2014-04-24 11:44:47 -070047 private Certificate[] mCertificateChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49 /**
50 * Create Signature from an existing raw byte array.
51 */
52 public Signature(byte[] signature) {
53 mSignature = signature.clone();
Kenny Roota8e65fd2014-04-24 11:44:47 -070054 mCertificateChain = null;
55 }
56
57 /**
58 * Create signature from a certificate chain. Used for backward
59 * compatibility.
60 *
61 * @throws CertificateEncodingException
62 * @hide
63 */
64 public Signature(Certificate[] certificateChain) throws CertificateEncodingException {
65 mSignature = certificateChain[0].getEncoded();
66 if (certificateChain.length > 1) {
67 mCertificateChain = Arrays.copyOfRange(certificateChain, 1, certificateChain.length);
68 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
70
Kenny Root11373412011-07-28 15:13:33 -070071 private static final int parseHexDigit(int nibble) {
72 if ('0' <= nibble && nibble <= '9') {
73 return nibble - '0';
74 } else if ('a' <= nibble && nibble <= 'f') {
75 return nibble - 'a' + 10;
76 } else if ('A' <= nibble && nibble <= 'F') {
77 return nibble - 'A' + 10;
78 } else {
79 throw new IllegalArgumentException("Invalid character " + nibble + " in hex string");
80 }
81 }
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
84 * Create Signature from a text representation previously returned by
Kenny Root11373412011-07-28 15:13:33 -070085 * {@link #toChars} or {@link #toCharsString()}. Signatures are expected to
86 * be a hex-encoded ASCII string.
87 *
88 * @param text hex-encoded string representing the signature
89 * @throws IllegalArgumentException when signature is odd-length
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
91 public Signature(String text) {
Kenny Rootd21d4442011-07-22 15:32:30 -070092 final byte[] input = text.getBytes();
93 final int N = input.length;
Kenny Root11373412011-07-28 15:13:33 -070094
95 if (N % 2 != 0) {
96 throw new IllegalArgumentException("text size " + N + " is not even");
97 }
98
Kenny Rootd21d4442011-07-22 15:32:30 -070099 final byte[] sig = new byte[N / 2];
100 int sigIndex = 0;
101
102 for (int i = 0; i < N;) {
Kenny Root11373412011-07-28 15:13:33 -0700103 final int hi = parseHexDigit(input[i++]);
104 final int lo = parseHexDigit(input[i++]);
105 sig[sigIndex++] = (byte) ((hi << 4) | lo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
Kenny Rootd21d4442011-07-22 15:32:30 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 mSignature = sig;
109 }
110
111 /**
112 * Encode the Signature as ASCII text.
113 */
114 public char[] toChars() {
115 return toChars(null, null);
116 }
117
118 /**
119 * Encode the Signature as ASCII text in to an existing array.
120 *
121 * @param existingArray Existing char array or null.
122 * @param outLen Output parameter for the number of characters written in
123 * to the array.
124 * @return Returns either <var>existingArray</var> if it was large enough
125 * to hold the ASCII representation, or a newly created char[] array if
126 * needed.
127 */
128 public char[] toChars(char[] existingArray, int[] outLen) {
129 byte[] sig = mSignature;
130 final int N = sig.length;
131 final int N2 = N*2;
132 char[] text = existingArray == null || N2 > existingArray.length
133 ? new char[N2] : existingArray;
134 for (int j=0; j<N; j++) {
135 byte v = sig[j];
136 int d = (v>>4)&0xf;
137 text[j*2] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
138 d = v&0xf;
139 text[j*2+1] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
140 }
141 if (outLen != null) outLen[0] = N;
142 return text;
143 }
144
145 /**
Kenny Root11373412011-07-28 15:13:33 -0700146 * Return the result of {@link #toChars()} as a String.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 */
148 public String toCharsString() {
Kenny Rootde0ff632010-09-03 16:02:27 -0700149 String str = mStringRef == null ? null : mStringRef.get();
150 if (str != null) {
151 return str;
152 }
153 str = new String(toChars());
154 mStringRef = new SoftReference<String>(str);
155 return str;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
158 /**
159 * @return the contents of this signature as a byte array.
160 */
161 public byte[] toByteArray() {
162 byte[] bytes = new byte[mSignature.length];
163 System.arraycopy(mSignature, 0, bytes, 0, mSignature.length);
164 return bytes;
165 }
166
Kenny Root05ca4c92011-09-15 10:36:25 -0700167 /**
168 * Returns the public key for this signature.
169 *
170 * @throws CertificateException when Signature isn't a valid X.509
171 * certificate; shouldn't happen.
172 * @hide
173 */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100174 @UnsupportedAppUsage
Kenny Root05ca4c92011-09-15 10:36:25 -0700175 public PublicKey getPublicKey() throws CertificateException {
176 final CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
177 final ByteArrayInputStream bais = new ByteArrayInputStream(mSignature);
178 final Certificate cert = certFactory.generateCertificate(bais);
179 return cert.getPublicKey();
180 }
181
Kenny Roota8e65fd2014-04-24 11:44:47 -0700182 /**
183 * Used for compatibility code that needs to check the certificate chain
184 * during upgrades.
185 *
186 * @throws CertificateEncodingException
187 * @hide
188 */
189 public Signature[] getChainSignatures() throws CertificateEncodingException {
190 if (mCertificateChain == null) {
191 return new Signature[] { this };
192 }
193
194 Signature[] chain = new Signature[1 + mCertificateChain.length];
195 chain[0] = this;
196
197 int i = 1;
198 for (Certificate c : mCertificateChain) {
199 chain[i++] = new Signature(c.getEncoded());
200 }
201
202 return chain;
203 }
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 @Override
206 public boolean equals(Object obj) {
207 try {
208 if (obj != null) {
209 Signature other = (Signature)obj;
Kenny Root11373412011-07-28 15:13:33 -0700210 return this == other || Arrays.equals(mSignature, other.mSignature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212 } catch (ClassCastException e) {
213 }
214 return false;
215 }
216
217 @Override
218 public int hashCode() {
219 if (mHaveHashCode) {
220 return mHashCode;
221 }
222 mHashCode = Arrays.hashCode(mSignature);
223 mHaveHashCode = true;
224 return mHashCode;
225 }
226
227 public int describeContents() {
228 return 0;
229 }
230
231 public void writeToParcel(Parcel dest, int parcelableFlags) {
232 dest.writeByteArray(mSignature);
233 }
234
235 public static final Parcelable.Creator<Signature> CREATOR
236 = new Parcelable.Creator<Signature>() {
237 public Signature createFromParcel(Parcel source) {
238 return new Signature(source);
239 }
240
241 public Signature[] newArray(int size) {
242 return new Signature[size];
243 }
244 };
245
246 private Signature(Parcel source) {
247 mSignature = source.createByteArray();
248 }
Jeff Sharkey94c91dc2013-03-06 16:25:50 -0800249
250 /**
251 * Test if given {@link Signature} sets are exactly equal.
252 *
253 * @hide
254 */
255 public static boolean areExactMatch(Signature[] a, Signature[] b) {
Jeff Sharkeyec55ef02014-07-08 11:28:00 -0700256 return (a.length == b.length) && ArrayUtils.containsAll(a, b)
257 && ArrayUtils.containsAll(b, a);
Jeff Sharkey94c91dc2013-03-06 16:25:50 -0800258 }
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -0800259
260 /**
261 * Test if given {@link Signature} sets are effectively equal. In rare
262 * cases, certificates can have slightly malformed encoding which causes
263 * exact-byte checks to fail.
264 * <p>
265 * To identify effective equality, we bounce the certificates through an
266 * decode/encode pass before doing the exact-byte check. To reduce attack
267 * surface area, we only allow a byte size delta of a few bytes.
268 *
269 * @throws CertificateException if the before/after length differs
270 * substantially, usually a signal of something fishy going on.
271 * @hide
272 */
273 public static boolean areEffectiveMatch(Signature[] a, Signature[] b)
274 throws CertificateException {
275 final CertificateFactory cf = CertificateFactory.getInstance("X.509");
276
277 final Signature[] aPrime = new Signature[a.length];
278 for (int i = 0; i < a.length; i++) {
279 aPrime[i] = bounce(cf, a[i]);
280 }
281 final Signature[] bPrime = new Signature[b.length];
282 for (int i = 0; i < b.length; i++) {
283 bPrime[i] = bounce(cf, b[i]);
284 }
285
286 return areExactMatch(aPrime, bPrime);
287 }
288
289 /**
Dan Cashman1dbe6d02018-01-23 11:18:28 -0800290 * Test if given {@link Signature} objects are effectively equal. In rare
291 * cases, certificates can have slightly malformed encoding which causes
292 * exact-byte checks to fail.
293 * <p>
294 * To identify effective equality, we bounce the certificates through an
295 * decode/encode pass before doing the exact-byte check. To reduce attack
296 * surface area, we only allow a byte size delta of a few bytes.
297 *
298 * @throws CertificateException if the before/after length differs
299 * substantially, usually a signal of something fishy going on.
300 * @hide
301 */
302 public static boolean areEffectiveMatch(Signature a, Signature b)
303 throws CertificateException {
304 final CertificateFactory cf = CertificateFactory.getInstance("X.509");
305
306 final Signature aPrime = bounce(cf, a);
307 final Signature bPrime = bounce(cf, b);
308
309 return aPrime.equals(bPrime);
310 }
311
312 /**
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -0800313 * Bounce the given {@link Signature} through a decode/encode cycle.
314 *
315 * @throws CertificateException if the before/after length differs
316 * substantially, usually a signal of something fishy going on.
317 * @hide
318 */
319 public static Signature bounce(CertificateFactory cf, Signature s) throws CertificateException {
320 final InputStream is = new ByteArrayInputStream(s.mSignature);
321 final X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
322 final Signature sPrime = new Signature(cert.getEncoded());
323
324 if (Math.abs(sPrime.mSignature.length - s.mSignature.length) > 2) {
325 throw new CertificateException("Bounced cert length looks fishy; before "
326 + s.mSignature.length + ", after " + sPrime.mSignature.length);
327 }
328
329 return sPrime;
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}