diff options
| author | Jack Magne <jmagne@dhcp-16-213.sjc.redhat.com> | 2014-02-28 18:46:36 -0800 |
|---|---|---|
| committer | Jack Magne <jmagne@dhcp-16-213.sjc.redhat.com> | 2014-03-05 10:03:52 -0800 |
| commit | 429a42b9ecbdecff3fd9c436e13856b6379d19c0 (patch) | |
| tree | a48b03e8ec34ada4185f7f624c2775a0e0387778 /base/common/src | |
| parent | 1d0f670f02c6e8bbbf8dcd432d7fb37dd1a30731 (diff) | |
TPS Rewrite Requested Review Changes:
1. Change the location of some of the classes.
2. Change the file names to reflect naming convention.
3. Change some of the method names to reflect convention.
4. Variable naming changes to reflect convention.
Diffstat (limited to 'base/common/src')
52 files changed, 3936 insertions, 0 deletions
diff --git a/base/common/src/org/dogtagpki/server/tps/TPSServlet.java b/base/common/src/org/dogtagpki/server/tps/TPSServlet.java new file mode 100644 index 000000000..d5c39834e --- /dev/null +++ b/base/common/src/org/dogtagpki/server/tps/TPSServlet.java @@ -0,0 +1,62 @@ +// --- 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 javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.dogtagpki.tps.TPSConnection; + +import com.netscape.certsrv.apps.CMS; + +/** + * @author Endi S. Dewata <edewata@redhat.com> + */ +public class TPSServlet extends HttpServlet { + + private static final long serialVersionUID = -1092227495262381074L; + + 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); + + CMS.debug("TPSConnection created: " + con); + + TPSSession session = new TPSSession(con); + + 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 ..."); + + } +} diff --git a/base/common/src/org/dogtagpki/server/tps/TPSSession.java b/base/common/src/org/dogtagpki/server/tps/TPSSession.java new file mode 100644 index 000000000..70949283c --- /dev/null +++ b/base/common/src/org/dogtagpki/server/tps/TPSSession.java @@ -0,0 +1,139 @@ +// --- 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.processor.TPSFormatProcessor; +import org.dogtagpki.server.tps.processor.TPSProcessor; +import org.dogtagpki.server.tps.processor.TPSProcessor.TPS_Status; +import org.dogtagpki.tps.TPSConnection; +import org.dogtagpki.tps.msg.BeginOp; +import org.dogtagpki.tps.msg.EndOp; +import org.dogtagpki.tps.msg.TPSMessage; + +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 read() 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 write(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 { + TPSProcessor.TPS_Status status = TPS_Status.STATUS_ERROR_BAD_STATUS; + CMS.debug("In TPSSession.process()"); + + TPSMessage firstMsg = read(); + + if (firstMsg == null) { + throw new IOException("Can't create first TPSMessage!"); + } + + TPSMessage.MsgType msg_type = firstMsg.getType(); + TPSMessage.OpType op_type = firstMsg.getOpType(); + + if (msg_type != TPSMessage.MsgType.MSG_BEGIN_OP) { + throw new IOException("Wong first message type read in TPSSession.process!"); + } + + switch (op_type) { + case OP_FORMAT: + + TPSFormatProcessor processor = new TPSFormatProcessor(); + 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 == TPSProcessor.TPS_Status.STATUS_NO_ERROR) { + result = EndOp.RESULT_GOOD; + } + + EndOp endOp = new EndOp(firstMsg.getOpType(), result, status); + + write(endOp); + + CMS.debug("TPSSession.process: leaving: result: " + result + " status: " + status); + + } + + private TPSConnection connection; + +} diff --git a/base/common/src/org/dogtagpki/tps/TPSConnection.java b/base/common/src/org/dogtagpki/tps/TPSConnection.java new file mode 100644 index 000000000..442d28dad --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; + +import org.dogtagpki.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/common/src/org/dogtagpki/tps/apdu/APDU.java b/base/common/src/org/dogtagpki/tps/apdu/APDU.java new file mode 100644 index 000000000..c880ba419 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/APDU.java @@ -0,0 +1,192 @@ +// --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; +import org.dogtagpki.tps.main.Util; + +public abstract class APDU { + + public static int DEFAULT_APDU_SIZE = 32; + + public enum 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() { + data = new TPSBuffer(); + } + + public APDU(APDU otherAPDU) { + data = new TPSBuffer(otherAPDU.getData()); + } + + void SetCLA(byte theCla) { + cla = theCla; + } + + void SetINS(byte theIns) { + ins = theIns; + } + + void SetP1(byte theP1) { + p1 = theP1; + } + + void SetP2(byte theP2) { + p2 = theP2; + } + + void SetData(TPSBuffer theData) { + data = new TPSBuffer(theData); + + } + + public void SetMAC(TPSBuffer theMac) { + mac = theMac; + } + + /** + * 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 encoding = new TPSBuffer(); + + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + + int m_mac_size = 0; + + if (mac != null) { + m_mac_size = mac.size(); + } + + encoding.add((byte) (data.size() + m_mac_size)); + + encoding.add(data); + + if (m_mac_size > 0) { + encoding.add(mac); + } + + return encoding; + } + + public void getDataToMAC(TPSBuffer data) { + //ToDO + } + + public Type getType() { + return Type.APDU_UNDEFINED; + } + + public TPSBuffer getData() { + return data; + } + + public TPSBuffer getMAC() { + return mac; + } + + public byte getCLA() { + return cla; + + } + + public byte getINS() { + return ins; + } + + public byte getP1() { + return p1; + } + + public byte getP2() { + return p2; + } + + public void dump() { + + int claInt = cla & 0xff; + int insInt = ins & 0xff; + int p1Int = p1 & 0xff; + int p2Int = p2 & 0xff; + + System.out.println("APDU: "); + System.out.println("CLA: " + Util.intToHex(claInt)); + System.out.println("INS: " + Util.intToHex(insInt)); + System.out.println("P1: " + Util.intToHex(p1Int)); + System.out.println("P2: " + Util.intToHex(p2Int)); + + data.dump(); + } + + protected byte cla; + protected byte ins; + protected byte p1; + protected byte p2; + + protected TPSBuffer data = null; + protected TPSBuffer plainText = null; + protected TPSBuffer mac = null; + +}; diff --git a/base/common/src/org/dogtagpki/tps/apdu/APDUResponse.java b/base/common/src/org/dogtagpki/tps/apdu/APDUResponse.java new file mode 100644 index 000000000..9cfc645bc --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/APDUResponse.java @@ -0,0 +1,75 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class APDUResponse extends APDU { + + public APDUResponse() { + super(); + + } + + public APDUResponse(TPSBuffer theData) { + SetData(theData); + + } + + public APDUResponse(APDUResponse cpy) { + super(cpy); + } + + public byte getSW1() { + if (data == null) { + return 0x0; + } else { + if (data.size() < 2) { + return 0x0; + } else { + return data.at(data.size() - 2); + } + } + + } + + public byte GetSW2() { + if (data == null) { + return 0x0; + } else { + if (data.size() < 2) { + return 0x0; + } else { + return data.at(data.size() - 1); + } + } + + } + + public static void main(String args[]) { + + APDUResponse resp = new APDUResponse(); + resp.dump(); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/CreateObject.java b/base/common/src/org/dogtagpki/tps/apdu/CreateObject.java new file mode 100644 index 000000000..d7b7f90ea --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/CreateObject.java @@ -0,0 +1,120 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class CreateObject 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 CreateObject(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); + + 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]); + + } + + @Override + public APDU.Type getType() { + return 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 }; + + CreateObject apdu = new CreateObject(object_id, permisisons, 56); + + if (apdu != null) { + + apdu.dump(); + } + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/CreatePin.java b/base/common/src/org/dogtagpki/tps/apdu/CreatePin.java new file mode 100644 index 000000000..078ee666a --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/CreatePin.java @@ -0,0 +1,46 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class CreatePin extends APDU { + + public CreatePin(byte theP1, byte theP2, TPSBuffer theData) { + + SetP1(theP1); + SetP2(theP2); + SetData(theData); + + } + + @Override + public APDU.Type getType() { + return APDU.Type.APDU_CREATE_PIN; + + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/DeleteFile.java b/base/common/src/org/dogtagpki/tps/apdu/DeleteFile.java new file mode 100644 index 000000000..e47b91d19 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/DeleteFile.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class DeleteFile extends APDU { + + public DeleteFile( 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.Type getType() { + return APDU.Type.APDU_DELETE_FILE; + + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ExternalAuthenticate.java b/base/common/src/org/dogtagpki/tps/apdu/ExternalAuthenticate.java new file mode 100644 index 000000000..6204bb219 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ExternalAuthenticate.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.tps.apdu; + +import org.dogtagpki.server.tps.channel.SecureChannel; +import org.dogtagpki.tps.main.TPSBuffer; + +public class ExternalAuthenticate extends APDU { + + public ExternalAuthenticate(TPSBuffer theData, SecureChannel.SecurityLevel sl) { + + SetCLA((byte) 0x84); + SetINS((byte) 0x82); + SetP1((byte) 0x01); + + if (sl == SecureChannel.SecurityLevel.SECURE_MSG_MAC_ENC) { + SetP1((byte) 0x03); + } else if (sl == SecureChannel.SecurityLevel.SECURE_MSG_NONE) { + SetP1((byte) 0x00); + } else { // default + SetP1((byte) 0x01); + } + + SetP2((byte) 0x00); + SetData(theData); + } + + public TPSBuffer GetHostCryptogram() + { + return getData(); + } + + @Override + public APDU.Type getType() + { + return APDU.Type.APDU_EXTERNAL_AUTHENTICATE; + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/FormatMuscleApplet.java b/base/common/src/org/dogtagpki/tps/apdu/FormatMuscleApplet.java new file mode 100644 index 000000000..2ef508e15 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/FormatMuscleApplet.java @@ -0,0 +1,50 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +/* Not sure this is used , provide stub right now. */ + +public class FormatMuscleApplet extends APDU { + public FormatMuscleApplet(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); + + } + + @Override + public APDU.Type getType() { + return APDU.Type.APDU_FORMAT_MUSCLE_APPLET; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GenerateKey.java b/base/common/src/org/dogtagpki/tps/apdu/GenerateKey.java new file mode 100644 index 000000000..da9d3d1da --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GenerateKey.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GenerateKey extends APDU { + + public GenerateKey(byte theP1, byte theP2, byte alg, + int keysize, byte option, + byte type, TPSBuffer wrapped_challenge, TPSBuffer key_check) { + + SetCLA((byte) 0x84); + SetINS((byte) 0x0C); + SetP1(theP1); + SetP2(theP2); + 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); + } + + } + + public APDU.Type getType() { + return APDU.Type.APDU_GENERATE_KEY; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GenerateKeyECC.java b/base/common/src/org/dogtagpki/tps/apdu/GenerateKeyECC.java new file mode 100644 index 000000000..03dca7be9 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GenerateKeyECC.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GenerateKeyECC extends APDU { + + public GenerateKeyECC(byte theP1, byte theP2, byte alg, + int keysize, byte option, + byte type, TPSBuffer wrapped_challenge, TPSBuffer key_check) { + + SetCLA((byte) 0x84); + SetINS((byte) 0x0D); + SetP1(theP1); + SetP2(theP2); + + 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.Type getType() { + return APDU.Type.APDU_GENERATE_KEY_ECC; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GetData.java b/base/common/src/org/dogtagpki/tps/apdu/GetData.java new file mode 100644 index 000000000..344bae675 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GetData.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GetData extends APDU { + + public GetData() + { + SetCLA((byte) 0x80); + SetINS((byte) 0xCA); + SetP1((byte) 0x9F); + SetP2((byte) 0x7F); + } + + @Override + public Type getType() + { + return APDU.Type.APDU_GET_DATA; + } + + @Override + public TPSBuffer getEncoding() + { + TPSBuffer encoding = new TPSBuffer(); + + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + encoding.add((byte) 0x2D); + + return encoding; + } /* Encode */ + + public static void main(String[] args) { + GetData get_data = new GetData(); + + get_data.dump(); + + } +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GetIssuerInfo.java b/base/common/src/org/dogtagpki/tps/apdu/GetIssuerInfo.java new file mode 100644 index 000000000..f8eac6e2d --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GetIssuerInfo.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GetIssuerInfo 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 GetIssuerInfo() + { + SetCLA((byte) 0x84); + SetINS((byte) 0xF6); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + } + + @Override + public APDU.Type getType() + { + return Type.APDU_GET_ISSUERINFO; + } + + @Override + public TPSBuffer getEncoding() + { + TPSBuffer encoding = new TPSBuffer(); + + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + encoding.add((byte) 0xe0); + + return encoding; + } /* Encode */ + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GetStatus.java b/base/common/src/org/dogtagpki/tps/apdu/GetStatus.java new file mode 100644 index 000000000..ec7109dcc --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GetStatus.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GetStatus extends APDU { + public GetStatus() + { + SetCLA((byte) 0xB0); + SetINS((byte) 0x3C); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + } + + @Override + public APDU.Type getType() + { + return Type.APDU_GET_STATUS; + } + + @Override + public TPSBuffer getEncoding() + { + TPSBuffer encoding = new TPSBuffer(); + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + encoding.add((byte) 16); + + return encoding; + } /* Encode */ + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/GetVersion.java b/base/common/src/org/dogtagpki/tps/apdu/GetVersion.java new file mode 100644 index 000000000..f9a2316b2 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/GetVersion.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class GetVersion extends APDU { + public GetVersion() + { + SetCLA((byte) 0xB0); + SetINS((byte) 0x70); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + } + + @Override + public APDU.Type getType() + { + return Type.APDU_GET_VERSION; + } + + @Override + public TPSBuffer getEncoding() + { + TPSBuffer data = new TPSBuffer(); + data.add(cla); + data.add(ins); + data.add(p1); + data.add(p2); + data.add((byte) 4); + + return data; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ImportKey.java b/base/common/src/org/dogtagpki/tps/apdu/ImportKey.java new file mode 100644 index 000000000..898960d95 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ImportKey.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ImportKey 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 ImportKey(byte p1) + { + SetCLA((byte) 0x84); + SetINS((byte) 0x32); + SetP1(p1); + SetP2((byte) 0x00); + + 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); + + } + + @Override + public Type getType() + { + return Type.APDU_IMPORT_KEY; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ImportKeyEnc.java b/base/common/src/org/dogtagpki/tps/apdu/ImportKeyEnc.java new file mode 100644 index 000000000..f754f835e --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ImportKeyEnc.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ImportKeyEnc 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 ImportKeyEnc(byte p1, byte p2, TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0x0A); + SetP1(p1); + SetP2(p2); + + SetData(theData); + } + + public Type getType() + { + return Type.APDU_IMPORT_KEY_ENC; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/InitializeUpdate.java b/base/common/src/org/dogtagpki/tps/apdu/InitializeUpdate.java new file mode 100644 index 000000000..1de8ae049 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/InitializeUpdate.java @@ -0,0 +1,62 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class InitializeUpdate extends APDU { + + /** + * Constructs Initialize Update APDU. + */ + public InitializeUpdate(byte key_version, byte key_index, TPSBuffer theData) { + SetINS((byte) 0x50); + SetP1(key_version); + SetP2(key_index); + SetData(theData); + } + + public TPSBuffer GetHostChallenge() + { + return getData(); + } + + public Type getType() + { + return Type.APDU_INITIALIZE_UPDATE; + } + + public TPSBuffer getEncoding() + { + TPSBuffer data = new TPSBuffer(); + + data.add(cla); + data.add(ins); + data.add(p1); + data.add(p2); + data.add((byte) data.size()); + data.add(data); + + return data; + } /* Encode */ + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/InstallApplet.java b/base/common/src/org/dogtagpki/tps/apdu/InstallApplet.java new file mode 100644 index 000000000..5b644a519 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/InstallApplet.java @@ -0,0 +1,97 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class InstallApplet extends APDU { + + public InstallApplet(TPSBuffer packageAID, TPSBuffer appletAID, + byte appPrivileges, int instanceSize, int appletMemorySize) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xE6); + SetP1((byte) 0x0C); + SetP2((byte) 0x00); + + 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 + } + + /** + * Constructs Install Applet APDU. + */ + public InstallApplet(TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xE6); + SetP1((byte) 0x0C); + SetP2((byte) 0x00); + SetData(theData); + } + + @Override + public Type getType() + { + return Type.APDU_INSTALL_APPLET; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/InstallLoad.java b/base/common/src/org/dogtagpki/tps/apdu/InstallLoad.java new file mode 100644 index 000000000..2fb9fc0e5 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/InstallLoad.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class InstallLoad extends APDU { + + public InstallLoad(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(); + inputData.add((byte) ((fileLen >> 8) & 0xff)); + inputData.add((byte) (fileLen & 0xff)); + + SetData(inputData); + } + + /** + * Constructs Install Load APDU. Used when data was pre-constructed + */ + public InstallLoad(TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xE6); + SetP1((byte) 0x02); + SetP2((byte) 0x00); + SetData(theData); + } + + @Override + public Type getType() + { + return Type.APDU_INSTALL_LOAD; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/Lifecycle.java b/base/common/src/org/dogtagpki/tps/apdu/Lifecycle.java new file mode 100644 index 000000000..c6e48b39f --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/Lifecycle.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.tps.apdu; + +public class Lifecycle extends APDU { + /** + * Constructs Lifecycle APDU. + */ + public Lifecycle(byte lifecycle) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xf0); + SetP1(lifecycle); + SetP2((byte) 0x00); + } + + @Override + public Type getType() + { + return Type.APDU_LIFECYCLE; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ListObjects.java b/base/common/src/org/dogtagpki/tps/apdu/ListObjects.java new file mode 100644 index 000000000..d44f984f1 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ListObjects.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ListObjects extends APDU { + public ListObjects(byte seq) + { + SetCLA((byte) 0xB0); + SetINS((byte) 0x58); + SetP1(seq); + SetP2((byte) 0x00); + } + + @Override + public Type getType() + { + return Type.APDU_LIST_OBJECTS; + } + + public TPSBuffer getEncoding() + { + TPSBuffer encoding = new TPSBuffer(); + + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + encoding.add((byte) 0x0E); + + return encoding; + } /* Encode */ + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ListPins.java b/base/common/src/org/dogtagpki/tps/apdu/ListPins.java new file mode 100644 index 000000000..066fa3043 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ListPins.java @@ -0,0 +1,58 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ListPins extends APDU { + + public ListPins(byte theRet_size) + { + SetCLA((byte) 0xB0); + SetINS((byte) 0x48); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + ret_size = theRet_size; + } + + public Type getType() + { + return Type.APDU_LIST_PINS; + } + + @Override + public TPSBuffer getEncoding() + { + TPSBuffer encoding = new TPSBuffer(); + + encoding.add(cla); + encoding.add(ins); + encoding.add(p1); + encoding.add(p2); + encoding.add(ret_size); + + return encoding; + } /* Encode */ + + private byte ret_size = 0; + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/LoadFile.java b/base/common/src/org/dogtagpki/tps/apdu/LoadFile.java new file mode 100644 index 000000000..99af6fc66 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/LoadFile.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class LoadFile extends APDU { + /** + * Constructs Load File APDU. + */ + public LoadFile(byte refControl, byte blockNum, TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xE8); + SetP1(refControl); + SetP2(blockNum); + + SetData(theData); + } + + @Override + public Type getType() + { + return Type.APDU_LOAD_FILE; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/PutKey.java b/base/common/src/org/dogtagpki/tps/apdu/PutKey.java new file mode 100644 index 000000000..acd2782a8 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/PutKey.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class PutKey extends APDU { + /** + * Constructs Put Key APDU. + */ + public PutKey(byte p1, byte p2, TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xd8); + SetP1(p1); + SetP2(p2); + SetData(theData); + } + + public Type getType() + { + return Type.APDU_PUT_KEY; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ReadBuffer.java b/base/common/src/org/dogtagpki/tps/apdu/ReadBuffer.java new file mode 100644 index 000000000..63d832be5 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ReadBuffer.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ReadBuffer extends APDU { + /** + * Constructs Read Buffer APDU. + */ + public ReadBuffer(int len, int offset) + { + SetCLA((byte) 0x84); + SetINS((byte) 0x08); + SetP1((byte) len); + SetP2((byte) 0x00); + data = new TPSBuffer(); + + data.add((byte) (offset / 256)); + data.add((byte) (offset % 256)); + } + + public Type getType() + { + return Type.APDU_READ_BUFFER; + } + + public int getLen() + { + return p1; + } + + public int GetOffset() + { + byte a = data.at(0); + byte b = data.at(1); + + return ((a << 8) + b); + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/ReadObject.java b/base/common/src/org/dogtagpki/tps/apdu/ReadObject.java new file mode 100644 index 000000000..8550a4143 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/ReadObject.java @@ -0,0 +1,81 @@ +/* --- 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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class ReadObject 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 ReadObject(byte[] object_id, int offset, int len) + { + SetCLA((byte) 0x84); + SetINS((byte) 0x56); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + 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); + } + + @Override + public Type getType() + { + return Type.APDU_READ_OBJECT; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/Select.java b/base/common/src/org/dogtagpki/tps/apdu/Select.java new file mode 100644 index 000000000..0a95c679c --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/Select.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class Select extends APDU { + public Select(byte p1, byte p2, TPSBuffer theData) + { + SetCLA((byte) 0x00); + SetINS((byte) 0xa4); + SetP1(p1); + SetP2(p2); + SetData(theData); + } + + public Type getType() + { + return Type.APDU_SELECT; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/SetIssuerInfo.java b/base/common/src/org/dogtagpki/tps/apdu/SetIssuerInfo.java new file mode 100644 index 000000000..563bcc6e6 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/SetIssuerInfo.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class SetIssuerInfo 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 SetIssuerInfo(byte p1, byte p2, TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0xF4); + SetP1(p1); + SetP2(p2); + SetData(theData); + } + + public TPSBuffer getIssuerInfo() + { + return getData(); + } + + public Type getType() + { + return Type.APDU_SET_ISSUERINFO; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/SetPin.java b/base/common/src/org/dogtagpki/tps/apdu/SetPin.java new file mode 100644 index 000000000..7da0e0729 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/SetPin.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class SetPin 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 SetPin(byte p1, byte p2, TPSBuffer theData) + { + SetCLA((byte) 0x84); + SetINS((byte) 0x04); + SetP1(p1); + SetP2(p2); + SetData(theData); + } + + public TPSBuffer getNewPIN() + { + return getData(); + } + + public Type getType() + { + return Type.APDU_SET_PIN; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/UnblockPin.java b/base/common/src/org/dogtagpki/tps/apdu/UnblockPin.java new file mode 100644 index 000000000..5c66bfc80 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/UnblockPin.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.tps.apdu; + +public class UnblockPin extends APDU { + /** + * Constructs Unblock Pin APDU. + */ + public UnblockPin() + { + SetCLA((byte) 0x84); + SetINS((byte) 0x02); + SetP1((byte) 0x00); + SetP2((byte) 0x00); + } + + public Type getType() + { + return Type.APDU_UNBLOCK_PIN; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/apdu/WriteObject.java b/base/common/src/org/dogtagpki/tps/apdu/WriteObject.java new file mode 100644 index 000000000..b761dca81 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/apdu/WriteObject.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.tps.apdu; + +import org.dogtagpki.tps.main.TPSBuffer; + +public class WriteObject 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 WriteObject(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 Type getType() + { + return Type.APDU_WRITE_OBJECT; + } + +} diff --git a/base/common/src/org/dogtagpki/tps/main/TPSBuffer.java b/base/common/src/org/dogtagpki/tps/main/TPSBuffer.java new file mode 100644 index 000000000..a0a71a601 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/main/TPSBuffer.java @@ -0,0 +1,290 @@ +// --- 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.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) { + + if (inBuf == null) { + buf = new byte[0]; + } + + buf = new byte[inBuf.length]; + System.arraycopy(inBuf, 0, buf, 0, inBuf.length); + } + + 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); + + 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(Util.intToHex(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); + + TPSBuffer b2 = new TPSBuffer(second); + + 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); + + 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/common/src/org/dogtagpki/tps/main/Util.java b/base/common/src/org/dogtagpki/tps/main/Util.java new file mode 100644 index 000000000..deecd5a57 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.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/common/src/org/dogtagpki/tps/msg/ASQRequest.java b/base/common/src/org/dogtagpki/tps/msg/ASQRequest.java new file mode 100644 index 000000000..2c9be5d86 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class ASQRequest extends TPSMessage { + public ASQRequest(String question) { + + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_ASQ_REQUEST)); + put(QUESTION_NAME,question); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/ASQResponse.java b/base/common/src/org/dogtagpki/tps/msg/ASQResponse.java new file mode 100644 index 000000000..bbc7d1bf2 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class ASQResponse extends TPSMessage { + + public ASQResponse(String answer) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_ASQ_RESPONSE)); + put(QUESTION_NAME,answer); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/BeginOp.java b/base/common/src/org/dogtagpki/tps/msg/BeginOp.java new file mode 100644 index 000000000..0b6ab9f1c --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + +import java.util.Map; + +import com.netscape.certsrv.apps.CMS; + +public class BeginOp extends TPSMessage { + public BeginOp(OpType theOp, Map<String,String> theExtensions) { + + CMS.debug("BeingOp op: " + theOp + " extensions: " + theExtensions); + put(OPERATION_TYPE_NAME, opTypeToInt(theOp)); + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_BEGIN_OP)); + extensions = theExtensions; + + } + + public OpType 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/common/src/org/dogtagpki/tps/msg/EndOp.java b/base/common/src/org/dogtagpki/tps/msg/EndOp.java new file mode 100644 index 000000000..40179c51c --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/EndOp.java @@ -0,0 +1,44 @@ +// --- 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.tps.msg; + +import org.dogtagpki.server.tps.processor.TPSProcessor; + + +public class EndOp extends TPSMessage { + + public static final int RESULT_GOOD = 0; + public static final int RESULT_ERROR = 1; + + + public EndOp(OpType theOp, int result, TPSProcessor.TPS_Status message) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_END_OP)); + put(OPERATION_TYPE_NAME, opTypeToInt(theOp)); + put(RESULT_NAME, result); + put(MESSAGE_NAME, TPSProcessor.statusToInt(message)); + } + + public static void main(String[] args) { + + EndOp end_msg = new EndOp(OpType.OP_FORMAT,0,TPSProcessor.TPS_Status.STATUS_NO_ERROR); + System.out.println(end_msg.encode()); + + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/ExtendedLoginRequest.java b/base/common/src/org/dogtagpki/tps/msg/ExtendedLoginRequest.java new file mode 100644 index 000000000..ee979ff34 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/ExtendedLoginRequest.java @@ -0,0 +1,89 @@ +// --- 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.tps.msg; + +import java.io.UnsupportedEncodingException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.dogtagpki.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(MsgType.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/common/src/org/dogtagpki/tps/msg/ExtendedLoginResponse.java b/base/common/src/org/dogtagpki/tps/msg/ExtendedLoginResponse.java new file mode 100644 index 000000000..557c2eb38 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/ExtendedLoginResponse.java @@ -0,0 +1,42 @@ +// --- 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.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/common/src/org/dogtagpki/tps/msg/LoginRequest.java b/base/common/src/org/dogtagpki/tps/msg/LoginRequest.java new file mode 100644 index 000000000..857aaacef --- /dev/null +++ b/base/common/src/org/dogtagpki/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.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(MsgType.MSG_LOGIN_REQUEST)); + + } + + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/LoginResponse.java b/base/common/src/org/dogtagpki/tps/msg/LoginResponse.java new file mode 100644 index 000000000..afde6849b --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class LoginResponse extends TPSMessage { + + public LoginResponse(String uid, String password ) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_LOGIN_RESPONSE)); + put(SCREEN_NAME_NAME,uid); + put(PASSWORD_NAME, password); + + } +} diff --git a/base/common/src/org/dogtagpki/tps/msg/NewPinRequest.java b/base/common/src/org/dogtagpki/tps/msg/NewPinRequest.java new file mode 100644 index 000000000..da4e098c2 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class NewPinRequest extends TPSMessage { + public NewPinRequest(int min_len, int max_len) { + + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_NEW_PIN_REQUEST)); + + put(MINIMUM_LENGTH_NAME,min_len); + put(MAXIMUM_LENGTH_NAME, max_len); + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/NewPinResponse.java b/base/common/src/org/dogtagpki/tps/msg/NewPinResponse.java new file mode 100644 index 000000000..eedb9e336 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class NewPinResponse extends TPSMessage { + public NewPinResponse(String new_pin) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_NEW_PIN_RESPONSE)); + put(NEW_PIN_NAME,new_pin); + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/SecureIdRequest.java b/base/common/src/org/dogtagpki/tps/msg/SecureIdRequest.java new file mode 100644 index 000000000..12e637e70 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class SecureIdRequest extends TPSMessage { + public SecureIdRequest(int pin_required, int next_value) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_SECUREID_REQUEST)); + + put(PIN_REQUIRED_NAME,pin_required); + put(NEXT_VALUE_NAME, next_value); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/SecureIdResponse.java b/base/common/src/org/dogtagpki/tps/msg/SecureIdResponse.java new file mode 100644 index 000000000..47f245c86 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + +public class SecureIdResponse extends TPSMessage { + public SecureIdResponse(String value, String pin) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_SECUREID_RESPONSE)); + + put(VALUE_NAME,value); + put(PIN_NAME,pin); + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/StatusUpdateRequest.java b/base/common/src/org/dogtagpki/tps/msg/StatusUpdateRequest.java new file mode 100644 index 000000000..db742c378 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/StatusUpdateRequest.java @@ -0,0 +1,36 @@ +// --- 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.tps.msg; + +public class StatusUpdateRequest extends TPSMessage { + public StatusUpdateRequest(int status, String info) { + + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_STATUS_UPDATE_REQUEST)); + put(STATUS_NAME, status); + put(INFO_NAME, info); + + } + + public static void main(String[] args) { + + StatusUpdateRequest req = new StatusUpdateRequest(10, "PROGRESS_APPLET_BLOCK"); + System.out.println(req.encode()); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/StatusUpdateResponse.java b/base/common/src/org/dogtagpki/tps/msg/StatusUpdateResponse.java new file mode 100644 index 000000000..1de95b820 --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + + +public class StatusUpdateResponse extends TPSMessage { + + public StatusUpdateResponse(int status) { + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.MSG_STATUS_UPDATE_RESPONSE)); + put(STATUS_NAME,status); + } + + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/TPSMessage.java b/base/common/src/org/dogtagpki/tps/msg/TPSMessage.java new file mode 100644 index 000000000..f656db846 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/TPSMessage.java @@ -0,0 +1,523 @@ +// --- 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.tps.msg; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.dogtagpki.tps.main.Util; + +import com.netscape.certsrv.apps.CMS; + +/** + * @author Endi S. Dewata <edewata@redhat.com> + */ +public class TPSMessage { + + public enum OpType { + OP_ENROLL, + OP_UNBLOCK, + OP_RESET_PIN, + OP_RENEW, + OP_FORMAT, + OP_UNDEFINED + } + + public enum MsgType { + 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 OpType getOpType() { + return intToOpType(getInt(OPERATION_TYPE_NAME)); + } + + protected OpType intToOpType(int i) { + OpType result = OpType.OP_UNDEFINED; + + if (i < 0) { + return result; + } + + switch (i) { + + case 0: + result = OpType.OP_UNDEFINED; + case 1: + result = OpType.OP_ENROLL; + break; + case 2: + result = OpType.OP_UNBLOCK; + break; + case 3: + result = OpType.OP_RESET_PIN; + break; + case 4: + result = OpType.OP_RENEW; + break; + case 5: + result = OpType.OP_FORMAT; + break; + default: + result = OpType.OP_UNDEFINED; + break; + + } + + return result; + } + + protected int opTypeToInt(OpType 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 MsgType intToMsgType(int i) { + + MsgType result = MsgType.MSG_UNDEFINED; + + if (i <= 1) { + return result; + } + + switch (i) { + case 2: + result = MsgType.MSG_BEGIN_OP; + break; + case 3: + result = MsgType.MSG_LOGIN_REQUEST; + break; + case 4: + result = MsgType.MSG_LOGIN_RESPONSE; + break; + case 5: + result = MsgType.MSG_SECUREID_REQUEST; + break; + case 6: + result = MsgType.MSG_SECUREID_RESPONSE; + break; + case 7: + result = MsgType.MSG_ASQ_REQUEST; + break; + case 8: + result = MsgType.MSG_ASQ_RESPONSE; + break; + case 9: + result = MsgType.MSG_TOKEN_PDU_REQUEST; + break; + case 10: + result = MsgType.MSG_TOKEN_PDU_RESPONSE; + break; + case 11: + result = MsgType.MSG_NEW_PIN_REQUEST; + break; + case 12: + result = MsgType.MSG_NEW_PIN_RESPONSE; + break; + case 13: + result = MsgType.MSG_END_OP; + break; + case 14: + result = MsgType.MSG_STATUS_UPDATE_REQUEST; + break; + case 15: + result = MsgType.MSG_STATUS_UPDATE_RESPONSE; + break; + case 16: + result = MsgType.MSG_EXTENDED_LOGIN_REQUEST; + break; + case 17: + result = MsgType.MSG_EXTENDED_LOGIN_RESPONSE; + break; + + default: + result = MsgType.MSG_UNDEFINED; + break; + } + + return result; + } + + protected int msgTypeToInt(MsgType 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."); + } + + MsgType val = intToMsgType(msg_type_int); + OpType 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); + + returnMsg = new_msg.createMessage(); + + return returnMsg; + } + + public MsgType 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/common/src/org/dogtagpki/tps/msg/TokenPDURequest.java b/base/common/src/org/dogtagpki/tps/msg/TokenPDURequest.java new file mode 100644 index 000000000..02f2699fe --- /dev/null +++ b/base/common/src/org/dogtagpki/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.tps.msg; + +import org.dogtagpki.tps.apdu.APDU; +import org.dogtagpki.tps.apdu.Select; +import org.dogtagpki.tps.main.TPSBuffer; +import org.dogtagpki.tps.main.Util; + +public class TokenPDURequest extends TPSMessage { + + public TokenPDURequest(APDU apdu) { + + put(MSG_TYPE_NAME, msgTypeToInt(MsgType.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 = null; + + byte[] select_aid = { (byte) 0xa0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0 }; + + TPSBuffer select = new TPSBuffer(select_aid); + + apdu = new Select((byte) 0x4, (byte) 0x0, select); + + TokenPDURequest request = new TokenPDURequest(apdu); + + System.out.println(request.encode()); + + } + +} diff --git a/base/common/src/org/dogtagpki/tps/msg/TokenPDUResponse.java b/base/common/src/org/dogtagpki/tps/msg/TokenPDUResponse.java new file mode 100644 index 000000000..4059c1b72 --- /dev/null +++ b/base/common/src/org/dogtagpki/tps/msg/TokenPDUResponse.java @@ -0,0 +1,67 @@ +// --- 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.tps.msg; + +import org.dogtagpki.tps.apdu.APDUResponse; +import org.dogtagpki.tps.main.TPSBuffer; +import org.dogtagpki.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); + + response = new APDUResponse(responseBuffer); + + } + + } + + private APDUResponse response; + + public APDUResponse 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()); + + } + +} |
