rasn1 0.6.8 → 0.7.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 +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +4 -3
- data/Changelog.md +11 -0
- data/README.md +11 -2
- data/lib/rasn1.rb +6 -5
- data/lib/rasn1/model.rb +83 -33
- data/lib/rasn1/types.rb +11 -8
- data/lib/rasn1/types/any.rb +16 -16
- data/lib/rasn1/types/base.rb +73 -66
- data/lib/rasn1/types/bit_string.rb +14 -16
- data/lib/rasn1/types/boolean.rb +15 -12
- data/lib/rasn1/types/choice.rb +9 -14
- data/lib/rasn1/types/constructed.rb +8 -9
- data/lib/rasn1/types/enumerated.rb +3 -1
- data/lib/rasn1/types/generalized_time.rb +23 -25
- data/lib/rasn1/types/ia5string.rb +6 -4
- data/lib/rasn1/types/integer.rb +21 -19
- data/lib/rasn1/types/null.rb +7 -7
- data/lib/rasn1/types/numeric_string.rb +5 -3
- data/lib/rasn1/types/object_id.rb +12 -11
- data/lib/rasn1/types/octet_string.rb +5 -5
- data/lib/rasn1/types/primitive.rb +2 -1
- data/lib/rasn1/types/printable_string.rb +5 -3
- data/lib/rasn1/types/sequence.rb +20 -6
- data/lib/rasn1/types/sequence_of.rb +13 -11
- data/lib/rasn1/types/set.rb +3 -1
- data/lib/rasn1/types/set_of.rb +3 -2
- data/lib/rasn1/types/utc_time.rb +5 -3
- data/lib/rasn1/types/utf8_string.rb +5 -3
- data/lib/rasn1/types/visible_string.rb +3 -2
- data/lib/rasn1/version.rb +3 -1
- data/rasn1.gemspec +1 -1
- metadata +6 -5
data/lib/rasn1/types/sequence.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 sequence
|
5
6
|
#
|
6
7
|
# A sequence is a collection of another ASN.1 types.
|
@@ -25,6 +26,7 @@ module RASN1
|
|
25
26
|
# seq.value # => String
|
26
27
|
# @author Sylvain Daubert
|
27
28
|
class Sequence < Constructed
|
29
|
+
# Sequence tag value
|
28
30
|
TAG = 0x10
|
29
31
|
|
30
32
|
# @see Base#initialize
|
@@ -35,7 +37,19 @@ module RASN1
|
|
35
37
|
|
36
38
|
def initialize_copy(other)
|
37
39
|
super
|
38
|
-
@value = @value.map
|
40
|
+
@value = @value.map(&:dup)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get element at index +idx+, or element of name +name+
|
44
|
+
# @param [Integer, String, Symbol] idx_or_name
|
45
|
+
# @return [Object]
|
46
|
+
def [](idx_or_name)
|
47
|
+
case idx_or_name
|
48
|
+
when Integer
|
49
|
+
@value[idx]
|
50
|
+
when String, Symbol
|
51
|
+
@value.find { |elt| elt.name == idx_or_name }
|
52
|
+
end
|
39
53
|
end
|
40
54
|
|
41
55
|
private
|
@@ -43,14 +57,14 @@ module RASN1
|
|
43
57
|
def value_to_der
|
44
58
|
case @value
|
45
59
|
when Array
|
46
|
-
@value.map
|
60
|
+
@value.map(&:to_der).join
|
47
61
|
else
|
48
62
|
@value.to_s
|
49
63
|
end
|
50
64
|
end
|
51
65
|
|
52
|
-
def der_to_value(der, ber:false)
|
53
|
-
if @value.is_a?(Array)
|
66
|
+
def der_to_value(der, ber: false)
|
67
|
+
if @value.is_a?(Array) && !@value.empty?
|
54
68
|
nb_bytes = 0
|
55
69
|
@value.each do |element|
|
56
70
|
nb_bytes += element.parse!(der[nb_bytes..-1])
|
@@ -58,7 +72,7 @@ module RASN1
|
|
58
72
|
else
|
59
73
|
@value = der
|
60
74
|
der.length
|
61
|
-
end
|
75
|
+
end
|
62
76
|
end
|
63
77
|
end
|
64
78
|
end
|
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 SEQUENCE OF
|
5
6
|
#
|
6
7
|
# A SEQUENCE OF is an array of one ASN.1 type.
|
@@ -45,6 +46,7 @@ module RASN1
|
|
45
46
|
# seqof[0][:int].value # => 12
|
46
47
|
# @author Sylvain Daubert
|
47
48
|
class SequenceOf < Constructed
|
49
|
+
# SequenceOf tag value
|
48
50
|
TAG = Sequence::TAG
|
49
51
|
|
50
52
|
# @return [Class, Base]
|
@@ -68,7 +70,7 @@ module RASN1
|
|
68
70
|
def initialize_copy(other)
|
69
71
|
super
|
70
72
|
@of_type = @of_type.dup
|
71
|
-
@value = @value.map
|
73
|
+
@value = @value.map(&:dup)
|
72
74
|
end
|
73
75
|
|
74
76
|
# Add an item to SEQUENCE OF
|
@@ -76,9 +78,11 @@ module RASN1
|
|
76
78
|
def <<(obj)
|
77
79
|
if of_type_class < Primitive
|
78
80
|
raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array)
|
81
|
+
|
79
82
|
@value += obj.map { |item| @of_type.new(item) }
|
80
83
|
elsif composed_of_type?
|
81
84
|
raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array)
|
85
|
+
|
82
86
|
new_value = of_type_class.new
|
83
87
|
@of_type.value.each_with_index do |type, i|
|
84
88
|
type2 = type.dup
|
@@ -112,17 +116,15 @@ module RASN1
|
|
112
116
|
end
|
113
117
|
|
114
118
|
def inspect(level=0)
|
115
|
-
str =
|
116
|
-
str <<
|
117
|
-
|
118
|
-
level = level.abs
|
119
|
-
str << "#{type}:\n"
|
120
|
-
level += 1
|
119
|
+
str = common_inspect(level)
|
120
|
+
str << "\n"
|
121
|
+
level = level.abs + 1
|
121
122
|
@value.each do |item|
|
122
123
|
case item
|
123
124
|
when Base, Model
|
124
|
-
next if item.optional?
|
125
|
-
|
125
|
+
next if item.optional? && item.value.nil?
|
126
|
+
|
127
|
+
str << item.inspect(level)
|
126
128
|
str << "\n" unless str.end_with?("\n")
|
127
129
|
else
|
128
130
|
str << ' ' * level + "#{item.inspect}\n"
|
@@ -142,7 +144,7 @@ module RASN1
|
|
142
144
|
end
|
143
145
|
|
144
146
|
def value_to_der
|
145
|
-
@value.map
|
147
|
+
@value.map(&:to_der).join
|
146
148
|
end
|
147
149
|
|
148
150
|
def der_to_value(der, ber:false)
|
data/lib/rasn1/types/set.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 set
|
5
6
|
#
|
6
7
|
# A set is a collection of another ASN.1 types.
|
@@ -20,6 +21,7 @@ module RASN1
|
|
20
21
|
# ]
|
21
22
|
# @author Sylvain Daubert
|
22
23
|
class Set < Sequence
|
24
|
+
# Set tag value
|
23
25
|
TAG = 0x11
|
24
26
|
end
|
25
27
|
end
|
data/lib/rasn1/types/set_of.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 SET OF
|
5
6
|
# @author Sylvain Daubert
|
6
7
|
class SetOf < SequenceOf
|
8
|
+
# SetOf tag value
|
7
9
|
TAG = Set::TAG
|
8
10
|
|
9
11
|
# A SET OF is encoded as a SET.
|
@@ -14,4 +16,3 @@ module RASN1
|
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
17
|
-
|
data/lib/rasn1/types/utc_time.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
|
3
5
|
module RASN1
|
4
6
|
module Types
|
5
|
-
|
6
7
|
# ASN.1 UTCTime
|
7
8
|
#
|
8
9
|
# +{#value} of a +UtcTime+ should be a ruby Time.
|
@@ -17,6 +18,7 @@ module RASN1
|
|
17
18
|
# not.
|
18
19
|
# @author Sylvain Daubert
|
19
20
|
class UtcTime < Primitive
|
21
|
+
# UtcTime tag value
|
20
22
|
TAG = 23
|
21
23
|
|
22
24
|
# Get ASN.1 type
|
@@ -26,11 +28,11 @@ module RASN1
|
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
29
|
-
|
31
|
+
|
30
32
|
def value_to_der
|
31
33
|
@value.getutc.strftime('%y%m%d%H%M%SZ')
|
32
34
|
end
|
33
|
-
|
35
|
+
|
34
36
|
def der_to_value(der, ber: false)
|
35
37
|
format = case der.size
|
36
38
|
when 11
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 UTF8 String
|
5
6
|
# @author Sylvain Daubert
|
6
7
|
class Utf8String < OctetString
|
8
|
+
# Utf8String tag value
|
7
9
|
TAG = 12
|
8
10
|
|
9
11
|
# Get ASN.1 type
|
@@ -13,12 +15,12 @@ module RASN1
|
|
13
15
|
end
|
14
16
|
|
15
17
|
private
|
16
|
-
|
18
|
+
|
17
19
|
def value_to_der
|
18
20
|
@value.to_s.force_encoding('UTF-8').force_encoding('BINARY')
|
19
21
|
end
|
20
22
|
|
21
|
-
def der_to_value(der, ber:false)
|
23
|
+
def der_to_value(der, ber: false)
|
22
24
|
super
|
23
25
|
@value.force_encoding('UTF-8')
|
24
26
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RASN1
|
2
4
|
module Types
|
3
|
-
|
4
5
|
# ASN.1 Visible String
|
5
6
|
# @author Sylvain Daubert
|
6
7
|
class VisibleString < IA5String
|
8
|
+
# VisibleString tag value
|
7
9
|
TAG = 26
|
8
10
|
|
9
11
|
# Get ASN.1 type
|
@@ -11,7 +13,6 @@ module RASN1
|
|
11
13
|
def self.type
|
12
14
|
'VisibleString'
|
13
15
|
end
|
14
|
-
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
data/lib/rasn1/version.rb
CHANGED
data/rasn1.gemspec
CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.required_ruby_version = '>= 2.3.0'
|
26
26
|
|
27
27
|
spec.add_development_dependency 'yard', '~>0.9'
|
28
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.17.3'
|
29
29
|
spec.add_development_dependency 'rake', '~> 10.0'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
31
|
spec.add_development_dependency 'simplecov', '~> 0.14'
|
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.
|
4
|
+
version: 0.7.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:
|
11
|
+
date: 2019-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.17.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.17.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- ".gitignore"
|
94
94
|
- ".rubocop.yml"
|
95
95
|
- ".travis.yml"
|
96
|
+
- Changelog.md
|
96
97
|
- Gemfile
|
97
98
|
- LICENSE
|
98
99
|
- README.md
|
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
146
|
version: '0'
|
146
147
|
requirements: []
|
147
148
|
rubyforge_project:
|
148
|
-
rubygems_version: 2.7.6
|
149
|
+
rubygems_version: 2.7.6.2
|
149
150
|
signing_key:
|
150
151
|
specification_version: 4
|
151
152
|
summary: Ruby ASN.1 library
|