Search in sources :

Example 16 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project robovm by robovm.

the class DSAPublicKeyTest method test_getY.

/**
     * java.security.interfaces.DSAPublicKey
     * #getY()
     * test covers following use cases
     *   Case 1: check with predefined p, q, g, x
     *   Case 2: check with random p, q, g, x. It takes some time (up to
     *           minute)
     */
public void test_getY() throws Exception {
    KeyPairGenerator keyGen = null;
    KeyPair keys = null;
    DSAPrivateKey priv = null;
    DSAPublicKey publ = null;
    // Case 1: check with predefined p, q, g, x
    keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(new DSAParameterSpec(Util.P, Util.Q, Util.G), new SecureRandom(new MySecureRandomSpi(), null) {
    });
    keys = keyGen.generateKeyPair();
    priv = (DSAPrivateKey) keys.getPrivate();
    publ = (DSAPublicKey) keys.getPublic();
    assertNotNull("Invalid Y value", publ.getY());
    // Case 2: check with random p, q, g, x. It takes some time (up to
    // minute)
    keyGen = KeyPairGenerator.getInstance("DSA");
    keys = keyGen.generateKeyPair();
    priv = (DSAPrivateKey) keys.getPrivate();
    publ = (DSAPublicKey) keys.getPublic();
    assertNotNull("Invalid Y value", publ.getY());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPair(java.security.KeyPair) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 17 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project jdk8u_jdk by JetBrains.

the class X509CertificatePair method checkPair.

/*
     * Check for a valid certificate pair
     */
private void checkPair() throws CertificateException {
    /* if either of pair is missing, return w/o error */
    if (forward == null || reverse == null) {
        return;
    }
    /*
         * If both elements of the pair are present, check that they
         * are a valid pair.
         */
    X500Principal fwSubject = forward.getSubjectX500Principal();
    X500Principal fwIssuer = forward.getIssuerX500Principal();
    X500Principal rvSubject = reverse.getSubjectX500Principal();
    X500Principal rvIssuer = reverse.getIssuerX500Principal();
    if (!fwIssuer.equals(rvSubject) || !rvIssuer.equals(fwSubject)) {
        throw new CertificateException("subject and issuer names in " + "forward and reverse certificates do not match");
    }
    /* check signatures unless key parameters are missing */
    try {
        PublicKey pk = reverse.getPublicKey();
        if (!(pk instanceof DSAPublicKey) || ((DSAPublicKey) pk).getParams() != null) {
            forward.verify(pk);
        }
        pk = forward.getPublicKey();
        if (!(pk instanceof DSAPublicKey) || ((DSAPublicKey) pk).getParams() != null) {
            reverse.verify(pk);
        }
    } catch (GeneralSecurityException e) {
        throw new CertificateException("invalid signature: " + e.getMessage());
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) X500Principal(javax.security.auth.x500.X500Principal) CertificateException(java.security.cert.CertificateException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 18 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project camel by apache.

the class DSAKeyPairIdentity method getPublicKeyBlob.

@Override
public byte[] getPublicKeyBlob() {
    DSAPublicKey publicKey = (DSAPublicKey) keyPair.getPublic();
    byte[] sshDss = ALGORITHM_TYPE.getBytes();
    DSAParams dsaParams = publicKey.getParams();
    byte[] pArray = dsaParams.getP().toByteArray();
    byte[] qArray = dsaParams.getQ().toByteArray();
    byte[] gArray = dsaParams.getG().toByteArray();
    byte[] yArray = publicKey.getY().toByteArray();
    byte[] result = new byte[sshDss.length + 4 + pArray.length + 4 + qArray.length + 4 + gArray.length + 4 + yArray.length + 4];
    int index = 0;
    byte[] intAsByteArray = ByteBuffer.allocate(4).putInt(sshDss.length).array();
    System.arraycopy(intAsByteArray, 0, result, index, 4);
    index += 4;
    System.arraycopy(sshDss, 0, result, index, sshDss.length);
    index += sshDss.length;
    intAsByteArray = ByteBuffer.allocate(4).putInt(pArray.length).array();
    System.arraycopy(intAsByteArray, 0, result, index, 4);
    index += 4;
    System.arraycopy(pArray, 0, result, index, pArray.length);
    index += pArray.length;
    intAsByteArray = ByteBuffer.allocate(4).putInt(qArray.length).array();
    System.arraycopy(intAsByteArray, 0, result, index, 4);
    index += 4;
    System.arraycopy(qArray, 0, result, index, qArray.length);
    index += qArray.length;
    intAsByteArray = ByteBuffer.allocate(4).putInt(gArray.length).array();
    System.arraycopy(intAsByteArray, 0, result, index, 4);
    index += 4;
    System.arraycopy(gArray, 0, result, index, gArray.length);
    index += gArray.length;
    intAsByteArray = ByteBuffer.allocate(4).putInt(yArray.length).array();
    System.arraycopy(intAsByteArray, 0, result, index, 4);
    index += 4;
    System.arraycopy(yArray, 0, result, index, yArray.length);
    return result;
}
Also used : DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 19 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project wycheproof by google.

the class DsaTest method testBasic.

/**
   * This is just a test for basic functionality of DSA. The test generates a public and private
   * key, generates a signature, verifies it and prints the whole thing out. This test is useful
   * when an implementation is seriously broken.
   */
@SlowTest(providers = { ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE })
@SuppressWarnings("InsecureCryptoUsage")
public void testBasic() throws Exception {
    int keySize = 2048;
    String algorithm = "SHA256WithDSA";
    String hashAlgorithm = "SHA-256";
    String message = "Hello";
    byte[] messageBytes = message.getBytes("UTF-8");
    KeyPairGenerator generator = java.security.KeyPairGenerator.getInstance("DSA");
    generator.initialize(keySize);
    KeyPair keyPair = generator.generateKeyPair();
    DSAPublicKey pub = (DSAPublicKey) keyPair.getPublic();
    DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
    Signature signer = Signature.getInstance(algorithm);
    Signature verifier = Signature.getInstance(algorithm);
    signer.initSign(priv);
    signer.update(messageBytes);
    byte[] signature = signer.sign();
    verifier.initVerify(pub);
    verifier.update(messageBytes);
    assertTrue(verifier.verify(signature));
    // Extract some parameters.
    byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
    DSAParams params = priv.getParams();
    // Print keys and signature, so that it can be used to generate new test vectors.
    System.out.println("Message:" + message);
    System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
    System.out.println("Params:");
    System.out.println("p:" + params.getP().toString());
    System.out.println("q:" + params.getQ().toString());
    System.out.println("g:" + params.getG().toString());
    System.out.println("Private key:");
    System.out.println("X:" + priv.getX().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
    System.out.println("Public key:");
    System.out.println("Y:" + pub.getY().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
    System.out.println("Signature:" + TestUtil.bytesToHex(signature));
    System.out.println("r:" + extractR(signature).toString());
    System.out.println("s:" + extractS(signature).toString());
}
Also used : KeyPair(java.security.KeyPair) Signature(java.security.Signature) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey) SlowTest(com.google.security.wycheproof.WycheproofRunner.SlowTest)

Example 20 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project robovm by robovm.

the class DSAKeyFactoryImpl method engineGetKeySpec.

/**
     * This method returns a specification for the supplied key.
     *
     * The specification will be returned in the form of an object of the type
     * specified by keySpec.
     *
     * @param key -
     *            either DSAPrivateKey or DSAPublicKey
     * @param keySpec -
     *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
     *
     * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
     *
     * @throws InvalidKeySpecException
     *             if "keySpec" is not a specification for DSAPublicKey or
     *             DSAPrivateKey
     */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    BigInteger p, q, g, x, y;
    if (key != null) {
        if (keySpec == null) {
            throw new NullPointerException("keySpec == null");
        }
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            if (keySpec.equals(DSAPrivateKeySpec.class)) {
                x = privateKey.getX();
                DSAParams params = privateKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPrivateKeySpec(x, p, q, g));
            }
            if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
                return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            if (keySpec.equals(DSAPublicKeySpec.class)) {
                y = publicKey.getY();
                DSAParams params = publicKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPublicKeySpec(y, p, q, g));
            }
            if (keySpec.equals(X509EncodedKeySpec.class)) {
                return (T) (new X509EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
        }
    }
    throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)19 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)11 PublicKey (java.security.PublicKey)10 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)10 BigInteger (java.math.BigInteger)9 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 InvalidKeyException (java.security.InvalidKeyException)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)6 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)6 IOException (java.io.IOException)5 GeneralSecurityException (java.security.GeneralSecurityException)5 KeyPairGenerator (java.security.KeyPairGenerator)5 X509Certificate (java.security.cert.X509Certificate)5 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 DSAParameterSpec (java.security.spec.DSAParameterSpec)4 CertificateException (java.security.cert.CertificateException)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3