AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1156946
Accepted
jcubic
jcubic
Asked: 2024-03-27 04:41:42 +0800 CST2024-03-27 04:41:42 +0800 CST 2024-03-27 04:41:42 +0800 CST

如何正确创建 CA 证书并签署 SSL/TLS 证书以在 Apache 中用于 localhost?

  • 772

我找到了这篇文章,这是我用来生成 CA 证书的内容:

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

我将其命名为“MyCert”

我用它来签署为本地主机生成的证书(它是 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

我已将 CA 证书添加到系统中(我使用 Fedora):

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

我重新启动了服务器,当访问时https://localhost

得到这个错误:

NET::ERR_CERT_COMMON_NAME_INVALID
Subject: localhost
Issuer: MyCert

我做错了什么?我使用 CA 证书的默认字段:

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

本地主机证书具有这些字段:

CN = localhost
C = pl

如何在 Fedora 上正确创建 CA 证书并为 Apache 签署证书?

ssl-certificate
  • 1 1 个回答
  • 193 Views

1 个回答

  • Voted
  1. Best Answer
    larsks
    2024-03-27T05:48:16+08:002024-03-27T05:48:16+08:00

    这对我有用:

    1. 生成CA证书。
    openssl req -x509 -new -nodes -newkey rsa:2048 -keyout myCA.key \
      -sha256 -days 1825 -out myCA.crt -subj /CN='localhost ca'
    
    1. 生成服务器证书请求。请注意,除了设置之外,CN=localhost我们还设置了subjectAlternativeName for localhost,因为:

    2000 年 5 月发布的 RFC 2818 反对在 HTTPS 证书中使用通用名称 (CN) 字段进行主题名称验证。相反,它建议使用“dns 名称”类型的“主题备用名称”扩展 (SAN)。参考

    (请注意,用于识别主机的 commonName 已于24 年前被弃用。)

    openssl req -newkey rsa:2048 -nodes -keyout localhost.key -out localhost.csr \
      -subj /CN=localhost -addext subjectAltName=DNS:localhost
    
    1. 签署证书请求以创建证书。请注意 的使用-copy_extensions copy,以便我们将subjectAlternativeName请求中的 复制到证书中。
    openssl x509 -req -in localhost.csr -copy_extensions copy \
      -CA myCA.crt -CAkey myCA.key -CAcreateserial -out localhost.crt -days 365 -sha256
    
    1. 将 CA 证书添加到信任存储区。
    sudo cp myCA.crt /etc/pki/ca-trust/source/anchors/
    sudo update-ca-trust
    

    我正在通过使用caddy设置一个简单的启用 TLS 的服务器来测试证书。我从这个开始Caddyfile:

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

    进而:

    caddy run
    

    现在我们可以看到curl信任该证书:

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

    如果我从信任存储中删除 CA 证书:

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

    然后我们会看到curl失败:

    $ 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.
    
    • 4

相关问题

  • SSL证书的区别?

  • 如何修复邮件服务器 SSL?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve