Class: SmcUtil::FileValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/smcutil/file_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileValidator

Returns a new instance of FileValidator.



7
8
9
# File 'lib/smcutil/file_validator.rb', line 7

def initialize(file)
  @file = file
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/smcutil/file_validator.rb', line 5

def errors
  @errors
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/smcutil/file_validator.rb', line 5

def file
  @file
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/smcutil/file_validator.rb', line 19

def valid?
  validate!

  !@errors.any?
end

#validate!Object



11
12
13
14
15
16
17
# File 'lib/smcutil/file_validator.rb', line 11

def validate!
  @errors = []

  validate_regions!
  validate_headers!
  validate_signature!
end

#validate_headers!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/smcutil/file_validator.rb', line 29

def validate_headers!
  return false unless @file.headers.any? || (@file.headers.count != @file.regions.count)

  hash_algorithm = nil

  case @file.headers[0].length
  when 20
    hash_algorithm = OpenSSL::Digest::SHA1
  when 32
    hash_algorithm = OpenSSL::Digest::SHA256
  when 64
    hash_algorithm = OpenSSL::Digest::SHA512
  else
    @errors << "ERROR: Digest of header row with length #{@file.headers[0].count} bytes is unsupported.  (But pull requests are accepted)" and return
  end

  zipped_regions = @file.regions.zip(@file.headers)
  zipped_regions.each do |region, header|
    begin
      hash = hash_algorithm.new

      hash << region.data

      if hash.digest == header
        puts "DEBUG: Validated hash for region #{region}" if SmcUtil::DEBUG
      else
        @errors << "Header hash for region #{region} does not match expected"
      end
    rescue => ex
      puts "ERROR: #{ex}"
      puts ex.backtrace
    end
  end
end

#validate_regions!Object



25
26
27
# File 'lib/smcutil/file_validator.rb', line 25

def validate_regions!
  @file.regions.any?
end

#validate_signature!Object



64
65
66
# File 'lib/smcutil/file_validator.rb', line 64

def validate_signature!
  # TODO: Find the RSA 2048 bit key that signs this
end