~cmars/hockeypuck/master

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
   Hockeypuck - OpenPGP key server
   Copyright (C) 2012-2014  Casey Marshall

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package openpgp

import (
	"bytes"
	"crypto/md5"
	"encoding/binary"
	"encoding/hex"
	"sort"
	"testing"

	"code.google.com/p/go.crypto/openpgp/armor"
	"code.google.com/p/go.crypto/openpgp/packet"
	"github.com/stretchr/testify/assert"
)

func TestBadSelfSigUid(t *testing.T) {
	f := MustInput(t, "badselfsig.asc")
	i := 0
	for keyRead := range ReadKeys(f) {
		assert.NotNil(t, keyRead.Error)
		i++
	}
	assert.Equal(t, 1, i)
}

func TestDupSig(t *testing.T) {
	{
		f := MustInput(t, "252B8B37.dupsig.asc")
		defer f.Close()
		block, err := armor.Decode(f)
		if err != nil {
			t.Fatal(err)
		}
		r := packet.NewOpaqueReader(block.Body)
		var packets []*packet.OpaquePacket
		for {
			if op, err := r.Next(); err != nil {
				break
			} else {
				packets = append(packets, op)
				t.Log("raw:", op)
			}
		}
		sksDigest := sksDigestOpaque(packets, md5.New())
		assert.Equal(t, sksDigest, "6d57b48c83d6322076d634059bb3b94b")
	}
	// Read a key
	{
		f := MustInput(t, "252B8B37.dupsig.asc")
		defer f.Close()
		block, err := armor.Decode(f)
		if err != nil {
			t.Fatal(err)
		}
		var key *Pubkey
		for keyRead := range readKeys(block.Body) {
			assert.Nil(t, keyRead.Error)
			key = keyRead.Pubkey
		}
		var packets []*packet.OpaquePacket
		key.Visit(func(rec PacketRecord) error {
			op, err := rec.GetOpaquePacket()
			assert.Nil(t, err)
			packets = append(packets, op)
			return err
		})
		r := packet.NewOpaqueReader(bytes.NewBuffer(key.Unsupported))
		for op, err := r.Next(); err == nil; op, err = r.Next() {
			packets = append(packets, op)
		}
		sksDigest := sksDigestOpaque(packets, md5.New())
		assert.Equal(t, sksDigest, "6d57b48c83d6322076d634059bb3b94b")
	}
	// Now read & resolve
	key := MustInputAscKey(t, "252B8B37.dupsig.asc")
	key.Visit(func(rec PacketRecord) error {
		op, err := rec.GetOpaquePacket()
		assert.Nil(t, err)
		t.Log("parsed:", op)
		return nil
	})
	r := packet.NewOpaqueReader(bytes.NewBuffer(key.Unsupported))
	for op, err := r.Next(); err == nil; op, err = r.Next() {
		t.Log("parsed:", op)
	}
	assert.Equal(t, key.Md5, "6d57b48c83d6322076d634059bb3b94b")
}

func TestPrimaryUidSelection(t *testing.T) {
	key := MustInputAscKey(t, "lp1195901.asc")
	Resolve(key)
	assert.NotNil(t, key.primaryUid)
	// Primary UID
	assert.Equal(t, key.primaryUid.Keywords, "Phil Pennock <phil.pennock@spodhuis.org>")
	for _, uid := range key.userIds {
		if uid.Keywords == "pdp@spodhuis.demon.nl" {
			// This uid sig is revoked
			assert.NotNil(t, uid.revSig)
		}
	}
	key = MustInputAscKey(t, "lp1195901_2.asc")
	Resolve(key)
	assert.NotNil(t, key.primaryUid)
	assert.Equal(t, key.primaryUid.Keywords, "Phil Pennock <phil.pennock@globnix.org>")
}

func TestSortPrimaryUid(t *testing.T) {
	key := MustInputAscKey(t, "lp1195901.asc")
	Resolve(key)
	Sort(key)
	expect := []string{
		"Phil Pennock <phil.pennock@spodhuis.org>",
		"Phil Pennock <phil.pennock@globnix.org>",
		"Phil Pennock <pdp@exim.org>",
		"Phil Pennock <pdp@spodhuis.org>",
		"Phil Pennock <pdp@spodhuis.demon.nl>"}
	for i := range key.userIds {
		assert.Equal(t, expect[i], key.userIds[i].Keywords)
	}
}

func TestKeyExpiration(t *testing.T) {
	key := MustInputAscKey(t, "lp1195901.asc")
	Resolve(key)
	Sort(key)
	key.Visit(func(rec PacketRecord) error {
		if sig, is := rec.(*Signature); is {
			if sig.Signature != nil && sig.Signature.KeyLifetimeSecs != nil {
				t.Logf("Key expiration %d", *sig.Signature.KeyLifetimeSecs)
			}
		}
		return nil
	})
}

func TestPrimaryUidFallback(t *testing.T) {
	f := MustInput(t, "snowcrash.gpg")
	var key *Pubkey
	for keyRead := range ReadKeys(f) {
		assert.Nil(t, keyRead.Error)
		key = keyRead.Pubkey
	}
	assert.NotNil(t, key)
	assert.NotEmpty(t, key.PrimaryUid)
	t.Log(key.PrimaryUid)
}

// TestUnsuppIgnored tests parsing key material containing
// packets which are not normally part of an exported public key --
// trust packets, in this case.
func TestUnsuppIgnored(t *testing.T) {
	f := MustInput(t, "snowcrash.gpg")
	var key *Pubkey
	for keyRead := range ReadKeys(f) {
		assert.Nil(t, keyRead.Error)
		key = keyRead.Pubkey
	}
	assert.NotNil(t, key)
	assert.Empty(t, key.Unsupported)
}

func TestMissingUidFk(t *testing.T) {
	key := MustInputAscKey(t, "d7346e26.asc")
	t.Log(key)
}

func TestV3NoUidSig(t *testing.T) {
	key := MustInputAscKey(t, "0xd46b7c827be290fe4d1f9291b1ebc61a.asc")
	assert.Equal(t, "0005127a8b7da8c32998d7e81dc92540", key.Md5)
	assert.Equal(t, "0760df64b3d82239", key.KeyId())
	f := MustInput(t, "0xd46b7c827be290fe4d1f9291b1ebc61a.asc")
	defer f.Close()
	block, err := armor.Decode(f)
	if err != nil {
		t.Fatal(err)
	}
	var kr *OpaqueKeyring
	for opkr := range ReadOpaqueKeyrings(block.Body) {
		kr = opkr
	}
	sort.Sort(sksPacketSorter{kr.Packets})
	h := md5.New()
	for _, opkt := range kr.Packets {
		binary.Write(h, binary.BigEndian, int32(opkt.Tag))
		binary.Write(h, binary.BigEndian, int32(len(opkt.Contents)))
		h.Write(opkt.Contents)
	}
	md5 := hex.EncodeToString(h.Sum(nil))
	assert.Equal(t, "0005127a8b7da8c32998d7e81dc92540", md5)
}