Public/New-RootCA.ps1
<# .SYNOPSIS This function configures the target Windows 2012 R2 or Windows 2016 Server to be a new Enterprise Root Certification Authority. .DESCRIPTION See .SYNOPSIS .NOTES .PARAMETER DomainAdminCredentials This parameter is MANDATORY. This parameter takes a PSCredential. The Domain Admin Credentials will be used to configure the new Root CA. This means that the Domain Account provided MUST be a member of the following Security Groups in Active Directory: - Domain Admins - Domain Users - Enterprise Admins - Group Policy Creator Owners - Schema Admins .PARAMETER RootCAIPOrFQDN This parameter is OPTIONAL. This parameter takes a string that represents an IPv4 address or DNS-Resolveable FQDN that refers to the target Windows Server that will become the new Enterprise Root CA. If it is NOT used, then the localhost will be configured as the new Enterprise Root CA. .PARAMETER CAType This parameter is OPTIONAL, however, its default value is "EnterpriseRootCA". This parameter takes a string that represents the type of Root Certificate Authority that the target server will become. Currently this parameter only accepts "EnterpriseRootCA" as a valid value. But in the future, "StandaloneRootCA" will also become a valid value. .PARAMETER NewComputerTemplateCommonName This parameter is OPTIONAL, however, its default value is "<DomainPrefix>" + "Computer". This parameter takes a string that represents the desired Common Name for the new custom Computer (Machine) Certificate Template. This updates some undesirable defaults that come with the default Computer (Machine) Certificate Template. .PARAMETER NewWebServerTemplateCommonName This parameter is OPTIONAL, however, its default value is "<DomainPrefix>" + "WebServer". This parameter takes a string that represents the desired Common Name for the new custom WebServer Certificate Template. This updates some undesirable defaults that come with the default WebServer Certificate Template. .PARAMETER FileOutputDirectory This parameter is OPTIONAL, however, its default value is "C:\NewRootCAOutput". This parameter takes a string that represents the full path to a directory that will contain all files generated by the New-RootCA function. IMPORTANT NOTE: This directory will be made available to the network (it will become an SMB Share) so that the Subordinate Certificate Authority can download needed files. This SMB share will only be available TEMPORARILY. It will NOT survive a reboot. .PARAMETER CryptoProvider This parameter is OPTIONAL, however, its default value is "Microsoft Software Key Storage Provider". This parameter takes a string that represents the Cryptographic Provider used by the new Root CA. Currently, the only valid value for this parameter is "Microsoft Software Key Storage Provider". .PARAMETER KeyLength This parameter is OPTIONAL, however, its default value is 2048. This parameter takes an integer with value 2048 or 4096. .PARAMETER HashAlgorithm This parameter is OPTIONAL, however, its default value is SHA256. This parameter takes a string with acceptable values as follows: "SHA1","SHA256","SHA384","SHA512","MD5","MD4","MD2" .PARAMETER KeyAlgorithmValue This parameter is OPTIONAL, however, its default value is RSA. This parameter takes a string with acceptable values: "RSA" .PARAMETER CDPUrl This parameter is OPTIONAL, however, its default value is "http://pki.$DomainName/certdata/<CaName><CRLNameSuffix>.crl" This parameter takes a string that represents a Certificate Distribution List Revocation URL. The current default configuration does not make this Url active, however, it still needs to be configured. .PARAMETER AIAUrl This parameter is OPTIONAL, however, its default value is "http://pki.$DomainName/certdata/<CaName><CertificateName>.crt" This parameter takes a string that represents an Authority Information Access (AIA) Url (i.e. the location where the certificate of of certificate's issuer can be downloaded). The current default configuration does not mahe this Url active, but it still needs to be configured. .EXAMPLE # Make the localhost a Root CA PS C:\Users\zeroadmin> $DomainAdminCreds = [pscredential]::new("alpha\alphaadmin",$(Read-Host 'Enter Passsword' -AsSecureString)) Enter Passsword: ************ PS C:\Users\zeroadmin> $CreateRootCASplatParams = @{ >> DomainAdminCredentials = $DomainAdminCreds >> } PS C:\Users\zeroadmin> $CreateRootCAResult = Create-RootCA @CreateRootCASplatParams .EXAMPLE # Make the Remote Host a Root CA PS C:\Users\zeroadmin> $DomainAdminCreds = [pscredential]::new("alpha\alphaadmin",$(Read-Host 'Enter Passsword' -AsSecureString)) Enter Passsword: ************ PS C:\Users\zeroadmin> $CreateRootCASplatParams = @{ >> DomainAdminCredentials = $DomainAdminCreds >> RootCAIPOrFQDN = "192.168.2.112" >> } PS C:\Users\zeroadmin> $CreateRootCAResult = Create-RootCA @CreateRootCASplatParams #> function New-RootCA { [CmdletBinding()] param ( [Parameter(Mandatory=$True)] [pscredential]$DomainAdminCredentials, [Parameter(Mandatory=$False)] [string]$RootCAIPOrFQDN, [Parameter(Mandatory=$False)] #[ValidateSet("EnterpriseRootCa","StandaloneRootCa")] [ValidateSet("EnterpriseRootCA")] [string]$CAType, [Parameter(Mandatory=$False)] [string]$NewComputerTemplateCommonName, [Parameter(Mandatory=$False)] [string]$NewWebServerTemplateCommonName, [Parameter(Mandatory=$False)] [string]$FileOutputDirectory, [Parameter(Mandatory=$False)] <# [ValidateSet("Microsoft Base Cryptographic Provider v1.0","Microsoft Base DSS and Diffie-Hellman Cryptographic Provider", "Microsoft Base DSS Cryptographic Provider","Microsoft Base Smart Card Crypto Provider", "Microsoft DH SChannel Cryptographic Provider","Microsoft Enhanced Cryptographic Provider v1.0", "Microsoft Enhanced DSS and Diffie-Hellman Cryptographic Provider", "Microsoft Enhanced RSA and AES Cryptographic Provider","Microsoft RSA SChannel Cryptographic Provider", "Microsoft Strong Cryptographic Provider","Microsoft Software Key Storage Provider", "Microsoft Passport Key Storage Provider")] #> [ValidateSet("Microsoft Software Key Storage Provider")] [string]$CryptoProvider, [Parameter(Mandatory=$False)] [ValidateSet("2048","4096")] [int]$KeyLength, [Parameter(Mandatory=$False)] [ValidateSet("SHA1","SHA256","SHA384","SHA512","MD5","MD4","MD2")] [string]$HashAlgorithm, # For now, stick to just using RSA [Parameter(Mandatory=$False)] #[ValidateSet("RSA","DH","DSA","ECDH_P256","ECDH_P521","ECDSA_P256","ECDSA_P384","ECDSA_P521")] [ValidateSet("RSA")] [string]$KeyAlgorithmValue, [Parameter(Mandatory=$False)] [ValidatePattern('http.*?\/<CaName><CRLNameSuffix>\.crl$')] [string]$CDPUrl, [Parameter(Mandatory=$False)] [ValidatePattern('http.*?\/<CaName><CertificateName>.crt$')] [string]$AIAUrl ) #region >> Helper Functions # NewUniqueString # TestIsValidIPAddress # ResolveHost # GetDomainController function SetupRootCA { [CmdletBinding()] param ( [Parameter(Mandatory=$True)] [pscredential]$DomainAdminCredentials, [Parameter(Mandatory=$True)] [System.Collections.ArrayList]$NetworkInfoPSObjects, [Parameter(Mandatory=$True)] [ValidateSet("EnterpriseRootCA")] [string]$CAType, [Parameter(Mandatory=$True)] [string]$NewComputerTemplateCommonName, [Parameter(Mandatory=$True)] [string]$NewWebServerTemplateCommonName, [Parameter(Mandatory=$True)] [string]$FileOutputDirectory, [Parameter(Mandatory=$True)] [ValidateSet("Microsoft Software Key Storage Provider")] [string]$CryptoProvider, [Parameter(Mandatory=$True)] [ValidateSet("2048","4096")] [int]$KeyLength, [Parameter(Mandatory=$True)] [ValidateSet("SHA1","SHA256","SHA384","SHA512","MD5","MD4","MD2")] [string]$HashAlgorithm, [Parameter(Mandatory=$True)] [ValidateSet("RSA")] [string]$KeyAlgorithmValue, [Parameter(Mandatory=$True)] [ValidatePattern('http.*?\/<CaName><CRLNameSuffix>\.crl$')] [string]$CDPUrl, [Parameter(Mandatory=$True)] [ValidatePattern('http.*?\/<CaName><CertificateName>.crt$')] [string]$AIAUrl ) #region >> Prep # Make sure we can find the Domain Controller(s) try { $DomainControllerInfo = GetDomainController -Domain $(Get-CimInstance win32_computersystem).Domain -WarningAction SilentlyContinue if (!$DomainControllerInfo -or $DomainControllerInfo.PrimaryDomainController -eq $null) {throw "Unable to find Primary Domain Controller! Halting!"} } catch { Write-Error $_ $global:FunctionResult = "1" return } # Make sure time is synchronized with NTP Servers/Domain Controllers (i.e. might be using NT5DS instead of NTP) # See: https://giritharan.com/time-synchronization-in-active-directory-domain/ $null = W32tm /resync /rediscover /nowait if (!$FileOutputDirectory) { $FileOutputDirectory = "C:\NewRootCAOutput" } if (!$(Test-Path $FileOutputDirectory)) { $null = New-Item -ItemType Directory -Path $FileOutputDirectory } try { Import-Module PSPKI -ErrorAction Stop } catch { try { $null = Install-PackageProvider -Name Nuget -Force -Confirm:$False $null = Set-PSRepository -Name PSGallery -InstallationPolicy Trusted Install-Module PSPKI -ErrorAction Stop -WarningAction SilentlyContinue Import-Module PSPKI -ErrorAction Stop } catch { Write-Error $_ $global:FunctionResult = "1" return } } try { Import-Module ServerManager -ErrorAction Stop } catch { Write-Error "Problem importing the ServerManager Module! Halting!" $global:FunctionResult = "1" return } $WindowsFeaturesToAdd = @( "Adcs-Cert-Authority" "RSAT-AD-Tools" ) foreach ($FeatureName in $WindowsFeaturesToAdd) { $SplatParams = @{ Name = $FeatureName } if ($FeatureName -eq "Adcs-Cert-Authority") { $SplatParams.Add("IncludeManagementTools",$True) } try { $null = Add-WindowsFeature @SplatParams } catch { Write-Error "Problem with 'Add-WindowsFeature $FeatureName'! Halting!" $global:FunctionResult = "1" return } } $RelevantRootCANetworkInfo = $NetworkInfoPSObjects | Where-Object {$_.ServerPurpose -eq "RootCA"} # Make sure WinRM in Enabled and Running on $env:ComputerName try { $null = Enable-PSRemoting -Force -ErrorAction Stop } catch { $null = Get-NetConnectionProfile | Where-Object {$_.NetworkCategory -eq 'Public'} | Set-NetConnectionProfile -NetworkCategory 'Private' try { $null = Enable-PSRemoting -Force } catch { Write-Error $_ Write-Error "Problem with Enabble-PSRemoting WinRM Quick Config! Halting!" $global:FunctionResult = "1" return } } # If $env:ComputerName is not part of a Domain, we need to add this registry entry to make sure WinRM works as expected if (!$(Get-CimInstance Win32_Computersystem).PartOfDomain) { $null = reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f } # Add the New Server's IP Addresses to $env:ComputerName's TrustedHosts $CurrentTrustedHosts = $(Get-Item WSMan:\localhost\Client\TrustedHosts).Value [System.Collections.ArrayList][array]$CurrentTrustedHostsAsArray = $CurrentTrustedHosts -split ',' $ItemsToAddToWSMANTrustedHosts = @( $RelevantRootCANetworkInfo.FQDN $RelevantRootCANetworkInfo.HostName $RelevantRootCANetworkInfo.IPAddress ) foreach ($NetItem in $ItemsToAddToWSMANTrustedHosts) { if ($CurrentTrustedHostsAsArray -notcontains $NetItem) { $null = $CurrentTrustedHostsAsArray.Add($NetItem) } } $UpdatedTrustedHostsString = $($CurrentTrustedHostsAsArray | Where-Object {![string]::IsNullOrWhiteSpace($_)}) -join ',' Set-Item WSMan:\localhost\Client\TrustedHosts $UpdatedTrustedHostsString -Force #endregion >> Prep #region >> Install ADCSCA try { $FinalCryptoProvider = $KeyAlgorithmValue + "#" + $CryptoProvider $InstallADCSCertAuthSplatParams = @{ Credential = $DomainAdminCredentials CAType = $CAType CryptoProviderName = $FinalCryptoProvider KeyLength = $KeyLength HashAlgorithmName = $HashAlgorithm CACommonName = $env:ComputerName CADistinguishedNameSuffix = $RelevantRootCANetworkInfo.DomainLDAPString DatabaseDirectory = $(Join-Path $env:SystemRoot "System32\CertLog") ValidityPeriod = "years" ValidityPeriodUnits = 20 Force = $True ErrorAction = "Stop" } $null = Install-AdcsCertificationAuthority @InstallADCSCertAuthSplatParams } catch { Write-Error $_ Write-Error "Problem with Install-AdcsCertificationAuthority cmdlet! Halting!" $global:FunctionResult = "1" return } try { $null = certutil -setreg CA\\CRLPeriod "Years" $null = certutil -setreg CA\\CRLPeriodUnits 1 $null = certutil -setreg CA\\CRLOverlapPeriod "Days" $null = certutil -setreg CA\\CRLOverlapUnits 7 Write-Host "Done initial certutil commands..." # Update the Local CDP $LocalCDP = (Get-CACrlDistributionPoint)[0] $null = $LocalCDP | Remove-CACrlDistributionPoint -Force $LocalCDP.PublishDeltaToServer = $false $null = $LocalCDP | Add-CACrlDistributionPoint -Force # Remove pre-existing ldap/http CDPs, add custom CDP $null = Get-CACrlDistributionPoint | Where-Object { $_.URI -like "http*" -or $_.Uri -like "ldap*" } | Remove-CACrlDistributionPoint -Force $null = Add-CACrlDistributionPoint -Uri $CDPUrl -AddToCertificateCdp -Force # Remove pre-existing ldap/http AIAs, add custom AIA $null = Get-CAAuthorityInformationAccess | Where-Object { $_.Uri -like "http*" -or $_.Uri -like "ldap*" } | Remove-CAAuthorityInformationAccess -Force $null = Add-CAAuthorityInformationAccess -Uri $AIAUrl -AddToCertificateAIA -Force Write-Host "Done CDP and AIA cmdlets..." # Enable all event auditing $null = certutil -setreg CA\\AuditFilter 127 Write-Host "Done final certutil command..." } catch { Write-Error $_ $global:FunctionResult = "1" return } try { $null = Restart-Service certsvc -ErrorAction Stop } catch { Write-Error $_ Write-Error "Problem with 'Restart-Service certsvc'! Halting!" $global:FunctionResult = "1" return } while ($(Get-Service certsvc).Status -ne "Running") { Write-Host "Waiting for the 'certsvc' service to start..." Start-Sleep -Seconds 5 } #endregion >> Install ADCSCA #region >> New Computer/Machine Template Write-Host "Creating new Machine Certificate Template..." while (!$WebServTempl -or !$ComputerTempl) { $ConfigContext = $([ADSI]"LDAP://RootDSE").ConfigurationNamingContext $LDAPLocation = "LDAP://CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext" $ADSI = New-Object System.DirectoryServices.DirectoryEntry($LDAPLocation,$DomainAdminCredentials.UserName,$($DomainAdminCredentials.GetNetworkCredential().Password),"Secure") $WebServTempl = $ADSI.psbase.children | Where-Object {$_.distinguishedName -match "CN=WebServer,"} $ComputerTempl = $ADSI.psbase.children | Where-Object {$_.distinguishedName -match "CN=Machine,"} Write-Host "Waiting for Active Directory 'LDAP://CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext' to contain default Machine/Computer and WebServer Certificate Templates..." Start-Sleep -Seconds 15 } $OIDRandComp = (Get-Random -Maximum 999999999999999).tostring('d15') $OIDRandComp = $OIDRandComp.Insert(8,'.') $CompOIDValue = $ComputerTempl.'msPKI-Cert-Template-OID' $NewCompTemplOID = $CompOIDValue.subString(0,$CompOIDValue.length-4)+$OIDRandComp $NewCompTempl = $ADSI.Create("pKICertificateTemplate","CN=$NewComputerTemplateCommonName") $NewCompTempl.put("distinguishedName","CN=$NewComputerTemplateCommonName,CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext") $NewCompTempl.put("flags","131680") $NewCompTempl.put("displayName","$NewComputerTemplateCommonName") $NewCompTempl.put("revision","100") $NewCompTempl.put("pKIDefaultKeySpec","1") $NewCompTempl.put("pKIMaxIssuingDepth","0") $pkiCritExt = "2.5.29.17","2.5.29.15" $NewCompTempl.put("pKICriticalExtensions",$pkiCritExt) $ExtKeyUse = "1.3.6.1.5.5.7.3.1","1.3.6.1.5.5.7.3.2" $NewCompTempl.put("pKIExtendedKeyUsage",$ExtKeyUse) $NewCompTempl.put("pKIDefaultCSPs","1,Microsoft RSA SChannel Cryptographic Provider") $NewCompTempl.put("msPKI-RA-Signature","0") $NewCompTempl.put("msPKI-Enrollment-Flag","0") $NewCompTempl.put("msPKI-Private-Key-Flag","0") # Used to be "50659328" $NewCompTempl.put("msPKI-Certificate-Name-Flag","1") $NewCompTempl.put("msPKI-Minimal-Key-Size","2048") $NewCompTempl.put("msPKI-Template-Schema-Version","2") # This needs to be either "1" or "2" for it to show up in the ADCS Website dropdown $NewCompTempl.put("msPKI-Template-Minor-Revision","2") $NewCompTempl.put("msPKI-Cert-Template-OID","$NewCompTemplOID") $AppPol = "1.3.6.1.5.5.7.3.1","1.3.6.1.5.5.7.3.2" $NewCompTempl.put("msPKI-Certificate-Application-Policy",$AppPol) $NewCompTempl.Setinfo() # Get the last few attributes from the existing default "CN=Machine" Certificate Template $NewCompTempl.pKIOverlapPeriod = $ComputerTempl.pKIOverlapPeriod # Used to be $WebServTempl.pKIOverlapPeriod $NewCompTempl.pKIKeyUsage = $ComputerTempl.pKIKeyUsage # Used to be $WebServTempl.pKIKeyUsage $NewCompTempl.pKIExpirationPeriod = $ComputerTempl.pKIExpirationPeriod # Used to be $WebServTempl.pKIExpirationPeriod $NewCompTempl.Setinfo() # Set Access Rights / Permissions on the $NewCompTempl LDAP object $AdObj = New-Object System.Security.Principal.NTAccount("Domain Computers") $identity = $AdObj.Translate([System.Security.Principal.SecurityIdentifier]) $adRights = "ExtendedRight" $type = "Allow" $ACE = New-Object System.DirectoryServices.ActiveDirectoryAccessRule($identity,$adRights,$type) $NewCompTempl.psbase.ObjectSecurity.SetAccessRule($ACE) $NewCompTempl.psbase.commitchanges() #endregion >> New Computer/Machine Template #region >> New WebServer Template Write-Host "Creating new WebServer Certificate Template..." $OIDRandWebServ = (Get-Random -Maximum 999999999999999).tostring('d15') $OIDRandWebServ = $OIDRandWebServ.Insert(8,'.') $WebServOIDValue = $WebServTempl.'msPKI-Cert-Template-OID' $NewWebServTemplOID = $WebServOIDValue.subString(0,$WebServOIDValue.length-4)+$OIDRandWebServ $NewWebServTempl = $ADSI.Create("pKICertificateTemplate", "CN=$NewWebServerTemplateCommonName") $NewWebServTempl.put("distinguishedName","CN=$NewWebServerTemplateCommonName,CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext") $NewWebServTempl.put("flags","131649") $NewWebServTempl.put("displayName","$NewWebServerTemplateCommonName") $NewWebServTempl.put("revision","100") $NewWebServTempl.put("pKIDefaultKeySpec","1") $NewWebServTempl.put("pKIMaxIssuingDepth","0") $pkiCritExt = "2.5.29.15" $NewWebServTempl.put("pKICriticalExtensions",$pkiCritExt) $ExtKeyUse = "1.3.6.1.5.5.7.3.1","1.3.6.1.5.5.7.3.2" $NewWebServTempl.put("pKIExtendedKeyUsage",$ExtKeyUse) $pkiCSP = "1,Microsoft RSA SChannel Cryptographic Provider","2,Microsoft DH SChannel Cryptographic Provider" $NewWebServTempl.put("pKIDefaultCSPs",$pkiCSP) $NewWebServTempl.put("msPKI-RA-Signature","0") $NewWebServTempl.put("msPKI-Enrollment-Flag","0") $NewWebServTempl.put("msPKI-Private-Key-Flag","0") # Used to be "16842752" $NewWebServTempl.put("msPKI-Certificate-Name-Flag","1") $NewWebServTempl.put("msPKI-Minimal-Key-Size","2048") $NewWebServTempl.put("msPKI-Template-Schema-Version","2") # This needs to be either "1" or "2" for it to show up in the ADCS Website dropdown $NewWebServTempl.put("msPKI-Template-Minor-Revision","2") $NewWebServTempl.put("msPKI-Cert-Template-OID","$NewWebServTemplOID") $AppPol = "1.3.6.1.5.5.7.3.1","1.3.6.1.5.5.7.3.2" $NewWebServTempl.put("msPKI-Certificate-Application-Policy",$AppPol) $NewWebServTempl.Setinfo() # Get the last few attributes from the existing default "CN=WebServer" Certificate Template $NewWebServTempl.pKIOverlapPeriod = $WebServTempl.pKIOverlapPeriod $NewWebServTempl.pKIKeyUsage = $WebServTempl.pKIKeyUsage $NewWebServTempl.pKIExpirationPeriod = $WebServTempl.pKIExpirationPeriod $NewWebServTempl.Setinfo() #endregion >> New WebServer Template #region >> Finish Up # Add the newly created custom Computer and WebServer Certificate Templates to List of Certificate Templates to Issue # For this to be (relatively) painless, we need the following PSPKI Module cmdlets $null = Get-CertificationAuthority -Name $env:ComputerName | Get-CATemplate | Add-CATemplate -Name $NewComputerTemplateCommonName | Set-CATemplate $null = Get-CertificationAuthority -Name $env:ComputerName | Get-CATemplate | Add-CATemplate -Name $NewWebServerTemplateCommonName | Set-CATemplate # Export New Certificate Templates to NewCert-Templates Directory $ldifdeUserName = $($DomainAdminCredentials.UserName -split "\\")[-1] $ldifdeDomain = $RelevantRootCANetworkInfo.DomainName $ldifdePwd = $DomainAdminCredentials.GetNetworkCredential().Password $null = ldifde -m -v -b $ldifdeUserName $ldifdeDomain $ldifdePwd -d "CN=$NewComputerTemplateCommonName,CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext" -f "$FileOutputDirectory\$NewComputerTemplateCommonName.ldf" $null = ldifde -m -v -b $ldifdeUserName $ldifdeDomain $ldifdePwd -d "CN=$NewWebServerTemplateCommonName,CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigContext" -f "$FileOutputDirectory\$NewWebServerTemplateCommonName.ldf" # Side Note: You can import Certificate Templates on another Certificate Authority via ldife.exe with: <# ldifde -i -k -f "$FileOutputDirectory\$NewComputerTemplateCommonName.ldf" ldifde -i -k -f "$FileOutputDirectory\$NewWebServerTemplateCommonName.ldf" #> # Generate New CRL and Copy Contents of CertEnroll to $FileOutputDirectory # NOTE: The below 'certutil -crl' outputs the new .crl file to "C:\Windows\System32\CertSrv\CertEnroll" # which happens to contain some other important files that we'll need $null = certutil -crl Copy-Item -Path "C:\Windows\System32\CertSrv\CertEnroll\*" -Recurse -Destination $FileOutputDirectory -Force # Convert RootCA .crt DER Certificate to Base64 Just in Case You Want to Use With Linux $CrtFileItem = Get-ChildItem -Path $FileOutputDirectory -File -Recurse | Where-Object {$_.Name -match "$env:ComputerName\.crt"} $null = certutil -encode $($CrtFileItem.FullName) $($CrtFileItem.FullName -replace '\.crt','_base64.cer') # Make $FileOutputDirectory a Network Share until the Subordinate CA can download the files # IMPORTANT NOTE: The below -CATimeout parameter should be in Seconds. So after 12000 seconds, the SMB Share # will no longer be available # IMPORTANT NOTE: The below -Temporary switch means that the SMB Share will NOT survive a reboot $null = New-SMBShare -Name RootCAFiles -Path $FileOutputDirectory -CATimeout 12000 -Temporary # Now the SMB Share should be available $RootCASMBShareFQDNLocation = '\\' + $RelevantRootCANetworkInfo.FQDN + "\RootCAFiles" $RootCASMBShareIPLocation = '\\' + $RelevantRootCANetworkInfo.IPAddress + "\RootCAFiles" Write-Host "Successfully configured Root Certificate Authority" -ForegroundColor Green Write-Host "RootCA Files needed by the new Subordinate/Issuing/Intermediate CA Server(s) are now TEMPORARILY available at SMB Share located:`n$RootCASMBShareFQDNLocation`nOR`n$RootCASMBShareIPLocation" -ForegroundColor Green #endregion >> Finish Up [pscustomobject] @{ SMBShareIPLocation = $RootCASMBShareIPLocation SMBShareFQDNLocation = $RootCASMBShareFQDNLocation } } #endregion >> Helper Functions #region >> Initial Prep $ElevationCheck = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) if (!$ElevationCheck) { Write-Error "You must run the build.ps1 as an Administrator (i.e. elevated PowerShell Session)! Halting!" $global:FunctionResult = "1" return } $NextHop = $(Get-NetRoute -AddressFamily IPv4 | Where-Object {$_.NextHop -ne "0.0.0.0"} | Sort-Object RouteMetric)[0].NextHop $PrimaryIP = $(Find-NetRoute -RemoteIPAddress $NextHop | Where-Object {$($_ | Get-Member).Name -contains "IPAddress"}).IPAddress [System.Collections.ArrayList]$NetworkLocationObjsToResolve = @() if ($PSBoundParameters['RootCAIPOrFQDN']) { $RootCAPSObj = [pscustomobject]@{ ServerPurpose = "RootCA" NetworkLocation = $RootCAIPOrFQDN } } else { $RootCAPSObj = [pscustomobject]@{ ServerPurpose = "RootCA" NetworkLocation = $env:ComputerName + "." + $(Get-CimInstance win32_computersystem).Domain } } $null = $NetworkLocationObjsToResolve.Add($RootCAPSObj) [System.Collections.ArrayList]$NetworkInfoPSObjects = @() foreach ($NetworkLocationObj in $NetworkLocationObjsToResolve) { if ($($NetworkLocation -split "\.")[0] -ne $env:ComputerName -and $NetworkLocation -ne $PrimaryIP -and $NetworkLocation -ne "$env:ComputerName.$($(Get-CimInstance win32_computersystem).Domain)" ) { try { $NetworkInfo = ResolveHost -HostNameOrIP $NetworkLocationObj.NetworkLocation $DomainName = $NetworkInfo.Domain $FQDN = $NetworkInfo.FQDN $IPAddr = $NetworkInfo.IPAddressList[0] $DomainShortName = $($DomainName -split "\.")[0] $DomainLDAPString = $(foreach ($StringPart in $($DomainName -split "\.")) {"DC=$StringPart"}) -join ',' if (!$NetworkInfo -or $DomainName -eq "Unknown" -or !$DomainName -or $FQDN -eq "Unknown" -or !$FQDN) { throw "Unable to gather Domain Name and/or FQDN info about '$NetworkLocation'! Please check DNS. Halting!" } } catch { Write-Error $_ $global:FunctionResult = "1" return } # Make sure WinRM in Enabled and Running on $env:ComputerName try { $null = Enable-PSRemoting -Force -ErrorAction Stop } catch { $null = Get-NetConnectionProfile | Where-Object {$_.NetworkCategory -eq 'Public'} | Set-NetConnectionProfile -NetworkCategory 'Private' try { $null = Enable-PSRemoting -Force } catch { Write-Error $_ Write-Error "Problem with Enabble-PSRemoting WinRM Quick Config! Halting!" $global:FunctionResult = "1" return } } # If $env:ComputerName is not part of a Domain, we need to add this registry entry to make sure WinRM works as expected if (!$(Get-CimInstance Win32_Computersystem).PartOfDomain) { $null = reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f } # Add the New Server's IP Addresses to $env:ComputerName's TrustedHosts $CurrentTrustedHosts = $(Get-Item WSMan:\localhost\Client\TrustedHosts).Value [System.Collections.ArrayList][array]$CurrentTrustedHostsAsArray = $CurrentTrustedHosts -split ',' $ItemsToAddToWSMANTrustedHosts = @($IPAddr,$FQDN,$($($FQDN -split "\.")[0])) foreach ($NetItem in $ItemsToAddToWSMANTrustedHosts) { if ($CurrentTrustedHostsAsArray -notcontains $NetItem) { $null = $CurrentTrustedHostsAsArray.Add($NetItem) } } $UpdatedTrustedHostsString = $($CurrentTrustedHostsAsArray | Where-Object {![string]::IsNullOrWhiteSpace($_)}) -join ',' Set-Item WSMan:\localhost\Client\TrustedHosts $UpdatedTrustedHostsString -Force } else { $DomainName = $(Get-CimInstance win32_computersystem).Domain $DomainShortName = $($DomainName -split "\.")[0] $DomainLDAPString = $(foreach ($StringPart in $($DomainName -split "\.")) {"DC=$StringPart"}) -join ',' $FQDN = $env:ComputerName + '.' + $DomainName $IPAddr = $PrimaryIP } $PSObj = [pscustomobject]@{ ServerPurpose = $NetworkLocationObj.ServerPurpose FQDN = $FQDN HostName = $($FQDN -split "\.")[0] IPAddress = $IPAddr DomainName = $DomainName DomainShortName = $DomainShortName DomainLDAPString = $DomainLDAPString } $null = $NetworkInfoPSObjects.Add($PSObj) } $RelevantRootCANetworkInfo = $NetworkInfoPSObjects | Where-Object {$_.ServerPurpose -eq "RootCA"} # Set some defaults if certain paramters are not used if (!$CAType) { $CAType = "EnterpriseRootCA" } if (!$NewComputerTemplateCommonName) { $NewComputerTemplateCommonName = $DomainShortName + "Computer" #$NewComputerTemplateCommonName = "Machine" } if (!$NewWebServerTemplateCommonName) { $NewWebServerTemplateCommonName = $DomainShortName + "WebServer" #$NewWebServerTemplateCommonName = "WebServer" } if (!$FileOutputDirectory) { $FileOutputDirectory = "C:\NewRootCAOutput" } if (!$CryptoProvider) { $CryptoProvider = "Microsoft Software Key Storage Provider" } if (!$KeyLength) { $KeyLength = 2048 } if (!$HashAlgorithm) { $HashAlgorithm = "SHA256" } if (!$KeyAlgorithmValue) { $KeyAlgorithmValue = "RSA" } if (!$CDPUrl) { $CDPUrl = "http://pki.$($RelevantRootCANetworkInfo.DomainName)/certdata/<CaName><CRLNameSuffix>.crl" } if (!$AIAUrl) { $AIAUrl = "http://pki.$($RelevantRootCANetworkInfo.DomainName)/certdata/<CaName><CertificateName>.crt" } # Create SetupRootCA Helper Function Splat Parameters $SetupRootCASplatParams = @{ DomainAdminCredentials = $DomainAdminCredentials NetworkInfoPSObjects = $NetworkInfoPSObjects CAType = $CAType NewComputerTemplateCommonName = $NewComputerTemplateCommonName NewWebServerTemplateCommonName = $NewWebServerTemplateCommonName FileOutputDirectory = $FileOutputDirectory CryptoProvider = $CryptoProvider KeyLength = $KeyLength HashAlgorithm = $HashAlgorithm KeyAlgorithmValue = $KeyAlgorithmValue CDPUrl = $CDPUrl AIAUrl = $AIAUrl } # Install any required PowerShell Modules... [array]$NeededModules = @( "PSPKI" ) $null = Install-PackageProvider -Name Nuget -Force -Confirm:$False $null = Set-PSRepository -Name PSGallery -InstallationPolicy Trusted [System.Collections.ArrayList]$FailedModuleInstall = @() foreach ($ModuleResource in $NeededModules) { try { $null = Install-Module $ModuleResource -ErrorAction Stop } catch { Write-Error $_ $null = $FailedModuleInstall.Add($ModuleResource) continue } } if ($FailedModuleInstall.Count -gt 0) { Write-Error "Problem installing the following DSC Modules:`n$($FailedModuleInstall -join "`n")" $global:FunctionResult = "1" return } #endregion >> Initial Prep #region >> Do RootCA Install if ($RelevantRootCANetworkInfo.HostName -ne $env:ComputerName) { $PSSessionName = NewUniqueString -ArrayOfStrings $(Get-PSSession).Name -PossibleNewUniqueString "ToRootCA" # Try to create a PSSession to the Root CA for 15 minutes, then give up $Counter = 0 while (![bool]$(Get-PSSession -Name $PSSessionName -ErrorAction SilentlyContinue)) { try { $RootCAPSSession = New-PSSession -ComputerName $RelevantRootCANetworkInfo.IPAddress -Credential $DomainAdminCredentials -Name $PSSessionName -ErrorAction SilentlyContinue if (![bool]$(Get-PSSession -Name $PSSessionName -ErrorAction SilentlyContinue)) {throw} } catch { if ($Counter -le 60) { Write-Warning "New-PSSession '$PSSessionName' failed. Trying again in 15 seconds..." Start-Sleep -Seconds 15 } else { Write-Error "Unable to create new PSSession to '$PSSessionName' using account '$($DomainAdminCredentials.UserName)'! Halting!" $global:FunctionResult = "1" return } } $Counter++ } if (!$RootCAPSSession) { Write-Error "Unable to create a PSSession to the Root CA Server at '$($RelevantRootCANetworkInfo.IPAddress)'! Halting!" $global:FunctionResult = "1" return } # Transfer any required PowerShell Modules [array]$ModulesToTransfer = foreach ($ModuleResource in $NeededModules) { $Module = Get-Module -ListAvailable $ModuleResource "$($($Module.ModuleBase -split $ModuleResource)[0])\$ModuleResource" } $ProgramFilesPSModulePath = "C:\Program Files\WindowsPowerShell\Modules" foreach ($ModuleDirPath in $ModulesToTransfer) { $CopyItemSplatParams = @{ Path = $ModuleDirPath Recurse = $True Destination = "$ProgramFilesPSModulePath\$($ModuleDirPath | Split-Path -Leaf)" ToSession = $RootCAPSSession Force = $True } Copy-Item @CopyItemSplatParams } $FunctionsForRemoteUse = @( ${Function:GetDomainController}.Ast.Extent.Text ${Function:SetupRootCA}.Ast.Extent.Text ) $Output = Invoke-Command -Session $RootCAPSSession -ScriptBlock { $using:FunctionsForRemoteUse | foreach { Invoke-Expression $_ } SetupRootCA @using:SetupRootCASplatParams } } else { $Output = SetupRootCA @SetupRootCASplatParams } $Output #endregion >> Do RootCA Install } # SIG # Begin signature block # MIIMiAYJKoZIhvcNAQcCoIIMeTCCDHUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUlc+TS6JvI51XqoQJ/xkUbwpk # ZlKgggn9MIIEJjCCAw6gAwIBAgITawAAAB/Nnq77QGja+wAAAAAAHzANBgkqhkiG # 9w0BAQsFADAwMQ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pjdeBgKMRuy4cZ0VCc/myO7uy7dq0CvVdXRsQC6Fqtr7yob9NbE # OdUYDBAGrt5ZAkw5YeL8H9E3JLGXtE7ir3ksT6Ki1mont2epJfHkO5JkmOI6XVtg # anuOGbo62885BOiXLu5+H2Fg+8ueTP40zFhfLh3e3Kj6Lm/NdovqqTBAsk04tFW9 # Hp4gWfVc0gTDwok3rHOrfIY35TGCAfUwggHxAgEBMFQwPTETMBEGCgmSJomT8ixk # ARkWA0xBQjEUMBIGCgmSJomT8ixkARkWBFpFUk8xEDAOBgNVBAMTB1plcm9TQ0EC # E1gAAAH5oOvjAv3166MAAQAAAfkwCQYFKw4DAhoFAKB4MBgGCisGAQQBgjcCAQwx # CjAIoAKAAKECgAAwGQYJKoZIhvcNAQkDMQwGCisGAQQBgjcCAQQwHAYKKwYBBAGC # NwIBCzEOMAwGCisGAQQBgjcCARUwIwYJKoZIhvcNAQkEMRYEFF11V8OJH5T5I0+R # MH49oZ9TAZS8MA0GCSqGSIb3DQEBAQUABIIBADSutGoVIFxZPMF1z387HhIe5k8X # USTYR7Qq4ym7JlsiG7xbiH4bRRW1MawEG8FGOSlgi3gc6BWl108dmkmae0aVp3Dg # K2KWMUJutEF7LYgEigIWCdU51N69cj0MOMs93W2yzZ4Px73vo6Gmrg6kZs1v2B0h # XSyacYrQvRSM7+PSuaBLfh3x47bFwpKRmFbP1LxzhttIj3Lk+bToSQttmYga9iUL # STj9J4eEEmk6J5B4Im+A9hT6Ho2xyWlO1cyID8Ms3GIBuvr42CVk0S74h4jhPJeO # Ao7kCv2QQueQYLUwRBMmpdQNxbumM4tqCN/omDenP5H3hfxKh/XnCple338= # SIG # End signature block |