引用自Wiki:html
ASN.1 is a standard interface description language for defining data structures that can be serialized and deserialized in a cross-platform way.
也就是说ASN.1是一种用来定义数据结构的接口描述语言,它不是二进制,也不是文件格式,看下面的例子你就会明白了:git
FooQuestion ::= SEQUENCE { trackingNumber INTEGER, question IA5String }
这段代码定义了FooQuestion的数据结构,下面是FooQuestion这个数据接口的某个具体的数据:web
myQuestion FooQuestion ::= SEQUENCE { trackingNumber 5, question "Anybody there?" }
ASN.1用在不少地方好比下面要讲的X.509和PKCS group of cryptography standards。安全
引用自Wiki:bash
ASN.1 is closely associated with a set of encoding rules that specify how to represent a data structure as a series of bytes
意思是ASN.1有一套关联的编码规则,这些编码规则用来规定如何用二进制来表示数据结构,DER是其中一种。数据结构
把上面的FooQuestion的例子用DER编码则是(16进制):ide
30 13 02 01 05 16 0e 41 6e 79 62 6f 64 79 20 74 68 65 72 65 3f
翻译过来就是:编码
30 — type tag indicating SEQUENCE 13 — length in octets of value that follows 02 — type tag indicating INTEGER 01 — length in octets of value that follows 05 — value (5) 16 — type tag indicating IA5String (IA5 means the full 7-bit ISO 646 set, including variants, but is generally US-ASCII) 0e — length in octets of value that follows 41 6e 79 62 6f 64 79 20 74 68 65 72 65 3f — value ("Anybody there?")
看到这里你应该对DER编码格式有一个比较好的认识了。加密
引用自Wiki:spa
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----- BASE64Encoded -----END label-----
label用来区份内容究竟是什么类型,下面会讲。
和PEM相关的RFC有不少,与本文内容相关的则是RFC7468,这里面规定了不少label,不过要注意不是全部label都会有对应的RFC或Specification,这些label只是一种约定俗成。
PEM实际上就是把DER编码的文件的二进制内容用base64编码一下,而后加上-----BEGIN label-----
这样的头和-----END label-----
这样的尾,中间则是DER文件的Base64编码。
咱们能够经过下面的方法验证这个结论,先生成一个RSA Private Key,编码格式是PEM格式:
openssl genrsa -out key.pem
查看一下文件内容,能够看到label是RSA PRIVATE KEY
:
-----BEGIN RSA PRIVATE KEY----- BASE64Encoded -----END RSA PRIVATE KEY-----
而后咱们把PEM格式转换成DER格式:
openssl rsa -in key.pem -outform der -out key.der
若是你这个时候看一下文件内容会发现都是二进制。而后咱们把DER文件的内容Base64一下,会看到内容和PEM文件同样(忽略头尾和换行):
base64 -i key.der -o key.der.base64
上面讲到的PEM是对证书、密码学Key文件的一种编码方式,下面举例这些证书、密码学Key文件格式:
引用自Wiki :
In cryptography, X.509 is a standard defining the format of public key certificates. X.509 certificates are used in many Internet protocols, including TLS/SSL, which is the basis for HTTPS, the secure protocol for browsing the web.
X.509是一个Public Key Certificates的格式标准,TLS/SSL使用它,TLS/SSL是HTTPS的基础因此HTTPS也使用它。而所谓Public Key Certificates又被称为Digital Certificate 或 Identity Certificate。
An X.509 certificate contains a public key and an identity (a hostname, or an organization, or an individual), and is either signed by a certificate authority or self-signed.
一个X.509 Certificate包含一个Public Key和一个身份信息,它要么是被CA签发的要么是自签发的。
下面这种张图就是一个X.509 Certificate:
事实上X.509 Certificate这个名词一般指代的是IETF的PKIX Certificate和CRL Profile,见RFC5280。因此当你看到PKIX Certificate字样的时候能够认为就是X.509 Certificate。
引用自Wiki:
In cryptography, PKCS stands for "Public Key Cryptography Standards"
前面提到的X.509是定义Public Key Certificates的格式的标准,看上去和PKCS有点像,但实际上不一样,PKCS是Public Key密码学标准。此外Public-Key Cryptography虽然名字看上去只涉及Public Key,实际上也涉及Priviate Key,所以PKCS也涉及Private Key。
PKCS一共有15个标准编号从1到15,这里只挑讲PKCS #一、PKCS #八、PKCS #12。
PKCS #1,RSA Cryptography Standard,定义了RSA Public Key和Private Key数学属性和格式,详见RFC8017。
PKCS #8,Private-Key Information Syntax Standard,用于加密或非加密地存储Private Certificate Keypairs(不限于RSA),详见RFC5858。
PKCS #12定义了一般用来存储Private Keys和Public Key Certificates(例如前面提到的X.509)的文件格式,使用基于密码的对称密钥进行保护。注意上述Private Keys和Public Key Certificates是复数形式,这意味着PKCS #12文件其实是一个Keystore,PKCS #12文件能够被用作Java Key Store(JKS),详见RFC7292。
若是你用本身的CA所签发了一个证书,运行下列命令能够生成PKCS #12 keystore:
openssl pkcs12 -export \ -in <cert> \ -inkey <private-key> \ -name my-cert \ -caname my-ca-root \ -CAfile <ca-cert> \ -chain -out <pkcs-file>
PKCS #12通常不导出PEM编码格式。
当你不知道你的PEM文件内容是什么格式的能够根据下面查询。
RFC7468 - Textual Encoding of Certificates
-----BEGIN CERTIFICATE----- BASE64Encoded -----END CERTIFICATE-----
RFC7468 - Textual Encoding of Subject Public Key Info
-----BEGIN PUBLIC KEY----- BASE64Encoded -----END PUBLIC KEY-----
没有RFC或权威Specification,该格式有时候被称为traditional format、SSLeay format(见SO)
-----BEGIN RSA PRIVATE KEY----- BASE64Encoded -----END RSA PRIVATE KEY-----
同上没有RFC或权威Specification
-----BEGIN RSA PUBLIC KEY----- BASE64Encoded -----END RSA PUBLIC KEY-----
RFC7468 - One Asymmetric Key and the Textual Encoding of PKCS #8 Private Key Info
-----BEGIN PRIVATE KEY----- BASE64Encoded -----END PRIVATE KEY-----
RFC7468 - Textual Encoding of PKCS #8 Encrypted Private Key Info
-----BEGIN ENCRYPTED PRIVATE KEY----- BASE64Encoded -----END ENCRYPTED PRIVATE KEY-----
生成PKCS #1格式的RSA Private Key
openssl genrsa -out private-key.p1.pem 2048
PKCS #1 -> Unencrypted PKCS #8
openssl pkcs8 -topk8 -in private-key.p1.pem -out private-key.p8.pem -nocrypt
PKCS #1 -> Encrypted PKCS #8
openssl pkcs8 -topk8 -in private-key.p1.pem -out private-key.p8.pem
过程当中会让你输入密码,你至少得输入4位,因此PKCS #8相比PKCS #1更安全。
PKCS #8 -> PKCS #1
openssl rsa -in private-key.p8.pem -out private-key.p1.pem
若是这个PKCS #8是加密的,那么你得输入密码。
提取指的是从Private Key中提取Public Key,openssl rsa
同时支持PKCS #1和PKCS #8的RSA Private Key,惟一的区别是若是PKCS #8是加密的,会要求你输入密码。
提取X.509格式RSA Public Key
openssl rsa -in private-key.pem -pubout -out public-key.x509.pem
提取PKCS #1格式RSA Public Key
openssl rsa -in private-key.pem -out public-key.p1.pem -RSAPublicKey_out
openssl x509 -in cert.pem -pubkey -noout > public-key.x509.pem
X.509 RSA Public Key -> PKCS #1 RSA Public Key
openssl rsa -pubin -in public-key.x509.pem -RSAPublicKey_out -out public-key.p1.pem
PKCS #1 RSA Public Key -> X.509 RSA Public Key
openssl rsa -RSAPublicKey_in -in public-key.p1.pem -pubout -out public-key.x509.pem