Windows 通过命令行安装根证书

本贴最后更新于 2175 天前,其中的信息可能已经渤澥桑田

背景

有的时候我们需要在用户机器上将自制的 CA 证书安装到“受信任的根证书颁发机构”下,这样就能使用该 CA 颁发自签名的证书了,以便后续完成一些安全相关的操作。

工具

在命令行安装证书需要用到证书管理器 certmgr.exe,这个二进制可执行文件在 Win10 中 大概 这个路径下,C:\Program Files (x86)\Windows Kits\8.1\bin\x86\certmgr.exe,建议把这个二进制打在程序的发布包中,或者让程序从网络上拉取到本地。

不带参直接运行的话会弹出一个简单的图形界面:

certmgrexepng

另外,还有个证书管理器 certmgr.msc,看上去功能更多一些,不过这个是纯图形界面的:

certmgrmscpng

certmgr.exe

可以通过该工具实现证书的管理操作:

D:\>certmgr.exe /?
Usage: CertMgr [options][-s [-r <location>][SourceStoreName]
                        [-s [-r <location>][DestinationStoreName]
Options:
 -add               Add certificates/CRLs/CTLs to a storeFile or a system store
 -del               Delete certificates/CRLs/CTLs from a storeFile or
                    a system store
 -put               Put an encoded certificate/CRL/CTL from a storeFile or
                    a system store to a file.  The file will be saved in X.509
                    format. -7 can be used to save the file in PKCS #7 format
 -s                 Indicate the store is a system store
 -r    <location>   The system store location
                        <currentUser|localMachine> Default to 'currentUser'
 -c                 Certificates in the store
 -crl               Certificates revocation lists(CRLs) in the store
 -ctl               Certificates trust lists(CTLs) in the store
 -v                 Verbose display of the certificates/CRLs/CTLs
 -all               All certificates/CRLs/CTLs in the store
 -n    <name>       Common name of the certificate
 -sha1 <thumbPrint> The sha1 hash of the certificate/CRLs/CTLs
 -7                 Save the destination store in PKCS #7 format
 -e    <encode>     Certificate/CRL/CTL encoding type.
                    Default to X509_ASN_ENCODING
 -f    <flag>       CertStore open flags.  Meaningful only if -y is set
 -y    <provider>   CertStore provider name

我们的目标是将 CA 证书导入到“受信任的根证书颁发机构”下,可以通过如下命令参数实现:certmgr.exe /c /add ca.crt /s root

导入之前可以通过 certmgr.exe /all /s root 来查询是否已经导入过。

结论

  • certmgr.exe 需要自己分发到确定的路径下再调用
  • 作为用户不要轻易安装来历不明的证书
  • Windows

    Microsoft Windows 是美国微软公司研发的一套操作系统,它问世于 1985 年,起初仅仅是 Microsoft-DOS 模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也慢慢的成为家家户户人们最喜爱的操作系统。

    215 引用 • 462 回帖
  • CA
    2 引用 • 13 回帖
  • 根证书
    1 引用 • 8 回帖
  • 安全

    安全永远都不是一个小问题。

    189 引用 • 813 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...