You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/01/22 22:47:59 UTC
[20/45] directory-kerberos git commit: DIRKRB-149 New layout
structure with the new name "Apache Kerby"
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kdc/src/test/java/org/apache/kerby/kerberos/kdc/server/KdcTest.java
----------------------------------------------------------------------
diff --git a/kerby-kdc/src/test/java/org/apache/kerby/kerberos/kdc/server/KdcTest.java b/kerby-kdc/src/test/java/org/apache/kerby/kerberos/kdc/server/KdcTest.java
new file mode 100644
index 0000000..e124e17
--- /dev/null
+++ b/kerby-kdc/src/test/java/org/apache/kerby/kerberos/kdc/server/KdcTest.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kdc.server;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+
+public class KdcTest {
+
+ private String serverHost = "localhost";
+ private short serverPort = 8088;
+
+ private ApacheKdcServer kdcServer;
+
+ @Before
+ public void setUp() throws Exception {
+ kdcServer = new ApacheKdcServer();
+ kdcServer.setKdcHost(serverHost);
+ kdcServer.setKdcTcpPort(serverPort);
+ kdcServer.init();
+ kdcServer.start();
+ }
+
+ @Test
+ public void testKdc() throws IOException, InterruptedException {
+ Thread.sleep(10);
+
+ SocketChannel socketChannel = SocketChannel.open();
+ socketChannel.configureBlocking(true);
+ SocketAddress sa = new InetSocketAddress(serverHost, serverPort);
+ socketChannel.connect(sa);
+
+ String BAD_KRB_MESSAGE = "Hello World!";
+ ByteBuffer writeBuffer = ByteBuffer.allocate(4 + BAD_KRB_MESSAGE.getBytes().length);
+ writeBuffer.putInt(BAD_KRB_MESSAGE.getBytes().length);
+ writeBuffer.put(BAD_KRB_MESSAGE.getBytes());
+ writeBuffer.flip();
+
+ socketChannel.write(writeBuffer);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ kdcServer.stop();
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/README
----------------------------------------------------------------------
diff --git a/kerby-kerb/README b/kerby-kerb/README
new file mode 100644
index 0000000..e0a1507
--- /dev/null
+++ b/kerby-kerb/README
@@ -0,0 +1,2 @@
+A Kerberos protocol and standards implementation with least dependencies (only relying on JCE).
+The provided APIs and facilities can be used as embedded.
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/pom.xml b/kerby-kerb/kerb-client/pom.xml
new file mode 100644
index 0000000..c4d1290
--- /dev/null
+++ b/kerby-kerb/kerb-client/pom.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. See accompanying LICENSE file.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerby-kerb</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>kerb-client</artifactId>
+ <name>Kerby-kerb Client</name>
+ <description>Kerby-kerb Client</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerby-config</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerb-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerb-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerby-event</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerby-pkix</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.kerby</groupId>
+ <artifactId>kerby-token</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
new file mode 100644
index 0000000..55e9b60
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
@@ -0,0 +1,321 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.event.Event;
+import org.apache.kerby.event.EventHub;
+import org.apache.kerby.event.EventWaiter;
+import org.apache.kerby.kerberos.kerb.KrbErrorCode;
+import org.apache.kerby.kerberos.kerb.client.event.KrbClientEvent;
+import org.apache.kerby.kerberos.kerb.client.event.KrbClientEventType;
+import org.apache.kerby.kerberos.kerb.client.request.*;
+import org.apache.kerby.kerberos.kerb.common.KrbErrorUtil;
+import org.apache.kerby.kerberos.kerb.common.KrbStreamingDecoder;
+import org.apache.kerby.kerberos.kerb.KrbErrorException;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.KrbError;
+import org.apache.kerby.kerberos.kerb.spec.common.PrincipalName;
+import org.apache.kerby.kerberos.kerb.spec.ticket.ServiceTicket;
+import org.apache.kerby.kerberos.kerb.spec.ticket.TgtTicket;
+import org.apache.kerby.token.KerbToken;
+import org.apache.kerby.transport.Connector;
+import org.apache.kerby.transport.Transport;
+import org.apache.kerby.transport.event.TransportEvent;
+import org.apache.kerby.transport.event.TransportEventType;
+import org.apache.kerby.transport.tcp.TcpConnector;
+
+import java.io.IOException;
+import java.security.PrivateKey;
+import java.security.cert.Certificate;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A krb client API for applications to interact with KDC
+ */
+public class KrbClient {
+
+ private EventHub eventHub;
+ private EventWaiter eventWaiter;
+ private Transport transport;
+
+ private KrbHandler krbHandler;
+ private KrbContext context;
+ private KrbConfig config;
+
+ /**
+ *
+ * @param kdcHost
+ * @param kdcPort
+ */
+ public KrbClient(String kdcHost, short kdcPort) {
+ this(new KrbConfig());
+
+ setKdcHost(kdcHost);
+ setKdcPort(kdcPort);
+ }
+
+ public KrbClient(KrbConfig config) {
+ this.config = config;
+ this.context = new KrbContext();
+ context.init(config);
+ }
+
+ /**
+ * Set KDC realm for ticket request
+ * @param realm
+ */
+ public void setKdcRealm(String realm) {
+ context.setKdcRealm(realm);
+ }
+
+ /**
+ *
+ * @param kdcHost
+ */
+ public void setKdcHost(String kdcHost) {
+ context.setKdcHost(kdcHost);
+ }
+
+ /**
+ *
+ * @param kdcPort
+ */
+ public void setKdcPort(short kdcPort) {
+ context.setKdcPort(kdcPort);
+ }
+
+ /**
+ * Set time out for connection
+ * @param timeout in seconds
+ */
+ public void setTimeout(long timeout) {
+ context.setTimeout(timeout);
+ }
+
+ public void init() {
+ this.krbHandler = new KrbHandler();
+ krbHandler.init(context);
+
+ this.eventHub = new EventHub();
+ eventHub.register(krbHandler);
+
+ Connector connector = new TcpConnector(new KrbStreamingDecoder());
+ eventHub.register(connector);
+
+ eventWaiter = eventHub.waitEvent(
+ TransportEventType.NEW_TRANSPORT,
+ KrbClientEventType.TGT_RESULT,
+ KrbClientEventType.TKT_RESULT
+ );
+
+ eventHub.start();
+
+ connector.connect(context.getKdcHost(), context.getKdcPort());
+ Event event = eventWaiter.waitEvent(TransportEventType.NEW_TRANSPORT);
+ transport = ((TransportEvent) event).getTransport();
+ }
+
+ /**
+ * Attempt to request a TGT and you'll be prompted to input a credential.
+ * Whatever credential requested to provide depends on KDC admin configuration.
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public TgtTicket requestTgtTicket(String principal, KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ AsRequest asRequest = new AsRequest(context);
+ asRequest.setKrbOptions(options);
+ return requestTgtTicket(principal, asRequest);
+ }
+
+ /**
+ * Request a TGT with user plain credential
+ * @param principal
+ * @param password
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public TgtTicket requestTgtTicket(String principal, String password,
+ KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ AsRequest asRequest = new AsRequestWithPasswd(context);
+ options.add(KrbOption.USER_PASSWD, password);
+ asRequest.setKrbOptions(options);
+ return requestTgtTicket(principal, asRequest);
+ }
+
+ /**
+ * Request a TGT with user x509 certificate credential
+ * @param principal
+ * @param certificate
+ * @param privateKey
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public TgtTicket requestTgtTicket(String principal, Certificate certificate,
+ PrivateKey privateKey, KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ AsRequestWithCert asRequest = new AsRequestWithCert(context);
+ options.add(KrbOption.PKINIT_X509_CERTIFICATE, certificate);
+ options.add(KrbOption.PKINIT_X509_PRIVATE_KEY, privateKey);
+ asRequest.setKrbOptions(options);
+ return requestTgtTicket(principal, asRequest);
+ }
+
+ /**
+ * Request a TGT with using Anonymous PKINIT
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public TgtTicket requestTgtTicket(KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ AsRequestWithCert asRequest = new AsRequestWithCert(context);
+ options.add(KrbOption.PKINIT_X509_ANONYMOUS);
+ asRequest.setKrbOptions(options);
+
+ String principal = AsRequestWithCert.ANONYMOUS_PRINCIPAL;
+ return requestTgtTicket(principal, asRequest);
+ }
+
+ /**
+ * Request a TGT with user token credential
+ * @param principal
+ * @param token
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public TgtTicket requestTgtTicket(String principal, KerbToken token,
+ KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ AsRequestWithToken asRequest = new AsRequestWithToken(context);
+ options.add(KrbOption.TOKEN_USER_ID_TOKEN, token);
+ asRequest.setKrbOptions(options);
+ return requestTgtTicket(principal, asRequest);
+ }
+
+ /**
+ * Request a service ticket targeting for a server with user plain credentials
+ * @param clientPrincipal
+ * @param password
+ * @param serverPrincipal
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public ServiceTicket requestServiceTicket(String clientPrincipal, String password,
+ String serverPrincipal, KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ TgtTicket tgt = requestTgtTicket(clientPrincipal, password, options);
+ return requestServiceTicket(tgt, serverPrincipal, options);
+ }
+
+ /**
+ * Request a service ticket targeting for a server with an user Access Token
+ * @param clientPrincipal
+ * @param token
+ * @param serverPrincipal
+ * @param options
+ * @return
+ * @throws KrbException
+ */
+ public ServiceTicket requestServiceTicket(String clientPrincipal, KerbToken token,
+ String serverPrincipal, KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ TgtTicket tgt = requestTgtTicket(clientPrincipal, token, options);
+ return requestServiceTicket(tgt, serverPrincipal, options);
+ }
+
+ private TgtTicket requestTgtTicket(String clientPrincipal, AsRequest tgtTktReq) throws KrbException {
+ tgtTktReq.setClientPrincipal(new PrincipalName(clientPrincipal));
+ tgtTktReq.setTransport(transport);
+
+ try {
+ return doRequestTgtTicket(tgtTktReq);
+ } catch(KrbErrorException e) {
+ KrbError krbError = e.getKrbError();
+ if (krbError.getErrorCode() == KrbErrorCode.KDC_ERR_PREAUTH_REQUIRED) {
+ try {
+ tgtTktReq.setEncryptionTypes(KrbErrorUtil.getEtypes(krbError));
+ } catch (IOException ioe) {
+ throw new KrbException("Failed to decode and get etypes from krbError", ioe);
+ }
+ tgtTktReq.getPreauthContext().setPreauthRequired(true);
+ return requestTgtTicket(clientPrincipal, tgtTktReq);
+ }
+ throw e;
+ }
+ }
+
+ private TgtTicket doRequestTgtTicket(AsRequest tgtTktReq) throws KrbException {
+ eventHub.dispatch(KrbClientEvent.createTgtIntentEvent(tgtTktReq));
+ Event resultEvent = null;
+ try {
+ resultEvent = eventWaiter.waitEvent(KrbClientEventType.TGT_RESULT,
+ context.getTimeout(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) {
+ throw new KrbException("Network timeout", e);
+ }
+ AsRequest asResponse = (AsRequest) resultEvent.getEventData();
+
+ return asResponse.getTicket();
+ }
+
+ /**
+ * Request a service ticket with a TGT targeting for a server
+ * @param tgt
+ * @param serverPrincipal
+ * @return
+ * @throws KrbException
+ */
+ public ServiceTicket requestServiceTicket(TgtTicket tgt, String serverPrincipal,
+ KrbOptions options) throws KrbException {
+ if (options == null) options = new KrbOptions();
+
+ TgsRequest ticketReq = new TgsRequest(context, tgt);
+ ticketReq.setServerPrincipal(new PrincipalName(serverPrincipal));
+ ticketReq.setTransport(transport);
+
+ eventHub.dispatch(KrbClientEvent.createTktIntentEvent(ticketReq));
+ Event resultEvent = null;
+ try {
+ resultEvent = eventWaiter.waitEvent(KrbClientEventType.TKT_RESULT,
+ context.getTimeout(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) {
+ throw new KrbException("Network timeout", e);
+ }
+ TgsRequest tgsResponse = (TgsRequest) resultEvent.getEventData();
+
+ return tgsResponse.getServiceTicket();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
new file mode 100644
index 0000000..e523c12
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
@@ -0,0 +1,161 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.config.Conf;
+import org.apache.kerby.kerberos.kerb.common.KrbConfHelper;
+import org.apache.kerby.kerberos.kerb.spec.common.EncryptionType;
+
+import java.util.List;
+
+public class KrbConfig {
+ protected Conf conf;
+
+ public KrbConfig() {
+ this.conf = new Conf();
+ }
+
+ public Conf getConf() {
+ return this.conf;
+ }
+
+ public boolean enableDebug() {
+ return conf.getBoolean(KrbConfigKey.KRB_DEBUG);
+ }
+
+ public String getKdcHost() {
+ return conf.getString(KrbConfigKey.KDC_HOST);
+ }
+
+ public short getKdcPort() {
+ Integer kdcPort = conf.getInt(KrbConfigKey.KDC_PORT);
+ return kdcPort.shortValue();
+ }
+
+ public String getKdcRealm() {
+ return conf.getString(KrbConfigKey.KDC_REALM);
+ }
+
+ public String getKdcDomain() {
+ return conf.getString(KrbConfigKey.KDC_DOMAIN);
+ }
+
+ public boolean isPreauthRequired() {
+ return conf.getBoolean(KrbConfigKey.PREAUTH_REQUIRED);
+ }
+
+ public String getTgsPrincipal() {
+ return conf.getString(KrbConfigKey.TGS_PRINCIPAL);
+ }
+
+ public long getAllowableClockSkew() {
+ return KrbConfHelper.getLongUnderSection(conf, KrbConfigKey.CLOCKSKEW);
+ }
+
+ public boolean isEmptyAddressesAllowed() {
+ return conf.getBoolean(KrbConfigKey.EMPTY_ADDRESSES_ALLOWED);
+ }
+
+ public boolean isForwardableAllowed() {
+ return KrbConfHelper.getBooleanUnderSection(conf, KrbConfigKey.FORWARDABLE);
+ }
+
+ public boolean isPostdatedAllowed() {
+ return conf.getBoolean(KrbConfigKey.POSTDATED_ALLOWED);
+ }
+
+ public boolean isProxiableAllowed() {
+ return KrbConfHelper.getBooleanUnderSection(conf, KrbConfigKey.PROXIABLE);
+ }
+
+ public boolean isRenewableAllowed() {
+ return conf.getBoolean(KrbConfigKey.RENEWABLE_ALLOWED);
+ }
+
+ public long getMaximumRenewableLifetime() {
+ return conf.getLong(KrbConfigKey.MAXIMUM_RENEWABLE_LIFETIME);
+ }
+
+ public long getMaximumTicketLifetime() {
+ return conf.getLong(KrbConfigKey.MAXIMUM_TICKET_LIFETIME);
+ }
+
+ public long getMinimumTicketLifetime() {
+ return conf.getLong(KrbConfigKey.MINIMUM_TICKET_LIFETIME);
+ }
+
+ public List<EncryptionType> getEncryptionTypes() {
+ return KrbConfHelper.getEncTypesUnderSection(conf, KrbConfigKey.PERMITTED_ENCTYPES);
+ }
+
+ public boolean isPaEncTimestampRequired() {
+ return conf.getBoolean(KrbConfigKey.PA_ENC_TIMESTAMP_REQUIRED);
+ }
+
+ public boolean isBodyChecksumVerified() {
+ return conf.getBoolean(KrbConfigKey.VERIFY_BODY_CHECKSUM);
+ }
+
+ public String getDefaultRealm() {
+ return KrbConfHelper.getStringUnderSection(conf, KrbConfigKey.DEFAULT_REALM);
+ }
+
+ public boolean getDnsLookUpKdc() {
+ return KrbConfHelper.getBooleanUnderSection(conf, KrbConfigKey.DNS_LOOKUP_KDC);
+ }
+
+ public boolean getDnsLookUpRealm() {
+ return KrbConfHelper.getBooleanUnderSection(conf, KrbConfigKey.DNS_LOOKUP_REALM);
+ }
+
+ public boolean getAllowWeakCrypto() {
+ return KrbConfHelper.getBooleanUnderSection(conf, KrbConfigKey.ALLOW_WEAK_CRYPTO);
+ }
+
+ public long getTicketLifetime() {
+ return KrbConfHelper.getLongUnderSection(conf, KrbConfigKey.TICKET_LIFETIME);
+ }
+
+ public long getRenewLifetime() {
+ return KrbConfHelper.getLongUnderSection(conf, KrbConfigKey.RENEW_LIFETIME);
+ }
+
+ public List<EncryptionType> getDefaultTgsEnctypes() {
+ return KrbConfHelper.getEncTypesUnderSection(conf, KrbConfigKey.DEFAULT_TGS_ENCTYPES);
+ }
+
+ public List<EncryptionType> getDefaultTktEnctypes() {
+ return KrbConfHelper.getEncTypesUnderSection(conf, KrbConfigKey.DEFAULT_TKT_ENCTYPES);
+ }
+
+ public String getDefaultLoggingLocation() {
+ return KrbConfHelper.getStringUnderSection(conf, KrbConfigKey.DEFAULT);
+ }
+
+ public String getKdcLoggingLocation() {
+ return KrbConfHelper.getStringUnderSection(conf, KrbConfigKey.KDC);
+ }
+
+ public String getAdminLoggingLocation() {
+ return KrbConfHelper.getStringUnderSection(conf, KrbConfigKey.ADMIN_SERVER);
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
new file mode 100644
index 0000000..111cc67
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.kerberos.kerb.common.SectionConfigKey;
+
+public enum KrbConfigKey implements SectionConfigKey {
+ KRB_DEBUG(true),
+ KDC_HOST("localhost"),
+ KDC_PORT(8015),
+ KDC_DOMAIN("example.com"),
+ KDC_REALM("EXAMPLE.COM"),
+ TGS_PRINCIPAL("krbtgt@EXAMPLE.COM"),
+ PREAUTH_REQUIRED(true),
+ CLOCKSKEW(5 * 60L, "libdefaults"),
+ EMPTY_ADDRESSES_ALLOWED(true),
+ PA_ENC_TIMESTAMP_REQUIRED(true),
+ MAXIMUM_TICKET_LIFETIME(24 * 3600L),
+ MINIMUM_TICKET_LIFETIME(1 * 3600L),
+ MAXIMUM_RENEWABLE_LIFETIME(48 * 3600L),
+ FORWARDABLE(true, "libdefaults"),
+ POSTDATED_ALLOWED(true),
+ PROXIABLE(true, "libdefaults"),
+ RENEWABLE_ALLOWED(true),
+ VERIFY_BODY_CHECKSUM(true),
+ PERMITTED_ENCTYPES("aes128-cts-hmac-sha1-96", "libdefaults"),
+ DEFAULT_REALM("EXAMPLE.COM", "libdefaults"),
+ DNS_LOOKUP_KDC(false, "libdefaults"),
+ DNS_LOOKUP_REALM(false, "libdefaults"),
+ ALLOW_WEAK_CRYPTO(true, "libdefaults"),
+ TICKET_LIFETIME(24 * 3600L, "libdefaults"),
+ RENEW_LIFETIME(48 * 3600L, "libdefaults"),
+ DEFAULT_TGS_ENCTYPES("aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 " +
+ "des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac " +
+ "camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4",
+ "libdefaults"),
+ DEFAULT_TKT_ENCTYPES("aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 " +
+ "des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac " +
+ "camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4",
+ "libdefaults"),
+
+ //key for logging location
+ DEFAULT(null, "logging"),
+ KDC(null, "logging"),
+ ADMIN_SERVER(null, "logging");
+
+ private Object defaultValue;
+ /**
+ * The name of a section where a config key is contained in MIT Kerberos config file.
+ */
+ private String sectionName;
+
+ private KrbConfigKey() {
+ this.defaultValue = null;
+ }
+
+ private KrbConfigKey(Object defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
+ private KrbConfigKey(Object defaultValue, String sectionName) {
+ this(defaultValue);
+ this.sectionName = sectionName;
+ }
+
+ /**
+ * Use the propertyKey, we can get the configuration value from Object Conf.
+ */
+ @Override
+ public String getPropertyKey() {
+ return name().toLowerCase();
+ }
+
+ @Override
+ public Object getDefaultValue() {
+ return this.defaultValue;
+ }
+
+ @Override
+ public String getSectionName() {
+ return sectionName;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
new file mode 100644
index 0000000..279c486
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.kerberos.kerb.client.preauth.PreauthHandler;
+import org.apache.kerby.kerberos.kerb.crypto.Nonce;
+
+public class KrbContext {
+
+ private String kdcRealm;
+ private KrbConfig config;
+ private String kdcHost;
+ private short kdcPort;
+ private long timeout = 10L;
+ private PreauthHandler preauthHandler;
+
+ public void init(KrbConfig config) {
+ this.config = config;
+ preauthHandler = new PreauthHandler();
+ preauthHandler.init(this);
+ }
+
+ public String getKdcHost() {
+ if (kdcHost != null) {
+ return kdcHost;
+ }
+ return config.getKdcHost();
+ }
+
+ public void setKdcHost(String kdcHost) {
+ this.kdcHost = kdcHost;
+ }
+
+ public short getKdcPort() {
+ if (kdcPort > 0) {
+ return kdcPort;
+ }
+ return config.getKdcPort();
+ }
+
+ public void setKdcPort(short kdcPort) {
+ this.kdcPort = kdcPort;
+ }
+
+ public void setTimeout(long timeout) {
+ this.timeout = timeout;
+ }
+
+ public long getTimeout() {
+ return this.timeout;
+ }
+
+ public KrbConfig getConfig() {
+ return config;
+ }
+
+ public void setKdcRealm(String realm) {
+ this.kdcRealm = realm;
+ }
+
+ public String getKdcRealm() {
+ if (kdcRealm != null) {
+ return kdcRealm;
+ }
+
+ return config.getKdcRealm();
+ }
+
+ public int generateNonce() {
+ return Nonce.value();
+ }
+
+ public long getTicketValidTime() {
+ return 8 * 60 * 60 * 1000;
+ }
+
+ public PreauthHandler getPreauthHandler() {
+ return preauthHandler;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbHandler.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbHandler.java
new file mode 100644
index 0000000..085ee57
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbHandler.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.event.AbstractEventHandler;
+import org.apache.kerby.event.Event;
+import org.apache.kerby.event.EventType;
+import org.apache.kerby.kerberos.kerb.client.event.KrbClientEvent;
+import org.apache.kerby.kerberos.kerb.client.event.KrbClientEventType;
+import org.apache.kerby.kerberos.kerb.client.preauth.PreauthHandler;
+import org.apache.kerby.kerberos.kerb.client.request.AsRequest;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+import org.apache.kerby.kerberos.kerb.common.KrbUtil;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.KrbMessage;
+import org.apache.kerby.kerberos.kerb.spec.common.KrbMessageType;
+import org.apache.kerby.kerberos.kerb.spec.kdc.KdcRep;
+import org.apache.kerby.kerberos.kerb.spec.kdc.KdcReq;
+import org.apache.kerby.transport.Transport;
+import org.apache.kerby.transport.event.MessageEvent;
+import org.apache.kerby.transport.event.TransportEventType;
+
+import java.nio.ByteBuffer;
+
+public class KrbHandler extends AbstractEventHandler {
+
+ private KrbContext context;
+ private PreauthHandler preauthHandler;
+
+ public void init(KrbContext context) {
+ this.context = context;
+ preauthHandler = new PreauthHandler();
+ preauthHandler.init(context);
+ }
+
+ @Override
+ public EventType[] getInterestedEvents() {
+ return new EventType[] {
+ TransportEventType.INBOUND_MESSAGE,
+ KrbClientEventType.TGT_INTENT,
+ KrbClientEventType.TKT_INTENT
+ };
+ }
+
+ @Override
+ protected void doHandle(Event event) throws Exception {
+ EventType eventType = event.getEventType();
+
+ if (eventType == KrbClientEventType.TGT_INTENT ||
+ eventType == KrbClientEventType.TKT_INTENT) {
+ KdcRequest kdcRequest = (KdcRequest) event.getEventData();
+ handleKdcRequest(kdcRequest);
+ } else if (event.getEventType() == TransportEventType.INBOUND_MESSAGE) {
+ handleMessage((MessageEvent) event);
+ }
+ }
+
+ protected void handleKdcRequest(KdcRequest kdcRequest) throws KrbException {
+ kdcRequest.process();
+ KdcReq kdcReq = kdcRequest.getKdcReq();
+ Transport transport = kdcRequest.getTransport();
+ transport.setAttachment(kdcRequest);
+ KrbUtil.sendMessage(kdcReq, transport);
+ }
+
+ protected void handleMessage(MessageEvent event) throws Exception {
+ ByteBuffer message = event.getMessage();
+ KrbMessage kdcRep = KrbUtil.decodeMessage(message);
+
+ KrbMessageType messageType = kdcRep.getMsgType();
+ if (messageType == KrbMessageType.AS_REP) {
+ KdcRequest kdcRequest = (KdcRequest) event.getTransport().getAttachment();
+ kdcRequest.processResponse((KdcRep) kdcRep);
+ dispatch(KrbClientEvent.createTgtResultEvent((AsRequest) kdcRequest));
+ } else if (messageType == KrbMessageType.TGS_REP) {
+ KdcRequest kdcRequest = (KdcRequest) event.getTransport().getAttachment();
+ kdcRequest.processResponse((KdcRep) kdcRep);
+ dispatch(KrbClientEvent.createTktResultEvent((TgsRequest) kdcRequest));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
new file mode 100644
index 0000000..64d0d56
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+public enum KrbOption {
+ LIFE_TIME("-l lifetime"),
+ START_TIME("-s start time"),
+ RENEWABLE_TIME("-r renewable lifetime"),
+ FORWARDABLE("-f forwardable"),
+ NOT_FORWARDABLE("-F not forwardable"),
+ PROXIABLE("-p proxiable"),
+ NOT_PROXIABLE("-P not proxiable"),
+ ANONYMOUS("-n anonymous"),
+ INCLUDE_ADDRESSES("-a include addresses"),
+ NOT_INCLUDE_ADDRESSES("-A do not include addresses"),
+ VALIDATE("-v validate"),
+ RENEW("-R renew"),
+ CANONICALIZE("-C canonicalize"),
+ AS_ENTERPRISE_PN("-E client is enterprise principal name"),
+ USE_KEYTAB("-k use keytab"),
+ USE_DFT_KEYTAB("-i use default client keytab (with -k)"),
+ USER_KEYTAB_FILE("-t filename of keytab to use"),
+ KRB5_CACHE("-c Kerberos 5 cache name"),
+ SERVICE("-S service"),
+ ARMOR_CACHE("-T armor credential cache"),
+ XATTR("-X <attribute>[=<value>]"),
+
+ USER_PASSWD("user_passwd", "User plain password"),
+
+ PKINIT_X509_IDENTITY("x509_identities", "X509 user private key and cert"),
+ PKINIT_X509_PRIVATE_KEY("x509_privatekey", "X509 user private key"),
+ PKINIT_X509_CERTIFICATE("x509_cert", "X509 user certificate"),
+ PKINIT_X509_ANCHORS("x509_anchors", "X509 anchors"),
+ PKINIT_X509_ANONYMOUS("x509_anonymous", "X509 anonymous"),
+ PKINIT_USING_RSA("using_rsa_or_dh", "Using RSA or DH"),
+
+ TOKEN_USING_IDTOKEN("using_id_token", "Using identity token"),
+ TOKEN_USER_ID_TOKEN("user_id_token", "User identity token"),
+ TOKEN_USER_AC_TOKEN("user_ac_token", "User access token"),
+
+ ;
+
+ private String name;
+ private String description;
+ private Object value;
+
+ KrbOption(String description) {
+ this.description = description;
+ }
+
+ KrbOption(String name, String description) {
+ this.name = name;
+ this.description = description;
+ }
+
+ public String getName() {
+ if (name != null) {
+ return name;
+ }
+ return name();
+ }
+
+ public String getDescription() {
+ return this.description;
+ }
+
+ public void setValue(Object value) {
+ this.value = value;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOptions.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOptions.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOptions.java
new file mode 100644
index 0000000..06bd361
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOptions.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class KrbOptions {
+
+ private Map<KrbOption, KrbOption> options = new HashMap<KrbOption, KrbOption>(4);
+
+ public void add(KrbOption option) {
+ if (option != null) {
+ options.put(option, option);
+ }
+ }
+
+ public void add(KrbOption option, Object optionValue) {
+ option.setValue(optionValue);
+ add(option);
+ }
+
+ public boolean contains(KrbOption option) {
+ return options.containsKey(option);
+ }
+
+ public KrbOption getOption(KrbOption option) {
+ if (! options.containsKey(option)) {
+ return null;
+ }
+
+ return options.get(option);
+ }
+
+ public Object getOptionValue(KrbOption option) {
+ if (! contains(option)) {
+ return null;
+ }
+ return options.get(option).getValue();
+ }
+
+ public String getStringOption(KrbOption option) {
+ Object value = getOptionValue(option);
+ if (value != null && value instanceof String) {
+ return (String) value;
+ }
+ return null;
+ }
+
+ public boolean getBooleanOption(KrbOption option) {
+ Object value = getOptionValue(option);
+ if (value != null) {
+ if (value instanceof String) {
+ String strVal = (String) value;
+ if (strVal.equalsIgnoreCase("true") ||
+ strVal.equalsIgnoreCase("yes") ||
+ strVal.equals("1")) {
+ return true;
+ }
+ } else if (value instanceof Boolean) {
+ return (Boolean) value;
+ }
+ }
+ return false;
+ }
+
+ public int getIntegerOption(KrbOption option) {
+ Object value = getOptionValue(option);
+ if (value != null) {
+ if (value instanceof String) {
+ String strVal = (String) value;
+ return Integer.valueOf(strVal);
+ } else if (value instanceof Integer) {
+ return (Integer) value;
+ }
+ }
+ return -1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEvent.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEvent.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEvent.java
new file mode 100644
index 0000000..291b33e
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEvent.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.event;
+
+import org.apache.kerby.event.Event;
+import org.apache.kerby.kerberos.kerb.client.request.AsRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+
+public class KrbClientEvent {
+
+ public static Event createTgtIntentEvent(AsRequest asRequest) {
+ return new Event(KrbClientEventType.TGT_INTENT, asRequest);
+ }
+
+ public static Event createTktIntentEvent(TgsRequest tgsRequest) {
+ return new Event(KrbClientEventType.TKT_INTENT, tgsRequest);
+ }
+
+ public static Event createTgtResultEvent(AsRequest asRequest) {
+ return new Event(KrbClientEventType.TGT_RESULT, asRequest);
+ }
+
+ public static Event createTktResultEvent(TgsRequest tgsRequest) {
+ return new Event(KrbClientEventType.TKT_RESULT, tgsRequest);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEventType.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEventType.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEventType.java
new file mode 100644
index 0000000..78fd424
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/event/KrbClientEventType.java
@@ -0,0 +1,29 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.event;
+
+import org.apache.kerby.event.EventType;
+
+public enum KrbClientEventType implements EventType {
+ TGT_INTENT,
+ TGT_RESULT,
+ TKT_INTENT,
+ TKT_RESULT
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/AbstractPreauthPlugin.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/AbstractPreauthPlugin.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/AbstractPreauthPlugin.java
new file mode 100644
index 0000000..09f5c58
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/AbstractPreauthPlugin.java
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.client.KrbContext;
+import org.apache.kerby.kerberos.kerb.client.KrbOptions;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlag;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlags;
+import org.apache.kerby.kerberos.kerb.preauth.PluginRequestContext;
+import org.apache.kerby.kerberos.kerb.preauth.PreauthPluginMeta;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.EncryptionType;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+import java.util.Collections;
+import java.util.List;
+
+public class AbstractPreauthPlugin implements KrbPreauth {
+
+ private PreauthPluginMeta pluginMeta;
+ protected KrbContext context;
+
+ public AbstractPreauthPlugin(PreauthPluginMeta meta) {
+ this.pluginMeta = meta;
+ }
+
+ @Override
+ public String getName() {
+ return pluginMeta.getName();
+ }
+
+ public int getVersion() {
+ return pluginMeta.getVersion();
+ }
+
+ public PaDataType[] getPaTypes() {
+ return pluginMeta.getPaTypes();
+ }
+
+ public void init(KrbContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public PluginRequestContext initRequestContext(KdcRequest kdcRequest) {
+ return null;
+ }
+
+ @Override
+ public void prepareQuestions(KdcRequest kdcRequest,
+ PluginRequestContext requestContext) throws KrbException {
+
+ kdcRequest.needAsKey();
+ }
+
+ @Override
+ public List<EncryptionType> getEncTypes(KdcRequest kdcRequest,
+ PluginRequestContext requestContext) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public void setPreauthOptions(KdcRequest kdcRequest,
+ PluginRequestContext requestContext, KrbOptions options) {
+
+ }
+
+ public void tryFirst(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaData outPadata) throws KrbException {
+
+ }
+
+ @Override
+ public boolean process(KdcRequest kdcRequest,
+ PluginRequestContext requestContext, PaDataEntry inPadata,
+ PaData outPadata) throws KrbException {
+
+ return false;
+ }
+
+ @Override
+ public boolean tryAgain(KdcRequest kdcRequest,
+ PluginRequestContext requestContext, PaDataType preauthType,
+ PaData errPadata, PaData outPadata) {
+ return false;
+ }
+
+ @Override
+ public PaFlags getFlags(PaDataType paType) {
+ PaFlags paFlags = new PaFlags(0);
+ paFlags.setFlag(PaFlag.PA_REAL);
+
+ return paFlags;
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/FastContext.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/FastContext.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/FastContext.java
new file mode 100644
index 0000000..88f7f51
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/FastContext.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.spec.common.EncryptionKey;
+import org.apache.kerby.kerberos.kerb.spec.fast.FastOptions;
+import org.apache.kerby.kerberos.kerb.spec.fast.KrbFastArmor;
+import org.apache.kerby.kerberos.kerb.spec.kdc.KdcReq;
+
+public class FastContext {
+
+ public KdcReq fastOuterRequest;
+ public EncryptionKey armorKey;
+ public KrbFastArmor fastArmor;
+ public FastOptions fastOptions;
+ public int nonce;
+ public int fastFlags;
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/KrbPreauth.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/KrbPreauth.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/KrbPreauth.java
new file mode 100644
index 0000000..2797ca4
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/KrbPreauth.java
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.client.KrbContext;
+import org.apache.kerby.kerberos.kerb.client.KrbOptions;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlags;
+import org.apache.kerby.kerberos.kerb.preauth.PluginRequestContext;
+import org.apache.kerby.kerberos.kerb.preauth.PreauthPluginMeta;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.EncryptionType;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+import java.util.List;
+
+/**
+ * Client side preauth plugin module
+ */
+public interface KrbPreauth extends PreauthPluginMeta {
+
+ /**
+ * Initializing preauth plugin context
+ */
+ public void init(KrbContext krbContext);
+
+ /**
+ * Initializing request context
+ */
+ public PluginRequestContext initRequestContext(KdcRequest kdcRequest);
+
+ /**
+ * Prepare questions to prompt to you asking for credential
+ */
+ public void prepareQuestions(KdcRequest kdcRequest,
+ PluginRequestContext requestContext) throws KrbException;
+
+ /**
+ * Get supported encryption types
+ */
+ public List<EncryptionType> getEncTypes(KdcRequest kdcRequest,
+ PluginRequestContext requestContext);
+
+ /**
+ * Set krb options passed from user
+ */
+ public void setPreauthOptions(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ KrbOptions preauthOptions);
+
+ /**
+ * Attempt to try any initial padata derived from user options
+ */
+ public void tryFirst(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaData outPadata) throws KrbException;
+
+ /**
+ * Process server returned paData and return back any result paData
+ * Return true indicating padata is added
+ */
+ public boolean process(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaDataEntry inPadata,
+ PaData outPadata) throws KrbException;
+
+ /**
+ * When another request to server in the 4 pass, any paData to provide?
+ * Return true indicating padata is added
+ */
+ public boolean tryAgain(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaDataType preauthType,
+ PaData errPadata,
+ PaData outPadata);
+
+ /**
+ * Return PA_REAL if pa_type is a real preauthentication type or PA_INFO if it is
+ * an informational type.
+ */
+ public PaFlags getFlags(PaDataType paType);
+
+ /**
+ * When exiting...
+ */
+ public void destroy();
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthContext.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthContext.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthContext.java
new file mode 100644
index 0000000..aa943e4
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthContext.java
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PreauthContext {
+ private boolean preauthRequired = true;
+ private PaData inputPaData;
+ private PaData outputPaData;
+ private PaData errorPaData;
+ private UserResponser userResponser = new UserResponser();
+ private PaDataType selectedPaType;
+ private PaDataType allowedPaType;
+ private List<PaDataType> triedPaTypes = new ArrayList<PaDataType>(1);
+ private List<PreauthHandle> handles = new ArrayList<PreauthHandle>(5);
+
+ public PreauthContext() {
+ this.selectedPaType = PaDataType.NONE;
+ this.allowedPaType = PaDataType.NONE;
+ this.outputPaData = new PaData();
+ }
+
+ public boolean isPreauthRequired() {
+ return preauthRequired;
+ }
+
+ public void setPreauthRequired(boolean preauthRequired) {
+ this.preauthRequired = preauthRequired;
+ }
+
+ public UserResponser getUserResponser() {
+ return userResponser;
+ }
+
+ public boolean isPaTypeAllowed(PaDataType paType) {
+ return (allowedPaType == PaDataType.NONE ||
+ allowedPaType == paType);
+ }
+
+ public PaData getOutputPaData() throws KrbException {
+ return outputPaData;
+ }
+
+ public boolean hasInputPaData() {
+ return (inputPaData != null && ! inputPaData.isEmpty());
+ }
+
+ public PaData getInputPaData() {
+ return inputPaData;
+ }
+
+ public void setInputPaData(PaData inputPaData) {
+ this.inputPaData = inputPaData;
+ }
+
+ public PaData getErrorPaData() {
+ return errorPaData;
+ }
+
+ public void setErrorPaData(PaData errorPaData) {
+ this.errorPaData = errorPaData;
+ }
+
+ public void setAllowedPaType(PaDataType paType) {
+ this.allowedPaType = paType;
+ }
+
+ public List<PreauthHandle> getHandles() {
+ return handles;
+ }
+
+ public PaDataType getAllowedPaType() {
+ return allowedPaType;
+ }
+
+ public boolean checkAndPutTried(PaDataType paType) {
+ for (PaDataType pt : triedPaTypes) {
+ if (pt == paType) {
+ return true;
+ }
+ }
+ triedPaTypes.add(paType);
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandle.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandle.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandle.java
new file mode 100644
index 0000000..2491fac
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandle.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.client.KrbOptions;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlags;
+import org.apache.kerby.kerberos.kerb.preauth.PluginRequestContext;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+public class PreauthHandle {
+
+ public KrbPreauth preauth;
+ public PluginRequestContext requestContext;
+
+ public PreauthHandle(KrbPreauth preauth) {
+ this.preauth = preauth;
+ }
+
+ public void initRequestContext(KdcRequest kdcRequest) {
+ requestContext = preauth.initRequestContext(kdcRequest);
+ }
+
+ public void prepareQuestions(KdcRequest kdcRequest) throws KrbException {
+ preauth.prepareQuestions(kdcRequest, requestContext);
+ }
+
+ public void setPreauthOptions(KdcRequest kdcRequest,
+ KrbOptions preauthOptions) throws KrbException {
+ preauth.setPreauthOptions(kdcRequest, requestContext, preauthOptions);
+ }
+
+ public void tryFirst(KdcRequest kdcRequest, PaData outPadata) throws KrbException {
+ preauth.tryFirst(kdcRequest, requestContext, outPadata);
+ }
+
+ public boolean process(KdcRequest kdcRequest,
+ PaDataEntry inPadata, PaData outPadata) throws KrbException {
+ return preauth.process(kdcRequest, requestContext, inPadata, outPadata);
+ }
+
+ public boolean tryAgain(KdcRequest kdcRequest,
+ PaDataType paType, PaData errPadata, PaData paData) {
+ return preauth.tryAgain(kdcRequest, requestContext, paType, errPadata, paData);
+ }
+
+ public boolean isReal(PaDataType paType) {
+ PaFlags paFlags = preauth.getFlags(paType);
+ return paFlags.isReal();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandler.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandler.java
new file mode 100644
index 0000000..39fe3a8
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/PreauthHandler.java
@@ -0,0 +1,249 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import org.apache.kerby.kerberos.kerb.client.KrbContext;
+import org.apache.kerby.kerberos.kerb.client.KrbOptions;
+import org.apache.kerby.kerberos.kerb.client.preauth.builtin.EncTsPreauth;
+import org.apache.kerby.kerberos.kerb.client.preauth.builtin.TgtPreauth;
+import org.apache.kerby.kerberos.kerb.client.preauth.pkinit.PkinitPreauth;
+import org.apache.kerby.kerberos.kerb.client.preauth.token.TokenPreauth;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.codec.KrbCodec;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.EtypeInfo;
+import org.apache.kerby.kerberos.kerb.spec.common.EtypeInfo2;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PreauthHandler {
+ private KrbContext krbContext;
+ private List<KrbPreauth> preauths;
+
+ public void init(KrbContext krbContext) {
+ this.krbContext = krbContext;
+ loadPreauthPlugins(krbContext);
+ }
+
+ private void loadPreauthPlugins(KrbContext context) {
+ preauths = new ArrayList<KrbPreauth>();
+
+ KrbPreauth preauth = new EncTsPreauth();
+ preauth.init(context);
+ preauths.add(preauth);
+
+ preauth = new TgtPreauth();
+ preauth.init(context);
+ preauths.add(preauth);
+
+ preauth = new PkinitPreauth();
+ preauth.init(context);
+ preauths.add(preauth);
+
+ preauth = new TokenPreauth();
+ preauth.init(context);
+ preauths.add(preauth);
+ }
+
+ public PreauthContext preparePreauthContext(KdcRequest kdcRequest) {
+ PreauthContext preauthContext = new PreauthContext();
+ preauthContext.setPreauthRequired(krbContext.getConfig().isPreauthRequired());
+ for (KrbPreauth preauth : preauths) {
+ PreauthHandle handle = new PreauthHandle(preauth);
+ handle.initRequestContext(kdcRequest);
+ preauthContext.getHandles().add(handle);
+ }
+
+ return preauthContext;
+ }
+
+ /**
+ * Process preauth inputs and options, prepare and generate pdata to be out
+ */
+ public void preauth(KdcRequest kdcRequest) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ if (!preauthContext.isPreauthRequired()) {
+ return;
+ }
+
+ if (!preauthContext.hasInputPaData()) {
+ tryFirst(kdcRequest, preauthContext.getOutputPaData());
+ return;
+ }
+
+ attemptETypeInfo(kdcRequest, preauthContext.getInputPaData());
+
+ setPreauthOptions(kdcRequest, kdcRequest.getPreauthOptions());
+
+ prepareUserResponses(kdcRequest, preauthContext.getInputPaData());
+
+ preauthContext.getUserResponser().respondQuestions();
+
+ if (!kdcRequest.isRetrying()) {
+ process(kdcRequest, preauthContext.getInputPaData(),
+ preauthContext.getOutputPaData());
+ } else {
+ tryAgain(kdcRequest, preauthContext.getInputPaData(),
+ preauthContext.getOutputPaData());
+ }
+ }
+
+ public void prepareUserResponses(KdcRequest kdcRequest,
+ PaData inPadata) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ for (PaDataEntry pae : inPadata.getElements()) {
+ if (! preauthContext.isPaTypeAllowed(pae.getPaDataType())) {
+ continue;
+ }
+
+ PreauthHandle handle = findHandle(kdcRequest, pae.getPaDataType());
+ if (handle == null) {
+ continue;
+ }
+
+ handle.prepareQuestions(kdcRequest);
+ }
+ }
+
+ public void setPreauthOptions(KdcRequest kdcRequest,
+ KrbOptions preauthOptions) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ for (PreauthHandle handle : preauthContext.getHandles()) {
+ handle.setPreauthOptions(kdcRequest, preauthOptions);
+ }
+ }
+
+ public void tryFirst(KdcRequest kdcRequest,
+ PaData outPadata) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ PreauthHandle handle = findHandle(kdcRequest,
+ preauthContext.getAllowedPaType());
+ handle.tryFirst(kdcRequest, outPadata);
+ }
+
+ public void process(KdcRequest kdcRequest,
+ PaData inPadata, PaData outPadata) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ /**
+ * Process all informational padata types, then the first real preauth type
+ * we succeed on
+ */
+ for (int real = 0; real <= 1; real ++) {
+ for (PaDataEntry pae : inPadata.getElements()) {
+
+ // Restrict real mechanisms to the chosen one if we have one
+ if (real >0 && !preauthContext.isPaTypeAllowed(pae.getPaDataType())) {
+ continue;
+ }
+
+ PreauthHandle handle = findHandle(kdcRequest,
+ preauthContext.getAllowedPaType());
+ if (handle == null) {
+ continue;
+ }
+
+ // Make sure this type is for the current pass
+ int tmpReal = handle.isReal(pae.getPaDataType()) ? 1 : 0;
+ if (tmpReal != real) {
+ continue;
+ }
+
+ if (real > 0 && preauthContext.checkAndPutTried(pae.getPaDataType())) {
+ continue;
+ }
+
+ boolean gotData = handle.process(kdcRequest, pae, outPadata);
+ if (real > 0 && gotData) {
+ return;
+ }
+ }
+ }
+ }
+
+ public void tryAgain(KdcRequest kdcRequest,
+ PaData inPadata, PaData outPadata) {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ PreauthHandle handle;
+ for (PaDataEntry pae : inPadata.getElements()) {
+ handle = findHandle(kdcRequest, pae.getPaDataType());
+ if (handle == null) continue;
+
+ boolean gotData = handle.tryAgain(kdcRequest,
+ pae.getPaDataType(), preauthContext.getErrorPaData(), outPadata);
+ }
+ }
+
+ public void destroy() {
+ for (KrbPreauth preauth : preauths) {
+ preauth.destroy();
+ }
+ }
+
+ private PreauthHandle findHandle(KdcRequest kdcRequest,
+ PaDataType paType) {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ for (PreauthHandle handle : preauthContext.getHandles()) {
+ for (PaDataType pt : handle.preauth.getPaTypes()) {
+ if (pt == paType) {
+ return handle;
+ }
+ }
+ }
+ return null;
+ }
+
+ private void attemptETypeInfo(KdcRequest kdcRequest,
+ PaData inPadata) throws KrbException {
+ PreauthContext preauthContext = kdcRequest.getPreauthContext();
+
+ // Find an etype-info2 or etype-info element in padata
+ EtypeInfo etypeInfo = null;
+ EtypeInfo2 etypeInfo2 = null;
+ PaDataEntry pae = inPadata.findEntry(PaDataType.ETYPE_INFO);
+ if (pae != null) {
+ etypeInfo = KrbCodec.decode(pae.getPaDataValue(), EtypeInfo.class);
+ } else {
+ pae = inPadata.findEntry(PaDataType.ETYPE_INFO2);
+ if (pae != null) {
+ etypeInfo2 = KrbCodec.decode(pae.getPaDataValue(), EtypeInfo2.class);
+ }
+ }
+
+ if (etypeInfo == null && etypeInfo2 == null) {
+ attemptSalt(kdcRequest, inPadata);
+ }
+ }
+
+ private void attemptSalt(KdcRequest kdcRequest,
+ PaData inPadata) throws KrbException {
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponseItem.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponseItem.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponseItem.java
new file mode 100644
index 0000000..bc33cc4
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponseItem.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+public class UserResponseItem {
+ protected String question;
+ protected String challenge;
+ protected String answer;
+
+ public UserResponseItem(String question, String challenge) {
+ this.question = question;
+ this.challenge = challenge;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponser.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponser.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponser.java
new file mode 100644
index 0000000..695e111
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/UserResponser.java
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UserResponser {
+
+ private List<UserResponseItem> items = new ArrayList<UserResponseItem>(1);
+
+ /**
+ * Let customize an interface like CMD or WEB UI to selectively respond all the questions
+ */
+ public void respondQuestions() {
+ // TODO
+ }
+
+ public UserResponseItem findQuestion(String question) {
+ for (UserResponseItem ri : items) {
+ if (ri.question.equals(question)) {
+ return ri;
+ }
+ }
+ return null;
+ }
+
+ public void askQuestion(String question, String challenge) {
+ UserResponseItem ri = findQuestion(question);
+ if (ri == null) {
+ items.add(new UserResponseItem(question, challenge));
+ } else {
+ ri.challenge = challenge;
+ }
+ }
+
+ public String getChallenge(String question) {
+ UserResponseItem ri = findQuestion(question);
+ if (ri != null) {
+ return ri.challenge;
+ }
+ return null;
+ }
+
+ public void setAnswer(String question, String answer) {
+ UserResponseItem ri = findQuestion(question);
+ if (ri == null) {
+ throw new IllegalArgumentException("Question isn't exist for the answer");
+ }
+ ri.answer = answer;
+ }
+
+ public String getAnswer(String question) {
+ UserResponseItem ri = findQuestion(question);
+ if (ri != null) {
+ return ri.answer;
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/EncTsPreauth.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/EncTsPreauth.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/EncTsPreauth.java
new file mode 100644
index 0000000..361e73b
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/EncTsPreauth.java
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth.builtin;
+
+import org.apache.kerby.kerberos.kerb.client.preauth.AbstractPreauthPlugin;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.common.EncryptionUtil;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlag;
+import org.apache.kerby.kerberos.kerb.preauth.PaFlags;
+import org.apache.kerby.kerberos.kerb.preauth.PluginRequestContext;
+import org.apache.kerby.kerberos.kerb.preauth.builtin.EncTsPreauthMeta;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.common.EncryptedData;
+import org.apache.kerby.kerberos.kerb.spec.common.KeyUsage;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaEncTsEnc;
+
+public class EncTsPreauth extends AbstractPreauthPlugin {
+
+ public EncTsPreauth() {
+ super(new EncTsPreauthMeta());
+ }
+
+ @Override
+ public void prepareQuestions(KdcRequest kdcRequest,
+ PluginRequestContext requestContext) throws KrbException {
+
+ kdcRequest.needAsKey();
+ }
+
+ public void tryFirst(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaData outPadata) throws KrbException {
+
+ if (kdcRequest.getAsKey() == null) {
+ kdcRequest.needAsKey();
+ }
+ outPadata.addElement(makeEntry(kdcRequest));
+ }
+
+ @Override
+ public boolean process(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaDataEntry inPadata,
+ PaData outPadata) throws KrbException {
+
+ if (kdcRequest.getAsKey() == null) {
+ kdcRequest.needAsKey();
+ }
+ outPadata.addElement(makeEntry(kdcRequest));
+
+ return true;
+ }
+
+ @Override
+ public PaFlags getFlags(PaDataType paType) {
+ PaFlags paFlags = new PaFlags(0);
+ paFlags.setFlag(PaFlag.PA_REAL);
+
+ return paFlags;
+ }
+
+ private PaDataEntry makeEntry(KdcRequest kdcRequest) throws KrbException {
+ PaEncTsEnc paTs = new PaEncTsEnc();
+ paTs.setPaTimestamp(kdcRequest.getPreauthTime());
+
+ EncryptedData paDataValue = EncryptionUtil.seal(paTs,
+ kdcRequest.getAsKey(), KeyUsage.AS_REQ_PA_ENC_TS);
+ PaDataEntry tsPaEntry = new PaDataEntry();
+ tsPaEntry.setPaDataType(PaDataType.ENC_TIMESTAMP);
+ tsPaEntry.setPaDataValue(paDataValue.encode());
+
+ return tsPaEntry;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/ceacb982/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/TgtPreauth.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/TgtPreauth.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/TgtPreauth.java
new file mode 100644
index 0000000..d5b2e9f
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/preauth/builtin/TgtPreauth.java
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client.preauth.builtin;
+
+import org.apache.kerby.kerberos.kerb.client.preauth.AbstractPreauthPlugin;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+import org.apache.kerby.kerberos.kerb.preauth.PluginRequestContext;
+import org.apache.kerby.kerberos.kerb.preauth.builtin.TgtPreauthMeta;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaData;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataEntry;
+import org.apache.kerby.kerberos.kerb.spec.pa.PaDataType;
+
+public class TgtPreauth extends AbstractPreauthPlugin {
+
+ public TgtPreauth() {
+ super(new TgtPreauthMeta());
+ }
+
+ public void tryFirst(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaData outPadata) throws KrbException {
+
+ outPadata.addElement(makeEntry(kdcRequest));
+ }
+
+ @Override
+ public boolean process(KdcRequest kdcRequest,
+ PluginRequestContext requestContext,
+ PaDataEntry inPadata,
+ PaData outPadata) throws KrbException {
+
+ outPadata.addElement(makeEntry(kdcRequest));
+
+ return true;
+ }
+
+ private PaDataEntry makeEntry(KdcRequest kdcRequest) throws KrbException {
+
+ TgsRequest tgsRequest = (TgsRequest) kdcRequest;
+
+ PaDataEntry paEntry = new PaDataEntry();
+ paEntry.setPaDataType(PaDataType.TGS_REQ);
+ paEntry.setPaDataValue(tgsRequest.getApReq().encode());
+
+ return paEntry;
+ }
+}