node
若是您不熟悉证书签名请求(CSRs), 先读第一节git
除了第一节, 这份文档以备忘纸条(cheat sheet)的格式编写, 包含对应的命令行算法
直接查看和问题相关的章节浏览器
大多数命令行为了浅显易读, 写成了多行的格式安全
若是须要从certificate authority(CA)获取一份SSL证书, 你须要先生成一份certificate signing request(CSR). 一个CSR包含的主要信息是key pair中的公钥, 以及一些附加的信息. 在被签名时这些信息都会被塞进证书.bash
不管何时, 建立一个CSR, 都会提示你输入一些证书相关的信息, 这些信息被称为Distinguished Name(DN). 在DN中最重要的字段就是Common Name (CN), 其内容应当与你将要使用此证书的主机的Fully Qualified Domain Name (FQDN)彻底一致. 在建立CSR时能够在命令行中传递信息来跳过交互填写的步骤.服务器
在DN中提供的其余字段是关于你的组织或业务的信息, 若是你从一个certificate authority购买了一份SSL证书, 这些字段例如"Organization"须要和你的机构一致.dom
下面是一个CSR的信息填写交互的例子工具
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:New York Locality Name (eg, city) []:Brooklyn Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Brooklyn Company Organizational Unit Name (eg, section) []:Technology Division Common Name (e.g. server FQDN or YOUR name) []:examplebrooklyn.com Email Address []:
若是你打算避免交互式的填写, 你能够使用 -subj
参数来输入这些信息, 下面是一个例子, 输入的信息和前面交互式的是同样的ui
-subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=examplebrooklyn.com"
如今你应该了解CSR的含义了, 能够根据须要查看下面的其余章节
这一节介绍如何使用生成CSR相关的OpenSSL命令(以及生成私钥, 若是尚未私钥的话). CSR能够用来从certificate authority请求SSL证书.
记住你能够用-subj
参数来避免交互式填写CSR信息.
若是你打算用HTTPS来加强你的Apache HTTP或Nginx服务器, 而且用一个Certificate Authority(CA)来签发SSL证书, 你须要使用这个. 生成一份CSR而后发送给CA要求其签发一份CA-signed SSL证书. 若是你的CA支持SHA-2, 在参数中添加-sha256
来生成SHA-2签名的CSR.
下面的命令用于生成一份2048-bit的私钥(domain.key), 和一份CSR(domain.csr)
openssl req \ -newkey rsa:2048 -nodes -keyout domain.key \ -out domain.csr
在弹出交互中填写相关信息
-newkey rsa:2048
参数用于指定私钥长度为2048bit, 而且使用RSA算法. -nodes
参数指定私钥不须要密码加密. -new
参数, 这里没使用可是实际上包含了, 用于指定生成CSR.
若是手里已经有一份私钥了, 要用这个私钥来从CA那里请求证书, 能够用这个方法.
下面的命令用于根据存在的私钥(domain.key)来生成新的CSR(domain.csr)
openssl req \ -key domain.key \ -new -out domain.csr
在弹出交互中填写相关信息
-key
参数用于指定现有的用于生成CSR的私钥文件, -new
参数用于指定生成CSR.
若是你手里有私钥, 想续签一个证书, 可是你的CA却找不到原先的CSR了, 这个方法能够省掉你从新输入CSR信息的时间, 会从当前的证书中提取相应的信息.
下面的命令用于根据存在的私钥(domain.key)和证书(domain.crt)生成一个新的CSR(domain.csr)
openssl x509 \ -in domain.crt \ -signkey domain.key \ -x509toreq -out domain.csr
-x509toreq
参数用于说明使用的是X509证书来生成CSR
若是你须要用SSL证书来增强服务安全性, 可是不想经过CA来签发证书, 一个可行而且免费的方案就是本身签发证书.
能够本身签发的证书就称为自签名证书. 自签名证书表示这个证书是使用本身的私钥来签名的. 自签名证书也能够像CA签发的证书同样, 用来加密数据, 可是你的用户将会看到一个警告, 提示他们这个证书并非他们的操做系统或浏览器信任的. 所以自签发证书仅适用于你不须要跟用户证实本身身份真实性的场合.
这一节介绍自签名证书相关的OpenSSL命令.
若是你须要用HTTPS来增强Apache HTTP或Nginx的服务安全性, 可是不想经过CA来签发证书, 能够使用这个方法.
下面的命令建立了一个2048bit的私钥(domain.key)和一个自签名证书(domain.crt)
openssl req \ -newkey rsa:2048 -nodes -keyout domain.key \ -x509 -days 365 -out domain.crt
在弹出交互中填写相关信息
-x509
参数用于告诉req要建立一个自签名证书. -days 365
参数指定有效期为365天. 一个临时的CSR会同时被建立, 用于收集关联的证书相关的信息.
若是你手里有私钥, 想用这个私钥来签发一个自签名证书.
下面的命令用现有的私钥(domain.key)生成一个自签名证书(domain.crt)
openssl req \ -key domain.key \ -new \ -x509 -days 365 -out domain.crt
在弹出交互中填写相关CSR信息
-x509
参数告诉req要建立一个自签名证书, -days 365
参数指定有效期为365天. -new
参数用于开启CSR信息填写
若是你手里有私钥和CSR, 想经过它们生成一个自签名证书.
下面的命令用于根据现有的私钥(domain.key)和CSR(domain.csr)生成一个新的自签名证书(domain.crt)
openssl x509 \ -signkey domain.key \ -in domain.csr \ -req -days 365 -out domain.crt
-days 365
参数用于指定有效期为365天.
证书和CSR编码是PEM格式, 不是文本, 要经过命令进行查看. 这一节介绍如何使用OpenSSL命令查看PEM编码格式的文件内容.
下面的命令用于明文展现CSR(domain.csr)的内容
openssl req -text -noout -verify -in domain.csr
下面的命令用于明文展现证书(domain.crt)的内容
openssl x509 -text -noout -in domain.crt
下面的命令用于校验使用CA证书(ca.crt)签发的证书(domain.crt)
openssl verify -verbose -CAFile ca.crt domain.crt
这一节介绍建立和校验私钥的OpenSSL命令
使用下面的命令建立一个有密码保护的2048bit私钥(domain.key)
openssl genrsa -des3 -out domain.key 2048
过程当中须要输入密码
使用下面的命令检查一个私钥(domain.key)是否有效
openssl rsa -check -in domain.key
若是私钥是被加密的, 会弹出提示让输入密码, 若是密码正确, 界面会展现解密后的私钥.
使用下面的命令检查私钥(domain.key)是否与证书(domain.crt)和CSR(domain.csr)匹配
openssl rsa -noout -modulus -in domain.key | openssl md5 openssl x509 -noout -modulus -in domain.crt | openssl md5 openssl req -noout -modulus -in domain.csr | openssl md5
若是上面命令的输出是相同的, 说明这三个文件相关的可能性很是高
下面的命令使用一个未加密的私钥(unencrypted.key)生成一个加密的私钥(encrypted.key)
openssl rsa -des3 \ -in unencrypted.key \ -out encrypted.key
过程当中须要输入密码
下面的命令使用加密的私钥(encrypted.key)输出解密的私钥(decrypted.key)
openssl rsa \ -in encrypted.key \ -out decrypted.key
过程当中须要输入密码
以上生成的证书都是ASCII PEM编码的X.509证书. 实际使用中存在多种编码格式和容器类型的证书, 各类应用有各自使用的证书类型, 而不少证书会在同一个文件里包含多种类型的信息例如私钥, 证书, CA证书.
OpenSSL能够用于在多种证书格式之间进行转换. 这一节介绍一些经常使用的转换.
下面的命令用于将PEM编码的证书(domain.crt)转换为DER编码的二进制格式证书(domain.der)
openssl x509 \ -in domain.crt \ -outform der -out domain.der
DER格式主要用于Java
下面的命令用于将DER编码的证书(domain.der)转换为PEM编码证书(domain.crt)
openssl x509 \ -inform der -in domain.der \ -out domain.crt
下面的命令用于将PEM编码的证书(domain.crt and ca-chain.crt) 添加到PKCS7文件(domain.p7b):
openssl crl2pkcs7 -nocrl \ -certfile domain.crt \ -certfile ca-chain.crt \ -out domain.p7b
注意你能够使用一个或多个-certfile
参数指定须要添加到PKS7文件的证书.
PKCS7文件, 也称P7B, 常使用于Java Keystores和Microsoft IIS (Windows). 是一种ASCII格式的文件, 能够包含证书和CA证书.
Use this command if you want to convert a PKCS7 file (domain.p7b) to a PEM file:
openssl pkcs7 \ -in domain.p7b \ -print_certs -out domain.crt
Note that if your PKCS7 file has multiple items in it (e.g. a certificate and a CA intermediate certificate), the PEM file that is created will contain all of the items in it.
Use this command if you want to take a private key (domain.key) and a certificate (domain.crt), and combine them into a PKCS12 file (domain.pfx):
openssl pkcs12 \ -inkey domain.key \ -in domain.crt \ -export -out domain.pfx
You will be prompted for export passwords, which you may leave blank. Note that you may add a chain of certificates to the PKCS12 file by concatenating the certificates together in a single PEM file (domain.crt) in this case.
PKCS12 files, also known as PFX files, are typically used for importing and exporting certificate chains in Micrsoft IIS (Windows).
Use this command if you want to convert a PKCS12 file (domain.pfx) and convert it to PEM format (domain.combined.crt):
openssl pkcs12 \ -in domain.pfx \ -nodes -out domain.combined.crt
Note that if your PKCS12 file has multiple items in it (e.g. a certificate and private key), the PEM file that is created will contain all of the items in it.
openssl version
命令能够用于检查当前使用的OpenSSL版本, 以及编译时使用的参数. 这些会影响到实际能使用的功能.
下面的命令用于展现版本和编译参数信息
openssl version -a
上面的文档使用的OpenSSL参数为:
OpenSSL 1.0.1f 6 Jan 2014 built on: Mon Apr 7 21:22:23 UTC 2014 platform: debian-amd64 options: bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) compiler: cc -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -DTERMIO -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DMD32_REG_T=int -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM OPENSSLDIR: "/usr/lib/ssl"
以上的内容基本覆盖了大部分使用OpenSSL处理SSL证书的功能, 除此之外, OpenSSL还有不少其余的功能未介绍.