J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * $Id: DOMXMLSignatureFactory.java,v 1.21 2005/09/23 19:59:11 mullan Exp $ |
| 27 | */ |
| 28 | package org.jcp.xml.dsig.internal.dom; |
| 29 | |
| 30 | import javax.xml.crypto.*; |
| 31 | import javax.xml.crypto.dsig.*; |
| 32 | import javax.xml.crypto.dsig.dom.DOMValidateContext; |
| 33 | import javax.xml.crypto.dsig.keyinfo.*; |
| 34 | import javax.xml.crypto.dsig.spec.*; |
| 35 | |
| 36 | import java.security.*; |
| 37 | import java.security.spec.AlgorithmParameterSpec; |
| 38 | import java.util.List; |
| 39 | import org.w3c.dom.Document; |
| 40 | import org.w3c.dom.Element; |
| 41 | import org.w3c.dom.Node; |
| 42 | |
| 43 | /** |
| 44 | * DOM-based implementation of XMLSignatureFactory. |
| 45 | * |
| 46 | * @author Sean Mullan |
| 47 | */ |
| 48 | public final class DOMXMLSignatureFactory extends XMLSignatureFactory { |
| 49 | |
| 50 | /** |
| 51 | * Initializes a new instance of this class. |
| 52 | */ |
| 53 | public DOMXMLSignatureFactory() {} |
| 54 | |
| 55 | public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki) { |
| 56 | return new DOMXMLSignature(si, ki, null, null, null); |
| 57 | } |
| 58 | |
| 59 | public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki, |
| 60 | List objects, String id, String signatureValueId) { |
| 61 | return new DOMXMLSignature(si, ki, objects, id, signatureValueId); |
| 62 | } |
| 63 | |
| 64 | public Reference newReference(String uri, DigestMethod dm) { |
| 65 | return newReference(uri, dm, null, null, null); |
| 66 | } |
| 67 | |
| 68 | public Reference newReference(String uri, DigestMethod dm, List transforms, |
| 69 | String type, String id) { |
| 70 | return new DOMReference(uri, type, dm, transforms, id); |
| 71 | } |
| 72 | |
| 73 | public Reference newReference(String uri, DigestMethod dm, |
| 74 | List appliedTransforms, Data result, List transforms, String type, |
| 75 | String id) { |
| 76 | if (appliedTransforms == null) { |
| 77 | throw new NullPointerException("appliedTransforms cannot be null"); |
| 78 | } |
| 79 | if (appliedTransforms.isEmpty()) { |
| 80 | throw new NullPointerException("appliedTransforms cannot be empty"); |
| 81 | } |
| 82 | if (result == null) { |
| 83 | throw new NullPointerException("result cannot be null"); |
| 84 | } |
| 85 | return new DOMReference |
| 86 | (uri, type, dm, appliedTransforms, result, transforms, id); |
| 87 | } |
| 88 | |
| 89 | public Reference newReference(String uri, DigestMethod dm, List transforms, |
| 90 | String type, String id, byte[] digestValue) { |
| 91 | if (digestValue == null) { |
| 92 | throw new NullPointerException("digestValue cannot be null"); |
| 93 | } |
| 94 | return new DOMReference |
| 95 | (uri, type, dm, null, null, transforms, id, digestValue); |
| 96 | } |
| 97 | |
| 98 | public SignedInfo newSignedInfo(CanonicalizationMethod cm, |
| 99 | SignatureMethod sm, List references) { |
| 100 | return newSignedInfo(cm, sm, references, null); |
| 101 | } |
| 102 | |
| 103 | public SignedInfo newSignedInfo(CanonicalizationMethod cm, |
| 104 | SignatureMethod sm, List references, String id) { |
| 105 | return new DOMSignedInfo(cm, sm, references, id); |
| 106 | } |
| 107 | |
| 108 | // Object factory methods |
| 109 | public XMLObject newXMLObject(List content, String id, String mimeType, |
| 110 | String encoding) { |
| 111 | return new DOMXMLObject(content, id, mimeType, encoding); |
| 112 | } |
| 113 | |
| 114 | public Manifest newManifest(List references) { |
| 115 | return newManifest(references, null); |
| 116 | } |
| 117 | |
| 118 | public Manifest newManifest(List references, String id) { |
| 119 | return new DOMManifest(references, id); |
| 120 | } |
| 121 | |
| 122 | public SignatureProperties newSignatureProperties(List props, String id) { |
| 123 | return new DOMSignatureProperties(props, id); |
| 124 | } |
| 125 | |
| 126 | public SignatureProperty newSignatureProperty |
| 127 | (List info, String target, String id) { |
| 128 | return new DOMSignatureProperty(info, target, id); |
| 129 | } |
| 130 | |
| 131 | public XMLSignature unmarshalXMLSignature(XMLValidateContext context) |
| 132 | throws MarshalException { |
| 133 | |
| 134 | if (context == null) { |
| 135 | throw new NullPointerException("context cannot be null"); |
| 136 | } |
| 137 | return unmarshal(((DOMValidateContext) context).getNode(), context); |
| 138 | } |
| 139 | |
| 140 | public XMLSignature unmarshalXMLSignature(XMLStructure xmlStructure) |
| 141 | throws MarshalException { |
| 142 | |
| 143 | if (xmlStructure == null) { |
| 144 | throw new NullPointerException("xmlStructure cannot be null"); |
| 145 | } |
| 146 | return unmarshal |
| 147 | (((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode(), |
| 148 | null); |
| 149 | } |
| 150 | |
| 151 | private XMLSignature unmarshal(Node node, XMLValidateContext context) |
| 152 | throws MarshalException { |
| 153 | |
| 154 | node.normalize(); |
| 155 | |
| 156 | Element element = null; |
| 157 | if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 158 | element = ((Document) node).getDocumentElement(); |
| 159 | } else if (node.getNodeType() == Node.ELEMENT_NODE) { |
| 160 | element = (Element) node; |
| 161 | } else { |
| 162 | throw new MarshalException |
| 163 | ("Signature element is not a proper Node"); |
| 164 | } |
| 165 | |
| 166 | // check tag |
| 167 | String tag = element.getLocalName(); |
| 168 | if (tag == null) { |
| 169 | throw new MarshalException("Document implementation must " + |
| 170 | "support DOM Level 2 and be namespace aware"); |
| 171 | } |
| 172 | if (tag.equals("Signature")) { |
| 173 | return new DOMXMLSignature(element, context); |
| 174 | } else { |
| 175 | throw new MarshalException("invalid Signature tag: " + tag); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public boolean isFeatureSupported(String feature) { |
| 180 | if (feature == null) { |
| 181 | throw new NullPointerException(); |
| 182 | } else { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public DigestMethod newDigestMethod(String algorithm, |
| 188 | DigestMethodParameterSpec params) throws NoSuchAlgorithmException, |
| 189 | InvalidAlgorithmParameterException { |
| 190 | if (algorithm == null) { |
| 191 | throw new NullPointerException(); |
| 192 | } |
| 193 | if (algorithm.equals(DigestMethod.SHA1)) { |
| 194 | return new DOMDigestMethod.SHA1(params); |
| 195 | } else if (algorithm.equals(DigestMethod.SHA256)) { |
| 196 | return new DOMDigestMethod.SHA256(params); |
| 197 | } else if (algorithm.equals(DOMDigestMethod.SHA384)) { |
| 198 | return new DOMDigestMethod.SHA384(params); |
| 199 | } else if (algorithm.equals(DigestMethod.SHA512)) { |
| 200 | return new DOMDigestMethod.SHA512(params); |
| 201 | } else { |
| 202 | throw new NoSuchAlgorithmException("unsupported algorithm"); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | public SignatureMethod newSignatureMethod(String algorithm, |
| 207 | SignatureMethodParameterSpec params) throws NoSuchAlgorithmException, |
| 208 | InvalidAlgorithmParameterException { |
| 209 | if (algorithm == null) { |
| 210 | throw new NullPointerException(); |
| 211 | } |
| 212 | if (algorithm.equals(SignatureMethod.RSA_SHA1)) { |
| 213 | return new DOMSignatureMethod.SHA1withRSA(params); |
| 214 | } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA256)) { |
| 215 | return new DOMSignatureMethod.SHA256withRSA(params); |
| 216 | } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA384)) { |
| 217 | return new DOMSignatureMethod.SHA384withRSA(params); |
| 218 | } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA512)) { |
| 219 | return new DOMSignatureMethod.SHA512withRSA(params); |
| 220 | } else if (algorithm.equals(SignatureMethod.DSA_SHA1)) { |
| 221 | return new DOMSignatureMethod.SHA1withDSA(params); |
| 222 | } else if (algorithm.equals(SignatureMethod.HMAC_SHA1)) { |
| 223 | return new DOMHMACSignatureMethod.SHA1(params); |
| 224 | } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA256)) { |
| 225 | return new DOMHMACSignatureMethod.SHA256(params); |
| 226 | } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA384)) { |
| 227 | return new DOMHMACSignatureMethod.SHA384(params); |
| 228 | } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA512)) { |
| 229 | return new DOMHMACSignatureMethod.SHA512(params); |
| 230 | } else { |
| 231 | throw new NoSuchAlgorithmException("unsupported algorithm"); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public Transform newTransform(String algorithm, |
| 236 | TransformParameterSpec params) throws NoSuchAlgorithmException, |
| 237 | InvalidAlgorithmParameterException { |
| 238 | TransformService spi = TransformService.getInstance(algorithm, "DOM"); |
| 239 | spi.init(params); |
| 240 | return new DOMTransform(spi); |
| 241 | } |
| 242 | |
| 243 | public Transform newTransform(String algorithm, |
| 244 | XMLStructure params) throws NoSuchAlgorithmException, |
| 245 | InvalidAlgorithmParameterException { |
| 246 | TransformService spi = TransformService.getInstance(algorithm, "DOM"); |
| 247 | if (params == null) { |
| 248 | spi.init(null); |
| 249 | } else { |
| 250 | spi.init(params, null); |
| 251 | } |
| 252 | return new DOMTransform(spi); |
| 253 | } |
| 254 | |
| 255 | public CanonicalizationMethod newCanonicalizationMethod(String algorithm, |
| 256 | C14NMethodParameterSpec params) throws NoSuchAlgorithmException, |
| 257 | InvalidAlgorithmParameterException { |
| 258 | TransformService spi = TransformService.getInstance(algorithm, "DOM"); |
| 259 | spi.init(params); |
| 260 | return new DOMCanonicalizationMethod(spi); |
| 261 | } |
| 262 | |
| 263 | public CanonicalizationMethod newCanonicalizationMethod(String algorithm, |
| 264 | XMLStructure params) throws NoSuchAlgorithmException, |
| 265 | InvalidAlgorithmParameterException { |
| 266 | TransformService spi = TransformService.getInstance(algorithm, "DOM"); |
| 267 | if (params == null) { |
| 268 | spi.init(null); |
| 269 | } else { |
| 270 | spi.init(params, null); |
| 271 | } |
| 272 | return new DOMCanonicalizationMethod(spi); |
| 273 | } |
| 274 | |
| 275 | public URIDereferencer getURIDereferencer() { |
| 276 | return DOMURIDereferencer.INSTANCE; |
| 277 | } |
| 278 | } |