Android签名机制之---签名验证过程详解

一、前言

今天是元旦,也是Single Dog的嚎叫之日,只能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:android中是如何验证一个Apk的签名。在前一篇文章中我们介绍了,Android中是如何对程序进行签名的,不了解的同学可以转战:

http://blog.csdn.net/jiangwei0910410003/article/details/50402000

当然在了解我们今天说到的知识点,这篇文章也是需要了解的,不然会有些知识点有些困惑的。


二、知识摘要

在我们没有开始这篇文章之前,我们回顾一下之前说到的签名机制流程:

1、对Apk中的每个文件做一次算法(数据摘要+Base64编码),保存到MANIFEST.MF文件中

2、对MANIFEST.MF整个文件做一次算法(数据摘要+Base64编码),存放到CERT.SF文件的头属性中,在对MANIFEST.MF文件中各个属性块做一次算法(数据摘要+Base64编码),存到到一个属性块中。

3、对CERT.SF文件做签名,内容存档到CERT.RSA中

所以通过上面的流程可以知道,我们今天来验证签名流程也是这三个步骤


三、代码分析

我们既然要了解Android中的应用程序的签名验证过程的话,那么我们肯定需要从一个类来开始看起,那就是PackageManagerService.Java,因为这个类是Apk在安装的过程中核心类:frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java

[java]  view plain  copy
  1. private void installPackageLI(InstallArgs args, PackageInstalledInfo res) {  
  2.     ……  
  3.     PackageParser pp = new PackageParser();  
  4.     ……  
  5.     try {  
  6.         pp.collectCertificates(pkg, parseFlags);  
  7.         pp.collectManifestDigest(pkg);  
  8.     } catch (PackageParserException e) {  
  9.         res.setError("Failed collect during installPackageLI", e);  
  10.         return;  
  11.     }  
  12.     ……  
我们可以看到,有一个核心类:PackageParser

frameworks\base\core\java\android\content\pm\PackageParser.java

这个类也是见名知意,就是需要解析Apk包,那么就会涉及到签名信息了,下面我们就从这个类开始入手:

[java]  view plain  copy
  1. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;  
  2. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;  
  3. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;  
  4. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;  
  5. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;  
  6. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_NOT_APK;  
  7. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;  
  8. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;  
我们看到了几个我们很熟悉的信息:

[java]  view plain  copy
  1. import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;  
这个是在安装apk包的时候出现的错误,没有证书:



那么我们就先来查找一下这个字段:

[java]  view plain  copy
  1. private static void collectCertificates(Package pkg, File apkFile, int flags)  
  2.         throws PackageParserException {  
  3.     final String apkPath = apkFile.getAbsolutePath();  
  4.   
  5.     StrictJarFile jarFile = null;  
  6.     try {  
  7.         jarFile = new StrictJarFile(apkPath);  
  8.   
  9.         // Always verify manifest, regardless of source  
  10.         final ZipEntry manifestEntry = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);  
  11.         if (manifestEntry == null) {  
  12.             throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,  
  13.                     "Package " + apkPath + " has no manifest");  
  14.         }  
  15.   
  16.         final List<ZipEntry> toVerify = new ArrayList<>();  
  17.         toVerify.add(manifestEntry);  
  18.   
  19.         // If we're parsing an untrusted package, verify all contents  
  20.         if ((flags & PARSE_IS_SYSTEM) == 0) {  
  21.             final Iterator<ZipEntry> i = jarFile.iterator();  
  22.             while (i.hasNext()) {  
  23.                 final ZipEntry entry = i.next();  
  24.   
  25.                 if (entry.isDirectory()) continue;  
  26.                 if (entry.getName().startsWith("META-INF/")) continue;  
  27.                 if (entry.getName().equals(ANDROID_MANIFEST_FILENAME)) continue;  
  28.   
  29.                 toVerify.add(entry);  
  30.             }  
  31.         }  
  32.   
  33.         // Verify that entries are signed consistently with the first entry  
  34.         // we encountered. Note that for splits, certificates may have  
  35.         // already been populated during an earlier parse of a base APK.  
  36.         for (ZipEntry entry : toVerify) {  
  37.             final Certificate[][] entryCerts = loadCertificates(jarFile, entry);  
  38.             if (ArrayUtils.isEmpty(entryCerts)) {  
  39.                 throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,  
  40.                         "Package " + apkPath + " has no certificates at entry "  
  41.                                 + entry.getName());  
  42.             }  
  43.             final Signature[] entrySignatures = convertToSignatures(entryCerts);  
  44.   
  45.             if (pkg.mCertificates == null) {  
  46.                 pkg.mCertificates = entryCerts;  
  47.                 pkg.mSignatures = entrySignatures;  
  48.                 pkg.mSigningKeys = new ArraySet<PublicKey>();  
  49.                 for (int i=0; i < entryCerts.length; i++) {  
  50.                     pkg.mSigningKeys.add(entryCerts[i][0].getPublicKey());  
  51.                 }  
  52.             } else {  
  53.                 if (!Signature.areExactMatch(pkg.mSignatures, entrySignatures)) {  
  54.                     throw new PackageParserException(  
  55.                             INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES, "Package " + apkPath  
  56.                             + " has mismatched certificates at entry "  
  57.                             + entry.getName());  
  58.                 }  
  59.             }  
  60.         }  
  61.     } catch (GeneralSecurityException e) {  
  62.         throw new PackageParserException(INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING,  
  63.                 "Failed to collect certificates from " + apkPath, e);  
  64.     } catch (IOException | RuntimeException e) {  
  65.         throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,  
  66.                 "Failed to collect certificates from " + apkPath, e);  
  67.     } finally {  
  68.         closeQuietly(jarFile);  
  69.     }  
  70. }  
这里看到了,当有异常的时候就会提示这个信息,我们在跟进去看看:
[java]  view plain  copy
  1. // Verify that entries are signed consistently with the first entry  
  2. // we encountered. Note that for splits, certificates may have  
  3. // already been populated during an earlier parse of a base APK.  
  4. for (ZipEntry entry : toVerify) {  
  5.     final Certificate[][] entryCerts = loadCertificates(jarFile, entry);  
  6.     if (ArrayUtils.isEmpty(entryCerts)) {  
  7.         throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,  
  8.                 "Package " + apkPath + " has no certificates at entry "  
  9.                         + entry.getName());  
  10.     }  
  11.     final Signature[] entrySignatures = convertToSignatures(entryCerts);  
  12.   
  13.     if (pkg.mCertificates == null) {  
  14.         pkg.mCertificates = entryCerts;  
  15.         pkg.mSignatures = entrySignatures;  
  16.         pkg.mSigningKeys = new ArraySet<PublicKey>();  
  17.         for (int i=0; i < entryCerts.length; i++) {  
  18.             pkg.mSigningKeys.add(entryCerts[i][0].getPublicKey());  
  19.         }  
  20.     } else {  
  21.         if (!Signature.areExactMatch(pkg.mSignatures, entrySignatures)) {  
  22.             throw new PackageParserException(  
  23.                     INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES, "Package " + apkPath  
  24.                     + " has mismatched certificates at entry "  
  25.                     + entry.getName());  
  26.         }  
  27.     }  
  28. }  
这里有一个重要的方法:loadCertificates

[java]  view plain  copy
  1. private static Certificate[][] loadCertificates(StrictJarFile jarFile, ZipEntry entry)  
  2.         throws PackageParserException {  
  3.     InputStream is = null;  
  4.     try {  
  5.         // We must read the stream for the JarEntry to retrieve  
  6.         // its certificates.  
  7.         is = jarFile.getInputStream(entry);  
  8.         readFullyIgnoringContents(is);  
  9.         return jarFile.getCertificateChains(entry);  
  10.     } catch (IOException | RuntimeException e) {  
  11.         throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,  
  12.                 "Failed reading " + entry.getName() + " in " + jarFile, e);  
  13.     } finally {  
  14.         IoUtils.closeQuietly(is);  
  15.     }  
  16. }  
这个方法是加载证书内容的


1、验证Apk中的每个文件的算法(数据摘要+Base64编码)和MANIFEST.MF文件中的对应属性块内容是否配对

首先获取StrictJarFile文件中的InputStream对象

StrictJarFile这个类:libcore\luni\src\main\java\java\util\jar\StrictJarFile.java

[java]  view plain  copy
  1. public InputStream getInputStream(ZipEntry ze) {  
  2.     final InputStream is = getZipInputStream(ze);  
  3.   
  4.     if (isSigned) {  
  5.         JarVerifier.VerifierEntry entry = verifier.initEntry(ze.getName());  
  6.         if (entry == null) {  
  7.             return is;  
  8.         }  
  9.   
  10.         return new JarFile.JarFileInputStream(is, ze.getSize(), entry);  
  11.     }  
  12.   
  13.     return is;  
  14. }  


1》获取到VerifierEntry对象entry

在JarVerifier.java:libcore\luni\src\main\java\java\util\jar\JarVerifier.java

[java]  view plain  copy
  1. VerifierEntry initEntry(String name) {  
  2.     // If no manifest is present by the time an entry is found,  
  3.     // verification cannot occur. If no signature files have  
  4.     // been found, do not verify.  
  5.     if (manifest == null || signatures.isEmpty()) {  
  6.         return null;  
  7.     }  
  8.     Attributes attributes = manifest.getAttributes(name);  
  9.     // entry has no digest  
  10.     if (attributes == null) {  
  11.         return null;  
  12.     }  
  13.     ArrayList<Certificate[]> certChains = new ArrayList<Certificate[]>();  
  14.     Iterator<Map.Entry<String, HashMap<String, Attributes>>> it = signatures.entrySet().iterator();  
  15.     while (it.hasNext()) {  
  16.         Map.Entry<String, HashMap<String, Attributes>> entry = it.next();  
  17.         HashMap<String, Attributes> hm = entry.getValue();  
  18.         if (hm.get(name) != null) {  
  19.             // Found an entry for entry name in .SF file  
  20.             String signatureFile = entry.getKey();  
  21.             Certificate[] certChain = certificates.get(signatureFile);  
  22.             if (certChain != null) {  
  23.                 certChains.add(certChain);  
  24.             }  
  25.         }  
  26.     }  
  27.     // entry is not signed  
  28.     if (certChains.isEmpty()) {  
  29.         return null;  
  30.     }  
  31.     Certificate[][] certChainsArray = certChains.toArray(new Certificate[certChains.size()][]);  
  32.     for (int i = 0; i < DIGEST_ALGORITHMS.length; i++) {  
  33.         final String algorithm = DIGEST_ALGORITHMS[i];  
  34.         final String hash = attributes.getValue(algorithm + "-Digest");  
  35.         if (hash == null) {  
  36.             continue;  
  37.         }  
  38.         byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1);  
  39.         try {  
  40.             return new VerifierEntry(name, MessageDigest.getInstance(algorithm), hashBytes,  
  41.                     certChainsArray, verifiedEntries);  
  42.         } catch (NoSuchAlgorithmException ignored) {  
  43.         }  
  44.     }  
  45.     return null;  
  46. }  
就是构造一个VerifierEntry对象:
[java]  view plain  copy
  1. /** 
  2.  * Stores and a hash and a message digest and verifies that massage digest 
  3.  * matches the hash. 
  4.  */  
  5. static class VerifierEntry extends OutputStream {  
  6.     private final String name;  
  7.     private final MessageDigest digest;  
  8.     private final byte[] hash;  
  9.     private final Certificate[][] certChains;  
  10.     private final Hashtable<String, Certificate[][]> verifiedEntries;  
  11.     VerifierEntry(String name, MessageDigest digest, byte[] hash,  
  12.             Certificate[][] certChains, Hashtable<String, Certificate[][]> verifedEntries) {  
  13.         this.name = name;  
  14.         this.digest = digest;  
  15.         this.hash = hash;  
  16.         this.certChains = certChains;  
  17.         this.verifiedEntries = verifedEntries;  
  18.     }  
  19.     /** 
  20.      * Updates a digest with one byte. 
  21.      */  
  22.      @Override  
  23.      public void write(int value) {  
  24.         digest.update((byte) value);  
  25.      }  
  26.      /** 
  27.       * Updates a digest with byte array. 
  28.       */  
  29.      @Override  
  30.      public void write(byte[] buf, int off, int nbytes) {  
  31.          digest.update(buf, off, nbytes);  
  32.      }  
  33.      /** 
  34.       * Verifies that the digests stored in the manifest match the decrypted 
  35.       * digests from the .SF file. This indicates the validity of the 
  36.       * signing, not the integrity of the file, as its digest must be 
  37.       * calculated and verified when its contents are read. 
  38.       * 
  39.       * @throws SecurityException 
  40.       *             if the digest value stored in the manifest does <i>not</i> 
  41.       *             agree with the decrypted digest as recovered from the 
  42.       *             <code>.SF</code> file. 
  43.       */  
  44.      void verify() {  
  45.          byte[] d = digest.digest();  
  46.          if (!MessageDigest.isEqual(d, Base64.decode(hash))) {  
  47.              throw invalidDigest(JarFile.MANIFEST_NAME, name, name);  
  48.          }  
  49.          verifiedEntries.put(name, certChains);  
  50.      }  
  51. }  
要构造这个对象,必须事先准备好参数。第一个参数很简单,就是要验证的文件名,直接将name传进来就好了。第二个参数是计算摘要的对象,可以通过MessageDigest.getInstance获得,不过要先告知到底要用哪个摘要算法,同样也是通过查看MANIFEST.MF文件中对应名字的属性值来决定的:


所以可以知道所用的摘要算法是SHA1。第三个参数是对应文件的摘要值,这是通过读取MANIFEST.MF文件获得的:


第四个参数是证书链,即对该apk文件签名的所有证书链信息。为什么是二维数组呢?这是因为Android允许用多个证书对apk进行签名,但是它们的证书文件名必须不同,这个知识点,我在之前的一篇文章中:签名过程详解 中有提到。

最后一个参数是已经验证过的文件列表,VerifierEntry在完成了对指定文件的摘要验证之后会将该文件的信息加到其中。

2》再去JarFile的JarFileInputStream类中看看:

[java]  view plain  copy
  1. static final class JarFileInputStream extends FilterInputStream {  
  2.     private long count;  
  3.   
  4.     private ZipEntry zipEntry;  
  5.   
  6.     private JarVerifier.VerifierEntry entry;  
  7.   
  8.     private boolean done = false;  
  9.   
  10.     JarFileInputStream(InputStream is, ZipEntry ze,  
  11.             JarVerifier.VerifierEntry e) {  
  12.         super(is);  
  13.         zipEntry = ze;  
  14.         count = zipEntry.getSize();  
  15.         entry = e;  
  16.     }  
  17.   
  18.     @Override  
  19.     public int read() throws IOException {  
  20.         if (done) {  
  21.             return -1;  
  22.         }  
  23.         if (count > 0) {  
  24.             int r = super.read();  
  25.             if (r != -1) {  
  26.                 entry.write(r);  
  27.                 count--;  
  28.             } else {  
  29.                 count = 0;  
  30.             }  
  31.             if (count == 0) {  
  32.                 done = true;  
  33.                 entry.verify();  
  34.             }  
  35.             return r;  
  36.         } else {  
  37.             done = true;  
  38.             entry.verify();  
  39.             return -1;  
  40.         }  
  41.     }  
  42.   
  43.     @Override  
  44.     public int read(byte[] buf, int off, int nbytes) throws IOException {  
  45.         if (done) {  
  46.             return -1;  
  47.         }  
  48.         if (count > 0) {  
  49.             int r = super.read(buf, off, nbytes);  
  50.             if (r != -1) {  
  51.                 int size = r;  
  52.                 if (count < size) {  
  53.                     size = (int) count;  
  54.                 }  
  55.                 entry.write(buf, off, size);  
  56.                 count -= size;  
  57.             } else {  
  58.                 count = 0;  
  59.             }  
  60.             if (count == 0) {  
  61.                 done = true;  
  62.                 entry.verify();  
  63.             }  
  64.             return r;  
  65.         } else {  
  66.             done = true;  
  67.             entry.verify();  
  68.             return -1;  
  69.         }  
  70.     }  
  71.   
  72.     @Override  
  73.     public int available() throws IOException {  
  74.         if (done) {  
  75.             return 0;  
  76.         }  
  77.         return super.available();  
  78.     }  
  79.   
  80.     @Override  
  81.     public long skip(long byteCount) throws IOException {  
  82.         return Streams.skipByReading(this, byteCount);  
  83.     }  
  84. }  


3》PackageParser的readFullyIgnoringContents方法:
[java]  view plain  copy
  1. public static long readFullyIgnoringContents(InputStream in) throws IOException {  
  2.     byte[] buffer = sBuffer.getAndSet(null);  
  3.     if (buffer == null) {  
  4.         buffer = new byte[4096];  
  5.     }  
  6.   
  7.     int n = 0;  
  8.     int count = 0;  
  9.     while ((n = in.read(buffer, 0, buffer.length)) != -1) {  
  10.         count += n;  
  11.     }  
  12.   
  13.     sBuffer.set(buffer);  
  14.     return count;  
  15. }  
得到第二步之后的一个InputStream对象,然后就开始read操作,这里我没发现什么猫腻,但是我们从第一件事做完之后可以发现,这里的InputStream对象其实是JarInputStream,所以我们可以去看一下他的read方法的实现:


玄机原来在这里,这里的JarFileInputStream.read确实会调用其父类的read读取指定的apk内文件的内容,并且将其传给JarVerifier.VerifierEntry.write函数。当文件读完后,会接着调用JarVerifier.VerifierEntry.verify函数对其进行验证。JarVerifier.VerifierEntry.write函数非常简单:


就是将读到的文件的内容传给digest,这个digest就是前面在构造JarVerifier.VerifierEntry传进来的,对应于在MANIFEST.MF文件中指定的摘要算法。万事具备,接下来想要验证就很简单了:


通过digest就可以算出apk内指定文件的真实摘要值。而记录在MANIFEST.MF文件中对应该文件的摘要值,也在构造JarVerifier.VerifierEntry时传递给了hash变量。不过这个hash值是经过Base64编码的。所以在比较之前,必须通过Base64解码。如果不一致的话,会抛出SecurityException异常:

[java]  view plain  copy
  1. private static SecurityException invalidDigest(String signatureFile, String name,  
  2.         String jarName) {  
  3.     throw new SecurityException(signatureFile + " has invalid digest for " + name +  
  4.             " in " + jarName);  
  5. }  
到这里我们就分析了,Android中是如何验证MANIFEST.MF文件中的内容的,我们这里再来看一下,这里抛出异常出去:


这里捕获到异常之后,会在抛异常出去:


在这里就会抛出异常信息,所以如果我们修改了一个Apk中的一个文件内容的话,这里肯定是安装不上的。


2、验证CERT.SF文件的签名信息和CERT.RSA中的内容是否一致

1》我们就来看看StrictJarFile中的getCertificateChains方法:


[java]  view plain  copy
  1. /** 
  2.  * Return all certificate chains for a given {@link ZipEntry} belonging to this jar. 
  3.  * This method MUST be called only after fully exhausting the InputStream belonging 
  4.  * to this entry. 
  5.  * 
  6.  * Returns {@code null} if this jar file isn't signed or if this method is 
  7.  * called before the stream is processed. 
  8.  */  
  9. public Certificate[][] getCertificateChains(ZipEntry ze) {  
  10.     if (isSigned) {  
  11.         return verifier.getCertificateChains(ze.getName());  
  12.     }  
  13.   
  14.     return null;  
  15. }  
这里有一个变量判断:isSigned,他是在构造方法中赋值的:
[java]  view plain  copy
  1. public StrictJarFile(String fileName) throws IOException {  
  2.     this.nativeHandle = nativeOpenJarFile(fileName);  
  3.     this.raf = new RandomAccessFile(fileName, "r");  
  4.   
  5.     try {  
  6.         // Read the MANIFEST and signature files up front and try to  
  7.         // parse them. We never want to accept a JAR File with broken signatures  
  8.         // or manifests, so it's best to throw as early as possible.  
  9.         HashMap<String, byte[]> metaEntries = getMetaEntries();  
  10.         this.manifest = new Manifest(metaEntries.get(JarFile.MANIFEST_NAME), true);  
  11.         this.verifier = new JarVerifier(fileName, manifest, metaEntries);  
  12.   
  13.         isSigned = verifier.readCertificates() && verifier.isSignedJar();  
  14.     } catch (IOException ioe) {  
  15.         nativeClose(this.nativeHandle);  
  16.         throw ioe;  
  17.     }  
  18.   
  19.     guard.open("close");  
  20. }  
去verifier中看看这两个方法:
[java]  view plain  copy
  1. /** 
  2.  * If the associated JAR file is signed, check on the validity of all of the 
  3.  * known signatures. 
  4.  * 
  5.  * @return {@code true} if the associated JAR is signed and an internal 
  6.  *         check verifies the validity of the signature(s). {@code false} if 
  7.  *         the associated JAR file has no entries at all in its {@code 
  8.  *         META-INF} directory. This situation is indicative of an invalid 
  9.  *         JAR file. 
  10.  *         <p> 
  11.  *         Will also return {@code true} if the JAR file is <i>not</i> 
  12.  *         signed. 
  13.  * @throws SecurityException 
  14.  *             if the JAR file is signed and it is determined that a 
  15.  *             signature block file contains an invalid signature for the 
  16.  *             corresponding signature file. 
  17.  */  
  18. synchronized boolean readCertificates() {  
  19.     if (metaEntries.isEmpty()) {  
  20.         return false;  
  21.     }  
  22.     Iterator<String> it = metaEntries.keySet().iterator();  
  23.     while (it.hasNext()) {  
  24.         String key = it.next();  
  25.         if (key.endsWith(".DSA") || key.endsWith(".RSA") || key.endsWith(".EC")) {  
  26.             verifyCertificate(key);  
  27.             it.remove();  
  28.         }  
  29.     }  
  30.     return true;  
  31. }  
这个方法其实很简单,就是判断metaEntries中是否为空,说白了,就是判断Apk中的META-INF文件夹中是否为空,只有文件就返回true。再来看看isSignedJar方法:
[java]  view plain  copy
  1. /** 
  2.  * Returns a <code>boolean</code> indication of whether or not the 
  3.  * associated jar file is signed. 
  4.  * 
  5.  * @return {@code true} if the JAR is signed, {@code false} 
  6.  *         otherwise. 
  7.  */  
  8. boolean isSignedJar() {  
  9.     return certificates.size() > 0;  
  10. }  
这个方法直接判断certificates这个集合是否为空。我们全局搜索一下这个集合在哪里存入的数据的地方,找到了verifyCertificate方法,同时我们发现,在上面的readCertificates方法中,就调用了这个方法,其实这个方法就是读取证书信息的。

下面来看一下verifyCertificate方法:

[java]  view plain  copy
  1. /** 
  2.  * @param certFile 
  3.  */  
  4. private void verifyCertificate(String certFile) {  
  5.     // Found Digital Sig, .SF should already have been read  
  6.     String signatureFile = certFile.substring(0, certFile.lastIndexOf('.')) + ".SF";  
  7.     byte[] sfBytes = metaEntries.get(signatureFile);  
  8.     if (sfBytes == null) {  
  9.         return;  
  10.     }  
  11.     byte[] manifestBytes = metaEntries.get(JarFile.MANIFEST_NAME);  
  12.     // Manifest entry is required for any verifications.  
  13.     if (manifestBytes == null) {  
  14.         return;  
  15.     }  
  16.     byte[] sBlockBytes = metaEntries.get(certFile);  
  17.     try {  
  18.         Certificate[] signerCertChain = JarUtils.verifySignature(  
  19.                 new ByteArrayInputStream(sfBytes),  
  20.                 new ByteArrayInputStream(sBlockBytes));  
  21.         if (signerCertChain != null) {  
  22.             certificates.put(signatureFile, signerCertChain);  
  23.         }  
  24.     } catch (IOException e) {  
  25.         return;  
  26.     } catch (GeneralSecurityException e) {  
  27.         throw failedVerification(jarName, signatureFile);  
  28.     }  
  29.     // Verify manifest hash in .sf file  
  30.     Attributes attributes = new Attributes();  
  31.     HashMap<String, Attributes> entries = new HashMap<String, Attributes>();  
  32.     try {  
  33.         ManifestReader im = new ManifestReader(sfBytes, attributes);  
  34.         im.readEntries(entries, null);  
  35.     } catch (IOException e) {  
  36.         return;  
  37.     }  
  38.     // Do we actually have any signatures to look at?  
  39.     if (attributes.get(Attributes.Name.SIGNATURE_VERSION) == null) {  
  40.         return;  
  41.     }  
  42.     boolean createdBySigntool = false;  
  43.     String createdBy = attributes.getValue("Created-By");  
  44.     if (createdBy != null) {  
  45.         createdBySigntool = createdBy.indexOf("signtool") != -1;  
  46.     }  
  47.     // Use .SF to verify the mainAttributes of the manifest  
  48.     // If there is no -Digest-Manifest-Main-Attributes entry in .SF  
  49.     // file, such as those created before java 1.5, then we ignore  
  50.     // such verification.  
  51.     if (mainAttributesEnd > 0 && !createdBySigntool) {  
  52.         String digestAttribute = "-Digest-Manifest-Main-Attributes";  
  53.         if (!verify(attributes, digestAttribute, manifestBytes, 0, mainAttributesEnd, falsetrue)) {  
  54.             throw failedVerification(jarName, signatureFile);  
  55.         }  
  56.     }  
  57.     // Use .SF to verify the whole manifest.  
  58.     String digestAttribute = createdBySigntool ? "-Digest" : "-Digest-Manifest";  
  59.     if (!verify(attributes, digestAttribute, manifestBytes, 0, manifestBytes.length, falsefalse)) {  
  60.         Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();  
  61.         while (it.hasNext()) {  
  62.             Map.Entry<String, Attributes> entry = it.next();  
  63.             Manifest.Chunk chunk = manifest.getChunk(entry.getKey());  
  64.             if (chunk == null) {  
  65.                 return;  
  66.             }  
  67.             if (!verify(entry.getValue(), "-Digest", manifestBytes,  
  68.                     chunk.start, chunk.end, createdBySigntool, false)) {  
  69.                 throw invalidDigest(signatureFile, entry.getKey(), jarName);  
  70.             }  
  71.         }  
  72.     }  
  73.     metaEntries.put(signatureFile, null);  
  74.     signatures.put(signatureFile, entries);  
  75. }  


2》获取证书信息,并且验证CERT.SF文件的签名信息和CERT.RSA中的内容是否一致。

[java]  view plain  copy
  1. // Found Digital Sig, .SF should already have been read  
  2. String signatureFile = certFile.substring(0, certFile.lastIndexOf('.')) + ".SF";  
  3. byte[] sfBytes = metaEntries.get(signatureFile);  
  4. if (sfBytes == null) {  
  5.     return;  
  6. }  
  7. byte[] manifestBytes = metaEntries.get(JarFile.MANIFEST_NAME);  
  8. // Manifest entry is required for any verifications.  
  9. if (manifestBytes == null) {  
  10.     return;  
  11. }  
  12. byte[] sBlockBytes = metaEntries.get(certFile);  
  13. try {  
  14.     Certificate[] signerCertChain = JarUtils.verifySignature(  
  15.             new ByteArrayInputStream(sfBytes),  
  16.             new ByteArrayInputStream(sBlockBytes));  
  17.     if (signerCertChain != null) {  
  18.         certificates.put(signatureFile, signerCertChain);  
  19.     }  
  20. catch (IOException e) {  
  21.     return;  
  22. catch (GeneralSecurityException e) {  
  23.     throw failedVerification(jarName, signatureFile);  
  24. }  

这里首先获取到,签名文件。我们在之前的一篇文章中说到了,签名文件和证书文件的名字是一样的。

同时这里还调用了JarUtils类:libcore\luni\src\main\java\org\apache\harmony\security\utils\JarUtils.java

中的verifySignature方法来获取证书,这里就不做太多的解释了,如何从一个RSA文件中获取证书,这样的代码网上也是有的,而且后面我会演示一下,如何获取。

[java]  view plain  copy
  1. /** 
  2.  * This method handle all the work with  PKCS7, ASN1 encoding, signature verifying, 
  3.  * and certification path building. 
  4.  * See also PKCS #7: Cryptographic Message Syntax Standard: 
  5.  * http://www.ietf.org/rfc/rfc2315.txt 
  6.  * @param signature - the input stream of signature file to be verified 
  7.  * @param signatureBlock - the input stream of corresponding signature block file 
  8.  * @return array of certificates used to verify the signature file 
  9.  * @throws IOException - if some errors occurs during reading from the stream 
  10.  * @throws GeneralSecurityException - if signature verification process fails 
  11.  */  
  12. public static Certificate[] verifySignature(InputStream signature, InputStream  
  13.         signatureBlock) throws IOException, GeneralSecurityException {  
  14.   
  15.     BerInputStream bis = new BerInputStream(signatureBlock);  
  16.     ContentInfo info = (ContentInfo)ContentInfo.ASN1.decode(bis);  
  17.     SignedData signedData = info.getSignedData();  
  18.     if (signedData == null) {  
  19.         throw new IOException("No SignedData found");  
  20.     }  
  21.     Collection<org.apache.harmony.security.x509.Certificate> encCerts  
  22.     = signedData.getCertificates();  
  23.     if (encCerts.isEmpty()) {  
  24.         return null;  
  25.     }  
  26.     X509Certificate[] certs = new X509Certificate[encCerts.size()];  
  27.     int i = 0;  
  28.     for (org.apache.harmony.security.x509.Certificate encCert : encCerts) {  
  29.         certs[i++] = new X509CertImpl(encCert);  
  30.     }  
  31.   
  32.     List<SignerInfo> sigInfos = signedData.getSignerInfos();  
  33.     SignerInfo sigInfo;  
  34.     if (!sigInfos.isEmpty()) {  
  35.         sigInfo = sigInfos.get(0);  
  36.     } else {  
  37.         return null;  
  38.     }  
  39.   
  40.     // Issuer  
  41.     X500Principal issuer = sigInfo.getIssuer();  
  42.   
  43.     // Certificate serial number  
  44.     BigInteger snum = sigInfo.getSerialNumber();  
  45.   
  46.     // Locate the certificate  
  47.     int issuerSertIndex = 0;  
  48.     for (i = 0; i < certs.length; i++) {  
  49.         if (issuer.equals(certs[i].getIssuerDN()) &&  
  50.                 snum.equals(certs[i].getSerialNumber())) {  
  51.             issuerSertIndex = i;  
  52.             break;  
  53.         }  
  54.     }  
  55.     if (i == certs.length) { // No issuer certificate found  
  56.         return null;  
  57.     }  
  58.   
  59.     if (certs[issuerSertIndex].hasUnsupportedCriticalExtension()) {  
  60.         throw new SecurityException("Can not recognize a critical extension");  
  61.     }  
  62.   
  63.     // Get Signature instance  
  64.     Signature sig = null;  
  65.     String da = sigInfo.getDigestAlgorithm();  
  66.     String dea = sigInfo.getDigestEncryptionAlgorithm();  
  67.     String alg = null;  
  68.     if (da != null && dea != null) {  
  69.         alg = da + "with" +  dea;  
  70.         try {  
  71.             sig = Signature.getInstance(alg, OpenSSLProvider.PROVIDER_NAME);  
  72.         } catch (NoSuchAlgorithmException e) {}  
  73.     }  
  74.     if (sig == null) {  
  75.         alg = da;  
  76.         if (alg == null) {  
  77.             return null;  
  78.         }  
  79.         try {  
  80.             sig = Signature.getInstance(alg, OpenSSLProvider.PROVIDER_NAME);  
  81.         } catch (NoSuchAlgorithmException e) {  
  82.             return null;  
  83.         }  
  84.     }  
  85.     sig.initVerify(certs[issuerSertIndex]);  
  86.     ......  

这里返回的是一个证书的数组。


3、MANIFEST.MF整个文件签名在CERT.SF文件中头属性中的值是否匹配以及验证MANIFEST.MF文件中的各个属性块的签名在CERT.SF文件中是否匹配

1》第一件事是:验证MANIFEST.MF整个文件签名在CERT.SF文件中头属性中的值是否匹配

[java]  view plain  copy
  1. // Use .SF to verify the mainAttributes of the manifest  
  2. // If there is no -Digest-Manifest-Main-Attributes entry in .SF  
  3. // file, such as those created before java 1.5, then we ignore  
  4. // such verification.  
  5. if (mainAttributesEnd > 0 && !createdBySigntool) {  
  6.     String digestAttribute = "-Digest-Manifest-Main-Attributes";  
  7.     if (!verify(attributes, digestAttribute, manifestBytes, 0, mainAttributesEnd, falsetrue)) {  
  8.         throw failedVerification(jarName, signatureFile);  
  9.     }  
  10. }  
这里的manifestBytes:

[java]  view plain  copy
  1. byte[] manifestBytes = metaEntries.get(JarFile.MANIFEST_NAME);  
就是MANIFEST.MF文件内容。继续看一下verify方法:
[java]  view plain  copy
  1. private boolean verify(Attributes attributes, String entry, byte[] data,  
  2.         int start, int end, boolean ignoreSecondEndline, boolean ignorable) {  
  3.     for (int i = 0; i < DIGEST_ALGORITHMS.length; i++) {  
  4.         String algorithm = DIGEST_ALGORITHMS[i];  
  5.         String hash = attributes.getValue(algorithm + entry);  
  6.         if (hash == null) {  
  7.             continue;  
  8.         }  
  9.         MessageDigest md;  
  10.         try {  
  11.             md = MessageDigest.getInstance(algorithm);  
  12.         } catch (NoSuchAlgorithmException e) {  
  13.             continue;  
  14.         }  
  15.         if (ignoreSecondEndline && data[end - 1] == '\n' && data[end - 2] == '\n') {  
  16.             md.update(data, start, end - 1 - start);  
  17.         } else {  
  18.             md.update(data, start, end - start);  
  19.         }  
  20.         byte[] b = md.digest();  
  21.         byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1);  
  22.         return MessageDigest.isEqual(b, Base64.decode(hashBytes));  
  23.     }  
  24.     return ignorable;  
  25. }  
这个方法其实很简单,就是验证传入的data数据块的数据摘要算法和传入的attributes中的算法块的值是否匹配,比如这里:
[java]  view plain  copy
  1. String algorithm = DIGEST_ALGORITHMS[i];  
  2. String hash = attributes.getValue(algorithm + entry);  
这里的algorithm是算法:
[java]  view plain  copy
  1. private static final String[] DIGEST_ALGORITHMS = new String[] {  
  2.     "SHA-512",  
  3.     "SHA-384",  
  4.     "SHA-256",  
  5.     "SHA1",  
  6. };  

这里的entry也是传入的,我们看到传入的是:-Digest


这样就是CERT.SF文件中的一个条目:



2》第二件事是:验证MANIFEST.MF文件中的各个属性块的签名在CERT.SF文件中是否匹配

[java]  view plain  copy
  1. // Use .SF to verify the whole manifest.  
  2. String digestAttribute = createdBySigntool ? "-Digest" : "-Digest-Manifest";  
  3. if (!verify(attributes, digestAttribute, manifestBytes, 0, manifestBytes.length, falsefalse)) {  
  4.     Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();  
  5.     while (it.hasNext()) {  
  6.         Map.Entry<String, Attributes> entry = it.next();  
  7.         Manifest.Chunk chunk = manifest.getChunk(entry.getKey());  
  8.         if (chunk == null) {  
  9.             return;  
  10.         }  
  11.         if (!verify(entry.getValue(), "-Digest", manifestBytes,  
  12.                 chunk.start, chunk.end, createdBySigntool, false)) {  
  13.             throw invalidDigest(signatureFile, entry.getKey(), jarName);  
  14.         }  
  15.     }  
  16. }  
这里我们可以看到也是同样调用verify方法来验证CERT.SF中的条目信息的。


最后我们再看一下是如何配对签名信息的,在PackageParser中的collectCertificates方法:


这里会比对已经安装的apk的签名和准备要安装的apk的签名是否一致,如果不一致的话,就会报错:


这个错,也是我们经常会遇到的,就是同样的apk,签名不一致导致的问题。

我们从上面的分析代码中可以看到,这里的Signature比对签名,其实就是比对证书中的公钥信息:


上面我们就看完了Android中验证签名信息的流程,下面我们再来梳理一下流程吧:

所有有关apk文件的签名验证工作都是在JarVerifier里面做的,一共分成三步:

1、JarVerifier.VerifierEntry.verify做了验证,即保证apk文件中包含的所有文件,对应的摘要值与MANIFEST.MF文件中记录的一致。

2、JarVeirifer.verifyCertificate使用证书文件(在META-INF目录下,以.DSA、.RSA或者.EC结尾的文件)检验签名文件(在META-INF目录下,和证书文件同名,但扩展名为.SF的文件)是没有被修改过的。这里我们可以注意到,Android中在验证的过程中对SF喝RSA文件的名字并不关心,这个在之前的 签名过程 文章中介绍到了。

3、JarVeirifer.verifyCertificate中使用签名文件CERT.SF,检验MANIFEST.MF文件中的内容也没有被篡改过

综上所述:

首先,如果你改变了apk包中的任何文件,那么在apk安装校验时,改变后的文件摘要信息与MANIFEST.MF的检验信息不同,于是验证失败,程序就不能成功安装。
其次,如果你对更改的过的文件相应的算出新的摘要值,然后更改MANIFEST.MF文件里面对应的属性值,那么必定与CERT.SF文件中算出的摘要值不一样,照样验证失败。
这里都会提示安装失败信息:


如果你还不死心,继续计算MANIFEST.MF的摘要值,相应的更改CERT.SF里面的值.

那么数字签名值必定与CERT.RSA文件中记录的不一样,还是失败。

这里的失败信息:


那么能不能继续伪造数字签名呢?不可能,因为没有数字证书对应的私钥。
所以,如果要重新打包后的应用程序能再Android设备上安装,必须对其进行重签名。
从上面的分析可以得出,只要修改了Apk中的任何内容,就必须重新签名,不然会提示安装失败,当然这里不会分析,后面一篇文章会注重分析为何会提示安装失败。


总结

到这里我们就介绍完了Android中的apk的签名验证过程,再结合之前的一篇文章,我们可以了解到了Android中的签名机制了。这个也是对Android中的安全机制的一个深入了解吧,新年快乐~~

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值