src/java.base/share/classes/sun/security/util/SignatureUtil.java
author valeriep
Mon, 21 May 2018 23:40:52 +0000
changeset 50204 3195a713e24d
child 53069 ba1a557b6ccd
child 56592 b1902b22005e
permissions -rw-r--r--
8146293: Add support for RSASSA-PSS Signature algorithm Summary: add RSASSA-PSS key and signature support to SunRsaSign provider Reviewed-by: wetmore
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.util.ObjectIdentifier;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    32
import sun.security.x509.AlgorithmId;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    33
import sun.security.rsa.RSAUtil;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    34
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    35
/**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    36
 * Utility class for Signature related operations. Currently used by various
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    37
 * internal PKI classes such as sun.security.x509.X509CertImpl,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    38
 * sun.security.pkcs.SignerInfo, for setting signature parameters.
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
 * @since   11
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
public class SignatureUtil {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    43
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    44
    // Utility method of creating an AlgorithmParameters object with
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    45
    // the specified algorithm name and encoding
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    46
    private static AlgorithmParameters createAlgorithmParameters(String algName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    47
            byte[] paramBytes) throws ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    48
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    49
        try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    50
            AlgorithmParameters result =
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    51
                AlgorithmParameters.getInstance(algName);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    52
            result.init(paramBytes);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    53
            return result;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    54
        } catch (NoSuchAlgorithmException | IOException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    55
            throw new ProviderException(e);
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
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    58
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    59
    private static AlgorithmParameterSpec getParamSpec(String sigName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    60
            AlgorithmParameters params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    61
            throws InvalidAlgorithmParameterException, ProviderException {
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 (params == null) return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    64
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    65
        if (sigName.toUpperCase().indexOf("RSA") == -1) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    66
            throw new ProviderException
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    67
                 ("Unrecognized algorithm for signature parameters " +
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    68
                  sigName);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    69
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    70
        // AlgorithmParameters.getAlgorithm() may returns oid if it's
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    71
        // created during DER decoding. Convert to use the standard name
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    72
        // before passing it to RSAUtil
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    73
        String alg = params.getAlgorithm();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    74
        if (alg.equalsIgnoreCase(sigName) || alg.indexOf(".") != -1) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    75
            try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    76
                params = createAlgorithmParameters(sigName,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    77
                    params.getEncoded());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    78
            } catch (IOException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    79
                throw new ProviderException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    80
            }
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
        return RSAUtil.getParamSpec(params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    83
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    84
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    85
    // Special method for setting the specified parameter bytes into the
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    86
    // specified Signature object as signature parameters.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    87
    public static void specialSetParameter(Signature sig, byte[] paramBytes)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    88
            throws InvalidAlgorithmParameterException, ProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    89
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    90
        AlgorithmParameters params = null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    91
        if (paramBytes != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    92
            String sigName = sig.getAlgorithm();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    93
            params = createAlgorithmParameters(sigName, paramBytes);
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
        specialSetParameter(sig, params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    96
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    97
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    98
    // Special method for setting the specified AlgorithmParameter object
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    99
    // into the specified Signature object as signature parameters.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   100
    public static void specialSetParameter(Signature sig,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   101
            AlgorithmParameters params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   102
            throws InvalidAlgorithmParameterException, ProviderException {
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
        String sigName = sig.getAlgorithm();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   105
        if (params != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   106
            sig.setParameter(getParamSpec(sigName, params));
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   107
        } else {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   108
            try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   109
                sig.setParameter(null);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   110
            } catch (UnsupportedOperationException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   111
                // ignore for maintaining backward compatibility
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   112
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   113
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   114
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   115
}