Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

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

public byte[] getChallenge(int size) {
    //TODO: Make this work for challenge sizes of 256 (or 0)
    if ((size > 256) || (size < 1)) {
        System.err.println("Argument 'size' for getChallenge should be in the range [1,256]");
        return null;
    }//  w  ww . j  a  v  a 2  s  .c  o m
    try {
        int realSize = size;
        if (realSize == 256) {
            realSize = 0;
        }
        ByteBuffer buf = ByteBuffer.allocate(7);
        System.out.println("Le: " + (byte) size);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getChallenge, 0, 0, 1, (byte) realSize, 0 });
        buf.position(0);
        System.out.println("Input for getChallenge: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from getChallenge: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        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);
    try {/*from w w  w . j a v a 2  s .  co  m*/
        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

@Override
public RSAVerificationKey readAuthenticationKey(int pin, int keyID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) keyID;
    ByteBuffer buffer = ByteBuffer.allocate(14);
    buffer.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readAuthenticationKey, 0, 0, 0, 0, 5 });
    buffer.put(data);//from   w w  w.  j  a  v  a 2 s  .c om
    buffer.put(new byte[] { 0, 0 });
    buffer.position(0);
    try {
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buffer));
        System.out.println("Response from readAuthenticationKey: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            RSAVerificationKey vkey = new RSAVerificationKey();
            vkey.n = new BigInteger(1, response.getData());
            return vkey;
        }
        return null;
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

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);
    try {/*from w  w  w  . j av a  2 s .c  om*/
        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 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);
    try {// w ww . j  a  v a 2  s. c o  m
        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 allocateCredential(int pin, URI credentialId, URI issuerParameters) {
    byte[] credIdBytes = null;
    credIdBytes = this.uriToByteArr(credentialId);
    if (credIdBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }/*ww  w .java  2s .co m*/

    byte issuerID = this.getIssuerIDFromUri(pin, issuerParameters);
    byte newCredentialID = this.getNewCredentialID(pin);
    if (newCredentialID == (byte) -1) {
        return SmartcardStatusCode.INSUFFICIENT_STORAGE;
    }
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setCredential, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(newCredentialID);
    buf.put(issuerID);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for setCredential: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setCredential: " + response);
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return this.evaluateStatus(response);
        }
    } catch (CardException e) {
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }

    //Then store the mapping from credentialURI to credentialID:
    TimingsLogger.logTiming("HardwareSmartcard.storeCredentialUriAndID", true);
    SmartcardStatusCode code = this.storeCredentialUriAndID(pin, credentialId, newCredentialID);
    TimingsLogger.logTiming("HardwareSmartcard.storeCredentialUriAndID", false);
    if (code != SmartcardStatusCode.OK) {
        System.err.println(
                "Credential stored correctly on card, but storing the Uri/ID failed with code: " + code);
        return code;
    }

    return SmartcardStatusCode.OK;
}

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

private BigInteger getGenerator(int pin, int groupID, int genID) {
    if (cachedGenerator.containsKey(groupID + ":" + genID)) {
        BigInteger cached = cachedGenerator.get(groupID + ":" + genID);
        System.out.println("Cached readGenerator: " + groupID + " : " + genID + " : " + cached);
        return cached;
    }//from  w w  w. j av a 2  s .  com

    ByteBuffer buf = ByteBuffer.allocate(15);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readGenerator, 0, 0, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { (byte) groupID, (byte) genID, 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readGenerator: " + groupID + " : " + genID + " : "
                    + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGenerator)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGenerator)", false);

        System.out.println("Response from readGenerator: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            BigInteger generator = new BigInteger(1, response.getData());
            System.out.println("Generator - is : " + groupID + " : " + genID + " : " + generator);
            cachedGenerator.put(groupID + ":" + genID, generator);
            return generator;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * //from   w  w  w .  ja v a2 s  .com
 * @param pin
 * @param groupID
 * @param compType 0: modulus, 1: group order 2: cofactor
 * @return
 */
private BigInteger getGroupComponent(int pin, int groupID, int compType) {
    if (cachedGroupComponent.containsKey(groupID + ":" + compType)) {
        BigInteger cached = cachedGroupComponent.get(groupID + ":" + compType);
        System.out.println("Cached readGroupComponent: " + groupID + " : " + compType + " : " + cached);
        return cached;
    }
    ByteBuffer buf = ByteBuffer.allocate(15);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readGroupComponent, 0, 0, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { (byte) groupID, (byte) compType, 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readGroupComponent: " + groupID + " : " + compType + " : "
                    + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGroupComponent)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGroupComponent)", false);

        System.out.println("Response from readGroupComponent: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            BigInteger groupComponent = new BigInteger(1, response.getData());
            System.out.println("GroupComponent - is : " + groupID + " : " + compType + " : " + groupComponent);

            cachedGroupComponent.put(groupID + ":" + compType, groupComponent);
            return groupComponent;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}