-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from fidesmo/feature-trace-logging
Add logging apdu traces
- Loading branch information
Showing
6 changed files
with
264 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 0 additions & 62 deletions
62
src/main/groovy/com/fidesmo/gradle/plugin/JnasmartcardioTransceiver.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2015 Fidesmo AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.fidesmo.gradle.plugin; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
import com.fidesmo.sec.utils.Hex; | ||
import nordpol.IsoCard; | ||
import nordpol.OnCardErrorListener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Wrap an IsoCard so that all transmitted APDUs are logged */ | ||
|
||
public class LoggingCard implements IsoCard { | ||
|
||
IsoCard underlying; | ||
Logger logger; | ||
|
||
public LoggingCard(IsoCard underlying) { | ||
this.underlying = underlying; | ||
this.logger = LoggerFactory.getLogger("transaction-trace"); | ||
} | ||
|
||
public void addOnCardErrorListener(OnCardErrorListener listener) { | ||
underlying.addOnCardErrorListener(listener); | ||
} | ||
|
||
public void removeOnCardErrorListener(OnCardErrorListener listener) { | ||
underlying.removeOnCardErrorListener(listener); | ||
} | ||
|
||
public boolean isConnected() { | ||
return underlying.isConnected(); | ||
} | ||
|
||
public void connect() throws IOException { | ||
underlying.connect(); | ||
} | ||
|
||
public void close() throws IOException { | ||
underlying.close(); | ||
} | ||
|
||
public int getTimeout() { | ||
return underlying.getTimeout(); | ||
} | ||
|
||
public void setTimeout(int t) { | ||
underlying.setTimeout(t); | ||
} | ||
|
||
public int getMaxTransceiveLength() throws IOException { | ||
return underlying.getMaxTransceiveLength(); | ||
} | ||
|
||
public byte[] transceive(byte[] command) throws IOException { | ||
logger.info("Send to card: " + Hex.encodeHex(command)); | ||
byte[] response = underlying.transceive(command); | ||
logger.info("Received from card: " + Hex.encodeHex(response)); | ||
return response; | ||
} | ||
|
||
public List<byte[]> transceive(List<byte[]> commands) throws IOException { | ||
List<byte[]> responses = new ArrayList<byte[]>(commands.size()); | ||
for (byte[] command: commands) { | ||
responses.add(transceive(command)); | ||
} | ||
return responses; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/com/fidesmo/gradle/plugin/SmartcardioCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2015 Fidesmo AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.fidesmo.gradle.plugin; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
import javax.smartcardio.Card; | ||
import javax.smartcardio.CardException; | ||
import javax.smartcardio.CommandAPDU; | ||
|
||
import nordpol.IsoCard; | ||
import nordpol.OnCardErrorListener; | ||
|
||
public class SmartcardioCard implements IsoCard { | ||
|
||
private Card card; | ||
private boolean openFlag = false; | ||
private int timeout = 15; | ||
private List<OnCardErrorListener> listeners = new ArrayList<OnCardErrorListener>(); | ||
|
||
public SmartcardioCard(Card card) { | ||
this.card = card; | ||
} | ||
|
||
public void addOnCardErrorListener(OnCardErrorListener listener) { | ||
listeners.add(listener); | ||
} | ||
|
||
public void removeOnCardErrorListener(OnCardErrorListener listener) { | ||
listeners.remove(listener); | ||
} | ||
|
||
public boolean isConnected() { | ||
return openFlag; | ||
} | ||
|
||
public void connect() { | ||
openFlag = true; | ||
} | ||
|
||
public void close() throws IOException { | ||
try { | ||
card.disconnect(false); | ||
openFlag = false; | ||
} catch (CardException e) { | ||
throw new IOException("Couldn't close card", e); | ||
} | ||
} | ||
|
||
public int getTimeout() { | ||
return timeout; | ||
} | ||
|
||
public void setTimeout(int t) { | ||
timeout = t; | ||
} | ||
|
||
public int getMaxTransceiveLength() { | ||
return 261; | ||
} | ||
|
||
public byte[] transceive(byte[] command) throws IOException { | ||
try { | ||
return card | ||
.getBasicChannel() | ||
.transmit(new CommandAPDU(command)) | ||
.getBytes(); | ||
} catch (CardException e) { | ||
throw new IOException("Couldn't close card", e); | ||
} | ||
} | ||
|
||
public List<byte[]> transceive(List<byte[]> commands) throws IOException { | ||
List<byte[]> responses = new ArrayList<byte[]>(commands.size()); | ||
for (byte[] command: commands) { | ||
responses.add(transceive(command)); | ||
} | ||
return responses; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/fidesmo/gradle/plugin/SmartcardioTransceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2015 Fidesmo AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.fidesmo.gradle.plugin; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.security.NoSuchAlgorithmException; | ||
import javax.smartcardio.TerminalFactory; | ||
import javax.smartcardio.CardTerminal; | ||
import javax.smartcardio.CardTerminals; | ||
import javax.smartcardio.CardException; | ||
|
||
import jnasmartcardio.Smartcardio; | ||
import rx.Observable; | ||
|
||
import com.fidesmo.sec.transceivers.Transceiver; | ||
import nordpol.IsoCard; | ||
|
||
|
||
/** Transceiver implementation using the javax.smartcardio package. | ||
* | ||
* This transceiver uses the jnasmartcardio package instead of the | ||
* default implementation. | ||
*/ | ||
public class SmartcardioTransceiver implements Transceiver { | ||
|
||
public Observable<IsoCard> getCard() { | ||
try { | ||
TerminalFactory factory = | ||
TerminalFactory.getInstance("PC/SC",null, new Smartcardio()); | ||
List<CardTerminal> terminalsWithCard = | ||
factory.terminals().list(CardTerminals.State.CARD_PRESENT); | ||
|
||
if (terminalsWithCard.size() == 0) { | ||
if (factory.terminals().list().size() == 0) { | ||
return Observable.error(new IOException("No terminals found")); | ||
} else { | ||
return Observable.error(new IOException("No card found")); | ||
} | ||
} | ||
|
||
return Observable | ||
.just((IsoCard) new LoggingCard(new SmartcardioCard(terminalsWithCard | ||
.get(0) | ||
.connect("*")))); | ||
} catch (NoSuchAlgorithmException e) { | ||
return Observable.error(new IOException("Error with jnassmartcardio", e)); | ||
} catch (CardException e) { | ||
return Observable.error(new IOException("Unable to get Smartcard", e)); | ||
} | ||
} | ||
} |