use of com.github.zhenwei.core.asn1.ASN1Object 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 com.github.zhenwei.core.asn1.ASN1Object 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);
}
}
}
}
use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.
the class BeginRsaPrivateKey method decode.
public static Key decode(final byte[] bytes) {
try {
final DerParser parser = new DerParser(bytes);
final Asn1Object sequence = parser.readObject();
if (sequence.getType() != Asn1Type.SEQUENCE) {
throw new IllegalArgumentException("Invalid DER: not a sequence");
}
// Parse inside the sequence
final DerParser p = sequence.createParser();
// Skip version
p.readObject();
final Rsa.Private build = Rsa.Private.builder().modulus(p.readObject().asInteger()).publicExponent(p.readObject().asInteger()).privateExponent(p.readObject().asInteger()).primeP(p.readObject().asInteger()).primeQ(p.readObject().asInteger()).primeExponentP(p.readObject().asInteger()).primeExponentQ(p.readObject().asInteger()).crtCoefficient(p.readObject().asInteger()).build();
final RSAPrivateCrtKey privateKey = build.toKey();
final RSAPublicKey publicKey = build.toPublic().toKey();
return new Key(privateKey, publicKey, Key.Type.PRIVATE, Key.Algorithm.RSA, Key.Format.PEM);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.
the class EcCurveParams method parseSequence.
public static ECParameterSpec parseSequence(final Asn1Object d1o1) throws IOException {
final ECField field;
final EllipticCurve ellipticCurve;
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 Oid oid = d3o1.asOID();
if (primeField.equals(oid)) {
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.INTEGER);
field = new ECFieldFp(d3o2.toInteger());
} else if (characteristicTwoField.equals(oid)) {
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o2.getValue());
final Asn1Object d4o1 = d4.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d4o2 = d4.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Oid basis = d4o2.asOID();
if (ppBasis.equals(basis)) {
final Asn1Object d4o3 = d4.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d5 = new DerParser(d4o3.getValue());
final Asn1Object d5o1 = d5.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d5o2 = d5.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d5o3 = d5.readObject().assertType(Asn1Type.INTEGER);
field = new ECFieldF2m(d4o1.asInteger().intValue(), new int[] { d5o3.asInteger().intValue(), d5o2.asInteger().intValue(), d5o1.asInteger().intValue() });
}
} else if (tpBasis.equals(basis)) {
final Asn1Object d5o1 = d4.readObject().assertType(Asn1Type.INTEGER);
field = new ECFieldF2m(d4o1.asInteger().intValue(), new int[] { d5o1.asInteger().intValue() });
} else {
throw new UnsupportedOperationException("Unsupported characteristic-two-basis " + basis);
}
}
} else {
throw new UnsupportedOperationException(oid.toString());
}
}
final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o3.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OCTET_STRING);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.OCTET_STRING);
final Asn1Object d3o3 = d3.readObject();
final BigInteger a = d3o1.toInteger();
final BigInteger b = d3o2.toInteger();
if (d3o3 == null) {
ellipticCurve = new EllipticCurve(field, a, b);
} else {
ellipticCurve = new EllipticCurve(field, a, b, d3o3.getPureValueBytes());
}
}
final Asn1Object d2o4 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
final Asn1Object d2o5 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o6 = d2.readObject().assertType(Asn1Type.INTEGER);
final ECPoint point = EcPoints.fromBytes(d2o4.getPureValueBytes());
return new ECParameterSpec(ellipticCurve, point, d2o5.toInteger(), d2o6.toInteger().intValue());
}
use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.
the class EcCurveParams method parseOid.
public static Oid parseOid(final byte[] data) throws IOException {
final DerParser d1 = new DerParser(data);
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
return d1o1.asOID();
}
Aggregations