Pierrick Grasland
2013-08-08 13:24:04 UTC
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");
}
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
Pierrick Grasland