Path: blob/master/test/jdk/sun/security/smartcardio/Utils.java
41149 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324// common utility functions for the PC/SC tests2526import java.io.StringReader;27import java.io.ByteArrayOutputStream;28import java.io.IOException;29import java.security.NoSuchAlgorithmException;30import java.util.Arrays;31import java.util.List;32import javax.smartcardio.CardTerminal;33import javax.smartcardio.CardChannel;34import javax.smartcardio.ResponseAPDU;35import javax.smartcardio.CommandAPDU;36import javax.smartcardio.TerminalFactory;3738public class Utils {3940static void setLibrary(String[] args) {41if ((args.length > 0) && args[0].equalsIgnoreCase("MUSCLE")) {42System.setProperty("sun.security.smartcardio.library", "/usr/local/$LIBISA/libpcsclite.so");43}44}4546static TerminalFactory getTerminalFactory(String provName) throws Exception {47try {48TerminalFactory factory = (provName == null)49? TerminalFactory.getInstance("PC/SC", null)50: TerminalFactory.getInstance("PC/SC", null, provName);51System.out.println(factory);52return factory;53} catch (NoSuchAlgorithmException e) {54Throwable cause = e.getCause();55if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {56return null;57}58throw e;59}60}6162static CardTerminal getTerminal(String[] args) throws Exception {63return getTerminal(args, null);64}6566static CardTerminal getTerminal(String[] args, String provider) throws Exception {67setLibrary(args);6869try {70TerminalFactory factory = (provider == null)71? TerminalFactory.getInstance("PC/SC", null)72: TerminalFactory.getInstance("PC/SC", null, provider);73System.out.println(factory);7475List<CardTerminal> terminals = factory.terminals().list();76System.out.println("Terminals: " + terminals);77if (terminals.isEmpty()) {78return null;79}80CardTerminal terminal = terminals.get(0);8182if (terminal.isCardPresent() == false) {83System.out.println("*** Insert card");84if (terminal.waitForCardPresent(20 * 1000) == false) {85throw new Exception("no card available");86}87}88System.out.println("card present: " + terminal.isCardPresent());8990return terminal;9192} catch (NoSuchAlgorithmException e) {93Throwable cause = e.getCause();94if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {95return null;96}97throw e;98}99}100101static final byte[] C1 = parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00");102static 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");103static 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");104105static void transmitTestCommand(CardChannel channel) throws Exception {106ResponseAPDU r = channel.transmit(new CommandAPDU(C1));107byte[] rb = r.getBytes();108if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {109System.out.println("expected: " + toString(R1a));110System.out.println("received: " + toString(rb));111throw new Exception("Response does not match");112}113}114115private final static char[] hexDigits = "0123456789abcdef".toCharArray();116117public static String toString(byte[] b) {118StringBuffer sb = new StringBuffer(b.length * 3);119for (int i = 0; i < b.length; i++) {120int k = b[i] & 0xff;121if (i != 0) {122sb.append(':');123}124sb.append(hexDigits[k >>> 4]);125sb.append(hexDigits[k & 0xf]);126}127return sb.toString();128}129130public static byte[] parse(String s) {131try {132int n = s.length();133ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);134StringReader r = new StringReader(s);135while (true) {136int b1 = nextNibble(r);137if (b1 < 0) {138break;139}140int b2 = nextNibble(r);141if (b2 < 0) {142throw new RuntimeException("Invalid string " + s);143}144int b = (b1 << 4) | b2;145out.write(b);146}147return out.toByteArray();148} catch (IOException e) {149throw new RuntimeException(e);150}151}152153private static int nextNibble(StringReader r) throws IOException {154while (true) {155int ch = r.read();156if (ch == -1) {157return -1;158} else if ((ch >= '0') && (ch <= '9')) {159return ch - '0';160} else if ((ch >= 'a') && (ch <= 'f')) {161return ch - 'a' + 10;162} else if ((ch >= 'A') && (ch <= 'F')) {163return ch - 'A' + 10;164}165}166}167168}169170171