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 @@ -609,9 +609,13 @@ public short hmacSign(byte[] keyBuf, short keyStart, short keyLength, byte[] dat
}

@Override
public short hmacSign(Object hmacKey, byte[] data, short dataStart, short dataLength,
public short hmacSign(Object key, byte[] data, short dataStart, short dataLength,
byte[] signature, short signatureStart) {
return 0;
if(!(key instanceof KMHmacKey)) {
KMException.throwIt(KMError.INVALID_ARGUMENT);
}
KMHmacKey hmacKey = (KMHmacKey) key;
return hmacSign(hmacKey.getKey(), data, dataStart, dataLength, signature, signatureStart);
}

@Override
Expand Down
47 changes: 36 additions & 11 deletions HAL/SocketTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ bool SocketTransport::openConnection() {
}

bool SocketTransport::sendData(const vector<uint8_t>& inData, vector<uint8_t>& output) {
uint8_t buffer[MAX_RECV_BUFFER_SIZE];
int count = 1;
while (!socketStatus && count++ < 5) {
sleep(1);
Expand All @@ -70,8 +69,13 @@ bool SocketTransport::sendData(const vector<uint8_t>& inData, vector<uint8_t>& o
LOG(ERROR) << "Failed to open socket connection";
return false;
}
// Prepend the input length to the inputData before sending.
vector<uint8_t> inDataPrependedLength;
inDataPrependedLength.push_back(static_cast<uint8_t>(inData.size() >> 8));
inDataPrependedLength.push_back(static_cast<uint8_t>(inData.size() & 0xFF));
inDataPrependedLength.insert(inDataPrependedLength.end(), inData.begin(), inData.end());

if (0 > send(mSocket, inData.data(), inData.size(), 0)) {
if (0 > send(mSocket, inDataPrependedLength.data(), inDataPrependedLength.size(), 0)) {
static int connectionResetCnt = 0; /* To avoid loop */
if (ECONNRESET == errno && connectionResetCnt == 0) {
// Connection reset. Try open socket and then sendData.
Expand All @@ -83,15 +87,7 @@ bool SocketTransport::sendData(const vector<uint8_t>& inData, vector<uint8_t>& o
connectionResetCnt = 0;
return false;
}

ssize_t valRead = read(mSocket, buffer, MAX_RECV_BUFFER_SIZE);
if (0 > valRead) {
LOG(ERROR) << "Failed to read data from socket.";
}
for (size_t i = 0; i < valRead; i++) {
output.push_back(buffer[i]);
}
return true;
return readData(output);
}

bool SocketTransport::closeConnection() {
Expand All @@ -104,4 +100,33 @@ bool SocketTransport::isConnected() {
return socketStatus;
}

bool SocketTransport::readData(vector<uint8_t>& output) {
uint8_t buffer[MAX_RECV_BUFFER_SIZE];
ssize_t expectedResponseLen = 0;
ssize_t totalBytesRead = 0;
// The first 2 bytes in the response contains the expected response length.
do {
size_t i = 0;
ssize_t numBytes = read(mSocket, buffer, MAX_RECV_BUFFER_SIZE);
if (0 > numBytes) {
LOG(ERROR) << "Failed to read data from socket.";
return false;
}
totalBytesRead += numBytes;
if (expectedResponseLen == 0) {
// First two bytes in the response contains the expected response length.
expectedResponseLen |= static_cast<ssize_t>(buffer[1] & 0xFF);
expectedResponseLen |= static_cast<ssize_t>((buffer[0] << 8) & 0xFF00);
// 2 bytes for storing the length.
expectedResponseLen += 2;
i = 2;
}
for (; i < numBytes; i++) {
output.push_back(buffer[i]);
}
} while(totalBytesRead < expectedResponseLen);

return true;
}

} // namespace keymint::javacard
1 change: 1 addition & 0 deletions HAL/SocketTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SocketTransport : public ITransport {
bool isConnected() override;

private:
bool readData(vector<uint8_t>& output);
/**
* Socket instance.
*/
Expand Down
3 changes: 2 additions & 1 deletion ProvisioningTool/include/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class SocketTransport
bool isConnected();

private:
bool readData(std::vector<uint8_t>& output);
SocketTransport() : mSocket(-1), socketStatus(false) {}
/**
* Socket instance.
*/
int mSocket;
bool socketStatus;
};
};
39 changes: 32 additions & 7 deletions ProvisioningTool/src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ bool SocketTransport::sendData(const std::vector<uint8_t>& inData, std::vector<u
std::cout << "Failed to open socket connection";
return false;
}
// Prepend the input length to the inputData before sending.
vector<uint8_t> inDataPrependedLength;
inDataPrependedLength.push_back(static_cast<uint8_t>(inData.size() >> 8));
inDataPrependedLength.push_back(static_cast<uint8_t>(inData.size() & 0xFF));
inDataPrependedLength.insert(inDataPrependedLength.end(), inData.begin(), inData.end());

if (0 > send(mSocket, inData.data(), inData.size(), 0)) {
if (0 > send(mSocket, inDataPrependedLength.data(), inDataPrependedLength.size(), 0)) {
static int connectionResetCnt = 0; /* To avoid loop */
if (ECONNRESET == errno && connectionResetCnt == 0) {
// Connection reset. Try open socket and then sendData.
Expand All @@ -86,14 +91,35 @@ bool SocketTransport::sendData(const std::vector<uint8_t>& inData, std::vector<u
connectionResetCnt = 0;
return false;
}
return readData(output);
}

ssize_t valRead = read(mSocket, buffer, MAX_RECV_BUFFER_SIZE);
if (0 > valRead) {
bool SocketTransport::readData(vector<uint8_t>& output) {
uint8_t buffer[MAX_RECV_BUFFER_SIZE];
ssize_t expectedResponseLen = 0;
ssize_t totalBytesRead = 0;
// The first 2 bytes in the response contains the expected response length.
do {
size_t i = 0;
ssize_t numBytes = read(mSocket, buffer, MAX_RECV_BUFFER_SIZE);
if (0 > numBytes) {
std::cout << "Failed to read data from socket.";
}
for (ssize_t i = 0; i < valRead; i++) {
return false;
}
totalBytesRead += numBytes;
if (expectedResponseLen == 0) {
// First two bytes in the response contains the expected response length.
expectedResponseLen |= static_cast<ssize_t>(buffer[1] & 0xFF);
expectedResponseLen |= static_cast<ssize_t>((buffer[0] << 8) & 0xFF00);
// 2 bytes for storing the length.
expectedResponseLen += 2;
i = 2;
}
for (; i < numBytes; i++) {
output.push_back(buffer[i]);
}
}
} while(totalBytesRead < expectedResponseLen);

return true;
}

Expand All @@ -106,4 +132,3 @@ bool SocketTransport::closeConnection() {
bool SocketTransport::isConnected() {
return socketStatus;
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Date;

import com.sun.javacard.apduio.CadTransportException;
import javacard.framework.Util;

/**
* This program demonstrates a simple TCP/IP socket server.
Expand Down Expand Up @@ -47,33 +48,36 @@ public static void main(String[] args) {

byte[] inBytes = new byte[65536];
int readLen = 0, index = 0;
System.out.println("Socket input buffer size: "
+ socket.getReceiveBufferSize());
short totalLen = 0;
short totalReadLen = 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;
System.out.println("Bytes read from index (" + index + ") socket: " + readLen + " Estimate read: "
+ isReader.available());
if (totalLen == 0) {
// First two bytes holds the actual request length.
totalLen = Util.getShort(inBytes, (short) 0);
totalLen += 2;
}
totalReadLen += readLen;
if (totalReadLen < totalLen) {
// Read from the socket till all the bytes are read.
index += readLen;
continue;
}
simulator.executeApdu(Arrays.copyOfRange(inBytes, (short) 2, totalReadLen));
outData = simulator.decodeDataOut();

byte[] finalOutData = new byte[outData.length + 2];
Util.setShort(finalOutData, (short) 0, (short) outData.length);
System.arraycopy(outData, 0, finalOutData, 2, outData.length);
output.write(finalOutData);
System.out.println("Return Data = " + Utils.byteArrayToHexString(finalOutData));
output.flush();
index = 0;
totalLen = 0;
totalReadLen = 0;
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public byte[] executeApdu(byte[] apdu) throws Exception {

@Override
public byte[] decodeDataOut() {
return response.getData();
byte[] resp = response.getData();
byte[] status = intToByteArray(response.getSW());
byte[] out = new byte[(resp.length + status.length)];
System.arraycopy(resp, 0, out, 0, resp.length);
System.arraycopy(status, 0, out, resp.length, status.length);
return out;
}

}