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