| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Creates a new self-signed certificate. |
| | 4 | | .DESCRIPTION |
| | 5 | | The New-KrSelfSignedCertificate function generates a self-signed certificate for use in development or testing s |
| | 6 | | This certificate can be used for securing communications or authentication purposes. |
| | 7 | | .PARAMETER DnsNames |
| | 8 | | The DNS name(s) for the certificate. |
| | 9 | | .PARAMETER KeyType |
| | 10 | | The type of key to use for the certificate (RSA or ECDSA). |
| | 11 | | .PARAMETER KeyLength |
| | 12 | | The length of the key in bits (only applicable for RSA). |
| | 13 | | .PARAMETER ValidDays |
| | 14 | | The number of days the certificate will be valid. |
| | 15 | | .PARAMETER Ephemeral |
| | 16 | | Indicates whether the certificate is ephemeral (temporary). |
| | 17 | | .PARAMETER Exportable |
| | 18 | | Indicates whether the private key is exportable. |
| | 19 | | .EXAMPLE |
| | 20 | | New-KrSelfSignedCertificate -Subject "CN=MyCert" -CertStoreLocation "Cert:\LocalMachine\My" |
| | 21 | |
|
| | 22 | | This example creates a self-signed certificate with the subject "CN=MyCert" and stores it in the local machine's |
| | 23 | | .NOTES |
| | 24 | | This function is intended for use in development and testing environments only. Do not use self-signed certifica |
| | 25 | | #> |
| | 26 | | function New-KrSelfSignedCertificate { |
| | 27 | | [KestrunRuntimeApi('Everywhere')] |
| | 28 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 29 | | [CmdletBinding()] |
| | 30 | | [OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])] |
| | 31 | | param( |
| | 32 | | [Parameter(Mandatory)] |
| | 33 | | [string[]]$DnsNames, |
| | 34 | |
|
| | 35 | | [ValidateSet('Rsa', 'Ecdsa')] |
| | 36 | | [string]$KeyType = 'Rsa', |
| | 37 | |
|
| | 38 | | [ValidateRange(256, 8192)] |
| | 39 | | [int]$KeyLength = 2048, |
| | 40 | |
|
| | 41 | | [ValidateRange(1, 3650)] |
| | 42 | | [int]$ValidDays = 365, |
| | 43 | |
|
| | 44 | | [switch]$Ephemeral, |
| | 45 | | [switch]$Exportable |
| | 46 | | ) |
| | 47 | |
|
| 1 | 48 | | $opts = [Kestrun.Certificates.CertificateManager+SelfSignedOptions]::new( |
| | 49 | | $DnsNames, |
| | 50 | | [Kestrun.Certificates.CertificateManager+KeyType]::$KeyType, |
| | 51 | | $KeyLength, |
| | 52 | | $null, # purposes |
| | 53 | | $ValidDays, |
| | 54 | | $Ephemeral.IsPresent, |
| | 55 | | $Exportable.IsPresent |
| | 56 | | ) |
| | 57 | |
|
| 1 | 58 | | return [Kestrun.Certificates.CertificateManager]::NewSelfSigned($opts) |
| | 59 | | } |
| | 60 | |
|