Skip to content

Commit 08e7c57

Browse files
committed
Add response signature per the new Sec. 2.4
1 parent 24f6922 commit 08e7c57

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

signatures_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,7 @@ Signature: sig1=:vR1E+sDgh0J3dZyVdPc7mK0ZbEMW3N47eDpFjXLE9g95Gx1KQLpdOmDQfedgdLz
17501750
`
17511751

17521752
// ";req" use case from draft, Sec. 2.3 of draft -10
1753+
// Note this has been changed with draft -17, see TestRequestBinding17
17531754
func TestRequestBinding(t *testing.T) {
17541755
req := readRequest(httpreq6)
17551756
contentDigest := req.Header.Values("Content-Digest")
@@ -1993,3 +1994,59 @@ func TestQPEncoding(t *testing.T) {
19931994
"@signature-params": ("@query-param";name="var" "@query-param";name="bar" "@query-param";name="fa%C3%A7ade%22%3A%20");created=8888;alg="hmac-sha256";keyid="key1"`
19941995
assert.Equal(t, expected, sigBase)
19951996
}
1997+
1998+
// TODO: this is changed from the example in the draft, specifically the Host header is different
1999+
var httpreq11 = `POST /foo?param=Value&Pet=dog HTTP/1.1
2000+
Host: origin.host.internal.example
2001+
Date: Tue, 20 Apr 2021 02:07:55 GMT
2002+
Content-Type: application/json
2003+
Content-Digest: sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:
2004+
Content-Length: 18
2005+
2006+
{"hello": "world"}
2007+
`
2008+
2009+
var httpres9 = `HTTP/1.1 503 Service Unavailable
2010+
Date: Tue, 20 Apr 2021 02:07:56 GMT
2011+
Content-Type: application/json
2012+
Content-Length: 62
2013+
Content-Digest: sha-512=:0Y6iCBzGg5rZtoXS95Ijz03mslf6KAMCloESHObfwnHJDbkkWWQz6PhhU9kxsTbARtY2PTBOzq24uJFpHsMuAg==:
2014+
Signature-Input: reqres=("@status" "content-digest" "content-type" "@authority";req "@method";req "@path";req "content-digest";req);created=1618884479;keyid="test-key-ecc-p256"
2015+
Signature: reqres=:9MG6AOgykOZTc/h2rnDc/g8L+/aXgdkV4hNDvpCxfbVrmLevWPfyvEC/8jBh+3XnVwBqqcJyhUXoFgWv1SMI7A==:
2016+
2017+
{"busy": true, "message": "Your call is very important to us"}
2018+
`
2019+
2020+
// ";req" use case from draft, with the latest draft -17 corrections (Sec. 2.4)
2021+
func TestRequestBinding17(t *testing.T) {
2022+
req := readRequest(httpreq11)
2023+
reqContentDigest := req.Header.Values("Content-Digest")
2024+
err := ValidateContentDigestHeader(reqContentDigest, &req.Body, []string{DigestSha512})
2025+
assert.NoError(t, err, "validate request digest")
2026+
2027+
res := readResponse(httpres9)
2028+
pubKey2, err := parseECPublicKeyFromPemStr(p256PubKey2)
2029+
assert.NoError(t, err, "read pub key")
2030+
fields2 := *NewFields().AddHeaders("@status", "content-digest", "content-type").
2031+
AddHeaderExt("@authority", false, false, true, false).
2032+
AddHeaderExt("@method", false, false, true, false).
2033+
AddHeaderExt("@path", false, false, true, false).
2034+
AddHeaderExt("content-digest", false, false, true, false)
2035+
verifier2, err := NewP256Verifier("test-key-ecc-p256", *pubKey2, NewVerifyConfig().SetVerifyCreated(false), fields2)
2036+
assert.NoError(t, err, "create verifier")
2037+
sigBase, err := verifyResponseDebug("reqres", *verifier2, res, req)
2038+
expected := `"@status": 503
2039+
"content-digest": sha-512=:0Y6iCBzGg5rZtoXS95Ijz03mslf6KAMCloESHObfwnHJDbkkWWQz6PhhU9kxsTbARtY2PTBOzq24uJFpHsMuAg==:
2040+
"content-type": application/json
2041+
"@authority";req: origin.host.internal.example
2042+
"@method";req: POST
2043+
"@path";req: /foo
2044+
"content-digest";req: sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:
2045+
"@signature-params": ("@status" "content-digest" "content-type" "@authority";req "@method";req "@path";req "content-digest";req);created=1618884479;keyid="test-key-ecc-p256"`
2046+
assert.NoError(t, err, "verify response")
2047+
assert.Equal(t, expected, sigBase, "Incorrect signature base for response")
2048+
2049+
responseContentDigest := res.Header.Values("Content-Digest")
2050+
err = ValidateContentDigestHeader(responseContentDigest, &res.Body, []string{DigestSha512})
2051+
assert.NoError(t, err, "validate response digest")
2052+
}

0 commit comments

Comments
 (0)