Search in sources :

Example 51 with DerValue

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

the class UnknownAttribute method main.

public static void main(String[] args) throws Exception {
    // Unknown attr
    PKCS9Attribute p1 = new PKCS9Attribute(PKCS9Attribute.CHALLENGE_PASSWORD_STR, "t0p5ecr3t");
    if (!p1.isKnown()) {
        throw new Exception();
    }
    // Unknown attr from DER
    byte[] data = { // SEQUENCE OF
    0x30, // SEQUENCE OF
    0x08, // OID 1.2.3 and
    0x06, // OID 1.2.3 and
    0x02, // OID 1.2.3 and
    0x2A, // OID 1.2.3 and
    0x03, // an empty SET
    0x31, // an empty SET
    0x02, // an empty SET
    0x05, // an empty SET
    0x00 };
    PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));
    if (p2.isKnown()) {
        throw new Exception();
    }
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    p2.derEncode(bout);
    new HexDumpEncoder().encodeBuffer(bout.toByteArray(), System.err);
    if (!Arrays.equals(data, bout.toByteArray())) {
        throw new Exception();
    }
    // Unknown attr from value
    try {
        new PKCS9Attribute(new ObjectIdentifier("1.2.3"), "hello");
        throw new Exception();
    } catch (IllegalArgumentException iae) {
    // Good. Unknown attr must have byte[] value type
    }
    PKCS9Attribute p3 = new PKCS9Attribute(new ObjectIdentifier("1.2.3"), new byte[] { 0x31, 0x02, 0x05, 0x00 });
    if (p3.isKnown()) {
        throw new Exception();
    }
    bout = new ByteArrayOutputStream();
    p3.derEncode(bout);
    if (!Arrays.equals(data, bout.toByteArray())) {
        throw new Exception();
    }
}
Also used : PKCS9Attribute(sun.security.pkcs.PKCS9Attribute) HexDumpEncoder(sun.misc.HexDumpEncoder) DerValue(sun.security.util.DerValue) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 52 with DerValue

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

the class NamedBitList method main.

public static void main(String[] args) throws Exception {
    boolean[] bb = (new boolean[] { true, false, true, false, false, false });
    GeneralNames gns = new GeneralNames();
    gns.add(new GeneralName(new DNSName("dns")));
    DerOutputStream out;
    // length should be 5 since only {T,F,T} should be encoded
    KeyUsageExtension x1 = new KeyUsageExtension(bb);
    check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);
    NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);
    check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);
    ReasonFlags r = new ReasonFlags(bb);
    out = new DerOutputStream();
    r.encode(out);
    check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);
    // Read sun.security.x509.DistributionPoint for ASN.1 definition
    DistributionPoint dp = new DistributionPoint(gns, bb, gns);
    out = new DerOutputStream();
    dp.encode(out);
    DerValue v = new DerValue(out.toByteArray());
    // skip distributionPoint
    v.data.getDerValue();
    // read reasons
    DerValue v2 = v.data.getDerValue();
    // reset to BitString since it's context-specfic[1] encoded
    v2.resetTag(DerValue.tag_BitString);
    // length should be 5 since only {T,F,T} should be encoded
    check(v2.getUnalignedBitString().length(), 3);
    BitArray ba;
    ba = new BitArray(new boolean[] { false, false, false });
    check(ba.length(), 3);
    ba = ba.truncate();
    check(ba.length(), 1);
    ba = new BitArray(new boolean[] { true, true, true, true, true, true, true, true, false, false });
    check(ba.length(), 10);
    check(ba.toByteArray().length, 2);
    ba = ba.truncate();
    check(ba.length(), 8);
    check(ba.toByteArray().length, 1);
    ba = new BitArray(new boolean[] { true, true, true, true, true, true, true, true, true, false });
    check(ba.length(), 10);
    check(ba.toByteArray().length, 2);
    ba = ba.truncate();
    check(ba.length(), 9);
    check(ba.toByteArray().length, 2);
}
Also used : GeneralNames(sun.security.x509.GeneralNames) DerOutputStream(sun.security.util.DerOutputStream) ReasonFlags(sun.security.x509.ReasonFlags) DerValue(sun.security.util.DerValue) GeneralName(sun.security.x509.GeneralName) DistributionPoint(sun.security.x509.DistributionPoint) BitArray(sun.security.util.BitArray) DNSName(sun.security.x509.DNSName) NetscapeCertTypeExtension(sun.security.x509.NetscapeCertTypeExtension) KeyUsageExtension(sun.security.x509.KeyUsageExtension)

Example 53 with DerValue

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

the class DomainComponentEncoding method testDN.

private static void testDN(String dn) throws Exception {
    X500Principal p = new X500Principal(dn);
    byte[] encoded = p.getEncoded();
    // name is a sequence of RDN's
    DerInputStream dis = new DerInputStream(encoded);
    DerValue[] nameseq = dis.getSequence(3);
    boolean passed = false;
    for (int i = 0; i < nameseq.length; i++) {
        // each RDN is a set of AttributeTypeAndValue
        DerInputStream is = new DerInputStream(nameseq[i].toByteArray());
        DerValue[] ava = is.getSet(3);
        for (int j = 0; j < ava.length; j++) {
            ObjectIdentifier oid = ava[j].data.getOID();
            if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {
                DerValue value = ava[j].data.getDerValue();
                if (value.getTag() == DerValue.tag_IA5String) {
                    passed = true;
                    break;
                } else {
                    throw new SecurityException("Test failed, expected DOMAIN_COMPONENT tag '" + DerValue.tag_IA5String + "', got '" + value.getTag() + "'");
                }
            }
        }
        if (passed) {
            break;
        }
    }
    if (passed) {
        System.out.println("Test passed");
    } else {
        throw new SecurityException("Test failed");
    }
}
Also used : DerValue(sun.security.util.DerValue) X500Principal(javax.security.auth.x500.X500Principal) DerInputStream(sun.security.util.DerInputStream) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 54 with DerValue

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

the class LocaleInTime method main.

public static void main(String[] args) throws Exception {
    DerOutputStream out = new DerOutputStream();
    out.putUTCTime(new Date());
    DerValue val = new DerValue(out.toByteArray());
    System.out.println(val.getUTCTime());
}
Also used : DerOutputStream(sun.security.util.DerOutputStream) DerValue(sun.security.util.DerValue) Date(java.util.Date)

Example 55 with DerValue

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

the class EmptyValue method main.

public static void main(String[] args) throws Exception {
    DerValue v = new DerValue(new byte[] { 4, 0 });
    if (v.getOctetString().length != 0) {
        throw new Exception("Get octet string error");
    }
    v = new DerValue(new byte[] { 0x30, 0 });
    if (v.data.available() != 0) {
        throw new Exception("Get sequence error");
    }
}
Also used : DerValue(sun.security.util.DerValue)

Aggregations

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