Search in sources :

Example 26 with DerValue

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

the class TSResponse method parse.

/*
     * Parses the timestamp response.
     *
     * @param status A buffer containing the ASN.1 BER encoded response.
     * @throws IOException The exception is thrown if a problem is encountered
     *         parsing the timestamp response.
     */
private void parse(byte[] tsReply) throws IOException {
    // Decode TimeStampResp
    DerValue derValue = new DerValue(tsReply);
    if (derValue.tag != DerValue.tag_Sequence) {
        throw new IOException("Bad encoding for timestamp response");
    }
    // Parse status
    DerValue statusInfo = derValue.data.getDerValue();
    this.status = statusInfo.data.getInteger();
    if (debug != null) {
        debug.println("timestamp response: status=" + this.status);
    }
    // Parse statusString, if present
    if (statusInfo.data.available() > 0) {
        byte tag = (byte) statusInfo.data.peekByte();
        if (tag == DerValue.tag_SequenceOf) {
            DerValue[] strings = statusInfo.data.getSequence(1);
            statusString = new String[strings.length];
            for (int i = 0; i < strings.length; i++) {
                statusString[i] = strings[i].getUTF8String();
                if (debug != null) {
                    debug.println("timestamp response: statusString=" + statusString[i]);
                }
            }
        }
    }
    // Parse failInfo, if present
    if (statusInfo.data.available() > 0) {
        this.failureInfo = statusInfo.data.getUnalignedBitString().toBooleanArray();
    }
    // Parse timeStampToken, if present
    if (derValue.data.available() > 0) {
        DerValue timestampToken = derValue.data.getDerValue();
        encodedTsToken = timestampToken.toByteArray();
        tsToken = new PKCS7(encodedTsToken);
        tstInfo = new TimestampToken(tsToken.getContentInfo().getData());
    }
    // Check the format of the timestamp response
    if (this.status == 0 || this.status == 1) {
        if (tsToken == null) {
            throw new TimestampException("Bad encoding for timestamp response: " + "expected a timeStampToken element to be present");
        }
    } else if (tsToken != null) {
        throw new TimestampException("Bad encoding for timestamp response: " + "expected no timeStampToken element to be present");
    }
}
Also used : PKCS7(sun.security.pkcs.PKCS7) DerValue(sun.security.util.DerValue) IOException(java.io.IOException)

Example 27 with DerValue

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

the class EncryptedPrivateKeyInfo method checkPKCS8Encoding.

@SuppressWarnings("fallthrough")
private void checkPKCS8Encoding(byte[] encodedKey) throws IOException {
    DerInputStream in = new DerInputStream(encodedKey);
    DerValue[] values = in.getSequence(3);
    switch(values.length) {
        case 4:
            checkTag(values[3], DerValue.TAG_CONTEXT, "attributes");
        /* fall through */
        case 3:
            checkTag(values[0], DerValue.tag_Integer, "version");
            keyAlg = AlgorithmId.parse(values[1]).getName();
            checkTag(values[2], DerValue.tag_OctetString, "privateKey");
            break;
        default:
            throw new IOException("invalid key encoding");
    }
}
Also used : DerValue(sun.security.util.DerValue) DerInputStream(sun.security.util.DerInputStream)

Example 28 with DerValue

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

the class X509CertPath method parsePKIPATH.

/**
 * Parse a PKIPATH format CertPath from an InputStream. Return an
 * unmodifiable List of the certificates.
 *
 * @param is the <code>InputStream</code> to read the data from
 * @return an unmodifiable List of the certificates
 * @exception CertificateException if an exception occurs
 */
private static List<X509Certificate> parsePKIPATH(InputStream is) throws CertificateException {
    List<X509Certificate> certList = null;
    CertificateFactory certFac = null;
    if (is == null) {
        throw new CertificateException("input stream is null");
    }
    try {
        DerInputStream dis = new DerInputStream(readAllBytes(is));
        DerValue[] seq = dis.getSequence(3);
        if (seq.length == 0) {
            return Collections.<X509Certificate>emptyList();
        }
        certFac = CertificateFactory.getInstance("X.509");
        certList = new ArrayList<X509Certificate>(seq.length);
        // append certs in reverse order (target to trust anchor)
        for (int i = seq.length - 1; i >= 0; i--) {
            certList.add((X509Certificate) certFac.generateCertificate(new ByteArrayInputStream(seq[i].toByteArray())));
        }
        return Collections.unmodifiableList(certList);
    } catch (IOException ioe) {
        throw new CertificateException("IOException parsing PkiPath data: " + ioe, ioe);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DerValue(sun.security.util.DerValue) CertificateException(java.security.cert.CertificateException) DerInputStream(sun.security.util.DerInputStream) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 29 with DerValue

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

the class X509CertificatePair method parse.

/* Parse the encoded bytes */
private void parse(DerValue val) throws IOException, CertificateException {
    if (val.tag != DerValue.tag_Sequence) {
        throw new IOException("Sequence tag missing for X509CertificatePair");
    }
    while (val.data != null && val.data.available() != 0) {
        DerValue opt = val.data.getDerValue();
        short tag = (byte) (opt.tag & 0x01f);
        switch(tag) {
            case TAG_FORWARD:
                if (opt.isContextSpecific() && opt.isConstructed()) {
                    if (forward != null) {
                        throw new IOException("Duplicate forward " + "certificate in X509CertificatePair");
                    }
                    opt = opt.data.getDerValue();
                    forward = X509Factory.intern(new X509CertImpl(opt.toByteArray()));
                }
                break;
            case TAG_REVERSE:
                if (opt.isContextSpecific() && opt.isConstructed()) {
                    if (reverse != null) {
                        throw new IOException("Duplicate reverse " + "certificate in X509CertificatePair");
                    }
                    opt = opt.data.getDerValue();
                    reverse = X509Factory.intern(new X509CertImpl(opt.toByteArray()));
                }
                break;
            default:
                throw new IOException("Invalid encoding of " + "X509CertificatePair");
        }
    }
    if (forward == null && reverse == null) {
        throw new CertificateException("at least one of certificate pair " + "must be non-null");
    }
}
Also used : DerValue(sun.security.util.DerValue) X509CertImpl(sun.security.x509.X509CertImpl) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException)

Example 30 with DerValue

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

the class X509CertificatePair method emit.

/* Translate to encoded bytes */
private void emit(DerOutputStream out) throws IOException, CertificateEncodingException {
    DerOutputStream tagged = new DerOutputStream();
    if (forward != null) {
        DerOutputStream tmp = new DerOutputStream();
        tmp.putDerValue(new DerValue(forward.getEncoded()));
        tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_FORWARD), tmp);
    }
    if (reverse != null) {
        DerOutputStream tmp = new DerOutputStream();
        tmp.putDerValue(new DerValue(reverse.getEncoded()));
        tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_REVERSE), tmp);
    }
    out.write(DerValue.tag_Sequence, tagged);
}
Also used : DerOutputStream(sun.security.util.DerOutputStream) DerValue(sun.security.util.DerValue)

Aggregations

DerValue (sun.security.util.DerValue)72 DerInputStream (sun.security.util.DerInputStream)26 IOException (java.io.IOException)25 ObjectIdentifier (sun.security.util.ObjectIdentifier)17 CertificateException (java.security.cert.CertificateException)12 DerOutputStream (sun.security.util.DerOutputStream)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 BigInteger (java.math.BigInteger)9 KeyStoreException (java.security.KeyStoreException)9 X509Certificate (java.security.cert.X509Certificate)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 UnrecoverableEntryException (java.security.UnrecoverableEntryException)8 CertificateFactory (java.security.cert.CertificateFactory)7 DestroyFailedException (javax.security.auth.DestroyFailedException)6 X500Principal (javax.security.auth.x500.X500Principal)6 X509CertImpl (sun.security.x509.X509CertImpl)6 AlgorithmId (sun.security.x509.AlgorithmId)5 AlgorithmParameters (java.security.AlgorithmParameters)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 KeyFactory (java.security.KeyFactory)4