author | jlahoda |
Tue, 24 Sep 2019 15:40:26 +0200 | |
branch | JDK-8226585-branch |
changeset 58290 | d885633d9de4 |
parent 50614 | 3810c9a2efa1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1337 | 2 |
* reserved comment block |
3 |
* DO NOT REMOVE OR ALTER! |
|
2 | 4 |
*/ |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
7 |
* or more contributor license agreements. See the NOTICE file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
9 |
* regarding copyright ownership. The ASF licenses this file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
10 |
* to you under the Apache License, Version 2.0 (the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
11 |
* "License"); you may not use this file except in compliance |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
1337 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
1337 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
16 |
* Unless required by applicable law or agreed to in writing, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
17 |
* software distributed under the License is distributed on an |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
19 |
* KIND, either express or implied. See the License for the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
21 |
* under the License. |
1337 | 22 |
*/ |
23 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
24 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
1337 | 25 |
*/ |
26 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
27 |
* $Id: Utils.java 1788465 2017-03-24 15:10:51Z coheigea $ |
2 | 28 |
*/ |
29 |
package org.jcp.xml.dsig.internal.dom; |
|
30 |
||
31 |
import java.io.ByteArrayOutputStream; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.IOException; |
|
34 |
import java.util.*; |
|
18240 | 35 |
import javax.xml.crypto.XMLCryptoContext; |
2 | 36 |
import org.w3c.dom.NamedNodeMap; |
37 |
import org.w3c.dom.Node; |
|
38 |
||
39 |
/** |
|
40 |
* Miscellaneous static utility methods for use in JSR 105 RI. |
|
41 |
* |
|
42 |
*/ |
|
43 |
public final class Utils { |
|
44 |
||
45 |
private Utils() {} |
|
46 |
||
47 |
public static byte[] readBytesFromStream(InputStream is) |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
48 |
throws IOException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18266
diff
changeset
|
49 |
{ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
50 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
51 |
byte[] buf = new byte[1024]; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
52 |
while (true) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
53 |
int read = is.read(buf); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
54 |
if (read == -1) { // EOF |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
55 |
break; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
56 |
} |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
57 |
baos.write(buf, 0, read); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
58 |
if (read < 1024) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
59 |
break; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
60 |
} |
2 | 61 |
} |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
62 |
return baos.toByteArray(); |
2 | 63 |
} |
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Converts an Iterator to a Set of Nodes, according to the XPath |
|
68 |
* Data Model. |
|
69 |
* |
|
70 |
* @param i the Iterator |
|
71 |
* @return the Set of Nodes |
|
72 |
*/ |
|
27747 | 73 |
static Set<Node> toNodeSet(Iterator<?> i) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
74 |
Set<Node> nodeSet = new HashSet<>(); |
2 | 75 |
while (i.hasNext()) { |
27747 | 76 |
Node n = (Node)i.next(); |
2 | 77 |
nodeSet.add(n); |
78 |
// insert attributes nodes to comply with XPath |
|
79 |
if (n.getNodeType() == Node.ELEMENT_NODE) { |
|
80 |
NamedNodeMap nnm = n.getAttributes(); |
|
81 |
for (int j = 0, length = nnm.getLength(); j < length; j++) { |
|
82 |
nodeSet.add(nnm.item(j)); |
|
83 |
} |
|
84 |
} |
|
85 |
} |
|
86 |
return nodeSet; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Returns the ID from a same-document URI (ex: "#id") |
|
91 |
*/ |
|
92 |
public static String parseIdFromSameDocumentURI(String uri) { |
|
93 |
if (uri.length() == 0) { |
|
94 |
return null; |
|
95 |
} |
|
96 |
String id = uri.substring(1); |
|
97 |
if (id != null && id.startsWith("xpointer(id(")) { |
|
98 |
int i1 = id.indexOf('\''); |
|
99 |
int i2 = id.indexOf('\'', i1+1); |
|
100 |
id = id.substring(i1+1, i2); |
|
101 |
} |
|
102 |
return id; |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Returns true if uri is a same-document URI, false otherwise. |
|
107 |
*/ |
|
108 |
public static boolean sameDocumentURI(String uri) { |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
109 |
return uri != null && (uri.length() == 0 || uri.charAt(0) == '#'); |
2 | 110 |
} |
18240 | 111 |
|
112 |
static boolean secureValidation(XMLCryptoContext xc) { |
|
18266
26e69da689b9
8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents:
18240
diff
changeset
|
113 |
if (xc == null) { |
26e69da689b9
8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents:
18240
diff
changeset
|
114 |
return false; |
26e69da689b9
8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents:
18240
diff
changeset
|
115 |
} |
18240 | 116 |
return getBoolean(xc, "org.jcp.xml.dsig.secureValidation"); |
117 |
} |
|
118 |
||
119 |
private static boolean getBoolean(XMLCryptoContext xc, String name) { |
|
120 |
Boolean value = (Boolean)xc.getProperty(name); |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
121 |
return value != null && value.booleanValue(); |
18240 | 122 |
} |
2 | 123 |
} |