How to Submit Timestamp Request to RFC 3161 Timestamp Server with Powershell

Viewed 572

How would one submit a timestamp request to a RFC 3161 Timestamp Server using PowerShell's Invoke-WebRequest?

First, I create the timestamp request:

openssl ts -query -data message.txt -cert -sha256 -no_nonce -out ts_test_msg_sha256.tsq

Then I can use this curl command to submit it to the Timestamp server. This is confirmed to work.

curl -k -H "Content-Type: application/timestamp-query" -H "Host:timestamp.digicert.com" --data-binary @ts_test_msg_sha256.tsq http://timestamp.digicert.com > ts_test_msg_sha256.tsr

I am trying to do the same thing in PowerShell, but it isnt working. This is the PowerShell command I have tried:

Invoke-WebRequest -uri http://timestamp.digicert.com -Headers @{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -body "ts_test_msg_sha256.tsq" -method POST > "ts_test_msg_sha256.tsr"

And I get this error:

Invoke-WebRequest : The remote server returned an error: (404) Not Found.

What am I doing wrong? As I understand it, the PowerShell command should be the exact same as the curl command.

EDIT: The final command I used was:

Invoke-WebRequest -uri http://timestamp.digicert.com -Headers @{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -infile "ts_test_msg_sha256.tsq" -method POST -outfile ts_test_msg_sha256.tsr | Out-Null
1 Answers

The TS server returns 404 in response to incorrect requests.

The proper request is

$R = Invoke-WebRequest -uri http://timestamp.digicert.com/ -ContentType 'application/timestamp-query' -InFile "ts_test_msg_sha256.tsq" -method POST

Please note -ContentType and -InFile parameters.

The response is saved in $R variable. To write the raw response body to a binary file:

if ($R.StatusCode -eq 200) { 
    Set-Content ts_test_msg_sha256.tsr -Value $R.Content -AsByteStream; 
    echo "Done" 
} else { 
    echo "Request failed: $($R.StatusCode)" 
}
Related