Path: blob/master/src/java.security.jgss/share/classes/org/ietf/jgss/GSSException.java
41155 views
/*1* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package org.ietf.jgss;2627/**28* This exception is thrown whenever a GSS-API error occurs, including29* any mechanism specific error. It may contain both the major and the30* minor GSS-API status codes. Major error codes are those defined at the31* GSS-API level in this class. Minor error codes are mechanism specific32* error codes that can provide additional information. The underlying33* mechanism implementation is responsible for setting appropriate minor34* status codes when throwing this exception. Aside from delivering the35* numeric error codes to the caller, this class performs the mapping from36* their numeric values to textual representations.37*38* @author Mayank Upadhyay39* @since 1.440*/41public class GSSException extends Exception {4243private static final long serialVersionUID = -2706218945227726672L;4445/**46* Channel bindings mismatch.47*/48public static final int BAD_BINDINGS = 1; //start with 14950/**51* Unsupported mechanism requested.52*/53public static final int BAD_MECH = 2;5455/**56* Invalid name provided.57*/58public static final int BAD_NAME = 3;5960/**61* Name of unsupported type provided.62*/63public static final int BAD_NAMETYPE = 4;6465/**66* Invalid status code.67*/68/*69* This is meant to be thrown by display_status which displays70* major/minor status when an incorrect status type is passed in to it!71*/72public static final int BAD_STATUS = 5;7374/**75* Token had invalid integrity check.76*/77public static final int BAD_MIC = 6;7879/**80* Security context expired.81*/82public static final int CONTEXT_EXPIRED = 7;8384/**85* Expired credentials.86*/87public static final int CREDENTIALS_EXPIRED = 8;8889/**90* Defective credentials.91*92*/93public static final int DEFECTIVE_CREDENTIAL = 9;9495/**96* Defective token.97*98*/99public static final int DEFECTIVE_TOKEN = 10;100101/**102* General failure, unspecified at GSS-API level.103*/104public static final int FAILURE = 11;105106/**107* Invalid security context.108*/109public static final int NO_CONTEXT = 12;110111/**112* Invalid credentials.113*/114public static final int NO_CRED = 13;115116/**117* Unsupported QOP value.118*/119public static final int BAD_QOP = 14;120121/**122* Operation unauthorized.123*/124public static final int UNAUTHORIZED = 15;125126/**127* Operation unavailable.128*/129public static final int UNAVAILABLE = 16;130131/**132* Duplicate credential element requested.133*/134public static final int DUPLICATE_ELEMENT = 17;135136/**137* Name contains multi-mechanism elements.138*/139public static final int NAME_NOT_MN = 18;140141/**142* The token was a duplicate of an earlier token.143* This is a fatal error code that may occur during144* context establishment. It is not used to indicate145* supplementary status values. The MessageProp object is146* used for that purpose.147*/148public static final int DUPLICATE_TOKEN = 19;149150/**151* The token's validity period has expired. This is a152* fatal error code that may occur during context establishment.153* It is not used to indicate supplementary status values.154* The MessageProp object is used for that purpose.155*/156public static final int OLD_TOKEN = 20;157158159/**160* A later token has already been processed. This is a161* fatal error code that may occur during context establishment.162* It is not used to indicate supplementary status values.163* The MessageProp object is used for that purpose.164*/165public static final int UNSEQ_TOKEN = 21;166167168/**169* An expected per-message token was not received. This is a170* fatal error code that may occur during context establishment.171* It is not used to indicate supplementary status values.172* The MessageProp object is used for that purpose.173*/174public static final int GAP_TOKEN = 22;175176177private static String[] messages = {178"Channel binding mismatch", // BAD_BINDINGS179"Unsupported mechanism requested", // BAD_MECH180"Invalid name provided", // BAD_NAME181"Name of unsupported type provided", //BAD_NAMETYPE182"Invalid input status selector", // BAD_STATUS183"Token had invalid integrity check", // BAD_SIG184"Specified security context expired", // CONTEXT_EXPIRED185"Expired credentials detected", // CREDENTIALS_EXPIRED186"Defective credential detected", // DEFECTIVE_CREDENTIAL187"Defective token detected", // DEFECTIVE_TOKEN188"Failure unspecified at GSS-API level", // FAILURE189"Security context init/accept not yet called or context deleted",190// NO_CONTEXT191"No valid credentials provided", // NO_CRED192"Unsupported QOP value", // BAD_QOP193"Operation unauthorized", // UNAUTHORIZED194"Operation unavailable", // UNAVAILABLE195"Duplicate credential element requested", //DUPLICATE_ELEMENT196"Name contains multi-mechanism elements", // NAME_NOT_MN197"The token was a duplicate of an earlier token", //DUPLICATE_TOKEN198"The token's validity period has expired", //OLD_TOKEN199"A later token has already been processed", //UNSEQ_TOKEN200"An expected per-message token was not received", //GAP_TOKEN201};202203/**204* The major code for this exception205*206* @serial207*/208private int major;209210/**211* The minor code for this exception212*213* @serial214*/215private int minor = 0;216217/**218* The text string for minor code219*220* @serial221*/222private String minorMessage = null;223224/**225* Alternate text string for major code226*227* @serial228*/229230private String majorString = null;231232/**233* Creates a GSSException object with a specified major code.234*235* @param majorCode the The GSS error code for the problem causing this236* exception to be thrown.237*/238public GSSException (int majorCode) {239240if (validateMajor(majorCode))241major = majorCode;242else243major = FAILURE;244}245246/**247* Construct a GSSException object with a specified major code and a248* specific major string for it.249*250* @param majorCode the fatal error code causing this exception.251* @param majorString an expicit message to be included in this exception252*/253GSSException (int majorCode, String majorString) {254255if (validateMajor(majorCode))256major = majorCode;257else258major = FAILURE;259this.majorString = majorString;260}261262263/**264* Creates a GSSException object with the specified major code, minor265* code, and minor code textual explanation. This constructor is to be266* used when the exception is originating from the underlying mechanism267* level. It allows the setting of both the GSS code and the mechanism268* code.269*270* @param majorCode the GSS error code for the problem causing this271* exception to be thrown.272* @param minorCode the mechanism level error code for the problem273* causing this exception to be thrown.274* @param minorString the textual explanation of the mechanism error275* code.276*/277public GSSException (int majorCode, int minorCode, String minorString) {278279if (validateMajor(majorCode))280major = majorCode;281else282major = FAILURE;283284minor = minorCode;285minorMessage = minorString;286}287288/**289* Returns the GSS-API level major error code for the problem causing290* this exception to be thrown. Major error codes are291* defined at the mechanism independent GSS-API level in this292* class. Mechanism specific error codes that might provide more293* information are set as the minor error code.294*295* @return int the GSS-API level major error code causing this exception296* @see #getMajorString297* @see #getMinor298* @see #getMinorString299*/300public int getMajor() {301return major;302}303304/**305* Returns the mechanism level error code for the problem causing this306* exception to be thrown. The minor code is set by the underlying307* mechanism.308*309* @return int the mechanism error code; 0 indicates that it has not310* been set.311* @see #getMinorString312* @see #setMinor313*/314public int getMinor(){315return minor;316}317318/**319* Returns a string explaining the GSS-API level major error code in320* this exception.321*322* @return String explanation string for the major error code323* @see #getMajor324* @see #toString325*/326public String getMajorString() {327328if (majorString != null)329return majorString;330else331return messages[major - 1];332}333334335/**336* Returns a string explaining the mechanism specific error code.337* If the minor status code is 0, then no mechanism level error details338* will be available.339*340* @return String a textual explanation of mechanism error code341* @see #getMinor342* @see #getMajorString343* @see #toString344*/345public String getMinorString() {346347return minorMessage;348}349350351/**352* Used by the exception thrower to set the mechanism353* level minor error code and its string explanation. This is used by354* mechanism providers to indicate error details.355*356* @param minorCode the mechanism specific error code357* @param message textual explanation of the mechanism error code358* @see #getMinor359*/360public void setMinor(int minorCode, String message) {361362minor = minorCode;363minorMessage = message;364}365366367/**368* Returns a textual representation of both the major and the minor369* status codes.370*371* @return a String with the error descriptions372*/373public String toString() {374return ("GSSException: " + getMessage());375}376377/**378* Returns a textual representation of both the major and the minor379* status codes.380*381* @return a String with the error descriptions382*/383public String getMessage() {384if (minor == 0)385return (getMajorString());386387return (getMajorString()388+ " (Mechanism level: " + getMinorString() + ")");389}390391392/*393* Validates the major code in the proper range.394*/395private boolean validateMajor(int major) {396397if (major > 0 && major <= messages.length)398return (true);399400return (false);401}402}403404405