20 * specific language governing permissions and limitations |
20 * specific language governing permissions and limitations |
21 * under the License. |
21 * under the License. |
22 */ |
22 */ |
23 package com.sun.org.apache.xml.internal.security.keys.keyresolver; |
23 package com.sun.org.apache.xml.internal.security.keys.keyresolver; |
24 |
24 |
|
25 import java.io.ByteArrayInputStream; |
|
26 import java.io.IOException; |
|
27 import java.io.InputStream; |
25 import java.security.PrivateKey; |
28 import java.security.PrivateKey; |
26 import java.security.PublicKey; |
29 import java.security.PublicKey; |
27 import java.security.cert.X509Certificate; |
30 import java.security.cert.X509Certificate; |
28 import java.util.HashMap; |
31 import java.util.HashMap; |
29 |
32 |
30 import javax.crypto.SecretKey; |
33 import javax.crypto.SecretKey; |
|
34 import javax.xml.parsers.DocumentBuilder; |
|
35 import javax.xml.parsers.ParserConfigurationException; |
31 |
36 |
32 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; |
37 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; |
|
38 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
|
39 import org.w3c.dom.Document; |
33 import org.w3c.dom.Element; |
40 import org.w3c.dom.Element; |
|
41 import org.xml.sax.SAXException; |
34 |
42 |
35 /** |
43 /** |
36 * This class is an abstract class for a child KeyInfo Element. |
44 * This class is an abstract class for a child KeyInfo Element. |
37 * |
45 * |
38 * If you want the your KeyResolver, at firstly you must extend this class, and register |
46 * If you want the your KeyResolver, at firstly you must extend this class, and register |
43 * </PRE> |
51 * </PRE> |
44 */ |
52 */ |
45 public abstract class KeyResolverSpi { |
53 public abstract class KeyResolverSpi { |
46 |
54 |
47 /** Field properties */ |
55 /** Field properties */ |
48 protected java.util.Map<String, String> properties = null; |
56 protected java.util.Map<String, String> properties; |
49 |
57 |
50 protected boolean globalResolver = false; |
58 protected boolean globalResolver = false; |
51 |
59 |
52 protected boolean secureValidation; |
60 protected boolean secureValidation; |
53 |
61 |
82 */ |
90 */ |
83 public PublicKey engineResolvePublicKey( |
91 public PublicKey engineResolvePublicKey( |
84 Element element, String baseURI, StorageResolver storage |
92 Element element, String baseURI, StorageResolver storage |
85 ) throws KeyResolverException { |
93 ) throws KeyResolverException { |
86 throw new UnsupportedOperationException(); |
94 throw new UnsupportedOperationException(); |
87 }; |
95 } |
88 |
96 |
89 /** |
97 /** |
90 * Method engineLookupAndResolvePublicKey |
98 * Method engineLookupAndResolvePublicKey |
91 * |
99 * |
92 * @param element |
100 * @param element |
105 } |
113 } |
106 return tmp.engineResolvePublicKey(element, baseURI, storage); |
114 return tmp.engineResolvePublicKey(element, baseURI, storage); |
107 } |
115 } |
108 |
116 |
109 private KeyResolverSpi cloneIfNeeded() throws KeyResolverException { |
117 private KeyResolverSpi cloneIfNeeded() throws KeyResolverException { |
110 KeyResolverSpi tmp = this; |
|
111 if (globalResolver) { |
118 if (globalResolver) { |
112 try { |
119 try { |
113 @SuppressWarnings("deprecation") |
120 @SuppressWarnings("deprecation") |
114 KeyResolverSpi krs = getClass().newInstance(); |
121 KeyResolverSpi tmp = getClass().newInstance(); |
115 tmp = krs; |
122 return tmp; |
116 } catch (InstantiationException e) { |
123 } catch (InstantiationException e) { |
117 throw new KeyResolverException("", e); |
124 throw new KeyResolverException(e, ""); |
118 } catch (IllegalAccessException e) { |
125 } catch (IllegalAccessException e) { |
119 throw new KeyResolverException("", e); |
126 throw new KeyResolverException(e, ""); |
120 } |
127 } |
121 } |
128 } |
122 return tmp; |
129 return this; |
123 } |
130 } |
124 |
131 |
125 /** |
132 /** |
126 * Method engineResolveCertificate |
133 * Method engineResolveCertificate |
127 * |
134 * |
134 */ |
141 */ |
135 public X509Certificate engineResolveX509Certificate( |
142 public X509Certificate engineResolveX509Certificate( |
136 Element element, String baseURI, StorageResolver storage |
143 Element element, String baseURI, StorageResolver storage |
137 ) throws KeyResolverException{ |
144 ) throws KeyResolverException{ |
138 throw new UnsupportedOperationException(); |
145 throw new UnsupportedOperationException(); |
139 }; |
146 } |
140 |
147 |
141 /** |
148 /** |
142 * Method engineLookupResolveX509Certificate |
149 * Method engineLookupResolveX509Certificate |
143 * |
150 * |
144 * @param element |
151 * @param element |
170 */ |
177 */ |
171 public SecretKey engineResolveSecretKey( |
178 public SecretKey engineResolveSecretKey( |
172 Element element, String baseURI, StorageResolver storage |
179 Element element, String baseURI, StorageResolver storage |
173 ) throws KeyResolverException{ |
180 ) throws KeyResolverException{ |
174 throw new UnsupportedOperationException(); |
181 throw new UnsupportedOperationException(); |
175 }; |
182 } |
176 |
183 |
177 /** |
184 /** |
178 * Method engineLookupAndResolveSecretKey |
185 * Method engineLookupAndResolveSecretKey |
179 * |
186 * |
180 * @param element |
187 * @param element |
221 * @param key |
228 * @param key |
222 * @param value |
229 * @param value |
223 */ |
230 */ |
224 public void engineSetProperty(String key, String value) { |
231 public void engineSetProperty(String key, String value) { |
225 if (properties == null) { |
232 if (properties == null) { |
226 properties = new HashMap<String, String>(); |
233 properties = new HashMap<>(); |
227 } |
234 } |
228 properties.put(key, value); |
235 properties.put(key, value); |
229 } |
236 } |
230 |
237 |
231 /** |
238 /** |
258 |
265 |
259 public void setGlobalResolver(boolean globalResolver) { |
266 public void setGlobalResolver(boolean globalResolver) { |
260 this.globalResolver = globalResolver; |
267 this.globalResolver = globalResolver; |
261 } |
268 } |
262 |
269 |
|
270 |
|
271 /** |
|
272 * Parses a byte array and returns the parsed Element. |
|
273 * |
|
274 * @param bytes |
|
275 * @return the Document Element after parsing bytes |
|
276 * @throws KeyResolverException if something goes wrong |
|
277 */ |
|
278 protected static Element getDocFromBytes(byte[] bytes, boolean secureValidation) throws KeyResolverException { |
|
279 DocumentBuilder db = null; |
|
280 try (InputStream is = new ByteArrayInputStream(bytes)) { |
|
281 db = XMLUtils.createDocumentBuilder(false, secureValidation); |
|
282 Document doc = db.parse(is); |
|
283 return doc.getDocumentElement(); |
|
284 } catch (SAXException ex) { |
|
285 throw new KeyResolverException(ex); |
|
286 } catch (IOException ex) { |
|
287 throw new KeyResolverException(ex); |
|
288 } catch (ParserConfigurationException ex) { |
|
289 throw new KeyResolverException(ex); |
|
290 } |
|
291 } |
|
292 |
263 } |
293 } |