SSL证书制作和认证原理
1. 简介
什么是 SSL, 什么是 TLS 呢?官话说 SSL 是安全套接层 (secure sockets layer), TLS 是 SSL 的继任者,叫传输层安全 (transport layer security)。说白点,就是在明文的上层和 TCP 层之间加上一层加密,这样就保证上层信息传输的安全。如HTTP 协议是明文传输,加上 SSL 层之后,就有了雅称 HTTPS。它存在的唯一目的就是保证上层通讯安全的一套机制。它的发展依次经历了下面几个时期,像手机软件升级一样,每次更新都添加或去除功能,比如引进新的加密算法,修改握手方式等。
2. 认证流程
参考地址:
- SSL/TLS 双向认证(一) – SSL/TLS 工作原理
3. 拓展
参考地址:
- 证书相关:rsa、crt文件、key文件、csr文件
- Java-JSSE-SSL/TLS编程代码实例-双向认证
3.1 OpenSSL命令生成证书
3.1.1 基本配置
vim /etc/ssl/openssl.cnf
mkdir certs
mkdir newcerts
openssl rand -out private/.rand 1000
openssl rand -writerand /root/.rnd
3.1.2 CA证书
openssl genrsa -aes256 -out private/cakey.pem 4096
openssl req -new -key private/cakey.pem -out private/ca.csr -subj "/C=CN/ST=ca/L=ca/O=ca/OU=ca/CN=ca"
openssl x509 -req -days 365 -sha1 -extensions v3_ca -signkey private/cakey.pem -in private/ca.csr -out certs/ca.cer
我这里的密码为:123456
3.1.3 二级CA
openssl genrsa -aes256 -out private/cakey-one.pem 4096
openssl req -new -key private/cakey-one.pem -out private/ca-one.csr -subj "/C=CN/ST=ca-one/L=ca-one/O=ca-one/OU=ca-one/CN=ca-one"
openssl x509 -req -days 365 -sha1 -extensions v3_ca -CA certs/ca.cer -CAkey private/cakey.pem -CAserial ca.srl -CAcreateserial -in private/ca-one.csr -out certs/cakey-one.cer
3.1.4 服务端证书
openssl genrsa -aes256 -out private/server-one.pem 1024
openssl req -new -key private/server-one.pem -out private/server-one.csr -subj "/C=CN/ST=server-one/L=server-one/O=server-one/OU=server-one/CN=server-one"
openssl x509 -req -days 365 -sha1 -extensions v3_ca -CA certs/cakey-one.cer -CAkey private/cakey-one.pem -CAserial ca.srl -CAcreateserial -in private/server-one.csr -out certs/server-one.cer
切换证书格式:
openssl pkcs12 -export -in certs/server-one.cer -inkey private/server-one.pem -out server-one.pkcs12
keytool -importkeystore -srckeystore server-one.pkcs12 -destkeystore server-one.keystore -srcstoretype pkcs12
3.1.5 客户端证书
keytool -importcert -alias ca-one -file certs/cakey-one.cer -keystore trust.keystore
keytool -importcert -alias ca -file certs/ca.cer -keystore trust.keystore
3.1.6 证书吊销列表
参考地址:
- openssl生成证书链多级证书、证书吊销列表(CRL)
- 证书介绍及openssl生成证书和吊销列表
3.2 KeyTool命令生成证书
参考地址:
- 通过keytool生成证书,并提取公钥和私钥
- The Most Common Java Keytool Keystore Commands (sslshopper.com)
- JAVA Keytool工具生成Keystore和Truststore文件
3.3 Openssl命令详解
通过openssl提取私钥
openssl pkcs12 -in server-one.pkcs12 -nocerts -nodes
openssl pkcs12 -in server-one.pkcs12 -nodes
openssl pkcs12 -in server-one.pkcs12 -nocerts -nodes -out private.key
通过openssl在pkcs12文件中提取密钥文件(密钥文件包含私钥,而私钥能够提取公钥):
openssl pkcs12 -in server-one.pkcs12 -nocerts -nodes -out test.key
3.4 Keytool命令详解
参考地址:
- keytool使用大全:p12(PKCS12)和jks互相转换等
列出证书:
keytool -list -v -keystore trust.keystore
通过keystore提取私钥:
keytool -list -rfc --keystore server-one.keystore | openssl x509 -inform pem -pubkey
|