aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/applet/AppletUtil.java
blob: 532b44efa51b9781e73625ee41de63b4404d66ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
    }
}