上节我们介绍了RSA的诞生和密钥对的生成,其中生成的密钥均是以PEM编码展示和存储的,那么这些密钥文件究竟都包含了哪些内容、都是以什么格式进行存储的呢。本节将介绍ASN.1,PKCS, X.509, DER, PEM等概念以及他们之间的关系。然后对密钥文件或证书文件的后缀做一些简单的介绍。
In telecommunications and computer networking, Abstract Syntax Notation One (ASN.1) is a standard and flexible notation that describes data structures for representing, encoding, transmitting, and decoding data. It provides a set of formal rules for describing the structure of objects that are independent of machine-specific encoding techniques and is a precise, formal notation that removes ambiguities.
ASN.1抽象语法标记(Abstract Syntax Notation One) ASN.1是一种 ISO/ITU-T 标准,描述了一种对数据进行表示、编码、传输和解码的数据格式。它提供了一整套正规的格式用于描述对象的结构,而不管语言上如何执行及这些数据的具体指代,也不用去管到底是什么样的应用程序。
重点:ASN.1是一种语言,一种标记语言,作用是描述数据结构。基于这种数据结构可以进行数据的表示、编码、传输和解码。
PKCS协议组和X.509协议均采用ASN.1来定义密钥或证书的数据结构。
The Public-Key Cryptography Standards (PKCS)是由美国RSA数据安全公司及其合作伙伴制定的一组公钥密码学标准,其中包括证书申请、证书更新、证书作废表发布、扩展证书内容以及数字签名、数字信封的格式等方面的一系列相关协议。
到1999年底,PKCS已经公布了15个标准,其编号分别为PCKS#1~15。其中比较常用的有PKCS#1, PKCS#7, PKCS#8以及PKCS#12。
PKCS #1 : RSA Cryptography Standard
Defines the mathematical properties and format of RSA public and private keys, and the basic algorithms and encoding/padding schemes for performing RSA encryption, decryption, and producing and verifying signatures.PKCS #7 : Cryptographic Message Syntax Standard
A PKCS #7 file only contains certificates and chain certificates (Intermediate CAs), not the private key.The file name extension is usually .p7b, .p7c.The most common platforms that support P7B files are Microsoft Windows and Java TomcatPKCS #8 : Private-Key Information Syntax Standard
Used to carry private certificate keypairs (encrypted or unencrypted).PKCS #12 : Personal Information Exchange Syntax Standard
The PKCS#12 or PFX/P12 format is a binary format for storing the server certificate, intermediate certificates, and the private key in one encryptable file.These files usually have extensions such as .pfx and .p12.They are typically used on Windows machines to import and export certificates and private keys下面是一些语法描述:
PKCS#1: RSA Public Key Syntax
RSAPublicKey ::= SEQUENCE { modulus INTEGER, -- n publicExponent INTEGER -- e }PKCS#1: RSA Private Key Syntax
RSAPrivateKey ::= SEQUENCE { version Version, modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- p prime2 INTEGER, -- q exponent1 INTEGER, -- d mod (p-1) exponent2 INTEGER, -- d mod (q-1) coefficient INTEGER, -- (inverse of q) mod p otherPrimeInfos OtherPrimeInfos OPTIONAL }PKCS#8: Private Key Syntax
PrivateKeyInfo ::= SEQUENCE { version Version, algorithm AlgorithmIdentifier, PrivateKey BIT STRING } AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }PKCS#8: Encrypted Private Key Syntax
EncryptedPrivateKeyInfo ::= SEQUENCE { encryptionAlgorithm EncryptionAlgorithmIdentifier, encryptedData EncryptedData } EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier EncryptedData ::= OCTET STRINGX.509 是密码学里公钥证书的格式标准。 X.509 证书己应用在包括TLS/SSL(WWW万维网安全浏览的基石)在内的众多 Internet协议里。同时它也用在很多非在线应用场景里,比如电子签名服务。X.509证书里含有公钥、身份信息(比如网络主机名,组织的名称或个体名称等)和签名信息(可以是证书签发机构CA的签名,也可以是自签名)。对于一份经由可信的证书签发机构签名或者可以通过其它方式验证的证书,证书的拥有者就可以用证书及相应的私钥来创建安全的通信,对文档进行数字签名。
另外除了证书本身功能,X.509还附带了证书吊销列表和用于从最终对证书进行签名的证书签发机构直到最终可信点为止的证书合法性验证算法。
X.509是ITU-T标准化部门基于他们之前的ASN.1定义的一套证书标准。
重点:
X.509证书除了包含公钥信息外,还包含身份信息(比如网络主机名,组织的名称或个体名称等)和签名信息(可以是证书签发机构CA的签名,也可以是自签名)。X.509证书的格式也是根据ASN.1语法定义的。The X.509 v3 certificate basic syntax is as follows.
Certificate ::= SEQUENCE { tbsCertificate TBSCertificate, signatureAlgorithm AlgorithmIdentifier, signatureValue BIT STRING } TBSCertificate ::= SEQUENCE { version [0] EXPLICIT Version DEFAULT v1, serialNumber CertificateSerialNumber, signature AlgorithmIdentifier, issuer Name, validity Validity, subject Name, subjectPublicKeyInfo SubjectPublicKeyInfo, issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 extensions [3] EXPLICIT Extensions OPTIONAL -- If present, version MUST be v3 } Version ::= INTEGER { v1(0), v2(1), v3(2) } CertificateSerialNumber ::= INTEGER Validity ::= SEQUENCE { notBefore Time, notAfter Time } Time ::= CHOICE { utcTime UTCTime, generalTime GeneralizedTime } UniqueIdentifier ::= BIT STRING SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING } Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension Extension ::= SEQUENCE { extnID OBJECT IDENTIFIER, critical BOOLEAN DEFAULT FALSE, extnValue OCTET STRING -- contains the DER encoding of an ASN.1 value -- corresponding to the extension type identified -- by extnID }与ASN.1相关的编码规则包括
基本编码规则(BER, Basic Encoding Rules) -X.209规范编码规则(CER, Canonical Encoding Rules)识别名编码规则(DER, Distinguished Encoding Rules)压缩编码规则(PER, Packed Encoding Rules)XML编码规则(XER, XML Encoding Rules)其中BER、CER、DER、PER都属于二进制编码,相关密钥文件和证书文件一般采用的是DER编码;
Privacy-Enhanced Mail (PEM) is a de facto file format for storing and sending cryptographic keys, certificates, and other data, based on a set of 1993 IETF standards defining “privacy-enhanced mail.”
PEM是一个用来存储和发送密码学key、证书和其他数据的文件格式的事实标准。许多使用ASN.1的密码学标准(比如X.509和PKCS)都使用DER编码,而DER编码的内容是二进制的,不适合与邮件传输(早期Email不能发送附件),因此使用PEM把二进制内容转换成ASCII码。文件内容的格式像下面这样:
-----BEGIN label----- BASE64 Encoded -----END label-----PEM实际上就是把DER编码的文件的二进制内容用base64编码一下,然后加上-----BEGIN label-----这样的头和-----END label-----这样的尾,中间则是DER文件的Base64编码。
PKCS #1 RSA Private Key
-----BEGIN RSA PRIVATE KEY----- BASE64 Encoded -----END RSA PRIVATE KEY-----PKCS #1 RSA Public Key
-----BEGIN RSA PUBLIC KEY----- BASE64 Encoded -----END RSA PUBLIC KEY-----PKCS #8 Private Key
-----BEGIN PRIVATE KEY----- BASE64 Encoded -----END PRIVATE KEY-----PKCS #8 Encrypted Private Key
-----BEGIN ENCRYPTED PRIVATE KEY----- BASE64 Encoded -----END ENCRYPTED PRIVATE KEY-----X.509 Certificate
-----BEGIN CERTIFICATE----- BASE64 Encoded -----END CERTIFICATE-----X.509 Certificate Subject Public Key Info
-----BEGIN PUBLIC KEY----- BASE64 Encoded -----END PUBLIC KEY-----密钥、证书文件的后缀看起来比较杂乱,有些让人搞不清该用什么样的后缀合适。密钥、证书文件的后缀命名其实是有一定规律的,其大致可以分为以下几类:
按不同规范分,如 PKCS#7文件的后缀为 .p7b, .p7c PKCS#8文件的后缀为 .key PKCS#12文件的后缀为 .p12, pfx X.509文件后缀为.cer, .crt按编码方式分, 如 DER编码方式的文件其后缀为 .der,PKCS#12规范的文件是二进制编码,可以采用.der后缀 PEM编码方式的文件其后缀为 .pem,像满足PKCS#1, PKCS#7,PKCS#8, X.509规范的文件一般采用的pem编码方式因此对于同一文件可以采用多种后缀进行命名,如对于一个证书文件,如果采用的PEM编码,可以采用的后缀有 .cer,.crt,.pem。
X.509、PKCS文件格式介绍 X.509 PKCS PKCS #1: RSA Cryptography Specifications Version 2.2 Textual Encodings of PKIX, PKCS, and CMS Structures Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile