Search in sources :

Example 41 with DerValue

use of sun.security.util.DerValue in project jdk8u_jdk by JetBrains.

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 42 with DerValue

use of sun.security.util.DerValue in project jdk8u_jdk by JetBrains.

the class SimpleValidator method getNetscapeCertTypeBit.

/**
     * Get the value of the specified bit in the Netscape certificate type
     * extension. If the extension is not present at all, we return true.
     */
static boolean getNetscapeCertTypeBit(X509Certificate cert, String type) {
    try {
        NetscapeCertTypeExtension ext;
        if (cert instanceof X509CertImpl) {
            X509CertImpl certImpl = (X509CertImpl) cert;
            ObjectIdentifier oid = OBJID_NETSCAPE_CERT_TYPE;
            ext = (NetscapeCertTypeExtension) certImpl.getExtension(oid);
            if (ext == null) {
                return true;
            }
        } else {
            byte[] extVal = cert.getExtensionValue(OID_NETSCAPE_CERT_TYPE);
            if (extVal == null) {
                return true;
            }
            DerInputStream in = new DerInputStream(extVal);
            byte[] encoded = in.getOctetString();
            encoded = new DerValue(encoded).getUnalignedBitString().toByteArray();
            ext = new NetscapeCertTypeExtension(encoded);
        }
        Boolean val = ext.get(type);
        return val.booleanValue();
    } catch (IOException e) {
        return false;
    }
}
Also used : X509CertImpl(sun.security.x509.X509CertImpl) DerValue(sun.security.util.DerValue) DerInputStream(sun.security.util.DerInputStream) IOException(java.io.IOException) NetscapeCertTypeExtension(sun.security.x509.NetscapeCertTypeExtension) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 43 with DerValue

use of sun.security.util.DerValue in project jdk8u_jdk by JetBrains.

the class X400Address method encode.

/**
     * Encode the X400 name into the DerOutputStream.
     *
     * @param out the DER stream to encode the X400Address to.
     * @exception IOException on encoding errors.
     */
public void encode(DerOutputStream out) throws IOException {
    DerValue derValue = new DerValue(nameValue);
    out.putDerValue(derValue);
}
Also used : DerValue(sun.security.util.DerValue)

Example 44 with DerValue

use of sun.security.util.DerValue in project jdk8u_jdk by JetBrains.

the class X509CertSelectorTest method getCertPubKeyAlgOID.

private ObjectIdentifier getCertPubKeyAlgOID(X509Certificate xcert) throws IOException {
    byte[] encodedKey = xcert.getPublicKey().getEncoded();
    DerValue val = new DerValue(encodedKey);
    if (val.tag != DerValue.tag_Sequence) {
        throw new RuntimeException("invalid key format");
    }
    return AlgorithmId.parse(val.data.getDerValue()).getOID();
}
Also used : DerValue(sun.security.util.DerValue)

Example 45 with DerValue

use of sun.security.util.DerValue in project jdk8u_jdk by JetBrains.

the class PKCS9Attributes method decode.

/**
     * Decode this set of PKCS9 attributes from the contents of its
     * DER encoding. Ignores unsupported attributes when directed.
     *
     * @param in
     * the contents of the DER encoding of the attribute set.
     *
     * @exception IOException
     * on i/o error, encoding syntax error, unacceptable or
     * unsupported attribute, or duplicate attribute.
     */
private byte[] decode(DerInputStream in) throws IOException {
    DerValue val = in.getDerValue();
    // save the DER encoding with its proper tag byte.
    byte[] derEncoding = val.toByteArray();
    derEncoding[0] = DerValue.tag_SetOf;
    DerInputStream derIn = new DerInputStream(derEncoding);
    DerValue[] derVals = derIn.getSet(3, true);
    PKCS9Attribute attrib;
    ObjectIdentifier oid;
    boolean reuseEncoding = true;
    for (int i = 0; i < derVals.length; i++) {
        try {
            attrib = new PKCS9Attribute(derVals[i]);
        } catch (ParsingException e) {
            if (ignoreUnsupportedAttributes) {
                // cannot reuse supplied DER encoding
                reuseEncoding = false;
                // skip
                continue;
            } else {
                throw e;
            }
        }
        oid = attrib.getOID();
        if (attributes.get(oid) != null)
            throw new IOException("Duplicate PKCS9 attribute: " + oid);
        if (permittedAttributes != null && !permittedAttributes.containsKey(oid))
            throw new IOException("Attribute " + oid + " not permitted in this attribute set");
        attributes.put(oid, attrib);
    }
    return reuseEncoding ? derEncoding : generateDerEncoding();
}
Also used : DerValue(sun.security.util.DerValue) DerInputStream(sun.security.util.DerInputStream) IOException(java.io.IOException) ObjectIdentifier(sun.security.util.ObjectIdentifier)

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