使用PowerShell创建自签名证书

6
我正在使用类似于此处找到的代码创建用于IIS的自签名证书:http://blogs.technet.com/b/vishalagarwal/archive/2009/08/22/generating-a-certificate-self-signed-using-powershell-and-certenroll-interfaces.aspx。虽然它运行得很好,但我想给它一个友好名称,以便在我想要将证书分配给动态创建的站点时更容易找到它。
请问是否有人知道如何更改上述内容以设置友好名称(我已尝试了显而易见的方法,但没有成功)。
有没有更好的通过PowerShell创建证书的方法,而不需要提示用户提供信息?
以下是我使用的脚本的跟进 - 基于上面的url但被转化为cmdlet:
function Add-SelfSignedCertificate
{
    [CmdletBinding()]
    param
    (
            [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)]
            [Alias('cn')]
            [string]$CommonName
    )

    $name = new-object -com "X509Enrollment.CX500DistinguishedName.1"
    $name.Encode("CN=$CommonName", 0)

    $key = new-object -com "X509Enrollment.CX509PrivateKey.1"
    $key.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
    $key.KeySpec = 1
    $key.Length = 1024
    $key.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"
    $key.MachineContext = 1
    $key.Create()

    $serverauthoid = new-object -com "X509Enrollment.CObjectId.1"
    $serverauthoid.InitializeFromValue("1.3.6.1.5.5.7.3.1")
    $ekuoids = new-object -com "X509Enrollment.CObjectIds.1"
    $ekuoids.add($serverauthoid)
    $ekuext = new-object -com "X509Enrollment.CX509ExtensionEnhancedKeyUsage.1"
    $ekuext.InitializeEncode($ekuoids)

    $cert = new-object -com "X509Enrollment.CX509CertificateRequestCertificate.1"
    $cert.InitializeFromPrivateKey(2, $key, "")
    $cert.Subject = $name
    $cert.Issuer = $cert.Subject
    $cert.NotBefore = get-date
    $cert.NotAfter = $cert.NotBefore.AddDays(90)
    $cert.X509Extensions.Add($ekuext)
    $cert.Encode()

    $enrollment = new-object -com "X509Enrollment.CX509Enrollment.1"
    $enrollment.InitializeFromRequest($cert)
    $certdata = $enrollment.CreateRequest(0)
    $enrollment.InstallResponse(2, $certdata, 0, "")
}
3个回答

14

也许对您的具体用途没有帮助,但在Windows 8.1和Server 2012中安装了一个新的Powershell CmdLet,非常快速且易于使用:

New-SelfSignedCertificate [-CertStoreLocation <String> ] [-CloneCert <Certificate> ] [-DnsName <String> ] [-Confirm] [-WhatIf] [ <CommonParameters>]

更多细节可在此处找到:https://learn.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps

在我的使用中,证书的友好名称始终设置为CmdLet中指定的第一个DnsName。

以下是将证书放置在本地计算机个人存储区中的示例:

New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName www.example.com

注意:为了使此功能正常工作,必须以管理员权限启动 PowerShell。


3

您可以直接在代码中设置CertificateFriendlyName,只需要知道在哪里进行设置:

$enrollment.InitializeFromRequest($cert)
$enrollment.CertificateFriendlyName = 'whatever'
$certdata = $enrollment.CreateRequest(0)

$key有一个友好名称,但我没有看到它在任何地方显示,所以我认为它对你没有帮助。


如果有人想要查看完全可用的示例,可以在http://aka.ms/AD2AAD的脚本中找到PowerShell代码。 - MikeBaz - MSFT

0

Scott Hanselman写了一篇不错的博客文章,介绍如何使用SDK工具makecert.exe创建自签名证书。那个工具看起来比你提到的代码要容易得多。使用makecert.exe,您可以使用-n选项指定主题名称。我曾经使用该主题名称来引用其他工具(如signtool.exe)中的证书。虽然,我发现主题名称不必唯一,因此我倾向于使用似乎是唯一的Thumbprint值。Signtool还将接受Thumbprint(通过/ sha1参数)以标识证书。


这将被包含在InstallShield脚本中,因此makecert.exe显示的弹出对话框会带来问题。 - roderickprince
自签名证书工具selfssl.exe是否可用?http://www.robbagby.com/iis/self-signed-certificates-on-iis-7-the-easy-way-and-the-most-effective-way/ - Keith Hill
1
makecert.exe现已被弃用。 - Jonathan Allen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接