Search in sources :

Example 56 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project core by jcryptool.

the class ImportExportManager method exportKeyPair.

public void exportKeyPair(IPath path, PrivateKey key, Certificate[] chain, char[] password) {
    PFX pfx;
    X509Certificate[] x509Chain = convert(chain);
    try {
        if (x509Chain.length > 1) {
            X509Certificate[] shortChain = new X509Certificate[x509Chain.length - 1];
            for (int i = 1; i < chain.length; i++) {
                shortChain[i - 1] = x509Chain[i];
            }
            pfx = new PFX(key, x509Chain[0], shortChain, password, null, null);
        } else {
            pfx = new PFX(key, x509Chain[0], null, password, null, null);
        }
        IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
        OutputStream os = new BufferedOutputStream(fileStore.openOutputStream(EFS.APPEND, null));
        DEREncoder encoder = new DEREncoder(os);
        pfx.encode(encoder);
        encoder.close();
        os.close();
    } catch (CertificateEncodingException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CertificateEncodingException while creating a PFX", e, true);
    } catch (GeneralSecurityException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "GeneralSecurityException while creating a PFX", e, true);
    } catch (ASN1Exception e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "ASN1Exception while creating a PFX", e, true);
    } catch (IOException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "IOException while creating a PFX", e, true);
    } catch (CoreException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CoreException while creating a PFX", e, true);
    }
}
Also used : PFX(codec.pkcs12.PFX) ASN1Exception(codec.asn1.ASN1Exception) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) CoreException(org.eclipse.core.runtime.CoreException) DEREncoder(codec.asn1.DEREncoder) IFileStore(org.eclipse.core.filesystem.IFileStore) BufferedOutputStream(java.io.BufferedOutputStream)

Example 57 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project core by jcryptool.

the class ImportExportManager method exportCertificate.

public void exportCertificate(IPath path, Certificate publicKey) {
    // $NON-NLS-1$
    LogUtil.logInfo("Exporting a certficate to " + path.toString());
    // $NON-NLS-1$
    LogUtil.logInfo("Certificate:" + publicKey.getType());
    BufferedOutputStream bos;
    try {
        IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
        bos = new BufferedOutputStream(fileStore.openOutputStream(EFS.APPEND, null));
        bos.write(publicKey.getEncoded());
        bos.close();
    } catch (CoreException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CoreException while accessing a file store", e, true);
    } catch (CertificateEncodingException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CertificateEncodingException while accessing a certificate", e, true);
    } catch (IOException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "IOException while initializing the output stream", e, false);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IFileStore(org.eclipse.core.filesystem.IFileStore) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 58 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project pdfbox by apache.

the class AddValidationInformation method addAllCertsToCertArray.

/**
 * Adds all certs to the certs-array. Make sure, all certificates are inside the certificateStore of
 * certInformationHelper
 *
 * @throws IOException
 */
private void addAllCertsToCertArray() throws IOException {
    try {
        for (X509Certificate cert : certInformationHelper.getCertificateStore().values()) {
            COSStream stream = writeDataToStream(cert.getEncoded());
            certs.add(stream);
        }
    } catch (CertificateEncodingException e) {
        throw new IOException(e);
    }
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 59 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project fdroidclient by f-droid.

the class CacheSwapAppsService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
    if (intent == null || !ACTION_PARSE_APP.equals(intent.getAction())) {
        Utils.debugLog(TAG, "received bad Intent: " + intent);
        return;
    }
    try {
        PackageManager pm = getPackageManager();
        String packageName = intent.getData().getSchemeSpecificPart();
        App app = new App(this, pm, packageName);
        SwapService.putAppInCache(packageName, app);
    } catch (CertificateEncodingException | IOException | PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : App(org.fdroid.fdroid.data.App) FDroidApp(org.fdroid.fdroid.FDroidApp) PackageManager(android.content.pm.PackageManager) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException)

Example 60 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project fdroidclient by f-droid.

the class App method initInstalledApk.

private void initInstalledApk(Context context, Apk apk, PackageInfo packageInfo, SanitizedFile apkFile) throws IOException, CertificateEncodingException {
    apk.compatible = true;
    apk.versionName = packageInfo.versionName;
    apk.versionCode = packageInfo.versionCode;
    apk.added = this.added;
    int[] minTargetMax = getMinTargetMaxSdkVersions(context, packageName);
    apk.minSdkVersion = minTargetMax[0];
    apk.targetSdkVersion = minTargetMax[1];
    apk.maxSdkVersion = minTargetMax[2];
    apk.packageName = this.packageName;
    apk.requestedPermissions = packageInfo.requestedPermissions;
    apk.apkName = apk.packageName + "_" + apk.versionCode + ".apk";
    initInstalledObbFiles(apk);
    final FeatureInfo[] features = packageInfo.reqFeatures;
    if (features != null && features.length > 0) {
        apk.features = new String[features.length];
        for (int i = 0; i < features.length; i++) {
            apk.features[i] = features[i].name;
        }
    }
    if (!apkFile.canRead()) {
        return;
    }
    apk.installedFile = apkFile;
    JarFile apkJar = new JarFile(apkFile);
    HashSet<String> abis = new HashSet<>(3);
    Pattern pattern = Pattern.compile("^lib/([a-z0-9-]+)/.*");
    for (Enumeration<JarEntry> jarEntries = apkJar.entries(); jarEntries.hasMoreElements(); ) {
        JarEntry jarEntry = jarEntries.nextElement();
        Matcher matcher = pattern.matcher(jarEntry.getName());
        if (matcher.matches()) {
            abis.add(matcher.group(1));
        }
    }
    apk.nativecode = abis.toArray(new String[abis.size()]);
    final JarEntry aSignedEntry = (JarEntry) apkJar.getEntry("AndroidManifest.xml");
    if (aSignedEntry == null) {
        apkJar.close();
        throw new CertificateEncodingException("null signed entry!");
    }
    byte[] rawCertBytes;
    // details, check out https://gitlab.com/fdroid/fdroidclient/issues/111.
    try {
        FDroidApp.disableSpongyCastleOnLollipop();
        final InputStream tmpIn = apkJar.getInputStream(aSignedEntry);
        byte[] buff = new byte[2048];
        // noinspection StatementWithEmptyBody
        while (tmpIn.read(buff, 0, buff.length) != -1) {
        /*
                 * NOP - apparently have to READ from the JarEntry before you can
                 * call getCerficates() and have it return != null. Yay Java.
                 */
        }
        tmpIn.close();
        if (aSignedEntry.getCertificates() == null || aSignedEntry.getCertificates().length == 0) {
            apkJar.close();
            throw new CertificateEncodingException("No Certificates found!");
        }
        final Certificate signer = aSignedEntry.getCertificates()[0];
        rawCertBytes = signer.getEncoded();
    } finally {
        FDroidApp.enableSpongyCastleOnLollipop();
    }
    apkJar.close();
    /*
         * I don't fully understand the loop used here. I've copied it verbatim
         * from getsig.java bundled with FDroidServer. I *believe* it is taking
         * the raw byte encoding of the certificate & converting it to a byte
         * array of the hex representation of the original certificate byte
         * array. This is then MD5 sum'd. It's a really bad way to be doing this
         * if I'm right... If I'm not right, I really don't know! see lines
         * 67->75 in getsig.java bundled with Fdroidserver
         */
    final byte[] fdroidSig = new byte[rawCertBytes.length * 2];
    for (int j = 0; j < rawCertBytes.length; j++) {
        byte v = rawCertBytes[j];
        int d = (v >> 4) & 0xF;
        fdroidSig[j * 2] = (byte) (d >= 10 ? ('a' + d - 10) : ('0' + d));
        d = v & 0xF;
        fdroidSig[j * 2 + 1] = (byte) (d >= 10 ? ('a' + d - 10) : ('0' + d));
    }
    apk.sig = Utils.hashBytes(fdroidSig, "md5");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) FeatureInfo(android.content.pm.FeatureInfo) CertificateEncodingException(java.security.cert.CertificateEncodingException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Certificate(java.security.cert.Certificate)

Aggregations

CertificateEncodingException (java.security.cert.CertificateEncodingException)210 X509Certificate (java.security.cert.X509Certificate)94 IOException (java.io.IOException)76 Certificate (java.security.cert.Certificate)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)27 KeyStoreException (java.security.KeyStoreException)19 MessageDigest (java.security.MessageDigest)19 ArrayList (java.util.ArrayList)19 X500Name (org.bouncycastle.asn1.x500.X500Name)16 CertificateException (java.security.cert.CertificateException)14 BigInteger (java.math.BigInteger)11 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)10 Bundle (android.os.Bundle)9 PublicKey (java.security.PublicKey)9 Date (java.util.Date)9 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 File (java.io.File)8 PrivateKey (java.security.PrivateKey)8 DEROctetString (org.bouncycastle.asn1.DEROctetString)8