PrOgrAMmer PrOgrAMmer - 1 month ago 15
Java Question

BouncyCastle: Extract public key from Certificate causes NullPointerException

import com.security.crypto.Configuration.Properties;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x509.Certificate;
import org.bouncycastle.jce.provider.X509CertificateObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws Exception {

String df="MIID/TCCAuWgAwIBAgIBATANBgkqhkiG9w0BAQUFADBjMRQwEgYDVQQDEwtleGFtcGxlLm9yZzELMAkGA1UEBhMCR1IxDzANBgNVBAgTBkF0aGVuczEPMA0GA1UEBxMGQXRoZW5zMQ0wCwYDVQQKEwRUZXN0MQ0wCwYDVQQLEwRUZXN0MB4XDTE2MDkxODE5NDkzMFoXDTE3MDkxODE5NDkzMFowYzEUMBIGA1UEAxMLZXhhbXBsZS5vcmcxCzAJBgNVBAYTAkdSMQ8wDQYDVQQIEwZBdGhlbnMxDzANBgNVBAcTBkF0aGVuczENMAsGA1UEChMEVGVzdDENMAsGA1UECxMEVGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIFsgF86ymuWPEMCZ5Jlj3pLeq2egQ7aiXD3pwhJQemwoC7wSUoViTt3vnNxVVd7HLQZE4igtCI/ZHJhCf5LKoJqxsAfNjq+KK7/9phznX9MroMpluF8LCKDH6otF7rqknLvI72M+oPYkxTYJv02Wb4+lGWn4aYpUh6XmQPDOW6Yoz7RfrY6t0skJMVGqQhg37n/xHrqcc8KmEy0SBpbsRPi6q8Vwsdbd0MiqWt9NdzfBz6kw8WjMSDAf+H4Nwhydgh4sXxSo2zQHobY+QrEr+RLJI++YsqgBeXYeAWbdKb9C6S16VIe8PNi4EqFYtoOzZcMWDl+1xACiFQemor9FRkCAwEAAaOBuzCBuDAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIC9DA7BgNVHSUENDAyBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDBAYIKwYBBQUHAwgwEQYJYIZIAYb4QgEBBAQDAgD3MCwGA1UdEQQlMCOGG2h0dHA6Ly9leGFtcGxlLm9yZy93ZWJpZCNtZYcEfwAAATAdBgNVHQ4EFgQUwOcGIZDvBxPH5FJ2ZKo6YoK1oIswDQYJKoZIhvcNAQEFBQADggEBAARO4d3/JNRmTg33HQ2OigiQ3yh/RCd2u9tF39EmU1tpg/nGMnVql+c+M7TnX51vqGJ2oX5nCY/vM2LgUGCqRcuZLRk2u0SzlaI1QlEPLLnsoCes5rU1tVm8xSUOVYp809F8Eiih0A+NZWbPuT83UgiJVtYOvvEWsnlpErkeP4KblS3z532651pTC/RzKO1saRPx4kBI7QAGogEtjbhvMX8099g0mBHvXcVxrIMTUY4sKntMlYQ4vQ4OxBTEXhKwEW1WJh8orXl3E0EkTFhbjkFE9gbqsS3h4ridMcmahoeIwnwckaU5zxgJ2t3ih35FzZXBmfv3qRgFG81Gdi+NH1U=";
X509Certificate cert = loadCertificate(df);
System.out.println(cert.getSigAlgName());//SHA1withRSA
PublicKey key=cert.getPublicKey();
System.out.println(key.getAlgorithm());//java.lang.NullPointerException
}

public static X509Certificate loadCertificate(String asn1)throws IOException, GeneralSecurityException
{
byte []data=Base64.decodeBase64(asn1.getBytes(Properties.CHAR_ENCODING));
ByteArrayInputStream inStream = new ByteArrayInputStream(data);
ASN1InputStream derin = new ASN1InputStream(inStream);
ASN1Primitive certInfo = derin.readObject();
ASN1Sequence seq = ASN1Sequence.getInstance(certInfo);
return new X509CertificateObject(Certificate.getInstance(seq));
}
}


i am using BouncyCastle to generate a X509Certificate from DER-encoded. Everything looks good the certificate loads with succes. But when i try get the public key from certificate i got NullpointerException when i tried to use the key just like the above example .What happens here?

SHA1withRSA
Exception in thread "main" java.lang.NullPointerException
at com.security.crypto.App.main(App.java:28)

Answer

For reasons that are not entirely clear to me you must add the Bouncycastle JCE provider. So, at the start of main, you need

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Comments