wss4r 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README +300 -0
- data/lib/wss4r/aws/utils.rb +37 -0
- data/lib/wss4r/config/config.rb +105 -0
- data/lib/wss4r/rpc/proxy.rb +26 -0
- data/lib/wss4r/rpc/router.rb +46 -0
- data/lib/wss4r/rpc/wssdriver.rb +19 -0
- data/lib/wss4r/security/crypto/certificate.rb +21 -0
- data/lib/wss4r/security/crypto/cipher.rb +161 -0
- data/lib/wss4r/security/crypto/hash.rb +35 -0
- data/lib/wss4r/security/exceptions/exceptions.rb +62 -0
- data/lib/wss4r/security/resolver.rb +23 -0
- data/lib/wss4r/security/security.rb +148 -0
- data/lib/wss4r/security/util/hash_util.rb +39 -0
- data/lib/wss4r/security/util/names.rb +38 -0
- data/lib/wss4r/security/util/namespaces.rb +21 -0
- data/lib/wss4r/security/util/reference_elements.rb +15 -0
- data/lib/wss4r/security/util/soap_parser.rb +73 -0
- data/lib/wss4r/security/util/transformer_factory.rb +29 -0
- data/lib/wss4r/security/util/types.rb +25 -0
- data/lib/wss4r/security/util/xmlcanonicalizer.rb +427 -0
- data/lib/wss4r/security/util/xmlutils.rb +58 -0
- data/lib/wss4r/security/xml/encrypted_data.rb +110 -0
- data/lib/wss4r/security/xml/encrypted_key.rb +74 -0
- data/lib/wss4r/security/xml/key_info.rb +52 -0
- data/lib/wss4r/security/xml/reference.rb +53 -0
- data/lib/wss4r/security/xml/reference_list.rb +24 -0
- data/lib/wss4r/security/xml/security.rb +92 -0
- data/lib/wss4r/security/xml/signature.rb +69 -0
- data/lib/wss4r/security/xml/signature_value.rb +26 -0
- data/lib/wss4r/security/xml/signed_info.rb +83 -0
- data/lib/wss4r/security/xml/timestamp.rb +47 -0
- data/lib/wss4r/security/xml/tokentypes.rb +180 -0
- data/lib/wss4r/server/wssstandaloneserver.rb +27 -0
- data/lib/wss4r/soap/processor.rb +92 -0
- data/lib/wss4r/tokenresolver/authenticateuserresolver.rb +34 -0
- data/lib/wss4r/tokenresolver/certificateresolver.rb +62 -0
- data/lib/wss4r/tokenresolver/databaseresolver.rb +56 -0
- data/lib/wss4r/tokenresolver/resolver.rb +13 -0
- metadata +95 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
module WSS4R
|
4
|
+
module Security
|
5
|
+
module Util
|
6
|
+
|
7
|
+
class HashUtil
|
8
|
+
def HashUtil::hash_encode64(value)
|
9
|
+
#zwei chr sind ein Hex-Wert
|
10
|
+
#wenn positiv -> passt
|
11
|
+
#wenn negativ -> wert = 256+chr_wert
|
12
|
+
j=0
|
13
|
+
ret = (" " * (value.size()/2))
|
14
|
+
0.step((value.size()-1),2) {|i|
|
15
|
+
hex = value[i..i+1].hex()
|
16
|
+
if (hex > 0)
|
17
|
+
ret[j] = hex
|
18
|
+
elsif
|
19
|
+
ret[j] = 256+(hex)
|
20
|
+
end
|
21
|
+
j=j+1
|
22
|
+
}
|
23
|
+
Base64.encode64(ret)
|
24
|
+
end
|
25
|
+
|
26
|
+
def HashUtil::byte_array(string)
|
27
|
+
ret=""
|
28
|
+
0.upto(string.size()-1) {|i|
|
29
|
+
ret = ret + string[i].to_s() + ","
|
30
|
+
}
|
31
|
+
ret=ret[0..-2]
|
32
|
+
ret
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
end #Util
|
38
|
+
end #Security
|
39
|
+
end #WSS4R
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
class Names
|
6
|
+
HEADER = "env:Header"
|
7
|
+
SECURITY = "wsse:Security"
|
8
|
+
BODY = "env:Body"
|
9
|
+
ENCRYPTED_DATA = "xenc:EncryptedData"
|
10
|
+
ENCRYPTION_METHOD = "xenc:EncryptionMethod"
|
11
|
+
CIPHER_DATA = "xenc:CipherData"
|
12
|
+
CIPHER_VALUE = "xenc:CipherValue"
|
13
|
+
ENCRYPTED_KEY = "xenc:EncryptedKey"
|
14
|
+
KEY_INFO = "ds:KeyInfo"
|
15
|
+
SECURITY_TOKEN_REFERENCE = "wsse:SecurityTokenReference"
|
16
|
+
KEY_IDENTIFIER = "wsse:KeyIdentifier"
|
17
|
+
REFERENCE_LIST = "xenc:ReferenceList"
|
18
|
+
DATA_REFERENCE = "xenc:DataReference"
|
19
|
+
REFERENCE_WSSE = "wsse:Reference"
|
20
|
+
REFERENCE_DS = "ds:Reference"
|
21
|
+
SIGNATURE_VALUE = "ds:SignatureValue"
|
22
|
+
SIGNATURE = "ds:Signature"
|
23
|
+
CANONICALIZATION_METHOD = "ds:CanonicalizationMethod"
|
24
|
+
SIGNATURE_METHOD = "ds:SignatureMethod"
|
25
|
+
TRANSFORMS = "ds:Transforms"
|
26
|
+
TRANSFORM = "ds:Transform"
|
27
|
+
DIGEST_METHOD = "ds:DigestMethod"
|
28
|
+
DIGEST_VALUE = "ds:DigestValue"
|
29
|
+
BINARY_SECURITY_TOKEN = "wsse:BinarySecurityToken"
|
30
|
+
SIGNED_INFO="ds:SignedInfo"
|
31
|
+
TIMESTAMP = "wsu:Timestamp"
|
32
|
+
CREATED = "wsu:Created"
|
33
|
+
EXPIRES = "wsu:Expires"
|
34
|
+
end
|
35
|
+
|
36
|
+
end #Util
|
37
|
+
end #Security
|
38
|
+
end #WSS4R
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
module Namespaces
|
6
|
+
XSI = "http://www.w3.org/2001/XMLSchema-instance"
|
7
|
+
XSD = "http://www.w3.org/2001/XMLSchema"
|
8
|
+
WSA = "http://schemas.xmlsoap.org/ws/2004/08/addressing"
|
9
|
+
WSU = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
|
10
|
+
SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
|
11
|
+
WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
|
12
|
+
XENC = "http://www.w3.org/2001/04/xmlenc#"
|
13
|
+
DS = "http://www.w3.org/2000/09/xmldsig#"
|
14
|
+
S11 = "http://schemas.xmlsoap.org/soap/envelope/"
|
15
|
+
S12 = "http://www.w3.org/2003/05/soap-envelope"
|
16
|
+
XENCD = "http://schemas.xmlsoap.org/soap/encoding/"
|
17
|
+
end
|
18
|
+
|
19
|
+
end #Util
|
20
|
+
end #Security
|
21
|
+
end #WSS4R
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
class ReferenceElements < Array
|
6
|
+
def initialize()
|
7
|
+
push("/env:Envelope/env:Header/wsse:Security/wsu:Timestamp")
|
8
|
+
push("/env:Envelope/env:Body")
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end #Util
|
14
|
+
end #Security
|
15
|
+
end #WSS4R
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
class SOAPParser
|
6
|
+
BODY = "/env:Envelope/env:Body"
|
7
|
+
ENVELOPE = "/env:Envelope"
|
8
|
+
HEADER = "/env:Envelope/env:Header"
|
9
|
+
|
10
|
+
KEY_IDENTIFIER = "//wsse:SecurityTokenReference//wsse:KeyIdentifier"
|
11
|
+
SECURITY = "/env:Envelope/env:Header/wsse:Security"
|
12
|
+
CIPHER_DATA = "//xenc:CipherData//xenc:CipherValue"
|
13
|
+
CIPHER_VALUE = "//xenc:CipherValue"
|
14
|
+
SIGNED_INFO = "//ds:SignedInfo"
|
15
|
+
ENCRYPTION_METHOD = "//xenc:EncryptionMethod"
|
16
|
+
KEY_INFO = "//ds:KeyInfo"
|
17
|
+
REFERENCE_LIST = "//env:Envelope/env:Header/wsse:Security/xenc:EncryptedKey/xenc:ReferenceList"
|
18
|
+
SIGNATURE = "/env:Envelope/env:Header/wsse:Security/ds:Signature"
|
19
|
+
ENCRYPTED_KEY = "/env:Envelope/env:Header/wsse:Security/xenc:EncryptedKey"
|
20
|
+
@@document = nil
|
21
|
+
@@prefix = nil
|
22
|
+
@@soap_ns = nil
|
23
|
+
@@soap_prefix = nil
|
24
|
+
|
25
|
+
def self.document()
|
26
|
+
@@document
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.document=(value)
|
30
|
+
@@document = value
|
31
|
+
prefix = @@document.root().prefix() #set the prefix to env or soap
|
32
|
+
BODY.gsub!("env:", prefix+":")
|
33
|
+
ENVELOPE.gsub!("env:", prefix+":")
|
34
|
+
HEADER.gsub!("env:", prefix+":")
|
35
|
+
SECURITY.gsub!("env:", prefix+":")
|
36
|
+
REFERENCE_LIST.gsub!("env:", prefix+":")
|
37
|
+
SIGNATURE.gsub!("env:", prefix+":")
|
38
|
+
ENCRYPTED_KEY.gsub!("env:", prefix+":")
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.soap_ns=(ns)
|
42
|
+
@@soap_ns = ns
|
43
|
+
end
|
44
|
+
def self.soap_prefix=(prefix)
|
45
|
+
@@soap_prefix = prefix
|
46
|
+
end
|
47
|
+
def self.soap_ns()
|
48
|
+
@@soap_ns
|
49
|
+
end
|
50
|
+
def self.soap_prefix()
|
51
|
+
@@soap_prefix
|
52
|
+
end
|
53
|
+
def self.part(type)
|
54
|
+
element = @@document.select(type)
|
55
|
+
element
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.element(element, type)
|
59
|
+
result = @@document.select_element(element, type)
|
60
|
+
result
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end #Util
|
65
|
+
end #Security
|
66
|
+
end #WSS4R
|
67
|
+
|
68
|
+
if __FILE__ == $0
|
69
|
+
document = REXML::Document.new(File.new(ARGV[0]))
|
70
|
+
WSS4R::Security::Util::SOAPParser.document=(document)
|
71
|
+
result = WSS4R::Security::Util::SOAPParser.part(WSS4R::Security::Util::SOAPParser::ENVELOPE)
|
72
|
+
puts(result)
|
73
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
class TransformerFactory
|
6
|
+
def TransformerFactory::get_instance(type)
|
7
|
+
case type
|
8
|
+
when "http://www.w3.org/2001/10/xml-exc-c14n#"
|
9
|
+
return XmlCanonicalizer.new(false,true)
|
10
|
+
else
|
11
|
+
return XmlCanonicalizer.new(false,true)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class DigestFactory
|
17
|
+
def DigestFactory::get_instance(type)
|
18
|
+
case type
|
19
|
+
when "http://www.w3.org/2000/09/xmldsig#sha1"
|
20
|
+
return CryptHash.new() #OpenSSL::Digest::SHA1.new()
|
21
|
+
else
|
22
|
+
return CryptHash.new() #OpenSSL::Digest::SHA1.new()
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end #WSS4R
|
28
|
+
end #Security
|
29
|
+
end #Util
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module WSS4R
|
2
|
+
module Security
|
3
|
+
module Util
|
4
|
+
|
5
|
+
module Types
|
6
|
+
VALUE_BASE64BINARY = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
|
7
|
+
VALUE_KEYIDENTIFIER = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier"
|
8
|
+
REFERENCE_VALUETYPE_X509 = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
|
9
|
+
ENCODING_X509V3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
|
10
|
+
ENCRYPTEDKEY = "http://www.w3.org/2001/04/xmlenc#EncryptedKey"
|
11
|
+
ALGORITHM_RSA15 = "http://www.w3.org/2001/04/xmlenc#rsa-1_5"
|
12
|
+
XENC_CONTENT = "http://www.w3.org/2001/04/xmlenc#Content"
|
13
|
+
ALGORITHM_3DES_CBC = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"
|
14
|
+
ALGORITHM_AES_CBC = "http://www.w3.org/2001/04/xmlenc#aes256-cbc"
|
15
|
+
ALGORITHM_AES128_CBC = "http://www.w3.org/2001/04/xmlenc#aes128-cbc"
|
16
|
+
CANON_C14N_EXCL = "http://www.w3.org/2001/10/xml-exc-c14n#"
|
17
|
+
SIG_ALG_RSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
|
18
|
+
DIG_METHOD_SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1"
|
19
|
+
PASSWORD_DIGEST = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"
|
20
|
+
PASSWORD_TEXT = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"
|
21
|
+
end
|
22
|
+
|
23
|
+
end #Util
|
24
|
+
end #Security
|
25
|
+
end #WSS4R
|
@@ -0,0 +1,427 @@
|
|
1
|
+
require "rexml/document"
|
2
|
+
require "base64"
|
3
|
+
|
4
|
+
include REXML
|
5
|
+
|
6
|
+
module WSS4R
|
7
|
+
module Security
|
8
|
+
module Util
|
9
|
+
|
10
|
+
class REXML::Instruction
|
11
|
+
def write(writer, indent=-1, transitive=false, ie_hack=false)
|
12
|
+
indent(writer, indent)
|
13
|
+
writer << START.sub(/\\/u, '')
|
14
|
+
writer << @target
|
15
|
+
writer << ' '
|
16
|
+
writer << @content if @content != nil
|
17
|
+
writer << STOP.sub(/\\/u, '')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class REXML::Attribute
|
22
|
+
def <=>(a2)
|
23
|
+
if (self === a2)
|
24
|
+
return 0
|
25
|
+
elsif (self == nil)
|
26
|
+
return -1
|
27
|
+
elsif (a2 == nil)
|
28
|
+
return 1
|
29
|
+
elsif (self.prefix() == a2.prefix())
|
30
|
+
return self.name()<=>a2.name()
|
31
|
+
end
|
32
|
+
if (self.prefix() == nil)
|
33
|
+
return -1
|
34
|
+
elsif (a2.prefix() == nil)
|
35
|
+
return 1
|
36
|
+
end
|
37
|
+
ret = self.namespace()<=>a2.namespace()
|
38
|
+
if (ret == 0)
|
39
|
+
ret = self.prefix()<=>a2.prefix()
|
40
|
+
end
|
41
|
+
return ret
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class REXML::Element
|
46
|
+
def search_namespace(prefix)
|
47
|
+
if (self.namespace(prefix) == nil)
|
48
|
+
return (self.parent().search_namespace(prefix)) if (self.parent() != nil)
|
49
|
+
else
|
50
|
+
return self.namespace(prefix)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
def rendered=(rendered)
|
54
|
+
@rendered = rendered
|
55
|
+
end
|
56
|
+
def rendered?()
|
57
|
+
return @rendered
|
58
|
+
end
|
59
|
+
def node_namespaces()
|
60
|
+
ns = Array.new()
|
61
|
+
ns.push(self.prefix())
|
62
|
+
self.attributes().each_attribute{|a|
|
63
|
+
if (a.prefix() != nil)
|
64
|
+
ns.push(a.prefix())
|
65
|
+
end
|
66
|
+
if (a.prefix() == "" && a.local_name() == "xmlns")
|
67
|
+
ns.push("xmlns")
|
68
|
+
end
|
69
|
+
}
|
70
|
+
ns
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class NamespaceNode
|
75
|
+
attr_reader :prefix, :uri
|
76
|
+
def initialize(prefix, uri)
|
77
|
+
@prefix = prefix
|
78
|
+
@uri = uri
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class XmlCanonicalizer
|
83
|
+
attr_accessor :prefix_list
|
84
|
+
|
85
|
+
BEFORE_DOC_ELEMENT = 0
|
86
|
+
INSIDE_DOC_ELEMENT = 1
|
87
|
+
AFTER_DOC_ELEMENT = 2
|
88
|
+
|
89
|
+
NODE_TYPE_ATTRIBUTE = 3
|
90
|
+
NODE_TYPE_WHITESPACE = 4
|
91
|
+
NODE_TYPE_COMMENT = 5
|
92
|
+
NODE_TYPE_PI = 6
|
93
|
+
NODE_TYPE_TEXT = 7
|
94
|
+
|
95
|
+
|
96
|
+
def initialize(with_comments, excl_c14n)
|
97
|
+
@with_comments = with_comments
|
98
|
+
@exclusive = excl_c14n
|
99
|
+
@res = ""
|
100
|
+
@state = BEFORE_DOC_ELEMENT
|
101
|
+
@xnl = Array.new()
|
102
|
+
@prevVisibleNamespacesStart = 0
|
103
|
+
@prevVisibleNamespacesEnd = 0
|
104
|
+
@visibleNamespaces = Array.new()
|
105
|
+
@inclusive_namespaces = Array.new()
|
106
|
+
@prefix_list = nil
|
107
|
+
@rendered_prefixes = Array.new()
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_inclusive_namespaces(prefix_list, element, visible_namespaces)
|
111
|
+
namespaces = element.attributes()
|
112
|
+
namespaces.each_attribute{|ns|
|
113
|
+
if (ns.prefix=="xmlns")
|
114
|
+
if (prefix_list.include?(ns.local_name()))
|
115
|
+
visible_namespaces.push(NamespaceNode.new("xmlns:"+ns.local_name(), ns.value()))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
}
|
119
|
+
parent = element.parent()
|
120
|
+
add_inclusive_namespaces(prefix_list, parent, visible_namespaces) if (parent)
|
121
|
+
visible_namespaces
|
122
|
+
end
|
123
|
+
|
124
|
+
def canonicalize(document)
|
125
|
+
write_document_node(document)
|
126
|
+
@res
|
127
|
+
end
|
128
|
+
|
129
|
+
def canonicalize_element(element)
|
130
|
+
@inclusive_namespaces = add_inclusive_namespaces(@prefix_list, element, @inclusive_namespaces) if (@prefix_list)
|
131
|
+
@preserve_document = element.document()
|
132
|
+
tmp_parent = element.parent()
|
133
|
+
body_string = remove_whitespace(element.to_s().gsub("\n","").gsub("\t","").gsub("\r",""))
|
134
|
+
document = Document.new(body_string)
|
135
|
+
tmp_parent.delete_element(element)
|
136
|
+
element = tmp_parent.add_element(document.root())
|
137
|
+
@preserve_element = element
|
138
|
+
document = Document.new(element.to_s())
|
139
|
+
ns = element.namespace(element.prefix())
|
140
|
+
document.root().add_namespace(element.prefix(), ns)
|
141
|
+
write_document_node(document)
|
142
|
+
@res
|
143
|
+
end
|
144
|
+
|
145
|
+
def write_document_node(document)
|
146
|
+
@state = BEFORE_DOC_ELEMENT
|
147
|
+
if (document.class().to_s() == "REXML::Element")
|
148
|
+
write_node(document)
|
149
|
+
else
|
150
|
+
document.each_child{|child|
|
151
|
+
write_node(child)
|
152
|
+
}
|
153
|
+
end
|
154
|
+
@res
|
155
|
+
end
|
156
|
+
|
157
|
+
def write_node(node)
|
158
|
+
visible = is_node_visible(node)
|
159
|
+
if ((node.node_type() == :text) && white_text?(node.value()))
|
160
|
+
res = node.value()
|
161
|
+
res.gsub("\r\n","\n")
|
162
|
+
#res = res.delete(" ").delete("\t")
|
163
|
+
res.delete("\r")
|
164
|
+
@res = @res + res
|
165
|
+
#write_text_node(node,visible) if (@state == INSIDE_DOC_ELEMENT)
|
166
|
+
return
|
167
|
+
end
|
168
|
+
if (node.node_type() == :text)
|
169
|
+
write_text_node(node, visible)
|
170
|
+
return
|
171
|
+
end
|
172
|
+
if (node.node_type() == :element)
|
173
|
+
write_element_node(node, visible) if (!node.rendered?())
|
174
|
+
node.rendered=(true)
|
175
|
+
end
|
176
|
+
if (node.node_type() == :processing_instruction)
|
177
|
+
end
|
178
|
+
if (node.node_type() == :comment)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def write_element_node(node, visible)
|
183
|
+
savedPrevVisibleNamespacesStart = @prevVisibleNamespacesStart
|
184
|
+
savedPrevVisibleNamespacesEnd = @prevVisibleNamespacesEnd
|
185
|
+
savedVisibleNamespacesSize = @visibleNamespaces.size()
|
186
|
+
state = @state
|
187
|
+
state = INSIDE_DOC_ELEMENT if (visible && state == BEFORE_DOC_ELEMENT)
|
188
|
+
@res = @res + "<" + node.expanded_name() if (visible)
|
189
|
+
write_namespace_axis(node, visible)
|
190
|
+
write_attribute_axis(node)
|
191
|
+
@res = @res + ">" if (visible)
|
192
|
+
node.each_child{|child|
|
193
|
+
write_node(child)
|
194
|
+
}
|
195
|
+
@res = @res + "</" +node.expanded_name() + ">" if (visible)
|
196
|
+
@state = AFTER_DOC_ELEMENT if (visible && state == BEFORE_DOC_ELEMENT)
|
197
|
+
@prevVisibleNamespacesStart = savedPrevVisibleNamespacesStart
|
198
|
+
@prevVisibleNamespacesEnd = savedPrevVisibleNamespacesEnd
|
199
|
+
@visibleNamespaces.slice!(savedVisibleNamespacesSize, @visibleNamespaces.size() - savedVisibleNamespacesSize) if (@visibleNamespaces.size() > savedVisibleNamespacesSize)
|
200
|
+
end
|
201
|
+
|
202
|
+
def write_namespace_axis(node, visible)
|
203
|
+
doc = node.document()
|
204
|
+
has_empty_namespace = false
|
205
|
+
list = Array.new()
|
206
|
+
cur = node
|
207
|
+
#while ((cur != nil) && (cur != doc) && (cur.node_type() != :document))
|
208
|
+
namespaces = cur.node_namespaces()
|
209
|
+
namespaces.each{|prefix|
|
210
|
+
next if ((prefix == "xmlns") && (node.namespace(prefix) == ""))
|
211
|
+
namespace = cur.namespace(prefix)
|
212
|
+
next if (is_namespace_node(namespace))
|
213
|
+
next if (node.namespace(prefix) != cur.namespace(prefix))
|
214
|
+
next if (prefix == "xml" && namespace == "http://www.w3.org/XML/1998/namespace")
|
215
|
+
next if (!is_node_visible(cur))
|
216
|
+
rendered = is_namespace_rendered(prefix, namespace)
|
217
|
+
@visibleNamespaces.push(NamespaceNode.new("xmlns:"+prefix,namespace)) if (visible)
|
218
|
+
if ((!rendered) && !list.include?(prefix))
|
219
|
+
list.push(prefix)
|
220
|
+
end
|
221
|
+
has_empty_namespace = true if (prefix == nil)
|
222
|
+
}
|
223
|
+
if (visible && !has_empty_namespace && !is_namespace_rendered(nil, nil))
|
224
|
+
@res = @res + ' xmlns=""'
|
225
|
+
end
|
226
|
+
#TODO: ns of inclusive_list
|
227
|
+
#=begin
|
228
|
+
if ((@prefix_list) && (node.to_s() == node.parent().to_s()))
|
229
|
+
#list.push(node.prefix())
|
230
|
+
@inclusive_namespaces.each{|ns|
|
231
|
+
prefix = ns.prefix().split(":")[1]
|
232
|
+
list.push(prefix) if (!list.include?(prefix) && (!node.attributes.prefixes.include?(prefix)))
|
233
|
+
}
|
234
|
+
@prefix_list = nil
|
235
|
+
end
|
236
|
+
#=end
|
237
|
+
list.sort!()
|
238
|
+
list.each{|prefix|
|
239
|
+
next if (prefix == "")
|
240
|
+
next if (@rendered_prefixes.include?(prefix))
|
241
|
+
@rendered_prefixes.push(prefix)
|
242
|
+
ns = node.namespace(prefix)
|
243
|
+
ns = @preserve_element.namespace(prefix) if (ns == nil)
|
244
|
+
@res = @res + normalize_string(" " + prefix + '="' + ns + '"', NODE_TYPE_TEXT) if (prefix == "xmlns")
|
245
|
+
@res = @res + normalize_string(" xmlns:" + prefix + '="' + ns + '"', NODE_TYPE_TEXT) if (prefix != nil && prefix != "xmlns")
|
246
|
+
}
|
247
|
+
if (visible)
|
248
|
+
@prevVisibleNamespacesStart = @prevVisibleNamespacesEnd
|
249
|
+
@prevVisibleNamespacesEnd = @visibleNamespaces.size()
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def write_attribute_axis(node)
|
254
|
+
list = Array.new()
|
255
|
+
#node.attributes().each_attribute{|attr|
|
256
|
+
# list.push(attr) if (!is_namespace_node(attr.value()) && !is_namespace_decl(attr)) # && is_node_visible(
|
257
|
+
#}
|
258
|
+
node.attributes().sort().each{|key, attr|
|
259
|
+
list.push(attr) if (!is_namespace_node(attr.value()) && !is_namespace_decl(attr)) # && is_node_visible(
|
260
|
+
}
|
261
|
+
|
262
|
+
if (!@exclusive && node.parent() != nil && node.parent().parent() != nil)
|
263
|
+
cur = node.parent()
|
264
|
+
while (cur != nil)
|
265
|
+
#next if (cur.attributes() == nil)
|
266
|
+
cur.each_attribute{|attribute|
|
267
|
+
next if (attribute.prefix() != "xml")
|
268
|
+
next if (attribute.prefix().index("xmlns") == 0)
|
269
|
+
next if (node.namespace(attribute.prefix()) == attribute.value())
|
270
|
+
found = true
|
271
|
+
list.each{|n|
|
272
|
+
if (n.prefix() == "xml" && n.value() == attritbute.value())
|
273
|
+
found = true
|
274
|
+
break
|
275
|
+
end
|
276
|
+
}
|
277
|
+
next if (found)
|
278
|
+
list.push(attribute)
|
279
|
+
}
|
280
|
+
end
|
281
|
+
end
|
282
|
+
list.each{|attribute|
|
283
|
+
if (attribute != nil)
|
284
|
+
if (attribute.name() != "xmlns")
|
285
|
+
@res = @res + " " + normalize_string(attribute.to_string(), NODE_TYPE_ATTRIBUTE).gsub("'",'"')
|
286
|
+
end
|
287
|
+
# else
|
288
|
+
# @res = @res + " " + normalize_string(attribute.name()+'="'+attribute.to_s()+'"', NODE_TYPE_ATTRIBUTE).gsub("'",'"')
|
289
|
+
#end
|
290
|
+
end
|
291
|
+
}
|
292
|
+
end
|
293
|
+
|
294
|
+
def is_namespace_node(namespace_uri)
|
295
|
+
return (namespace_uri == "http://www.w3.org/2000/xmlns/")
|
296
|
+
end
|
297
|
+
|
298
|
+
def is_namespace_rendered(prefix, uri)
|
299
|
+
is_empty_ns = prefix == nil && uri == nil
|
300
|
+
if (is_empty_ns)
|
301
|
+
start = 0
|
302
|
+
else
|
303
|
+
start = @prevVisibleNamespacesStart
|
304
|
+
end
|
305
|
+
@visibleNamespaces.each{|ns|
|
306
|
+
if (ns.prefix() == "xmlns:"+prefix.to_s() && ns.uri() == uri)
|
307
|
+
return true
|
308
|
+
end
|
309
|
+
}
|
310
|
+
return is_empty_ns
|
311
|
+
#(@visibleNamespaces.size()-1).downto(start) {|i|
|
312
|
+
# ns = @visibleNamespaces[i]
|
313
|
+
# return true if (ns.prefix() == "xmlns:"+prefix.to_s() && ns.uri() == uri)
|
314
|
+
# #p = ns.prefix() if (ns.prefix().index("xmlns") == 0)
|
315
|
+
# #return ns.uri() == uri if (p == prefix)
|
316
|
+
#}
|
317
|
+
#return is_empty_ns
|
318
|
+
end
|
319
|
+
|
320
|
+
def is_node_visible(node)
|
321
|
+
return true if (@xnl.size() == 0)
|
322
|
+
@xnl.each{|element|
|
323
|
+
return true if (element == node)
|
324
|
+
}
|
325
|
+
return false
|
326
|
+
end
|
327
|
+
|
328
|
+
def normalize_string(input, type)
|
329
|
+
sb = ""
|
330
|
+
return input
|
331
|
+
end
|
332
|
+
#input.each_byte{|b|
|
333
|
+
# if (b ==60 && (type == NODE_TYPE_ATTRIBUTE || is_text_node(type)))
|
334
|
+
# sb = sb + "<"
|
335
|
+
# elsif (b == 62 && is_text_node(type))
|
336
|
+
# sb = sb + ">"
|
337
|
+
# elsif (b == 38 && (is_text_node(type) || is_text_node(type))) #Ampersand
|
338
|
+
# sb = sb + "&"
|
339
|
+
# elsif (b == 34 && is_text_node(type)) #Quote
|
340
|
+
# sb = sb + """
|
341
|
+
# elsif (b == 9 && is_text_node(type)) #Tabulator
|
342
|
+
# sb = sb + "	"
|
343
|
+
# elsif (b == 11 && is_text_node(type)) #CR
|
344
|
+
# sb = sb + "
"
|
345
|
+
# elsif (b == 13 && (type == NODE_TYPE_ATTRIBUTE || (is_text_node(type) && type != NODE_TYPE_WHITESPACE) || type == NODE_TYPE_COMMENT || type == NODE_TYPE_PI))
|
346
|
+
# sb = sb + "
"
|
347
|
+
# elsif (b == 13)
|
348
|
+
# next
|
349
|
+
# else
|
350
|
+
# sb = sb.concat(b)
|
351
|
+
# end
|
352
|
+
#}
|
353
|
+
#sb
|
354
|
+
#end
|
355
|
+
|
356
|
+
def write_text_node(node, visible)
|
357
|
+
if (visible)
|
358
|
+
@res = @res + normalize_string(node.value(), node.node_type())
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
def white_text?(text)
|
363
|
+
return true if ((text.strip() == "") || (text.strip() == nil))
|
364
|
+
return false
|
365
|
+
end
|
366
|
+
|
367
|
+
def is_namespace_decl(attribute)
|
368
|
+
#return true if (attribute.name() == "xmlns")
|
369
|
+
return true if (attribute.prefix().index("xmlns") == 0)
|
370
|
+
return false
|
371
|
+
end
|
372
|
+
|
373
|
+
def is_text_node(type)
|
374
|
+
return true if (type == NODE_TYPE_TEXT || type == NODE_TYPE_CDATA || type == NODE_TYPE_WHITESPACE)
|
375
|
+
return false
|
376
|
+
end
|
377
|
+
|
378
|
+
def remove_whitespace(string)
|
379
|
+
new_string = ""
|
380
|
+
in_white = false
|
381
|
+
string.each_byte{|b|
|
382
|
+
#if (in_white && b == 32)
|
383
|
+
#else
|
384
|
+
if !(in_white && b == 32)
|
385
|
+
new_string = new_string + b.chr()
|
386
|
+
end
|
387
|
+
if (b == 62) #>
|
388
|
+
in_white = true
|
389
|
+
end
|
390
|
+
if (b == 60) #<
|
391
|
+
in_white = false
|
392
|
+
end
|
393
|
+
}
|
394
|
+
new_string
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end #Util
|
398
|
+
end #Security
|
399
|
+
end #WSS4R
|
400
|
+
|
401
|
+
|
402
|
+
if __FILE__ == $0
|
403
|
+
document = Document.new(File.new(ARGV[0]))
|
404
|
+
body = nil
|
405
|
+
c = WSS4R::Security::Util::XmlCanonicalizer.new(false, true)
|
406
|
+
|
407
|
+
if (ARGV.size() == 3)
|
408
|
+
body = ARGV[2]
|
409
|
+
if (body == "true")
|
410
|
+
element = XPath.match(document, "/soap:Envelope/soap:Body")[0]
|
411
|
+
element = XPath.first(document, "/soap:Envelope/soap:Header/wsse:Security/Signature/SignedInfo")
|
412
|
+
result = c.canonicalize_element(element)
|
413
|
+
puts("-----")
|
414
|
+
puts(result)
|
415
|
+
puts("-----")
|
416
|
+
puts(result.size())
|
417
|
+
puts("-----")
|
418
|
+
puts(CryptHash.new().digest_b64(result))
|
419
|
+
end
|
420
|
+
else
|
421
|
+
result = c.canonicalize(document)
|
422
|
+
end
|
423
|
+
|
424
|
+
file = File.new(ARGV[1], "wb")
|
425
|
+
file.write(result)
|
426
|
+
file.close()
|
427
|
+
end
|