Advertisement
Guest User

bananas

a guest
Jul 5th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.96 KB | None | 0 0
  1. /**
  2. * <p>Title: pteidlib JNI Test</p>
  3. * <p>Description: Test pteidlib jni interface</p>
  4. * <p>Copyright: Copyright (c) 2007</p>
  5. * <p>Company: Zetes</p>
  6. * @author not attributable
  7. * @version 1.0
  8. */
  9.  
  10. import java.io.*;
  11. import pteidlib.*;
  12. import java.math.BigInteger;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public class Test
  17. {
  18. static
  19. {
  20. try
  21. {
  22. System.loadLibrary("pteidlibj");
  23. }
  24. catch (UnsatisfiedLinkError e)
  25. {
  26. System.err.println("Native code library failed to load.\n" + e);
  27. System.exit(1);
  28. }
  29. }
  30.  
  31. public void PrintIDData(PTEID_ID idData)
  32. {
  33. System.out.println("DeliveryEntity : " + idData.deliveryEntity);
  34. System.out.println("PAN : " + idData.cardNumberPAN);
  35. System.out.println("...");
  36. }
  37.  
  38. public void PrintAddressData(PTEID_ADDR adData)
  39. {
  40. if("N".equals(adData.addrType))
  41. {
  42. System.out.println("Type : National");
  43. System.out.println("Street : " + adData.street);
  44. System.out.println("Municipality : " + adData.municipality);
  45. System.out.println("...");
  46. }
  47. else
  48. {
  49. System.out.println("Type : International");
  50. System.out.println("Address : " + adData.addressF);
  51. System.out.println("City : " + adData.cityF);
  52. System.out.println("...");
  53. }
  54. }
  55.  
  56. private static void fillPersoFile()
  57. {
  58. byte PTEID_AUTH_PIN_ID = (byte)0x81;
  59. byte[] persoFile = { (byte)0x3F, (byte)0x00, (byte)0x5F, (byte)0x00, (byte)0xEF, (byte)0x07 };
  60. String[] poema = new String[3];
  61. poema[0] = "As armas e os baroes assinalados\n" +
  62. "Que da Ocidental praia Lusitana,\n" +
  63. "Por mares nunca dantes navegados\n" +
  64. "Passaram ainda além da Taprobana,\n" +
  65. "Em perigos e guerras esforçados\n" +
  66. "Mais do que prometia a força humana\n" +
  67. "E entre gente remota edificaram\n" +
  68. "Novo Reino, que tanto sublimaram;\n\n";
  69. poema[1] = "E tambem as memorias gloriosas\n" +
  70. "Daqueles Reis que foram dilatando\n" +
  71. "A Fe, o Imperio, e as terras viciosas\n" +
  72. "De Africa e de Asia andaram devastando,\n" +
  73. "E aqueles que por obras valerosas\n" +
  74. "Se vão da lei da Morte libertando\n" +
  75. "Cantando espalharei por toda a parte\n" +
  76. "Se a tanto me ajudar o engenho e arte.\n\n";
  77. poema[2] = "Cessem do sábio Grego e do Troiano\n" +
  78. "As navegacoes grandes que fizeram;\n" +
  79. "Cale-se de Alexandro e de Trajano\n" +
  80. "A fama das vitórias que tiveram;\n" +
  81. "Que eu canto o peito ilustre Lusitano,\n" +
  82. "A quem Neptuno e Marte obedeceram:\n" +
  83. "Cesse tudo o que a Musa antiga canta,\n" +
  84. "Que outro valor mais alto se alevanta.";
  85.  
  86. int countOffset = 0;
  87. for (String estrofe : poema)
  88. {
  89. try {
  90. //byte[] infile = Encoding.UTF8.GetBytes(estrofe);
  91. byte[] infile = estrofe.getBytes();
  92. pteid.WriteFileInOffset(persoFile, infile, PTEID_AUTH_PIN_ID, countOffset);
  93. countOffset += infile.length;
  94. } catch (PteidException ex) {
  95. Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  96. }
  97. }
  98. }
  99.  
  100. public static void main(String[] args)
  101. {
  102. int ret = 0;
  103. Test test = new Test();
  104. try
  105. {
  106. // test.TestCVC();
  107.  
  108. pteid.Init("");
  109.  
  110. //test.TestChangeAddress();
  111.  
  112. // Don't check the integrity of the ID, address and photo (!)
  113. pteid.SetSODChecking(false);
  114.  
  115. int cardtype = pteid.GetCardType();
  116. switch (cardtype)
  117. {
  118. case pteid.CARD_TYPE_IAS07:
  119. System.out.println("IAS 0.7 card\n");
  120. break;
  121. case pteid.CARD_TYPE_IAS101:
  122. System.out.println("IAS 1.0.1 card\n");
  123. break;
  124. case pteid.CARD_TYPE_ERR:
  125. System.out.println("Unable to get the card type\n");
  126. break;
  127. default:
  128. System.out.println("Unknown card type\n");
  129. }
  130.  
  131. fillPersoFile();
  132.  
  133. // // Read ID Data
  134. // PTEID_ID idData = pteid.GetID();
  135. // if (null != idData)
  136. // {
  137. // test.PrintIDData(idData);
  138. // }
  139. //
  140. // // Read Address
  141. // PTEID_ADDR adData = pteid.GetAddr();
  142. // if (null != adData)
  143. // {
  144. // test.PrintAddressData(adData);
  145. // }
  146. //
  147. // // Read Picture Data
  148. // PTEID_PIC picData = pteid.GetPic();
  149. // if (null != picData)
  150. // {
  151. // try
  152. // {
  153. // String photo = "photo.jp2";
  154. // FileOutputStream oFile = new FileOutputStream(photo);
  155. // oFile.write(picData.picture);
  156. // oFile.close();
  157. // System.out.println("Created " + photo);
  158. // }
  159. // catch (FileNotFoundException excep)
  160. // {
  161. // System.out.println(excep.getMessage());
  162. // }
  163. // catch(Exception e)
  164. // {
  165. // e.printStackTrace();
  166. // }
  167. // }
  168. //
  169. // // PIN operations
  170. // int triesLeft = pteid.VerifyPIN((byte)0x83, null);
  171. // //triesLeft = pteid.ChangePIN((byte)0x83, null, null);
  172. //
  173. // // Read Certificates
  174. // PTEID_Certif[] certs = pteid.GetCertificates();
  175. // System.out.println("Number of certs found: " + certs.length);
  176. //
  177. // // Read Pins
  178. // PTEID_Pin[] pins = pteid.GetPINs();
  179. //
  180. // // Read TokenInfo
  181. // PTEID_TokenInfo token = pteid.GetTokenInfo();
  182. //
  183. // // Read personal Data
  184. // byte[] filein = {0x3F, 0x00, 0x5F, 0x00, (byte)0xEF, 0x07};
  185. // byte[] file = pteid.ReadFile(filein, (byte)0x81);
  186. //
  187. // // Write personal data
  188. // String data = "Hallo JNI";
  189. // pteid.WriteFile(filein, data.getBytes(), (byte)0x81);
  190. //
  191. // pteid.Exit(pteid.PTEID_EXIT_LEAVE_CARD);
  192. }
  193. catch (PteidException ex)
  194. {
  195. ex.printStackTrace();
  196. //System.out.println(ex.getMessage());
  197. }
  198. catch (Exception ex)
  199. {
  200. ex.printStackTrace();
  201. //System.out.println(ex.getMessage());
  202. }
  203. }
  204.  
  205. ////////////////////////////////// CVC ////////////////////////////////////
  206.  
  207. // Modify these values for you test card!
  208. static String csCvcCertRole01 = "7F2181CD5F37818086B81455BCF0F508CF79FE9CAD574CA631EBC392D78869C4DC29DB193D75AC7E1BB1852AA57FA54C7E7FA97CBB536F2FA384C90C4FF62EAB119156016353AEAFD0F2E2B41BF89CCFE2C5F463A4A30DC38F2B9145DA3F12C40E2F394E7EE606A4C9377253D6E46D7B538B34C712B964F4A20A5724E0F6E88E0D5D1188C39B75A85F383C6917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D0001000142085054474F56101106"; // CVC cert for writing
  209. static String csCvcCertRole02 = "7F2181CD5F3781804823ED79D2F59E61E842ABE0A58919E63F362C9133E873CA77DD79AD01009247460DFE0294DD0ABAABE1D262E69A165F2F1AC6E953E8ABBE3BF1D2ACD6EB69EE83AB918D6F5116589BE0D40E780D5635238B78AA4290AD32F2A6316D24B417E06591DE6A775C38CFD918CA4FD11146EA20E06FE7F73CA7B3D3058FA259745D875F383C6917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D0001000142085054474F56101106"; // CVC cert for reading
  210. static String csCvcMod = "924557F6E1C2F1898B391D9255CC72FD7F11128BA148CFEBD1F58AF3F363778157E262FD72A76BCCA0AB43D8F5272E00D21B8B0EE4CC7DA86C8189DEC0DDC58C6A54A81BCE5E52076917D61A07CFF92106D1885E393F68BA863520887168CE884242ED86F2F80397B42B883D931F8CCB141DC3579E5AB798B8CCF9A189B83B8D"; // private key modulus
  211. static String csCvcExp = "3B35A8CAFE4E6C79D20AB7C6C1C67611D97AEEB7E8FCD175D353030187578F4BA368B7CB82BAF4EF2B66C89B2D79C3AC7F60B8E4B98771A258F202FE51B23441EB29C68569B608EF1F4B3CF15C68744AA7A3800E364739D3C6DCB078EFB81EA3197C843EE17BD9BCF1E0FEB4FFB6719F923C63105206A2F5A77A0437D762E781"; // private key exponent
  212.  
  213. /**
  214. * BigInteger -> bytes array, taking into account that a leading 0x00 byte
  215. * can be added if the MSB is 1 (so we have to remove this 0x00 byte)
  216. */
  217. byte[] ToBytes(BigInteger bi, int size)
  218. {
  219. byte[] b = bi.toByteArray();
  220. if (b.length == size)
  221. return b;
  222. else if (b.length != size + 1) {
  223. System.out.println("length = " + b.length + " instead of " + size);
  224. return null;
  225. }
  226. byte[] r = new byte[size];
  227. System.arraycopy(b, 1, r, 0, size);
  228. return r;
  229. }
  230.  
  231. public byte[] SignChallenge(byte[] challenge)
  232. {
  233. BigInteger mod = new BigInteger("00" + csCvcMod, 16);
  234. BigInteger exp = new BigInteger("00" + csCvcExp, 16);
  235. BigInteger chall = new BigInteger(1, challenge);
  236.  
  237. BigInteger signat = chall.modPow(exp, mod);
  238.  
  239. return ToBytes(signat, 128);
  240. }
  241.  
  242. public void TestCVC() throws Exception
  243. {
  244. /* Convert a hex string to byte[] by using the BigInteger class,
  245. * taking into account that the MSB is taken to be the sign bit
  246. * (hence adding the "00")
  247. * CVC certs are always 209 bytes long */
  248. byte[] cert1 = ToBytes(new BigInteger("00" + csCvcCertRole01, 16), 209);
  249. byte[] cert2 = ToBytes(new BigInteger("00" + csCvcCertRole02, 16), 209);
  250.  
  251. byte[] fileAddr = { 0x3F, 0x00, 0x5F, 0x00, (byte)0xEF, 0x05 };
  252.  
  253. // Read the Address file
  254.  
  255. pteid.Init("");
  256. pteid.SetSODChecking(false);
  257.  
  258. byte[] challenge = pteid.CVC_Init(cert2);
  259. byte[] signat = SignChallenge(challenge);
  260. pteid.CVC_Authenticate(signat);
  261.  
  262. PTEID_ADDR addr = pteid.CVC_GetAddr();
  263. String country = addr.country;
  264. System.out.println("Reading address:");
  265. System.out.println(" addrType = " + addr.addrType);
  266. System.out.println(" country = " + country);
  267.  
  268. pteid.Exit(pteid.PTEID_EXIT_UNPOWER);
  269.  
  270. // Write to the Address file
  271.  
  272. System.out.println("Changing country name to \"XX\"");
  273.  
  274. pteid.Init("");
  275. pteid.SetSODChecking(false);
  276.  
  277. challenge = pteid.CVC_Init(cert1);
  278. signat = SignChallenge(challenge);
  279. pteid.CVC_Authenticate(signat);
  280.  
  281. addr.country = "XX";
  282. pteid.CVC_WriteAddr(addr);
  283.  
  284. pteid.Exit(pteid.PTEID_EXIT_UNPOWER);
  285.  
  286. System.out.println(" done");
  287.  
  288. // Read the Address file again
  289.  
  290. pteid.Init("");
  291. pteid.SetSODChecking(false);
  292.  
  293. challenge = pteid.CVC_Init(cert2);
  294. signat = SignChallenge(challenge);
  295. pteid.CVC_Authenticate(signat);
  296.  
  297. addr = pteid.CVC_GetAddr();
  298. System.out.println("Reading address again:");
  299. System.out.println(" addrType = " + addr.addrType);
  300. System.out.println(" country = " + country);
  301.  
  302. pteid.Exit(pteid.PTEID_EXIT_UNPOWER);
  303.  
  304. // Restore the previous address
  305.  
  306. System.out.println("Restoring country name");
  307.  
  308. pteid.Init("");
  309. pteid.SetSODChecking(false);
  310.  
  311. challenge = pteid.CVC_Init(cert1);
  312. signat = SignChallenge(challenge);
  313. pteid.CVC_Authenticate(signat);
  314.  
  315. addr.country = country;
  316. pteid.CVC_WriteAddr(addr);
  317.  
  318. pteid.Exit(pteid.PTEID_EXIT_UNPOWER);
  319.  
  320. System.out.println(" done");
  321. }
  322.  
  323. private static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  324. public static String ToHex(byte[] ba)
  325. {
  326. StringBuffer buf = new StringBuffer(3 * ba.length + 2);
  327. for (int i = 0; i < ba.length; i++)
  328. {
  329. int c = ba[i];
  330. if (c < 0)
  331. c += 256;
  332. buf.append(HEX[c / 16]);
  333. buf.append(HEX[c % 16]);
  334. buf.append(' ');
  335. }
  336.  
  337. return new String(buf);
  338. }
  339.  
  340. private static byte[] makeBA(int len, byte val)
  341. {
  342. byte ret[] = new byte[len];
  343. for (int i = 0; i < len; i++)
  344. ret[i] = val;
  345. return ret;
  346. }
  347.  
  348. /** Works only with when pteidlib is build with emulation code!!
  349. */
  350. public void TestChangeAddress() throws Exception
  351. {
  352. System.out.println("\n*********************************************\n");
  353.  
  354. // CVC_Init_SM101()
  355. byte[] ret = pteid.CVC_Init_SM101();
  356. System.out.println("CVC_Init_SM101: " + ToHex(ret));
  357.  
  358. System.out.println("\n*********************************************\n");
  359.  
  360. // CVC_Authenticate_SM101
  361. byte[] signedChallenge = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
  362. byte[] ifdSerialNr = { 0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44};
  363. byte[] iccSerialNr = { 0x44, 0x33, 0x22, 0x11 , 0x44, 0x33, 0x22, 0x11 };
  364. byte[] keyIfd= makeBA(32, (byte) 0x01);
  365. byte[] encKey = makeBA(16, (byte) 0x02);
  366. byte[] macKey = makeBA(16, (byte) 0x03);
  367. ret = pteid.CVC_Authenticate_SM101(signedChallenge,
  368. ifdSerialNr, iccSerialNr, keyIfd, encKey, macKey);
  369. System.out.println("CVC_Authenticate_SM101: " + ToHex(ret));
  370.  
  371. System.out.println("\n*********************************************\n");
  372.  
  373. // CVC_R_Init()
  374. PTEID_DH_Params dhParams = pteid.CVC_R_Init();
  375. System.out.println("CVC_R_Init: G = " + ToHex(dhParams.G));
  376. System.out.println("CVC_R_Init: P = " + ToHex(dhParams.P));
  377. System.out.println("CVC_R_Init: Q = " + ToHex(dhParams.Q));
  378.  
  379. System.out.println("\n*********************************************\n");
  380.  
  381. // CVC_R_DH_Auth()
  382. byte[] Kidf = makeBA(8, (byte) 0x22);
  383. byte[] cvcCert = ToBytes(new BigInteger("00" + csCvcCertRole01, 16), 209);
  384. PTEID_DH_Auth_Response dhAuthResp = pteid.CVC_R_DH_Auth(Kidf, cvcCert);
  385. System.out.println("CVC_R_DH_Auth: Kicc = " + ToHex(dhAuthResp.Kicc));
  386. System.out.println("CVC_R_DH_Auth: challenge = " + ToHex(dhAuthResp.challenge));
  387.  
  388. System.out.println("\n*********************************************\n");
  389.  
  390. // CVC_R_ValidateSignature
  391. System.out.println("CVC_R_ValidateSignature: signedChallenge = " + ToHex(signedChallenge));
  392. pteid.CVC_R_ValidateSignature(signedChallenge);
  393.  
  394. System.out.println("\n*********************************************\n");
  395.  
  396. // SendAPDU()
  397. ret = pteid.SendAPDU(new byte[] {0x00, 0x20, 0x00, (byte) 0x81});
  398. System.out.println("Response to case 1 APDU: " + ToHex(ret));
  399. ret = pteid.SendAPDU(new byte[] { (byte)0x80, (byte)0x84, 0x00, 0x00, 0x08 });
  400. System.out.println("Response to case 2 APDU: " + ToHex(ret));
  401. ret = pteid.SendAPDU(new byte[] { 0x00, (byte)0xA4, 0x02, 0x0C, 0x02, 0x2F, 0x00 });
  402. System.out.println("Response to case 3 APDU: " + ToHex(ret));
  403. ret = pteid.SendAPDU(new byte[] { 0x00, (byte)0xA4, 0x02, 0x00, 0x02, 0x50, 0x31, 0x50 });
  404. System.out.println("Response to case 4 APDU: " + ToHex(ret));
  405.  
  406. System.out.println("\n*********************************************\n");
  407.  
  408. // ChangeAddress()
  409. byte[] serverCaCert = makeBA(1200, (byte) 0x05);
  410. PTEID_Proxy_Info proxyInfo = new PTEID_Proxy_Info();
  411. proxyInfo.proxy = "10.3.98.67";
  412. proxyInfo.port = 4444;
  413. proxyInfo.username = "userX";
  414. proxyInfo.password = "passwdX";
  415. pteid.ChangeAddress("https://www.test.com/ChangeAddress", serverCaCert,
  416. proxyInfo, "secretcode", "processcode");
  417.  
  418. System.out.println("\n*********************************************\n");
  419.  
  420. // GetChangeAddressProgress()
  421. int res = pteid.GetChangeAddressProgress();
  422. System.out.println("GetChangeAddressProgress(): returned " + res);
  423.  
  424. System.out.println("\n*********************************************\n");
  425.  
  426. // CancelChangeAddress()
  427. pteid.CancelChangeAddress();
  428. System.out.println("GetChangeAddressProgress(): done");
  429.  
  430. System.out.println("\n*********************************************\n");
  431.  
  432. // GetLastWebErrorMessage()
  433. String msg = pteid.GetLastWebErrorMessage();
  434. System.out.println("GetLastWebErrorMessage(): returned " + msg);
  435.  
  436. System.out.println("\n*********************************************\n");
  437. }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement