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);
}
}
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);
}
}
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);
}
}
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();
}
}
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");
}
Aggregations