Debian Codegraph @

openpgp/read_security_test.go

raw
1 package openpgp
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "io"
7 "testing"
8 "time"
9 )
10
11 func secPacketLength(n int) []byte {
12 switch {
13 case n < 192:
14 return []byte{byte(n)}
15 case n < 8384:
16 n -= 192
17 return []byte{byte(n>>8) + 192, byte(n)}
18 default:
19 return []byte{0xff, byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)}
20 }
21 }
22
23 func secWrapPacket(tag byte, body []byte) []byte {
24 out := []byte{0xc0 | tag}
25 out = append(out, secPacketLength(len(body))...)
26 return append(out, body...)
27 }
28
29 func secOnePassSig(keyId uint64) []byte {
30 var body bytes.Buffer
31 body.WriteByte(0x03)
32 body.WriteByte(0x00)
33 body.WriteByte(0x08)
34 body.WriteByte(0x01)
35 var id [8]byte
36 binary.BigEndian.PutUint64(id[:], keyId)
37 body.Write(id[:])
38 body.WriteByte(0x01)
39 return secWrapPacket(4, body.Bytes())
40 }
41
42 func secLiteral(data []byte) []byte {
43 var body bytes.Buffer
44 body.WriteByte('b')
45 body.WriteByte(0x00)
46 body.Write([]byte{0x00, 0x00, 0x00, 0x00})
47 body.Write(data)
48 return secWrapPacket(11, body.Bytes())
49 }
50
51 func secSigNoIssuer() []byte {
52 creation := []byte{0x05, 0x02, 0x00, 0x00, 0x00, 0x01}
53 var body bytes.Buffer
54 body.WriteByte(0x04)
55 body.WriteByte(0x00)
56 body.WriteByte(0x01)
57 body.WriteByte(0x08)
58 binary.Write(&body, binary.BigEndian, uint16(len(creation)))
59 body.Write(creation)
60 binary.Write(&body, binary.BigEndian, uint16(0))
61 body.Write([]byte{0x00, 0x00})
62 body.Write([]byte{0x00, 0x00})
63 return secWrapPacket(2, body.Bytes())
64 }
65
66 func TestReadV1NilIssuerNoPanic(t *testing.T) {
67 entity, err := NewEntity("test", "", "[email protected]", nil)
68 if err != nil {
69 t.Fatal(err)
70 }
71 signingKey, ok := entity.SigningKey(time.Now())
72 if !ok {
73 t.Fatal("no signing key")
74 }
75
76 var msg []byte
77 msg = append(msg, secOnePassSig(signingKey.PublicKey.KeyId)...)
78 msg = append(msg, secLiteral([]byte("hello"))...)
79 msg = append(msg, secSigNoIssuer()...)
80
81 md, err := ReadMessage(bytes.NewReader(msg), EntityList{entity}, nil, nil)
82 if err != nil {
83 return
84 }
85 io.Copy(io.Discard, md.UnverifiedBody)
86 }