src/share/classes/org/jcp/xml/dsig/internal/dom/DOMCanonicalizationMethod.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: DOMCanonicalizationMethod.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
  26  */
  27 package org.jcp.xml.dsig.internal.dom;
  28 
  29 import java.io.OutputStream;
  30 import java.security.InvalidAlgorithmParameterException;
  31 import java.security.Provider;

  32 
  33 import org.w3c.dom.Element;
  34 
  35 import javax.xml.crypto.*;
  36 import javax.xml.crypto.dsig.*;
  37 
  38 /**
  39  * DOM-based abstract implementation of CanonicalizationMethod.
  40  *
  41  * @author Sean Mullan
  42  */
  43 public class DOMCanonicalizationMethod extends DOMTransform
  44     implements CanonicalizationMethod {
  45 
  46     /**
  47      * Creates a <code>DOMCanonicalizationMethod</code>.
  48      *
  49      * @param spi TransformService
  50      */
  51     public DOMCanonicalizationMethod(TransformService spi)
  52         throws InvalidAlgorithmParameterException {

  53         super(spi);
  54         if (!(spi instanceof ApacheCanonicalizer) &&
  55                 !isC14Nalg(spi.getAlgorithm())) {
  56             throw new InvalidAlgorithmParameterException(
  57                 "Illegal CanonicalizationMethod");
  58         }
  59     }
  60 
  61     /**
  62      * Creates a <code>DOMCanonicalizationMethod</code> from an element. This
  63      * ctor invokes the abstract {@link #unmarshalParams unmarshalParams}
  64      * method to unmarshal any algorithm-specific input parameters.
  65      *
  66      * @param cmElem a CanonicalizationMethod element
  67      */
  68     public DOMCanonicalizationMethod(Element cmElem, XMLCryptoContext context,
  69         Provider provider) throws MarshalException {


  70         super(cmElem, context, provider);
  71         if (!(spi instanceof ApacheCanonicalizer) &&
  72                 !isC14Nalg(spi.getAlgorithm())) {
  73             throw new MarshalException("Illegal CanonicalizationMethod");
  74         }
  75     }
  76 
  77     /**
  78      * Canonicalizes the specified data using the underlying canonicalization
  79      * algorithm. This is a convenience method that is equivalent to invoking
  80      * the {@link #transform transform} method.
  81      *
  82      * @param data the data to be canonicalized
  83      * @param xc the <code>XMLCryptoContext</code> containing
  84      *     additional context (may be <code>null</code> if not applicable)
  85      * @return the canonicalized data
  86      * @throws NullPointerException if <code>data</code> is <code>null</code>
  87      * @throws TransformException if an unexpected error occurs while
  88      *    canonicalizing the data
  89      */
  90     public Data canonicalize(Data data, XMLCryptoContext xc)
  91         throws TransformException {

  92         return transform(data, xc);
  93     }
  94 
  95     public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os)
  96         throws TransformException {

  97         return transform(data, xc, os);
  98     }
  99 

 100     public boolean equals(Object o) {
 101         if (this == o) {
 102             return true;
 103         }
 104 
 105         if (!(o instanceof CanonicalizationMethod)) {
 106             return false;
 107         }
 108         CanonicalizationMethod ocm = (CanonicalizationMethod) o;
 109 
 110         return (getAlgorithm().equals(ocm.getAlgorithm()) &&
 111             DOMUtils.paramsEqual(getParameterSpec(), ocm.getParameterSpec()));
 112     }
 113 












 114     private static boolean isC14Nalg(String alg) {
 115         return (alg.equals(CanonicalizationMethod.INCLUSIVE) ||
 116                 alg.equals(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS) ||
 117                 alg.equals(CanonicalizationMethod.EXCLUSIVE) ||
 118                 alg.equals(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS) ||
 119                 alg.equals(DOMCanonicalXMLC14N11Method.C14N_11) ||
 120                 alg.equals(DOMCanonicalXMLC14N11Method.C14N_11_WITH_COMMENTS));
 121     }
 122 }
   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: DOMCanonicalizationMethod.java 1333415 2012-05-03 12:03:51Z coheigea $
  28  */
  29 package org.jcp.xml.dsig.internal.dom;
  30 
  31 import java.io.OutputStream;
  32 import java.security.InvalidAlgorithmParameterException;
  33 import java.security.Provider;
  34 import java.security.spec.AlgorithmParameterSpec;
  35 
  36 import org.w3c.dom.Element;
  37 
  38 import javax.xml.crypto.*;
  39 import javax.xml.crypto.dsig.*;
  40 
  41 /**
  42  * DOM-based abstract implementation of CanonicalizationMethod.
  43  *
  44  * @author Sean Mullan
  45  */
  46 public class DOMCanonicalizationMethod extends DOMTransform 
  47     implements CanonicalizationMethod {
  48 
  49     /**
  50      * Creates a <code>DOMCanonicalizationMethod</code>.
  51      *
  52      * @param spi TransformService
  53      */
  54     public DOMCanonicalizationMethod(TransformService spi)
  55         throws InvalidAlgorithmParameterException
  56     {
  57         super(spi);
  58         if (!(spi instanceof ApacheCanonicalizer) &&
  59                 !isC14Nalg(spi.getAlgorithm())) {
  60             throw new InvalidAlgorithmParameterException(
  61                 "Illegal CanonicalizationMethod");
  62         }
  63     }
  64 
  65     /**
  66      * Creates a <code>DOMCanonicalizationMethod</code> from an element. This 
  67      * ctor invokes the abstract {@link #unmarshalParams unmarshalParams} 
  68      * method to unmarshal any algorithm-specific input parameters.
  69      *
  70      * @param cmElem a CanonicalizationMethod element
  71      */
  72     public DOMCanonicalizationMethod(Element cmElem, XMLCryptoContext context,
  73                                      Provider provider)
  74         throws MarshalException
  75     {
  76         super(cmElem, context, provider);
  77         if (!(spi instanceof ApacheCanonicalizer) &&
  78                 !isC14Nalg(spi.getAlgorithm())) {
  79             throw new MarshalException("Illegal CanonicalizationMethod");
  80         }
  81     }
  82 
  83     /**
  84      * Canonicalizes the specified data using the underlying canonicalization
  85      * algorithm. This is a convenience method that is equivalent to invoking
  86      * the {@link #transform transform} method.
  87      *
  88      * @param data the data to be canonicalized
  89      * @param xc the <code>XMLCryptoContext</code> containing
  90      *     additional context (may be <code>null</code> if not applicable)
  91      * @return the canonicalized data
  92      * @throws NullPointerException if <code>data</code> is <code>null</code>
  93      * @throws TransformException if an unexpected error occurs while
  94      *    canonicalizing the data
  95      */
  96     public Data canonicalize(Data data, XMLCryptoContext xc)
  97         throws TransformException
  98     {
  99         return transform(data, xc);
 100     }
 101 
 102     public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os)
 103         throws TransformException
 104     {
 105         return transform(data, xc, os);
 106     }
 107 
 108     @Override
 109     public boolean equals(Object o) {
 110         if (this == o) {
 111             return true;
 112         }
 113 
 114         if (!(o instanceof CanonicalizationMethod)) {
 115             return false;
 116         }
 117         CanonicalizationMethod ocm = (CanonicalizationMethod)o;
 118 
 119         return (getAlgorithm().equals(ocm.getAlgorithm()) &&
 120             DOMUtils.paramsEqual(getParameterSpec(), ocm.getParameterSpec()));
 121     }
 122 
 123     @Override
 124     public int hashCode() {
 125         int result = 17;
 126         result = 31 * result + getAlgorithm().hashCode();
 127         AlgorithmParameterSpec spec = getParameterSpec();
 128         if (spec != null) {
 129             result = 31 * result + spec.hashCode();
 130         }
 131 
 132         return result;
 133     }
 134     
 135     private static boolean isC14Nalg(String alg) {
 136         return (alg.equals(CanonicalizationMethod.INCLUSIVE) ||
 137                 alg.equals(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS) ||
 138                 alg.equals(CanonicalizationMethod.EXCLUSIVE) ||
 139                 alg.equals(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS) ||
 140                 alg.equals(DOMCanonicalXMLC14N11Method.C14N_11) ||
 141                 alg.equals(DOMCanonicalXMLC14N11Method.C14N_11_WITH_COMMENTS));
 142     }
 143 }