如何生成牛仔SSL证书?

时间:2016-01-15 08:36:51

标签: openssl erlang ssl-certificate cowboy erlangweb

在cowboy的ssl目录中,我看到3个文件 cowboy-ca.crtserver.crtserver.key。 可能在他们拥有cowboy-ca.key的目录的某个地方,这是不需要的。

我可以猜测cowboy-ca.crt是某个默认CA的公钥,并且用于使用csr文件为服务器的密钥对签署server.crt,并且当客户端连接到牛仔它下载并安装server.crt文件以建立到服务器的安全连接,我是否正确? 问题是如何使用openssl和我自己的CA生成所有这些文件?

2 个答案:

答案 0 :(得分:1)

网上有这方面的教程,但我碰巧记录了以前做过的事情。它比您需要的要多得多,但会告诉您如何实现创建自己的CA和从中签名的证书的基础知识。我的记录粘贴在下面,创建一个CA,创建一个由CA签名的中间CA,最后创建一个可以在服务器上使用的证书。您显然不需要中间CA,因此您应该跳过中间位并使用根CA而不是中间CA签署最终/结束证书,例如,在创建 end.crt时,而不是使用 ../inter_ca/inter.key 进行签名,请使用 ../ root_ca / rootca.key 等。

我假设你在这里问正确的问题,并且自签名证书确实是你想要的。

在创建证书和密钥之后,还提供了有关配置Apache的指导,以及如何使用OpenSSL工具验证其是否正常工作的说明(在任何SSL TCP连接上,以便' s也适用于牛仔或其他任何东西)。这将显示信任链,虽然您的信任链将是深度1而不是深度2,因为您将省略中间CA.

创建一些可以使用的目录:

mkdir inter_ca_demo
cd inter_ca_demo
mkdir root_ca inter_ca end_cert
cd root_ca

创建密钥:

openssl genrsa -out rootca.key 2048

输出类似于:

Generating RSA private key, 2048 bit long modulus
...........................................................+++
.............................+++
e is 65537 (0x10001)

自签名以创建根CA证书(您必须输入将编码到证书中的各种信息):

openssl req -x509 -new -nodes -key rootca.key -days 1024 -out rootca.pem

它应该看起来像(在这里你可以看到我输入的内容):

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:GB
State or Province Name (full name) [Some-State]:London
Locality Name (eg, city) []:London
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Method Analysis Ltd
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:methodanalysis.com
Email Address []:ca_admin@methodanalysis.com

在继续使用中间版之前,您必须重命名,复制或创建指向根证书的链接,以便可以通过哈希算法找到它。这是为了确保在CA路径中存在大量可信证书时性能不会降低。要执行此操作,您必须使用以下命令找出哈希值:

openssl x509 -noout -hash -in rootca.pem

输出应该是这样的:

03ed4e37

然后创建链接,将 .0 添加到你的哈希(作为上一个命令的输出):

ln -s rootca.pem 03ed4e37.0

现在创建中间CA密钥和CSR(证书签名请求)(您必须输入将编码到证书中的各种信息):

cd ../inter_ca
openssl genrsa -out inter.key 2048
openssl req -new -key inter.key -out inter.csr

看起来应该是这样的:

$ openssl genrsa -out inter.key 2048
Generating RSA private key, 2048 bit long modulus
.........................................+++
..............................................................+++
e is 65537 (0x10001)
$ openssl req -new -key inter.key -out inter.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:GB
State or Province Name (full name) [Some-State]:London
Locality Name (eg, city) []:London
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Intermediate certificates R US Ltd    
Organizational Unit Name (eg, section) []:               
Common Name (e.g. server FQDN or YOUR name) []:intermediatecasrus.com
Email Address []:ca_admin@intermediatecasrus.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

现在创建一个文件(v3x509extensions.txt),其中包含指示这应该是中间CA的数据,然后生成中间证书,使用根CA签名:

echo 'basicConstraints=CA:TRUE' > v3x509extensions.txt
openssl x509 -req -extfile v3x509extensions.txt -in inter.csr -CA ../root_ca/rootca.pem -CAkey ../root_ca/rootca.key -CAcreateserial -out inter.crt -days 200

看起来应该是这样的:

Signature ok
subject=/C=GB/ST=London/L=London/O=Intermediate certificates R US Ltd/CN=intermediatecasrus.com/emailAddress=ca_admin@intermediatecasrus.com
Getting CA Private Key

现在创建您的最终密钥(您将用于SSL网站(例如)),从中创建CSR,并使用您的中间证书对其进行签名,生成新证书:

cd ../end_cert
openssl genrsa -out end.key 2048
openssl req -new -key end.key -out end.csr
openssl x509 -req -in end.csr -CA ../inter_ca/inter.crt -CAkey ../inter_ca/inter.key -CAcreateserial -out end.crt -days 500

看起来应该是这样的:

$ openssl genrsa -out end.key 2048
Generating RSA private key, 2048 bit long modulus
................................................+++
.....................................................+++
e is 65537 (0x10001)
$ openssl req -new -key end.key -out end.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:GB
State or Province Name (full name) [Some-State]:London
Locality Name (eg, city) []:London
Organization Name (eg, company) [Internet Widgits Pty Ltd]:End User Ltd
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:intermediatecademo-enduser.com
Email Address []:support@intermediatecademo-enduser.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
$ openssl x509 -req -in end.csr -CA ../inter_ca/inter.crt -CAkey ../inter_ca/inter.key -CAcreateserial -out end.crt -days 500
Signature ok
subject=/C=GB/ST=London/L=London/O=End User Ltd/CN=intermediatecademo-enduser.com/emailAddress=support@intermediatecademo-enduser.com
Getting CA Private Key

此时,您可以通过验证信任链来检查事情是否正常,在这里我将中间结构与结束'结合起来。证书,然后仅使用根CA路径验证它们(请记住,如果没有中间CA,则无法验证' end'证书,因此必须使用' end&#39 ;证书):

cat ../inter_ca/inter.crt end.crt | openssl verify -CApath ../root_ca

输出应为:

stdin: OK

例如,如果您将这些密钥用于apache,则可以像下面这样配置它们:

SSLCertificateFile       FULL_PATH_TO/inter_ca_demo/end_cert/end.crt
SSLCertificateKeyFile    FULL_PATH_TO/inter_ca_demo/end_cert/end.key

SSLCertificateChainFile  FULL_PATH_TO/inter_ca_demo/inter_ca/inter.crt

SSLCACertificatePath     FULL_PATH_TO/inter_ca_demo/root_ca

Apache现在将提供中间证书及其自身,允许Web客户端使用' openssl verify'执行相同类型的验证(以及其他检查)。

如果您确实将这些证书与apache一起使用,并且您创建了一个名为&intercadecademo-enduser.com'的网站。与“结束”保持一致您创建的证书,您也可以使用openssl从客户端角度验证证书:

openssl s_client -connect intermediatecademo-enduser.com:443 -CApath root_ca -verify 5

输出应该是这样的:

verify depth is 5
CONNECTED(00000004)
depth=2 C = GB, ST = London, L = London, O = Method Analysis Ltd, CN = methodanalysis.com, emailAddress = ca_admin@methodanalysis.com
verify return:1
depth=1 C = GB, ST = London, L = London, O = Intermediate certificates R US Ltd, CN = intermediatecasrus.com, emailAddress = ca_admin@intermediatecasrus.com
verify return:1
depth=0 C = GB, ST = London, L = London, O = End User Ltd, CN = intermediatecademo-enduser.com, emailAddress = support@intermediatecademo-enduser.com
verify return:1
---
Certificate chain
 0 s:/C=GB/ST=London/L=London/O=End User Ltd/CN=intermediatecademo-enduser.com/emailAddress=support@intermediatecademo-enduser.com
   i:/C=GB/ST=London/L=London/O=Intermediate certificates R US Ltd/CN=intermediatecasrus.com/emailAddress=ca_admin@intermediatecasrus.com
 1 s:/C=GB/ST=London/L=London/O=Intermediate certificates R US Ltd/CN=intermediatecasrus.com/emailAddress=ca_admin@intermediatecasrus.com
   i:/C=GB/ST=London/L=London/O=Method Analysis Ltd/CN=methodanalysis.com/emailAddress=ca_admin@methodanalysis.com
---
Server certificate

...
...
...

    Start Time: 1445696823
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

现在它会挂起,等待发送数据。你可以按CTRL + C退出。

答案 1 :(得分:0)

你对证书关系基本上是正确的;有一个服务器证书和私钥,以及一个CA证书。

由于它们仅用于测试目的,我认为牛仔的作者并不打算包括他用来生成服务器证书的CA私钥。

关于SSL证书,牛仔没有什么特别之处。假设您正在查看ssl_hello_world example,您可以在ssl_hello_world_app.erl中看到正在使用的证书。

从那里,这些SSL选项被传递给Erlang SSL应用程序,文档记录为here

文档说明(虽然不是很明显)这些是标准的PEM编码证书和密钥文件。例如,您可以使用OpenSSL生成它们。