Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/CancelMultipart.java
41152 views
/*1* Copyright (c) 2021, Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 825883326* @library /test/lib ..27* @modules jdk.crypto.cryptoki/sun.security.pkcs11:open28* @run main/othervm CancelMultipart29*/3031import java.lang.reflect.Field;32import java.nio.ByteBuffer;33import java.security.Key;34import java.security.Provider;35import java.security.ProviderException;36import javax.crypto.Cipher;37import javax.crypto.IllegalBlockSizeException;38import javax.crypto.spec.SecretKeySpec;3940public class CancelMultipart extends PKCS11Test {4142private static Provider provider;43private static Key key;4445static {46key = new SecretKeySpec(new byte[16], "AES");47}4849private static class SessionLeaker {50private LeakOperation op;51private LeakInputType type;5253SessionLeaker(LeakOperation op, LeakInputType type) {54this.op = op;55this.type = type;56}5758private void leakAndTry() throws Exception {59Cipher cipher = op.getCipher();60try {61type.doOperation(cipher,62(op instanceof LeakDecrypt ?63LeakInputType.DECRYPT_MODE :64null));65throw new Exception("PKCS11Exception expected, invalid block"66+ "size");67} catch (ProviderException | IllegalBlockSizeException e) {68// Exception expected - session returned to the SessionManager69// should be cancelled. That's what will be tested now.70}7172tryCipherInit();73}74}7576private static interface LeakOperation {77Cipher getCipher() throws Exception;78}7980private static interface LeakInputType {81static int DECRYPT_MODE = 1;82void doOperation(Cipher cipher, int mode) throws Exception;83}8485private static class LeakDecrypt implements LeakOperation {86public Cipher getCipher() throws Exception {87Cipher cipher = Cipher.getInstance(88"AES/ECB/PKCS5Padding", provider);89cipher.init(Cipher.DECRYPT_MODE, key);90return cipher;91}92}9394private static class LeakByteBuffer implements LeakInputType {95public void doOperation(Cipher cipher, int mode) throws Exception {96if (mode == DECRYPT_MODE) {97cipher.update(ByteBuffer.allocate(1), ByteBuffer.allocate(1));98cipher.doFinal(ByteBuffer.allocate(0), ByteBuffer.allocate(1));99}100}101}102103private static class LeakByteArray implements LeakInputType {104public void doOperation(Cipher cipher, int mode) throws Exception {105if (mode == DECRYPT_MODE) {106cipher.update(new byte[1]);107cipher.doFinal(new byte[1], 0, 0);108}109}110}111112public static void main(String[] args) throws Exception {113main(new CancelMultipart(), args);114}115116@Override117public void main(Provider p) throws Exception {118init(p);119120// Try multiple paths:121122executeTest(new SessionLeaker(new LeakDecrypt(), new LeakByteArray()),123"P11Cipher::implDoFinal(byte[], int, int)");124125executeTest(new SessionLeaker(new LeakDecrypt(), new LeakByteBuffer()),126"P11Cipher::implDoFinal(ByteBuffer)");127128System.out.println("TEST PASS - OK");129}130131private static void executeTest(SessionLeaker sl, String testName)132throws Exception {133try {134sl.leakAndTry();135System.out.println(testName + ": OK");136} catch (Exception e) {137System.out.println(testName + ": FAILED");138throw e;139}140}141142private static void init(Provider p) throws Exception {143provider = p;144145// The max number of sessions is 2 because, in addition to the146// operation (i.e. PKCS11::getNativeKeyInfo), a session to hold147// the P11Key object is needed.148setMaxSessions(2);149}150151/*152* This method is intended to generate pression on the number of sessions153* to be used from the NSS Software Token, so sessions with (potentially)154* active operations are reused.155*/156private static void setMaxSessions(int maxSessions) throws Exception {157Field tokenField = Class.forName("sun.security.pkcs11.SunPKCS11")158.getDeclaredField("token");159tokenField.setAccessible(true);160Field sessionManagerField = Class.forName("sun.security.pkcs11.Token")161.getDeclaredField("sessionManager");162sessionManagerField.setAccessible(true);163Field maxSessionsField = Class.forName("sun.security.pkcs11.SessionManager")164.getDeclaredField("maxSessions");165maxSessionsField.setAccessible(true);166Object sessionManagerObj = sessionManagerField.get(167tokenField.get(provider));168maxSessionsField.setInt(sessionManagerObj, maxSessions);169}170171private static void tryCipherInit() throws Exception {172Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", provider);173174// A CKR_OPERATION_ACTIVE error may be thrown if a session was175// returned to the Session Manager with an active operation, and176// we try to initialize the Cipher using it.177//178// Given that the maximum number of sessions was forced to 2, we know179// that the session to be used here was already used in a previous180// (failed) operation. Thus, the test asserts that the operation was181// properly cancelled.182cipher.init(Cipher.ENCRYPT_MODE, key);183184// If initialization passes, finish gracefully so other paths can185// be tested under the current maximum number of sessions.186cipher.doFinal(new byte[16], 0, 0);187}188}189190191