Skip to content

Commit 4b6ba50

Browse files
setting encoding of literals in asn1 file to binary by default
1 parent f8097ec commit 4b6ba50

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/openssl/asn1.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding: binary
12
# frozen_string_literal: true
23
#--
34
#
@@ -102,7 +103,10 @@ def der_value
102103

103104
def cons_to_der
104105
ary = @value.to_a
105-
str = "".b
106+
107+
return to_der_internal(nil, true) if ary.empty?
108+
109+
str = +""
106110

107111
@value.each_with_index do |item, idx|
108112
if @indefinite_length && item.is_a?(EndOfContent)
@@ -142,13 +146,13 @@ def to_der_internal(body, constructed = false)
142146

143147
str << body
144148
if @indefinite_length
145-
str << "\x00\x00\x00\x00".b
149+
str << "\x00\x00\x00\x00"
146150
end
147151
else
148152
str = ASN1.put_object(constructed, @indefinite_length, body_len, @tag, @tag_class)
149153
str << body
150154
if @indefinite_length
151-
str << "\x00\x00".b
155+
str << "\x00\x00"
152156
end
153157
end
154158

@@ -246,15 +250,15 @@ def to_der
246250

247251
class Null < Primitive
248252
def der_value
249-
"".b
253+
""
250254
end
251255
end
252256

253257
class Boolean < Primitive
254258
def der_value
255259
raise TypeError, "Can't convert nil into Boolean" if @value.nil?
256260

257-
@value ? "\xff".b : "\x00".b
261+
@value ? "\xff" : "\x00"
258262
end
259263
end
260264

@@ -285,7 +289,7 @@ def der_value
285289
"the range 0 to 7"
286290
end
287291

288-
return "\x00".b if @value.empty?
292+
return "\x00" if @value.empty?
289293

290294
@unused_bits.chr << super
291295
end
@@ -338,7 +342,7 @@ def der_value
338342
end
339343

340344
class UTCTime < Primitive
341-
FORMAT = "%y%m%d%H%M%SZ".b.freeze
345+
FORMAT = "%y%m%d%H%M%SZ".freeze
342346

343347
def der_value
344348
value = if @value.is_a?(Time)
@@ -352,7 +356,7 @@ def der_value
352356
end
353357

354358
class GeneralizedTime < Primitive
355-
FORMAT = "%Y%m%d%H%M%SZ".b.freeze
359+
FORMAT = "%Y%m%d%H%M%SZ".freeze
356360
def der_value
357361
value = if @value.is_a?(Time)
358362
@value
@@ -370,7 +374,7 @@ def initialize
370374
end
371375

372376
def to_der
373-
"\x00\x00".b
377+
"\x00\x00"
374378
end
375379
end
376380

@@ -398,7 +402,7 @@ def put_object(constructed, indefinite_length, length, tag, tag_class)
398402
end
399403

400404
if constructed && indefinite_length
401-
str << 0x80.chr
405+
str << "\x80"
402406
else
403407
str << put_length(length)
404408
end
@@ -421,12 +425,12 @@ def put_integer(value)
421425

422426
if value >= 0
423427
data = value.to_bn.to_s(2)
424-
data.prepend("\x00".b) if data.empty? || data.getbyte(0) >= 0x80
428+
data.prepend("\x00") if data.empty? || data.getbyte(0) >= 0x80
425429
else
426430
value = value.to_bn
427431
value += (1 << (value.num_bits + 7) / 8 * 8)
428432
data = value.to_s(2)
429-
data.prepend("\xff".b) if data.empty? || data.getbyte(0) < 0x80
433+
data.prepend("\xff") if data.empty? || data.getbyte(0) < 0x80
430434
end
431435

432436
data

0 commit comments

Comments
 (0)