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