Search in sources :

Example 1 with DerWriter

use of io.churchkey.asn1.DerWriter in project churchkey by tomitribe.

the class FooTest method test4.

@Ignore
@Test
public void test4() throws Exception {
    final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
    final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
    final RSAPrivateCrtKey key = (RSAPrivateCrtKey) Keys.decode(bytes).getKey();
    final DerWriter d4 = new DerWriter();
    d4.integer(ZERO);
    d4.integer(key.getModulus());
    d4.integer(key.getPublicExponent());
    d4.integer(key.getPrivateExponent());
    d4.integer(key.getPrimeP());
    d4.integer(key.getPrimeQ());
    d4.integer(key.getPrimeExponentP());
    d4.integer(key.getPrimeExponentQ());
    d4.integer(key.getCrtCoefficient());
    final DerWriter d3b = new DerWriter();
    d3b.writeObject(Asn1Object.sequence(d4.bytes()));
    final DerWriter d3a = new DerWriter();
    d3a.writeObject(Asn1Object.objectIdentifier(Oid.fromString("1.2.840.113549.1.1.1")));
    d3a.writeObject(Asn1Object.nill());
    final DerWriter d2 = new DerWriter();
    d2.integer(ZERO);
    d2.writeObject(Asn1Object.sequence(d3a.bytes()));
    d2.writeObject(Asn1Object.octetString(d3b.bytes()));
    final DerWriter d1 = new DerWriter();
    d1.writeObject(Asn1Object.sequence(d2.bytes()));
    printOctets(d1.bytes());
    final String private_key = Pem.builder().type("PRIVATE KEY").data(d1.bytes()).wrap(64).format();
    assertEquals(new String(bytes), private_key);
    Asn1Dump.print(d1.bytes());
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Resource(io.churchkey.Resource) DerWriter(io.churchkey.asn1.DerWriter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with DerWriter

use of io.churchkey.asn1.DerWriter in project churchkey by tomitribe.

the class FooTest method test2.

@Ignore
@Test
public void test2() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        try (DerWriter w = new DerWriter(baos)) {
            final DerWriter sequence = new DerWriter();
            sequence.integer(BigInteger.valueOf(-1));
            sequence.integer(BigInteger.valueOf(129));
            sequence.integer(new byte[] { 0, 0 }, 0, 2);
            sequence.integer(new byte[] { 0, 1 }, 0, 2);
            sequence.close();
            final byte[] bytes = sequence.bytes();
            // Asn1Dump.print(bytes);
            final Asn1Object sequenceAsn1 = new Asn1Object(Asn1Class.UNIVERSAL, Asn1Type.SEQUENCE, false, bytes.length, bytes);
            w.writeObject(sequenceAsn1);
        }
    } finally {
        baos.close();
    }
    Asn1Dump.print(baos.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DerWriter(io.churchkey.asn1.DerWriter) Asn1Object(io.churchkey.asn1.Asn1Object) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with DerWriter

use of io.churchkey.asn1.DerWriter in project churchkey by tomitribe.

the class FooTest method test5.

@Ignore
@Test
public void test5() throws Exception {
    final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
    final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
    final RSAPrivateCrtKey key = (RSAPrivateCrtKey) Keys.decode(bytes).getKey();
    final byte[] bytes1 = write().sequence(write().integer(ZERO).sequence(write().objectIdentifier(Oid.fromString("1.2.840.113549.1.1.1")).nill().bytes()).octetString(write().sequence(write().integer(ZERO).integer(key.getModulus()).integer(key.getPublicExponent()).integer(key.getPrivateExponent()).integer(key.getPrimeP()).integer(key.getPrimeQ()).integer(key.getPrimeExponentP()).integer(key.getPrimeExponentQ()).integer(key.getCrtCoefficient()).bytes()).bytes()).bytes()).bytes();
    printOctets(bytes1);
    final String private_key = Pem.builder().type("PRIVATE KEY").data(bytes1).wrap(64).format();
    assertEquals(new String(bytes), private_key);
    Asn1Dump.print(new DerWriter().sequence(new DerWriter().integer(ZERO).sequence(new DerWriter().objectIdentifier(Oid.fromString("1.2.840.113549.1.1.1")).nill().bytes()).octetString(new DerWriter().sequence(new DerWriter().integer(ZERO).integer(key.getModulus()).integer(key.getPublicExponent()).integer(key.getPrivateExponent()).integer(key.getPrimeP()).integer(key.getPrimeQ()).integer(key.getPrimeExponentP()).integer(key.getPrimeExponentQ()).integer(key.getCrtCoefficient()).bytes()).bytes()).bytes()).bytes());
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Resource(io.churchkey.Resource) DerWriter(io.churchkey.asn1.DerWriter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with DerWriter

use of io.churchkey.asn1.DerWriter in project churchkey by tomitribe.

the class FooTest method test.

@Ignore
@Test
public void test() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        try (DerWriter w = new DerWriter(baos)) {
            w.integer(BigInteger.valueOf(-1));
            w.integer(BigInteger.valueOf(129));
            w.integer(new byte[] { 0, 0 }, 0, 2);
            w.integer(new byte[] { 0, 1 }, 0, 2);
        }
    } finally {
        baos.close();
    }
    Asn1Dump.print(baos.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DerWriter(io.churchkey.asn1.DerWriter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DerWriter (io.churchkey.asn1.DerWriter)4 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4 Resource (io.churchkey.Resource)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)2 Asn1Object (io.churchkey.asn1.Asn1Object)1