/
signature.rb
349 lines (302 loc) · 9.15 KB
/
signature.rb
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'pgp/packet/packet'
require 'pgp/pkeyalgorithm'
require 'pgp/hashalgorithm'
require 'pgp/packet/sigsubpacket'
module PGP
module Packet
class Signature < Packet
TYPES = {
0x00 => "Signature of a binary document",
0x01 => "Signature of a canonical text document",
0x02 => "Standalone signature",
0x10 => "Generic certification of a User ID and Public Key packet",
0x11 => "Persona certification of a User ID and Public Key packet",
0x12 => "Casual certification of a User ID and Public Key packet",
0x13 => "Positive certification of a User ID and Public Key packet",
0x18 => "Subkey Binding Signature",
0x1F => "Signature directly on a key",
0x20 => "Key revocation signature",
0x28 => "Subkey revocation signature",
0x30 => "Certification revocation signature",
0x40 => "Timestamp signature",
}
def self.typelabel(type)
TYPES[type]
end
def initialize(type = nil, pkey_algorithm = nil, hash_algorithm = nil)
super(2)
self.type = type
self.pkey_algorithm = pkey_algorithm
self.hash_algorithm = hash_algorithm
@version = 4
@target = nil
@signature = nil
@secretkey = nil
@hashedsubpacket = []
@unhashedsubpacket = []
end
def scan(io)
super
io.puts "Sig type - #{typelabel}(#{@type})"
io.puts PKeyAlgorithm.dump_summary(@pkey_algorithm)
io.puts HashAlgorithm.dump_summary(@hash_algorithm)
end
attr_accessor :version
attr_accessor :target
attr_accessor :signature
attr_accessor :secretkey
def typelabel
Signature.typelabel(@type)
end
def type
@type
end
def type=(type)
unless TYPES.key?(type)
raise "Unknown type: #{type}"
end
@type = type
end
def pkey_algorithm=(pkey_algorithm)
unless PKeyAlgorithm.include?(pkey_algorithm)
raise "Unknown algorithm: #{pkey_algorithm}"
end
@pkey_algorithm = pkey_algorithm
end
def hash_algorithm=(hash_algorithm)
unless HashAlgorithm.include?(hash_algorithm)
raise "Unknown algorithm: #{hash_algorithm}"
end
@hash_algorithm = hash_algorithm
end
def hashedsubpacket
@hashedsubpacket
end
def unhashedsubpacket
@unhashedsubpacket
end
def verify(pubkey, target)
packed_decoded_hash = MPI.to_bytes(pubkey.decrypt(@signature))
decoded_hash = PKeyAlgorithm.decode_hash(@pkey_algorithm, pubkey.nbits,
@hash_algorithm, packed_decoded_hash)
hashed_body = dump_version + dump_signature_type + dump_pkey_algorithm +
dump_hash_algorithm + dump_hashedsubpacket
calced_hash = calc_hash(target + dump_hash_magic(hashed_body))
decoded_hash == calced_hash
end
private
def dump_body
hashed_body = dump_version + dump_signature_type + dump_pkey_algorithm +
dump_hash_algorithm + dump_hashedsubpacket
hash = calc_hash(@target + dump_hash_magic(hashed_body))
hashed_body + dump_unhashedsubpacket + dump_hash_quicktest(hash) +
dump_signature(hash)
end
def dump_hash_magic(body)
body + dump_2octet(0x04ff) + dump_4octet(body.size)
end
def dump_signature_type
dump_1octet(@type)
end
def dump_pkey_algorithm
dump_1octet(@pkey_algorithm)
end
def dump_hash_algorithm
dump_1octet(@hash_algorithm)
end
def dump_hashedsubpacket
subpacket = @hashedsubpacket.collect { |packet| packet.dump }.join
dump_2octet(subpacket.length) + subpacket
end
def dump_unhashedsubpacket
subpacket = @unhashedsubpacket.collect { |packet| packet.dump }.join
dump_2octet(subpacket.length) + subpacket
end
def dump_hash_quicktest(hash)
hash[0, 2]
end
def dump_signature(hash)
unless @secretkey
STDERR.puts("No secret key given: dumping dummy signature")
return MPI.encode(0)
end
encoded_hash = PKeyAlgorithm.encode_hash(@pkey_algorithm, @secretkey.nbits,
@hash_algorithm, hash)
@signature = @secretkey.encrypt(MPI.from_bytes(encoded_hash))
# should we check :collect instead of :each?
if @signature.respond_to?(:each)
result = ''
@signature.each do |sig|
result << MPI.encode(sig)
end
result
else
MPI.encode(@signature)
end
end
def calc_hash(data)
HashAlgorithm.calc(@hash_algorithm, data)
end
def self.loader(port, length)
version = load_version(port)
case version
when 2, 3
raise "ToDo"
when 4
loader_v4(port, length - 1)
else
raise "Unknown version: #{version}"
end
end
def self.loader_v4(port, length)
initpos = port.readlength
signature_type = load_signature_type(port)
pkey_algorithm = load_pkey_algorithm(port)
hash_algorithm = load_hash_algorithm(port)
packet = new(signature_type, pkey_algorithm, hash_algorithm)
#
sublength = load_2octet(port)
sub = load_subpacket(port, sublength)
packet.hashedsubpacket.replace(sub)
#
sublength = load_2octet(port)
sub = load_subpacket(port, sublength)
packet.unhashedsubpacket.replace(sub)
qt = load_hash_quicktest(port)
# ToDo: test qt
packet.signature = load_signature(port,
length - (port.readlength - initpos), pkey_algorithm)
packet
end
def self.scanner(io, port, length)
version = load_version(port)
io.puts "Version - #{version}"
case version
when 2, 3
scanner_v3(io, port, length - 1)
when 4
scanner_v4(io, port, length - 1)
else
raise "Unknown version: #{version}"
end
end
def self.scanner_v3(io, port, length)
require 'pgp/packet/signaturev3'
initpos = port.readlength
header_length = load_1octet(port)
raise "Must be 5" if header_length != 5
signature_type = load_signature_type(port)
creation_time = load_time(port)
keyid = port.read(8)
pkey_algorithm = load_pkey_algorithm(port)
hash_algorithm = load_hash_algorithm(port)
qt = load_hash_quicktest(port)
packet = SignatureV3.new(signature_type, pkey_algorithm, hash_algorithm)
packet.creation_time = creation_time
packet.keyid = keyid
packet.scan(io)
io.puts "Hash left 2 bytes - " + qt.unpack("H*")[0]
io.puts "Signature: #{PKeyAlgorithm.label(pkey_algorithm)}"
io.indent(4) do
sig = load_signature(port,
length - (port.readlength - initpos), pkey_algorithm)
io.puts PKeyAlgorithm.dump_signature(pkey_algorithm, sig)
end
end
def self.scanner_v4(io, port, length)
initpos = port.readlength
signature_type = load_signature_type(port)
pkey_algorithm = load_pkey_algorithm(port)
hash_algorithm = load_hash_algorithm(port)
packet = new(signature_type, pkey_algorithm, hash_algorithm)
packet.scan(io)
io.puts "Hashed Sub:"
io.indent(4) do
sublength = load_2octet(port)
scan_subpacket(io, port, sublength)
end
io.puts "Unhashed Sub:"
io.indent(4) do
sublength = load_2octet(port)
scan_subpacket(io, port, sublength)
end
qt = load_hash_quicktest(port)
io.puts "Hash left 2 bytes - " + qt.unpack("H*")[0]
io.puts "Signature: #{PKeyAlgorithm.label(pkey_algorithm)}"
io.indent(4) do
sig = load_signature(port,
length - (port.readlength - initpos), pkey_algorithm)
io.puts PKeyAlgorithm.dump_signature(pkey_algorithm, sig)
end
end
def self.load_version(port)
version = load_1octet(port)
if version != 2 and version != 3 and version != 4
raise "Version #{version} signature packet not supported"
end
version
end
def self.load_signature_type(port)
load_1octet(port)
end
def self.load_pkey_algorithm(port)
load_1octet(port)
end
def self.load_hash_algorithm(port)
load_1octet(port)
end
def self.load_subpacket(port, length)
subpackets = []
initpos = port.readlength
while (port.readlength - initpos) < length
subpackets << SigSubPacket::Packet.load(port)
end
if (port.readlength - initpos) != length
raise "Illegal subpacket format"
end
subpackets
end
def self.scan_subpacket(io, port, length)
initpos = port.readlength
while (port.readlength - initpos) < length
SigSubPacket::Packet.scan(port, io)
end
if (port.readlength - initpos) != length
raise "Illegal subpacket format: #{port.readlength - initpos}/#{length}"
end
end
def self.load_hash_quicktest(port)
port.read(2)
end
def self.load_signature(port, length, algorithm)
size = PKeyAlgorithm.signaturesize(algorithm)
if size.nil?
port.read(length)
elsif size == 1
MPI.load(port)
else
result = []
size.times do
result << MPI.load(port)
end
result
end
end
add_loader(2, method(:loader))
add_scanner(2, method(:scanner))
end
end
end