vchain_client 1.0.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,25 +3,143 @@ module VChainClient
3
3
  class Signatures
4
4
 
5
5
  @config = nil
6
+ @log = nil
6
7
 
7
8
  def initialize(config)
8
9
  @config = config
10
+
11
+ @log = Log4r::Logger["vchain_client"]
9
12
  end
10
13
 
11
14
  def signRequest(document, timestamp)
12
- priv_key_path = @config["private_key_location"]
13
-
14
- priv_key = File.read(priv_key_path)
15
+ OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)
15
16
 
16
- whole_sign = document.to_json + timestamp.to_s
17
+ priv_key_path = @config["private_key_location"]
17
18
 
18
- ec = OpenSSL::PKey::EC.new(priv_key)
19
+ if @log.debug?
20
+ @log.debug("[Signatures.signRequest] input:")
21
+ @log.debug("-> timestamp: "+ timestamp.to_s)
22
+ @log.debug("-> key path: #{priv_key_path}")
23
+ @log.debug("-> input:")
24
+ @log.debug(document)
25
+ end
26
+
27
+ priv_key = nil
28
+
29
+ begin
30
+
31
+ priv_key = File.read(priv_key_path)
32
+
33
+ rescue => e
34
+ if @log.error?
35
+ @log.error("[Signatures.signRequest] File.read raised exception:")
36
+ @log.error("#{e.class}, #{e.message}")
37
+ @log.error("-> timestamp: "+ timestamp.to_s)
38
+ @log.error("-> input:")
39
+ @log.error(document)
40
+ @log.error("--> priv_key_path: #{priv_key_path}")
41
+ end
42
+
43
+ raise e
44
+ end
45
+
46
+ if priv_key == nil
47
+ if @log.error?
48
+ @log.error("[Signatures.signRequest] failed to load private key")
49
+ @log.error("-> timestamp: "+ timestamp.to_s)
50
+ @log.error("-> input:")
51
+ @log.error(document)
52
+ @log.error("--> priv_key_path: #{priv_key_path}")
53
+ end
54
+
55
+ return nil
56
+ end
57
+
58
+ if @log.debug?
59
+ @log.debug("[Signatures.signRequest] priv key is loaded")
60
+ end
61
+
62
+ ec = nil
63
+
64
+ begin
65
+
66
+ ec = OpenSSL::PKey::EC.new(priv_key)
67
+
68
+ rescue => e
69
+ if @log.error?
70
+ @log.error("[Signatures.signRequest] OpenSSL::PKey::EC.new raised exception:")
71
+ @log.error("#{e.class}, #{e.message}")
72
+ @log.error("-> timestamp: "+ timestamp.to_s)
73
+ @log.error("-> input:")
74
+ @log.error(document)
75
+ @log.error("--> priv_key_path: #{priv_key_path}")
76
+ end
77
+
78
+ raise e
79
+ end
80
+
81
+ if ec == nil
82
+ if @log.error?
83
+ @log.error("[Signatures.signRequest] failed init EC key")
84
+ @log.error("-> timestamp: "+ timestamp.to_s)
85
+ @log.error("-> input:")
86
+ @log.error(document)
87
+ @log.error("--> priv_key_path: #{priv_key_path}")
88
+ end
89
+
90
+ return nil
91
+ end
92
+
93
+ if @log.debug?
94
+ @log.debug("[Signatures.signRequest] key initialized")
95
+ end
19
96
 
20
97
  digest = OpenSSL::Digest::SHA256.new
21
98
 
22
- whole_signature = ec.sign(digest, whole_sign)
99
+ whole_sign = document.to_json + timestamp.to_s
23
100
 
24
- return Base64.encode64(whole_signature).gsub(/\n/, "")
101
+ if @log.debug?
102
+ @log.debug("[Signatures.signRequest] whole_to_sign: "+ whole_sign)
103
+ end
104
+
105
+ whole_signature = nil
106
+
107
+ begin
108
+
109
+ whole_signature = ec.sign(digest, whole_sign)
110
+
111
+ rescue => e
112
+ if @log.error?
113
+ @log.error("[Signatures.signRequest] ec.sign raised exception:")
114
+ @log.error("#{e.class}, #{e.message}")
115
+ @log.error("-> timestamp: "+ timestamp.to_s)
116
+ @log.error("-> input:")
117
+ @log.error(document)
118
+ @log.error("--> priv_key_path: #{priv_key_path}")
119
+ @log.error("--> whole_sign: #{whole_sign}")
120
+ end
121
+
122
+ raise e
123
+ end
124
+
125
+ if whole_signature == nil
126
+ if @log.error?
127
+ @log.error("[Signatures.signRequest] failed to sign")
128
+ @log.error("-> timestamp: "+ timestamp.to_s)
129
+ @log.error("-> input:")
130
+ @log.error(document)
131
+ @log.error("--> priv_key_path: #{priv_key_path}")
132
+ @log.error("--> whole_sign: #{whole_sign}")
133
+ end
134
+
135
+ return nil
136
+ end
137
+
138
+ if @log.debug?
139
+ @log.debug("[Signatures.signRequest] whole_signature raw: "+ Base64.encode64(whole_signature))
140
+ end
141
+
142
+ return Base64.encode64(whole_signature).gsub(/\n/, "")
25
143
  end
26
144
 
27
145
  def verifySignature(what_to_check, signature, public_key)
@@ -29,19 +147,128 @@ module VChainClient
29
147
  pub_key += public_key
30
148
  pub_key += "\n-----END PUBLIC KEY-----"
31
149
 
32
- ec = OpenSSL::PKey::EC.new(pub_key)
150
+ if @log.debug?
151
+ @log.debug("[Signatures.verifySignature] input:")
152
+ @log.debug("-> what_to_check: #{what_to_check}")
153
+ @log.debug("-> signature: "+ Base64.encode64(signature))
154
+ @log.debug("-> public_key: "+ pub_key)
155
+ end
156
+
157
+ ec = nil
158
+
159
+ begin
160
+
161
+ ec = OpenSSL::PKey::EC.new(pub_key)
162
+
163
+ rescue => e
164
+ if @log.error?
165
+ @log.error("[Signatures.verifySignature] OpenSSL::PKey::EC.new raised exception:")
166
+ @log.error("#{e.class}, #{e.message}")
167
+ @log.debug("-> what_to_check: #{what_to_check}")
168
+ @log.debug("-> signature: "+ Base64.encode64(signature))
169
+ @log.debug("-> public_key: "+ pub_key)
170
+ @log.error(document)
171
+ @log.error("--> pub_key: #{pub_key}")
172
+ end
173
+
174
+ raise e
175
+ end
176
+
177
+ if ec == nil
178
+ if @log.error?
179
+ @log.error("[Signatures.verifySignature] failed init EC key")
180
+ @log.debug("-> what_to_check: #{what_to_check}")
181
+ @log.debug("-> signature: "+ Base64.encode64(signature))
182
+ @log.debug("-> public_key: "+ pub_key)
183
+ @log.error("--> pub_key: #{pub_key}")
184
+ end
185
+
186
+ return false
187
+ end
188
+
189
+ if @log.debug?
190
+ @log.debug("[Signatures.verifySignature] key created")
191
+ end
33
192
 
34
193
  digest = OpenSSL::Digest::SHA256.new
35
194
 
36
- return ec.verify(digest, signature, what_to_check)
195
+ begin
196
+
197
+ return ec.verify(digest, signature, what_to_check)
198
+
199
+ rescue => e
200
+ if @log.error?
201
+ @log.error("[Signatures.verifySignature] ec.verify raised exception:")
202
+ @log.error("#{e.class}, #{e.message}")
203
+ @log.debug("-> what_to_check: #{what_to_check}")
204
+ @log.debug("-> signature: "+ Base64.encode64(signature))
205
+ @log.debug("-> public_key: "+ pub_key)
206
+ @log.error(document)
207
+ @log.error("--> signature: "+ Base64.encode64(signature))
208
+ @log.error("--> what_to_check: #{what_to_check}")
209
+ end
210
+
211
+ raise e
212
+ end
37
213
  end
38
214
 
39
215
  def signVerification(verification_type, data, timestamp)
40
216
 
217
+ OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)
218
+
41
219
  this_client_id = @config["blockstack"]["client_id"]
42
220
  priv_key_path = @config["private_key_location"]
43
221
 
44
- priv_key = File.read(priv_key_path)
222
+ if @log.debug?
223
+ @log.debug("[Signatures.signVerification] input:")
224
+ @log.debug("-> this_client_id: #{this_client_id}")
225
+ @log.debug("-> timestamp: "+ timestamp.to_s)
226
+ @log.debug("-> verification_type: #{verification_type}")
227
+ @log.debug("-> key path: #{priv_key_path}")
228
+ @log.debug("-> data:")
229
+ @log.debug(data)
230
+ end
231
+
232
+ priv_key = nil
233
+
234
+ begin
235
+
236
+ priv_key = File.read(priv_key_path)
237
+
238
+ rescue => e
239
+ if @log.error?
240
+ @log.error("[Signatures.signVerification] File.read raised exception:")
241
+ @log.error("#{e.class}, #{e.message}")
242
+ @log.error("-> this_client_id: #{this_client_id}")
243
+ @log.error("-> timestamp: "+ timestamp.to_s)
244
+ @log.error("-> verification_type: #{verification_type}")
245
+ @log.error("-> key path: #{priv_key_path}")
246
+ @log.error("-> data:")
247
+ @log.error(data)
248
+ @log.error("--> priv_key_path: #{priv_key_path}")
249
+ end
250
+
251
+ raise e
252
+ end
253
+
254
+ if priv_key == nil
255
+ if @log.error?
256
+ @log.error("[Signatures.signVerification] failed to load private key")
257
+ @log.error("-> this_client_id: #{this_client_id}")
258
+ @log.error("-> timestamp: "+ timestamp.to_s)
259
+ @log.error("-> verification_type: #{verification_type}")
260
+ @log.error("-> key path: #{priv_key_path}")
261
+ @log.error("-> data:")
262
+ @log.error(data)
263
+ @log.error("--> priv_key_path: #{priv_key_path}")
264
+ end
265
+
266
+ return nil
267
+ end
268
+
269
+ if @log.debug?
270
+ @log.debug("[Signatures.signVerification] priv key loaded")
271
+ end
45
272
 
46
273
  output = {}
47
274
 
@@ -49,6 +276,10 @@ module VChainClient
49
276
  field = rec[0]
50
277
  value = rec[1]
51
278
 
279
+ if @log.debug?
280
+ @log.debug("[Signatures.signVerification] field: #{field}, value: #{value}")
281
+ end
282
+
52
283
  if field != 'type' && field != 'client_id'
53
284
 
54
285
  field_hash = Digest::SHA512.hexdigest(field)
@@ -61,17 +292,107 @@ module VChainClient
61
292
  what_to_sign += timestamp.to_s
62
293
  what_to_sign += this_client_id
63
294
 
64
- ec = OpenSSL::PKey::EC.new(priv_key)
295
+ if @log.debug?
296
+ @log.debug("[Signatures.signVerification] field_hash: #{field_hash}")
297
+ @log.debug("[Signatures.signVerification] value_hash: #{value_hash}")
298
+ end
299
+
300
+ ec = nil
301
+
302
+ begin
303
+
304
+ ec = OpenSSL::PKey::EC.new(priv_key)
305
+
306
+ rescue => e
307
+ if @log.error?
308
+ @log.error("[Signatures.signVerification] OpenSSL::PKey::EC.new raised exception:")
309
+ @log.error("#{e.class}, #{e.message}")
310
+ @log.error("-> this_client_id: #{this_client_id}")
311
+ @log.error("-> timestamp: "+ timestamp.to_s)
312
+ @log.error("-> verification_type: #{verification_type}")
313
+ @log.error("-> key path: #{priv_key_path}")
314
+ @log.error("-> data:")
315
+ @log.error(data)
316
+ @log.error("--> priv_key_path: #{priv_key_path}")
317
+ end
318
+
319
+ raise e
320
+ end
321
+
322
+ if ec == nil
323
+ if @log.error?
324
+ @log.error("[Signatures.signVerification] failed init EC key")
325
+ @log.error("-> this_client_id: #{this_client_id}")
326
+ @log.error("-> timestamp: "+ timestamp.to_s)
327
+ @log.error("-> verification_type: #{verification_type}")
328
+ @log.error("-> key path: #{priv_key_path}")
329
+ @log.error("-> data:")
330
+ @log.error(data)
331
+ @log.error("--> priv_key_path: #{priv_key_path}")
332
+ end
333
+
334
+ return nil
335
+ end
336
+
337
+ if @log.debug?
338
+ @log.debug("[Signatures.signVerification] key created")
339
+ end
65
340
 
66
341
  digest = OpenSSL::Digest::SHA256.new
67
342
 
68
- signature = ec.sign(digest, what_to_sign)
343
+ signature = nil
344
+
345
+ begin
346
+
347
+ signature = ec.sign(digest, what_to_sign)
348
+
349
+ rescue => e
350
+ if @log.error?
351
+ @log.error("[Signatures.signVerification] ec.sign raised exception:")
352
+ @log.error("#{e.class}, #{e.message}")
353
+ @log.error("-> this_client_id: #{this_client_id}")
354
+ @log.error("-> timestamp: "+ timestamp.to_s)
355
+ @log.error("-> verification_type: #{verification_type}")
356
+ @log.error("-> key path: #{priv_key_path}")
357
+ @log.error("-> data:")
358
+ @log.error(data)
359
+ @log.error("--> priv_key_path: #{priv_key_path}")
360
+ @log.error("--> what_to_sign: #{what_to_sign}")
361
+ end
362
+
363
+ raise e
364
+ end
365
+
366
+ if signature == nil
367
+ if @log.error?
368
+ @log.error("[Signatures.signVerification] failed to sign")
369
+ @log.error("-> this_client_id: #{this_client_id}")
370
+ @log.error("-> timestamp: "+ timestamp.to_s)
371
+ @log.error("-> verification_type: #{verification_type}")
372
+ @log.error("-> key path: #{priv_key_path}")
373
+ @log.error("-> data:")
374
+ @log.error(data)
375
+ @log.error("--> priv_key_path: #{priv_key_path}")
376
+ @log.error("--> what_to_sign: #{what_to_sign}")
377
+ end
378
+
379
+ return nil
380
+ end
381
+
382
+ if @log.debug?
383
+ @log.debug("[Signatures.signVerification] signature raw: "+ Base64.encode64(signature))
384
+ end
69
385
 
70
386
  output[field] = Base64.encode64(signature).gsub(/\n/, "")
71
387
 
72
388
  end
73
389
  }
74
390
 
391
+ if @log.debug?
392
+ @log.debug("[Signatures.signVerification] output:")
393
+ @log.debug(output)
394
+ end
395
+
75
396
  return output
76
397
  end
77
398
 
@@ -84,18 +405,84 @@ module VChainClient
84
405
  what_to_check += blockstack_client_id
85
406
  what_to_check += sig_version
86
407
 
87
- return self.verifySignature(what_to_check, signature, pubkey)
408
+ if @log.debug?
409
+ @log.debug("[Signatures.checkTreeSignature] input:")
410
+ @log.debug("-> tree_root_hash: #{tree_root_hash}")
411
+ @log.debug("-> blockchain_txid: #{blockchain_txid}")
412
+ @log.debug("-> blockchain_block_hash: #{blockchain_block_hash}")
413
+ @log.debug("-> blockchain_timestamp: #{blockchain_timestamp}")
414
+ @log.debug("-> blockstack_client_id: #{blockstack_client_id}")
415
+ @log.debug("-> sig_version: #{sig_version}")
416
+ @log.debug("-> signature: "+ Base64.encode64(signature))
417
+ @log.debug("-> pubkey: #{pubkey}")
418
+ end
419
+
420
+ begin
421
+
422
+ return self.verifySignature(what_to_check, signature, pubkey)
423
+
424
+ rescue => e
425
+ if @log.error?
426
+ @log.error("[Signatures.checkTreeSignature] verifySignature raised exception:")
427
+ @log.error("#{e.class}, #{e.message}")
428
+ @log.error("-> tree_root_hash: #{tree_root_hash}")
429
+ @log.error("-> blockchain_txid: #{blockchain_txid}")
430
+ @log.error("-> blockchain_block_hash: #{blockchain_block_hash}")
431
+ @log.error("-> blockchain_timestamp: #{blockchain_timestamp}")
432
+ @log.error("-> blockstack_client_id: #{blockstack_client_id}")
433
+ @log.error("-> sig_version: #{sig_version}")
434
+ @log.error("-> signature: "+ Base64.encode64(signature))
435
+ @log.error("-> pubkey: #{pubkey}")
436
+ @log.error("--> what_to_check: #{what_to_check}")
437
+ @log.error("--> signature: "+ Base64.encode64(signature))
438
+ @log.error("--> pubkey: #{pubkey}")
439
+ end
440
+
441
+ raise e
442
+ end
88
443
  end
89
444
 
90
445
  def checkVerificationSignature(field_hash, data_hash, verification_type, timestamp, blockstack_client_id, pubkey, signature)
91
446
 
447
+ if @log.debug?
448
+ @log.debug("[Signatures.checkVerificationSignature] input:")
449
+ @log.debug("-> field_hash: #{field_hash}")
450
+ @log.debug("-> data_hash: #{data_hash}")
451
+ @log.debug("-> verification_type: #{verification_type}")
452
+ @log.debug("-> timestamp: "+ timestamp.to_s)
453
+ @log.debug("-> blockstack_client_id: #{blockstack_client_id}")
454
+ @log.debug("-> signature: "+ Base64.encode64(signature))
455
+ @log.debug("-> pubkey: #{pubkey}")
456
+ end
457
+
92
458
  what_to_check = field_hash
93
459
  what_to_check += data_hash
94
460
  what_to_check += verification_type
95
461
  what_to_check += timestamp.to_s
96
462
  what_to_check += blockstack_client_id
97
463
 
98
- return self.verifySignature(what_to_check, signature, pubkey)
464
+ begin
465
+
466
+ return self.verifySignature(what_to_check, signature, pubkey)
467
+
468
+ rescue => e
469
+ if @log.error?
470
+ @log.error("[Signatures.checkVerificationSignature] verifySignature raised exception:")
471
+ @log.error("#{e.class}, #{e.message}")
472
+ @log.error("-> field_hash: #{field_hash}")
473
+ @log.error("-> data_hash: #{data_hash}")
474
+ @log.error("-> verification_type: #{verification_type}")
475
+ @log.error("-> timestamp: "+ timestamp.to_s)
476
+ @log.error("-> blockstack_client_id: #{blockstack_client_id}")
477
+ @log.error("-> signature: "+ Base64.encode64(signature))
478
+ @log.error("-> pubkey: #{pubkey}")
479
+ @log.error("--> what_to_check: #{what_to_check}")
480
+ @log.error("--> signature: "+ Base64.encode64(signature))
481
+ @log.error("--> pubkey: #{pubkey}")
482
+ end
483
+
484
+ raise e
485
+ end
99
486
  end
100
487
 
101
488
  end