0N/A/*
661N/A * reserved comment block
661N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
661N/A * Copyright 2005 The Apache Software Foundation.
661N/A *
661N/A * Licensed under the Apache License, Version 2.0 (the "License");
661N/A * you may not use this file except in compliance with the License.
661N/A * You may obtain a copy of the License at
661N/A *
661N/A * http://www.apache.org/licenses/LICENSE-2.0
661N/A *
661N/A * Unless required by applicable law or agreed to in writing, software
661N/A * distributed under the License is distributed on an "AS IS" BASIS,
661N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
661N/A * See the License for the specific language governing permissions and
661N/A * limitations under the License.
661N/A *
661N/A */
661N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
661N/A */
661N/A/*
661N/A * $Id: DOMXMLSignatureFactory.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
0N/A */
0N/Apackage org.jcp.xml.dsig.internal.dom;
0N/A
0N/Aimport javax.xml.crypto.*;
0N/Aimport javax.xml.crypto.dsig.*;
0N/Aimport javax.xml.crypto.dsig.dom.DOMValidateContext;
0N/Aimport javax.xml.crypto.dsig.keyinfo.*;
0N/Aimport javax.xml.crypto.dsig.spec.*;
0N/A
661N/Aimport java.security.InvalidAlgorithmParameterException;
661N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.util.List;
0N/Aimport org.w3c.dom.Document;
0N/Aimport org.w3c.dom.Element;
0N/Aimport org.w3c.dom.Node;
0N/A
0N/A/**
0N/A * DOM-based implementation of XMLSignatureFactory.
0N/A *
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class DOMXMLSignatureFactory extends XMLSignatureFactory {
0N/A
0N/A /**
0N/A * Initializes a new instance of this class.
0N/A */
0N/A public DOMXMLSignatureFactory() {}
0N/A
0N/A public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki) {
0N/A return new DOMXMLSignature(si, ki, null, null, null);
0N/A }
0N/A
0N/A public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki,
0N/A List objects, String id, String signatureValueId) {
0N/A return new DOMXMLSignature(si, ki, objects, id, signatureValueId);
0N/A }
0N/A
0N/A public Reference newReference(String uri, DigestMethod dm) {
0N/A return newReference(uri, dm, null, null, null);
0N/A }
0N/A
0N/A public Reference newReference(String uri, DigestMethod dm, List transforms,
0N/A String type, String id) {
661N/A return new DOMReference(uri, type, dm, transforms, id, getProvider());
0N/A }
0N/A
0N/A public Reference newReference(String uri, DigestMethod dm,
0N/A List appliedTransforms, Data result, List transforms, String type,
0N/A String id) {
0N/A if (appliedTransforms == null) {
0N/A throw new NullPointerException("appliedTransforms cannot be null");
0N/A }
0N/A if (appliedTransforms.isEmpty()) {
0N/A throw new NullPointerException("appliedTransforms cannot be empty");
0N/A }
0N/A if (result == null) {
0N/A throw new NullPointerException("result cannot be null");
0N/A }
0N/A return new DOMReference
661N/A (uri, type, dm, appliedTransforms, result, transforms, id, getProvider());
0N/A }
0N/A
0N/A public Reference newReference(String uri, DigestMethod dm, List transforms,
0N/A String type, String id, byte[] digestValue) {
0N/A if (digestValue == null) {
0N/A throw new NullPointerException("digestValue cannot be null");
0N/A }
0N/A return new DOMReference
661N/A (uri, type, dm, null, null, transforms, id, digestValue, getProvider());
0N/A }
0N/A
0N/A public SignedInfo newSignedInfo(CanonicalizationMethod cm,
0N/A SignatureMethod sm, List references) {
0N/A return newSignedInfo(cm, sm, references, null);
0N/A }
0N/A
0N/A public SignedInfo newSignedInfo(CanonicalizationMethod cm,
0N/A SignatureMethod sm, List references, String id) {
0N/A return new DOMSignedInfo(cm, sm, references, id);
0N/A }
0N/A
0N/A // Object factory methods
0N/A public XMLObject newXMLObject(List content, String id, String mimeType,
0N/A String encoding) {
0N/A return new DOMXMLObject(content, id, mimeType, encoding);
0N/A }
0N/A
0N/A public Manifest newManifest(List references) {
0N/A return newManifest(references, null);
0N/A }
0N/A
0N/A public Manifest newManifest(List references, String id) {
0N/A return new DOMManifest(references, id);
0N/A }
0N/A
0N/A public SignatureProperties newSignatureProperties(List props, String id) {
0N/A return new DOMSignatureProperties(props, id);
0N/A }
0N/A
0N/A public SignatureProperty newSignatureProperty
0N/A (List info, String target, String id) {
0N/A return new DOMSignatureProperty(info, target, id);
0N/A }
0N/A
0N/A public XMLSignature unmarshalXMLSignature(XMLValidateContext context)
0N/A throws MarshalException {
0N/A
0N/A if (context == null) {
0N/A throw new NullPointerException("context cannot be null");
0N/A }
0N/A return unmarshal(((DOMValidateContext) context).getNode(), context);
0N/A }
0N/A
0N/A public XMLSignature unmarshalXMLSignature(XMLStructure xmlStructure)
0N/A throws MarshalException {
0N/A
0N/A if (xmlStructure == null) {
0N/A throw new NullPointerException("xmlStructure cannot be null");
0N/A }
0N/A return unmarshal
0N/A (((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode(),
0N/A null);
0N/A }
0N/A
0N/A private XMLSignature unmarshal(Node node, XMLValidateContext context)
0N/A throws MarshalException {
0N/A
0N/A node.normalize();
0N/A
0N/A Element element = null;
0N/A if (node.getNodeType() == Node.DOCUMENT_NODE) {
0N/A element = ((Document) node).getDocumentElement();
0N/A } else if (node.getNodeType() == Node.ELEMENT_NODE) {
0N/A element = (Element) node;
0N/A } else {
0N/A throw new MarshalException
0N/A ("Signature element is not a proper Node");
0N/A }
0N/A
0N/A // check tag
0N/A String tag = element.getLocalName();
0N/A if (tag == null) {
0N/A throw new MarshalException("Document implementation must " +
0N/A "support DOM Level 2 and be namespace aware");
0N/A }
0N/A if (tag.equals("Signature")) {
661N/A return new DOMXMLSignature(element, context, getProvider());
0N/A } else {
0N/A throw new MarshalException("invalid Signature tag: " + tag);
0N/A }
0N/A }
0N/A
0N/A public boolean isFeatureSupported(String feature) {
0N/A if (feature == null) {
0N/A throw new NullPointerException();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public DigestMethod newDigestMethod(String algorithm,
0N/A DigestMethodParameterSpec params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
0N/A if (algorithm == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (algorithm.equals(DigestMethod.SHA1)) {
0N/A return new DOMDigestMethod.SHA1(params);
0N/A } else if (algorithm.equals(DigestMethod.SHA256)) {
0N/A return new DOMDigestMethod.SHA256(params);
0N/A } else if (algorithm.equals(DOMDigestMethod.SHA384)) {
0N/A return new DOMDigestMethod.SHA384(params);
0N/A } else if (algorithm.equals(DigestMethod.SHA512)) {
0N/A return new DOMDigestMethod.SHA512(params);
0N/A } else {
0N/A throw new NoSuchAlgorithmException("unsupported algorithm");
0N/A }
0N/A }
0N/A
0N/A public SignatureMethod newSignatureMethod(String algorithm,
0N/A SignatureMethodParameterSpec params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
0N/A if (algorithm == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (algorithm.equals(SignatureMethod.RSA_SHA1)) {
0N/A return new DOMSignatureMethod.SHA1withRSA(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA256)) {
0N/A return new DOMSignatureMethod.SHA256withRSA(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA384)) {
0N/A return new DOMSignatureMethod.SHA384withRSA(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.RSA_SHA512)) {
0N/A return new DOMSignatureMethod.SHA512withRSA(params);
0N/A } else if (algorithm.equals(SignatureMethod.DSA_SHA1)) {
0N/A return new DOMSignatureMethod.SHA1withDSA(params);
0N/A } else if (algorithm.equals(SignatureMethod.HMAC_SHA1)) {
0N/A return new DOMHMACSignatureMethod.SHA1(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA256)) {
0N/A return new DOMHMACSignatureMethod.SHA256(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA384)) {
0N/A return new DOMHMACSignatureMethod.SHA384(params);
0N/A } else if (algorithm.equals(DOMSignatureMethod.HMAC_SHA512)) {
0N/A return new DOMHMACSignatureMethod.SHA512(params);
0N/A } else {
0N/A throw new NoSuchAlgorithmException("unsupported algorithm");
0N/A }
0N/A }
0N/A
0N/A public Transform newTransform(String algorithm,
0N/A TransformParameterSpec params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
661N/A TransformService spi;
661N/A try {
661N/A spi = TransformService.getInstance(algorithm, "DOM");
661N/A } catch (NoSuchAlgorithmException nsae) {
661N/A spi = TransformService.getInstance(algorithm, "DOM", getProvider());
661N/A }
0N/A spi.init(params);
0N/A return new DOMTransform(spi);
0N/A }
0N/A
0N/A public Transform newTransform(String algorithm,
0N/A XMLStructure params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
661N/A TransformService spi;
661N/A try {
661N/A spi = TransformService.getInstance(algorithm, "DOM");
661N/A } catch (NoSuchAlgorithmException nsae) {
661N/A spi = TransformService.getInstance(algorithm, "DOM", getProvider());
661N/A }
0N/A if (params == null) {
0N/A spi.init(null);
0N/A } else {
0N/A spi.init(params, null);
0N/A }
0N/A return new DOMTransform(spi);
0N/A }
0N/A
0N/A public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
0N/A C14NMethodParameterSpec params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
661N/A TransformService spi;
661N/A try {
661N/A spi = TransformService.getInstance(algorithm, "DOM");
661N/A } catch (NoSuchAlgorithmException nsae) {
661N/A spi = TransformService.getInstance(algorithm, "DOM", getProvider());
661N/A }
0N/A spi.init(params);
0N/A return new DOMCanonicalizationMethod(spi);
0N/A }
0N/A
0N/A public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
0N/A XMLStructure params) throws NoSuchAlgorithmException,
0N/A InvalidAlgorithmParameterException {
661N/A TransformService spi;
661N/A try {
661N/A spi = TransformService.getInstance(algorithm, "DOM");
661N/A } catch (NoSuchAlgorithmException nsae) {
661N/A spi = TransformService.getInstance(algorithm, "DOM", getProvider());
661N/A }
0N/A if (params == null) {
0N/A spi.init(null);
0N/A } else {
0N/A spi.init(params, null);
0N/A }
0N/A return new DOMCanonicalizationMethod(spi);
0N/A }
0N/A
0N/A public URIDereferencer getURIDereferencer() {
0N/A return DOMURIDereferencer.INSTANCE;
0N/A }
0N/A}