src/java.base/share/classes/sun/security/util/SignatureUtil.java
author valeriep
Wed, 19 Dec 2018 02:27:44 +0000
changeset 53069 ba1a557b6ccd
parent 50204 3195a713e24d
child 54483 ac20c3bdc55d
permissions -rw-r--r--
8214096: sun.security.util.SignatureUtil passes null parameter, so JCE validation fails Summary: Changed SignatureUtil.specialSetParameter to ignore null signature parameters Reviewed-by: mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     1
/*
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     4
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    10
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    15
 * accompanied this code).
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    16
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    20
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    23
 * questions.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    24
 */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    25
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    26
package sun.security.util;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    27
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    28
import java.io.IOException;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    29
import java.security.*;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    30
import java.security.spec.*;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    31
import sun.security.rsa.RSAUtil;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    32
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    33
/**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    34
 * Utility class for Signature related operations. Currently used by various
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    35
 * internal PKI classes such as sun.security.x509.X509CertImpl,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    36
 * sun.security.pkcs.SignerInfo, for setting signature parameters.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    37
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    38
 * @since   11
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    39
 */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    40
public class SignatureUtil {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    41
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    42
    // Utility method of creating an AlgorithmParameters object with
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    43
    // the specified algorithm name and encoding
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    44
    private static AlgorithmParameters createAlgorithmParameters(String algName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    45
            byte[] paramBytes) throws ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    46
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    47
        try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    48
            AlgorithmParameters result =
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    49
                AlgorithmParameters.getInstance(algName);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    50
            result.init(paramBytes);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    51
            return result;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    52
        } catch (NoSuchAlgorithmException | IOException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    53
            throw new ProviderException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    54
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    55
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    56
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    57
    private static AlgorithmParameterSpec getParamSpec(String sigName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    58
            AlgorithmParameters params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    59
            throws InvalidAlgorithmParameterException, ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    60
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    61
        if (params == null) return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    62
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    63
        if (sigName.toUpperCase().indexOf("RSA") == -1) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    64
            throw new ProviderException
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    65
                 ("Unrecognized algorithm for signature parameters " +
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    66
                  sigName);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    67
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    68
        // AlgorithmParameters.getAlgorithm() may returns oid if it's
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    69
        // created during DER decoding. Convert to use the standard name
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    70
        // before passing it to RSAUtil
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    71
        String alg = params.getAlgorithm();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    72
        if (alg.equalsIgnoreCase(sigName) || alg.indexOf(".") != -1) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    73
            try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    74
                params = createAlgorithmParameters(sigName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    75
                    params.getEncoded());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    76
            } catch (IOException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    77
                throw new ProviderException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    78
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    79
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    80
        return RSAUtil.getParamSpec(params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    81
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    82
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    83
    // Special method for setting the specified parameter bytes into the
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    84
    // specified Signature object as signature parameters.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    85
    public static void specialSetParameter(Signature sig, byte[] paramBytes)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    86
            throws InvalidAlgorithmParameterException, ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    87
        if (paramBytes != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    88
            String sigName = sig.getAlgorithm();
53069
ba1a557b6ccd 8214096: sun.security.util.SignatureUtil passes null parameter, so JCE validation fails
valeriep
parents: 50204
diff changeset
    89
            AlgorithmParameters params =
ba1a557b6ccd 8214096: sun.security.util.SignatureUtil passes null parameter, so JCE validation fails
valeriep
parents: 50204
diff changeset
    90
                createAlgorithmParameters(sigName, paramBytes);
ba1a557b6ccd 8214096: sun.security.util.SignatureUtil passes null parameter, so JCE validation fails
valeriep
parents: 50204
diff changeset
    91
            specialSetParameter(sig, params);
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    92
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    93
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    94
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    95
    // Special method for setting the specified AlgorithmParameter object
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    96
    // into the specified Signature object as signature parameters.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    97
    public static void specialSetParameter(Signature sig,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    98
            AlgorithmParameters params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    99
            throws InvalidAlgorithmParameterException, ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   100
        if (params != null) {
53069
ba1a557b6ccd 8214096: sun.security.util.SignatureUtil passes null parameter, so JCE validation fails
valeriep
parents: 50204
diff changeset
   101
            String sigName = sig.getAlgorithm();
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   102
            sig.setParameter(getParamSpec(sigName, params));
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   103
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   104
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   105
}