Skip to content

Commit

Permalink
make BaseKeyStoreFactory more universal
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Apr 14, 2016
1 parent 55f74b9 commit 5628b82
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/main/java/org/acra/security/BaseKeyStoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,45 @@
* @author F43nd1r
* @since 4.8.3
*/
@SuppressWarnings({"WeakerAccess","unused"})
@SuppressWarnings({"WeakerAccess", "unused"})
public abstract class BaseKeyStoreFactory implements KeyStoreFactory {

public enum Type {
CERTIFICATE,
KEYSTORE
}

private final String certificateType;

/**
* creates a new KeyStoreFactory for the default certificate type {@link ACRAConstants#DEFAULT_CERTIFICATE_TYPE}
*/
public BaseKeyStoreFactory(){
public BaseKeyStoreFactory() {
this(ACRAConstants.DEFAULT_CERTIFICATE_TYPE);
}

/**
* creates a new KeyStoreFactory with the specified certificate type
*
* @param certificateType the certificate type
*/
public BaseKeyStoreFactory(String certificateType) {
this.certificateType = certificateType;
}

abstract public InputStream getInputStream(@NonNull Context context);
abstract protected InputStream getInputStream(@NonNull Context context);

protected String getKeyStoreType() {
return KeyStore.getDefaultType();
}

protected Type getStreamType() {
return Type.CERTIFICATE;
}

protected char[] getPassword() {
return null;
}

@Override
@Nullable
Expand All @@ -70,20 +88,26 @@ public final KeyStore create(@NonNull Context context) {
if (inputStream != null) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance(certificateType);
Certificate certificate = certificateFactory.generateCertificate(bufferedInputStream);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", certificate);
KeyStore keyStore = KeyStore.getInstance(getKeyStoreType());
switch (getStreamType()) {
case CERTIFICATE:
CertificateFactory certificateFactory = CertificateFactory.getInstance(certificateType);
Certificate certificate = certificateFactory.generateCertificate(bufferedInputStream);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", certificate);
break;
case KEYSTORE:
keyStore.load(bufferedInputStream, getPassword());
}
return keyStore;
} catch (CertificateException e) {
ACRA.log.e(LOG_TAG, "Could not load certificate", e);
} catch (KeyStoreException e) {
ACRA.log.e(LOG_TAG, "Could not load certificate", e);
ACRA.log.e(LOG_TAG, "Could not load keystore", e);
} catch (NoSuchAlgorithmException e) {
ACRA.log.e(LOG_TAG, "Could not load certificate", e);
ACRA.log.e(LOG_TAG, "Could not load keystore", e);
} catch (IOException e) {
ACRA.log.e(LOG_TAG, "Could not load certificate", e);
ACRA.log.e(LOG_TAG, "Could not load keystore", e);
} finally {
IOUtils.safeClose(bufferedInputStream);
}
Expand Down

0 comments on commit 5628b82

Please sign in to comment.