use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.
the class EcCurveParams method parse.
public static ECParameterSpec parse(final byte[] data) throws IOException {
final DerParser d1 = new DerParser(data);
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
return parseSequence(d1o1);
}
use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.
the class FooTest method test3.
@Ignore
@Test
public void test3() throws Exception {
final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
final Pem pem = Pem.parse(bytes);
{
final DerParser d1 = new DerParser(pem.getData());
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d2 = new DerParser(d1o1.getValue());
final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o2.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.NULL);
}
final Asn1Object d2o3 = d2.readObject().assertType(OCTET_STRING);
{
final DerParser d3 = new DerParser(d2o3.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o1.getValue());
final BigInteger version = d4.readBigInteger();
final RSAPrivateCrtKey privateKey = Rsa.Private.builder().modulus(d4.readBigInteger()).publicExponent(d4.readBigInteger()).privateExponent(d4.readBigInteger()).primeP(d4.readBigInteger()).primeQ(d4.readBigInteger()).primeExponentP(d4.readBigInteger()).primeExponentQ(d4.readBigInteger()).crtCoefficient(d4.readBigInteger()).build().toKey();
final Key key1 = new Key(privateKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
System.out.println(key1);
}
}
}
}
}
use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.
the class BeginEcParameters method decode.
public static Object decode(final byte[] bytes) throws IOException {
if (!Utils.startsWith("-----BEGIN EC PARAMETERS-----", bytes)) {
throw new IllegalArgumentException("Contents do not start with -----BEGIN EC PARAMETERS-----");
}
final Pem pem = Pem.parse(bytes);
final byte[] data = pem.getData();
final Asn1Type type = new DerParser(data).readObject().getType();
if (type == Asn1Type.SEQUENCE) {
return EcCurveParams.parse(data);
}
if (type == Asn1Type.OBJECT_IDENTIFIER) {
return EcCurveParams.parseOid(data);
}
throw new UnsupportedOperationException("Unexpected ASN1 type: " + type);
}
use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.
the class BeginPrivateKey method decodeDsaKey.
private static Key decodeDsaKey(final byte[] bytes) throws IOException {
final Dsa.Private.Builder dsa = Dsa.Private.builder();
final DerParser d1 = new DerParser(bytes);
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d2 = new DerParser(d1o1.getValue());
final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o2.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o2.getValue());
dsa.p(d4.readBigInteger());
dsa.q(d4.readBigInteger());
dsa.g(d4.readBigInteger());
}
}
final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
{
final DerParser d3 = new DerParser(d2o3.getValue());
dsa.x(d3.readBigInteger());
final Dsa.Private build = dsa.build();
final DSAPrivateKey privateKey = build.toKey();
final DSAPublicKey publicKey = build.toPublic().toKey();
return new Key(privateKey, publicKey, Key.Type.PRIVATE, DSA, Key.Format.PEM);
}
}
}
use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.
the class BeginPrivateKey method decodeRsaKey.
private static Key decodeRsaKey(final byte[] bytes) throws IOException {
final DerParser d1 = new DerParser(bytes);
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d2 = new DerParser(d1o1.getValue());
final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o2.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.NULL);
}
final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
{
final DerParser d3 = new DerParser(d2o3.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o1.getValue());
final BigInteger version = d4.readBigInteger();
final Rsa.Private build = Rsa.Private.builder().modulus(d4.readBigInteger()).publicExponent(d4.readBigInteger()).privateExponent(d4.readBigInteger()).primeP(d4.readBigInteger()).primeQ(d4.readBigInteger()).primeExponentP(d4.readBigInteger()).primeExponentQ(d4.readBigInteger()).crtCoefficient(d4.readBigInteger()).build();
final RSAPrivateCrtKey privateKey = build.toKey();
final RSAPublicKey publicKey = build.toPublic().toKey();
return new Key(privateKey, publicKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
}
}
}
}
Aggregations