Repository Public
1package protocol23import (4 "crypto/ecdsa"5 "crypto/elliptic"6 "crypto/rand"7 "crypto/sha256"8 "encoding/hex"9 "testing"10)1112func TestRequestSigningStringVectors(t *testing.T) {13 cases := []struct {14 method, uri string15 ts int6416 nonce string17 body []byte18 want string19 wantDigest string // hex SHA-256 of want20 }{21 {"POST", "/agent/v1/report", 1795033200000, "AAECAwQFBgcICQoLDA0ODw", []byte(`{"v":1}`),22 "mcc-agent-request-v1\nPOST\n/agent/v1/report\n1795033200000\nAAECAwQFBgcICQoLDA0ODw\nbfdd1f1ce70ce0cf2a7ab4a9b5ad5ae0e0a1f2e1c4f8f6f5a9b9f0f6a5c0b5d1", ""},23 {"GET", "/agent/v1/commands?wait=25", 1, "nnnnnnnnnnnnnnnnnnnnnn", nil,24 "mcc-agent-request-v1\nGET\n/agent/v1/commands?wait=25\n1\nnnnnnnnnnnnnnnnnnnnnnn\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},25 }26 // The body hash of the first vector is computed, not trusted from the literal above.27 h := sha256.Sum256(cases[0].body)28 cases[0].want = "mcc-agent-request-v1\nPOST\n/agent/v1/report\n1795033200000\nAAECAwQFBgcICQoLDA0ODw\n" + hex.EncodeToString(h[:])29 for i, c := range cases {30 got := string(RequestSigningString(c.method, c.uri, c.ts, c.nonce, c.body))31 if got != c.want {32 t.Errorf("vector %d:\n got %q\nwant %q", i, got, c.want)33 }34 }35 // Fixed, independently known digests.36 if BodyHashHex(nil) != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {37 t.Error("empty body hash")38 }39 if BodyHashHex([]byte("abc")) != "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" {40 t.Error("abc body hash")41 }42}4344func TestSignVerifyRequest(t *testing.T) {45 key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)46 other, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)47 body := []byte(`{"seq":1}`)48 nonce := NewRequestNonce()49 sig, err := SignRequest(key, "POST", "/agent/v1/report", 1000, nonce, body)50 if err != nil {51 t.Fatal(err)52 }53 ok := func(pub *ecdsa.PublicKey, m, u string, ts int64, n string, b []byte, s string) bool {54 return VerifyRequest(pub, m, u, ts, n, b, s)55 }56 if !ok(&key.PublicKey, "POST", "/agent/v1/report", 1000, nonce, body, sig) {57 t.Fatal("valid signature refused")58 }59 bad := []struct {60 name string61 res bool62 }{63 {"other key", ok(&other.PublicKey, "POST", "/agent/v1/report", 1000, nonce, body, sig)},64 {"method", ok(&key.PublicKey, "GET", "/agent/v1/report", 1000, nonce, body, sig)},65 {"path", ok(&key.PublicKey, "POST", "/agent/v1/renew", 1000, nonce, body, sig)},66 {"query", ok(&key.PublicKey, "POST", "/agent/v1/report?x=1", 1000, nonce, body, sig)},67 {"ts", ok(&key.PublicKey, "POST", "/agent/v1/report", 1001, nonce, body, sig)},68 {"nonce", ok(&key.PublicKey, "POST", "/agent/v1/report", 1000, nonce+"A", body, sig)},69 {"body", ok(&key.PublicKey, "POST", "/agent/v1/report", 1000, nonce, []byte(`{"seq":2}`), sig)},70 {"garbage sig", ok(&key.PublicKey, "POST", "/agent/v1/report", 1000, nonce, body, "not base64!")},71 {"empty sig", ok(&key.PublicKey, "POST", "/agent/v1/report", 1000, nonce, body, "")},72 {"nil key", ok(nil, "POST", "/agent/v1/report", 1000, nonce, body, sig)},73 }74 for _, b := range bad {75 if b.res {76 t.Errorf("%s: tampered request accepted", b.name)77 }78 }79}80