1 /**
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|
  5  * |#       #|  Copyright (c) 1999-2018 CardContact Software & System Consulting
  6  * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
  7  *  ---------
  8  *
  9  *  This file is part of OpenSCDP.
 10  *
 11  *  OpenSCDP is free software; you can redistribute it and/or modify
 12  *  it under the terms of the GNU General Public License version 2 as
 13  *  published by the Free Software Foundation.
 14  *
 15  *  OpenSCDP is distributed in the hope that it will be useful,
 16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  *  GNU General Public License for more details.
 19  *
 20  *  You should have received a copy of the GNU General Public License
 21  *  along with OpenSCDP; if not, write to the Free Software
 22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 23  *
 24  * @fileoverview Implementation of ISO 7816-4 APDU processing
 25  */
 26 
 27 
 28 
 29 /**
 30  * Create an APDU
 31  *
 32  * <p>This constructor supports the signatures</p>
 33  * <ul>
 34  *  <li>APDU(ByteString command)</li>
 35  *  <li>APDU(Number cla, Number ins, Number p1, Number p2)</li>
 36  *  <li>APDU(Number cla, Number ins, Number p1, Number p2, data)</li>
 37  *  <li>APDU(Number cla, Number ins, Number p1, Number p2, data, Ne)</li>
 38  * </ul>
 39  * @class Class implementing support for command and response APDUs
 40  * @constructor
 41  * @param {ByteString} command the command APDU
 42  * @param {Number} cla the class byte
 43  * @param {Number} ins the instruction byte
 44  * @param {Number} p1 the first parameter
 45  * @param {Number} p2 the second parameter
 46  * @param {ByteString} data the data field (optional)
 47  * @param {Number} Ne the number of expected bytes (optional)
 48  */
 49 function APDU() {
 50 	if (arguments.length > 0) {
 51 		var arg = arguments[0];
 52 		if (arg instanceof ByteString) {
 53 			if (arguments.length != 1) {
 54 				throw new GPError("APDU", GPError.INVALID_ARGUMENTS, APDU.SW_GENERALERROR, "Only one argument of type ByteString expected");
 55 			}
 56 			this.fromByteString(arg);
 57 		} else {
 58 			if ((arguments.length < 4) || (arguments.length > 6)) {
 59 				throw new GPError("APDU", GPError.INVALID_ARGUMENTS, APDU.SW_GENERALERROR, "4 to 6 arguments expected");
 60 			}
 61 
 62 			for (var i = 0; i < 4; i++) {
 63 				if (typeof(arguments[i]) != "number") {
 64 					throw new GPError("APDU", GPError.INVALID_TYPE, APDU.SW_GENERALERROR, "Argument must be of type Number");
 65 				}
 66 			}
 67 			this.cla = arguments[0];
 68 			this.ins = arguments[1];
 69 			this.p1 = arguments[2];
 70 			this.p2 = arguments[3];
 71 
 72 			var i = 4;
 73 			if (arguments.length > i) {
 74 				if (arguments[i] instanceof ByteString) {
 75 					this.cdata = arguments[i];
 76 					i++;
 77 				}
 78 			}
 79 
 80 			if (arguments.length > i) {
 81 				if (typeof(arguments[i]) != "number") {
 82 					throw new GPError("APDU", GPError.INVALID_TYPE, APDU.SW_GENERALERROR, "Argument must be of type Number");
 83 				}
 84 				this.ne = arguments[i];
 85 			}
 86 		}
 87 	}
 88 	this.rapdu = null;
 89 	this.SW = APDU.SW_GENERALERROR;
 90 }
 91 
 92 exports.APDU = APDU;
 93 
 94 
 95 
 96 APDU.INS_DEACTIVATE		= 0x04;
 97 APDU.INS_VERIFY			= 0x20;
 98 APDU.INS_MANAGE_SE		= 0x22;
 99 APDU.INS_CHANGE_REFERENCE_DATA	= 0x24;
100 APDU.INS_PSO			= 0x2A;
101 APDU.INS_RESET_RETRY_COUNTER	= 0x2C;
102 APDU.INS_ACTIVATE		= 0x44;
103 APDU.INS_GENERATE_KEY_PAIR	= 0x46;
104 APDU.INS_INITIALIZE_UPDATE	= 0x50;
105 APDU.INS_EXTERNAL_AUTHENTICATE	= 0x82;
106 APDU.INS_GET_CHALLENGE		= 0x84;
107 APDU.INS_GENERAL_AUTHENTICATE	= 0x86;
108 APDU.INS_COMPUTE_DIGITAL_SIGN	= 0x9E;
109 APDU.INS_SELECT			= 0xA4;
110 APDU.INS_READBINARY		= 0xB0;
111 APDU.INS_READ_BINARY		= 0xB0;
112 APDU.INS_READ_RECORD		= 0xB2;
113 APDU.INS_VERIFY_CERTIFICATE	= 0xBE;
114 APDU.INS_UPDATE_BINARY		= 0xD6;
115 APDU.INS_PUT_KEY		= 0xD8;
116 APDU.INS_DELETE			= 0xE4;
117 APDU.INS_TERMINATE		= 0xE6;
118 
119 APDU.SW_OK                 = 0x9000;      	/* Process completed                 */
120 
121 APDU.SW_TIMEOUT            = 0x6401;      	/* Exec error: Command timeout       */
122 
123 APDU.SW_OKMOREDATA         = 0x6100;      	/*-Process completed, more data available*/
124 APDU.SW_WARNING            = 0x6200;      	/*-Warning: NV-Ram not changed       */
125 APDU.SW_WARNING1           = 0x6201;      	/*-Warning: NV-Ram not changed 1     */
126 APDU.SW_DATAINV            = 0x6281;      	/*-Warning: Part of data corrupted   */
127 APDU.SW_EOF                = 0x6282;      	/*-Warning: End of file reached      */
128 APDU.SW_INVFILE            = 0x6283;      	/* Warning: Invalidated file         */
129 APDU.SW_INVFORMAT          = 0x6284;      	/* Warning: Invalid file control     */
130 APDU.SW_WARNINGNVCHG       = 0x6300;      	/*-Warning: NV-Ram changed           */
131 APDU.SW_WARNINGCOUNT       = 0x63C0;      	/*-Warning: Warning with counter     */
132 APDU.SW_WARNING0LEFT       = 0x63C0;      	/*-Warning: Verify fail, no try left */
133 APDU.SW_WARNING1LEFT       = 0x63C1;      	/*-Warning: Verify fail, 1 try left  */
134 APDU.SW_WARNING2LEFT       = 0x63C2;      	/*-Warning: Verify fail, 2 tries left*/
135 APDU.SW_WARNING3LEFT       = 0x63C3;      	/*-Warning: Verify fail, 3 tries left*/
136 APDU.SW_EXECERR            = 0x6400;      	/*-Exec error: NV-Ram not changed    */
137 APDU.SW_MEMERR             = 0x6501;      	/*-Exec error: Memory failure        */
138 APDU.SW_MEMERRWRITE        = 0x6581;      	/*-Exec error: Memory failure        */
139 APDU.SW_WRONGLENGTH        = 0x6700;      	/*-Checking error: Wrong length      */
140 
141 APDU.SW_CLANOTSUPPORTED    = 0x6800;      	/*-Checking error: Function in CLA byte not supported */
142 APDU.SW_LCNOTSUPPORTED     = 0x6881;      	/*-Checking error: Logical channel not supported */
143 APDU.SW_SMNOTSUPPORTED     = 0x6882;      	/*-Checking error: Secure Messaging not supported */
144 APDU.SW_LASTCMDEXPECTED    = 0x6883;      	/*-Checking error: Last command of the chain expected */
145 APDU.SW_CHAINNOTSUPPORTED  = 0x6884;      	/*-Checking error: Command chaining not supported */
146 
147 APDU.SW_COMNOTALLOWED      = 0x6900;      	/*-Checking error: Command not allowed */
148 APDU.SW_COMINCOMPATIBLE    = 0x6981;      	/*-Checking error: Command incompatible with file structure */
149 APDU.SW_SECSTATNOTSAT      = 0x6982;      	/*-Checking error: Security condition not satisfied */
150 APDU.SW_AUTHMETHLOCKED     = 0x6983;      	/*-Checking error: Authentication method locked */
151 APDU.SW_REFDATANOTUSABLE   = 0x6984;      	/*-Checking error: Reference data not usable */
152 APDU.SW_CONDOFUSENOTSAT    = 0x6985;      	/*-Checking error: Condition of use not satisfied */
153 APDU.SW_COMNOTALLOWNOEF    = 0x6986;      	/*-Checking error: Command not allowed (no current EF) */
154 APDU.SW_SMOBJMISSING       = 0x6987;      	/*-Checking error: Expected secure messaging object missing */
155 APDU.SW_INCSMDATAOBJECT    = 0x6988;      	/*-Checking error: Incorrect secure messaging data object */
156 
157 APDU.SW_INVPARA            = 0x6A00;      	/*-Checking error: Wrong parameter P1-P2 */
158 APDU.SW_INVDATA            = 0x6A80;      	/*-Checking error: Incorrect parameter in the command data field*/
159 APDU.SW_FUNCNOTSUPPORTED   = 0x6A81;      	/*-Checking error: Function not supported */
160 APDU.SW_NOAPPL             = 0x6A82;      	/*-Checking error: File not found    */
161 APDU.SW_FILENOTFOUND       = 0x6A82;      	/*-Checking error: File not found    */
162 APDU.SW_RECORDNOTFOUND     = 0x6A83;      	/*-Checking error: Record not found    */
163 APDU.SW_OUTOFMEMORY        = 0x6A84;      	/*-Checking error: Not enough memory space in the file   */
164 APDU.SW_INVLCTLV           = 0x6A85;      	/*-Checking error: Nc inconsistent with TLV structure */
165 APDU.SW_INVACC             = 0x6A85;      	/*-Checking error: Access cond. n/f  */
166 APDU.SW_INCP1P2            = 0x6A86;      	/*-Checking error: Incorrect P1-P2   */
167 APDU.SW_INVLC              = 0x6A87;      	/*-Checking error: Lc inconsistent with P1-P2 */
168 APDU.SW_RDNOTFOUND         = 0x6A88;      	/*-Checking error: Reference data not found*/
169 APDU.SW_FILEEXISTS         = 0x6A89;      	/*-Checking error: File already exists */
170 APDU.SW_DFNAMEEXISTS       = 0x6A8A;      	/*-Checking error: DF name already exists */
171 
172 APDU.SW_INVP1P2            = 0x6B00;      	/*-Checking error: Wrong parameter P1-P2 */
173 APDU.SW_INVLE              = 0x6C00;      	/*-Checking error: Invalid Le        */
174 APDU.SW_INVINS             = 0x6D00;      	/*-Checking error: Wrong instruction */
175 APDU.SW_INVCLA             = 0x6E00;      	/*-Checking error: Class not supported */
176 APDU.SW_ACNOTSATISFIED     = 0x9804;      	/* Access conditions not satisfied   */
177 APDU.SW_NOMORESTORAGE      = 0x9210;      	/* No more storage available         */
178 APDU.SW_GENERALERROR       = 0x6F00;      	/*-Checking error: No precise diagnosis */
179 
180 
181 /**
182  * Create an APDU object from the encoded form (Called internally)
183  *
184  * @param {ByteString} bs
185  */
186 APDU.prototype.fromByteString = function(bs) {
187 	if (bs.length < 4) {
188 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_GENERALERROR, "Command APDU must be at least 4 bytes long");
189 	}
190 	this.cla = bs.byteAt(0);
191 	this.ins = bs.byteAt(1);
192 	this.p1 = bs.byteAt(2);
193 	this.p2 = bs.byteAt(3);
194 
195 	if (bs.length > 4) {
196 		var extended = false;
197 
198 		var i = 4;
199 		var l = bs.length - i;
200 		var n = bs.byteAt(i++);
201 		l--;
202 
203 		if ((n == 0) && (l > 0)) {
204 			extended = true;
205 			if (l < 2) {
206 				throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Extended length APDU too short");
207 			}
208 			n = (bs.byteAt(i) << 8) + bs.byteAt(i + 1);
209 			i += 2;
210 			l -= 2;
211 		}
212 
213 		if (l > 0) {	// Case 3s / Case 3e / Case 4s / Case 4e
214 			if (l < n) {
215 				throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Invalid Lc in APDU");
216 			}
217 			this.cdata = bs.bytes(i, n);
218 			i += n;
219 			l -= n;
220 
221 			if (l > 0) {	// Case 4s / Case 4e
222 				n = bs.byteAt(i++);
223 				l--;
224 				if (extended) {
225 					if (l < 1) {
226 						throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Invalid Le in extended APDU");
227 					}
228 					n = (n << 8) + bs.byteAt(i++);
229 					l--;
230 				}
231 				this.ne = (extended && (n == 0) ? 65536 : n);
232 			}
233 		} else {
234 			this.ne = (extended && (n == 0) ? 65536 : n);
235 		}
236 
237 		if (l > 0) {
238 			throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Too many bytes in APDU");
239 		}
240 	}
241 }
242 
243 
244 
245 /**
246  * Get encoded command APDU
247  *
248  * @type ByteString
249  * @return the encoded command APDU
250  */
251 APDU.prototype.getCommandAPDU = function() {
252 	var bb = new ByteBuffer();
253 
254 	bb.append(this.cla);
255 	bb.append(this.ins);
256 	bb.append(this.p1);
257 	bb.append(this.p2);
258 
259 	var hasCData = (typeof(this.cdata) != "undefined");
260 	var hasNe = (typeof(this.ne) != "undefined");
261 
262 	var extended = ((hasCData && this.cdata.length > 255) ||
263 					(hasNe && this.ne > 256));
264 
265 	if (extended) {
266 		bb.append(0);
267 	}
268 
269 	if (hasCData && this.cdata.length > 0) {
270 		if (extended) {
271 			bb.append(this.cdata.length >> 8);
272 		}
273 		bb.append(this.cdata.length & 0xFF);
274 		bb.append(this.cdata);
275 	}
276 
277 	if (hasNe) {
278 		if (extended) {
279 			bb.append(this.ne >> 8);
280 		}
281 		bb.append(this.ne & 0xFF);
282 	}
283 
284 	return bb.toByteString();
285 }
286 
287 
288 
289 /**
290  * Get encoded response APDU
291  *
292  * @type ByteString
293  * @return the encoded response APDU
294  */
295 APDU.prototype.getResponseAPDU = function() {
296 	var bb = new ByteBuffer();
297 
298 	if (this.rdata) {
299 		bb.append(this.rdata);
300 	}
301 
302 	bb.append(this.SW >> 8);
303 	bb.append(this.SW & 0xFF);
304 
305 	return bb.toByteString();
306 }
307 
308 
309 
310 /**
311  * Gets the class byte
312  *
313  * @type Number
314  * @return the class byte
315  */
316 APDU.prototype.getCLA = function() {
317 	return this.cla;
318 }
319 
320 
321 
322 /**
323  * Sets the class byte, e.g. after a transformation
324  *
325  * @parameter {Number} the new CLA byte
326  */
327 APDU.prototype.setCLA = function(cla) {
328 	this.cla = cla;
329 }
330 
331 
332 
333 /**
334  * Test if command is an ISO command
335  *
336  * @type boolean
337  * @return true if command has ISO class byte
338  */
339 APDU.prototype.isISO = function() {
340 	return (this.cla & 0x80) == 0x00;
341 }
342 
343 
344 
345 /**
346  * Test if command uses extended length
347  *
348  * @type boolean
349  * @return true if APDU uses extended length
350  */
351 APDU.prototype.isExtendedLength = function() {
352 	return (((typeof(this.cdata) != "undefined") && this.cdata.length > 255) || ((typeof(this.ne) != "undefined") && (this.ne > 256)));
353 }
354 
355 
356 
357 /**
358  * Test if command chaining is indicated
359  *
360  * @type boolean
361  * @return true if chaining bit is set
362  */
363 APDU.prototype.isChained = function() {
364 	return (this.cla & 0x10) == 0x10;
365 }
366 
367 
368 
369 /**
370  * Test if command is send using secure messaging
371  *
372  * @type boolean
373  * @return true if secure messaging is indicated in CLA byte
374  */
375 APDU.prototype.isSecureMessaging = function() {
376 	return (this.cla & 0x08) == 0x08;
377 }
378 
379 
380 
381 /**
382  * Test if command is send using secure messaging
383  *
384  * @type boolean
385  * @return true if secure messaging is using an authenticated header
386  */
387 APDU.prototype.isAuthenticatedHeader = function() {
388 	return (this.cla & 0x0C) == 0x0C;
389 }
390 
391 
392 
393 /**
394  * Gets the instruction byte
395  *
396  * @type Number
397  * @return the instruction byte
398  */
399 APDU.prototype.getINS = function() {
400 	return this.ins;
401 }
402 
403 
404 
405 /**
406  * Gets the P1 byte
407  *
408  * @type Number
409  * @return the P1 byte
410  */
411 APDU.prototype.getP1 = function() {
412 	return this.p1;
413 }
414 
415 
416 
417 /**
418  * Gets the P2 byte
419  *
420  * @type Number
421  * @return the P2 byte
422  */
423 APDU.prototype.getP2 = function() {
424 	return this.p2;
425 }
426 
427 
428 
429 /**
430  * Set the command data
431  *
432  * @param {ByteString} cdata the command data
433  */
434 APDU.prototype.setCData = function(cdata) {
435 	this.cdata = cdata;
436 }
437 
438 
439 
440 /**
441  * Gets the command data
442  *
443  * @type ByteString
444  * @return the command data, if any else undefined
445  */
446 APDU.prototype.getCData = function() {
447 	return this.cdata;
448 }
449 
450 
451 
452 /**
453  * Check if APDU has command data
454  *
455  * @type boolean
456  * @return true if command APDU has data field
457  */
458 APDU.prototype.hasCData = function() {
459 	return ((typeof(this.cdata) != "undefined") && (this.cdata != null));
460 }
461 
462 
463 
464 /**
465  * Gets the command data as a list of TLV objects
466  *
467  * @type TLVList
468  * @return the command data as TLV list, if any else undefined
469  */
470 APDU.prototype.getCDataAsTLVList = function() {
471 	if (typeof(this.cdata) == "undefined") {
472 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_INVDATA, "No data in command APDU");
473 	}
474 
475 	try	{
476 		var a = new TLVList(this.cdata, TLV.EMV);
477 	}
478 	catch(e) {
479 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_INVDATA, "Invalid TLV data in command APDU");
480 	}
481 
482 	return a;
483 }
484 
485 
486 
487 /**
488  * Gets the number of expected bytes
489  *
490  * @type Number
491  * @return the number of expected bytes or undefined
492  */
493 APDU.prototype.getNe = function() {
494 	return this.ne;
495 }
496 
497 
498 
499 /**
500  * Check if APDU has Le field
501  *
502  * @type boolean
503  * @return true if command APDU has Le field
504  */
505 APDU.prototype.hasLe = function() {
506 	return typeof(this.ne) != "undefined";
507 }
508 
509 
510 
511 /**
512  * Set secure channel object to be used in wrap and unwrap methods
513  *
514  * @param {SecureChannel} secureChannel the channel
515  */
516 APDU.prototype.setSecureChannel = function(secureChannel) {
517 	this.secureChannel = secureChannel;
518 }
519 
520 
521 
522 /**
523  * Return the secure channel, if any
524  *
525  * @type SecureChannel
526  * @return the secure channel
527  */
528 APDU.prototype.getSecureChannel = function() {
529 	return this.secureChannel;
530 }
531 
532 
533 
534 /**
535  * Test if a secure channel is defined for this APDU
536  *
537  * @type boolean
538  * @return true, if secure channel is set
539  */
540 APDU.prototype.hasSecureChannel = function() {
541 	return (typeof(this.secureChannel) != "undefined") && (this.secureChannel != null);
542 }
543 
544 
545 
546 /**
547  * Wrap APDU using secure channel
548  */
549 APDU.prototype.wrap = function() {
550 	if (this.hasSecureChannel()) {
551 		this.secureChannel.wrap(this);
552 	}
553 }
554 
555 
556 
557 /**
558  * Unwrap APDU using secure channel
559  */
560 APDU.prototype.unwrap = function() {
561 	if (this.hasSecureChannel()) {
562 		this.secureChannel.unwrap(this);
563 	}
564 }
565 
566 
567 
568 /**
569  * Sets the response data field for the response APDU
570  *
571  * @param {ByteString} data the response data field
572  */
573 APDU.prototype.setRData = function(data) {
574 	if ((data.length > 256) && !this.isExtendedLength()) {
575 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Too many bytes in APDU");
576 	}
577 	this.rdata = data;
578 }
579 
580 
581 
582 /**
583  * Get the response data
584  *
585  * @type ByteString
586  * @return the response data
587  */
588 APDU.prototype.getRData = function() {
589 	return this.rdata;
590 }
591 
592 
593 
594 /**
595  * Check if APDU has response data
596  *
597  * @type boolean
598  * @return true if response APDU has data field
599  */
600 APDU.prototype.hasRData = function() {
601 	return ((typeof(this.rdata) != "undefined") && (this.rdata != null));
602 }
603 
604 
605 
606 /**
607  * Sets the status word for the response ADPU
608  *
609  * @param {Number} sw the status word
610  */
611 APDU.prototype.setSW = function(sw) {
612 	this.SW = sw;
613 }
614 
615 
616 
617 /**
618  * Get the status word
619  *
620  * @type Number
621  * @return the status word
622  */
623 APDU.prototype.getSW = function() {
624 	return this.SW;
625 }
626 
627 
628 
629 /**
630  * Return a human readable form of this object
631  */
632 APDU.prototype.toString = function() {
633 	return this.getCommandAPDU().toString(HEX) + " : " + this.getResponseAPDU().toString(HEX);
634 }
635 
636 
637 
638 /**
639  * Simple unit test
640  */
641 APDU.test = function() {
642 	// Case 1
643 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C);
644 	print(a);
645 	assert(!a.isExtendedLength());
646 	var b = a.getCommandAPDU();
647 	assert(b.toString(HEX) == "00A4000C");
648 	var c = new APDU(b);
649 	assert(a.toString() == c.toString());
650 
651 	// Case 2 Short
652 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, 0);
653 	print(a);
654 	assert(!a.isExtendedLength());
655 	var b = a.getCommandAPDU();
656 	assert(b.toString(HEX) == "00A4000C00");
657 	var c = new APDU(b);
658 	assert(a.toString() == c.toString());
659 
660 	// Case 2 Extended
661 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, 65536);
662 	print(a);
663 	assert(a.isExtendedLength());
664 	var b = a.getCommandAPDU();
665 	assert(b.toString(HEX) == "00A4000C000000");
666 	var c = new APDU(b);
667 	print(c);
668 	assert(a.toString() == c.toString());
669 
670 	// Case 3 Short
671 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX));
672 	print(a);
673 	assert(!a.isExtendedLength());
674 	var b = a.getCommandAPDU();
675 	assert(b.toString(HEX) == "00A4000C023F00");
676 	var c = new APDU(b);
677 	assert(a.toString() == c.toString());
678 
679 	// Case 3 Extended
680 	var data = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
681 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString(data, HEX));
682 	print(a);
683 	assert(a.isExtendedLength());
684 	var b = a.getCommandAPDU();
685 	assert(b.toString(HEX) == "00A4000C000100" + data);
686 	var c = new APDU(b);
687 	assert(a.toString() == c.toString());
688 
689 	// Case 4 Short
690 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX), 0);
691 	print(a);
692 	assert(!a.isExtendedLength());
693 	var b = a.getCommandAPDU();
694 	assert(b.toString(HEX) == "00A4000C023F0000");
695 	var c = new APDU(b);
696 	assert(a.toString() == c.toString());
697 
698 	// Case 4b Extended
699 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString(data, HEX), 0);
700 	print(a);
701 	assert(a.isExtendedLength());
702 	var b = a.getCommandAPDU();
703 	assert(b.toString(HEX) == "00A4000C000100" + data + "0000");
704 	var c = new APDU(b);
705 	assert(a.toString() == c.toString());
706 
707 	// Case 4b Extended
708 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX), 65536);
709 	print(a);
710 	assert(a.isExtendedLength());
711 	var b = a.getCommandAPDU();
712 	assert(b.toString(HEX) == "00A4000C0000023F000000");
713 	var c = new APDU(b);
714 	assert(a.toString() == c.toString());
715 }
716