The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content.pm; |
| 18 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame^] | 19 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 23 | import com.android.internal.util.ArrayUtils; |
| 24 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 25 | import java.io.ByteArrayInputStream; |
Jeff Sharkey | d68b87c | 2014-11-12 12:18:11 -0800 | [diff] [blame] | 26 | import java.io.InputStream; |
Kenny Root | de0ff63 | 2010-09-03 16:02:27 -0700 | [diff] [blame] | 27 | import java.lang.ref.SoftReference; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 28 | import java.security.PublicKey; |
| 29 | import java.security.cert.Certificate; |
Kenny Root | a8e65fd | 2014-04-24 11:44:47 -0700 | [diff] [blame] | 30 | import java.security.cert.CertificateEncodingException; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 31 | import java.security.cert.CertificateException; |
| 32 | import java.security.cert.CertificateFactory; |
Jeff Sharkey | d68b87c | 2014-11-12 12:18:11 -0800 | [diff] [blame] | 33 | import java.security.cert.X509Certificate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import java.util.Arrays; |
| 35 | |
| 36 | /** |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 37 | * Opaque, immutable representation of a signing certificate associated with an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * application package. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 39 | * <p> |
| 40 | * This class name is slightly misleading, since it's not actually a signature. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | */ |
| 42 | public class Signature implements Parcelable { |
| 43 | private final byte[] mSignature; |
| 44 | private int mHashCode; |
| 45 | private boolean mHaveHashCode; |
Kenny Root | de0ff63 | 2010-09-03 16:02:27 -0700 | [diff] [blame] | 46 | private SoftReference<String> mStringRef; |
Kenny Root | a8e65fd | 2014-04-24 11:44:47 -0700 | [diff] [blame] | 47 | private Certificate[] mCertificateChain; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Create Signature from an existing raw byte array. |
| 51 | */ |
| 52 | public Signature(byte[] signature) { |
| 53 | mSignature = signature.clone(); |
Kenny Root | a8e65fd | 2014-04-24 11:44:47 -0700 | [diff] [blame] | 54 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 71 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Create Signature from a text representation previously returned by |
Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 85 | * {@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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | */ |
| 91 | public Signature(String text) { |
Kenny Root | d21d444 | 2011-07-22 15:32:30 -0700 | [diff] [blame] | 92 | final byte[] input = text.getBytes(); |
| 93 | final int N = input.length; |
Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 94 | |
| 95 | if (N % 2 != 0) { |
| 96 | throw new IllegalArgumentException("text size " + N + " is not even"); |
| 97 | } |
| 98 | |
Kenny Root | d21d444 | 2011-07-22 15:32:30 -0700 | [diff] [blame] | 99 | final byte[] sig = new byte[N / 2]; |
| 100 | int sigIndex = 0; |
| 101 | |
| 102 | for (int i = 0; i < N;) { |
Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 103 | final int hi = parseHexDigit(input[i++]); |
| 104 | final int lo = parseHexDigit(input[i++]); |
| 105 | sig[sigIndex++] = (byte) ((hi << 4) | lo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
Kenny Root | d21d444 | 2011-07-22 15:32:30 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | 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 Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 146 | * Return the result of {@link #toChars()} as a String. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | */ |
| 148 | public String toCharsString() { |
Kenny Root | de0ff63 | 2010-09-03 16:02:27 -0700 | [diff] [blame] | 149 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
| 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 Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 167 | /** |
| 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 Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame^] | 174 | @UnsupportedAppUsage |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 175 | 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 Root | a8e65fd | 2014-04-24 11:44:47 -0700 | [diff] [blame] | 182 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public boolean equals(Object obj) { |
| 207 | try { |
| 208 | if (obj != null) { |
| 209 | Signature other = (Signature)obj; |
Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 210 | return this == other || Arrays.equals(mSignature, other.mSignature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 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 Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 249 | |
| 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 Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 256 | return (a.length == b.length) && ArrayUtils.containsAll(a, b) |
| 257 | && ArrayUtils.containsAll(b, a); |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 258 | } |
Jeff Sharkey | d68b87c | 2014-11-12 12:18:11 -0800 | [diff] [blame] | 259 | |
| 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 Cashman | 1dbe6d0 | 2018-01-23 11:18:28 -0800 | [diff] [blame] | 290 | * 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 Sharkey | d68b87c | 2014-11-12 12:18:11 -0800 | [diff] [blame] | 313 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |