Discussion:
Loading self signed certificate (and private key) with Bouncycastle
Pierrick Grasland
2013-08-08 13:24:04 UTC
Permalink
Hello,

When trying to load a self signed certificate / private key (generated with
Openssl), I'm encountering an exception :

Caused by: java.lang.IllegalArgumentException: failed to construct sequence
from byte[]: DER length more than 4 bytes: 26
at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source)
~[na:na]
at org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(Unknown
Source) ~[na:na]
at
com.nexcom.wsrtc.gateway.crypto.CertificateManager.loadPrivateKeyFromFile(CertificateManager.java:91)
~[na:na]
at
com.nexcom.wsrtc.gateway.crypto.CertificateManager.initialize(CertificateManager.java:62)
~[na:na]


The certificate and key are generated using this openssl command :

openssl req -new -x509 -keyout key.pem -out cert.pem -days 1095

And I'm trying to load them using these methods :

private Certificate loadCertificateFromFile() throws IOException {
__logger .debug("Loading certificate from file : {}",
certificateLocation);
InputStream stream = new FileInputStream(certificateLocation);
PemReader reader = new PemReader(new InputStreamReader(stream));
PemObject pem = reader.readPemObject();
reader.close();
if (pem.getType().endsWith("CERTIFICATE"))
{
return Certificate.getInstance(pem.getContent());
}
throw new IllegalArgumentException("'resource' doesn't specify a
valid certificate");
}

private AsymmetricKeyParameter loadPrivateKeyFromFile() throws
IOException
{

__logger.debug("Loading private key from file : {}",
privateKeyLocation);

InputStream stream = new FileInputStream(privateKeyLocation);
PemReader reader = new PemReader(new InputStreamReader(stream));
PemObject pem = reader.readPemObject();
reader.close();

if (pem.getType().endsWith("RSA PRIVATE KEY"))
{
RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent());
return new RSAPrivateCrtKeyParameters(rsa.getModulus(),
rsa.getPublicExponent(),
rsa.getPrivateExponent(), rsa.getPrime1(),
rsa.getPrime2(), rsa.getExponent1(),
rsa.getExponent2(), rsa.getCoefficient());
}
if (pem.getType().endsWith("PRIVATE KEY"))
{
return PrivateKeyFactory.createKey(pem.getContent());
}
throw new IllegalArgumentException("'resource' doesn't specify a
valid private key");
}
From my traces, it seems that only the method loadPrivateKeyFromFile failed.
Did I miss some parameters in my certificate / key generation ?
--
Pierrick Grasland
Matthew Hall
2013-08-08 18:06:07 UTC
Permalink
Post by Pierrick Grasland
return Certificate.getInstance(pem.getContent());
return PrivateKeyFactory.createKey(pem.getContent());
Hi,

Make sure you check the BC Unit Tests that come with the BC source code:

https://github.com/bcgit/bc-java

They will explain the right way of doing these kinds of tasks.

I don't think these are the correct factory methods to call... normally there
are some simpler ways of getting the data from PEM, you can use the PEM stream
object to pull items out of the PEM one-by-one, instead of having to hard-code
calls to other factories.

Matthew.
Pierrick Grasland
2013-08-08 18:14:13 UTC
Permalink
Hello,

Thanks, I found the solution later in the afternoon. My key was encrypted.
Regenerating with -nodes parameter allow me to use it.
Post by Matthew Hall
Post by Pierrick Grasland
return Certificate.getInstance(pem.getContent());
return PrivateKeyFactory.createKey(pem.getContent());
Hi,
https://github.com/bcgit/bc-java
They will explain the right way of doing these kinds of tasks.
I don't think these are the correct factory methods to call... normally there
are some simpler ways of getting the data from PEM, you can use the PEM stream
object to pull items out of the PEM one-by-one, instead of having to hard-code
calls to other factories.
Matthew.
--
Pierrick Grasland
Loading...