openssl是个可以管理SSL/TLS证书的开源工具。openssl工具默认输出的证书格式为PEM,所以在apache,postfix等系统中可以直接,方便的使用。
openssl安装目录的结构:
/usr/share/ssl
-> drwx------ 3 root root 4096 Apr 29 23:58 CA
-> lrwxrwxrwx 1 root root 19 Apr 29 23:58 cert.pem -> certs/ca-bundle.crt
-> drwxr-xr-x 2 root root 4096 Apr 30 00:13 certs
-> drwxr-xr-x 2 root root 4096 Aug 13 2006 lib
-> drwxr-xr-x 3 root root 4096 Jul 19 01:29
misc 脚本工具
-> -rw-r--r-- 1 root root 7569 Aug 13 2006
openssl.cnf openssl配置文件
-> drwxr-xr-x 2 root root 4096 Apr 30 00:12 private
在openssl的misc目录下,可以使用CA(bash脚本)来创建根证书,证书,私钥,是个非常方便的工具。如果创建根证书和私钥,那么默认会在misc目录下创建一个demoCA的目录存放信息。
/usr/share/ssl/misc
-> -rwxr-xr-x 1 root root 3505 Aug 13 2006 CA
-> -rwxr-xr-x 1 root root 119 Aug 13 2006 c_hash
-> -rwxr-xr-x 1 root root 152 Aug 13 2006 c_info
-> -rwxr-xr-x 1 root root 113 Aug 13 2006 c_issuer
-> -rwxr-xr-x 1 root root 110 Aug 13 2006 c_name
-> drwxr-xr-x 6 root root 4096 Jul 19 01:30
demoCA ./CA -newca创建
(创建的目录位置也会根据不同的openssl版本有所不同)
./CA -newca将会建立作为certificate authority所必须的私钥,根证书以及一系列文件。
CA还可以建立私钥和证书签名请求等文件,但是由于私钥默认是需要passphrase来保护的,而我们在web系统和mail系统使用过程中,不希望因为passphrase而造成无法使用,就可以使用openssl来建立这些文件。
Note:其实CA脚本也是使用openssl来完成的。
openssl使用方法:
openssl genrsa -des3 -out privkey.pem 1024
genrsa: 生成密钥对的算法
-des3: passphrase算法
-out: 私钥文件
1024为rsa的密钥位数
openssl req -newkey rsa:1024 -keyout privkey.pem [-out privreq.pem]
openssl req -new -nodes -keyout mailkey.pem
-out mailreq.pem -days 365
req: 创建csr
-new: 用于创建csr
-newkey: 创建csr和私钥
-nodes: 无需passphrase
-keyout: 生成私钥文件
-out: 指定csr文件
-days: 证书有效期
openssl req -new -key mailkey.pem -out mailreq.pem
此命令与上个命令不同之处在与mailkey.pem这个私钥文件必须存在
openssl req -x509 -newkey rsa:1024 -keyout key.pem -out req.pem
生成自签名的证书,也就是根证书,并且生成key.pem私钥文件,req.pem为根证书。
openssl ca -out mail_signed_cert.pem -infiles mailreq.pem
此命令会利用demoCA目录下的私钥和公钥对mailreq.pem请求文件进行签署,生成证书,所以,如果这些文件不存在,那么这条命令将不成功。
openssl x509 -req -in csrfile -out cert -CA ca.crt -CAkey ca.key -CAcreateserial -days 365
使用指定的CA私钥和CA根证书,对csrfile进行签署,生成cert证书文件。
openssl rsa|req|x509|pkcs12 [-text] [-noout] [-fingerprint] -in <rsa file|req file|x509 file|pkcs12 file>
查看相应的文件内容
openssl pkcs12 -in cert.pem [-inkey privkey.pem] [-certfile ca.pem] -out cert.p12 -export -name "My Certificate"
-export: 表示创建pkcs12文件,而不是简单地解读一个pkcs12文件
-inkey: 私钥文件,可选
-certfile: 其他的证书文件,可选
-name: 这个名称只作显示用,导入此文件的软件会显示这个名字