http://blog.csdn.net/u010571535/article/details/8992543#
一、Android证书的格式
Android签名中证书的格式采用X.509标准的版本三,不过省略了一些内容。
X.509证书格式如下图所示:
二、证书的格式示例
从Chrome浏览器中导出一个证书,打开之后的样子如下图所示:
说明:由上图可以看出,在证书中,开发者的公钥是显式存在的。
三、对CERT.RSA进行详细解析
重要说明:编写Java程序和编写Android应用App解析Apk的CERT.RSA文件,得到的结果是不一样的。按道理,都是解析的同一个文件,为什么结果不一样呢?经过我们分析,发现不一样的地方是开发者公钥,但是这是由于二者的显示格式不同导致的。App解析得到的文件时十六进制显示的,而Java解析得到的结果却是十进制显示的。我们利用Java的BigInteger编写程序,将十六进制转换为十进制,验证了我们的猜想。
明确了上面的问题之后,对CERT.RSA 文件进行详细解析,得到下图:
说明:
(1)首先,我们的通常所说的证书的签名,是生成证书的时候CA对整个证书的所有域签名的得到的,而不是对某一部分签名得到的。整个签名就是上图中部分一的最下面的一段十六进制的内容;
(2)编程中的获取到的内容实质上是就是上图中的部分二,这是一个证书的所有内容;
(3)部分一种的公钥等信息就是从部分二中得来的,可以直接在部分二中找到。
(4)可以猜测,部分一中的其他信息也是从部分二中得来,只不过编码方式不一样,所以显示不同而已。
-----------------------------------------------------------------------------------------------------------
可以通过: openssl pkcs7 -inform DER -in CERT.RSA -noout -print_certs -text 将 CERT.RSA证书中的内容答应出来。
Certificate: Data: Version: 3 (0x2) Serial Number: 1005375368 (0x3beccf88) Signature Algorithm: sha256WithRSAEncryption Issuer: C=US, O=Android, CN=Android Debug Validity Not Before: Dec 29 10:10:49 2014 GMT Not After : Dec 21 10:10:49 2044 GMT Subject: C=US, O=Android, CN=Android Debug Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:d8:b9:a8:66:5f:09:b0:04:86:3f:23:9e:c2:84: 93:a0:e3:dc:a2:5f:d3:ab:48:29:e9:e6:33:16:69: 56:87:4f:34:41:ce:be:3e:d0:a4:fa:33:27:e7:79: 7d:d3:0b:63:22:d3:27:f9:7b:f2:23:30:23:13:39: 24:4c:92:f2:15:60:35:ed:91:87:77:86:e7:f0:df: 2d:93:fd:59:26:86:93:4e:e5:13:e2:bf:55:5a:6a: e7:3d:ac:48:50:10:d4:96:71:2d:c7:f9:df:91:0b: b8:b0:0f:8d:b9:f2:8f:ec:65:33:c6:6c:a9:79:b1: da:74:d1:0f:cc:7a:c3:ed:d1:a2:85:42:86:ae:88: 2a:dd:3c:8b:b0:d5:e5:45:07:42:a0:96:65:b4:ac: 2f:98:7e:8a:36:2f:b8:0d:a7:1c:0f:38:f1:56:9c: 13:f6:3c:a0:2c:f7:a9:e8:cb:17:95:44:4f:dc:e2: ce:b7:9e:a3:f8:c2:fa:71:c0:f0:20:76:29:34:34: d9:e0:18:12:f9:ec:40:a9:2a:f1:39:cd:30:43:da: de:92:71:92:88:fd:28:0a:17:b7:cd:c8:d8:fb:45: ca:1c:61:bc:13:91:2e:01:b2:20:36:1b:18:b8:aa: e0:95:e0:ab:98:fa:9c:1b:6b:7f:bd:27:66:ab:5e: d0:9b Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: A5:02:31:28:2E:76:1B:10:11:68:AD:32:5C:8F:DC:00:30:80:BB:7C Signature Algorithm: sha256WithRSAEncryption ba:0d:dc:9a:f9:12:97:12:b0:7c:d6:07:e7:bd:27:32:46:60: 75:4b:e3:b4:3e:09:f7:90:53:cd:4d:f5:bf:61:3f:2a:2e:2f: d0:c8:50:c2:3b:29:62:10:25:7d:6b:53:48:88:e1:76:60:33: 52:45:ac:df:5f:49:95:ee:ac:75:81:14:46:2f:e0:26:30:be: d8:5d:2b:1c:a2:2f:30:34:c9:f4:9c:e4:c4:86:54:71:69:3d: de:2d:97:fe:dc:68:9f:13:d0:b9:86:e8:24:b9:75:04:cf:db: 1d:cf:d0:36:db:06:b9:87:37:3c:89:25:02:12:8b:fc:42:9e: 5e:18:69:25:5f:2a:52:ab:5a:a1:19:f6:3f:3b:9c:d3:5d:bd: ce:0b:5d:a0:f9:d6:0b:de:05:ff:89:24:b3:ad:9b:4e:5c:9f: 07:c2:b2:d9:55:44:49:e7:b7:cf:a2:ca:68:ae:64:0d:8a:56: 43:9b:de:ad:e5:d6:d5:86:76:ba:34:d4:90:e8:0a:ba:26:73: 8c:4b:7d:8c:92:02:bb:62:2c:9b:83:e8:d3:2f:74:01:72:89: b4:a2:08:12:c3:64:90:40:b2:8e:60:fd:db:6b:08:a7:db:32: 98:f7:7a:ce:c9:78:46:42:7d:09:31:6a:5f:b8:81:87:2f:62: 1e:e2:d3:7d
Java
这是最简单的
import sun.security.pkcs.PKCS7; import java.io.FileInputStream; import java.io.IOException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class Test { public static void main(String[] args) throws CertificateException, IOException { FileInputStream fis = new FileInputStream("/Users/wangchen/Desktop/CERT.RSA"); PKCS7 pkcs7 = new PKCS7(fis); X509Certificate publicKey = pkcs7.getCertificates()[0]; System.out.println("issuer1:" + publicKey.getIssuerDN()); System.out.println("subject2:" + publicKey.getSubjectDN()); System.out.println(publicKey.getPublicKey()); } }
使用openssl-dev 的C API
#include #include #include #include <string> #include using namespace std; string to_string(X509_NAME* name) { BIO* mem = BIO_new(BIO_s_mem()); if (mem == NULL) return NULL; if (X509_NAME_print_ex(mem, name, 0, XN_FLAG_RFC2253) < 0) return NULL; string str; char buf[128]; while((BIO_gets(mem, &buf[0], sizeof(buf))) > 0) { str.append(buf); } BIO_free(mem); return str; } int main() { FILE* fp; if (!(fp = fopen("CERT.RSA", "rb"))) { fprintf(stderr, "Error reading input pkcs7 file\n" ); exit(1); } /* todo: 这里可能有内存漏洞,有空查一下文档 */ PKCS7* pkcs7 = d2i_PKCS7_fp(fp, NULL); X509* cert = sk_X509_pop(pkcs7->d.sign->cert); string subject = to_string(X509_get_subject_name(cert)); string issuer = to_string(X509_get_issuer_name(cert)); char *modulus = BN_bn2dec(X509_get_pubkey(cert)->pkey.rsa->n); cout << subject << endl; OPENSSL_free(modulus); fclose(fp); return 0; }
分类: Android Pro
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/4482667.html如需转载请自行联系原作者
demoblog