1 /*
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|  
  5  * |#       #|  Copyright (c) 1999-2006 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  *  Display the status of the MuscleCard Applet
 25  */
 26 
 27 load("tools.js");
 28 
 29 var reset = true;
 30 var resp;
 31 
 32 // If we still have a valid card handle, then we use GetStatus directly
 33 if (typeof(card) != "undefined") {
 34 	try	{
 35 		// Try MSCGetStatus
 36 		resp = card.sendApdu(0xB0, 0x3C, 0x00, 0x00, 0x05);
 37 		reset = false;
 38 	}
 39 	catch(e) {
 40 		// The card handle is no longer valid, e.g. the card
 41 		// has been removed or replaced
 42 	}
 43 }
 44 
 45 if (reset) {
 46 	var card = new Card(_scsh3.reader);
 47 	card.reset(Card.RESET_COLD);
 48 	
 49 	// Select Applet
 50 	card.sendApdu(0x00, 0xA4, 0x04, 0x00, mcaid, [0x9000]);
 51 	resp = card.sendApdu(0xB0, 0x3C, 0x00, 0x00, 0x05);
 52 }
 53 
 54 
 55 if (card.SW == 0x9C05) {
 56         print("Applet not initialized");
 57 } else {
 58 	if (card.SW1 == 0x61) {
 59 		var rem = card.sendApdu(0x00, 0xC0, 0x00, 0x00, card.SW1);
 60 		resp = resp.concat(rem);
 61 	}
 62 
 63 	printStatus(resp);
 64 
 65 	// MSCListPINs	
 66 	resp = card.sendApdu(0xB0, 0x48, 0x00, 0x00, 0x02, [0x9000]);
 67 	var v = resp.toUnsigned();
 68 	print("List PINs: " + accessMaskToString(v, "none", "all", ",") + " (" + v.toString(16) + ")");
 69 
 70 	print("-- List Objects ------------------------------------------------");
 71 	// MSCListObjects	
 72 
 73 	resp = card.sendApdu(0xB0, 0x58, 0x00, 0x00, 0x0E);
 74 	
 75 	while(resp.length > 0) {
 76 //		print(resp);
 77 
 78 		print("  Object Id     : " + resp.bytes(0, 4));
 79 		var objid = resp.bytes(0, 4).toUnsigned();
 80 		var size = resp.bytes(4, 4).toUnsigned();
 81 		print("  Size          : " + size);
 82 		var v = resp.bytes(8, 2).toUnsigned();
 83 		print("  Read Access   : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
 84 		var v = resp.bytes(10, 2).toUnsigned();
 85 		print("  Write Access  : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
 86 		var v = resp.bytes(12, 2).toUnsigned();
 87 		print("  Delete Access : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
 88 
 89 		try	{
 90 			var content = readObject(card, objid, 0, size);
 91 			print(content);
 92 		}
 93 		catch(e) {
 94 			if (e instanceof GPError) {
 95 				print("Error reading object - SW1/SW2 = " + e.reason.toString(16));
 96 			} else {
 97 				print("Exception reading object: " + e);
 98 			}
 99 		}
100 		
101 		resp = card.sendApdu(0xB0, 0x58, 0x01, 0x00, 0x0E);
102 	}
103 
104 	print("-- List Keys ---------------------------------------------------");
105 
106 	// MSCListKeys
107 	resp = card.sendApdu(0xB0, 0x3A, 0x00, 0x00, 0x0B);
108 	
109 	while(resp.length > 0) {
110 //		print(resp);
111 		print("  Key Number    : " + resp.byteAt(0));
112 		var v = resp.byteAt(1)
113 		print("  Key Type      : " + mckeytypes[v] + " (" + v + ")");
114 		print("  Key Partner   : " + resp.byteAt(2));
115 		print("  Key Size      : " + resp.bytes(3, 2).toUnsigned());
116 		var v = resp.bytes(5, 2).toUnsigned();
117 		print("  Read Access   : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
118 		var v = resp.bytes(7, 2).toUnsigned();
119 		print("  Write Access  : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
120 		var v = resp.bytes(9, 2).toUnsigned();
121 		print("  Use Access    : " + accessConditionToString(v) + " (" + v.toString(16) + ")");
122 		
123 		resp = card.sendApdu(0xB0, 0x3A, 0x01, 0x00, 0x0B);
124 	}
125 }