使用mkcert建立自签名证书

原文发布于:mengqi.info/html/2019/2…html

在作程序开发的时候,免不了要接触https加密通讯,你可能须要本身生成证书,虽然可使用openssl完成这个工做,可是openssl是一个庞大和复杂的工具,有着使人眼花缭乱的参数,若是你没有太多的密码学知识,只是为了在本机生成一个自签名证书,方便本身开发和测试,那么能够试一试这个用Go语言写的命令行工具:mkcert,很是简单易用。linux

mkcert的Github地址:github.com/FiloSottile…,该项目有18000多颗星,做者Filippo Valsorda在2018年加入了Go的开发团队。关于mkcert,做者给出的一句话介绍:git

mkcert is a simple tool for making locally-trusted development certificates. It requires no configuration.github

做者提供了编译好的二进制程序,包含Linux/Windows/macOS三个版本,可直接下载使用:github.com/FiloSottile…。你也可使用brew安装,或者经过源码编译,具体详见做者在Github上面的说明。web

下面,我以debian linux为例,介绍一下mkcert 1.3的使用方式:chrome

  1. 将mkcert下载之后,放到一个合适的地方,并添加执行权限:
ln -s ~/download/mkcert-v1.3.0-linux-amd64 ~/bin/mkcert
chmod u+x ~/bin/mkcert
复制代码
  1. 在linux下,mkcert依赖certutil,若是没有安装certutil,可使用下面的命令安装:
sudo apt install libnss3-tools
复制代码
  1. 生成CA(certificate authority,即证书颁发机构),运行下面的命令能够在~/.local/share/mkcert/生成rootCA.pemrootCA-key.pem两个文件,这个命令只需运行一次,由于生成的CA能够反复使用;
mkcert -install
复制代码
  1. 建立自签名证书,例如要为域名:test.local和IP:127.0.0.1建立证书,可使用以下的命令:
mkcert test.local 127.0.0.1
复制代码

上述命令会自动使用第3步建立的CA生成证书文件,其中xxx.pem为证书,xxx-key.pem为私钥,你也可使用-cert-file-key-file两个参数设置生成文件的文件名。浏览器

生成了证书和私钥之后,就能够在web服务器开启https了。安全

以我本身的web服务器ran为例,可使用-cert-key参数设置证书和私钥的路径,这时会默认在443端口开启web服务(使用较低的端口须要使用管理员权限),具体命令以下:bash

sudo ran -l -cert /path/to/cert -key /path/to/key
复制代码

接下来,能够打开浏览器测试一下了:服务器

在浏览器中测试

从上图能够看到,chrome浏览器地址栏中显示了一把小锁,表示是安全的链接。若是把地址修改为 https://127.0.0.2,浏览器就会提示为不安全的链接,这是由于刚才使用mkcert建立证书的时侯,并无设置127.0.0.2这个地址。

不安全的链接

在使用mkcert的过程当中,我发现了一个问题:虽然生成的证书在浏览器里会显示为安全的,可是使用curl测试的时候却报错了,意思大概就是找不到自建的CA:

$ curl https://127.0.0.1
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
复制代码

我在终端里找到了刚才运行mkcert -install时出现的提示:

Using the local CA at "/home/<user>/.local/share/mkcert"
Installing to the system store is not yet supported on this Linux but Firefox and/or Chrome/Chromium will still work.
You can also manually install the root certificate at "/home/<user>/.local/share/mkcert/rootCA.pem".
The local CA is now installed in the Firefox and/or Chrome/Chromium trust store (requires browser restart)!
复制代码

里面有一句:Installing to the system store is not yet supported on this Linux。

好吧,那么我来手工安装一下刚才生成的CA(如下命令均须要用管理员权限运行):

  1. 进入/usr/share/ca-certificates文件夹,建立一个新文件夹local,在这个文件夹中建立一个指向mkcert生成的证书的软连接:
cd /usr/share/ca-certificates
mkdir local
cd local
ln -s /home/<user>/.local/share/mkcert/rootCA.pem my-local-ca.crt
复制代码
  1. 编辑文件/etc/ca-certificates.conf,添加一行:
local/my-local-ca.crt
复制代码
  1. 运行下面的命令:
update-ca-certificates
复制代码

这样,使用curl链接的时候就没有报错了:

$ curl https://127.0.0.1
<h1>hello world</h1>
复制代码
相关文章
相关标签/搜索