Search in sources :

Example 81 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project i2pplus by vituperative.

the class SigUtil method toJavaDSAKey.

public static DSAPrivateKey toJavaDSAKey(SigningPrivateKey pk) throws GeneralSecurityException {
    KeyFactory kf = KeyFactory.getInstance("DSA");
    // x p q g
    KeySpec ks = new DSAPrivateKeySpec(new BigInteger(1, pk.getData()), // KeySpec ks = new DSAPrivateKeySpec(new NativeBigInteger(1, pk.getData()),
    CryptoConstants.dsap, CryptoConstants.dsaq, CryptoConstants.dsag);
    return (DSAPrivateKey) kf.generatePrivate(ks);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 82 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project i2pplus by vituperative.

the class SigUtil method fromJavaKey.

/**
 *  Use if SigType is unknown.
 *  For efficiency, use fromJavakey(pk, type) if type is known.
 *
 *  @param pk JAVA key!
 *  @throws IllegalArgumentException on unknown type
 *  @since 0.9.18
 */
public static SigningPrivateKey fromJavaKey(PrivateKey pk) throws GeneralSecurityException {
    if (pk instanceof DSAPrivateKey) {
        return fromJavaKey((DSAPrivateKey) pk);
    }
    if (pk instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) pk;
        AlgorithmParameterSpec spec = k.getParams();
        SigType type;
        if (spec.equals(SigType.ECDSA_SHA256_P256.getParams()))
            type = SigType.ECDSA_SHA256_P256;
        else if (spec.equals(SigType.ECDSA_SHA384_P384.getParams()))
            type = SigType.ECDSA_SHA384_P384;
        else if (spec.equals(SigType.ECDSA_SHA512_P521.getParams()))
            type = SigType.ECDSA_SHA512_P521;
        else {
            // failing on Android (ticket #2296)
            throw new IllegalArgumentException("Unknown EC type: " + pk.getClass() + " spec: " + spec.getClass());
        }
        return fromJavaKey(k, type);
    }
    if (pk instanceof EdDSAPrivateKey) {
        return fromJavaKey((EdDSAPrivateKey) pk, SigType.EdDSA_SHA512_Ed25519);
    }
    if (pk instanceof RSAPrivateKey) {
        RSAPrivateKey k = (RSAPrivateKey) pk;
        int sz = k.getModulus().bitLength();
        SigType type;
        if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
            type = SigType.RSA_SHA256_2048;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA384_3072.getParams()).getKeysize())
            type = SigType.RSA_SHA384_3072;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA512_4096.getParams()).getKeysize())
            type = SigType.RSA_SHA512_4096;
        else
            throw new IllegalArgumentException("Unknown RSA type");
        return fromJavaKey(k, type);
    }
    throw new IllegalArgumentException("Unknown type: " + pk.getClass());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ECPoint(java.security.spec.ECPoint)

Example 83 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project connectbot by connectbot.

the class SSH method retrieveIdentities.

@Override
public Map<String, byte[]> retrieveIdentities() {
    Map<String, byte[]> pubKeys = new HashMap<>(manager.loadedKeypairs.size());
    for (Entry<String, KeyHolder> entry : manager.loadedKeypairs.entrySet()) {
        KeyPair pair = entry.getValue().pair;
        try {
            PrivateKey privKey = pair.getPrivate();
            if (privKey instanceof RSAPrivateKey) {
                RSAPublicKey pubkey = (RSAPublicKey) pair.getPublic();
                pubKeys.put(entry.getKey(), RSASHA1Verify.get().encodePublicKey(pubkey));
            } else if (privKey instanceof DSAPrivateKey) {
                DSAPublicKey pubkey = (DSAPublicKey) pair.getPublic();
                pubKeys.put(entry.getKey(), DSASHA1Verify.get().encodePublicKey(pubkey));
            } else if (privKey instanceof ECPrivateKey) {
                ECPublicKey pubkey = (ECPublicKey) pair.getPublic();
                pubKeys.put(entry.getKey(), ECDSASHA2Verify.getVerifierForKey(pubkey).encodePublicKey(pubkey));
            } else if (privKey instanceof Ed25519PrivateKey) {
                Ed25519PublicKey pubkey = (Ed25519PublicKey) pair.getPublic();
                pubKeys.put(entry.getKey(), Ed25519Verify.get().encodePublicKey(pubkey));
            }
        } catch (IOException ignored) {
        }
    }
    return pubKeys;
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) Ed25519PrivateKey(com.trilead.ssh2.crypto.keys.Ed25519PrivateKey) Ed25519PrivateKey(com.trilead.ssh2.crypto.keys.Ed25519PrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) HashMap(java.util.HashMap) KeyHolder(org.connectbot.service.TerminalManager.KeyHolder) IOException(java.io.IOException) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) Ed25519PublicKey(com.trilead.ssh2.crypto.keys.Ed25519PublicKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 84 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project SpringRemote by HaleyWang.

the class SSH2KeyPairFile method writeKeyPair.

public static byte[] writeKeyPair(ASCIIArmour armour, String password, SecureRandom random, KeyPair keyPair) throws SSH2FatalException {
    ASN1Object pem;
    PublicKey publicKey = keyPair.getPublic();
    int headType;
    if (publicKey instanceof DSAPublicKey) {
        DSAPublicKey pubKey = (DSAPublicKey) keyPair.getPublic();
        DSAPrivateKey prvKey = (DSAPrivateKey) keyPair.getPrivate();
        DSAParams params = pubKey.getParams();
        pem = new PEMDSAPrivate(0, params.getP(), params.getQ(), params.getG(), pubKey.getY(), prvKey.getX());
        headType = TYPE_PEM_DSA;
    } else if (publicKey instanceof RSAPublicKey) {
        RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) keyPair.getPrivate();
        pem = new PEMRSAPrivate(0, pubKey.getModulus(), pubKey.getPublicExponent(), prvKey.getPrivateExponent(), prvKey.getPrimeP(), prvKey.getPrimeQ(), prvKey.getCrtCoefficient());
        headType = TYPE_PEM_RSA;
    } else if (publicKey instanceof ECPublicKey) {
        ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey prvKey = (ECPrivateKey) keyPair.getPrivate();
        pem = new PEMECPrivate(pubKey, prvKey);
        headType = TYPE_PEM_EC;
    } else {
        throw new SSH2FatalException("Unsupported key type: " + publicKey);
    }
    armour.setHeaderLine(BEGIN_PRV_KEY[headType]);
    armour.setTailLine(END_PRV_KEY[headType]);
    ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
    ASN1DER der = new ASN1DER();
    try {
        der.encode(enc, pem);
    } catch (IOException e) {
        throw new SSH2FatalException("Error while DER encoding");
    }
    byte[] keyBlob = enc.toByteArray();
    if (password != null && password.length() > 0) {
        byte[] iv = new byte[16];
        random.setSeed(keyBlob);
        for (int i = 0; i < iv.length; i++) {
            byte[] r = new byte[1];
            do {
                random.nextBytes(r);
                iv[i] = r[0];
            } while (iv[i] == 0x00);
        }
        armour.setHeaderField(PRV_PROCTYPE, "4,ENCRYPTED");
        armour.setHeaderField(PRV_DEKINFO, "AES-128-CBC," + HexDump.toString(iv).toUpperCase());
        int encLen = (16 - (keyBlob.length % 16)) + keyBlob.length;
        byte[] encBuf = new byte[encLen];
        doCipher(Cipher.ENCRYPT_MODE, "AES/CBC/PKCS5Padding", password, keyBlob, keyBlob.length, encBuf, iv);
        keyBlob = encBuf;
    }
    return keyBlob;
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ASN1DER(com.mindbright.asn1.ASN1DER) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAParams(java.security.interfaces.DSAParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ASN1Object(com.mindbright.asn1.ASN1Object)

Example 85 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project SpringRemote by HaleyWang.

the class DSAWithSHA1 method sign.

protected byte[] sign(byte[] data) {
    DSAPrivateKey key = (DSAPrivateKey) privateKey;
    DSAParams parm = key.getParams();
    BigInteger x = key.getX();
    BigInteger p = parm.getP();
    BigInteger q = parm.getQ();
    BigInteger g = parm.getG();
    BigInteger[] sign = DSAAlgorithm.sign(x, p, q, g, data);
    if (sign == null || sign.length != 2) {
        return null;
    }
    BigInteger r = sign[0];
    BigInteger s = sign[1];
    // Encode
    DSASIG dsasig = new DSASIG(r, s);
    ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
    ASN1DER der = new ASN1DER();
    try {
        der.encode(enc, dsasig);
    } catch (IOException e) {
    // This should not happen
    }
    return enc.toByteArray();
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)86 BigInteger (java.math.BigInteger)35 DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)26 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)25 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 IOException (java.io.IOException)18 KeyPair (java.security.KeyPair)18 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)15 PrivateKey (java.security.PrivateKey)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 KeyFactory (java.security.KeyFactory)12 RSAPublicKey (java.security.interfaces.RSAPublicKey)12 PublicKey (java.security.PublicKey)11 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 InvalidKeyException (java.security.InvalidKeyException)9 KeyPairGenerator (java.security.KeyPairGenerator)9 ECPublicKey (java.security.interfaces.ECPublicKey)9