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  * @fileoverview Reset card to transport configuration
 25  */
 26 
 27 load("mifare.js");
 28 
 29 
 30 var card = new Card(_scsh3.reader);
 31 
 32 card.reset(Card.RESET_COLD);
 33 
 34 var mif = new Mifare(card);
 35 
 36 print("UID: " + mif.getUID());
 37 
 38 if (_scsh3.reader.substr(0, 3) != "SCM") {
 39 	var keyaid = 0x00;			// Use for ACR and Omnikey readers
 40 	var keybid = 0x01;			// Use for ACR and Omnikey readers
 41 } else {
 42 	print("SCM Reader detected.");
 43 	var keyaid = 0x60;			// Use for SCS SDI 010 and 011
 44 	var keybid = 0x61;			// Use for SCS SDI 010 and 011
 45 }
 46 
 47 var empty = new ByteString("00000000000000000000000000000000", HEX);
 48 
 49 for (var i = 0; i < 16; i++) {
 50 	var s = mif.newSector(i);
 51 	s.setKeyId(keyaid, Mifare.KEY_A);
 52 	s.setKeyId(keybid, Mifare.KEY_B);
 53 	
 54 	var ki = s.authenticatePublic(0, Mifare.KEY_A);
 55 
 56 	if (ki < 0) {
 57 		print("Unknown key A - skipping sector " + i);
 58 		continue;
 59 	}
 60 	
 61 	var header = s.read(3);
 62 	print(s.toString());
 63 
 64 	var ac = s.getACforBlock(3);
 65 	if (ac == Sector.AC_UPDATE_WITH_KEYB) {
 66 		var ki = s.authenticatePublic(0, Mifare.KEY_B);
 67 		
 68 		if (ki < 0) {
 69 			print("Unknown key B - skipping sector " + i);
 70 			continue;
 71 		}
 72 	}
 73 	
 74 	s.setKeyA(new ByteString("FFFFFFFFFFFF", HEX));
 75 	s.setKeyB(new ByteString("FFFFFFFFFFFF", HEX));
 76 	s.setHeaderDataByte(new ByteString("69", HEX));
 77 
 78 	s.setACforBlock(0, Sector.AC_ALWAYS);
 79 	s.setACforBlock(1, Sector.AC_ALWAYS);
 80 	s.setACforBlock(2, Sector.AC_ALWAYS);
 81 	s.setACforBlock(3, Sector.AC_UPDATE_AC_NOKEY_B);
 82 	
 83 	s.update(3);
 84 	
 85 	var key = new ByteString("FFFFFFFFFFFF", HEX);
 86 	mif.loadKey(keyaid, key);
 87 	s.authenticate(0, Mifare.KEY_A);
 88 	
 89 	if (i > 0) {
 90 		s.update(0, empty);
 91 	}
 92 	s.update(1, empty);
 93 	s.update(2, empty);
 94 }
 95