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, referenced_documents = {}) ⇒ Reference

Returns a new instance of Reference.



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

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

Instance Attribute Details

#errors ⇒ Object

Returns the value of attribute errors.



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

def errors
  @errors
end

#id_attr ⇒ Object

Returns the value of attribute id_attr.



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

def id_attr
  @id_attr
end

#reference ⇒ Object

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_value ⇒ Object



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

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_method ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xmldsig/reference.rb', line 71

def digest_method
  algorithm = reference.at_xpath("descendant::ds:DigestMethod", NAMESPACES).get_attribute("Algorithm")
  case algorithm =~ /sha(.*?)$/i && $1.to_i
    when 512
      Digest::SHA512
    when 256
      Digest::SHA256
    when 1
      Digest::SHA1
    else
      Digest::SHA256
  end
end

#digest_value ⇒ Object



57
58
59
# File 'lib/xmldsig/reference.rb', line 57

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

#digest_value=(digest_value) ⇒ Object



85
86
87
88
# File 'lib/xmldsig/reference.rb', line 85

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

#document ⇒ Object



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

def document
  reference.document
end

#reference_uri ⇒ Object



53
54
55
# File 'lib/xmldsig/reference.rb', line 53

def reference_uri
  reference.get_attribute("URI")
end

#referenced_node ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xmldsig/reference.rb', line 23

def referenced_node
  if reference_uri && reference_uri != ""
    if @id_attr.nil? && reference_uri.start_with?("cid:")
      content_id = reference_uri[4..-1]
      if @referenced_documents.has_key?(content_id)
        @referenced_documents[content_id].dup
      else
        raise(
            ReferencedNodeNotFound,
            "Could not find referenced document with ContentId #{content_id}"
        )
      end
    else
      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
    end
  else
    document.dup.root
  end
end

#sign ⇒ Object



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

def sign
  self.digest_value = calculate_digest_value
end

#transforms ⇒ Object



90
91
92
# File 'lib/xmldsig/reference.rb', line 90

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

#validate_digest_value ⇒ Object



94
95
96
97
98
# File 'lib/xmldsig/reference.rb', line 94

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