sun.security.krb5.Credentials Java Examples

The following examples show how to use sun.security.krb5.Credentials. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Krb5LoginModule.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #2
Source File: Krb5LoginModule.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isOld(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        Date authTime = creds.getAuthTime();
        long now = System.currentTimeMillis();
        if (authTime != null) {
            // pass the mid between auth and end
            return now - authTime.getTime() > endTime.getTime() - now;
        } else {
            // will expire in less than 2 hours
            return now <= endTime.getTime() - 1000*3600*2L;
        }
    }
    return false;
}
 
Example #3
Source File: Krb5LoginModule.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isOld(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        Date authTime = creds.getAuthTime();
        long now = System.currentTimeMillis();
        if (authTime != null) {
            // pass the mid between auth and end
            return now - authTime.getTime() > endTime.getTime() - now;
        } else {
            // will expire in less than 2 hours
            return now <= endTime.getTime() - 1000*3600*2L;
        }
    }
    return false;
}
 
Example #4
Source File: Krb5Util.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #5
Source File: Krb5Util.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Credentials ticketToCreds(KerberosTicket kerbTicket)
        throws KrbException, IOException {
    KerberosPrincipal clientAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetClientAlias(kerbTicket);
    KerberosPrincipal serverAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetServerAlias(kerbTicket);
    return new Credentials(
        kerbTicket.getEncoded(),
        kerbTicket.getClient().getName(),
        (clientAlias != null ? clientAlias.getName() : null),
        kerbTicket.getServer().getName(),
        (serverAlias != null ? serverAlias.getName() : null),
        kerbTicket.getSessionKey().getEncoded(),
        kerbTicket.getSessionKeyType(),
        kerbTicket.getFlags(),
        kerbTicket.getAuthTime(),
        kerbTicket.getStartTime(),
        kerbTicket.getEndTime(),
        kerbTicket.getRenewTill(),
        kerbTicket.getClientAddresses());
}
 
Example #6
Source File: Krb5Util.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #7
Source File: Krb5LoginModule.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #8
Source File: Krb5LoginModule.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isOld(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        Date authTime = creds.getAuthTime();
        long now = System.currentTimeMillis();
        if (authTime != null) {
            // pass the mid between auth and end
            return now - authTime.getTime() > endTime.getTime() - now;
        } else {
            // will expire in less than 2 hours
            return now <= endTime.getTime() - 1000*3600*2L;
        }
    }
    return false;
}
 
Example #9
Source File: Krb5LoginModule.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #10
Source File: Krb5LoginModule.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (creds.getRenewTill() == null) {
            // Renewable ticket without renew-till. Illegal and ignored.
            return creds;
        }
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #11
Source File: Krb5ProxyCredential.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static Krb5CredElement tryImpersonation(GSSCaller caller,
        Krb5InitCredential initiator) throws GSSException {

    try {
        KerberosTicket proxy = initiator.proxyTicket;
        if (proxy != null) {
            Credentials proxyCreds = Krb5Util.ticketToCreds(proxy);
            return new Krb5ProxyCredential(initiator,
                    Krb5NameElement.getInstance(proxyCreds.getClient()),
                    proxyCreds.getTicket());
        } else {
            return initiator;
        }
    } catch (KrbException | IOException e) {
        throw new GSSException(GSSException.DEFECTIVE_CREDENTIAL, -1,
                "Cannot create proxy credential");
    }
}
 
Example #12
Source File: Krb5Util.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #13
Source File: Context.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Saves the tickets to a ccache file.
 *
 * @param file pathname of the ccache file
 * @return true if created, false otherwise.
 */
public boolean 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();
            return true;
        }
    }
    return false;
}
 
Example #14
Source File: Krb5ProxyCredential.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static Krb5CredElement tryImpersonation(GSSCaller caller,
        Krb5InitCredential initiator) throws GSSException {

    try {
        KerberosTicket proxy = initiator.proxyTicket;
        if (proxy != null) {
            Credentials proxyCreds = Krb5Util.ticketToCreds(proxy);
            return new Krb5ProxyCredential(initiator,
                    Krb5NameElement.getInstance(proxyCreds.getClient()),
                    proxyCreds.getTicket());
        } else {
            return initiator;
        }
    } catch (KrbException | IOException e) {
        throw new GSSException(GSSException.DEFECTIVE_CREDENTIAL, -1,
                "Cannot create proxy credential");
    }
}
 
Example #15
Source File: Krb5Util.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static Credentials ticketToCreds(KerberosTicket kerbTicket)
        throws KrbException, IOException {
    KerberosPrincipal clientAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetClientAlias(kerbTicket);
    KerberosPrincipal serverAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetServerAlias(kerbTicket);
    return new Credentials(
        kerbTicket.getEncoded(),
        kerbTicket.getClient().getName(),
        (clientAlias != null ? clientAlias.getName() : null),
        kerbTicket.getServer().getName(),
        (serverAlias != null ? serverAlias.getName() : null),
        kerbTicket.getSessionKey().getEncoded(),
        kerbTicket.getSessionKeyType(),
        kerbTicket.getFlags(),
        kerbTicket.getAuthTime(),
        kerbTicket.getStartTime(),
        kerbTicket.getEndTime(),
        kerbTicket.getRenewTill(),
        kerbTicket.getClientAddresses());
}
 
Example #16
Source File: Krb5LoginModule.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #17
Source File: Krb5Util.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #18
Source File: Krb5Util.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Credentials ticketToCreds(KerberosTicket kerbTicket)
        throws KrbException, IOException {
    KerberosPrincipal clientAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetClientAlias(kerbTicket);
    KerberosPrincipal serverAlias = KerberosSecrets
            .getJavaxSecurityAuthKerberosAccess()
            .kerberosTicketGetServerAlias(kerbTicket);
    return new Credentials(
        kerbTicket.getEncoded(),
        kerbTicket.getClient().getName(),
        (clientAlias != null ? clientAlias.getName() : null),
        kerbTicket.getServer().getName(),
        (serverAlias != null ? serverAlias.getName() : null),
        kerbTicket.getSessionKey().getEncoded(),
        kerbTicket.getSessionKeyType(),
        kerbTicket.getFlags(),
        kerbTicket.getAuthTime(),
        kerbTicket.getStartTime(),
        kerbTicket.getEndTime(),
        kerbTicket.getRenewTill(),
        kerbTicket.getClientAddresses());
}
 
Example #19
Source File: Krb5LoginModule.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isOld(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        Date authTime = creds.getAuthTime();
        long now = System.currentTimeMillis();
        if (authTime != null) {
            // pass the mid between auth and end
            return now - authTime.getTime() > endTime.getTime() - now;
        } else {
            // will expire in less than 2 hours
            return now <= endTime.getTime() - 1000*3600*2L;
        }
    }
    return false;
}
 
Example #20
Source File: KerberosTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static KerberosTicket getKerberosTicket ( KerberosPrincipal principal, String password, Long expire ) throws Exception {
    PrincipalName principalName = convertPrincipal(principal);
    KrbAsReqBuilder builder = new KrbAsReqBuilder(principalName, password != null ? password.toCharArray() : new char[0]);

    if ( expire != null ) {
        System.out.println("Request expires " + expire);
        KerberosTime till = new KerberosTime(expire);
        Field tillF = builder.getClass().getDeclaredField("till");
        tillF.setAccessible(true);
        tillF.set(builder, till);
    }

    Credentials creds = builder.action().getCreds();
    builder.destroy();

    KerberosTicket ticket = Krb5Util.credsToTicket(creds);
    System.out.println("Ends " + ticket.getEndTime().getTime());
    return ticket;
}
 
Example #21
Source File: Krb5LoginModule.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #22
Source File: Krb5LoginModule.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private Credentials renewCredentials(Credentials creds)
{
    Credentials lcreds;
    try {
        if (!creds.isRenewable())
            throw new RefreshFailedException("This ticket" +
                            " is not renewable");
        if (System.currentTimeMillis() > cred.getRenewTill().getTime())
            throw new RefreshFailedException("This ticket is past "
                                         + "its last renewal time.");
        lcreds = creds.renew();
        if (debug)
            System.out.println("Renewed Kerberos Ticket");
    } catch (Exception e) {
        lcreds = null;
        if (debug)
            System.out.println("Ticket could not be renewed : "
                            + e.getMessage());
    }
    return lcreds;
}
 
Example #23
Source File: Context.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Saves the tickets to a ccache file.
 *
 * @param file pathname of the ccache file
 * @return true if created, false otherwise.
 */
public boolean 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();
            return true;
        }
    }
    return false;
}
 
Example #24
Source File: Krb5Util.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #25
Source File: Krb5Util.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #26
Source File: Krb5Util.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static KerberosTicket credsToTicket(Credentials serviceCreds) {
    EncryptionKey sessionKey =  serviceCreds.getSessionKey();
    return new KerberosTicket(
        serviceCreds.getEncoded(),
        new KerberosPrincipal(serviceCreds.getClient().getName()),
        new KerberosPrincipal(serviceCreds.getServer().getName(),
                            KerberosPrincipal.KRB_NT_SRV_INST),
        sessionKey.getBytes(),
        sessionKey.getEType(),
        serviceCreds.getFlags(),
        serviceCreds.getAuthTime(),
        serviceCreds.getStartTime(),
        serviceCreds.getEndTime(),
        serviceCreds.getRenewTill(),
        serviceCreds.getClientAddresses());
}
 
Example #27
Source File: ServiceCreds.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Credentials getInitCred() {
    if (destroyed) {
        throw new IllegalStateException("This object is destroyed");
    }
    if (tgt == null) {
        return null;
    }
    try {
        return Krb5Util.ticketToCreds(tgt);
    } catch (KrbException | IOException e) {
        return null;
    }
}
 
Example #28
Source File: Krb5LoginModule.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isCurrent(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        return (System.currentTimeMillis() <= endTime.getTime());
    }
    return true;
}
 
Example #29
Source File: ServiceCreds.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Credentials getInitCred() {
    if (destroyed) {
        throw new IllegalStateException("This object is destroyed");
    }
    if (tgt == null) {
        return null;
    }
    try {
        return Krb5Util.ticketToCreds(tgt);
    } catch (KrbException | IOException e) {
        return null;
    }
}
 
Example #30
Source File: Krb5LoginModule.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isCurrent(Credentials creds)
{
    Date endTime = creds.getEndTime();
    if (endTime != null) {
        return (System.currentTimeMillis() <= endTime.getTime());
    }
    return true;
}