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 * ===========================================================================
25 *
26 * (C) Copyright IBM Corp. 2003 All Rights Reserved.
27 *
28 * ===========================================================================
29 */
30/*
31 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
32 */
33/*
34 * $Id: XMLDSigRI.java 1400021 2012-10-19 10:16:04Z coheigea $
35 */
36package org.jcp.xml.dsig.internal.dom;
37
38import java.util.*;
39import java.security.*;
40
41import javax.xml.crypto.dsig.*;
42
43/**
44 * The XMLDSig RI Provider.
45 *
46 * @author Joyce Leung
47 */
48
49/**
50 * Defines the XMLDSigRI provider.
51 */
52
53public final class XMLDSigRI extends Provider {
54
55    static final long serialVersionUID = -5049765099299494554L;
56
57    private static final String INFO = "XMLDSig " +
58        "(DOM XMLSignatureFactory; DOM KeyInfoFactory; " +
59        "C14N 1.0, C14N 1.1, Exclusive C14N, Base64, Enveloped, XPath, " +
60        "XPath2, XSLT TransformServices)";
61
62    private static final String VER =
63        AccessController.doPrivileged(new PrivilegedAction<>() {
64            public String run() {
65                return System.getProperty("java.specification.version");
66            }
67        });
68
69    private static final class ProviderService extends Provider.Service {
70
71        ProviderService(Provider p, String type, String algo, String cn) {
72            super(p, type, algo, cn, null, null);
73        }
74
75        ProviderService(Provider p, String type, String algo, String cn,
76            String[] aliases) {
77            super(p, type, algo, cn,
78                (aliases == null? null : Arrays.asList(aliases)), null);
79        }
80
81        ProviderService(Provider p, String type, String algo, String cn,
82            String[] aliases, HashMap<String, String> attrs) {
83            super(p, type, algo, cn,
84                  (aliases == null? null : Arrays.asList(aliases)), attrs);
85        }
86
87        @Override
88        public Object newInstance(Object ctrParamObj)
89            throws NoSuchAlgorithmException {
90            String type = getType();
91            if (ctrParamObj != null) {
92                throw new InvalidParameterException
93                    ("constructorParameter not used with " + type + " engines");
94            }
95
96            String algo = getAlgorithm();
97            try {
98                if (type.equals("XMLSignatureFactory")) {
99                    if (algo.equals("DOM")) {
100                        return new DOMXMLSignatureFactory();
101                    }
102                } else if (type.equals("KeyInfoFactory")) {
103                    if (algo.equals("DOM")) {
104                        return new DOMKeyInfoFactory();
105                    }
106                } else if (type.equals("TransformService")) {
107                    if (algo.equals(CanonicalizationMethod.INCLUSIVE) ||
108                        algo.equals(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS)) {
109                        return new DOMCanonicalXMLC14NMethod();
110                    } else if (algo.equals("http://www.w3.org/2006/12/xml-c14n11") ||
111                        algo.equals("http://www.w3.org/2006/12/xml-c14n11#WithComments")) {
112                        return new DOMCanonicalXMLC14N11Method();
113                    } else if (algo.equals(CanonicalizationMethod.EXCLUSIVE) ||
114                        algo.equals(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS)) {
115                        return new DOMExcC14NMethod();
116                    } else if (algo.equals(Transform.BASE64)) {
117                        return new DOMBase64Transform();
118                    } else if (algo.equals(Transform.ENVELOPED)) {
119                        return new DOMEnvelopedTransform();
120                    } else if (algo.equals(Transform.XPATH2)) {
121                        return new DOMXPathFilter2Transform();
122                    } else if (algo.equals(Transform.XPATH)) {
123                        return new DOMXPathTransform();
124                    } else if (algo.equals(Transform.XSLT)) {
125                        return new DOMXSLTTransform();
126                    }
127                 }
128            } catch (Exception ex) {
129                throw new NoSuchAlgorithmException("Error constructing " +
130                    type + " for " + algo + " using XMLDSig", ex);
131            }
132            throw new ProviderException("No impl for " + algo +
133                " " + type);
134        }
135    }
136
137    public XMLDSigRI() {
138        /* We are the XMLDSig provider */
139        super("XMLDSig", VER, INFO);
140
141        final Provider p = this;
142        AccessController.doPrivileged(new PrivilegedAction<Void>() {
143            public Void run() {
144                HashMap<String, String> MECH_TYPE = new HashMap<>();
145                MECH_TYPE.put("MechanismType", "DOM");
146
147                putService(new ProviderService(p, "XMLSignatureFactory",
148                    "DOM", "org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory"));
149
150                putService(new ProviderService(p, "KeyInfoFactory",
151                    "DOM", "org.jcp.xml.dsig.internal.dom.DOMKeyInfoFactory"));
152
153
154                // Inclusive C14N
155                putService(new ProviderService(p, "TransformService",
156                    CanonicalizationMethod.INCLUSIVE,
157                    "org.jcp.xml.dsig.internal.dom.DOMCanonicalXMLC14NMethod",
158                    new String[] {"INCLUSIVE"}, MECH_TYPE));
159
160                // InclusiveWithComments C14N
161                putService(new ProviderService(p, "TransformService",
162                    CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
163                    "org.jcp.xml.dsig.internal.dom.DOMCanonicalXMLC14NMethod",
164                    new String[] {"INCLUSIVE_WITH_COMMENTS"}, MECH_TYPE));
165
166                // Inclusive C14N 1.1
167                putService(new ProviderService(p, "TransformService",
168                    "http://www.w3.org/2006/12/xml-c14n11",
169                    "org.jcp.xml.dsig.internal.dom.DOMCanonicalXMLC14N11Method",
170                    null, MECH_TYPE));
171
172                // InclusiveWithComments C14N 1.1
173                putService(new ProviderService(p, "TransformService",
174                    "http://www.w3.org/2006/12/xml-c14n11#WithComments",
175                    "org.jcp.xml.dsig.internal.dom.DOMCanonicalXMLC14N11Method",
176                    null, MECH_TYPE));
177
178                // Exclusive C14N
179                putService(new ProviderService(p, "TransformService",
180                    CanonicalizationMethod.EXCLUSIVE,
181                    "org.jcp.xml.dsig.internal.dom.DOMExcC14NMethod",
182                    new String[] {"EXCLUSIVE"}, MECH_TYPE));
183
184                // ExclusiveWithComments C14N
185                putService(new ProviderService(p, "TransformService",
186                    CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS,
187                    "org.jcp.xml.dsig.internal.dom.DOMExcC14NMethod",
188                    new String[] {"EXCLUSIVE_WITH_COMMENTS"}, MECH_TYPE));
189
190                // Base64 Transform
191                putService(new ProviderService(p, "TransformService",
192                    Transform.BASE64,
193                    "org.jcp.xml.dsig.internal.dom.DOMBase64Transform",
194                    new String[] {"BASE64"}, MECH_TYPE));
195
196                // Enveloped Transform
197                putService(new ProviderService(p, "TransformService",
198                    Transform.ENVELOPED,
199                    "org.jcp.xml.dsig.internal.dom.DOMEnvelopedTransform",
200                    new String[] {"ENVELOPED"}, MECH_TYPE));
201
202                // XPath2 Transform
203                putService(new ProviderService(p, "TransformService",
204                    Transform.XPATH2,
205                    "org.jcp.xml.dsig.internal.dom.DOMXPathFilter2Transform",
206                    new String[] {"XPATH2"}, MECH_TYPE));
207
208                // XPath Transform
209                putService(new ProviderService(p, "TransformService",
210                    Transform.XPATH,
211                    "org.jcp.xml.dsig.internal.dom.DOMXPathTransform",
212                    new String[] {"XPATH"}, MECH_TYPE));
213
214                // XSLT Transform
215                putService(new ProviderService(p, "TransformService",
216                    Transform.XSLT,
217                    "org.jcp.xml.dsig.internal.dom.DOMXSLTTransform",
218                    new String[] {"XSLT"}, MECH_TYPE));
219                return null;
220            }
221        });
222    }
223}
224