1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package packet
6
7import (
8	"bytes"
9	"crypto"
10	"encoding/hex"
11	"io"
12	"io/ioutil"
13	"testing"
14
15	"golang.org/x/crypto/openpgp/armor"
16)
17
18func TestSignatureV3Read(t *testing.T) {
19	r := v3KeyReader(t)
20	Read(r)                // Skip public key
21	Read(r)                // Skip uid
22	packet, err := Read(r) // Signature
23	if err != nil {
24		t.Error(err)
25		return
26	}
27	sig, ok := packet.(*SignatureV3)
28	if !ok || sig.SigType != SigTypeGenericCert || sig.PubKeyAlgo != PubKeyAlgoRSA || sig.Hash != crypto.MD5 {
29		t.Errorf("failed to parse, got: %#v", packet)
30	}
31}
32
33func TestSignatureV3Reserialize(t *testing.T) {
34	r := v3KeyReader(t)
35	Read(r) // Skip public key
36	Read(r) // Skip uid
37	packet, err := Read(r)
38	if err != nil {
39		t.Error(err)
40		return
41	}
42	sig := packet.(*SignatureV3)
43	out := new(bytes.Buffer)
44	if err = sig.Serialize(out); err != nil {
45		t.Errorf("error reserializing: %s", err)
46		return
47	}
48	expected, err := ioutil.ReadAll(v3KeyReader(t))
49	if err != nil {
50		t.Error(err)
51		return
52	}
53	expected = expected[4+141+4+39:] // See pgpdump offsets below, this is where the sig starts
54	if !bytes.Equal(expected, out.Bytes()) {
55		t.Errorf("output doesn't match input (got vs expected):\n%s\n%s", hex.Dump(out.Bytes()), hex.Dump(expected))
56	}
57}
58
59func v3KeyReader(t *testing.T) io.Reader {
60	armorBlock, err := armor.Decode(bytes.NewBufferString(keySigV3Armor))
61	if err != nil {
62		t.Fatalf("armor Decode failed: %v", err)
63	}
64	return armorBlock.Body
65}
66
67// keySigV3Armor is some V3 public key I found in an SKS dump.
68// Old: Public Key Packet(tag 6)(141 bytes)
69//      Ver 4 - new
70//      Public key creation time - Fri Sep 16 17:13:54 CDT 1994
71//      Pub alg - unknown(pub 0)
72//      Unknown public key(pub 0)
73// Old: User ID Packet(tag 13)(39 bytes)
74//      User ID - Armin M. Warda <warda@nephilim.ruhr.de>
75// Old: Signature Packet(tag 2)(149 bytes)
76//      Ver 4 - new
77//      Sig type - unknown(05)
78//      Pub alg - ElGamal Encrypt-Only(pub 16)
79//      Hash alg - unknown(hash 46)
80//      Hashed Sub: unknown(sub 81, critical)(1988 bytes)
81const keySigV3Armor = `-----BEGIN PGP PUBLIC KEY BLOCK-----
82Version: SKS 1.0.10
83
84mI0CLnoYogAAAQQA1qwA2SuJwfQ5bCQ6u5t20ulnOtY0gykf7YjiK4LiVeRBwHjGq7v30tGV
855Qti7qqRW4Ww7CDCJc4sZMFnystucR2vLkXaSoNWoFm4Fg47NiisDdhDezHwbVPW6OpCFNSi
86ZAamtj4QAUBu8j4LswafrJqZqR9336/V3g8Yil2l48kABRG0J0FybWluIE0uIFdhcmRhIDx3
87YXJkYUBuZXBoaWxpbS5ydWhyLmRlPoiVAgUQLok2xwXR6zmeWEiZAQE/DgP/WgxPQh40/Po4
88gSkWZCDAjNdph7zexvAb0CcUWahcwiBIgg3U5ErCx9I5CNVA9U+s8bNrDZwgSIeBzp3KhWUx
89524uhGgm6ZUTOAIKA6CbV6pfqoLpJnRYvXYQU5mIWsNa99wcu2qu18OeEDnztb7aLA6Ra9OF
90YFCbq4EjXRoOrYM=
91=LPjs
92-----END PGP PUBLIC KEY BLOCK-----`
93