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.rsa.RSAUtil;
  32 
  33 /**
  34  * Utility class for Signature related operations. Currently used by various
  35  * internal PKI classes such as sun.security.x509.X509CertImpl,
  36  * sun.security.pkcs.SignerInfo, for setting signature parameters.
  37  *
  38  * @since   11
  39  */
  40 public class SignatureUtil {
  41 
  42     // Utility method of creating an AlgorithmParameters object with
  43     // the specified algorithm name and encoding
  44     private static AlgorithmParameters createAlgorithmParameters(String algName,
  45             byte[] paramBytes) throws ProviderException {
  46 
  47         try {
  48             AlgorithmParameters result =
  49                 AlgorithmParameters.getInstance(algName);
  50             result.init(paramBytes);
  51             return result;
  52         } catch (NoSuchAlgorithmException | IOException e) {
  53             throw new ProviderException(e);
  54         }
  55     }
  56 
  57     private static AlgorithmParameterSpec getParamSpec(String sigName,
  58             AlgorithmParameters params)
  59             throws InvalidAlgorithmParameterException, ProviderException {
  60 
  61         if (params == null) return null;
  62 
  63         if (sigName.toUpperCase().indexOf("RSA") == -1) {
  64             throw new ProviderException
  65                  ("Unrecognized algorithm for signature parameters " +
  66                   sigName);
  67         }
  68         // AlgorithmParameters.getAlgorithm() may returns oid if it's
  69         // created during DER decoding. Convert to use the standard name
  70         // before passing it to RSAUtil
  71         String alg = params.getAlgorithm();
  72         if (alg.equalsIgnoreCase(sigName) || alg.indexOf(".") != -1) {
  73             try {
  74                 params = createAlgorithmParameters(sigName,
  75                     params.getEncoded());
  76             } catch (IOException e) {
  77                 throw new ProviderException(e);
  78             }
  79         }
  80         return RSAUtil.getParamSpec(params);
  81     }
  82 
  83     // Special method for setting the specified parameter bytes into the
  84     // specified Signature object as signature parameters.
  85     public static void specialSetParameter(Signature sig, byte[] paramBytes)
  86             throws InvalidAlgorithmParameterException, ProviderException {
  87         if (paramBytes != null) {
  88             String sigName = sig.getAlgorithm();
  89             AlgorithmParameters params =
  90                 createAlgorithmParameters(sigName, paramBytes);
  91             specialSetParameter(sig, params);
  92         }
  93     }
  94 
  95     // Special method for setting the specified AlgorithmParameter object
  96     // into the specified Signature object as signature parameters.
  97     public static void specialSetParameter(Signature sig,
  98             AlgorithmParameters params)
  99             throws InvalidAlgorithmParameterException, ProviderException {
 100         if (params != null) {
 101             String sigName = sig.getAlgorithm();
 102             sig.setParameter(getParamSpec(sigName, params));
 103         }
 104     }
 105 }