Class: Xmldsig::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/xmldsig/reference.rb

Defined Under Namespace

Classes: ReferencedNodeNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference, id_attr = nil) ⇒ Reference

Returns a new instance of Reference.



8
9
10
11
12
# File 'lib/xmldsig/reference.rb', line 8

def initialize(reference, id_attr = nil)
  @reference = reference
  @errors    = []
  @id_attr = id_attr
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/xmldsig/reference.rb', line 3

def errors
  @errors
end

#id_attrObject

Returns the value of attribute id_attr.



3
4
5
# File 'lib/xmldsig/reference.rb', line 3

def id_attr
  @id_attr
end

#referenceObject

Returns the value of attribute reference.



3
4
5
# File 'lib/xmldsig/reference.rb', line 3

def reference
  @reference
end

Instance Method Details

#calculate_digest_valueObject



48
49
50
51
52
53
54
55
56
# File 'lib/xmldsig/reference.rb', line 48

def calculate_digest_value
  transformed = transforms.apply(referenced_node)
  case transformed
    when String
      digest_method.digest transformed
    when Nokogiri::XML::Node
      digest_method.digest Canonicalizer.new(transformed).canonicalize
  end
end

#digest_methodObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xmldsig/reference.rb', line 58

def digest_method
  algorithm = reference.at_xpath("descendant::ds:DigestMethod", NAMESPACES).get_attribute("Algorithm")
  case algorithm
    when "http://www.w3.org/2001/04/xmlenc#sha512"
      Digest::SHA512
    when "http://www.w3.org/2001/04/xmlenc#sha256"
      Digest::SHA256
    when "http://www.w3.org/2000/09/xmldsig#sha1"
      Digest::SHA1
  end
end

#digest_valueObject



44
45
46
# File 'lib/xmldsig/reference.rb', line 44

def digest_value
  Base64.decode64 reference.at_xpath("descendant::ds:DigestValue", NAMESPACES).content
end

#digest_value=(digest_value) ⇒ Object



70
71
72
73
# File 'lib/xmldsig/reference.rb', line 70

def digest_value=(digest_value)
  reference.at_xpath("descendant::ds:DigestValue", NAMESPACES).content =
      Base64.encode64(digest_value).chomp
end

#documentObject



14
15
16
# File 'lib/xmldsig/reference.rb', line 14

def document
  reference.document
end

#reference_uriObject



40
41
42
# File 'lib/xmldsig/reference.rb', line 40

def reference_uri
  reference.get_attribute("URI")
end

#referenced_nodeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xmldsig/reference.rb', line 22

def referenced_node
  if reference_uri && reference_uri != ""
    id = reference_uri[1..-1]
    referenced_node_xpath = @id_attr ? "//*[@#{@id_attr}=$uri]" : "//*[@ID=$uri or @wsu:Id=$uri]"
    variable_bindings = { 'uri' => id }
    if ref = document.dup.at_xpath(referenced_node_xpath, NAMESPACES, variable_bindings)
      ref
    else
      raise(
          ReferencedNodeNotFound,
          "Could not find the referenced node #{id}'"
      )
    end
  else
    document.dup.root
  end
end

#signObject



18
19
20
# File 'lib/xmldsig/reference.rb', line 18

def sign
  self.digest_value = calculate_digest_value
end

#transformsObject



75
76
77
# File 'lib/xmldsig/reference.rb', line 75

def transforms
  Transforms.new(reference.xpath("descendant::ds:Transform", NAMESPACES))
end

#validate_digest_valueObject



79
80
81
82
83
# File 'lib/xmldsig/reference.rb', line 79

def validate_digest_value
  unless digest_value == calculate_digest_value
    @errors << :digest_value
  end
end