Creazione certificato autofirmato in PowerShell

In Windows 8/Windows server 2012 con PowerShell 3.0 è stata indrodotto il cmdlet New-SelfSignedCertificate che permette la creazione di un certificato autofirmato, nelle versioni successive di PowerShell tale cmdlet è stato migliorato con l’aggiunta di nuove opzioni.

In PowerShell 5.1 disponibile nativamente in Windows 10 Anniversary Edition/Windows Server 2016 il cmdlet permette la creazione semplice e completa di un certificato autofirmato diventando quindi una valida e semplice alternativa a OpenSSL per questo tipo di esigenza in ambiente Windows.

Di seguito un esempio di script che crea un certificato autofirmato con algoritimo RSA per la firma digitale di documenti e codice (a riguardo si vedano le opzioni KeyUsage e TextExtension).

Dopo aver creato il certificato nello store dell’utente lo script lo esporta in formato pfx e cer.

# Variabili di utilità
$timeStamp = Get-Date -format “yyyy-MM-dd-HH-mm-ss”

# Impostazioni di configurazione
$certFriendlyName = “Certificato Self-Signed per firma codice [$timeStamp]”
$certSubject = “O=DevAdmin,L=Cuneo,S=Cuneo,C=IT”
$certExportPath = “C:\Certs”
$certExportFileName = “DevAdminSelfSignedCodeSigning”
$certPFXPassword = “PassW0rd!”
$certDurationMonths = 36
$certKeyLength = 2048
$certHashAlgorithm = “SHA256”
$certKeyUsage = “DigitalSignature”
$certEnhancedKeyUsage = “2.5.29.37={text}1.3.6.1.5.5.7.3.3, 1.3.6.1.5.5.7.3.8”

# Creazione Certificato autofirmato
$cert=New-SelfSignedCertificate -FriendlyName $certFriendlyName -Subject $certSubject `
       -CertStoreLocation Cert:\CurrentUser\My `
       -NotAfter (Get-Date).AddMonths($certDurationMonths) `
       -KeyAlgorithm RSA -KeyLength $certKeyLength -HashAlgorithm $certHashAlgorithm `
       -KeyUsage $certKeyUsage `
       -TextExtension @(“$certEnhancedKeyUsage”) `
       -KeyExportPolicy Exportable `
       -Provider “Microsoft Enhanced RSA and AES Cryptographic Provider”

# Creazione directory per esportazione certificati
New-Item -Path $certExportPath -ItemType Directory -Force

# Export Certificato su file PFX con entire chain e tutte le external properties
$certPFXFilePath = Join-Path -Path $certExportPath -ChildPath ($certExportFileName + “.” +$timeStamp + “.pfx”)

$certPassword = ConvertTo-SecureString -String $certPFXPassword -Force –AsPlainText
Export-PfxCertificate -Cert $cert -FilePath $certPFXFilePath -Password $CertPassword

# Export Chiave pubblica su file CER
$certCERFilePath = Join-Path -Path $certExportPath -ChildPath ($certExportFileName + “.” +$timeStamp + “.cer”)
Export-Certificate -Cert $cert -FilePath $certCERFilePath

Si noti anche il parametro DNSName che permette di creare certificato SAN per uno più siti:

  • -DnsName www.devadmin.it
  • -DnsName www.devadmin.it, www.ictpower.it
  • -DnsName *.devadmin.it

Di seguito quanto riportato nella documentazione a riguardo del parametro DNSName:

“Specifies one or more DNS names to put into the subject alternative name extension of the certificate when a certificate to be copied is not specified via the CloneCert parameter. The first DNS name is also saved as the Subject Name. If no signing certificate is specified, the first DNS name is also saved as the Issuer Name.”

Si noti che i certificati autofirmati così generati non saranno ritenuti affidabili fino a che non vengono aggiunti anche ai certificati Trusted Root CA.

Per maggiori informazioni si vedano:

Lo script è disponibile sul mio repository GitHub al seguente link https://github.com/ermannog/PowerShell/tree/master/Create-SelfSignedCertificate.