2020
2121 . PARAMETER Dns
2222 Specifies one or more DNS Names to be written into the Subject Alternative Name (SAN) Extension of the Certificate Request.
23- May be left Empty if you specify a Subject, Upn or IP instead.
23+ May be left Empty if you specify a Subject, Upn, Email or IP instead.
2424
2525 . PARAMETER Upn
2626 Specifies one or more User Principal Names to be written into the Subject Alternative Name (SAN) Extension of the Certificate Request.
27- May be left Empty if you specify a Subject, DnsName or IP instead.
27+ May be left Empty if you specify a Subject, DnsName, Email or IP instead.
28+
29+ . PARAMETER Email
30+ Specifies one or more E-Mail addresses (RFC 822) to be written into the Subject Alternative Name (SAN) Extension of the Certificate Request.
31+ May be left Empty if you specify a Subject, DnsName, Upn or IP instead.
2832
2933 . PARAMETER IP
3034 Specifies or more IP Addresses to be written into the Subject Alternative Name (SAN) Extension of the Certificate Request.
31- May be left Empty if you specify a Subject, DnsName or Upn instead.
35+ May be left Empty if you specify a Subject, DnsName, Email or Upn instead.
36+
37+ . PARAMETER Smime
38+ Specifies the S/MIME Capabilities the requestor supports.
3239
3340 . PARAMETER Aki
3441 Specifies the Authority Key Identifier Attribute to be included in the Request.
@@ -158,12 +165,43 @@ Function New-CertificateRequest {
158165 [mailaddress []]
159166 $Upn ,
160167
168+ [Alias (" RFC822Name" )]
169+ [Alias (" E-Mail" )]
170+ [Parameter (Mandatory = $False )]
171+ [ValidateNotNullOrEmpty ()]
172+ [mailaddress []]
173+ $Email ,
174+
161175 [Alias (" IPAddress" )]
162176 [Parameter (Mandatory = $False )]
163177 [ValidateNotNullOrEmpty ()]
164178 [System.Net.IPAddress []]
165179 $IP ,
166180
181+ [Alias (" SmimeCapabilities" )]
182+ [Parameter (Mandatory = $False )]
183+ [ValidateSet (
184+ " des" ,
185+ " des3" ,
186+ " rc2" ,
187+ " rc4" ,
188+ " des3wrap" ,
189+ " rc2wrap" ,
190+ " aes128" ,
191+ " aes192" ,
192+ " aes256" ,
193+ " aes128wrap" ,
194+ " aes192wrap" ,
195+ " aes256wrap" ,
196+ " md5" ,
197+ " sha1" ,
198+ " sha256" ,
199+ " sha384" ,
200+ " sha512"
201+ )]
202+ [String []]
203+ $Smime ,
204+
167205 [Alias (" AuthorityKeyIdentifier" )]
168206 [Parameter (Mandatory = $False )]
169207 [ValidatePattern (" ^[0-9a-fA-F]{40}$" )]
@@ -286,7 +324,7 @@ Function New-CertificateRequest {
286324 }
287325 }
288326
289- If ((-not $Dns ) -and (-not $Upn ) -and (-not $IP ) -and ((-not $Subject ) -or ($Subject -eq " CN=" ))) {
327+ If ((-not $Dns ) -and (-not $Upn ) -and (-not $Email ) -and ( -not $ IP ) -and ((-not $Subject ) -or ($Subject -eq " CN=" ))) {
290328 Write-Error - Message " You must provide an Identity, either in Form ob a Subject or Subject Alternative Name!"
291329 return
292330 }
@@ -500,46 +538,60 @@ Function New-CertificateRequest {
500538 }
501539
502540 # Set the Subject Alternative Names Extension if specified as Argument
503- If ($Upn -or $Dns -or $IP ) {
541+ If ($Upn -or $Email -or $ Dns -or $IP ) {
504542
505543 # https://docs.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ix509extensionalternativenames
506544 $SubjectAlternativeNamesExtension = New-Object - ComObject X509Enrollment.CX509ExtensionAlternativeNames
507545 $Sans = New-Object - ComObject X509Enrollment.CAlternativeNames
508546
547+ # https://msdn.microsoft.com/en-us/library/aa374981(VS.85).aspx
548+
509549 Foreach ($Entry in $Upn ) {
510550
511- # https://msdn.microsoft.com/en-us/library/aa374981(VS.85).aspx
512551 $AlternativeNameObject = New-Object - ComObject X509Enrollment.CAlternativeName
513552 $AlternativeNameObject.InitializeFromString (
514553 $XCN_CERT_ALT_NAME_USER_PRINCIPLE_NAME ,
515554 $Entry
516555 )
517556 $Sans.Add ($AlternativeNameObject )
557+ [void ]([System.Runtime.Interopservices.Marshal ]::ReleaseComObject($AlternativeNameObject ))
558+
559+ }
560+
561+ Foreach ($Entry in $Email ) {
562+
563+ $AlternativeNameObject = New-Object - ComObject X509Enrollment.CAlternativeName
564+ $AlternativeNameObject.InitializeFromString (
565+ $XCN_CERT_ALT_NAME_RFC822_NAME ,
566+ $Entry
567+ )
568+ $Sans.Add ($AlternativeNameObject )
569+ [void ]([System.Runtime.Interopservices.Marshal ]::ReleaseComObject($AlternativeNameObject ))
518570
519571 }
520572
521573 Foreach ($Entry in $Dns ) {
522574
523- # https://msdn.microsoft.com/en-us/library/aa374981(VS.85).aspx
524575 $AlternativeNameObject = New-Object - ComObject X509Enrollment.CAlternativeName
525576 $AlternativeNameObject.InitializeFromString (
526577 $XCN_CERT_ALT_NAME_DNS_NAME ,
527578 $Entry
528579 )
529580 $Sans.Add ($AlternativeNameObject )
581+ [void ]([System.Runtime.Interopservices.Marshal ]::ReleaseComObject($AlternativeNameObject ))
530582
531583 }
532584
533585 Foreach ($Entry in $IP ) {
534586
535- # https://msdn.microsoft.com/en-us/library/aa374981(VS.85).aspx
536587 $AlternativeNameObject = New-Object - ComObject X509Enrollment.CAlternativeName
537588 $AlternativeNameObject.InitializeFromRawData (
538589 $XCN_CERT_ALT_NAME_IP_ADDRESS ,
539590 $XCN_CRYPT_STRING_BASE64 ,
540591 [Convert ]::ToBase64String($Entry.GetAddressBytes ())
541592 )
542593 $Sans.Add ($AlternativeNameObject )
594+ [void ]([System.Runtime.Interopservices.Marshal ]::ReleaseComObject($AlternativeNameObject ))
543595
544596 }
545597
@@ -550,6 +602,38 @@ Function New-CertificateRequest {
550602 $CertificateRequestObject.X509Extensions.Add ($SubjectAlternativeNamesExtension )
551603
552604 }
605+
606+ # Set the S/MIME Capabilities Extension if specified as Argument
607+ If ($Smime ) {
608+
609+ # https://docs.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ix509extensionsmimecapabilities
610+ $SmimeExtension = New-Object - ComObject X509Enrollment.CX509ExtensionSmimeCapabilities
611+
612+ # https://docs.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ismimecapabilities
613+ $SmimeCapabilitiesObject = New-Object - ComObject X509Enrollment.CSmimeCapabilities
614+
615+ $Smime | ForEach-Object - Process {
616+
617+ # The Bit length is only relevant for RC2 and RC4. We use the same defaults as Microsoft does.
618+ If ($_ -in (" rc2" , " rc2wrap" , " rc4" )) { $BitCount = 128 } Else { $BitCount = 0 }
619+
620+ $OidObject = New-Object - ComObject X509Enrollment.CObjectId
621+ $OidObject.InitializeFromValue ($SmimeCapabilityToOidTable [$_ ])
622+
623+ # https://docs.microsoft.com/en-us/windows/win32/api/certenroll/nf-certenroll-ismimecapability-initialize
624+ $SmimeCapabilityObject = New-Object - ComObject X509Enrollment.CSmimeCapability
625+ $SmimeCapabilityObject.Initialize (
626+ $OidObject ,
627+ $BitCount
628+ )
629+ $SmimeCapabilitiesObject.Add ($SmimeCapabilityObject )
630+ }
631+
632+ $SmimeExtension.InitializeEncode ($SmimeCapabilitiesObject )
633+
634+ # Adding the Extension to the Certificate
635+ $CertificateRequestObject.X509Extensions.Add ($SmimeExtension )
636+ }
553637
554638 # Set the Authority Key Identifier Extension if specified as Argument
555639 If ($Aki ) {
0 commit comments