Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

In this page you can find the example usage for java.nio ByteBuffer put.

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private SmartcardStatusCode setGenerator(int mode, byte[] g, int groupID, int genID, RSAKeyPair rootKey)
        throws CardException {
    g = this.removeSignBit(g);

    byte[] data = new byte[2];
    data[0] = (byte) groupID;
    data[1] = (byte) genID;
    if (mode == 1) {
        this.putData(g);
    } else {/*ww w. j  a v  a 2  s .  com*/
        System.out.println("Can only use setGenerator in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }
    ByteBuffer buf = ByteBuffer.allocate(7);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setGenerator, 0, 0, 2, (byte) groupID, (byte) genID });
    buf.position(0);
    System.out.println("Input for set Generator: " + Arrays.toString(buf.array()));
    ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
    System.out.println("Response from setGenerator: " + response);
    return this.evaluateStatus(response);
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public BigInteger computeDevicePublicKey(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(13);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDevicePublicKey, 0, 0, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0, 0 });
    buf.position(0);/*from ww w.j a va2 s  .  com*/
    try {
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDevicePublicKey)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDevicePublicKey)", false);
        System.out.println("Response from getDevicePublicKey: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return new BigInteger(1, response.getData());
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private BigInteger computeDevicePublicKeyResponse(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(13);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDeviceResponse, 0, 0, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0, 0 });
    buf.position(0);/*ww w . ja  va2s  .  c  o  m*/
    try {
        if (printInput)
            System.out.println("Input for getDeviceResponse: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from getDeviceResponse: " + response);
        System.out.println("And this is the output: " + Arrays.toString(response.getData()));
        System.out.println("which gives this BigInteger: " + new BigInteger(1, response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return new BigInteger(1, response.getData());
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * //from  w w w .j a  v  a2s.c om
 * @param mode
 * @param component
 * @param groupID 
 * @param compType (0: mod, 1: order, 2: co-factor)
 * @param rootKey
 * @return
 * @throws CardException
 */
private SmartcardStatusCode setGroupComponent(int mode, byte[] component, int groupID, int compType,
        RSAKeyPair rootKey) throws CardException {
    component = removeSignBit(component);

    byte[] data = new byte[2];
    data[0] = (byte) groupID;
    data[1] = (byte) compType;
    if (mode == 1) {
        this.putData(component);
    } else {
        System.out.println("Can only use setGroupComponent in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }
    ByteBuffer buf = ByteBuffer.allocate(7);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setGroupComponent, 0, 0, 2, (byte) groupID,
            (byte) compType });
    buf.position(0);
    System.out.println("Input for set Group Component: " + Arrays.toString(buf.array()));
    ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
    System.out.println("Response from setGroupComponent: " + response);
    return this.evaluateStatus(response);
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private BigInteger computeDevicePublicKeyCommitment(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(13);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDeviceCommitment, 0, 0, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0, 0 });
    buf.position(0);/*from   w w  w  . ja v  a 2  s. c o  m*/
    try {
        if (printInput)
            System.out.println("Input for getDeviceCommitment: " + Arrays.toString(buf.array()));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDeviceCommitment)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDeviceCommitment)", false);
        System.out.println("Response from getDeviceCommitment: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            System.out.println("Or this bigInt: " + new BigInteger(1, response.getData()));
            return new BigInteger(1, response.getData());
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public SmartcardStatusCode changePin(int pin, int newPin) {
    byte[] data = new byte[8];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    System.arraycopy(this.pinToByteArr(newPin), 0, data, 4, 4);
    try {/*w w  w. j a  v  a 2  s . c om*/
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.changePin, 0, 0, 8 });
        buf.put(data);
        buf.position(0);
        if (printInput)
            System.out.println("Input for changePin: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from changePin: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
  * /*w w w. j a  v a2s  .  co m*/
  * @param pin
  * @param credentialID
  * @return byte array containing: issuerID || size(v) [2 bytes] || size(kv) [2 bytes] || status || prescount
  */
private byte[] readCredential(int pin, int credentialID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) credentialID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCredential, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCredential: " + Arrays.toString(buf.array()));
        System.out.println("Reading the on-board credential with ID=" + credentialID);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCredential: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@SuppressWarnings("unused")
private List<Byte> listCounters(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCounters, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0 });
    buf.position(0);//  w  ww. j  a  v  a 2 s .c  o m
    try {
        if (printInput)
            System.out.println("Input for listCounters: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from listCounters: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            List<Byte> counters = new ArrayList<Byte>();
            byte[] counterIDs = response.getData();
            for (byte counterID : counterIDs) {
                counters.add(counterID);
            }
            return counters;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public List<Byte> listCredentialIDs(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCredentials, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put((byte) 0);
    buf.position(0);//from  w  w w .j  av  a  2s .  co  m
    try {
        if (printInput)
            System.out.println("Input for listCredentials: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from listCredentials: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            List<Byte> credentialIDs = new ArrayList<Byte>();
            byte[] creds = response.getData();
            for (byte cred : creds) {
                credentialIDs.add(cred);
            }
            return credentialIDs;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * //ww  w .j  a v  a2s . c o  m
 * @param pin
 * @param counterID
 * @return the 7-byte stream keyID || index || threshold || cursor(4 bytes).
 */
private byte[] readCounter(int pin, int counterID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) counterID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCounter, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCounter: " + response);
        System.out.println("With data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}