包org.ietf.jgss介绍
该JAVA Package提供了一个标准的框架,允许应用程序开发人员使用统一的API从各种底层安全机制(如Kerberos)中利用安全服务,如身份验证,数据完整性和数据机密性。应用程序可以通过唯一的对象标识符标识(object identifier)来选择使用的安全机制,如Kerberos v5 GSS-API机制的对象标识符为1.2.840.113554.1.2.2。此机制可通过GSSManager类的默认实例获得。应用程序通过实例化GSSManager,然后GSSManager作为Security Context 安全上下文的工厂类。应用程序可以使用GSSManager创建安全上下文,然后通过Security Conetext上下文建立循环来建立发送端与接收端的共享上下文,然后就可以从该上下文获得诸如数据完整性和机密性的数据保护。
注意:GSS-API不与参与通信的发送端和接收端执行任何通信,它负责生成令牌,应用程序必须以某种方式传输令牌到相应的一方来建立共享的Security Context。
下面是主要的JGSS的接口和类:

GSSManager
GSSManager类是其他重要GSS-API类的工厂类,还提供有关所支持安全机制的信息。它可以创建实现以下三个GSS-API接口的类的实例:GSSName,GSSCredential和GSSContext。通过GSSManager,可以查询可用;
通过静态方法getInstance获取默认GSSManager子类的实例,应用程序可以实例化GSSManager的其他子类。默认的GSSManager实例支持Kerberos v5 GSS-API机制。该机制的Oid为“1.2.840.113554.1.2.2”,在RFC 1964中定义。
该类常见的重要方法如下:

下面一段示例演示了如何使用GSSManager的常见用法:
GSSManager manager = GSSManager.getInstance();
Oid krb5Mechanism = new Oid(“1.2.840.113554.1.2.2”);
Oid krb5PrincipalNameType = new Oid(“1.2.840.113554.1.2.2.1”);
// Identify who the client wishes to be
GSSName userName = manager.createName(“duke”, GSSName.NT_USER_NAME);
// Identify the name of the server. This uses a Kerberos specific
// name format.
GSSName serverName = manager.createName(“nfs/foo.sun.com”,
krb5PrincipalNameType);
// Acquire credentials for the user
GSSCredential userCreds = manager.createCredential(userName,
GSSCredential.DEFAULT_LIFETIME,
krb5Mechanism,
GSSCredential.INITIATE_ONLY);
// Instantiate and initialize a security context that will be
// established with the server
GSSContext context = manager.createContext(serverName,
krb5Mechanism,
userCreds,
GSSContext.DEFAULT_LIFETIME);
GSSContext
此接口封装GSS-API安全上下文(Security Context),并提供上下文中可用的安全服务。GSS-API以独立于底层传输协议的方式运行,依赖于应用程序来传输由发送端和接收端的安全上下文生成的令牌。如果调用者使用默认的GSSManager实例化上下文,则Kerberos v5 GSS-API机制可用于上下文建立。该机制由Oid“1.2.840.113554.1.2.2”标识。
通常情况下,Security Context建立发生在一个循环中,其中发送端调用initSecContext,接收端调用acceptSecContext直到建立上下文。在此循环中,initSecContext和acceptSecContext方法生成应用程序发送给对等方的安全令牌。根据具体情况,对等体将此类令牌作为输入传递给acceptSecContext或initSecContext方法;
当不再需要上下文时,应用程序应调用dispose以释放上下文可能正在使用的任何系统资源。
该类常见的重要方法如下:

下面一段示例演示了如何使用GSSContext的常见用法:
// Create a context using default credentials
// and the implementation specific default mechanism
GSSManager manager …
GSSName targetName …
GSSContext context = manager.createContext(targetName, null, null,
GSSContext.INDEFINITE_LIFETIME);
// set desired context options prior to context establishment
context.requestConf(true);
context.requestMutualAuth(true);
context.requestReplayDet(true);
context.requestSequenceDet(true);
// establish a context between peers
byte []inToken = new byte[0];
// Loop while there still is a token to be processed
while (!context.isEstablished()) {
byte[] outToken
= context.initSecContext(inToken, 0, inToken.length);
// send the output token if generated
if (outToken != null)
sendToken(outToken);
if (!context.isEstablished()) {
inToken = readToken();
}
// display context information
System.out.println(“Remaining lifetime in seconds = “
+ context.getLifetime());
System.out.println(“Context mechanism = ” + context.getMech());
System.out.println(“Initiator = ” + context.getSrcName());
System.out.println(“Acceptor = ” + context.getTargName());
if (context.getConfState())
System.out.println(“Confidentiality (i.e., privacy) is available”);
if (context.getIntegState())
System.out.println(“Integrity is available”);
// perform wrap on an application supplied message, appMsg,
// using QOP = 0, and requesting privacy service
byte [] appMsg …
MessageProp mProp = new MessageProp(0, true);
byte []tok = context.wrap(appMsg, 0, appMsg.length, mProp);
sendToken(tok);
// release the local-end of the context
context.dispose();
GSSName类
该接口封装了单个GSSAPI的主体实体,应用程序通过GSSManager类中存在的createName方法来创建该实例。
下面给出该类支持的GSSName类型:

下面一段示例演示了如何使用GSSContext的常见用法:
GSSManager manager = GSSManager.getInstance();
// create a host based service name
GSSName name = manager.createName(“service@host”,
GSSName.NT_HOSTBASED_SERVICE);
Oid krb5 = new Oid(“1.2.840.113554.1.2.2”);
GSSName mechName = name.canonicalize(krb5);
// the above two steps are equivalent to the following
GSSName mechName = manager.createName(“service@host”,
GSSName.NT_HOSTBASED_SERVICE, krb5);
// perform name comparison
if (name.equals(mechName))
print(“Names are equals.”);
// obtain textual representation of name and its printable
// name type
print(mechName.toString() +
mechName.getStringNameType().toString());
// export and re-import the name
byte [] exportName = mechName.export();
// create a new name object from the exported buffer
GSSName newName = manager.createName(exportName,
GSSName.NT_EXPORT_NAME);
GSSCredential
该接口封装实体主体的GSSAPI安全凭证,凭证包含必要的加密信息以便所对应的实体能够创建安全上下文。根据不同的底层安全机制,该安全凭证可能包含不同的凭证元素,每个元素包含特定安全机制的信息。
应用程序根据所传递的参数来创建凭证对象,应用程序也可以获取有关凭证对象的特定信息。当不再需要凭证时,应用程序应调用dispose方法以释放凭证对象所持有的任何资源并销毁任何加密的敏感信息。
下面列出该接口的主要属性信息:

下面示例代码演示为特定实体创建GSSCredential实现,查询其属性已经销毁对象;
GSSManager manager = GSSManager.getInstance();
// start by creating a name object for the entity
GSSName name = manager.createName(“myusername”, GSSName.NT_USER_NAME);
// now acquire credentials for the entity
GSSCredential cred = manager.createCredential(name,
GSSCredential.ACCEPT_ONLY);
// display credential information – name, remaining lifetime,
// and the mechanisms it has been acquired over
System.out.println(cred.getName().toString());
System.out.println(cred.getRemainingLifetime());
Oid [] mechs = cred.getMechs();
if (mechs != null) {
for (int i = 0; i < mechs.length; i++)
System.out.println(mechs[i].toString());
}
// release system resources held by the credential
cred.dispose();
MessageProp类
当使用GSSContext对象的wrap和getMIC方法时,MessageProp对象用于指定所需的数据保护质量(QOP),并且指定对数据进行加密与否。默认QOP的设置为0。
当使用GSSContext对象的unwrap和verifyMIC方法时,MessageProp对象用于指定所需的数据保护质量(QOP),并且指定对数据进行加密与否 。在verifyMIC的情况下,数据是否进行加密的状态将始终为false。
下面列出该类的主要方法:

OID类
该类表示通用对象标识符(Universal Object Identifiers)及其关联的操作。Oids是GSSAPI框架内用于识别安全机制和名称格式的全局可解释标识符。Oid的结构和编码在ISOIEC-8824和ISOIEC-8825中定义。例如,Kerberos V5安全机制的Oid表示是“1.2.840.113554.1.2.2”
下面列出该类的主要构造函数:
