Search in sources :

Example 16 with InvalidKeyException

use of java.security.InvalidKeyException in project platform_frameworks_base by android.

the class AndroidKeyStoreSignatureSpiBase method engineSign.

@Override
protected final byte[] engineSign() throws SignatureException {
    if (mCachedException != null) {
        throw new SignatureException(mCachedException);
    }
    byte[] signature;
    try {
        ensureKeystoreOperationInitialized();
        byte[] additionalEntropy = KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng(appRandom, getAdditionalEntropyAmountForSign());
        signature = mMessageStreamer.doFinal(EmptyArray.BYTE, 0, 0, // no signature provided -- it'll be generated by this invocation
        null, additionalEntropy);
    } catch (InvalidKeyException | KeyStoreException e) {
        throw new SignatureException(e);
    }
    resetWhilePreservingInitState();
    return signature;
}
Also used : SignatureException(java.security.SignatureException) KeyStoreException(android.security.KeyStoreException) InvalidKeyException(java.security.InvalidKeyException)

Example 17 with InvalidKeyException

use of java.security.InvalidKeyException in project wycheproof by google.

the class EcdhTest method testDistinctCurves.

@SuppressWarnings("InsecureCryptoUsage")
public void testDistinctCurves(String algorithm, ECPrivateKey priv, ECPublicKey pub) throws Exception {
    KeyAgreement kaA;
    try {
        kaA = KeyAgreement.getInstance(algorithm);
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Algorithm not supported: " + algorithm);
        return;
    }
    byte[] shared;
    try {
        kaA.init(priv);
        kaA.doPhase(pub, true);
        shared = kaA.generateSecret();
    } catch (InvalidKeyException ex) {
        // This is expected.
        return;
    }
    // Printing some information to determine what might have gone wrong:
    // E.g., if the generated secret is the same as the x-coordinate of the public key
    // then it is likely that the ECDH computation was using a fake group with small order.
    // Such a situation is probably exploitable.
    // This probably is exploitable. If the curve of the private key was used for the ECDH
    // then the generated secret and the x-coordinate of the public key are likely
    // distinct.
    EllipticCurve pubCurve = pub.getParams().getCurve();
    EllipticCurve privCurve = priv.getParams().getCurve();
    ECPoint pubW = pub.getW();
    System.out.println("testDistinctCurves: algorithm=" + algorithm);
    System.out.println("Private key: a=" + privCurve.getA() + " b=" + privCurve.getB() + " p" + EcUtil.getModulus(privCurve));
    System.out.println("        s =" + priv.getS());
    System.out.println("Public key: a=" + pubCurve.getA() + " b=" + pubCurve.getB() + " p" + EcUtil.getModulus(pubCurve));
    System.out.println("        w = (" + pubW.getAffineX() + ", " + pubW.getAffineY() + ")");
    System.out.println("          = (" + pubW.getAffineX().toString(16) + ", " + pubW.getAffineY().toString(16) + ")");
    System.out.println("generated shared secret:" + TestUtil.bytesToHex(shared));
    fail("Generated secret with distinct Curves using " + algorithm);
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) ECPoint(java.security.spec.ECPoint)

Example 18 with InvalidKeyException

use of java.security.InvalidKeyException in project j2objc by google.

the class X509Key method decode.

/**
     * Initialize an X509Key object from an input stream.  The data on that
     * input stream must be encoded using DER, obeying the X.509
     * <code>SubjectPublicKeyInfo</code> format.  That is, the data is a
     * sequence consisting of an algorithm ID and a bit string which holds
     * the key.  (That bit string is often used to encapsulate another DER
     * encoded sequence.)
     *
     * <P>Subclasses should not normally redefine this method; they should
     * instead provide a <code>parseKeyBits</code> method to parse any
     * fields inside the <code>key</code> member.
     *
     * <P>The exception to this rule is that since private keys need not
     * be encoded using the X.509 <code>SubjectPublicKeyInfo</code> format,
     * private keys may override this method, <code>encode</code>, and
     * of course <code>getFormat</code>.
     *
     * @param in an input stream with a DER-encoded X.509
     *          SubjectPublicKeyInfo value
     * @exception InvalidKeyException on parsing errors.
     */
public void decode(InputStream in) throws InvalidKeyException {
    DerValue val;
    try {
        val = new DerValue(in);
        if (val.tag != DerValue.tag_Sequence)
            throw new InvalidKeyException("invalid key format");
        algid = AlgorithmId.parse(val.data.getDerValue());
        setKey(val.data.getUnalignedBitString());
        parseKeyBits();
        if (val.data.available() != 0)
            throw new InvalidKeyException("excess key data");
    } catch (IOException e) {
        // e.printStackTrace ();
        throw new InvalidKeyException("IOException: " + e.getMessage());
    }
}
Also used : InvalidKeyException(java.security.InvalidKeyException)

Example 19 with InvalidKeyException

use of java.security.InvalidKeyException in project j2objc by google.

the class X509Key method getEncodedInternal.

public byte[] getEncodedInternal() throws InvalidKeyException {
    byte[] encoded = encodedKey;
    if (encoded == null) {
        try {
            DerOutputStream out = new DerOutputStream();
            encode(out);
            encoded = out.toByteArray();
        } catch (IOException e) {
            throw new InvalidKeyException("IOException : " + e.getMessage());
        }
        encodedKey = encoded;
    }
    return encoded;
}
Also used : InvalidKeyException(java.security.InvalidKeyException)

Example 20 with InvalidKeyException

use of java.security.InvalidKeyException in project j2objc by google.

the class X509Key method buildX509Key.

/*
     * Factory interface, building the kind of key associated with this
     * specific algorithm ID or else returning this generic base class.
     * See the description above.
     */
static PublicKey buildX509Key(AlgorithmId algid, BitArray key) throws IOException, InvalidKeyException {
    /*
         * Use the algid and key parameters to produce the ASN.1 encoding
         * of the key, which will then be used as the input to the
         * key factory.
         */
    DerOutputStream x509EncodedKeyStream = new DerOutputStream();
    encode(x509EncodedKeyStream, algid, key);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(x509EncodedKeyStream.toByteArray());
    try {
        // Instantiate the key factory of the appropriate algorithm
        KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
        // Generate the public key
        return keyFac.generatePublic(x509KeySpec);
    } catch (NoSuchAlgorithmException e) {
    // Return generic X509Key with opaque key data (see below)
    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException(e.getMessage(), e);
    }
    /*
         * Try again using JDK1.1-style for backwards compatibility.
         */
    String classname = "";
    try {
        Properties props;
        String keytype;
        Provider sunProvider;
        sunProvider = Security.getProvider("SUN");
        if (sunProvider == null)
            throw new InstantiationException();
        classname = sunProvider.getProperty("PublicKey.X.509." + algid.getName());
        if (classname == null) {
            throw new InstantiationException();
        }
        Class keyClass = null;
        try {
            keyClass = Class.forName(classname);
        } catch (ClassNotFoundException e) {
            ClassLoader cl = ClassLoader.getSystemClassLoader();
            if (cl != null) {
                keyClass = cl.loadClass(classname);
            }
        }
        Object inst = null;
        X509Key result;
        if (keyClass != null)
            inst = keyClass.newInstance();
        if (inst instanceof X509Key) {
            result = (X509Key) inst;
            result.algid = algid;
            result.setKey(key);
            result.parseKeyBits();
            return result;
        }
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
        // this should not happen.
        throw new IOException(classname + " [internal error]");
    }
    X509Key result = new X509Key(algid, key);
    return result;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Properties(java.util.Properties) Provider(java.security.Provider) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33