Repository navigation
Expand file tree
/
Copy pathPacketCableCompiler.java
More file actions
528 lines (415 loc) · 15.9 KB
/
Copy pathPacketCableCompiler.java
File metadata and controls
528 lines (415 loc) · 15.9 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright 2015 - 2026 Comcast Cable Communications Management, LLC
* SPDX-License-Identifier: Apache-2.0
*/
package com.comcast.oscar.compiler;
import com.comcast.oscar.configurationfile.ConfigurationFileTypeConstants;
import com.comcast.oscar.dictionary.TlvDictionary;
import com.comcast.oscar.tlv.TlvBuilder;
import com.comcast.oscar.tlv.TlvException;
import com.comcast.oscar.tlv.TlvVariableBinding;
import com.comcast.oscar.utilities.BinaryConversion;
import com.comcast.oscar.utilities.CheckSum;
import com.comcast.oscar.utilities.HexString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.snmp4j.asn1.BER;
import org.snmp4j.asn1.BERInputStream;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
public class PacketCableCompiler extends TlvBuilder {
// Log4J2 logging
private static final Logger logger = LogManager.getLogger(PacketCableCompiler.class);
private Integer iPacketCableVersion = 0;
private ByteArrayOutputStream baosTlvFinalizeArray = new ByteArrayOutputStream();
/**
* @param iPacketCableVersion
*/
public PacketCableCompiler(Integer iPacketCableVersion) {
super();
this.iPacketCableVersion = iPacketCableVersion;
}
/**
* this methods creates the SHA-1 Hash required for BASIC.1 file integrity check based on the
* PacketCable Version
*/
public void commit() {
byte[] bTlvFileMarker = null;
byte[] bSHA1 = null;
// Create new ByteArrayOutputStream object
if (baosTlvFinalizeArray.size() != 0) {
baosTlvFinalizeArray = new ByteArrayOutputStream();
}
// Strip FILE Markers Just in case they are already there
try {
bTlvFileMarker =
stripTlv(
BinaryConversion.byteToUnsignedInteger(PacketCableConstants.FILE_MARKER),
stripIntegrityHash(super.toByteArray()));
} catch (TlvException e2) {
e2.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = STRIP-FILE-MRK: "
+ new HexString(bTlvFileMarker).toString());
// Insert StartOfFileMarker + EndOfFileMarker
try {
bTlvFileMarker = setEndOfFileMarker(setStartOfFileMarker(bTlvFileMarker));
} catch (IOException e) {
e.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = SET-FILE-MRK: "
+ new HexString(bTlvFileMarker).toString());
// Calculate SHA-1 HASH with File Markers
bSHA1 = CheckSum.getSHA1(bTlvFileMarker);
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = SHA1-HASH-HEX: "
+ new HexString(bSHA1).toString());
// Strip FILE Markers
try {
bTlvFileMarker =
stripTlv(
BinaryConversion.byteToUnsignedInteger(PacketCableConstants.FILE_MARKER),
bTlvFileMarker);
} catch (TlvException e2) {
e2.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = STRIP-FILE-MRK-HEX: "
+ new HexString(bTlvFileMarker).toString());
// Insert SHA1 HASH BASIC.1 Authentication
try {
bTlvFileMarker = insertSHA1Hash(bTlvFileMarker, bSHA1);
} catch (IOException e1) {
e1.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = ADD-SHA1-OID-HASH-HEX: "
+ new HexString(bTlvFileMarker).toString());
// Insert StartOfFileMarker + EndOfFileMarker
try {
bTlvFileMarker = setEndOfFileMarker(setStartOfFileMarker(bTlvFileMarker));
} catch (IOException e) {
e.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.finalize() = SET-FILE-MRK: "
+ new HexString(bTlvFileMarker).toString());
try {
this.baosTlvFinalizeArray.write(bTlvFileMarker);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @return byte[]
* @see com.comcast.oscar.tlv.TlvBuild#toByteArray()
*/
@Override
public byte[] toByteArray() {
if (baosTlvFinalizeArray.size() == 0) {
return super.toByteArray();
} else {
return baosTlvFinalizeArray.toByteArray();
}
}
/**
* @return String
*/
@Override
public String toString() {
// If this is 0, then finalized was never called
if (baosTlvFinalizeArray.size() == 0) {
return super.toString();
} else {
return new HexString(baosTlvFinalizeArray.toByteArray()).toString();
}
}
/**
* @param sSeperation
* @return String
*/
public String toString(String sSeperation) {
// If this is 0, then finalized was never called
if (baosTlvFinalizeArray.size() == 0) {
return super.toString();
} else {
return new HexString(baosTlvFinalizeArray.toByteArray()).toString(":");
}
}
/**
* @param iType
* @param bTlvByteArray
* @return the byteArray without the Type you specified * @throws TlvException
*/
public static byte[] stripTlv(int iType, byte[] bTlvByteArray) throws TlvException {
if ((iType < 0) || (iType > 255))
throw new TlvException("TlvBuilder.stripTlv() Out Of Range: " + iType);
if (bTlvByteArray == null) throw new TlvException("TlvBuilder.stripTlv() Byte Array is NULL");
logger.debug(
() ->
"PacketCableCompiler.stripTlv(i,b) -> StripType: "
+ iType
+ " - ByteArray Size: "
+ bTlvByteArray.length);
logger.debug(
() ->
"PacketCableCompiler.stripTlv(i,b) -> Hex: "
+ new HexString(bTlvByteArray).toString(":"));
ByteArrayOutputStream baosStripedTlvByteArray = new ByteArrayOutputStream();
int iIndex = 0;
int iTypeLength = 0;
int iByteLengthOffSet = 0;
while (iIndex < bTlvByteArray.length) {
if (logger.isDebugEnabled()) {
logger.debug(
"+----------------------------------------------------------------------------+");
logger.debug(
"PacketCableCompiler.stripTlv(i,b)"
+ " -> Type: "
+ BinaryConversion.byteToUnsignedInteger(bTlvByteArray[iIndex])
+ " - Index: "
+ iIndex
+ " - ByteArray Size: "
+ bTlvByteArray.length);
}
// Check for the DoubleByte Length TLV - Total Hack!!! - Next Version, I will use the
// Dictionary
if (bTlvByteArray[iIndex] == PacketCableConstants.SNMP_TLV_64) {
logger.debug("PacketCableCompiler.stripTlv(i,b) -> Found: Type64 - Index: {}", iIndex);
ByteArrayOutputStream baosMultiByteLength = new ByteArrayOutputStream();
// Create a ByteArray of ByteLength
baosMultiByteLength.write(bTlvByteArray, (iIndex + TLV_LENGTH_POS_OFFSET), 2);
// NumByteLength + 1
iByteLengthOffSet = 3;
// Get Integer from ByteArray
iTypeLength = new HexString(baosMultiByteLength.toByteArray()).toInteger();
logger.debug(
"PacketCableCompiler.stripTlv(i,b) -> Found: Type64 - Index: {} - TypeLength: {}",
iIndex,
iTypeLength);
} else {
iTypeLength =
(BinaryConversion.byteToUnsignedInteger(bTlvByteArray[iIndex + TLV_LENGTH_POS_OFFSET]))
+ TLV_TYPE_LENGTH_OVERHEAD;
iByteLengthOffSet = 0;
}
// If no type match, Copy TLV to ByteArray , I have to clean this up
if (BinaryConversion.byteToUnsignedInteger(bTlvByteArray[iIndex]) != iType) {
baosStripedTlvByteArray.write(bTlvByteArray, iIndex, (iTypeLength + iByteLengthOffSet));
logger.debug(
"PacketCableCompiler.stripTlv(i,b) -> Copying Bytes to ByteArray - Index: {} -"
+ " TypeLength: {}",
iIndex,
iTypeLength);
logger.debug(
() ->
"PacketCableCompiler.stripTlv(i,b) -> Hex: "
+ new HexString(baosStripedTlvByteArray.toByteArray()).toString(":"));
}
// Goto Next TLV Index
iIndex += (iByteLengthOffSet + iTypeLength);
if (logger.isDebugEnabled())
logger.debug(
"PacketCableCompiler.stripTlv(i,b) -> NextIndex: "
+ iIndex
+ " - ByteArray Size: "
+ bTlvByteArray.length);
}
return baosStripedTlvByteArray.toByteArray();
}
/**
* This method will find each TopLevel TLV and put it into a HexString Object
*
* @param baTlv
* @return list of HexString Objects * @throws TlvException
*/
public static List<HexString> getTopLevelTlvToHexStringList(byte[] baTlv) throws TlvException {
logger.debug(
"+==============================================================================================================+");
List<HexString> lhs = new ArrayList<HexString>();
int iLength = 0;
int iNumByteLength = 0;
for (int iIndex = 0; iIndex < baTlv.length; ) {
ByteArrayOutputStream baosTopLevelTLV = new ByteArrayOutputStream();
if (baTlv[iIndex] == PacketCableConstants.SNMP_TLV_64) {
iNumByteLength = PacketCableConstants.SNMP_TLV_64_BYTE_LENGTH;
iLength = TlvBuilder.getTlvLength(baTlv, iIndex, iNumByteLength);
} else { // 1 byte Length
iNumByteLength = PacketCableConstants.PACKET_CABLE_STANDARD_BYTE_LENGTH;
iLength = TlvBuilder.getTlvLength(baTlv, iIndex, iNumByteLength);
}
// Build ByteArray
baosTopLevelTLV.write(
baTlv, iIndex, (iLength + iNumByteLength + TlvBuilder.TLV_TYPE_OVERHEAD));
// Create HexString Object
HexString hsTLV = new HexString(baosTopLevelTLV.toByteArray());
logger.debug(
() ->
"PacketCableCompiler.getTopLevelTlvToHexStringList() - TLV -> "
+ hsTLV.toString(":"));
// Load HexString Object to List
lhs.add(hsTLV);
iIndex += (iLength + iNumByteLength + TlvBuilder.TLV_TYPE_OVERHEAD);
}
return lhs;
}
/**
* @param baTLV
* @return TlvBuilder
* @throws NullPointerException
*/
public static TlvBuilder stripFileMarkers(byte[] baTLV) throws NullPointerException {
if (baTLV == null)
throw new NullPointerException("PacketCableCompiler.stripFileMarkers() - Null Byte Array");
byte[] baTlvTemp = null;
try {
baTlvTemp =
stripTlv(BinaryConversion.byteToUnsignedInteger(PacketCableConstants.FILE_MARKER), baTLV);
} catch (TlvException e) {
e.printStackTrace();
}
TlvDictionary dsq = new TlvDictionary(TlvDictionary.PACKET_CABLE_DICTIONARY_TABLE_NAME);
TlvBuilder tb = new TlvBuilder();
tb.add(new TlvVariableBinding(baTlvTemp, dsq.getTopLevelByteLength()));
return tb;
}
/**
* This Method will remove the existing File Markers and re-add them to the ByteArray
*
* @param baTLV
* @return TlvBuilder
* @throws NullPointerException
*/
public static TlvBuilder setFileMarkers(byte[] baTLV) throws NullPointerException {
TlvBuilder tb = null;
// Get TlvBuilder without FileMarkets
tb = stripFileMarkers(baTLV);
// Add file markers to the ByteArrya
byte[] baPcTlv = null;
try {
baPcTlv = setEndOfFileMarker(setStartOfFileMarker(tb.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
// Get Type to Byte Length Mapping
TlvDictionary dsq = new TlvDictionary(TlvDictionary.PACKET_CABLE_DICTIONARY_TABLE_NAME);
// Get TlvVariableBinding due to variable byte Length TLVs
TlvVariableBinding tvb = new TlvVariableBinding(baPcTlv, dsq.getTopLevelByteLength());
// Create new TlvBuilder
tb = new TlvBuilder();
// Add TlvVariableBinding
tb.add(tvb);
return tb;
}
/* *********************************************************************************
* Private Methods
**********************************************************************************/
/** Remove only this edition's generated BASIC.1 hash before recalculating it. */
private byte[] stripIntegrityHash(byte[] configurationBytes) throws TlvException {
String integrityOid =
iPacketCableVersion == ConfigurationFileTypeConstants.PKT_CABLE_20_CONFIGURATION_TYPE
? PacketCableConstants.PKT_CABLE_20_BASIC_AUTH_OID
: PacketCableConstants.PKT_CABLE_10_BASIC_AUTH_OID;
ByteArrayOutputStream retainedTlvs = new ByteArrayOutputStream();
for (HexString encodedTlv : getTopLevelTlvToHexStringList(configurationBytes)) {
byte[] tlvBytes = encodedTlv.toByteArray();
int tlvType = Byte.toUnsignedInt(tlvBytes[0]);
if (tlvType == PacketCableConstants.SNMP_TLV_11
|| tlvType == PacketCableConstants.SNMP_TLV_64) {
int lengthWidth =
tlvType == PacketCableConstants.SNMP_TLV_64
? PacketCableConstants.SNMP_TLV_64_BYTE_LENGTH
: PacketCableConstants.PACKET_CABLE_STANDARD_BYTE_LENGTH;
int valueOffset = TLV_TYPE_OVERHEAD + lengthWidth;
VariableBinding binding = new VariableBinding();
try {
binding.decodeBER(
new BERInputStream(
ByteBuffer.wrap(tlvBytes, valueOffset, tlvBytes.length - valueOffset)));
} catch (IOException exception) {
throw new IllegalArgumentException("Invalid PacketCable SNMP binding", exception);
}
if (binding.getOid().equals(new OID(integrityOid))) {
continue;
}
}
retainedTlvs.writeBytes(tlvBytes);
}
return retainedTlvs.toByteArray();
}
/**
* @param bTlvArray
* @return byte[]
* @throws IOException
*/
private static byte[] setStartOfFileMarker(byte[] bTlvArray) throws IOException {
ByteArrayOutputStream baosbTlvArray = new ByteArrayOutputStream();
byte[] bSOF = {PacketCableConstants.FILE_MARKER, 0x01, PacketCableConstants.START_OF_FILE};
baosbTlvArray.write(bSOF);
baosbTlvArray.write(bTlvArray);
return baosbTlvArray.toByteArray();
}
/**
* @param bTlvArray
* @return byte[]
* @throws IOException
*/
private static byte[] setEndOfFileMarker(byte[] bTlvArray) throws IOException {
ByteArrayOutputStream baosbTlvArray = new ByteArrayOutputStream();
byte[] bEOF = {PacketCableConstants.FILE_MARKER, 0x01, PacketCableConstants.END_OF_FILE};
baosbTlvArray.write(bTlvArray);
baosbTlvArray.write(bEOF);
return baosbTlvArray.toByteArray();
}
/**
* @param bTlvArray
* @param bSHA1
* @return byte[]
* @throws IOException
*/
private byte[] insertSHA1Hash(byte[] bTlvArray, byte[] bSHA1) throws IOException {
ByteArrayOutputStream baosbTlvArray = new ByteArrayOutputStream();
// Write into Array
baosbTlvArray.write(bTlvArray);
// Create SHA-1 TLV
TlvBuilder tbTlvSHA1 = new TlvBuilder();
// Select Correct Packet Cable Version
if (iPacketCableVersion == ConfigurationFileTypeConstants.PKT_CABLE_10_CONFIGURATION_TYPE) {
tbTlvSHA1.add(
PacketCableConstants.SNMP_TLV_11,
new OID(PacketCableConstants.PKT_CABLE_10_BASIC_AUTH_OID),
BER.OCTETSTRING,
bSHA1);
} else if (iPacketCableVersion
== ConfigurationFileTypeConstants.PKT_CABLE_15_CONFIGURATION_TYPE) {
tbTlvSHA1.add(
PacketCableConstants.SNMP_TLV_11,
new OID(PacketCableConstants.PKT_CABLE_15_BASIC_AUTH_OID),
BER.OCTETSTRING,
bSHA1);
} else if (iPacketCableVersion
== ConfigurationFileTypeConstants.PKT_CABLE_20_CONFIGURATION_TYPE) {
tbTlvSHA1.add(
PacketCableConstants.SNMP_TLV_11,
new OID(PacketCableConstants.PKT_CABLE_20_BASIC_AUTH_OID),
BER.OCTETSTRING,
bSHA1);
}
// Added SHA1 Hash BASIC.1 Authentication
baosbTlvArray.write(tbTlvSHA1.toByteArray());
return baosbTlvArray.toByteArray();
}
}