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

@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);/* w w w  .  j a  v a2s . com*/
    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 SmartcardStatusCode deleteCredential(int pin, URI credentialId) {
    byte credID = this.getCredentialIDFromUri(pin, credentialId);
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.removeCredential, 0, 0, 5 });
    buf.put(this.pinToByteArr(pin));
    buf.put(credID);//ww w.j  a  va 2 s. c  o  m
    buf.position(0);
    try {
        System.out.println("Removing credential with uri: " + credentialId);
        this.deleteBlob(pin, credentialId);
        if (credentialId.toString().startsWith(UProveCryptoEngineUserImpl.UProveCredential)) {
            URI reloadURI = URI.create(credentialId.toString() + ReloadStorageManager.URI_POSTFIX);
            if (reloadURI.toString().contains(":") && !reloadURI.toString().contains("_")) {
                reloadURI = URI.create(reloadURI.toString().replaceAll(":", "_")); //change all ':' to '_'
            }
            this.deleteBlob(pin, reloadURI);
            System.out.println("deleted the reload blob of the credential: " + reloadURI);
        }
        this.removeCredentialUri(pin, credentialId);
        if (printInput)
            System.out.println("Input for removeCredential: " + Arrays.toString(buf.array()));
        System.out.println("Trying to remove on-board credential with ID=" + credID);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("response from RemoveCredential: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

/**
 * @param pin/*from w ww  .ja va2 s .c o  m*/
 * @param issuerID
 * @return byte array containing: groupID || genID1 || genID2 || numpres || counterID
 */
private byte[] readIssuer(int pin, int issuerID) {
    if (cachedIssuerByteArray.containsKey(issuerID)) {
        byte[] cached = cachedIssuerByteArray.get(issuerID);
        System.out.println("ReadIssuer - use cached : " + (cached == null ? null : Arrays.toString(cached)));
        return cached;
    }

    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) issuerID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readIssuer, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 5);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readIssuer: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readIssuer: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            cachedIssuerByteArray.put(issuerID, response.getData());
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    cachedIssuerByteArray.put(issuerID, null);
    return null;
}

From source file:org.usergrid.management.cassandra.ManagementServiceImpl.java

public String generateOAuthSecretKey(AuthPrincipalType type) {
    long timestamp = System.currentTimeMillis();
    ByteBuffer bytes = ByteBuffer.allocate(20);
    bytes.put(sha(timestamp + OAUTH_SECRET_SALT + UUID.randomUUID()));
    String secret = type.getBase64Prefix() + encodeBase64URLSafeString(bytes.array());
    return secret;
}

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

private void putData(byte[] data) {
    try {/*from  w w w .j av  a 2 s.c  o m*/
        ByteBuffer buf = ByteBuffer.allocate(7 + data.length);
        buf.put((byte) this.ABC4TRUSTCMD);
        buf.put(this.putData);
        buf.put(new byte[] { 0, 0, 0 });
        buf.put(this.intLengthToShortByteArr(data.length));
        buf.put(data);
        buf.position(0);
        System.out.println("Input to PutData: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from putData: " + response);
    } catch (CardException e) {
        e.printStackTrace();
    }
}

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

private SmartcardStatusCode setCounter(int counterID, int keyID, int index, int threshold, byte[] cursor,
        RSAKeyPair rootKey) {//from   www.j ava2s.  com
    if (cursor.length != 4) {
        throw new RuntimeException("Cursor should be of length 4");
    }
    byte[] data = new byte[8];
    data[0] = (byte) counterID;
    data[1] = (byte) keyID;
    data[2] = (byte) index;
    data[3] = (byte) threshold;
    System.arraycopy(cursor, 0, data, 4, 4);
    try {
        int mode = this.getMode();
        if (mode == 2) {
            System.out.println("Can only use setCounter in root mode");
            return SmartcardStatusCode.UNAUTHORIZED;
        }
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setCounter, 0, 0, 8 });
        buf.put(data);
        buf.position(0);
        System.out.println("Input for setCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setCounter: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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 a  va2 s  .c  o m

    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  ww  w .j  a v a  2 s .c  om
 * @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;
}

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

@Override
public BigInteger computeCredentialFragment(int pin, URI credentialId) {
    //fragment is equal to the public key of a credential
    if (cachedCredentialFragment.containsKey(credentialId)) {
        BigInteger cached = cachedCredentialFragment.get(credentialId);
        System.out.println("Cached getCredentialPublicKey: " + credentialId + " - " + cached);
        return cached;
    }/*from   w  w w .ja  v  a2  s .co m*/
    int credID = this.getCredentialIDFromUri(pin, credentialId);
    ByteBuffer buf = ByteBuffer.allocate(14);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getCredentialPublicKey, 0, 0, 0, 0, 5 });
    buf.put(this.pinToByteArr(pin));
    buf.put((byte) credID);
    buf.put(new byte[] { 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println(
                    "Input for getCredentialPublicKey: " + credentialId + " : " + Arrays.toString(buf.array()));

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

        System.out.println("Response from getCredentialPublicKey (fragment): " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            BigInteger credentialFragment = new BigInteger(1, response.getData());
            System.out.println("which gives this BigInteger:  " + credentialFragment);
            cachedCredentialFragment.put(credentialId, credentialFragment);
            return credentialFragment;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

private BigInteger getScopeExclusiveCommitment(int pin, URI scope) {
    byte[] uri = this.uriToByteArr(scope);
    ByteBuffer buf = ByteBuffer.allocate(13 + uri.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getScopeExclusiveCommitment, 0, 0, 0 });
    buf.put(this.intLengthToShortByteArr(4 + uri.length));
    buf.put(this.pinToByteArr(pin));
    buf.put(uri);/*from w w w  .  j  av a 2s.  com*/
    buf.put(new byte[] { 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for getScopeExclusiveCommitment: " + Arrays.toString(buf.array()));

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

        System.out.println("Response from getScopeExclusiveCommitment: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return new BigInteger(1, response.getData());
        } else {
            throw new RuntimeException("Failed scope exclusive Commitment. Card answered: " + response);
        }
    } catch (CardException e) {
        throw new RuntimeException("getScopeExclusiveCommitment crashed.", e);
    }
}