Search in sources :

Example 26 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

the class BasicChecker method makeInheritedParamsKey.

/**
 * Internal method to create a new key with inherited key parameters.
 *
 * @param keyValueKey key from which to obtain key value
 * @param keyParamsKey key from which to obtain key parameters
 * @return new public key having value and parameters
 * @throws CertPathValidatorException if keys are not appropriate types
 * for this operation
 */
static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, PublicKey keyParamsKey) throws CertPathValidatorException {
    if (!(keyValueKey instanceof DSAPublicKey) || !(keyParamsKey instanceof DSAPublicKey))
        throw new CertPathValidatorException("Input key is not " + "appropriate type for " + "inheriting parameters");
    DSAParams params = ((DSAPublicKey) keyParamsKey).getParams();
    if (params == null)
        throw new CertPathValidatorException("Key parameters missing");
    try {
        BigInteger y = ((DSAPublicKey) keyValueKey).getY();
        KeyFactory kf = KeyFactory.getInstance("DSA");
        DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
        return kf.generatePublic(ks);
    } catch (GeneralSecurityException e) {
        throw new CertPathValidatorException("Unable to generate key with" + " inherited parameters: " + e.getMessage(), e);
    }
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) KeyFactory(java.security.KeyFactory) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 27 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

the class DSAPublicKeySpecTest method testDSAPublicKeySpec.

/**
 * Test for <code>DSAPublicKeySpec</code> ctor
 */
public final void testDSAPublicKeySpec() {
    KeySpec ks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertTrue(ks instanceof DSAPublicKeySpec);
}
Also used : DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 28 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

the class DSAPublicKeySpecTest method testGetQ.

/**
 * Test for <code>getQ</code> method
 */
public final void testGetQ() {
    DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertEquals(3, dpks.getQ().intValue());
}
Also used : BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 29 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project karaf by apache.

the class PublickeyLoginModule method equals.

public static boolean equals(PublicKey key, String storedKey) throws FailedLoginException {
    try {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(storedKey)));
        String identifier = readString(dis);
        if (key instanceof DSAPublicKey) {
            if (!"ssh-dss".equals(identifier)) {
                return false;
            }
            BigInteger p = readBigInteger(dis);
            BigInteger q = readBigInteger(dis);
            BigInteger g = readBigInteger(dis);
            BigInteger y = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
            KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof RSAKey) {
            if (!"ssh-rsa".equals(identifier)) {
                return false;
            }
            BigInteger exponent = readBigInteger(dis);
            BigInteger modulus = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof ECPublicKey) {
            String ecIdentifier = readString(dis);
            if (!identifier.equals("ecdsa-sha2-" + ecIdentifier) || !nistSecMap.containsKey(ecIdentifier)) {
                return false;
            }
            // Overall size of the x + y coordinates. We only support uncompressed points here, so
            // to read x + y we ignore the "04" byte using (size - 1) / 2
            int size = dis.readInt();
            byte[] bytes = new byte[(size - 1) / 2];
            dis.skipBytes(1);
            dis.read(bytes, 0, bytes.length);
            BigInteger x = new BigInteger(bytes);
            dis.read(bytes, 0, bytes.length);
            BigInteger y = new BigInteger(bytes);
            KeyFactory keyFactory = KeyFactory.getInstance("EC");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
            parameters.init(new ECGenParameterSpec(nistSecMap.get(ecIdentifier)));
            ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
            ECPoint pubPoint = new ECPoint(x, y);
            KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
            PublicKey generatedPublicKey = keyFactory.generatePublic(keySpec);
            return key.equals(generatedPublicKey);
        } else {
            throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
        }
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
        throw new FailedLoginException("Unable to check public key");
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) KeyFactory(java.security.KeyFactory) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint) DSAPublicKey(java.security.interfaces.DSAPublicKey) FailedLoginException(javax.security.auth.login.FailedLoginException) ECPublicKey(java.security.interfaces.ECPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 30 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec 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

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)63 BigInteger (java.math.BigInteger)45 KeyFactory (java.security.KeyFactory)37 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)23 PublicKey (java.security.PublicKey)22 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)19 KeySpec (java.security.spec.KeySpec)19 DSAParams (java.security.interfaces.DSAParams)17 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 PrivateKey (java.security.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)9 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)9 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 IOException (java.io.IOException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 InvalidKeyException (java.security.InvalidKeyException)5 KeyPair (java.security.KeyPair)5