Search in sources :

Example 1 with HexDumpEncoder

use of sun.security.util.HexDumpEncoder in project Bytecoder by mirkosertic.

the class GCMParameters method engineToString.

/*
     * Returns a formatted string describing the parameters.
     */
protected String engineToString() {
    String LINE_SEP = System.getProperty("line.separator");
    HexDumpEncoder encoder = new HexDumpEncoder();
    StringBuilder sb = new StringBuilder(LINE_SEP + "    iv:" + LINE_SEP + "[" + encoder.encodeBuffer(iv) + "]");
    sb.append(LINE_SEP + "tLen(bits):" + LINE_SEP + tLen * 8 + LINE_SEP);
    return sb.toString();
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 2 with HexDumpEncoder

use of sun.security.util.HexDumpEncoder in project Bytecoder by mirkosertic.

the class PBEParameters method engineToString.

/*
     * Returns a formatted string describing the parameters.
     */
protected String engineToString() {
    String LINE_SEP = System.getProperty("line.separator");
    String saltString = LINE_SEP + "    salt:" + LINE_SEP + "[";
    HexDumpEncoder encoder = new HexDumpEncoder();
    saltString += encoder.encodeBuffer(salt);
    saltString += "]";
    return saltString + LINE_SEP + "    iterationCount:" + LINE_SEP + Debug.toHexString(BigInteger.valueOf(iCount)) + LINE_SEP;
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 3 with HexDumpEncoder

use of sun.security.util.HexDumpEncoder in project Bytecoder by mirkosertic.

the class Debug method printHex.

static void printHex(String prefix, ByteBuffer bb) {
    HexDumpEncoder dump = new HexDumpEncoder();
    synchronized (System.out) {
        System.out.println(prefix);
        try {
            dump.encodeBuffer(bb.slice(), System.out);
        } catch (Exception e) {
        // ignore
        }
        System.out.flush();
    }
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 4 with HexDumpEncoder

use of sun.security.util.HexDumpEncoder in project Bytecoder by mirkosertic.

the class CipherBox method encrypt.

/*
     * Encrypts a ByteBuffer block of data, returning the size of the
     * resulting block.
     *
     * The byte buffers position and limit initially define the amount
     * to encrypt.  On return, the position and limit are
     * set to last position padded/encrypted.  The limit may have changed
     * because of the added padding bytes.
     */
int encrypt(ByteBuffer bb, int outLimit) {
    int len = bb.remaining();
    if (cipher == null) {
        bb.position(bb.limit());
        return len;
    }
    int pos = bb.position();
    int blockSize = cipher.getBlockSize();
    if (cipherType == BLOCK_CIPHER) {
        // addPadding adjusts pos/limit
        len = addPadding(bb, blockSize);
        bb.position(pos);
    }
    if (debug != null && Debug.isOn("plaintext")) {
        try {
            HexDumpEncoder hd = new HexDumpEncoder();
            System.out.println("Padded plaintext before ENCRYPTION:  len = " + len);
            hd.encodeBuffer(bb.duplicate(), System.out);
        } catch (IOException e) {
        }
    }
    /*
         * Encrypt "in-place".  This does not add its own padding.
         */
    ByteBuffer dup = bb.duplicate();
    if (cipherType == AEAD_CIPHER) {
        try {
            int outputSize = cipher.getOutputSize(dup.remaining());
            if (outputSize > bb.remaining()) {
                // reserved space for the authentication tag.
                if (outLimit < pos + outputSize) {
                    // unlikely to happen
                    throw new ShortBufferException("need more space in output buffer");
                }
                bb.limit(pos + outputSize);
            }
            int newLen = cipher.doFinal(dup, bb);
            if (newLen != outputSize) {
                throw new RuntimeException("Cipher buffering error in JCE provider " + cipher.getProvider().getName());
            }
            return newLen;
        } catch (IllegalBlockSizeException | BadPaddingException | ShortBufferException ibse) {
            // unlikely to happen
            throw new RuntimeException("Cipher error in AEAD mode in JCE provider " + cipher.getProvider().getName(), ibse);
        }
    } else {
        int newLen;
        try {
            newLen = cipher.update(dup, bb);
        } catch (ShortBufferException sbe) {
            // unlikely to happen
            throw new RuntimeException("Cipher buffering error " + "in JCE provider " + cipher.getProvider().getName());
        }
        if (bb.position() != dup.position()) {
            throw new RuntimeException("bytebuffer padding error");
        }
        if (newLen != len) {
            // catch BouncyCastle buffering error
            throw new RuntimeException("Cipher buffering error " + "in JCE provider " + cipher.getProvider().getName());
        }
        return newLen;
    }
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder) IOException(java.io.IOException)

Example 5 with HexDumpEncoder

use of sun.security.util.HexDumpEncoder in project Bytecoder by mirkosertic.

the class CipherBox method decrypt.

/*
     * Decrypts a block of data, returning the size of the
     * resulting block if padding was required.
     *
     * For SSLv3 and TLSv1.0, with block ciphers in CBC mode the
     * Initialization Vector (IV) for the first record is generated by
     * the handshake protocol, the IV for subsequent records is the
     * last ciphertext block from the previous record.
     *
     * From TLSv1.1, the implicit IV is replaced with an explicit IV to
     * protect against CBC attacks.
     *
     * Differentiating between bad_record_mac and decryption_failed alerts
     * may permit certain attacks against CBC mode. It is preferable to
     * uniformly use the bad_record_mac alert to hide the specific type of
     * the error.
     */
int decrypt(byte[] buf, int offset, int len, int tagLen) throws BadPaddingException {
    if (cipher == null) {
        return len;
    }
    try {
        int newLen;
        if (cipherType == AEAD_CIPHER) {
            try {
                newLen = cipher.doFinal(buf, offset, len, buf, offset);
            } catch (IllegalBlockSizeException ibse) {
                // unlikely to happen
                throw new RuntimeException("Cipher error in AEAD mode in JCE provider " + cipher.getProvider().getName(), ibse);
            }
        } else {
            newLen = cipher.update(buf, offset, len, buf, offset);
            if (newLen != len) {
                // catch BouncyCastle buffering error
                throw new RuntimeException("Cipher buffering error " + "in JCE provider " + cipher.getProvider().getName());
            }
        }
        if (debug != null && Debug.isOn("plaintext")) {
            try {
                HexDumpEncoder hd = new HexDumpEncoder();
                System.out.println("Padded plaintext after DECRYPTION:  len = " + newLen);
                hd.encodeBuffer(new ByteArrayInputStream(buf, offset, newLen), System.out);
            } catch (IOException e) {
            }
        }
        if (cipherType == BLOCK_CIPHER) {
            int blockSize = cipher.getBlockSize();
            newLen = removePadding(buf, offset, newLen, tagLen, blockSize, protocolVersion);
            if (protocolVersion.useTLS11PlusSpec()) {
                if (newLen < blockSize) {
                    throw new BadPaddingException("The length after " + "padding removal (" + newLen + ") should be larger " + "than <" + blockSize + "> since explicit IV used");
                }
            }
        }
        return newLen;
    } catch (ShortBufferException e) {
        // unlikely to happen, we should have enough buffer space here
        throw new ArrayIndexOutOfBoundsException(e.toString());
    }
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Aggregations

HexDumpEncoder (sun.security.util.HexDumpEncoder)27 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CRLException (java.security.cert.CRLException)2 PRF (sun.security.ssl.CipherSuite.PRF)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 CertificateException (java.security.cert.CertificateException)1 Extension (java.security.cert.Extension)1 X509CRLEntry (java.security.cert.X509CRLEntry)1 ArrayList (java.util.ArrayList)1