-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from PKISharp/allow-external-account-binding
Allow external account binding
- Loading branch information
Showing
12 changed files
with
272 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,15 @@ function New-Account { | |
.PARAMETER EmailAddresses | ||
Contact adresses for certificate expiration mails and similar. | ||
.PARAMETER ExternalAccountKID | ||
The account KID assigned by the external account verification. | ||
.PARAMETER ExternalAccountAlgorithm | ||
The algorithm to be used to hash the external account binding. | ||
.PARAMETER ExternalAccountMACKey | ||
The key to hash the external account binding object (needs to be base64 or base64url encoded) | ||
.EXAMPLE | ||
PS> New-Account -AcceptTOS -EmailAddresses "[email protected]" -AutomaticAccountHandling | ||
|
@@ -53,17 +62,42 @@ function New-Account { | |
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]] | ||
$EmailAddresses | ||
$EmailAddresses, | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding", Mandatory = $true)] | ||
[ValidateNotNull()] | ||
[string] | ||
$ExternalAccountKID, | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding")] | ||
[ValidateSet('HS256','HS384','HS512')] | ||
[string] | ||
$ExternalAccountAlgorithm = 'HS256', | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding", Mandatory = $true)] | ||
[ValidateNotNull()] | ||
[string] | ||
$ExternalAccountMACKey | ||
) | ||
|
||
$Contacts = @($EmailAddresses | ForEach-Object { if($_.StartsWith("mailto:")) { $_ } else { "mailto:$_" } }); | ||
$contacts = @($EmailAddresses | ForEach-Object { if($_.StartsWith("mailto:")) { $_ } else { "mailto:$_" } }); | ||
|
||
$payload = @{ | ||
"termsOfServiceAgreed"=$AcceptTOS.IsPresent; | ||
"contact"=$Contacts; | ||
"contact"=$contacts; | ||
} | ||
|
||
$serviceDirectory = $State.GetServiceDirectory(); | ||
$url = $serviceDirectory.NewAccount; | ||
|
||
if($PSCmdlet.ParameterSetName -ne "ExternalAccountBinding" -and $serviceDirectory.Meta.ExternalAccountRequired) { | ||
throw "The ACME service requires an external account to create a new ACME account. Provide `-ExternalAccount*` Parameters." | ||
} | ||
|
||
$url = $State.GetServiceDirectory().NewAccount; | ||
if($PSCmdlet.ParameterSetName -eq "ExternalAccountBinding") { | ||
$externalAccountBinding = New-ExternalAccountPayload -State $State -ExternalAccountKID $ExternalAccountKID -ExternalAccountMACKey $ExternalAccountMACKey -ExternalAccountAlgorithm $ExternalAccountAlgorithm; | ||
$payload.Add("externalAccountBinding", $externalAccountBinding); | ||
} | ||
|
||
if($PSCmdlet.ShouldProcess("New-Account", "Sending account registration to ACME Server $Url")) { | ||
$response = Invoke-SignedWebRequest -Url $url -State $State -Payload $payload -SuppressKeyId -ErrorAction 'Stop' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function ConvertFrom-UrlBase64 { | ||
param( | ||
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
[ValidateNotNull()] | ||
[string] $InputText | ||
) | ||
|
||
process { | ||
$base64 = $InputText.Replace('-','+'); | ||
$base64 = $base64.Replace('_', '/'); | ||
|
||
while($base64.Length % 4 -ne 0) { | ||
$base64 += '=' | ||
} | ||
|
||
return [Convert]::FromBase64String($base64); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
function New-ExternalAccountPayload { | ||
param( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateNotNull()] | ||
[ValidateScript({$_.AccountKeyExists()})] | ||
[AcmeState] | ||
$State, | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding", Mandatory = $true)] | ||
[ValidateNotNull()] | ||
[string] | ||
$ExternalAccountKID, | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding")] | ||
[ValidateSet('HS256','HS384','HS512')] | ||
[string] | ||
$ExternalAccountAlgorithm = 'HS256', | ||
|
||
[Parameter(ParameterSetName = "ExternalAccountBinding", Mandatory = $true)] | ||
[ValidateNotNull()] | ||
[string] | ||
$ExternalAccountMACKey | ||
) | ||
|
||
process { | ||
$macKeyBytes = ConvertFrom-UrlBase64 $ExternalAccountMACKey; | ||
$macAlgorithm = switch ($ExternalAccountAlgorithm) { | ||
"HS256" { [Security.Cryptography.HMACSHA256]::new($macKeyBytes); break; } | ||
"HS384" { [Security.Cryptography.HMACSHA384]::new($macKeyBytes); break; } | ||
"HS512" { [Security.Cryptography.HMACSHA512]::new($macKeyBytes); break; } | ||
} | ||
|
||
$eaHeader = @{ | ||
"alg" = $ExternalAccountAlgorithm; | ||
"kid" = $ExternalAccountKID; | ||
"url" = $url; | ||
} | ConvertTo-Json -Compress | ConvertTo-UrlBase64 | ||
$eaPayload = $State.GetAccountKey().ExportPublicJwk() | ConvertTo-Json -Compress | ConvertTo-UrlBase64; | ||
|
||
$eaHashContent = [Text.Encoding]::ASCII.GetBytes("$($eaHeader).$($eaPayload)"); | ||
$eaSignature = (ConvertTo-UrlBase64 -InputBytes $macAlgorithm.ComputeHash($eaHashContent)); | ||
|
||
$externalAccountBinding = @{ | ||
"protected" = $eaHeader; | ||
"payload" = $eaPayload; | ||
"signature" = $eaSignature; | ||
}; | ||
|
||
return $externalAccountBinding; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
InModuleScope "ACME-PS" { | ||
Describe "UnitTesting New-ExternalAccountPayload" -Tag "UnitTest" { | ||
$simpleState = Get-State -Path "$PSScriptRoot\states\simple"; | ||
$state = New-State -WarningAction 'SilentlyContinue'; | ||
|
||
$state.Set($simpleState.GetServiceDirectory()) | ||
$state.SetNonce($simpleState.GetNonce()); | ||
$state.Set($simpleState.GetAccountKey()); | ||
|
||
$result = New-ExternalAccountPayload -State $State ` | ||
-ExternalAccountKID "myKID" -ExternalAccountAlgorithm "HS256" ` | ||
-ExternalAccountMACKey "SLrdl4skg66W0NxZMwwAKPSvDtin-41SCweDRDBxMSSyh5AyoL1mNva6IMhFP13uyOQv5RI40WnnvzyXGlp77w" | ||
It 'Returns an Object' { | ||
$result | Should -Not -BeNullOrEmpty; | ||
} | ||
It 'Contains protected' { | ||
$result.ContainsKey("protected") | Should -BeTrue; | ||
} | ||
It 'Contains payload' { | ||
$result.ContainsKey("payload") | Should -BeTrue; | ||
} | ||
It 'Contains signature' { | ||
$result.ContainsKey("signature") | Should -BeTrue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
InModuleScope ACME-PS { | ||
Context 'ConvertTo-Base64Url and ConvertFrom-Base64 url are roundtrippable' { | ||
$value = [byte[]]@(131,251,190,1); | ||
|
||
$base64Form = ConvertTo-UrlBase64 -InputBytes $value; | ||
It 'Converted the input successfully' { | ||
$base64Form | Should -Be "g_u-AQ"; | ||
} | ||
|
||
$roundtripped = ConvertFrom-UrlBase64 $base64Form; | ||
It 'Should match the orginial array' { | ||
$roundtripped | Should -Be $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.