src/share/classes/org/jcp/xml/dsig/internal/dom/DOMXMLSignatureFactory.java

Print this page


   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         }


 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 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   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
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  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.
  22  */
  23 /*
  24  * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 /*
  27  * $Id: DOMXMLSignatureFactory.java 1333869 2012-05-04 10:42:44Z coheigea $
  28  */
  29 package org.jcp.xml.dsig.internal.dom;
  30 
  31 import javax.xml.crypto.*;
  32 import javax.xml.crypto.dom.DOMCryptoContext;
  33 import javax.xml.crypto.dsig.*;
  34 import javax.xml.crypto.dsig.dom.DOMValidateContext;
  35 import javax.xml.crypto.dsig.keyinfo.*;
  36 import javax.xml.crypto.dsig.spec.*;
  37 
  38 import java.security.InvalidAlgorithmParameterException;
  39 import java.security.NoSuchAlgorithmException;

  40 import java.util.List;
  41 import org.w3c.dom.Document;
  42 import org.w3c.dom.Element;
  43 import org.w3c.dom.Node;
  44 
  45 /**
  46  * DOM-based implementation of XMLSignatureFactory.
  47  *
  48  * @author Sean Mullan
  49  */
  50 public final class DOMXMLSignatureFactory extends XMLSignatureFactory {
  51 
  52     /**
  53      * Initializes a new instance of this class.
  54      */
  55     public DOMXMLSignatureFactory() {}
  56 
  57     public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki) {
  58         return new DOMXMLSignature(si, ki, null, null, null);
  59     }
  60 
  61     @SuppressWarnings("unchecked")
  62     public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki,
  63         List objects, String id, String signatureValueId) {
  64         return new DOMXMLSignature(si, ki, objects, id, signatureValueId);
  65     }
  66 
  67     public Reference newReference(String uri, DigestMethod dm) {
  68         return newReference(uri, dm, null, null, null);
  69     }
  70 
  71     @SuppressWarnings("unchecked")
  72     public Reference newReference(String uri, DigestMethod dm, List transforms,
  73         String type, String id) {
  74         return new DOMReference(uri, type, dm, transforms, id, getProvider());
  75     }
  76 
  77     @SuppressWarnings("unchecked")
  78     public Reference newReference(String uri, DigestMethod dm, 
  79         List appliedTransforms, Data result, List transforms, String type, 
  80         String id) {
  81         if (appliedTransforms == null) {
  82             throw new NullPointerException("appliedTransforms cannot be null");
  83         }
  84         if (appliedTransforms.isEmpty()) {
  85             throw new NullPointerException("appliedTransforms cannot be empty");
  86         }
  87         if (result == null) {
  88             throw new NullPointerException("result cannot be null");
  89         }
  90         return new DOMReference
  91             (uri, type, dm, appliedTransforms, result, transforms, id, getProvider());
  92     }
  93 
  94     @SuppressWarnings("unchecked")
  95     public Reference newReference(String uri, DigestMethod dm, List transforms,
  96         String type, String id, byte[] digestValue) {
  97         if (digestValue == null) {
  98             throw new NullPointerException("digestValue cannot be null");
  99         }
 100         return new DOMReference
 101             (uri, type, dm, null, null, transforms, id, digestValue, getProvider());
 102     }
 103 
 104     @SuppressWarnings("unchecked")
 105     public SignedInfo newSignedInfo(CanonicalizationMethod cm,
 106         SignatureMethod sm, List references) {
 107         return newSignedInfo(cm, sm, references, null);
 108     }
 109 
 110     @SuppressWarnings("unchecked")
 111     public SignedInfo newSignedInfo(CanonicalizationMethod cm,
 112         SignatureMethod sm, List references, String id) {
 113         return new DOMSignedInfo(cm, sm, references, id);
 114     }
 115 
 116     // Object factory methods
 117     @SuppressWarnings("unchecked")
 118     public XMLObject newXMLObject(List content, String id, String mimeType,
 119         String encoding) {
 120         return new DOMXMLObject(content, id, mimeType, encoding);
 121     }
 122 
 123     @SuppressWarnings("unchecked")
 124     public Manifest newManifest(List references) {
 125         return newManifest(references, null);
 126     }
 127 
 128     @SuppressWarnings("unchecked")
 129     public Manifest newManifest(List references, String id) {
 130         return new DOMManifest(references, id);
 131     }
 132 
 133     @SuppressWarnings("unchecked")
 134     public SignatureProperties newSignatureProperties(List props, String id) {
 135         return new DOMSignatureProperties(props, id);
 136     }
 137 
 138     @SuppressWarnings("unchecked")
 139     public SignatureProperty newSignatureProperty
 140         (List info, String target, String id) {
 141         return new DOMSignatureProperty(info, target, id);
 142     }
 143 
 144     public XMLSignature unmarshalXMLSignature(XMLValidateContext context)
 145         throws MarshalException {
 146 
 147         if (context == null) {
 148             throw new NullPointerException("context cannot be null");
 149         }
 150         return unmarshal(((DOMValidateContext) context).getNode(), context);
 151     }
 152 
 153     public XMLSignature unmarshalXMLSignature(XMLStructure xmlStructure)
 154         throws MarshalException {
 155 
 156         if (xmlStructure == null) {
 157             throw new NullPointerException("xmlStructure cannot be null");
 158         }
 159         if (!(xmlStructure instanceof javax.xml.crypto.dom.DOMStructure)) {
 160             throw new ClassCastException("xmlStructure must be of type DOMStructure");
 161         }
 162         return unmarshal
 163             (((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode(), 
 164              new UnmarshalContext());
 165     }
 166 
 167     private static class UnmarshalContext extends DOMCryptoContext {
 168         UnmarshalContext() {}
 169     }
 170 
 171     private XMLSignature unmarshal(Node node, XMLCryptoContext context) 
 172         throws MarshalException {
 173 
 174         node.normalize();
 175         
 176         Element element = null;
 177         if (node.getNodeType() == Node.DOCUMENT_NODE) {
 178             element = ((Document) node).getDocumentElement();
 179         } else if (node.getNodeType() == Node.ELEMENT_NODE) {
 180             element = (Element) node;
 181         } else {
 182             throw new MarshalException
 183                 ("Signature element is not a proper Node");
 184         }
 185 
 186         // check tag
 187         String tag = element.getLocalName();
 188         if (tag == null) {
 189             throw new MarshalException("Document implementation must " +
 190                 "support DOM Level 2 and be namespace aware");
 191         }


 224     }
 225 
 226     public SignatureMethod newSignatureMethod(String algorithm,
 227         SignatureMethodParameterSpec params) throws NoSuchAlgorithmException,
 228         InvalidAlgorithmParameterException {
 229         if (algorithm == null) {
 230             throw new NullPointerException();
 231         }
 232         if (algorithm.equals(SignatureMethod.RSA_SHA1)) {
 233             return new DOMSignatureMethod.SHA1withRSA(params);
 234         } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA256)) {
 235             return new DOMSignatureMethod.SHA256withRSA(params);
 236         } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA384)) {
 237             return new DOMSignatureMethod.SHA384withRSA(params);
 238         } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA512)) {
 239             return new DOMSignatureMethod.SHA512withRSA(params);
 240         } else if (algorithm.equals(SignatureMethod.DSA_SHA1)) {
 241             return new DOMSignatureMethod.SHA1withDSA(params);
 242         } else if (algorithm.equals(SignatureMethod.HMAC_SHA1)) {
 243             return new DOMHMACSignatureMethod.SHA1(params);
 244         } else if (algorithm.equals(DOMHMACSignatureMethod.HMAC_SHA256)) {
 245             return new DOMHMACSignatureMethod.SHA256(params);
 246         } else if (algorithm.equals(DOMHMACSignatureMethod.HMAC_SHA384)) {
 247             return new DOMHMACSignatureMethod.SHA384(params);
 248         } else if (algorithm.equals(DOMHMACSignatureMethod.HMAC_SHA512)) {
 249             return new DOMHMACSignatureMethod.SHA512(params);
 250         } else if (algorithm.equals(DOMSignatureMethod.ECDSA_SHA1)) {
 251             return new DOMSignatureMethod.SHA1withECDSA(params);
 252         } else if (algorithm.equals(DOMSignatureMethod.ECDSA_SHA256)) {
 253             return new DOMSignatureMethod.SHA256withECDSA(params);
 254         } else if (algorithm.equals(DOMSignatureMethod.ECDSA_SHA384)) {
 255             return new DOMSignatureMethod.SHA384withECDSA(params);
 256         } else if (algorithm.equals(DOMSignatureMethod.ECDSA_SHA512)) {
 257             return new DOMSignatureMethod.SHA512withECDSA(params);
 258         } else {
 259             throw new NoSuchAlgorithmException("unsupported algorithm");
 260         }
 261     }
 262 
 263     public Transform newTransform(String algorithm,
 264         TransformParameterSpec params) throws NoSuchAlgorithmException,
 265         InvalidAlgorithmParameterException {
 266         
 267         TransformService spi;
 268         if (getProvider() == null) {
 269             spi = TransformService.getInstance(algorithm, "DOM");
 270         } else {
 271             try {
 272                 spi = TransformService.getInstance(algorithm, "DOM", getProvider());
 273             } catch (NoSuchAlgorithmException nsae) {
 274                 spi = TransformService.getInstance(algorithm, "DOM");
 275             }
 276         }
 277         
 278         spi.init(params);
 279         return new DOMTransform(spi);
 280     }
 281 
 282     public Transform newTransform(String algorithm,
 283         XMLStructure params) throws NoSuchAlgorithmException,
 284         InvalidAlgorithmParameterException {
 285         TransformService spi;
 286         if (getProvider() == null) {
 287             spi = TransformService.getInstance(algorithm, "DOM");
 288         } else {
 289             try {
 290                 spi = TransformService.getInstance(algorithm, "DOM", getProvider());
 291             } catch (NoSuchAlgorithmException nsae) {
 292                 spi = TransformService.getInstance(algorithm, "DOM");
 293             }
 294         }
 295         
 296         if (params == null) {
 297             spi.init(null);
 298         } else {
 299             spi.init(params, null);
 300         }
 301         return new DOMTransform(spi);
 302     }
 303 
 304     public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
 305         C14NMethodParameterSpec params) throws NoSuchAlgorithmException,
 306         InvalidAlgorithmParameterException {
 307         TransformService spi;
 308         if (getProvider() == null) {
 309             spi = TransformService.getInstance(algorithm, "DOM");
 310         } else {
 311             try {
 312                 spi = TransformService.getInstance(algorithm, "DOM", getProvider());
 313             } catch (NoSuchAlgorithmException nsae) {
 314                 spi = TransformService.getInstance(algorithm, "DOM");
 315             }
 316         }
 317         
 318         spi.init(params);
 319         return new DOMCanonicalizationMethod(spi);
 320     }
 321 
 322     public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
 323         XMLStructure params) throws NoSuchAlgorithmException,
 324         InvalidAlgorithmParameterException {
 325         TransformService spi; 
 326         if (getProvider() == null) {
 327             spi = TransformService.getInstance(algorithm, "DOM");
 328         } else {
 329             try {
 330                 spi = TransformService.getInstance(algorithm, "DOM", getProvider());
 331             } catch (NoSuchAlgorithmException nsae) {
 332                 spi = TransformService.getInstance(algorithm, "DOM");
 333             }
 334         }
 335         if (params == null) {
 336             spi.init(null);
 337         } else {
 338             spi.init(params, null);
 339         }
 340 
 341         return new DOMCanonicalizationMethod(spi);
 342     }
 343 
 344     public URIDereferencer getURIDereferencer() {
 345         return DOMURIDereferencer.INSTANCE;
 346     }
 347 }