src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/Init.java
author weijun
Thu, 10 Oct 2019 17:36:38 +0300
changeset 59240 b3116877866f
parent 50614 3810c9a2efa1
permissions -rw-r--r--
8231507: Update Apache Santuario (XML Signature) to version 2.1.4 Reviewed-by: weijun Contributed-by: fedor.burdun@azulsystems.com, weijun.wang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
     5
/**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
     8
 * distributed with this work for additional information
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    11
 * "License"); you may not use this file except in compliance
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    12
 * with the License. You may obtain a copy of the License at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    17
 * software distributed under the License is distributed on an
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    19
 * KIND, either express or implied. See the License for the
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    20
 * specific language governing permissions and limitations
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    21
 * under the License.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.PrivilegedAction;
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    28
import java.security.PrivilegedActionException;
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    29
import java.security.PrivilegedExceptionAction;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    30
import java.util.ArrayList;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    31
import java.util.List;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    36
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.security.transforms.Transform;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    39
import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.security.utils.I18n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class does the configuration of the library. This includes creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the mapping of Canonicalization and Transform algorithms. Initialization is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * done by calling {@link Init#init} which should be done in any static block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of the files of this library. We ensure that this call is only executed once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    55
public class Init {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    57
    /** The namespace for CONF file **/
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    58
    public static final String CONF_NS = "http://www.xmlsecurity.org/NS/#configuration";
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    59
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    60
    private static final com.sun.org.slf4j.internal.Logger LOG =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    61
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(Init.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    63
    /** Field alreadyInitialized */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    64
    private static boolean alreadyInitialized = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    66
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    67
     * Method isInitialized
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    68
     * @return true if the library is already initialized.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    69
     */
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    70
    public static final synchronized boolean isInitialized() {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    71
        return Init.alreadyInitialized;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    72
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    73
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    74
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    75
     * Method init
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    76
     *
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    77
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    78
    public static synchronized void init() {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    79
        if (alreadyInitialized) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    80
            return;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    81
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    83
        InputStream is =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    84
            AccessController.doPrivileged(
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    85
                (PrivilegedAction<InputStream>)
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    86
                    () -> {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    87
                        String cfile =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    88
                            System.getProperty("com.sun.org.apache.xml.internal.security.resource.config");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    89
                        if (cfile == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    90
                            return null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    91
                        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    92
                        return Init.class.getResourceAsStream(cfile);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    93
                    }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    94
                );
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    95
        if (is == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    96
            dynamicInit();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    97
        } else {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    98
            fileInit(is);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
    99
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   101
        alreadyInitialized = true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   102
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   103
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   104
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   105
     * Dynamically initialise the library by registering the default algorithms/implementations
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   106
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   107
    private static void dynamicInit() {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   108
        //
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   109
        // Load the Resource Bundle - the default is the English resource bundle.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   110
        // To load another resource bundle, call I18n.init(...) before calling this
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   111
        // method.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   112
        //
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   113
        I18n.init("en", "US");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   115
        LOG.debug("Registering default algorithms");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   116
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   117
        try {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   118
            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>(){
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   119
                @Override public Void run() throws XMLSecurityException {
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   120
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   121
                    // Bind the default prefixes
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   122
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   123
                    ElementProxy.registerDefaultPrefixes();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   124
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   125
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   126
                    // Set the default Transforms
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   127
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   128
                    Transform.registerDefaultAlgorithms();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   129
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   130
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   131
                    // Set the default signature algorithms
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   132
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   133
                    SignatureAlgorithm.registerDefaultAlgorithms();
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   134
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   135
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   136
                    // Set the default JCE algorithms
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   137
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   138
                    JCEMapper.registerDefaultAlgorithms();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   139
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   140
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   141
                    // Set the default c14n algorithms
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   142
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   143
                    Canonicalizer.registerDefaultAlgorithms();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   145
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   146
                    // Register the default resolvers
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   147
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   148
                    ResourceResolver.registerDefaultResolvers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   150
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   151
                    // Register the default key resolvers
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   152
                    //
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   153
                    KeyResolver.registerDefaultResolvers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   155
                    return null;
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   156
                }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   157
            });
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   158
        } catch (PrivilegedActionException ex) {
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   159
            XMLSecurityException xse = (XMLSecurityException)ex.getException();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   160
            LOG.error(xse.getMessage(), xse);
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   161
            xse.printStackTrace();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   162
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   163
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   165
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   166
     * Initialise the library from a configuration file
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   167
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   168
    private static void fileInit(InputStream is) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   169
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /* read library configuration file */
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 50614
diff changeset
   171
            Document doc = XMLUtils.read(is, false);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   172
            Node config = doc.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   173
            for (; config != null; config = config.getNextSibling()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if ("Configuration".equals(config.getLocalName())) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   175
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   178
            if (config == null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   179
                LOG.error("Error in reading configuration file - Configuration element not found");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   180
                return;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   181
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   182
            for (Node el = config.getFirstChild(); el != null; el = el.getNextSibling()) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   183
                if (Node.ELEMENT_NODE != el.getNodeType()) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   184
                    continue;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   185
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   186
                String tag = el.getLocalName();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   187
                if ("ResourceBundles".equals(tag)) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   188
                    Element resource = (Element)el;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   189
                    /* configure internationalization */
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   190
                    Attr langAttr = resource.getAttributeNodeNS(null, "defaultLanguageCode");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   191
                    Attr countryAttr = resource.getAttributeNodeNS(null, "defaultCountryCode");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   192
                    String languageCode =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   193
                        (langAttr == null) ? null : langAttr.getNodeValue();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   194
                    String countryCode =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   195
                        (countryAttr == null) ? null : countryAttr.getNodeValue();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   196
                    I18n.init(languageCode, countryCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   198
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   199
                if ("CanonicalizationMethods".equals(tag)) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   200
                    Element[] list =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   201
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "CanonicalizationMethod");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   202
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   203
                    for (Element element : list) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   204
                        String uri = element.getAttributeNS(null, "URI");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   205
                        String javaClass =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   206
                            element.getAttributeNS(null, "JAVACLASS");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   207
                        try {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   208
                            Canonicalizer.register(uri, javaClass);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   209
                            LOG.debug("Canonicalizer.register({}, {})", uri, javaClass);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   210
                        } catch (ClassNotFoundException e) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   211
                            Object exArgs[] = { uri, javaClass };
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   212
                            LOG.error(I18n.translate("algorithm.classDoesNotExist", exArgs));
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   213
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   214
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   215
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   216
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   217
                if ("TransformAlgorithms".equals(tag)) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   218
                    Element[] tranElem =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   219
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "TransformAlgorithm");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   221
                    for (Element element : tranElem) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   222
                        String uri = element.getAttributeNS(null, "URI");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   223
                        String javaClass =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   224
                            element.getAttributeNS(null, "JAVACLASS");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   225
                        try {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   226
                            Transform.register(uri, javaClass);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   227
                            LOG.debug("Transform.register({}, {})", uri, javaClass);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   228
                        } catch (ClassNotFoundException e) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   229
                            Object exArgs[] = { uri, javaClass };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   231
                            LOG.error(I18n.translate("algorithm.classDoesNotExist", exArgs));
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   232
                        } catch (NoClassDefFoundError ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   233
                            LOG.warn("Not able to found dependencies for algorithm, I'll keep working.");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   234
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   235
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   236
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   238
                if ("JCEAlgorithmMappings".equals(tag)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   239
                    Node algorithmsNode = ((Element)el).getElementsByTagName("Algorithms").item(0);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   240
                    if (algorithmsNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   241
                        Element[] algorithms =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   242
                            XMLUtils.selectNodes(algorithmsNode.getFirstChild(), CONF_NS, "Algorithm");
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   243
                        for (Element element : algorithms) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   244
                            String id = element.getAttributeNS(null, "URI");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   245
                            JCEMapper.register(id, new JCEMapper.Algorithm(element));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   247
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   248
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   249
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   250
                if ("SignatureAlgorithms".equals(tag)) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   251
                    Element[] sigElems =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   252
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "SignatureAlgorithm");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   253
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   254
                    for (Element sigElem : sigElems) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   255
                        String uri = sigElem.getAttributeNS(null, "URI");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   256
                        String javaClass =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   257
                            sigElem.getAttributeNS(null, "JAVACLASS");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   258
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   259
                        /** $todo$ handle registering */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   261
                        try {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   262
                            SignatureAlgorithm.register(uri, javaClass);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   263
                            LOG.debug("SignatureAlgorithm.register({}, {})", uri, javaClass);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   264
                        } catch (ClassNotFoundException e) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   265
                            Object exArgs[] = { uri, javaClass };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   267
                            LOG.error(I18n.translate("algorithm.classDoesNotExist", exArgs));
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   268
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   269
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   270
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   272
                if ("ResourceResolvers".equals(tag)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   273
                    Element[] resolverElem =
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   274
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "Resolver");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   275
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   276
                    for (Element element : resolverElem) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   277
                        String javaClass =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   278
                            element.getAttributeNS(null, "JAVACLASS");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   279
                        String description =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   280
                            element.getAttributeNS(null, "DESCRIPTION");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   282
                        if (description != null && description.length() > 0) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   283
                            LOG.debug("Register Resolver: {}: {}", javaClass, description);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   284
                        } else {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   285
                            LOG.debug("Register Resolver: {}: For unknown purposes", javaClass);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   286
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   287
                        try {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   288
                            ResourceResolver.register(javaClass);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   289
                        } catch (Throwable e) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   290
                            LOG.warn(
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   291
                                 "Cannot register:" + javaClass
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   292
                                 + " perhaps some needed jars are not installed",
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   293
                                 e
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   294
                             );
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   295
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   296
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   297
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   299
                if ("KeyResolver".equals(tag)){
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   300
                    Element[] resolverElem =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   301
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "Resolver");
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   302
                    List<String> classNames = new ArrayList<>(resolverElem.length);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   303
                    for (Element element : resolverElem) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   304
                        String javaClass =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   305
                            element.getAttributeNS(null, "JAVACLASS");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   306
                        String description =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   307
                            element.getAttributeNS(null, "DESCRIPTION");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   309
                        if (description != null && description.length() > 0) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   310
                            LOG.debug("Register Resolver: {}: {}", javaClass, description);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   311
                        } else {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   312
                            LOG.debug("Register Resolver: {}: For unknown purposes", javaClass);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   313
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   314
                        classNames.add(javaClass);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   315
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   316
                    KeyResolver.registerClassNames(classNames);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   317
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   320
                if ("PrefixMappings".equals(tag)){
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   321
                    LOG.debug("Now I try to bind prefixes:");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   323
                    Element[] nl =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   324
                        XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "PrefixMapping");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   326
                    for (Element element : nl) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   327
                        String namespace = element.getAttributeNS(null, "namespace");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   328
                        String prefix = element.getAttributeNS(null, "prefix");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   329
                        LOG.debug("Now I try to bind {} to {}", prefix, namespace);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   330
                        ElementProxy.setDefaultPrefix(namespace, prefix);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   331
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   332
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   334
        } catch (Exception e) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   335
            LOG.error("Bad: ", e);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   336
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   337
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 13791
diff changeset
   340