@@ -9,6 +9,38 @@ import (
99 "testing"
1010)
1111
12+ // FuzzReadObject is a fuzz test that will generate random input data in an
13+ // attempt to find crash-causing inputs
14+ // https://go.dev/doc/security/fuzz
15+ func FuzzReadObject (f * testing.F ) {
16+ // seed corpus used to guide the fuzzing engine
17+ seedCorpus := []struct {
18+ input []byte
19+ offset int
20+ }{
21+ {[]byte {0x30 , 0x85 }, 0 },
22+ {[]byte {0x30 , 0x84 , 0x80 , 0x0 , 0x0 , 0x0 }, 0 },
23+ {[]byte {0x30 , 0x82 , 0x0 , 0x1 }, 0 },
24+ {[]byte {0x30 , 0x80 , 0x1 , 0x2 , 0x1 , 0x2 }, 0 },
25+ {[]byte {0x30 , 0x80 , 0x1 , 0x2 }, 0 },
26+ {[]byte {0x30 , 0x03 , 0x01 , 0x02 }, 0 },
27+ {[]byte {0x30 }, 0 },
28+ {[]byte ("?0" ), 0 },
29+ }
30+ for _ , tc := range seedCorpus {
31+ f .Add (tc .input , tc .offset ) // Use f.Add to provide a seed corpus
32+ }
33+ f .Fuzz (func (t * testing.T , ber []byte , offset int ) {
34+ if offset < 0 {
35+ return
36+ }
37+ _ , _ , err := readObject (ber , offset )
38+ if err != nil {
39+ t .Log (ber , offset )
40+ }
41+ })
42+ }
43+
1244func TestBer2Der (t * testing.T ) {
1345 // indefinite length fixture
1446 ber := []byte {0x30 , 0x80 , 0x02 , 0x01 , 0x01 , 0x00 , 0x00 }
@@ -44,13 +76,14 @@ func TestBer2Der_Negatives(t *testing.T) {
4476 Input []byte
4577 ErrorContains string
4678 }{
47- {[]byte {0x30 , 0x85 }, "tag length too long " },
79+ {[]byte {0x30 , 0x85 }, "end of ber data reached " },
4880 {[]byte {0x30 , 0x84 , 0x80 , 0x0 , 0x0 , 0x0 }, "length is negative" },
4981 {[]byte {0x30 , 0x82 , 0x0 , 0x1 }, "length has leading zero" },
5082 {[]byte {0x30 , 0x80 , 0x1 , 0x2 , 0x1 , 0x2 }, "Invalid BER format" },
51- {[]byte {0x30 , 0x80 , 0x1 , 0x2 }, "BER tag length is more than available data" },
83+ {[]byte {0x30 , 0x80 , 0x1 , 0x2 }, "end of ber data reached " },
5284 {[]byte {0x30 , 0x03 , 0x01 , 0x02 }, "length is more than available data" },
5385 {[]byte {0x30 }, "end of ber data reached" },
86+ {[]byte ("?0" ), "end of ber data reached" },
5487 }
5588
5689 for _ , fixture := range fixtures {
0 commit comments