1 /*
   2  * Copyright (c) 2018, 2019, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8214096 8216039
  27  * @summary Make sure SignatureUtil works with null algorithm parameters
  28  * @modules java.base/sun.security.util
  29  */
  30 import java.security.*;
  31 import java.security.spec.AlgorithmParameterSpec;
  32 import sun.security.util.SignatureUtil;
  33 
  34 public class SetNullSigParams {
  35 
  36     public static void main(String[] args) throws Exception {
  37         Signature sig = new SpecialSigImpl();
  38         SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
  39         SignatureUtil.initSignWithParam(sig, null, null, null);
  40     }
  41 
  42     // Sample Signature impl class which simulates 3rd party provider behavior
  43     // and throws NPE when given null algorithm parameters
  44     // For max backward-compatibility, sun.security.util.SignatureUtil class
  45     // now calls setParameter() only when algorithm parameters is non-null
  46     private static class SpecialSigImpl extends Signature {
  47         SpecialSigImpl() {
  48             super("ANY");
  49         }
  50         @Override
  51         protected void engineInitVerify(PublicKey publicKey)
  52                 throws InvalidKeyException {}
  53         @Override
  54         protected void engineInitSign(PrivateKey privateKey)
  55                 throws InvalidKeyException {}
  56         @Override
  57         protected void engineUpdate(byte b) throws SignatureException {}
  58         @Override
  59         protected void engineUpdate(byte[] b, int off, int len)
  60                 throws SignatureException {}
  61         @Override
  62         protected byte[] engineSign() throws SignatureException { return null; }
  63         @Override
  64         protected boolean engineVerify(byte[] sigBytes)
  65                 throws SignatureException { return false; }
  66         @Override
  67         protected void engineSetParameter(String param, Object value)
  68                 throws InvalidParameterException {}
  69         @Override
  70         protected void engineSetParameter(AlgorithmParameterSpec params)
  71                 throws InvalidAlgorithmParameterException {
  72             if (params == null) throw new NullPointerException("Test Failed");
  73         }
  74         @Override
  75         protected Object engineGetParameter(String param)
  76                 throws InvalidParameterException { return null; }
  77     }
  78 }