rasn1 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d54d76005f13feca236452aa2a5bfeb4f260d187ae2ad26fbe598b4b6f2bbf8
4
- data.tar.gz: 8554ee3f4a4cf278af9dca1707842dbfe44d0aa495a0b60fe389d9bf08351b07
3
+ metadata.gz: 1889a0ada74e2d9255ce6af799caa441fd2d2cacfaf0097b5afd1730ed9a9fd9
4
+ data.tar.gz: e0453969129814385bd272c362373b12ae0e38201145280c360ac3696dd95cb1
5
5
  SHA512:
6
- metadata.gz: 1c334783007d9fb69cb9176b9f2da4e6db3037aadbaad2c4d8070878e475b47c7700ff779d11b184b4881e54cd0810dfa231365c777d090503360a1d36af7ae2
7
- data.tar.gz: 9a997e1123a8fd23743b03f533e59a5c0d9848713f27910c1b93858c388fd86ecdf8cb7742f6ecd250b5dc5a42d3ec0d48fa4421ddf16c9f118f7e68e7b60ab0
6
+ metadata.gz: 4dc47229a202f0bfa7d56a129cd5e1807009b9147da6e844d5a47589fbf0e8eab9bebbb51f81866a0222eb4a6dbf953d381f266ec332e697b6caf1b894fa6c18
7
+ data.tar.gz: 4fc5127882d478d111452dff0cac6e226d270f47b4798b6a365f735c0bb1402ec417640b48be9839f7336fcf6d526f2a771d90bdc6ed64b0cefc3258d9e22bff
@@ -1,4 +1,3 @@
1
- TargetRubyVersion: 2.3
2
1
  Layout/SpaceAroundEqualsInParameterDefault:
3
2
  EnforcedStyle: no_space
4
3
  Lint/EmptyWhen:
@@ -60,6 +60,8 @@ module RASN1
60
60
  class Model
61
61
 
62
62
  class << self
63
+ # @return [Hash]
64
+ attr_reader :options
63
65
 
64
66
  # Use another model in this model
65
67
  # @param [String,Symbol] name
@@ -90,7 +92,7 @@ module RASN1
90
92
  # @return [void]
91
93
  def inherited(klass)
92
94
  super
93
- root = @root
95
+ root = defined?(@root )? @root : nil
94
96
  klass.class_eval { @root = root }
95
97
  end
96
98
 
@@ -218,7 +220,7 @@ module RASN1
218
220
  # Give type name (aka class name)
219
221
  # @return [String]
220
222
  def type
221
- return @type if @type
223
+ return @type if defined? @type
222
224
  @type = self.to_s.gsub(/.*::/, '')
223
225
  end
224
226
 
@@ -347,7 +349,7 @@ module RASN1
347
349
  root = self.class.class_eval { @root }
348
350
  @root = root[0]
349
351
  @elements = {}
350
- @elements[@root] = get_type(root[1], self.class.class_eval { @options } || {})
352
+ @elements[@root] = get_type(root[1], self.class.options || {})
351
353
  root
352
354
  end
353
355
 
@@ -3,7 +3,7 @@ module RASN1
3
3
 
4
4
  # ASN.1 ANY: accepts any types
5
5
  #
6
- # If `any#value` is `nil`, `any` will be encoded as a {Null} object.
6
+ # If `any#value` is `nil` and Any object is not {#optional?}, `any` will be encoded as a {Null} object.
7
7
  # @author Sylvain Daubert
8
8
  class Any < Base
9
9
 
@@ -13,7 +13,7 @@ module RASN1
13
13
  when Base, Model
14
14
  @value.to_der
15
15
  when nil
16
- Null.new.to_der
16
+ optional? ? '' : Null.new.to_der
17
17
  else
18
18
  @value.to_s
19
19
  end
@@ -25,6 +25,12 @@ module RASN1
25
25
  # @param [Boolean] ber if +true+, accept BER encoding
26
26
  # @return [Integer] total number of parsed bytes
27
27
  def parse!(der, ber: false)
28
+ if der.nil? or der.empty?
29
+ return 0 if optional?
30
+
31
+ raise ASN1Error, "Expected ANY but get nothing"
32
+ end
33
+
28
34
  total_length, = get_data(der, ber)
29
35
  @value = der[0, total_length]
30
36
  total_length
@@ -70,7 +70,7 @@ module RASN1
70
70
  # Get ASN.1 type
71
71
  # @return [String]
72
72
  def self.type
73
- return @type if @type
73
+ return @type if defined? @type
74
74
  @type = self.to_s.gsub(/.*::/, '').gsub(/([a-z0-9])([A-Z])/, '\1 \2').upcase
75
75
  end
76
76
 
@@ -168,14 +168,14 @@ module RASN1
168
168
  # @return [::Boolean,nil] return +nil+ if not tagged, return +true+
169
169
  # if explicit, else +false+
170
170
  def explicit?
171
- @tag.nil? ? @tag : @tag == :explicit
171
+ !defined?(@tag) ? nil : @tag == :explicit
172
172
  end
173
173
 
174
174
  # Say if a tagged type is implicit
175
175
  # @return [::Boolean,nil] return +nil+ if not tagged, return +true+
176
176
  # if implicit, else +false+
177
177
  def implicit?
178
- @tag.nil? ? @tag : @tag == :implicit
178
+ !defined?(@tag) ? nil : @tag == :implicit
179
179
  end
180
180
 
181
181
  # @abstract This method SHOULD be partly implemented by subclasses, which
@@ -211,7 +211,7 @@ module RASN1
211
211
  else # false
212
212
  0
213
213
  end
214
- (@tag_value || self.class::TAG) | CLASSES[@asn1_class] | pc
214
+ tag_value | CLASSES[@asn1_class] | pc
215
215
  end
216
216
 
217
217
  # @abstract This method SHOULD be partly implemented by subclasses to parse
@@ -326,7 +326,7 @@ module RASN1
326
326
  @constructed = options[:constructed]
327
327
  end
328
328
 
329
- @asn1_class = :context if @tag and @asn1_class == :universal
329
+ @asn1_class = :context if defined?(@tag) && (@asn1_class == :universal)
330
330
  end
331
331
 
332
332
  def build_tag?
@@ -349,8 +349,14 @@ module RASN1
349
349
  end
350
350
  end
351
351
 
352
+ def tag_value
353
+ return @tag_value if defined? @tag_value
354
+
355
+ self.class::TAG
356
+ end
357
+
352
358
  def encode_tag
353
- if (@tag_value || self.class::TAG) <= MAX_TAG
359
+ if tag_value <= MAX_TAG
354
360
  [tag].pack('C')
355
361
  else
356
362
  raise ASN1Error, 'multi-byte tag value are not supported'
@@ -426,7 +432,8 @@ module RASN1
426
432
  end
427
433
 
428
434
  def raise_tag_error(tag)
429
- msg = "Expected #{self2name} but get #{tag2name(tag)}"
435
+ msg = name.nil? ? '' : "#{name}: "
436
+ msg << "Expected #{self2name} but get #{tag2name(tag)}"
430
437
  raise ASN1Error, msg
431
438
  end
432
439
 
@@ -434,7 +441,7 @@ module RASN1
434
441
  name = CLASSES.key(tag & 0xc0).to_s.upcase
435
442
  name << " #{tag & Constructed::ASN1_PC > 0 ? 'CONSTRUCTED' : 'PRIMITIVE'}"
436
443
  if implicit? || explicit?
437
- name << ' 0x%02X' % (tag & 0x1f)
444
+ name << ' 0x%02X (0x%02X)' % [tag & 0x1f, tag]
438
445
  else
439
446
  name << ' ' << self.class.type
440
447
  end
@@ -449,7 +456,7 @@ module RASN1
449
456
  type = Types.constants.map { |c| Types.const_get(c) }.
450
457
  select { |klass| klass < Primitive || klass < Constructed }.
451
458
  find { |klass| klass::TAG == itag & 0x1f }
452
- name << " #{type.nil? ? "0x%02X" % (itag & 0x1f) : type.encode_type }"
459
+ name << " #{type.nil? ? "0x%02X (0x%02X)" % [itag & 0x1f, itag] : type.encode_type }"
453
460
  end
454
461
  end
455
462
  end
@@ -95,7 +95,7 @@ module RASN1
95
95
  str << ' ' * level if level > 0
96
96
  str << "#{name} " if name
97
97
  str << "#{type}:"
98
- if @chosen.nil?
98
+ if !defined? @chosen
99
99
  str << ' not chosen!'
100
100
  else
101
101
  str << "\n#{@value[@chosen].inspect(level+1)}"
@@ -105,10 +105,10 @@ module RASN1
105
105
  private
106
106
 
107
107
  def check_chosen
108
- raise ChoiceError if @chosen.nil?
108
+ raise ChoiceError if !defined?(@chosen) || @chosen.nil?
109
109
  end
110
110
  end
111
111
  end
112
112
  end
113
113
 
114
-
114
+
@@ -1,3 +1,3 @@
1
1
  module RASN1
2
- VERSION = '0.6.7'
2
+ VERSION = '0.6.8'
3
3
  end
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.6.7
4
+ version: 0.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Daubert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-18 00:00:00.000000000 Z
11
+ date: 2018-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard