rasn1 0.6.0 → 0.6.1

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
  SHA1:
3
- metadata.gz: 38f025ab48cabca987b7e0813332e9228ab1933a
4
- data.tar.gz: 84ed306ccd84238b89ea1e02f2b73a9bf8558f41
3
+ metadata.gz: 8afb668f624fee248cce8fe52bd51b57dc42a88f
4
+ data.tar.gz: 041fe9c1bf42b220b616d4c751b7acef8e98afc8
5
5
  SHA512:
6
- metadata.gz: a3b4edc322d49eb3e68ecccada5e5022c7f0d08e183a71b991faf62b488c6e70753e531e0a7b5d7bce6bf0a9f4a41da74162f476631097abfd42631962856529
7
- data.tar.gz: 6144b8db2f536adc0f8d0f61727724b556058fadd14f02666d5f91df8dfd7f0b92a93a2e30c4a51526a61d6997f3b102e8a2b876d2fc74556190e8cf512854cf
6
+ metadata.gz: 32f94898893e828e3bc37569bde1cea6e72c577e64748aa87e6c30ffca7065eeb0365c2f10c2d26db89ff71ca2d03f2d9c8281175544d3af0b87b6f263fec24a
7
+ data.tar.gz: 5261798fa1336eb18234316d4224bfb12a55fe6a6583c36fe01cc2f40eeeb68cbcb839af83d5ccd3ec031c7eb72c2d50143568ce267ce03587dc28020e564f6f
data/lib/rasn1/model.rb CHANGED
@@ -167,9 +167,26 @@ module RASN1
167
167
  # @param [Symbol,String] name name of object in model
168
168
  # @param [Hash] options
169
169
  # @see Types::Utf8String#initialize
170
+ # @method numeric_string(name, options)
171
+ # @param [Symbol,String] name name of object in model
172
+ # @param [Hash] options
173
+ # @see Types::NumericString#initialize
174
+ # @method printable_string(name, options)
175
+ # @param [Symbol,String] name name of object in model
176
+ # @param [Hash] options
177
+ # @see Types::PrintableString#initialize
178
+ # @method visible_string(name, options)
179
+ # @param [Symbol,String] name name of object in model
180
+ # @param [Hash] options
181
+ # @see Types::VisibleString#initialize
182
+ # @method ia5_string(name, options)
183
+ # @param [Symbol,String] name name of object in model
184
+ # @param [Hash] options
185
+ # @see Types::IA5String#initialize
170
186
  Types.primitives.each do |prim|
171
187
  next if prim == Types::ObjectId
172
- class_eval "def #{prim.type.downcase.gsub(/\s+/, '_')}(name, options={})\n" \
188
+ method_name = prim.type.gsub(/([a-z0-9])([A-Z])/, '\1_\2').downcase.gsub(/\s+/, '_')
189
+ class_eval "def #{method_name}(name, options={})\n" \
173
190
  " options.merge!(name: name)\n" \
174
191
  " proc = Proc.new do |opts|\n" \
175
192
  " #{prim.to_s}.new(options.merge(opts))\n" \
@@ -74,6 +74,17 @@ module RASN1
74
74
  @type = self.to_s.gsub(/.*::/, '').gsub(/([a-z0-9])([A-Z])/, '\1 \2').upcase
75
75
  end
76
76
 
77
+ # Parse a DER or BER string
78
+ # @param [String] der_or_ber string to parse
79
+ # @param [Hash] options
80
+ # @option options [Boolean] :ber if +true+, parse a BER string, else a DER one
81
+ # @note More options are supported. See {Base#initialize}.
82
+ def self.parse(der_or_ber, options={})
83
+ obj = self.new(options)
84
+ obj.parse!(der_or_ber, ber: options[:ber])
85
+ obj
86
+ end
87
+
77
88
 
78
89
  # @overload initialize(options={})
79
90
  # @param [Hash] options
@@ -113,12 +124,18 @@ module RASN1
113
124
 
114
125
  # Used by +#dup+ and +#clone+. Deep copy @value.
115
126
  def initialize_copy(other)
116
- @value = @value.nil? ? nil : @value.dup
117
- begin
118
- @default = @default.dup
119
- rescue TypeError => ex
120
- raise unless ex.message =~ /can't (dup|clone)/
121
- end
127
+ @value = case
128
+ when NilClass, TrueClass, FalseClass, Integer
129
+ @value
130
+ else
131
+ @value.dup
132
+ end
133
+ @default = case
134
+ when NilClass, TrueClass, FalseClass, Integer
135
+ @default
136
+ else
137
+ @default.dup
138
+ end
122
139
  end
123
140
 
124
141
  # Get value or default value
@@ -233,6 +250,9 @@ module RASN1
233
250
  end
234
251
  else
235
252
  str << "#{type}: #{value.inspect}"
253
+ str << " OPTIONAL" if optional?
254
+ str << " DEFAULT #{@default}" unless @default.nil?
255
+ str
236
256
  end
237
257
  end
238
258
 
@@ -5,10 +5,10 @@ module RASN1
5
5
  #
6
6
  # == Create a CHOICE
7
7
  # A CHOICE is defined this way:
8
- # choice = Choice.new(:a_choice)
9
- # choice.value = [Integer.new(:int1, implicit: 0, class: :context),
10
- # Integer.new(:int2, implicit: 1, class: :context),
11
- # OctetString.new(:os, implicit: 2, class: :context)]
8
+ # choice = Choice.new
9
+ # choice.value = [Integer.new(implicit: 0, class: :context),
10
+ # Integer.new(implicit: 1, class: :context),
11
+ # OctetString.new(implicit: 2, class: :context)]
12
12
  # The chosen type may be set this way:
13
13
  # choice.chosen = 0 # choose :int1
14
14
  # The chosen value may be set these ways:
@@ -23,7 +23,6 @@ module RASN1
23
23
  when Base, Model
24
24
  next if item.optional? and item.value.nil?
25
25
  str << ' ' * level
26
- str << "#{item.name} " unless item.name.nil?
27
26
  str << "#{item.inspect(level)}\n"
28
27
  else
29
28
  str << item.inspect
@@ -5,8 +5,8 @@ module RASN1
5
5
  #
6
6
  # An enumerated type permits to assign names to integer values. It may be defined
7
7
  # different ways:
8
- # enum = RASN1::Types::Enumerated.new(:name, enum: { 'a' => 0, 'b' => 1, 'c' => 2 })
9
- # enum = RASN1::Types::Enumerated.new(:name, enum: { a: 0, b: 1, c: 2 })
8
+ # enum = RASN1::Types::Enumerated.new(enum: { 'a' => 0, 'b' => 1, 'c' => 2 })
9
+ # enum = RASN1::Types::Enumerated.new(enum: { a: 0, b: 1, c: 2 })
10
10
  # Its value should be setting as an Integer or a String/symbol:
11
11
  # enum.value = :b
12
12
  # enum.value = 1 # equivalent to :b
@@ -9,7 +9,7 @@ module RASN1
9
9
  # Get ASN.1 type
10
10
  # @return [String]
11
11
  def self.type
12
- self.to_s.gsub(/.*::/, '')
12
+ 'IA5String'
13
13
  end
14
14
 
15
15
  private
@@ -4,8 +4,8 @@ module RASN1
4
4
  # ASN.1 Octet String
5
5
  #
6
6
  # An OCTET STRINT may contain another primtive object:
7
- # os = OctetString.new(:os)
8
- # int = Integer.new(:int)
7
+ # os = OctetString.new
8
+ # int = Integer.new
9
9
  # int.value = 12
10
10
  # os.value = int
11
11
  # os.to_der # => DER string with INTEGER in OCTET STRING
@@ -12,11 +12,11 @@ module RASN1
12
12
  # house [1] IMPLICIT INTEGER DEFAULT 0
13
13
  # }
14
14
  # do:
15
- # seq = RASN1::Types::Sequence.new(:record)
15
+ # seq = RASN1::Types::Sequence.new
16
16
  # seq.value = [
17
- # RASN1::Types::Integer.new(:id),
18
- # RASN1::Types::Integer.new(:room, explicit: 0, optional: true),
19
- # RASN1::Types::Integer.new(:house, implicit: 1, default: 0)
17
+ # RASN1::Types::Integer.new
18
+ # RASN1::Types::Integer.new(explicit: 0, optional: true),
19
+ # RASN1::Types::Integer.new(implicit: 1, default: 0)
20
20
  # ]
21
21
  #
22
22
  # A sequence may also be used without value to not parse sequence content:
@@ -35,7 +35,7 @@ module RASN1
35
35
 
36
36
  def initialize_copy(other)
37
37
  super
38
- @value.map! { |v| v.dup }
38
+ @value = @value.map { |v| v.dup }
39
39
  end
40
40
 
41
41
  private
@@ -10,16 +10,16 @@ module RASN1
10
10
  # Integers ::= SEQUENCE OF INTEGER
11
11
  # do:
12
12
  # # Create a SEQUENCE OF INTEGER
13
- # seqof = RASN1::Types::SequenceOf.new(:record, RASN1::Types::Integer)
13
+ # seqof = RASN1::Types::SequenceOf.new(RASN1::Types::Integer)
14
14
  # # Set integer values
15
- # seqof.value << [1, 2, 3, 4]
15
+ # seqof.value = [1, 2, 3, 4]
16
16
  #
17
17
  # == Use with {Constructed} types
18
18
  # SEQUENCE OF may be used to create sequence of composed types. For example:
19
- # composed_type = RASN1::Sequence.new(:comp)
20
- # commposed_type.value = [RASN1::Types::Integer(:id),
21
- # RASN1::Types::OctetString(:data)]
22
- # seqof = RASN1::SequenceOf.new(:comp, composed_type)
19
+ # composed_type = RASN1::Types::Sequence.new
20
+ # commposed_type.value = [RASN1::Types::Integer,
21
+ # RASN1::Types::OctetString]
22
+ # seqof = RASN1::Types::SequenceOf.new(composed_type)
23
23
  # seqof << [0, 'data0']
24
24
  # seqof << [1, 'data1']
25
25
  #
@@ -62,7 +62,7 @@ module RASN1
62
62
  def initialize_copy(other)
63
63
  super
64
64
  @of_type = @of_type.dup
65
- @value.map! { |v| v.dup }
65
+ @value = @value.map { |v| v.dup }
66
66
  end
67
67
 
68
68
  # Add an item to SEQUENCE OF
@@ -148,16 +148,8 @@ module RASN1
148
148
  of_type_class.new(:t)
149
149
  end
150
150
  nb_bytes += type.parse!(der[nb_bytes, der.length])
151
- #value = if composed_of_type?
152
- # type.value.map { |obj| obj.value }
153
- # elsif of_type_class < Model
154
- # type.to_h[type.to_h.keys.first]
155
- # else
156
- # type.value
157
- # end
158
151
  @value << type
159
152
  end
160
- #puts "#{name}: #{@value.inspect}"
161
153
  end
162
154
 
163
155
  def explicit_type
@@ -12,11 +12,11 @@ module RASN1
12
12
  # house [1] IMPLICIT INTEGER DEFAULT 0
13
13
  # }
14
14
  # do:
15
- # set = RASN1::Types::Set.new(:record)
15
+ # set = RASN1::Types::Set.new
16
16
  # set.value = [
17
- # RASN1::Types::Integer(:id),
18
- # RASN1::Types::Integer(:id, explicit: 0, optional: true),
19
- # RASN1::Types::Integer(:id, implicit: 1, default: 0)
17
+ # RASN1::Types::Integer
18
+ # RASN1::Types::Integer(explicit: 0, optional: true),
19
+ # RASN1::Types::Integer(implicit: 1, default: 0)
20
20
  # ]
21
21
  # @author Sylvain Daubert
22
22
  class Set < Sequence
@@ -5,6 +5,13 @@ module RASN1
5
5
  # @author Sylvain Daubert
6
6
  class VisibleString < IA5String
7
7
  TAG = 26
8
+
9
+ # Get ASN.1 type
10
+ # @return [String]
11
+ def self.type
12
+ 'VisibleString'
13
+ end
14
+
8
15
  end
9
16
  end
10
17
  end
data/lib/rasn1/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RASN1
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
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.0
4
+ version: 0.6.1
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-12-16 00:00:00.000000000 Z
11
+ date: 2017-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard