jdk/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/JavaUtils.java
author mullan
Wed, 11 Jun 2014 16:25:59 -0400
changeset 27081 1ceee8d3844d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8038913: Bolster XML support Reviewed-by: xuelei, skoivu
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
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    12
 * with the License. You may obtain a copy of the License at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
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.utils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    31
import java.security.SecurityPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A collection of different, general-purpose methods for JAVA-specific things
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Christian Geuer-Pollmann
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class JavaUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    39
    /** {@link org.apache.commons.logging} logging facility */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    40
    private static java.util.logging.Logger log =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        java.util.logging.Logger.getLogger(JavaUtils.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    43
    private static final SecurityPermission REGISTER_PERMISSION =
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    44
        new SecurityPermission(
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    45
            "com.sun.org.apache.xml.internal.security.register");
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    46
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    47
    private JavaUtils() {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    48
        // we don't allow instantiation
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    49
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    51
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    52
     * Method getBytesFromFile
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    53
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    54
     * @param fileName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    55
     * @return the bytes read from the file
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    56
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    57
     * @throws FileNotFoundException
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    58
     * @throws IOException
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    59
     */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    60
    public static byte[] getBytesFromFile(String fileName)
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    61
        throws FileNotFoundException, IOException {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    62
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    63
        byte refBytes[] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    65
        FileInputStream fisRef = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    66
        UnsyncByteArrayOutputStream baos = null;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    67
        try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    68
            fisRef = new FileInputStream(fileName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    69
            baos = new UnsyncByteArrayOutputStream();
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    70
            byte buf[] = new byte[1024];
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    71
            int len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    73
            while ((len = fisRef.read(buf)) > 0) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    74
                baos.write(buf, 0, len);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    75
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    77
            refBytes = baos.toByteArray();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    78
        } finally {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    79
            if (baos != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    80
                baos.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    81
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    82
            if (fisRef != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    83
                fisRef.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
    84
            }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    85
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    87
        return refBytes;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    88
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    90
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    91
     * Method writeBytesToFilename
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    92
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    93
     * @param filename
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    94
     * @param bytes
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    95
     */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    96
    public static void writeBytesToFilename(String filename, byte[] bytes) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    97
        FileOutputStream fos = null;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    98
        try {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    99
            if (filename != null && bytes != null) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   100
                File f = new File(filename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   102
                fos = new FileOutputStream(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   104
                fos.write(bytes);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   105
                fos.close();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   106
            } else {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   107
                if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   108
                    log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   109
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   110
            }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   111
        } catch (IOException ex) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   112
            if (fos != null) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   113
                try {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   114
                    fos.close();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   115
                } catch (IOException ioe) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   116
                    if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   117
                        log.log(java.util.logging.Level.FINE, ioe.getMessage(), ioe);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   118
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   119
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   120
            }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   121
        }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   124
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   125
     * This method reads all bytes from the given InputStream till EOF and
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   126
     * returns them as a byte array.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   127
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   128
     * @param inputStream
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   129
     * @return the bytes read from the stream
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   130
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   131
     * @throws FileNotFoundException
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   132
     * @throws IOException
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   133
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   134
    public static byte[] getBytesFromStream(InputStream inputStream) throws IOException {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   135
        UnsyncByteArrayOutputStream baos = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   137
        byte[] retBytes = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   138
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   139
            baos = new UnsyncByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   140
            byte buf[] = new byte[4 * 1024];
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   141
            int len;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   142
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   143
            while ((len = inputStream.read(buf)) > 0) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   144
                baos.write(buf, 0, len);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   145
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   146
            retBytes = baos.toByteArray();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   147
        } finally {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   148
            baos.close();
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   149
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 1337
diff changeset
   151
        return retBytes;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   152
    }
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   153
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   154
    /**
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   155
     * Throws a {@code SecurityException} if a security manager is installed
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   156
     * and the caller is not allowed to register an implementation of an
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   157
     * algorithm, transform, or other security sensitive XML Signature function.
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   158
     *
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   159
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   160
     *    caller has not been granted the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   161
     *    {@literal "com.sun.org.apache.xml.internal.security.register"}
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   162
     *    {@code SecurityPermission}
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   163
     */
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   164
    public static void checkRegisterPermission() {
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   165
        SecurityManager sm = System.getSecurityManager();
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   166
        if (sm != null) {
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   167
            sm.checkPermission(REGISTER_PERMISSION);
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   168
        }
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   169
    }
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   170
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   171
    /**
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   172
     * Converts an ASN.1 DSA value to a XML Signature DSA Value.
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   173
     *
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   174
     * The JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   175
     * pairs (see section 2.2.2 of RFC 3279); the XML Signature requires the
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   176
     * core BigInteger values.
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   177
     *
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   178
     * @param asn1Bytes the ASN.1 encoded bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   179
     * @param size size of r and s in bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   180
     * @return the XML Signature encoded bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   181
     * @throws IOException if the bytes are not encoded correctly
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   182
     * @see <A HREF="http://www.w3.org/TR/xmldsig-core1/#sec-DSA">6.4.1 DSA</A>
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   183
     */
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   184
    public static byte[] convertDsaASN1toXMLDSIG(byte[] asn1Bytes, int size)
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   185
        throws IOException
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   186
    {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   187
        if (asn1Bytes[0] != 48 || asn1Bytes[1] != asn1Bytes.length - 2
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   188
            || asn1Bytes[2] != 2) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   189
            throw new IOException("Invalid ASN.1 format of DSA signature");
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   190
        }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   191
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   192
        byte rLength = asn1Bytes[3];
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   193
        int i;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   194
        for (i = rLength; i > 0 && asn1Bytes[4 + rLength - i] == 0; i--);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   195
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   196
        byte sLength = asn1Bytes[5 + rLength];
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   197
        int j;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   198
        for (j = sLength;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   199
             j > 0 && asn1Bytes[6 + rLength + sLength - j] == 0; j--);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   200
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   201
        if (i > size || asn1Bytes[4 + rLength] != 2 || j > size) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   202
            throw new IOException("Invalid ASN.1 format of DSA signature");
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   203
        } else {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   204
            byte[] xmldsigBytes = new byte[size * 2];
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   205
            System.arraycopy(asn1Bytes, 4 + rLength - i, xmldsigBytes,
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   206
                             size - i, i);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   207
            System.arraycopy(asn1Bytes, 6 + rLength + sLength - j,
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   208
                             xmldsigBytes, size * 2 - j, j);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   209
            return xmldsigBytes;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   210
        }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   211
    }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   212
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   213
    /**
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   214
     * Converts an XML Signature DSA Value to a ASN.1 DSA value.
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   215
     *
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   216
     * The JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   217
     * pairs (see section 2.2.2 of RFC 3279); the XML Signature requires the
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   218
     * core BigInteger values.
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   219
     *
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   220
     * @param xmldsigBytes the XML Signature encoded bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   221
     * @param size size of r and s in bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   222
     * @return the ASN.1 encoded bytes
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   223
     * @throws IOException if the bytes are not encoded correctly
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   224
     * @see <A HREF="http://www.w3.org/TR/xmldsig-core1/#sec-DSA">6.4.1 DSA</A>
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   225
     */
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   226
    public static byte[] convertDsaXMLDSIGtoASN1(byte[] xmldsigBytes, int size)
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   227
        throws IOException
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   228
    {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   229
        int totalSize = size * 2;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   230
        if (xmldsigBytes.length != totalSize) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   231
            throw new IOException("Invalid XMLDSIG format of DSA signature");
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   232
        }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   233
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   234
        int i;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   235
        for (i = size; i > 0 && xmldsigBytes[size - i] == 0; i--);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   236
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   237
        int j = i;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   238
        if (xmldsigBytes[size - i] < 0) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   239
            j++;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   240
        }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   241
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   242
        int k;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   243
        for (k = size; k > 0 && xmldsigBytes[totalSize - k] == 0; k--);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   244
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   245
        int l = k;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   246
        if (xmldsigBytes[totalSize - k] < 0) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   247
            l++;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   248
        }
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   249
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   250
        byte[] asn1Bytes = new byte[6 + j + l];
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   251
        asn1Bytes[0] = 48;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   252
        asn1Bytes[1] = (byte)(4 + j + l);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   253
        asn1Bytes[2] = 2;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   254
        asn1Bytes[3] = (byte)j;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   255
        System.arraycopy(xmldsigBytes, size - i, asn1Bytes, 4 + j - i, i);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   256
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   257
        asn1Bytes[4 + j] = 2;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   258
        asn1Bytes[5 + j] = (byte) l;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   259
        System.arraycopy(xmldsigBytes, totalSize - k, asn1Bytes,
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   260
                         6 + j + l - k, k);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   261
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   262
        return asn1Bytes;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 18780
diff changeset
   263
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}