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_EXTERNAL_AUTHENTICATE	= 0x82;
105 APDU.INS_GET_CHALLENGE		= 0x84;
106 APDU.INS_GENERAL_AUTHENTICATE	= 0x86;
107 APDU.INS_COMPUTE_DIGITAL_SIGN	= 0x9E;
108 APDU.INS_SELECT			= 0xA4;
109 APDU.INS_READBINARY		= 0xB0;
110 APDU.INS_READ_BINARY		= 0xB0;
111 APDU.INS_READ_RECORD		= 0xB2;
112 APDU.INS_VERIFY_CERTIFICATE	= 0xBE;
113 APDU.INS_UPDATE_BINARY		= 0xD6;
114 APDU.INS_TERMINATE		= 0xE6;
115 
116 APDU.SW_OK                 = 0x9000;      	/* Process completed                 */
117 
118 APDU.SW_TIMEOUT            = 0x6401;      	/* Exec error: Command timeout       */
119 
120 APDU.SW_OKMOREDATA         = 0x6100;      	/*-Process completed, more data available*/
121 APDU.SW_WARNING            = 0x6200;      	/*-Warning: NV-Ram not changed       */
122 APDU.SW_WARNING1           = 0x6201;      	/*-Warning: NV-Ram not changed 1     */
123 APDU.SW_DATAINV            = 0x6281;      	/*-Warning: Part of data corrupted   */
124 APDU.SW_EOF                = 0x6282;      	/*-Warning: End of file reached      */
125 APDU.SW_INVFILE            = 0x6283;      	/* Warning: Invalidated file         */
126 APDU.SW_INVFORMAT          = 0x6284;      	/* Warning: Invalid file control     */
127 APDU.SW_WARNINGNVCHG       = 0x6300;      	/*-Warning: NV-Ram changed           */
128 APDU.SW_WARNINGCOUNT       = 0x63C0;      	/*-Warning: Warning with counter     */
129 APDU.SW_WARNING0LEFT       = 0x63C0;      	/*-Warning: Verify fail, no try left */
130 APDU.SW_WARNING1LEFT       = 0x63C1;      	/*-Warning: Verify fail, 1 try left  */
131 APDU.SW_WARNING2LEFT       = 0x63C2;      	/*-Warning: Verify fail, 2 tries left*/
132 APDU.SW_WARNING3LEFT       = 0x63C3;      	/*-Warning: Verify fail, 3 tries left*/
133 APDU.SW_EXECERR            = 0x6400;      	/*-Exec error: NV-Ram not changed    */
134 APDU.SW_MEMERR             = 0x6501;      	/*-Exec error: Memory failure        */
135 APDU.SW_MEMERRWRITE        = 0x6581;      	/*-Exec error: Memory failure        */
136 APDU.SW_WRONGLENGTH        = 0x6700;      	/*-Checking error: Wrong length      */
137 
138 APDU.SW_CLANOTSUPPORTED    = 0x6800;      	/*-Checking error: Function in CLA byte not supported */
139 APDU.SW_LCNOTSUPPORTED     = 0x6881;      	/*-Checking error: Logical channel not supported */
140 APDU.SW_SMNOTSUPPORTED     = 0x6882;      	/*-Checking error: Secure Messaging not supported */
141 APDU.SW_LASTCMDEXPECTED    = 0x6883;      	/*-Checking error: Last command of the chain expected */
142 APDU.SW_CHAINNOTSUPPORTED  = 0x6884;      	/*-Checking error: Command chaining not supported */
143 
144 APDU.SW_COMNOTALLOWED      = 0x6900;      	/*-Checking error: Command not allowed */
145 APDU.SW_COMINCOMPATIBLE    = 0x6981;      	/*-Checking error: Command incompatible with file structure */
146 APDU.SW_SECSTATNOTSAT      = 0x6982;      	/*-Checking error: Security condition not satisfied */
147 APDU.SW_AUTHMETHLOCKED     = 0x6983;      	/*-Checking error: Authentication method locked */
148 APDU.SW_REFDATANOTUSABLE   = 0x6984;      	/*-Checking error: Reference data not usable */
149 APDU.SW_CONDOFUSENOTSAT    = 0x6985;      	/*-Checking error: Condition of use not satisfied */
150 APDU.SW_COMNOTALLOWNOEF    = 0x6986;      	/*-Checking error: Command not allowed (no current EF) */
151 APDU.SW_SMOBJMISSING       = 0x6987;      	/*-Checking error: Expected secure messaging object missing */
152 APDU.SW_INCSMDATAOBJECT    = 0x6988;      	/*-Checking error: Incorrect secure messaging data object */
153 
154 APDU.SW_INVPARA            = 0x6A00;      	/*-Checking error: Wrong parameter P1-P2 */
155 APDU.SW_INVDATA            = 0x6A80;      	/*-Checking error: Incorrect parameter in the command data field*/
156 APDU.SW_FUNCNOTSUPPORTED   = 0x6A81;      	/*-Checking error: Function not supported */
157 APDU.SW_NOAPPL             = 0x6A82;      	/*-Checking error: File not found    */
158 APDU.SW_FILENOTFOUND       = 0x6A82;      	/*-Checking error: File not found    */
159 APDU.SW_RECORDNOTFOUND     = 0x6A83;      	/*-Checking error: Record not found    */
160 APDU.SW_OUTOFMEMORY        = 0x6A84;      	/*-Checking error: Not enough memory space in the file   */
161 APDU.SW_INVLCTLV           = 0x6A85;      	/*-Checking error: Nc inconsistent with TLV structure */
162 APDU.SW_INVACC             = 0x6A85;      	/*-Checking error: Access cond. n/f  */
163 APDU.SW_INCP1P2            = 0x6A86;      	/*-Checking error: Incorrect P1-P2   */
164 APDU.SW_INVLC              = 0x6A87;      	/*-Checking error: Lc inconsistent with P1-P2 */
165 APDU.SW_RDNOTFOUND         = 0x6A88;      	/*-Checking error: Reference data not found*/
166 APDU.SW_FILEEXISTS         = 0x6A89;      	/*-Checking error: File already exists */
167 APDU.SW_DFNAMEEXISTS       = 0x6A8A;      	/*-Checking error: DF name already exists */
168 
169 APDU.SW_INVP1P2            = 0x6B00;      	/*-Checking error: Wrong parameter P1-P2 */
170 APDU.SW_INVLE              = 0x6C00;      	/*-Checking error: Invalid Le        */
171 APDU.SW_INVINS             = 0x6D00;      	/*-Checking error: Wrong instruction */
172 APDU.SW_INVCLA             = 0x6E00;      	/*-Checking error: Class not supported */
173 APDU.SW_ACNOTSATISFIED     = 0x9804;      	/* Access conditions not satisfied   */
174 APDU.SW_NOMORESTORAGE      = 0x9210;      	/* No more storage available         */
175 APDU.SW_GENERALERROR       = 0x6F00;      	/*-Checking error: No precise diagnosis */
176 
177 
178 /**
179  * Create an APDU object from the encoded form (Called internally)
180  *
181  * @param {ByteString} bs
182  */
183 APDU.prototype.fromByteString = function(bs) {
184 	if (bs.length < 4) {
185 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_GENERALERROR, "Command APDU must be at least 4 bytes long");
186 	}
187 	this.cla = bs.byteAt(0);
188 	this.ins = bs.byteAt(1);
189 	this.p1 = bs.byteAt(2);
190 	this.p2 = bs.byteAt(3);
191 
192 	if (bs.length > 4) {
193 		var extended = false;
194 
195 		var i = 4;
196 		var l = bs.length - i;
197 		var n = bs.byteAt(i++);
198 		l--;
199 
200 		if ((n == 0) && (l > 0)) {
201 			extended = true;
202 			if (l < 2) {
203 				throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Extended length APDU too short");
204 			}
205 			n = (bs.byteAt(i) << 8) + bs.byteAt(i + 1);
206 			i += 2;
207 			l -= 2;
208 		}
209 
210 		if (l > 0) {	// Case 3s / Case 3e / Case 4s / Case 4e
211 			if (l < n) {
212 				throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Invalid Lc in APDU");
213 			}
214 			this.cdata = bs.bytes(i, n);
215 			i += n;
216 			l -= n;
217 
218 			if (l > 0) {	// Case 4s / Case 4e
219 				n = bs.byteAt(i++);
220 				l--;
221 				if (extended) {
222 					if (l < 1) {
223 						throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Invalid Le in extended APDU");
224 					}
225 					n = (n << 8) + bs.byteAt(i++);
226 					l--;
227 				}
228 				this.ne = (extended && (n == 0) ? 65536 : n);
229 			}
230 		} else {
231 			this.ne = (extended && (n == 0) ? 65536 : n);
232 		}
233 
234 		if (l > 0) {
235 			throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_WRONGLENGTH, "Too many bytes in APDU");
236 		}
237 	}
238 }
239 
240 
241 
242 /**
243  * Get encoded command APDU
244  *
245  * @type ByteString
246  * @return the encoded command APDU
247  */
248 APDU.prototype.getCommandAPDU = function() {
249 	var bb = new ByteBuffer();
250 
251 	bb.append(this.cla);
252 	bb.append(this.ins);
253 	bb.append(this.p1);
254 	bb.append(this.p2);
255 
256 	var hasCData = (typeof(this.cdata) != "undefined");
257 	var hasNe = (typeof(this.ne) != "undefined");
258 
259 	var extended = ((hasCData && this.cdata.length > 255) ||
260 					(hasNe && this.ne > 256));
261 
262 	if (extended) {
263 		bb.append(0);
264 	}
265 
266 	if (hasCData && this.cdata.length > 0) {
267 		if (extended) {
268 			bb.append(this.cdata.length >> 8);
269 		}
270 		bb.append(this.cdata.length & 0xFF);
271 		bb.append(this.cdata);
272 	}
273 
274 	if (hasNe) {
275 		if (extended) {
276 			bb.append(this.ne >> 8);
277 		}
278 		bb.append(this.ne & 0xFF);
279 	}
280 
281 	return bb.toByteString();
282 }
283 
284 
285 
286 /**
287  * Get encoded response APDU
288  *
289  * @type ByteString
290  * @return the encoded response APDU
291  */
292 APDU.prototype.getResponseAPDU = function() {
293 	var bb = new ByteBuffer();
294 
295 	if (this.rdata) {
296 		bb.append(this.rdata);
297 	}
298 
299 	bb.append(this.SW >> 8);
300 	bb.append(this.SW & 0xFF);
301 
302 	return bb.toByteString();
303 }
304 
305 
306 
307 /**
308  * Gets the class byte
309  *
310  * @type Number
311  * @return the class byte
312  */
313 APDU.prototype.getCLA = function() {
314 	return this.cla;
315 }
316 
317 
318 
319 /**
320  * Test if command is an ISO command
321  *
322  * @type boolean
323  * @return true if command has ISO class byte
324  */
325 APDU.prototype.isISO = function() {
326 	return (this.cla & 0x80) == 0x00;
327 }
328 
329 
330 
331 /**
332  * Test if command chaining is indicated
333  *
334  * @type boolean
335  * @return true if chaining bit is set
336  */
337 APDU.prototype.isChained = function() {
338 	return (this.cla & 0x10) == 0x10;
339 }
340 
341 
342 
343 /**
344  * Test if command is send using secure messaging
345  *
346  * @type boolean
347  * @return true if secure messaging is indicated in CLA byte
348  */
349 APDU.prototype.isSecureMessaging = function() {
350 	return (this.cla & 0x08) == 0x08;
351 }
352 
353 
354 
355 /**
356  * Test if command is send using secure messaging
357  *
358  * @type boolean
359  * @return true if secure messaging is using an authenticated header
360  */
361 APDU.prototype.isAuthenticatedHeader = function() {
362 	return (this.cla & 0x0C) == 0x0C;
363 }
364 
365 
366 
367 /**
368  * Gets the instruction byte
369  *
370  * @type Number
371  * @return the instruction byte
372  */
373 APDU.prototype.getINS = function() {
374 	return this.ins;
375 }
376 
377 
378 
379 /**
380  * Gets the P1 byte
381  *
382  * @type Number
383  * @return the P1 byte
384  */
385 APDU.prototype.getP1 = function() {
386 	return this.p1;
387 }
388 
389 
390 
391 /**
392  * Gets the P2 byte
393  *
394  * @type Number
395  * @return the P2 byte
396  */
397 APDU.prototype.getP2 = function() {
398 	return this.p2;
399 }
400 
401 
402 
403 /**
404  * Set the command data
405  *
406  * @param {ByteString} cdata the command data
407  */
408 APDU.prototype.setCData = function(cdata) {
409 	this.cdata = cdata;
410 }
411 
412 
413 
414 /**
415  * Gets the command data
416  *
417  * @type ByteString
418  * @return the command data, if any else undefined
419  */
420 APDU.prototype.getCData = function() {
421 	return this.cdata;
422 }
423 
424 
425 
426 /**
427  * Check if APDU has command data
428  *
429  * @type boolean
430  * @return true if command APDU has data field
431  */
432 APDU.prototype.hasCData = function() {
433 	return ((typeof(this.cdata) != "undefined") && (this.cdata != null));
434 }
435 
436 
437 
438 /**
439  * Gets the command data as a list of TLV objects
440  *
441  * @type TLVList
442  * @return the command data as TLV list, if any else undefined
443  */
444 APDU.prototype.getCDataAsTLVList = function() {
445 	if (typeof(this.cdata) == "undefined") {
446 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_INVDATA, "No data in command APDU");
447 	}
448 
449 	try	{
450 		var a = new TLVList(this.cdata, TLV.EMV);
451 	}
452 	catch(e) {
453 		throw new GPError("APDU", GPError.INVALID_DATA, APDU.SW_INVDATA, "Invalid TLV data in command APDU");
454 	}
455 
456 	return a;
457 }
458 
459 
460 
461 /**
462  * Gets the number of expected bytes
463  *
464  * @type Number
465  * @return the number of expected bytes or undefined
466  */
467 APDU.prototype.getNe = function() {
468 	return this.ne;
469 }
470 
471 
472 
473 /**
474  * Check if APDU has Le field
475  *
476  * @type boolean
477  * @return true if command APDU has Le field
478  */
479 APDU.prototype.hasLe = function() {
480 	return typeof(this.ne) != "undefined";
481 }
482 
483 
484 
485 /**
486  * Set secure channel object to be used in wrap and unwrap methods
487  *
488  * @param {SecureChannel} secureChannel the channel
489  */
490 APDU.prototype.setSecureChannel = function(secureChannel) {
491 	this.secureChannel = secureChannel;
492 }
493 
494 
495 
496 /**
497  * Return the secure channel, if any
498  *
499  * @type SecureChannel
500  * @return the secure channel
501  */
502 APDU.prototype.getSecureChannel = function() {
503 	return this.secureChannel;
504 }
505 
506 
507 
508 /**
509  * Test if a secure channel is defined for this APDU
510  *
511  * @type boolean
512  * @return true, if secure channel is set
513  */
514 APDU.prototype.hasSecureChannel = function() {
515 	return (typeof(this.secureChannel) != "undefined") && (this.secureChannel != null);
516 }
517 
518 
519 
520 /**
521  * Wrap APDU using secure channel
522  */
523 APDU.prototype.wrap = function() {
524 	if (this.hasSecureChannel()) {
525 		this.secureChannel.wrap(this);
526 	}
527 }
528 
529 
530 
531 /**
532  * Unwrap APDU using secure channel
533  */
534 APDU.prototype.unwrap = function() {
535 	if (this.hasSecureChannel()) {
536 		this.secureChannel.unwrap(this);
537 	}
538 }
539 
540 
541 
542 /**
543  * Sets the response data field for the response APDU
544  *
545  * @param {ByteString} data the response data field
546  */
547 APDU.prototype.setRData = function(data) {
548 	this.rdata = data;
549 }
550 
551 
552 
553 /**
554  * Get the response data
555  *
556  * @type ByteString
557  * @return the response data
558  */
559 APDU.prototype.getRData = function() {
560 	return this.rdata;
561 }
562 
563 
564 
565 /**
566  * Check if APDU has response data
567  *
568  * @type boolean
569  * @return true if response APDU has data field
570  */
571 APDU.prototype.hasRData = function() {
572 	return ((typeof(this.rdata) != "undefined") && (this.rdata != null));
573 }
574 
575 
576 
577 /**
578  * Sets the status word for the response ADPU
579  *
580  * @param {Number} sw the status word
581  */
582 APDU.prototype.setSW = function(sw) {
583 	this.SW = sw;
584 }
585 
586 
587 
588 /**
589  * Get the status word
590  *
591  * @type Number
592  * @return the status word
593  */
594 APDU.prototype.getSW = function() {
595 	return this.SW;
596 }
597 
598 
599 
600 /**
601  * Return a human readable form of this object
602  */
603 APDU.prototype.toString = function() {
604 	return this.getCommandAPDU().toString(HEX) + " : " + this.getResponseAPDU().toString(HEX);
605 }
606 
607 
608 
609 /**
610  * Simple unit test
611  */
612 APDU.test = function() {
613 	// Case 1
614 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C);
615 	print(a);
616 	var b = a.getCommandAPDU();
617 	assert(b.toString(HEX) == "00A4000C");
618 	var c = new APDU(b);
619 	assert(a.toString() == c.toString());
620 
621 	// Case 2 Short
622 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, 0);
623 	print(a);
624 	var b = a.getCommandAPDU();
625 	assert(b.toString(HEX) == "00A4000C00");
626 	var c = new APDU(b);
627 	assert(a.toString() == c.toString());
628 
629 	// Case 2 Extended
630 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, 65536);
631 	print(a);
632 	var b = a.getCommandAPDU();
633 	assert(b.toString(HEX) == "00A4000C000000");
634 	var c = new APDU(b);
635 	print(c);
636 	assert(a.toString() == c.toString());
637 
638 	// Case 3 Short
639 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX));
640 	print(a);
641 	var b = a.getCommandAPDU();
642 	assert(b.toString(HEX) == "00A4000C023F00");
643 	var c = new APDU(b);
644 	assert(a.toString() == c.toString());
645 
646 	// Case 3 Extended
647 	var data = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
648 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString(data, HEX));
649 	print(a);
650 	var b = a.getCommandAPDU();
651 	assert(b.toString(HEX) == "00A4000C000100" + data);
652 	var c = new APDU(b);
653 	assert(a.toString() == c.toString());
654 
655 	// Case 4 Short
656 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX), 0);
657 	print(a);
658 	var b = a.getCommandAPDU();
659 	assert(b.toString(HEX) == "00A4000C023F0000");
660 	var c = new APDU(b);
661 	assert(a.toString() == c.toString());
662 
663 	// Case 4b Extended
664 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString(data, HEX), 0);
665 	print(a);
666 	var b = a.getCommandAPDU();
667 	assert(b.toString(HEX) == "00A4000C000100" + data + "0000");
668 	var c = new APDU(b);
669 	assert(a.toString() == c.toString());
670 
671 	// Case 4b Extended
672 	var a = new APDU(0x00, 0xA4, 0x00, 0x0C, new ByteString("3F00", HEX), 65536);
673 	print(a);
674 	var b = a.getCommandAPDU();
675 	assert(b.toString(HEX) == "00A4000C0000023F000000");
676 	var c = new APDU(b);
677 	assert(a.toString() == c.toString());
678 
679 }
680