1. /*
  2. * @(#)GSSException.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package org.ietf.jgss;
  8. /**
  9. * This exception is thrown whenever a GSS-API error occurs, including
  10. * any mechanism specific error. It may contain both the major and the
  11. * minor GSS-API status codes. Major error codes are those defined at the
  12. * GSS-API level in this class. Minor error codes are mechanism specific
  13. * error codes that can provide additional information. The underlying
  14. * mechanism implementation is responsible for setting appropriate minor
  15. * status codes when throwing this exception. Aside from delivering the
  16. * numeric error codes to the caller, this class performs the mapping from
  17. * their numeric values to textual representations. <p>
  18. *
  19. * @author Mayank Upadhyay
  20. * @version 1.11, 12/19/03
  21. * @since 1.4
  22. */
  23. public class GSSException extends Exception {
  24. private static final long serialVersionUID = -2706218945227726672L;
  25. /**
  26. * Channel bindings mismatch.
  27. */
  28. public static final int BAD_BINDINGS = 1; //start with 1
  29. /**
  30. * Unsupported mechanism requested.
  31. */
  32. public static final int BAD_MECH = 2;
  33. /**
  34. * Invalid name provided.
  35. */
  36. public static final int BAD_NAME = 3;
  37. /**
  38. * Name of unsupported type provided.
  39. */
  40. public static final int BAD_NAMETYPE = 4;
  41. /**
  42. * Invalid status code.
  43. */
  44. /*
  45. * This is meant to be thrown by display_status which displays
  46. * major/minor status when an incorrect status type is passed in to it!
  47. */
  48. public static final int BAD_STATUS = 5;
  49. /**
  50. * Token had invalid integrity check.
  51. */
  52. public static final int BAD_MIC = 6;
  53. /**
  54. * Security context expired.
  55. */
  56. public static final int CONTEXT_EXPIRED = 7;
  57. /**
  58. * Expired credentials.
  59. */
  60. public static final int CREDENTIALS_EXPIRED = 8;
  61. /**
  62. * Defective credentials.
  63. *
  64. */
  65. public static final int DEFECTIVE_CREDENTIAL = 9;
  66. /**
  67. * Defective token.
  68. *
  69. */
  70. public static final int DEFECTIVE_TOKEN = 10;
  71. /**
  72. * General failure, unspecified at GSS-API level.
  73. */
  74. public static final int FAILURE = 11;
  75. /**
  76. * Invalid security context.
  77. */
  78. public static final int NO_CONTEXT = 12;
  79. /**
  80. * Invalid credentials.
  81. */
  82. public static final int NO_CRED = 13;
  83. /**
  84. * Unsupported QOP value.
  85. */
  86. public static final int BAD_QOP = 14;
  87. /**
  88. * Operation unauthorized.
  89. */
  90. public static final int UNAUTHORIZED = 15;
  91. /**
  92. * Operation unavailable.
  93. */
  94. public static final int UNAVAILABLE = 16;
  95. /**
  96. * Duplicate credential element requested.
  97. */
  98. public static final int DUPLICATE_ELEMENT = 17;
  99. /**
  100. * Name contains multi-mechanism elements.
  101. */
  102. public static final int NAME_NOT_MN = 18;
  103. /**
  104. * The token was a duplicate of an earlier token.
  105. * This is a fatal error code that may occur during
  106. * context establishment. It is not used to indicate
  107. * supplementary status values. The MessageProp object is
  108. * used for that purpose.
  109. */
  110. public static final int DUPLICATE_TOKEN = 19;
  111. /**
  112. * The token's validity period has expired. This is a
  113. * fatal error code that may occur during context establishment.
  114. * It is not used to indicate supplementary status values.
  115. * The MessageProp object is used for that purpose.
  116. */
  117. public static final int OLD_TOKEN = 20;
  118. /**
  119. * A later token has already been processed. This is a
  120. * fatal error code that may occur during context establishment.
  121. * It is not used to indicate supplementary status values.
  122. * The MessageProp object is used for that purpose.
  123. */
  124. public static final int UNSEQ_TOKEN = 21;
  125. /**
  126. * An expected per-message token was not received. This is a
  127. * fatal error code that may occur during context establishment.
  128. * It is not used to indicate supplementary status values.
  129. * The MessageProp object is used for that purpose.
  130. */
  131. public static final int GAP_TOKEN = 22;
  132. private static String[] messages = {
  133. "Channel binding mismatch", // BAD_BINDINGS
  134. "Unsupported mechanism requested", // BAD_MECH
  135. "Invalid name provided", // BAD_NAME
  136. "Name of unsupported type provided", //BAD_NAMETYPE
  137. "Invalid input status selector", // BAD_STATUS
  138. "Token had invalid integrity check", // BAD_SIG
  139. "Specified security context expired", // CONTEXT_EXPIRED
  140. "Expired credentials detected", // CREDENTIALS_EXPIRED
  141. "Defective credential detected", // DEFECTIVE_CREDENTIAL
  142. "Defective token detected", // DEFECTIVE_TOKEN
  143. "Failure unspecified at GSS-API level", // FAILURE
  144. "Security context init/accept not yet called or context deleted",
  145. // NO_CONTEXT
  146. "No valid credentials provided", // NO_CRED
  147. "Unsupported QOP value", // BAD_QOP
  148. "Operation unauthorized", // UNAUTHORIZED
  149. "Operation unavailable", // UNAVAILABLE
  150. "Duplicate credential element requested", //DUPLICATE_ELEMENT
  151. "Name contains multi-mechanism elements", // NAME_NOT_MN
  152. "The token was a duplicate of an earlier token", //DUPLICATE_TOKEN
  153. "The token's validity period has expired", //OLD_TOKEN
  154. "A later token has already been processed", //UNSEQ_TOKEN
  155. "An expected per-message token was not received", //GAP_TOKEN
  156. };
  157. /**
  158. * The major code for this exception
  159. *
  160. * @serial
  161. */
  162. private int major;
  163. /**
  164. * The minor code for this exception
  165. *
  166. * @serial
  167. */
  168. private int minor = 0;
  169. /**
  170. * The text string for minor code
  171. *
  172. * @serial
  173. */
  174. private String minorMessage = null;
  175. /**
  176. * Alternate text string for major code
  177. *
  178. * @serial
  179. */
  180. private String majorString = null;
  181. /**
  182. * Creates a GSSException object with a specified major code.
  183. *
  184. * @param majorCode the The GSS error code for the problem causing this
  185. * exception to be thrown.
  186. */
  187. public GSSException (int majorCode) {
  188. if (validateMajor(majorCode))
  189. major = majorCode;
  190. else
  191. major = FAILURE;
  192. }
  193. /**
  194. * Construct a GSSException object with a specified major code and a
  195. * specific major string for it.
  196. *
  197. * @param majorCode the fatal error code causing this exception.
  198. * @param majorString an expicit message to be included in this exception
  199. */
  200. GSSException (int majorCode, String majorString) {
  201. if (validateMajor(majorCode))
  202. major = majorCode;
  203. else
  204. major = FAILURE;
  205. this.majorString = majorString;
  206. }
  207. /**
  208. * Creates a GSSException object with the specified major code, minor
  209. * code, and minor code textual explanation. This constructor is to be
  210. * used when the exception is originating from the underlying mechanism
  211. * level. It allows the setting of both the GSS code and the mechanism
  212. * code.
  213. *
  214. * @param majorCode the GSS error code for the problem causing this
  215. * exception to be thrown.
  216. * @param minorCode the mechanism level error code for the problem
  217. * causing this exception to be thrown.
  218. * @param minorString the textual explanation of the mechanism error
  219. * code.
  220. */
  221. public GSSException (int majorCode, int minorCode, String minorString) {
  222. if (validateMajor(majorCode))
  223. major = majorCode;
  224. else
  225. major = FAILURE;
  226. minor = minorCode;
  227. minorMessage = minorString;
  228. }
  229. /**
  230. * Returns the GSS-API level major error code for the problem causing
  231. * this exception to be thrown. Major error codes are
  232. * defined at the mechanism independent GSS-API level in this
  233. * class. Mechanism specific error codes that might provide more
  234. * information aer set as the minor error code.
  235. *
  236. * @return int the GSS-API level major error code causing this exception
  237. * @see #getMajorString
  238. * @see #getMinor
  239. * @see #getMinorString
  240. */
  241. public int getMajor() {
  242. return major;
  243. }
  244. /**
  245. * Returns the mechanism level error code for the problem causing this
  246. * exception to be thrown. The minor code is set by the underlying
  247. * mechanism.
  248. *
  249. * @return int the mechanism error code; 0 indicates that it has not
  250. * been set.
  251. * @see #getMinorString
  252. * @see #setMinor
  253. */
  254. public int getMinor(){
  255. return minor;
  256. }
  257. /**
  258. * Returns a string explaining the GSS-API level major error code in
  259. * this exception.
  260. *
  261. * @return String explanation string for the major error code
  262. * @see #getMajor
  263. * @see #toString
  264. */
  265. public String getMajorString() {
  266. if (majorString != null)
  267. return majorString;
  268. else
  269. return messages[major - 1];
  270. }
  271. /**
  272. * Returns a string explaining the mechanism specific error code.
  273. * If the minor status code is 0, then no mechanism level error details
  274. * will be available.
  275. *
  276. * @return String a textual explanation of mechanism error code
  277. * @see #getMinor
  278. * @see #getMajorString
  279. * @see #toString
  280. */
  281. public String getMinorString() {
  282. return minorMessage;
  283. }
  284. /**
  285. * Used by the exception thrower to set the mechanism
  286. * level minor error code and its string explanation. This is used by
  287. * mechanism providers to indicate error details.
  288. *
  289. * @param minorCode the mechanism specific error code
  290. * @param message textual explanation of the mechanism error code
  291. * @see #getMinor
  292. */
  293. public void setMinor(int minorCode, String message) {
  294. minor = minorCode;
  295. minorMessage = message;
  296. }
  297. /**
  298. * Returns a textual representation of both the major and the minor
  299. * status codes.
  300. *
  301. * @return a String with the error descriptions
  302. */
  303. public String toString() {
  304. return ("GSSException: " + getMessage());
  305. }
  306. /**
  307. * Returns a textual representation of both the major and the minor
  308. * status codes.
  309. *
  310. * @return a String with the error descriptions
  311. */
  312. public String getMessage() {
  313. if (minor == 0)
  314. return (getMajorString());
  315. return (getMajorString()
  316. + " (Mechanism level: " + getMinorString() + ")");
  317. }
  318. /*
  319. * Validates the major code in the proper range.
  320. */
  321. private boolean validateMajor(int major) {
  322. if (major > 0 && major <= messages.length)
  323. return (true);
  324. return (false);
  325. }
  326. }