OpenSSL 证书命令速查合集

六月 20, 2024 [网络与安全] #Linux #obsidian #密码学 #合集 #网络

OpenSSL 命令速查手册:证书、私钥与证书签名请求文件

OpenSSL是一个功能极其强大的命令行工具,可以用来完成公钥体系(PublicKeyInfrastructure) 及HTTPS相关的很多任务。这个速查手册整理了常用的OpenSSL命令的使用方法,例如生成私钥、 生成证书签名请求以及证书格式转换等。

序:关于证书签名请求(CSR)

如果你要从证书颁发机构(CA)获取一个SSL证书,那首先需要先生成一个证书签名请求(CSR)。 CSR的主要内容是密钥对中的公钥,以及一些额外的信息 —— 这些内容都将在签名时插入到 证书里。

当使用openssl生成证书签名请求时,需要输入证书的唯一标识信息(Distinguished _N__ame), 其中重要的一项是常见名(CommonName),它应当是你要部署证书的主机的 域名全称(FQDN)。

DN中的其他条目用来提供关于你的机构的额外信息。如果你在从证书颁发机构购买SSL证书, 那么通常也需要这些额外的字段,例如组织机构(Organization),以便能够真实地展示 你的机构详情。

下面是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 []:

也可以非交互方式提供生成CSR时要求的信息,任何需要CSR信息的OpenSSL命令都可以添加 -subj选项。例如:

If you want to non-interactively answer the CSR information prompt, you can do so by adding the -subj option to any OpenSSL commands that request CSR information. Here is an example of the option, using the same information displayed in the code block above:

-subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=examplebrooklyn.com"

一、生成证书签名请求

这一部分涵盖与生成CSR(以及私钥)相关的OpenSSL命令。CSR可以用来从证书颁发机构 请求SSL证书。

记住,你可以交互式的添加CSR信息,也可以使用-subj选项以非交互的方式添加同样的信息。

1.1 生成私钥和CSR

如果你需要使用HTTPS来加固你的web服务器,那么你会向证书颁发机构申请一个证书。这里 生成的CSR可以发送给CA来发行其签名的SSL证书。

下面的命令创建一个2048位的私钥(domain.key)以及一个CSR(domain.csr):

openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -out domain.csr

这里需要交互地输入CSR信息,以便完成整个过程。

-newkey rsa:2048选项声明了使用RAS算法生成2048位的私钥。-nodes选项表明我们 不使用密码加密私钥。上面隐含了-new选项,表示要生成一个CSR。

1.2 使用已有私钥生成CSR

如果你已经有了一个私钥,那么可以直接用它来向CA申请证书。

下面的命令使用一个已有的私钥(domain.key)创建一个新的CSR(domain.csr):

openssl req \
       -key domain.key \
       -new -out domain.csr

-key选项用来指定已有的私钥文件,-new选项表明我们要生成一个CSR。

1.3 使用已有的证书和私钥生成CSR

如果你需要续订已有的证书,但你和CA都没有原始的CSR,那可以再次生成CSR。 例如,下面的命令使用已有的证书(domain.crt)和私钥(domain.key)创建 一个新的CSR:

openssl x509 \
       -in domain.crt \
       -signkey domain.key \
       -x509toreq -out domain.csr

-x509toreq选项表明我们要使用X509证书来制作CSR。

二、生成SSL证书

如果你只是想用SSL证书加固你的web服务器,但是并不需要CA签名的证书,那么 一个简单的方法是自己签发证书。

一种常见的你可以签发的类型是自签名证书 —— 使用自己的私钥签发的证书。 自签名证书可以向CA签发的证书一样用于加密数据,但是你的用户将收到提示 说明该证书不被其计算机或浏览器信息。因此,自签名证书只能在不需要 向用户证明你的身份时使用,例如非生产环境或者非公开服务。

这一部分的内容涵盖自签名证书生成相关的OpenSSL命令。

2.1 生成自签名证书

如果你需要使用HTTPS加固服务器,但不需要CA签发的证书,就可以使用 自签名证书。

下面的命令创建一个2048位的私钥(domain.key)以及一个自签名证书(domain.crt):

openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -x509 -days 365 -out domain.crt

-x509选项指出我们要创建自签名证书,-days 365选项声明该证书的有效期为365天。 在上面的命令执行过程中将创建一个临时CSR来收集与证书相关的CSR信息。

2.2 使用已有私钥生成自签名证书

也可以使用已有私钥来生成自签名证书。例如,下面的命令使用已有的私钥(domain.key) 生成一个自签名证书(domain.crt):

openssl req \
       -key domain.key \
       -new \
       -x509 -days 365 -out domain.crt

-new选项用来启动CSR信息采集提示。

2.3 使用已有的私钥和CSR生成自签名证书

第三种办法是使用已有的私钥和CSR来生成自签名证书。例如,下面的命令使用私钥(domain.key) 和CSR(domain.csr)创建一个自签名证书(domain:crt):

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

三、查看证书

证书和CSR文件都采用PEM编码格式,并不适合人类阅读。这一部分主要介绍 OpenSSL中查看PEM编码文件的命令。

3.1 查看CSR条目

下面的命令可以查看CSR文件的明文文本并进行验证:

openssl req -text -noout -verify -in domain.csr

3.2 查看证书条目

下面的命令可以查看证书文件的明文文本:

openssl x509 -text -noout -in domain.crt

3.3 验证证书是否由CA签发

下面的命令用来验证证书doman.crt是否由证书颁发机构(ca.crt)签发:

openssl verify -verbose -CAFile ca.crt domain.crt

四、私钥生成与验证

这部分介绍与私钥生成和验证相关的OpenSSL命令。

4.1 创建私钥

下面的命令创建一个密码保护的2048位私钥domain.key:

openssl genrsa -des3 -out domain.key 2048

上面命令会提示输入密码。

4.2 验证私钥

下面的命令可以验证私钥domain.key是否有效:

openssl rsa -check -in domain.key

如果私钥是加密的,命令会提示输入密码,验证密码成功则会 显示不加密的私钥。

4.3 验证私钥与证书和CSR匹配

使用下面的命令验证私钥domain.key是否与证书domain.crt以及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

如果上面三个命令的输出一致,那么有极高的概率可以认为私钥、证书和 CSR是相关的。

4.4 加密私钥

下面的命令将私钥unencrypted.key加密,输出加密后的私钥encrypted.key:

openssl rsa -des3 \
       -in unencrypted.key \
       -out encrypted.key

上面命令执行时会提示设置密码。

4.5 解密私钥

下面的命令将加密私钥encrypted.key解密,并输出明文结果:

openssl rsa \
       -in encrypted.key \
       -out decrypted.key

上面命令执行时会提示输入解密密码。

五、证书格式转换

我们之前接触的证书都是X.509格式,采用ASCII的PEM编码。还有其他 一些证书编码格式与容器类型。OpenSSL可以用来在众多不同类型之间 转换证书。这一部分主要介绍与证书格式转换相关的OpenSSL命令。

5.1 PEM转DER

可以将PEM编码的证书domain.crt转换为二进制DER编码的证书domain.der:

openssl x509 \
       -in domain.crt \
       -outform der -out domain.der

DER格式通常用于Java。

5.2 DER转PEM

同样,可以将DER编码的证书(domain.der)转换为PEM编码(domain.crt):

openssl x509 \
       -inform der -in domain.der \
       -out domain.crt

5.3 PEM转PKCS7

可以将PEM证书(domain.crt和ca-chain.crt)添加到一个PKCS7(domain.p7b) 文件中:

openssl crl2pkcs7 -nocrl \
       -certfile domain.crt \
       -certfile ca-chain.crt \
       -out domain.p7b

使用-certfile选项指定要添加到PKCS7中的证书。

PKCS7文件也被称为P7B,通常用于Java的Keystore和微软的IIS中保存证书的ASCII文件。

5.4 PKCS7转换为PEM

使用下面的命令将PKCS7文件(domain.p7b)转换为PEM文件:

openssl pkcs7 \
       -in domain.p7b \
       -print_certs -out domain.crt

如果PKCS7文件中包含多个证书,例如一个普通证书和一个中间CA证书,那么输出的 PEM文件中将包含所有的证书。

5.5 PEM转换为PKCS12

可以将私钥文件(domain.key)和证书文件(domain.crt)组合起来生成PKCS12 文件(domain.pfx):

openssl pkcs12 \
       -inkey domain.key \
       -in domain.crt \
       -export -out domain.pfx

上面的命令将提示你输入导出密码,可以留空不填。

PKCS12文件也被称为PFX文件,通常用于导入/导出微软IIS中的证书链。

5.6 PKCS12转换为PEM

也可以将PKCS12文件(domain.pfx)转换为PEM格式(domain.combined.crt):

openssl pkcs12 \
       -in domain.pfx \
       -nodes -out domain.combined.crt

注意如果PKCS12文件中包含多个条目,例如证书及其私钥,那么生成的PEM 文件中将包含所有条目。

The Most Common OpenSSL Commands

One of the most versatile SSL tools is OpenSSL which is an open source implementation of the SSL protocol. There are versions of OpenSSL for nearly every platform, including Windows, Linux, and Mac OS X. OpenSSL is commonly used to create the CSR and private key for many different platforms, including Apache. However, it also has hundreds of different functions that allow you to view the details of a CSR or certificate, compare an MD5 hash of the certificate and private key (to make sure they match), verify that a certificate is installed properly on any website, and convert the certificate to a different format. A compiled version of OpenSSL for Windows can be found here.

If you don't want to bother with OpenSSL, you can do many of the same things with our SSL Certificate Tools. Below, we have listed the most common OpenSSL commands and their usage:

General OpenSSL Commands

These commands allow you to generate CSRs, Certificates, Private Keys and do other miscellaneous tasks.

Checking Using OpenSSL

If you need to check the information within a Certificate, CSR or Private Key, use these commands. You can also check CSRs and check certificates using our online tools.

Debugging Using OpenSSL

If you are receiving an error that the private doesn't match the certificate or that a certificate that you installed to a site is not trusted, try one of these commands. If you are trying to verify that an SSL certificate is installed correctly, be sure to check out the SSL Checker.

Converting Using OpenSSL

These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software. For example, you can convert a normal PEM file that would work with Apache to a PFX (PKCS#12) file and use it with Tomcat or IIS. Use our SSL Converter to convert certificates without messing with OpenSSL.

在 Linux 上生成自签名 SSL 证书的方法


出处:https://comate.baidu.com/zh/page/5D9vk3ewpL


在Linux上生成自签名SSL证书,可以通过OpenSSL或mkcert工具来实现。本文将介绍这两种方法,并说明如何生成一个有效期为无限期的自签名SSL证书(尽管出于安全考虑,通常不建议这样做)。

使用OpenSSL生成自签名SSL证书

生成私钥

这条命令会生成一个2048位的RSA私钥文件server.key。

生成证书签名请求(CSR)

执行时,系统会提示输入证书信息,如国家、省份、城市等。

自签名生成证书

要生成有效期为“无限期”的证书,可以将-days参数设置为一个非常大的值,如36500天。

使用mkcert生成自签名SSL证书

mkcert是一个简化的工具,适用于本地开发。

下载安装mkcert

根据操作系统,从mkcert的GitHub页面下载并安装。

生成证书

-install选项会将mkcert的根证书安装到系统信任存储中。

配置Apache服务器使用自签名证书

复制证书和私钥到Apache配置目录

例如,放在/etc/apache2/ssl/目录下。

编辑Apache配置文件

在相关的站点配置文件中添加或修改以下配置:

重启Apache服务器

注意事项

通过以上方法,你可以在Linux上生成自签名SSL证书,并将其配置到Apache服务器上。但请记住,自签名证书主要用于开发和测试环境,不建议在生产环境中使用。

openssl 命令流水账(nixops.me)

Openssl命令用法比较多,整理了一下平时常用的命令,做了个流水账,方便以后复杂粘贴。

一、生成公钥和私钥

openssl genrsa -out nixops.me.key 2048
openssl rsa -in nixops.me.key -pubout -out nixops.me.crt

一条命令生成:

openssl  req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout nixops.me.key -out nixops.me.crt

验证私钥和公钥:

openssl rsa -check -in nixops.me.key
openssl x509 -text -noout -in nixops.me.crt

二、生成私钥和证书请求文件csr

openssl  genrsa -out nixops.me.key 2048
openssl req -new -sha256 -key nixops.me.key -out nixops.me.csr

查看csr 信息:

openssl req -noout -text -in nixops.me.csr

三、自建CA

生成CA私钥:

openssl genrsa -out CAKey.pem 2048

生成CA证书:

openssl req -x509 -sha256 -new -nodes -key CAKey.pem -days 3650 -out CACert.pem

也可以使用不用交互,直接提供相关信息:

openssl req -x509 -sha256 -new -nodes -key CAKey.pem -days 3650 -out CACert.pem  -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=myorganization/OU=nixps LTD/CN=nixops.me"

查看CA证书详细信息:

openssl x509 -in CACert.pem -text

使用CA签发证书:

openssl x509 -req -CA CACert.pem -CAkey CAKey.pem  -CAcreateserial -in nixops.me.csr -out nixops.me.issue.crt -days 365 -sha256

查看签发者信息:

openssl x509  -noout -issuer -issuer_hash -in nixops.me.issue.crt

四、使用openssl加密解密文件

4.1 生成并验证私钥

openssl genrsa -out nixops.me.pem 2048
openssl rsa -in nixops.me.pem -text -noout

4.2 导出公钥并验证

openssl rsa -in nixops.pem -pubout -out pub.pem
openssl rsa -in pub.pem -pubin -text -noout

4.3 加解密小文件

使用这种方式1024位的私钥可以加密小于86字节的文件,2048位的私钥可以加密小于214字节的文件。 用公钥加密:

openssl rsautl -encrypt -inkey pub.pem -pubin -in file.txt -out file.bin

用私钥解密:

openssl rsautl -decrypt -inkey nixops.pem -in file.bin

4.4 加解密大文件

用公钥加密:

openssl  smime  -encrypt -aes256  -in  Large.zip  -binary  -outform DEM  -out  Encrypted.zip  pub.pem

用私钥解密:

openssl  smime -decrypt  -in  Encrypted.zip  -binary -inform DEM -inkey nixops.pem  -out  Large.zip

五、证书格式转化

一般有以下几种标准格式:

5.1 DER/CER/CRT 转 PEM

先查看证书信息,在转格式:

openssl x509 -in cert.der -inform der -text -noout
openssl x509 -in cert.der -inform der -outform pem -out cert.pem

5.2 PEM转DER/CER/CRT

openssl x509 -in cert.pem -text -noout
openssl x509 -in cert.pem -outform der -out cert.der

5.3 PFX转PEM

openssl pkcs12 -info -nodes -in site.pfx
openssl pkcs12 -in site.pfx -out site.pem -nodes

5.4 JKS转PEM

需要JDK中提供的keytool工具配合openssl,先用keytool转成PKCS12格式:

keytool -importkeystore -srckeystore cert.jks -destkeystore cert.pkcs -srcstoretype JKS -deststoretype PKCS12

在用openssl转成pem格式:

openssl pkcs12 -in cert.pkcs -out cert.pem

六、其它一些技巧

6.1 移除证书中的密码

openssl rsa -in cert.key -out nopass.key

6.2 查看公钥的hash

openssl x509 -noout -hash -in cert.pem

6.3 查看在线网站的证书信息

openssl s_client -connect www.baidu.com:443 -showcerts

6.4 查看网站证书的有效期

查看本地证书:

openssl x509 -dates -noout -in file.pem  #查看证书签发时间和有效期
openssl x509 -startdate -noout -in file.pem #查看签发时间
openssl x509 -enddate -noout -in file.pem   #查看有效时间
openssl x509 -checkend 86400 -noout -in file.pem #检查证书是否在一天内过期,用echo $?即可判断

查看在线证书:

openssl s_client -connect www.baidu.com:443 -servername www.baidu.com 2> /dev/null |  openssl x509 -noout  -dates

提取过期时间:

openssl s_client  -connect www.baidu.com:443 -servername www.baidu.com 2>/dev/null |openssl x509 -enddate -noout |cut -d "=" -f 2

用date命令转换一下日期格式:

date --date="$(openssl s_client  -connect www.baidu.com:443 -servername www.baidu.com 2>/dev/null |openssl x509 -enddate -noout |cut -d "=" -f 2)" --iso-8601

6.5 检查网站是否接受指定版本的SSL协议

协议有TLS 1.0(tls1)、TLS 1.1(tls1_1) TLS 1.2(tls1_2),在高版本的openssl中默认已经禁用了SSL V2(ssl2)、SSL V3(ssl3)

openssl s_client  -connect www.baidu.com:443 -tls1

6.6 检查网站是否支持指定的加密算法

openssl s_client  -connect www.baidu.com:443 -tls1_2 -cipher 'ECDHE-RSA-AES128-GCM-SHA256'

参考文章:https://jamielinux.com/docs/openssl-certificate-authority/index.htmlhttps://stackoverflow.com/questions/21297853/how-to-determine-ssl-cert-expiration-date-from-a-pem-encoded-certificatehttps://geekflare.com/openssl-commands-certificates/

索引
OpenSSL 命令速查手册:证书、私钥与证书签名请求文件
The Most Common OpenSSL Commands
在 Linux 上生成自签名 SSL 证书的方法
openssl 命令流水账(nixops.me)