Debian Codegraph @

openpgp/packet/signature_nested_test.go

raw
1 package packet
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 const embeddedSignatureSubpacketByte = 32
9
10 func sigBodyWithEmbedded(sigType byte, embedded []byte) []byte {
11 var hashed []byte
12 hashed = append(hashed, 0x05, 0x02, 0x00, 0x00, 0x00, 0x01) // creation time subpacket
13 if embedded != nil {
14 sub := append([]byte{embeddedSignatureSubpacketByte}, embedded...)
15 hashed = append(hashed, byte(len(sub)))
16 hashed = append(hashed, sub...)
17 }
18
19 var body []byte
20 body = append(body, 0x04, sigType, 0x01, 0x08)
21 body = append(body, byte(len(hashed)>>8), byte(len(hashed)))
22 body = append(body, hashed...)
23 body = append(body, 0x00, 0x00) // unhashed subpackets length
24 body = append(body, 0x00, 0x00) // hash tag
25 body = append(body, 0x00, 0x00) // RSA signature MPI, 0 bits
26 return body
27 }
28
29 // A primary key binding signature must not contain an embedded signature, so a
30 // nested embedding is rejected while parsing the inner signature.
31 func TestNestedEmbeddedSignatureRejected(t *testing.T) {
32 deepest := sigBodyWithEmbedded(byte(SigTypePrimaryKeyBinding), nil)
33 inner := sigBodyWithEmbedded(byte(SigTypePrimaryKeyBinding), deepest)
34 outer := sigBodyWithEmbedded(byte(SigTypeSubkeyBinding), inner)
35 pkt := append([]byte{0xc0 | 2, byte(len(outer))}, outer...)
36
37 if _, err := Read(bytes.NewReader(pkt)); err == nil {
38 t.Fatal("expected an error for nested embedded signatures, got nil")
39 }
40 }