signature_v3_test.go 2.7 KB
Newer Older
zhangweiwei's avatar
init  
zhangweiwei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package packet

import (
	"bytes"
	"crypto"
	"encoding/hex"
	"io"
	"io/ioutil"
	"testing"

	"golang.org/x/crypto/openpgp/armor"
)

func TestSignatureV3Read(t *testing.T) {
	r := v3KeyReader(t)
	Read(r)                // Skip public key
	Read(r)                // Skip uid
	packet, err := Read(r) // Signature
	if err != nil {
		t.Error(err)
		return
	}
	sig, ok := packet.(*SignatureV3)
	if !ok || sig.SigType != SigTypeGenericCert || sig.PubKeyAlgo != PubKeyAlgoRSA || sig.Hash != crypto.MD5 {
		t.Errorf("failed to parse, got: %#v", packet)
	}
}

func TestSignatureV3Reserialize(t *testing.T) {
	r := v3KeyReader(t)
	Read(r) // Skip public key
	Read(r) // Skip uid
	packet, err := Read(r)
	if err != nil {
		t.Error(err)
		return
	}
	sig := packet.(*SignatureV3)
	out := new(bytes.Buffer)
	if err = sig.Serialize(out); err != nil {
		t.Errorf("error reserializing: %s", err)
		return
	}
	expected, err := ioutil.ReadAll(v3KeyReader(t))
	if err != nil {
		t.Error(err)
		return
	}
	expected = expected[4+141+4+39:] // See pgpdump offsets below, this is where the sig starts
	if !bytes.Equal(expected, out.Bytes()) {
		t.Errorf("output doesn't match input (got vs expected):\n%s\n%s", hex.Dump(out.Bytes()), hex.Dump(expected))
	}
}

func v3KeyReader(t *testing.T) io.Reader {
	armorBlock, err := armor.Decode(bytes.NewBufferString(keySigV3Armor))
	if err != nil {
		t.Fatalf("armor Decode failed: %v", err)
	}
	return armorBlock.Body
}

// keySigV3Armor is some V3 public key I found in an SKS dump.
// Old: Public Key Packet(tag 6)(141 bytes)
//      Ver 4 - new
//      Public key creation time - Fri Sep 16 17:13:54 CDT 1994
//      Pub alg - unknown(pub 0)
//      Unknown public key(pub 0)
// Old: User ID Packet(tag 13)(39 bytes)
//      User ID - Armin M. Warda <warda@nephilim.ruhr.de>
// Old: Signature Packet(tag 2)(149 bytes)
//      Ver 4 - new
//      Sig type - unknown(05)
//      Pub alg - ElGamal Encrypt-Only(pub 16)
//      Hash alg - unknown(hash 46)
//      Hashed Sub: unknown(sub 81, critical)(1988 bytes)
const keySigV3Armor = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.0.10

mI0CLnoYogAAAQQA1qwA2SuJwfQ5bCQ6u5t20ulnOtY0gykf7YjiK4LiVeRBwHjGq7v30tGV
5Qti7qqRW4Ww7CDCJc4sZMFnystucR2vLkXaSoNWoFm4Fg47NiisDdhDezHwbVPW6OpCFNSi
ZAamtj4QAUBu8j4LswafrJqZqR9336/V3g8Yil2l48kABRG0J0FybWluIE0uIFdhcmRhIDx3
YXJkYUBuZXBoaWxpbS5ydWhyLmRlPoiVAgUQLok2xwXR6zmeWEiZAQE/DgP/WgxPQh40/Po4
gSkWZCDAjNdph7zexvAb0CcUWahcwiBIgg3U5ErCx9I5CNVA9U+s8bNrDZwgSIeBzp3KhWUx
524uhGgm6ZUTOAIKA6CbV6pfqoLpJnRYvXYQU5mIWsNa99wcu2qu18OeEDnztb7aLA6Ra9OF
YFCbq4EjXRoOrYM=
=LPjs
-----END PGP PUBLIC KEY BLOCK-----`