1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2005 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 /* 22 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * $Id: DOMXMLSignatureFactory.java,v 1.2 2008/07/24 15:20:32 mullan Exp $ 26 */ 27 package org.jcp.xml.dsig.internal.dom; 28 29 import javax.xml.crypto.*; 30 import javax.xml.crypto.dsig.*; 31 import javax.xml.crypto.dsig.dom.DOMValidateContext; 32 import javax.xml.crypto.dsig.keyinfo.*; 33 import javax.xml.crypto.dsig.spec.*; 34 35 import java.security.InvalidAlgorithmParameterException; 36 import java.security.NoSuchAlgorithmException; 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, getProvider()); 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, getProvider()); 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, getProvider()); 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, getProvider()); 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; 239 try { 240 spi = TransformService.getInstance(algorithm, "DOM"); 241 } catch (NoSuchAlgorithmException nsae) { 242 spi = TransformService.getInstance(algorithm, "DOM", getProvider()); 243 } 244 spi.init(params); 245 return new DOMTransform(spi); 246 } 247 248 public Transform newTransform(String algorithm, 249 XMLStructure params) throws NoSuchAlgorithmException, 250 InvalidAlgorithmParameterException { 251 TransformService spi; 252 try { 253 spi = TransformService.getInstance(algorithm, "DOM"); 254 } catch (NoSuchAlgorithmException nsae) { 255 spi = TransformService.getInstance(algorithm, "DOM", getProvider()); 256 } 257 if (params == null) { 258 spi.init(null); 259 } else { 260 spi.init(params, null); 261 } 262 return new DOMTransform(spi); 263 } 264 265 public CanonicalizationMethod newCanonicalizationMethod(String algorithm, 266 C14NMethodParameterSpec params) throws NoSuchAlgorithmException, 267 InvalidAlgorithmParameterException { 268 TransformService spi; 269 try { 270 spi = TransformService.getInstance(algorithm, "DOM"); 271 } catch (NoSuchAlgorithmException nsae) { 272 spi = TransformService.getInstance(algorithm, "DOM", getProvider()); 273 } 274 spi.init(params); 275 return new DOMCanonicalizationMethod(spi); 276 } 277 278 public CanonicalizationMethod newCanonicalizationMethod(String algorithm, 279 XMLStructure params) throws NoSuchAlgorithmException, 280 InvalidAlgorithmParameterException { 281 TransformService spi; 282 try { 283 spi = TransformService.getInstance(algorithm, "DOM"); 284 } catch (NoSuchAlgorithmException nsae) { 285 spi = TransformService.getInstance(algorithm, "DOM", getProvider()); 286 } 287 if (params == null) { 288 spi.init(null); 289 } else { 290 spi.init(params, null); 291 } 292 return new DOMCanonicalizationMethod(spi); 293 } 294 295 public URIDereferencer getURIDereferencer() { 296 return DOMURIDereferencer.INSTANCE; 297 } 298 }