Search in sources :

Example 71 with NoSuchProviderException

use of java.security.NoSuchProviderException in project robovm by robovm.

the class myCertStore method testCertStore16.

/**
     * Test for <code>getType()</code> method
     */
public void testCertStore16() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!initParams()) {
        return;
    }
    CertStore certS;
    for (int i = 0; i < dValid.length; i++) {
        certS = CertStore.getInstance(dValid[i], dParams);
        assertEquals("Incorrect type", certS.getType(), dValid[i]);
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderCol);
            assertEquals("Incorrect type", certS.getType(), dValid[i]);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderColName);
            assertEquals("Incorrect type", certS.getType(), dValid[i]);
        } catch (NoSuchProviderException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
    }
}
Also used : NoSuchProviderException(java.security.NoSuchProviderException) CertStore(java.security.cert.CertStore)

Example 72 with NoSuchProviderException

use of java.security.NoSuchProviderException in project robovm by robovm.

the class myCertStore method testCertStore17.

/**
     * Test for <code>getProvider()</code> method
     */
public void testCertStore17() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!initParams()) {
        return;
    }
    CertStore certS;
    for (int i = 0; i < dValid.length; i++) {
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderCol);
            assertEquals("Incorrect provider", certS.getProvider(), defaultProviderCol);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderColName);
            assertEquals("Incorrect provider", certS.getProvider(), defaultProviderCol);
        } catch (NoSuchProviderException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
    }
}
Also used : NoSuchProviderException(java.security.NoSuchProviderException) CertStore(java.security.cert.CertStore)

Example 73 with NoSuchProviderException

use of java.security.NoSuchProviderException in project robovm by robovm.

the class myCertStore method testCertStore18.

/**
     * Test for <code>getCertStoreParameters()</code> method
     */
public void testCertStore18() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!initParams()) {
        return;
    }
    CertStore certS;
    for (int i = 0; i < dValid.length; i++) {
        certS = CertStore.getInstance(dValid[i], dParams);
        assertEquals("Incorrect parameters", ((CollectionCertStoreParameters) certS.getCertStoreParameters()).getCollection(), ((CollectionCertStoreParameters) dParams).getCollection());
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderCol);
            assertEquals("Incorrect parameters", ((CollectionCertStoreParameters) certS.getCertStoreParameters()).getCollection(), ((CollectionCertStoreParameters) dParams).getCollection());
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
        try {
            certS = CertStore.getInstance(dValid[i], dParams, defaultProviderColName);
            assertEquals("Incorrect parameters", ((CollectionCertStoreParameters) certS.getCertStoreParameters()).getCollection(), ((CollectionCertStoreParameters) dParams).getCollection());
        } catch (NoSuchProviderException e) {
            fail("Unexpected IllegalArgumentException " + e.getMessage());
        }
    }
}
Also used : NoSuchProviderException(java.security.NoSuchProviderException) CertStore(java.security.cert.CertStore)

Example 74 with NoSuchProviderException

use of java.security.NoSuchProviderException in project Conversations by siacs.

the class XmppAxolotlMessage method decrypt.

public XmppAxolotlPlaintextMessage decrypt(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException {
    XmppAxolotlPlaintextMessage plaintextMessage = null;
    byte[] key = unpackKey(session, sourceDeviceId);
    if (key != null) {
        try {
            if (key.length >= 32) {
                int authtaglength = key.length - 16;
                Log.d(Config.LOGTAG, "found auth tag as part of omemo key");
                byte[] newCipherText = new byte[key.length - 16 + ciphertext.length];
                byte[] newKey = new byte[16];
                System.arraycopy(ciphertext, 0, newCipherText, 0, ciphertext.length);
                System.arraycopy(key, 16, newCipherText, ciphertext.length, authtaglength);
                System.arraycopy(key, 0, newKey, 0, newKey.length);
                ciphertext = newCipherText;
                key = newKey;
            }
            Cipher cipher = Cipher.getInstance(CIPHERMODE, PROVIDER);
            SecretKeySpec keySpec = new SecretKeySpec(key, KEYTYPE);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            String plaintext = new String(cipher.doFinal(ciphertext));
            plaintextMessage = new XmppAxolotlPlaintextMessage(Config.OMEMO_PADDING ? plaintext.trim() : plaintext, session.getFingerprint());
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | NoSuchProviderException e) {
            throw new CryptoFailedException(e);
        }
    }
    return plaintextMessage;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException)

Example 75 with NoSuchProviderException

use of java.security.NoSuchProviderException in project nhin-d by DirectProject.

the class CreateSignedPKCS7 method create.

/**
	 * Creates a pcks7 file from the certificate and key files.
	 * @param anchorDir :The Directory where the .der files are present.
	 * @param createFile : The .p7m File name.
	 * @param metaFile :One XML file as per required specification of TrustBundle metadata schema. 
	 * @param p12certiFile : The .p12 file.
	 * @param passkey :Pass Key for the .p12 file if present or else it should be blank.
	 * @param destDir : The Destination folder where the output .p7m files will be created.
	 * 	 * @return File : Returns the created SignedBundle as a .p7m file.
	 */
public File create(String anchorDir, File createFile, File metaFile, boolean metaExists, File p12certiFile, String passKey) {
    File pkcs7File = null;
    FileOutputStream outStr = null;
    InputStream inStr = null;
    try {
        // Create the unsigned Trust Bundle
        CreateUnSignedPKCS7 unSignedPKCS7 = new CreateUnSignedPKCS7();
        File unsigned = unSignedPKCS7.create(anchorDir, createFile, metaFile, metaExists);
        byte[] unsignedByte = loadFileData(unsigned);
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        CMSSignedData unsignedData = new CMSSignedData(unsignedByte);
        // Create the certificate array
        KeyStore ks = java.security.KeyStore.getInstance("PKCS12", "BC");
        ks.load(new FileInputStream(p12certiFile), defaultPwd.toCharArray());
        ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
        Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            String alias = (String) aliases.nextElement();
            if (ks.getKey(alias, defaultPwd.toCharArray()) != null && ks.getKey(alias, defaultPwd.toCharArray()) instanceof PrivateKey) {
                ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build((PrivateKey) ks.getKey(alias, defaultPwd.toCharArray()));
                X509CertificateHolder holder = new X509CertificateHolder(ks.getCertificate(alias).getEncoded());
                certList.add((X509Certificate) ks.getCertificate(alias));
                gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()).build(sha1Signer, holder));
            }
        }
        Store certStores = new JcaCertStore(certList);
        gen.addCertificates(certStores);
        CMSSignedData sigData = gen.generate(new CMSProcessableByteArray(unsignedData.getEncoded()), true);
        //SignedData encapInfo = SignedData.getInstance(sigData.getContentInfo().getContent());
        pkcs7File = getPKCS7OutFile(createFile);
        outStr = new FileOutputStream(pkcs7File);
        outStr.write(sigData.getEncoded());
    } catch (CMSException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (IOException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (KeyStoreException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (NoSuchProviderException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (NoSuchAlgorithmException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (CertificateException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (UnrecoverableKeyException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (OperatorCreationException e) {
        // e.printStackTrace(System.err);
        return null;
    } catch (Exception e) {
        // e.printStackTrace(System.err);
        return null;
    } finally {
        IOUtils.closeQuietly(outStr);
        IOUtils.closeQuietly(inStr);
    }
    return pkcs7File;
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) PrivateKey(java.security.PrivateKey) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ArrayList(java.util.ArrayList) Store(org.bouncycastle.util.Store) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) KeyStore(java.security.KeyStore) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ContentSigner(org.bouncycastle.operator.ContentSigner) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CMSSignedData(org.bouncycastle.cms.CMSSignedData) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) CMSException(org.bouncycastle.cms.CMSException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) JcaSignerInfoGeneratorBuilder(org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder) FileOutputStream(java.io.FileOutputStream) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) JcaDigestCalculatorProviderBuilder(org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) NoSuchProviderException(java.security.NoSuchProviderException) File(java.io.File) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)102 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)75 InvalidKeyException (java.security.InvalidKeyException)33 IOException (java.io.IOException)31 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 CertificateException (java.security.cert.CertificateException)19 SignatureException (java.security.SignatureException)15 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 KeyStoreException (java.security.KeyStoreException)12 X509Certificate (java.security.cert.X509Certificate)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 KeyFactory (java.security.KeyFactory)8 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8