Search in sources :

Example 1 with Dsa

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");
}
Also used : Dsa(io.churchkey.dsa.Dsa) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 2 with Dsa

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);
        }
    }
}
Also used : Dsa(io.churchkey.dsa.Dsa) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DerParser(io.churchkey.asn1.DerParser) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) Asn1Object(io.churchkey.asn1.Asn1Object) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

Key (io.churchkey.Key)2 Dsa (io.churchkey.dsa.Dsa)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 ECPrivateKey (java.security.interfaces.ECPrivateKey)2 ECPublicKey (java.security.interfaces.ECPublicKey)2 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 Asn1Object (io.churchkey.asn1.Asn1Object)1 DerParser (io.churchkey.asn1.DerParser)1 BigInteger (java.math.BigInteger)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 ArrayList (java.util.ArrayList)1 SecretKey (javax.crypto.SecretKey)1