/* * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Context of a JGSS subject, encapsulating Subject and GSSContext. * * Three "constructors", which acquire the (private) credentials and fill * it into the Subject: * * 1. static fromJAAS(): Creates a Context using a JAAS login config entry * 2. static fromUserPass(): Creates a Context using a username and a password * 3. delegated(): A new context which uses the delegated credentials from a * previously established acceptor Context * * Two context initiators, which create the GSSContext object inside: * * 1. startAsClient() * 2. startAsServer() * * Privileged action: * doAs(): Performs an action in the name of the Subject * * Handshake process: * static handShake(initiator, acceptor) * * A four-phase typical data communication which includes all four GSS * actions (wrap, unwrap, getMic and veryfyMiC): * static transmit(message, from, to)
*/ publicclass Context {
/** * Using the delegated credentials from a previous acceptor
*/ public Context delegated() throws Exception {
Context out = new Context();
out.s = s; try {
out.cred = Subject.callAs(s, new Callable<GSSCredential>() {
@Override public GSSCredential call() throws Exception {
GSSCredential cred = x.getDelegCred(); if (cred == null && x.getCredDelegState() ||
cred != null && !x.getCredDelegState()) { thrownew Exception("getCredDelegState not match");
} return cred;
}
});
} catch (CompletionException ce) { throw (Exception) ce.getCause();
}
out.name = name + " as " + out.cred.getName().toString(); return out;
}
/** * No JAAS login at all, can be used to test JGSS without JAAS
*/ publicstatic Context fromThinAir() throws Exception {
Context out = new Context();
out.s = new Subject(); return out;
}
/** * Logins with a JAAS login config entry name
*/ publicstatic Context fromJAAS(final String name) throws Exception {
Context out = new Context();
out.name = name;
LoginContext lc = new LoginContext(name);
lc.login();
out.s = lc.getSubject(); return out;
}
/** * Logins with username/password as a new Subject
*/ publicstatic Context fromUserPass(
String user, char[] pass, boolean storeKey) throws Exception { return fromUserPass(new Subject(), user, pass, storeKey);
}
/** * Logins with username/password as an existing Subject. The * same subject can be used multiple times to simulate multiple logins.
*/ publicstatic Context fromUserPass(Subject s,
String user, char[] pass, boolean storeKey) throws Exception {
Context out = new Context();
out.name = user;
out.s = s;
Krb5LoginModule krb5 = new Krb5LoginModule();
Map<String, String> map = new HashMap<>();
Map<String, Object> shared = new HashMap<>();
/** * Logins with username/keytab as an existing Subject. The * same subject can be used multiple times to simulate multiple logins.
*/ publicstatic Context fromUserKtab(
String user, String ktab, boolean storeKey) throws Exception { return fromUserKtab(new Subject(), user, ktab, storeKey);
}
/** * Logins with username/keytab as a new subject,
*/ publicstatic Context fromUserKtab(Subject s,
String user, String ktab, boolean storeKey) throws Exception { return fromUserKtab(s, user, ktab, false, storeKey);
}
/** * Logins with username/keytab as a client.
*/ publicstatic Context fromUserKtabAsClient(
String user, String ktab, boolean storeKey) throws Exception { return fromUserKtab(new Subject(), user, ktab, true, storeKey);
}
privatestatic Context fromUserKtab(Subject s,
String user, String ktab, boolean isInitiator, boolean storeKey) throws Exception {
Context out = new Context();
out.name = user;
out.s = s;
Krb5LoginModule krb5 = new Krb5LoginModule();
Map<String, String> map = new HashMap<>();
/** * Starts as a server * @param mech GSS mech * @throws java.lang.Exception
*/ publicvoid startAsServer(final Oid mech) throws Exception {
startAsServer(null, mech, false);
}
publicvoid startAsServer(final String name, final Oid mech) throws Exception {
startAsServer(name, mech, false);
} /** * Starts as a server with the specified service name * @param name the service name * @param mech GSS mech * @throws java.lang.Exception
*/ publicvoid startAsServer(final String name, final Oid mech, finalboolean asInitiator) throws Exception {
doAs(new Action() {
@Override publicbyte[] run(Context me, byte[] dummy) throws Exception {
GSSManager m = GSSManager.getInstance();
me.cred = m.createCredential(
name == null ? null :
(name.indexOf('@') < 0 ?
m.createName(name, null) :
m.createName(name, GSSName.NT_HOSTBASED_SERVICE)),
GSSCredential.INDEFINITE_LIFETIME,
mech,
asInitiator?
GSSCredential.INITIATE_AND_ACCEPT:
GSSCredential.ACCEPT_ONLY);
me.x = m.createContext(me.cred); returnnull;
}
}, null);
}
/** * Accesses the internal GSSContext object. Currently it's used for -- * * 1. calling requestXXX() before handshake * 2. accessing source name * * Note: If the application needs to do any privileged call on this * object, please use doAs(). Otherwise, it can be done directly. The * methods listed above are all non-privileged calls. * * @return the GSSContext object
*/ public GSSContext x() { return x;
}
/** * Accesses the internal subject. * @return the subject
*/ public Subject s() { return s;
}
/** * Returns the cred inside, if there is one
*/ public GSSCredential cred() { return cred;
}
/** * Disposes the GSSContext within * @throws org.ietf.jgss.GSSException
*/ publicvoid dispose() throws GSSException {
x.dispose();
}
/** * Does something using the Subject inside * @param action the action * @param in the input byte * @return the output byte * @throws java.lang.Exception
*/ publicbyte[] doAs(final Action action, finalbyte[] in) throws Exception { try { return Subject.callAs(s, new Callable<byte[]>() {
/** * Transmits a message from one Context to another. The sender wraps the * message and sends it to the receiver. The receiver unwraps it, creates * a MIC of the clear text and sends it back to the sender. The sender * verifies the MIC against the message sent earlier. * @param message the message * @param s1 the sender * @param s2 the receiver * @throws java.lang.Exception If anything goes wrong
*/ staticpublicvoid transmit(String message, final Context s1, final Context s2) throws Exception {
transmit(message.getBytes(), s1, s2);
}
/** * Transmits a message from one Context to another. The sender wraps the * message and sends it to the receiver. The receiver unwraps it, creates * a MIC of the clear text and sends it back to the sender. The sender * verifies the MIC against the message sent earlier. * @param messageBytes the message * @param s1 the sender * @param s2 the receiver * @throws java.lang.Exception If anything goes wrong
*/ staticpublicvoid transmit(byte[] messageBytes, final Context s1, final Context s2) throws Exception {
System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n",
s1.name, s2.name); byte[] wrapped = s1.wrap(messageBytes, true); byte[] unwrapped = s2.unwrap(wrapped, s2.x.getConfState()); if (!Arrays.equals(messageBytes, unwrapped)) { thrownew Exception("wrap/unwrap mismatch");
} byte[] mic = s2.getMic(unwrapped);
s1.verifyMic(mic, messageBytes);
}
/** * Returns a string description of a MessageProp object * @param prop the object * @return the description
*/ staticpublic String printProp(MessageProp prop) {
StringBuffer sb = new StringBuffer();
sb.append("MessagePop: ");
sb.append("QOP="+ prop.getQOP() + ", ");
sb.append(prop.getPrivacy()?"privacy, ":"");
sb.append(prop.isDuplicateToken()?"dup, ":"");
sb.append(prop.isGapToken()?"gap, ":"");
sb.append(prop.isOldToken()?"old, ":"");
sb.append(prop.isUnseqToken()?"unseq, ":""); if (prop.getMinorStatus() != 0) {
sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")");
} return sb.toString();
}
public Context impersonate(final String someone) throws Exception { try {
GSSCredential creds = Subject.callAs(s, new Callable<GSSCredential>() {
@Override public GSSCredential call() throws Exception {
GSSManager m = GSSManager.getInstance();
GSSName other = m.createName(someone, GSSName.NT_USER_NAME); if (Context.this.cred == null) {
Context.this.cred = m.createCredential(GSSCredential.INITIATE_ONLY);
} return (GSSCredential) Class.forName("com.sun.security.jgss.ExtendedGSSCredential")
.getMethod("impersonate", GSSName.class)
.invoke(Context.this.cred, other);
}
});
Context out = new Context();
out.s = s;
out.cred = creds;
out.name = name + " as " + out.cred.getName().toString(); return out;
} catch (CompletionException ce) {
Exception e = (Exception) ce.getCause(); if (e instanceof InvocationTargetException) { throw (Exception)((InvocationTargetException) e).getTargetException();
} else { throw e;
}
}
}
/** * Saves the tickets to a ccache file. * * @param file pathname of the ccache file * @return true if created, false otherwise.
*/ publicboolean ccache(String file) throws Exception {
Set<KerberosTicket> tickets
= s.getPrivateCredentials(KerberosTicket.class); if (tickets != null && !tickets.isEmpty()) {
CredentialsCache cc = null; for (KerberosTicket t : tickets) {
Credentials cred = Krb5Util.ticketToCreds(t); if (cc == null) {
cc = CredentialsCache.create(cred.getClient(), file);
}
cc.update(cred.toCCacheCreds());
} if (cc != null) {
cc.save(); returntrue;
}
} returnfalse;
}
/** * Handshake (security context establishment process) between two Contexts * @param c the initiator * @param s the acceptor * @throws java.lang.Exception
*/ staticpublicvoid handshake(final Context c, final Context s) throws Exception { byte[] t = newbyte[0]; while (true) { if (t != null || !c.x.isEstablished()) t = c.take(t); if (t != null || !s.x.isEstablished()) t = s.take(t); if (c.x.isEstablished() && s.x.isEstablished()) break;
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.44 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.