使用 OpenSSL 在 Powershell 中为 300 多台服务器生成证书

Using OpenSSL to generate certificates for 300+servers in Powershell

好久没来了

我的环境中有 300 台新服务器,我的组织想要为使用“OpenSSL”生成证书。我有系统的名称,并且我之前使用过 OpenssL 来执行此操作,事实是,我不想整天坐着输入每条信息 300 多次。我想自动化这个。到目前为止,一切正常,直到我必须输入州、通用名称、地点、组织等信息。

我的问题是,使用Powershell;如何将此信息输入 Openssl?

仅供参考,过去两天我一直在网上搜索。我经常遇到 bash 脚本、Openssl 手册、“如何”(没有自动化)来生成证书,尤其是使用 powershell。

我确实遇到过一个结果,该人使用了一个函数,但它所做的只是生成一个请求,而不是多个,而且他根本没有输入任何信息。但他最终获得​​了 CSR,所以我很困惑它到底是如何工作的。

这是我当前的代码,为了保护隐私,我已经清理了它:

$ServerList = 'C:\Temp\server_names.txt' #This is the input file with the server names. It is all clean and only contains the names. Not FQDNs.
$ServerlistContent = get-content $ServerList #Dump the contents of the input file into a variable 

$keyPath = "D:\Openssl\OpenSSL-Win64\bin\Servers\Key\" #Output path for the '.key' file to be generated
$PemPath = "D:\Openssl\OpenSSL-Win64\bin\Servers\Pem\" #Output path for the '.pem' file to be generated

$FQDN = '.contoso.com' #Part of the common name, this will be concatenated before being used.

#Entries below this point are meant to be used when entering the information needed to generate the Certificate
$Conutry_Name = 'US'
$State_Name = ''
$Locality_Name = ''
$Org_Name = 'Contoso'
$Org_Unit_Name = 'HR'
$Email_Address = ''
$Challenge_pwd = ''
$optional_Name = ''
#Entries Above this point are meant to be used when entering the information needed to generate the Certificate

#Begin looping through the variable "$ServerlistContent" so that this Can be automated
foreach ($server in $ServerlistContent) {

    $Commonname = $server + $FQDN #Entry to be used for generating the certificate request.
    $Serverkey = $Server + ".key"
    $key_out = $keypath + $serverkey #Used to Create the path needed (to include the server name and file extension) for the Openssl Command
    $Serverpem = $Server + "_req.pem"
    $Pem_out = $PemPath + $Serverpem #Used to Create the path needed (to include the server name and file extension) for the Openssl Command

    D:\Openssl\OpenSSL-Win64\bin\openssl.exe genrsa -out $key_out 2048

    D:\Openssl\OpenSSL-Win64\bin\openssl.exe req -new -key $key_out -sha256 -out $Pem_out -verify -newhdr
}

#After this point, here is where I run into trouble, Powershell just hangs and i cannot enter anything, not even manually. (See Image 1)

图片 1:

在深入研究如何将主题参数传递给 openssl 之前,我必须警告您,预期的方法很可能是 一个非常糟糕的想法!

私钥千万不要离开主机!

public-key 密码学背后的整个思想是大部分密钥 material 必须保持 秘密(或 私有),而验证者只需要它的一小部分(public 密钥)来加密只有私钥持有者才能解密的数据——在turn 可用于身份验证。

如果私钥的副本存在于多台机器上,那么多台机器都可以声明与相应证书关联的身份(即服务器或网站名称)。

出于这个原因,我会强烈建议编写一个脚本在本地生成单个密钥+证书,然后在每台机器上执行脚本——这样,私钥material 永远不必离开主人。


通过 openssl req

假设您使用的是 openssl 的最新可用版本,req 上下文应该有一个 -subj 开关,您可以将完整的主题传递到该开关:

openssl req -new -key $key_out -sha256 -out $Pem_out -verify -newhdr -subj "/C=US/ST=NY/L=Albany/O=SuperCorp Ltd./CN=server.fqdn"

所以您需要做的就是根据您已经准备好的变量构造主题字符串:

$subject = "/C=${Country_Name}/ST=${State_Name}/L=${Locality_Name}/O=${Org_Name}/OU=${Org_Unit_Name}/CN=server.fqdn"
openssl req -new -key $key_out -sha256 -out $Pem_out -verify -newhdr -subj $subject