From 46354eca223c473f7849b7480aaf6b3a2345e49f Mon Sep 17 00:00:00 2001
From: Jack Magne <jmagne@dhcp-16-213.sjc.redhat.com>
Date: Tue, 10 Sep 2013 12:00:41 -0700
Subject: [PATCH] First cut at Java TPS Buffer class and APDU class.

1. Also simple framework for working with APDU command.
2. Implemented a few APDU commands in TPS_Processor class.
3. Can now attempt a format operation with TPS client.
   The code can performa a few apdu's talking to the client
   and return a success "EndOp" apdu to terminate the conversation.
4. APDU are being encoded/decoded properly to appease tpsclient.

More info.

1. Patch is large but most of it consists of many similar apdu and msg classes.
2. APDU and msg classes are now bare bones and may need more work. Will address when class is needed.
3. A test tpsclient script call it (format.tst) to test this out is as follows:

op=var_set name=ra_host value=localhost
op=var_set name=ra_port value=8080
op=var_set name=ra_uri value=/tps/tps
op=token_set cuid=40906145C76224192D2B msn=0120304 app_ver=6FBBC105 key_info=0101 major_ver=1 minor_ver=1
op=token_set auth_key=404142434445464748494a4b4c4d4e4f
op=token_set mac_key=404142434445464748494a4b4c4d4e4f
op=token_set kek_key=404142434445464748494a4b4c4d4e4f
op=ra_format uid=jmagne pwd=redhat new_pin=rehat num_threads=1
op=exit

4: Execute as follows:

tpsclient < format.tst
---
 .../com/netscape/certsrv/tps/TPSConnection.java    |  98 ----
 .../src/com/netscape/certsrv/tps/TPSMessage.java   | 101 ----
 .../org/dogtagpki/server/tps/TPSConnection.java    | 108 ++++
 .../src/org/dogtagpki/server/tps/TPSServlet.java   |  31 +-
 .../src/org/dogtagpki/server/tps/TPSSession.java   | 138 ++++++
 .../src/org/dogtagpki/server/tps/apdu/APDU.java    | 188 +++++++
 .../dogtagpki/server/tps/apdu/APDU_Response.java   |  77 +++
 .../server/tps/apdu/Create_Object_APDU.java        | 122 +++++
 .../dogtagpki/server/tps/apdu/Create_Pin_APDU.java |  48 ++
 .../server/tps/apdu/Delete_File_APDU.java          |  53 ++
 .../tps/apdu/External_Authenticate_APDU.java       |  59 +++
 .../server/tps/apdu/Format_Muscle_Applet_APDU.java |  57 +++
 .../server/tps/apdu/Generate_Key_APDU.java         |  63 +++
 .../server/tps/apdu/Generate_Key_ECC_APDU.java     |  66 +++
 .../dogtagpki/server/tps/apdu/Get_Data_APDU.java   |  61 +++
 .../server/tps/apdu/Get_IssuerInfo_APDU.java       |  76 +++
 .../dogtagpki/server/tps/apdu/Get_Status_APDU.java |  53 ++
 .../server/tps/apdu/Get_Version_APDU.java          |  56 +++
 .../dogtagpki/server/tps/apdu/Import_Key_APDU.java |  73 +++
 .../server/tps/apdu/Import_Key_Enc_APDU.java       |  65 +++
 .../server/tps/apdu/Initialize_Update_APDU.java    |  64 +++
 .../server/tps/apdu/Install_Applet_APDU.java       |  99 ++++
 .../server/tps/apdu/Install_Load_APDU.java         |  74 +++
 .../dogtagpki/server/tps/apdu/Lifecycle_APDU.java  |  42 ++
 .../server/tps/apdu/List_Objects_APDU.java         |  54 ++
 .../dogtagpki/server/tps/apdu/List_Pins_APDU.java  |  59 +++
 .../dogtagpki/server/tps/apdu/Load_File_APDU.java  |  45 ++
 .../dogtagpki/server/tps/apdu/Put_Key_APDU.java    |  43 ++
 .../server/tps/apdu/Read_Buffer_APDU.java          |  60 +++
 .../server/tps/apdu/Read_Object_APDU.java          |  82 ++++
 .../org/dogtagpki/server/tps/apdu/Select_APDU.java |  40 ++
 .../server/tps/apdu/Set_IssuerInfo_APDU.java       |  67 +++
 .../dogtagpki/server/tps/apdu/Set_Pin_APDU.java    |  67 +++
 .../server/tps/apdu/Unblock_Pin_APDU.java          |  40 ++
 .../server/tps/apdu/Write_Object_APDU.java         |  96 ++++
 .../server/tps/channel/Secure_Channel.java         |  38 ++
 .../src/org/dogtagpki/server/tps/engine/TPS.java   | 101 ++++
 .../org/dogtagpki/server/tps/main/TPSBuffer.java   | 286 +++++++++++
 .../src/org/dogtagpki/server/tps/main/Util.java    | 128 +++++
 .../org/dogtagpki/server/tps/msg/ASQRequest.java   |  29 ++
 .../org/dogtagpki/server/tps/msg/ASQResponse.java  |  29 ++
 .../src/org/dogtagpki/server/tps/msg/BeginOp.java  |  57 +++
 .../src/org/dogtagpki/server/tps/msg/EndOp.java    |  43 ++
 .../server/tps/msg/ExtendedLoginRequest.java       |  92 ++++
 .../server/tps/msg/ExtendedLoginResponse.java      |  43 ++
 .../org/dogtagpki/server/tps/msg/LoginRequest.java |  32 ++
 .../dogtagpki/server/tps/msg/LoginResponse.java    |  29 ++
 .../dogtagpki/server/tps/msg/NewPinRequest.java    |  30 ++
 .../dogtagpki/server/tps/msg/NewPinResponse.java   |  27 +
 .../dogtagpki/server/tps/msg/SecureIdRequest.java  |  30 ++
 .../dogtagpki/server/tps/msg/SecureIdResponse.java |  28 ++
 .../server/tps/msg/StatusUpdateRequest.java        |  38 ++
 .../server/tps/msg/StatusUpdateResponse.java       |  29 ++
 .../org/dogtagpki/server/tps/msg/TPSMessage.java   | 525 ++++++++++++++++++++
 .../dogtagpki/server/tps/msg/TokenPDURequest.java  |  61 +++
 .../dogtagpki/server/tps/msg/TokenPDUResponse.java |  70 +++
 .../server/tps/processor/TPS_Format_Processor.java |  43 ++
 .../server/tps/processor/TPS_Processor.java        | 546 +++++++++++++++++++++
 58 files changed, 4644 insertions(+), 215 deletions(-)
 delete mode 100644 base/common/src/com/netscape/certsrv/tps/TPSConnection.java
 delete mode 100644 base/common/src/com/netscape/certsrv/tps/TPSMessage.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/TPSConnection.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSession.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU_Response.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Object_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Pin_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Delete_File_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/External_Authenticate_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Format_Muscle_Applet_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_ECC_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Data_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_IssuerInfo_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Status_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Version_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_Enc_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Initialize_Update_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Applet_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Load_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Lifecycle_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Objects_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Pins_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Load_File_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Put_Key_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Buffer_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Object_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Select_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_IssuerInfo_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_Pin_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Unblock_Pin_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Write_Object_APDU.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/channel/Secure_Channel.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/engine/TPS.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/main/TPSBuffer.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/main/Util.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/BeginOp.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/EndOp.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateRequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TPSMessage.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDURequest.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDUResponse.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Format_Processor.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Processor.java

diff --git a/base/common/src/com/netscape/certsrv/tps/TPSConnection.java b/base/common/src/com/netscape/certsrv/tps/TPSConnection.java
deleted file mode 100644
index 00e201c..0000000
--- a/base/common/src/com/netscape/certsrv/tps/TPSConnection.java
+++ /dev/null
@@ -1,98 +0,0 @@
-// --- BEGIN COPYRIGHT BLOCK ---
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; version 2 of the License.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License along
-// with this program; if not, write to the Free Software Foundation, Inc.,
-// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-//
-// (C) 2013 Red Hat, Inc.
-// All rights reserved.
-// --- END COPYRIGHT BLOCK ---
-package com.netscape.certsrv.tps;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintStream;
-
-/**
- * @author Endi S. Dewata <edewata@redhat.com>
- */
-public class TPSConnection {
-
-    public InputStream in;
-    public PrintStream out;
-    public boolean chunked;
-
-    public TPSConnection(InputStream in, OutputStream out) {
-        this(in, out, false);
-    }
-
-    public TPSConnection(InputStream in, OutputStream out, boolean chunked) {
-        this.in = in;
-        this.out = new PrintStream(out);
-        this.chunked = chunked;
-    }
-
-    public TPSMessage read() throws IOException {
-
-        StringBuilder sb = new StringBuilder();
-        int b;
-
-        // read the first parameter
-        while ((b = in.read()) >= 0) {
-            char c = (char)b;
-            if (c == '&') break;
-            sb.append(c);
-        }
-
-        if (b < 0) throw new IOException("Unexpected end of stream");
-
-        // parse message size
-        String nvp = sb.toString();
-        String[] s = nvp.split("=");
-        int size = Integer.parseInt(s[1]);
-
-        sb.append('&');
-
-        // read the rest of message
-        for (int i=0; i<size; i++) {
-
-            b = in.read();
-            if (b < 0) throw new IOException("Unexpected end of stream");
-
-            char c = (char)b;
-            sb.append(c);
-        }
-
-        // parse the entire message
-        return new TPSMessage(sb.toString());
-    }
-
-    public void write(TPSMessage message) throws IOException {
-        String s = message.encode();
-
-        if (chunked) {
-            // send message length + EOL
-            out.print(Integer.toHexString(s.length()));
-            out.print("\r\n");
-        }
-
-        // send message
-        out.print(s);
-
-        if (chunked) {
-            // send EOL
-            out.print("\r\n");
-        }
-
-        out.flush();
-    }
-}
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSMessage.java b/base/common/src/com/netscape/certsrv/tps/TPSMessage.java
deleted file mode 100644
index c25459b..0000000
--- a/base/common/src/com/netscape/certsrv/tps/TPSMessage.java
+++ /dev/null
@@ -1,101 +0,0 @@
-// --- BEGIN COPYRIGHT BLOCK ---
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; version 2 of the License.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License along
-// with this program; if not, write to the Free Software Foundation, Inc.,
-// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-//
-// (C) 2013 Red Hat, Inc.
-// All rights reserved.
-// --- END COPYRIGHT BLOCK ---
-package com.netscape.certsrv.tps;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * @author Endi S. Dewata <edewata@redhat.com>
- */
-public class TPSMessage {
-
-    Map<String, String> map = new LinkedHashMap<String, String>();
-
-    public TPSMessage() {
-    }
-
-    public TPSMessage(String message) {
-        decode(message);
-    }
-
-    public TPSMessage(Map<String, String> map) {
-        this.map.putAll(map);
-    }
-
-    public void put(String key, String value) {
-        map.put(key, value);
-    }
-
-    public void put(String key, Integer value) {
-        map.put(key, value.toString());
-    }
-
-    public void put(String key, byte[] bytes) {
-        StringBuilder sb = new StringBuilder();
-
-        for (byte b : bytes) {
-            sb.append("%");
-            sb.append(String.format("%02X", b));
-        }
-
-        map.put(key, sb.toString());
-    }
-
-    public void decode(String message) {
-
-        for (String nvp : message.split("&")) {
-            String[] s = nvp.split("=");
-
-            String key = s[0];
-            String value = s[1];
-
-            // skip message size
-            if (key.equals("s")) continue;
-
-            map.put(key, value);
-        }
-    }
-
-    public String encode() {
-
-        StringBuilder sb = new StringBuilder();
-
-        // encode message type
-        String type = map.get("msg_type");
-        sb.append("msg_type=" + type);
-
-        // encode other parameters
-        for (String key : map.keySet()) {
-
-            if (key.equals("msg_type")) continue;
-
-            String value = map.get(key);
-            sb.append("&" + key + "=" + value);
-        }
-
-        String message = sb.toString();
-
-        // encode message_size
-        return "s=" + message.length() + "&" + message;
-    }
-
-    public String toString() {
-        return map.toString();
-    }
-}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSConnection.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSConnection.java
new file mode 100644
index 0000000..83c7b1f
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSConnection.java
@@ -0,0 +1,108 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import org.dogtagpki.server.tps.msg.TPSMessage;
+
+import com.netscape.certsrv.apps.CMS;
+
+/**
+ * @author Endi S. Dewata <edewata@redhat.com>
+ */
+public class TPSConnection {
+
+    public InputStream in;
+    public PrintStream out;
+    public boolean chunked;
+
+    public TPSConnection(InputStream in, OutputStream out) {
+        this(in, out, false);
+    }
+
+    public TPSConnection(InputStream in, OutputStream out, boolean chunked) {
+        this.in = in;
+        this.out = new PrintStream(out);
+        this.chunked = chunked;
+    }
+
+    public TPSMessage read() throws IOException {
+        CMS.debug("TPSMessage read()");
+
+        StringBuilder sb = new StringBuilder();
+        int b;
+
+        // read the first parameter
+        while ((b = in.read()) >= 0) {
+            char c = (char)b;
+            if (c == '&') break;
+            sb.append(c);
+        }
+
+        if (b < 0) throw new IOException("Unexpected end of stream");
+
+        // parse message size
+        String nvp = sb.toString();
+        String[] s = nvp.split("=");
+        int size = Integer.parseInt(s[1]);
+
+        sb.append('&');
+
+        // read the rest of message
+        for (int i=0; i<size; i++) {
+
+            b = in.read();
+            if (b < 0) throw new IOException("Unexpected end of stream");
+
+            char c = (char)b;
+            sb.append(c);
+        }
+
+        CMS.debug("TPSMessage.read: Reading:  " + sb.toString());
+
+        // parse the entire message
+        return  TPSMessage.createMessage(sb.toString());
+    }
+
+    public void write(TPSMessage message) throws IOException {
+        String s = message.encode();
+
+        CMS.debug("TPSMessage.write: Writing: " + s);
+
+
+        if (chunked) {
+            // send message length + EOL
+            out.print(Integer.toHexString(s.length()));
+            out.print("\r\n");
+        }
+
+        // send message
+        out.print(s);
+
+        if (chunked) {
+            // send EOL
+            out.print("\r\n");
+        }
+
+        out.flush();
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSServlet.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSServlet.java
index 4fcec9d..3839353 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSServlet.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSServlet.java
@@ -23,8 +23,7 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import com.netscape.certsrv.tps.TPSConnection;
-import com.netscape.certsrv.tps.TPSMessage;
+import com.netscape.certsrv.apps.CMS;
 
 /**
  * @author Endi S. Dewata <edewata@redhat.com>
@@ -35,27 +34,27 @@ public class TPSServlet extends HttpServlet {
 
     public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
 
+        CMS.debug("Hello from tps.service " + request);
         response.setHeader("Transfer-Encoding", "chunked");
 
         TPSConnection con = new TPSConnection(
                 request.getInputStream(), response.getOutputStream(), true);
 
-        TPSMessage message = con.read();
-        System.out.println("Receive: " + message);
+        CMS.debug("TPSConnection created: " + con);
 
-        message = new TPSMessage();
-        message.put("msg_type", 9);
-        message.put("pdu_size", 12);
-        message.put("pdu_data", new byte[] {
-                (byte)0x00, (byte)0xA4, (byte)0x04, (byte)0x00,
-                (byte)0x07, (byte)0xA0, (byte)0x00, (byte)0x00,
-                (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00
-        });
+        TPSSession session = new TPSSession(con);
 
-        System.out.println("Send: " + message);
-        con.write(message);
+        CMS.debug("TPSSession created: " + session);
+
+        if (session != null) {
+            try {
+                session.process();
+            } catch (Exception e) {
+                CMS.debug("TPSServlet.service: Can't process incoming message exception occured: " + e);
+            }
+        }
+
+        CMS.debug("After session.process() exiting ...");
 
-        message = con.read();
-        System.out.println("Receive: " + message);
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSession.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSession.java
new file mode 100644
index 0000000..df7d51c
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSession.java
@@ -0,0 +1,138 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps;
+
+import java.io.IOException;
+
+import org.dogtagpki.server.tps.msg.BeginOp;
+import org.dogtagpki.server.tps.msg.EndOp;
+import org.dogtagpki.server.tps.msg.TPSMessage;
+import org.dogtagpki.server.tps.processor.TPS_Format_Processor;
+import org.dogtagpki.server.tps.processor.TPS_Processor;
+import org.dogtagpki.server.tps.processor.TPS_Processor.TPS_Status;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.EBaseException;
+
+public class TPSSession {
+
+    public TPSSession(TPSConnection conn) {
+
+        CMS.debug("TPSSession constructor conn: " + conn);
+        connection = conn;
+    }
+
+    public TPSConnection getConnection() {
+        return connection;
+    }
+
+    public TPSMessage ReadMsg() throws IOException {
+        TPSMessage message = null;
+
+        if (connection != null) {
+            CMS.debug("TPSSession.process() about to call read on connection : " + connection);
+
+            try {
+                message = connection.read();
+                CMS.debug("TPSSession.process() created message " + message);
+
+            } catch (Exception e) {
+                //Catch here so we can log
+                CMS.debug("Exception reading from the client: " + e.toString());
+                throw new IOException(e.toString());
+            }
+        } else {
+            throw new IOException("No connection available in TPSSession instance!");
+        }
+
+        return message;
+    }
+
+    public void WriteMsg(TPSMessage msg) throws IOException {
+
+        if (connection != null) {
+
+            try {
+                connection.write(msg);
+            } catch (Exception e) {
+                //Catch here so we can log
+                CMS.debug("Exception reading from the client: " + e.toString());
+                throw new IOException(e.toString());
+            }
+
+        } else {
+            throw new IOException("No conneciton available in TPSSession instance!");
+        }
+    }
+
+    public void process() throws IOException, EBaseException {
+        TPS_Processor.TPS_Status status = TPS_Status.STATUS_ERROR_BAD_STATUS;
+        CMS.debug("In TPSSession.process()");
+
+        TPSMessage firstMsg = ReadMsg();
+
+        if (firstMsg == null) {
+            throw new IOException("Can't create first TPSMessage!");
+        }
+
+        TPSMessage.Msg_Type msg_type = firstMsg.GetType();
+        TPSMessage.Op_Type op_type = firstMsg.getOpType();
+
+        if (msg_type != TPSMessage.Msg_Type.MSG_BEGIN_OP) {
+            throw new IOException("Wong first message type read in TPSSession.process!");
+        }
+
+        switch (op_type) {
+        case OP_FORMAT:
+
+            TPS_Format_Processor processor = new TPS_Format_Processor();
+            BeginOp beginOp = (BeginOp) firstMsg;
+            status = processor.Process(this, beginOp);
+
+        case OP_ENROLL:
+            break;
+        case OP_RENEW:
+            break;
+        case OP_RESET_PIN:
+            break;
+        case OP_UNBLOCK:
+            break;
+        case OP_UNDEFINED:
+            break;
+        default:
+            break;
+
+        }
+
+        int result = EndOp.RESULT_ERROR;
+
+        if (status == TPS_Processor.TPS_Status.STATUS_NO_ERROR) {
+            result = EndOp.RESULT_GOOD;
+        }
+
+        EndOp endOp = new EndOp(firstMsg.getOpType(), result, status);
+
+        WriteMsg(endOp);
+
+        CMS.debug("TPSSession.process: leaving: result: " + result + " status: " + status);
+
+    }
+
+    private TPSConnection connection;
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU.java
new file mode 100644
index 0000000..9706c15
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU.java
@@ -0,0 +1,188 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public abstract class APDU {
+
+    public static int DEFAULT_APDU_SIZE = 32;
+
+    public enum APDU_Type {
+        APDU_UNDEFINED,
+        APDU_CREATE_OBJECT,
+        APDU_EXTERNAL_AUTHENTICATE,
+        APDU_INITIALIZE_UPDATE,
+        APDU_LIFECYCLE,
+        APDU_READ_BUFFER,
+        APDU_SET_PIN,
+        APDU_UNBLOCK_PIN,
+        APDU_WRITE_OBJECT,
+        APDU_GENERATE_KEY,
+        APDU_PUT_KEY,
+        APDU_SELECT,
+        APDU_GET_VERSION,
+        APDU_DELETE_FILE,
+        APDU_INSTALL_APPLET,
+        APDU_FORMAT_MUSCLE_APPLET,
+        APDU_LOAD_FILE,
+        APDU_INSTALL_LOAD,
+        APDU_GET_STATUS,
+        APDU_LIST_PINS,
+        APDU_CREATE_PIN,
+        APDU_GET_DATA,
+        APDU_READ_OBJECT,
+        APDU_LIST_OBJECTS,
+        APDU_IMPORT_KEY,
+        APDU_IMPORT_KEY_ENC,
+        APDU_SET_ISSUERINFO,
+        APDU_GET_ISSUERINFO,
+        APDU_GENERATE_KEY_ECC
+    }
+
+    public APDU() {
+        m_data = new TPSBuffer();
+    }
+
+    public APDU(APDU otherAPDU) {
+        m_data = new TPSBuffer(otherAPDU.GetData());
+    }
+
+    void SetCLA(byte cla) {
+        m_cla = cla;
+    }
+
+    void SetINS(byte ins) {
+        m_ins = ins;
+    }
+
+    void SetP1(byte p1) {
+        m_p1 = p1;
+    }
+
+    void SetP2(byte p2) {
+        m_p2 = p2;
+    }
+
+    void SetData(TPSBuffer data) {
+        m_data = new TPSBuffer(data);
+
+    }
+
+    public void SetMAC(TPSBuffer mac) {
+        m_mac = mac;
+    }
+
+    /**
+     * Retrieves APDU's encoding.
+     * The encoding of APDU is as follows:
+     *
+     * CLA 1 byte
+     * INS 1 byte
+     * P1 1 byte
+     * P2 1 byte
+     * <Data Size> 1 byte
+     * <Data> <Data Size> byte(s)
+     * 0 1 byte
+     *
+     * @param data the result buffer which will contain the actual data
+     *            including the APDU header, data, and pre-calculated mac.
+     */
+
+    public TPSBuffer GetEncoding() {
+
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+
+        int m_mac_size = 0;
+
+        if (m_mac != null) {
+            m_mac_size = m_mac.size();
+        }
+
+        data.add((byte) (m_data.size() + m_mac_size));
+
+        data.add(m_data);
+
+        if (m_mac_size > 0) {
+            data.add(m_mac);
+        }
+
+        return data;
+    }
+
+    public void GetDataToMAC(TPSBuffer data) {
+
+    }
+
+    /* PRStatus SecureMessage(PK11SymKey *encSessionKey); */
+
+    public APDU_Type GetType() {
+        return APDU_Type.APDU_UNDEFINED;
+    }
+
+    public TPSBuffer GetData() {
+        return m_data;
+    }
+
+    public TPSBuffer GetMAC() {
+        return m_mac;
+    }
+
+    public byte GetCLA() {
+        return m_cla;
+
+    }
+
+    public byte GetINS() {
+        return m_ins;
+    }
+
+    public byte GetP1() {
+        return m_p1;
+    }
+
+    public byte GetP2() {
+        return m_p2;
+    }
+
+    public void dump() {
+
+        System.out.println("APDU: ");
+        System.out.println("CLA: " + m_cla);
+        System.out.println("INS: " + m_ins);
+        System.out.println("P1: " + m_p1);
+        System.out.println("P2: " + m_p2);
+
+        m_data.dump();
+    }
+
+    protected byte m_cla;
+    protected byte m_ins;
+    protected byte m_p1;
+    protected byte m_p2;
+
+    protected TPSBuffer m_data = null;
+    protected TPSBuffer m_plainText = null;
+    protected TPSBuffer m_mac = null;
+
+};
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU_Response.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU_Response.java
new file mode 100644
index 0000000..7b14671
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/APDU_Response.java
@@ -0,0 +1,77 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class APDU_Response extends APDU {
+
+    public APDU_Response() {
+        super();
+
+    }
+
+    public APDU_Response(TPSBuffer data) {
+        super();
+        SetData(data);
+
+    }
+
+    public APDU_Response(APDU_Response cpy) {
+        super(cpy);
+    }
+
+    public byte GetSW1() {
+        if (m_data == null) {
+            return 0x0;
+        } else {
+            if (m_data.size() < 2) {
+                return 0x0;
+            } else {
+                return m_data.at(m_data.size() - 2);
+            }
+        }
+
+    }
+
+    public byte GetSW2() {
+        if (m_data == null) {
+            return 0x0;
+        } else {
+            if (m_data.size() < 2) {
+                return 0x0;
+            } else {
+                return m_data.at(m_data.size() - 1);
+            }
+        }
+
+    }
+
+
+    public static void main(String args[]) {
+
+        APDU_Response resp = new APDU_Response();
+        resp.dump();
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Object_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Object_APDU.java
new file mode 100644
index 0000000..91b2fe6
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Object_APDU.java
@@ -0,0 +1,122 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Create_Object_APDU extends APDU {
+    /**
+     * Constructs a Create Object APDU. This APDU is usually sent right
+     * before Write_Buffer_APDU is sent. This APDU only creates an Object
+     * on token, but does not actually writes object content until
+     * Write_Buffer_APDU is sent.
+     *
+     * CreateObject APDU format:
+     * CLA 0x84
+     * INS 0x5a
+     * P1 0x00
+     * P2 0x00
+     * lc 0x0e
+     * DATA <Object Parameters>
+     *
+     * [DATA] Object Parameters are:
+     * Long Object ID;
+     * Long Object Size;
+     * ObjectACL ObjectACL;
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     * 9C 08 - object already exists
+     * 9C 01 - insufficient memory on card to complete the operation
+     *
+     * NOTE:
+     * Observe that the PIN identity is hard-coded at n.2 for each
+     * permission. In Housekey, this is probably a non-issue, however,
+     * in housekey, do we not allow multiple people (presumably closely
+     * -related) to share one token with individual certs? We should
+     * consider exposing this as an input param.
+     *
+     * @param object_id as defined in APDU
+     * @param len length of object
+     * @see APDU
+     */
+
+    public Create_Object_APDU(byte[] object_id, byte[] permissions, int len) {
+
+        if (object_id.length != 4)
+            return;
+
+        if (permissions.length != 6)
+            return;
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x5a);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+
+        TPSBuffer data = new TPSBuffer();
+
+        data.add((object_id[0]));
+        data.add((object_id[1]));
+        data.add((object_id[2]));
+        data.add((object_id[3]));
+
+        data.add((byte) (len >> 24));
+        data.add((byte) ((len >> 16) & 0xff));
+        data.add((byte) ((len >> 8) & 0xff));
+        data.add((byte) (len & 0xff));
+
+        data.add(permissions[0]);
+        data.add(permissions[1]);
+        data.add(permissions[2]);
+        data.add(permissions[3]);
+        data.add(permissions[4]);
+        data.add(permissions[5]);
+
+        SetData(data);
+
+    }
+
+    @Override
+    public APDU.APDU_Type GetType() {
+        return APDU.APDU_Type.APDU_CREATE_OBJECT;
+
+     }
+
+    public static void main(String args[]) {
+
+        byte[] object_id = { 0x01, 0x02, 0x3, 0x4 };
+        byte[] permisisons = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x6 };
+
+        Create_Object_APDU apdu = new Create_Object_APDU(object_id, permisisons, 56);
+
+        if (apdu != null) {
+
+            apdu.dump();
+        }
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Pin_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Pin_APDU.java
new file mode 100644
index 0000000..3bb8a9a
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Create_Pin_APDU.java
@@ -0,0 +1,48 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Create_Pin_APDU  extends APDU {
+
+    public Create_Pin_APDU(byte p1, byte p2, TPSBuffer data) {
+
+        SetP1(p1);
+        SetP2(p2);
+        SetData(data);
+
+    }
+
+    @Override
+    public APDU.APDU_Type GetType() {
+       return APDU.APDU_Type.APDU_CREATE_PIN;
+
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Delete_File_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Delete_File_APDU.java
new file mode 100644
index 0000000..119bd4d
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Delete_File_APDU.java
@@ -0,0 +1,53 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Delete_File_APDU extends APDU {
+
+    public Delete_File_APDU( TPSBuffer aid) {
+        SetCLA((byte) 0x84);
+        SetINS((byte)0xE4);
+        SetP1((byte)0x00);
+        SetP2((byte)0x00);
+
+
+        TPSBuffer AIDTLV = new TPSBuffer();
+
+        AIDTLV.add((byte) 0x4f);
+        AIDTLV.add((byte) aid.size());
+
+        AIDTLV.add(aid);
+
+        SetData(AIDTLV);
+
+    }
+
+    @Override
+    public APDU.APDU_Type GetType() {
+        return APDU.APDU_Type.APDU_DELETE_FILE;
+
+     }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/External_Authenticate_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/External_Authenticate_APDU.java
new file mode 100644
index 0000000..0e66c0d
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/External_Authenticate_APDU.java
@@ -0,0 +1,59 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.channel.Secure_Channel;
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class External_Authenticate_APDU extends APDU {
+
+    public External_Authenticate_APDU(TPSBuffer data, Secure_Channel.SecurityLevel sl) {
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x82);
+        SetP1((byte) 0x01);
+
+        if (sl == Secure_Channel.SecurityLevel.SECURE_MSG_MAC_ENC) {
+            SetP1((byte) 0x03);
+        } else if (sl == Secure_Channel.SecurityLevel.SECURE_MSG_NONE) {
+            SetP1((byte) 0x00);
+        } else { // default
+            SetP1((byte) 0x01);
+        }
+
+        SetP2((byte) 0x00);
+        SetData(data);
+    }
+
+    public TPSBuffer GetHostCryptogram()
+    {
+        return GetData();
+    }
+
+    @Override
+    public APDU.APDU_Type GetType()
+    {
+        return APDU.APDU_Type.APDU_EXTERNAL_AUTHENTICATE;
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Format_Muscle_Applet_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Format_Muscle_Applet_APDU.java
new file mode 100644
index 0000000..5fac379
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Format_Muscle_Applet_APDU.java
@@ -0,0 +1,57 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+/* Not sure this is used , provide stub right now. */
+
+public class Format_Muscle_Applet_APDU  extends APDU {
+    public Format_Muscle_Applet_APDU(short memSize,
+            TPSBuffer PIN0, byte pin0Tries,
+            TPSBuffer unblockPIN0, byte unblock0Tries,
+            TPSBuffer PIN1, byte pin1Tries,
+            TPSBuffer unblockPIN1, byte unblock1Tries,
+            short objCreationPermissions,
+            short keyCreationPermissions,
+            short pinCreationPermissions) {
+
+        SetCLA((byte) 0xB0);
+        SetINS((byte)0x2A);
+        SetP1((byte)0x00);
+        SetP2((byte)0x00);
+
+        SetData(null);
+    }
+
+
+    @Override
+    public APDU.APDU_Type GetType() {
+        return APDU.APDU_Type.APDU_FORMAT_MUSCLE_APPLET;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding() {
+        return super.GetEncoding();
+
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_APDU.java
new file mode 100644
index 0000000..bc0b9d1
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_APDU.java
@@ -0,0 +1,63 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Generate_Key_APDU extends APDU {
+
+    public Generate_Key_APDU(byte p1, byte p2, byte alg,
+            int keysize, byte option,
+            byte type, TPSBuffer wrapped_challenge, TPSBuffer key_check) {
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x0C);
+        SetP1(p1);
+        SetP2(p2);
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(alg);
+        data.add((byte) (keysize / 256));
+        data.add((byte) (keysize % 256));
+
+        data.add(option);
+
+        data.add(type);
+
+        data.add((byte) wrapped_challenge.size());
+        data.add(wrapped_challenge);
+
+        data.add((byte) key_check.size());
+
+        if (key_check.size() > 0) {
+            data.add(key_check);
+        }
+
+        SetData(data);
+
+    }
+
+    public APDU.APDU_Type GetType() {
+        return APDU.APDU_Type.APDU_GENERATE_KEY;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_ECC_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_ECC_APDU.java
new file mode 100644
index 0000000..8a0fea8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Generate_Key_ECC_APDU.java
@@ -0,0 +1,66 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Generate_Key_ECC_APDU extends APDU {
+
+    public Generate_Key_ECC_APDU(byte p1, byte p2, byte alg,
+            int keysize, byte option,
+            byte type, TPSBuffer wrapped_challenge, TPSBuffer key_check) {
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x0D);
+        SetP1(p1);
+        SetP2(p2);
+
+        TPSBuffer data1 = new TPSBuffer();
+
+        data1.add(alg);
+
+        data1.add((byte) (keysize / 256));
+
+        data1.add((byte) (keysize % 256));
+
+        data1.add(option);
+        data1.add(type);
+
+        data1.add((byte) wrapped_challenge.size());
+
+        data1.add(wrapped_challenge);
+
+        data1.add((byte) key_check.size());
+
+        if (key_check.size() > 0) {
+            data1.add(key_check);
+        }
+
+        SetData(data1);
+    }
+
+    @Override
+    public APDU.APDU_Type GetType() {
+        return APDU.APDU_Type.APDU_GENERATE_KEY_ECC;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Data_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Data_APDU.java
new file mode 100644
index 0000000..24a79e0
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Data_APDU.java
@@ -0,0 +1,61 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Get_Data_APDU extends APDU {
+
+    public Get_Data_APDU ()
+    {
+        SetCLA((byte)0x80);
+        SetINS((byte)0xCA);
+        SetP1((byte)0x9F);
+        SetP2((byte)0x7F);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+            return APDU.APDU_Type.APDU_GET_DATA;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add( m_p1);
+        data.add(m_p2);
+        data.add((byte)0x2D);
+
+        return data;
+    } /* Encode */
+
+    public static void main(String[] args) {
+        Get_Data_APDU get_data = new Get_Data_APDU();
+
+        get_data.dump();
+
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_IssuerInfo_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_IssuerInfo_APDU.java
new file mode 100644
index 0000000..23cc4a8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_IssuerInfo_APDU.java
@@ -0,0 +1,76 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Get_IssuerInfo_APDU extends APDU {
+    /**
+     * Constructs GetIssuer APDU.
+     *
+     * SecureGetIssuer APDU format:
+     * CLA 0x84
+     * INS 0xF6
+     * P1 0x00
+     * P2 0x00
+     * lc 0xE0
+     * DATA <Issuer Info>
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     *
+     * @param p1 always 0x00
+     * @param p2 always 0x00
+     * @param data issuer info
+     * @see APDU
+     */
+    public Get_IssuerInfo_APDU()
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xF6);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+    }
+
+    @Override
+    public APDU.APDU_Type GetType()
+    {
+        return APDU_Type.APDU_GET_ISSUERINFO;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add((byte) 0xe0);
+
+        return data;
+    } /* Encode */
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Status_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Status_APDU.java
new file mode 100644
index 0000000..9569272
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Status_APDU.java
@@ -0,0 +1,53 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Get_Status_APDU extends APDU {
+    public Get_Status_APDU()
+    {
+        SetCLA((byte) 0xB0);
+        SetINS((byte) 0x3C);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+    }
+
+    @Override
+    public APDU.APDU_Type GetType()
+    {
+        return APDU_Type.APDU_GET_STATUS;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add((byte) 16);
+
+        return data;
+    } /* Encode */
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Version_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Version_APDU.java
new file mode 100644
index 0000000..0c58ee3
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Get_Version_APDU.java
@@ -0,0 +1,56 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Get_Version_APDU extends APDU {
+    public Get_Version_APDU ()
+    {
+        SetCLA((byte)0xB0);
+        SetINS((byte)0x70);
+        SetP1((byte)0x00);
+        SetP2((byte)0x00);
+    }
+
+    @Override
+    public APDU.APDU_Type GetType()
+    {
+            return APDU_Type.APDU_GET_VERSION;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add((byte)4);
+
+        return data;
+    } /* Encode */
+
+
+
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_APDU.java
new file mode 100644
index 0000000..e63d1c8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_APDU.java
@@ -0,0 +1,73 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Import_Key_APDU extends APDU {
+    /**
+     * Constructs Import Key APDU.
+     *
+     * CLA 0x84
+     * INS 0x32
+     * P1 Key Number (0x00 -0x0F) - key slot number defined in CS.cfg
+     * P2 0x00
+     * P3 Import Parameters Length (6 bytes: 3 shorts if just for ACL)
+     * DATA Import Parameters
+     *
+     * This function allows th eimport of a key into the card by (over)-writing the Cardlet memory.  Object ID 0xFFFFFFFE needs to be initialized with a key blob before invocation of this function so tha tit can retrieve the key from this object. The exact key blob contents depend on th ekey's algorithm, type and actual import parameters.  The key's number, algorithm type, and parameters are specified by argumetns P1, P2, P3, and DATA.  Appropriate values for these are specified below:
+
+    [DATA]
+    Import Parameters:
+    KeyACL ACL for the imported key;
+    Byte[] Additional parameters; // Optional
+    If KeyBlob's Encoding is BLOB_ENC_PLAIN(0x00), there are no additional parameters.
+     */
+    public Import_Key_APDU (byte p1)
+    {
+        SetCLA((byte)0x84);
+        SetINS((byte)0x32);
+        SetP1(p1);
+        SetP2((byte)0x00);
+        //    SetP3(p3);
+
+        TPSBuffer data = new TPSBuffer();
+        data.add((byte) 0xFF);
+
+        data.add((byte) 0xFF);
+        data.add((byte) 0x40); // means "write" allowed for RA only
+        data.add((byte) 0x00);
+        data.add((byte) 0xFF) ;// means "use" allowed for everyone
+        data.add((byte) 0xFF);
+
+        SetData(data);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+            return APDU_Type.APDU_IMPORT_KEY;
+    }
+
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_Enc_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_Enc_APDU.java
new file mode 100644
index 0000000..962c24a
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Import_Key_Enc_APDU.java
@@ -0,0 +1,65 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Import_Key_Enc_APDU extends APDU {
+
+    /**
+     * Constructs Import Key Encrypted APDU.
+     *
+     * CLA 0x80
+     * INS 0x0A
+     * P1 private Key Number (0x00 -0x0F) - key slot number defined in CMS.cfg
+     * P2 public Key Number (0x00 -0x0F) - key slot number defined in CMS.cfg
+     * DATA:
+     * Wrapped Key DesKey
+     * Byte IV_Length
+     * Byte IV_Data
+     *
+     * This function allows the import of a key into the card by (over)-writing the Cardlet memory. Object ID 0xFFFFFFFE
+     * needs to be initialized with a key blob before invocation of this function so that it can retrieve the key from
+     * this object. The exact key blob contents depend on the key's algorithm, type and actual import parameters. The
+     * key's number, algorithm type, and parameters are specified by argumetns P1, P2, P3, and DATA. Appropriate values
+     * for these are specified below:
+     *
+     * [DATA]
+     * Import Parameters:
+     * ...to be provided
+     */
+    public Import_Key_Enc_APDU(byte p1, byte p2, TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x0A);
+        SetP1(p1);
+        SetP2(p2);
+
+        SetData(data);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_IMPORT_KEY_ENC;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Initialize_Update_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Initialize_Update_APDU.java
new file mode 100644
index 0000000..36fa5f1
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Initialize_Update_APDU.java
@@ -0,0 +1,64 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Initialize_Update_APDU extends APDU {
+
+    /**
+     * Constructs Initialize Update APDU.
+     */
+    public Initialize_Update_APDU(byte key_version, byte key_index, TPSBuffer data)
+    {
+        SetCLA((byte) 0x80);
+        SetINS((byte) 0x50);
+        SetP1(key_version);
+        SetP2(key_index);
+        SetData(data);
+    }
+
+    public TPSBuffer GetHostChallenge()
+    {
+        return GetData();
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_INITIALIZE_UPDATE;
+    }
+
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add((byte) m_data.size());
+        data.add(m_data);
+
+        return data;
+    } /* Encode */
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Applet_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Applet_APDU.java
new file mode 100644
index 0000000..0ea439d
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Applet_APDU.java
@@ -0,0 +1,99 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Install_Applet_APDU extends APDU {
+
+    public Install_Applet_APDU(TPSBuffer packageAID, TPSBuffer appletAID,
+            byte appPrivileges, int instanceSize, int appletMemorySize)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xE6);
+        SetP1((byte) 0x0C);
+        SetP2((byte) 0x00);
+
+        TPSBuffer data = new TPSBuffer();
+        data.add((byte) packageAID.size());
+        data.add(packageAID);
+        data.add((byte) appletAID.size());
+        data.add(appletAID);
+        data.add((byte) appletAID.size());
+        data.add(appletAID);
+
+        data.add((byte) 0x01); // length of application privileges byte
+        data.add(appPrivileges);
+
+        TPSBuffer installParams = new TPSBuffer();
+        installParams.add((byte) 0xEF);
+        installParams.add((byte) 0x04);
+        installParams.add((byte) 0xC8);
+        installParams.add((byte) 0x02);
+
+        installParams.add((byte) ((instanceSize >> 8) & 0xff));
+        installParams.add((byte) (instanceSize & 0xff));
+        installParams.add((byte) 0xC9);
+
+        //Now add some applet specific init data that the applet supports
+        //Length of applet specific data
+
+        installParams.add((byte) 0x04);
+
+        //Issuer info length.
+        //Leave this to zero since TPS already writes phone home info to card.
+        installParams.add((byte) 0x00);
+
+        //Length of applet memory size
+        installParams.add((byte) 0x02);
+
+        // Applet memory block size
+
+        installParams.add((byte) ((appletMemorySize >> 8) & 0xff));
+        installParams.add((byte) (appletMemorySize & 0xff));
+
+        data.add((byte) installParams.size());
+        data.add(installParams);
+        data.add((byte) 0x00); // size of token return data
+
+        SetData(data);
+    }
+
+    /**
+     * Constructs Install Applet APDU.
+     */
+    public Install_Applet_APDU(TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xE6);
+        SetP1((byte) 0x0C);
+        SetP2((byte) 0x00);
+        SetData(data);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_INSTALL_APPLET;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Load_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Load_APDU.java
new file mode 100644
index 0000000..db4002f
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Install_Load_APDU.java
@@ -0,0 +1,74 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Install_Load_APDU extends APDU {
+
+    public Install_Load_APDU(TPSBuffer packageAID, TPSBuffer sdAID,
+            int fileLen)
+    {
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xE6);
+        SetP1((byte) 0x02);
+        SetP2((byte) 0x00);
+
+        TPSBuffer inputData = new TPSBuffer();
+        inputData.add((byte) packageAID.size());
+        inputData.add(packageAID);
+        inputData.add((byte) sdAID.size());
+        inputData.add(sdAID);
+        inputData.add((byte) 0x0);
+        inputData.add((byte) 0x6);
+        inputData.add((byte) 0xEF);
+        inputData.add((byte) 0x04);
+        inputData.add((byte) 0xC6);
+        inputData.add((byte) 0x02);
+        fileLen += 24 + sdAID.size(); // !!! XXX
+        inputData.add((byte) ((fileLen >> 8) & 0xff));
+        inputData.add((byte) (fileLen & 0xff));
+
+        SetData(inputData);
+    }
+
+    /**
+     * Constructs Install Load APDU. Used when data was pre-constructed
+     */
+    public Install_Load_APDU(TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xE6);
+        SetP1((byte) 0x02);
+        SetP2((byte) 0x00);
+        SetData(data);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_INSTALL_LOAD;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Lifecycle_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Lifecycle_APDU.java
new file mode 100644
index 0000000..ccc7864
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Lifecycle_APDU.java
@@ -0,0 +1,42 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+public class Lifecycle_APDU extends APDU {
+    /**
+     * Constructs Lifecycle APDU.
+     */
+    public Lifecycle_APDU(byte lifecycle)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xf0);
+        SetP1(lifecycle);
+        SetP2((byte) 0x00);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_LIFECYCLE;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Objects_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Objects_APDU.java
new file mode 100644
index 0000000..820fb4c
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Objects_APDU.java
@@ -0,0 +1,54 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class List_Objects_APDU extends APDU {
+    public List_Objects_APDU(byte seq)
+    {
+        SetCLA((byte) 0xB0);
+        SetINS((byte) 0x58);
+        SetP1(seq);
+        SetP2((byte) 0x00);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_LIST_OBJECTS;
+    }
+
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add((byte) 0x0E);
+
+        return data;
+    } /* Encode */
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Pins_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Pins_APDU.java
new file mode 100644
index 0000000..f3b171b
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/List_Pins_APDU.java
@@ -0,0 +1,59 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+
+
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class List_Pins_APDU extends APDU {
+
+    public List_Pins_APDU(byte ret_size)
+    {
+        SetCLA((byte) 0xB0);
+        SetINS((byte) 0x48);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+        m_ret_size = ret_size;
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_LIST_PINS;
+    }
+
+    @Override
+    public TPSBuffer GetEncoding()
+    {
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(m_cla);
+        data.add(m_ins);
+        data.add(m_p1);
+        data.add(m_p2);
+        data.add(m_ret_size);
+
+        return data;
+    } /* Encode */
+
+    private byte m_ret_size = 0;
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Load_File_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Load_File_APDU.java
new file mode 100644
index 0000000..6fcb6e8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Load_File_APDU.java
@@ -0,0 +1,45 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Load_File_APDU extends APDU {
+    /**
+     * Constructs Load File APDU.
+     */
+    public Load_File_APDU(byte refControl, byte blockNum, TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xE8);
+        SetP1(refControl);
+        SetP2(blockNum);
+
+        SetData(data);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_LOAD_FILE;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Put_Key_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Put_Key_APDU.java
new file mode 100644
index 0000000..69efac1
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Put_Key_APDU.java
@@ -0,0 +1,43 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Put_Key_APDU extends APDU {
+    /**
+     * Constructs Put Key APDU.
+     */
+    public Put_Key_APDU(byte p1, byte p2, TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xd8);
+        SetP1(p1);
+        SetP2(p2);
+        SetData(data);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_PUT_KEY;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Buffer_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Buffer_APDU.java
new file mode 100644
index 0000000..4045f45
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Buffer_APDU.java
@@ -0,0 +1,60 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Read_Buffer_APDU extends APDU {
+    /**
+     * Constructs Read Buffer APDU.
+     */
+    public Read_Buffer_APDU(int len, int offset)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x08);
+        SetP1((byte) len);
+        SetP2((byte) 0x00);
+        TPSBuffer data = new TPSBuffer();
+
+        data.add((byte) (offset / 256));
+        data.add((byte) (offset % 256));
+        SetData(data);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_READ_BUFFER;
+    }
+
+    public int GetLen()
+    {
+        return m_p1;
+    }
+
+    public int GetOffset()
+    {
+        byte a = m_data.at(0);
+        byte b = m_data.at(1);
+
+        return ((a << 8) + b);
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Object_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Object_APDU.java
new file mode 100644
index 0000000..4d8456b
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Read_Object_APDU.java
@@ -0,0 +1,82 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Read_Object_APDU extends APDU {
+    /**
+     * Constructs Read Object APDU.
+     *
+     * ReadObject APDU format:
+     * CLA 0x84
+     * INS 0x56
+     * P1 0x00
+     * P2 0x00
+     * lc 0x09
+     * DATA <Data Parameters>
+     *
+     * [DATA] Parameters are:
+     * Long Object ID;
+     * Long Offset
+     * Byte Data Size;
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     * 9C 07 - object not found
+     *
+     * @param object_id as defined in APDU
+     * @param offset
+     * @param data
+     * @see APDU
+     */
+
+    public Read_Object_APDU(byte[] object_id, int offset, int len)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x56);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+        TPSBuffer data = new TPSBuffer();
+
+        data.add(object_id[0]);
+        data.add(object_id[1]);
+        data.add(object_id[2]);
+        data.add(object_id[3]);
+
+        data.add((byte) ((offset >> 24) & 0xff));
+        data.add((byte) ((offset >> 16) & 0xff));
+        data.add((byte) ((offset >> 8) & 0xff));
+        data.add((byte) (offset & 0xff));
+        data.add((byte) len);
+        SetData(data);
+    }
+
+    @Override
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_READ_OBJECT;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Select_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Select_APDU.java
new file mode 100644
index 0000000..3894c43
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Select_APDU.java
@@ -0,0 +1,40 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Select_APDU extends APDU {
+    public Select_APDU(byte p1, byte p2, TPSBuffer data)
+    {
+        SetCLA((byte) 0x00);
+        SetINS((byte) 0xa4);
+        SetP1(p1);
+        SetP2(p2);
+        SetData(data);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_SELECT;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_IssuerInfo_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_IssuerInfo_APDU.java
new file mode 100644
index 0000000..02d47a7
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_IssuerInfo_APDU.java
@@ -0,0 +1,67 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Set_IssuerInfo_APDU extends APDU {
+    /**
+     * Constructs SetIssuer APDU.
+     *
+     * SecureSetIssuer APDU format:
+     * CLA 0x84
+     * INS 0xF4
+     * P1 0x00
+     * P2 0x00
+     * lc 0xE0
+     * DATA <Issuer Info>
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     *
+     * @param p1 always 0x00
+     * @param p2 always 0x00
+     * @param data issuer info
+     * @see APDU
+     */
+    public Set_IssuerInfo_APDU(byte p1, byte p2, TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0xF4);
+        SetP1(p1);
+        SetP2(p2);
+        SetData(data);
+    }
+
+    public TPSBuffer GetIssuerInfo()
+    {
+        return GetData();
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_SET_ISSUERINFO;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_Pin_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_Pin_APDU.java
new file mode 100644
index 0000000..5fb319e
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Set_Pin_APDU.java
@@ -0,0 +1,67 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Set_Pin_APDU extends APDU {
+    /**
+     * Constructs SetPin APDU.
+     *
+     * SecureSetPIN APDU format:
+     * CLA 0x80
+     * INS 0x04
+     * P1 <Pin number>
+     * P2 0x00
+     * lc <data length>
+     * DATA <New Pin Value>
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     *
+     * @param p1 Pin number: 0x00 - 0x07
+     * @param p2 always 0x00
+     * @param data pin
+     * @see APDU
+     */
+    public Set_Pin_APDU(byte p1, byte p2, TPSBuffer data)
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x04);
+        SetP1(p1);
+        SetP2(p2);
+        SetData(data);
+    }
+
+    public TPSBuffer GetNewPIN()
+    {
+        return GetData();
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_SET_PIN;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Unblock_Pin_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Unblock_Pin_APDU.java
new file mode 100644
index 0000000..ba47124
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Unblock_Pin_APDU.java
@@ -0,0 +1,40 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+public class Unblock_Pin_APDU extends APDU {
+    /**
+     * Constructs Unblock Pin APDU.
+     */
+    public Unblock_Pin_APDU()
+    {
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x02);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_UNBLOCK_PIN;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Write_Object_APDU.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Write_Object_APDU.java
new file mode 100644
index 0000000..b0f031f
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/apdu/Write_Object_APDU.java
@@ -0,0 +1,96 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.apdu;
+
+import org.dogtagpki.server.tps.main.TPSBuffer;
+
+public class Write_Object_APDU extends APDU {
+    /**
+     * Constructs Write Buffer APDU. This APDU is usually sent right after
+     * the Create_Object_APDU is sent. This APDU writes the actual object
+     * content into the object that was created with Create_Object_APDU.
+     * This APDU is used for both write and re-writes of data.
+     * The object data is stored starting from the byte specified by the
+     * offset parameter.
+     * Up to 240 bytes can be transferred with a single APDU. If more bytes
+     * need to be transferred, then multiple WriteObject commands must be
+     * used with different offsets.
+     *
+     * WriteObject APDU format:
+     * CLA 0x84
+     * INS 0x54
+     * P1 0x00
+     * P2 0x00
+     * lc Data Size + 9
+     * DATA <Data Parameters>
+     *
+     * [DATA] Parameters are:
+     * Long Object ID;
+     * Long Offset
+     * Byte Data Size;
+     * Byte[] Object Data
+     *
+     * Connection requirement:
+     * Secure Channel
+     *
+     * Possible error Status Codes:
+     * 9C 06 - unauthorized
+     * 9C 07 - object not found
+     *
+     * @param object_id as defined in APDU
+     * @param offset
+     * @param data
+     * @see APDU
+     */
+    public Write_Object_APDU(byte[] object_id, int offset, TPSBuffer data)
+    {
+        if (object_id.length != 4) {
+            return;
+        }
+
+        SetCLA((byte) 0x84);
+        SetINS((byte) 0x54);
+        SetP1((byte) 0x00);
+        SetP2((byte) 0x00);
+
+        TPSBuffer data1 = new TPSBuffer();
+
+        data1.add(object_id[0]);
+        data1.add(object_id[1]);
+
+        data1.add(object_id[2]);
+        data1.add(object_id[3]);
+
+        data1.add((byte) ((offset >> 24) & 0xff));
+        data1.add((byte) ((offset >> 16) & 0xff));
+        data1.add((byte) ((offset >> 8) & 0xff));
+        data1.add((byte) (offset & 0xff));
+        data1.add((byte) data.size());
+        data1.add(data);
+        SetData(data1);
+    }
+
+    public APDU_Type GetType()
+    {
+        return APDU_Type.APDU_WRITE_OBJECT;
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/channel/Secure_Channel.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/channel/Secure_Channel.java
new file mode 100644
index 0000000..c1301b8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/channel/Secure_Channel.java
@@ -0,0 +1,38 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.channel;
+
+public class Secure_Channel {
+
+   public enum SecurityLevel {
+        SECURE_MSG_ANY ,
+        SECURE_MSG_MAC ,
+        SECURE_MSG_NONE , // not yet supported
+        SECURE_MSG_MAC_ENC
+    }
+
+    public Secure_Channel() {
+
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/engine/TPS.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/engine/TPS.java
new file mode 100644
index 0000000..36c9a82
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/engine/TPS.java
@@ -0,0 +1,101 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.engine;
+
+public class TPS {
+
+    public static final String TKS_RESPONSE_STATUS = "status";
+    public static final String TKS_RESPONSE_SessionKey = "sessionKey";
+    public static final String TKS_RESPONSE_EncSessionKey = "encSessionKey";
+    public static final String TKS_RESPONSE_KEK_DesKey = "kek_wrapped_desKey";
+    public static final String TKS_RESPONSE_DRM_Trans_DesKey = "drm_trans_wrapped_desKey";
+    public static final String TKS_RESPONSE_HostCryptogram = "hostCryptogram";
+
+    public static final String CFG_DEBUG_ENABLE = "logging.debug.enable";
+    public static final String CFG_DEBUG_FILENAME = "logging.debug.filename";
+    public static final String CFG_DEBUG_LEVEL = "logging.debug.level";
+    public static final String CFG_AUDIT_ENABLE = "logging.audit.enable";
+    public static final String CFG_AUDIT_FILENAME = "logging.audit.filename";
+    public static final String CFG_SIGNED_AUDIT_FILENAME = "logging.audit.signedAuditFilename";
+    public static final String CFG_AUDIT_LEVEL = "logging.audit.level";
+    public static final String CFG_AUDIT_SIGNED = "logging.audit.logSigning";
+    public static final String CFG_AUDIT_SIGNING_CERT_NICK = "logging.audit.signedAuditCertNickname";
+    public static final String CFG_ERROR_ENABLE = "logging.error.enable";
+    public static final String CFG_ERROR_FILENAME = "logging.error.filename";
+    public static final String CFG_ERROR_LEVEL = "logging.error.level";
+    public static final String CFG_SELFTEST_ENABLE = "selftests.container.logger.enable";
+    public static final String CFG_SELFTEST_FILENAME = "selftests.container.logger.fileName";
+    public static final String CFG_SELFTEST_LEVEL = "selftests.container.logger.level";
+    public static final String CFG_CHANNEL_SEC_LEVEL = "channel.securityLevel";
+    public static final String CFG_CHANNEL_ENCRYPTION = "channel.encryption";
+    public static final String CFG_APPLET_CARDMGR_INSTANCE_AID = "applet.aid.cardmgr_instance";
+    public static final String CFG_APPLET_NETKEY_INSTANCE_AID = "applet.aid.netkey_instance";
+    public static final String CFG_APPLET_NETKEY_FILE_AID = "applet.aid.netkey_file";
+    public static final String CFG_APPLET_NETKEY_OLD_INSTANCE_AID = "applet.aid.netkey_old_instance";
+    public static final String CFG_APPLET_NETKEY_OLD_FILE_AID = "applet.aid.netkey_old_file";
+    public static final String CFG_APPLET_SO_PIN = "applet.so_pin";
+    public static final String CFG_APPLET_DELETE_NETKEY_OLD = "applet.delete_old";
+    public static final String CFG_AUDIT_SELECTED_EVENTS="logging.audit.selected.events";
+    public static final String CFG_AUDIT_NONSELECTABLE_EVENTS="logging.audit.nonselectable.events";
+    public static final String CFG_AUDIT_SELECTABLE_EVENTS="logging.audit.selectable.events";
+    public static final String CFG_AUDIT_BUFFER_SIZE = "logging.audit.buffer.size";
+    public static final String CFG_AUDIT_FLUSH_INTERVAL = "logging.audit.flush.interval";
+    public static final String CFG_AUDIT_FILE_TYPE = "logging.audit.file.type";
+    public static final String CFG_DEBUG_FILE_TYPE = "logging.debug.file.type";
+    public static final String CFG_ERROR_FILE_TYPE = "logging.error.file.type";
+    public static final String CFG_SELFTEST_FILE_TYPE = "selftests.container.logger.file.type";
+    public static final String CFG_AUDIT_PREFIX = "logging.audit";
+    public static final String CFG_ERROR_PREFIX = "logging.error";
+    public static final String CFG_DEBUG_PREFIX = "logging.debug";
+    public static final String CFG_SELFTEST_PREFIX = "selftests.container.logger";
+    public static final String CFG_TOKENDB_ALLOWED_TRANSITIONS = "tokendb.allowedTransitions";
+    public static final String CFG_OPERATIONS_ALLOWED_TRANSITIONS = "tps.operations.allowedTransitions";
+
+    public static final String CFG_PRINTBUF_FULL = "tps.printBufFull";
+    public static final String CFG_RECV_BUF_SIZE = "tps.recvBufSize";
+    public static final String CFG_AUTHS_ENABLE="auth.enable";
+
+    /* default values */
+    public static final String CFG_DEF_CARDMGR_INSTANCE_AID = "A0000000030000";
+    public static final String CFG_DEF_NETKEY_INSTANCE_AID = "627601FF000000";
+    public static final String CFG_DEF_NETKEY_FILE_AID = "627601FF0000";
+    public static final String CFG_DEF_NETKEY_OLD_INSTANCE_AID = "A00000000101";
+    public static final String CFG_DEF_NETKEY_OLD_FILE_AID = "A000000001";
+    public static final String CFG_DEF_APPLET_SO_PIN = "000000000000";
+
+    /* External reg values */
+
+    public static final String CFG_EXTERNAL_REG = "externalReg";
+
+
+
+    TPS() {
+    }
+
+    public int Initialize(String cfg_path) {
+
+        int rc = -1;
+
+        return rc;
+    }
+
+    public static void main(String[] args) {
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/main/TPSBuffer.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/main/TPSBuffer.java
new file mode 100644
index 0000000..56f57de
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/main/TPSBuffer.java
@@ -0,0 +1,286 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation;
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor,
+// Boston, MA  02110-1301  USA
+//
+// Copyright (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.server.tps.main;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+
+public class TPSBuffer {
+
+     private   byte[]  buf;
+       // int res;
+
+        /**
+         * Creates an empty Buffer.
+         */
+      public TPSBuffer() {
+          buf = new byte[0];
+      }
+
+      public TPSBuffer(String str) {
+
+          if(str != null) {
+              buf = Util.Str2ByteArray(str);
+          } else {
+              buf = new byte[0];
+          }
+      }
+
+        /**
+         * Creates a Buffer of length 'len', with each byte initialized to 'b'.
+         */
+       public  TPSBuffer( int len, byte b) {
+            buf = new byte[len];
+            Arrays.fill(buf, b);
+        }
+
+        /**
+         * Creates a Buffer of length 'len', initialized to zeroes.
+         */
+       public TPSBuffer(int len) {
+            buf = new byte[len];
+            Arrays.fill(buf,(byte)0);
+            len = 0;
+
+        }
+
+        /**
+         * Creates a Buffer of length 'len', initialized from 'buf'. 'buf' must
+         * contain at least 'len' bytes.
+         */
+       public TPSBuffer(byte[] inBuf, int inLen) {
+
+            buf = new byte[inLen];
+            System.arraycopy(inBuf, 0, buf , 0, inLen);
+        }
+
+       public TPSBuffer(TPSBuffer cpy) {
+
+            if(cpy == null) {
+                buf = new byte[0];
+                return;
+            }
+
+            byte[] srcBytes = cpy.toBytesArray();
+
+            int srcLen = srcBytes.length;
+
+            buf = new byte[srcLen];
+
+            System.arraycopy(srcBytes,0, buf, 0, srcLen);
+
+        }
+
+       public byte at(int i) {
+           if (i < 0 || i > size()) {
+               return 0x0;
+           }
+
+           return buf[i];
+       }
+
+        /**
+         * Returns true if the two buffers are the same length and contain
+         * the same byte at each offset.
+         */
+        public boolean equals(TPSBuffer  cmp) {
+
+             byte[] cmpBytes = cmp.toBytesArray();
+
+             if (cmpBytes == null)
+                 return false;
+
+             return Arrays.equals(buf, cmpBytes);
+
+         }
+
+        public void add(TPSBuffer addend) {
+
+             if (addend == null)
+                 return ;
+
+             byte[] addBytes = addend.toBytesArray();
+             addBytes(addBytes);
+         }
+
+        /**
+         * Append operators.
+         */
+
+       public void add(byte b) {
+            byte[] addBytes = new byte[1];
+            addBytes[0] = b;
+
+            addBytes(addBytes);
+        }
+
+        public void addBytes(byte[] addBytes) {
+            if (addBytes == null)
+                return;
+
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+
+            bytes.write(buf, 0, buf.length);
+            bytes.write(addBytes,0,addBytes.length);
+
+            buf = bytes.toByteArray();
+
+        }
+
+        public byte[]  toBytesArray()  {
+            return buf;
+        }
+
+        /**
+         * The length of buffer. The actual amount of space allocated may be
+         * higher--see capacity().
+         */
+        public int size() {
+            return buf.length;
+        }
+
+
+        /**
+         * Sets all bytes in the buffer to 0.
+         */
+        public void zeroize() {
+            Arrays.fill(buf, (byte) 0);
+
+        }
+
+        /**
+         * Changes the length of the Buffer. If 'newLen' is shorter than the
+         * current length, the Buffer is truncated. If 'newLen' is longer, the
+         * new bytes are initialized to 0. If 'newLen' is the same as size(),
+         * this is a no-op.
+         */
+        public void resize(int newLen) {
+            byte[] tmp = new byte[buf.length];
+
+            System.arraycopy(buf, 0, tmp, 0, buf.length);
+
+            buf = new byte[newLen];
+            System.arraycopy(tmp, 0, buf, 0, tmp.length);
+        }
+
+        /**
+         * Returns a new Buffer that is a substring of this Buffer, starting
+         * from offset 'start' and continuing for 'len' bytes. This Buffer
+         * must have size() >= (start + len).
+         */
+        public TPSBuffer substr(int start, int theLen) {
+
+            if ( start <= 0 || theLen <= 0 || ((start + theLen) > buf.length)) {
+                return null;
+            }
+
+            byte[] tmp = new byte[theLen];
+
+            System.arraycopy(buf,start, tmp,0, theLen);
+
+            TPSBuffer ret = new TPSBuffer(tmp, theLen);
+
+            return ret;
+        }
+
+        /**
+         * dump()s this Buffer to stdout.
+         */
+        public void dump() {
+            String newLine = System.getProperty("line.separator");
+            System.out.println(newLine + "Buffer Contents: " + newLine);
+            for(int i = 0 ;i < buf.length ; i ++) {
+                int val = buf[i] &  0xff;
+                System.out.print(  val + " " );
+                if ( ((i % 8 ) == 7)) {
+                    System.out.print(newLine);
+                }
+            }
+            System.out.print(newLine);
+
+        }
+
+        public String toHexString() {
+            final String HEX_DIGITS = "0123456789ABCDEF";
+
+            StringBuffer result = new StringBuffer(buf.length * 2);
+
+            for (int i = 0; i < buf.length; i++)
+            {
+                char c = (char) buf[i];
+
+                result.append(HEX_DIGITS.charAt((c & 0xF0) >> 4));
+                result.append(HEX_DIGITS.charAt(c & 0x0F));
+
+            }
+
+            return result.toString();
+        }
+
+        public static void main(String[] args) {
+
+            byte[] first =  {0x41, 0x42, 0x43 ,0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a };
+            byte[] second = {0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a };
+            byte[] third =  {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a };
+
+            TPSBuffer b1 = new TPSBuffer(first, first.length);
+
+            TPSBuffer b2 = new TPSBuffer(second,second.length);
+
+            System.out.println("Buffer b1");
+            b1.dump();
+
+            System.out.println("Buffer b2");
+            b2.dump();
+
+            b1.addBytes(second);
+
+            System.out.println("Buffer b1 + byte array: ");
+            b1.dump();
+
+            b1.add(b2);
+
+            System.out.println("Buffer b1 with b2 added to it: ");
+            b1.dump();
+
+            TPSBuffer b3 = new TPSBuffer(third, third.length);
+
+            System.out.println("Buffer b3: ");
+            b3.dump();
+
+            TPSBuffer b4 = b3.substr(1, 4);
+
+            System.out.println("Substr of Buffer b3 from 1 length 4: ");
+
+            b4.dump();
+
+            TPSBuffer b5 = new TPSBuffer(b4);
+
+            System.out.println("Buffer b5 instantiated from Buffer b4");
+
+            b5.dump();
+
+            TPSBuffer b6 = new TPSBuffer("A0000000030000");
+            b6.dump();
+
+        }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/main/Util.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/main/Util.java
new file mode 100644
index 0000000..74c9521
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/main/Util.java
@@ -0,0 +1,128 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ */
+package org.dogtagpki.server.tps.main;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+
+
+public class Util {
+
+    public Util() {
+    }
+
+    public static byte[]  Str2ByteArray (String s)  {
+        int len = s.length() / 2;
+
+
+        byte[]  ret = new byte[len];
+
+        for (int i = 0; i < len; i ++) {
+               ret[i] =  (byte) ((byte) Util.hexToBin(s.charAt(i*2)) * 16 +  Util.hexToBin(s.charAt(i*2+1)));
+        }
+
+        return ret;
+    }
+
+    public static int hexToBin(char ch) {
+        if ('0' <= ch && ch <= '9')
+            return ch - '0';
+        if ('A' <= ch && ch <= 'F')
+            return ch - 'A' + 10;
+        if ('a' <= ch && ch <= 'f')
+            return ch - 'a' + 10;
+        return -1;
+    }
+
+    public static String intToHex(int val) {
+
+        return Integer.toHexString(val);
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public static String URIDecode(String encoded) throws UnsupportedEncodingException {
+
+        return URLDecoder.decode(encoded, "UTF-8");
+    }
+
+    public static String URIEncode(String decoded) throws UnsupportedEncodingException {
+
+        return URLEncoder.encode(decoded, "UTF-8");
+    }
+
+    public static byte[] URIDecodeFromHex(String buff) {
+
+        byte[] result = null;
+        byte[] tmp = null;
+
+        int i;
+
+        int len = buff.length();
+        int sum = 0;
+
+        if (len == 0)
+            return null;
+
+        tmp = new byte[len];
+
+        for (i = 0; i < len; i++) {
+            if (buff.charAt(i) == '+') {
+                tmp[sum++] = ' ';
+            } else if (buff.charAt(i) == '%') {
+                tmp[sum++] = (byte) ((hexToBin(buff.charAt(i + 1)) << 4) + hexToBin(buff.charAt(i + 2)));
+                i += 2;
+            } else {
+                tmp[sum++] = (byte) buff.charAt(i);
+            }
+        }
+
+        result = new byte[sum];
+
+        System.arraycopy(tmp, 0, result, 0, sum);
+
+        return result;
+    }
+
+    public static String URIEncodeInHex(byte[] buff) {
+
+        final String HEX_DIGITS = "0123456789ABCDEF";
+
+        StringBuffer result = new StringBuffer(buff.length * 2);
+
+        for (int i = 0; i < buff.length; i++)
+        {
+            char c = (char) buff[i];
+
+            result.append('%');
+            result.append(HEX_DIGITS.charAt((c & 0xF0) >> 4));
+            result.append(HEX_DIGITS.charAt(c & 0x0F));
+
+        }
+
+        return result.toString();
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQRequest.java
new file mode 100644
index 0000000..aefd510
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQRequest.java
@@ -0,0 +1,29 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class ASQRequest extends TPSMessage {
+    public ASQRequest(String question) {
+
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_ASQ_REQUEST));
+        put(QUESTION_NAME,question);
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQResponse.java
new file mode 100644
index 0000000..73abdc3
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ASQResponse.java
@@ -0,0 +1,29 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class ASQResponse extends TPSMessage {
+
+    public ASQResponse(String answer) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_ASQ_RESPONSE));
+        put(QUESTION_NAME,answer);
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/BeginOp.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/BeginOp.java
new file mode 100644
index 0000000..f136f2b
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/BeginOp.java
@@ -0,0 +1,57 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import java.util.Map;
+
+import com.netscape.certsrv.apps.CMS;
+
+public class BeginOp extends TPSMessage {
+    public BeginOp(Op_Type theOp, Map<String,String> theExtensions) {
+
+        CMS.debug("BeingOp op: " + theOp + " extensions: " + theExtensions);
+        put(OPERATION_TYPE_NAME, opTypeToInt(theOp));
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_BEGIN_OP));
+        extensions = theExtensions;
+
+    }
+
+    public Op_Type GetOpType() {
+
+        int opTypeInt =  getInt(OPERATION_TYPE_NAME);
+        return intToOpType(opTypeInt);
+    }
+
+
+    public Map<String,String> GetExtensions() {
+        return extensions;
+    }
+
+    public String getExtension(String extName) {
+
+        String result = null;
+
+        if(extName == null)
+            return result;
+
+        return extensions.get(extName);
+
+    }
+
+    private    Map<String, String> extensions;
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/EndOp.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/EndOp.java
new file mode 100644
index 0000000..e89133a
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/EndOp.java
@@ -0,0 +1,43 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import org.dogtagpki.server.tps.processor.TPS_Processor;
+
+public class EndOp extends TPSMessage {
+
+    public static final int  RESULT_GOOD = 0;
+    public static final int  RESULT_ERROR = 1;
+
+
+    public EndOp(Op_Type theOp, int result, TPS_Processor.TPS_Status message) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_END_OP));
+        put(OPERATION_TYPE_NAME, opTypeToInt(theOp));
+        put(RESULT_NAME, result);
+        put(MESSAGE_NAME, TPS_Processor.statusToInt(message));
+    }
+
+    public static void main(String[] args) {
+
+        EndOp end_msg = new EndOp(Op_Type.OP_FORMAT,0,TPS_Processor.TPS_Status.STATUS_NO_ERROR);
+        System.out.println(end_msg.encode());
+
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginRequest.java
new file mode 100644
index 0000000..47bd230
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginRequest.java
@@ -0,0 +1,92 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import java.io.UnsupportedEncodingException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.dogtagpki.server.tps.main.Util;
+
+public class ExtendedLoginRequest extends TPSMessage {
+
+   public ExtendedLoginRequest(int invalid_pw, int blocked, Set<String> params, String title, String description) {
+
+
+        put(INVALID_PWD_NAME, invalid_pw);
+        put(BLOCKED_NAME,blocked);
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_EXTENDED_LOGIN_REQUEST));
+        this.params = params;
+
+    }
+
+    @Override
+
+    public String encode() {
+
+        if(!params.isEmpty()) {
+
+            int i = 0;
+            for(Iterator<String> iter = params.iterator(); iter.hasNext(); ) {
+
+                String curParam = null;
+
+                try {
+                    curParam  = Util.URIEncode(iter.next());
+                } catch (UnsupportedEncodingException e) {
+                    curParam = null;
+                }
+
+                if(curParam  != null && curParam.length() > 0) {
+
+                    String name = "&" + PARAMETER_NAME + Integer.toString(i++);
+                    String value = curParam;
+
+                    put(name, value);
+
+                }
+
+            }
+
+        }
+
+        return  super.encode();
+
+    }
+
+    private Set<String> params;
+
+    public static void main(String[] args) {
+
+        final String title = "LDAP Authentication";
+        final String description = "This authenticates user against the LDAP directory.";
+
+        Set<String> params = new HashSet<String>();
+
+        params.add("id=UID&name=LDAP User ID&desc=LDAP User ID&type=string&option=");
+        params.add("id=PASSWORD&name=LDAP Password&desc=LDAP Password&type=password&option=");
+
+
+        ExtendedLoginRequest ext_login_req = new ExtendedLoginRequest(0, 0, params, title, description);
+
+        System.out.println(ext_login_req.encode());
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginResponse.java
new file mode 100644
index 0000000..e481676
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/ExtendedLoginResponse.java
@@ -0,0 +1,43 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ExtendedLoginResponse extends TPSMessage {
+
+   public ExtendedLoginResponse(String msg) {
+
+        super(msg);
+
+        authParams = new HashMap<String,String>();
+        //ToDo process the actual params
+    }
+
+    public static void main(String[] args) {
+
+
+    }
+
+    public Map<String,String> getAuthParams() {
+        return authParams;
+    }
+
+    private Map<String,String> authParams;
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginRequest.java
new file mode 100644
index 0000000..a340d47
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginRequest.java
@@ -0,0 +1,32 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class LoginRequest extends TPSMessage {
+
+    public LoginRequest(int invalid_pwd, int blocked) {
+
+        put(INVALID_PWD_NAME, invalid_pwd);
+        put(BLOCKED_NAME,blocked);
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_LOGIN_REQUEST));
+
+    }
+
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginResponse.java
new file mode 100644
index 0000000..e7c7fd6
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/LoginResponse.java
@@ -0,0 +1,29 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class LoginResponse  extends TPSMessage {
+
+    public LoginResponse(String uid, String password ) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_LOGIN_RESPONSE));
+        put(SCREEN_NAME_NAME,uid);
+        put(PASSWORD_NAME, password);
+
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinRequest.java
new file mode 100644
index 0000000..24810a8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinRequest.java
@@ -0,0 +1,30 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class NewPinRequest extends TPSMessage {
+    public NewPinRequest(int min_len, int max_len) {
+
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_NEW_PIN_REQUEST));
+
+        put(MINIMUM_LENGTH_NAME,min_len);
+        put(MAXIMUM_LENGTH_NAME, max_len);
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinResponse.java
new file mode 100644
index 0000000..903753f
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/NewPinResponse.java
@@ -0,0 +1,27 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class NewPinResponse  extends TPSMessage {
+    public NewPinResponse(String new_pin) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_NEW_PIN_RESPONSE));
+        put(NEW_PIN_NAME,new_pin);
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdRequest.java
new file mode 100644
index 0000000..d3b2504
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdRequest.java
@@ -0,0 +1,30 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class SecureIdRequest extends TPSMessage {
+    public SecureIdRequest(int pin_required, int next_value) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_SECUREID_REQUEST));
+
+        put(PIN_REQUIRED_NAME,pin_required);
+        put(NEXT_VALUE_NAME, next_value);
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdResponse.java
new file mode 100644
index 0000000..a4bf2c8
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/SecureIdResponse.java
@@ -0,0 +1,28 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+public class SecureIdResponse extends TPSMessage {
+    public SecureIdResponse(String value, String pin) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_SECUREID_RESPONSE));
+
+        put(VALUE_NAME,value);
+        put(PIN_NAME,pin);
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateRequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateRequest.java
new file mode 100644
index 0000000..f38ee91
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateRequest.java
@@ -0,0 +1,38 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class StatusUpdateRequest extends TPSMessage {
+    public StatusUpdateRequest(int status, String info) {
+
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_STATUS_UPDATE_REQUEST));
+        put(STATUS_NAME, status);
+        put(INFO_NAME, info);
+
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+        StatusUpdateRequest req = new StatusUpdateRequest(10,"PROGRESS_APPLET_BLOCK");
+        System.out.println(req.encode());
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateResponse.java
new file mode 100644
index 0000000..d96e865
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/StatusUpdateResponse.java
@@ -0,0 +1,29 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+
+public class StatusUpdateResponse extends TPSMessage {
+
+    public StatusUpdateResponse(int status) {
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_STATUS_UPDATE_RESPONSE));
+        put(STATUS_NAME,status);
+    }
+
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TPSMessage.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TPSMessage.java
new file mode 100644
index 0000000..0981148
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TPSMessage.java
@@ -0,0 +1,525 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.dogtagpki.server.tps.main.Util;
+
+import com.netscape.certsrv.apps.CMS;
+
+/**
+ * @author Endi S. Dewata <edewata@redhat.com>
+ */
+public class TPSMessage {
+
+    public enum Op_Type {
+        OP_ENROLL,
+        OP_UNBLOCK,
+        OP_RESET_PIN,
+        OP_RENEW,
+        OP_FORMAT,
+        OP_UNDEFINED
+    }
+
+    public enum Msg_Type {
+        MSG_UNDEFINED,
+        MSG_BEGIN_OP,
+        MSG_LOGIN_REQUEST,
+        MSG_LOGIN_RESPONSE,
+        MSG_SECUREID_REQUEST,
+        MSG_SECUREID_RESPONSE,
+        MSG_ASQ_REQUEST,
+        MSG_ASQ_RESPONSE,
+        MSG_NEW_PIN_REQUEST,
+        MSG_NEW_PIN_RESPONSE,
+        MSG_TOKEN_PDU_REQUEST,
+        MSG_TOKEN_PDU_RESPONSE,
+        MSG_END_OP,
+        MSG_STATUS_UPDATE_REQUEST,
+        MSG_STATUS_UPDATE_RESPONSE,
+        MSG_EXTENDED_LOGIN_REQUEST,
+        MSG_EXTENDED_LOGIN_RESPONSE
+    }
+
+    //HTTP Protocol values
+    public static final String MSG_TYPE_NAME = "msg_type";
+    public static final String OPERATION_TYPE_NAME = "operation";
+    public static final String EXTENSIONS_NAME = "extensions";
+
+    public static final String INVALID_PWD_NAME = "invalid_pw";
+    public static final String BLOCKED_NAME = "blocked";
+    public static final String SCREEN_NAME_NAME = "screen_name";
+    public static final String PASSWORD_NAME = "password";
+    public static final String PIN_REQUIRED_NAME = "pin_required";
+    public static final String NEXT_VALUE_NAME = "next_value";
+    public static final String VALUE_NAME = "value";
+    public static final String PIN_NAME = "pin";
+    public static final String QUESTION_NAME = "question";
+    public static final String ANSWER_NAME = "answer";
+    public static final String MINIMUM_LENGTH_NAME = "minimum_length";
+    public static final String MAXIMUM_LENGTH_NAME = "maximum_length";
+    public static final String NEW_PIN_NAME = "new_pin";
+    public static final String PDU_SIZE_NAME = "pdu_size";
+    public static final String PDU_DATA_NAME = "pdu_data";
+    public static final String RESULT_NAME = "result";
+    public static final String MESSAGE_NAME = "message";
+    public static final String STATUS_NAME = "current_state";
+    public static final String INFO_NAME = "next_task_name";
+    public static final String REQUIRED_PARAMETER_NAME = "required_parameter";
+    public static final String PARAMETER_NAME = "parameter";
+
+    private Map<String, String> map = new LinkedHashMap<String, String>();
+
+    public TPSMessage() {
+    }
+
+    public TPSMessage(String message) {
+        decode(message);
+    }
+
+    public TPSMessage(Map<String, String> map) {
+        this.map.putAll(map);
+    }
+
+    public void put(String key, String value) {
+        map.put(key, value);
+    }
+
+    public void put(String key, Integer value) {
+        map.put(key, value.toString());
+    }
+
+    public void put(String key, byte[] bytes) {
+        StringBuilder sb = new StringBuilder();
+
+        for (byte b : bytes) {
+            sb.append("%");
+            sb.append(String.format("%02X", b));
+        }
+
+        map.put(key, sb.toString());
+    }
+
+    public String get(String name) {
+        String result = null;
+
+        result = map.get(name);
+
+        return result;
+    }
+
+    public int getInt(String name) {
+
+        int result = 0;
+
+        String value = map.get(name);
+
+        if (value != null) {
+            result = Integer.parseInt(value);
+        }
+
+        return result;
+    }
+
+    public static Map<String, String> decodeToMap(String message) {
+
+        Map<String, String> msgMap = new LinkedHashMap<String, String>();
+
+        for (String nvp : message.split("&")) {
+            String[] s = nvp.split("=");
+
+            String key = s[0];
+            String value = s[1];
+
+            // skip message size
+            if (key.equals("s"))
+                continue;
+
+            msgMap.put(key, value);
+        }
+
+        return msgMap;
+
+    }
+
+    public void decode(String message) {
+
+        for (String nvp : message.split("&")) {
+            String[] s = nvp.split("=");
+
+            String key = s[0];
+            String value = s[1];
+
+            // skip message size
+            if (key.equals("s"))
+                continue;
+
+            map.put(key, value);
+        }
+    }
+
+    public String encode() {
+
+        StringBuilder sb = new StringBuilder();
+
+        // encode message type
+        String type = map.get("msg_type");
+        sb.append("msg_type=" + type);
+
+        // encode other parameters
+        for (String key : map.keySet()) {
+
+            if (key.equals("msg_type"))
+                continue;
+
+            String value = map.get(key);
+            sb.append("&" + key + "=" + value);
+        }
+
+        String message = sb.toString();
+
+        // encode message_size
+        return "s=" + message.length() + "&" + message;
+    }
+
+    public String toString() {
+        return map.toString();
+    }
+
+    public Op_Type getOpType() {
+        return intToOpType(getInt(OPERATION_TYPE_NAME));
+    }
+
+    protected Op_Type intToOpType(int i) {
+        Op_Type result = Op_Type.OP_UNDEFINED;
+
+        if (i < 0) {
+            return result;
+        }
+
+        switch (i) {
+
+        case 0:
+            result = Op_Type.OP_UNDEFINED;
+        case 1:
+            result = Op_Type.OP_ENROLL;
+            break;
+        case 2:
+            result = Op_Type.OP_UNBLOCK;
+            break;
+        case 3:
+            result = Op_Type.OP_RESET_PIN;
+            break;
+        case 4:
+            result = Op_Type.OP_RENEW;
+            break;
+        case 5:
+            result = Op_Type.OP_FORMAT;
+            break;
+        default:
+            result = Op_Type.OP_UNDEFINED;
+            break;
+
+        }
+
+        return result;
+    }
+
+    protected int opTypeToInt(Op_Type op) {
+        int result = 0;
+
+        switch (op) {
+
+        case OP_ENROLL:
+            result = 1;
+            break;
+        case OP_UNBLOCK:
+            result = 2;
+            break;
+        case OP_RESET_PIN:
+            result = 3;
+            break;
+        case OP_RENEW:
+            result = 4;
+            break;
+        case OP_FORMAT:
+            result = 5;
+            break;
+        case OP_UNDEFINED:
+            result = 0;
+        default:
+            result = 0;
+            break;
+
+        }
+
+        return result;
+    }
+
+    protected Msg_Type intToMsgType(int i) {
+
+        Msg_Type result = Msg_Type.MSG_UNDEFINED;
+
+        if (i <= 1) {
+            return result;
+        }
+
+        switch (i) {
+        case 2:
+            result = Msg_Type.MSG_BEGIN_OP;
+            break;
+        case 3:
+            result = Msg_Type.MSG_LOGIN_REQUEST;
+            break;
+        case 4:
+            result = Msg_Type.MSG_LOGIN_RESPONSE;
+            break;
+        case 5:
+            result = Msg_Type.MSG_SECUREID_REQUEST;
+            break;
+        case 6:
+            result = Msg_Type.MSG_SECUREID_RESPONSE;
+            break;
+        case 7:
+            result = Msg_Type.MSG_ASQ_REQUEST;
+            break;
+        case 8:
+            result = Msg_Type.MSG_ASQ_RESPONSE;
+            break;
+        case 9:
+            result = Msg_Type.MSG_TOKEN_PDU_REQUEST;
+            break;
+        case 10:
+            result = Msg_Type.MSG_TOKEN_PDU_RESPONSE;
+            break;
+        case 11:
+            result = Msg_Type.MSG_NEW_PIN_REQUEST;
+            break;
+        case 12:
+            result = Msg_Type.MSG_NEW_PIN_RESPONSE;
+            break;
+        case 13:
+            result = Msg_Type.MSG_END_OP;
+            break;
+        case 14:
+            result = Msg_Type.MSG_STATUS_UPDATE_REQUEST;
+            break;
+        case 15:
+            result = Msg_Type.MSG_STATUS_UPDATE_RESPONSE;
+            break;
+        case 16:
+            result = Msg_Type.MSG_EXTENDED_LOGIN_REQUEST;
+            break;
+        case 17:
+            result = Msg_Type.MSG_EXTENDED_LOGIN_RESPONSE;
+            break;
+
+        default:
+            result = Msg_Type.MSG_UNDEFINED;
+            break;
+        }
+
+        return result;
+    }
+
+    protected int msgTypeToInt(Msg_Type type) {
+
+        int result = 0;
+
+        switch (type) {
+        case MSG_BEGIN_OP:
+            result = 2;
+            break;
+        case MSG_LOGIN_REQUEST:
+            result = 3;
+            break;
+        case MSG_LOGIN_RESPONSE:
+            result = 4;
+            break;
+        case MSG_SECUREID_REQUEST:
+            result = 5;
+            break;
+        case MSG_SECUREID_RESPONSE:
+            result = 6;
+            break;
+        case MSG_ASQ_REQUEST:
+            result = 7;
+            break;
+        case MSG_ASQ_RESPONSE:
+            result = 8;
+            break;
+        case MSG_TOKEN_PDU_REQUEST:
+            result = 9;
+            break;
+        case MSG_TOKEN_PDU_RESPONSE:
+            result = 10;
+            break;
+        case MSG_NEW_PIN_REQUEST:
+            result = 11;
+            break;
+        case MSG_NEW_PIN_RESPONSE:
+            result = 12;
+            break;
+        case MSG_END_OP:
+            result = 13;
+            break;
+        case MSG_STATUS_UPDATE_REQUEST:
+            result = 14;
+            break;
+        case MSG_STATUS_UPDATE_RESPONSE:
+            result = 15;
+            break;
+        case MSG_EXTENDED_LOGIN_REQUEST:
+            result = 16;
+            break;
+        case MSG_EXTENDED_LOGIN_RESPONSE:
+            result = 17;
+            break;
+
+        default:
+            result = 0;
+            break;
+        }
+
+        return result;
+    }
+
+    private TPSMessage createMessage() {
+
+        TPSMessage result = null;
+
+        String msg_type = get(MSG_TYPE_NAME);
+        String op_type = get(OPERATION_TYPE_NAME);
+        String extensions = get(EXTENSIONS_NAME);
+
+        CMS.debug("TPSMessage msg_type: " + msg_type);
+        CMS.debug("TPSMessage operation: " + op_type);
+        CMS.debug("TPSMessage extensions: " + extensions);
+
+        String decoded = null;
+        Map<String, String> extsMap = null;
+        if (extensions != null) {
+            try {
+                decoded = Util.URIDecode(extensions);
+            } catch (Exception e) {
+                CMS.debug("TPSMessage.createMessage: Util.URIDecode failed: " + e);
+                return null;
+            }
+
+            System.out.println("decoded extensions : " + decoded);
+
+            extsMap = decodeToMap(decoded);
+        }
+
+        int msg_type_int = 0;
+        int op_type_int = 0;
+
+        try {
+            if (msg_type != null) {
+                msg_type_int = Integer.parseInt(msg_type);
+            }
+            if (op_type != null) {
+                op_type_int = Integer.parseInt(op_type);
+            }
+        } catch (NumberFormatException e) {
+            CMS.debug("TPSMessage.createMessage: Error obtaining msg_type or op_type from incoming message.");
+        }
+
+        Msg_Type val = intToMsgType(msg_type_int);
+        Op_Type op_val = intToOpType(op_type_int);
+
+        switch (val) {
+        case MSG_BEGIN_OP:
+            result = new BeginOp(op_val, extsMap);
+
+            break;
+        case MSG_ASQ_REQUEST:
+            break;
+        case MSG_ASQ_RESPONSE:
+            break;
+        case MSG_END_OP:
+            break;
+        case MSG_EXTENDED_LOGIN_REQUEST:
+            break;
+        case MSG_EXTENDED_LOGIN_RESPONSE:
+            break;
+        case MSG_LOGIN_REQUEST:
+            break;
+        case MSG_LOGIN_RESPONSE:
+            break;
+        case MSG_NEW_PIN_REQUEST:
+            break;
+        case MSG_NEW_PIN_RESPONSE:
+            break;
+        case MSG_SECUREID_REQUEST:
+            break;
+        case MSG_SECUREID_RESPONSE:
+            break;
+        case MSG_STATUS_UPDATE_REQUEST:
+            break;
+        case MSG_STATUS_UPDATE_RESPONSE:
+            break;
+        case MSG_TOKEN_PDU_REQUEST:
+            break;
+        case MSG_TOKEN_PDU_RESPONSE:
+            result = new TokenPDUResponse(encode());
+            break;
+        case MSG_UNDEFINED:
+            return result;
+        default:
+            return result;
+
+        }
+
+        return result;
+
+    }
+
+    public static TPSMessage createMessage(String message) {
+
+        CMS.debug("TPSMessage.createMessage: message: " + message);
+
+        TPSMessage new_msg = null;
+        TPSMessage returnMsg = null;
+
+        new_msg = new TPSMessage(message);
+
+        if (new_msg != null) {
+            returnMsg = new_msg.createMessage();
+        }
+
+        return returnMsg;
+    }
+
+    public Msg_Type GetType() {
+
+        int res = getInt(MSG_TYPE_NAME);
+        return intToMsgType(res);
+    }
+
+    public static void main(String[] args) {
+        String encoded = "s=204&msg_type=2&operation=5&extensions=tokenType%3DuserKey%26clientVersion%3DESC+1%2E0%2E1%26tokenATR%3D3BFF1400FF8131FE458025A00000005657534336353003003B%26statusUpdate%3Dtrue%26extendedLoginRequest%3Dtrue%26";
+        BeginOp testMessage = (BeginOp) TPSMessage.createMessage(encoded);
+        System.out.println("Encoded msg: " + testMessage.encode());
+        System.out.println("msg Extensions: " + testMessage.GetExtensions());
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDURequest.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDURequest.java
new file mode 100644
index 0000000..545c98c
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDURequest.java
@@ -0,0 +1,61 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import org.dogtagpki.server.tps.apdu.APDU;
+import org.dogtagpki.server.tps.apdu.Select_APDU;
+import org.dogtagpki.server.tps.main.TPSBuffer;
+import org.dogtagpki.server.tps.main.Util;
+
+public class TokenPDURequest extends TPSMessage {
+
+    public TokenPDURequest(APDU apdu) {
+
+        put(MSG_TYPE_NAME, msgTypeToInt(Msg_Type.MSG_TOKEN_PDU_REQUEST));
+
+        if(apdu != null) {
+
+            TPSBuffer encoding = apdu.GetEncoding();
+            int apduSize = encoding.size();
+
+            String apdu_value = Util.URIEncodeInHex(encoding.toBytesArray());
+
+            put(PDU_SIZE_NAME,apduSize);
+            put(PDU_DATA_NAME,apdu_value);
+
+        }
+
+    }
+
+    public static void main(String[] args) {
+
+        Select_APDU apdu = null;
+
+        byte[] select_aid = { (byte) 0xa0, 0x0, 0x0, 0x0, 0x3, 0x0 , 0x0 };
+
+        TPSBuffer select = new TPSBuffer(select_aid, select_aid.length);
+
+        apdu = new Select_APDU((byte) 0x4,(byte) 0x0, select);
+
+        TokenPDURequest  request = new TokenPDURequest(apdu);
+
+        System.out.println(request.encode());
+
+     }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDUResponse.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDUResponse.java
new file mode 100644
index 0000000..4daef81
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/msg/TokenPDUResponse.java
@@ -0,0 +1,70 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.msg;
+
+import org.dogtagpki.server.tps.apdu.APDU_Response;
+import org.dogtagpki.server.tps.main.TPSBuffer;
+import org.dogtagpki.server.tps.main.Util;
+
+public class TokenPDUResponse extends TPSMessage {
+    public TokenPDUResponse(String message) {
+
+        super(message);
+        response = null;
+
+
+        String size = get(PDU_SIZE_NAME);
+        String apduData = get(PDU_DATA_NAME);
+
+        int sizeI = Integer.parseInt(size);
+
+        byte[] decoded_pdu_data = Util.URIDecodeFromHex(apduData);
+
+        if( decoded_pdu_data.length ==  sizeI ) {
+
+            TPSBuffer responseBuffer = new TPSBuffer(decoded_pdu_data, decoded_pdu_data.length);
+
+            response = new APDU_Response(responseBuffer);
+
+        }
+
+
+    }
+
+
+    private APDU_Response response;
+
+    public APDU_Response GetResponseAPDU() {
+        return response;
+    }
+
+    public static void main(String[] args) {
+
+        String pdu_data = "s=46&msg_type=10&pdu_size=6&pdu_data=R%B3F%85%90%00";
+        TokenPDUResponse msg = new TokenPDUResponse(pdu_data);
+
+        System.out.println(msg.encode());
+
+        String pdu_data1 = "s=38&msg_type=10&pdu_size=2&pdu_data=%90%00";
+        TokenPDUResponse msg1 = new TokenPDUResponse(pdu_data1);
+
+        System.out.println(msg1.encode());
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Format_Processor.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Format_Processor.java
new file mode 100644
index 0000000..c0a6be9
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Format_Processor.java
@@ -0,0 +1,43 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.processor;
+
+import org.dogtagpki.server.tps.TPSSession;
+import org.dogtagpki.server.tps.msg.TPSMessage;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.EBaseException;
+
+public class TPS_Format_Processor extends TPS_Processor {
+
+    public TPS_Format_Processor() {
+
+    }
+
+    @Override
+    public TPS_Status Process(TPSSession session, TPSMessage message) throws EBaseException {
+        CMS.debug("In TPS_Format_Processor.Process.");
+        return super.Format(session,message);
+    }
+
+    public static void main(String[] args) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Processor.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Processor.java
new file mode 100644
index 0000000..33e8cf0
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/TPS_Processor.java
@@ -0,0 +1,546 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.tps.processor;
+
+import org.dogtagpki.server.tps.TPSSession;
+import org.dogtagpki.server.tps.apdu.APDU;
+import org.dogtagpki.server.tps.apdu.APDU_Response;
+import org.dogtagpki.server.tps.apdu.Get_Data_APDU;
+import org.dogtagpki.server.tps.apdu.Get_Status_APDU;
+import org.dogtagpki.server.tps.apdu.Select_APDU;
+import org.dogtagpki.server.tps.engine.TPS;
+import org.dogtagpki.server.tps.main.TPSBuffer;
+import org.dogtagpki.server.tps.main.Util;
+import org.dogtagpki.server.tps.msg.TPSMessage;
+import org.dogtagpki.server.tps.msg.TokenPDURequest;
+import org.dogtagpki.server.tps.msg.TokenPDUResponse;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.base.IConfigStore;
+
+public abstract class TPS_Processor {
+
+    public enum TPS_Status {
+        STATUS_NO_ERROR,
+        STATUS_ERROR_SNAC,
+        STATUS_ERROR_SEC_INIT_UPDATE,
+        STATUS_ERROR_CREATE_CARDMGR,
+        STATUS_ERROR_MAC_RESET_PIN_PDU,
+        STATUS_ERROR_MAC_CERT_PDU,
+        STATUS_ERROR_MAC_LIFESTYLE_PDU,
+        STATUS_ERROR_MAC_ENROLL_PDU,
+        STATUS_ERROR_READ_OBJECT_PDU,
+        STATUS_ERROR_BAD_STATUS,
+        STATUS_ERROR_CA_RESPONSE,
+        STATUS_ERROR_READ_BUFFER_OVERFLOW,
+        STATUS_ERROR_TOKEN_RESET_PIN_FAILED,
+        STATUS_ERROR_CONNECTION,
+        STATUS_ERROR_LOGIN,
+        STATUS_ERROR_DB,
+        STATUS_ERROR_TOKEN_DISABLED,
+        STATUS_ERROR_SECURE_CHANNEL,
+        STATUS_ERROR_MISCONFIGURATION,
+        STATUS_ERROR_UPGRADE_APPLET,
+        STATUS_ERROR_KEY_CHANGE_OVER,
+        STATUS_ERROR_EXTERNAL_AUTH,
+        STATUS_ERROR_DEFAULT_TOKENTYPE_NOT_FOUND,
+        STATUS_ERROR_DEFAULT_TOKENTYPE_PARAMS_NOT_FOUND,
+        STATUS_ERROR_PUBLISH,
+        STATUS_ERROR_LDAP_CONN,
+        STATUS_ERROR_DISABLED_TOKEN,
+        STATUS_ERROR_NOT_PIN_RESETABLE,
+        STATUS_ERROR_CONN_LOST,
+        STATUS_ERROR_CREATE_TUS_TOKEN_ENTRY,
+        STATUS_ERROR_NO_SUCH_TOKEN_STATE,
+        STATUS_ERROR_NO_SUCH_LOST_REASON,
+        STATUS_ERROR_UNUSABLE_TOKEN_KEYCOMPROMISE,
+        STATUS_ERROR_INACTIVE_TOKEN_NOT_FOUND,
+        STATUS_ERROR_HAS_AT_LEAST_ONE_ACTIVE_TOKEN,
+        STATUS_ERROR_CONTACT_ADMIN,
+        STATUS_ERROR_RECOVERY_IS_PROCESSED,
+        STATUS_ERROR_RECOVERY_FAILED,
+        STATUS_ERROR_NO_OPERATION_ON_LOST_TOKEN,
+        STATUS_ERROR_KEY_ARCHIVE_OFF,
+        STATUS_ERROR_NO_TKS_CONNID,
+        STATUS_ERROR_UPDATE_TOKENDB_FAILED,
+        STATUS_ERROR_REVOKE_CERTIFICATES_FAILED,
+        STATUS_ERROR_NOT_TOKEN_OWNER,
+        STATUS_ERROR_RENEWAL_IS_PROCESSED,
+        STATUS_ERROR_RENEWAL_FAILED
+    };
+
+    public static final int RESULT_NO_ERROR = 0;
+    public static final int RESULT_ERROR = -1;
+
+    public static final int CPLC_DATA_SIZE = 47;
+    public static final int CPLC_MSN_INDEX = 41;
+    public static final int CPLC_MSN_SIZE  = 4;
+
+    TPS_Processor() {
+    }
+
+    TPS_Status Process(TPSSession session, TPSMessage beginMsg) throws EBaseException {
+        return TPS_Status.STATUS_NO_ERROR;
+    }
+
+    TPSBuffer extractTokenNSM(TPSBuffer cplc_data) {
+        if (cplc_data == null || cplc_data.size() < CPLC_DATA_SIZE) {
+            CMS.debug("TPS_Processor.extractTokenMSN: cplc_data: invalid length.");
+            return null;
+        }
+
+        TPSBuffer token_msn = cplc_data.substr(CPLC_MSN_INDEX, CPLC_MSN_SIZE);
+
+        return token_msn;
+
+    }
+
+    TPSBuffer extractTokenCUID(TPSBuffer cplc_data) {
+
+        if (cplc_data == null || cplc_data.size() < 47) {
+            CMS.debug("TPS_Processor.extractTokenCUID: cplc_data: invalid length.");
+            return null;
+        }
+
+        TPSBuffer token1 = cplc_data.substr(3, 4);
+        TPSBuffer token2 = cplc_data.substr(19, 2);
+        TPSBuffer token3 = cplc_data.substr(15, 4);
+
+        TPSBuffer token_cuid = new TPSBuffer();
+
+        token_cuid.add(token1);
+        token_cuid.add(token2);
+        token_cuid.add(token3);
+
+        return token_cuid;
+
+    }
+
+    int SelectCardManager(TPSSession session, String prefix, String tokenType) {
+
+        return 0;
+    }
+
+    /**
+     * Select applet.
+     *
+     * Global Platform Open Platform Card Specification
+     * Version 2.0.1 Page 9-22
+     *
+     * Sample Data:
+     *
+     * _____________ CLA
+     * | __________ INS
+     * | | _______ P1
+     * | | | ____ P2
+     * | | | | _ Len
+     * | | | | |
+     * 00 A4 04 00 07
+     * 53 4C 42 47 49 4E 41
+     *
+     */
+
+    int SelectApplet(TPSSession session, byte p1, byte p2, TPSBuffer aid)  {
+
+        CMS.debug("In TPS_Processor.SelectApplet.");
+        int rc = RESULT_ERROR;
+
+        //Test data until we can read the CFG
+
+        if (aid.size() == 0) {
+            return RESULT_ERROR;
+        }
+
+        Select_APDU select_apdu = new Select_APDU(p1, p2, aid);
+
+        APDU_Response respApdu = HandleAPDURequest(session, select_apdu);
+
+        if(checkTokenPDUResponse(respApdu) == true) {
+            rc = RESULT_NO_ERROR;
+        }
+
+        return rc;
+
+    }
+
+    TPSBuffer GetStatus(TPSSession session) {
+
+        CMS.debug("In TPS_Processor.GetStatus.");
+
+        TPSBuffer result = null;
+
+        Get_Status_APDU get_status_apdu = new Get_Status_APDU();
+
+        APDU_Response respApdu = HandleAPDURequest(session, get_status_apdu);
+
+        if( respApdu != null) {
+            result = respApdu.GetData();
+        }
+
+        return result;
+    }
+
+    APDU_Response HandleAPDURequest(TPSSession session, APDU apdu) {
+        APDU_Response response = null;
+
+        if(session == null || apdu == null) {
+            return response;
+        }
+
+        TokenPDURequest request_msg = new TokenPDURequest(apdu);
+
+        try {
+            session.WriteMsg(request_msg);
+        } catch (Exception e) {
+            CMS.debug("TPS_Processor.HandleAPDURequest failed WriteMsg: " + e.toString());
+            return response;
+
+        }
+
+        TokenPDUResponse response_msg = null;
+
+        try {
+            response_msg = (TokenPDUResponse) session.ReadMsg();
+        } catch (Exception e) {
+            CMS.debug("TPS_Processor.HandleAPDURequest failed ReadMsg: " + e.toString());
+            return response;
+
+        }
+
+        response = response_msg.GetResponseAPDU();
+
+        if (checkTokenPDUResponse(response) == true) {
+            CMS.debug("TPS_Processor.HandleAPDURequest : apdu response is success");
+        } else {
+            CMS.debug("TPS_Processor.HandleAPDURequest: apdu response is failure.");
+        }
+
+        return response;
+
+    }
+
+    TPSBuffer GetCplcData(TPSSession session) {
+        CMS.debug("In TPS_Processor.GetData");
+        TPSBuffer result = null;
+
+        Get_Data_APDU get_data_apdu = new Get_Data_APDU();
+
+        APDU_Response respApdu = HandleAPDURequest(session, get_data_apdu);
+
+        result = respApdu.GetData();
+
+        return result;
+
+    }
+
+    TPS_Status Format(TPSSession session, TPSMessage message) throws EBaseException {
+
+        IConfigStore configStore = CMS.getConfigStore();
+
+        String CardManagerAID = null;
+        String NetKeyAID = null;
+
+        String External_Reg_Cfg = TPS.CFG_EXTERNAL_REG + "." + "enable";
+        boolean isExternalReg = false;
+
+        try {
+            CardManagerAID = configStore.getString(TPS.CFG_APPLET_CARDMGR_INSTANCE_AID,
+                    TPS.CFG_DEF_CARDMGR_INSTANCE_AID);
+            NetKeyAID = configStore.getString(TPS.CFG_APPLET_NETKEY_INSTANCE_AID, TPS.CFG_DEF_NETKEY_INSTANCE_AID);
+            CMS.debug("In TPS_Processor.Format. CardManagerAID: " + CardManagerAID + " NetKeyAID: " + NetKeyAID);
+            this.isExternalReg = configStore.getBoolean(External_Reg_Cfg,false);
+            CMS.debug("In TPS_Processor.Format isExternalReg: " + isExternalReg);
+        } catch (EBaseException e1) {
+            CMS.debug("In TPS_Processor.Format: Error obtaining config values.");
+            e1.printStackTrace();
+            throw new EBaseException("TPS error getting config values from config store.");
+        }
+
+        TPS_Status ret = TPS_Status.STATUS_NO_ERROR;
+
+        TPSBuffer aidBuf = new TPSBuffer(CardManagerAID);
+
+        int select_rc = RESULT_NO_ERROR;
+
+        select_rc = SelectApplet(session, (byte) 0x04, (byte) 0x00, aidBuf);
+
+        if (select_rc ==  RESULT_ERROR) {
+            ret = TPS_Status.STATUS_ERROR_SECURE_CHANNEL;
+            return ret;
+        }
+
+        TPSBuffer cplc_data = GetCplcData(session);
+
+        if (cplc_data == null) {
+            ret = TPS_Status.STATUS_ERROR_SECURE_CHANNEL;
+            return ret;
+        } else {
+            CMS.debug("cplc_data: " + cplc_data.toString());
+        }
+
+        TPSBuffer token_cuid = extractTokenCUID(cplc_data);
+
+        String cuid = null;
+
+        if (token_cuid != null) {
+            cuid = token_cuid.toHexString();
+            CMS.debug("TPS_Processor.Format: token_cuid str: " + cuid);
+        } else {
+            ret = TPS_Status.STATUS_ERROR_SECURE_CHANNEL;
+            return ret;
+        }
+
+        TPSBuffer token_msn = extractTokenNSM(cplc_data);
+
+        String msn = null;
+
+        if (token_msn != null) {
+            msn = token_msn.toHexString();
+            CMS.debug("TPS_Processor.Format: token_msn str: " + msn);
+        }
+
+        /**
+         * Checks if the netkey has the required applet version.
+         */
+
+        TPSBuffer netkeyAid = new TPSBuffer(NetKeyAID);
+
+        select_rc = RESULT_NO_ERROR;
+
+        select_rc = SelectApplet(session, (byte) 0x04, (byte) 0x00, netkeyAid);
+
+        if (select_rc == RESULT_ERROR) {
+            ret = TPS_Status.STATUS_ERROR_SECURE_CHANNEL;
+            return ret;
+        }
+
+        TPSBuffer status = GetStatus(session);
+
+        byte major_version = 0x0;
+        byte minor_version = 0x0;
+        byte app_major_version = 0x0;
+        byte app_minor_version = 0x0;
+
+        if (status != null) {
+            CMS.debug("TPS_Processor.Format: status: " + status.toHexString());
+            major_version = status.at(0);
+            minor_version = status.at(1);
+            app_major_version = status.at(2);
+            app_minor_version = status.at(3);
+            CMS.debug("TPS_Processor.Format: major_version " + major_version + " minor_version: " + minor_version
+                    + " app_major_version: " + app_major_version + " app_minor_version: " + app_minor_version);
+
+        } else {
+            ret = TPS_Status.STATUS_ERROR_SECURE_CHANNEL;
+        }
+
+        if(isExternalReg) {
+            //ToDo, do some external Reg stuff along with authentication
+        } else {
+            //ToDo, Do some authentication
+        }
+
+        return ret;
+    }
+
+    public static int statusToInt(TPS_Status status) {
+
+        int result = 0;
+
+        switch (status) {
+        case STATUS_NO_ERROR:
+            result = 0;
+            break;
+        case STATUS_ERROR_SNAC:
+            result = 1;
+            break;
+        case STATUS_ERROR_SEC_INIT_UPDATE:
+            result = 2;
+            break;
+        case STATUS_ERROR_CREATE_CARDMGR:
+            result = 3;
+            break;
+        case STATUS_ERROR_MAC_RESET_PIN_PDU:
+            result = 4;
+            break;
+        case STATUS_ERROR_MAC_CERT_PDU:
+            result = 5;
+            break;
+        case STATUS_ERROR_MAC_LIFESTYLE_PDU:
+            result = 6;
+            break;
+        case STATUS_ERROR_MAC_ENROLL_PDU:
+            result = 7;
+            break;
+        case STATUS_ERROR_READ_OBJECT_PDU:
+            result = 8;
+            break;
+        case STATUS_ERROR_BAD_STATUS:
+            result = 9;
+            break;
+        case STATUS_ERROR_CA_RESPONSE:
+            result = 10;
+            break;
+        case STATUS_ERROR_READ_BUFFER_OVERFLOW:
+            result = 11;
+            break;
+        case STATUS_ERROR_TOKEN_RESET_PIN_FAILED:
+            result = 12;
+            break;
+        case STATUS_ERROR_CONNECTION:
+            result = 13;
+            break;
+        case STATUS_ERROR_LOGIN:
+            result = 14;
+            break;
+        case STATUS_ERROR_DB:
+            result = 15;
+            break;
+        case STATUS_ERROR_TOKEN_DISABLED:
+            result = 16;
+            break;
+        case STATUS_ERROR_SECURE_CHANNEL:
+            result = 17;
+            break;
+        case STATUS_ERROR_MISCONFIGURATION:
+            result = 18;
+            break;
+        case STATUS_ERROR_UPGRADE_APPLET:
+            result = 19;
+            break;
+        case STATUS_ERROR_KEY_CHANGE_OVER:
+            result = 20;
+            break;
+        case STATUS_ERROR_EXTERNAL_AUTH:
+            result = 21;
+            break;
+        case STATUS_ERROR_DEFAULT_TOKENTYPE_NOT_FOUND:
+            result = 22;
+            break;
+        case STATUS_ERROR_DEFAULT_TOKENTYPE_PARAMS_NOT_FOUND:
+            result = 23;
+            break;
+        case STATUS_ERROR_PUBLISH:
+            result = 24;
+            break;
+        case STATUS_ERROR_LDAP_CONN:
+            result = 25;
+            break;
+        case STATUS_ERROR_DISABLED_TOKEN:
+            result = 26;
+            break;
+        case STATUS_ERROR_NOT_PIN_RESETABLE:
+            result = 27;
+            break;
+        case STATUS_ERROR_CONN_LOST:
+            result = 28;
+            break;
+        case STATUS_ERROR_CREATE_TUS_TOKEN_ENTRY:
+            result = 29;
+            break;
+        case STATUS_ERROR_NO_SUCH_TOKEN_STATE:
+            result = 30;
+            break;
+        case STATUS_ERROR_NO_SUCH_LOST_REASON:
+            result = 31;
+            break;
+        case STATUS_ERROR_UNUSABLE_TOKEN_KEYCOMPROMISE:
+            result = 32;
+            break;
+        case STATUS_ERROR_INACTIVE_TOKEN_NOT_FOUND:
+            result = 33;
+            break;
+        case STATUS_ERROR_HAS_AT_LEAST_ONE_ACTIVE_TOKEN:
+            result = 34;
+            break;
+        case STATUS_ERROR_CONTACT_ADMIN:
+            result = 35;
+            break;
+        case STATUS_ERROR_RECOVERY_IS_PROCESSED:
+            result = 36;
+            break;
+        case STATUS_ERROR_RECOVERY_FAILED:
+            result = 37;
+            break;
+        case STATUS_ERROR_NO_OPERATION_ON_LOST_TOKEN:
+            result = 38;
+            break;
+        case STATUS_ERROR_KEY_ARCHIVE_OFF:
+            result = 39;
+            break;
+        case STATUS_ERROR_NO_TKS_CONNID:
+            result = 40;
+            break;
+        case STATUS_ERROR_UPDATE_TOKENDB_FAILED:
+            result = 41;
+            break;
+        case STATUS_ERROR_REVOKE_CERTIFICATES_FAILED:
+            result = 42;
+            break;
+        case STATUS_ERROR_NOT_TOKEN_OWNER:
+            result = 43;
+            break;
+        case STATUS_ERROR_RENEWAL_IS_PROCESSED:
+            result = 44;
+            break;
+        case STATUS_ERROR_RENEWAL_FAILED:
+            result = 45;
+            break;
+        default:
+            break;
+        }
+
+        return result;
+
+    }
+
+    boolean checkTokenPDUResponse(APDU_Response response) {
+
+        boolean result = false;
+
+        if (response == null)
+            return result;
+
+        byte sw1 = response.GetSW1();
+        byte sw2 = response.GetSW2();
+
+        int int1 = sw1 & 0xff;
+        int int2 = sw2 & 0xff;
+
+        CMS.debug("checkTokenPDUResponse: sw1: " +  "0x" + Util.intToHex(int1) + " sw2: " + "0x" + Util.intToHex(int2));
+
+        if (sw1 == (byte) 0x90 && sw2 == 0x0)
+            result = true;
+
+        return result;
+    }
+
+
+    private boolean isExternalReg = false;
+
+    public boolean getIsExternalReg() {
+        return isExternalReg;
+    }
+
+
+    public static void main(String[] args) {
+    }
+
+
+}
-- 
1.8.3.1

