Skip to content

Commit a46b07b

Browse files
utctime and gentime: inline formats, constrain input data
utctime should not take dates into account with a year below 1950 and above 2049
1 parent dead363 commit a46b07b

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/openssl/asn1.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,11 @@ def der_value
376376
end
377377

378378
class UTCTime < Primitive
379-
FORMAT = "%y%m%d%H%M%SZ".freeze
380-
381379
private
382380

381+
YEAR_RANGE = 1950..2049
382+
private_constant :YEAR_RANGE
383+
383384
# :nodoc:
384385
def der_value
385386
value = if @value.is_a?(Time)
@@ -388,13 +389,13 @@ def der_value
388389
Time.at(Integer(@value))
389390
end
390391

391-
value.utc.strftime(FORMAT)
392+
raise OpenSSL::ASN1::ASN1Error unless YEAR_RANGE.include?(value.year)
393+
394+
value.utc.strftime("%y%m%d%H%M%SZ")
392395
end
393396
end
394397

395398
class GeneralizedTime < Primitive
396-
FORMAT = "%Y%m%d%H%M%SZ".freeze
397-
398399
private
399400

400401
# :nodoc:
@@ -405,7 +406,7 @@ def der_value
405406
Time.at(Integer(@value))
406407
end
407408

408-
value.utc.strftime(FORMAT)
409+
value.utc.strftime("%Y%m%d%H%M%SZ")
409410
end
410411
end
411412

0 commit comments

Comments
 (0)