rasn1 0.2.0 → 0.3.0

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: 379d5a61021e3b58ffb46317f72905e9e3972044
4
- data.tar.gz: 60bb0d280cccd6c999744045fb45f334863d46f4
3
+ metadata.gz: 0fc2ee81216604dd99cf1bd78ab08ddd732bf32f
4
+ data.tar.gz: 66c4bece600e0a18f447f596c467ed76f5e4bbbc
5
5
  SHA512:
6
- metadata.gz: beb7960cfdef060b22f81dea31d7b93ece7781afad2b0a7db92606b860e8ff00ab1ae7991a86d5594b97b18b53c2888d6d83f11e4fe6e84abb33cfdddb491d75
7
- data.tar.gz: 2fe8288bb99e4c21ead959855e3b0611c182abc6f2e23a8a60ff0babe7ebe62ef9a7795ec8f0512d50792f2e02278b192e8d62879c6f7815836ebb3c2c3287a0
6
+ metadata.gz: 93cc5dabca26821d3d9a44eab7099d9cd4f2011b250beb25ba97d44371db1eaa4b242b0b459a8bc4232fc0a5022bd762bcd1826edb6335688cae437738900601
7
+ data.tar.gz: dc6a6cb74e5b0bdb84c3fb3f6faff06a979d7221b0f8caa025a06a193c672ca7c1e2732d1a106f1b7f99ca8b60f7790089832fa5fa8ff845283ccb26ffc750f5
data/lib/rasn1/model.rb CHANGED
@@ -49,6 +49,13 @@ module RASN1
49
49
  # record2[:a_record][:room] = 43
50
50
  # or like this:
51
51
  # record2 = Record2.new(rented: true, a_record: { id: 65537, room: 43 })
52
+ #
53
+ # == Delegation
54
+ # {Model} may delegate some methods to its root element. Thus, if root element
55
+ # is, for example, a {Types::Choice}, model may delegate +#chosen+ and +#chosen_value+.
56
+ #
57
+ # All methods defined by root may be delegated by model, unless model also defines
58
+ # this method.
52
59
  # @author Sylvain Daubert
53
60
  class Model
54
61
 
@@ -128,6 +135,13 @@ module RASN1
128
135
  @root = [name, proc]
129
136
  end
130
137
 
138
+ # Give type name (aka class name)
139
+ # @return [String]
140
+ def type
141
+ return @type if @type
142
+ @type = self.to_s.gsub(/.*::/, '')
143
+ end
144
+
131
145
  # Parse a DER/BER encoded string
132
146
  # @param [String] str
133
147
  # @param [Boolean] ber accept BER encoding or not
@@ -154,6 +168,15 @@ module RASN1
154
168
  @elements[name]
155
169
  end
156
170
 
171
+ # Set value of element +name+. Element should be a {Base}.
172
+ # @param [String,Symbol] name
173
+ # @param [Object] value
174
+ # @return [Object] value
175
+ def []=(name, value)
176
+ raise Error, "cannot set value for a Model" if @elements[name].is_a? Model
177
+ @elements[name].value = value
178
+ end
179
+
157
180
  # Get name frm root type
158
181
  # @return [String,Symbol]
159
182
  def name
@@ -183,6 +206,12 @@ module RASN1
183
206
  @elements[@root]
184
207
  end
185
208
 
209
+ # Give type name (aka class name)
210
+ # @return [String]
211
+ def type
212
+ self.class.type
213
+ end
214
+
186
215
  # Parse a DER/BER encoded string, and modify object in-place.
187
216
  # @param [String] str
188
217
  # @param [Boolean] ber accept BER encoding or not
@@ -191,6 +220,16 @@ module RASN1
191
220
  @elements[@root].parse!(str.dup.force_encoding('BINARY'), ber: ber)
192
221
  end
193
222
 
223
+ # Delegate some methods to root element
224
+ # @param [Symbol] meth
225
+ def method_missing(meth, *args)
226
+ if @elements[@root].respond_to? meth
227
+ @elements[@root].send meth, *args
228
+ else
229
+ super
230
+ end
231
+ end
232
+
194
233
  private
195
234
 
196
235
  def is_composed?(el)
@@ -194,6 +194,12 @@ module RASN1
194
194
  total_length
195
195
  end
196
196
 
197
+ # Give size in octets of encoded value
198
+ # @return [Integer]
199
+ def value_size
200
+ value_to_der.size
201
+ end
202
+
197
203
 
198
204
  private
199
205
 
@@ -52,7 +52,12 @@ module RASN1
52
52
  # @raise [ChoiceError] {#chosen} not set
53
53
  def chosen_value
54
54
  check_chosen
55
- @value[@chosen].value
55
+ case @value[@chosen]
56
+ when Base
57
+ @value[@chosen].value
58
+ when Model
59
+ @value[@chosen]
60
+ end
56
61
  end
57
62
 
58
63
  # @note {#chosen} MUST be set before calling this method
@@ -76,7 +81,7 @@ module RASN1
76
81
  @chosen = i
77
82
  nb_bytes = element.parse!(der)
78
83
  parsed = true
79
- break nb_bytes
84
+ return nb_bytes
80
85
  rescue ASN1Error
81
86
  @chosen = nil
82
87
  next
@@ -0,0 +1,27 @@
1
+ module RASN1
2
+ module Types
3
+
4
+ # ASN.1 IA5 String
5
+ # @author Sylvain Daubert
6
+ class IA5String < OctetString
7
+ TAG = 22
8
+
9
+ # Get ASN.1 type
10
+ # @return [String]
11
+ def self.type
12
+ self.to_s.gsub(/.*::/, '')
13
+ end
14
+
15
+ private
16
+
17
+ def value_to_der
18
+ @value.to_s.force_encoding('US-ASCII').force_encoding('BINARY')
19
+ end
20
+
21
+ def der_to_value(der, ber:false)
22
+ super
23
+ @value.force_encoding('US-ASCII')
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ module RASN1
2
+ module Types
3
+
4
+ # ASN.1 Numeric String
5
+ # @author Sylvain Daubert
6
+ class NumericString < OctetString
7
+ TAG = 18
8
+
9
+ # Get ASN.1 type
10
+ # @return [String]
11
+ def self.type
12
+ 'NumericString'
13
+ end
14
+
15
+ private
16
+
17
+ def value_to_der
18
+ check_characters
19
+ @value.to_s.force_encoding('BINARY')
20
+ end
21
+
22
+ def der_to_value(der, ber:false)
23
+ super
24
+ check_characters
25
+ end
26
+
27
+ def check_characters
28
+ if @value.to_s =~ /([^0-9 ])/
29
+ raise ASN1Error, "NUMERIC STRING #@name: invalid character: '#{$1}'"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module RASN1
2
+ module Types
3
+
4
+ # ASN.1 Printable String
5
+ # @author Sylvain Daubert
6
+ class PrintableString < OctetString
7
+ TAG = 19
8
+
9
+ # Get ASN.1 type
10
+ # @return [String]
11
+ def self.type
12
+ 'PrintableString'
13
+ end
14
+
15
+ private
16
+
17
+ def value_to_der
18
+ check_characters
19
+ @value.to_s.force_encoding('BINARY')
20
+ end
21
+
22
+ def der_to_value(der, ber:false)
23
+ super
24
+ check_characters
25
+ end
26
+
27
+ def check_characters
28
+ if @value.to_s =~ /([^a-zA-Z0-9 '=\(\)\+,\-\.\/:\?])/
29
+ raise ASN1Error, "PRINTABLE STRING #@name: invalid character: '#{$1}'"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -6,6 +6,12 @@ module RASN1
6
6
  class Utf8String < OctetString
7
7
  TAG = 12
8
8
 
9
+ # Get ASN.1 type
10
+ # @return [String]
11
+ def self.type
12
+ 'UTF8String'
13
+ end
14
+
9
15
  private
10
16
 
11
17
  def value_to_der
@@ -13,7 +19,8 @@ module RASN1
13
19
  end
14
20
 
15
21
  def der_to_value(der, ber:false)
16
- @value = der.force_encoding('UTF-8')
22
+ super
23
+ @value.force_encoding('UTF-8')
17
24
  end
18
25
  end
19
26
  end
@@ -0,0 +1,10 @@
1
+ module RASN1
2
+ module Types
3
+
4
+ # ASN.1 Visible String
5
+ # @author Sylvain Daubert
6
+ class VisibleString < IA5String
7
+ TAG = 26
8
+ end
9
+ end
10
+ end
data/lib/rasn1/types.rb CHANGED
@@ -29,6 +29,10 @@ require_relative 'types/null'
29
29
  require_relative 'types/object_id'
30
30
  require_relative 'types/enumerated'
31
31
  require_relative 'types/utf8_string'
32
+ require_relative 'types/numeric_string'
33
+ require_relative 'types/printable_string'
34
+ require_relative 'types/ia5string'
35
+ require_relative 'types/visible_string'
32
36
  require_relative 'types/constructed'
33
37
  require_relative 'types/sequence'
34
38
  require_relative 'types/sequence_of'
data/lib/rasn1/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RASN1
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
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.2.0
4
+ version: 0.3.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-09 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -102,17 +102,21 @@ files:
102
102
  - lib/rasn1/types/choice.rb
103
103
  - lib/rasn1/types/constructed.rb
104
104
  - lib/rasn1/types/enumerated.rb
105
+ - lib/rasn1/types/ia5string.rb
105
106
  - lib/rasn1/types/integer.rb
106
107
  - lib/rasn1/types/null.rb
108
+ - lib/rasn1/types/numeric_string.rb
107
109
  - lib/rasn1/types/object_id.rb
108
110
  - lib/rasn1/types/octet_string.rb
109
111
  - lib/rasn1/types/primitive.rb
112
+ - lib/rasn1/types/printable_string.rb
110
113
  - lib/rasn1/types/sequence.rb
111
114
  - lib/rasn1/types/sequence_of.rb
112
115
  - lib/rasn1/types/set.rb
113
116
  - lib/rasn1/types/set_of.rb
114
117
  - lib/rasn1/types/set_spec.rb
115
118
  - lib/rasn1/types/utf8_string.rb
119
+ - lib/rasn1/types/visible_string.rb
116
120
  - lib/rasn1/version.rb
117
121
  - rasn1.gemspec
118
122
  homepage: https://github.com/sdaubert/rasn1