rasn1 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4242a3051584aaa4b2f6d2603e45b1d516da098
4
- data.tar.gz: fe8a39c6b026c8d408cc97417ab470b2cd8c8c88
3
+ metadata.gz: 1db3f68204210c681a17f1d0a367d680906997b8
4
+ data.tar.gz: 8fec0011426f3b19dd61ef1ba57d904e0de14596
5
5
  SHA512:
6
- metadata.gz: 3ef413c1ce1070a8d0d647e66a8022ae38947a71cb8d2b2ce1947379162459fe94fe6d1e53f2b4656d336e48efc66db22b33027010273b22b517ee2994048009
7
- data.tar.gz: 2998c9805c9e0f017b5e1cb994ddb26de41e393f670da34429de4104eaa319cb7fdeeefd60badd75c08aacb3b43be6f0cd9c915e3d2a59670987066be3986100
6
+ metadata.gz: d1df79db814d23882b371fbe165c5966e19b75f17bacd9b97f2f9ad8b6dad967e060f01eff201389192562c54ad011ead484a1f0ce941dce33cdf081cc19978d
7
+ data.tar.gz: 7e4184159aee769387c5ecb62f6d1b02e48f477780597cc2dcd50d8bf6f1be93613e8319c90fdfdf4967e1f4f98ca9187c06911fa396fc229d40e4c461c12e3f
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /pkg/
9
9
  /spec/reports/
10
10
  /tmp/
11
+ /vendor/
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1
4
3
  - 2.2.7
5
4
  - 2.3.4
6
5
  - 2.4.1
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/rasn1.svg)](https://badge.fury.io/rb/rasn1)
2
+ [![Build Status](https://travis-ci.org/sdaubert/rasn1.svg?branch=master)](https://travis-ci.org/sdaubert/rasn1)
2
3
 
3
4
  # Rasn1
4
5
 
@@ -29,9 +30,15 @@ Record ::= SEQUENCE {
29
30
  room [0] INTEGER OPTIONAL,
30
31
  house [1] INTEGER DEFAULT 0
31
32
  }
33
+
34
+ ComplexRecord ::= SEQUENCE {
35
+ bool BOOLEAN,
36
+ data [0] EXPLICIT OCTET STRING,
37
+ a_record Record
38
+ }
32
39
  ```
33
40
 
34
- ## Create a ASN.1 model
41
+ ### Create a ASN.1 model
35
42
 
36
43
  ```ruby
37
44
  class Record < RASN1::Model
@@ -48,12 +55,12 @@ More comple classes may be designed by nesting simple classes. For example:
48
55
  class ComplexRecord < RASN1::Model
49
56
  sequence :cplx_record,
50
57
  content: [boolean(:bool),
51
- octet_string(:data, explicit: 0),
52
- model(:a_record, Record)]
58
+ octet_string(:data, explicit: 0),
59
+ model(:a_record, Record)]
53
60
  end
54
61
  ```
55
62
 
56
- ## Parse a DER-encoded string
63
+ ### Parse a DER-encoded string
57
64
  ```ruby
58
65
  record = Record.parse(der_string)
59
66
  record[:id] # => RASN1::Types::Integer
@@ -66,9 +73,16 @@ record[:room].optional # => true
66
73
  record[:house].default # => 0
67
74
 
68
75
  record[:id].to_der # => String
76
+
77
+ cplx_record = ComplexRecord.parse(der_string)
78
+ cplx_record[:bool] # => RASN1::Types::Boolean
79
+ cplx_record[:bool].value # => TrueClass/FalseClass
80
+ cplx_record[:data].value # => String
81
+ cplx_record[:data].explicit? # => true
82
+ cplx_record[:a_record] # => Record
69
83
  ```
70
84
 
71
- ## Generate a DER-encoded string
85
+ ### Generate a DER-encoded string
72
86
  ```ruby
73
87
  record = Record.new(id: 12)
74
88
  record[:id].to_i # => 12
@@ -85,7 +99,10 @@ record.set id: 124, house: 155
85
99
  record.to_der # => String
86
100
  ```
87
101
 
102
+ ### More information
103
+
104
+ see https://github.com/sdaubert/rasn1/wiki
105
+
88
106
  ## Contributing
89
107
 
90
108
  Bug reports and pull requests are welcome on GitHub at https://github.com/sdaubert/rasn1.
91
-
@@ -174,6 +174,7 @@ module RASN1
174
174
  # @param [String] str
175
175
  # @param [Boolean] ber accept BER encoding or not
176
176
  # @return [Model]
177
+ # @raise [ASN1Error] error on parsing
177
178
  def parse(str, ber: false)
178
179
  model = new
179
180
  model.parse! str, ber: ber
@@ -244,6 +245,7 @@ module RASN1
244
245
  # @param [String] str
245
246
  # @param [Boolean] ber accept BER encoding or not
246
247
  # @return [Integer] number of parsed bytes
248
+ # @raise [ASN1Error] error on parsing
247
249
  def parse!(str, ber: false)
248
250
  @elements[@root].parse!(str.dup.force_encoding('BINARY'), ber: ber)
249
251
  end
@@ -258,6 +260,10 @@ module RASN1
258
260
  end
259
261
  end
260
262
 
263
+ def inspect(level=0)
264
+ ' ' * level + "#{type} #{root.inspect(-level)}"
265
+ end
266
+
261
267
  private
262
268
 
263
269
  def is_composed?(el)
@@ -200,6 +200,11 @@ module RASN1
200
200
  value_to_der.size
201
201
  end
202
202
 
203
+ def inspect(level=0)
204
+ str = ''
205
+ str << ' ' * level if level > 0
206
+ str << "#{name} #{type}: #{value}"
207
+ end
203
208
 
204
209
  private
205
210
 
@@ -8,6 +8,30 @@ module RASN1
8
8
  class Constructed < Base
9
9
  # Constructed value
10
10
  ASN1_PC = 0x20
11
+
12
+ def inspect(level=0)
13
+ case @value
14
+ when Array
15
+ str = ''
16
+ str << ' ' * level if level > 0
17
+ level = level.abs
18
+ str << "#{type}:\n"
19
+ level += 1
20
+ @value.each do |item|
21
+ case item
22
+ when Base, Model
23
+ p item
24
+ next if item.optional? and item.value.nil?
25
+ str << ' ' * level + "#{item.inspect(level)}\n"
26
+ else
27
+ str << item.inspect
28
+ end
29
+ end
30
+ str
31
+ else
32
+ super
33
+ end
34
+ end
11
35
  end
12
36
  end
13
37
  end
@@ -13,6 +13,12 @@ module RASN1
13
13
  class OctetString < Primitive
14
14
  TAG = 0x04
15
15
 
16
+ def inspect(level=0)
17
+ str = ''
18
+ str << ' ' * level if level > 0
19
+ str << "#{name} #{type}: #{value.inspect}"
20
+ end
21
+
16
22
  private
17
23
 
18
24
  def value_to_der
@@ -30,4 +36,3 @@ module RASN1
30
36
  end
31
37
  end
32
38
  end
33
-
@@ -54,6 +54,28 @@ module RASN1
54
54
  @value = []
55
55
  end
56
56
 
57
+ def inspect(level=0)
58
+ str = ''
59
+ str << ' ' * level if level > 0
60
+ level = level.abs
61
+ str << "#{type}:\n"
62
+ level += 1
63
+ @value.each do |item|
64
+ case item
65
+ when Base, Model
66
+ next if item.optional? and item.value.nil?
67
+ str << ' ' * level + "#{item.inspect(level)}"
68
+ str << "\n" unless item.is_a?(Model)
69
+ when Hash
70
+ type = of_type_class.new(item)
71
+ str << ' ' * level + "#{type.inspect(level)}"
72
+ else
73
+ str << ' ' * level + "#{item.inspect}\n"
74
+ end
75
+ end
76
+ str
77
+ end
78
+
57
79
  private
58
80
 
59
81
  def of_type_class
@@ -125,4 +147,3 @@ module RASN1
125
147
  end
126
148
  end
127
149
  end
128
-
@@ -1,3 +1,3 @@
1
1
  module RASN1
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -6,6 +6,7 @@ require 'rasn1/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'rasn1'
8
8
  spec.version = RASN1::VERSION
9
+ spec.license = 'MIT'
9
10
  spec.authors = ['Sylvain Daubert']
10
11
  spec.email = ['sylvain.daubert@laposte.net']
11
12
 
@@ -16,8 +17,6 @@ Gem::Specification.new do |spec|
16
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
18
  f.match(%r{^(test|spec|features)/})
18
19
  end
19
- #spec.bindir = 'exe'
20
- #spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
20
  spec.require_paths = ['lib']
22
21
 
23
22
  spec.required_ruby_version = '>= 2.1.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasn1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Daubert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -115,13 +115,13 @@ files:
115
115
  - lib/rasn1/types/sequence_of.rb
116
116
  - lib/rasn1/types/set.rb
117
117
  - lib/rasn1/types/set_of.rb
118
- - lib/rasn1/types/set_spec.rb
119
118
  - lib/rasn1/types/utf8_string.rb
120
119
  - lib/rasn1/types/visible_string.rb
121
120
  - lib/rasn1/version.rb
122
121
  - rasn1.gemspec
123
122
  homepage: https://github.com/sdaubert/rasn1
124
- licenses: []
123
+ licenses:
124
+ - MIT
125
125
  metadata: {}
126
126
  post_install_message:
127
127
  rdoc_options: []
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  requirements: []
141
141
  rubyforge_project:
142
- rubygems_version: 2.5.2
142
+ rubygems_version: 2.6.13
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: Ruby ASN.1 library
@@ -1,61 +0,0 @@
1
- module RASN1
2
- module Types
3
-
4
- describe Set do
5
- before(:each) do
6
- @set = Set.new(:set)
7
- @bool = Boolean.new(:bool, default: true)
8
- @int = Integer.new(:int)
9
- @bs = BitString.new(:bs)
10
- @set.value = [@bool, @int, @bs]
11
-
12
- @no_bool_der = binary("\x30\x09\x02\x01\x2A\x03\x04\x01\x01\x04\x06")
13
- @der = binary("\x30\x0c\x01\x01\x00\x02\x01\x2A\x03\x04\x01\x01\x04\x06")
14
- end
15
-
16
- describe '.type' do
17
- it 'gets ASN.1 type' do
18
- expect(Set.type).to eq('SET')
19
- end
20
- end
21
-
22
- describe '#initialize' do
23
- it 'creates an Set with default values' do
24
- set = Set.new(:set)
25
- expect(set).to be_constructed
26
- expect(set).to_not be_optional
27
- expect(set.asn1_class).to eq(:universal)
28
- expect(set.default).to eq(nil)
29
- end
30
- end
31
-
32
- describe '#to_der' do
33
- it 'generates a DER string' do
34
- @int.value = 42
35
- @bs.value = [1,4,6].pack('C3')
36
- @bs.bit_length = 23
37
- expect(@set.to_der).to eq(@no_bool_der)
38
-
39
- @bool.value = false
40
- expect(@set.to_der).to eq(@der)
41
- end
42
- end
43
-
44
- describe '#parse!' do
45
- it 'parses DER string' do
46
- @set.parse!(@der)
47
- expect(@bool.value).to be(false)
48
- expect(@int.value).to eq(42)
49
- expect(@bs.value).to eq([1,4,6].pack('C3'))
50
- expect(@bs.bit_length).to eq(23)
51
-
52
- @set.parse!(@no_bool_der)
53
- expect(@bool.value).to be(true)
54
- expect(@int.value).to eq(42)
55
- expect(@bs.value).to eq([1,4,6].pack('C3'))
56
- expect(@bs.bit_length).to eq(23)
57
- end
58
- end
59
- end
60
- end
61
- end