Skip to content

Commit

Permalink
Merge pull request #26 from fidesmo/feature-send-apdu
Browse files Browse the repository at this point in the history
Add basic console implementation using jline
  • Loading branch information
slomo committed Nov 6, 2015
2 parents 5b1d301 + b70469b commit 3581944
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Features
* upload executable load file to the Fidesmo server [^1]
* install applet to Fidesmo card [^1]
* delete applet from Fidesmo card [^1]
* send and receives APDUs

[^1]: These features interact with the fidesmo server, hence a working internet connection and a
[fidesmo developer account](https://developer.fidesmo.com)(free of charge) is required.
Expand Down Expand Up @@ -76,6 +77,22 @@ command:
This will take the first defined applet and create an instance of on the card with the same aid as
the applet.

Console
-------

To start up the APDU console one must make sure to disabled the gradle
daemon and silence all other output from gradle. This can be done
using:

./gradlew --no-daemon -q console

Commands are sent using the send command, e.g.

send 00A404000BA00000061702000200000100

To show the a list of all commands use the `help` command. To exit the
console use the `exit` command

Assign a specific AID to the applet instance
--------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ repositories {
dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.fidesmo:gradle-javacard:0.2.4'
compile 'com.fidesmo:gradle-javacard:0.2.6'
compile 'com.fidesmo:sec-client-core:0.1.26'
compile 'io.github.jnasmartcardio:jnasmartcardio:0.2.4'
compile 'jline:jline:2.13'
runtime 'ch.qos.logback:logback-classic:1.1.3'
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
mavenDeploy 'org.kuali.maven.wagons:maven-s3-wagon:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.InvalidUserDataException
import org.gradle.api.GradleException
import org.gradle.api.tasks.JavaExec

import com.fidesmo.gradle.javacard.JavacardPlugin
import com.fidesmo.gradle.javacard.JavacardExtension
Expand Down Expand Up @@ -112,5 +113,9 @@ class FidesmoPlugin implements Plugin<Project> {

project.tasks.create('installToLocalCard', OperationTask, installToLocalCard(true))
project.tasks.create('installToLocalCardUnencrypted', OperationTask, installToLocalCard(false))

project.tasks.create('console') << {
Console.run();
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/fidesmo/gradle/plugin/Console.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.fidesmo.gradle.plugin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;

import jline.Terminal;
import jline.TerminalFactory;
import jline.console.ConsoleReader;
import jline.console.UserInterruptException;
import com.fidesmo.sec.utils.Hex;
import nordpol.IsoCard;

/** Simple command line to communicate with card.
*
*/
public class Console {

public static void run() throws IOException {
Terminal terminal = TerminalFactory.create();
terminal.setEchoEnabled(true);
ConsoleReader reader = new ConsoleReader("Fidesmo console",
System.in,
System.out,
terminal);
reader.setHandleUserInterrupt(true);
PrintWriter out = new PrintWriter(reader.getOutput());
IsoCard card = (new SmartcardioTransceiver(out))
.getCard()
.toBlocking()
.first();

String line;
try {
while((line = reader.readLine("> ")) != null) {
String[] tokens = line.split(" ");
if (tokens[0].equals("send")) {
if (tokens.length < 2) {
out.println("To few arguments for send command");
}

byte[] apdu = Hex.decodeHex(tokens[1].toUpperCase());
card.transceive(apdu); // output displayed through log
} else if (line.equals("exit")) {
card.close();
break;
} else if (line.equals("help")) {
out.println("send <hex string> -- send apdu to card");
out.println("help -- display this message");
out.println("exit -- exit this shell");
} else {
out.println("Unknown command. Type 'exit' to leave.");
}
}
} catch(UserInterruptException uie) {
try {
card.close();
} catch(IOException ioe) {
/* Ignore */
}
}
}
}
23 changes: 20 additions & 3 deletions src/main/java/com/fidesmo/gradle/plugin/LoggingCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.fidesmo.gradle.plugin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;

Expand All @@ -33,11 +34,27 @@
public class LoggingCard implements IsoCard {

IsoCard underlying;
PrintWriter writer;
Logger logger;

public LoggingCard(IsoCard underlying) {
this.underlying = underlying;
this.logger = LoggerFactory.getLogger("transaction-trace");
this.logger = LoggerFactory.getLogger(LoggingCard.class);
this.writer = null;
}

public LoggingCard(IsoCard underlying, PrintWriter writer) {
this.underlying = underlying;
this.logger = LoggerFactory.getLogger(LoggingCard.class);
this.writer = writer;
}

private void log(String msg) {
if(writer != null) {
writer.println(msg);
} else {
logger.info(msg);
}
}

public void addOnCardErrorListener(OnCardErrorListener listener) {
Expand Down Expand Up @@ -73,9 +90,9 @@ public int getMaxTransceiveLength() throws IOException {
}

public byte[] transceive(byte[] command) throws IOException {
logger.info("Send to card: " + Hex.encodeHex(command));
log("==> ApduCommand(" + Hex.encodeHex(command) + ")");
byte[] response = underlying.transceive(command);
logger.info("Received from card: " + Hex.encodeHex(response));
log("<== ApduResponse(" + Hex.encodeHex(response) + ")");
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.fidesmo.gradle.plugin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.security.NoSuchAlgorithmException;
import javax.smartcardio.TerminalFactory;
Expand All @@ -38,6 +39,15 @@
* default implementation.
*/
public class SmartcardioTransceiver implements Transceiver {
private PrintWriter writer;

public SmartcardioTransceiver() {
this.writer = null;
}

public SmartcardioTransceiver(PrintWriter writer) {
this.writer = writer;
}

public Observable<IsoCard> getCard() {
try {
Expand All @@ -57,7 +67,7 @@ public Observable<IsoCard> getCard() {
return Observable
.just((IsoCard) new LoggingCard(new SmartcardioCard(terminalsWithCard
.get(0)
.connect("*"))));
.connect("*")), writer));
} catch (NoSuchAlgorithmException e) {
return Observable.error(new IOException("Error with jnassmartcardio", e));
} catch (CardException e) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDOUT-COMPACT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<logger name="com.fidesmo.gradle.plugin.LoggingCard" level="INFO" additivity="false">
<appender-ref ref="STDOUT-COMPACT" />
</logger>

<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 3581944

Please sign in to comment.