Search in sources :

Example 21 with HexDumpEncoder

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

the class X509CertInfo method toString.

/**
 * Returns a printable representation of the certificate.
 */
public String toString() {
    if (subject == null || pubKey == null || interval == null || issuer == null || algId == null || serialNum == null) {
        throw new NullPointerException("X.509 cert is incomplete");
    }
    StringBuilder sb = new StringBuilder();
    sb.append("[\n").append("  ").append(version).append('\n').append("  Subject: ").append(subject).append('\n').append("  Signature Algorithm: ").append(algId).append('\n').append("  Key:  ").append(pubKey).append('\n').append("  ").append(interval).append('\n').append("  Issuer: ").append(issuer).append('\n').append("  ").append(serialNum).append('\n');
    // optional v2, v3 extras
    if (issuerUniqueId != null) {
        sb.append("  Issuer Id:\n").append(issuerUniqueId).append('\n');
    }
    if (subjectUniqueId != null) {
        sb.append("  Subject Id:\n").append(subjectUniqueId).append('\n');
    }
    if (extensions != null) {
        Collection<Extension> allExts = extensions.getAllExtensions();
        Extension[] exts = allExts.toArray(new Extension[0]);
        sb.append("\nCertificate Extensions: ").append(exts.length);
        for (int i = 0; i < exts.length; i++) {
            sb.append("\n[").append(i + 1).append("]: ");
            Extension ext = exts[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");
            }
        }
        Map<String, Extension> invalid = extensions.getUnparseableExtensions();
        if (invalid.isEmpty() == false) {
            sb.append("\nUnparseable certificate extensions: ").append(invalid.size());
            int i = 1;
            for (Extension ext : invalid.values()) {
                sb.append("\n[").append(i++).append("]: ").append(ext);
            }
        }
    }
    sb.append("\n]");
    return sb.toString();
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder) IOException(java.io.IOException)

Example 22 with HexDumpEncoder

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

the class UnparseableExtension method parseExtension.

// Parse the encoded extension
private void parseExtension(Extension ext) throws IOException {
    try {
        Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
        if (extClass == null) {
            // Unsupported extension
            if (ext.isCritical()) {
                unsupportedCritExt = true;
            }
            if (map.put(ext.getExtensionId().toString(), ext) == null) {
                return;
            } else {
                throw new IOException("Duplicate extensions not allowed");
            }
        }
        Constructor<?> cons = extClass.getConstructor(PARAMS);
        Object[] passed = new Object[] { Boolean.valueOf(ext.isCritical()), ext.getExtensionValue() };
        CertAttrSet<?> certExt = (CertAttrSet<?>) cons.newInstance(passed);
        if (map.put(certExt.getName(), (Extension) certExt) != null) {
            throw new IOException("Duplicate extensions not allowed");
        }
    } catch (InvocationTargetException invk) {
        Throwable e = invk.getTargetException();
        if (ext.isCritical() == false) {
            // ignore errors parsing non-critical extensions
            if (unparseableExtensions == null) {
                unparseableExtensions = new TreeMap<String, Extension>();
            }
            unparseableExtensions.put(ext.getExtensionId().toString(), new UnparseableExtension(ext, e));
            if (debug != null) {
                debug.println("Error parsing extension: " + ext);
                e.printStackTrace();
                HexDumpEncoder h = new HexDumpEncoder();
                System.err.println(h.encodeBuffer(ext.getExtensionValue()));
            }
            return;
        }
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw new IOException(e);
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 23 with HexDumpEncoder

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

the class OCSPRequest method encodeBytes.

byte[] encodeBytes() throws IOException {
    // encode tbsRequest
    DerOutputStream tmp = new DerOutputStream();
    DerOutputStream requestsOut = new DerOutputStream();
    for (CertId certId : certIds) {
        DerOutputStream certIdOut = new DerOutputStream();
        certId.encode(certIdOut);
        requestsOut.write(DerValue.tag_Sequence, certIdOut);
    }
    tmp.write(DerValue.tag_Sequence, requestsOut);
    if (!extensions.isEmpty()) {
        DerOutputStream extOut = new DerOutputStream();
        for (Extension ext : extensions) {
            ext.encode(extOut);
            if (ext.getId().equals(PKIXExtensions.OCSPNonce_Id.toString())) {
                nonce = ext.getValue();
            }
        }
        DerOutputStream extsOut = new DerOutputStream();
        extsOut.write(DerValue.tag_Sequence, extOut);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2), extsOut);
    }
    DerOutputStream tbsRequest = new DerOutputStream();
    tbsRequest.write(DerValue.tag_Sequence, tmp);
    // OCSPRequest without the signature
    DerOutputStream ocspRequest = new DerOutputStream();
    ocspRequest.write(DerValue.tag_Sequence, tbsRequest);
    byte[] bytes = ocspRequest.toByteArray();
    if (dump) {
        HexDumpEncoder hexEnc = new HexDumpEncoder();
        debug.println("OCSPRequest bytes...\n\n" + hexEnc.encode(bytes) + "\n");
    }
    return bytes;
}
Also used : Extension(java.security.cert.Extension) HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 24 with HexDumpEncoder

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

the class CertId method toString.

/**
 * Create a string representation of the CertId.
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("CertId \n");
    sb.append("Algorithm: " + hashAlgId.toString() + "\n");
    sb.append("issuerNameHash \n");
    HexDumpEncoder encoder = new HexDumpEncoder();
    sb.append(encoder.encode(issuerNameHash));
    sb.append("\nissuerKeyHash: \n");
    sb.append(encoder.encode(issuerKeyHash));
    sb.append("\n" + certSerialNumber.toString());
    return sb.toString();
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

Example 25 with HexDumpEncoder

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

the class CertId method encode.

/**
 * Encode the CertId using ASN.1 DER.
 * The hash algorithm used is SHA-1.
 */
public void encode(DerOutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    hashAlgId.encode(tmp);
    tmp.putOctetString(issuerNameHash);
    tmp.putOctetString(issuerKeyHash);
    certSerialNumber.encode(tmp);
    out.write(DerValue.tag_Sequence, tmp);
    if (debug) {
        HexDumpEncoder encoder = new HexDumpEncoder();
        System.out.println("Encoded certId is " + encoder.encode(out.toByteArray()));
    }
}
Also used : HexDumpEncoder(sun.security.util.HexDumpEncoder)

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