After a couple of days struggling with this issue, I discovered, after loading a PEM certificate file with a chain of certs, that pemParser.readObject() would finally return a useable object instead of null. My initial attempts were with a number of single cert files, that could all be read by an online parser but would return null when read by pemParser.readObject(). After this discovery I duplicated the cert in one of the single cert files, so that there would be a chain of two, and pemParser.readObject() now reads it. I initially had dependencies for version 1.78 in my build.gradle(app) file but updated them to 1.84, which I found still has the problem.
dependencies{
implementation 'org.bouncycastle:bcprov-jdk15to18:1.84' // Latest stable version
implementation 'org.bouncycastle:bcpkix-jdk15to18:1.84' // Latest stable version
}
My project is compiled with Android Studio Otter running on Windows 11. I added these lines to my read function ensure that the default Android version of BC was not causing conflicts, but this did not resolve the problem:
Security.removeProvider("BC"); // Remove old Android BC
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
The following is the code to use an InputStreamReader for an InputStream created for a cert file read from the assets folder of the project:
// === Option 1: Read PEM certificate ===
try(InputStreamReader inputStreamReader = new InputStreamReader(certFile(ctx), "UTF-8");
PEMParser pemParser = new PEMParser(inputStreamReader)){
String encoding = inputStreamReader.getEncoding();
int certStr = inputStreamReader.read();
Object object;
object = pemParser.readObject();
if (object instanceof X509CertificateHolder) {
X509CertificateHolder certHolder = (X509CertificateHolder) object;
X500Name subject = certHolder.getSubject();
X500Name issuer = certHolder.getIssuer();
Date expired = certHolder.getNotAfter();
Date issued = certHolder.getNotBefore();
System.out.println("PEM Certificate Issued: " + issued);
System.out.println("PEM Certificate Expires: " + expired);
System.out.println("PEM Certificate Subject: " + subject);
System.out.println("PEM Certificate Issuer: " + issuer);
}
}
After a couple of days struggling with this issue, I discovered, after loading a PEM certificate file with a chain of certs, that pemParser.readObject() would finally return a useable object instead of null. My initial attempts were with a number of single cert files, that could all be read by an online parser but would return null when read by pemParser.readObject(). After this discovery I duplicated the cert in one of the single cert files, so that there would be a chain of two, and pemParser.readObject() now reads it. I initially had dependencies for version 1.78 in my build.gradle(app) file but updated them to 1.84, which I found still has the problem.
dependencies{
implementation 'org.bouncycastle:bcprov-jdk15to18:1.84' // Latest stable version
implementation 'org.bouncycastle:bcpkix-jdk15to18:1.84' // Latest stable version
}
My project is compiled with Android Studio Otter running on Windows 11. I added these lines to my read function ensure that the default Android version of BC was not causing conflicts, but this did not resolve the problem:
The following is the code to use an InputStreamReader for an InputStream created for a cert file read from the assets folder of the project: