1 /* 2 * Copyright (c) 2014, 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 com.oracle.security.ucrypto; 27 28 import java.util.*; 29 import java.security.ProviderException; 30 31 /** 32 * The exception class used by SunUcrypto provider. An exception 33 * object of this class indicates that a function call to the underlying 34 * native calls returned a value not equal to CRYPTO_SUCCESS. 35 * 36 * @since 1.9 37 */ 38 public final class UcryptoException extends ProviderException { 39 40 private static final long serialVersionUID = -933864511110035746L; 41 42 // NOTE: check /usr/include/sys/crypto/common.h for updates 43 private static final String ERROR_MSG[] = { 44 "CRYPTO_SUCCESS", 45 "CRYPTO_CANCEL", 46 "CRYPTO_HOST_MEMORY", 47 "CRYPTO_GENERAL_ERROR", 48 "CRYPTO_FAILED", 49 "CRYPTO_ARGUMENTS_BAD", 50 "CRYPTO_ATTRIBUTE_READ_ONLY", 51 "CRYPTO_ATTRIBUTE_SENSITIVE", 52 "CRYPTO_ATTRIBUTE_TYPE_INVALID", 53 "CRYPTO_ATTRIBUTE_VALUE_INVALID", 54 "CRYPTO_CANCELED", 55 "CRYPTO_DATA_INVALID", 56 "CRYPTO_DATA_LEN_RANGE", 57 "CRYPTO_DEVICE_ERROR", 58 "CRYPTO_DEVICE_MEMORY", 59 "CRYPTO_DEVICE_REMOVED", 60 "CRYPTO_ENCRYPTED_DATA_INVALID", 61 "CRYPTO_ENCRYPTED_DATA_LEN_RANGE", 62 "CRYPTO_KEY_HANDLE_INVALID", 63 "CRYPTO_KEY_SIZE_RANGE", 64 "CRYPTO_KEY_TYPE_INCONSISTENT", 65 "CRYPTO_KEY_NOT_NEEDED", 66 "CRYPTO_KEY_CHANGED", 67 "CRYPTO_KEY_NEEDED", 68 "CRYPTO_KEY_INDIGESTIBLE", 69 "CRYPTO_KEY_FUNCTION_NOT_PERMITTED", 70 "CRYPTO_KEY_NOT_WRAPPABLE", 71 "CRYPTO_KEY_UNEXTRACTABLE", 72 "CRYPTO_MECHANISM_INVALID", 73 "CRYPTO_MECHANISM_PARAM_INVALID", 74 "CRYPTO_OBJECT_HANDLE_INVALID", 75 "CRYPTO_OPERATION_IS_ACTIVE", 76 "CRYPTO_OPERATION_NOT_INITIALIZED", 77 "CRYPTO_PIN_INCORRECT", 78 "CRYPTO_PIN_INVALID", 79 "CRYPTO_PIN_LEN_RANGE", 80 "CRYPTO_PIN_EXPIRED", 81 "CRYPTO_PIN_LOCKED", 82 "CRYPTO_SESSION_CLOSED", 83 "CRYPTO_SESSION_COUNT", 84 "CRYPTO_SESSION_HANDLE_INVALID", 85 "CRYPTO_SESSION_READ_ONLY", 86 "CRYPTO_SESSION_EXISTS", 87 "CRYPTO_SESSION_READ_ONLY_EXISTS", 88 "CRYPTO_SESSION_READ_WRITE_SO_EXISTS", 89 "CRYPTO_SIGNATURE_INVALID", 90 "CRYPTO_SIGNATURE_LEN_RANGE", 91 "CRYPTO_TEMPLATE_INCOMPLETE", 92 "CRYPTO_TEMPLATE_INCONSISTENT", 93 "CRYPTO_UNWRAPPING_KEY_HANDLE_INVALID", 94 "CRYPTO_UNWRAPPING_KEY_SIZE_RANGE", 95 "CRYPTO_UNWRAPPING_KEY_TYPE_INCONSISTENT", 96 "CRYPTO_USER_ALREADY_LOGGED_IN", 97 "CRYPTO_USER_NOT_LOGGED_IN", 98 "CRYPTO_USER_PIN_NOT_INITIALIZED", 99 "CRYPTO_USER_TYPE_INVALID", 100 "CRYPTO_USER_ANOTHER_ALREADY_LOGGED_IN", 101 "CRYPTO_USER_TOO_MANY_TYPES", 102 "CRYPTO_WRAPPED_KEY_INVALID", 103 "CRYPTO_WRAPPED_KEY_LEN_RANGE", 104 "CRYPTO_WRAPPING_KEY_HANDLE_INVALID", 105 "CRYPTO_WRAPPING_KEY_SIZE_RANGE", 106 "CRYPTO_WRAPPING_KEY_TYPE_INCONSISTENT", 107 "CRYPTO_RANDOM_SEED_NOT_SUPPORTED", 108 "CRYPTO_RANDOM_NO_RNG", 109 "CRYPTO_DOMAIN_PARAMS_INVALID", 110 "CRYPTO_BUFFER_TOO_SMALL", 111 "CRYPTO_INFORMATION_SENSITIVE", 112 "CRYPTO_NOT_SUPPORTED", 113 "CRYPTO_QUEUED", 114 "CRYPTO_BUFFER_TOO_BIG", 115 "CRYPTO_INVALID_CONTEXT", 116 "CRYPTO_INVALID_MAC", 117 "CRYPTO_MECH_NOT_SUPPORTED", 118 "CRYPTO_INCONSISTENT_ATTRIBUTE", 119 "CRYPTO_NO_PERMISSION", 120 "CRYPTO_INVALID_PROVIDER_ID", 121 "CRYPTO_VERSION_MISMATCH", 122 "CRYPTO_BUSY", 123 "CRYPTO_UNKNOWN_PROVIDER", 124 "CRYPTO_MODVERIFICATION_FAILED", 125 "CRYPTO_OLD_CTX_TEMPLATE", 126 "CRYPTO_WEAK_KEY", 127 "CRYPTO_FIPS140_ERROR" 128 }; 129 130 /** 131 * The error code if this exception is triggered by a Ucrypto error. 132 */ 133 private final int errorCode; 134 135 /** 136 * This method gets the corresponding text error message from a 137 * predefined mapping. If mapping is not found, then it returns the error 138 * code as a hex-string. 139 * 140 * @return The message or the error code; e.g. "CRYPTO_DATA_INVALID" or 141 * "0x88". 142 */ 143 static String getErrorMessage(int errorCode) { 144 String message; 145 if (errorCode < ERROR_MSG.length) { 146 message = ERROR_MSG[errorCode]; 147 } else { 148 message = "0x" + Integer.toHexString(errorCode); 149 } 150 return message; 151 } 152 153 /** 154 * Constructor taking the error code as defined for the CRYPTO_* constants 155 */ 156 public UcryptoException(int rv) { 157 super(getErrorMessage(rv)); 158 this.errorCode = rv; 159 } 160 161 public UcryptoException(String message) { 162 super(message); 163 errorCode = -1; 164 } 165 166 public UcryptoException(String message, Throwable cause) { 167 super(message, cause); 168 errorCode = -1; 169 } 170 171 /** 172 * Returns the Ucrypto error code. 173 * 174 * @return The error code. 175 */ 176 public int getErrorCode() { 177 return errorCode; 178 } 179 }