use of io.churchkey.dsa.Dsa in project churchkey by tomitribe.
the class JwkParser method asDsaKey.
private Key asDsaKey(final JsonObject jsonObject) {
final Jwk jwk = new Jwk(jsonObject);
final BigInteger p = jwk.getBigInteger("p");
final BigInteger q = jwk.getBigInteger("q");
final BigInteger g = jwk.getBigInteger("g");
final BigInteger x = jwk.getBigInteger("x");
final BigInteger y = jwk.getBigInteger("y");
final List<String> missing = new ArrayList<>();
if (p == null)
missing.add("p");
if (q == null)
missing.add("q");
if (g == null)
missing.add("g");
if (missing.size() != 0) {
throw new InvalidJwkKeySpecException("DSA", missing);
}
if (x != null) {
final Dsa.Private build = Dsa.Private.builder().p(p).q(q).g(g).x(x).build();
final DSAPrivateKey privateKey = build.toKey();
final DSAPublicKey publicKey = build.toPublic().toKey();
final Map<String, String> attributes = getAttributes(jsonObject, "kty", "p", "q", "q", "x", "y");
return new Key(privateKey, publicKey, Key.Type.PRIVATE, Key.Algorithm.DSA, Key.Format.JWK, attributes);
}
if (y != null) {
final DSAPublicKey publicKey = Dsa.Public.builder().p(p).q(q).g(g).y(y).build().toKey();
final Map<String, String> attributes = getAttributes(jsonObject, "kty", "p", "q", "q", "x", "y");
return new Key(publicKey, Key.Type.PUBLIC, Key.Algorithm.DSA, Key.Format.JWK, attributes);
}
throw new InvalidJwkKeySpecException("DSA", "x", "y");
}
use of io.churchkey.dsa.Dsa 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);
}
}
}
Aggregations