【问题标题】:How can I create a self-signed cert for localhost?如何为 localhost 创建自签名证书?
【发布时间】:2011-12-31 11:36:43
【问题描述】:

我已经完成了How do you use https / SSL on localhost? 中详述的步骤,但这会为我的机器名称设置一个自签名证书,当通过https://localhost 浏览它时,我会收到 IE 警告。

有没有办法为“localhost”创建一个自签名证书来避免这个警告?

【问题讨论】:

标签: iis windows-7 ssl-certificate


【解决方案1】:

由于这个问题被标记为IIS,我找不到关于如何获得可信证书的好答案,我将为此付出 2 美分:

首先以管理员身份在 PowerShell 中使用来自@AuriRahimzadeh 的命令:

New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My"

这很好,但是证书不受信任,会导致以下错误。是因为没有安装在Trusted Root Certification Authorities中。

通过启动mmc.exe来解决这个问题。

然后转到:

文件 -> 添加或删除管理单元 -> 证书 -> 添加 -> 计算机帐户 -> 本地计算机。点击完成。

展开Personal 文件夹,您将看到您的localhost 证书:

将证书复制到Trusted Root Certification Authorities - Certificates文件夹中。

最后一步是打开Internet Information Services (IIS) Manager 或直接打开inetmgr.exe。从那里转到您的站点,选择Bindings...Add...Edit...。设置https 并从下拉列表中选择您的证书。

您的证书现在受信任:

【讨论】:

  • 这应该是公认的答案!有用!如果您弄乱了本地主机和受信任的证书。确保删除所有旧的 localhost 证书(通过 mmc 控制台和 IIS(顶级托管服务器)
  • 谢谢!关于这个主题有很多 SO 答案和博客文章,但很少谈论证书信任问题。提示:也许您应该解释一下为什么不使用默认 SSL 端口 443?我猜是因为这个端口经常被其他进程占用。
  • 非常好的答案,并且在 2020 年的 IIS 上运行良好。非常感谢!
  • 我一刷新Certificates 文件夹,证书就会被删除。
  • 在 Windows 11 上并且没有 IIS,这几乎可以工作。我需要先导出然后重新导入证书,然后才能让 Edge/Brave 识别它。重新启动机器或浏览器等并没有实现这一点。在受信任的证书上完成导出/删除/导入后,它就可以工作了。
【解决方案2】:

虽然这篇文章是针对 Windows 标记的,但它是 OS X 上的相关问题,我没有在其他地方看到答案。以下是在 OS X 上为 localhost 创建自签名证书的步骤:

# Use 'localhost' for the 'Common name'
openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 365 -keyout localhost.key -out localhost.crt

# Add the cert to your keychain
open localhost.crt

Keychain Access 中,双击这个新的本地主机证书。展开“信任”旁边的箭头并选择“始终信任”。 Chrome 和 Safari 现在应该信任这个证书。例如,如果您想将此证书与 node.js 一起使用:

var options = {
    key: fs.readFileSync('/path/to/localhost.key').toString(),
    cert: fs.readFileSync('/path/to/localhost.crt').toString(),
    ciphers: 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384',
    honorCipherOrder: true,
    secureProtocol: 'TLSv1_2_method'
};

var server = require('https').createServer(options, app);

【讨论】:

  • 第一个命令 ssh-keygen 是不必要的,因为 openssl 命令创建了一个新密钥(并覆盖了 ssh 创建的那个)。
  • 您还可以通过将-subj '/CN=localhost' 添加到openssl 参数来完全自动化该过程。
  • 要让 OS X 从命令行信任它而不是到处点击,你可以这样做:sudo security add-trusted-cert -p ssl -d -r trustRoot -k ~/Library/Keychains/login.keychain localhost.crt
  • 也与 linux 相关。非常感谢。
  • 我按照所有这些步骤操作,在 Chrome 60 中获得了 ERR_SSL_VERSION_OR_CIPHER_MISMATCH,而 Safari 10.1.2 也不喜欢它。
【解决方案3】:

您可以使用 PowerShell 通过 new-selfsignedcertificate cmdlet 生成自签名证书:

New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My"

注意:makecert.exe 已弃用。

Cmdlet 参考: https://technet.microsoft.com/itpro/powershell/windows/pkiclient/new-selfsignedcertificate

【讨论】:

  • 这应该是 2017 年的答案。
  • 对于那些关注但不知道如何安装生成的证书的人,请按照此视频中的步骤操作,它对我有用! youtube.com/watch?v=y4uKPUFmSZ0
  • key 和 crt 文件存储在哪里?
  • @woojoo666 带有-KeyLocation 标志,您可以指定位置。
  • New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My" -KeyLocation "c:\users\mad\certs" 在指定目录中不生成文件。
【解决方案4】:

在这个问题上花了很多时间后,我发现每当我遵循使用 IIS 制作自签名证书的建议时,我发现颁发给和颁发者不正确。 SelfSSL.exe 是解决这个问题的关键。下面这个网站不仅提供了一步一步制作自签名证书的方法,还解决了 Issued To 和 Issued by 问题。 Here 是我找到的用于制作自签名证书的最佳解决方案。如果您希望以视频形式观看相同的教程click here

SelfSSL 的示例使用如下所示:

SelfSSL /N:CN=YourWebsite.com /V:1000 /S:2

SelfSSL /? 将提供参数列表和解释。

【讨论】:

  • RobBagby.com 的文章非常适合我。很好地找到亨利。
  • 超级!该视频质量较差,但所需的一切都有。
  • robbagby 站点的链接已失效。请参阅我对 CodeCowboyOrg 的 2 个高质量 youtube 教程的回答
  • 已编辑答案以链接到 robbagby.com 上文章的 Wayback 存档。
【解决方案5】:

如果您尝试创建一个自签名证书,让您可以使用http://localhost/mysite 那么这里有一个方法来创建它

makecert -r -n "CN=localhost" -b 01/01/2000 -e 01/01/2099 -eku 1.3.6.1.5.5.7.3.1 -sv localhost.pvk localhost.cer
cert2spc localhost.cer localhost.spc
pvk2pfx -pvk localhost.pvk -spc localhost.spc -pfx localhost.pfx

来自http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/32bc5a61-1f7b-4545-a514-a11652f11200

【讨论】:

  • 我的证书去哪了 :o 它不在 C:\Windows\system32
  • 顺便说一下,makecert.exe 工具是通过 Visual Studio 命令提示符为未来的读者使用的。您还可以使用 Internet 信息服务 (IIS) 资源工具包工具并安装 SelfSSL 1.0。 microsoft.com/downloads/en/…
  • 虽然快速回答很好,但它们并不总是有帮助。 “证书不能用作 SSL 服务器证书”是我在 IIS7 中看到的错误。 Op 还提到了浏览器警告,而不是“我如何创建证书”?
  • eku 用于代码签名,如果您想要 SSL 证书,您需要使用 eku 1.3.6.1.5.5.7.3.1。我个人是 -eku 1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.3,它为您提供客户端身份验证、服务器身份验证和代码签名。
  • @EaterOfCode 我在 C:\Windows\SysWOW64
【解决方案6】:

我会推荐 Pluralsight 的用于创建自签名证书的工具:http://blog.pluralsight.com/selfcert-create-a-self-signed-certificate-interactively-gui-or-programmatically-in-net

将您的证书制作为 .pfx 并将其导入 IIS。并将其添加为受信任的根证书颁发机构。

【讨论】:

  • 在尝试了各种 PowerShell“魔法”和其他不起作用的东西之后 - Pluralsight 的工具在 30 秒内为我生成了一个工作脚本,其中包括它所花费的时间吃披萨(我的意思是+下载并提取工具)。由于我仍在这台旧笔记本电脑上运行 Windows 7 - 这对我来说是完美的解决方案,谢谢!
【解决方案7】:

是的,不是的。自签名证书会导致该警告消息,因为该证书未由受信任的证书颁发机构签名。您可以考虑使用几个选项来删除本地计算机上的此警告。有关详细信息,请参阅此问题的排名最高的答案:

What do I need to do to get Internet Explorer 8 to accept a self signed certificate?

希望这会有所帮助!


编辑:

抱歉,我最初并不知道您被限制在本地主机上。您可以尝试按照以下链接中的说明“使用正确的通用名称生成自签名证书”。

http://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-in-iis-7.html

【讨论】:

  • 不,警告信息存在,因为 URL (localhost) 与机器名称下颁发的证书名称不匹配。如果我切换到mymachinename,那么我不会收到错误消息。不幸的是,出于不影响问题的原因,我必须使用 localhost 而不是机器名。
【解决方案8】:

你可以试试 mkcert。

macos: brew install mkcert

【讨论】:

    【解决方案9】:

    在 LAN(局域网)中,我们有一台服务器计算机,这里名为 xhost,运行 Windows 10,IIS 被激活为 WebServer。我们必须通过像谷歌浏览器这样的浏览器访问这台计算机,不仅从 localhost 通过 https://localhost/ 从服务器本身,而且还从 LAN 中的其他主机使用 URL https://xhost /:

    
    https://localhost/
    https://xhost/
    https://xhost.local/
    ...
    

    通过这种访问方式,我们这里没有全限定域名,只有本地计算机名xhost。

    或来自 WAN:

    
    https://dev.example.org/
    ...
    

    您应将 xhost 替换为您的真实本地计算机名称。

    以上解决方案都不能满足我们。经过几天的尝试,我们采用了openssl.exe的解决方案。我们使用 2 个证书 - 一个 CA(自我认证的权威证书)RootCA.crt 和由前者认证的 xhost.crt。我们使用 PowerShell。

    1。创建并更改为安全目录:

    cd C:\users\so\crt

    2。生成 RootCA.pem、RootCA.key 和 RootCA.crt 作为自我认证的证书颁发机构:

    
    openssl req -x509 -nodes -new -sha256 -days 10240 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=ZA/CN=RootCA-CA"
    openssl x509 -outform pem -in RootCA.pem -out RootCA.crt
    

    3。申请认证:xhost.key, xhost.csr:

    C: Country
    ST: State
    L: locality (city)
    O: Organization Name
    Organization Unit
    CN: Common Name
    
        
    
    
    openssl req -new -nodes -newkey rsa:2048 -keyout xhost.key -out xhost.csr -subj "/C=ZA/ST=FREE STATE/L=Golden Gate Highlands National Park/O=WWF4ME/OU=xhost.home/CN=xhost.local"
    

    4。获得 RootCA.pem 认证的 xhost.crt:

    
    openssl x509 -req -sha256 -days 1024 -in xhost.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -extfile domains.ext -out xhost.crt
    

    使用 extfile domain.ext 文件定义访问服务器网站的多种安全方式:

    authorityKeyIdentifier=keyid,issuer
    basicConstraints=CA:FALSE
    keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
    subjectAltName = @alt_names
    [alt_names]
    DNS.1 = localhost
    DNS.2 = xhost
    DNS.3 = xhost.local
    DNS.4 = dev.example.org
    DNS.5 = 192.168.1.2
    

    5。制作 xhost.pfx PKCS #12,

    结合私有 xhost.key 和证书 xhost.crt,允许导入 iis。此步骤要求输入密码,请按[RETURN]键使其为空(无密码):

    
    openssl pkcs12 -export -out xhost.pfx -inkey xhost.key -in xhost.crt
    

    6。在 iis10 中导入 xhost.pfx

    安装在 xhost 计算机上(这里是 localhost)。并重启 IIS 服务。

    IIS10 Gestionnaire des services Internet (IIS) (%windir%\system32\inetsrv\InetMgr.exe)
    

    7。在端口 443 上将 ssl 与 xhost.local 证书绑定。

    重新启动 IIS 服务。

    8。将 RootCA.crt 导入 受信任的根证书颁发机构

    在将访问该网站 https://xhost/ 的任何计算机上通过 Google Chrome。

    \Google Chrome/…/设置 /[高级]/隐私和安全/安全/管理证书

    导入 RootCA.crt

    浏览器会显示这个有效的证书树:

    
    RootCA-CA
      |_____ xhost.local
    

    即使https://dev.example.org 通过 WAN,也不会通过 LAN 出现证书错误。

    这是整个 Powershell 脚本 socrt.ps1 文件,用于生成所有必需的证书文件:

    
    #
    # Generate:
    #   RootCA.pem, RootCA.key RootCA.crt
    #
    #   xhost.key xhost.csr xhost.crt
    #   xhost.pfx
    #
    # created  15-EEC-2020
    # modified 15-DEC-2020
    #
    #
    # change to a safe directory:
    #
    
    cd C:\users\so\crt
    
    #
    # Generate RootCA.pem, RootCA.key & RootCA.crt as Certification Authority:
    #
    openssl req -x509 -nodes -new -sha256 -days 10240 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=ZA/CN=RootCA-CA"
    openssl x509 -outform pem -in RootCA.pem -out RootCA.crt
    
    #
    # get RootCA.pfx: permitting to import into iis10: not required.
    #
    #openssl pkcs12 -export -out RootCA.pfx -inkey RootCA.key -in RootCA.crt
    
    #
    # get xhost.key xhost.csr:
    #   C: Country
    #   ST: State
    #   L: locality (city)
    #   O: Organization Name
    #   OU: Organization Unit
    #   CN: Common Name
    #
    openssl req -new -nodes -newkey rsa:2048 -keyout xhost.key -out xhost.csr -subj "/C=ZA/ST=FREE STATE/L=Golden Gate Highlands National Park/O=WWF4ME/OU=xhost.home/CN=xhost.local"
    
    #
    # get xhost.crt certified by RootCA.pem:
    # to show content:
    #   openssl x509 -in xhost.crt -noout -text
    #
    openssl x509 -req -sha256 -days 1024 -in xhost.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -extfile domains.ext -out xhost.crt
    
    #
    # get xhost.pfx, permitting to import into iis:
    #
    openssl pkcs12 -export -out xhost.pfx -inkey xhost.key -in xhost.crt
    
    #
    # import xhost.pfx in iis10 installed in xhost computer (here localhost).
    #
    

    要安装适用于 Windows 的 openSSL,请访问https://slproweb.com/products/Win32OpenSSL.html

    【讨论】:

      【解决方案10】:

      生成本地证书的最快方法。

      openssl req -x509 -out localhost.crt -keyout localhost.key \
        -newkey rsa:2048 -nodes -sha256 \
        -subj '/CN=localhost' -extensions EXT -config <( \
         printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
      

      【讨论】:

        【解决方案11】:

        如果您使用的是 Visual Studio,可以使用 IIS Express explained here 轻松设置和启用 SSL

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-11-07
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-07
          • 2012-11-28
          相关资源
          最近更新 更多