|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package crypto |
| 8 | + |
| 9 | +import ( |
| 10 | + "strconv" |
| 11 | + "testing" |
| 12 | + |
| 13 | + csp "github.com/IBM/idemix/bccsp/types" |
| 14 | + math "github.com/IBM/mathlib" |
| 15 | + "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +// auditInfoWithCurveID returns audit info JSON whose EID pseudonym audit data |
| 21 | +// carries the given curve ID in the named field. The curve ID goes in verbatim, |
| 22 | +// so it can be a value no curve is registered under. |
| 23 | +func auditInfoWithCurveID(field string, curveID int) []byte { |
| 24 | + return []byte(`{"EidNymAuditData":{"` + field + `":{"curve":` + |
| 25 | + strconv.Itoa(curveID) + `,"element":"AQID"}},"Schema":"test-schema"}`) |
| 26 | +} |
| 27 | + |
| 28 | +// TestDeserializeAuditInfoOutOfRangeCurveID covers the curve IDs no curve is |
| 29 | +// registered under. mathlib uses each of them to index its curve table, so before |
| 30 | +// the guard every one of these payloads killed the process with an |
| 31 | +// "index out of range" panic rather than being rejected. |
| 32 | +func TestDeserializeAuditInfoOutOfRangeCurveID(t *testing.T) { |
| 33 | + for _, curveID := range []int{-1, len(math.Curves), len(math.Curves) + 1, 999999} { |
| 34 | + for _, field := range []string{"Nym", "Rand", "Attr"} { |
| 35 | + t.Run(field+"/"+strconv.Itoa(curveID), func(t *testing.T) { |
| 36 | + raw := auditInfoWithCurveID(field, curveID) |
| 37 | + |
| 38 | + var err error |
| 39 | + require.NotPanics(t, func() { |
| 40 | + _, err = DeserializeAuditInfo(raw) |
| 41 | + }) |
| 42 | + require.Error(t, err) |
| 43 | + |
| 44 | + require.NotPanics(t, func() { |
| 45 | + err = (&AuditInfo{}).FromBytes(raw) |
| 46 | + }) |
| 47 | + require.Error(t, err) |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// TestDeserializeAuditInfoOutOfRangeCurveIDEvasions collects payloads that hid an |
| 54 | +// out-of-range curve ID from an earlier version of this guard, which pre-validated |
| 55 | +// the raw bytes with its own decode pass instead of wrapping the real one. Each |
| 56 | +// exploits a way that pass diverged from what encoding/json actually traverses, |
| 57 | +// and each panicked while the pre-validation reported the payload as clean. |
| 58 | +func TestDeserializeAuditInfoOutOfRangeCurveIDEvasions(t *testing.T) { |
| 59 | + for name, raw := range map[string]string{ |
| 60 | + // json.Unmarshal validates the whole input up front and decodes nothing on |
| 61 | + // a syntax error, but FromBytes' decoder reads one value and ignores what |
| 62 | + // follows it. Found by FuzzDeserializeAuditInfoNoPanic. |
| 63 | + "trailing garbage": `{"RhNymAuditData":{"Attr":{"curve":9}}}0`, |
| 64 | + // mathlib's UnmarshalJSON runs on every occurrence of a key, so the first |
| 65 | + // one panics even though only the last survives in the decoded result. |
| 66 | + "duplicate outer key": `{"EidNymAuditData":{"Nym":{"curve":999999,"element":"AQID"}},` + |
| 67 | + `"EidNymAuditData":{"Nym":null}}`, |
| 68 | + "duplicate inner key": `{"EidNymAuditData":{"Nym":{"curve":999999,"element":"AQID"},"Nym":null}}`, |
| 69 | + // encoding/json records a type error and keeps decoding, so the curve |
| 70 | + // element after it is still reached. |
| 71 | + "type error first": `{"EidNymAuditData":5,"RhNymAuditData":{"Nym":{"curve":999999,"element":"AQID"}}}`, |
| 72 | + // Key matching is case-insensitive. |
| 73 | + "unexpected key casing": `{"eidnymauditdata":{"nym":{"CURVE":999999,"element":"AQID"}}}`, |
| 74 | + } { |
| 75 | + t.Run(name, func(t *testing.T) { |
| 76 | + var err error |
| 77 | + require.NotPanics(t, func() { |
| 78 | + _, err = DeserializeAuditInfo([]byte(raw)) |
| 79 | + }) |
| 80 | + require.Error(t, err) |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestUnmarshalAuditInfo covers the guard on its own: a decode that panics becomes |
| 86 | +// an error naming the panic, and anything else is passed through untouched. |
| 87 | +func TestUnmarshalAuditInfo(t *testing.T) { |
| 88 | + err := UnmarshalAuditInfo(func() error { |
| 89 | + panic("boom") |
| 90 | + }) |
| 91 | + require.Error(t, err) |
| 92 | + assert.Contains(t, err.Error(), "caught panic") |
| 93 | + assert.Contains(t, err.Error(), "boom") |
| 94 | + |
| 95 | + sentinel := errors.New("decode failed") |
| 96 | + assert.Equal(t, sentinel, UnmarshalAuditInfo(func() error { return sentinel })) |
| 97 | + require.NoError(t, UnmarshalAuditInfo(func() error { return nil })) |
| 98 | +} |
| 99 | + |
| 100 | +// TestAuditInfoPopulatedCurveElementsRoundTrip makes sure the guard does not get |
| 101 | +// in the way of audit info that actually carries curve elements — the case the |
| 102 | +// fuzz seed corpus used to miss, since a zero-value AttrNymAuditData marshals its |
| 103 | +// mathlib fields as JSON null and never reaches their UnmarshalJSON. |
| 104 | +func TestAuditInfoPopulatedCurveElementsRoundTrip(t *testing.T) { |
| 105 | + for curveID := range math.Curves { |
| 106 | + t.Run(strconv.Itoa(curveID), func(t *testing.T) { |
| 107 | + curve := math.Curves[curveID] |
| 108 | + auditData := func() *csp.AttrNymAuditData { |
| 109 | + return &csp.AttrNymAuditData{ |
| 110 | + Nym: curve.GenG1, |
| 111 | + Rand: curve.NewZrFromInt(7), |
| 112 | + Attr: curve.NewZrFromInt(11), |
| 113 | + } |
| 114 | + } |
| 115 | + auditInfo := &AuditInfo{ |
| 116 | + Attributes: [][]byte{ |
| 117 | + []byte("attr0"), |
| 118 | + []byte("attr1"), |
| 119 | + []byte("enrollment-id"), |
| 120 | + []byte("revocation-handle"), |
| 121 | + }, |
| 122 | + Schema: "test-schema", |
| 123 | + EidNymAuditData: auditData(), |
| 124 | + RhNymAuditData: auditData(), |
| 125 | + } |
| 126 | + raw, err := auditInfo.Bytes() |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + deserialized, err := DeserializeAuditInfo(raw) |
| 130 | + require.NoError(t, err) |
| 131 | + assert.Equal(t, auditInfo.EnrollmentID(), deserialized.EnrollmentID()) |
| 132 | + assert.Equal(t, auditInfo.RevocationHandle(), deserialized.RevocationHandle()) |
| 133 | + assert.True(t, auditInfo.EidNymAuditData.Nym.Equals(deserialized.EidNymAuditData.Nym)) |
| 134 | + assert.True(t, auditInfo.EidNymAuditData.Rand.Equals(deserialized.EidNymAuditData.Rand)) |
| 135 | + assert.True(t, auditInfo.RhNymAuditData.Attr.Equals(deserialized.RhNymAuditData.Attr)) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// TestDeserializeAuditInfoInRangeCurveIDAccepted checks the guard does not turn |
| 141 | +// every curve ID into an error: the boundary IDs a curve is registered under are |
| 142 | +// still decoded. |
| 143 | +func TestDeserializeAuditInfoInRangeCurveIDAccepted(t *testing.T) { |
| 144 | + for _, curveID := range []int{0, len(math.Curves) - 1} { |
| 145 | + t.Run(strconv.Itoa(curveID), func(t *testing.T) { |
| 146 | + curve := math.Curves[curveID] |
| 147 | + raw, err := (&AuditInfo{ |
| 148 | + Attributes: [][]byte{{0}, {1}, []byte("eid"), []byte("rh")}, |
| 149 | + EidNymAuditData: &csp.AttrNymAuditData{ |
| 150 | + Nym: curve.GenG1, Rand: curve.NewZrFromInt(1), Attr: curve.NewZrFromInt(2), |
| 151 | + }, |
| 152 | + RhNymAuditData: &csp.AttrNymAuditData{ |
| 153 | + Nym: curve.GenG1, Rand: curve.NewZrFromInt(3), Attr: curve.NewZrFromInt(4), |
| 154 | + }, |
| 155 | + }).Bytes() |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + ai, err := DeserializeAuditInfo(raw) |
| 159 | + require.NoError(t, err) |
| 160 | + assert.Equal(t, curveID, int(ai.EidNymAuditData.Nym.CurveID())) |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
0 commit comments