大网数据广告活动
服务器运维问答

How to properly create CA certificate and sign the SSL/TLS cert to use in Apache for localhost?

· 52CE 我爱测

I've found this article and this what I used to generate CA cert: openssl genrsa -des3 -out myCA.key 2048 openssl req -x509 -new -nodes -key myCA.key -sha256 -days 1825 -out myCA.pem I name it "MyCert" And I used it to sign the c…

问题原文

原帖链接 · jcubic · CC BY-SA 4.0

I've found this article and this what I used to generate CA cert:

openssl genrsa -des3 -out myCA.key 2048
openssl req -x509 -new -nodes -key myCA.key -sha256 -days 1825 -out myCA.pem

I name it "MyCert"

And I used it to sign the cert I generated for localhost (it was the output of ChatGPT)

$ openssl req -newkey rsa:2048 -nodes -keyout localhost.key -out localhost.csr
$ openssl x509 -req -in localhost.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial -out localhost.crt -days 365 -sha256

I've added the CA cert to the system (I use Fedora):

sudo cp myCA.pem /etc/pki/ca-trust/source/anchors/
sudo update-ca-trust extract

I restarted the server and when access https://localhost

Got this error:

NET::ERR_CERT_COMMON_NAME_INVALID
Subject: localhost
Issuer: MyCert

What did I wrong? I use default fields for the CA cert:

CN = MyCert
O = Default Company Ltd
L = Default City
C = pl

and the localhost cert have those fields:

CN = localhost
C = pl

How to properly create CA cert and sign the cert for Apache on Fedora?

已采纳解答

原帖链接 · larsks · CC BY-SA 4.0

This worked for me:

  1. Generate CA certificate.
openssl req -x509 -new -nodes -newkey rsa:2048 -keyout myCA.key \
  -sha256 -days 1825 -out myCA.crt -subj /CN='localhost ca'
  1. Generate the server certificate request. Note that in addition to setting CN=localhost we're also setting a subjectAlternativeName for localhost, because:

RFC 2818, published in May 2000, deprecates the use of the Common Name (CN) field in HTTPS certificates for subject name verification. Instead, it recommends using the “Subject Alternative Name” extension (SAN) of the “dns name” type. ref

(Note that the deprecation of commonName for identifying hosts was 24 years ago.)

openssl req -newkey rsa:2048 -nodes -keyout localhost.key -out localhost.csr \
  -subj /CN=localhost -addext subjectAltName=DNS:localhost
  1. Sign the certificate request to create a certificate. Note the use of -copy_extensions copy so that we copy the subjectAlternativeName from the request to the certificate.
openssl x509 -req -in localhost.csr -copy_extensions copy \
  -CA myCA.crt -CAkey myCA.key -CAcreateserial -out localhost.crt -days 365 -sha256
  1. Add the CA certificate to the trust store.
sudo cp myCA.crt /etc/pki/ca-trust/source/anchors/
sudo update-ca-trust

I'm testing the certificate by setting up a simple TLS-enabled server using caddy. I start with this Caddyfile:

{
    http_port 8080
}
:8443 {
    respond "this is a test"
    tls localhost.crt localhost.key
}

And then:

caddy run

And now we can see that curl trusts the certificate:

$ curl https://localhost:8443
this is a test

If I remove the CA certificate from the trust store:

sudo rm  /etc/pki/ca-trust/source/anchors/myCA.crt
sudo update-ca-trust

Then we will see that curl fails:

$ curl https://localhost:8443
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-03-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。