diff options
| author | petrs | 2017-06-08 14:56:23 +0200 |
|---|---|---|
| committer | GitHub | 2017-06-08 14:56:23 +0200 |
| commit | 359b4bb5be1a822e389e54b9697504f4f0b43d34 (patch) | |
| tree | 771dac996ecab9e676a0b1b3c6d14692c5dad8a9 /src/cz/crcs/ectester/applet/AppletUtil.java | |
| parent | 7cf4a9d7272c1abd97a160c92ec1534d5ff82373 (diff) | |
| parent | 3f1483635615a0c599ae2e75858be4e39eb08a64 (diff) | |
| download | ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.tar.gz ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.tar.zst ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.zip | |
Merge pull request #1 from petrs/devel
Merge current dev version
Diffstat (limited to 'src/cz/crcs/ectester/applet/AppletUtil.java')
| -rw-r--r-- | src/cz/crcs/ectester/applet/AppletUtil.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/applet/AppletUtil.java b/src/cz/crcs/ectester/applet/AppletUtil.java new file mode 100644 index 0000000..532b44e --- /dev/null +++ b/src/cz/crcs/ectester/applet/AppletUtil.java @@ -0,0 +1,56 @@ +package cz.crcs.ectester.applet; + +import javacard.framework.APDU; +import javacard.framework.ISO7816; +import javacard.framework.ISOException; +import javacard.framework.Util; +import javacard.security.KeyAgreement; +import javacard.security.KeyPair; +import javacard.security.Signature; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class AppletUtil { + + private static short nullCheck(Object obj, short sw) { + if (obj == null) + ISOException.throwIt(sw); + return ISO7816.SW_NO_ERROR; + } + + public static short objCheck(Object obj) { + return nullCheck(obj, ECTesterApplet.SW_OBJECT_NULL); + } + + public static short keypairCheck(KeyPair keyPair) { + return nullCheck(keyPair, ECTesterApplet.SW_KEYPAIR_NULL); + } + + public static short kaCheck(KeyAgreement keyAgreement) { + return nullCheck(keyAgreement, ECTesterApplet.SW_KA_NULL); + } + + public static short signCheck(Signature signature) { + return nullCheck(signature, ECTesterApplet.SW_SIGNATURE_NULL); + } + + public static short readAPDU(APDU apdu, byte[] buffer, short length) { + short read = apdu.setIncomingAndReceive(); + read += apdu.getOffsetCdata(); + short total = apdu.getIncomingLength(); + if (total > length) { + return 0; + } + byte[] apduBuffer = apdu.getBuffer(); + + short sum = 0; + + do { + Util.arrayCopyNonAtomic(apduBuffer, (short) 0, buffer, sum, read); + sum += read; + read = apdu.receiveBytes((short) 0); + } while (sum < total); + return 0; + } +} |
