https://blog.csdn.net/cowbin2012/article/details/100134114
首先要有一个CA根证书,然后用CA根证书来签发用户证书。用户进行证书申请:一般先生成一个私钥,然后用私钥生成证书请求(证书请求里应含有公钥信息),再利用证书服务器的CA根证书来签发证书。
生成CA私钥(.key)–>生成CA证书请求(.csr)–>自签名得到根证书(.crt)(CA给自已颁发的证书)。
# Generate CA private key (制作ca.key 私钥)
openssl genrsa -out ca.key 2048
# Generate CSR
openssl req -new -key ca.key -out ca.csr
#OpenSSL创建的自签名证书在chrome端无法信任,需要添加如下
echo "subjectAltName=DNS:rojao.test.com,IP:10.10.2.137" > cert_extensions
# Generate Self Signed certificate(CA 根证书)
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -extfile cert_extensions -out ca.crt
整个提示将如下所示:
OutputCountry Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Rojao, Inc.
Organizational Unit Name (eg, section) []:R&D Department
Common Name (e.g. server FQDN or YOUR name) []:Rojao CA ROOT
Email Address []:[email protected]
在实际的软件开发工作中,往往服务器就采用这种自签名的方式,因为毕竟找第三方签名机构是要给钱的,也是需要花时间的。
生成私钥(.key)–>生成证书请求(.csr)–>用CA根证书签名得到证书(.crt)
# private key
openssl genrsa -des3 -out server.key 2048
# generate csr
openssl req -new -key server.key -out server.csr
#OpenSSL创建的自签名证书在chrome端无法信任,需要添加如下
echo "subjectAltName=DNS:rojao.test.com,IP:10.10.2.137" > cert_extensions
# generate certificate
openssl ca -in server.csr -out server.crt -extfile cert_extensions -cert ca.crt -keyfile ca.key
最重要的一行是Common Name (e.g. server FQDN or YOUR name)那一行。您需要输入与服务器关联的域名,或者是您服务器的公共IP地址。
整个提示将如下所示:
OutputCountry Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Rojao, Inc.
Organizational Unit Name (eg, section) []:R&D Department
Common Name (e.g. server FQDN or YOUR name) []:10.10.2.137
Email Address []:[email protected]
假如报错:/etc/pki/CA/index.txt: No such file or directory
unable to open '/etc/pki/CA/index.txt
解决:创建/etc/pki/CA/index.txt文件
mkdir -p CA/newcerts
touch CA/index.txt
touch CA/serial
echo “01” > CA/serial
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key
报错:failed to update database
TXT_DB error number 2
删除之前创建的 index.txt serial 文件后重建
有时需要用到pem格式的证书,可以用以下方式合并证书文件(crt)和私钥文件(key)来生成
cat client.crt client.key> client.pem
cat server.crt server.key > server.pem
结果:
服务端证书:ca.crt, server.key, server.crt, server.pem
客户端证书:ca.crt, client.key, client.crt, client.pem
生成pfx(p12)格式 证书
openssl pkcs12 -export -in server.crt -out server.p12 -inkey server.key