Path: blob/master/src/java.security.jgss/share/classes/org/ietf/jgss/GSSCredential.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 interface encapsulates the GSS-API credentials for an entity. A29* credential contains all the necessary cryptographic information to30* enable the creation of a context on behalf of the entity that it31* represents. It may contain multiple, distinct, mechanism specific32* credential elements, each containing information for a specific33* security mechanism, but all referring to the same entity. A credential34* may be used to perform context initiation, acceptance, or both.<p>35*36* Credentials are instantiated using one of the37* {@code createCredential} methods in the {@link GSSManager38* GSSManager} class. GSS-API credential creation is not39* intended to provide a "login to the network" function, as such a40* function would involve the creation of new credentials rather than41* merely acquiring a handle to existing credentials. The42* <a href=package-summary.html#useSubjectCredsOnly>section on credential43* acquisition</a> in the package level description describes44* how existing credentials are acquired in the Java platform. GSS-API45* implementations must impose a local access-control policy on callers to46* prevent unauthorized callers from acquiring credentials to which they47* are not entitled. <p>48*49* Applications will create a credential object passing the desired50* parameters. The application can then use the query methods to obtain51* specific information about the instantiated credential object.52* When the credential is no longer needed, the application should call53* the {@link #dispose() dispose} method to release any resources held by54* the credential object and to destroy any cryptographically sensitive55* information.<p>56*57* This example code demonstrates the creation of a GSSCredential58* implementation for a specific entity, querying of its fields, and its59* release when it is no longer needed:60* <pre>61* GSSManager manager = GSSManager.getInstance();62*63* // start by creating a name object for the entity64* GSSName name = manager.createName("myusername", GSSName.NT_USER_NAME);65*66* // now acquire credentials for the entity67* GSSCredential cred = manager.createCredential(name,68* GSSCredential.ACCEPT_ONLY);69*70* // display credential information - name, remaining lifetime,71* // and the mechanisms it has been acquired over72* System.out.println(cred.getName().toString());73* System.out.println(cred.getRemainingLifetime());74*75* Oid [] mechs = cred.getMechs();76* if (mechs != null) {77* for (int i = 0; i{@literal <} mechs.length; i++)78* System.out.println(mechs[i].toString());79* }80*81* // release system resources held by the credential82* cred.dispose();83* </pre>84*85* @see GSSManager#createCredential(int)86* @see GSSManager#createCredential(GSSName, int, Oid, int)87* @see GSSManager#createCredential(GSSName, int, Oid[], int)88* @see #dispose()89*90* @author Mayank Upadhyay91* @since 1.492*/93public interface GSSCredential extends Cloneable{9495/**96* Credential usage flag requesting that it be usable97* for both context initiation and acceptance.98*99*/100public static final int INITIATE_AND_ACCEPT = 0;101102103/**104* Credential usage flag requesting that it be usable105* for context initiation only.106*107*/108public static final int INITIATE_ONLY = 1;109110111/**112* Credential usage flag requesting that it be usable113* for context acceptance only.114*115*/116public static final int ACCEPT_ONLY = 2;117118119/**120* A lifetime constant representing the default credential lifetime. This121* value it set to 0.122*/123public static final int DEFAULT_LIFETIME = 0;124125/**126* A lifetime constant representing indefinite credential lifetime.127* This value must is set to the maximum integer value in Java -128* {@link java.lang.Integer#MAX_VALUE Integer.MAX_VALUE}.129*/130public static final int INDEFINITE_LIFETIME = Integer.MAX_VALUE;131132/**133* Releases any sensitive information that the GSSCredential object may134* be containing. Applications should call this method as soon as the135* credential is no longer needed to minimize the time any sensitive136* information is maintained.137*138* @throws GSSException containing the following139* major error codes:140* {@link GSSException#FAILURE GSSException.FAILURE}141*/142public void dispose() throws GSSException;143144/**145* Retrieves the name of the entity that the credential asserts.146*147* @return a GSSName representing the entity148*149* @throws GSSException containing the following150* major error codes:151* {@link GSSException#FAILURE GSSException.FAILURE}152*/153public GSSName getName() throws GSSException;154155/**156* Retrieves a Mechanism Name of the entity that the credential157* asserts. This is equivalent to calling {@link158* GSSName#canonicalize(Oid) canonicalize} on the value returned by159* the other form of {@link #getName() getName}.160*161* @param mech the Oid of the mechanism for which the Mechanism Name162* should be returned.163* @return a GSSName representing the entity canonicalized for the164* desired mechanism165*166* @throws GSSException containing the following167* major error codes:168* {@link GSSException#BAD_MECH GSSException.BAD_MECH},169* {@link GSSException#FAILURE GSSException.FAILURE}170*/171public GSSName getName(Oid mech) throws GSSException;172173/**174* Returns the remaining lifetime in seconds for a credential. The175* remaining lifetime is the minimum lifetime amongst all of the underlying176* mechanism specific credential elements.177*178* @return the minimum remaining lifetime in seconds for this179* credential. A return value of {@link #INDEFINITE_LIFETIME180* INDEFINITE_LIFETIME} indicates that the credential does181* not expire. A return value of 0 indicates that the credential is182* already expired.183*184* @see #getRemainingInitLifetime(Oid)185* @see #getRemainingAcceptLifetime(Oid)186*187* @throws GSSException containing the following188* major error codes:189* {@link GSSException#FAILURE GSSException.FAILURE}190*/191public int getRemainingLifetime() throws GSSException;192193/**194* Returns the lifetime in seconds for the credential to remain capable195* of initiating security contexts using the specified mechanism. This196* method queries the initiator credential element that belongs to the197* specified mechanism.198*199* @return the number of seconds remaining in the life of this credential200* element. A return value of {@link #INDEFINITE_LIFETIME201* INDEFINITE_LIFETIME} indicates that the credential element does not202* expire. A return value of 0 indicates that the credential element is203* already expired.204*205* @param mech the Oid of the mechanism whose initiator credential element206* should be queried.207*208* @throws GSSException containing the following209* major error codes:210* {@link GSSException#BAD_MECH GSSException.BAD_MECH},211* {@link GSSException#FAILURE GSSException.FAILURE}212*/213public int getRemainingInitLifetime(Oid mech) throws GSSException;214215/**216* Returns the lifetime in seconds for the credential to remain capable217* of accepting security contexts using the specified mechanism. This218* method queries the acceptor credential element that belongs to the219* specified mechanism.220*221* @return the number of seconds remaining in the life of this credential222* element. A return value of {@link #INDEFINITE_LIFETIME223* INDEFINITE_LIFETIME} indicates that the credential element does not224* expire. A return value of 0 indicates that the credential element is225* already expired.226*227* @param mech the Oid of the mechanism whose acceptor credential element228* should be queried.229*230* @throws GSSException containing the following231* major error codes:232* {@link GSSException#BAD_MECH GSSException.BAD_MECH},233* {@link GSSException#FAILURE GSSException.FAILURE}234*/235public int getRemainingAcceptLifetime(Oid mech) throws GSSException;236237/**238* Returns the credential usage mode. In other words, it239* tells us if this credential can be used for initiating or accepting240* security contexts. It does not tell us which mechanism(s) has to be241* used in order to do so. It is expected that an application will allow242* the GSS-API to pick a default mechanism after calling this method.243*244* @return The return value will be one of {@link #INITIATE_ONLY245* INITIATE_ONLY}, {@link #ACCEPT_ONLY ACCEPT_ONLY}, and {@link246* #INITIATE_AND_ACCEPT INITIATE_AND_ACCEPT}.247*248* @throws GSSException containing the following249* major error codes:250* {@link GSSException#FAILURE GSSException.FAILURE}251*/252public int getUsage() throws GSSException;253254/**255* Returns the credential usage mode for a specific mechanism. In other256* words, it tells us if this credential can be used257* for initiating or accepting security contexts with a given underlying258* mechanism.259*260* @return The return value will be one of {@link #INITIATE_ONLY261* INITIATE_ONLY}, {@link #ACCEPT_ONLY ACCEPT_ONLY}, and {@link262* #INITIATE_AND_ACCEPT INITIATE_AND_ACCEPT}.263* @param mech the Oid of the mechanism whose credentials usage mode is264* to be determined.265*266* @throws GSSException containing the following267* major error codes:268* {@link GSSException#BAD_MECH GSSException.BAD_MECH},269* {@link GSSException#FAILURE GSSException.FAILURE}270*/271public int getUsage(Oid mech) throws GSSException;272273/**274* Returns a list of mechanisms supported by this credential. It does275* not tell us which ones can be used to initiate276* contexts and which ones can be used to accept contexts. The277* application must call the {@link #getUsage(Oid) getUsage} method with278* each of the returned Oid's to determine the possible modes of279* usage.280*281* @return an array of Oid's corresponding to the supported mechanisms.282*283* @throws GSSException containing the following284* major error codes:285* {@link GSSException#FAILURE GSSException.FAILURE}286*/287public Oid[] getMechs() throws GSSException;288289/**290* Adds a mechanism specific credential-element to an existing291* credential. This method allows the construction of credentials, one292* mechanism at a time.<p>293*294* This routine is envisioned to be used mainly by context acceptors295* during the creation of acceptor credentials which are to be used296* with a variety of clients using different security mechanisms.<p>297*298* This routine adds the new credential element "in-place". To add the299* element in a new credential, first call {@code clone} to obtain a300* copy of this credential, then call its {@code add} method.<p>301*302* As always, GSS-API implementations must impose a local access-control303* policy on callers to prevent unauthorized callers from acquiring304* credentials to which they are not entitled.305*306* Non-default values for initLifetime and acceptLifetime cannot always307* be honored by the underlying mechanisms, thus callers should be308* prepared to call {@link #getRemainingInitLifetime(Oid)309* getRemainingInitLifetime} and {@link #getRemainingAcceptLifetime(Oid)310* getRemainingAcceptLifetime} on the credential.311*312* @param name the name of the principal for whom this credential is to313* be acquired. Use {@code null} to specify the default314* principal.315* @param initLifetime the number of seconds that the credential element316* should remain valid for initiating of security contexts. Use {@link317* GSSCredential#INDEFINITE_LIFETIME GSSCredential.INDEFINITE_LIFETIME}318* to request that the credentials have the maximum permitted lifetime319* for this. Use {@link GSSCredential#DEFAULT_LIFETIME320* GSSCredential.DEFAULT_LIFETIME} to request default credential lifetime321* for this.322* @param acceptLifetime the number of seconds that the credential323* element should remain valid for accepting security contexts. Use {@link324* GSSCredential#INDEFINITE_LIFETIME GSSCredential.INDEFINITE_LIFETIME}325* to request that the credentials have the maximum permitted lifetime326* for this. Use {@link GSSCredential#DEFAULT_LIFETIME327* GSSCredential.DEFAULT_LIFETIME} to request default credential lifetime328* for this.329* @param mech the mechanism over which the credential is to be acquired.330* @param usage the usage mode that this credential331* element should add to the credential. The value332* of this parameter must be one of:333* {@link #INITIATE_AND_ACCEPT INITIATE_AND_ACCEPT},334* {@link #ACCEPT_ONLY ACCEPT_ONLY}, and335* {@link #INITIATE_ONLY INITIATE_ONLY}.336*337* @throws GSSException containing the following338* major error codes:339* {@link GSSException#DUPLICATE_ELEMENT340* GSSException.DUPLICATE_ELEMENT},341* {@link GSSException#BAD_MECH GSSException.BAD_MECH},342* {@link GSSException#BAD_NAMETYPE GSSException.BAD_NAMETYPE},343* {@link GSSException#NO_CRED GSSException.NO_CRED},344* {@link GSSException#CREDENTIALS_EXPIRED345* GSSException.CREDENTIALS_EXPIRED},346* {@link GSSException#FAILURE GSSException.FAILURE}347*/348public void add(GSSName name, int initLifetime, int acceptLifetime,349Oid mech, int usage) throws GSSException;350351/**352* Tests if this GSSCredential asserts the same entity as the supplied353* object. The two credentials must be acquired over the same354* mechanisms and must refer to the same principal.355*356* @return {@code true} if the two GSSCredentials assert the same357* entity; {@code false} otherwise.358* @param another another GSSCredential for comparison to this one359*/360public boolean equals(Object another);361362/**363* Returns a hashcode value for this GSSCredential.364*365* @return a hashCode value366*/367public int hashCode();368369}370371372