Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11Exception.java
41161 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2*/34/* Copyright (c) 2002 Graz University of Technology. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15*16* 3. The end-user documentation included with the redistribution, if any, must17* include the following acknowledgment:18*19* "This product includes software developed by IAIK of Graz University of20* Technology."21*22* Alternately, this acknowledgment may appear in the software itself, if23* and wherever such third-party acknowledgments normally appear.24*25* 4. The names "Graz University of Technology" and "IAIK of Graz University of26* Technology" must not be used to endorse or promote products derived from27* this software without prior written permission.28*29* 5. Products derived from this software may not be called30* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior31* written permission of Graz University of Technology.32*33* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED34* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED35* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR36* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE37* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,38* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,39* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,40* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON41* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,42* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY43* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE44* POSSIBILITY OF SUCH DAMAGE.45*/4647package sun.security.pkcs11.wrapper;4849import java.util.*;50import static sun.security.pkcs11.wrapper.PKCS11Constants.*;5152/**53* This is the superclass of all checked exceptions used by this package. An54* exception of this class indicates that a function call to the underlying55* PKCS#11 module returned a value not equal to CKR_OK. The application can get56* the returned value by calling getErrorCode(). A return value not equal to57* CKR_OK is the only reason for such an exception to be thrown.58* PKCS#11 defines the meaning of an error-code, which may depend on the59* context in which the error occurs.60*61* @author <a href="mailto:[email protected]"> Karl Scheibelhofer </a>62* @invariants63*/64public class PKCS11Exception extends Exception {65private static final long serialVersionUID = 4077027363729192L;6667/**68* The code of the error which was the reason for this exception.69*/70protected long errorCode;7172protected String errorMsg;7374public static final long CKR_GENERAL_ERROR = RV.CKR_GENERAL_ERROR.value;75public static final long CKR_ATTRIBUTE_TYPE_INVALID =76RV.CKR_ATTRIBUTE_TYPE_INVALID.value;77public static final long CKR_DATA_LEN_RANGE = RV.CKR_DATA_LEN_RANGE.value;78public static final long CKR_ENCRYPTED_DATA_INVALID =79RV.CKR_ENCRYPTED_DATA_INVALID.value;80public static final long CKR_ENCRYPTED_DATA_LEN_RANGE =81RV.CKR_ENCRYPTED_DATA_LEN_RANGE.value;82public static final long CKR_MECHANISM_INVALID =83RV.CKR_MECHANISM_INVALID.value;84public static final long CKR_MECHANISM_PARAM_INVALID =85RV.CKR_MECHANISM_PARAM_INVALID.value;86public static final long CKR_OPERATION_NOT_INITIALIZED =87RV.CKR_OPERATION_NOT_INITIALIZED.value;88public static final long CKR_PIN_INCORRECT =89RV.CKR_PIN_INCORRECT.value;90public static final long CKR_SIGNATURE_INVALID =91RV.CKR_SIGNATURE_INVALID.value;92public static final long CKR_SIGNATURE_LEN_RANGE =93RV.CKR_SIGNATURE_LEN_RANGE.value;94public static final long CKR_USER_ALREADY_LOGGED_IN =95RV.CKR_USER_ALREADY_LOGGED_IN.value;96public static final long CKR_USER_NOT_LOGGED_IN =97RV.CKR_USER_NOT_LOGGED_IN.value;98public static final long CKR_BUFFER_TOO_SMALL =99RV.CKR_BUFFER_TOO_SMALL.value;100public static final long CKR_CRYPTOKI_ALREADY_INITIALIZED =101RV.CKR_CRYPTOKI_ALREADY_INITIALIZED.value;102103// enum for all PKCS#11 return value104static enum RV {105CKR_OK(0x00000000L),106CKR_CANCEL(0x00000001L),107CKR_HOST_MEMORY(0x00000002L),108CKR_SLOT_ID_INVALID(0x00000003L),109CKR_GENERAL_ERROR(0x00000005L),110CKR_FUNCTION_FAILED(0x00000006L),111CKR_ARGUMENTS_BAD(0x00000007L),112CKR_NO_EVENT(0x00000008L),113CKR_NEED_TO_CREATE_THREADS(0x00000009L),114CKR_CANT_LOCK(0x0000000AL),115CKR_ATTRIBUTE_READ_ONLY(0x00000010L),116CKR_ATTRIBUTE_SENSITIVE(0x00000011L),117CKR_ATTRIBUTE_TYPE_INVALID(0x00000012L),118CKR_ATTRIBUTE_VALUE_INVALID(0x00000013L),119CKR_ACTION_PROHIBITED(0x0000001BL),120CKR_DATA_INVALID(0x00000020L),121CKR_DATA_LEN_RANGE(0x00000021L),122CKR_DEVICE_ERROR(0x00000030L),123CKR_DEVICE_MEMORY(0x00000031L),124CKR_DEVICE_REMOVED(0x00000032L),125CKR_ENCRYPTED_DATA_INVALID(0x00000040L),126CKR_ENCRYPTED_DATA_LEN_RANGE(0x00000041L),127CKR_AEAD_DECRYPT_FAILED(0x00000042L),128CKR_FUNCTION_CANCELED(0x00000050L),129CKR_FUNCTION_NOT_PARALLEL(0x00000051L),130CKR_FUNCTION_NOT_SUPPORTED(0x00000054L),131CKR_KEY_HANDLE_INVALID(0x00000060L),132CKR_KEY_SIZE_RANGE(0x00000062L),133CKR_KEY_TYPE_INCONSISTENT(0x00000063L),134CKR_KEY_NOT_NEEDED(0x00000064L),135CKR_KEY_CHANGED(0x00000065L),136CKR_KEY_NEEDED(0x00000066L),137CKR_KEY_INDIGESTIBLE(0x00000067L),138CKR_KEY_FUNCTION_NOT_PERMITTED(0x00000068L),139CKR_KEY_NOT_WRAPPABLE(0x00000069L),140CKR_KEY_UNEXTRACTABLE(0x0000006AL),141CKR_MECHANISM_INVALID(0x00000070L),142CKR_MECHANISM_PARAM_INVALID(0x00000071L),143CKR_OBJECT_HANDLE_INVALID(0x00000082L),144CKR_OPERATION_ACTIVE(0x00000090L),145CKR_OPERATION_NOT_INITIALIZED(0x00000091L),146CKR_PIN_INCORRECT(0x000000A0L),147CKR_PIN_INVALID(0x000000A1L),148CKR_PIN_LEN_RANGE(0x000000A2L),149CKR_PIN_EXPIRED(0x000000A3L),150CKR_PIN_LOCKED(0x000000A4L),151CKR_SESSION_CLOSED(0x000000B0L),152CKR_SESSION_COUNT(0x000000B1L),153CKR_SESSION_HANDLE_INVALID(0x000000B3L),154CKR_SESSION_PARALLEL_NOT_SUPPORTED(0x000000B4L),155CKR_SESSION_READ_ONLY(0x000000B5L),156CKR_SESSION_EXISTS(0x000000B6L),157CKR_SESSION_READ_ONLY_EXISTS(0x000000B7L),158CKR_SESSION_READ_WRITE_SO_EXISTS(0x000000B8L),159CKR_SIGNATURE_INVALID(0x000000C0L),160CKR_SIGNATURE_LEN_RANGE(0x000000C1L),161CKR_TEMPLATE_INCOMPLETE(0x000000D0L),162CKR_TEMPLATE_INCONSISTENT(0x000000D1L),163CKR_TOKEN_NOT_PRESENT(0x000000E0L),164CKR_TOKEN_NOT_RECOGNIZED(0x000000E1L),165CKR_TOKEN_WRITE_PROTECTED(0x000000E2L),166CKR_UNWRAPPING_KEY_HANDLE_INVALID(0x000000F0L),167CKR_UNWRAPPING_KEY_SIZE_RANGE(0x000000F1L),168CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT(0x000000F2L),169CKR_USER_ALREADY_LOGGED_IN(0x00000100L),170CKR_USER_NOT_LOGGED_IN(0x00000101L),171CKR_USER_PIN_NOT_INITIALIZED(0x00000102L),172CKR_USER_TYPE_INVALID(0x00000103L),173CKR_USER_ANOTHER_ALREADY_LOGGED_IN(0x00000104L),174CKR_USER_TOO_MANY_TYPES(0x00000105L),175CKR_WRAPPED_KEY_INVALID(0x00000110L),176CKR_WRAPPED_KEY_LEN_RANGE(0x00000112L),177CKR_WRAPPING_KEY_HANDLE_INVALID(0x00000113L),178CKR_WRAPPING_KEY_SIZE_RANGE(0x00000114L),179CKR_WRAPPING_KEY_TYPE_INCONSISTENT(0x00000115L),180CKR_RANDOM_SEED_NOT_SUPPORTED(0x00000120L),181CKR_RANDOM_NO_RNG(0x00000121L),182CKR_DOMAIN_PARAMS_INVALID(0x00000130L),183CKR_CURVE_NOT_SUPPORTED(0x00000140L),184CKR_BUFFER_TOO_SMALL(0x00000150L),185CKR_SAVED_STATE_INVALID(0x00000160L),186CKR_INFORMATION_SENSITIVE(0x00000170L),187CKR_STATE_UNSAVEABLE(0x00000180L),188CKR_CRYPTOKI_NOT_INITIALIZED(0x00000190L),189CKR_CRYPTOKI_ALREADY_INITIALIZED(0x00000191L),190CKR_MUTEX_BAD(0x000001A0L),191CKR_MUTEX_NOT_LOCKED(0x000001A1L),192CKR_NEW_PIN_MODE(0x000001B0L),193CKR_NEXT_OTP(0x000001B1L),194CKR_EXCEEDED_MAX_ITERATIONS(0x000001B5L),195CKR_FIPS_SELF_TEST_FAILED(0x000001B6L),196CKR_LIBRARY_LOAD_FAILED(0x000001B7L),197CKR_PIN_TOO_WEAK(0x000001B8L),198CKR_PUBLIC_KEY_INVALID(0x000001B9L),199CKR_FUNCTION_REJECTED(0x00000200L),200CKR_TOKEN_RESOURCE_EXCEEDED(0x00000201L),201CKR_OPERATION_CANCEL_FAILED(0x00000202L),202CKR_VENDOR_DEFINED(0x80000000L);203204private final long value;205206RV(long value) {207this.value = value;208}209};210211private static String lookup(long errorCode) {212for (RV r : RV.values()) {213if (r.value == errorCode) {214return r.name();215}216}217// for unknown PKCS11 return values, just use hex as its string218return "0x" + Functions.toFullHexString((int)errorCode);219}220221/**222* Constructor taking the error code (the CKR_* constants in PKCS#11) and223* extra info for error message.224*/225public PKCS11Exception(long errorCode, String extraInfo) {226this.errorCode = errorCode;227this.errorMsg = lookup(errorCode);228if (extraInfo != null) {229this.errorMsg += extraInfo;230}231}232233/**234* This method gets the corresponding text error message from235* a property file. If this file is not available, it returns the error236* code as a hex-string.237*238* @return The message or the error code; e.g. "CKR_DEVICE_ERROR" or239* "0x00000030".240* @preconditions241* @postconditions (result <> null)242*/243public String getMessage() {244return errorMsg;245}246247/**248* Returns the PKCS#11 error code.249*250* @return The error code; e.g. 0x00000030.251* @preconditions252* @postconditions253*/254public long getErrorCode() {255return errorCode;256}257}258259260