~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/openssl/test_asn1.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
begin
2
 
  require "openssl"
3
 
  require_relative 'utils'
4
 
rescue LoadError
5
 
end
6
 
require 'test/unit'
 
1
require_relative 'utils'
7
2
 
8
3
class  OpenSSL::TestASN1 < Test::Unit::TestCase
9
4
  def test_decode
208
203
      assert_equal(v, OpenSSL::ASN1.decode(type.new(v).to_der).value)
209
204
    end
210
205
  end
 
206
 
 
207
  def test_primitive_cannot_set_infinite_length
 
208
    begin
 
209
      prim = OpenSSL::ASN1::Integer.new(50)
 
210
      assert_equal(false, prim.infinite_length)
 
211
      prim.infinite_length = true
 
212
      flunk('Could set infinite length on primitive value')
 
213
    rescue NoMethodError => e
 
214
      #ok
 
215
    end
 
216
  end
 
217
 
 
218
  def test_decode_all
 
219
    expected = %w{ 02 01 01 02 01 02 02 01 03 }
 
220
    raw = [expected.join('')].pack('H*')
 
221
    ary = OpenSSL::ASN1.decode_all(raw)
 
222
    assert_equal(3, ary.size)
 
223
    ary.each_with_index do |asn1, i|
 
224
      assert_universal(OpenSSL::ASN1::INTEGER, asn1)
 
225
      assert_equal(i + 1, asn1.value)
 
226
    end
 
227
  end
 
228
 
 
229
  def test_create_inf_length_primitive
 
230
    expected = %w{ 24 80 04 01 61 00 00 }
 
231
    raw = [expected.join('')].pack('H*')
 
232
    val = OpenSSL::ASN1::OctetString.new('a')
 
233
    cons = OpenSSL::ASN1::Constructive.new([val,
 
234
                                            OpenSSL::ASN1::EndOfContent.new],
 
235
                                            OpenSSL::ASN1::OCTET_STRING,
 
236
                                            nil,
 
237
                                            :UNIVERSAL)
 
238
    cons.infinite_length = true
 
239
    assert_equal(nil, cons.tagging)
 
240
    assert_equal(raw, cons.to_der)
 
241
    asn1 = OpenSSL::ASN1.decode(raw)
 
242
    assert(asn1.infinite_length)
 
243
    assert_equal(raw, asn1.to_der)
 
244
  end
 
245
 
 
246
  def test_cons_without_inf_length_forbidden
 
247
    assert_raise(OpenSSL::ASN1::ASN1Error) do
 
248
      val = OpenSSL::ASN1::OctetString.new('a')
 
249
      cons = OpenSSL::ASN1::Constructive.new([val],
 
250
                                            OpenSSL::ASN1::OCTET_STRING,
 
251
                                            nil,
 
252
                                            :UNIVERSAL)
 
253
      cons.to_der
 
254
    end
 
255
  end
 
256
 
 
257
  def test_cons_without_array_forbidden
 
258
    assert_raise(OpenSSL::ASN1::ASN1Error) do
 
259
      val = OpenSSL::ASN1::OctetString.new('a')
 
260
      cons = OpenSSL::ASN1::Constructive.new(val,
 
261
                                            OpenSSL::ASN1::OCTET_STRING,
 
262
                                            nil,
 
263
                                            :UNIVERSAL)
 
264
      cons.infinite_length = true
 
265
      cons.to_der
 
266
    end
 
267
  end
 
268
 
 
269
  def test_parse_empty_sequence
 
270
    expected = %w{ A0 07 30 02 30 00 02 01 00 }
 
271
    raw = [expected.join('')].pack('H*')
 
272
    asn1 = OpenSSL::ASN1.decode(raw)
 
273
    assert_equal(raw, asn1.to_der)
 
274
    assert_equal(2, asn1.value.size)
 
275
    seq = asn1.value[0]
 
276
    assert_equal(1, seq.value.size)
 
277
    inner_seq = seq.value[0]
 
278
    assert_equal(0, inner_seq.value.size)
 
279
  end
 
280
 
 
281
  def test_parse_tagged_0_infinite
 
282
    expected = %w{ 30 80 02 01 01 80 01 02 00 00 }
 
283
    raw = [expected.join('')].pack('H*')
 
284
    asn1 = OpenSSL::ASN1.decode(raw)
 
285
    assert_equal(3, asn1.value.size)
 
286
    int = asn1.value[0]
 
287
    assert_universal(OpenSSL::ASN1::INTEGER, int)
 
288
    tagged = asn1.value[1]
 
289
    assert_equal(0, tagged.tag)
 
290
    assert_universal(OpenSSL::ASN1::EOC, asn1.value[2])
 
291
    assert_equal(raw, asn1.to_der)
 
292
  end
 
293
 
 
294
  def test_seq_infinite_length
 
295
    begin
 
296
      content = [ OpenSSL::ASN1::Null.new(nil),
 
297
                  OpenSSL::ASN1::EndOfContent.new ]
 
298
      cons = OpenSSL::ASN1::Sequence.new(content)
 
299
      cons.infinite_length = true
 
300
      expected = %w{ 30 80 05 00 00 00 }
 
301
      raw = [expected.join('')].pack('H*')
 
302
      assert_equal(raw, cons.to_der)
 
303
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
304
    end
 
305
  end
 
306
 
 
307
  def test_set_infinite_length
 
308
    begin
 
309
      content = [ OpenSSL::ASN1::Null.new(nil),
 
310
                  OpenSSL::ASN1::EndOfContent.new() ]
 
311
      cons = OpenSSL::ASN1::Set.new(content)
 
312
      cons.infinite_length = true
 
313
      expected = %w{ 31 80 05 00 00 00 }
 
314
      raw = [expected.join('')].pack('H*')
 
315
      assert_equal(raw, cons.to_der)
 
316
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
317
    end
 
318
  end
 
319
 
 
320
  def test_octet_string_infinite_length
 
321
    begin
 
322
      octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
 
323
                 OpenSSL::ASN1::EndOfContent.new() ]
 
324
      cons = OpenSSL::ASN1::Constructive.new(
 
325
        octets,
 
326
        OpenSSL::ASN1::OCTET_STRING,
 
327
        nil,
 
328
        :UNIVERSAL)
 
329
      cons.infinite_length = true
 
330
      expected = %w{ 24 80 04 03 61 61 61 00 00 }
 
331
      raw = [expected.join('')].pack('H*')
 
332
      assert_equal(raw, cons.to_der)
 
333
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
334
    end
 
335
  end
 
336
 
 
337
  def test_prim_explicit_tagging
 
338
    begin
 
339
      oct_str = OpenSSL::ASN1::OctetString.new("a", 0, :EXPLICIT)
 
340
      expected = %w{ A0 03 04 01 61 }
 
341
      raw = [expected.join('')].pack('H*')
 
342
      assert_equal(raw, oct_str.to_der)
 
343
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
344
    end
 
345
  end
 
346
 
 
347
  def test_prim_explicit_tagging_tag_class
 
348
    begin
 
349
      oct_str = OpenSSL::ASN1::OctetString.new("a", 0, :EXPLICIT)
 
350
      oct_str2 = OpenSSL::ASN1::OctetString.new(
 
351
        "a",
 
352
        0,
 
353
        :EXPLICIT,
 
354
        :CONTEXT_SPECIFIC)
 
355
      assert_equal(oct_str.to_der, oct_str2.to_der)
 
356
    end
 
357
  end
 
358
 
 
359
  def test_prim_implicit_tagging
 
360
    begin
 
361
      int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT)
 
362
      expected = %w{ 80 01 01 }
 
363
      raw = [expected.join('')].pack('H*')
 
364
      assert_equal(raw, int.to_der)
 
365
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
366
    end
 
367
  end
 
368
 
 
369
  def test_prim_implicit_tagging_tag_class
 
370
    begin
 
371
      int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT)
 
372
      int2 = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT, :CONTEXT_SPECIFIC);
 
373
      assert_equal(int.to_der, int2.to_der)
 
374
    end
 
375
  end
 
376
 
 
377
  def test_cons_explicit_tagging
 
378
    begin
 
379
      content = [ OpenSSL::ASN1::PrintableString.new('abc') ]
 
380
      seq = OpenSSL::ASN1::Sequence.new(content, 2, :EXPLICIT)
 
381
      expected = %w{ A2 07 30 05 13 03 61 62 63 }
 
382
      raw = [expected.join('')].pack('H*')
 
383
      assert_equal(raw, seq.to_der)
 
384
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
385
    end
 
386
  end
 
387
 
 
388
  def test_cons_explicit_tagging_inf_length
 
389
    begin
 
390
      content = [ OpenSSL::ASN1::PrintableString.new('abc') ,
 
391
                  OpenSSL::ASN1::EndOfContent.new() ]
 
392
      seq = OpenSSL::ASN1::Sequence.new(content, 2, :EXPLICIT)
 
393
      seq.infinite_length = true
 
394
      expected = %w{ A2 80 30 80 13 03 61 62 63 00 00 00 00 }
 
395
      raw = [expected.join('')].pack('H*')
 
396
      assert_equal(raw, seq.to_der)
 
397
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
398
    end
 
399
  end
 
400
 
 
401
  def test_cons_implicit_tagging
 
402
    begin
 
403
      content = [ OpenSSL::ASN1::Null.new(nil) ]
 
404
      seq = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT)
 
405
      expected = %w{ A1 02 05 00 }
 
406
      raw = [expected.join('')].pack('H*')
 
407
      assert_equal(raw, seq.to_der)
 
408
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
409
    end
 
410
  end
 
411
 
 
412
  def test_cons_implicit_tagging_inf_length
 
413
    begin
 
414
      content = [ OpenSSL::ASN1::Null.new(nil),
 
415
                  OpenSSL::ASN1::EndOfContent.new() ]
 
416
      seq = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT)
 
417
      seq.infinite_length = true
 
418
      expected = %w{ A1 80 05 00 00 00 }
 
419
      raw = [expected.join('')].pack('H*')
 
420
      assert_equal(raw, seq.to_der)
 
421
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
422
    end
 
423
  end
 
424
 
 
425
  def test_octet_string_infinite_length_explicit_tagging
 
426
    begin
 
427
      octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
 
428
                 OpenSSL::ASN1::EndOfContent.new() ]
 
429
      cons = OpenSSL::ASN1::Constructive.new(
 
430
        octets,
 
431
        1,
 
432
        :EXPLICIT)
 
433
      cons.infinite_length = true
 
434
      expected = %w{ A1 80 24 80 04 03 61 61 61 00 00 00 00 }
 
435
      raw = [expected.join('')].pack('H*')
 
436
      assert_equal(raw, cons.to_der)
 
437
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
438
    end
 
439
  end
 
440
 
 
441
  def test_octet_string_infinite_length_implicit_tagging
 
442
    begin
 
443
      octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
 
444
                 OpenSSL::ASN1::EndOfContent.new() ]
 
445
      cons = OpenSSL::ASN1::Constructive.new(
 
446
        octets,
 
447
        0,
 
448
        :IMPLICIT)
 
449
      cons.infinite_length = true
 
450
      expected = %w{ A0 80 04 03 61 61 61 00 00 }
 
451
      raw = [expected.join('')].pack('H*')
 
452
      assert_equal(raw, cons.to_der)
 
453
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
454
    end
 
455
  end
 
456
 
 
457
  def test_recursive_octet_string_infinite_length
 
458
    begin
 
459
      octets_sub1 = [ OpenSSL::ASN1::OctetString.new("\x01"),
 
460
                      OpenSSL::ASN1::EndOfContent.new() ]
 
461
      octets_sub2 = [ OpenSSL::ASN1::OctetString.new("\x02"),
 
462
                      OpenSSL::ASN1::EndOfContent.new() ]
 
463
      container1 = OpenSSL::ASN1::Constructive.new(
 
464
        octets_sub1,
 
465
        OpenSSL::ASN1::OCTET_STRING,
 
466
        nil,
 
467
        :UNIVERSAL)
 
468
      container1.infinite_length = true
 
469
      container2 = OpenSSL::ASN1::Constructive.new(
 
470
        octets_sub2,
 
471
        OpenSSL::ASN1::OCTET_STRING,
 
472
        nil,
 
473
        :UNIVERSAL)
 
474
      container2.infinite_length = true
 
475
      octets3 = OpenSSL::ASN1::OctetString.new("\x03")
 
476
 
 
477
      octets = [ container1, container2, octets3,
 
478
                 OpenSSL::ASN1::EndOfContent.new() ]
 
479
      cons = OpenSSL::ASN1::Constructive.new(
 
480
        octets,
 
481
        OpenSSL::ASN1::OCTET_STRING,
 
482
        nil,
 
483
        :UNIVERSAL)
 
484
      cons.infinite_length = true
 
485
      expected = %w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 }
 
486
      raw = [expected.join('')].pack('H*')
 
487
      assert_equal(raw, cons.to_der)
 
488
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
489
    end
 
490
  end
 
491
 
 
492
  def test_bit_string_infinite_length
 
493
    begin
 
494
      content = [ OpenSSL::ASN1::BitString.new("\x01"),
 
495
                  OpenSSL::ASN1::EndOfContent.new() ]
 
496
      cons = OpenSSL::ASN1::Constructive.new(
 
497
        content,
 
498
        OpenSSL::ASN1::BIT_STRING,
 
499
        nil,
 
500
        :UNIVERSAL)
 
501
      cons.infinite_length = true
 
502
      expected = %w{ 23 80 03 02 00 01 00 00 }
 
503
      raw = [expected.join('')].pack('H*')
 
504
      assert_equal(raw, cons.to_der)
 
505
      assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
 
506
    end
 
507
  end
 
508
 
 
509
  def test_primitive_inf_length
 
510
    assert_raises(OpenSSL::ASN1::ASN1Error) do
 
511
      spec = %w{ 02 80 02 01 01 00 00 }
 
512
      raw = [spec.join('')].pack('H*')
 
513
      OpenSSL::ASN1.decode(raw)
 
514
      OpenSSL::ASN1.decode_all(raw)
 
515
    end
 
516
  end
 
517
 
 
518
  def test_recursive_octet_string_parse
 
519
    test = %w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 }
 
520
    raw = [test.join('')].pack('H*')
 
521
    asn1 = OpenSSL::ASN1.decode(raw)
 
522
    assert_equal(OpenSSL::ASN1::Constructive, asn1.class)
 
523
    assert_universal(OpenSSL::ASN1::OCTET_STRING, asn1)
 
524
    assert_equal(true, asn1.infinite_length)
 
525
    assert_equal(4, asn1.value.size)
 
526
    nested1 = asn1.value[0]
 
527
    assert_equal(OpenSSL::ASN1::Constructive, nested1.class)
 
528
    assert_universal(OpenSSL::ASN1::OCTET_STRING, nested1)
 
529
    assert_equal(true, nested1.infinite_length)
 
530
    assert_equal(2, nested1.value.size)
 
531
    oct1 = nested1.value[0]
 
532
    assert_universal(OpenSSL::ASN1::OCTET_STRING, oct1)
 
533
    assert_equal(false, oct1.infinite_length)
 
534
    assert_universal(OpenSSL::ASN1::EOC, nested1.value[1])
 
535
    assert_equal(false, nested1.value[1].infinite_length)
 
536
    nested2 = asn1.value[1]
 
537
    assert_equal(OpenSSL::ASN1::Constructive, nested2.class)
 
538
    assert_universal(OpenSSL::ASN1::OCTET_STRING, nested2)
 
539
    assert_equal(true, nested2.infinite_length)
 
540
    assert_equal(2, nested2.value.size)
 
541
    oct2 = nested2.value[0]
 
542
    assert_universal(OpenSSL::ASN1::OCTET_STRING, oct2)
 
543
    assert_equal(false, oct2.infinite_length)
 
544
    assert_universal(OpenSSL::ASN1::EOC, nested2.value[1])
 
545
    assert_equal(false, nested2.value[1].infinite_length)
 
546
    oct3 = asn1.value[2]
 
547
    assert_universal(OpenSSL::ASN1::OCTET_STRING, oct3)
 
548
    assert_equal(false, oct3.infinite_length)
 
549
    assert_universal(OpenSSL::ASN1::EOC, asn1.value[3])
 
550
    assert_equal(false, asn1.value[3].infinite_length)
 
551
  end
 
552
 
 
553
  private
 
554
 
 
555
  def assert_universal(tag, asn1)
 
556
    assert_equal(tag, asn1.tag)
 
557
    if asn1.respond_to?(:tagging)
 
558
      assert_nil(asn1.tagging)
 
559
    end
 
560
    assert_equal(:UNIVERSAL, asn1.tag_class)
 
561
  end
 
562
 
211
563
end if defined?(OpenSSL)
 
564