Search in sources :

Example 6 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.  position and limit
     * point to the end of the decrypted/depadded data.  The initial
     * limit and new limit may be different, given we may
     * have stripped off some padding bytes.
     *
     *  @see decrypt(byte[], int, int)
     */
int decrypt(ByteBuffer bb, int tagLen) throws BadPaddingException {
    int len = bb.remaining();
    if (cipher == null) {
        bb.position(bb.limit());
        return len;
    }
    try {
        /*
             * Decrypt "in-place".
             */
        int pos = bb.position();
        ByteBuffer dup = bb.duplicate();
        int newLen;
        if (cipherType == AEAD_CIPHER) {
            try {
                newLen = cipher.doFinal(dup, bb);
            } catch (IllegalBlockSizeException ibse) {
                // unlikely to happen
                throw new RuntimeException("Cipher error in AEAD mode \"" + ibse.getMessage() + " \"in JCE provider " + cipher.getProvider().getName());
            }
        } else {
            newLen = cipher.update(dup, bb);
            if (newLen != len) {
                // catch BouncyCastle buffering error
                throw new RuntimeException("Cipher buffering error " + "in JCE provider " + cipher.getProvider().getName());
            }
        }
        // reset the limit to the end of the decryted data
        bb.limit(pos + newLen);
        if (debug != null && Debug.isOn("plaintext")) {
            try {
                HexDumpEncoder hd = new HexDumpEncoder();
                System.out.println("Padded plaintext after DECRYPTION:  len = " + newLen);
                hd.encodeBuffer(bb.duplicate().position(pos), System.out);
            } catch (IOException e) {
            }
        }
        /*
             * Remove the block padding.
             */
        if (cipherType == BLOCK_CIPHER) {
            int blockSize = cipher.getBlockSize();
            bb.position(pos);
            newLen = removePadding(bb, tagLen, blockSize, protocolVersion);
            // check the explicit IV of TLS v1.1 or later
            if (protocolVersion.useTLS11PlusSpec()) {
                if (newLen < blockSize) {
                    throw new BadPaddingException("The length after " + "padding removal (" + newLen + ") should be larger " + "than <" + blockSize + "> since explicit IV used");
                }
                // reset the position to the end of the decrypted data
                bb.position(bb.limit());
            }
        }
        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) IOException(java.io.IOException)

Example 7 with HexDumpEncoder

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

the class CipherBox method encrypt.

/*
     * Encrypts a block of data, returning the size of the
     * resulting block.
     */
int encrypt(byte[] buf, int offset, int len) {
    if (cipher == null) {
        return len;
    }
    try {
        int blockSize = cipher.getBlockSize();
        if (cipherType == BLOCK_CIPHER) {
            len = addPadding(buf, offset, len, blockSize);
        }
        if (debug != null && Debug.isOn("plaintext")) {
            try {
                HexDumpEncoder hd = new HexDumpEncoder();
                System.out.println("Padded plaintext before ENCRYPTION:  len = " + len);
                hd.encodeBuffer(new ByteArrayInputStream(buf, offset, len), System.out);
            } catch (IOException e) {
            }
        }
        if (cipherType == AEAD_CIPHER) {
            try {
                return cipher.doFinal(buf, offset, len, buf, offset);
            } catch (IllegalBlockSizeException | BadPaddingException ibe) {
                // unlikely to happen
                throw new RuntimeException("Cipher error in AEAD mode in JCE provider " + cipher.getProvider().getName(), ibe);
            }
        } else {
            int 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());
            }
            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)

Example 8 with HexDumpEncoder

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

the class SignerInfo method toString.

public String toString() {
    HexDumpEncoder hexDump = new HexDumpEncoder();
    String out = "";
    out += "Signer Info for (issuer): " + issuerName + "\n";
    out += "\tversion: " + Debug.toHexString(version) + "\n";
    out += "\tcertificateSerialNumber: " + Debug.toHexString(certificateSerialNumber) + "\n";
    out += "\tdigestAlgorithmId: " + digestAlgorithmId + "\n";
    if (authenticatedAttributes != null) {
        out += "\tauthenticatedAttributes: " + authenticatedAttributes + "\n";
    }
    out += "\tdigestEncryptionAlgorithmId: " + digestEncryptionAlgorithmId + "\n";
    out += "\tencryptedDigest: " + "\n" + hexDump.encodeBuffer(encryptedDigest) + "\n";
    if (unauthenticatedAttributes != null) {
        out += "\tunauthenticatedAttributes: " + unauthenticatedAttributes + "\n";
    }
    return out;
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 9 with HexDumpEncoder

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

the class PKCS9Attribute method toString.

/**
 * Returns a string representation of this attribute.
 */
public String toString() {
    StringBuilder sb = new StringBuilder(100);
    sb.append("[");
    if (index == -1) {
        sb.append(oid.toString());
    } else {
        sb.append(OID_NAME_TABLE.get(PKCS9_OIDS[index]));
    }
    sb.append(": ");
    if (index == -1 || SINGLE_VALUED[index]) {
        if (value instanceof byte[]) {
            // special case for octet string
            HexDumpEncoder hexDump = new HexDumpEncoder();
            sb.append(hexDump.encodeBuffer((byte[]) value));
        } else {
            sb.append(value.toString());
        }
        sb.append("]");
        return sb.toString();
    } else {
        // multi-valued
        boolean first = true;
        Object[] values = (Object[]) value;
        for (int j = 0; j < values.length; j++) {
            if (first)
                first = false;
            else
                sb.append(", ");
            sb.append(values[j].toString());
        }
        return sb.toString();
    }
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 10 with HexDumpEncoder

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

the class X509CRLImpl method toStringWithAlgName.

// Specifically created for keytool to append a (weak) label to sigAlg
public String toStringWithAlgName(String name) {
    StringBuilder sb = new StringBuilder();
    sb.append("X.509 CRL v").append(version + 1).append('\n');
    if (sigAlgId != null)
        sb.append("Signature Algorithm: ").append(name).append(", OID=").append(sigAlgId.getOID()).append('\n');
    if (issuer != null)
        sb.append("Issuer: ").append(issuer).append('\n');
    if (thisUpdate != null)
        sb.append("\nThis Update: ").append(thisUpdate).append('\n');
    if (nextUpdate != null)
        sb.append("Next Update: ").append(nextUpdate).append('\n');
    if (revokedList.isEmpty())
        sb.append("\nNO certificates have been revoked\n");
    else {
        sb.append("\nRevoked Certificates: ").append(revokedList.size());
        int i = 1;
        for (X509CRLEntry entry : revokedList) {
            sb.append("\n[").append(i++).append("] ").append(entry);
        }
    }
    if (extensions != null) {
        Collection<Extension> allExts = extensions.getAllExtensions();
        Object[] objs = allExts.toArray();
        sb.append("\nCRL Extensions: ").append(objs.length);
        for (int i = 0; i < objs.length; i++) {
            sb.append("\n[").append(i + 1).append("]: ");
            Extension ext = (Extension) objs[i];
            try {
                if (OIDMap.getClass(ext.getExtensionId()) == null) {
                    sb.append(ext);
                    byte[] extValue = ext.getExtensionValue();
                    if (extValue != null) {
                        DerOutputStream out = new DerOutputStream();
                        out.putOctetString(extValue);
                        extValue = out.toByteArray();
                        HexDumpEncoder enc = new HexDumpEncoder();
                        sb.append("Extension unknown: ").append("DER encoded OCTET string =\n").append(enc.encodeBuffer(extValue)).append('\n');
                    }
                } else {
                    // sub-class exists
                    sb.append(ext);
                }
            } catch (Exception e) {
                sb.append(", Error parsing this extension");
            }
        }
    }
    if (signature != null) {
        HexDumpEncoder encoder = new HexDumpEncoder();
        sb.append("\nSignature:\n").append(encoder.encodeBuffer(signature)).append('\n');
    } else {
        sb.append("NOT signed yet\n");
    }
    return sb.toString();
}
Also used : X509CRLEntry(java.security.cert.X509CRLEntry) HexDumpEncoder(sun.security.util.HexDumpEncoder) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CRLException(java.security.cert.CRLException) NoSuchProviderException(java.security.NoSuchProviderException)

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