J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * reserved comment block |
| 3 | * DO NOT REMOVE OR ALTER! |
| 4 | */ |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 5 | /** |
| 6 | * Licensed to the Apache Software Foundation (ASF) under one |
| 7 | * or more contributor license agreements. See the NOTICE file |
| 8 | * distributed with this work for additional information |
| 9 | * regarding copyright ownership. The ASF licenses this file |
| 10 | * to you under the Apache License, Version 2.0 (the |
| 11 | * "License"); you may not use this file except in compliance |
| 12 | * with the License. You may obtain a copy of the License at |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 13 | * |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 15 | * |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 16 | * Unless required by applicable law or agreed to in writing, |
| 17 | * software distributed under the License is distributed on an |
| 18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | * KIND, either express or implied. See the License for the |
| 20 | * specific language governing permissions and limitations |
| 21 | * under the License. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | package com.sun.org.apache.xml.internal.security.utils.resolver.implementations; |
| 24 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 25 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 26 | import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 27 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverContext; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 28 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException; |
| 29 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 30 | import org.w3c.dom.Document; |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 31 | import org.w3c.dom.Element; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 32 | import org.w3c.dom.Node; |
| 33 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 34 | /** |
| 35 | * This resolver is used for resolving same-document URIs like URI="" of URI="#id". |
| 36 | * |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 37 | * @author $Author: coheigea $ |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 38 | * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel">The Reference processing model in the XML Signature spec</A> |
| 39 | * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-Same-Document">Same-Document URI-References in the XML Signature spec</A> |
| 40 | * @see <A HREF="http://www.ietf.org/rfc/rfc2396.txt">Section 4.2 of RFC 2396</A> |
| 41 | */ |
| 42 | public class ResolverFragment extends ResourceResolverSpi { |
| 43 | |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 44 | /** {@link org.apache.commons.logging} logging facility */ |
| 45 | private static java.util.logging.Logger log = |
| 46 | java.util.logging.Logger.getLogger(ResolverFragment.class.getName()); |
| 47 | |
| 48 | @Override |
| 49 | public boolean engineIsThreadSafe() { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Method engineResolve |
| 55 | * |
| 56 | * @inheritDoc |
| 57 | * @param uri |
| 58 | * @param baseURI |
| 59 | */ |
| 60 | public XMLSignatureInput engineResolveURI(ResourceResolverContext context) |
| 61 | throws ResourceResolverException { |
| 62 | |
| 63 | Document doc = context.attr.getOwnerElement().getOwnerDocument(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 64 | |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 65 | Node selectedElem = null; |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 66 | if (context.uriToResolve.equals("")) { |
| 67 | /* |
| 68 | * Identifies the node-set (minus any comment nodes) of the XML |
| 69 | * resource containing the signature |
| 70 | */ |
| 71 | if (log.isLoggable(java.util.logging.Level.FINE)) { |
| 72 | log.log(java.util.logging.Level.FINE, "ResolverFragment with empty URI (means complete document)"); |
| 73 | } |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 74 | selectedElem = doc; |
| 75 | } else { |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 76 | /* |
| 77 | * URI="#chapter1" |
| 78 | * Identifies a node-set containing the element with ID attribute |
| 79 | * value 'chapter1' of the XML resource containing the signature. |
| 80 | * XML Signature (and its applications) modify this node-set to |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 81 | * include the element plus all descendants including namespaces and |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 82 | * attributes -- but not comments. |
| 83 | */ |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 84 | String id = context.uriToResolve.substring(1); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 85 | |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 86 | selectedElem = doc.getElementById(id); |
| 87 | if (selectedElem == null) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 88 | Object exArgs[] = { id }; |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 89 | throw new ResourceResolverException( |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 90 | "signature.Verification.MissingID", exArgs, context.attr, context.baseUri |
| 91 | ); |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 92 | } |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 93 | if (context.secureValidation) { |
| 94 | Element start = context.attr.getOwnerDocument().getDocumentElement(); |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 95 | if (!XMLUtils.protectAgainstWrappingAttack(start, id)) { |
| 96 | Object exArgs[] = { id }; |
| 97 | throw new ResourceResolverException( |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 98 | "signature.Verification.MultipleIDs", exArgs, context.attr, context.baseUri |
| 99 | ); |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 102 | if (log.isLoggable(java.util.logging.Level.FINE)) { |
| 103 | log.log(java.util.logging.Level.FINE, |
| 104 | "Try to catch an Element with ID " + id + " and Element was " + selectedElem |
| 105 | ); |
| 106 | } |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 107 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 108 | |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 109 | XMLSignatureInput result = new XMLSignatureInput(selectedElem); |
| 110 | result.setExcludeComments(true); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 111 | |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 112 | result.setMIMEType("text/xml"); |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 113 | if (context.baseUri != null && context.baseUri.length() > 0) { |
| 114 | result.setSourceURI(context.baseUri.concat(context.uriToResolve)); |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 115 | } else { |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 116 | result.setSourceURI(context.uriToResolve); |
Sean Mullan | 848ec37 | 2013-04-22 11:23:33 +0100 | [diff] [blame] | 117 | } |
| 118 | return result; |
| 119 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 120 | |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 121 | /** |
| 122 | * Method engineCanResolve |
| 123 | * @inheritDoc |
| 124 | * @param uri |
| 125 | * @param baseURI |
| 126 | */ |
| 127 | public boolean engineCanResolveURI(ResourceResolverContext context) { |
| 128 | if (context.uriToResolve == null) { |
| 129 | if (log.isLoggable(java.util.logging.Level.FINE)) { |
| 130 | log.log(java.util.logging.Level.FINE, "Quick fail for null uri"); |
| 131 | } |
| 132 | return false; |
| 133 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 134 | |
Sean Mullan | e8695e2 | 2013-07-05 15:54:42 -0400 | [diff] [blame] | 135 | if (context.uriToResolve.equals("") || |
| 136 | ((context.uriToResolve.charAt(0) == '#') && !context.uriToResolve.startsWith("#xpointer(")) |
| 137 | ) { |
| 138 | if (log.isLoggable(java.util.logging.Level.FINE)) { |
| 139 | log.log(java.util.logging.Level.FINE, "State I can resolve reference: \"" + context.uriToResolve + "\""); |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | if (log.isLoggable(java.util.logging.Level.FINE)) { |
| 144 | log.log(java.util.logging.Level.FINE, "Do not seem to be able to resolve reference: \"" + context.uriToResolve + "\""); |
| 145 | } |
| 146 | return false; |
| 147 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 148 | |
| 149 | } |