| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
import android.util.TypedXmlSerializer; |
| 25 |
|
| 26 |
import com.android.internal.util.ArrayUtils; |
| 27 |
|
| 28 |
import java.io.ByteArrayInputStream; |
| 29 |
import java.io.IOException; |
| 30 |
import java.io.InputStream; |
| 31 |
import java.lang.ref.SoftReference; |
| 32 |
import java.security.PublicKey; |
| 33 |
import java.security.cert.Certificate; |
| 34 |
import java.security.cert.CertificateEncodingException; |
| 35 |
import java.security.cert.CertificateException; |
| 36 |
import java.security.cert.CertificateFactory; |
| 37 |
import java.security.cert.X509Certificate; |
| 38 |
import java.util.Arrays; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class Signature implements Parcelable { |
| 47 |
private final byte[] mSignature; |
| 48 |
private int mHashCode; |
| 49 |
private boolean mHaveHashCode; |
| 50 |
private SoftReference<String> mStringRef; |
| 51 |
private Certificate[] mCertificateChain; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private int mFlags; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public Signature(byte[] signature) { |
| 71 |
mSignature = signature.clone(); |
| 72 |
mCertificateChain = null; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public Signature(Certificate[] certificateChain) throws CertificateEncodingException { |
| 83 |
mSignature = certificateChain[0].getEncoded(); |
| 84 |
if (certificateChain.length > 1) { |
| 85 |
mCertificateChain = Arrays.copyOfRange(certificateChain, 1, certificateChain.length); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private static final int parseHexDigit(int nibble) { |
| 90 |
if ('0' <= nibble && nibble <= '9') { |
| 91 |
return nibble - '0'; |
| 92 |
} else if ('a' <= nibble && nibble <= 'f') { |
| 93 |
return nibble - 'a' + 10; |
| 94 |
} else if ('A' <= nibble && nibble <= 'F') { |
| 95 |
return nibble - 'A' + 10; |
| 96 |
} else { |
| 97 |
throw new IllegalArgumentException("Invalid character " + nibble + " in hex string"); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public Signature(String text) { |
| 110 |
final byte[] input = text.getBytes(); |
| 111 |
final int N = input.length; |
| 112 |
|
| 113 |
if (N % 2 != 0) { |
| 114 |
throw new IllegalArgumentException("text size " + N + " is not even"); |
| 115 |
} |
| 116 |
|
| 117 |
final byte[] sig = new byte[N / 2]; |
| 118 |
int sigIndex = 0; |
| 119 |
|
| 120 |
for (int i = 0; i < N;) { |
| 121 |
final int hi = parseHexDigit(input[i++]); |
| 122 |
final int lo = parseHexDigit(input[i++]); |
| 123 |
sig[sigIndex++] = (byte) ((hi << 4) | lo); |
| 124 |
} |
| 125 |
|
| 126 |
mSignature = sig; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public Signature(Signature other) { |
| 135 |
mSignature = other.mSignature.clone(); |
| 136 |
Certificate[] otherCertificateChain = other.mCertificateChain; |
| 137 |
if (otherCertificateChain != null && otherCertificateChain.length > 1) { |
| 138 |
mCertificateChain = Arrays.copyOfRange(otherCertificateChain, 1, |
| 139 |
otherCertificateChain.length); |
| 140 |
} |
| 141 |
mFlags = other.mFlags; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public void setFlags(int flags) { |
| 149 |
this.mFlags = flags; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public int getFlags() { |
| 157 |
return mFlags; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public char[] toChars() { |
| 164 |
return toChars(null, null); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public char[] toChars(char[] existingArray, int[] outLen) { |
| 178 |
byte[] sig = mSignature; |
| 179 |
final int N = sig.length; |
| 180 |
final int N2 = N*2; |
| 181 |
char[] text = existingArray == null || N2 > existingArray.length |
| 182 |
? new char[N2] : existingArray; |
| 183 |
for (int j=0; j<N; j++) { |
| 184 |
byte v = sig[j]; |
| 185 |
int d = (v>>4)&0xf; |
| 186 |
text[j*2] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d)); |
| 187 |
d = v&0xf; |
| 188 |
text[j*2+1] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d)); |
| 189 |
} |
| 190 |
if (outLen != null) outLen[0] = N; |
| 191 |
return text; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public String toCharsString() { |
| 198 |
String str = mStringRef == null ? null : mStringRef.get(); |
| 199 |
if (str != null) { |
| 200 |
return str; |
| 201 |
} |
| 202 |
str = new String(toChars()); |
| 203 |
mStringRef = new SoftReference<String>(str); |
| 204 |
return str; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public byte[] toByteArray() { |
| 211 |
byte[] bytes = new byte[mSignature.length]; |
| 212 |
System.arraycopy(mSignature, 0, bytes, 0, mSignature.length); |
| 213 |
return bytes; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@UnsupportedAppUsage |
| 224 |
public PublicKey getPublicKey() throws CertificateException { |
| 225 |
final CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 226 |
final ByteArrayInputStream bais = new ByteArrayInputStream(mSignature); |
| 227 |
final Certificate cert = certFactory.generateCertificate(bais); |
| 228 |
return cert.getPublicKey(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public Signature[] getChainSignatures() throws CertificateEncodingException { |
| 239 |
if (mCertificateChain == null) { |
| 240 |
return new Signature[] { this }; |
| 241 |
} |
| 242 |
|
| 243 |
Signature[] chain = new Signature[1 + mCertificateChain.length]; |
| 244 |
chain[0] = this; |
| 245 |
|
| 246 |
int i = 1; |
| 247 |
for (Certificate c : mCertificateChain) { |
| 248 |
chain[i++] = new Signature(c.getEncoded()); |
| 249 |
} |
| 250 |
|
| 251 |
return chain; |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public boolean equals(@Nullable Object obj) { |
| 256 |
try { |
| 257 |
if (obj != null) { |
| 258 |
Signature other = (Signature)obj; |
| 259 |
|
| 260 |
|
| 261 |
return this == other || Arrays.equals(mSignature, other.mSignature); |
| 262 |
} |
| 263 |
} catch (ClassCastException e) { |
| 264 |
} |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public int hashCode() { |
| 270 |
if (mHaveHashCode) { |
| 271 |
return mHashCode; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
mHashCode = Arrays.hashCode(mSignature); |
| 276 |
mHaveHashCode = true; |
| 277 |
return mHashCode; |
| 278 |
} |
| 279 |
|
| 280 |
public int describeContents() { |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 285 |
dest.writeByteArray(mSignature); |
| 286 |
} |
| 287 |
|
| 288 |
public static final @android.annotation.NonNull Parcelable.Creator<Signature> CREATOR |
| 289 |
= new Parcelable.Creator<Signature>() { |
| 290 |
public Signature createFromParcel(Parcel source) { |
| 291 |
return new Signature(source); |
| 292 |
} |
| 293 |
|
| 294 |
public Signature[] newArray(int size) { |
| 295 |
return new Signature[size]; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
public void writeToXmlAttributeBytesHex(@NonNull TypedXmlSerializer out, |
| 301 |
@Nullable String namespace, @NonNull String name) throws IOException { |
| 302 |
out.attributeBytesHex(namespace, name, mSignature); |
| 303 |
} |
| 304 |
|
| 305 |
private Signature(Parcel source) { |
| 306 |
mSignature = source.createByteArray(); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public static boolean areExactMatch(Signature[] a, Signature[] b) { |
| 315 |
return (ArrayUtils.size(a) == ArrayUtils.size(b)) && ArrayUtils.containsAll(a, b) |
| 316 |
&& ArrayUtils.containsAll(b, a); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public static boolean areEffectiveMatch(Signature[] a, Signature[] b) |
| 333 |
throws CertificateException { |
| 334 |
final CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 335 |
|
| 336 |
final Signature[] aPrime = new Signature[a.length]; |
| 337 |
for (int i = 0; i < a.length; i++) { |
| 338 |
aPrime[i] = bounce(cf, a[i]); |
| 339 |
} |
| 340 |
final Signature[] bPrime = new Signature[b.length]; |
| 341 |
for (int i = 0; i < b.length; i++) { |
| 342 |
bPrime[i] = bounce(cf, b[i]); |
| 343 |
} |
| 344 |
|
| 345 |
return areExactMatch(aPrime, bPrime); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public static boolean areEffectiveMatch(Signature a, Signature b) |
| 362 |
throws CertificateException { |
| 363 |
final CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 364 |
|
| 365 |
final Signature aPrime = bounce(cf, a); |
| 366 |
final Signature bPrime = bounce(cf, b); |
| 367 |
|
| 368 |
return aPrime.equals(bPrime); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public static Signature bounce(CertificateFactory cf, Signature s) throws CertificateException { |
| 379 |
final InputStream is = new ByteArrayInputStream(s.mSignature); |
| 380 |
final X509Certificate cert = (X509Certificate) cf.generateCertificate(is); |
| 381 |
final Signature sPrime = new Signature(cert.getEncoded()); |
| 382 |
|
| 383 |
if (Math.abs(sPrime.mSignature.length - s.mSignature.length) > 2) { |
| 384 |
throw new CertificateException("Bounced cert length looks fishy; before " |
| 385 |
+ s.mSignature.length + ", after " + sPrime.mSignature.length); |
| 386 |
} |
| 387 |
|
| 388 |
return sPrime; |
| 389 |
} |
| 390 |
} |