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 }