forked from qoire/ledger4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPDUWrapper.java
More file actions
190 lines (154 loc) · 6.75 KB
/
APDUWrapper.java
File metadata and controls
190 lines (154 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.aion.ledger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import static org.aion.ledger.ByteUtilities.trimHead;
import static org.aion.ledger.ByteUtilities.trimTail;
import static org.aion.ledger.Constants.PACKET_SIZE;
public class APDUWrapper {
public static class SerializedPacket {
public byte[] data;
public int offset;
public SerializedPacket(@Nonnull final byte[] serialized,
@Nonnull final int offset) {
this.data = serialized;
this.offset = offset;
}
}
public static class DeserializedPacket {
public byte[] data;
// total response length of all command data
// this will only be a non-zero value in the first packet
public int totalResponseLength;
public DeserializedPacket(@Nonnull final byte[] data,
@Nonnull final int totalResponseLength) {
this.data = data;
this.totalResponseLength = totalResponseLength;
}
}
public static class DeserializationException extends Exception {
public DeserializationException(String reason) {
super(reason);
}
}
static SerializedPacket serializePacket(@Nonnull final int channel,
@Nonnull final byte[] command,
@Nonnull final int commandOffset,
@Nonnull final int sequenceIdx,
@Nonnull final boolean ble) {
// assert some invariants
// TODO: convert to proper runtime exceptions later
if (channel <= 0) {
throw new IllegalArgumentException("channel must be > 0");
}
if (channel > (2 << 16)) {
throw new IllegalArgumentException("channel must be <= (2 << 16)");
}
if (sequenceIdx > (2 << 16)) {
throw new IllegalArgumentException("sequenceIdx must be <= (2 << 16)");
}
// allocate into 64 byte chunks
final ByteBuffer buf = ByteBuffer.allocate(PACKET_SIZE);
if (!ble) {
buf.putShort((short) channel);
}
buf.put((byte) 0x05);
buf.putShort((short) sequenceIdx);
if (sequenceIdx == 0) {
buf.putShort((short) command.length);
}
// remaining capacity is attributed to command data
final int capacity = Math.min(buf.remaining(), command.length - commandOffset);
final byte[] cmdArr = new byte[capacity];
System.arraycopy(command, commandOffset, cmdArr, 0, capacity);
buf.put(cmdArr);
final byte[] bufArr = buf.array();
return new SerializedPacket(bufArr, capacity);
}
public static byte[] wrapCommandAPDU(@Nonnull final int channel,
@Nonnull final byte[] command,
@Nonnull final boolean ble) {
int commandOffset = 0;
int sequenceIdx = 0;
byte[] outBuf = new byte[0];
while (command.length - commandOffset > 0) {
SerializedPacket packet = serializePacket(channel, command, commandOffset, sequenceIdx, ble);
// TODO: optimize later
outBuf = ByteBuffer.allocate(outBuf.length + packet.data.length)
.put(outBuf).put(packet.data).array();
commandOffset += packet.offset;
sequenceIdx++;
}
return outBuf;
}
public static DeserializedPacket deserializePacket(@Nonnull final int channel,
@Nonnull final byte[] buffer,
@Nonnull final int sequenceIdx,
@Nonnull final boolean ble) throws DeserializationException {
if ((sequenceIdx == 0 && buffer.length < 7) || (sequenceIdx > 0 && buffer.length < 5)) {
// TODO: more meaningful exception
throw new DeserializationException("Cannot deserialize packet, header information missing");
}
final int offset = sequenceIdx * PACKET_SIZE;
// TODO: this can be optimized, offsets anyone?
byte[] trimBuf = trimHead(buffer, offset);
trimBuf = trimTail(trimBuf, trimBuf.length - PACKET_SIZE);
ByteBuffer buf = ByteBuffer.wrap(trimBuf);
if (!ble) {
final int dChannel = buf.getShort() & 0x0000FFFF;
if (dChannel != channel) {
throw new DeserializationException("Invalid channel");
}
}
if (buf.get() != 0x05) {
throw new DeserializationException("Invalid tag");
}
final int dSequenceIdx = buf.getShort() & 0x0000FFFF;
if (dSequenceIdx != sequenceIdx) {
throw new DeserializationException("Invalid sequenceIdx");
}
int totalResponseLength = 0;
if (sequenceIdx == 0) {
totalResponseLength = buf.getShort() & 0x0000FFFF;
}
final byte[] payload = new byte[buf.remaining()];
buf.get(payload);
return new DeserializedPacket(payload, totalResponseLength);
}
@Nullable
public static byte[] unwrapResponseAPDU(final int channel,
@Nonnull byte[] data,
final boolean ble)
throws DeserializationException {
int sequenceIdx = 0;
int totalResponseLength = 0;
ByteBuffer outBuffer = null;
while (true) {
int extraHeaderSize = 0;
// the first packet always contains extra information
if (sequenceIdx == 0) {
extraHeaderSize = 2;
}
DeserializedPacket packet = deserializePacket(channel, data, sequenceIdx, ble);
// this is always guaranteed to run on the first loop
if (sequenceIdx == 0) {
totalResponseLength = packet.totalResponseLength;
outBuffer = ByteBuffer.allocate(totalResponseLength);
}
if ((5 + extraHeaderSize + totalResponseLength) > data.length) {
return null;
}
byte[] deserializedPayload = packet.data;
if (outBuffer.remaining() < deserializedPayload.length) {
deserializedPayload = trimTail(deserializedPayload, deserializedPayload.length - outBuffer.remaining());
}
outBuffer.put(deserializedPayload);
if (!outBuffer.hasRemaining()) {
break;
}
sequenceIdx++;
}
return outBuffer.array();
}
}