1 /** 2 * --------- 3 * |.##> <##.| SmartCard-HSM Support Scripts 4 * |# #| 5 * |# #| Copyright (c) 2011-2012 CardContact Software & System Consulting 6 * |'##> <##'| Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de) 7 * --------- 8 * 9 * Consult your license package for usage terms and conditions. 10 * 11 * @fileoverview SmartCard-HSM Explorer 12 */ 13 14 15 load("tools/CardOutlineFactory2.0.js"); 16 load("../lib/smartcardhsm.js"); 17 18 19 /** 20 * SmartCard-HSM Outline that displays all contained EFs 21 * 22 * @param {Object} factory the factory creating this instance 23 * @param {Application} instance the application instance created from the application profile 24 */ 25 function scHSMOutline(factory, instance) { 26 this.factory = factory; 27 this.instance = instance; 28 29 // Create OutlineNode object and register in OutlineDF object 30 var view = new OutlineNode("SmartCard-HSM Applet", true); 31 view.setUserObject(this); 32 this.view = view; 33 } 34 35 36 37 /** 38 * Expand listener that enumerates files 39 */ 40 scHSMOutline.prototype.expandListener = function() { 41 var view = this.view; 42 43 try { 44 this.factory.schsm = new SmartCardHSM(this.factory.card); 45 var files = this.factory.schsm.enumerateObjects(); 46 47 for (var i = 0; i < files.length; i += 2) { 48 var fid = files.bytes(i, 2); 49 var fidstr = fid.toString(HEX); 50 var name = "EF." + fidstr; 51 52 if (fidstr == "2F02") { 53 name = "EF.C_DevAut"; 54 } 55 if (fid.byteAt(0) == SmartCardHSM.KEYPREFIX) { 56 name = "Key " + fid.byteAt(1); 57 } 58 var profile = { fid: fid.toString(HEX), format: "asn1" }; 59 var ef = this.factory.newOutlineEF(this.factory.schsm.card, name, profile); 60 view.insert(ef.view); 61 } 62 } 63 64 catch(e) { 65 print("Error reading EF.DIR: " + e); 66 } 67 } 68 69 70 71 /** 72 * Collapse listener that removes files from the view 73 */ 74 scHSMOutline.prototype.collapseListener = function() { 75 var view = this.view; 76 while (view.childs.length > 0) { 77 view.remove(view.childs[0]); 78 } 79 } 80 81 82 83 /** 84 * Create an EF view elements 85 * 86 */ 87 function scHSMOutlineEF(factory, df, name, profile) { 88 if (arguments.length == 0) 89 return; 90 91 this.factory = factory; 92 this.df = df; 93 94 // Create OutlineNode object and register in OutlineEF object 95 var view = new OutlineNode(name, true); 96 view.setIcon("document"); 97 view.setUserObject(this); 98 this.view = view; 99 100 this.profile = profile; 101 } 102 103 104 105 /** 106 * Expand listener that reads file content 107 */ 108 scHSMOutlineEF.prototype.expandListener = function() { 109 if (this.expanded) 110 return; 111 112 var view = this.view; 113 var efdesc = this.profile; 114 115 try { 116 var ef = new CardFile(this.df, ":" + efdesc.fid); 117 } 118 catch(e) { 119 print(e); 120 return; 121 } 122 123 var fcp = ef.getFCPBytes(); 124 if (fcp && (fcp.length > 1)) { 125 var fcpmodel = this.factory.newOutlineFCP(fcp); 126 view.insert(fcpmodel.view); 127 } 128 129 var bs = this.factory.schsm.readBinary(new ByteString(efdesc.fid, HEX)); 130 131 var bindata = this.factory.newDataOutline(bs, efdesc.format); 132 view.insert(bindata.view); 133 134 this.expanded = true; 135 } 136 137 138 139 /** 140 * Class overwriting the default CardOutlineFactory 141 * 142 */ 143 function scHSMCardOutlineFactory(card) { 144 this.card = card; 145 } 146 147 // Inherit from prototype 148 scHSMCardOutlineFactory.prototype = new CardOutlineFactory(); 149 150 // Restore constructor 151 scHSMCardOutlineFactory.constructor = scHSMCardOutlineFactory; 152 153 154 155 /** 156 * Overwrite newOutlineApplet() function 157 */ 158 scHSMCardOutlineFactory.prototype.newOutlineApplet = function(instance) { 159 160 return new scHSMOutline(this, instance); 161 } 162 163 164 165 /** 166 * Overwrite newOutlineApplet() function 167 */ 168 scHSMCardOutlineFactory.prototype.newOutlineEF = function(df, name, profile) { 169 return new scHSMOutlineEF(this, df, name, profile); 170 } 171 172 173 174 // 175 // Context menu 176 // 177 function OutlineCardActionListener(node, action) { 178 switch(action) { 179 case "Verify USER.PIN": 180 var card = node.userObject.card; 181 var dialog = new Packages.opencard.core.service.DefaultCHVDialog(); 182 var pin = dialog.getCHV(1); 183 184 if ((pin != null) && (pin.length > 0)) { 185 card.sendApdu(0x00, 0x20, 0x00, 0x01, new ByteString(pin, ASCII)); 186 if (this.card.SW != 0x9000) { 187 throw new GPError("GeneratorCard", GPError.DEVICE_ERROR, this.card.SW, "PIN Verification Error - SW1/SW2 = " + this.card.SW.toString(16) + " - " + this.card.SWMSG); 188 } 189 } else { 190 print("PIN entry cancelled"); 191 } 192 193 break; 194 } 195 } 196 197 198 199 // Create crypto object 200 var crypto = new Crypto(); 201 202 // Create application factory that holds all application profiles 203 var af = new ApplicationFactory(crypto); 204 205 // Add application profiles 206 af.addApplicationProfile("ap_sc_hsm.xml"); 207 208 // Create card object 209 var card = new Card(_scsh3.reader, "cp_sc_hsm.xml"); 210 211 // Create card outline factory 212 var of = new scHSMCardOutlineFactory(card); 213 214 // Create list of AIDs, just in case the EF.DIR is empty 215 // This is just temporary to make sure the explorer works 216 // even for card with a defect in EF.DIR 217 218 var aidlist = new Array(); 219 220 // and go... 221 try { 222 var sc = new OutlineCard(of, card, af, aidlist); 223 // sc.view.setContextMenu(["Verify USER.PIN"]); 224 sc.view.setToolTip("Click right to access context menu"); 225 sc.actionListener = OutlineCardActionListener; 226 sc.view.show(); 227 } 228 229 catch(e) { 230 print("Problem accessing the card: " + e); 231 } 232