D 的个人博客

但行好事莫问前程

  menu
417 文章
3446695 浏览
12 当前访客
ღゝ◡╹)ノ❤️

Windows 通过命令行安装根证书

背景

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

工具

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

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

certmgrexepng

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

certmgrmscpng

certmgr.exe

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

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

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

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

结论

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