1 /*
   2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 // common utility functions for the PC/SC tests
  26 
  27 import java.io.StringReader;
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.IOException;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import javax.smartcardio.CardTerminal;
  33 import javax.smartcardio.CardChannel;
  34 import javax.smartcardio.ResponseAPDU;
  35 import javax.smartcardio.CommandAPDU;
  36 import javax.smartcardio.TerminalFactory;
  37 
  38 public class Utils {
  39 
  40     static void setLibrary(String[] args) {
  41         if ((args.length > 0) && args[0].equalsIgnoreCase("MUSCLE")) {
  42             System.setProperty("sun.security.smartcardio.library", "/usr/local/$LIBISA/libpcsclite.so");
  43         }
  44     }
  45 
  46     static CardTerminal getTerminal(String[] args) throws Exception {
  47         setLibrary(args);
  48 
  49         TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
  50         System.out.println(factory);
  51 
  52         List<CardTerminal> terminals = factory.terminals().list();
  53         System.out.println("Terminals: " + terminals);
  54         if (terminals.isEmpty()) {
  55             throw new Exception("No card terminals available");
  56         }
  57         CardTerminal terminal = terminals.get(0);
  58 
  59         if (terminal.isCardPresent() == false) {
  60             System.out.println("*** Insert card");
  61             if (terminal.waitForCardPresent(20 * 1000) == false) {
  62                 throw new Exception("no card available");
  63             }
  64         }
  65         return terminal;
  66     }
  67 
  68     static final byte[] C1 = parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00");
  69     static final byte[] R1a = parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 25 90 00");
  70     static final byte[] R1b = parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 55 90 00");
  71 
  72     static void transmitTestCommand(CardChannel channel) throws Exception {
  73         ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
  74         byte[] rb = r.getBytes();
  75         if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
  76             System.out.println("expected: " + toString(R1a));
  77             System.out.println("received: " + toString(rb));
  78             throw new Exception("Response does not match");
  79         }
  80     }
  81 
  82     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  83 
  84     public static String toString(byte[] b) {
  85         StringBuffer sb = new StringBuffer(b.length * 3);
  86         for (int i = 0; i < b.length; i++) {
  87             int k = b[i] & 0xff;
  88             if (i != 0) {
  89                 sb.append(':');
  90             }
  91             sb.append(hexDigits[k >>> 4]);
  92             sb.append(hexDigits[k & 0xf]);
  93         }
  94         return sb.toString();
  95     }
  96 
  97     public static byte[] parse(String s) {
  98         try {
  99             int n = s.length();
 100             ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
 101             StringReader r = new StringReader(s);
 102             while (true) {
 103                 int b1 = nextNibble(r);
 104                 if (b1 < 0) {
 105                     break;
 106                 }
 107                 int b2 = nextNibble(r);
 108                 if (b2 < 0) {
 109                     throw new RuntimeException("Invalid string " + s);
 110                 }
 111                 int b = (b1 << 4) | b2;
 112                 out.write(b);
 113             }
 114             return out.toByteArray();
 115         } catch (IOException e) {
 116             throw new RuntimeException(e);
 117         }
 118     }
 119 
 120     private static int nextNibble(StringReader r) throws IOException {
 121         while (true) {
 122             int ch = r.read();
 123             if (ch == -1) {
 124                 return -1;
 125             } else if ((ch >= '0') && (ch <= '9')) {
 126                 return ch - '0';
 127             } else if ((ch >= 'a') && (ch <= 'f')) {
 128                 return ch - 'a' + 10;
 129             } else if ((ch >= 'A') && (ch <= 'F')) {
 130                 return ch - 'A' + 10;
 131             }
 132         }
 133     }
 134 
 135 }