blob: cdc18435fe3a9639ad2c52646a25ef6806a13942 (
plain) (
blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
package org.uic.barcode.dynamicFrame.v1;
import org.uic.barcode.asn1.datatypesimpl.OctetString;
import org.uic.barcode.asn1.uper.UperEncoder;
import org.uic.barcode.ticket.EncodingFormatException;
import org.uic.barcode.dynamicFrame.api.IData;
import org.uic.barcode.dynamicFrame.api.IDynamicFrame;
import org.uic.barcode.dynamicFrame.api.ILevel1Data;
import org.uic.barcode.dynamicFrame.api.ILevel2Data;
import org.uic.barcode.dynamicFrame.api.SimpleData;
import org.uic.barcode.dynamicFrame.api.SimpleLevel1Data;
import org.uic.barcode.dynamicFrame.api.SimpleLevel2Data;
public class DynamicFrameCoderV1 {
public static void decode(IDynamicFrame frame, byte[] bytes) {
DynamicFrame asnFrame = UperEncoder.decode(bytes,DynamicFrame.class);
frame.setFormat(asnFrame.getFormat());
if (asnFrame.getLevel2Signature() != null) {
frame.setLevel2Signature(asnFrame.getLevel2Signature().toByteArray());
}
if (asnFrame.getLevel2SignedData() != null) {
frame.setLevel2Data(new SimpleLevel2Data());
populateApi(frame.getLevel2Data(), asnFrame.getLevel2SignedData());
}
}
public static ILevel1Data decodeLevel1(byte[] bytes) {
Level1DataType asnData = UperEncoder.decode(bytes,Level1DataType.class);
return populateApi(asnData);
}
private static void populateApi(ILevel2Data level2, Level2DataType asnLevel2) {
if (asnLevel2 == null) return;
level2.setLevel1Signature(asnLevel2.getLevel1SignatureBytes());
if (asnLevel2.getLevel1Data() != null) {
level2.setLevel1Data(new SimpleLevel1Data());
level2.setLevel1Data(populateApi(asnLevel2.getLevel1Data()));
}
if (asnLevel2.getLevel2Data() != null) {
level2.setLevel2Data(new SimpleData());
level2.setLevel2Data(populateApi(asnLevel2.getLevel2Data()));
}
}
private static IData populateApi(DataType asnData) {
IData data = new SimpleData();
data.setData(asnData.getByteData());
data.setFormat(asnData.getFormat());
return data;
}
private static ILevel1Data populateApi(Level1DataType asnLevel1) {
ILevel1Data level1 = new SimpleLevel1Data();
level1.setKeyId(asnLevel1.getKeyId());
level1.setSecurityProvider(asnLevel1.getSecurityProvider());
level1.setLevel1KeyAlg(asnLevel1.getLevel1KeyAlg());
level1.setLevel1SigningAlg(asnLevel1.getLevel1SigningAlg());
level1.setLevel2KeyAlg(asnLevel1.getLevel2KeyAlg());
level1.setLevel2SigningAlg(asnLevel1.getLevel2SigningAlg());
if (asnLevel1.getLevel2publicKey() != null) {
level1.setLevel2publicKey(asnLevel1.getLevel2publicKey().toByteArray());
}
if (asnLevel1.getData() != null && !asnLevel1.getData().isEmpty()) {
for (DataType asnData : asnLevel1.getData()) {
IData data = new SimpleData();
data.setData(asnData.getByteData());
data.setFormat(asnData.getFormat());
level1.addData(data);
}
}
return level1;
}
public static byte[] encode(IDynamicFrame dynamicFrame) throws EncodingFormatException {
DynamicFrame asnDynamicFrame = populateAsn(dynamicFrame);
return UperEncoder.encode(asnDynamicFrame);
}
public static byte[] encode(ILevel1Data level1Data) throws EncodingFormatException {
Level1DataType asn = populateAsn(level1Data);
return UperEncoder.encode(asn);
}
public static byte[] getEncoded(String path, byte[] data) {
if (path.endsWith("Level1Data")){
return UperEncoder.extract(data, "Level1DataType" ,DynamicFrame.class );
} else if (path.endsWith("Level2Data")){
return UperEncoder.extract(data, "Level2DataType" ,DynamicFrame.class );
}
return null;
}
public static byte[] encodeLevel2Data(ILevel2Data level2Data) throws EncodingFormatException {
Level2DataType asn = populateAsn(level2Data);
return UperEncoder.encode(asn);
}
private static DynamicFrame populateAsn(IDynamicFrame frame) throws EncodingFormatException {
DynamicFrame asnFrame = new DynamicFrame();
asnFrame.setFormat(frame.getFormat());
if (frame.getLevel2Signature() != null && frame.getLevel2Signature().length > 0) {
asnFrame.setLevel2Signature(new OctetString(frame.getLevel2Signature()));
}
Level2DataType asnLevel2 = populateAsn(frame.getLevel2Data());
if (asnLevel2 != null) {
asnFrame.setLevel2SignedData(asnLevel2);
}
return asnFrame;
}
private static Level2DataType populateAsn(ILevel2Data level2) throws EncodingFormatException {
Level2DataType asnLevel2 = new Level2DataType();
asnLevel2.setLevel1Signature(level2.getLevel1Signature());
Level1DataType asnLevel1 = populateAsn(level2.getLevel1Data());
asnLevel2.setLevel1Data(asnLevel1);
if (level2.getLevel2Data() != null) {
DataType asnData = new DataType();
asnData.setFormat(level2.getLevel2Data().getFormat());
asnData.setData(new OctetString(level2.getLevel2Data().getData()));
asnLevel2.setLevel2Data(asnData);
}
return asnLevel2;
}
private static Level1DataType populateAsn(ILevel1Data level1) throws EncodingFormatException {
Level1DataType asnLevel1 = new Level1DataType();
asnLevel1.setSecurityProvider(level1.getSecurityProvider());
asnLevel1.setKeyId(level1.getKeyId());
asnLevel1.setLevel1KeyAlg(level1.getLevel1KeyAlg());
asnLevel1.setLevel1SigningAlg(level1.getLevel1SigningAlg());
asnLevel1.setLevel2KeyAlg(level1.getLevel2KeyAlg());
if (level1.getLevel2publicKey() != null && level1.getLevel2publicKey().length > 0) {
asnLevel1.setLevel2publicKey(new OctetString(level1.getLevel2publicKey()));
}
asnLevel1.setLevel2SigningAlg(level1.getLevel2SigningAlg());
if (level1.getData() != null && !level1.getData().isEmpty()) {
asnLevel1.setData(new SequenceOfDataType());
for (IData data : level1.getData()) {
DataType asnData = new DataType();
asnData.setByteData(data.getData());
asnData.setFormat(data.getFormat());
asnLevel1.getData().add(asnData);
}
}
return asnLevel1;
}
public static byte[] encodeLevel1(IDynamicFrame frame) throws EncodingFormatException {
Level1DataType asnLevel1Data = populateAsn(frame.getLevel2Data().getLevel1Data());
return UperEncoder.encode(asnLevel1Data);
}
}
|