Private/Wissen/X_Technology/X74_PKI.ps1

<#
 
# PKI
 
Public Key Infrastructure
 
- **Hashtags** PKI Certificate PFX Private Public Key
- **Version** 2020.01.31
 
#>


# Enhanced Key Usage = 2.5.29.37
# Server Authentication = 1.3.6.1.5.5.7.3.1
# Client Authentication = 1.3.6.1.5.5.7.3.2
# Code Signing = 1.3.6.1.5.5.7.3.3
# Secure Email = 1.3.6.1.5.5.7.3.4
# IPSec End System = 1.3.6.1.5.5.7.3.5
# IPSec Tunnel = 1.3.6.1.5.5.7.3.6
# IPSec User = 1.3.6.1.5.5.7.3.7
# Timestamp Signing = 1.3.6.1.5.5.7.3.8
# OCSP Signing = 1.3.6.1.5.5.7.3.9
# Dokumentverschlüsselung = 1.3.6.1.4.1.311.80.1
# EFS CRYPTO = 1.3.6.1.4.1.311.10.3.4
# EFS RECOVERY = 1.3.6.1.4.1.311.10.3.4.1

$params = @{
    FriendlyName                = "AKPT Superman"
    Subject                     = "CN=_AKPT Superman (Doctor S. Man), E=s.man@krypton.universe"
    HashAlgorithm               = "SHA512"
    KeyAlgorithm                = "RSA"
    KeyLength                   = 4096
    KeyExportPolicy             = [Microsoft.CertificateServices.Commands.KeyExportPolicy]::ExportableEncrypted
    KeySpec                     = [Microsoft.CertificateServices.Commands.KeySpec]::KeyExchange
    CertStoreLocation           = "Cert:\CurrentUser\My"
    Type                        = [Microsoft.CertificateServices.Commands.CertificateType]::Custom
    TextExtension               = @("2.5.29.37={text}1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.3,1.3.6.1.5.5.7.3.4,1.3.6.1.5.5.7.3.5,1.3.6.1.5.5.7.3.6,1.3.6.1.5.5.7.3.7,1.3.6.1.5.5.7.3.8,1.3.6.1.5.5.7.3.9,1.3.6.1.4.1.311.80.1,1.3.6.1.4.1.311.10.3.4,1.3.6.1.4.1.311.10.3.4.1", `
                                    "2.5.29.17={text}email=s.man@krypton.universe&upn=s.man@krypton.universe")
    NotAfter                    = (Get-Date).AddYears(50)
    AlternateSignatureAlgorithm = $true
}
$myPfxCert = New-SelfSignedCertificate @params

$myPfxCert | Export-PfxCertificate -Password (Read-Host -AsSecureString) -FilePath c:\temp\crypt.pfx

Set-AuthenticodeSignature -Certificate $myPfxCert -FilePath C:\temp\SecureString.txt.ps1
Get-AuthenticodeSignature -FilePath C:\temp\SecureString.txt.ps1 | fl *

$cryptedText = "Hallo Würzburg!" | Protect-CmsMessage -To $myPfxCert
$cryptedText
$cryptedText | Unprotect-CmsMessage -To $myPfxCert




#--------------------------------------------------------------------------------------


# TIPP Siehe betroffene Cmdlets:
Get-Command -Name * -Module PKI


$PfxPassword = Read-Host -Prompt "Set PFX-Pwd" -AsSecureString

$params = @{
    FriendlyName                = "AKPT Superman"
    Subject                     = "CN=_AKPT Superman (Doctor S. Man), E=s.man@krypton.universe"
    HashAlgorithm               = "SHA512"
    KeyAlgorithm                = "RSA"
    KeyLength                   = 4096
    KeyExportPolicy             = [Microsoft.CertificateServices.Commands.KeyExportPolicy]::ExportableEncrypted
    KeySpec                     = [Microsoft.CertificateServices.Commands.KeySpec]::KeyExchange
    CertStoreLocation           = "Cert:\CurrentUser\My"
    Type                        = [Microsoft.CertificateServices.Commands.CertificateType]::Custom
    TextExtension               = @("2.5.29.37={text}1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.3,1.3.6.1.5.5.7.3.4,1.3.6.1.5.5.7.3.5,1.3.6.1.5.5.7.3.6,1.3.6.1.5.5.7.3.7,1.3.6.1.5.5.7.3.8,1.3.6.1.5.5.7.3.9,1.3.6.1.4.1.311.80.1,1.3.6.1.4.1.311.10.3.4,1.3.6.1.4.1.311.10.3.4.1", `
                                    "2.5.29.17={text}email=s.man@krypton.universe&upn=s.man@krypton.universe")
    NotAfter                    = (Get-Date).AddYears(50)
    AlternateSignatureAlgorithm = $true
}
$myPfxCert = New-SelfSignedCertificate @params
$myPfxCert | Export-PfxCertificate -Password $PfxPassword -FilePath c:\temp\Superman.pfx
$myPfxCert | Export-Certificate -Type CERT -Force -FilePath "C:\Temp\Superman.cer"

Get-ChildItem -Path "Cert:\" -Recurse | 
    Where-Object Subject -Like "*s.man@krypton.universe" | 
    Remove-Item


Import-Certificate -FilePath "C:\Temp\Superman.cer" -CertStoreLocation "Cert:\LocalMachine\Root"

Import-PfxCertificate -FilePath "C:\Temp\Superman.pfx" -CertStoreLocation "Cert:\CurrentUser\My" -Password $PfxPassword

Get-PfxData -Password $PfxPassword -FilePath "C:\Temp\Superman.pfx" | Format-List -Property *


Test-Certificate -Cert $myPfxCert -Policy SSL
Test-Certificate -Cert $myPfxCert -EKU "1.3.6.1.5.5.7.3.1"

# TODO:
# Erstellt eine neue Aufgabe im Aufgabenplaner, die ausgelöst wird, wenn ein Zertifikat ersetzt wird, abgelaufen ist oder kurz vor dem Ablauf steht. New-CertificateNotificationTask
# Übermittelt eine Zertifikatanforderung an einen Registrierungsserver und installiert die Antwort oder ruft ein Zertifikat für eine zuvor übermittelte Anforderung ab. Get-Certificate
# Legt die Richtlinie für die automatische Registrierung lokaler Zertifikate fest. Set-CertificateAutoEnrollmentPolicy
Set-CertificateAutoEnrollmentPolicy -PolicyState Enabled -ExpirationPercentage 90 -EnableMyStoreManagement -EnableBalloonNotifications -context User

Get-Certificate


Get-ChildItem -Path "C:\Temp" | Where-Object Extension -In ".cer", ".pfx" | Remove-Item