Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cc_binary {
vendor: true,
relative_install_path: "hw",
srcs: [
"provision_tool.cpp",
"ProvisionTool.cpp",
],
shared_libs: [
"libdl",
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 10 additions & 5 deletions provisioning_tool/README.md → ProvisioningTool/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Provisioning tool
This directory contains provisioning tool which helps in provisioning
the secure element by using the APIs exposed by Provision library.
This tool takes the input parameters from json file. A sample
json file is located in this directory with name [sample_json.txt](https://github.com/BKSSMVenkateswarlu/JavaCardKeymaster/blob/master/provisioning_tool/sample_json.txt) for
your reference.
This tool takes the input parameters from json file.

#### Build
This tool can be built along with aosp build. It has dependency on
[libjc_common](https://github.com/BKSSMVenkateswarlu/JavaCardKeymaster/blob/master/HAL/keymaster/Android.bp) and
libjc_provision.
[libjc_common](../HAL/keymaster/Android.bp) and
[libjc_provision](Android.bp).

#### Sample resources for quick testing
A sample json file is located in this directory with name [sample_json.txt](sample_json.txt)
for your reference. Also the required certificates and keys can be found
in [test_resources](test_resources) directory. Copy the certificates and the key into the
emulator/device filesystem in their respective paths mentioned in the
sample_json.txt.

#### Usage
<pre>
Expand Down
File renamed without changes.
Binary file added ProvisioningTool/test_resources/batch_cert.der
Binary file not shown.
Binary file added ProvisioningTool/test_resources/batch_key.der
Binary file not shown.
Binary file added ProvisioningTool/test_resources/ca_cert.der
Binary file not shown.
Binary file added ProvisioningTool/test_resources/ca_key.der
Binary file not shown.
Binary file not shown.
Binary file added ProvisioningTool/test_resources/intermediate_key.der
Binary file not shown.
17 changes: 17 additions & 0 deletions TestingTools/JCProxy/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JCProxy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file added TestingTools/JCProxy/lib/apduio-RELEASE71.jar
Binary file not shown.
Binary file not shown.
107 changes: 107 additions & 0 deletions TestingTools/JCProxy/src/com/android/javacard/jcproxy/JCProxyMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.android.javacard.jcproxy;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import com.sun.javacard.apduio.CadTransportException;

/**
* This program demonstrates a simple TCP/IP socket server.
*
* @author www.codejava.net
*/
public class JCProxyMain {

public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Port no is expected as argument.");
return;
}

int port = Integer.parseInt(args[0]);
Simulator simulator = new JCardSimulator();

try (ServerSocket serverSocket = new ServerSocket(port)) {
simulator.initaliseSimulator();
if (!simulator.setupKeymasterOnSimulator()) {
System.out.println("Failed to setup Java card keymaster simulator.");
System.exit(-1);
}
byte[] outData;

while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println("\n\n\n\n\n");
System.out.println("------------------------New client connected on "
+ socket.getPort() + "--------------------");
OutputStream output = null;
InputStream isReader = null;
try {
socket.setReceiveBufferSize(1024 * 5);
output = socket.getOutputStream();
isReader = socket.getInputStream();

byte[] inBytes = new byte[65536];
int readLen = 0, index = 0;
System.out.println("Socket input buffer size: "
+ socket.getReceiveBufferSize());
while ((readLen = isReader.read(inBytes, index, 1024 * 5)) > 0) {
if (readLen > 0) {
System.out.println("Bytes read from index (" + index
+ ") socket: " + readLen + " Estimate read: "
+ isReader.available());
byte[] outBytes;

try {
outBytes = simulator.executeApdu(
Arrays.copyOfRange(inBytes, 0, index + readLen));
outData = simulator.decodeDataOut();
System.out.println(
"Return Data " + Utils.byteArrayToHexString(outData));
byte[] finalOutData = new byte[outData.length
+ outBytes.length];
System.arraycopy(outData, 0, finalOutData, 0, outData.length);
System.arraycopy(outBytes, 0, finalOutData, outData.length,
outBytes.length);
output.write(finalOutData);
output.flush();
index = 0;
} catch (IllegalArgumentException e) {
e.printStackTrace();
index = readLen;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (output != null)
output.close();
if (isReader != null)
isReader.close();
socket.close();
}
} catch (IOException e) {
break;
} catch (Exception e) {
break;
}
System.out.println("Client disconnected.");
}
simulator.disconnectSimulator();
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
} catch (CadTransportException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.android.javacard.jcproxy;

import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;

import com.android.javacard.keymaster.KMJCardSimApplet;
import com.licel.jcardsim.smartcardio.CardSimulator;
import com.licel.jcardsim.utils.AIDUtil;

import javacard.framework.AID;

public class JCardSimulator implements Simulator {

private CardSimulator simulator;
ResponseAPDU response;

public JCardSimulator() {
simulator = new CardSimulator();
}

@Override
public void initaliseSimulator() throws Exception {
}

@Override
public void disconnectSimulator() throws Exception {
AID appletAID1 = AIDUtil.create("A000000062");
// Delete i.e. uninstall applet
simulator.deleteApplet(appletAID1);
}

@Override
public boolean setupKeymasterOnSimulator() throws Exception {
AID appletAID1 = AIDUtil.create("A000000062");
simulator.installApplet(appletAID1, KMJCardSimApplet.class);
// Select applet
simulator.selectApplet(appletAID1);
return true;
}

private final byte[] intToByteArray(int value) {
return new byte[] {
(byte) (value >>> 8), (byte) value };
}

@Override
public byte[] executeApdu(byte[] apdu) throws Exception {
System.out.println("Executing APDU = " + Utils.byteArrayToHexString(apdu));
CommandAPDU apduCmd = new CommandAPDU(apdu);
response = simulator.transmitCommand(apduCmd);
System.out.println("Status = "
+ Utils.byteArrayToHexString(intToByteArray(response.getSW())));
return intToByteArray(response.getSW());
}

@Override
public byte[] decodeDataOut() {
return response.getData();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.android.javacard.jcproxy;

public interface Simulator {
byte[] STATUS_OK = Utils.hexStringToByteArray("9000");

void initaliseSimulator() throws Exception;

void disconnectSimulator() throws Exception;

public boolean setupKeymasterOnSimulator() throws Exception;

byte[] executeApdu(byte[] apdu) throws Exception;

byte[] decodeDataOut();
}
28 changes: 28 additions & 0 deletions TestingTools/JCProxy/src/com/android/javacard/jcproxy/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.android.javacard.jcproxy;

public class Utils {

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
if (len % 2 != 0)
throw new IllegalArgumentException("Expecting each byte of 2 char.");
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
}
11 changes: 11 additions & 0 deletions TestingTools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TestingTools
[JCProxy](JCProxy) is a testing tool, which provides a way to communicate with
JCardSimulator from android emulator/device.
It basically opens a socket connection on the port (port mentioned in program arguments)
and listens for the incomming data on this port. This tool uses apduio and JCarsim jars
to validate and transmit the APDUs to the Keymaster Applet.

###Build
Import JCProxy server application either in Eclipse or IntelliJ. Add the provided jars inside
[lib](JCProxy/lib) directory to the project and also add [Keymaster Applet](../Applet) as
dependent project. Add port number (Ex: 8080) as program arguments.