openssl命令学习

如今,2,048 位 RSA 密钥被认为是安全的,或者 256 位 ECDSA 密钥被认为是安全的。

使用RSA生成私钥:

openssl genpkey -out fd.key \
-algorithm RSA \
-pkeyopt rsa_keygen_bits:2048 \
-aes-128-cbc

# 若不带 \ -aes-128-cbc 则不需要输入密码

使用 ECDSA 生成私钥:

openssl genpkey -out fd.key \
-algorithm EC  \
-pkeyopt ec_paramgen_curve:P-256 \
-aes-128-cbc

生成公钥:

openssl pkey -in fd.key -pubout -out fd-public.key

查看密钥:

cat fd.key
openssl pkey -in fd.key -text -noout

请求生成CSR(证书签名请求)*

openssl req -new -key fd.key -out fd.csr

交互式命令,会要求输入很多东西,一般只用填Common Name,即网站的域名或地址,比如:localhost,其他东西不想填可以输入 .

通过配置文件生成CSR,免交互:

openssl req -new -config fd.cnf -key fd.key -out fd.csr

sudo vim fd.cnf

[req]
prompt = no
distinguished_name = dn
req_extensions = ext
input_password = PASSPHRASE

[dn]
CN = localhost
emailAddress = webmaster@feistyduck.com
O = Feisty Duck Ltd
L = London
C = GB

[ext]
subjectAltName = DNS:localhost,DNS:192.168.10.224

从现有证书创建CSR:

openssl x509 -x509toreq -in fd.crt -out fd.csr -signkey fd.key

检测CSR是否正确:

openssl req -text -in fd.csr -noout

生成CRT证书:

openssl x509 -req -days 365 -in fd.csr -signkey fd.key -out fd.crt

直接通过私钥生成CRT证书:

openssl req -new -x509 -days 365 -key fd.key -out fd.crt

交互式命令,需要输入一些东西。

命令行生成CRT证书:

 openssl req -new -x509 -days 365 -key fd.key -out fd.crt \
 -subj "/C=GB/L=London/O=Feisty Duck Ltd/CN=localhost"

创建对多个主机名生效的证书:

openssl x509 -req -days 365 \
-in fd.csr -signkey fd.key -out fd.crt \
-extfile fd.ext

sudo vim fd.ext

subjectAltName = DNS:localhost, DNS:*.feistyduck.com, DNS:feistyduck.com

查看CRT证书:

openssl x509 -text -in fd.crt -noout

测试ssl链接:

openssl s_client -crlf \
-connect localhost:443 \
-servername localhost

参考文档

https://www.feistyduck.com/library/openssl-cookbook/online/

https://www.openssl.org/docs/man3.0/man7/crypto.html
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容