rasn1 0.16.1 → 0.16.3

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
  SHA256:
3
- metadata.gz: dd8cb204d6f0a5c24e321b88124844057a360272c4e738a0cf1dab3458d93c66
4
- data.tar.gz: 4635d97697b8b91a836f7d7c28b27f0a0ee31a215b6f928f8932d55dfe268b12
3
+ metadata.gz: 56e418f5906034865b82fd6dd271da80be89d601e4298230b3e36919d13dae22
4
+ data.tar.gz: d1f5d699c1e88e10b82a43fd3b9e0105041cb16ab11701751a6183088c2cca33
5
5
  SHA512:
6
- metadata.gz: 07db598261b37e78e8329b7b20427c2f2ba7c24a53cccaed3aa61ba3413180cecdccaa665a6b502f77a2f00163b3af370e96b0ff5098f9ea519c62f1a86f0e47
7
- data.tar.gz: 53a4a006d4a32964d412c92ff8821636d01beb0753d07259c7b08418f97a16d16ccbe47619ae9f999f5d22561e1b1fe1073b446202c242fac38316852fa29035
6
+ metadata.gz: a0efd01e244b3e61260eb50ab1317e2f200a2d2a311ae51293f2e409f20086857ff334edd2019358f7929cc14ae657124733848c99abdce80ad67b91c1377a15
7
+ data.tar.gz: 83ae40e970a4c185fe9652d10e38ac9cd1effa4c28a0362172cf619c675ce400be4bf2935e889b35ace98f3cfb0cee7ce8923d0dc571e5cc6df0cca829036f4e
data/lib/rasn1/errors.rb CHANGED
@@ -26,7 +26,7 @@ module RASN1
26
26
 
27
27
  # CHOICE error: {Types::Choice#chosen} not set
28
28
  class ChoiceError < RASN1::Error
29
- # @param [Types::Base] object
29
+ # @param object [Types::Base]
30
30
  def initialize(object)
31
31
  @object = object
32
32
  super()
@@ -42,7 +42,7 @@ module RASN1
42
42
  # @version 0.11.0
43
43
  # @author Sylvain Daubert
44
44
  class ConstraintError < Error
45
- # @param [Types::Base] object
45
+ # @param object [Types::Base]
46
46
  def initialize(object)
47
47
  @object = object
48
48
  super()
data/lib/rasn1/model.rb CHANGED
@@ -70,9 +70,9 @@ module RASN1
70
70
  class Model # rubocop:disable Metrics/ClassLength
71
71
  # @private Base Element
72
72
  BaseElem = Struct.new(:name, :proc, :content) do
73
- # @param [String,Symbol] name
74
- # @param [Proc] proc
75
- # @param [Array,nil] content
73
+ # @param name [String,Symbol]
74
+ # @param proc [Proc]
75
+ # @param content [Array,nil]
76
76
  def initialize(name, proc, content)
77
77
  check_duplicates(content.map(&:name) + [name]) unless content.nil?
78
78
  super
@@ -80,13 +80,17 @@ module RASN1
80
80
 
81
81
  private
82
82
 
83
- # @return [Array<String>] The duplicate names found in the array
83
+ # @param names [Array<String, Symbol>]
84
+ # @return [Array<String,Symbol>] The duplicate names found in the array
84
85
  def find_all_duplicate_names(names)
85
86
  names.group_by { |name| name }
86
87
  .select { |_name, values| values.length > 1 }
87
88
  .keys
88
89
  end
89
90
 
91
+ # @param names [Array<String, Symbol>]
92
+ # @return [void]
93
+ # @raise [ModelValidationError] duplicate names in model definition
90
94
  def check_duplicates(names)
91
95
  duplicates = find_all_duplicate_names(names)
92
96
  raise ModelValidationError, "Duplicate name #{duplicates.first} found" if duplicates.any?
@@ -113,16 +117,16 @@ module RASN1
113
117
  attr_reader :options
114
118
 
115
119
  # Use another model in this model
116
- # @param [String,Symbol] name
117
- # @param [Class] model_klass
120
+ # @param name [String,Symbol]
121
+ # @param model_klass [Class]
118
122
  # @return [Elem]
119
123
  def model(name, model_klass)
120
124
  @root = ModelElem.new(name, model_klass)
121
125
  end
122
126
 
123
127
  # Use a {Wrapper} around a {Types::Base} or a {Model} object
124
- # @param [Types::Base,Model] element
125
- # @param [Hash] options
128
+ # @param element [Types::Base,Model]
129
+ # @param options [Hash]
126
130
  # @return [WrapElem]
127
131
  # @since 0.12
128
132
  def wrapper(element, options={})
@@ -140,7 +144,7 @@ module RASN1
140
144
  # class Model2 < Model1
141
145
  # root_options implicit: 1
142
146
  # end
143
- # @param [Hash] options
147
+ # @param options [Hash]
144
148
  # @return [void]
145
149
  # @since 0.12.0 may change name through +:name+
146
150
  def root_options(options)
@@ -152,7 +156,7 @@ module RASN1
152
156
  end
153
157
 
154
158
  # On inheritance, create +@root+ class variable
155
- # @param [Class] klass
159
+ # @param klass [Class]
156
160
  # @return [void]
157
161
  def inherited(klass)
158
162
  super
@@ -161,8 +165,8 @@ module RASN1
161
165
  end
162
166
 
163
167
  # @private
164
- # @param [String,Symbol] accel_name
165
- # @param [Class] klass
168
+ # @param accel_name [String,Symbol]
169
+ # @param klass [Class]
166
170
  # @since 0.11.0
167
171
  # @since 0.12.0 track source location on error (adfoster-r7)
168
172
  def define_type_accel_base(accel_name, klass)
@@ -178,8 +182,8 @@ module RASN1
178
182
  end
179
183
 
180
184
  # @private
181
- # @param [String,Symbol] accel_name
182
- # @param [Class] klass
185
+ # @param accel_name [String,Symbol]
186
+ # @param klass [Class]
183
187
  # @since 0.11.0
184
188
  # @since 0.12.0 track source location on error (adfoster-r7)
185
189
  def define_type_accel_of(accel_name, klass)
@@ -195,8 +199,8 @@ module RASN1
195
199
  end
196
200
 
197
201
  # Define an accelarator to access a type in a model definition
198
- # @param [String] accel_name
199
- # @param [Class] klass class to instanciate
202
+ # @param accel_name [String]
203
+ # @param klass [Class] class to instanciate
200
204
  # @since 0.11.0
201
205
  # @since 0.12.0 track source location on error (adfoster-r7)
202
206
  def define_type_accel(accel_name, klass)
@@ -207,8 +211,8 @@ module RASN1
207
211
  end
208
212
  end
209
213
 
210
- # @param [Symbol,String] name name of object in model
211
- # @param [Hash] options
214
+ # @param name [Symbol,String] name of object in model
215
+ # @param options [Hash]
212
216
  # @return [Elem]
213
217
  # @note This method is named +objectid+ and not +object_id+ to not override
214
218
  # +Object#object_id+.
@@ -219,8 +223,8 @@ module RASN1
219
223
  @root = BaseElem.new(name, proc, nil)
220
224
  end
221
225
 
222
- # @param [Symbol,String] name name of object in model
223
- # @param [Hash] options
226
+ # @param name [Symbol,String] name of object in model
227
+ # @param options [Hash]
224
228
  # @return [Elem]
225
229
  # @see Types::Any#initialize
226
230
  def any(name, options={})
@@ -238,8 +242,8 @@ module RASN1
238
242
  end
239
243
 
240
244
  # Parse a DER/BER encoded string
241
- # @param [String] str
242
- # @param [Boolean] ber accept BER encoding or not
245
+ # @param str [String]
246
+ # @param ber [Boolean] accept BER encoding or not
243
247
  # @return [Model]
244
248
  # @raise [ASN1Error] error on parsing
245
249
  def parse(str, ber: false)
@@ -253,20 +257,20 @@ module RASN1
253
257
 
254
258
  # @!method sequence(name, options)
255
259
  # @!scope class
256
- # @param [Symbol,String] name name of object in model
257
- # @param [Hash] options
260
+ # @param name [Symbol,String] name of object in model
261
+ # @param options [Hash]
258
262
  # @return [Elem]
259
263
  # @see Types::Sequence#initialize
260
264
  # @!method set(name, options)
261
265
  # @!scope class
262
- # @param [Symbol,String] name name of object in model
263
- # @param [Hash] options
266
+ # @param name [Symbol,String] name of object in model
267
+ # @param options [Hash]
264
268
  # @return [Elem]
265
269
  # @see Types::Set#initialize
266
270
  # @!method choice(name, options)
267
271
  # @!scope class
268
- # @param [Symbol,String] name name of object in model
269
- # @param [Hash] options
272
+ # @param name [Symbol,String] name of object in model
273
+ # @param options [Hash]
270
274
  # @return [Elem]
271
275
  # @see Types::Choice#initialize
272
276
  %w[sequence set choice].each do |type|
@@ -275,16 +279,16 @@ module RASN1
275
279
 
276
280
  # @!method sequence_of(name, type, options)
277
281
  # @!scope class
278
- # @param [Symbol,String] name name of object in model
279
- # @param [Model, Types::Base] type type for SEQUENCE OF
280
- # @param [Hash] options
282
+ # @param name [Symbol,String] name of object in model
283
+ # @param type [Model, Types::Base] type for SEQUENCE OF
284
+ # @param options [Hash]
281
285
  # @return [Elem]
282
286
  # @see Types::SequenceOf#initialize
283
287
  # @!method set_of(name, type, options)
284
288
  # @!scope class
285
- # @param [Symbol,String] name name of object in model
286
- # @param [Model, Types::Base] type type for SET OF
287
- # @param [Hash] options
289
+ # @param name [Symbol,String] name of object in model
290
+ # @param type [Model, Types::Base] type for SET OF
291
+ # @param options [Hash]
288
292
  # @return [Elem]
289
293
  # @see Types::SetOf#initialize
290
294
  %w[sequence set].each do |type|
@@ -293,80 +297,80 @@ module RASN1
293
297
 
294
298
  # @!method boolean(name, options)
295
299
  # @!scope class
296
- # @param [Symbol,String] name name of object in model
297
- # @param [Hash] options
300
+ # @param name [Symbol,String] name of object in model
301
+ # @param options [Hash]
298
302
  # @return [Elem]
299
303
  # @see Types::Boolean#initialize
300
304
  # @!method integer(name, options)
301
305
  # @!scope class
302
- # @param [Symbol,String] name name of object in model
303
- # @param [Hash] options
306
+ # @param name [Symbol,String] name of object in model
307
+ # @param options [Hash]
304
308
  # @return [Elem]
305
309
  # @see Types::Integer#initialize
306
310
  # @!method bit_string(name, options)
307
311
  # @!scope class
308
- # @param [Symbol,String] name name of object in model
309
- # @param [Hash] options
312
+ # @param name [Symbol,String] name of object in model
313
+ # @param options [Hash]
310
314
  # @return [Elem]
311
315
  # @see Types::BitString#initialize
312
316
  # @!method bmp_string(name, options)
313
317
  # @!scope class
314
- # @param [Symbol,String] name name of object in model
315
- # @param [Hash] options
318
+ # @param name [Symbol,String] name of object in model
319
+ # @param options [Hash]
316
320
  # @return [Elem]
317
321
  # @see Types::BmpString#initialize
318
322
  # @!method octet_string(name, options)
319
323
  # @!scope class
320
- # @param [Symbol,String] name name of object in model
321
- # @param [Hash] options
324
+ # @param name [Symbol,String] name of object in model
325
+ # @param options [Hash]
322
326
  # @return [Elem]
323
327
  # @see Types::OctetString#initialize
324
328
  # @!method null(name, options)
325
329
  # @!scope class
326
- # @param [Symbol,String] name name of object in model
327
- # @param [Hash] options
330
+ # @param name [Symbol,String] name of object in model
331
+ # @param options [Hash]
328
332
  # @return [Elem]
329
333
  # @see Types::Null#initialize
330
334
  # @!method enumerated(name, options)
331
335
  # @!scope class
332
- # @param [Symbol,String] name name of object in model
333
- # @param [Hash] options
336
+ # @param name [Symbol,String] name of object in model
337
+ # @param options [Hash]
334
338
  # @return [Elem]
335
339
  # @see Types::Enumerated#initialize
336
340
  # @!method universal_string(name, options)
337
341
  # @!scope class
338
- # @param [Symbol,String] name name of object in model
339
- # @param [Hash] options
342
+ # @param name [Symbol,String] name of object in model
343
+ # @param options [Hash]
340
344
  # @return [Elem]
341
345
  # @see Types::UniversalString#initialize
342
346
  # @!method utf8_string(name, options)
343
347
  # @!scope class
344
- # @param [Symbol,String] name name of object in model
345
- # @param [Hash] options
348
+ # @param name [Symbol,String] name of object in model
349
+ # @param options [Hash]
346
350
  # @return [Elem]
347
351
  # @see Types::Utf8String#initialize
348
352
  # @!method numeric_string(name, options)
349
353
  # @!scope class
350
- # @param [Symbol,String] name name of object in model
351
- # @param [Hash] options
354
+ # @param name [Symbol,String] name of object in model
355
+ # @param options [Hash]
352
356
  # @return [Elem]
353
357
  # @see Types::NumericString#initialize
354
358
  # @!method printable_string(name, options)
355
359
  # @!scope class
356
- # @param [Symbol,String] name name of object in model
357
- # @param [Hash] options
360
+ # @param name [Symbol,String] name of object in model
361
+ # @param options [Hash]
358
362
  # @return [Elem]
359
363
  # @see Types::PrintableString#initialize
360
364
  # @!method visible_string(name, options)
361
365
  # @!scope class
362
- # @param [Symbol,String] name name of object in model
363
- # @param [Hash] options
366
+ # @param name [Symbol,String] name of object in model
367
+ # @param options [Hash]
364
368
  # @return [Elem]
365
369
  # @see Types::VisibleString#initialize
366
370
  # @!method ia5_string(name, options)
367
371
  # @!scope class
368
- # @param [Symbol,String] name name of object in model
369
- # @param [Hash] options
372
+ # @param name [Symbol,String] name of object in model
373
+ # @param options [Hash]
370
374
  # @return [Elem]
371
375
  # @see Types::IA5String#initialize
372
376
  Types.primitives.each do |prim|
@@ -380,20 +384,21 @@ module RASN1
380
384
  attr_reader :root
381
385
 
382
386
  # Create a new instance of a {Model}
383
- # @param [Hash] args
387
+ # @param args [Hash]
384
388
  def initialize(args={})
385
389
  @elements = {}
386
390
  generate_root(args)
391
+ args.delete(:name)
387
392
  lazy_initialize(args) unless args.empty?
388
393
  end
389
394
 
390
395
  # @overload [](name)
391
396
  # Access an element of the model by its name
392
- # @param [Symbol] name
397
+ # @param name [Symbol]
393
398
  # @return [Model, Types::Base, Wrapper]
394
399
  # @overload [](idx)
395
400
  # Access an element of root element by its index. Root element must be a {Types::Sequence} or {Types::SequenceOf}.
396
- # @param [Integer] idx
401
+ # @param idx [Integer]
397
402
  # @return [Model, Types::Base, Wrapper]
398
403
  def [](name_or_idx)
399
404
  case name_or_idx
@@ -408,8 +413,8 @@ module RASN1
408
413
  end
409
414
 
410
415
  # Set value of element +name+. Element should be a {Types::Base}.
411
- # @param [String,Symbol] name
412
- # @param [Object] value
416
+ # @param name [String,Symbol]
417
+ # @param value [Object]
413
418
  # @return [Object] value
414
419
  def []=(name, value)
415
420
  # Here, use #[] to force generation for lazy elements
@@ -419,7 +424,8 @@ module RASN1
419
424
  end
420
425
 
421
426
  # clone @elements and initialize @root from this new @element.
422
- def initialize_copy(_other)
427
+ # @return [void]
428
+ def initialize_copy(*)
423
429
  @elements = @elements.clone
424
430
  @root = @elements[@root_name]
425
431
  end
@@ -454,8 +460,8 @@ module RASN1
454
460
  end
455
461
 
456
462
  # Parse a DER/BER encoded string, and modify object in-place.
457
- # @param [String] der
458
- # @param [Boolean] ber accept BER encoding or not
463
+ # @param der [String]
464
+ # @param ber [Boolean] accept BER encoding or not
459
465
  # @return [Integer] number of parsed bytes
460
466
  # @raise [ASN1Error] error on parsing
461
467
  def parse!(der, ber: false)
@@ -463,6 +469,7 @@ module RASN1
463
469
  end
464
470
 
465
471
  # @private
472
+ # @!macro do_parse
466
473
  # @see Types::Base#do_parse
467
474
  def do_parse(der, ber: false)
468
475
  root.do_parse(der, ber: ber)
@@ -473,8 +480,8 @@ module RASN1
473
480
  # @return [Object,nil]
474
481
  # @overload value(name, *args)
475
482
  # Direct access to the value of +name+ (nested) element of model.
476
- # @param [String,Symbol] name
477
- # @param [Array<Integer,String,Symbol>] args more argument to access element. May be
483
+ # @param name [String,Symbol]
484
+ # @param args [Array<Integer,String,Symbol>] more argument to access element. May be
478
485
  # used to access content of a SequenceOf or a SetOf
479
486
  # @return [Object,nil]
480
487
  # @return [Object,nil]
@@ -507,10 +514,11 @@ module RASN1
507
514
  end
508
515
  end
509
516
 
510
- # Return a hash image of model
511
- # @return [Hash]
512
517
  # Delegate some methods to root element
513
- # @param [Symbol] meth
518
+ # @param meth [Symbol]
519
+ # @param args [Array]
520
+ # @param kwargs [Hash]
521
+ # @return [Object]
514
522
  def method_missing(meth, *args, **kwargs)
515
523
  if root.respond_to?(meth)
516
524
  root.send(meth, *args, **kwargs)
@@ -519,18 +527,20 @@ module RASN1
519
527
  end
520
528
  end
521
529
 
530
+ # @param meth [Symbol]
522
531
  # @return [Boolean]
523
532
  def respond_to_missing?(meth, *)
524
533
  root.respond_to?(meth) || super
525
534
  end
526
535
 
536
+ # @param level [Integer]
527
537
  # @return [String]
528
538
  def inspect(level=0)
529
539
  "#{' ' * level}(#{type}) #{root.inspect(-level)}"
530
540
  end
531
541
 
532
542
  # Objects are equal if they have same class AND same DER
533
- # @param [Base] other
543
+ # @param other [Base]
534
544
  # @return [Boolean]
535
545
  def ==(other)
536
546
  (other.class == self.class) && (other.to_der == self.to_der)
@@ -539,7 +549,7 @@ module RASN1
539
549
  protected
540
550
 
541
551
  # Initialize model elements from +args+
542
- # @param [Hash,Array] args
552
+ # @param args [Hash,Array]
543
553
  # @return [void]
544
554
  def lazy_initialize(args)
545
555
  case args
@@ -551,7 +561,7 @@ module RASN1
551
561
  end
552
562
 
553
563
  # Initialize an element from a hash
554
- # @param [Hash] args
564
+ # @param args [Hash]
555
565
  # @return [void]
556
566
  def lazy_initialize_hash(args)
557
567
  args.each do |name, value|
@@ -567,7 +577,7 @@ module RASN1
567
577
  end
568
578
 
569
579
  # Initialize an sequence element from an array
570
- # @param [Array] ary
580
+ # @param ary [Array]
571
581
  # @return [void]
572
582
  def lazy_initialize_array(ary)
573
583
  raise Error, 'Only sequence types may be initialized with an array' unless SEQUENCE_TYPES.any? { |klass| root.is_a?(klass) }
@@ -578,7 +588,7 @@ module RASN1
578
588
  end
579
589
 
580
590
  # Give a (nested) element from its name
581
- # @param [String, Symbol] name
591
+ # @param name [String, Symbol]
582
592
  # @return [Model, Types::Base, nil]
583
593
  def by_name(name)
584
594
  elt = self[name]
@@ -596,6 +606,8 @@ module RASN1
596
606
 
597
607
  private
598
608
 
609
+ # @param args [Hash]
610
+ # @return [void]
599
611
  def generate_root(args)
600
612
  opts = args.slice(:name, :explicit, :implicit, :optional, :class, :default, :constructed, :tag_value)
601
613
  root = self.class.class_eval { @root }
@@ -606,6 +618,9 @@ module RASN1
606
618
  @elements[@root_name] = @root
607
619
  end
608
620
 
621
+ # @param elt [BaseElem, ModelElem,WrapElem]
622
+ # @param opts [Hash]
623
+ # @return[Types::Base,Model,Wrapper]
609
624
  def generate_element(elt, opts={})
610
625
  case elt
611
626
  when BaseElem
@@ -618,6 +633,9 @@ module RASN1
618
633
  end
619
634
  end
620
635
 
636
+ # @param elt [WrapElem]
637
+ # @param opts [Hash]
638
+ # @return[Wrapper]
621
639
  def generate_wrapper_element(elt, opts)
622
640
  wrapped = elt.element.is_a?(ModelElem) ? elt.element.klass : generate_element(elt.element)
623
641
  options = elt.options.merge(opts)
@@ -628,6 +646,9 @@ module RASN1
628
646
  wrapper
629
647
  end
630
648
 
649
+ # @param elt [BaseElem]
650
+ # @param opts [Hash]
651
+ # @return[Types::Base]
631
652
  def generate_base_element(elt, opts)
632
653
  element = elt.proc.call(opts)
633
654
  return element if elt.content.nil?
@@ -642,6 +663,8 @@ module RASN1
642
663
  # @author sdaubert
643
664
  # @author lemontree55
644
665
  # @author adfoster-r7
666
+ # @param element [Model, Types::Base, Wrapper,nil]
667
+ # @return [Hash]
645
668
  def private_to_h(element=nil) # rubocop:disable Metrics/CyclomaticComplexity
646
669
  my_element = element || root
647
670
  value = case my_element
@@ -665,6 +688,8 @@ module RASN1
665
688
  end
666
689
  end
667
690
 
691
+ # @param elt [Model]
692
+ # @return [Hash]
668
693
  def model_to_h(elt)
669
694
  hsh = elt.to_h
670
695
  if root.is_a?(Types::Choice)
@@ -674,6 +699,8 @@ module RASN1
674
699
  end
675
700
  end
676
701
 
702
+ # @param elt [Types::SequenceOf]
703
+ # @return [Array<Hash>]
677
704
  def sequence_of_to_h(elt)
678
705
  if elt.of_type < Model
679
706
  elt.value&.map { |el| el.to_h.values.first }
@@ -682,6 +709,8 @@ module RASN1
682
709
  end
683
710
  end
684
711
 
712
+ # @param seq [Types::Sequence]
713
+ # @return [Hash]
685
714
  def sequence_to_h(seq)
686
715
  ary = seq.value&.map do |el|
687
716
  next if el.optional? && el.value.nil?
@@ -698,6 +727,9 @@ module RASN1
698
727
  ary.compact.to_h
699
728
  end
700
729
 
730
+ # @param elt [Types::Choice]
731
+ # @return [Hash]
732
+ # @raise [ChoiceError] no chosen element
701
733
  def choice_to_h(elt)
702
734
  raise ChoiceError.new(elt) if elt.chosen.nil?
703
735
 
@@ -705,10 +737,14 @@ module RASN1
705
737
  { chosen.name => private_to_h(chosen) }
706
738
  end
707
739
 
740
+ # @param key [String,Symbol]
741
+ # @return [Symbol]
708
742
  def unwrap_keyname(key)
709
743
  key.to_s.delete_suffix('_wrapper').to_sym
710
744
  end
711
745
 
746
+ # @param wrap [Wrapper]
747
+ # @return [Hash]
712
748
  def wrapper_to_h(wrap)
713
749
  el = wrap.element
714
750
  case el